import { AIStreamEvent, AIStreamEventListeners } from '../types/stream.types.js'; /** * AIStream wraps an SSE response and provides three consumption patterns: * 1. for-await-of (AsyncIterable) * 2. .on(event, handler) convenience listeners * 3. .finalResponse() to await the aggregated result * * Internally it reads from the SSE fetch Response. Events are buffered * and dispatched to both the async iterator and the .on() listeners. * * The stream is consumed exactly once — calling for-await OR .on() * starts reading. .finalResponse() can be called in either mode. */ export declare class AIStream implements AsyncIterable { private reader; private decoder; private buffer; private done; private finalResult; private error; private abortController; private listeners; private completionResolve; private completionReject; private completionPromise; private started; /** * Create a stream wrapper for an SSE fetch response. * * Most applications receive AIStream instances from streaming operations such * as `client.ai.insights.get({ stream: true })` or * `client.ai.chat.sendMessage({ stream: true })`. * * @param response - Fetch Response containing an SSE body * @param abortController - Controller used to cancel the underlying stream * @throws Error when the response does not contain a readable body */ constructor(response: Response, abortController: AbortController); /** * AsyncIterable implementation — enables `for await (const event of stream)` * * @returns An async iterator that yields stream events as they arrive */ [Symbol.asyncIterator](): AsyncIterableIterator; /** * Register event-specific listeners. Starts consuming the stream * in the background if not already started via for-await. * * @param event - Event type to listen for: `progress`, `text`, or `error` * @param handler - Callback invoked when the event is received * @returns The stream instance for chaining listener registrations * * @example * ```typescript * stream * .on('progress', message => console.log(message)) * .on('text', content => appendToUI(content)); * ``` */ on(event: K, handler: AIStreamEventListeners[K]): this; /** * Returns a promise that resolves with the aggregated final response * when the stream completes. Works with both for-await and .on() patterns. * * If the stream has not started yet, calling this method starts background * consumption automatically. * * @returns The complete response produced by the streaming operation */ finalResponse(): Promise; /** * Abort the stream. * * Cancels the underlying fetch request and stops reading additional SSE * events. */ abort(): void; /** * Background consumer for .on() and standalone .finalResponse() usage */ private consumeInBackground; private dispatchToListeners; /** * Reads from the SSE response and parses the next batch of events. * Returns null when the stream is done. */ private readNextEvents; /** * Parses a single SSE event text block into an AIStreamEvent. * Also handles the 'complete' server event to extract the final result. */ private parseSSEEvent; } //# sourceMappingURL=ai-stream.d.ts.map