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

chat-architecture.md

625 lines33.4 KB

Chat Stream Architecture

Canonical reference for AG-UI chunk processing. The StreamProcessor class cross-references sections of this document. When modifying stream handling behavior, update this document first, then code.


Table of Contents

  1. System Overview
  2. Wire format (HTTP body)
  3. Single-Shot Text Response
  4. Single-Shot Tool Call Response
  5. Parallel Tool Calls (Single Shot)
  6. Text-Then-Tool Interleaving (Single Shot)
  7. Thinking/Reasoning Content
  8. Tool Results and the TOOL_CALL_END Dual Role
  9. Client Tools and Approval Flows
  10. Multi-Iteration Agent Loop
  11. Adapter Contract
  12. StreamProcessor Internal State
  13. UIMessage Part Ordering Invariants
  14. Testing Strategy

System Overview

┌──────────────────────────────────────────────────────────────┐
│                        chat() function                       │
│                                                              │
│   ┌────────────┐    AG-UI events     ┌──────────────────┐    │
│   │   Adapter  │ ──────────────────> │   TextEngine     │    │
│   │ (provider) │                     │ (agent loop)     │    │
│   └────────────┘                     └────────┬─────────┘    │
│                                               │              │
│                                       yield AG-UI events     │
│                                               │              │
└───────────────────────────────────────────────┼──────────────┘
                                                │
                                                v
                                      ┌──────────────────┐
                                      │ StreamProcessor  │
                                      │ (UI state owner) │
                                      └──────────────────┘
                                                │
                                                v
                                        UIMessage[] updates
                                       (onMessagesChange)

Two components consume the AG-UI event stream:

  • TextEngine (server, chat/index.ts) -- Orchestrates the agent loop. Accumulates text and tool calls for its own bookkeeping, decides whether to execute tools and re-invoke the adapter. Yields all events downstream.
  • StreamProcessor (client, chat/stream/processor.ts) -- The single source of truth for UIMessage[] state. Consumes AG-UI events and maintains the conversation as an array of UIMessage objects with typed parts.

Both trust the adapter to emit events in the correct order. The processor does not attempt error recovery for out-of-order events.


Wire format (HTTP body)

The HTTP body posted by @tanstack/ai-client to a chat() endpoint is the AG-UI RunAgentInput shape from @ag-ui/core:

{
  "threadId": "thread-...",
  "runId": "run-...",
  "state": {},
  "messages": [...],
  "tools": [...],
  "context": [],
  "forwardedProps": {}
}

Each entry in messages is either:

  • A TanStack anchor — a UIMessage with its canonical parts array, augmented with AG-UI mirror fields (content for system/user/assistant; toolCalls for assistant) so AG-UI Zod parsing succeeds.
  • An AG-UI fan-out duplicate — a {role:'tool',...} or {role:'reasoning',...} entry generated from each ToolResultPart/ThinkingPart on the assistant anchor. Strict AG-UI server consumers walk these role-based messages directly.

On the server, chatParamsFromRequestBody validates the body and returns the parsed fields. convertMessagesToModelMessages (called inside chat()) handles dedup: when an anchor's parts already contain a tool-result, the matching fan-out tool message is dropped from the ModelMessage[] fed to the LLM. reasoning and activity messages are dropped (no ModelMessage equivalent today); developer messages collapse to system.

For the migration story when upgrading, see docs/migration/ag-ui-compliance.md.


Single-Shot Text Response

The simplest possible flow. The model returns text with no tool calls.

Required chunk sequence from the adapter

#   Event                  Required Fields                        Notes
──  ─────────────────────  ─────────────────────────────────────  ──────────────────────────────────────────
1   RUN_STARTED            runId                                  MUST be first event
2   TEXT_MESSAGE_START      messageId, role: "assistant"           MUST precede any TEXT_MESSAGE_CONTENT
3   TEXT_MESSAGE_CONTENT    messageId, delta: "Hello"              One per token; delta is the increment
4   TEXT_MESSAGE_CONTENT    messageId, delta: " world"             Appended to previous
5   TEXT_MESSAGE_CONTENT    messageId, delta: "!"                  ...
6   TEXT_MESSAGE_END        messageId                              Signals text segment complete
7   RUN_FINISHED           runId, finishReason: "stop"            MUST be last meaningful event

Processor behavior, step by step

#Chunk receivedProcessor actionState change
1RUN_STARTEDIgnored. No handler.(none)
2TEXT_MESSAGE_STARThandleTextMessageStartEvent(): Flushes any pending text from a prior segment, then resets currentSegmentText = "" and lastEmittedText = "".Segment accumulators reset.
3TEXT_MESSAGE_CONTENThandleTextMessageContentEvent(): Calls ensureAssistantMessage() (lazily creates the assistant UIMessage on first content chunk). Appends delta to both currentSegmentText and totalTextContent. Chunk strategy decides emission. emitTextUpdate() calls updateTextPart() which: if last part is TextPart, replaces its content; otherwise appends a new TextPart.UIMessage created, parts: [{ type: "text", content: "Hello" }]
4TEXT_MESSAGE_CONTENTSame as above. currentSegmentText is now "Hello world". updateTextPart() replaces the last TextPart.parts: [{ type: "text", content: "Hello world" }]
5TEXT_MESSAGE_CONTENTSame.parts: [{ type: "text", content: "Hello world!" }]
6TEXT_MESSAGE_ENDIgnored. No handler.(none)
7RUN_FINISHEDhandleRunFinishedEvent(): Sets finishReason = "stop", isDone = true. Calls completeAllToolCalls() (no-op, no tool calls).Done.
—Stream endsfinalizeStream(): Calls completeAllToolCalls() (no-op). Flushes any pending text. Fires onStreamEnd.Final.

Key invariant

updateTextPart() appends vs. replaces based on the last part's type. If the last part is type: "text", its content is replaced (not appended to). If the last part is anything else (tool-call, tool-result, thinking), a new TextPart is pushed. This is how multi-segment text (text before and after tool calls) works.

Final UIMessage

{
  id: "msg-...",
  role: "assistant",
  parts: [
    { type: "text", content: "Hello world!" }
  ]
}

Single-Shot Tool Call Response

The model returns text AND one tool call. This is the most common source of bugs because the tool call events are interleaved with text events in a single stream.

Required chunk sequence from the adapter

#    Event                  Required Fields                                     Notes
───  ─────────────────────  ──────────────────────────────────────────────────  ──────────────────────────────────────────────────
1    RUN_STARTED            runId                                               First event
2    TEXT_MESSAGE_START     messageId, role: "assistant"                        Before any text content
3    TEXT_MESSAGE_CONTENT   messageId, delta: "Let me check."                   Text before tool call
4    TEXT_MESSAGE_END       messageId                                           Text segment done
5    TOOL_CALL_START        toolCallId: "call_1", toolCallName: "getWeather"    MUST have toolCallId + toolCallName
6    TOOL_CALL_ARGS         toolCallId: "call_1", delta: '{"city":'             Incremental JSON string
7    TOOL_CALL_ARGS         toolCallId: "call_1", delta: '"NYC"}'               More JSON
8    TOOL_CALL_END          toolCallId: "call_1", toolCallName: "getWeather"    Signals arguments finalized
9    RUN_FINISHED           runId, finishReason: "tool_calls"                   MUST be "tool_calls" when tools requested

Critical ordering rules

  1. TOOL_CALL_START MUST precede any TOOL_CALL_ARGS for the same toolCallId. The processor creates internal state (InternalToolCallState) in the toolCalls Map on TOOL_CALL_START. If TOOL_CALL_ARGS arrives for an unknown toolCallId, the args are silently dropped (the existingToolCall lookup fails).

  2. TOOL_CALL_END MUST come after all TOOL_CALL_ARGS for that toolCallId. TOOL_CALL_END transitions the tool call to input-complete state and does a final JSON parse of accumulated arguments. The part's input is set only when that parse is a strict JSON.parse success, or when TOOL_CALL_END.input supplies it. A supplied TOOL_CALL_END.input also replaces the accumulated arguments string, so the two never disagree when an adapter normalizes what it streamed. Incomplete or invalid arguments leave input unset; the raw arguments string is the fallback. Any TOOL_CALL_ARGS after TOOL_CALL_END for the same ID will still be processed (appending to arguments), but the state has already been set to input-complete. When text deltas arrive between TOOL_CALL_ARGS deltas, completion still waits for TOOL_CALL_END or the RUN_FINISHED / finalizeStream safety net.

  3. RUN_FINISHED with finishReason: "tool_calls" MUST come last. The TextEngine uses this to decide whether to enter the tool execution phase. The StreamProcessor uses it as a signal to force-complete any tool calls still not in input-complete state (safety net).

  4. Text events and tool call events can interleave, but within each tool call the order MUST be: START -> ARGS* -> END.

Processor behavior, step by step

#ChunkProcessor actionUIMessage parts after
1RUN_STARTEDIgnored.[]
2TEXT_MESSAGE_STARTResets segment accumulators.[]
3TEXT_MESSAGE_CONTENTensureAssistantMessage() creates UIMessage. Appends delta. updateTextPart() creates TextPart.[text: "Let me check."]
4TEXT_MESSAGE_ENDIgnored.[text: "Let me check."]
5TOOL_CALL_STARTensureAssistantMessage() (already exists). Creates InternalToolCallState in Map with state: "awaiting-input". updateToolCallPart() appends a ToolCallPart.[text: "Let me check.", tool-call(call_1, awaiting-input, args: "")]
6TOOL_CALL_ARGSAppends delta to arguments. State transitions awaiting-input -> input-streaming. updateToolCallPart() updates existing ToolCallPart by ID.[text: "Let me check.", tool-call(call_1, input-streaming, args: '{"city":')]
7TOOL_CALL_ARGSAppends delta. Stays input-streaming. Arguments now complete.[text: "Let me check.", tool-call(call_1, input-streaming, args: '{"city":"NYC"}')]
8TOOL_CALL_ENDcompleteToolCall(): Sets state: "input-complete". Does final JSON parse. No result field, so no tool-result part created.[text: "Let me check.", tool-call(call_1, input-complete, args: '{"city":"NYC"}')]
9RUN_FINISHEDSets finishReason = "tool_calls". completeAllToolCalls(): iterates all tool calls; call_1 already input-complete, so no-op.Same as above.
—Stream endsfinalizeStream(): Flushes pending text (already emitted). Fires onStreamEnd.Final.

Tool call state transitions

TOOL_CALL_START received     →  state: "awaiting-input"      (created in Map + UIMessage)
First TOOL_CALL_ARGS         →  state: "input-streaming"     (only if delta is non-empty)
Subsequent TOOL_CALL_ARGS    →  state: "input-streaming"     (no change, args accumulate)
TOOL_CALL_END received       →  state: "input-complete"      (final parse, authoritative)
  OR
RUN_FINISHED received        →  state: "input-complete"      (safety net via completeAllToolCalls)

What the processor does NOT do at this stage

  • It does not execute the tool. That's the TextEngine's job.
  • It does not create a tool-result part. That happens later when TOOL_CALL_END arrives WITH a result field (from TextEngine after execution).
  • It does not validate that toolCallName matches a known tool. The processor is tool-definition-agnostic.

Parallel Tool Calls (Single Shot)

The model requests multiple tools in one response. Adapters may interleave the events.

Required chunk sequence (interleaved)

#    Event                  Key Fields
───  ─────────────────────  ──────────────────────────────────────────
1    RUN_STARTED            runId
2    TOOL_CALL_START        toolCallId: "call_1", toolCallName: "getWeather", index: 0
3    TOOL_CALL_START        toolCallId: "call_2", toolCallName: "getTime", index: 1
4    TOOL_CALL_ARGS         toolCallId: "call_1", delta: '{"city":"NYC"}'
5    TOOL_CALL_ARGS         toolCallId: "call_2", delta: '{"tz":"EST"}'
6    TOOL_CALL_END          toolCallId: "call_1", toolCallName: "getWeather"
7    TOOL_CALL_END          toolCallId: "call_2", toolCallName: "getTime"
8    RUN_FINISHED           finishReason: "tool_calls"

Alternative valid ordering (sequential)

TOOL_CALL_START  call_1
TOOL_CALL_ARGS   call_1
TOOL_CALL_END    call_1
TOOL_CALL_START  call_2
TOOL_CALL_ARGS   call_2
TOOL_CALL_END    call_2
RUN_FINISHED

Both orderings are valid. The processor tracks tool calls by toolCallId in a Map, not by position. The index field on TOOL_CALL_START is stored for reference but the Map key is always toolCallId.

Processor guarantees

  • Each TOOL_CALL_START creates a new entry in the Map. Duplicate toolCallId on a second TOOL_CALL_START is a no-op (the if (!existingToolCall) guard).
  • TOOL_CALL_ARGS finds its tool call by toolCallId. If the ID is unknown, the event is silently dropped.
  • TOOL_CALL_END finds its tool call by toolCallId. If already input-complete, it's a no-op (the state guard).
  • RUN_FINISHED with completeAllToolCalls() force-completes any tool call not yet input-complete.

Final UIMessage parts

[
  { type: "tool-call", id: "call_1", name: "getWeather", arguments: '{"city":"NYC"}', state: "input-complete" },
  { type: "tool-call", id: "call_2", name: "getTime", arguments: '{"tz":"EST"}', state: "input-complete" },
]

Parts are ordered by when TOOL_CALL_START was first received, since updateToolCallPart() appends new parts at the end.


Text-Then-Tool Interleaving (Single Shot)

A response with text, then a tool call, then the tool result comes back, then more text. This tests the multi-segment text logic.

Chunk sequence (first adapter call + TextEngine tool result + second adapter call)

── First adapter stream ──────────────────────────────────
1    RUN_STARTED
2    TEXT_MESSAGE_START      messageId: "m1"
3    TEXT_MESSAGE_CONTENT    delta: "Checking weather..."
4    TEXT_MESSAGE_END
5    TOOL_CALL_START        toolCallId: "call_1", toolCallName: "getWeather"
6    TOOL_CALL_ARGS         toolCallId: "call_1", delta: '{"city":"NYC"}'
7    TOOL_CALL_END          toolCallId: "call_1"
8    RUN_FINISHED           finishReason: "tool_calls"

── TextEngine executes tool, yields result ──────────────
9    TOOL_CALL_END          toolCallId: "call_1", result: '{"temp":"72F"}'

── Second adapter stream ─────────────────────────────────
10   TEXT_MESSAGE_START      messageId: "m2"
11   TEXT_MESSAGE_CONTENT    delta: "It's 72°F in NYC."
12   TEXT_MESSAGE_END
13   RUN_FINISHED           finishReason: "stop"

Critical: TEXT_MESSAGE_START resets the text segment

At step 10, handleTextMessageStartEvent():

  • Flushes any pending text from the previous segment.
  • Resets currentSegmentText = "" and lastEmittedText = "".

This means the next TEXT_MESSAGE_CONTENT (step 11) starts accumulating into a fresh segment. When emitTextUpdate() is called, updateTextPart() sees the last part is a tool-result (not text), so it pushes a new TextPart rather than replacing.

UIMessage parts progression

After step 3:  [text: "Checking weather..."]
After step 5:  [text: "Checking weather...", tool-call(call_1, awaiting-input)]
After step 7:  [text: "Checking weather...", tool-call(call_1, input-complete)]
After step 9:  [text: "Checking weather...", tool-call(call_1, input-complete, output: {...}), tool-result(call_1, complete)]
After step 11: [text: "Checking weather...", tool-call(call_1, ...), tool-result(call_1, ...), text: "It's 72°F in NYC."]

Note: two separate TextParts in the final message. This preserves the interleaving for accurate round-trip conversion to ModelMessages.


Thinking/Reasoning Content

Chunk sequence

1    RUN_STARTED
2    STEP_STARTED           stepId, stepType: "thinking"       (informational, ignored by processor)
3    STEP_FINISHED          stepId, delta: "Let me think"
4    STEP_FINISHED          stepId, delta: " about this..."
5    TEXT_MESSAGE_START
6    TEXT_MESSAGE_CONTENT    delta: "Here's my answer."
7    TEXT_MESSAGE_END
8    RUN_FINISHED           finishReason: "stop"

Processor behavior

  • STEP_STARTED -- Ignored (no handler).
  • STEP_FINISHED -- handleStepFinishedEvent(): Calls ensureAssistantMessage(). Appends delta to thinkingContent. updateThinkingPart() either updates existing ThinkingPart or pushes a new one. Always replaces (not accumulates) -- the full thinkingContent string is set.

Final UIMessage parts

[
  { type: "thinking", content: "Let me think about this..." },
  { type: "text", content: "Here's my answer." }
]

Tool Results and the TOOL_CALL_END Dual Role

TOOL_CALL_END serves two purposes depending on whether result is present:

Without result (from adapter)

Signals that the tool call's input arguments are finalized.

  • Transitions state to input-complete.
  • Does final JSON parse of accumulated arguments.
  • If input field is provided, uses it as canonical parsed arguments.
  • No tool-result part is created.

With result (from TextEngine after execution)

Signals that the tool has been executed and the result is available.

  • Transitions the input state to input-complete (if not already).
  • Creates/updates two things:
    1. updateToolCallWithOutput() -- Sets output on the tool-call part (for UI rendering consistency).
    2. updateToolResultPart() -- Creates a tool-result part (for LLM conversation history).
  • The result field is a JSON string.
  • On an output-error result, the tool-call part transitions to the terminal error state (symmetric with the tool-result part's error state), so UIs can render failure from part.state without inspecting the error-shaped output. The completion safety net never downgrades an error part back to input-complete.

This distinction is critical

Adapters emit TOOL_CALL_END without result -- they're signaling "arguments are done." The TextEngine emits TOOL_CALL_END with result -- it's signaling "tool was executed, here's the output."

If an adapter incorrectly includes result, the processor will store it as a tool-result part immediately, which may cause unexpected behavior in the agent loop.


Client Tools and Approval Flows

The TextEngine (not the adapter) pauses the run with an interrupt. It ends the run with one terminal chunk:

RUN_FINISHED { outcome: { type: "interrupt", interrupts: [...] } }

Each interrupt carries toolCallId and a binding in metadata that says what it is for.

Approval interrupt

{ id: approvalId, reason: "tool_call", toolCallId,
  metadata: { kind: "approval", toolName, input, <binding kind: "tool-approval"> } }

Processor action (handleInterrupts()):

  1. updateToolCallApproval() sets state: "approval-requested" and adds approval metadata to the tool-call part.
  2. Fires onApprovalRequest.
  3. The client calls resolveInterrupt(true/false) on the bound tool-approval interrupt, which resumes the run.

Client tool interrupt

{ id: "client_tool_<toolCallId>", reason: "tanstack:client_tool_execution", toolCallId,
  metadata: { kind: "client_tool", toolName, input, <binding kind: "client-tool-execution"> } }

Processor action: fires onToolCall. The client runs the tool. Its result (addToolResult) answers the bound interrupt, which resumes the run.


Multi-Iteration Agent Loop

The TextEngine may call the adapter multiple times within a single chat() call (when tools are involved).

Iteration cycle

do {
  processText:      Call adapter.chatStream(), yield events, accumulate tool calls
  executeToolCalls: If finishReason == "tool_calls", execute tools, yield TOOL_CALL_END with results
} while (agentLoopStrategy says continue)

What the StreamProcessor sees

The processor receives a single flat stream of AG-UI events from all iterations. It does not know about iterations. Each TEXT_MESSAGE_START resets its text segment accumulator, which is how multi-iteration text works correctly.

StreamProcessor.process() vs manual processChunk()

  • process(stream) -- Consumes the entire async iterable, calls processChunk() for each event, then finalizeStream(). Used for simple consumption.
  • processChunk(chunk) -- Processes a single event. Used when the consumer needs to interleave its own logic (e.g., the client framework integration).

Adapter Contract

Mandatory events and ordering

Every adapter chatStream() implementation MUST emit events in this order:

RUN_STARTED                                     ← exactly once, first
  (TEXT_MESSAGE_START                           ← before any text content
    TEXT_MESSAGE_CONTENT*                       ← zero or more text deltas
  TEXT_MESSAGE_END)?                            ← if text was started
  (TOOL_CALL_START                              ← before any args for this tool
    TOOL_CALL_ARGS*                             ← zero or more arg deltas
  TOOL_CALL_END)?                               ← for each tool call
RUN_FINISHED | RUN_ERROR                        ← exactly once, last

In EBNF-like notation:

Stream        ::= RUN_STARTED Content* Terminal
Content       ::= TextBlock | ToolCall | ThinkingBlock
TextBlock     ::= TEXT_MESSAGE_START TEXT_MESSAGE_CONTENT* TEXT_MESSAGE_END
ToolCall      ::= TOOL_CALL_START TOOL_CALL_ARGS* TOOL_CALL_END
ThinkingBlock ::= STEP_STARTED? STEP_FINISHED+
Terminal      ::= RUN_FINISHED | RUN_ERROR

Event field requirements

RUN_STARTED

  • runId: string -- Unique run identifier.

TEXT_MESSAGE_START

  • messageId: string -- Unique message identifier.
  • role: "assistant" -- Always assistant for generated messages.

TEXT_MESSAGE_CONTENT

  • messageId: string -- Must match the preceding TEXT_MESSAGE_START.
  • delta: string -- Non-empty incremental text token.
  • content?: string -- (Optional) Full accumulated text. For debugging only; processor uses delta.

TEXT_MESSAGE_END

  • messageId: string -- Must match.

TOOL_CALL_START

  • toolCallId: string -- Globally unique within this stream. The processor uses this as a Map key.
  • toolCallName: string -- Name of the tool being called. Must not be empty.
  • index?: number -- (Optional) For parallel tool calls. Defaults to toolCalls.size (order of arrival).

TOOL_CALL_ARGS

  • toolCallId: string -- Must match a preceding TOOL_CALL_START.
  • delta: string -- Incremental JSON argument fragment. Concatenated by the processor.

TOOL_CALL_END

  • toolCallId: string -- Must match.
  • toolCallName?: string -- (Optional) Must match. Kept in metadata.tanstack on the wire.
  • input?: unknown -- (Optional) Parsed arguments. If provided, overrides the processor's accumulated string parse.
  • result?: string -- (Optional) Adapters MUST NOT set this. Reserved for TextEngine tool execution results.

RUN_FINISHED

  • runId: string -- Must match RUN_STARTED.
  • finishReason: "stop" | "length" | "content_filter" | "tool_calls" | null
    • "tool_calls" -- The model wants tool execution. TextEngine will enter tool phase.
    • "stop" -- Normal completion. TextEngine exits loop.
    • Other values are informational.
  • usage?: { promptTokens, completionTokens, totalTokens } -- (Optional) Token usage.

RUN_ERROR

  • error: { message: string, code?: string } -- Error details.

STEP_FINISHED (thinking)

  • stepId: string -- Step identifier.
  • delta: string -- Incremental thinking content.
  • content?: string -- (Optional) Full accumulated thinking.

What adapters MUST NOT do

  1. Do not emit TOOL_CALL_ARGS before TOOL_CALL_START for the same toolCallId. The processor will silently drop the args (no entry in the Map).

  2. Do not emit TOOL_CALL_END with a result field. This field is reserved for the TextEngine. If an adapter sets it, the processor will immediately create a tool-result part, corrupting the agent loop flow.

  3. Do not omit RUN_FINISHED. Without it, the TextEngine cannot determine whether to execute tools. The StreamProcessor's finalizeStream() will force-complete tool calls as a safety net, but this is a fallback for network errors -- not normal operation.

  4. Do not emit TEXT_MESSAGE_CONTENT without a preceding TEXT_MESSAGE_START. The text will still be processed (the processor doesn't validate this), but TEXT_MESSAGE_START is needed to reset the segment accumulator for correct multi-segment text after tool calls.

  5. Do not use empty strings for toolCallId or toolCallName. The ToolCallManager.getToolCalls() filters out entries where name.trim().length === 0.

  6. Do not reuse toolCallId values within a single stream. The processor's Map deduplicates by ID -- a second TOOL_CALL_START with the same ID is silently ignored.

  1. Provide TOOL_CALL_END.input with parsed arguments. This gives the processor an authoritative parse rather than relying on partial-JSON parsing of accumulated deltas.

  2. Provide TEXT_MESSAGE_CONTENT.content with accumulated text. Useful for debugging, but the processor always uses delta for accumulation.

  3. Use deterministic toolCallId values from the provider (e.g., OpenAI's call_* IDs) rather than generating random ones, to enable debugging and replay.


StreamProcessor Internal State

Stream-scoped state (reset per process() or prepareAssistantMessage())

FieldTypePurpose
totalTextContentstringAll text across all segments (for ProcessorResult.content)
currentSegmentTextstringText in the current segment (reset on TEXT_MESSAGE_START)
lastEmittedTextstringLast text sent to updateTextPart() (prevents duplicate emissions)
thinkingContentstringAccumulated thinking text
toolCallsMap<string, InternalToolCallState>Active tool calls keyed by toolCallId
toolCallOrderstring[]Order in which tool calls were first seen
finishReasonstring | nullFrom RUN_FINISHED
isDonebooleanSet by RUN_FINISHED

Conversation-scoped state (persists across streams)

FieldTypePurpose
messagesUIMessage[]The full conversation
currentAssistantMessageIdstring | nullID of the message being streamed (null before first content)

The lazy message creation pattern

The assistant UIMessage is not created when prepareAssistantMessage() is called. It's created lazily by ensureAssistantMessage() on the first content-bearing chunk. This prevents empty assistant messages from appearing in the UI during auto-continuation iterations that produce no content.

Content-bearing chunks that trigger ensureAssistantMessage():

  • TEXT_MESSAGE_CONTENT
  • TOOL_CALL_START
  • STEP_FINISHED
  • RUN_ERROR

The message takes the server id on the chunk when it has one (TOOL_CALL_START.parentMessageId, TEXT_MESSAGE_CONTENT.messageId). Thinking chunks carry no assistant id, so they make a placeholder with a client id. The placeholder takes the first server id that arrives in TOOL_CALL_START.parentMessageId or TEXT_MESSAGE_START.messageId, and keeps it. A later model call's TEXT_MESSAGE_START with a new id then starts a new UIMessage. It does not rename this one, so each UIMessage keeps the id the server stored for it. If a message with that parentMessageId already exists, the placeholder joins it, so the thinking stays with its tool call. A tool call always goes to the message its parentMessageId names, not to the active message.


UIMessage Part Ordering Invariants

The parts array within a single assistant UIMessage maintains a strict ordering that mirrors the stream:

  1. ThinkingPart (if present) -- Always managed as a single part, updated in place.
  2. TextPart (first segment) -- Created on first text content.
  3. ToolCallPart(s) -- Appended when tool calls start.
  4. ToolResultPart(s) -- Appended when tool results arrive.
  5. TextPart (second segment) -- Created when text resumes after tool calls.

The critical logic is in updateTextPart():

  • If the last part is a TextPart → replace its content (same segment continues).
  • If the last part is anything else → push a new TextPart (new segment after tools).

This means tool-call and tool-result parts act as "segment separators" for text content.

Round-trip fidelity

This ordering is preserved through uiMessageToModelMessages() which walks parts in order and flushes assistant segments at tool-result boundaries. See messages.ts for the conversion logic.


Testing Strategy

The StreamProcessor should be tested with recorded chunk sequences covering:

Single-shot scenarios (no agent loop)

  1. Text only -- RUN_STARTED → TEXT_MESSAGE_START → TEXT_MESSAGE_CONTENT* → TEXT_MESSAGE_END → RUN_FINISHED(stop)
  2. Tool call only (no text) -- RUN_STARTED → TOOL_CALL_START → TOOL_CALL_ARGS* → TOOL_CALL_END → RUN_FINISHED(tool_calls)
  3. Text then tool call -- Text block followed by tool call block.
  4. Parallel tool calls -- Multiple interleaved TOOL_CALL_START/ARGS/END sequences.
  5. Empty text deltas -- Verify empty delta in TOOL_CALL_ARGS doesn't transition from awaiting-input.
  6. Missing TOOL_CALL_END -- Verify RUN_FINISHED safety net completes the tool call.
  7. TOOL_CALL_END with input override -- Verify input field overrides accumulated parse.
  8. Thinking then text -- STEP_FINISHED events followed by text events.

Tool result scenarios

  1. TOOL_CALL_END with result -- Verify both output on tool-call part and tool-result part are created.
  2. Client tool via CUSTOM -- Verify onToolCall fires, then addToolResult() creates correct parts.
  3. Approval flow -- Verify state transitions: input-complete → approval-requested → approval-responded.

Multi-segment text

  1. Text → tool → text -- Verify two separate TextParts with TEXT_MESSAGE_START reset.
  2. Text → tool → result → text -- Verify parts order: text, tool-call, tool-result, text.

Edge cases

  1. No content (empty stream) -- Only RUN_STARTED + RUN_FINISHED. No assistant message created.
  2. RUN_ERROR -- Verify assistant message is created and onError fires.
  3. Duplicate TOOL_CALL_START (same ID) -- Verify it's a no-op.
  4. TOOL_CALL_ARGS for unknown ID -- Verify silently dropped.

Each test should assert:

  • The final UIMessage.parts array matches expected types, order, and content.
  • The ProcessorResult has correct content, toolCalls, and finishReason.
  • onMessagesChange was called the expected number of times.
  • Granular events (onTextUpdate, onToolCallStateChange) fired with correct arguments.