/** * SSE Event Handler - Reusable Server-Sent Events Logic * * Purpose: Centralized SSE streaming logic for world events * * Features: * - Sets up SSE response headers and connection * - Wires world event listeners (MESSAGE, SSE, SYSTEM, WORLD) * - Handles world activity state tracking (response-start, idle) * - Forwards tool events (tool-start, tool-result, tool-error, tool-progress) as SSE events * - Automatic stream completion when world becomes idle * - Timeout fallback (60s) if world never becomes idle * - Proper cleanup on client disconnect or stream end * * Event Routing: * - 'world' channel carries both WorldActivityEvent and WorldToolEvent types * - WorldActivityEvent (response-start, idle, response-end) → manages stream lifecycle * - WorldToolEvent (tool-start, tool-result, tool-error, tool-progress) → forwarded as SSE events * * Usage: * ```typescript * const handler = createSSEHandler(req, res, world, 'chat'); * * // Publish your event (message, tool result, etc.) * publishMessage(world, message, sender); * * // Optional: Send custom event * handler.sendSSE({ type: 'custom', data: {...} }); * ``` * * Created: 2025-11-10 - Extracted from api.ts for reusability * Updated: 2026-03-01 - Skip synthesis for 'edit' context to prevent duplicate "From human" messages after message edits. * Updated: 2026-03-11 - Exposed a readiness promise so chat/edit dispatch waits until SSE listeners are attached, preventing the web client from missing the initial user-message echo. * Updated: 2026-03-11 - Replaced optional-call resolution of the readiness promise with an explicit helper to satisfy * strict TypeScript control-flow analysis in the build config. * Updated: 2026-02-27 - Scoped realtime log forwarding by world/chat to prevent cross-chat log leakage in chat-scoped streams. * Updated: 2026-02-26 - Added realtime log-stream forwarding (`type: 'log'`) to SSE clients to align web error visibility with Electron. * Updated: 2026-02-20 - Removed stale legacy event-channel SSE forwarding from this handler. * Updated: 2026-02-21 - Refresh fallback timeout on shell assistant-stream SSE activity (`start`/`chunk`/`end` + `toolName='shell_cmd'`) as well as legacy `tool-stream`. * Updated: 2026-02-11 - Extended fallback timeout on tool-stream events to prevent premature timeout * Updated: 2026-02-08 - Removed manual tool-intervention SSE commentary and kept generic tool_call forwarding * Updated: 2025-11-10 - Added tool event forwarding to SSE channel */ import { Request, Response } from 'express'; import { World } from '../core/index.js'; export interface SSEHandler { /** * Resolves once synthesis has finished and live listeners are attached. */ ready: Promise; /** * Send a Server-Sent Event to the client * @param data - Data object to send (will be JSON stringified) */ sendSSE: (data: any) => void; /** * Manually end the SSE response */ endResponse: () => void; /** * Check if the response has ended */ isEnded: () => boolean; } /** * Create and configure an SSE handler for streaming world events * * @param req - Express request object * @param res - Express response object * @param world - World instance to listen to * @param context - Context label for logging (e.g., 'chat', 'tool-result') * @returns SSEHandler interface with sendSSE, endResponse, and isEnded methods */ export declare function createSSEHandler(req: Request, res: Response, world: World, context?: string, scopedChatId?: string | null): SSEHandler;