Open History to see the last commit
1---
2id: ToolCallManager
3title: ToolCallManager
4---
5
6# Class: ToolCallManager
7
8Defined in: [activities/chat/tools/tool-calls.ts:51](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L51)
9
10Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.
11
12Responsibilities:
13- Accumulates streaming tool call chunks (ID, name, arguments)
14- Validates tool calls (filters out incomplete ones)
15- Executes tool `execute` functions with parsed arguments
16- Emits `tool_result` chunks 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") {
30 manager.addToolCallChunk(chunk);
31 }
32}
33
34// After stream completes, execute tools
35if (manager.hasToolCalls()) {
36 const toolResults = yield* manager.executeTools(doneChunk);
37 messages = [...messages, ...toolResults];
38 manager.clear();
39}
40```
41
42## Constructors
43
44### Constructor
45
46```ts
47new ToolCallManager(tools): ToolCallManager;
48```
49
50Defined in: [activities/chat/tools/tool-calls.ts:55](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L55)
51
52#### Parameters
53
54##### tools
55
56readonly [`Tool`](../interfaces/Tool.md)\<`ZodType`\<`unknown`, `unknown`, `$ZodTypeInternals`\<`unknown`, `unknown`\>\>, `ZodType`\<`unknown`, `unknown`, `$ZodTypeInternals`\<`unknown`, `unknown`\>\>, `string`\>[]
57
58#### Returns
59
60`ToolCallManager`
61
62## Methods
63
64### addToolCallChunk()
65
66```ts
67addToolCallChunk(chunk): void;
68```
69
70Defined in: [activities/chat/tools/tool-calls.ts:63](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L63)
71
72Add a tool call chunk to the accumulator
73Handles streaming tool calls by accumulating arguments
74
75#### Parameters
76
77##### chunk
78
79###### index
80
81`number`
82
83###### toolCall
84
85\{
86 `function`: \{
87 `arguments`: `string`;
88 `name`: `string`;
89 \};
90 `id`: `string`;
91 `type`: `"function"`;
92\}
93
94###### toolCall.function
95
96\{
97 `arguments`: `string`;
98 `name`: `string`;
99\}
100
101###### toolCall.function.arguments
102
103`string`
104
105###### toolCall.function.name
106
107`string`
108
109###### toolCall.id
110
111`string`
112
113###### toolCall.type
114
115`"function"`
116
117#### Returns
118
119`void`
120
121***
122
123### clear()
124
125```ts
126clear(): void;
127```
128
129Defined in: [activities/chat/tools/tool-calls.ts:208](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L208)
130
131Clear the tool calls map for the next iteration
132
133#### Returns
134
135`void`
136
137***
138
139### executeTools()
140
141```ts
142executeTools(doneChunk): AsyncGenerator<ToolResultStreamChunk, ModelMessage<
143 | string
144 | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
145| null>[], void>;
146```
147
148Defined in: [activities/chat/tools/tool-calls.ts:121](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L121)
149
150Execute all tool calls and return tool result messages
151Also yields tool_result chunks for streaming
152
153#### Parameters
154
155##### doneChunk
156
157[`DoneStreamChunk`](../interfaces/DoneStreamChunk.md)
158
159#### Returns
160
161`AsyncGenerator`\<[`ToolResultStreamChunk`](../interfaces/ToolResultStreamChunk.md), [`ModelMessage`](../interfaces/ModelMessage.md)\<
162 \| `string`
163 \| [`ContentPart`](../type-aliases/ContentPart.md)\<`unknown`, `unknown`, `unknown`, `unknown`, `unknown`\>[]
164 \| `null`\>[], `void`\>
165
166***
167
168### getToolCalls()
169
170```ts
171getToolCalls(): ToolCall[];
172```
173
174Defined in: [activities/chat/tools/tool-calls.ts:111](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L111)
175
176Get all complete tool calls (filtered for valid ID and name)
177
178#### Returns
179
180[`ToolCall`](../interfaces/ToolCall.md)[]
181
182***
183
184### hasToolCalls()
185
186```ts
187hasToolCalls(): boolean;
188```
189
190Defined in: [activities/chat/tools/tool-calls.ts:104](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/tools/tool-calls.ts#L104)
191
192Check if there are any complete tool calls to execute
193
194#### Returns
195
196`boolean`