(
blocks: ContentBlock[],
delta: { type: 'text' | 'reasoning'; text: string },
)
| 390 | } |
| 391 | |
| 392 | export const appendTextToRootStream = ( |
| 393 | blocks: ContentBlock[], |
| 394 | delta: { type: 'text' | 'reasoning'; text: string }, |
| 395 | ) => { |
| 396 | if (!delta.text) { |
| 397 | return blocks |
| 398 | } |
| 399 | |
| 400 | // For reasoning type (from native reasoning_chunk events), use original behavior |
| 401 | if (delta.type === 'reasoning') { |
| 402 | const nextBlocks = [...blocks] |
| 403 | const lastBlock = nextBlocks[nextBlocks.length - 1] |
| 404 | |
| 405 | if ( |
| 406 | lastBlock && |
| 407 | lastBlock.type === 'text' && |
| 408 | lastBlock.textType === 'reasoning' |
| 409 | ) { |
| 410 | const updatedBlock: ContentBlock = { |
| 411 | ...lastBlock, |
| 412 | content: lastBlock.content + delta.text, |
| 413 | } |
| 414 | nextBlocks[nextBlocks.length - 1] = updatedBlock |
| 415 | return nextBlocks |
| 416 | } |
| 417 | |
| 418 | const newBlock: ContentBlock = { |
| 419 | type: 'text', |
| 420 | content: delta.text, |
| 421 | textType: 'reasoning', |
| 422 | thinkingCollapseState: 'preview', |
| 423 | thinkingId: generateThinkingId(), |
| 424 | } |
| 425 | |
| 426 | return [...nextBlocks, newBlock] |
| 427 | } |
| 428 | |
| 429 | // For text type: first close any open native reasoning block, then parse for <think> tags |
| 430 | const blocksWithClosedReasoning = closeNativeReasoningBlock(blocks) |
| 431 | return appendTextWithThinkParsingToBlocks(blocksWithClosedReasoning, delta.text) |
| 432 | } |
| 433 | |
| 434 | export const appendTextToAgentBlock = ( |
| 435 | blocks: ContentBlock[], |
no test coverage detected