| 4689 | * Stops early at maxCount for efficiency |
| 4690 | */ |
| 4691 | export function countToolCalls( |
| 4692 | messages: Message[], |
| 4693 | toolName: string, |
| 4694 | maxCount?: number, |
| 4695 | ): number { |
| 4696 | let count = 0 |
| 4697 | for (const msg of messages) { |
| 4698 | if (!msg) continue |
| 4699 | if (msg.type === 'assistant' && Array.isArray(msg.message.content)) { |
| 4700 | const hasToolUse = msg.message.content.some( |
| 4701 | (block): block is ToolUseBlock => |
| 4702 | block.type === 'tool_use' && block.name === toolName, |
| 4703 | ) |
| 4704 | if (hasToolUse) { |
| 4705 | count++ |
| 4706 | if (maxCount && count >= maxCount) { |
| 4707 | return count |
| 4708 | } |
| 4709 | } |
| 4710 | } |
| 4711 | } |
| 4712 | return count |
| 4713 | } |
| 4714 | |
| 4715 | /** |
| 4716 | * Check if the most recent tool call succeeded (has result without is_error) |