@juspay/neurolink
Version:
Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applicatio
256 lines (255 loc) • 6.61 kB
TypeScript
/**
* Real-time Streaming Support
*
* Provides dedicated streaming capabilities including Server-Sent Events (SSE),
* WebSocket connections, and async iterators for real-time data streaming.
*
* @module @neurolink/client/streaming
*/
import type { ClientStreamEvent as StreamEvent, ClientStreamCallbacks, ClientStreamResult as StreamResult, ClientWebSocketOptions, ClientWebSocketState, ClientWebSocketMessageHandler, SSEConnectionOptions, SSEConnectionState, StreamingClientConfig, StreamingRequestOptions } from "../types/index.js";
/**
* Server-Sent Events (SSE) Client
*
* Provides a robust SSE connection with automatic reconnection,
* event parsing, and async iterator support.
*
* @example Basic usage
* ```typescript
* const sse = new SSEClient('https://api.example.com/stream');
*
* sse.on('message', (data) => console.log(data));
* sse.on('error', (error) => console.error(error));
*
* await sse.connect({ body: { prompt: 'Hello' } });
* ```
*
* @example Async iterator usage
* ```typescript
* const sse = new SSEClient('https://api.example.com/stream');
*
* for await (const event of sse.events({ body: { prompt: 'Hello' } })) {
* if (event.type === 'text') {
* console.log(event.content);
* }
* }
* ```
*/
export declare class SSEClient {
private url;
private options;
private state;
private abortController;
private reconnectAttempts;
private eventHandlers;
constructor(url: string, options?: SSEConnectionOptions);
/**
* Connect to SSE endpoint
*/
connect(requestOptions?: {
body?: unknown;
headers?: Record<string, string>;
}): Promise<void>;
private _connectSSE;
/**
* Disconnect from SSE endpoint
*/
disconnect(): void;
/**
* Process the SSE stream
*/
private processStream;
/**
* Handle a stream event
*/
private handleEvent;
/**
* Check if should attempt reconnection
*/
private shouldReconnect;
/**
* Attempt reconnection
*/
private reconnect;
/**
* Register event handler
*/
on(event: string, callback: (...args: unknown[]) => void): void;
/**
* Remove event handler
*/
off(event: string, callback: (...args: unknown[]) => void): void;
/**
* Emit event
*/
private emit;
/**
* Get current connection state
*/
getState(): SSEConnectionState;
/**
* Create async iterator for events
*
* @example
* ```typescript
* for await (const event of sse.events({ body: { prompt: 'Hello' } })) {
* console.log(event);
* }
* ```
*/
events(requestOptions?: {
body?: unknown;
headers?: Record<string, string>;
}): AsyncGenerator<StreamEvent, void, unknown>;
}
/**
* WebSocket Streaming Client
*
* Provides WebSocket-based streaming with automatic reconnection,
* heartbeat, and message handling.
*
* @example
* ```typescript
* const ws = new WebSocketStreamingClient({
* url: 'wss://api.example.com/ws',
* autoReconnect: true,
* });
*
* ws.on('message', (data) => console.log(data));
*
* await ws.connect();
* ws.send({ type: 'chat', content: 'Hello' });
* ```
*/
export declare class WebSocketStreamingClient {
private options;
private ws;
private state;
private reconnectAttempts;
private heartbeatInterval;
private eventHandlers;
constructor(options: ClientWebSocketOptions);
/**
* Connect to WebSocket server
*/
connect(): Promise<void>;
private _connectWS;
/**
* Disconnect from WebSocket server
*/
disconnect(): void;
/**
* Send message to server
*/
send(data: unknown): void;
/**
* Send message and wait for response
*/
request<T>(data: unknown, timeout?: number): Promise<T>;
/**
* Handle incoming message
*/
private handleMessage;
/**
* Start heartbeat
*/
private startHeartbeat;
/**
* Stop heartbeat
*/
private stopHeartbeat;
/**
* Attempt reconnection
*/
private attemptReconnect;
/**
* Register event handler
*/
on(event: string, callback: ClientWebSocketMessageHandler): void;
/**
* Remove event handler
*/
off(event: string, callback: ClientWebSocketMessageHandler): void;
/**
* Emit event
*/
private emit;
/**
* Get current connection state
*/
getState(): ClientWebSocketState;
/**
* Create async iterator for messages
*/
messages(): AsyncGenerator<unknown, void, unknown>;
}
/**
* Streaming Client Factory
*
* Creates streaming clients for real-time communication with NeuroLink API.
*
* @example SSE streaming
* ```typescript
* const client = createStreamingClient({
* baseUrl: 'https://api.example.com',
* apiKey: 'your-key',
* transport: 'sse',
* });
*
* const result = await client.stream({
* input: { text: 'Hello' },
* callbacks: {
* onText: (text) => console.log(text),
* },
* });
* ```
*
* @example WebSocket streaming
* ```typescript
* const client = createStreamingClient({
* baseUrl: 'https://api.example.com',
* apiKey: 'your-key',
* transport: 'websocket',
* });
*
* await client.connect();
* const result = await client.stream({
* input: { text: 'Hello' },
* });
* ```
*/
export declare function createStreamingClient(config: StreamingClientConfig): {
connect: () => Promise<void>;
disconnect: () => void;
stream: (options: StreamingRequestOptions & {
callbacks?: ClientStreamCallbacks;
}) => Promise<StreamResult>;
send: (data: unknown) => void;
on: (event: string, callback: ClientWebSocketMessageHandler) => void;
off: (event: string, callback: ClientWebSocketMessageHandler) => void;
getState: () => ClientWebSocketState;
};
/**
* Create an async iterable from streaming response
*
* @example
* ```typescript
* const stream = createAsyncStream(fetch('/api/stream', { method: 'POST' }));
*
* for await (const event of stream) {
* console.log(event);
* }
* ```
*/
export declare function createAsyncStream(responsePromise: Promise<Response>): AsyncGenerator<StreamEvent, void, unknown>;
/**
* Collect streaming events into a single result
*
* @example
* ```typescript
* const result = await collectStream(
* createAsyncStream(fetch('/api/stream', { method: 'POST' }))
* );
* console.log(result.content);
* ```
*/
export declare function collectStream(stream: AsyncIterable<StreamEvent>): Promise<StreamResult>;