export type CalliopeEvent = { type: string; data: Record; ts: string; }; /** * SSE connection lifecycle: * - `connecting` — first attach attempt in flight * - `open` — stream established (EventSource.onopen fired) * - `reconnecting` — stream dropped; the 3s re-attach loop is running */ export type EventConnectionState = 'connecting' | 'open' | 'reconnecting'; type Listener = (event: CalliopeEvent) => void; type StateListener = (state: EventConnectionState) => void; export function connectEvents( onEvent: Listener, onState?: StateListener, ): (signal?: AbortSignal) => void { let es: EventSource | null = null; let closed = false; let firstAttach = true; let everOpened = false; let reconnectTimer: ReturnType | null = null; const attach = () => { if (closed) return; onState?.(firstAttach ? 'connecting' : 'reconnecting'); firstAttach = false; es = new EventSource('/api/events'); es.onopen = () => { onState?.('open'); // Reconnected after a drop: everything missed while the stream was // down is gone forever (no replay), so ask the page to refetch. // Listeners gate on `everOpened` so the very first open is a no-op. if (everOpened) { onEvent({ type: 'events.resync', data: {}, ts: new Date().toISOString() }); } everOpened = true; }; const handler = (e: MessageEvent) => { try { const parsed = JSON.parse(e.data) as CalliopeEvent; onEvent(parsed); } catch { /* ignore */ } }; // Listen to named events + default message [ 'agent.thinking', 'agent.session.updated', 'agent.message', 'agent.token', 'agent.tool', 'agent.plan', 'agent.task', 'job.created', 'job.started', 'job.progress', 'job.completed', 'job.failed', 'job.deleted', 'asset.ready', 'story.ready', 'canvas.updated', 'shot.updated', 'shot.capture.saved', ].forEach((name) => es!.addEventListener(name, handler as EventListener)); es.onmessage = handler; es.onerror = () => { onState?.('reconnecting'); es?.close(); if (!closed) { reconnectTimer = setTimeout(attach, 3000); } }; }; attach(); return () => { closed = true; if (reconnectTimer) clearTimeout(reconnectTimer); es?.close(); }; }