/** * AI Streaming Server Extension * Server-Sent Events (SSE) and WebSocket support for AI chat streaming * * Features: * - SSE streaming for AI responses * - WebSocket for bi-directional chat * - Message queue management * - Rate limiting for AI APIs * - Response caching * - Conversation context * - Token-based streaming */ import { Request, Response, NextFunction } from 'express'; import { EventEmitter } from 'events'; export interface AIStreamConfig { /** SSE endpoint path */ ssePath?: string; /** WebSocket path */ wsPath?: string; /** Rate limit (requests per minute) */ rateLimit?: number; /** Cache TTL (seconds) */ cacheTTL?: number; /** Max concurrent streams */ maxConcurrentStreams?: number; /** Enable debug logging */ debug?: boolean; } export interface StreamMessage { id: string; type: 'token' | 'message' | 'error' | 'done'; data: string | any; timestamp: number; conversationId?: string; } export interface ConversationContext { id: string; messages: Array<{ role: string; content: string; }>; createdAt: number; lastActivity: number; tokenCount: number; } export declare class AIStreamingServer extends EventEmitter { private config; private clients; private conversations; private rateLimitMap; private cache; private activeStreams; constructor(config?: AIStreamConfig); /** * SSE Middleware - Stream AI responses */ sseMiddleware(): (req: Request, res: Response, next: NextFunction) => void; /** * Send SSE message to client */ send(clientId: string, message: StreamMessage): boolean; /** * Broadcast to all clients */ broadcast(message: StreamMessage): void; /** * Stream AI response (token by token) */ streamAIResponse(clientId: string, aiStream: AsyncIterable, conversationId?: string): Promise; /** * Rate limiting middleware */ rateLimitMiddleware(): (req: Request, res: Response, next: NextFunction) => void; /** * Cache AI response */ cacheResponse(key: string, data: any, ttl?: number): void; /** * Get cached response */ getCachedResponse(key: string): any | null; /** * Create conversation context */ createConversation(id?: string): ConversationContext; /** * Add message to conversation */ addMessage(conversationId: string, role: string, content: string): void; /** * Get conversation context */ getConversation(conversationId: string): ConversationContext | null; /** * Clear old conversations (cleanup) */ cleanupConversations(maxAge?: number): void; /** * Estimate token count (rough approximation) */ private estimateTokens; /** * Generate unique ID */ private generateId; /** * Get stats */ getStats(): any; } export default AIStreamingServer;