Open History to see the last commit
1---
2id: StreamProcessor
3title: StreamProcessor
4---
5
6# Class: StreamProcessor
7
8Defined in: [activities/chat/stream/processor.ts:168](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L168)
9
10StreamProcessor - State machine for processing AI response streams
11
12Manages the full UIMessage[] conversation and emits events on changes.
13
14State tracking:
15- Full message array
16- Current assistant message being streamed
17- Text content accumulation
18- Multiple parallel tool calls
19- Tool call completion detection
20
21Tool call completion is detected when:
221. A new tool call starts at a different index
232. Text content arrives
243. Stream ends
25
26## Constructors
27
28### Constructor
29
30```ts
31new StreamProcessor(options): StreamProcessor;
32```
33
34Defined in: [activities/chat/stream/processor.ts:197](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L197)
35
36#### Parameters
37
38##### options
39
40[`StreamProcessorOptions`](../interfaces/StreamProcessorOptions.md) = `{}`
41
42#### Returns
43
44`StreamProcessor`
45
46## Methods
47
48### addToolApprovalResponse()
49
50```ts
51addToolApprovalResponse(approvalId, approved): void;
52```
53
54Defined in: [activities/chat/stream/processor.ts:311](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L311)
55
56Add an approval response (called by client after handling onApprovalRequest)
57
58#### Parameters
59
60##### approvalId
61
62`string`
63
64##### approved
65
66`boolean`
67
68#### Returns
69
70`void`
71
72***
73
74### addToolResult()
75
76```ts
77addToolResult(
78 toolCallId,
79 output,
80 error?): void;
81```
82
83Defined in: [activities/chat/stream/processor.ts:267](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L267)
84
85Add a tool result (called by client after handling onToolCall)
86
87#### Parameters
88
89##### toolCallId
90
91`string`
92
93##### output
94
95`any`
96
97##### error?
98
99`string`
100
101#### Returns
102
103`void`
104
105***
106
107### addUserMessage()
108
109```ts
110addUserMessage(content): UIMessage;
111```
112
113Defined in: [activities/chat/stream/processor.ts:225](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L225)
114
115Add a user message to the conversation
116
117#### Parameters
118
119##### content
120
121`string`
122
123#### Returns
124
125[`UIMessage`](../interfaces/UIMessage.md)
126
127***
128
129### areAllToolsComplete()
130
131```ts
132areAllToolsComplete(): boolean;
133```
134
135Defined in: [activities/chat/stream/processor.ts:342](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L342)
136
137Check if all tool calls in the last assistant message are complete
138Useful for auto-continue logic
139
140#### Returns
141
142`boolean`
143
144***
145
146### clearMessages()
147
148```ts
149clearMessages(): void;
150```
151
152Defined in: [activities/chat/stream/processor.ts:374](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L374)
153
154Clear all messages
155
156#### Returns
157
158`void`
159
160***
161
162### finalizeStream()
163
164```ts
165finalizeStream(): void;
166```
167
168Defined in: [activities/chat/stream/processor.ts:948](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L948)
169
170Finalize the stream - complete all pending operations
171
172#### Returns
173
174`void`
175
176***
177
178### getMessages()
179
180```ts
181getMessages(): UIMessage[];
182```
183
184Defined in: [activities/chat/stream/processor.ts:334](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L334)
185
186Get current messages
187
188#### Returns
189
190[`UIMessage`](../interfaces/UIMessage.md)[]
191
192***
193
194### getRecording()
195
196```ts
197getRecording(): ChunkRecording | null;
198```
199
200Defined in: [activities/chat/stream/processor.ts:1034](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L1034)
201
202Get the current recording
203
204#### Returns
205
206[`ChunkRecording`](../interfaces/ChunkRecording.md) \| `null`
207
208***
209
210### getState()
211
212```ts
213getState(): ProcessorState;
214```
215
216Defined in: [activities/chat/stream/processor.ts:1007](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L1007)
217
218Get current processor state (legacy)
219
220#### Returns
221
222[`ProcessorState`](../interfaces/ProcessorState.md)
223
224***
225
226### process()
227
228```ts
229process(stream): Promise<ProcessorResult>;
230```
231
232Defined in: [activities/chat/stream/processor.ts:387](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L387)
233
234Process a stream and emit events through handlers
235
236#### Parameters
237
238##### stream
239
240`AsyncIterable`\<`any`\>
241
242#### Returns
243
244`Promise`\<[`ProcessorResult`](../interfaces/ProcessorResult.md)\>
245
246***
247
248### processChunk()
249
250```ts
251processChunk(chunk): void;
252```
253
254Defined in: [activities/chat/stream/processor.ts:415](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L415)
255
256Process a single chunk from the stream
257
258#### Parameters
259
260##### chunk
261
262[`StreamChunk`](../type-aliases/StreamChunk.md)
263
264#### Returns
265
266`void`
267
268***
269
270### removeMessagesAfter()
271
272```ts
273removeMessagesAfter(index): void;
274```
275
276Defined in: [activities/chat/stream/processor.ts:366](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L366)
277
278Remove messages after a certain index (for reload/retry)
279
280#### Parameters
281
282##### index
283
284`number`
285
286#### Returns
287
288`void`
289
290***
291
292### reset()
293
294```ts
295reset(): void;
296```
297
298Defined in: [activities/chat/stream/processor.ts:1057](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L1057)
299
300Full reset (including messages)
301
302#### Returns
303
304`void`
305
306***
307
308### setMessages()
309
310```ts
311setMessages(messages): void;
312```
313
314Defined in: [activities/chat/stream/processor.ts:217](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L217)
315
316Set the messages array (e.g., from persisted state)
317
318#### Parameters
319
320##### messages
321
322[`UIMessage`](../interfaces/UIMessage.md)[]
323
324#### Returns
325
326`void`
327
328***
329
330### startAssistantMessage()
331
332```ts
333startAssistantMessage(): string;
334```
335
336Defined in: [activities/chat/stream/processor.ts:243](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L243)
337
338Start streaming a new assistant message
339Returns the message ID
340
341#### Returns
342
343`string`
344
345***
346
347### startRecording()
348
349```ts
350startRecording(): void;
351```
352
353Defined in: [activities/chat/stream/processor.ts:1021](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L1021)
354
355Start recording chunks
356
357#### Returns
358
359`void`
360
361***
362
363### toModelMessages()
364
365```ts
366toModelMessages(): ModelMessage<
367 | string
368 | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
369 | null>[];
370```
371
372Defined in: [activities/chat/stream/processor.ts:323](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L323)
373
374Get the conversation as ModelMessages (for sending to LLM)
375
376#### Returns
377
378[`ModelMessage`](../interfaces/ModelMessage.md)\<
379 \| `string`
380 \| [`ContentPart`](../type-aliases/ContentPart.md)\<`unknown`, `unknown`, `unknown`, `unknown`, `unknown`\>[]
381 \| `null`\>[]
382
383***
384
385### replay()
386
387```ts
388static replay(recording, options?): Promise<ProcessorResult>;
389```
390
391Defined in: [activities/chat/stream/processor.ts:1066](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/activities/chat/stream/processor.ts#L1066)
392
393Replay a recording through the processor
394
395#### Parameters
396
397##### recording
398
399[`ChunkRecording`](../interfaces/ChunkRecording.md)
400
401##### options?
402
403[`StreamProcessorOptions`](../interfaces/StreamProcessorOptions.md)
404
405#### Returns
406
407`Promise`\<[`ProcessorResult`](../interfaces/ProcessorResult.md)\>