/** * @license * Copyright 2024 Nuraly, Laabidi Aymen * SPDX-License-Identifier: MIT */ import { Socket } from 'socket.io-client'; import type { ChatbotProvider, ProviderCapabilities, ProviderConfig, ChatbotContext } from '../core/types.js'; /** * Socket.io event configuration */ export interface SocketEventConfig { /** Event name to emit when sending a message */ send: string; /** Event name for complete response */ response: string; /** Event name for streaming chunks (optional) */ stream?: string; /** Event name for errors */ error?: string; /** Event name for typing indicator start */ typingStart?: string; /** Event name for typing indicator end */ typingEnd?: string; /** Event name for tool/function call start */ toolCall?: string; /** Event name for tool/function call end */ toolCallEnd?: string; } /** * Socket provider configuration */ export interface SocketProviderConfig extends ProviderConfig { /** Socket.io server URL */ url: string; /** Socket.io path (default: /socket.io) */ path?: string; /** Socket.io namespace (optional) */ namespace?: string; /** Event names configuration */ events: SocketEventConfig; /** Custom headers for socket handshake */ headers?: Record; /** Auto-reconnect on disconnect (default: true) */ autoReconnect?: boolean; /** Reconnection attempts (default: 5) */ reconnectionAttempts?: number; /** Build custom payload from message and context */ buildPayload?: (text: string, context: ChatbotContext) => any; /** Extract message from response data */ extractMessage?: (data: any) => string; /** Response timeout in ms (default: 30000) */ responseTimeout?: number; } /** * Generic Socket.io provider for real-time chatbot communication * * Works with any socket.io backend - workflows, custom AI servers, etc. * * @example Basic usage * ```typescript * const provider = new SocketProvider(); * await provider.connect({ * url: 'http://localhost:8000', * path: '/socket.io/chat', * events: { * send: 'chat:message', * response: 'chat:response', * stream: 'chat:stream' * } * }); * ``` * * @example With custom payload builder * ```typescript * const provider = new SocketProvider(); * await provider.connect({ * url: 'http://localhost:8000', * events: { send: 'ask', response: 'answer' }, * buildPayload: (text, context) => ({ * question: text, * threadId: context.currentThread?.id, * modules: context.selectedModules * }) * }); * ``` * * @example Workflow backend * ```typescript * const provider = new SocketProvider(); * await provider.connect({ * url: 'http://localhost:8000', * path: '/socket.io/workflow', * events: { * send: 'workflow:trigger', * response: 'execution:completed', * stream: 'execution:node-completed', * error: 'execution:failed' * }, * buildPayload: (text, context) => ({ * workflowId: context.metadata.workflowId, * input: { message: text } * }) * }); * ``` */ export declare class SocketProvider implements ChatbotProvider { readonly id = "socket"; readonly name = "Socket.io Provider"; readonly capabilities: ProviderCapabilities; protected socket: Socket | null; protected config: SocketProviderConfig | null; protected connected: boolean; /** Called when a tool/function call is detected */ onToolCall?: (name: string) => void; /** Called when a tool/function call ends */ onToolCallEnd?: () => void; protected responseResolvers: Map void; reject: (error: Error) => void; chunks: string[]; isStreaming: boolean; }>; connect(config: SocketProviderConfig): Promise; protected setupEventListeners(): void; protected getLatestResolver(): { resolve: (value: string) => void; reject: (error: Error) => void; chunks: string[]; isStreaming: boolean; } | null | undefined; protected extractMessageId(data: any): string; protected extractMessage(data: any): string; disconnect(): Promise; isConnected(): boolean; sendMessage(text: string, context: ChatbotContext): AsyncIterator; protected buildPayload(text: string, context: ChatbotContext, messageId: string): any; /** * Emit a custom event on the socket */ emit(event: string, data?: any): void; /** * Subscribe to a custom event */ on(event: string, callback: (data: any) => void): void; /** * Unsubscribe from a custom event */ off(event: string, callback?: (data: any) => void): void; /** * Get the underlying socket instance for advanced usage */ getSocket(): Socket | null; /** * Format error messages with title and description */ protected formatError(title: string, description: string): string; onError(error: Error): void; } //# sourceMappingURL=socket-provider.d.ts.map