Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

StreamProcessor.md

540 lines11.0 KB

1---
2id: StreamProcessor
3title: StreamProcessor
4---
5​
6Defined in: [packages/ai/src/activities/chat/stream/processor.ts:237](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L237)
7​
8StreamProcessor - State machine for processing AI response streams
9​
10Manages the full UIMessage[] conversation and emits events on changes.
11Trusts the adapter contract: adapters emit clean AG-UI events in the
12correct order.
13​
14State tracking:
15- Full message array
16- Per-message stream state (text, tool calls, thinking)
17- Multiple concurrent message streams
18- Tool call completion via TOOL_CALL_END events
19​
20## See
21​
22 - docs/chat-architecture.md#streamprocessor-internal-state — State field reference
23 - docs/chat-architecture.md#adapter-contract — What this class expects from adapters
24​
25## Constructors
26​
27### Constructor
28​
29```ts
30new StreamProcessor(options?): StreamProcessor;
31```
32​
33Defined in: [packages/ai/src/activities/chat/stream/processor.ts:307](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L307)
34​
35#### Parameters
36​
37##### options?
38​
39[`StreamProcessorOptions`](../interfaces/StreamProcessorOptions.md) = `{}`
40​
41#### Returns
42​
43`StreamProcessor`
44​
45## Methods
46​
47### addToolApprovalResponse()
48​
49```ts
50addToolApprovalResponse(approvalId, approved): void;
51```
52​
53Defined in: [packages/ai/src/activities/chat/stream/processor.ts:511](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L511)
54​
55Add an approval response (called by client after handling onApprovalRequest)
56​
57#### Parameters
58​
59##### approvalId
60​
61`string`
62​
63##### approved
64​
65`boolean`
66​
67#### Returns
68​
69`void`
70​
71***
72​
73### addToolResult()
74​
75```ts
76addToolResult(
77 toolCallId,
78 output,
79 error?): void;
80```
81​
82Defined in: [packages/ai/src/activities/chat/stream/processor.ts:460](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L460)
83​
84Add a tool result (called by client after handling onToolCall)
85​
86#### Parameters
87​
88##### toolCallId
89​
90`string`
91​
92##### output
93​
94`any`
95​
96##### error?
97​
98`string`
99​
100#### Returns
101​
102`void`
103​
104***
105​
106### addUserMessage()
107​
108```ts
109addUserMessage(
110 content,
111 id?,
112 metadata?): UIMessage;
113```
114​
115Defined in: [packages/ai/src/activities/chat/stream/processor.ts:387](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L387)
116​
117Add a user message to the conversation.
118Supports both simple string content and multimodal content arrays.
119​
120#### Parameters
121​
122##### content
123​
124`string` \| [`ContentPart`](../type-aliases/ContentPart.md)[]
125​
126The message content (string or array of content parts)
127​
128##### id?
129​
130`string`
131​
132Optional custom message ID (generated if not provided)
133​
134##### metadata?
135​
136`Record`\<`string`, `any`\>
137​
138Optional AG-UI metadata bag
139​
140#### Returns
141​
142[`UIMessage`](../interfaces/UIMessage.md)
143​
144The created UIMessage
145​
146#### Example
147​
148```ts
149// Simple text message
150processor.addUserMessage('Hello!')
151​
152// Multimodal message with image
153processor.addUserMessage([
154 { type: 'text', content: 'What is in this image?' },
155 { type: 'image', source: { type: 'url', value: 'https://example.com/photo.jpg' } }
156])
157​
158// With custom ID
159processor.addUserMessage('Hello!', 'custom-id-123')
160```
161​
162***
163​
164### areAllToolsComplete()
165​
166```ts
167areAllToolsComplete(): boolean;
168```
169​
170Defined in: [packages/ai/src/activities/chat/stream/processor.ts:551](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L551)
171​
172Check if all tool calls in the last assistant message are complete
173Useful for auto-continue logic
174​
175#### Returns
176​
177`boolean`
178​
179***
180​
181### clearMessages()
182​
183```ts
184clearMessages(): void;
185```
186​
187Defined in: [packages/ai/src/activities/chat/stream/processor.ts:642](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L642)
188​
189Clear all messages
190​
191#### Returns
192​
193`void`
194​
195***
196​
197### finalizeStream()
198​
199```ts
200finalizeStream(): void;
201```
202​
203Defined in: [packages/ai/src/activities/chat/stream/processor.ts:3642](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L3642)
204​
205Finalize the stream — complete all pending operations.
206​
207Called when the async iterable ends (stream closed). Acts as the final
208safety net: completes any remaining tool calls, flushes un-emitted text,
209and fires onStreamEnd.
210​
211#### Returns
212​
213`void`
214​
215#### See
216​
217docs/chat-architecture.md#single-shot-text-response — Finalization step
218​
219***
220​
221### getCurrentAssistantMessageId()
222​
223```ts
224getCurrentAssistantMessageId(): string | null;
225```
226​
227Defined in: [packages/ai/src/activities/chat/stream/processor.ts:444](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L444)
228​
229Get the current assistant message ID (if one has been created).
230Returns null if prepareAssistantMessage() was called but no content
231has arrived yet.
232​
233#### Returns
234​
235`string` \| `null`
236​
237***
238​
239### getMessages()
240​
241```ts
242getMessages(): UIMessage<unknown>[];
243```
244​
245Defined in: [packages/ai/src/activities/chat/stream/processor.ts:543](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L543)
246​
247Get current messages
248​
249#### Returns
250​
251[`UIMessage`](../interfaces/UIMessage.md)\<`unknown`\>[]
252​
253***
254​
255### getRecording()
256​
257```ts
258getRecording(): ChunkRecording | null;
259```
260​
261Defined in: [packages/ai/src/activities/chat/stream/processor.ts:3810](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L3810)
262​
263Get the current recording
264​
265#### Returns
266​
267[`ChunkRecording`](../interfaces/ChunkRecording.md) \| `null`
268​
269***
270​
271### getState()
272​
273```ts
274getState(): ProcessorState;
275```
276​
277Defined in: [packages/ai/src/activities/chat/stream/processor.ts:3767](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L3767)
278​
279Get current processor state (aggregated across all messages)
280​
281#### Returns
282​
283[`ProcessorState`](../interfaces/ProcessorState.md)
284​
285***
286​
287### prepareAssistantMessage()
288​
289```ts
290prepareAssistantMessage(): void;
291```
292​
293Defined in: [packages/ai/src/activities/chat/stream/processor.ts:422](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L422)
294​
295Prepare for a new assistant message stream.
296Does NOT create the message immediately -- the message is created lazily
297when the first content-bearing chunk arrives via ensureAssistantMessage().
298This prevents empty assistant messages from flickering in the UI when
299auto-continuation produces no content.
300​
301#### Returns
302​
303`void`
304​
305***
306​
307### prependMessages()
308​
309```ts
310prependMessages(messages): void;
311```
312​
313Defined in: [packages/ai/src/activities/chat/stream/processor.ts:348](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L348)
314​
315Put older UI messages at the front of the conversation.
316​
317Skip a message if its id is already in the list. Keep the existing message.
318Then emit the same messages-change event as `setMessages`.
319​
320Use this for older history pages. The first hydrate window uses `setMessages`.
321​
322#### Parameters
323​
324##### messages
325​
326[`UIMessage`](../interfaces/UIMessage.md)\<`unknown`\>[]
327​
328Older UI messages in insertion order. The first item is the oldest.
329​
330#### Returns
331​
332`void`
333​
334#### Example
335​
336```ts
337processor.setMessages([newest])
338processor.prependMessages([oldest])
339```
340​
341***
342​
343### process()
344​
345```ts
346process(stream): Promise<ProcessorResult>;
347```
348​
349Defined in: [packages/ai/src/activities/chat/stream/processor.ts:669](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L669)
350​
351Process a stream and emit events through handlers
352​
353#### Parameters
354​
355##### stream
356​
357`AsyncIterable`\<`any`\>
358​
359#### Returns
360​
361`Promise`\<[`ProcessorResult`](../interfaces/ProcessorResult.md)\>
362​
363***
364​
365### processChunk()
366​
367```ts
368processChunk(chunk): void;
369```
370​
371Defined in: [packages/ai/src/activities/chat/stream/processor.ts:703](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L703)
372​
373Process a single chunk from the stream.
374​
375Central dispatch for all AG-UI events. Each event type maps to a specific
376handler. Events not listed in the switch are intentionally ignored
377(STATE_SNAPSHOT, STATE_DELTA).
378​
379#### Parameters
380​
381##### chunk
382​
383[`AGUIEvent`](../type-aliases/AGUIEvent.md)
384​
385#### Returns
386​
387`void`
388​
389#### See
390​
391docs/chat-architecture.md#adapter-contract — Expected event types and ordering
392​
393***
394​
395### removeMessagesAfter()
396​
397```ts
398removeMessagesAfter(index): void;
399```
400​
401Defined in: [packages/ai/src/activities/chat/stream/processor.ts:591](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L591)
402​
403Remove messages after a certain index (for reload/retry)
404​
405#### Parameters
406​
407##### index
408​
409`number`
410​
411#### Returns
412​
413`void`
414​
415***
416​
417### reset()
418​
419```ts
420reset(): void;
421```
422​
423Defined in: [packages/ai/src/activities/chat/stream/processor.ts:3842](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L3842)
424​
425Full reset (including messages)
426​
427#### Returns
428​
429`void`
430​
431***
432​
433### setMessages()
434​
435```ts
436setMessages(messages): void;
437```
438​
439Defined in: [packages/ai/src/activities/chat/stream/processor.ts:327](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L327)
440​
441Set the messages array (e.g., from persisted state)
442​
443#### Parameters
444​
445##### messages
446​
447[`UIMessage`](../interfaces/UIMessage.md)\<`unknown`\>[]
448​
449#### Returns
450​
451`void`
452​
453***
454​
455### ~~startAssistantMessage()~~
456​
457```ts
458startAssistantMessage(messageId?): string;
459```
460​
461Defined in: [packages/ai/src/activities/chat/stream/processor.ts:431](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L431)
462​
463#### Parameters
464​
465##### messageId?
466​
467`string`
468​
469#### Returns
470​
471`string`
472​
473#### Deprecated
474​
475Use prepareAssistantMessage() instead. This eagerly creates
476an assistant message which can cause empty message flicker.
477​
478***
479​
480### startRecording()
481​
482```ts
483startRecording(): void;
484```
485​
486Defined in: [packages/ai/src/activities/chat/stream/processor.ts:3797](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L3797)
487​
488Start recording chunks
489​
490#### Returns
491​
492`void`
493​
494***
495​
496### toModelMessages()
497​
498```ts
499toModelMessages(): ModelMessage<
500 | string
501 | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
502 | null>[];
503```
504​
505Defined in: [packages/ai/src/activities/chat/stream/processor.ts:532](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L532)
506​
507Get the conversation as ModelMessages (for sending to LLM)
508​
509#### Returns
510​
511[`ModelMessage`](../interfaces/ModelMessage.md)\<
512 \| `string`
513 \| [`ContentPart`](../type-aliases/ContentPart.md)\<`unknown`, `unknown`, `unknown`, `unknown`, `unknown`\>[]
514 \| `null`\>[]
515​
516***
517​
518### replay()
519​
520```ts
521static replay(recording, options?): Promise<ProcessorResult>;
522```
523​
524Defined in: [packages/ai/src/activities/chat/stream/processor.ts:3863](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/stream/processor.ts#L3863)
525​
526Replay a recording through the processor
527​
528#### Parameters
529​
530##### recording
531​
532[`ChunkRecording`](../interfaces/ChunkRecording.md)
533​
534##### options?
535​
536[`StreamProcessorOptions`](../interfaces/StreamProcessorOptions.md)
537​
538#### Returns
539​
540`Promise`\<[`ProcessorResult`](../interfaces/ProcessorResult.md)\>