/** * Fallback LLM client that tries multiple providers in sequence. * Provides resilience through provider failover and model fallback. */ import type { LLMClient, Message, CompletionOptions, ProviderInfo, LLMConfig, LLMProviderId, StreamingOptions, StreamingResult } from './client.js'; /** * Health status for a provider. */ export interface ProviderHealth { provider: LLMProviderId; healthy: boolean; lastChecked: Date; lastError?: string; consecutiveFailures: number; } /** * Configuration for fallback behavior. */ export interface FallbackConfig { /** Primary provider configurations in priority order */ providers: LLMConfig[]; /** Whether to use Ollama as final fallback (default: true) */ useOllamaFallback?: boolean; /** Ollama model to use for fallback */ ollamaModel?: string; /** Health check interval in ms (default: 60000) */ healthCheckIntervalMs?: number; /** Max consecutive failures before marking unhealthy (default: 3) */ maxConsecutiveFailures?: number; /** Time to wait before retrying unhealthy provider in ms (default: 300000) */ unhealthyRetryDelayMs?: number; /** Callback for usage tracking (applied to all providers) */ onUsage?: (inputTokens: number, outputTokens: number) => void; } /** * Fallback result with metadata. */ export interface FallbackResult { response: string; provider: LLMProviderId; model: string; attemptedProviders: LLMProviderId[]; failedProviders: Array<{ provider: LLMProviderId; error: string; }>; } /** * LLM client with automatic provider failover. */ export declare class FallbackLLMClient implements LLMClient { private clients; private providerOrder; private health; private config; constructor(config: FallbackConfig); /** * Initialize all provider clients. */ private initializeClients; /** * Get combined provider info. */ getProviderInfo(): ProviderInfo; /** * Get the current primary (first healthy) client. */ private getPrimaryClient; /** * Check if a provider is currently healthy. */ private healthCheckInProgress; private isProviderHealthy; /** * Mark a provider as failed. */ private markProviderFailed; /** * Mark a provider as successful. */ private markProviderSuccess; /** * Determine if an error should trigger failover to next provider. */ private shouldFailover; /** * Execute a completion with fallback. */ private executeWithFallback; /** * Chat completion with fallback. */ chat(messages: Message[], options?: CompletionOptions): Promise; /** * Single prompt completion with fallback. */ complete(prompt: string, options?: CompletionOptions): Promise; /** * Parse JSON from response. */ parseJSON(response: string): T; /** * Stream completion from a single prompt with fallback. */ stream(prompt: string, options?: StreamingOptions): Promise; /** * Stream chat completion with fallback. */ streamChat(messages: Message[], options?: StreamingOptions): Promise; /** * Get health status for all providers. */ getProviderHealth(): ProviderHealth[]; /** * Get list of available providers in priority order. */ getProviderOrder(): LLMProviderId[]; /** * Check health of all providers (for Ollama this pings the server). */ checkHealth(): Promise; /** * Manually mark a provider as unhealthy. */ disableProvider(providerId: LLMProviderId): void; /** * Manually mark a provider as healthy. */ enableProvider(providerId: LLMProviderId): void; } /** * Create a fallback LLM client from available providers. * Automatically detects available API keys and sets up fallback chain. */ export declare function createFallbackClient(options?: { preferredOrder?: LLMProviderId[]; useOllamaFallback?: boolean; ollamaModel?: string; onUsage?: (inputTokens: number, outputTokens: number) => void; }): FallbackLLMClient; //# sourceMappingURL=fallback.d.ts.map