| 109 | if (!message.blocks) return message |
| 110 | |
| 111 | const updateBlocksRecursively = ( |
| 112 | blocks: ContentBlock[], |
| 113 | ): ContentBlock[] => { |
| 114 | let foundTarget = false |
| 115 | const result = blocks.map((block) => { |
| 116 | // Handle thinking blocks - just match by thinkingId |
| 117 | if (block.type === 'text' && block.thinkingId === id) { |
| 118 | foundTarget = true |
| 119 | const isExpanded = block.thinkingCollapseState === 'expanded' |
| 120 | return { |
| 121 | ...block, |
| 122 | thinkingCollapseState: isExpanded ? 'preview' as const : 'expanded' as const, |
| 123 | userOpened: !isExpanded, // Mark as user-opened if expanding |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Handle agent blocks |
| 128 | if (block.type === 'agent' && block.agentId === id) { |
| 129 | foundTarget = true |
| 130 | const wasCollapsed = block.isCollapsed ?? false |
| 131 | return { |
| 132 | ...block, |
| 133 | isCollapsed: !wasCollapsed, |
| 134 | userOpened: wasCollapsed, // Mark as user-opened if expanding |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Handle tool blocks |
| 139 | if (block.type === 'tool' && block.toolCallId === id) { |
| 140 | foundTarget = true |
| 141 | const wasCollapsed = block.isCollapsed ?? false |
| 142 | return { |
| 143 | ...block, |
| 144 | isCollapsed: !wasCollapsed, |
| 145 | userOpened: wasCollapsed, // Mark as user-opened if expanding |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Handle agent-list blocks |
| 150 | if (block.type === 'agent-list' && block.id === id) { |
| 151 | foundTarget = true |
| 152 | const wasCollapsed = block.isCollapsed ?? false |
| 153 | return { |
| 154 | ...block, |
| 155 | isCollapsed: !wasCollapsed, |
| 156 | userOpened: wasCollapsed, // Mark as user-opened if expanding |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Recursively update nested blocks inside agent blocks |
| 161 | if (block.type === 'agent' && block.blocks) { |
| 162 | const updatedBlocks = updateBlocksRecursively(block.blocks) |
| 163 | // Only create new block if nested blocks actually changed |
| 164 | if (updatedBlocks !== block.blocks) { |
| 165 | foundTarget = true |
| 166 | return { |
| 167 | ...block, |
| 168 | blocks: updatedBlocks, |