/** * Lightweight streaming event store for agents. * Agents publish partial responses so external subscribers can poll for streaming updates. */ export type StreamingEventType = 'start' | 'progress' | 'done' | 'error'; export interface StreamingEvent { sessionId: string; type: StreamingEventType; content: string; timestamp: string; metadata?: Record; } class StreamingService { private events = new Map(); private readonly maxEventsPerSession = 50; publish(sessionId: string, event: Omit & { timestamp?: string }): void { const timestamp = event.timestamp || new Date().toISOString(); const payload: StreamingEvent = { sessionId, content: event.content, type: event.type, metadata: event.metadata, timestamp }; const existing = this.events.get(sessionId) || []; existing.push(payload); if (existing.length > this.maxEventsPerSession) { existing.shift(); } this.events.set(sessionId, existing); } consume(sessionId: string): StreamingEvent[] { const stored = this.events.get(sessionId) || []; this.events.delete(sessionId); return stored; } clear(sessionId: string): void { this.events.delete(sessionId); } } export const streamingService = new StreamingService();