* Append a fully-merged part to the message. * * - ContentPart -> message.content * - ToolCall -> message.toolCalls (the `_streamIndex` routing key is * registered in the map and stripped before storage). * - ToolCallPart -> ignored (orphaned delta without a matching pending
( message: Message, part: StreamedMessagePart, toolCallIndexMap: Map<number | string, number>, )
| 292 | * - ToolCallPart -> ignored (orphaned delta without a matching pending call) |
| 293 | */ |
| 294 | function flushPart( |
| 295 | message: Message, |
| 296 | part: StreamedMessagePart, |
| 297 | toolCallIndexMap: Map<number | string, number>, |
| 298 | ): void { |
| 299 | if (isContentPart(part)) { |
| 300 | message.content.push(part); |
| 301 | return; |
| 302 | } |
| 303 | if (isToolCall(part)) { |
| 304 | const streamIndex = part._streamIndex; |
| 305 | const stored: StoredToolCall = { |
| 306 | type: 'function', |
| 307 | id: part.id, |
| 308 | name: part.name, |
| 309 | arguments: part.arguments, |
| 310 | extras: part.extras, |
| 311 | }; |
| 312 | const ordinal = message.toolCalls.length; |
| 313 | message.toolCalls.push(stored as ToolCall); |
| 314 | if (streamIndex !== undefined) { |
| 315 | toolCallIndexMap.set(streamIndex, ordinal); |
| 316 | } |
| 317 | } |
| 318 | // ToolCallPart: orphaned delta — silently ignore. |
| 319 | } |
| 320 | |
| 321 | function formatFinishReasonHint(stream: StreamedMessage): string { |
| 322 | if (stream.finishReason === null && stream.rawFinishReason === null) return ''; |
no test coverage detected