Defined in: activities/chat/tools/tool-calls.ts:46
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_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: activities/chat/tools/tool-calls.ts:50
readonly Tool<SchemaInput, SchemaInput, string>[]
ToolCallManager
addToolCallArgsEvent(event): void;
Defined in: activities/chat/tools/tool-calls.ts:72
Add a TOOL_CALL_ARGS event to accumulate arguments (AG-UI)
void
addToolCallStartEvent(event): void;
Defined in: activities/chat/tools/tool-calls.ts:57
Add a TOOL_CALL_START event to begin tracking a tool call (AG-UI)
void
clear(): void;
Defined in: activities/chat/tools/tool-calls.ts:215
Clear the tool calls map for the next iteration
void
completeToolCall(event): void;
Defined in: activities/chat/tools/tool-calls.ts:86
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: activities/chat/tools/tool-calls.ts:118
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[];
Defined in: activities/chat/tools/tool-calls.ts:107
Get all complete tool calls (filtered for valid ID and name)
ToolCall[]
hasToolCalls(): boolean;
Defined in: activities/chat/tools/tool-calls.ts:100
Check if there are any complete tool calls to execute
boolean