UNPKG

@ai-sdk/ui-utils

Version:

Important: this is an internal API. Expect breaking changes.

1 lines 104 kB
{"version":3,"sources":["../src/index.ts","../src/assistant-stream-parts.ts","../src/process-chat-response.ts","../src/duplicated/usage.ts","../src/parse-partial-json.ts","../src/fix-json.ts","../src/data-stream-parts.ts","../src/process-data-stream.ts","../src/process-chat-text-response.ts","../src/process-text-stream.ts","../src/call-chat-api.ts","../src/call-completion-api.ts","../src/data-url.ts","../src/extract-max-tool-invocation-step.ts","../src/get-message-parts.ts","../src/fill-message-parts.ts","../src/is-deep-equal-data.ts","../src/prepare-attachments-for-request.ts","../src/process-assistant-stream.ts","../src/schema.ts","../src/zod-schema.ts","../src/should-resubmit-messages.ts","../src/update-tool-call-result.ts"],"sourcesContent":["export * from './types';\n\nexport { generateId } from '@ai-sdk/provider-utils';\n\n// Export stream data utilities for custom stream implementations,\n// both on the client and server side.\n// NOTE: this is experimental / internal and may change without notice\nexport {\n formatAssistantStreamPart,\n parseAssistantStreamPart,\n} from './assistant-stream-parts';\nexport type {\n AssistantStreamPart,\n AssistantStreamString,\n} from './assistant-stream-parts';\nexport { callChatApi } from './call-chat-api';\nexport { callCompletionApi } from './call-completion-api';\nexport { formatDataStreamPart, parseDataStreamPart } from './data-stream-parts';\nexport type { DataStreamPart, DataStreamString } from './data-stream-parts';\nexport { getTextFromDataUrl } from './data-url';\nexport type { DeepPartial } from './deep-partial';\nexport { extractMaxToolInvocationStep } from './extract-max-tool-invocation-step';\nexport { fillMessageParts } from './fill-message-parts';\nexport { getMessageParts } from './get-message-parts';\nexport { isDeepEqualData } from './is-deep-equal-data';\nexport { parsePartialJson } from './parse-partial-json';\nexport { prepareAttachmentsForRequest } from './prepare-attachments-for-request';\nexport { processAssistantStream } from './process-assistant-stream';\nexport { processDataStream } from './process-data-stream';\nexport { processTextStream } from './process-text-stream';\nexport { asSchema, jsonSchema } from './schema';\nexport type { Schema } from './schema';\nexport {\n isAssistantMessageWithCompletedToolCalls,\n shouldResubmitMessages,\n} from './should-resubmit-messages';\nexport { updateToolCallResult } from './update-tool-call-result';\nexport { zodSchema } from './zod-schema';\n","import { AssistantMessage, DataMessage, JSONValue } from './types';\n\nexport type AssistantStreamString =\n `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\nexport interface AssistantStreamPart<\n CODE extends string,\n NAME extends string,\n TYPE,\n> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: AssistantStreamPart<'0', 'text', string> = {\n code: '0',\n name: 'text',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: 'text', value };\n },\n};\n\nconst errorStreamPart: AssistantStreamPart<'3', 'error', string> = {\n code: '3',\n name: 'error',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: 'error', value };\n },\n};\n\nconst assistantMessageStreamPart: AssistantStreamPart<\n '4',\n 'assistant_message',\n AssistantMessage\n> = {\n code: '4',\n name: 'assistant_message',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('id' in value) ||\n !('role' in value) ||\n !('content' in value) ||\n typeof value.id !== 'string' ||\n typeof value.role !== 'string' ||\n value.role !== 'assistant' ||\n !Array.isArray(value.content) ||\n !value.content.every(\n item =>\n item != null &&\n typeof item === 'object' &&\n 'type' in item &&\n item.type === 'text' &&\n 'text' in item &&\n item.text != null &&\n typeof item.text === 'object' &&\n 'value' in item.text &&\n typeof item.text.value === 'string',\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: 'assistant_message',\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlDataStreamPart: AssistantStreamPart<\n '5',\n 'assistant_control_data',\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: '5',\n name: 'assistant_control_data',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('threadId' in value) ||\n !('messageId' in value) ||\n typeof value.threadId !== 'string' ||\n typeof value.messageId !== 'string'\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: 'assistant_control_data',\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst dataMessageStreamPart: AssistantStreamPart<\n '6',\n 'data_message',\n DataMessage\n> = {\n code: '6',\n name: 'data_message',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('role' in value) ||\n !('data' in value) ||\n typeof value.role !== 'string' ||\n value.role !== 'data'\n ) {\n throw new Error(\n '\"data_message\" parts expect an object with a \"role\" and \"data\" property.',\n );\n }\n\n return {\n type: 'data_message',\n value: value as DataMessage,\n };\n },\n};\n\nconst assistantStreamParts = [\n textStreamPart,\n errorStreamPart,\n assistantMessageStreamPart,\n assistantControlDataStreamPart,\n dataMessageStreamPart,\n] as const;\n\ntype AssistantStreamParts =\n | typeof textStreamPart\n | typeof errorStreamPart\n | typeof assistantMessageStreamPart\n | typeof assistantControlDataStreamPart\n | typeof dataMessageStreamPart;\n\ntype AssistantStreamPartValueType = {\n [P in AssistantStreamParts as P['name']]: ReturnType<P['parse']>['value'];\n};\n\nexport type AssistantStreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessageStreamPart.parse>\n | ReturnType<typeof assistantControlDataStreamPart.parse>\n | ReturnType<typeof dataMessageStreamPart.parse>;\n\nexport const assistantStreamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessageStreamPart.code]: assistantMessageStreamPart,\n [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,\n [dataMessageStreamPart.code]: dataMessageStreamPart,\n} as const;\n\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,\n [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,\n [dataMessageStreamPart.name]: dataMessageStreamPart.code,\n} as const;\n\nexport const validCodes = assistantStreamParts.map(part => part.code);\n\nexport const parseAssistantStreamPart = (\n line: string,\n): AssistantStreamPartType => {\n const firstSeparatorIndex = line.indexOf(':');\n\n if (firstSeparatorIndex === -1) {\n throw new Error('Failed to parse stream string. No separator found.');\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof assistantStreamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof assistantStreamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return assistantStreamPartsByCode[code].parse(jsonValue);\n};\n\nexport function formatAssistantStreamPart<\n T extends keyof AssistantStreamPartValueType,\n>(type: T, value: AssistantStreamPartValueType[T]): AssistantStreamString {\n const streamPart = assistantStreamParts.find(part => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n","import { LanguageModelV1FinishReason } from '@ai-sdk/provider';\nimport { generateId as generateIdFunction } from '@ai-sdk/provider-utils';\nimport {\n calculateLanguageModelUsage,\n LanguageModelUsage,\n} from './duplicated/usage';\nimport { parsePartialJson } from './parse-partial-json';\nimport { processDataStream } from './process-data-stream';\nimport type {\n JSONValue,\n ReasoningUIPart,\n TextUIPart,\n ToolInvocation,\n ToolInvocationUIPart,\n UIMessage,\n UseChatOptions,\n} from './types';\n\nexport async function processChatResponse({\n stream,\n update,\n onToolCall,\n onFinish,\n generateId = generateIdFunction,\n getCurrentDate = () => new Date(),\n lastMessage,\n}: {\n stream: ReadableStream<Uint8Array>;\n update: (options: {\n message: UIMessage;\n data: JSONValue[] | undefined;\n replaceLastMessage: boolean;\n }) => void;\n onToolCall?: UseChatOptions['onToolCall'];\n onFinish?: (options: {\n message: UIMessage | undefined;\n finishReason: LanguageModelV1FinishReason;\n usage: LanguageModelUsage;\n }) => void;\n generateId?: () => string;\n getCurrentDate?: () => Date;\n lastMessage: UIMessage | undefined;\n}) {\n const replaceLastMessage = lastMessage?.role === 'assistant';\n let step = replaceLastMessage\n ? 1 +\n // find max step in existing tool invocations:\n (lastMessage.toolInvocations?.reduce((max, toolInvocation) => {\n return Math.max(max, toolInvocation.step ?? 0);\n }, 0) ?? 0)\n : 0;\n\n const message: UIMessage = replaceLastMessage\n ? structuredClone(lastMessage)\n : {\n id: generateId(),\n createdAt: getCurrentDate(),\n role: 'assistant',\n content: '',\n parts: [],\n };\n\n let currentTextPart: TextUIPart | undefined = undefined;\n let currentReasoningPart: ReasoningUIPart | undefined = undefined;\n let currentReasoningTextDetail:\n | { type: 'text'; text: string; signature?: string }\n | undefined = undefined;\n\n function updateToolInvocationPart(\n toolCallId: string,\n invocation: ToolInvocation,\n ) {\n const part = message.parts.find(\n part =>\n part.type === 'tool-invocation' &&\n part.toolInvocation.toolCallId === toolCallId,\n ) as ToolInvocationUIPart | undefined;\n\n if (part != null) {\n part.toolInvocation = invocation;\n } else {\n message.parts.push({\n type: 'tool-invocation',\n toolInvocation: invocation,\n });\n }\n }\n\n const data: JSONValue[] = [];\n\n // keep list of current message annotations for message\n let messageAnnotations: JSONValue[] | undefined = replaceLastMessage\n ? lastMessage?.annotations\n : undefined;\n\n // keep track of partial tool calls\n const partialToolCalls: Record<\n string,\n { text: string; step: number; index: number; toolName: string }\n > = {};\n\n let usage: LanguageModelUsage = {\n completionTokens: NaN,\n promptTokens: NaN,\n totalTokens: NaN,\n };\n let finishReason: LanguageModelV1FinishReason = 'unknown';\n\n function execUpdate() {\n // make a copy of the data array to ensure UI is updated (SWR)\n const copiedData = [...data];\n\n // keeps the currentMessage up to date with the latest annotations,\n // even if annotations preceded the message creation\n if (messageAnnotations?.length) {\n message.annotations = messageAnnotations;\n }\n\n const copiedMessage = {\n // deep copy the message to ensure that deep changes (msg attachments) are updated\n // with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.\n ...structuredClone(message),\n // add a revision id to ensure that the message is updated with SWR. SWR uses a\n // hashing approach by default to detect changes, but it only works for shallow\n // changes. This is why we need to add a revision id to ensure that the message\n // is updated with SWR (without it, the changes get stuck in SWR and are not\n // forwarded to rendering):\n revisionId: generateId(),\n } as UIMessage;\n\n update({\n message: copiedMessage,\n data: copiedData,\n replaceLastMessage,\n });\n }\n\n await processDataStream({\n stream,\n onTextPart(value) {\n if (currentTextPart == null) {\n currentTextPart = {\n type: 'text',\n text: value,\n };\n message.parts.push(currentTextPart);\n } else {\n currentTextPart.text += value;\n }\n\n message.content += value;\n execUpdate();\n },\n onReasoningPart(value) {\n if (currentReasoningTextDetail == null) {\n currentReasoningTextDetail = { type: 'text', text: value };\n if (currentReasoningPart != null) {\n currentReasoningPart.details.push(currentReasoningTextDetail);\n }\n } else {\n currentReasoningTextDetail.text += value;\n }\n\n if (currentReasoningPart == null) {\n currentReasoningPart = {\n type: 'reasoning',\n reasoning: value,\n details: [currentReasoningTextDetail],\n };\n message.parts.push(currentReasoningPart);\n } else {\n currentReasoningPart.reasoning += value;\n }\n\n message.reasoning = (message.reasoning ?? '') + value;\n\n execUpdate();\n },\n onReasoningSignaturePart(value) {\n if (currentReasoningTextDetail != null) {\n currentReasoningTextDetail.signature = value.signature;\n }\n },\n onRedactedReasoningPart(value) {\n if (currentReasoningPart == null) {\n currentReasoningPart = {\n type: 'reasoning',\n reasoning: '',\n details: [],\n };\n message.parts.push(currentReasoningPart);\n }\n\n currentReasoningPart.details.push({\n type: 'redacted',\n data: value.data,\n });\n\n currentReasoningTextDetail = undefined;\n\n execUpdate();\n },\n onFilePart(value) {\n message.parts.push({\n type: 'file',\n mimeType: value.mimeType,\n data: value.data,\n });\n\n execUpdate();\n },\n onSourcePart(value) {\n message.parts.push({\n type: 'source',\n source: value,\n });\n\n execUpdate();\n },\n onToolCallStreamingStartPart(value) {\n if (message.toolInvocations == null) {\n message.toolInvocations = [];\n }\n\n // add the partial tool call to the map\n partialToolCalls[value.toolCallId] = {\n text: '',\n step,\n toolName: value.toolName,\n index: message.toolInvocations.length,\n };\n\n const invocation = {\n state: 'partial-call',\n step,\n toolCallId: value.toolCallId,\n toolName: value.toolName,\n args: undefined,\n } as const;\n\n message.toolInvocations.push(invocation);\n\n updateToolInvocationPart(value.toolCallId, invocation);\n\n execUpdate();\n },\n onToolCallDeltaPart(value) {\n const partialToolCall = partialToolCalls[value.toolCallId];\n\n partialToolCall.text += value.argsTextDelta;\n\n const { value: partialArgs } = parsePartialJson(partialToolCall.text);\n\n const invocation = {\n state: 'partial-call',\n step: partialToolCall.step,\n toolCallId: value.toolCallId,\n toolName: partialToolCall.toolName,\n args: partialArgs,\n } as const;\n\n message.toolInvocations![partialToolCall.index] = invocation;\n\n updateToolInvocationPart(value.toolCallId, invocation);\n\n execUpdate();\n },\n async onToolCallPart(value) {\n const invocation = {\n state: 'call',\n step,\n ...value,\n } as const;\n\n if (partialToolCalls[value.toolCallId] != null) {\n // change the partial tool call to a full tool call\n message.toolInvocations![partialToolCalls[value.toolCallId].index] =\n invocation;\n } else {\n if (message.toolInvocations == null) {\n message.toolInvocations = [];\n }\n\n message.toolInvocations.push(invocation);\n }\n\n updateToolInvocationPart(value.toolCallId, invocation);\n\n execUpdate();\n\n // invoke the onToolCall callback if it exists. This is blocking.\n // In the future we should make this non-blocking, which\n // requires additional state management for error handling etc.\n if (onToolCall) {\n const result = await onToolCall({ toolCall: value });\n if (result != null) {\n const invocation = {\n state: 'result',\n step,\n ...value,\n result,\n } as const;\n\n // store the result in the tool invocation\n message.toolInvocations![message.toolInvocations!.length - 1] =\n invocation;\n\n updateToolInvocationPart(value.toolCallId, invocation);\n\n execUpdate();\n }\n }\n },\n onToolResultPart(value) {\n const toolInvocations = message.toolInvocations;\n\n if (toolInvocations == null) {\n throw new Error('tool_result must be preceded by a tool_call');\n }\n\n // find if there is any tool invocation with the same toolCallId\n // and replace it with the result\n const toolInvocationIndex = toolInvocations.findIndex(\n invocation => invocation.toolCallId === value.toolCallId,\n );\n\n if (toolInvocationIndex === -1) {\n throw new Error(\n 'tool_result must be preceded by a tool_call with the same toolCallId',\n );\n }\n\n const invocation = {\n ...toolInvocations[toolInvocationIndex],\n state: 'result' as const,\n ...value,\n } as const;\n\n toolInvocations[toolInvocationIndex] = invocation;\n\n updateToolInvocationPart(value.toolCallId, invocation);\n\n execUpdate();\n },\n onDataPart(value) {\n data.push(...value);\n execUpdate();\n },\n onMessageAnnotationsPart(value) {\n if (messageAnnotations == null) {\n messageAnnotations = [...value];\n } else {\n messageAnnotations.push(...value);\n }\n\n execUpdate();\n },\n onFinishStepPart(value) {\n step += 1;\n\n // reset the current text and reasoning parts\n currentTextPart = value.isContinued ? currentTextPart : undefined;\n currentReasoningPart = undefined;\n currentReasoningTextDetail = undefined;\n },\n onStartStepPart(value) {\n // keep message id stable when we are updating an existing message:\n if (!replaceLastMessage) {\n message.id = value.messageId;\n }\n\n // add a step boundary part to the message\n message.parts.push({ type: 'step-start' });\n execUpdate();\n },\n onFinishMessagePart(value) {\n finishReason = value.finishReason;\n if (value.usage != null) {\n usage = calculateLanguageModelUsage(value.usage);\n }\n },\n onErrorPart(error) {\n throw new Error(error);\n },\n });\n\n onFinish?.({ message, finishReason, usage });\n}\n","/**\nRepresents the number of tokens used in a prompt and completion.\n */\nexport type LanguageModelUsage = {\n /**\nThe number of tokens used in the prompt.\n */\n promptTokens: number;\n\n /**\nThe number of tokens used in the completion.\n */\n completionTokens: number;\n\n /**\nThe total number of tokens used (promptTokens + completionTokens).\n */\n totalTokens: number;\n};\n\n/**\nRepresents the number of tokens used in an embedding.\n */\nexport type EmbeddingModelUsage = {\n /**\nThe number of tokens used in the embedding.\n */\n tokens: number;\n};\n\nexport function calculateLanguageModelUsage({\n promptTokens,\n completionTokens,\n}: {\n promptTokens: number;\n completionTokens: number;\n}): LanguageModelUsage {\n return {\n promptTokens,\n completionTokens,\n totalTokens: promptTokens + completionTokens,\n };\n}\n","import { JSONValue } from '@ai-sdk/provider';\nimport { safeParseJSON } from '@ai-sdk/provider-utils';\nimport { fixJson } from './fix-json';\n\nexport function parsePartialJson(jsonText: string | undefined): {\n value: JSONValue | undefined;\n state:\n | 'undefined-input'\n | 'successful-parse'\n | 'repaired-parse'\n | 'failed-parse';\n} {\n if (jsonText === undefined) {\n return { value: undefined, state: 'undefined-input' };\n }\n\n let result = safeParseJSON({ text: jsonText });\n\n if (result.success) {\n return { value: result.value, state: 'successful-parse' };\n }\n\n result = safeParseJSON({ text: fixJson(jsonText) });\n\n if (result.success) {\n return { value: result.value, state: 'repaired-parse' };\n }\n\n return { value: undefined, state: 'failed-parse' };\n}\n","type State =\n | 'ROOT'\n | 'FINISH'\n | 'INSIDE_STRING'\n | 'INSIDE_STRING_ESCAPE'\n | 'INSIDE_LITERAL'\n | 'INSIDE_NUMBER'\n | 'INSIDE_OBJECT_START'\n | 'INSIDE_OBJECT_KEY'\n | 'INSIDE_OBJECT_AFTER_KEY'\n | 'INSIDE_OBJECT_BEFORE_VALUE'\n | 'INSIDE_OBJECT_AFTER_VALUE'\n | 'INSIDE_OBJECT_AFTER_COMMA'\n | 'INSIDE_ARRAY_START'\n | 'INSIDE_ARRAY_AFTER_VALUE'\n | 'INSIDE_ARRAY_AFTER_COMMA';\n\n// Implemented as a scanner with additional fixing\n// that performs a single linear time scan pass over the partial JSON.\n//\n// The states should ideally match relevant states from the JSON spec:\n// https://www.json.org/json-en.html\n//\n// Please note that invalid JSON is not considered/covered, because it\n// is assumed that the resulting JSON will be processed by a standard\n// JSON parser that will detect any invalid JSON.\nexport function fixJson(input: string): string {\n const stack: State[] = ['ROOT'];\n let lastValidIndex = -1;\n let literalStart: number | null = null;\n\n function processValueStart(char: string, i: number, swapState: State) {\n {\n switch (char) {\n case '\"': {\n lastValidIndex = i;\n stack.pop();\n stack.push(swapState);\n stack.push('INSIDE_STRING');\n break;\n }\n\n case 'f':\n case 't':\n case 'n': {\n lastValidIndex = i;\n literalStart = i;\n stack.pop();\n stack.push(swapState);\n stack.push('INSIDE_LITERAL');\n break;\n }\n\n case '-': {\n stack.pop();\n stack.push(swapState);\n stack.push('INSIDE_NUMBER');\n break;\n }\n case '0':\n case '1':\n case '2':\n case '3':\n case '4':\n case '5':\n case '6':\n case '7':\n case '8':\n case '9': {\n lastValidIndex = i;\n stack.pop();\n stack.push(swapState);\n stack.push('INSIDE_NUMBER');\n break;\n }\n\n case '{': {\n lastValidIndex = i;\n stack.pop();\n stack.push(swapState);\n stack.push('INSIDE_OBJECT_START');\n break;\n }\n\n case '[': {\n lastValidIndex = i;\n stack.pop();\n stack.push(swapState);\n stack.push('INSIDE_ARRAY_START');\n break;\n }\n }\n }\n }\n\n function processAfterObjectValue(char: string, i: number) {\n switch (char) {\n case ',': {\n stack.pop();\n stack.push('INSIDE_OBJECT_AFTER_COMMA');\n break;\n }\n case '}': {\n lastValidIndex = i;\n stack.pop();\n break;\n }\n }\n }\n\n function processAfterArrayValue(char: string, i: number) {\n switch (char) {\n case ',': {\n stack.pop();\n stack.push('INSIDE_ARRAY_AFTER_COMMA');\n break;\n }\n case ']': {\n lastValidIndex = i;\n stack.pop();\n break;\n }\n }\n }\n\n for (let i = 0; i < input.length; i++) {\n const char = input[i];\n const currentState = stack[stack.length - 1];\n\n switch (currentState) {\n case 'ROOT':\n processValueStart(char, i, 'FINISH');\n break;\n\n case 'INSIDE_OBJECT_START': {\n switch (char) {\n case '\"': {\n stack.pop();\n stack.push('INSIDE_OBJECT_KEY');\n break;\n }\n case '}': {\n lastValidIndex = i;\n stack.pop();\n break;\n }\n }\n break;\n }\n\n case 'INSIDE_OBJECT_AFTER_COMMA': {\n switch (char) {\n case '\"': {\n stack.pop();\n stack.push('INSIDE_OBJECT_KEY');\n break;\n }\n }\n break;\n }\n\n case 'INSIDE_OBJECT_KEY': {\n switch (char) {\n case '\"': {\n stack.pop();\n stack.push('INSIDE_OBJECT_AFTER_KEY');\n break;\n }\n }\n break;\n }\n\n case 'INSIDE_OBJECT_AFTER_KEY': {\n switch (char) {\n case ':': {\n stack.pop();\n stack.push('INSIDE_OBJECT_BEFORE_VALUE');\n\n break;\n }\n }\n break;\n }\n\n case 'INSIDE_OBJECT_BEFORE_VALUE': {\n processValueStart(char, i, 'INSIDE_OBJECT_AFTER_VALUE');\n break;\n }\n\n case 'INSIDE_OBJECT_AFTER_VALUE': {\n processAfterObjectValue(char, i);\n break;\n }\n\n case 'INSIDE_STRING': {\n switch (char) {\n case '\"': {\n stack.pop();\n lastValidIndex = i;\n break;\n }\n\n case '\\\\': {\n stack.push('INSIDE_STRING_ESCAPE');\n break;\n }\n\n default: {\n lastValidIndex = i;\n }\n }\n\n break;\n }\n\n case 'INSIDE_ARRAY_START': {\n switch (char) {\n case ']': {\n lastValidIndex = i;\n stack.pop();\n break;\n }\n\n default: {\n lastValidIndex = i;\n processValueStart(char, i, 'INSIDE_ARRAY_AFTER_VALUE');\n break;\n }\n }\n break;\n }\n\n case 'INSIDE_ARRAY_AFTER_VALUE': {\n switch (char) {\n case ',': {\n stack.pop();\n stack.push('INSIDE_ARRAY_AFTER_COMMA');\n break;\n }\n\n case ']': {\n lastValidIndex = i;\n stack.pop();\n break;\n }\n\n default: {\n lastValidIndex = i;\n break;\n }\n }\n\n break;\n }\n\n case 'INSIDE_ARRAY_AFTER_COMMA': {\n processValueStart(char, i, 'INSIDE_ARRAY_AFTER_VALUE');\n break;\n }\n\n case 'INSIDE_STRING_ESCAPE': {\n stack.pop();\n lastValidIndex = i;\n\n break;\n }\n\n case 'INSIDE_NUMBER': {\n switch (char) {\n case '0':\n case '1':\n case '2':\n case '3':\n case '4':\n case '5':\n case '6':\n case '7':\n case '8':\n case '9': {\n lastValidIndex = i;\n break;\n }\n\n case 'e':\n case 'E':\n case '-':\n case '.': {\n break;\n }\n\n case ',': {\n stack.pop();\n\n if (stack[stack.length - 1] === 'INSIDE_ARRAY_AFTER_VALUE') {\n processAfterArrayValue(char, i);\n }\n\n if (stack[stack.length - 1] === 'INSIDE_OBJECT_AFTER_VALUE') {\n processAfterObjectValue(char, i);\n }\n\n break;\n }\n\n case '}': {\n stack.pop();\n\n if (stack[stack.length - 1] === 'INSIDE_OBJECT_AFTER_VALUE') {\n processAfterObjectValue(char, i);\n }\n\n break;\n }\n\n case ']': {\n stack.pop();\n\n if (stack[stack.length - 1] === 'INSIDE_ARRAY_AFTER_VALUE') {\n processAfterArrayValue(char, i);\n }\n\n break;\n }\n\n default: {\n stack.pop();\n break;\n }\n }\n\n break;\n }\n\n case 'INSIDE_LITERAL': {\n const partialLiteral = input.substring(literalStart!, i + 1);\n\n if (\n !'false'.startsWith(partialLiteral) &&\n !'true'.startsWith(partialLiteral) &&\n !'null'.startsWith(partialLiteral)\n ) {\n stack.pop();\n\n if (stack[stack.length - 1] === 'INSIDE_OBJECT_AFTER_VALUE') {\n processAfterObjectValue(char, i);\n } else if (stack[stack.length - 1] === 'INSIDE_ARRAY_AFTER_VALUE') {\n processAfterArrayValue(char, i);\n }\n } else {\n lastValidIndex = i;\n }\n\n break;\n }\n }\n }\n\n let result = input.slice(0, lastValidIndex + 1);\n\n for (let i = stack.length - 1; i >= 0; i--) {\n const state = stack[i];\n\n switch (state) {\n case 'INSIDE_STRING': {\n result += '\"';\n break;\n }\n\n case 'INSIDE_OBJECT_KEY':\n case 'INSIDE_OBJECT_AFTER_KEY':\n case 'INSIDE_OBJECT_AFTER_COMMA':\n case 'INSIDE_OBJECT_START':\n case 'INSIDE_OBJECT_BEFORE_VALUE':\n case 'INSIDE_OBJECT_AFTER_VALUE': {\n result += '}';\n break;\n }\n\n case 'INSIDE_ARRAY_START':\n case 'INSIDE_ARRAY_AFTER_COMMA':\n case 'INSIDE_ARRAY_AFTER_VALUE': {\n result += ']';\n break;\n }\n\n case 'INSIDE_LITERAL': {\n const partialLiteral = input.substring(literalStart!, input.length);\n\n if ('true'.startsWith(partialLiteral)) {\n result += 'true'.slice(partialLiteral.length);\n } else if ('false'.startsWith(partialLiteral)) {\n result += 'false'.slice(partialLiteral.length);\n } else if ('null'.startsWith(partialLiteral)) {\n result += 'null'.slice(partialLiteral.length);\n }\n }\n }\n }\n\n return result;\n}\n","import {\n LanguageModelV1FinishReason,\n LanguageModelV1Source,\n} from '@ai-sdk/provider';\nimport { ToolCall, ToolResult } from '@ai-sdk/provider-utils';\nimport { JSONValue } from './types';\n\nexport type DataStreamString =\n `${(typeof DataStreamStringPrefixes)[keyof typeof DataStreamStringPrefixes]}:${string}\\n`;\n\nexport interface DataStreamPart<\n CODE extends string,\n NAME extends string,\n TYPE,\n> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: DataStreamPart<'0', 'text', string> = {\n code: '0',\n name: 'text',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: 'text', value };\n },\n};\n\nconst dataStreamPart: DataStreamPart<'2', 'data', Array<JSONValue>> = {\n code: '2',\n name: 'data',\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: 'data', value };\n },\n};\n\nconst errorStreamPart: DataStreamPart<'3', 'error', string> = {\n code: '3',\n name: 'error',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: 'error', value };\n },\n};\n\nconst messageAnnotationsStreamPart: DataStreamPart<\n '8',\n 'message_annotations',\n Array<JSONValue>\n> = {\n code: '8',\n name: 'message_annotations',\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"message_annotations\" parts expect an array value.');\n }\n\n return { type: 'message_annotations', value };\n },\n};\n\nconst toolCallStreamPart: DataStreamPart<\n '9',\n 'tool_call',\n ToolCall<string, any>\n> = {\n code: '9',\n name: 'tool_call',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('toolCallId' in value) ||\n typeof value.toolCallId !== 'string' ||\n !('toolName' in value) ||\n typeof value.toolName !== 'string' ||\n !('args' in value) ||\n typeof value.args !== 'object'\n ) {\n throw new Error(\n '\"tool_call\" parts expect an object with a \"toolCallId\", \"toolName\", and \"args\" property.',\n );\n }\n\n return {\n type: 'tool_call',\n value: value as unknown as ToolCall<string, any>,\n };\n },\n};\n\nconst toolResultStreamPart: DataStreamPart<\n 'a',\n 'tool_result',\n Omit<ToolResult<string, any, any>, 'args' | 'toolName'>\n> = {\n code: 'a',\n name: 'tool_result',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('toolCallId' in value) ||\n typeof value.toolCallId !== 'string' ||\n !('result' in value)\n ) {\n throw new Error(\n '\"tool_result\" parts expect an object with a \"toolCallId\" and a \"result\" property.',\n );\n }\n\n return {\n type: 'tool_result',\n value: value as unknown as Omit<\n ToolResult<string, any, any>,\n 'args' | 'toolName'\n >,\n };\n },\n};\n\nconst toolCallStreamingStartStreamPart: DataStreamPart<\n 'b',\n 'tool_call_streaming_start',\n { toolCallId: string; toolName: string }\n> = {\n code: 'b',\n name: 'tool_call_streaming_start',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('toolCallId' in value) ||\n typeof value.toolCallId !== 'string' ||\n !('toolName' in value) ||\n typeof value.toolName !== 'string'\n ) {\n throw new Error(\n '\"tool_call_streaming_start\" parts expect an object with a \"toolCallId\" and \"toolName\" property.',\n );\n }\n\n return {\n type: 'tool_call_streaming_start',\n value: value as unknown as { toolCallId: string; toolName: string },\n };\n },\n};\n\nconst toolCallDeltaStreamPart: DataStreamPart<\n 'c',\n 'tool_call_delta',\n { toolCallId: string; argsTextDelta: string }\n> = {\n code: 'c',\n name: 'tool_call_delta',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('toolCallId' in value) ||\n typeof value.toolCallId !== 'string' ||\n !('argsTextDelta' in value) ||\n typeof value.argsTextDelta !== 'string'\n ) {\n throw new Error(\n '\"tool_call_delta\" parts expect an object with a \"toolCallId\" and \"argsTextDelta\" property.',\n );\n }\n\n return {\n type: 'tool_call_delta',\n value: value as unknown as {\n toolCallId: string;\n argsTextDelta: string;\n },\n };\n },\n};\n\nconst finishMessageStreamPart: DataStreamPart<\n 'd',\n 'finish_message',\n {\n finishReason: LanguageModelV1FinishReason;\n // TODO v5 remove usage from finish event (only on step-finish)\n usage?: {\n promptTokens: number;\n completionTokens: number;\n };\n }\n> = {\n code: 'd',\n name: 'finish_message',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('finishReason' in value) ||\n typeof value.finishReason !== 'string'\n ) {\n throw new Error(\n '\"finish_message\" parts expect an object with a \"finishReason\" property.',\n );\n }\n\n const result: {\n finishReason: LanguageModelV1FinishReason;\n usage?: {\n promptTokens: number;\n completionTokens: number;\n };\n } = {\n finishReason: value.finishReason as LanguageModelV1FinishReason,\n };\n\n if (\n 'usage' in value &&\n value.usage != null &&\n typeof value.usage === 'object' &&\n 'promptTokens' in value.usage &&\n 'completionTokens' in value.usage\n ) {\n result.usage = {\n promptTokens:\n typeof value.usage.promptTokens === 'number'\n ? value.usage.promptTokens\n : Number.NaN,\n completionTokens:\n typeof value.usage.completionTokens === 'number'\n ? value.usage.completionTokens\n : Number.NaN,\n };\n }\n\n return {\n type: 'finish_message',\n value: result,\n };\n },\n};\n\nconst finishStepStreamPart: DataStreamPart<\n 'e',\n 'finish_step',\n {\n isContinued: boolean;\n finishReason: LanguageModelV1FinishReason;\n usage?: {\n promptTokens: number;\n completionTokens: number;\n };\n }\n> = {\n code: 'e',\n name: 'finish_step',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('finishReason' in value) ||\n typeof value.finishReason !== 'string'\n ) {\n throw new Error(\n '\"finish_step\" parts expect an object with a \"finishReason\" property.',\n );\n }\n\n const result: {\n isContinued: boolean;\n finishReason: LanguageModelV1FinishReason;\n usage?: {\n promptTokens: number;\n completionTokens: number;\n };\n } = {\n finishReason: value.finishReason as LanguageModelV1FinishReason,\n isContinued: false,\n };\n\n if (\n 'usage' in value &&\n value.usage != null &&\n typeof value.usage === 'object' &&\n 'promptTokens' in value.usage &&\n 'completionTokens' in value.usage\n ) {\n result.usage = {\n promptTokens:\n typeof value.usage.promptTokens === 'number'\n ? value.usage.promptTokens\n : Number.NaN,\n completionTokens:\n typeof value.usage.completionTokens === 'number'\n ? value.usage.completionTokens\n : Number.NaN,\n };\n }\n\n if ('isContinued' in value && typeof value.isContinued === 'boolean') {\n result.isContinued = value.isContinued;\n }\n\n return {\n type: 'finish_step',\n value: result,\n };\n },\n};\n\nconst startStepStreamPart: DataStreamPart<\n 'f',\n 'start_step',\n {\n messageId: string;\n }\n> = {\n code: 'f',\n name: 'start_step',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('messageId' in value) ||\n typeof value.messageId !== 'string'\n ) {\n throw new Error(\n '\"start_step\" parts expect an object with an \"id\" property.',\n );\n }\n\n return {\n type: 'start_step',\n value: {\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst reasoningStreamPart: DataStreamPart<'g', 'reasoning', string> = {\n code: 'g',\n name: 'reasoning',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"reasoning\" parts expect a string value.');\n }\n return { type: 'reasoning', value };\n },\n};\n\nconst sourcePart: DataStreamPart<'h', 'source', LanguageModelV1Source> = {\n code: 'h',\n name: 'source',\n parse: (value: JSONValue) => {\n if (value == null || typeof value !== 'object') {\n throw new Error('\"source\" parts expect a Source object.');\n }\n\n return {\n type: 'source',\n value: value as LanguageModelV1Source,\n };\n },\n};\n\nconst redactedReasoningStreamPart: DataStreamPart<\n 'i',\n 'redacted_reasoning',\n { data: string }\n> = {\n code: 'i',\n name: 'redacted_reasoning',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('data' in value) ||\n typeof value.data !== 'string'\n ) {\n throw new Error(\n '\"redacted_reasoning\" parts expect an object with a \"data\" property.',\n );\n }\n return { type: 'redacted_reasoning', value: { data: value.data } };\n },\n};\n\nconst reasoningSignatureStreamPart: DataStreamPart<\n 'j',\n 'reasoning_signature',\n { signature: string }\n> = {\n code: 'j',\n name: 'reasoning_signature',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('signature' in value) ||\n typeof value.signature !== 'string'\n ) {\n throw new Error(\n '\"reasoning_signature\" parts expect an object with a \"signature\" property.',\n );\n }\n return {\n type: 'reasoning_signature',\n value: { signature: value.signature },\n };\n },\n};\n\nconst fileStreamPart: DataStreamPart<\n 'k',\n 'file',\n {\n data: string; // base64 encoded data\n mimeType: string;\n }\n> = {\n code: 'k',\n name: 'file',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('data' in value) ||\n typeof value.data !== 'string' ||\n !('mimeType' in value) ||\n typeof value.mimeType !== 'string'\n ) {\n throw new Error(\n '\"file\" parts expect an object with a \"data\" and \"mimeType\" property.',\n );\n }\n return { type: 'file', value: value as { data: string; mimeType: string } };\n },\n};\n\nconst dataStreamParts = [\n textStreamPart,\n dataStreamPart,\n errorStreamPart,\n messageAnnotationsStreamPart,\n toolCallStreamPart,\n toolResultStreamPart,\n toolCallStreamingStartStreamPart,\n toolCallDeltaStreamPart,\n finishMessageStreamPart,\n finishStepStreamPart,\n startStepStreamPart,\n reasoningStreamPart,\n sourcePart,\n redactedReasoningStreamPart,\n reasoningSignatureStreamPart,\n fileStreamPart,\n] as const;\n\nexport const dataStreamPartsByCode = Object.fromEntries(\n dataStreamParts.map(part => [part.code, part]),\n) as {\n [K in (typeof dataStreamParts)[number]['code']]: (typeof dataStreamParts)[number];\n};\n\ntype DataStreamParts = (typeof dataStreamParts)[number];\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype DataStreamPartValueType = {\n [P in DataStreamParts as P['name']]: ReturnType<P['parse']>['value'];\n};\n\nexport type DataStreamPartType = ReturnType<DataStreamParts['parse']>;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n * - 6: (OpenAI) tool_call responses\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n * 6: {\"tool_call\": {\"id\": \"tool_0\", \"type\": \"function\", \"function\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}}\n *```\n */\nexport const DataStreamStringPrefixes = Object.fromEntries(\n dataStreamParts.map(part => [part.name, part.code]),\n) as {\n [K in DataStreamParts['name']]: (typeof dataStreamParts)[number]['code'];\n};\n\nexport const validCodes = dataStreamParts.map(part => part.code);\n\n/**\nParses a stream part from a string.\n\n@param line The string to parse.\n@returns The parsed stream part.\n@throws An error if the string cannot be parsed.\n */\nexport const parseDataStreamPart = (line: string): DataStreamPartType => {\n const firstSeparatorIndex = line.indexOf(':');\n\n if (firstSeparatorIndex === -1) {\n throw new Error('Failed to parse stream string. No separator found.');\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof dataStreamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof dataStreamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return dataStreamPartsByCode[code].parse(jsonValue);\n};\n\n/**\nPrepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\nand appends a new line.\n\nIt ensures type-safety for the part type and value.\n */\nexport function formatDataStreamPart<T extends keyof DataStreamPartValueType>(\n type: T,\n value: DataStreamPartValueType[T],\n): DataStreamString {\n const streamPart = dataStreamParts.find(part => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n","import { DataStreamPartType, parseDataStreamPart } from './data-stream-parts';\n\nconst NEWLINE = '\\n'.charCodeAt(0);\n\n// concatenates all the chunks into a single Uint8Array\nfunction concatChunks(chunks: Uint8Array[], totalLength: number) {\n const concatenatedChunks = new Uint8Array(totalLength);\n\n let offset = 0;\n for (const chunk of chunks) {\n concatenatedChunks.set(chunk, offset);\n offset += chunk.length;\n }\n chunks.length = 0;\n\n return concatenatedChunks;\n}\n\nexport async function processDataStream({\n stream,\n onTextPart,\n onReasoningPart,\n onReasoningSignaturePart,\n onRedactedReasoningPart,\n onSourcePart,\n onFilePart,\n onDataPart,\n onErrorPart,\n onToolCallStreamingStartPart,\n onToolCallDeltaPart,\n onToolCallPart,\n onToolResultPart,\n onMessageAnnotationsPart,\n onFinishMessagePart,\n onFinishStepPart,\n onStartStepPart,\n}: {\n stream: ReadableStream<Uint8Array>;\n onTextPart?: (\n streamPart: (DataStreamPartType & { type: 'text' })['value'],\n ) => Promise<void> | void;\n onReasoningPart?: (\n streamPart: (DataStreamPartType & { type: 'reasoning' })['value'],\n ) => Promise<void> | void;\n onReasoningSignaturePart?: (\n streamPart: (DataStreamPartType & { type: 'reasoning_signature' })['value'],\n ) => Promise<void> | void;\n onRedactedReasoningPart?: (\n streamPart: (DataStreamPartType & { type: 'redacted_reasoning' })['value'],\n ) => Promise<void> | void;\n onFilePart?: (\n streamPart: (DataStreamPartType & { type: 'file' })['value'],\n ) => Promise<void> | void;\n onSourcePart?: (\n streamPart: (DataStreamPartType & { type: 'source' })['value'],\n ) => Promise<void> | void;\n onDataPart?: (\n streamPart: (DataStreamPartType & { type: 'data' })['value'],\n ) => Promise<void> | void;\n onErrorPart?: (\n streamPart: (DataStreamPartType & { type: 'error' })['value'],\n ) => Promise<void> | void;\n onToolCallStreamingStartPart?: (\n streamPart: (DataStreamPartType & {\n type: 'tool_call_streaming_start';\n })['value'],\n ) => Promise<void> | void;\n onToolCallDeltaPart?: (\n streamPart: (DataStreamPartType & { type: 'tool_call_delta' })['value'],\n ) => Promise<void> | void;\n onToolCallPart?: (\n streamPart: (DataStreamPartType & { type: 'tool_call' })['value'],\n ) => Promise<void> | void;\n onToolResultPart?: (\n streamPart: (DataStreamPartType & { type: 'tool_result' })['value'],\n ) => Promise<void> | void;\n onMessageAnnotationsPart?: (\n streamPart: (DataStreamPartType & {\n type: 'message_annotations';\n })['value'],\n ) => Promise<void> | void;\n onFinishMessagePart?: (\n streamPart: (DataStreamPartType & { type: 'finish_message' })['value'],\n ) => Promise<void> | void;\n onFinishStepPart?: (\n streamPart: (DataStreamPartType & { type: 'finish_step' })['value'],\n ) => Promise<void> | void;\n onStartStepPart?: (\n streamPart: (DataStreamPartType & { type: 'start_step' })['value'],\n ) => Promise<void> | void;\n}): Promise<void> {\n // implementation note: this slightly more complex algorithm is required\n // to pass the tests in the edge environment.\n\n const reader = stream.getReader();\n const decoder = new TextDecoder();\n const chunks: Uint8Array[] = [];\n let totalLength = 0;\n\n while (true) {\n const { value } = await reader.read();\n\n if (value) {\n chunks.push(value);\n totalLength += value.length;\n if (value[value.length - 1] !== NEWLINE) {\n // if the last character is not a newline, we have not read the whole JSON value\n continue;\n }\n }\n\n if (chunks.length === 0) {\n break; // we have reached the end of the stream\n }\n\n const concatenatedChunks = concatChunks(chunks, totalLength);\n totalLength = 0;\n\n const streamParts = decoder\n .decode(concatenatedChunks, { stream: true })\n .split('\\n')\n .filter(line => line !== '') // splitting leaves an empty string at the end\n .map(parseDataStreamPart);\n\n for (const { type, value } of streamParts) {\n switch (type) {\n case 'text':\n await onTextPart?.(value);\n break;\n case 'reasoning':\n await onReasoningPart?.(value);\n break;\n case 'reasoning_signature':\n await onReasoningSignaturePart?.(valu