Open History to see the last commit
1---
2id: ToolCallManager
3title: ToolCallManager
4---
5
6# Class: ToolCallManager
7
8Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:83](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L83)
9
10Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.
11
12Responsibilities:
13- Accumulates streaming tool call events (ID, name, arguments)
14- Validates tool calls (filters out incomplete ones)
15- Executes tool `execute` functions with parsed arguments
16- Emits `TOOL_CALL_END` events for client visibility
17- Returns tool result messages for conversation history
18
19This class is used internally by the AI.chat() method to handle the automatic
20tool execution loop. It can also be used independently for custom tool execution logic.
21
22## Example
23
24```typescript
25const manager = new ToolCallManager(tools);
26
27// During streaming, accumulate tool calls
28for await (const chunk of stream) {
29 if (chunk.type === 'TOOL_CALL_START') {
30 manager.addToolCallStartEvent(chunk);
31 } else if (chunk.type === 'TOOL_CALL_ARGS') {
32 manager.addToolCallArgsEvent(chunk);
33 }
34}
35
36// After stream completes, execute tools
37if (manager.hasToolCalls()) {
38 const toolResults = yield* manager.executeTools(finishEvent);
39 messages = [...messages, ...toolResults];
40 manager.clear();
41}
42```
43
44## Constructors
45
46### Constructor
47
48```ts
49new ToolCallManager(tools): ToolCallManager;
50```
51
52Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:87](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L87)
53
54#### Parameters
55
56##### tools
57
58readonly [`Tool`](../interfaces/Tool.md)\<[`SchemaInput`](../type-aliases/SchemaInput.md), [`SchemaInput`](../type-aliases/SchemaInput.md), `string`\>[]
59
60#### Returns
61
62`ToolCallManager`
63
64## Methods
65
66### addToolCallArgsEvent()
67
68```ts
69addToolCallArgsEvent(event): void;
70```
71
72Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:113](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L113)
73
74Add a TOOL_CALL_ARGS event to accumulate arguments (AG-UI)
75
76#### Parameters
77
78##### event
79
80[`ToolCallArgsEvent`](../interfaces/ToolCallArgsEvent.md)
81
82#### Returns
83
84`void`
85
86***
87
88### addToolCallStartEvent()
89
90```ts
91addToolCallStartEvent(event): void;
92```
93
94Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:94](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L94)
95
96Add a TOOL_CALL_START event to begin tracking a tool call (AG-UI)
97
98#### Parameters
99
100##### event
101
102[`ToolCallStartEvent`](../interfaces/ToolCallStartEvent.md)
103
104#### Returns
105
106`void`
107
108***
109
110### clear()
111
112```ts
113clear(): void;
114```
115
116Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:262](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L262)
117
118Clear the tool calls map for the next iteration
119
120#### Returns
121
122`void`
123
124***
125
126### completeToolCall()
127
128```ts
129completeToolCall(event): void;
130```
131
132Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:127](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L127)
133
134Complete a tool call with its final input
135Called when TOOL_CALL_END is received
136
137#### Parameters
138
139##### event
140
141[`ToolCallEndEvent`](../interfaces/ToolCallEndEvent.md)
142
143#### Returns
144
145`void`
146
147***
148
149### executeTools()
150
151```ts
152executeTools(finishEvent): AsyncGenerator<ToolCallEndEvent, ModelMessage<
153 | string
154 | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
155| null>[], void>;
156```
157
158Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:162](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L162)
159
160Execute all tool calls and return tool result messages
161Yields TOOL_CALL_END events for streaming
162
163#### Parameters
164
165##### finishEvent
166
167[`RunFinishedEvent`](../interfaces/RunFinishedEvent.md)
168
169RUN_FINISHED event from the stream
170
171#### Returns
172
173`AsyncGenerator`\<[`ToolCallEndEvent`](../interfaces/ToolCallEndEvent.md), [`ModelMessage`](../interfaces/ModelMessage.md)\<
174 \| `string`
175 \| [`ContentPart`](../type-aliases/ContentPart.md)\<`unknown`, `unknown`, `unknown`, `unknown`, `unknown`\>[]
176 \| `null`\>[], `void`\>
177
178***
179
180### getToolCalls()
181
182```ts
183getToolCalls(): ToolCall<unknown>[];
184```
185
186Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:151](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L151)
187
188Get all complete tool calls (filtered for valid ID and name)
189
190#### Returns
191
192[`ToolCall`](../interfaces/ToolCall.md)\<`unknown`\>[]
193
194***
195
196### hasToolCalls()
197
198```ts
199hasToolCalls(): boolean;
200```
201
202Defined in: [packages/typescript/ai/src/activities/chat/tools/tool-calls.ts:144](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L144)
203
204Check if there are any complete tool calls to execute
205
206#### Returns
207
208`boolean`