( toolUseMessages: ToolUseBlock[], assistantMessages: AssistantMessage[], canUseTool: CanUseToolFn, toolUseContext: ToolUseContext, )
| 17 | } |
| 18 | |
| 19 | export async function* runTools( |
| 20 | toolUseMessages: ToolUseBlock[], |
| 21 | assistantMessages: AssistantMessage[], |
| 22 | canUseTool: CanUseToolFn, |
| 23 | toolUseContext: ToolUseContext, |
| 24 | ): AsyncGenerator<MessageUpdate, void> { |
| 25 | let currentContext = toolUseContext |
| 26 | for (const { isConcurrencySafe, blocks } of partitionToolCalls( |
| 27 | toolUseMessages, |
| 28 | currentContext, |
| 29 | )) { |
| 30 | if (isConcurrencySafe) { |
| 31 | const queuedContextModifiers: Record< |
| 32 | string, |
| 33 | ((context: ToolUseContext) => ToolUseContext)[] |
| 34 | > = {} |
| 35 | // Run read-only batch concurrently |
| 36 | for await (const update of runToolsConcurrently( |
| 37 | blocks, |
| 38 | assistantMessages, |
| 39 | canUseTool, |
| 40 | currentContext, |
| 41 | )) { |
| 42 | if (update.contextModifier) { |
| 43 | const { toolUseID, modifyContext } = update.contextModifier |
| 44 | if (!queuedContextModifiers[toolUseID]) { |
| 45 | queuedContextModifiers[toolUseID] = [] |
| 46 | } |
| 47 | queuedContextModifiers[toolUseID].push(modifyContext) |
| 48 | } |
| 49 | yield { |
| 50 | message: update.message, |
| 51 | newContext: currentContext, |
| 52 | } |
| 53 | } |
| 54 | for (const block of blocks) { |
| 55 | const modifiers = queuedContextModifiers[block.id] |
| 56 | if (!modifiers) { |
| 57 | continue |
| 58 | } |
| 59 | for (const modifier of modifiers) { |
| 60 | currentContext = modifier(currentContext) |
| 61 | } |
| 62 | } |
| 63 | yield { newContext: currentContext } |
| 64 | } else { |
| 65 | // Run non-read-only batch serially |
| 66 | for await (const update of runToolsSerially( |
| 67 | blocks, |
| 68 | assistantMessages, |
| 69 | canUseTool, |
| 70 | currentContext, |
| 71 | )) { |
| 72 | if (update.newContext) { |
| 73 | currentContext = update.newContext |
| 74 | } |
| 75 | yield { |
| 76 | message: update.message, |
no test coverage detected