Class: ToolCallManager#
Defined in: activities/chat/tools/tool-calls.ts:51
Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.
Responsibilities:
- Accumulates streaming tool call chunks (ID, name, arguments)
- Validates tool calls (filters out incomplete ones)
- Executes tool
executefunctions with parsed arguments - Emits
tool_resultchunks for client visibility - Returns tool result messages for conversation history
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.
Example#
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();
}
Constructors#
Constructor#
new ToolCallManager(tools): ToolCallManager;
Defined in: activities/chat/tools/tool-calls.ts:55
Parameters#
tools#
readonly Tool<ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>, ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>, string>[]
Returns#
ToolCallManager
Methods#
addToolCallChunk()#
addToolCallChunk(chunk): void;
Defined in: activities/chat/tools/tool-calls.ts:63
Add a tool call chunk to the accumulator Handles streaming tool calls by accumulating arguments
Parameters#
chunk#
index#
number
toolCall#
{
function: {
arguments: string;
name: string;
};
id: string;
type: "function";
}
toolCall.function#
{
arguments: string;
name: string;
}
toolCall.function.arguments#
string
toolCall.function.name#
string
toolCall.id#
string
toolCall.type#
"function"
Returns#
void
clear()#
clear(): void;
Defined in: activities/chat/tools/tool-calls.ts:208
Clear the tool calls map for the next iteration
Returns#
void
executeTools()#
executeTools(doneChunk): AsyncGenerator<ToolResultStreamChunk, ModelMessage<
| string
| ContentPart<unknown, unknown, unknown, unknown, unknown>[]
| null>[], void>;
Defined in: activities/chat/tools/tool-calls.ts:121
Execute all tool calls and return tool result messages Also yields tool_result chunks for streaming
Parameters#
doneChunk#
Returns#
AsyncGenerator<ToolResultStreamChunk, ModelMessage<
| string
| ContentPart<unknown, unknown, unknown, unknown, unknown>[]
| null>[], void>
getToolCalls()#
getToolCalls(): ToolCall[];
Defined in: activities/chat/tools/tool-calls.ts:111
Get all complete tool calls (filtered for valid ID and name)
Returns#
ToolCall[]
hasToolCalls()#
hasToolCalls(): boolean;
Defined in: activities/chat/tools/tool-calls.ts:104
Check if there are any complete tool calls to execute
Returns#
boolean