| 56 | } |
| 57 | |
| 58 | function extractToolResultBlock( |
| 59 | message: UserMessage, |
| 60 | ): { isError: boolean; text: string | undefined } | null { |
| 61 | if (!Array.isArray(message.message.content)) { |
| 62 | return null |
| 63 | } |
| 64 | const block = message.message.content.find( |
| 65 | c => c.type === 'tool_result', |
| 66 | ) |
| 67 | if (!block) { |
| 68 | return null |
| 69 | } |
| 70 | if (typeof block.content === 'string') { |
| 71 | return { isError: Boolean(block.is_error), text: block.content } |
| 72 | } |
| 73 | const text = block.content |
| 74 | .map(c => { |
| 75 | if (c.type === 'text') { |
| 76 | return c.text |
| 77 | } |
| 78 | return `[${c.type}]` |
| 79 | }) |
| 80 | .join('\n') |
| 81 | return { isError: Boolean(block.is_error), text } |
| 82 | } |
| 83 | |
| 84 | function ensureRecord(input: unknown): Record<string, unknown> { |
| 85 | if (input && typeof input === 'object' && !Array.isArray(input)) { |