| 215 | } |
| 216 | |
| 217 | function normalizeAssistantContent(content) { |
| 218 | if (typeof content === 'string') { |
| 219 | return [{ type: 'text', text: content }] |
| 220 | } |
| 221 | |
| 222 | if (!Array.isArray(content)) { |
| 223 | return [] |
| 224 | } |
| 225 | |
| 226 | return content |
| 227 | .map(block => { |
| 228 | if (block?.type === 'text') { |
| 229 | return { |
| 230 | type: 'text', |
| 231 | text: typeof block.text === 'string' ? block.text : '', |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (block?.type === 'tool_use') { |
| 236 | return { |
| 237 | type: 'tool_use', |
| 238 | id: block.id || `toolu_${Date.now()}`, |
| 239 | name: block.name || 'tool', |
| 240 | input: |
| 241 | typeof block.input === 'object' && block.input !== null ? block.input : {}, |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return null |
| 246 | }) |
| 247 | .filter(Boolean) |
| 248 | } |
| 249 | |
| 250 | function printAssistantText(content) { |
| 251 | const text = content |