Skip to content

Stream State

The ResponseStreamStateMachine is the core state machine that drives the streaming pipeline. It produces ResponseStreamEvent arrays from every method call and maintains a live snapshot property that reflects the current response at all times.

State Structure

Lifecycle Phases

The state machine has five phases, tracked via ResponseStreamPhase:

PhaseDescription
IDLEInitial state before start() is called
IN_PROGRESSActively processing deltas (text, reasoning, refusal, tool calls)
COMPLETEDStream finished normally via finish()
INCOMPLETEStream finished with incomplete output via finish()
FAILEDStream terminated due to error via fail()

Phase transitions are validated — any call from the wrong phase throws a BridgeError with an appropriate code.

Event Production

The state machine produces events directly from its methods. Each delta method returns an array of ResponseStreamEvent objects.

Sequence

start() -> response.created, response.in_progress
  ├── text() -> output_item.added, content_part.added, output_text.delta
  ├── refusal() -> output_item.added, content_part.added, refusal.delta
  ├── reasoning() -> output_item.added, reasoning_text_part.added, reasoning_text.delta
  ├── toolCall() -> output_item.added, function_call_arguments.delta
  └── usage() -> (updates snapshot, no events emitted)

deferFinish() -> (records pending reason, no events)
finish() -> closes open blocks, emits terminal event (response.completed/incomplete)
fail() -> response.failed

Auto-close on Finish

When finish() is called, the state machine automatically closes any open blocks (active text, refusal, reasoning, and tool calls), emitting all remaining done events before the terminal event.

The Snapshot

The snapshot getter returns a ResponseObject that is always current — it includes the live output items, output text, and terminal status fields (when applicable). This snapshot is used by ResponseSessionPersistenceTransformer for session persistence.

Tool Call State

Tool calls are tracked via ToolCallBlock instances, which maintain accumulators per stream index. The toolCall() method applies incremental changes and emits events when the call becomes sufficiently formed. Custom tool calls (shell, apply_patch, local_shell) are detected through the ToolIdentityMap and reconstructed with their proper output types.

Error Handling

The state machine validates phase transitions and throws BridgeError for:

CodeWhen
bridge.stream.output_before_startDelta received before start()
bridge.stream.delta_after_terminalDelta received after finish() or fail()
bridge.stream.invalid_transitionMethod called in an unexpected phase
bridge.stream.incomplete_tool_callStream ended with an unfinished tool call

Error Hierarchy