UNPKG

@hivetechs/hive-ai

Version:

Real-time streaming AI consensus platform with HTTP+SSE MCP integration for Claude Code, VS Code, Cursor, and Windsurf - powered by OpenRouter's unified API

146 lines (145 loc) 4.34 kB
/** * Bulletproof Error Handling System * * Comprehensive error handling with retry logic, exponential backoff, * circuit breakers, and intelligent fallback mechanisms. */ export interface RetryConfig { maxRetries: number; initialDelay: number; maxDelay: number; backoffMultiplier: number; jitter: boolean; } export interface CircuitBreakerConfig { failureThreshold: number; recoveryTimeout: number; monitoringWindow: number; } export interface FallbackConfig { enableModelFallback: boolean; enableProviderFallback: boolean; maxFallbackAttempts: number; fallbackModels: string[]; } export declare enum ErrorType { NETWORK_ERROR = "NETWORK_ERROR", API_ERROR = "API_ERROR", RATE_LIMIT = "RATE_LIMIT", MODEL_UNAVAILABLE = "MODEL_UNAVAILABLE", AUTHENTICATION_ERROR = "AUTHENTICATION_ERROR", VALIDATION_ERROR = "VALIDATION_ERROR", TIMEOUT_ERROR = "TIMEOUT_ERROR", UNKNOWN_ERROR = "UNKNOWN_ERROR" } export interface ErrorContext { type: ErrorType; message: string; statusCode?: number; retryable: boolean; provider: string; model: string; stage: string; timestamp: Date; requestId?: string; } export declare class CircuitBreaker { private config; private failures; private lastFailureTime; private state; private nextAttemptTime; constructor(config: CircuitBreakerConfig); execute<T>(operation: () => Promise<T>): Promise<T>; private onSuccess; private onFailure; getState(): { state: string; failures: number; nextAttemptTime?: Date; }; } export declare class ErrorHandler { private circuitBreakers; private retryConfig; private fallbackConfig; private errorLog; constructor(retryConfig?: RetryConfig, fallbackConfig?: FallbackConfig); /** * Execute operation with comprehensive error handling */ executeWithRetry<T>(operation: () => Promise<T>, context: { provider: string; model: string; stage: string; requestId?: string; }): Promise<T>; /** * Execute with automatic fallback to alternative models/providers */ executeWithFallback<T>(operation: (provider: string, model: string) => Promise<T>, primaryProvider: string, primaryModel: string, stage: string, requestId?: string): Promise<{ result: T; usedProvider: string; usedModel: string; attempts: number; }>; /** * Classify error type and determine if it's retryable */ private classifyError; /** * Calculate retry delay with exponential backoff and jitter */ private calculateDelay; /** * Get or create circuit breaker for provider/model combination */ private getCircuitBreaker; /** * Log error for monitoring and debugging */ private logError; /** * Get error statistics for monitoring */ getErrorStats(timeWindow?: number): { totalErrors: number; errorsByType: Record<string, number>; errorsByProvider: Record<string, number>; errorsByModel: Record<string, number>; recentErrors: ErrorContext[]; }; /** * Get circuit breaker status for all monitored services */ getCircuitBreakerStatus(): Record<string, { state: string; failures: number; nextAttemptTime?: Date; }>; /** * Reset circuit breaker for a specific provider/model */ resetCircuitBreaker(provider: string, model: string): boolean; /** * Update retry configuration */ updateRetryConfig(config: Partial<RetryConfig>): void; /** * Update fallback configuration */ updateFallbackConfig(config: Partial<FallbackConfig>): void; /** * Sleep utility for delays */ private sleep; } export declare const globalErrorHandler: ErrorHandler; /** * Decorator for automatic error handling */ export declare function withErrorHandling(provider: string, model: string, stage: string): (target: any, propertyName: string, descriptor: PropertyDescriptor) => PropertyDescriptor; /** * Decorator for automatic fallback handling */ export declare function withFallback(stage: string): (target: any, propertyName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;