UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

106 lines (105 loc) • 3.29 kB
/** * WebSocketHandler - Unified WebSocket Support * * Provides cross-framework WebSocket handling for real-time AI interactions. * Supports connection management, message routing, and graceful shutdown. */ import type { WebSocketConfig, WebSocketHandler as IWebSocketHandler, WebSocketConnection, WebSocketMessage, WebSocketRequestId, AuthenticatedUser } from "../../types/index.js"; import type { NeuroLink } from "../../neurolink.js"; /** * WebSocket connection manager */ export declare class WebSocketConnectionManager { private connections; private config; private pingIntervals; private handlers; constructor(config?: WebSocketConfig); /** * Register a handler for a path */ registerHandler(path: string, handler: IWebSocketHandler): void; /** * Get handler for a path */ getHandler(path: string): IWebSocketHandler | undefined; /** * Handle new connection */ handleConnection(socket: unknown, path: string, user?: AuthenticatedUser): Promise<WebSocketConnection>; /** * Handle incoming message */ handleMessage(connectionId: string, data: string | ArrayBuffer, isBinary: boolean): Promise<void>; /** * Handle connection close */ handleClose(connectionId: string, code: number, reason: string): Promise<void>; /** * Handle connection error */ handleError(connectionId: string, error: Error): Promise<void>; /** * Get connection by ID */ getConnection(connectionId: string): WebSocketConnection | undefined; /** * Get all connections */ getAllConnections(): WebSocketConnection[]; /** * Get connections for a user */ getConnectionsByUser(userId: string): WebSocketConnection[]; /** * Get connections for a path */ getConnectionsByPath(path: string): WebSocketConnection[]; /** * Send message to a connection */ send(connectionId: string, data: string | ArrayBuffer): void; /** * Broadcast message to all connections */ broadcast(data: string | ArrayBuffer, filter?: (conn: WebSocketConnection) => boolean): void; /** * Close a connection */ close(connectionId: string, code?: number, reason?: string): Promise<void>; /** * Close all connections */ closeAll(code?: number, reason?: string): Promise<void>; /** * Get connection count */ getConnectionCount(): number; /** * Start ping interval for a connection */ private startPingInterval; /** * Stop ping interval for a connection */ private stopPingInterval; } /** * WebSocket message router for handling different message types */ export declare class WebSocketMessageRouter { private routes; /** * Register a message route */ route(type: string, handler: (connection: WebSocketConnection, payload: unknown, requestId?: WebSocketRequestId) => Promise<unknown>): void; /** * Handle incoming message */ handle(connection: WebSocketConnection, message: WebSocketMessage): Promise<unknown>; /** * Get registered routes */ getRoutes(): string[]; } export declare function createAgentWebSocketHandler(neurolink: NeuroLink): IWebSocketHandler;