( blocks: ContentBlock[], options: UpdateToolBlockOptions, )
| 590 | * Recursively processes nested agent blocks. |
| 591 | */ |
| 592 | export const updateToolBlockWithOutput = ( |
| 593 | blocks: ContentBlock[], |
| 594 | options: UpdateToolBlockOptions, |
| 595 | ): ContentBlock[] => { |
| 596 | const { toolCallId, toolOutput } = options |
| 597 | |
| 598 | return blocks.map((block) => { |
| 599 | if (block.type === 'tool' && block.toolCallId === toolCallId) { |
| 600 | let output: string |
| 601 | if (block.toolName === 'run_terminal_command') { |
| 602 | const parsed = (toolOutput?.[0] as any)?.value |
| 603 | if (parsed?.stdout || parsed?.stderr) { |
| 604 | output = (parsed.stdout || '') + (parsed.stderr || '') |
| 605 | } else { |
| 606 | output = formatToolOutput(toolOutput) |
| 607 | } |
| 608 | } else { |
| 609 | output = formatToolOutput(toolOutput) |
| 610 | } |
| 611 | return { ...block, output } |
| 612 | } else if (block.type === 'agent' && block.blocks) { |
| 613 | const updatedBlocks = updateToolBlockWithOutput(block.blocks, options) |
| 614 | // Avoid creating new block if nested blocks didn't change |
| 615 | if (isEqual(block.blocks, updatedBlocks)) { |
| 616 | return block |
| 617 | } |
| 618 | return { ...block, blocks: updatedBlocks } |
| 619 | } |
| 620 | return block |
| 621 | }) |
| 622 | } |
no test coverage detected