UNPKG

@xynehq/jaf

Version:

Juspay Agent Framework - A purely functional agent framework with immutable state and composable tools

100 lines 5.28 kB
/** * JAF ADK Layer - LLM Error Handling * * Comprehensive error handling for LLM API failures following JAF patterns */ import { AdkErrorObject } from '../types.js'; export interface LLMError extends AdkErrorObject { provider?: string; model?: string; retryable?: boolean; statusCode?: number; responseBody?: string; } export interface LLMRetryConfig { maxRetries: number; baseDelay: number; maxDelay: number; retryableErrors: string[]; } export declare const LLM_ERROR_TYPES: { readonly INVALID_API_KEY: "INVALID_API_KEY"; readonly UNAUTHORIZED: "UNAUTHORIZED"; readonly QUOTA_EXCEEDED: "QUOTA_EXCEEDED"; readonly INVALID_REQUEST: "INVALID_REQUEST"; readonly MODEL_NOT_FOUND: "MODEL_NOT_FOUND"; readonly UNSUPPORTED_MODEL: "UNSUPPORTED_MODEL"; readonly INVALID_PARAMETERS: "INVALID_PARAMETERS"; readonly RATE_LIMITED: "RATE_LIMITED"; readonly CONCURRENT_LIMIT: "CONCURRENT_LIMIT"; readonly SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE"; readonly TIMEOUT: "TIMEOUT"; readonly NETWORK_ERROR: "NETWORK_ERROR"; readonly CONTENT_FILTERED: "CONTENT_FILTERED"; readonly CONTENT_TOO_LONG: "CONTENT_TOO_LONG"; readonly FUNCTION_CALL_ERROR: "FUNCTION_CALL_ERROR"; readonly FUNCTION_NOT_FOUND: "FUNCTION_NOT_FOUND"; readonly UNKNOWN_ERROR: "UNKNOWN_ERROR"; }; export declare const RETRYABLE_ERRORS: Set<"TIMEOUT" | "RATE_LIMITED" | "CONCURRENT_LIMIT" | "SERVICE_UNAVAILABLE" | "NETWORK_ERROR">; export declare const createLLMError: (message: string, code: string, context?: { provider?: string; model?: string; statusCode?: number; responseBody?: string; }) => LLMError; export declare const createLLMTimeoutError: (provider: string, model: string, timeout: number) => LLMError; export declare const createLLMRateLimitError: (provider: string, model: string, resetTime?: number) => LLMError; export declare const createLLMQuotaError: (provider: string, model: string) => LLMError; export declare const createLLMContentFilterError: (provider: string, model: string) => LLMError; export declare const classifyLLMError: (error: Error, provider: string, model: string) => LLMError; export declare const DEFAULT_RETRY_CONFIG: LLMRetryConfig; export declare const shouldRetryError: (error: LLMError, config?: LLMRetryConfig) => boolean; export declare const calculateRetryDelay: (attempt: number, config?: LLMRetryConfig, jitter?: boolean) => number; export declare const withLLMRetry: <T extends unknown[], R>(fn: (...args: T) => Promise<R>, config?: Partial<LLMRetryConfig>, provider?: string, model?: string) => (...args: T) => Promise<R>; export declare const withLLMTimeout: <T extends unknown[], R>(fn: (...args: T) => Promise<R>, timeoutMs: number, provider?: string, model?: string) => (...args: T) => Promise<R>; export interface CircuitBreakerConfig { failureThreshold: number; resetTimeout: number; monitoringPeriod: number; } export interface CircuitBreakerState { failures: number; lastFailureTime: number; state: 'closed' | 'open' | 'half-open'; } export declare const createCircuitBreaker: <T extends unknown[], R>(fn: (...args: T) => Promise<R>, config: CircuitBreakerConfig, provider?: string, model?: string) => (...args: T) => Promise<R>; export declare const createFallbackStrategy: <T extends unknown[], R>(primaryFn: (...args: T) => Promise<R>, fallbackFn: (...args: T) => Promise<R>, shouldFallback?: (error: LLMError) => boolean) => (...args: T) => Promise<R>; export interface LLMErrorMetrics { totalErrors: number; errorsByType: Record<string, number>; errorsByProvider: Record<string, number>; errorsByModel: Record<string, number>; lastError?: LLMError; lastErrorTime?: number; } export declare const createLLMErrorMonitor: () => { recordError: (error: LLMError) => void; getMetrics: () => LLMErrorMetrics; resetMetrics: () => void; }; export interface LLMErrorLogger { logError: (error: LLMError, context?: Record<string, unknown>) => void; logRetry: (error: LLMError, attempt: number, delay: number) => void; logRecovery: (error: LLMError, recoveryMethod: string) => void; } export declare const createLLMErrorLogger: () => LLMErrorLogger; export declare const defaultLLMErrorHandler: { classify: (error: Error, provider: string, model: string) => LLMError; retry: <T extends unknown[], R>(fn: (...args: T) => Promise<R>, config?: Partial<LLMRetryConfig>, provider?: string, model?: string) => (...args: T) => Promise<R>; timeout: <T extends unknown[], R>(fn: (...args: T) => Promise<R>, timeoutMs: number, provider?: string, model?: string) => (...args: T) => Promise<R>; fallback: <T extends unknown[], R>(primaryFn: (...args: T) => Promise<R>, fallbackFn: (...args: T) => Promise<R>, shouldFallback?: (error: LLMError) => boolean) => (...args: T) => Promise<R>; circuitBreaker: <T extends unknown[], R>(fn: (...args: T) => Promise<R>, config: CircuitBreakerConfig, provider?: string, model?: string) => (...args: T) => Promise<R>; monitor: { recordError: (error: LLMError) => void; getMetrics: () => LLMErrorMetrics; resetMetrics: () => void; }; logger: LLMErrorLogger; }; //# sourceMappingURL=error-handler.d.ts.map