/** * llm.do - Universal LLM Gateway WebSocket Transport * * Provides a persistent WebSocket connection to llm.do gateway * that replaces repeated fetch calls with multiplexed requests. * * Benefits: * - Persistent connection (no repeated handshakes) * - Lower latency for multiple requests * - Unified authentication across all providers * - Automatic provider routing * * @packageDocumentation */ /** * WebSocket message types (Cloudflare AI Gateway protocol) */ export interface UniversalRequest { type: 'universal.create'; request: { eventId: string; provider: string; endpoint: string; headers?: Record; query?: Record; }; } export interface UniversalCreated { type: 'universal.created'; eventId: string; metadata?: { cacheStatus?: string; requestId?: string; }; response: { status: number; headers?: Record; body: unknown; }; } export interface UniversalStream { type: 'universal.stream'; eventId: string; chunk: string | unknown; } export interface UniversalDone { type: 'universal.done'; eventId: string; metadata?: { cacheStatus?: string; requestId?: string; usage?: { promptTokens?: number; completionTokens?: number; totalTokens?: number; }; }; } export interface UniversalError { type: 'universal.error'; eventId: string; error: { message: string; code?: string; }; } export type GatewayMessage = UniversalCreated | UniversalStream | UniversalDone | UniversalError; /** * Connection state */ type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'closed'; /** * llm.do configuration */ export interface LLMConfig { /** llm.do WebSocket URL (default: wss://llm.do/ws) */ url: string; /** Authentication token (DO_TOKEN) */ token: string; /** Auto-reconnect on disconnect (default: true) */ autoReconnect?: boolean | undefined; /** Max reconnect attempts (default: 5) */ maxReconnectAttempts?: number | undefined; /** Reconnect delay in ms (default: 1000) */ reconnectDelay?: number | undefined; } /** * llm.do WebSocket client * * Maintains a persistent WebSocket connection and provides a fetch-compatible * interface that routes requests through llm.do gateway. * * @example * ```ts * const llm = new LLM({ token: process.env.DO_TOKEN }) * * await llm.connect() * * // Use as fetch replacement * const response = await llm.fetch('https://api.openai.com/v1/chat/completions', { * method: 'POST', * body: JSON.stringify({ ... }) * }) * ``` */ export declare class LLM { private ws; private state; private pendingRequests; private connectPromise; private reconnectAttempts; private eventIdCounter; readonly config: Required> & { autoReconnect: boolean; maxReconnectAttempts: number; reconnectDelay: number; }; constructor(config: LLMConfig); /** * Get the WebSocket URL */ get wsUrl(): string; /** * Get current connection state */ get connectionState(): ConnectionState; /** * Check if connected */ get isConnected(): boolean; /** * Connect to llm.do */ connect(): Promise; private doConnect; private handleDisconnect; private handleMessage; /** * Generate a unique event ID */ private generateEventId; /** * Detect provider from URL */ private detectProvider; /** * Extract endpoint path from URL */ private extractEndpoint; /** * Fetch-compatible interface that routes through llm.do * * @example * ```ts * const response = await llm.fetch('https://api.openai.com/v1/chat/completions', { * method: 'POST', * headers: { 'Content-Type': 'application/json' }, * body: JSON.stringify({ * model: 'gpt-4o', * messages: [{ role: 'user', content: 'Hello!' }] * }) * }) * ``` */ fetch(url: string | URL, init?: RequestInit): Promise; /** * Create a fetch function bound to this connection * * Returns a function that can be used as a drop-in replacement for fetch * in AI SDK provider configurations. */ createFetch(): typeof fetch; /** * Close the WebSocket connection */ close(): void; } /** * Get or create the default llm.do connection * * Uses environment variables for configuration: * - LLM_URL: WebSocket URL (default: wss://llm.do/ws) * - DO_TOKEN: Authentication token * * @example * ```ts * const llm = getLLM() * await llm.connect() * const response = await llm.fetch(url, options) * ``` */ export declare function getLLM(config?: LLMConfig): LLM; /** * Create a fetch function that uses llm.do WebSocket * * Drop-in replacement for the custom fetch in provider configuration. * Automatically connects on first use. * * @example * ```ts * import { createOpenAI } from '@ai-sdk/openai' * import { createLLMFetch } from 'ai-providers' * * const openai = createOpenAI({ * apiKey: 'llm.do', // Placeholder - llm.do handles auth * baseURL: 'https://api.openai.com/v1', * fetch: createLLMFetch() * }) * ``` */ export declare function createLLMFetch(config?: LLMConfig): typeof fetch; export {}; //# sourceMappingURL=llm.do.d.ts.map