Stream execution updates using Server-Sent Events (SSE).
This endpoint provides real-time execution updates by combining:
1. Historical messages from database (last 200 messages)
2. Live events from Redis (sub-second latency)
3. Workflow state from Temporal (200ms polling)
The endpoint supports:
- Progressive history loading (no blocking on full history)
- Last-Event-ID resumption for reconnection
- Gap detection and notification
- Graceful degradation when services unavailable
- Backward compatible with existing clients
Architecture:
This endpoint uses the ExecutionStreamer class which orchestrates
all phases of streaming:
1. Immediate connection (<50ms)
2. History loading (progressive, one message at a time)
3. History completion notification
4. Live event streaming (until workflow completes or timeout)
Gap Recovery:
- Supports Last-Event-ID pattern for reconnection
- Client sends last_event_id query param or Last-Event-ID header
- Server resumes from that point or detects gaps
- Buffered events replayed if available
- Client notified if gap unrecoverable
SSE Event Types:
- connected: Initial connection acknowledgment
- message: Chat message (history or live)
- message_chunk: Streaming message chunk
- member_message_chunk: Team member message chunk
- history_complete: All historical messages sent
- status: Execution status update
- tool_started: Tool execution started
- tool_completed: Tool execution completed
- degraded: Service degradation notification
- recovered: Service recovery notification
- gap_detected: Gap in event stream detected
- error: Error occurred
- keepalive: Connection keepalive (comment)
SSE Format:
- id: {execution_id}_{counter}_{timestamp_micros}
- event: {event_type}
- data: {json object}
Args:
execution_id: Execution ID to stream
request: FastAPI request object (for headers)
last_event_id: Last event ID received by client (for resumption)
organization: Authenticated organization (injected by auth middleware)
db: Database session (injected by dependency)
Returns:
StreamingResponse with text/event-stream content
Raises:
HTTPException 404: Execution not found or not authorized
HTTPException 500: Critical initialization error