* Walk messages and build tool_use_id → tool_name from assistant tool_use * blocks. tool_use always precedes its tool_result (model calls, then result * arrives), so by the time budget enforcement sees a result, its name is known.
(messages: Message[])
| 553 | * arrives), so by the time budget enforcement sees a result, its name is known. |
| 554 | */ |
| 555 | function buildToolNameMap(messages: Message[]): Map<string, string> { |
| 556 | const map = new Map<string, string>() |
| 557 | for (const message of messages) { |
| 558 | if (message.type !== 'assistant') continue |
| 559 | const content = message.message.content |
| 560 | if (!Array.isArray(content)) continue |
| 561 | for (const block of content) { |
| 562 | if (block.type === 'tool_use') { |
| 563 | map.set(block.id, block.name) |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | return map |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Extract candidate tool_result blocks from a single user message: blocks |
no test coverage detected