/** * Server-Sent Events (SSE) streaming utilities * * Provides helpers for connecting to and consuming SSE streams from the otto server. */ import { type ClientEvent, type NotificationAction, type NotificationEvent, type NotificationLevel, type ServerEvent, type SessionStatusEvent } from '@ottocode/sdk/events/protocol'; export interface SSEEvent { id?: string; event?: string; data: string; retry?: number; } export type SSERequestMethod = 'GET' | 'POST'; export type SSETransportMode = 'direct' | 'tunnel'; interface SSERequestOptions { /** Explicit request method. Overrides transportMode. */ method?: SSERequestMethod; /** Selects GET for direct connections or POST for tunnel compatibility. */ transportMode?: SSETransportMode; } export interface SSEStreamOptions extends SSERequestOptions { /** * Base URL of the API server */ baseUrl: string; /** * Session ID to stream events for */ sessionId: string; /** * Project path (optional) */ projectPath?: string; /** * Project id (optional) */ projectId?: string; /** * Custom fetch implementation */ fetch?: typeof fetch; /** * Additional request headers */ headers?: HeadersInit; /** * Callback for each parsed SSE event */ onEvent: (event: SSEEvent) => void; /** * Error handler */ onError?: (error: Error) => void; /** * Connection closed handler */ onClose?: () => void; } export interface ClientEventsStreamOptions extends SSERequestOptions { /** * Base URL of the API server */ baseUrl: string; /** * Project path (optional) */ projectPath?: string; /** * Project id (optional) */ projectId?: string; /** * Custom fetch implementation */ fetch?: typeof fetch; /** * Additional request headers */ headers?: HeadersInit; /** * Callback for each parsed SSE event */ onEvent: (event: SSEEvent) => void; /** * Error handler */ onError?: (error: Error) => void; /** * Connection closed handler */ onClose?: () => void; } export declare function buildSessionStreamUrl(options: { baseUrl: string; sessionId: string; projectPath?: string; projectId?: string; }): string; export declare function buildClientEventsStreamUrl(options: { baseUrl: string; projectPath?: string; projectId?: string; }): string; /** * Build the URL for the multiplexed project event stream. One SSE connection * carries every session event for the project plus global client events. */ export declare function buildProjectEventsStreamUrl(options: { baseUrl: string; projectPath?: string; projectId?: string; sessionIds?: string[]; }): string; export interface ConsumeSSEOptions extends SSERequestOptions { url: string; fetch?: typeof fetch; headers?: HeadersInit; signal?: AbortSignal; onEvent: (event: SSEEvent) => void; } /** Consumes one SSE response with incremental decoding and deterministic cleanup. */ export declare function consumeSSE(options: ConsumeSSEOptions): Promise; /** * Create an SSE stream connection to a session * * @example * ```typescript * import { createSSEStream } from '@ottocode/api'; * * const controller = new AbortController(); * * createSSEStream({ * baseUrl: 'http://localhost:9100', * sessionId: 'session-123', * onEvent: (event) => { * console.log('Event:', event.event, event.data); * const data = JSON.parse(event.data); * // Handle different event types... * }, * onError: (error) => { * console.error('Stream error:', error); * }, * onClose: () => { * console.log('Stream closed'); * } * }, controller.signal); * * // Later: controller.abort() to close the stream * ``` */ export declare function createSSEStream(options: SSEStreamOptions, signal?: AbortSignal): Promise; /** * Create an app-level SSE stream connection for global client events. */ export declare function createClientEventsStream(options: ClientEventsStreamOptions, signal?: AbortSignal): Promise; export type { ClientEvent, NotificationAction, NotificationEvent, NotificationLevel, ServerEvent, SessionStatusEvent, }; /** Parses the JSON data field from a server session event. */ export declare function parseServerEvent(raw: string): ServerEvent | null; /** Parses the JSON data field from a global client event. */ export declare function parseClientEvent(raw: string): ClientEvent | null; /** * Type guard to check if an event is a specific type */ export declare function isServerEvent(event: unknown, type: T): event is Extract; /** * Type guard to check if a global client event is a specific type. */ export declare function isClientEvent(event: unknown, type: T): event is Extract; //# sourceMappingURL=streaming.d.ts.map