Skip to main content

SSE Streaming Responses

Antigravity Manager converts Gemini’s streaming responses into OpenAI and Claude SSE (Server-Sent Events) formats, enabling real-time token-by-token output.

Architecture

OpenAI SSE Format

Location: src-tauri/src/proxy/mappers/openai/streaming.rs

Event Sequence

  1. Initial chunk with role:
  1. Content chunks as they arrive:
  1. Reasoning chunks (for thinking models):
  1. Tool call chunks:
  1. Final chunk with usage:

Streaming State Machine

The mapper maintains state across chunks:

Heartbeat Mechanism

To prevent connection timeouts, heartbeat pings are sent every 15 seconds:
This is an SSE comment (: prefix) that clients ignore but keeps the connection alive.

Buffer Management

Incoming bytes are buffered and processed line-by-line:
This handles fragmented network packets correctly.

Claude SSE Format

Location: src-tauri/src/proxy/mappers/claude/streaming.rs Claude’s streaming format is more complex with explicit content block lifecycle:

Event Types

1. message_start
2. content_block_start
3. content_block_delta (multiple)
4. content_block_stop
5. message_delta (final)
6. message_stop

Thinking Block Streaming

Thinking content gets separate blocks:
Signatures are streamed separately to support validation.

Tool Use Streaming

Tool calls are streamed differently than OpenAI: Block start with empty input:
Input delta with full JSON:
Block stop:

State Machine

Signature Management

Signatures are buffered and emitted at block end:

Web Search Grounding

Search results are appended as markdown text blocks:

Error Handling in Streams

When errors occur mid-stream: OpenAI:
Claude:
The stream is then terminated gracefully.

MCP XML Bridge

For MCP tool calls, XML syntax is supported:
This is automatically detected and converted:
This improves MCP compatibility with large results.

Parameter Remapping

Gemini often uses different parameter names:
This fixes common hallucinations.

Fuzzy Tool Matching

For MCP tools, names are fuzzy-matched:
Strategies:
  1. Exact suffix match (puppeteer_navigatemcp__puppeteer__puppeteer_navigate)
  2. Substring containment
  3. Token overlap scoring

Performance Optimizations

Zero-Copy Parsing

SSE lines are parsed without allocations:

Async Buffering

Chunks are yielded asynchronously:
This allows backpressure from slow clients.

Early Termination

On error, stream stops immediately:

Testing

Stream processing is tested with:

See Also