@@ -391,21 +391,19 @@ export interface ParserState { |
391 | 391 | readonly name: string |
392 | 392 | readonly providerMetadataKey: string |
393 | 393 | readonly tools: ToolStream.State<string> |
394 | | - // Call ids that already emitted their terminal tool-call event, so duplicate |
395 | | - // or late item events for the same call stay no-ops. Keyed by `call_id` |
396 | | - // because duplicate events may disagree on whether `item.id` is present. |
| 394 | + // Call ids stay independent of item ids, which may be omitted or reused. |
397 | 395 | readonly completedTools: ReadonlySet<string> |
398 | 396 | readonly hasFunctionCall: boolean |
399 | 397 | readonly lifecycle: Lifecycle.State |
400 | 398 | readonly outputItems: Readonly<Record<number, string>> |
401 | | - readonly messageItems: ReadonlySet<string> |
402 | | - readonly messagePhases: Readonly<Record<string, MessagePhase | null>> |
| 399 | + readonly message: { readonly id: string; readonly phase: MessagePhase | null | undefined } | undefined |
403 | 400 | readonly reasoningItems: Readonly<Record<string, ReasoningStreamItem>> |
404 | 401 | } |
405 | 402 |
|
406 | 403 | type ReasoningSummaryStatus = "active" | "can-conclude" | "concluded" |
407 | 404 |
|
408 | 405 | interface ReasoningStreamItem { |
| 406 | + readonly open: boolean |
409 | 407 | readonly encryptedContent: string | null | undefined |
410 | 408 | // Keyed by the wire protocol's numeric `summary_index`. JS object keys coerce to |
411 | 409 | // strings, but typing the map as `Record<number, ...>` documents intent |
@@ -830,16 +828,16 @@ const TERMINAL_TYPES = new Set(["error", "response.completed", "response.incompl |
830 | 828 | export const terminal = (event: Event) => TERMINAL_TYPES.has(event.type) |
831 | 829 |
|
832 | 830 | const onOutputTextDelta = (state: ParserState, event: Event, id: string): StepResult => { |
833 | | - if (!event.delta || !state.messageItems.has(id)) return [state, NO_EVENTS] |
| 831 | + if (!event.delta || state.message?.id !== id) return [state, NO_EVENTS] |
834 | 832 | const events: LLMEvent[] = [] |
835 | | - const phase = state.messagePhases[id] |
| 833 | + const phase = state.message.phase |
836 | 834 | const metadata = providerMetadata(state, { itemId: id, ...(phase === undefined ? {} : { phase }) }) |
837 | 835 | const lifecycle = Lifecycle.textStart(state.lifecycle, events, id, metadata) |
838 | 836 | return [{ ...state, lifecycle: Lifecycle.textDelta(lifecycle, events, id, event.delta) }, events] |
839 | 837 | } |
840 | 838 |
|
841 | 839 | const onOutputTextDone = (state: ParserState, event: Event, id: string): StepResult => { |
842 | | - if (state.messageItems.has(id)) { |
| 840 | + if (state.message?.id === id) { |
843 | 841 | if (state.lifecycle.text.has(id) || event.text === undefined) return [state, NO_EVENTS] |
844 | 842 | return onOutputTextDelta(state, { ...event, delta: event.text }, id) |
845 | 843 | } |
@@ -852,8 +850,7 @@ export const outputItemID = (state: ParserState, event: Event) => |
852 | 850 |
|
853 | 851 | const startReasoningSummaryPart = (state: ParserState, itemID: string, index: number): StepResult => { |
854 | 852 | const item = state.reasoningItems[itemID] |
855 | | - if (!item || index === 0 || item.summaryParts[index] !== undefined) return [state, NO_EVENTS] |
856 | | - if (Object.values(item.summaryParts).every((status) => status === "concluded")) return [state, NO_EVENTS] |
| 853 | + if (!item?.open || index === 0 || item.summaryParts[index] !== undefined) return [state, NO_EVENTS] |
857 | 854 |
|
858 | 855 | const events: LLMEvent[] = [] |
859 | 856 | const lifecycle = Object.entries(item.summaryParts) |
@@ -893,26 +890,19 @@ const startReasoningSummaryPart = (state: ParserState, itemID: string, index: nu |
893 | 890 |
|
894 | 891 | export const onReasoningDelta = (state: ParserState, event: Event, itemID: string): StepResult => { |
895 | 892 | const item = state.reasoningItems[itemID] |
896 | | - if (!event.delta || !item) return [state, NO_EVENTS] |
| 893 | + if (!event.delta || !item?.open) return [state, NO_EVENTS] |
897 | 894 | const index = event.summary_index ?? 0 |
898 | 895 | if (item.summaryParts[index] === "concluded") return [state, NO_EVENTS] |
899 | | - // An unseen index cannot reopen an item whose parts have all concluded. |
900 | | - if ( |
901 | | - item.summaryParts[index] === undefined && |
902 | | - Object.values(item.summaryParts).every((status) => status === "concluded") |
903 | | - ) |
904 | | - return [state, NO_EVENTS] |
905 | | - const started: StepResult = |
906 | | - item.summaryParts[index] === undefined ? startReasoningSummaryPart(state, itemID, index) : [state, NO_EVENTS] |
907 | | - const current = started[0].reasoningItems[itemID] |
908 | | - if (!current) return started |
909 | | - const events: LLMEvent[] = [...started[1]] |
| 896 | + const [started, emitted] = startReasoningSummaryPart(state, itemID, index) |
| 897 | + const current = started.reasoningItems[itemID] |
| 898 | + if (!current) return [started, emitted] |
| 899 | + const events: LLMEvent[] = [...emitted] |
910 | 900 | return [ |
911 | 901 | { |
912 | | - ...started[0], |
913 | | - lifecycle: Lifecycle.reasoningDelta(started[0].lifecycle, events, `${itemID}:${index}`, event.delta), |
| 902 | + ...started, |
| 903 | + lifecycle: Lifecycle.reasoningDelta(started.lifecycle, events, `${itemID}:${index}`, event.delta), |
914 | 904 | reasoningItems: { |
915 | | - ...started[0].reasoningItems, |
| 905 | + ...started.reasoningItems, |
916 | 906 | [itemID]: { ...current, deltaIndexes: new Set([...current.deltaIndexes, index]) }, |
917 | 907 | }, |
918 | 908 | }, |
@@ -925,7 +915,7 @@ export const onReasoningDelta = (state: ParserState, event: Event, itemID: strin |
925 | 915 | // as a single delta unless that summary index already streamed one. |
926 | 916 | export const onReasoningDone = (state: ParserState, event: Event, itemID: string): StepResult => { |
927 | 917 | const item = state.reasoningItems[itemID] |
928 | | - if (!item || typeof event.text !== "string") return [state, NO_EVENTS] |
| 918 | + if (!item?.open || typeof event.text !== "string") return [state, NO_EVENTS] |
929 | 919 | const index = event.summary_index ?? 0 |
930 | 920 | if (item.deltaIndexes.has(index)) return [state, NO_EVENTS] |
931 | 921 | return onReasoningDelta(state, { ...event, delta: event.text }, itemID) |
@@ -949,28 +939,27 @@ const onOutputItemAdded = (state: ParserState, event: Event): StepResult => { |
949 | 939 | if (item?.type === "message" && item.id !== undefined) { |
950 | 940 | const itemID = item.id |
951 | 941 | const phase = messagePhase(item.phase) |
952 | | - // A new message item is an implicit boundary for every earlier message |
953 | | - // item: text still streaming is ended, and all older items leave the |
954 | | - // tracked set so their late deltas stay no-ops instead of overlapping. |
| 942 | + // A new message closes earlier messages, including ones that never streamed. |
955 | 943 | const events: LLMEvent[] = [] |
956 | 944 | const lifecycle = [...state.lifecycle.text] |
957 | 945 | .filter((id) => id !== itemID) |
958 | 946 | .reduce((lifecycle, id) => { |
959 | | - const openPhase = state.messagePhases[id] |
| 947 | + const openPhase = state.message?.id === id ? state.message.phase : undefined |
960 | 948 | return Lifecycle.textEnd( |
961 | 949 | lifecycle, |
962 | 950 | events, |
963 | 951 | id, |
964 | 952 | providerMetadata(state, { itemId: id, ...(openPhase === undefined ? {} : { phase: openPhase }) }), |
965 | 953 | ) |
966 | 954 | }, state.lifecycle) |
967 | | - const nextPhase = phase === undefined ? state.messagePhases[itemID] : phase |
968 | 955 | return [ |
969 | 956 | { |
970 | 957 | ...state, |
971 | 958 | lifecycle, |
972 | | - messageItems: new Set([itemID]), |
973 | | - messagePhases: nextPhase === undefined ? {} : { [itemID]: nextPhase }, |
| 959 | + message: { |
| 960 | + id: itemID, |
| 961 | + phase: phase === undefined && state.message?.id === itemID ? state.message.phase : phase, |
| 962 | + }, |
974 | 963 | }, |
975 | 964 | events, |
976 | 965 | ] |
@@ -985,6 +974,7 @@ const onOutputItemAdded = (state: ParserState, event: Event): StepResult => { |
985 | 974 | reasoningItems: { |
986 | 975 | ...state.reasoningItems, |
987 | 976 | [item.id]: { |
| 977 | + open: true, |
988 | 978 | encryptedContent: item.encrypted_content, |
989 | 979 | summaryParts: { 0: "active" }, |
990 | 980 | deltaIndexes: new Set(), |
@@ -996,8 +986,6 @@ const onOutputItemAdded = (state: ParserState, event: Event): StepResult => { |
996 | 986 | } |
997 | 987 | if (item?.type !== "function_call" || !item.call_id) return [state, NO_EVENTS] |
998 | 988 | const id = item.id ?? item.call_id |
999 | | - // Pending tools always store the call id, so this also catches duplicates |
1000 | | - // that disagree on whether `item.id` is present. |
1001 | 989 | if (Object.values(state.tools).some((tool) => tool?.id === item.call_id) || state.completedTools.has(item.call_id)) |
1002 | 990 | return [state, NO_EVENTS] |
1003 | 991 | const metadata = item.id !== undefined ? providerMetadata(state, { itemId: item.id }) : undefined |
@@ -1026,7 +1014,7 @@ const onReasoningSummaryPartAdded = (state: ParserState, event: Event): StepResu |
1026 | 1014 | const onReasoningSummaryPartDone = (state: ParserState, event: Event): StepResult => { |
1027 | 1015 | if (event.item_id === undefined || event.summary_index === undefined) return [state, NO_EVENTS] |
1028 | 1016 | const item = state.reasoningItems[event.item_id] |
1029 | | - if (!item) return [state, NO_EVENTS] |
| 1017 | + if (!item?.open) return [state, NO_EVENTS] |
1030 | 1018 | if (item.summaryParts[event.summary_index] !== "active") return [state, NO_EVENTS] |
1031 | 1019 | return [ |
1032 | 1020 | { |
@@ -1083,11 +1071,8 @@ const onOutputItemDone = Effect.fn("OpenResponses.onOutputItemDone")(function* ( |
1083 | 1071 |
|
1084 | 1072 | if (item.type === "message" && item.id !== undefined) { |
1085 | 1073 | const itemPhase = messagePhase(item.phase) |
1086 | | - const phase = itemPhase === undefined ? state.messagePhases[item.id] : itemPhase |
| 1074 | + const phase = itemPhase === undefined && state.message?.id === item.id ? state.message.phase : itemPhase |
1087 | 1075 | const events: LLMEvent[] = [] |
1088 | | - const messageItems = new Set(state.messageItems) |
1089 | | - messageItems.delete(item.id) |
1090 | | - const { [item.id]: _phase, ...messagePhases } = state.messagePhases |
1091 | 1076 | return [ |
1092 | 1077 | { |
1093 | 1078 | ...state, |
@@ -1097,8 +1082,7 @@ const onOutputItemDone = Effect.fn("OpenResponses.onOutputItemDone")(function* ( |
1097 | 1082 | item.id, |
1098 | 1083 | providerMetadata(state, { itemId: item.id, ...(phase === undefined ? {} : { phase }) }), |
1099 | 1084 | ), |
1100 | | - messageItems, |
1101 | | - messagePhases, |
| 1085 | + message: state.message?.id === item.id ? undefined : state.message, |
1102 | 1086 | }, |
1103 | 1087 | events, |
1104 | 1088 | ] satisfies StepResult |
@@ -1157,14 +1141,13 @@ const onOutputItemDone = Effect.fn("OpenResponses.onOutputItemDone")(function* ( |
1157 | 1141 | const metadata = reasoningMetadata(state, item) |
1158 | 1142 | const reasoningItem = state.reasoningItems[item.id] |
1159 | 1143 | if (reasoningItem) { |
| 1144 | + if (!reasoningItem.open) return [state, NO_EVENTS] satisfies StepResult |
1160 | 1145 | const lifecycle = Object.entries(reasoningItem.summaryParts) |
1161 | 1146 | .filter((entry) => entry[1] === "active" || entry[1] === "can-conclude") |
1162 | 1147 | .reduce( |
1163 | 1148 | (lifecycle, entry) => Lifecycle.reasoningEnd(lifecycle, events, `${item.id}:${entry[0]}`, metadata), |
1164 | 1149 | state.lifecycle, |
1165 | 1150 | ) |
1166 | | - // Keep the fully-concluded entry so duplicate or late events for this |
1167 | | - // item remain no-ops instead of reopening lifecycle state. |
1168 | 1151 | return [ |
1169 | 1152 | { |
1170 | 1153 | ...state, |
@@ -1173,10 +1156,8 @@ const onOutputItemDone = Effect.fn("OpenResponses.onOutputItemDone")(function* ( |
1173 | 1156 | ...state.reasoningItems, |
1174 | 1157 | [item.id]: { |
1175 | 1158 | ...reasoningItem, |
| 1159 | + open: false, |
1176 | 1160 | encryptedContent: item.encrypted_content ?? reasoningItem.encryptedContent, |
1177 | | - summaryParts: Object.fromEntries( |
1178 | | - Object.keys(reasoningItem.summaryParts).map((index) => [index, "concluded" as const]), |
1179 | | - ), |
1180 | 1161 | }, |
1181 | 1162 | }, |
1182 | 1163 | }, |
@@ -1194,6 +1175,7 @@ const onOutputItemDone = Effect.fn("OpenResponses.onOutputItemDone")(function* ( |
1194 | 1175 | reasoningItems: { |
1195 | 1176 | ...state.reasoningItems, |
1196 | 1177 | [item.id]: { |
| 1178 | + open: false, |
1197 | 1179 | encryptedContent: item.encrypted_content, |
1198 | 1180 | summaryParts: { 0: "concluded" }, |
1199 | 1181 | deltaIndexes: new Set(), |
@@ -1223,7 +1205,7 @@ const onResponseFinish = Effect.fn("OpenResponses.onResponseFinish")(function* ( |
1223 | 1205 | if ( |
1224 | 1206 | id === undefined || |
1225 | 1207 | ((item.type !== "function_call" || !current.tools[id]) && |
1226 | | - (item.type !== "reasoning" || !current.reasoningItems[id])) |
| 1208 | + (item.type !== "reasoning" || !current.reasoningItems[id]?.open)) |
1227 | 1209 | ) |
1228 | 1210 | return Effect.succeed([current, events] satisfies StepResult) |
1229 | 1211 | return onOutputItemDone(current, { type: "response.output_item.done", item }).pipe( |
@@ -1399,8 +1381,7 @@ export const initial = (request: LLMRequest, extension: Extension = BASE): Parse |
1399 | 1381 | completedTools: new Set<string>(), |
1400 | 1382 | lifecycle: Lifecycle.initial(), |
1401 | 1383 | outputItems: {}, |
1402 | | - messageItems: new Set<string>(), |
1403 | | - messagePhases: {}, |
| 1384 | + message: undefined, |
1404 | 1385 | reasoningItems: {}, |
1405 | 1386 | }) |
1406 | 1387 |
|
|