UNPKG

@llamaindex/ui

Version:

A comprehensive UI component library built with React, TypeScript, and Tailwind CSS for LlamaIndex applications

69 lines (67 loc) 2.43 kB
/** * Shared Streaming Utility * * Provides transparent request deduplication and sharing for streaming operations. * Multiple subscribers to the same stream will share a single underlying connection, * improving performance and reducing server load. */ interface StreamSubscriber<TEvent> { onStart?: () => void; onData?: (events: TEvent) => void; onError?: (error: Error) => void; onCancel?: () => void; onSuccess?: (allEvents: TEvent[]) => void; onComplete?: () => void; } interface StreamExecutor<TEvent> { (subscriber: StreamSubscriber<TEvent>, signal: AbortSignal): Promise<TEvent[]>; } interface StreamOperation<TEvent> { promise: Promise<TEvent[]>; unsubscribe: () => void; disconnect: () => void; cancel: () => Promise<void>; } declare class SharedStreamingManager<TEvent = any> { private activeStreams; /** * Subscribe to a shared stream identified by key. * If stream already exists, reuses it and sends historical events. * If stream doesn't exist, creates a new one using the executor. * * @param streamKey - Unique identifier for the stream * @param subscriber - Event handlers for the stream * @param executor - Function that performs the actual streaming * @param externalSignal - Optional abort signal from caller * @returns Promise that resolves with all events and unsubscribe function */ subscribe(streamKey: string, subscriber: StreamSubscriber<TEvent>, executor: StreamExecutor<TEvent>, canceler: () => Promise<void>): StreamOperation<TEvent>; /** * Get current events for a stream without subscribing */ getStreamEvents(streamKey: string): TEvent[]; /** * Check if a stream is currently active */ isStreamActive(streamKey: string): boolean; /** * Get number of subscribers for a stream */ getSubscriberCount(streamKey: string): number; /** * Force close a stream and all its subscribers */ closeStream(streamKey: string): void; /** * Close all active streams */ closeAllStreams(): void; private subscribeToExistingStream; private createNewStream; private executeStream; private cancel; private disconnect; private unsubscribe; private cleanupStream; } export { type StreamOperation as S, type StreamSubscriber as a, type StreamExecutor as b, SharedStreamingManager as c };