本页还没有译文
本页 180 段还没译成Русский,下面显示的是英文原文。StreamProcessor
还没翻译译自英文(tanstack.com)本页 180 段都还没译
Defined in: packages/ai/src/activities/chat/stream/processor.ts:265
StreamProcessor - State machine for processing AI response streams
Manages the full UIMessage[] conversation and emits events on changes. Trusts the adapter contract: adapters emit clean AG-UI events in the correct order.
State tracking:
- Full message array
- Per-message stream state (text, tool calls, thinking)
- Multiple concurrent message streams
- Tool call completion via TOOL_CALL_END events
See
- docs/chat-architecture.md#streamprocessor-internal-state — State field reference
- docs/chat-architecture.md#adapter-contract — What this class expects from adapters
Constructors
Constructor
new StreamProcessor(options?): StreamProcessor;Defined in: packages/ai/src/activities/chat/stream/processor.ts:325
Parameters
options?
Returns
StreamProcessor
Methods
addToolApprovalResponse()
addToolApprovalResponse(approvalId, approved): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:529
Add an approval response (called by client after handling onApprovalRequest)
Parameters
approvalId
string
approved
boolean
Returns
void
addToolResult()
addToolResult(
toolCallId,
output,
error?): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:478
Add a tool result (called by client after handling onToolCall)
Parameters
toolCallId
string
output
any
error?
string
Returns
void
addUserMessage()
addUserMessage(
content,
id?,
metadata?): UIMessage;Defined in: packages/ai/src/activities/chat/stream/processor.ts:405
Add a user message to the conversation. Supports both simple string content and multimodal content arrays.
Parameters
content
string | ContentPart[]
The message content (string or array of content parts)
id?
string
Optional custom message ID (generated if not provided)
metadata?
Record<string, any>
Optional AG-UI metadata bag
Returns
The created UIMessage
Example
// Simple text message
processor.addUserMessage('Hello!')
// Multimodal message with image
processor.addUserMessage([
{ type: 'text', content: 'What is in this image?' },
{ type: 'image', source: { type: 'url', value: 'https://example.com/photo.jpg' } }
])
// With custom ID
processor.addUserMessage('Hello!', 'custom-id-123')areAllToolsComplete()
areAllToolsComplete(): boolean;Defined in: packages/ai/src/activities/chat/stream/processor.ts:569
Check if all tool calls in the last assistant message are complete Useful for auto-continue logic
Returns
boolean
clearMessages()
clearMessages(): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:654
Clear all messages
Returns
void
finalizeStream()
finalizeStream(): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:3536
Finalize the stream — complete all pending operations.
Called when the async iterable ends (stream closed). Acts as the final safety net: completes any remaining tool calls, flushes un-emitted text, and fires onStreamEnd.
Returns
void
See
docs/chat-architecture.md#single-shot-text-response — Finalization step
getCurrentAssistantMessageId()
getCurrentAssistantMessageId(): string | null;Defined in: packages/ai/src/activities/chat/stream/processor.ts:462
Get the current assistant message ID (if one has been created). Returns null if prepareAssistantMessage() was called but no content has arrived yet.
Returns
string | null
getMessages()
getMessages(): UIMessage<unknown>[];Defined in: packages/ai/src/activities/chat/stream/processor.ts:561
Get current messages
Returns
UIMessage<unknown>[]
getRecording()
getRecording(): ChunkRecording | null;Defined in: packages/ai/src/activities/chat/stream/processor.ts:3704
Get the current recording
Returns
ChunkRecording | null
getState()
getState(): ProcessorState;Defined in: packages/ai/src/activities/chat/stream/processor.ts:3661
Get current processor state (aggregated across all messages)
Returns
prepareAssistantMessage()
prepareAssistantMessage(): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:440
Prepare for a new assistant message stream. Does NOT create the message immediately -- the message is created lazily when the first content-bearing chunk arrives via ensureAssistantMessage(). This prevents empty assistant messages from flickering in the UI when auto-continuation produces no content.
Returns
void
prependMessages()
prependMessages(messages): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:366
Put older UI messages at the front of the conversation.
Skip a message if its id is already in the list. Keep the existing message.
Then emit the same messages-change event as setMessages.
Use this for older history pages. The first hydrate window uses setMessages.
Parameters
messages
UIMessage<unknown>[]
Older UI messages in insertion order. The first item is the oldest.
Returns
void
Example
processor.setMessages([newest])
processor.prependMessages([oldest])process()
process(stream): Promise<ProcessorResult>;Defined in: packages/ai/src/activities/chat/stream/processor.ts:679
Process a stream and emit events through handlers
Parameters
stream
AsyncIterable<any>
Returns
Promise<ProcessorResult>
processChunk()
processChunk(chunk): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:713
Process a single chunk from the stream.
Central dispatch for all AG-UI events. Each event type maps to a specific handler. Events not listed in the switch are intentionally ignored (STATE_SNAPSHOT, STATE_DELTA).
Parameters
chunk
Returns
void
See
docs/chat-architecture.md#adapter-contract — Expected event types and ordering
removeMessagesAfter()
removeMessagesAfter(index): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:609
Remove messages after a certain index (for reload/retry)
Parameters
index
number
Returns
void
reset()
reset(): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:3734
Full reset (including messages)
Returns
void
setMessages()
setMessages(messages): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:345
Set the messages array (e.g., from persisted state)
Parameters
messages
UIMessage<unknown>[]
Returns
void
startAssistantMessage()
startAssistantMessage(messageId?): string;Defined in: packages/ai/src/activities/chat/stream/processor.ts:449
Parameters
messageId?
string
Returns
string
Deprecated
Use prepareAssistantMessage() instead. This eagerly creates an assistant message which can cause empty message flicker.
startRecording()
startRecording(): void;Defined in: packages/ai/src/activities/chat/stream/processor.ts:3691
Start recording chunks
Returns
void
toModelMessages()
toModelMessages(): ModelMessage<
| string
| ContentPart<unknown, unknown, unknown, unknown, unknown>[]
| null>[];Defined in: packages/ai/src/activities/chat/stream/processor.ts:550
Get the conversation as ModelMessages (for sending to LLM)
Returns
ModelMessage<
| string
| ContentPart<unknown, unknown, unknown, unknown, unknown>[]
| null>[]
replay()
static replay(recording, options?): Promise<ProcessorResult>;Defined in: packages/ai/src/activities/chat/stream/processor.ts:3755
Replay a recording through the processor
Parameters
recording
options?
Returns
Promise<ProcessorResult>