| id | ToolCallManager |
|---|---|
| title | ToolCallManager |
Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:83
Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.
Responsibilities:
- Accumulates streaming tool call events (ID, name, arguments)
- Validates tool calls (filters out incomplete ones)
- Executes tool
executefunctions with parsed arguments - Emits
TOOL_CALL_ENDevents 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.
const manager = new ToolCallManager(tools);
// During streaming, accumulate tool calls
for await (const chunk of stream) {
if (chunk.type === 'TOOL_CALL_START') {
manager.addToolCallStartEvent(chunk);
} else if (chunk.type === 'TOOL_CALL_ARGS') {
manager.addToolCallArgsEvent(chunk);
}
}
// After stream completes, execute tools
if (manager.hasToolCalls()) {
const toolResults = yield* manager.executeTools(finishEvent);
messages = [...messages, ...toolResults];
manager.clear();
}new ToolCallManager(tools): ToolCallManager;Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:87
readonly Tool<SchemaInput, SchemaInput, string>[]
ToolCallManager
addToolCallArgsEvent(event): void;Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:113
Add a TOOL_CALL_ARGS event to accumulate arguments (AG-UI)
void
addToolCallStartEvent(event): void;Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:94
Add a TOOL_CALL_START event to begin tracking a tool call (AG-UI)
void
clear(): void;Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:262
Clear the tool calls map for the next iteration
void
completeToolCall(event): void;Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:127
Complete a tool call with its final input Called when TOOL_CALL_END is received
void
executeTools(finishEvent): AsyncGenerator<ToolCallEndEvent, ModelMessage<
| string
| ContentPart<unknown, unknown, unknown, unknown, unknown>[]
| null>[], void>;Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:162
Execute all tool calls and return tool result messages Yields TOOL_CALL_END events for streaming
RUN_FINISHED event from the stream
AsyncGenerator<ToolCallEndEvent, ModelMessage<
| string
| ContentPart<unknown, unknown, unknown, unknown, unknown>[]
| null>[], void>
getToolCalls(): ToolCall<unknown>[];Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:151
Get all complete tool calls (filtered for valid ID and name)
ToolCall<unknown>[]
hasToolCalls(): boolean;Defined in: packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:144
Check if there are any complete tool calls to execute
boolean