/** * Distributed Inference Client * * Client-side integration with Cloudflare Workers distributed inference. * Provides seamless connection to edge workers for large model inference. * * Features: * - Automatic worker discovery and health monitoring * - Connection pooling with WebSocket/WebTransport * - Request multiplexing for efficient bandwidth usage * - Graceful degradation on network issues * - Real-time latency tracking and worker selection */ /** * Connection state for distributed workers */ type ConnectionState$1 = 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'error'; /** * Worker health and status */ interface WorkerStatus { /** Worker endpoint URL */ endpoint: string; /** Current connection state */ state: ConnectionState$1; /** Whether worker is healthy */ healthy: boolean; /** Last health check timestamp */ lastHealthCheck: number; /** Average latency in ms */ avgLatencyMs: number; /** Latency percentiles */ latencyP50: number; latencyP95: number; latencyP99: number; /** Request success rate (0-1) */ successRate: number; /** Current queue depth */ queueDepth: number; /** Worker capabilities */ capabilities: { maxBatchSize: number; maxSeqLength: number; supportsFP16: boolean; supportsKVCache: boolean; models: string[]; }; /** Worker metadata */ region: string; workerId: string; } /** * Configuration for distributed client */ interface DistributedClientConfig { /** Worker endpoints to connect to */ endpoints: string[]; /** Primary coordinator endpoint */ coordinatorEndpoint?: string; /** API key for authentication */ apiKey?: string; /** Connection timeout in ms */ connectionTimeoutMs?: number; /** Request timeout in ms */ requestTimeoutMs?: number; /** Health check interval in ms */ healthCheckIntervalMs?: number; /** Maximum concurrent requests per worker */ maxConcurrentRequests?: number; /** Enable WebSocket connections */ enableWebSocket?: boolean; /** Enable WebTransport (if available) */ enableWebTransport?: boolean; /** Retry configuration */ retry?: { maxRetries: number; baseDelayMs: number; maxDelayMs: number; backoffMultiplier: number; }; /** Callbacks */ onConnectionChange?: (endpoint: string, state: ConnectionState$1) => void; onWorkerHealthChange?: (status: WorkerStatus) => void; onError?: (error: Error, endpoint: string) => void; } /** * Options for distributed inference requests */ interface DistributedInferenceOptions { /** Model to use */ model?: string; /** Maximum tokens to generate */ maxTokens?: number; /** Temperature for sampling */ temperature?: number; /** Top-p sampling */ topP?: number; /** Top-k sampling */ topK?: number; /** Stop sequences */ stopSequences?: string[]; /** Enable streaming */ stream?: boolean; /** Use FP16 for hidden state transfer */ useFP16?: boolean; /** Enable speculative decoding */ speculative?: boolean; /** Preferred worker region */ preferredRegion?: string; /** Request priority (higher = more urgent) */ priority?: number; /** Timeout for this specific request */ timeoutMs?: number; /** Abort signal */ signal?: AbortSignal; } /** * Result from distributed inference */ interface DistributedResult { /** Generated text */ text: string; /** Token IDs */ tokens: number[]; /** Generation stats */ stats: { totalTokens: number; promptTokens: number; completionTokens: number; totalLatencyMs: number; firstTokenLatencyMs: number; tokensPerSecond: number; }; /** Worker that processed the request */ worker: { endpoint: string; region: string; workerId: string; }; /** Speculative decoding stats (if enabled) */ speculative?: { draftTokens: number; acceptedTokens: number; acceptanceRate: number; }; /** Cache stats */ cache?: { kvCacheHit: boolean; embeddingCacheHit: boolean; savedComputeMs: number; }; } /** * Distributed Inference Client * * Manages connections to multiple edge workers and routes requests * to the optimal worker based on health, latency, and load. */ declare class DistributedClient { private config; private connections; private workerStatus; private latencyTrackers; private healthCheckInterval; private requestCounter; private isShutdown; constructor(config: DistributedClientConfig); /** * Initialize a worker connection */ private initializeWorker; /** * Connect to a worker via WebSocket */ connect(endpoint: string): Promise; /** * Connect via WebSocket */ private connectWebSocket; /** * Handle incoming WebSocket message */ private handleMessage; /** * Stream event handlers */ private streamHandlers; private dispatchStreamEvent; /** * Schedule reconnection attempt */ private scheduleReconnect; /** * Update connection state and notify */ private updateConnectionState; /** * Update worker status */ private updateWorkerStatus; /** * Record latency sample */ private recordLatency; /** * Start periodic health checks */ private startHealthChecks; /** * Check health of a worker */ checkHealth(endpoint: string): Promise; /** * Select the best worker for a request */ selectWorker(options?: DistributedInferenceOptions): string | null; /** * Generate text using distributed inference */ generate(prompt: string, options?: DistributedInferenceOptions): Promise; /** * Generate via WebSocket connection */ private generateViaWebSocket; /** * Generate via HTTP fallback */ private generateViaHTTP; /** * Stream generation with token callback */ stream(prompt: string, options?: DistributedInferenceOptions): AsyncGenerator; /** * Get all worker statuses */ getWorkerStatuses(): WorkerStatus[]; /** * Get healthy worker count */ getHealthyWorkerCount(): number; /** * Disconnect from a specific worker */ disconnect(endpoint: string): void; /** * Disconnect from all workers */ disconnectAll(): void; /** * Shutdown the client */ shutdown(): void; } /** * Pre-configured client presets */ declare const DISTRIBUTED_PRESETS: { /** Single region, low latency */ readonly singleRegion: { readonly connectionTimeoutMs: 5000; readonly requestTimeoutMs: 30000; readonly healthCheckIntervalMs: 15000; readonly maxConcurrentRequests: 20; readonly enableWebSocket: true; readonly retry: { readonly maxRetries: 2; readonly baseDelayMs: 500; readonly maxDelayMs: 5000; readonly backoffMultiplier: 2; }; }; /** Multi-region, high availability */ readonly multiRegion: { readonly connectionTimeoutMs: 10000; readonly requestTimeoutMs: 60000; readonly healthCheckIntervalMs: 30000; readonly maxConcurrentRequests: 10; readonly enableWebSocket: true; readonly retry: { readonly maxRetries: 5; readonly baseDelayMs: 1000; readonly maxDelayMs: 30000; readonly backoffMultiplier: 2; }; }; /** Development/testing */ readonly development: { readonly connectionTimeoutMs: 30000; readonly requestTimeoutMs: 120000; readonly healthCheckIntervalMs: 60000; readonly maxConcurrentRequests: 5; readonly enableWebSocket: false; readonly retry: { readonly maxRetries: 1; readonly baseDelayMs: 1000; readonly maxDelayMs: 5000; readonly backoffMultiplier: 1; }; }; }; /** * Model Router * * Intelligent routing decisions for model inference requests. * Determines optimal execution path based on: * - Model size and capabilities * - Device resources (memory, GPU) * - Network experiences * - User preferences * - Cost optimization */ /** * Routing strategy type */ type RoutingStrategy = 'local-first' | 'edge-first' | 'cost-optimized' | 'latency-optimized' | 'quality-optimized' | 'adaptive' | 'hybrid'; /** * Inference source */ type InferenceSource = 'local-wasm' | 'local-webgpu' | 'edge' | 'cloud'; /** * Model capability requirements */ interface ModelCapabilities { /** Minimum required memory in MB */ minMemoryMB: number; /** Whether WebGPU is required */ requiresWebGPU: boolean; /** Whether model can run in WASM */ supportsWASM: boolean; /** Maximum sequence length supported locally */ maxLocalSeqLength: number; /** Model parameter count */ parameterCount: number; /** Quantization format */ quantization: 'fp32' | 'fp16' | 'int8' | 'int4'; /** Whether streaming is supported */ supportsStreaming: boolean; } /** * Device capabilities detected at runtime */ interface DeviceCapabilities { /** Available memory in MB */ availableMemoryMB: number; /** Whether WebGPU is available */ hasWebGPU: boolean; /** WebGPU device limits (if available) */ webGPULimits?: { maxBufferSize: number; maxStorageBufferBindingSize: number; maxComputeWorkgroupSizeX: number; }; /** Whether WASM SIMD is available */ hasWASMSIMD: boolean; /** Number of logical CPUs */ cpuCores: number; /** Device type */ deviceType: 'desktop' | 'mobile' | 'tablet' | 'unknown'; /** Estimated compute power (relative score) */ computeScore: number; } /** * Network experiences */ interface NetworkConditions { /** Effective connection type */ effectiveType: 'slow-2g' | '2g' | '3g' | '4g' | '5g' | 'wifi' | 'unknown'; /** Estimated bandwidth in Mbps */ bandwidthMbps: number; /** Round-trip time in ms */ rttMs: number; /** Whether we're currently offline */ isOffline: boolean; /** Whether data saver is enabled */ dataSaverEnabled: boolean; } /** * Latency statistics */ interface LatencyStats { /** Source type */ source: InferenceSource; /** Average latency in ms */ avgLatencyMs: number; /** P50 latency */ p50Ms: number; /** P95 latency */ p95Ms: number; /** P99 latency */ p99Ms: number; /** Sample count */ samples: number; /** Success rate */ successRate: number; /** Tokens per second */ avgTokensPerSecond: number; } /** * Route decision */ interface RouteDecision { /** Primary source for inference */ primary: InferenceSource; /** Alias for primary (for backwards compatibility) */ source?: InferenceSource; /** Fallback sources in order of preference */ fallbacks: InferenceSource[]; /** Reasoning for the decision */ reason: string; /** Confidence score (0-1) */ confidence: number; /** Estimated latency for primary source */ estimatedLatencyMs: number; /** Estimated tokens per second */ estimatedTPS: number; /** Estimated cost for this routing decision */ estimatedCost?: number; /** Emotion-aware quality multiplier (0-1) */ emotionQualityMultiplier?: number; /** Whether to use hybrid mode */ hybrid?: { /** Tokens to generate locally before switching */ localTokens: number; /** Switch threshold (when to hand off to edge) */ switchThreshold: number; }; /** Model modifications */ modelAdjustments?: { /** Use quantized version */ useQuantized: boolean; /** Reduce max tokens */ maxTokens?: number; /** Use smaller batch size */ batchSize?: number; }; } /** * Model router configuration */ interface ModelRouterConfig { /** Default routing strategy */ defaultStrategy: RoutingStrategy; /** Model-specific overrides */ modelOverrides?: Record; /** Latency thresholds */ thresholds?: { /** Max acceptable local latency before switching to edge */ maxLocalLatencyMs: number; /** Max acceptable edge latency before switching to cloud */ maxEdgeLatencyMs: number; /** Minimum tokens per second for acceptable experience */ minTokensPerSecond: number; }; /** Cost weights for optimization */ costWeights?: { /** Weight for edge compute costs */ edgeCompute: number; /** Weight for bandwidth costs */ bandwidth: number; /** Weight for latency */ latency: number; }; /** Enable adaptive learning */ enableAdaptiveLearning?: boolean; /** Callback when route decision is made */ onRouteDecision?: (decision: RouteDecision) => void; } /** * Initialization config for ModelRouter. * Includes runtime-detected capabilities and legacy aliases. */ interface ModelRouterInitConfig extends Partial { /** Device capabilities (optional; can be detected if omitted) */ deviceCapabilities?: DeviceCapabilities; /** Network experiences override (optional; can be detected if omitted) */ networkConditions?: NetworkConditions; /** Model capability map keyed by model id */ modelCapabilities?: Record; /** Legacy alias for defaultStrategy */ strategy?: RoutingStrategy; } /** * Model Router * * Makes intelligent routing decisions for model inference. */ declare class ModelRouter { private config; private deviceCapabilities; private networkConditions; private latencyHistory; private modelCapabilities; constructor(config?: ModelRouterInitConfig); /** * Initialize latency history with defaults */ private initializeLatencyHistory; /** * Get default latency estimate for source */ private getDefaultLatency; /** * Get default TPS estimate for source */ private getDefaultTPS; /** * Detect device capabilities */ detectDeviceCapabilities(): Promise; /** * Detect WASM SIMD support */ private detectWASMSIMD; /** * Detect device type */ private detectDeviceType; /** * Calculate compute score */ private calculateComputeScore; /** * Detect network experiences */ detectNetworkConditions(): NetworkConditions; /** * Register model capabilities */ registerModel(modelId: string, capabilities: ModelCapabilities): void; /** * Record latency observation */ recordLatency(source: InferenceSource, latencyMs: number, success: boolean, tokensPerSecond?: number): void; /** * Get route decision for a model request */ route(modelId: string, options?: { preferLatency?: boolean; preferQuality?: boolean; strategy?: RoutingStrategy; maxTokens?: number; streamingRequired?: boolean; qualityPriority?: number; latencyPriority?: number; }): RouteDecision | null; /** * Async version of route for backward compatibility */ routeAsync(modelId: string, options?: { strategy?: RoutingStrategy; maxTokens?: number; streamingRequired?: boolean; qualityPriority?: number; latencyPriority?: number; }): Promise; /** * Check if model can run locally */ private canRunLocally; /** * Route with local-first strategy */ private routeLocalFirst; /** * Route with edge-first strategy */ private routeEdgeFirst; /** * Route with cost optimization */ private routeCostOptimized; /** * Route with latency optimization */ private routeLatencyOptimized; /** * Route with quality optimization */ private routeQualityOptimized; /** * Route with hybrid mode */ private routeHybrid; /** * Route adaptively based on all factors */ private routeAdaptive; /** * Create route decision */ private routingDecisionCount; private createDecision; private lastModelId; /** * Get latency statistics */ getLatencyStats(): Map; /** * Get device capabilities */ getDeviceCapabilities(): DeviceCapabilities | null; /** * Get network experiences */ getNetworkConditions(): NetworkConditions | null; /** * Estimate cost for inference on a given source */ estimateCost(modelId: string, source: InferenceSource, tokenCount: number): number; /** * Get routing statistics and metrics */ getRoutingStats(): { totalDecisions: number; sourceStats: Record; }; /** * Update device capabilities (for testing and dynamic updates) */ updateDeviceCapabilities(capabilities: DeviceCapabilities): void; /** * Register multiple models at once */ registerModels(models: Record): void; } /** * Pre-configured routing strategies */ declare const ROUTING_STRATEGIES: { /** Prioritize local execution for privacy */ readonly privacy: { readonly defaultStrategy: RoutingStrategy; readonly thresholds: { readonly maxLocalLatencyMs: 10000; readonly maxEdgeLatencyMs: 5000; readonly minTokensPerSecond: 2; }; }; /** Prioritize speed */ readonly performance: { readonly defaultStrategy: RoutingStrategy; readonly thresholds: { readonly maxLocalLatencyMs: 2000; readonly maxEdgeLatencyMs: 1000; readonly minTokensPerSecond: 20; }; }; /** Minimize costs */ readonly economy: { readonly defaultStrategy: RoutingStrategy; readonly thresholds: { readonly maxLocalLatencyMs: 15000; readonly maxEdgeLatencyMs: 10000; readonly minTokensPerSecond: 1; }; }; /** Balanced approach */ readonly balanced: { readonly defaultStrategy: RoutingStrategy; readonly thresholds: { readonly maxLocalLatencyMs: 5000; readonly maxEdgeLatencyMs: 2000; readonly minTokensPerSecond: 5; }; }; }; /** * Edge Inference Adapter (Domain-Agnostic) * * Unified interface that bridges local inference engines (WebGPU, WASM) * with distributed edge workers. Provides seamless fallback and hybrid * execution patterns. Fully generic and extensible for any domain. * * Features: * - Domain-agnostic routing and inference * - Pluggable context adapters for domain customization * - Flexible quality scoring and routing customization * - Works with any model, domain, or application */ /** * Custom context that can be provided for domain-specific behavior * This is completely optional and extensible */ interface InferenceContext { /** Domain-specific context data */ [key: string]: unknown; } /** * Inference request options */ interface InferenceRequest$2 { /** Input prompt */ prompt: string; /** System prompt (optional) */ systemPrompt?: string; /** Model ID to use */ modelId?: string; /** Maximum tokens to generate */ maxTokens?: number; /** Temperature for sampling */ temperature?: number; /** Top-p sampling */ topP?: number; /** Top-k sampling */ topK?: number; /** Stop sequences */ stopSequences?: string[]; /** Whether to stream the response */ stream?: boolean; /** Abort signal for cancellation */ signal?: AbortSignal; /** Force specific inference source */ forceSource?: InferenceSource; /** Priority (higher = more important) */ priority?: number; /** Custom metadata */ metadata?: Record; } /** * Inference result */ interface InferenceResult$1 { /** Generated text */ text: string; /** Model used */ model: string; /** Source used for inference */ source: InferenceSource; /** Token usage */ usage: { promptTokens: number; completionTokens: number; totalTokens: number; }; /** Timing information */ timing: { /** Total time in ms */ totalMs: number; /** Time to first token in ms */ firstTokenMs: number; /** Tokens per second */ tokensPerSecond: number; }; /** Whether result came from cache */ cached: boolean; /** Finish reason */ finishReason: 'stop' | 'length' | 'content_filter' | 'error'; /** Original route decision */ routeDecision?: RouteDecision; } /** * Streaming chunk */ interface StreamChunk { /** Token text */ text: string; /** Token index */ index: number; /** Source being used */ source: InferenceSource; /** Whether this is the final chunk */ done: boolean; /** Cumulative text so far */ cumulative?: string; /** Finish reason (only on last chunk) */ finishReason?: InferenceResult$1['finishReason']; } /** * Local inference engine interface */ interface LocalInferenceEngine { /** Engine type */ type: 'webgpu' | 'wasm'; /** Whether engine is initialized */ isInitialized(): boolean; /** Initialize the engine */ initialize(): Promise; /** Check if model is loaded */ hasModel(modelId: string): boolean; /** Load a model */ loadModel(modelId: string): Promise; /** Unload a model */ unloadModel(modelId: string): Promise; /** Generate text */ generate(request: InferenceRequest$2): Promise; /** Stream text generation */ stream(request: InferenceRequest$2): AsyncGenerator; /** Get engine status */ getStatus(): { initialized: boolean; loadedModels: string[]; memoryUsage: number; }; } /** * Adapter configuration */ interface EdgeInferenceAdapterConfig { /** Model router instance */ router: ModelRouter; /** Distributed client instance */ distributedClient: DistributedClient; /** Local WebGPU engine (optional) */ webgpuEngine?: LocalInferenceEngine; /** Local WASM engine (optional) */ wasmEngine?: LocalInferenceEngine; /** Default model ID */ defaultModel?: string; /** Enable caching */ enableCache?: boolean; /** Cache TTL in ms */ cacheTTL?: number; /** Maximum retry attempts */ maxRetries?: number; /** Retry delay in ms */ retryDelay?: number; /** Enable telemetry */ enableTelemetry?: boolean; /** Callback on inference complete */ onComplete?: (result: InferenceResult$1) => void; /** Callback on error */ onError?: (error: Error, source: InferenceSource) => void; /** Callback on fallback */ onFallback?: (from: InferenceSource, to: InferenceSource, reason: string) => void; /** * Optional custom context for domain-specific behavior * This enables extensibility without modifying the adapter */ customContext?: InferenceContext; /** * Custom routing decision hook * Allows domains to customize routing based on their needs */ customRouting?: (decision: RouteDecision, context?: InferenceContext) => RouteDecision; /** * Custom quality scoring hook * Allows domains to compute quality scores based on their metrics */ customQualityScore?: (options: { model: string; source: InferenceSource; context?: InferenceContext; }) => number; } /** * Edge Inference Adapter * * Provides a unified interface for inference that automatically * routes between local and distributed execution. */ declare class EdgeInferenceAdapter { private config; private cache; private initialized; private activeRequests; private totalRequests; private successfulRequests; constructor(config: EdgeInferenceAdapterConfig); /** * Initialize the adapter */ initialize(): Promise; /** * Generate text completion */ generate(request: InferenceRequest$2): Promise; /** * Stream text generation */ stream(request: InferenceRequest$2): AsyncGenerator; /** * Execute inference with fallback chain */ private executeWithFallback; /** * Execute on specific source */ private executeOnSource; /** * Execute on local engine */ private executeLocal; /** * Execute on distributed edge workers */ private executeDistributed; /** * Execute on cloud (OpenAI-compatible endpoint) */ private executeCloud; /** * Convert distributed result to adapter result */ private convertDistributedResult; /** * Create stream generator for source */ private createStreamGenerator; /** * Estimate token count (simple approximation) */ private estimateTokens; /** * Delay helper */ private delay; /** * Ensure adapter is initialized */ private ensureInitialized; /** * Get adapter statistics */ getStats(): { initialized: boolean; activeRequests: number; totalRequests: number; successRate: number; localEngines: { webgpu: boolean; wasm: boolean; }; }; /** * Clear cache */ clearCache(): void; /** * Get router */ getRouter(): ModelRouter; /** * Get distributed client */ getDistributedClient(): DistributedClient; /** * Get optimal route for a model based on current context * Uses custom routing hook if provided, otherwise default routing */ getRoute(modelId: string): RouteDecision; /** * Get optimal configuration for a model */ getOptimalConfig(modelId: string, options?: { quality?: 'low' | 'balanced' | 'high'; }): Record; /** * Get quality score for a routing decision * Uses custom quality scoring if provided, otherwise generic scoring */ getQualityScore(options: { model: string; source: InferenceSource; context?: InferenceContext; }): number; /** * Get hybrid inference options */ getHybridOptions(options: { model: string; quality?: 'low' | 'balanced' | 'high'; }): Record; /** * Set custom context for domain-specific behavior * For example: emotional context, user preferences, domain metrics */ setContext(context: InferenceContext): void; /** * Get current context */ getContext(): InferenceContext | undefined; /** * Update context (backwards compatible alias) * @deprecated Use setContext() instead */ setEmotionContext(_context: unknown): void; setEmotionContext(_emotion: { category: string; intensity: number; }): void; } /** * Create adapter with default configuration */ declare function createEdgeInferenceAdapter(router: ModelRouter, distributedClient: DistributedClient, options?: Partial): EdgeInferenceAdapter; /** * Streaming Client * * Enhanced streaming client with advanced features: * - Backpressure handling * - Automatic reconnection * - Token buffering * - Stream merging (for hybrid mode) * - Quality of Service guarantees */ /** * Stream configuration */ interface StreamConfig { /** Buffer size for tokens */ bufferSize?: number; /** Enable backpressure handling */ enableBackpressure?: boolean; /** High water mark for backpressure (pause threshold) */ highWaterMark?: number; /** Low water mark for backpressure (resume threshold) */ lowWaterMark?: number; /** Enable automatic reconnection */ autoReconnect?: boolean; /** Maximum reconnection attempts */ maxReconnectAttempts?: number; /** Reconnection delay in ms */ reconnectDelay?: number; /** Timeout for stream inactivity */ inactivityTimeout?: number; /** Enable stream merging for hybrid mode */ enableMerging?: boolean; } /** * Stream state */ type StreamState = 'connecting' | 'open' | 'paused' | 'resuming' | 'closing' | 'closed' | 'error' | 'reconnecting'; /** * Stream event */ interface StreamEvent { type: 'token' | 'done' | 'error' | 'state' | 'stats'; timestamp: number; data: unknown; } /** * Token event */ interface TokenEvent { /** Token text */ token: string; /** Token index */ index: number; /** Latency since last token */ latencyMs: number; /** Source of the token */ source: 'local' | 'edge' | 'cloud'; /** Logprob if available */ logprob?: number; } /** * Stream statistics */ interface StreamStats { /** Total tokens received */ tokenCount: number; /** Total characters received */ charCount: number; /** Tokens per second */ tokensPerSecond: number; /** Characters per second */ charsPerSecond: number; /** Average token latency */ avgTokenLatencyMs: number; /** Time to first token */ firstTokenLatencyMs: number; /** Total stream duration */ durationMs: number; /** Reconnection count */ reconnectCount: number; /** Paused count (backpressure) */ pausedCount: number; /** Buffer utilization (0-1) */ bufferUtilization: number; } /** * Stream controller for flow control */ interface StreamController { /** Pause the stream */ pause(): void; /** Resume the stream */ resume(): void; /** Cancel the stream */ cancel(): void; /** Get current state */ getState(): StreamState; /** Get statistics */ getStats(): StreamStats; /** Check if stream is active */ isActive(): boolean; } /** * Stream observer callbacks */ interface StreamObserver { /** Called for each token */ onToken?: (event: TokenEvent) => void; /** Called when stream completes */ onComplete?: (stats: StreamStats) => void; /** Called on error */ onError?: (error: Error) => void; /** Called on state change */ onStateChange?: (state: StreamState, previousState: StreamState) => void; /** Called on reconnection */ onReconnect?: (attempt: number) => void; /** Called when backpressure activates */ onBackpressure?: (paused: boolean) => void; } /** * Streaming Client * * Provides enhanced streaming with backpressure, reconnection, * and quality guarantees. */ declare class StreamingClient { private config; private state; private buffer; private stats; private reconnectAttempts; private inactivityTimer; private lastTokenTime; private abortController; constructor(config?: StreamConfig); /** * Create empty stats object */ private createEmptyStats; /** * Stream from a source */ stream(source: AsyncGenerator | ReadableStream, observer: StreamObserver): Promise; /** * Stream from WebSocket */ streamFromWebSocket(ws: WebSocket, observer: StreamObserver): Promise; /** * Stream from Server-Sent Events */ streamFromSSE(url: string, observer: StreamObserver, options?: RequestInit): Promise; /** * Merge multiple streams (for hybrid mode) */ mergeStreams(streams: AsyncGenerator[]): AsyncGenerator; /** * Wrap iterator.next() with index */ private wrapNext; /** * Convert source to async generator */ private toAsyncGenerator; /** * Handle backpressure state change */ private handleBackpressure; /** * Reset inactivity timer */ private resetInactivityTimer; /** * Clear inactivity timer */ private clearInactivityTimer; /** * Attempt reconnection */ private attemptReconnect; /** * Create stream controller */ private createController; /** * Reset client state */ private reset; /** * Get current state */ getState(): StreamState; /** * Get current stats */ getStats(): StreamStats; } /** * Create preconfigured streaming client */ declare const STREAMING_PRESETS: { /** High performance streaming */ performance: StreamConfig; /** Memory efficient streaming */ lowMemory: StreamConfig; /** Reliable streaming */ reliable: StreamConfig; }; /** * Connection Manager * * Manages WebSocket and HTTP connections to edge workers: * - Connection pooling * - Lifecycle management * - Health monitoring * - Graceful degradation * - Protocol negotiation */ /** * Connection type */ type ConnectionType = 'websocket' | 'http' | 'webtransport'; /** * Connection state */ type ConnectionState = 'connecting' | 'connected' | 'authenticated' | 'idle' | 'active' | 'draining' | 'disconnecting' | 'disconnected' | 'error'; /** * Connection quality */ interface ConnectionQuality { /** Round-trip time in ms */ rttMs: number; /** Packet loss rate (0-1) */ packetLoss: number; /** Jitter in ms */ jitterMs: number; /** Quality score (0-100) */ score: number; /** Last measurement timestamp */ measuredAt: number; } /** * Connection info */ interface ConnectionInfo { /** Connection ID */ id: string; /** Worker endpoint URL */ endpoint: string; /** Connection type */ type: ConnectionType; /** Current state */ state: ConnectionState; /** Connection quality */ quality: ConnectionQuality; /** Creation timestamp */ createdAt: number; /** Last activity timestamp */ lastActivityAt: number; /** Request count */ requestCount: number; /** Error count */ errorCount: number; /** Whether connection is authenticated */ authenticated: boolean; /** Metadata */ metadata: Record; } /** * Pool configuration */ interface PoolConfig { /** Minimum connections to maintain */ minConnections: number; /** Maximum connections per endpoint */ maxConnectionsPerEndpoint: number; /** Maximum total connections */ maxTotalConnections: number; /** Idle timeout in ms */ idleTimeout: number; /** Connection timeout in ms */ connectionTimeout: number; /** Maximum request age in ms */ maxRequestAge: number; /** Enable connection warming */ enableWarming: boolean; /** Warming interval in ms */ warmingInterval: number; /** Quality check interval in ms */ qualityCheckInterval: number; /** Minimum quality score (0-100) */ minQualityScore: number; } /** * Protocol capabilities */ interface ProtocolCapabilities { /** Supported message types */ messageTypes: string[]; /** Supports streaming */ streaming: boolean; /** Supports binary messages */ binary: boolean; /** Maximum message size */ maxMessageSize: number; /** Supports compression */ compression: boolean; /** Supports multiplexing */ multiplexing: boolean; /** Protocol version */ version: string; } /** * Connection manager configuration */ interface ConnectionManagerConfig { /** Pool configuration */ pool: Partial; /** Preferred connection types in order */ preferredTypes: ConnectionType[]; /** Authentication token provider */ authProvider?: () => Promise; /** Enable automatic quality monitoring */ enableQualityMonitoring: boolean; /** Callback on connection state change */ onStateChange?: (id: string, state: ConnectionState, previousState: ConnectionState) => void; /** Callback on connection quality change */ onQualityChange?: (id: string, quality: ConnectionQuality) => void; /** Callback on connection error */ onError?: (id: string, error: Error) => void; } /** * Managed connection wrapper */ declare class ManagedConnection { private config; readonly info: ConnectionInfo; private socket; private pendingRequests; private messageQueue; private pingInterval; private capabilities; constructor(endpoint: string, type: ConnectionType, config: ConnectionManagerConfig); /** * Connect to endpoint */ connect(): Promise; /** * Connect via WebSocket */ private connectWebSocket; /** * Authenticate the connection */ private authenticate; /** * Send message and wait for response */ request(data: unknown, timeout?: number): Promise; /** * Send WebSocket request */ private sendWebSocketRequest; /** * Send HTTP request */ private sendHttpRequest; /** * Send message (fire and forget) */ send(data: unknown): Promise; /** * Handle incoming message */ private handleMessage; /** * Start heartbeat */ private startHeartbeat; /** * Stop heartbeat */ private stopHeartbeat; /** * Update connection quality */ private updateQuality; /** * Calculate quality score */ private calculateQualityScore; /** * Reject all pending requests */ private rejectPendingRequests; /** * Update connection state */ private updateState; /** * Get pool config with defaults */ private getPoolConfig; /** * Disconnect */ disconnect(): Promise; /** * Check if connection is healthy */ isHealthy(): boolean; /** * Get capabilities */ getCapabilities(): ProtocolCapabilities | null; } /** * Connection Manager * * Manages a pool of connections to edge workers. */ declare class ConnectionManager { private config; private poolConfig; private connections; private endpointConnections; private warmingInterval; private qualityCheckInterval; constructor(config?: Partial); /** * Start the connection manager */ start(): void; /** * Stop the connection manager */ stop(): Promise; /** * Get or create connection to endpoint */ getConnection(endpoint: string, type?: ConnectionType): Promise; /** * Release connection back to pool */ releaseConnection(connection: ManagedConnection): void; /** * Remove connection from pool */ private removeConnection; /** * Get least recently used connection */ private getLeastRecentlyUsedConnection; /** * Warm connections for known endpoints */ private warmConnections; /** * Check connection quality and cleanup */ private checkConnectionQuality; /** * Get all connections info */ getConnections(): ConnectionInfo[]; /** * Get connections for endpoint */ getEndpointConnections(endpoint: string): ConnectionInfo[]; /** * Get pool statistics */ getStats(): { totalConnections: number; healthyConnections: number; activeConnections: number; endpointCount: number; avgQualityScore: number; totalRequests: number; totalErrors: number; }; } /** * Pre-configured connection manager presets */ declare const CONNECTION_PRESETS: { /** High performance for real-time applications */ realtime: Partial; /** Balanced for general use */ balanced: Partial; /** Low resource for mobile/constrained devices */ lowResource: Partial; }; /** * Offline Support Module * * Provides offline-first inference capabilities: * - Service Worker integration * - Request queuing * - Background sync * - Offline model management * - Conflict resolution */ /** * Offline state */ type OfflineState$1 = 'online' | 'offline' | 'syncing' | 'partial'; /** * Queue priority */ type QueuePriority = 'high' | 'typical' | 'low' | 'background'; /** * Queued request */ interface QueuedRequest { /** Request ID */ id: string; /** Request type */ type: 'inference' | 'sync' | 'feedback' | 'custom'; /** Request payload */ payload: unknown; /** Priority */ priority: QueuePriority; /** Created timestamp */ createdAt: number; /** Number of retry attempts */ retryCount: number; /** Maximum retries */ maxRetries: number; /** Expiry timestamp (0 = never) */ expiresAt: number; /** Whether request requires network */ requiresNetwork: boolean; /** Callback URL for background sync */ callbackUrl?: string; /** Metadata */ metadata: Record; } /** * Sync conflict */ interface SyncConflict { /** Request ID */ requestId: string; /** Local data */ local: unknown; /** Remote data */ remote: unknown; /** Conflict type */ type: 'version' | 'content' | 'deleted'; /** Resolution strategy suggestion */ suggestedResolution: 'local' | 'remote' | 'merge' | 'manual'; } /** * Offline model info */ interface OfflineModel { /** Model ID */ id: string; /** Model name */ name: string; /** Model size in bytes */ sizeBytes: number; /** Whether model is cached */ cached: boolean; /** Cache timestamp */ cachedAt: number; /** Last used timestamp */ lastUsedAt: number; /** Model version */ version: string; /** Whether model supports offline */ offlineCapable: boolean; /** Required features */ requiredFeatures: ('webgpu' | 'wasm' | 'simd')[]; } /** * Offline storage quota */ interface StorageQuota { /** Total quota in bytes */ total: number; /** Used storage in bytes */ used: number; /** Available storage in bytes */ available: number; /** Persistent storage granted */ persistent: boolean; } /** * Offline config */ interface OfflineSupportConfig { /** Enable offline mode */ enabled: boolean; /** Queue storage key */ queueStorageKey: string; /** Maximum queue size */ maxQueueSize: number; /** Default request expiry (ms) */ defaultExpiry: number; /** Sync interval (ms) */ syncInterval: number; /** Enable background sync */ enableBackgroundSync: boolean; /** Service worker path */ serviceWorkerPath?: string; /** Model cache name */ modelCacheName: string; /** Maximum cached models */ maxCachedModels: number; /** Callback on state change */ onStateChange?: (state: OfflineState$1) => void; /** Callback on queue change */ onQueueChange?: (queue: QueuedRequest[]) => void; /** Callback on sync conflict */ onConflict?: (conflict: SyncConflict) => Promise<'local' | 'remote' | 'merge'>; /** Callback on sync complete */ onSyncComplete?: (synced: number, failed: number) => void; } /** * Offline Support * * Provides offline-first capabilities for edge inference. */ declare class OfflineSupport { private config; private state; private queue; private syncInterval; private serviceWorkerRegistration; private cachedModels; constructor(config?: Partial); /** * Initialize offline support */ initialize(): Promise; /** * Shutdown offline support */ shutdown(): Promise; /** * Register service worker */ private registerServiceWorker; /** * Handle service worker message */ private handleServiceWorkerMessage; /** * Handle online event */ private handleOnline; /** * Handle offline event */ private handleOffline; /** * Queue a request for later processing */ queueRequest(type: QueuedRequest['type'], payload: unknown, options?: Partial>): Promise; /** * Sort queue by priority */ private sortQueue; /** * Sync queue with server */ syncQueue(): Promise<{ synced: number; failed: number; }>; /** * Process a queued request */ private processQueuedRequest; /** * Handle sync complete from service worker */ private handleSyncComplete; /** * Handle request result from service worker */ private handleRequestResult; /** * Save queue to storage */ private saveQueue; /** * Load queue from storage */ private loadQueue; /** * Load cached models info */ private loadCachedModels; /** * Extract model ID from URL */ private extractModelId; /** * Cache a model for offline use */ cacheModel(modelId: string, modelUrl: string, metadata?: Partial): Promise; /** * Remove a cached model */ uncacheModel(modelId: string): Promise; /** * Evict old models to free up space */ private evictOldModels; /** * Check if model is available offline */ isModelAvailableOffline(modelId: string): boolean; /** * Get cached model info */ getCachedModel(modelId: string): OfflineModel | undefined; /** * Get all cached models */ getCachedModels(): OfflineModel[]; /** * Get storage quota */ getStorageQuota(): Promise; /** * Request persistent storage */ requestPersistentStorage(): Promise; /** * Get current offline state */ getState(): OfflineState$1; /** * Get queued requests */ getQueue(): QueuedRequest[]; /** * Get queue size */ getQueueSize(): number; /** * Clear queue */ clearQueue(): Promise; /** * Remove specific request from queue */ removeFromQueue(requestId: string): Promise; /** * Trigger background sync */ triggerBackgroundSync(): Promise; } /** * Pre-configured offline support presets */ declare const OFFLINE_PRESETS: { /** Aggressive caching for fully offline apps */ offlineFirst: Partial; /** Light caching for mostly online apps */ onlineFirst: Partial; /** No offline support */ disabled: Partial; }; /** * Bandwidth Optimizer * * Optimizes network usage for model inference: * - FP16/FP8 transfer formats * - Compression algorithms * - Adaptive quality based on network * - Partial model loading * - Delta updates */ /** * Compression algorithm */ type CompressionAlgorithm = 'none' | 'gzip' | 'brotli' | 'lz4' | 'zstd'; /** * Transfer format */ type TransferFormat = 'fp32' | 'fp16' | 'fp8' | 'int8' | 'int4'; /** * Quality level */ type QualityLevel = 'ultra' | 'high' | 'medium' | 'low' | 'minimal'; /** * Network profile */ interface NetworkProfile { /** Effective bandwidth in Mbps */ bandwidthMbps: number; /** Round-trip time in ms */ rttMs: number; /** Packet loss rate (0-1) */ packetLoss: number; /** Connection type */ connectionType: 'wifi' | '4g' | '3g' | '2g' | 'slow-2g' | 'unknown'; /** Whether data saver is enabled */ dataSaver: boolean; /** Metered connection */ metered: boolean; } /** * Optimization strategy */ interface OptimizationStrategy { /** Transfer format to use */ format: TransferFormat; /** Compression algorithm */ compression: CompressionAlgorithm; /** Quality level */ quality: QualityLevel; /** Chunk size for streaming */ chunkSize: number; /** Whether to prefetch next chunks */ prefetch: boolean; /** Maximum concurrent requests */ maxConcurrent: number; /** Use delta updates if available */ useDelta: boolean; /** Estimated bandwidth saving (0-1) */ estimatedSaving: number; } /** * Transfer statistics */ interface TransferStats { /** Bytes sent */ bytesSent: number; /** Bytes received */ bytesReceived: number; /** Bytes saved by optimization */ bytesSaved: number; /** Compression ratio */ compressionRatio: number; /** Average transfer speed in bytes/s */ avgSpeed: number; /** Transfer count */ transferCount: number; /** Failed transfers */ failedTransfers: number; } /** * Optimizer configuration */ interface BandwidthOptimizerConfig { /** Enable automatic optimization */ autoOptimize: boolean; /** Preferred compression */ preferredCompression: CompressionAlgorithm; /** Preferred format */ preferredFormat: TransferFormat; /** Quality thresholds */ qualityThresholds: { ultraMinBandwidth: number; highMinBandwidth: number; mediumMinBandwidth: number; lowMinBandwidth: number; }; /** Maximum chunk size in bytes */ maxChunkSize: number; /** Minimum chunk size in bytes */ minChunkSize: number; /** Enable prefetching */ enablePrefetch: boolean; /** Callback on strategy change */ onStrategyChange?: (strategy: OptimizationStrategy) => void; /** Callback on network change */ onNetworkChange?: (profile: NetworkProfile) => void; } /** * Bandwidth Optimizer * * Optimizes network usage for edge inference. */ declare class BandwidthOptimizer { private config; private networkProfile; private currentStrategy; private stats; private networkMonitorInterval; constructor(config?: Partial); /** * Create empty stats */ private createEmptyStats; /** * Start network monitoring */ startMonitoring(intervalMs?: number): void; /** * Stop network monitoring */ stopMonitoring(): void; /** * Detect current network profile */ private detectNetworkProfile; /** * Map effective type to connection type */ private mapEffectiveType; /** * Update network profile */ private updateNetworkProfile; /** * Handle network change event */ private handleNetworkChange; /** * Calculate optimization strategy */ private calculateStrategy; /** * Calculate optimal chunk size */ private calculateChunkSize; /** * Estimate bandwidth saving */ private estimateSaving; /** * Recalculate strategy */ private recalculateStrategy; /** * Get current strategy */ getStrategy(): OptimizationStrategy; /** * Get network profile */ getNetworkProfile(): NetworkProfile; /** * Set manual strategy override */ setStrategy(strategy: Partial): void; /** * Optimize request headers */ getOptimizedHeaders(): Record; /** * Optimize request body */ optimizeBody(body: ArrayBuffer | Blob | string): Promise<{ data: ArrayBuffer | Blob | string; headers: Record; originalSize: number; optimizedSize: number; }>; /** * Get body size */ private getBodySize; /** * Compress data */ private compress; /** * Decompress response */ decompressResponse(response: Response): Promise; /** * Record transfer */ recordTransfer(bytes: number, failed?: boolean): void; /** * Update compression ratio */ private updateCompressionRatio; /** * Get transfer stats */ getStats(): TransferStats; /** * Reset stats */ resetStats(): void; /** * Get quality for current network */ getRecommendedQuality(): QualityLevel; /** * Check if network is suitable for operation */ isNetworkSuitable(minBandwidth: number): boolean; } /** * Pre-configured optimizer presets */ declare const BANDWIDTH_PRESETS: { /** Maximum quality, no optimization */ maxQuality: Partial; /** Balanced quality and bandwidth */ balanced: Partial; /** Aggressive optimization for slow networks */ dataSaver: Partial; }; /** * Model Preloader * * Predictive model loading and cache warming: * - Preload models before they're needed * - LRU-based model eviction * - Priority-based loading queue * - Background weight fetching * - Speculative prefetching */ /** * Preload priority */ type PreloadPriority = 'critical' | 'high' | 'typical' | 'low' | 'background'; /** * Model load state */ type ModelLoadState = 'unknown' | 'queued' | 'downloading' | 'parsing' | 'initializing' | 'ready' | 'error' | 'evicted'; /** * Model metadata */ interface ModelMetadata { /** Model ID */ id: string; /** Human-readable name */ name: string; /** Model version */ version: string; /** Size in bytes */ sizeBytes: number; /** Required features */ requiredFeatures: ('webgpu' | 'wasm' | 'simd')[]; /** Model family (for similar model detection) */ family?: string; /** Tags for matching */ tags?: string[]; /** Model URL */ url?: string; /** CDN URL (for faster loading) */ cdnUrl?: string; } /** * Model load progress */ interface ModelLoadProgress { /** Model ID */ modelId: string; /** Current state */ state: ModelLoadState; /** Download progress (0-1) */ downloadProgress: number; /** Bytes downloaded */ bytesDownloaded: number; /** Total bytes */ totalBytes: number; /** Download speed in bytes/s */ speed: number; /** Estimated time remaining in ms */ estimatedTimeMs: number; /** Error if any */ error?: string; } /** * Preload request */ interface PreloadRequest { /** Model ID */ modelId: string; /** Priority */ priority: PreloadPriority; /** Requested timestamp */ requestedAt: number; /** Callback on ready */ onReady?: () => void; /** Callback on progress */ onProgress?: (progress: ModelLoadProgress) => void; /** Callback on error */ onError?: (error: Error) => void; } /** * Preloader configuration */ interface ModelPreloaderConfig { /** Maximum cache size in bytes */ maxCacheSize: number; /** Maximum models to keep loaded */ maxModels: number; /** Enable predictive prefetching */ enablePredictive: boolean; /** Prediction confidence threshold (0-1) */ predictionThreshold: number; /** Background fetch interval in ms */ backgroundInterval: number; /** CDN base URL */ cdnBaseUrl?: string; /** Callback on model ready */ onModelReady?: (modelId: string) => void; /** Callback on model evicted */ onModelEvicted?: (modelId: string) => void; /** Callback on cache full */ onCacheFull?: () => void; } /** * Model Preloader * * Manages model preloading with predictive prefetching. */ declare class ModelPreloader { private config; private cache; private queue; private activeDownloads; private usagePatterns; private modelRegistry; private backgroundInterval; private currentCacheSize; constructor(config?: Partial); /** * Start the preloader */ start(): void; /** * Stop the preloader */ stop(): void; /** * Register a model */ registerModel(metadata: ModelMetadata): void; /** * Preload a model */ preload(modelId: string, options?: { priority?: PreloadPriority; onReady?: () => void; onProgress?: (progress: ModelLoadProgress) => void; onError?: (error: Error) => void; }): Promise; /** * Sort queue by priority */ private sortQueue; /** * Process the preload queue */ private processQueue; /** * Download a model */ private downloadModel; /** * Evict models to make room */ private evictModels; /** * Evict a specific model */ private evictModel; /** * Record model usage */ recordUsage(modelId: string, contextModelIds?: string[]): void; /** * Trigger predictive prefetching */ private triggerPredictivePrefetch; /** * Process background tasks */ private processBackgroundTasks; /** * Predict models needed based on time */ private predictBasedOnTime; /** * Check if model is ready */ isReady(modelId: string): boolean; /** * Get model load state */ getState(modelId: string): ModelLoadState; /** * Get all cached models */ getCachedModels(): Array<{ modelId: string; state: ModelLoadState; sizeBytes: number; lastAccessed: number; accessCount: number; }>; /** * Get cache statistics */ getStats(): { totalModels: number; readyModels: number; cacheSize: number; maxCacheSize: number; queueSize: number; activeDownloads: number; }; /** * Clear all cached models */ clearCache(): Promise; /** * Warm cache with common models */ warmCache(modelIds: string[]): Promise; } /** * Pre-configured preloader presets */ declare const PRELOADER_PRESETS: { /** Aggressive preloading for fast devices */ aggressive: Partial; /** Conservative for limited devices */ conservative: Partial; /** Balanced for typical devices */ balanced: Partial; }; /** * Cache Coordinator * * Coordinates caching across multiple layers: * - Cross-tab KV cache sharing * - Cache invalidation * - Distributed cache synchronization * - Cache statistics and monitoring */ /** * Cache layer */ type CacheLayer = 'memory' | 'indexeddb' | 'opfs' | 'broadcast' | 'edge'; /** * Cache entry */ interface CacheEntry { /** Cache key */ key: string; /** Cached value */ value: T; /** Created timestamp */ createdAt: number; /** Expires at timestamp (0 = never) */ expiresAt: number; /** Size in bytes */ sizeBytes: number; /** Hit count */ hitCount: number; /** Version for conflict resolution */ version: number; /** Tags for grouped invalidation */ tags: string[]; /** Source layer */ layer: CacheLayer; } /** * Cache invalidation reason */ type InvalidationReason = 'expired' | 'evicted' | 'manual' | 'tag' | 'broadcast' | 'version'; /** * Cache event */ interface CacheEvent { type: 'hit' | 'miss' | 'set' | 'invalidate' | 'sync'; key: string; layer: CacheLayer; timestamp: number; reason?: InvalidationReason; } /** * Cache statistics */ interface CacheStats { /** Total entries */ totalEntries: number; /** Total size in bytes */ totalSizeBytes: number; /** Hit count */ hits: number; /** Miss count */ misses: number; /** Hit rate (0-1) */ hitRate: number; /** Entries per layer */ layerStats: Record; /** Eviction count */ evictions: number; /** Average entry age in ms */ avgAgeMs: number; } /** * KV cache entry for LLM inference */ interface KVCacheEntry { /** Model ID */ modelId: string; /** Prompt hash for cache key */ promptHash: string; /** Key cache tensor */ keys: Float32Array | ArrayBuffer; /** Value cache tensor */ values: Float32Array | ArrayBuffer; /** Sequence length */ seqLength: number; /** Number of layers */ numLayers: number; /** Attention heads */ numHeads: number; /** Head dimension */ headDim: number; /** Created at */ createdAt: number; /** Size in bytes */ sizeBytes: number; } /** * Coordinator configuration */ interface CacheCoordinatorConfig { /** Enable cross-tab sharing */ enableCrossTab: boolean; /** Broadcast channel name */ broadcastChannelName: string; /** Maximum memory cache size in bytes */ maxMemoryCacheSize: number; /** Maximum entries in memory */ maxMemoryEntries: number; /** Default TTL in ms */ defaultTTL: number; /** Enable persistent cache */ enablePersistent: boolean; /** Persistent cache name */ persistentCacheName: string; /** Sync interval for cross-tab in ms */ syncInterval: number; /** Callback on cache event */ onCacheEvent?: (event: CacheEvent) => void; /** Callback on stats update */ onStatsUpdate?: (stats: CacheStats) => void; } /** * Cache Coordinator * * Coordinates caching across memory, persistent storage, and tabs. */ declare class CacheCoordinator { private config; private memoryCache; private broadcastChannel; private syncInterval; private stats; private kvCache; constructor(config?: Partial); /** * Create empty stats */ private createEmptyStats; /** * Initialize the coordinator */ initialize(): Promise; /** * Shutdown the coordinator */ shutdown(): Promise; /** * Get a cached value */ get(key: string, options?: { layers?: CacheLayer[]; }): Promise; /** * Get from specific layer */ private getFromLayer; /** * Get from IndexedDB */ private getFromIndexedDB; /** * Get from OPFS */ private getFromOPFS; /** * Set a cached value */ set(key: string, value: T, options?: { ttl?: number; tags?: string[]; layers?: CacheLayer[]; broadcast?: boolean; }): Promise; /** * Set in specific layer */ private setInLayer; /** * Set in IndexedDB */ private setInIndexedDB; /** * Set in OPFS */ private setInOPFS; /** * Open IndexedDB */ private openDB; /** * Invalidate a cached value */ invalidate(key: string, options?: { layers?: CacheLayer[]; broadcast?: boolean; reason?: InvalidationReason; }): Promise; /** * Invalidate in specific layer */ private invalidateInLayer; /** * Delete from IndexedDB */ private deleteFromIndexedDB; /** * Delete from OPFS */ private deleteFromOPFS; /** * Invalidate by tag */ invalidateByTag(tag: string, options?: { layers?: CacheLayer[]; broadcast?: boolean; }): Promise; /** * Store KV cache for LLM inference */ storeKVCache(entry: KVCacheEntry): Promise; /** * Get KV cache for LLM inference */ getKVCache(modelId: string, promptHash: string): Promise; /** * Handle broadcast message */ private handleBroadcastMessage; /** * Emit cache event */ private emitEvent; /** * Sync stats */ private syncStats; /** * Get cache statistics */ getStats(): CacheStats; /** * Clear all caches */ clear(): Promise; } /** * Pre-configured coordinator presets */ declare const CACHE_PRESETS: { /** High performance with large memory cache */ performance: Partial; /** Memory efficient */ lowMemory: Partial; /** Balanced */ balanced: Partial; }; /** * Metrics Reporter * * Client-side telemetry and performance monitoring: * - Inference latency tracking * - Token throughput monitoring * - Error rate tracking * - Resource usage monitoring * - Custom metric support */ /** * Metric type */ type MetricType = 'counter' | 'gauge' | 'histogram' | 'timing'; /** * Metric value */ interface MetricValue { /** Metric name */ name: string; /** Metric type */ type: MetricType; /** Current value */ value: number; /** Unit of measurement */ unit?: string; /** Labels for dimensional filtering */ labels: Record; /** Timestamp */ timestamp: number; } /** * Histogram bucket */ interface HistogramBucket { /** Upper bound */ le: number; /** Count in bucket */ count: number; } /** * Histogram data */ interface HistogramData { /** Bucket counts */ buckets: HistogramBucket[]; /** Sum of all values */ sum: number; /** Count of observations */ count: number; /** Minimum value */ min: number; /** Maximum value */ max: number; /** Average */ avg: number; /** Percentiles */ percentiles: { p50: number; p90: number; p95: number; p99: number; }; } /** * Performance summary */ interface PerformanceSummary { /** Time period in ms */ periodMs: number; /** Inference metrics */ inference: { totalRequests: number; successfulRequests: number; failedRequests: number; avgLatencyMs: number; p95LatencyMs: number; tokensGenerated: number; avgTokensPerSecond: number; }; /** Resource metrics */ resources: { avgMemoryUsage: number; peakMemoryUsage: number; avgCpuUsage: number; networkBytesSent: number; networkBytesReceived: number; }; /** Cache metrics */ cache: { hitRate: number; missRate: number; evictionCount: number; cacheSize: number; }; /** Error metrics */ errors: { totalErrors: number; errorRate: number; errorsByType: Record; }; } /** * Reporter configuration */ interface MetricsReporterConfig { /** Enable metrics collection */ enabled: boolean; /** Collection interval in ms */ collectionInterval: number; /** Report interval in ms */ reportInterval: number; /** Endpoint for remote reporting */ reportEndpoint?: string; /** API key for remote reporting */ apiKey?: string; /** Enable console logging */ enableConsoleLogging: boolean; /** Maximum stored metrics */ maxStoredMetrics: number; /** Histogram buckets for latency */ latencyBuckets: number[]; /** Callback on metric collected */ onMetricCollected?: (metric: MetricValue) => void; /** Callback on report generated */ onReportGenerated?: (summary: PerformanceSummary) => void; } /** * Metrics Reporter * * Collects and reports performance metrics. */ declare class MetricsReporter { private config; private store; private storedMetrics; private collectionInterval; private reportInterval; private sessionStartTime; private lastReportTime; constructor(config?: Partial); /** * Create empty metric store */ private createEmptyStore; /** * Start metrics collection */ start(): void; /** * Stop metrics collection */ stop(): void; /** * Increment a counter */ increment(name: string, value?: number, labels?: Record): void; /** * Set a gauge value */ gauge(name: string, value: number, labels?: Record): void; /** * Record a histogram observation */ histogram(name: string, value: number, labels?: Record): void; /** * Record a timing */ timing(name: string, durationMs: number, labels?: Record): void; /** * Time a function execution */ time(name: string, fn: () => T | Promise, labels?: Record): Promise; /** * Create a timer for manual timing */ startTimer(name: string, labels?: Record): () => number; /** * Record inference metrics */ recordInference(metrics: { model: string; source: string; success: boolean; latencyMs: number; tokensGenerated: number; promptTokens: number; error?: string; }): void; /** * Record cache metrics */ recordCacheEvent(event: 'hit' | 'miss' | 'eviction', labels?: Record): void; /** * Record network metrics */ recordNetworkTransfer(type: 'sent' | 'received', bytes: number, labels?: Record): void; /** * Record resource usage */ recordResourceUsage(): void; /** * Create key from name and labels */ private createKey; /** * Emit a metric */ private emitMetric; /** * Collect periodic metrics */ private collect; /** * Calculate histogram data */ private calculateHistogram; /** * Generate performance report */ generateReport(): PerformanceSummary; /** * Send report to remote endpoint */ private sendReport; /** * Get all stored metrics */ getStoredMetrics(): MetricValue[]; /** * Get specific counter value */ getCounter(name: string, labels?: Record): number; /** * Get specific gauge value */ getGauge(name: string, labels?: Record): number; /** * Get histogram data */ getHistogramData(name: string, labels?: Record): HistogramData | null; /** * Reset all metrics */ reset(): void; } /** * Pre-configured reporter presets */ declare const METRICS_PRESETS: { /** Detailed metrics for development */ development: Partial; /** Production metrics */ production: Partial; /** Minimal metrics */ minimal: Partial; /** Disabled */ disabled: Partial; }; /** * Error development Module * * Robust error handling and development: * - Retry strategies (exponential backoff, jitter) * - Circuit breaker pattern * - Fallback chains * - Error classification and routing */ /** * Error category */ type ErrorCategory = 'network' | 'timeout' | 'rate_limit' | 'quota' | 'auth' | 'validation' | 'resource' | 'compute' | 'unknown'; /** * Error severity */ type ErrorSeverity = 'recoverable' | 'transient' | 'permanent'; /** * Classified error */ interface ClassifiedError { /** Original error */ original: Error; /** Error category */ category: ErrorCategory; /** Severity level */ severity: ErrorSeverity; /** Is retryable */ retryable: boolean; /** Suggested retry delay in ms */ retryAfterMs?: number; /** Additional context */ context: Record; } /** * Retry strategy */ type RetryStrategy = 'immediate' | 'linear' | 'exponential' | 'exponential-jitter' | 'fibonacci' | 'custom'; /** * Retry configuration */ interface RetryConfig { /** Maximum retry attempts */ maxRetries: number; /** Base delay in ms */ baseDelay: number; /** Maximum delay in ms */ maxDelay: number; /** Retry strategy */ strategy: RetryStrategy; /** Jitter factor (0-1) for exponential-jitter */ jitterFactor: number; /** Multiplier for exponential backoff */ multiplier: number; /** Only retry on specific error categories */ retryCategories?: ErrorCategory[]; /** Callback before retry */ onRetry?: (attempt: number, error: ClassifiedError, delayMs: number) => void; /** Callback on final unmet expectations */ onExhausted?: (error: ClassifiedError, attempts: number) => void; } /** * Circuit breaker state */ type CircuitState = 'closed' | 'open' | 'half-open'; /** * Circuit breaker configuration */ interface CircuitBreakerConfig { /** unmet expectations threshold to open circuit */ failureThreshold: number; /** Success threshold to close circuit in half-open state */ successThreshold: number; /** Time to wait before attempting half-open */ resetTimeoutMs: number; /** Sliding window size for unmet expectations counting */ windowSize: number; /** Callback on state change */ onStateChange?: (from: CircuitState, to: CircuitState) => void; } /** * Fallback definition */ interface Fallback { /** Fallback name */ name: string; /** Fallback function */ fn: () => T | Promise; /** Categories this fallback handles */ categories?: ErrorCategory[]; /** Priority (lower = higher priority) */ priority: number; } /** * development configuration */ interface ErrorRecoveryConfig { /** Retry configuration */ retry: RetryConfig; /** Circuit breaker configuration */ circuitBreaker: CircuitBreakerConfig; /** Enable error classification */ enableClassification: boolean; /** Error pattern matchers */ errorPatterns: ErrorPattern[]; } /** * Error pattern for classification */ interface ErrorPattern { /** Pattern name */ name: string; /** Message pattern (regex or string) */ messagePattern?: RegExp | string; /** Error code pattern */ codePattern?: RegExp | string; /** Status code range */ statusCodes?: number[]; /** Resulting category */ category: ErrorCategory; /** Resulting severity */ severity: ErrorSeverity; /** Override retryable */ retryable?: boolean; /** Override retry delay */ retryAfterMs?: number; } /** * Circuit Breaker * * Prevents cascading failures by stopping requests when error rate is high. */ declare class CircuitBreaker { private config; private state; private failures; private successes; private lastFailureTime; private nextAttemptTime; constructor(config?: Partial); /** * Get current state */ getState(): CircuitState; /** * Check if circuit allows requests */ canPass(): boolean; /** * Record a successful request */ recordSuccess(): void; /** * Record a failed request */ recordFailure(): void; /** * Transition to new state */ private transition; /** * Remove failures outside the time window */ private pruneFailures; /** * Reset the circuit breaker */ reset(): void; } /** * Error development * * Comprehensive error handling with retry, circuit breaker, and fallback chains. */ declare class ErrorRecovery { private config; private circuitBreaker; private fallbacks; constructor(config?: Partial); /** * Classify an error */ classifyError(error: Error, statusCode?: number): ClassifiedError; /** * Calculate retry delay */ calculateDelay(attempt: number, config?: RetryConfig): number; /** * Execute with retry */ withRetry(fn: () => T | Promise, config?: Partial): Promise; /** * Execute with circuit breaker */ withCircuitBreaker(fn: () => T | Promise): Promise; /** * Register a fallback */ registerFallback(key: string, fallback: Fallback): void; /** * Execute with fallback chain */ withFallback(key: string, primary: () => T | Promise, errorCategory?: ErrorCategory): Promise; /** * Execute with all development mechanisms */ execute(fn: () => T | Promise, options?: { fallbackKey?: string; retryConfig?: Partial; }): Promise; /** * Get circuit breaker state */ getCircuitState(): CircuitState; /** * Reset circuit breaker */ resetCircuit(): void; /** * Sleep helper */ private sleep; } /** * Pre-configured development presets */ declare const RECOVERY_PRESETS: { /** Aggressive retry for critical operations */ aggressive: Partial; /** Conservative retry */ conservative: Partial; /** Balanced (default) */ balanced: Partial; /** Fast fail for latency-sensitive operations */ fastFail: Partial; }; /** * Convenience function to create development instance */ declare function createErrorRecovery(preset?: keyof typeof RECOVERY_PRESETS, overrides?: Partial): ErrorRecovery; /** * Batch Scheduler * * Client-side request batching for efficiency: * - Automatic request grouping * - Priority-based scheduling * - Optimal batch sizing * - Debouncing and throttling */ /** * Batch item */ interface BatchItem { /** Unique request ID */ id: string; /** Request data */ data: T; /** Priority (lower = higher priority) */ priority: number; /** Resolve function */ resolve: (result: R) => void; /** Reject function */ reject: (error: Error) => void; /** Timestamp when added */ addedAt: number; /** Request deadline (optional) */ deadline?: number; } /** * Batch result */ interface BatchResult { /** Item ID */ id: string; /** Success flag */ success: boolean; /** Result if successful */ result?: R; /** Error if failed */ error?: Error; } /** * Batch statistics */ interface BatchStats { /** Total items processed */ totalItems: number; /** Total batches processed */ totalBatches: number; /** Average batch size */ avgBatchSize: number; /** Average wait time in ms */ avgWaitTime: number; /** Items currently queued */ queuedItems: number; /** Success rate */ successRate: number; } /** * Batch scheduler configuration */ interface BatchSchedulerConfig { /** Maximum batch size */ maxBatchSize: number; /** Maximum wait time before flushing (ms) */ maxWaitTime: number; /** Minimum batch size before flushing */ minBatchSize: number; /** Process batch function */ processBatch: (items: T[]) => Promise; /** Enable priority scheduling */ enablePriority: boolean; /** Enable deadline-based scheduling */ enableDeadlines: boolean; /** Callback when batch is processed */ onBatchProcessed?: (batchSize: number, durationMs: number) => void; /** Callback on error */ onError?: (error: Error, items: T[]) => void; } /** * Batch Scheduler * * Groups requests into batches for efficient processing. */ declare class BatchScheduler { private config; private queue; private flushTimer; private processing; private stats; private successCount; private failureCount; private totalWaitTime; constructor(config: Partial> & Pick, 'processBatch'>); /** * Add item to batch queue */ add(data: T, priority?: number, deadline?: number): Promise; /** * Add multiple items */ addMany(items: T[], priority?: number): Promise; /** * Force flush the queue */ flush(): Promise; /** * Process a single batch */ private processBatch; /** * Check if we should flush immediately */ private shouldFlushImmediately; /** * Schedule a flush */ private scheduleFlush; /** * Generate unique ID */ private generateId; /** * Get current statistics */ getStats(): BatchStats; /** * Get queue length */ getQueueLength(): number; /** * Clear the queue */ clear(): void; /** * Reset statistics */ resetStats(): void; } /** * Debounced batch scheduler * * Waits for a quiet period before processing. */ declare class DebouncedBatchScheduler extends BatchScheduler { private debounceTimeout; private debounceMs; constructor(config: Partial> & Pick, 'processBatch'>, debounceMs?: number); /** * Add with debouncing */ add(data: T, priority?: number, deadline?: number): Promise; } /** * Throttled batch scheduler * * Limits batch processing rate. */ declare class ThrottledBatchScheduler extends BatchScheduler { private lastProcessTime; private throttleMs; private throttleTimer; constructor(config: Partial> & Pick, 'processBatch'>, throttleMs?: number); /** * Flush with throttling */ flush(): Promise; } /** * Inference batch scheduler * * Specialized for inference requests. */ interface InferenceRequest$1 { /** Model ID */ model: string; /** Input prompt */ prompt: string; /** Max tokens */ maxTokens?: number; /** Temperature */ temperature?: number; } interface InferenceResponse { /** Generated text */ text: string; /** Tokens used */ tokensUsed: number; /** Latency in ms */ latencyMs: number; } declare class InferenceBatchScheduler extends BatchScheduler { constructor(batchInference: (requests: InferenceRequest$1[]) => Promise, config?: Partial, 'processBatch'>>); /** * Queue an inference request */ infer(request: InferenceRequest$1): Promise; /** * Queue high-priority inference */ inferUrgent(request: InferenceRequest$1): Promise; /** * Queue low-priority (background) inference */ inferBackground(request: InferenceRequest$1): Promise; } /** * Pre-configured batch presets */ declare const BATCH_PRESETS: { /** Low latency - small batches, fast flush */ lowLatency: Partial>; /** High throughput - larger batches, longer wait */ highThroughput: Partial>; /** Balanced */ balanced: Partial>; /** Realtime - immediate processing */ realtime: Partial>; }; /** * Create a batch scheduler with preset */ declare function createBatchScheduler(processBatch: (items: T[]) => Promise, preset?: keyof typeof BATCH_PRESETS, overrides?: Partial>): BatchScheduler; /** * Token Budget Manager * * Token counting, cost estimation, and rate limiting: * - Token counting for prompts and completions * - Cost estimation per model * - Budget enforcement * - Usage tracking and alerts */ /** * Token pricing (per 1000 tokens) */ interface TokenPricing { /** Input token price per 1K tokens */ inputPer1K: number; /** Output token price per 1K tokens */ outputPer1K: number; /** Currency */ currency: string; } /** * Model pricing configuration */ interface ModelPricing { /** Model ID */ modelId: string; /** Model display name */ displayName: string; /** Token pricing */ pricing: TokenPricing; /** Max context window */ maxContextTokens: number; /** Max output tokens */ maxOutputTokens: number; } /** * Usage record */ interface UsageRecord { /** Timestamp */ timestamp: number; /** Model used */ modelId: string; /** Input tokens */ inputTokens: number; /** Output tokens */ outputTokens: number; /** Estimated cost */ estimatedCost: number; /** Request ID */ requestId?: string; /** Metadata */ metadata?: Record; } /** * Budget period */ type BudgetPeriod = 'hourly' | 'daily' | 'weekly' | 'monthly' | 'unlimited'; /** * Budget configuration */ interface BudgetConfig { /** Budget period */ period: BudgetPeriod; /** Maximum cost per period */ maxCost?: number; /** Maximum tokens per period */ maxTokens?: number; /** Maximum requests per period */ maxRequests?: number; /** Alert threshold (0-1) */ alertThreshold: number; /** Callback when alert threshold reached */ onAlert?: (usage: BudgetUsage) => void; /** Callback when budget exceeded */ onBudgetExceeded?: (usage: BudgetUsage) => void; } /** * Budget usage */ interface BudgetUsage { /** Period start */ periodStart: number; /** Period end */ periodEnd: number; /** Total cost */ totalCost: number; /** Total tokens */ totalTokens: number; /** Total requests */ totalRequests: number; /** Cost limit */ costLimit?: number; /** Token limit */ tokenLimit?: number; /** Request limit */ requestLimit?: number; /** Cost usage percentage */ costUsagePercent: number; /** Token usage percentage */ tokenUsagePercent: number; /** Request usage percentage */ requestUsagePercent: number; /** Is over budget */ isOverBudget: boolean; /** Alert triggered */ alertTriggered: boolean; } /** * Token budget manager configuration */ interface TokenBudgetManagerConfig { /** Budget configuration */ budget: BudgetConfig; /** Model pricing */ modelPricing: ModelPricing[]; /** Default model ID */ defaultModelId?: string; /** Token estimator (characters to tokens ratio) */ tokenEstimatorRatio: number; /** Maximum stored usage records */ maxStoredRecords: number; /** Persist usage to storage */ persistUsage: boolean; /** Storage key prefix */ storageKeyPrefix: string; } /** * Token Budget Manager * * Manages token usage, costs, and budgets. */ declare class TokenBudgetManager { private config; private usageRecords; private periodUsage; private alertTriggered; constructor(config?: Partial); /** * Initialize period usage */ private initializePeriodUsage; /** * Get period bounds */ private getPeriodBounds; /** * Estimate tokens from text */ estimateTokens(text: string): number; /** * Get model pricing */ getModelPricing(modelId: string): ModelPricing | undefined; /** * Calculate cost */ calculateCost(modelId: string, inputTokens: number, outputTokens: number): number; /** * Estimate cost before request */ estimateCost(modelId: string, prompt: string, maxOutputTokens?: number): { inputTokens: number; outputTokens: number; estimatedCost: number; maxCost: number; }; /** * Check if request is within budget */ checkBudget(modelId: string, prompt: string, maxOutputTokens?: number): { allowed: boolean; reason?: string; remainingBudget: BudgetUsage; }; /** * Record usage */ recordUsage(modelId: string, inputTokens: number, outputTokens: number, requestId?: string, metadata?: Record): UsageRecord; /** * Update usage percentages */ private updateUsagePercentages; /** * Check for budget alerts */ private checkAlerts; /** * Check if period has reset */ private checkPeriodReset; /** * Get current usage */ getCurrentUsage(): BudgetUsage; /** * Get usage history */ getUsageHistory(limit?: number): UsageRecord[]; /** * Get usage by model */ getUsageByModel(): Map; /** * Get remaining budget */ getRemainingBudget(): { cost: number | null; tokens: number | null; requests: number | null; }; /** * Set budget limits */ setBudgetLimits(limits: { maxCost?: number; maxTokens?: number; maxRequests?: number; }): void; /** * Add model pricing */ addModelPricing(pricing: ModelPricing): void; /** * Persist usage to storage */ private persistUsage; /** * Load persisted usage */ private loadPersistedUsage; /** * Reset usage */ resetUsage(): void; } /** * Pre-configured budget presets */ declare const BUDGET_PRESETS: { /** Free tier */ free: Partial; /** Basic tier */ basic: Partial; /** Pro tier */ pro: Partial; /** Enterprise - unlimited */ enterprise: Partial; /** Development - tracking only */ development: Partial; }; /** * Create a token budget manager with preset */ declare function createTokenBudgetManager(preset?: keyof typeof BUDGET_PRESETS, overrides?: Partial): TokenBudgetManager; /** * React Hooks for Distributed Inference * * Enhanced hooks for edge/distributed inference: * - useDistributedInference - Core distributed inference hook * - useModelRouter - Intelligent routing hook * - useStreamingInference - Real-time streaming hook * - useBudget - Token budget management hook * - useOfflineStatus - Offline status and sync hook * - useInferenceMetrics - Performance metrics hook */ /** * Distributed inference configuration */ interface UseDistributedInferenceConfig { /** Model ID */ model: string; /** Initial routing strategy */ routingStrategy?: RoutingStrategy; /** Enable offline support */ enableOffline?: boolean; /** Enable metrics collection */ enableMetrics?: boolean; /** Maximum retries */ maxRetries?: number; /** Timeout in ms */ timeout?: number; } /** * Inference request */ interface InferenceRequest { /** Input prompt */ prompt: string; /** System prompt */ systemPrompt?: string; /** Max tokens */ maxTokens?: number; /** Temperature */ temperature?: number; /** Top P */ topP?: number; /** Stop sequences */ stopSequences?: string[]; } /** * Inference result */ interface InferenceResult { /** Generated text */ text: string; /** Tokens used */ tokensUsed: number; /** Source (local/edge/cloud) */ source: 'local' | 'edge' | 'cloud'; /** Latency in ms */ latencyMs: number; /** Model used */ model: string; /** Finish reason */ finishReason: 'stop' | 'length' | 'error'; } /** * Inference state */ interface InferenceState { /** Is loading/generating */ isLoading: boolean; /** Current result */ result: InferenceResult | null; /** Current error */ error: Error | null; /** Is online */ isOnline: boolean; /** Current routing decision */ routingDecision: RouteDecision | null; /** Circuit breaker state */ circuitState: CircuitState; } /** * useDistributedInference Hook * * Core hook for distributed inference with automatic routing. */ declare function useDistributedInference(config: UseDistributedInferenceConfig): { generate: (request: InferenceRequest) => Promise; reset: () => void; /** Is loading/generating */ isLoading: boolean; /** Current result */ result: InferenceResult | null; /** Current error */ error: Error | null; /** Is online */ isOnline: boolean; /** Current routing decision */ routingDecision: RouteDecision | null; /** Circuit breaker state */ circuitState: CircuitState; }; /** * Streaming inference state */ interface StreamingState { /** Is streaming */ isStreaming: boolean; /** Tokens received so far */ tokens: string[]; /** Current text */ text: string; /** Error if any */ error: Error | null; /** Stream finished */ isComplete: boolean; /** Tokens per second */ tokensPerSecond: number; } /** * useStreamingInference Hook * * Real-time streaming inference with backpressure handling. */ declare function useStreamingInference(config: UseDistributedInferenceConfig): { stream: (request: InferenceRequest) => AsyncGenerator; stop: () => void; reset: () => void; /** Is streaming */ isStreaming: boolean; /** Tokens received so far */ tokens: string[]; /** Current text */ text: string; /** Error if any */ error: Error | null; /** Stream finished */ isComplete: boolean; /** Tokens per second */ tokensPerSecond: number; }; /** * Budget state */ interface BudgetState { /** Current usage */ usage: BudgetUsage | null; /** Recent usage records */ recentRecords: UsageRecord[]; /** Is over budget */ isOverBudget: boolean; /** Alert triggered */ alertTriggered: boolean; /** Remaining budget */ remaining: { cost: number | null; tokens: number | null; requests: number | null; }; } /** * useBudget Hook * * Token budget management and tracking. */ declare function useBudget(): { checkBudget: (modelId: string, prompt: string, maxOutputTokens?: number) => { allowed: boolean; reason?: string; }; recordUsage: (modelId: string, inputTokens: number, outputTokens: number) => void; setBudgetLimits: (limits: { maxCost?: number; maxTokens?: number; maxRequests?: number; }) => void; /** Current usage */ usage: BudgetUsage | null; /** Recent usage records */ recentRecords: UsageRecord[]; /** Is over budget */ isOverBudget: boolean; /** Alert triggered */ alertTriggered: boolean; /** Remaining budget */ remaining: { cost: number | null; tokens: number | null; requests: number | null; }; }; /** * Offline state */ interface OfflineState { /** Is online */ isOnline: boolean; /** Is syncing */ isSyncing: boolean; /** Pending requests count */ pendingRequests: number; /** Last sync time */ lastSyncTime: number | null; /** Sync error */ syncError: Error | null; /** Cached models */ cachedModels: string[]; } /** * useOfflineStatus Hook * * Offline status and sync management. */ declare function useOfflineStatus(): { triggerSync: () => Promise; queueRequest: () => void; cacheModel: (modelId: string) => Promise; isModelCached: (modelId: string) => boolean; /** Is online */ isOnline: boolean; /** Is syncing */ isSyncing: boolean; /** Pending requests count */ pendingRequests: number; /** Last sync time */ lastSyncTime: number | null; /** Sync error */ syncError: Error | null; /** Cached models */ cachedModels: string[]; }; /** * Metrics state */ interface MetricsState { /** Performance summary */ summary: PerformanceSummary | null; /** Recent metrics */ recentMetrics: MetricValue[]; /** Is collecting */ isCollecting: boolean; } /** * useInferenceMetrics Hook * * Performance metrics and monitoring. */ declare function useInferenceMetrics(options?: { collectionInterval?: number; autoStart?: boolean; }): { start: () => void; stop: () => void; recordMetric: (name: string, value: number, labels?: Record) => void; /** Performance summary */ summary: PerformanceSummary | null; /** Recent metrics */ recentMetrics: MetricValue[]; /** Is collecting */ isCollecting: boolean; }; /** * Model status state */ interface ModelStatusState { /** Model ID */ modelId: string | null; /** Is loaded */ isLoaded: boolean; /** Is loading */ isLoading: boolean; /** Download progress (0-100) */ downloadProgress: number; /** Load error */ error: Error | null; /** Backend type */ backend: 'webgpu' | 'wasm' | 'remote' | null; /** Model size in bytes */ sizeBytes: number | null; } /** * useModelStatus Hook * * Track model loading and status. */ declare function useModelStatus(): { loadModel: (modelId: string) => Promise; unloadModel: () => void; /** Model ID */ modelId: string | null; /** Is loaded */ isLoaded: boolean; /** Is loading */ isLoading: boolean; /** Download progress (0-100) */ downloadProgress: number; /** Load error */ error: Error | null; /** Backend type */ backend: "webgpu" | "wasm" | "remote" | null; /** Model size in bytes */ sizeBytes: number | null; }; /** * Combined edgework hook for convenience */ declare function useEdgeworkInference(config: UseDistributedInferenceConfig): { isLoading: boolean; result: InferenceResult | null; error: Error | null; generate: (request: InferenceRequest) => Promise; isStreaming: boolean; streamText: string; stream: (request: InferenceRequest) => AsyncGenerator; stopStream: () => void; usage: BudgetUsage | null; checkBudget: (modelId: string, prompt: string, maxOutputTokens?: number) => { allowed: boolean; reason?: string; }; recordUsage: (modelId: string, inputTokens: number, outputTokens: number) => void; isOnline: boolean; isSyncing: boolean; pendingRequests: number; cacheModel: (modelId: string) => Promise; metrics: PerformanceSummary | null; isModelLoaded: boolean; loadModel: (modelId: string) => Promise; modelBackend: "webgpu" | "wasm" | "remote" | null; }; /** * Distributed Inference Client * * Client-side integration with Cloudflare Workers distributed inference. * Provides seamless fallback between local WASM/WebGPU and remote distributed inference. */ declare const index_BANDWIDTH_PRESETS: typeof BANDWIDTH_PRESETS; declare const index_BATCH_PRESETS: typeof BATCH_PRESETS; declare const index_BUDGET_PRESETS: typeof BUDGET_PRESETS; type index_BandwidthOptimizer = BandwidthOptimizer; declare const index_BandwidthOptimizer: typeof BandwidthOptimizer; type index_BandwidthOptimizerConfig = BandwidthOptimizerConfig; type index_BatchItem = BatchItem; type index_BatchResult = BatchResult; type index_BatchScheduler = BatchScheduler; declare const index_BatchScheduler: typeof BatchScheduler; type index_BatchSchedulerConfig = BatchSchedulerConfig; type index_BatchStats = BatchStats; type index_BudgetConfig = BudgetConfig; type index_BudgetPeriod = BudgetPeriod; type index_BudgetState = BudgetState; type index_BudgetUsage = BudgetUsage; declare const index_CACHE_PRESETS: typeof CACHE_PRESETS; declare const index_CONNECTION_PRESETS: typeof CONNECTION_PRESETS; type index_CacheCoordinator = CacheCoordinator; declare const index_CacheCoordinator: typeof CacheCoordinator; type index_CacheCoordinatorConfig = CacheCoordinatorConfig; type index_CacheEntry = CacheEntry; type index_CacheEvent = CacheEvent; type index_CacheLayer = CacheLayer; type index_CacheStats = CacheStats; type index_CircuitBreaker = CircuitBreaker; declare const index_CircuitBreaker: typeof CircuitBreaker; type index_CircuitBreakerConfig = CircuitBreakerConfig; type index_CircuitState = CircuitState; type index_ClassifiedError = ClassifiedError; type index_CompressionAlgorithm = CompressionAlgorithm; type index_ConnectionInfo = ConnectionInfo; type index_ConnectionManager = ConnectionManager; declare const index_ConnectionManager: typeof ConnectionManager; type index_ConnectionManagerConfig = ConnectionManagerConfig; type index_ConnectionQuality = ConnectionQuality; declare const index_DISTRIBUTED_PRESETS: typeof DISTRIBUTED_PRESETS; type index_DebouncedBatchScheduler = DebouncedBatchScheduler; declare const index_DebouncedBatchScheduler: typeof DebouncedBatchScheduler; type index_DistributedClient = DistributedClient; declare const index_DistributedClient: typeof DistributedClient; type index_DistributedClientConfig = DistributedClientConfig; type index_DistributedInferenceOptions = DistributedInferenceOptions; type index_DistributedResult = DistributedResult; type index_EdgeInferenceAdapter = EdgeInferenceAdapter; declare const index_EdgeInferenceAdapter: typeof EdgeInferenceAdapter; type index_EdgeInferenceAdapterConfig = EdgeInferenceAdapterConfig; type index_ErrorCategory = ErrorCategory; type index_ErrorPattern = ErrorPattern; type index_ErrorRecovery = ErrorRecovery; declare const index_ErrorRecovery: typeof ErrorRecovery; type index_ErrorRecoveryConfig = ErrorRecoveryConfig; type index_ErrorSeverity = ErrorSeverity; type index_Fallback = Fallback; type index_HistogramBucket = HistogramBucket; type index_HistogramData = HistogramData; type index_InferenceBatchScheduler = InferenceBatchScheduler; declare const index_InferenceBatchScheduler: typeof InferenceBatchScheduler; type index_InferenceContext = InferenceContext; type index_InferenceResponse = InferenceResponse; type index_InferenceState = InferenceState; type index_InvalidationReason = InvalidationReason; type index_KVCacheEntry = KVCacheEntry; type index_LatencyStats = LatencyStats; declare const index_METRICS_PRESETS: typeof METRICS_PRESETS; type index_MetricType = MetricType; type index_MetricValue = MetricValue; type index_MetricsReporter = MetricsReporter; declare const index_MetricsReporter: typeof MetricsReporter; type index_MetricsReporterConfig = MetricsReporterConfig; type index_MetricsState = MetricsState; type index_ModelCapabilities = ModelCapabilities; type index_ModelLoadProgress = ModelLoadProgress; type index_ModelLoadState = ModelLoadState; type index_ModelMetadata = ModelMetadata; type index_ModelPreloader = ModelPreloader; declare const index_ModelPreloader: typeof ModelPreloader; type index_ModelPreloaderConfig = ModelPreloaderConfig; type index_ModelPricing = ModelPricing; type index_ModelRouter = ModelRouter; declare const index_ModelRouter: typeof ModelRouter; type index_ModelRouterConfig = ModelRouterConfig; type index_ModelStatusState = ModelStatusState; type index_NetworkProfile = NetworkProfile; declare const index_OFFLINE_PRESETS: typeof OFFLINE_PRESETS; type index_OfflineModel = OfflineModel; type index_OfflineSupport = OfflineSupport; declare const index_OfflineSupport: typeof OfflineSupport; type index_OfflineSupportConfig = OfflineSupportConfig; type index_OptimizationStrategy = OptimizationStrategy; declare const index_PRELOADER_PRESETS: typeof PRELOADER_PRESETS; type index_PerformanceSummary = PerformanceSummary; type index_PreloadPriority = PreloadPriority; type index_PreloadRequest = PreloadRequest; type index_QualityLevel = QualityLevel; type index_QueuedRequest = QueuedRequest; declare const index_RECOVERY_PRESETS: typeof RECOVERY_PRESETS; declare const index_ROUTING_STRATEGIES: typeof ROUTING_STRATEGIES; type index_RetryConfig = RetryConfig; type index_RetryStrategy = RetryStrategy; type index_RouteDecision = RouteDecision; type index_RoutingStrategy = RoutingStrategy; declare const index_STREAMING_PRESETS: typeof STREAMING_PRESETS; type index_StreamConfig = StreamConfig; type index_StreamEvent = StreamEvent; type index_StreamState = StreamState; type index_StreamStats = StreamStats; type index_StreamingClient = StreamingClient; declare const index_StreamingClient: typeof StreamingClient; type index_StreamingState = StreamingState; type index_SyncConflict = SyncConflict; type index_ThrottledBatchScheduler = ThrottledBatchScheduler; declare const index_ThrottledBatchScheduler: typeof ThrottledBatchScheduler; type index_TokenBudgetManager = TokenBudgetManager; declare const index_TokenBudgetManager: typeof TokenBudgetManager; type index_TokenBudgetManagerConfig = TokenBudgetManagerConfig; type index_TokenEvent = TokenEvent; type index_TokenPricing = TokenPricing; type index_TransferFormat = TransferFormat; type index_UsageRecord = UsageRecord; type index_UseDistributedInferenceConfig = UseDistributedInferenceConfig; type index_WorkerStatus = WorkerStatus; declare const index_createBatchScheduler: typeof createBatchScheduler; declare const index_createEdgeInferenceAdapter: typeof createEdgeInferenceAdapter; declare const index_createErrorRecovery: typeof createErrorRecovery; declare const index_createTokenBudgetManager: typeof createTokenBudgetManager; declare const index_useBudget: typeof useBudget; declare const index_useDistributedInference: typeof useDistributedInference; declare const index_useEdgeworkInference: typeof useEdgeworkInference; declare const index_useInferenceMetrics: typeof useInferenceMetrics; declare const index_useModelStatus: typeof useModelStatus; declare const index_useOfflineStatus: typeof useOfflineStatus; declare const index_useStreamingInference: typeof useStreamingInference; declare namespace index { export { index_BANDWIDTH_PRESETS as BANDWIDTH_PRESETS, index_BATCH_PRESETS as BATCH_PRESETS, index_BUDGET_PRESETS as BUDGET_PRESETS, index_BandwidthOptimizer as BandwidthOptimizer, type index_BandwidthOptimizerConfig as BandwidthOptimizerConfig, type InferenceRequest$1 as BatchInferenceRequest, type index_BatchItem as BatchItem, type index_BatchResult as BatchResult, index_BatchScheduler as BatchScheduler, type index_BatchSchedulerConfig as BatchSchedulerConfig, type index_BatchStats as BatchStats, type index_BudgetConfig as BudgetConfig, type index_BudgetPeriod as BudgetPeriod, type index_BudgetState as BudgetState, type index_BudgetUsage as BudgetUsage, index_CACHE_PRESETS as CACHE_PRESETS, index_CONNECTION_PRESETS as CONNECTION_PRESETS, index_CacheCoordinator as CacheCoordinator, type index_CacheCoordinatorConfig as CacheCoordinatorConfig, type index_CacheEntry as CacheEntry, type index_CacheEvent as CacheEvent, type index_CacheLayer as CacheLayer, type index_CacheStats as CacheStats, index_CircuitBreaker as CircuitBreaker, type index_CircuitBreakerConfig as CircuitBreakerConfig, type index_CircuitState as CircuitState, type index_ClassifiedError as ClassifiedError, type index_CompressionAlgorithm as CompressionAlgorithm, type index_ConnectionInfo as ConnectionInfo, index_ConnectionManager as ConnectionManager, type index_ConnectionManagerConfig as ConnectionManagerConfig, type ConnectionState as ConnectionManagerState, type index_ConnectionQuality as ConnectionQuality, type ConnectionState$1 as ConnectionState, index_DISTRIBUTED_PRESETS as DISTRIBUTED_PRESETS, index_DebouncedBatchScheduler as DebouncedBatchScheduler, index_DistributedClient as DistributedClient, type index_DistributedClientConfig as DistributedClientConfig, type index_DistributedInferenceOptions as DistributedInferenceOptions, type index_DistributedResult as DistributedResult, index_EdgeInferenceAdapter as EdgeInferenceAdapter, type index_EdgeInferenceAdapterConfig as EdgeInferenceAdapterConfig, type index_ErrorCategory as ErrorCategory, type index_ErrorPattern as ErrorPattern, index_ErrorRecovery as ErrorRecovery, type index_ErrorRecoveryConfig as ErrorRecoveryConfig, type index_ErrorSeverity as ErrorSeverity, type index_Fallback as Fallback, type index_HistogramBucket as HistogramBucket, type index_HistogramData as HistogramData, type InferenceRequest as HooksInferenceRequest, type InferenceResult as HooksInferenceResult, type OfflineState as HooksOfflineState, index_InferenceBatchScheduler as InferenceBatchScheduler, type index_InferenceContext as InferenceContext, type InferenceRequest$2 as InferenceRequest, type index_InferenceResponse as InferenceResponse, type InferenceResult$1 as InferenceResult, type index_InferenceState as InferenceState, type index_InvalidationReason as InvalidationReason, type index_KVCacheEntry as KVCacheEntry, type index_LatencyStats as LatencyStats, index_METRICS_PRESETS as METRICS_PRESETS, type index_MetricType as MetricType, type index_MetricValue as MetricValue, index_MetricsReporter as MetricsReporter, type index_MetricsReporterConfig as MetricsReporterConfig, type index_MetricsState as MetricsState, type index_ModelCapabilities as ModelCapabilities, type index_ModelLoadProgress as ModelLoadProgress, type index_ModelLoadState as ModelLoadState, type index_ModelMetadata as ModelMetadata, index_ModelPreloader as ModelPreloader, type index_ModelPreloaderConfig as ModelPreloaderConfig, type index_ModelPricing as ModelPricing, index_ModelRouter as ModelRouter, type index_ModelRouterConfig as ModelRouterConfig, type index_ModelStatusState as ModelStatusState, type index_NetworkProfile as NetworkProfile, index_OFFLINE_PRESETS as OFFLINE_PRESETS, type index_OfflineModel as OfflineModel, type OfflineState$1 as OfflineState, index_OfflineSupport as OfflineSupport, type index_OfflineSupportConfig as OfflineSupportConfig, type index_OptimizationStrategy as OptimizationStrategy, index_PRELOADER_PRESETS as PRELOADER_PRESETS, type index_PerformanceSummary as PerformanceSummary, type index_PreloadPriority as PreloadPriority, type index_PreloadRequest as PreloadRequest, type index_QualityLevel as QualityLevel, type index_QueuedRequest as QueuedRequest, index_RECOVERY_PRESETS as RECOVERY_PRESETS, index_ROUTING_STRATEGIES as ROUTING_STRATEGIES, type index_RetryConfig as RetryConfig, type index_RetryStrategy as RetryStrategy, type index_RouteDecision as RouteDecision, type index_RoutingStrategy as RoutingStrategy, index_STREAMING_PRESETS as STREAMING_PRESETS, type index_StreamConfig as StreamConfig, type index_StreamEvent as StreamEvent, type index_StreamState as StreamState, type index_StreamStats as StreamStats, index_StreamingClient as StreamingClient, type index_StreamingState as StreamingState, type index_SyncConflict as SyncConflict, index_ThrottledBatchScheduler as ThrottledBatchScheduler, index_TokenBudgetManager as TokenBudgetManager, type index_TokenBudgetManagerConfig as TokenBudgetManagerConfig, type index_TokenEvent as TokenEvent, type index_TokenPricing as TokenPricing, type index_TransferFormat as TransferFormat, type index_UsageRecord as UsageRecord, type index_UseDistributedInferenceConfig as UseDistributedInferenceConfig, type index_WorkerStatus as WorkerStatus, index_createBatchScheduler as createBatchScheduler, index_createEdgeInferenceAdapter as createEdgeInferenceAdapter, index_createErrorRecovery as createErrorRecovery, index_createTokenBudgetManager as createTokenBudgetManager, index_useBudget as useBudget, index_useDistributedInference as useDistributedInference, index_useEdgeworkInference as useEdgeworkInference, index_useInferenceMetrics as useInferenceMetrics, index_useModelStatus as useModelStatus, index_useOfflineStatus as useOfflineStatus, index_useStreamingInference as useStreamingInference }; } export { type OfflineState as $, type ConnectionInfo as A, BANDWIDTH_PRESETS as B, CACHE_PRESETS as C, ConnectionManager as D, type ConnectionManagerConfig as E, type ConnectionState as F, type ConnectionQuality as G, type ConnectionState$1 as H, type InferenceRequest$1 as I, DISTRIBUTED_PRESETS as J, DebouncedBatchScheduler as K, DistributedClient as L, type DistributedClientConfig as M, type DistributedInferenceOptions as N, type DistributedResult as O, EdgeInferenceAdapter as P, type EdgeInferenceAdapterConfig as Q, type ErrorCategory as R, type ErrorPattern as S, ErrorRecovery as T, type ErrorRecoveryConfig as U, type ErrorSeverity as V, type Fallback as W, type HistogramBucket as X, type HistogramData as Y, type InferenceRequest as Z, type InferenceResult as _, BATCH_PRESETS as a, createErrorRecovery as a$, InferenceBatchScheduler as a0, type InferenceContext as a1, type InferenceRequest$2 as a2, type InferenceResponse as a3, type InferenceResult$1 as a4, type InferenceState as a5, type InvalidationReason as a6, type KVCacheEntry as a7, type LatencyStats as a8, METRICS_PRESETS as a9, type QualityLevel as aA, type QueuedRequest as aB, RECOVERY_PRESETS as aC, ROUTING_STRATEGIES as aD, type RetryConfig as aE, type RetryStrategy as aF, type RouteDecision as aG, type RoutingStrategy as aH, STREAMING_PRESETS as aI, type StreamConfig as aJ, type StreamEvent as aK, type StreamState as aL, type StreamStats as aM, StreamingClient as aN, type StreamingState as aO, type SyncConflict as aP, ThrottledBatchScheduler as aQ, TokenBudgetManager as aR, type TokenBudgetManagerConfig as aS, type TokenEvent as aT, type TokenPricing as aU, type TransferFormat as aV, type UsageRecord as aW, type UseDistributedInferenceConfig as aX, type WorkerStatus as aY, createBatchScheduler as aZ, createEdgeInferenceAdapter as a_, type MetricType as aa, type MetricValue as ab, MetricsReporter as ac, type MetricsReporterConfig as ad, type MetricsState as ae, type ModelCapabilities as af, type ModelLoadProgress as ag, type ModelLoadState as ah, type ModelMetadata as ai, ModelPreloader as aj, type ModelPreloaderConfig as ak, type ModelPricing as al, ModelRouter as am, type ModelRouterConfig as an, type ModelStatusState as ao, type NetworkProfile as ap, OFFLINE_PRESETS as aq, type OfflineModel as ar, type OfflineState$1 as as, OfflineSupport as at, type OfflineSupportConfig as au, type OptimizationStrategy as av, PRELOADER_PRESETS as aw, type PerformanceSummary as ax, type PreloadPriority as ay, type PreloadRequest as az, BUDGET_PRESETS as b, createTokenBudgetManager as b0, useBudget as b1, useDistributedInference as b2, useEdgeworkInference as b3, useInferenceMetrics as b4, useModelStatus as b5, useOfflineStatus as b6, useStreamingInference as b7, BandwidthOptimizer as c, type BandwidthOptimizerConfig as d, type BatchItem as e, type BatchResult as f, BatchScheduler as g, type BatchSchedulerConfig as h, index as i, type BatchStats as j, type BudgetConfig as k, type BudgetPeriod as l, type BudgetState as m, type BudgetUsage as n, CONNECTION_PRESETS as o, CacheCoordinator as p, type CacheCoordinatorConfig as q, type CacheEntry as r, type CacheEvent as s, type CacheLayer as t, type CacheStats as u, CircuitBreaker as v, type CircuitBreakerConfig as w, type CircuitState as x, type ClassifiedError as y, type CompressionAlgorithm as z };