( blocks: ContentBlock[], handlers: BlockProcessorHandlers, )
| 117 | * @returns An array of ReactNode elements |
| 118 | */ |
| 119 | export function processBlocks( |
| 120 | blocks: ContentBlock[], |
| 121 | handlers: BlockProcessorHandlers, |
| 122 | ): ReactNode[] { |
| 123 | const nodes: ReactNode[] = [] |
| 124 | |
| 125 | for (let i = 0; i < blocks.length; ) { |
| 126 | const block = blocks[i] |
| 127 | |
| 128 | // Handle reasoning text blocks (thinking) |
| 129 | if (isReasoningTextBlock(block)) { |
| 130 | const start = i |
| 131 | const reasoningBlocks: TextContentBlock[] = [] |
| 132 | while (i < blocks.length) { |
| 133 | const currentBlock = blocks[i] |
| 134 | if (!isReasoningTextBlock(currentBlock)) break |
| 135 | reasoningBlocks.push(currentBlock) |
| 136 | i++ |
| 137 | } |
| 138 | |
| 139 | const node = handlers.onReasoningGroup(reasoningBlocks, start) |
| 140 | if (node !== null) { |
| 141 | nodes.push(node) |
| 142 | } |
| 143 | continue |
| 144 | } |
| 145 | |
| 146 | // Handle image blocks |
| 147 | if (isImageBlock(block)) { |
| 148 | if (handlers.onImageBlock) { |
| 149 | const node = handlers.onImageBlock(block, i) |
| 150 | if (node !== null) { |
| 151 | nodes.push(node) |
| 152 | } |
| 153 | } |
| 154 | i++ |
| 155 | continue |
| 156 | } |
| 157 | |
| 158 | // Handle tool blocks |
| 159 | if (block.type === 'tool') { |
| 160 | const start = i |
| 161 | const { group: toolBlocks, nextIndex } = groupConsecutiveToolBlocks( |
| 162 | blocks, |
| 163 | i, |
| 164 | ) |
| 165 | i = nextIndex |
| 166 | |
| 167 | const node = handlers.onToolGroup(toolBlocks, start, nextIndex) |
| 168 | if (node !== null) { |
| 169 | nodes.push(node) |
| 170 | } |
| 171 | continue |
| 172 | } |
| 173 | |
| 174 | // Handle agent blocks |
| 175 | if (block.type === 'agent') { |
| 176 | if (isImplementorAgent(block)) { |
no test coverage detected