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

StreamProcessor.md

407 lines7.6 KB

Class: StreamProcessor

Defined in: activities/chat/stream/processor.ts:168

StreamProcessor - State machine for processing AI response streams

Manages the full UIMessage[] conversation and emits events on changes.

State tracking:

  • Full message array
  • Current assistant message being streamed
  • Text content accumulation
  • Multiple parallel tool calls
  • Tool call completion detection

Tool call completion is detected when:

  1. A new tool call starts at a different index
  2. Text content arrives
  3. Stream ends

Constructors

Constructor

new StreamProcessor(options): StreamProcessor;

Defined in: activities/chat/stream/processor.ts:197

Parameters

options

StreamProcessorOptions = {}

Returns

StreamProcessor

Methods

addToolApprovalResponse()

addToolApprovalResponse(approvalId, approved): void;

Defined in: activities/chat/stream/processor.ts:311

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: activities/chat/stream/processor.ts:267

Add a tool result (called by client after handling onToolCall)

Parameters

toolCallId

string

output

any

error?

string

Returns

void


addUserMessage()

addUserMessage(content): UIMessage;

Defined in: activities/chat/stream/processor.ts:225

Add a user message to the conversation

Parameters

content

string

Returns

UIMessage


areAllToolsComplete()

areAllToolsComplete(): boolean;

Defined in: activities/chat/stream/processor.ts:342

Check if all tool calls in the last assistant message are complete Useful for auto-continue logic

Returns

boolean


clearMessages()

clearMessages(): void;

Defined in: activities/chat/stream/processor.ts:374

Clear all messages

Returns

void


finalizeStream()

finalizeStream(): void;

Defined in: activities/chat/stream/processor.ts:948

Finalize the stream - complete all pending operations

Returns

void


getMessages()

getMessages(): UIMessage[];

Defined in: activities/chat/stream/processor.ts:334

Get current messages

Returns

UIMessage[]


getRecording()

getRecording(): ChunkRecording | null;

Defined in: activities/chat/stream/processor.ts:1034

Get the current recording

Returns

ChunkRecording | null


getState()

getState(): ProcessorState;

Defined in: activities/chat/stream/processor.ts:1007

Get current processor state (legacy)

Returns

ProcessorState


process()

process(stream): Promise<ProcessorResult>;

Defined in: activities/chat/stream/processor.ts:387

Process a stream and emit events through handlers

Parameters

stream

AsyncIterable<any>

Returns

Promise<ProcessorResult>


processChunk()

processChunk(chunk): void;

Defined in: activities/chat/stream/processor.ts:415

Process a single chunk from the stream

Parameters

chunk

StreamChunk

Returns

void


removeMessagesAfter()

removeMessagesAfter(index): void;

Defined in: activities/chat/stream/processor.ts:366

Remove messages after a certain index (for reload/retry)

Parameters

index

number

Returns

void


reset()

reset(): void;

Defined in: activities/chat/stream/processor.ts:1057

Full reset (including messages)

Returns

void


setMessages()

setMessages(messages): void;

Defined in: activities/chat/stream/processor.ts:217

Set the messages array (e.g., from persisted state)

Parameters

messages

UIMessage[]

Returns

void


startAssistantMessage()

startAssistantMessage(): string;

Defined in: activities/chat/stream/processor.ts:243

Start streaming a new assistant message Returns the message ID

Returns

string


startRecording()

startRecording(): void;

Defined in: activities/chat/stream/processor.ts:1021

Start recording chunks

Returns

void


toModelMessages()

toModelMessages(): ModelMessage<
  | string
  | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
  | null>[];

Defined in: activities/chat/stream/processor.ts:323

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: activities/chat/stream/processor.ts:1066

Replay a recording through the processor

Parameters

recording

ChunkRecording

options?

StreamProcessorOptions

Returns

Promise<ProcessorResult>