/** * Failover Orchestrator * * Wraps provider operations with intelligent retry and failover logic. * Integrates health tracking, circuit breakers, and cost-aware routing. */ import type { Logger } from '../observability/logger.js'; import type { ProviderManager } from '../providers/manager.js'; import type { Config, CompletionResponse, Message } from '../types.js'; import { type CircuitBreakerOptions } from '../utils/circuit-breaker.js'; import { ProviderHealthTracker, type ProviderHealth } from './health-tracker.js'; import { PricingService } from './pricing.js'; import type { PersistedFailoverEvent, PersistedProviderHealth } from '../persistence/state-schema.js'; /** * Options for a single completion request with failover. */ export interface FailoverCompletionOptions { /** Request temperature */ temperature?: number; /** Maximum tokens to generate */ max_tokens?: number; /** Request timeout in ms */ timeout_ms?: number; } /** * Result of a completion with failover metadata. */ export interface FailoverCompletionResult { /** The completion response */ response: CompletionResponse; /** Provider that handled the request */ provider: string; /** Model used */ model: string; /** Number of retry attempts */ retryCount: number; /** Number of providers tried */ failoverCount: number; /** Total latency in ms */ latencyMs: number; /** Whether failover was triggered */ usedFailover: boolean; } /** * Configuration for the failover orchestrator. */ export interface FailoverOrchestratorConfig { /** Enable failover (default: true) */ enabled?: boolean; /** Maximum total retries across all providers (default: 6) */ maxTotalRetries?: number; /** Health check interval in ms (default: 60000) */ healthCheckIntervalMs?: number; /** Cooldown period in ms (default: 300000) */ cooldownMs?: number; /** Use cheapest healthy provider when no preference (default: false) */ preferCostEfficient?: boolean; /** Circuit breaker options per provider */ circuitBreaker?: Partial; } /** * Failover event for logging and persistence. */ export interface FailoverEvent { timestamp: Date; role: string; fromProvider: string; toProvider: string; reason: string; errorCode?: number; errorMessage?: string; } /** * Orchestrates provider requests with intelligent failover. * * Features: * - Automatic retry with exponential backoff * - Multi-provider failover chain * - Health tracking and cooldown management * - Circuit breaker per provider * - Cost-aware provider selection * - Failover event logging for diagnostics * * @example * ```typescript * const orchestrator = new FailoverOrchestrator( * providers, * config, * logger * ); * * // Execute with automatic failover * const result = await orchestrator.executeWithFailover( * 'coder', * messages, * { temperature: 0.7 } * ); * ``` */ export declare class FailoverOrchestrator { private readonly providers; private readonly appConfig; private readonly logger; private readonly config; private readonly healthTracker; private readonly pricingService; private readonly circuitBreakers; private readonly failoverEvents; private readonly maxEventHistory; constructor(providers: ProviderManager, appConfig: Config, logger: Logger, config?: FailoverOrchestratorConfig); /** * Execute a completion request with automatic failover. */ executeWithFailover(role: string, messages: Message[], options: FailoverCompletionOptions): Promise; /** * Get available providers for a role, sorted by health and optionally cost. */ getAvailableProviders(role: string): Array<{ provider: string; model: string; }>; /** * Get health status for all tracked providers. */ getProviderHealth(): Map; /** * Get recent failover events. */ getFailoverEvents(): FailoverEvent[]; /** * Initialize the orchestrator (fetch pricing, start health checks). */ initialize(): Promise; /** * Shutdown the orchestrator (stop health checks, persist state). */ shutdown(): void; /** * Serialize state for persistence. */ serializeState(): { providerHealth: PersistedProviderHealth[]; failoverEvents: PersistedFailoverEvent[]; }; /** * Restore state from persistence. */ restoreState(state: { providerHealth?: PersistedProviderHealth[]; failoverEvents?: PersistedFailoverEvent[]; }): void; /** * Get the pricing service for external use. */ getPricingService(): PricingService; /** * Get the health tracker for external use. */ getHealthTracker(): ProviderHealthTracker; /** * Build the ordered chain of providers to try for a role. */ private buildProviderChain; /** * Execute a single provider request with retry logic. */ private executeWithRetry; /** * Execute without failover (single provider only). */ private executeSingleProvider; /** * Check if an error should trigger failover. */ private shouldTriggerFailover; /** * Get the next provider in the chain that hasn't been tried. */ private getNextProvider; /** * Get or create a circuit breaker for a provider. */ private getOrCreateCircuitBreaker; /** * Record a failover event for diagnostics. */ private recordFailoverEvent; } //# sourceMappingURL=orchestrator.d.ts.map