{"version":3,"file":"assistant-message-frame.d.ts","sourceRoot":"","sources":["../../src/utils/assistant-message-frame.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,WAAW,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAGnH;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAC9B;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,gBAAgB,CAAA;CAAE,GAC5C;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,WAAW,CAAA;CAAE,GAClE;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC3D;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAAE,GACnF;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,eAAe,CAAA;CAAE,GAC1E;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/D;IACA,IAAI,EAAE,cAAc,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,EAAE,OAAO,CAAC;CAClB,GACD;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,GACpE;IAAE,IAAI,EAAE,qBAAqB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACnE;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/D;IACA,IAAI,EAAE,cAAc,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IACjC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAqGL;;;;GAIG;AACH,qBAAa,4BAA4B;IACxC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwC;IAE/D,MAAM,CAAC,KAAK,EAAE,qBAAqB,GAAG,qBAAqB,GAAG,SAAS,CA6ItE;IAED,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,KAAK;IAUb,OAAO,CAAC,QAAQ;IAKhB,OAAO,CAAC,eAAe;CAgBvB;AA0CD;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,QAAQ,CAAC,qBAAqB,CAAC,GAAG,gBAAgB,GAAG,SAAS,CAsHlH","sourcesContent":["import type { AssistantMessage, AssistantMessageEvent, TextContent, ThinkingContent, ToolCall } from \"../types.ts\";\nimport { parseStreamingJson } from \"./json-parse.ts\";\n\n/**\n * Compact, replayable assistant-message progress. Terminal settlement is\n * intentionally excluded and must be persisted separately.\n */\nexport type AssistantMessageFrame =\n\t| { type: \"start\"; partial: AssistantMessage }\n\t| { type: \"text_start\"; contentIndex: number; content: TextContent }\n\t| { type: \"text_delta\"; contentIndex: number; delta: string }\n\t| { type: \"text_end\"; contentIndex: number; content: string; textSignature?: string }\n\t| { type: \"thinking_start\"; contentIndex: number; content: ThinkingContent }\n\t| { type: \"thinking_delta\"; contentIndex: number; delta: string }\n\t| {\n\t\t\ttype: \"thinking_end\";\n\t\t\tcontentIndex: number;\n\t\t\tcontent: string;\n\t\t\tthinkingSignature?: string;\n\t\t\tredacted?: boolean;\n\t  }\n\t| { type: \"toolcall_start\"; contentIndex: number; toolCall: ToolCall }\n\t| { type: \"toolcall_checkpoint\"; contentIndex: number; json: string }\n\t| { type: \"toolcall_delta\"; contentIndex: number; delta: string }\n\t| {\n\t\t\ttype: \"toolcall_end\";\n\t\t\tcontentIndex: number;\n\t\t\tid: string;\n\t\t\tname: string;\n\t\t\targuments: ToolCall[\"arguments\"];\n\t\t\tthoughtSignature?: string;\n\t\t\tnamespace?: string;\n\t  };\n\ntype EncoderBlockState =\n\t| { kind: \"text\" | \"thinking\"; coveredChars: number; deltaChars: number }\n\t| {\n\t\t\tkind: \"toolCall\";\n\t\t\tcaughtUp: boolean;\n\t\t\tcatchupJson: string;\n\t\t\tsnapshotArguments: string;\n\t  };\n\ntype ReducerBlockState =\n\t| { kind: \"text\"; ended: boolean }\n\t| { kind: \"thinking\"; ended: boolean }\n\t| { kind: \"toolCall\"; ended: boolean; json: string };\n\nfunction cloneTextContent(content: TextContent): TextContent {\n\treturn {\n\t\ttype: \"text\",\n\t\ttext: content.text,\n\t\t...(content.textSignature === undefined ? {} : { textSignature: content.textSignature }),\n\t};\n}\n\nfunction cloneThinkingContent(content: ThinkingContent): ThinkingContent {\n\treturn {\n\t\ttype: \"thinking\",\n\t\tthinking: content.thinking,\n\t\t...(content.thinkingSignature === undefined ? {} : { thinkingSignature: content.thinkingSignature }),\n\t\t...(content.redacted === undefined ? {} : { redacted: content.redacted }),\n\t};\n}\n\nfunction cloneToolCall(toolCall: ToolCall): ToolCall {\n\treturn {\n\t\ttype: \"toolCall\",\n\t\tid: toolCall.id,\n\t\tname: toolCall.name,\n\t\targuments: structuredClone(toolCall.arguments),\n\t\t...(toolCall.thoughtSignature === undefined ? {} : { thoughtSignature: toolCall.thoughtSignature }),\n\t\t...(toolCall.namespace === undefined ? {} : { namespace: toolCall.namespace }),\n\t};\n}\n\nfunction cloneStartMessage(message: AssistantMessage): AssistantMessage {\n\treturn {\n\t\trole: \"assistant\",\n\t\tcontent: [],\n\t\tapi: message.api,\n\t\tprovider: message.provider,\n\t\tmodel: message.model,\n\t\t...(message.responseModel === undefined ? {} : { responseModel: message.responseModel }),\n\t\t...(message.responseId === undefined ? {} : { responseId: message.responseId }),\n\t\t...(message.providerThinkingLevel === undefined ? {} : { providerThinkingLevel: message.providerThinkingLevel }),\n\t\t...(message.diagnostics === undefined ? {} : { diagnostics: structuredClone(message.diagnostics) }),\n\t\tusage: structuredClone(message.usage),\n\t\tstopReason: \"pending\",\n\t\ttimestamp: message.timestamp,\n\t};\n}\n\nfunction assertContentIndex(contentIndex: number): void {\n\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0) {\n\t\tthrow new Error(`Invalid assistant message frame contentIndex: ${contentIndex}`);\n\t}\n}\n\nfunction eventBlock(event: Exclude<AssistantMessageEvent, { type: \"start\" | \"done\" | \"error\" }>) {\n\tassertContentIndex(event.contentIndex);\n\tconst block = event.partial.content[event.contentIndex];\n\tif (!block) {\n\t\tthrow new Error(`${event.type} event has no content block at index ${event.contentIndex}`);\n\t}\n\treturn block;\n}\n\nfunction serializedArguments(argumentsValue: ToolCall[\"arguments\"]): string {\n\tconst serialized = JSON.stringify(argumentsValue);\n\tif (serialized === undefined) throw new Error(\"Tool-call arguments are not JSON-serializable\");\n\treturn serialized;\n}\n\nconst EMPTY_PARSED_TOOL_ARGUMENTS = serializedArguments(parseStreamingJson<ToolCall[\"arguments\"]>(\"\"));\n\nfunction isJsonPrefix(snapshot: unknown, current: unknown): boolean {\n\tif (typeof snapshot === \"string\") return typeof current === \"string\" && current.startsWith(snapshot);\n\tif (Array.isArray(snapshot)) {\n\t\treturn (\n\t\t\tArray.isArray(current) &&\n\t\t\tsnapshot.length <= current.length &&\n\t\t\tsnapshot.every((value, index) => isJsonPrefix(value, current[index]))\n\t\t);\n\t}\n\tif (typeof snapshot !== \"object\" || snapshot === null) return Object.is(snapshot, current);\n\tif (typeof current !== \"object\" || current === null || Array.isArray(current)) return false;\n\tconst currentRecord = current as Record<string, unknown>;\n\treturn Object.entries(snapshot).every(\n\t\t([key, value]) => Object.hasOwn(currentRecord, key) && isJsonPrefix(value, currentRecord[key]),\n\t);\n}\n\n/**\n * Encodes one assistant stream. `partial` remains a shared live accumulator;\n * the encoder uses per-block offsets to avoid replaying deltas already visible\n * when an older queued event is consumed.\n */\nexport class AssistantMessageFrameEncoder {\n\tprivate started = false;\n\tprivate terminal = false;\n\tprivate readonly blocks = new Map<number, EncoderBlockState>();\n\n\tencode(event: AssistantMessageEvent): AssistantMessageFrame | undefined {\n\t\tif (this.terminal) throw new Error(`Assistant message event ${event.type} follows a terminal event`);\n\n\t\tswitch (event.type) {\n\t\t\tcase \"start\":\n\t\t\t\tif (this.started) throw new Error(\"Assistant message stream contains more than one start event\");\n\t\t\t\tthis.started = true;\n\t\t\t\treturn { type: \"start\", partial: cloneStartMessage(event.partial) };\n\t\t\tcase \"done\":\n\t\t\t\tif (!this.started) throw new Error(\"Assistant message done event appears before start\");\n\t\t\t\tthis.terminal = true;\n\t\t\t\treturn undefined;\n\t\t\tcase \"error\":\n\t\t\t\tthis.terminal = true;\n\t\t\t\treturn undefined;\n\t\t}\n\n\t\tif (!this.started) throw new Error(`Assistant message ${event.type} event appears before start`);\n\n\t\tswitch (event.type) {\n\t\t\tcase \"text_start\": {\n\t\t\t\tconst content = eventBlock(event);\n\t\t\t\tif (content.type !== \"text\") {\n\t\t\t\t\tthrow new Error(`text_start event points to ${content.type} block at index ${event.contentIndex}`);\n\t\t\t\t}\n\t\t\t\tthis.startBlock(event.contentIndex, {\n\t\t\t\t\tkind: \"text\",\n\t\t\t\t\tcoveredChars: content.text.length,\n\t\t\t\t\tdeltaChars: 0,\n\t\t\t\t});\n\t\t\t\treturn { type: \"text_start\", contentIndex: event.contentIndex, content: cloneTextContent(content) };\n\t\t\t}\n\t\t\tcase \"text_delta\":\n\t\t\t\treturn this.encodeTextDelta(event.contentIndex, event.delta, \"text\");\n\t\t\tcase \"text_end\": {\n\t\t\t\tconst content = eventBlock(event);\n\t\t\t\tif (content.type !== \"text\") {\n\t\t\t\t\tthrow new Error(`text_end event points to ${content.type} block at index ${event.contentIndex}`);\n\t\t\t\t}\n\t\t\t\tthis.endBlock(event.contentIndex, \"text\");\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\tcontentIndex: event.contentIndex,\n\t\t\t\t\tcontent: event.content,\n\t\t\t\t\t...(content.textSignature === undefined ? {} : { textSignature: content.textSignature }),\n\t\t\t\t};\n\t\t\t}\n\t\t\tcase \"thinking_start\": {\n\t\t\t\tconst content = eventBlock(event);\n\t\t\t\tif (content.type !== \"thinking\") {\n\t\t\t\t\tthrow new Error(`thinking_start event points to ${content.type} block at index ${event.contentIndex}`);\n\t\t\t\t}\n\t\t\t\tthis.startBlock(event.contentIndex, {\n\t\t\t\t\tkind: \"thinking\",\n\t\t\t\t\tcoveredChars: content.thinking.length,\n\t\t\t\t\tdeltaChars: 0,\n\t\t\t\t});\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"thinking_start\",\n\t\t\t\t\tcontentIndex: event.contentIndex,\n\t\t\t\t\tcontent: cloneThinkingContent(content),\n\t\t\t\t};\n\t\t\t}\n\t\t\tcase \"thinking_delta\":\n\t\t\t\treturn this.encodeTextDelta(event.contentIndex, event.delta, \"thinking\");\n\t\t\tcase \"thinking_end\": {\n\t\t\t\tconst content = eventBlock(event);\n\t\t\t\tif (content.type !== \"thinking\") {\n\t\t\t\t\tthrow new Error(`thinking_end event points to ${content.type} block at index ${event.contentIndex}`);\n\t\t\t\t}\n\t\t\t\tthis.endBlock(event.contentIndex, \"thinking\");\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\tcontentIndex: event.contentIndex,\n\t\t\t\t\tcontent: event.content,\n\t\t\t\t\t...(content.thinkingSignature === undefined ? {} : { thinkingSignature: content.thinkingSignature }),\n\t\t\t\t\t...(content.redacted === undefined ? {} : { redacted: content.redacted }),\n\t\t\t\t};\n\t\t\t}\n\t\t\tcase \"toolcall_start\": {\n\t\t\t\tconst content = eventBlock(event);\n\t\t\t\tif (content.type !== \"toolCall\") {\n\t\t\t\t\tthrow new Error(`toolcall_start event points to ${content.type} block at index ${event.contentIndex}`);\n\t\t\t\t}\n\t\t\t\tconst snapshotArguments = serializedArguments(content.arguments);\n\t\t\t\tconst caughtUp = snapshotArguments === EMPTY_PARSED_TOOL_ARGUMENTS;\n\t\t\t\tthis.startBlock(event.contentIndex, {\n\t\t\t\t\tkind: \"toolCall\",\n\t\t\t\t\tcaughtUp,\n\t\t\t\t\tcatchupJson: \"\",\n\t\t\t\t\tsnapshotArguments: caughtUp ? \"\" : snapshotArguments,\n\t\t\t\t});\n\t\t\t\treturn { type: \"toolcall_start\", contentIndex: event.contentIndex, toolCall: cloneToolCall(content) };\n\t\t\t}\n\t\t\tcase \"toolcall_delta\": {\n\t\t\t\tconst state = this.block(event.contentIndex, \"toolCall\");\n\t\t\t\tif (state.kind !== \"toolCall\") throw new Error(\"Unreachable tool-call encoder state\");\n\t\t\t\tif (state.caughtUp) {\n\t\t\t\t\treturn event.delta.length === 0\n\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t: { type: \"toolcall_delta\", contentIndex: event.contentIndex, delta: event.delta };\n\t\t\t\t}\n\t\t\t\tstate.catchupJson += event.delta;\n\t\t\t\tconst argumentsValue = parseStreamingJson<ToolCall[\"arguments\"]>(state.catchupJson);\n\t\t\t\tif (serializedArguments(argumentsValue) !== state.snapshotArguments) {\n\t\t\t\t\t// Legacy grammar calls include the initial input in toolcall_start, but their\n\t\t\t\t\t// JSON delta stream still begins at an empty input. Its parsed arguments can\n\t\t\t\t\t// therefore extend, rather than exactly reproduce, the start snapshot.\n\t\t\t\t\tconst snapshotArguments = parseStreamingJson<ToolCall[\"arguments\"]>(state.snapshotArguments);\n\t\t\t\t\tif (!isJsonPrefix(snapshotArguments, argumentsValue)) return undefined;\n\t\t\t\t}\n\t\t\t\tstate.caughtUp = true;\n\t\t\t\tstate.snapshotArguments = \"\";\n\t\t\t\tconst json = state.catchupJson;\n\t\t\t\tstate.catchupJson = \"\";\n\t\t\t\treturn json.length === 0\n\t\t\t\t\t? undefined\n\t\t\t\t\t: { type: \"toolcall_checkpoint\", contentIndex: event.contentIndex, json };\n\t\t\t}\n\t\t\tcase \"toolcall_end\": {\n\t\t\t\tconst content = eventBlock(event);\n\t\t\t\tif (content.type !== \"toolCall\") {\n\t\t\t\t\tthrow new Error(`toolcall_end event points to ${content.type} block at index ${event.contentIndex}`);\n\t\t\t\t}\n\t\t\t\tif (event.toolCall.type !== \"toolCall\") {\n\t\t\t\t\tthrow new Error(`toolcall_end event has invalid tool call at index ${event.contentIndex}`);\n\t\t\t\t}\n\t\t\t\tthis.endBlock(event.contentIndex, \"toolCall\");\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"toolcall_end\",\n\t\t\t\t\tcontentIndex: event.contentIndex,\n\t\t\t\t\tid: event.toolCall.id,\n\t\t\t\t\tname: event.toolCall.name,\n\t\t\t\t\targuments: structuredClone(event.toolCall.arguments),\n\t\t\t\t\t...(event.toolCall.thoughtSignature === undefined\n\t\t\t\t\t\t? {}\n\t\t\t\t\t\t: { thoughtSignature: event.toolCall.thoughtSignature }),\n\t\t\t\t\t...(event.toolCall.namespace === undefined ? {} : { namespace: event.toolCall.namespace }),\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate startBlock(contentIndex: number, state: EncoderBlockState): void {\n\t\tassertContentIndex(contentIndex);\n\t\tif (this.blocks.has(contentIndex)) {\n\t\t\tthrow new Error(`Assistant message block ${contentIndex} starts more than once`);\n\t\t}\n\t\tthis.blocks.set(contentIndex, state);\n\t}\n\n\tprivate block(contentIndex: number, kind: EncoderBlockState[\"kind\"]): EncoderBlockState {\n\t\tassertContentIndex(contentIndex);\n\t\tconst state = this.blocks.get(contentIndex);\n\t\tif (state === undefined) throw new Error(`Assistant message ${kind} block ${contentIndex} has not started`);\n\t\tif (state.kind !== kind) {\n\t\t\tthrow new Error(`Assistant message block ${contentIndex} is ${state.kind}, not ${kind}`);\n\t\t}\n\t\treturn state;\n\t}\n\n\tprivate endBlock(contentIndex: number, kind: EncoderBlockState[\"kind\"]): void {\n\t\tthis.block(contentIndex, kind);\n\t\tthis.blocks.delete(contentIndex);\n\t}\n\n\tprivate encodeTextDelta(\n\t\tcontentIndex: number,\n\t\tdelta: string,\n\t\tkind: \"text\" | \"thinking\",\n\t): AssistantMessageFrame | undefined {\n\t\tconst state = this.block(contentIndex, kind);\n\t\tif (state.kind === \"toolCall\") throw new Error(\"Unreachable text encoder state\");\n\t\tconst deltaStart = state.deltaChars;\n\t\tstate.deltaChars += delta.length;\n\t\tconst covered = Math.max(0, state.coveredChars - deltaStart);\n\t\tif (covered >= delta.length) return undefined;\n\t\tconst uncovered = covered === 0 ? delta : delta.slice(covered);\n\t\treturn kind === \"text\"\n\t\t\t? { type: \"text_delta\", contentIndex, delta: uncovered }\n\t\t\t: { type: \"thinking_delta\", contentIndex, delta: uncovered };\n\t}\n}\n\nfunction appendBlock(\n\tmessage: AssistantMessage,\n\tstates: Map<number, ReducerBlockState>,\n\tcontentIndex: number,\n\tblock: TextContent | ThinkingContent | ToolCall,\n\tstate: ReducerBlockState,\n): void {\n\tassertContentIndex(contentIndex);\n\tif (contentIndex !== message.content.length) {\n\t\tconst reason = contentIndex < message.content.length ? \"already exists\" : \"would leave a gap\";\n\t\tthrow new Error(`Cannot start assistant message block at index ${contentIndex}: ${reason}`);\n\t}\n\tmessage.content.push(structuredClone(block));\n\tstates.set(contentIndex, state);\n}\n\nfunction activeBlock(\n\tmessage: AssistantMessage,\n\tstates: Map<number, ReducerBlockState>,\n\tcontentIndex: number,\n\texpectedKind: ReducerBlockState[\"kind\"],\n\tframeType: AssistantMessageFrame[\"type\"],\n): { block: TextContent | ThinkingContent | ToolCall; state: ReducerBlockState } {\n\tassertContentIndex(contentIndex);\n\tconst state = states.get(contentIndex);\n\tconst block = message.content[contentIndex];\n\tif (!state || !block) {\n\t\tthrow new Error(`${frameType} frame has no started block at index ${contentIndex}`);\n\t}\n\tif (state.kind !== expectedKind || block.type !== expectedKind) {\n\t\tthrow new Error(\n\t\t\t`${frameType} frame expected ${expectedKind} block at index ${contentIndex}, found ${block.type}`,\n\t\t);\n\t}\n\tif (state.ended) {\n\t\tthrow new Error(`${frameType} frame follows the end of block at index ${contentIndex}`);\n\t}\n\treturn { block, state };\n}\n\n/**\n * Replay compact frames without mutating them. Returns `undefined` when the\n * iterable contains no start frame.\n */\nexport function reduceAssistantMessageFrames(frames: Iterable<AssistantMessageFrame>): AssistantMessage | undefined {\n\tlet message: AssistantMessage | undefined;\n\tlet frameBeforeStart: AssistantMessageFrame[\"type\"] | undefined;\n\tconst states = new Map<number, ReducerBlockState>();\n\n\tfor (const frame of frames) {\n\t\tif (frame.type === \"start\") {\n\t\t\tif (message) throw new Error(\"Assistant message frame sequence contains more than one start frame\");\n\t\t\tif (frameBeforeStart !== undefined)\n\t\t\t\tthrow new Error(`${frameBeforeStart} frame appears before the start frame`);\n\t\t\tmessage = structuredClone(frame.partial);\n\t\t\tcontinue;\n\t\t}\n\t\tif (!message) {\n\t\t\tframeBeforeStart ??= frame.type;\n\t\t\tcontinue;\n\t\t}\n\n\t\tswitch (frame.type) {\n\t\t\tcase \"text_start\":\n\t\t\t\tif (frame.content.type !== \"text\") {\n\t\t\t\t\tthrow new Error(`text_start frame contains ${frame.content.type} content`);\n\t\t\t\t}\n\t\t\t\tappendBlock(message, states, frame.contentIndex, frame.content, { kind: \"text\", ended: false });\n\t\t\t\tbreak;\n\t\t\tcase \"text_delta\": {\n\t\t\t\tconst { block } = activeBlock(message, states, frame.contentIndex, \"text\", frame.type);\n\t\t\t\tif (block.type !== \"text\") throw new Error(\"Unreachable text frame state\");\n\t\t\t\tblock.text += frame.delta;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"text_end\": {\n\t\t\t\tconst { block, state } = activeBlock(message, states, frame.contentIndex, \"text\", frame.type);\n\t\t\t\tif (block.type !== \"text\") throw new Error(\"Unreachable text frame state\");\n\t\t\t\tblock.text = frame.content;\n\t\t\t\tdelete block.textSignature;\n\t\t\t\tif (frame.textSignature !== undefined) block.textSignature = frame.textSignature;\n\t\t\t\tstate.ended = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"thinking_start\":\n\t\t\t\tif (frame.content.type !== \"thinking\") {\n\t\t\t\t\tthrow new Error(`thinking_start frame contains ${frame.content.type} content`);\n\t\t\t\t}\n\t\t\t\tappendBlock(message, states, frame.contentIndex, frame.content, {\n\t\t\t\t\tkind: \"thinking\",\n\t\t\t\t\tended: false,\n\t\t\t\t});\n\t\t\t\tbreak;\n\t\t\tcase \"thinking_delta\": {\n\t\t\t\tconst { block } = activeBlock(message, states, frame.contentIndex, \"thinking\", frame.type);\n\t\t\t\tif (block.type !== \"thinking\") throw new Error(\"Unreachable thinking frame state\");\n\t\t\t\tblock.thinking += frame.delta;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"thinking_end\": {\n\t\t\t\tconst { block, state } = activeBlock(message, states, frame.contentIndex, \"thinking\", frame.type);\n\t\t\t\tif (block.type !== \"thinking\") throw new Error(\"Unreachable thinking frame state\");\n\t\t\t\tblock.thinking = frame.content;\n\t\t\t\tdelete block.thinkingSignature;\n\t\t\t\tdelete block.redacted;\n\t\t\t\tif (frame.thinkingSignature !== undefined) block.thinkingSignature = frame.thinkingSignature;\n\t\t\t\tif (frame.redacted !== undefined) block.redacted = frame.redacted;\n\t\t\t\tstate.ended = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"toolcall_start\":\n\t\t\t\tif (frame.toolCall.type !== \"toolCall\") {\n\t\t\t\t\tthrow new Error(`toolcall_start frame contains ${frame.toolCall.type} content`);\n\t\t\t\t}\n\t\t\t\tappendBlock(message, states, frame.contentIndex, frame.toolCall, {\n\t\t\t\t\tkind: \"toolCall\",\n\t\t\t\t\tended: false,\n\t\t\t\t\tjson: \"\",\n\t\t\t\t});\n\t\t\t\tbreak;\n\t\t\tcase \"toolcall_checkpoint\": {\n\t\t\t\tconst { block, state } = activeBlock(message, states, frame.contentIndex, \"toolCall\", frame.type);\n\t\t\t\tif (block.type !== \"toolCall\" || state.kind !== \"toolCall\") {\n\t\t\t\t\tthrow new Error(\"Unreachable tool-call checkpoint state\");\n\t\t\t\t}\n\t\t\t\tstate.json = frame.json;\n\t\t\t\tblock.arguments = parseStreamingJson<ToolCall[\"arguments\"]>(frame.json);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"toolcall_delta\": {\n\t\t\t\tconst { block, state } = activeBlock(message, states, frame.contentIndex, \"toolCall\", frame.type);\n\t\t\t\tif (block.type !== \"toolCall\" || state.kind !== \"toolCall\") {\n\t\t\t\t\tthrow new Error(\"Unreachable tool-call frame state\");\n\t\t\t\t}\n\t\t\t\tstate.json += frame.delta;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"toolcall_end\": {\n\t\t\t\tconst { block, state } = activeBlock(message, states, frame.contentIndex, \"toolCall\", frame.type);\n\t\t\t\tif (block.type !== \"toolCall\") throw new Error(\"Unreachable tool-call frame state\");\n\t\t\t\tblock.id = frame.id;\n\t\t\t\tblock.name = frame.name;\n\t\t\t\tblock.arguments = structuredClone(frame.arguments);\n\t\t\t\tdelete block.thoughtSignature;\n\t\t\t\tdelete block.namespace;\n\t\t\t\tif (frame.thoughtSignature !== undefined) block.thoughtSignature = frame.thoughtSignature;\n\t\t\t\tif (frame.namespace !== undefined) block.namespace = frame.namespace;\n\t\t\t\tstate.ended = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!message) return undefined;\n\tfor (const [contentIndex, state] of states) {\n\t\tif (state.kind !== \"toolCall\" || state.ended || state.json.length === 0) continue;\n\t\tconst block = message.content[contentIndex];\n\t\tif (block?.type !== \"toolCall\") throw new Error(\"Unreachable tool-call frame state\");\n\t\tblock.arguments = parseStreamingJson<ToolCall[\"arguments\"]>(state.json);\n\t}\n\n\treturn message;\n}\n"]}