(messages: Message[])
| 143 | * and thinking blocks but not images. |
| 144 | */ |
| 145 | export function stripImagesFromMessages(messages: Message[]): Message[] { |
| 146 | return messages.map(message => { |
| 147 | if (message.type !== 'user') { |
| 148 | return message |
| 149 | } |
| 150 | |
| 151 | const content = message.message.content |
| 152 | if (!Array.isArray(content)) { |
| 153 | return message |
| 154 | } |
| 155 | |
| 156 | let hasMediaBlock = false |
| 157 | const newContent = content.flatMap(block => { |
| 158 | if (block.type === 'image') { |
| 159 | hasMediaBlock = true |
| 160 | return [{ type: 'text' as const, text: '[image]' }] |
| 161 | } |
| 162 | if (block.type === 'document') { |
| 163 | hasMediaBlock = true |
| 164 | return [{ type: 'text' as const, text: '[document]' }] |
| 165 | } |
| 166 | // Also strip images/documents nested inside tool_result content arrays |
| 167 | if (block.type === 'tool_result' && Array.isArray(block.content)) { |
| 168 | let toolHasMedia = false |
| 169 | const newToolContent = block.content.map(item => { |
| 170 | if (item.type === 'image') { |
| 171 | toolHasMedia = true |
| 172 | return { type: 'text' as const, text: '[image]' } |
| 173 | } |
| 174 | if (item.type === 'document') { |
| 175 | toolHasMedia = true |
| 176 | return { type: 'text' as const, text: '[document]' } |
| 177 | } |
| 178 | return item |
| 179 | }) |
| 180 | if (toolHasMedia) { |
| 181 | hasMediaBlock = true |
| 182 | return [{ ...block, content: newToolContent }] |
| 183 | } |
| 184 | } |
| 185 | return [block] |
| 186 | }) |
| 187 | |
| 188 | if (!hasMediaBlock) { |
| 189 | return message |
| 190 | } |
| 191 | |
| 192 | return { |
| 193 | ...message, |
| 194 | message: { |
| 195 | ...message.message, |
| 196 | content: newContent, |
| 197 | }, |
| 198 | } as typeof message |
| 199 | }) |
| 200 | } |
| 201 | |
| 202 | /** |
no outgoing calls
no test coverage detected