Skip to content

Stream Reconstruction

Stream reconstruction is the bridge layer that translates heterogeneous provider stream deltas into a uniform sequence of OpenAI-compatible ResponseStreamEvent objects. Without it, every upstream provider would emit chunks in its own shape, and downstream consumers (SSE encoders, trace loggers, session persistence) would need provider-specific logic. The reconstruction layer gives GodeX a single, predictable event model regardless of which LLM provider is handling the request.

At a Glance

ConcernComponentKey File
State machine phasesResponseStreamStateMachineresponse-stream-state-machine.ts:37
Delta-to-event mappingmapProviderDeltasToEventsstream-reconstructor.ts:17
Delta validationvalidateDelta + helpersstream-reconstructor.ts:69
Delta type contractProviderStreamDeltastream-delta.ts:28
Deferred terminal eventsdeferFinish / deferTerminalresponse-stream-state-machine.ts:263

Stream State Machine Phases

The ResponseStreamPhase enum defines five phases that every stream passes through:

PhaseDescription
IDLEInitial state; no events emitted yet
IN_PROGRESSStream is actively receiving deltas
COMPLETEDStream finished normally
INCOMPLETEStream hit a length or context-window limit
FAILEDStream terminated due to an error

The transition logic lives in response-stream-state-machine.ts:787, which maps the provider finish reason to the correct terminal phase.

Block Management

While in the IN_PROGRESS phase, the state machine manages four categories of output blocks:

Block TypeFieldsTracked By
TextitemId, outputIndex, contentIndex, textactiveText
Refusalsame shape as TextactiveRefusal
ReasoningitemId, outputIndex, contentIndex, textactiveReasoning
Tool CallcallId, providerName, arguments, customInputactiveToolCalls Map

Each block is lazily created on its first delta and closed when the stream reaches a terminal phase via closeActiveBlocks (response-stream-state-machine.ts:428).

Delta Validation Pipeline

mapProviderDeltasToEvents (stream-reconstructor.ts:17) is the core loop. Before any state machine transition, every raw delta passes through validateDelta which enforces the ProviderStreamDelta contract (stream-delta.ts:28).

Validated FieldRules
textOptional string
reasoningOptional string
refusalOptional string
toolCallOptional object; index must be non-negative integer; id/type/name/arguments must be strings
usageObject with required input_tokens, output_tokens, total_tokens (finite numbers)
finishReasonString, null, or undefined
errorObject with required message string and optional code string

Unrecognized fields cause a BridgeError with code BRIDGE_STREAM_INVALID_TRANSITION (stream-reconstructor.ts:120).

Deferred Terminal Events

When deferTerminal is true (as it is in the streaming pipeline), the state machine's deferFinish method stores the finish reason without transitioning to a terminal phase (response-stream-state-machine.ts:263). This allows downstream transformers -- notably the output contract validation transformer -- to inspect and potentially rewrite the terminal event before it reaches the client.

The ProviderStreamEventBridge.flush method (stream-pipeline.ts:123) calls machine.finish(machine.deferredFinishReason) when the upstream stream closes, ensuring the terminal event is always emitted.

Tool Call Reconstruction

Tool call blocks are tracked by streamIndex in a Map<number, ToolCallBlock> (response-stream-state-machine.ts:94). When a tool call block is closed, the state machine:

  1. Verifies both callId and providerName are present (otherwise throws BRIDGE_STREAM_INCOMPLETE_TOOL_CALL)
  2. Checks the ToolIdentityMap to determine if the tool is a custom tool
  3. Calls restoreToolCall from call-restorer.ts:16 to map the provider function call back to the correct Responses API type (function_call, local_shell_call, shell_call, apply_patch_call, or custom_tool_call)

Error Normalization

Provider errors are normalized through normalizeError (response-stream-state-machine.ts:753), which maps the provider error code against a known set of ResponseErrorCode values. Unknown codes fall back to server_error.

Cross-References

  • Streaming Pipeline -- orchestrates the transform chain that feeds into this state machine
  • Output Contracts -- uses deferred terminal events for JSON validation
  • Tool Planning -- produces the ToolIdentityMap consumed during tool call reconstruction
  • Sync Pipeline -- non-streaming counterpart that reconstructs a complete ResponseObject

References