* Partition tool calls into batches where each batch is either: * 1. A single non-read-only tool, or * 2. Multiple consecutive read-only tools
( toolUseMessages: ToolUseBlock[], toolUseContext: ToolUseContext, )
| 89 | * 2. Multiple consecutive read-only tools |
| 90 | */ |
| 91 | function partitionToolCalls( |
| 92 | toolUseMessages: ToolUseBlock[], |
| 93 | toolUseContext: ToolUseContext, |
| 94 | ): Batch[] { |
| 95 | return toolUseMessages.reduce((acc: Batch[], toolUse) => { |
| 96 | const tool = findToolByName(toolUseContext.options.tools, toolUse.name) |
| 97 | const parsedInput = tool?.inputSchema.safeParse(toolUse.input) |
| 98 | const isConcurrencySafe = parsedInput?.success |
| 99 | ? (() => { |
| 100 | try { |
| 101 | return Boolean(tool?.isConcurrencySafe(parsedInput.data)) |
| 102 | } catch { |
| 103 | // If isConcurrencySafe throws (e.g., due to shell-quote parse failure), |
| 104 | // treat as not concurrency-safe to be conservative |
| 105 | return false |
| 106 | } |
| 107 | })() |
| 108 | : false |
| 109 | if (isConcurrencySafe && acc[acc.length - 1]?.isConcurrencySafe) { |
| 110 | acc[acc.length - 1]!.blocks.push(toolUse) |
| 111 | } else { |
| 112 | acc.push({ isConcurrencySafe, blocks: [toolUse] }) |
| 113 | } |
| 114 | return acc |
| 115 | }, []) |
| 116 | } |
| 117 | |
| 118 | async function* runToolsSerially( |
| 119 | toolUseMessages: ToolUseBlock[], |
no test coverage detected