* Recursively updates isCollapsed on all collapsible blocks. * Returns both the updated blocks and whether any changes were made.
( blocks: ContentBlock[], collapsed: boolean, )
| 147 | * Returns both the updated blocks and whether any changes were made. |
| 148 | */ |
| 149 | function updateBlocksRecursively( |
| 150 | blocks: ContentBlock[], |
| 151 | collapsed: boolean, |
| 152 | ): UpdateBlocksResult { |
| 153 | let anyChanged = false |
| 154 | const result = blocks.map((block) => { |
| 155 | if (!isCollapsibleBlock(block)) { |
| 156 | return block |
| 157 | } |
| 158 | |
| 159 | // Handle agent blocks specially due to nested blocks |
| 160 | if (block.type === 'agent') { |
| 161 | const currentCollapsed = getBlockCollapsedState(block) |
| 162 | let updatedBlock = block |
| 163 | let blockChanged = false |
| 164 | |
| 165 | // Check if this block's state needs updating |
| 166 | if (currentCollapsed !== collapsed) { |
| 167 | blockChanged = true |
| 168 | updatedBlock = { |
| 169 | ...block, |
| 170 | isCollapsed: collapsed, |
| 171 | userOpened: !collapsed ? true : block.userOpened, |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Recursively update nested blocks |
| 176 | if (block.blocks) { |
| 177 | const nested = updateBlocksRecursively(block.blocks, collapsed) |
| 178 | if (nested.changed) { |
| 179 | blockChanged = true |
| 180 | updatedBlock = { |
| 181 | ...updatedBlock, |
| 182 | blocks: nested.blocks, |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (blockChanged) { |
| 188 | anyChanged = true |
| 189 | return updatedBlock |
| 190 | } |
| 191 | return block |
| 192 | } |
| 193 | |
| 194 | // Handle all other collapsible blocks (tool, text with thinkingId, agent-list) |
| 195 | const updated = createUpdatedBlock(block, collapsed) |
| 196 | if (updated) { |
| 197 | anyChanged = true |
| 198 | return updated |
| 199 | } |
| 200 | return block |
| 201 | }) |
| 202 | |
| 203 | return { blocks: anyChanged ? result : blocks, changed: anyChanged } |
| 204 | } |
| 205 | |
| 206 | /** |
no test coverage detected