( blocks: ContentBlock[], agentId: string, update: AgentTextUpdate, )
| 23 | | { type: 'text'; mode: 'replace'; content: string } |
| 24 | |
| 25 | const updateAgentText = ( |
| 26 | blocks: ContentBlock[], |
| 27 | agentId: string, |
| 28 | update: AgentTextUpdate, |
| 29 | ) => { |
| 30 | return updateBlocksRecursively(blocks, agentId, (block) => { |
| 31 | if (block.type !== 'agent') { |
| 32 | return block |
| 33 | } |
| 34 | |
| 35 | const agentBlocks = block.blocks ? [...block.blocks] : [] |
| 36 | const text = update.content ?? '' |
| 37 | |
| 38 | if (update.mode === 'replace') { |
| 39 | const updatedBlocks = [...agentBlocks] |
| 40 | let replaced = false |
| 41 | |
| 42 | for (let i = updatedBlocks.length - 1; i >= 0; i--) { |
| 43 | const entry = updatedBlocks[i] |
| 44 | if (entry.type === 'text') { |
| 45 | replaced = true |
| 46 | if (entry.content === text && block.content === text) { |
| 47 | return block |
| 48 | } |
| 49 | updatedBlocks[i] = { ...entry, content: text } |
| 50 | break |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (!replaced) { |
| 55 | updatedBlocks.push({ type: 'text', content: text }) |
| 56 | } |
| 57 | |
| 58 | return { |
| 59 | ...block, |
| 60 | content: text, |
| 61 | blocks: updatedBlocks, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (!text) { |
| 66 | return block |
| 67 | } |
| 68 | |
| 69 | // Handle native reasoning chunks for agent blocks |
| 70 | if (update.textType === 'reasoning') { |
| 71 | const updatedAgentBlocks = appendNativeReasoningToBlocks(agentBlocks, text) |
| 72 | const updatedContent = (block.content ?? '') + text |
| 73 | return { |
| 74 | ...block, |
| 75 | content: updatedContent, |
| 76 | blocks: updatedAgentBlocks, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // For regular text: first close any open native reasoning block, then use think tag parsing |
| 81 | const blocksWithClosedReasoning = closeNativeReasoningBlock(agentBlocks) |
| 82 | const updatedAgentBlocks = appendTextWithThinkParsingToBlocks( |
no test coverage detected