Defined in: tools/tool-calls.ts:41
Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.
Responsibilities:
This class is used internally by the AI.chat() method to handle the automatic tool execution loop. It can also be used independently for custom tool execution logic.
const manager = new ToolCallManager(tools);
// During streaming, accumulate tool calls
for await (const chunk of stream) {
if (chunk.type === "tool_call") {
manager.addToolCallChunk(chunk);
}
}
// After stream completes, execute tools
if (manager.hasToolCalls()) {
const toolResults = yield* manager.executeTools(doneChunk);
messages = [...messages, ...toolResults];
manager.clear();
}
const manager = new ToolCallManager(tools);
// During streaming, accumulate tool calls
for await (const chunk of stream) {
if (chunk.type === "tool_call") {
manager.addToolCallChunk(chunk);
}
}
// After stream completes, execute tools
if (manager.hasToolCalls()) {
const toolResults = yield* manager.executeTools(doneChunk);
messages = [...messages, ...toolResults];
manager.clear();
}
new ToolCallManager(tools): ToolCallManager;
new ToolCallManager(tools): ToolCallManager;
Defined in: tools/tool-calls.ts:45
readonly Tool<ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>, ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>, string>[]
ToolCallManager
addToolCallChunk(chunk): void;
addToolCallChunk(chunk): void;
Defined in: tools/tool-calls.ts:53
Add a tool call chunk to the accumulator Handles streaming tool calls by accumulating arguments
number
{ function: { arguments: string; name: string; }; id: string; type: "function"; }
{ arguments: string; name: string; }
string
string
string
"function"
void
clear(): void;
clear(): void;
Defined in: tools/tool-calls.ts:193
Clear the tool calls map for the next iteration
void
executeTools(doneChunk): AsyncGenerator<ToolResultStreamChunk, ModelMessage<
| string
| ContentPart<unknown, unknown, unknown, unknown>[]
| null>[], void>;
executeTools(doneChunk): AsyncGenerator<ToolResultStreamChunk, ModelMessage<
| string
| ContentPart<unknown, unknown, unknown, unknown>[]
| null>[], void>;
Defined in: tools/tool-calls.ts:111
Execute all tool calls and return tool result messages Also yields tool_result chunks for streaming
AsyncGenerator<ToolResultStreamChunk, ModelMessage< | string | ContentPart<unknown, unknown, unknown, unknown>[] | null>[], void>
getToolCalls(): ToolCall[];
getToolCalls(): ToolCall[];
Defined in: tools/tool-calls.ts:101
Get all complete tool calls (filtered for valid ID and name)
ToolCall[]
hasToolCalls(): boolean;
hasToolCalls(): boolean;
Defined in: tools/tool-calls.ts:94
Check if there are any complete tool calls to execute
boolean
