/** * Error Translation Layer * * Normalizes error formats from different LLM providers (Anthropic, OpenAI, Gemini) * into AgentRouter's standardized error types. This allows consistent error handling * regardless of which provider produced the error. * * Provider error format differences: * - Anthropic: { type: "error", error: { type, message } } * - OpenAI: { error: { message, type, code } } * - Gemini: { error: { message, status, code } } * * All errors are mapped to the appropriate AgentRouterError subclass with the * original error preserved in the `cause` property for debugging. */ import { AgentRouterError } from '../types.js'; /** * Anthropic API error structure. * Format: { type: "error", error: { type: string, message: string } } */ export interface AnthropicError { type?: 'error'; error?: { type?: string; message?: string; }; status?: number; statusCode?: number; message?: string; } /** * OpenAI API error structure. * Format: { error: { message: string, type: string, code: string } } */ export interface OpenAIError { error?: { message?: string; type?: string; code?: string | null; param?: string | null; }; status?: number; statusCode?: number; message?: string; } /** * Google Gemini API error structure. * Format: { error: { message: string, status: string, code: number } } */ export interface GeminiError { error?: { message?: string; status?: string; code?: number; details?: { '@type'?: string; reason?: string; domain?: string; metadata?: Record; }[]; }; status?: number; statusCode?: number; message?: string; } /** * Generic error with HTTP-like properties (for errors from fetch, SDK wrappers, etc.) */ export interface HttpError extends Error { status?: number; statusCode?: number; code?: string; response?: { status?: number; data?: unknown; }; } /** * Translate an Anthropic API error to an AgentRouterError. * * Anthropic error format: * ```json * { * "type": "error", * "error": { * "type": "invalid_request_error" | "authentication_error" | "rate_limit_error" | ..., * "message": "Human-readable error message" * } * } * ``` * * Error types from Anthropic: * - invalid_request_error: Bad request parameters (400) * - authentication_error: Invalid API key (401) * - permission_error: Forbidden (403) * - not_found_error: Resource not found (404) * - rate_limit_error: Rate limited (429) * - api_error: Internal server error (500) * - overloaded_error: Overloaded (529) * * @param error - The raw Anthropic error * @returns Normalized AgentRouterError */ export declare function translateAnthropicError(error: AnthropicError): AgentRouterError; /** * Translate an OpenAI API error to an AgentRouterError. * * OpenAI error format: * ```json * { * "error": { * "message": "Human-readable error message", * "type": "invalid_request_error" | "authentication_error" | "rate_limit_error" | ..., * "code": "specific_error_code" | null, * "param": "parameter_name" | null * } * } * ``` * * Common error types from OpenAI: * - invalid_request_error: Bad request (400) * - invalid_api_key: Invalid API key (401) * - insufficient_quota: Quota exceeded (429) * - rate_limit_exceeded: Rate limited (429) * - server_error: Internal error (500) * - engine_overloaded: Overloaded (503) * * @param error - The raw OpenAI error * @returns Normalized AgentRouterError */ export declare function translateOpenAIError(error: OpenAIError): AgentRouterError; /** * Translate a Google Gemini API error to an AgentRouterError. * * Gemini error format: * ```json * { * "error": { * "code": 400, * "message": "Human-readable error message", * "status": "INVALID_ARGUMENT" | "UNAUTHENTICATED" | "RESOURCE_EXHAUSTED" | ..., * "details": [...] * } * } * ``` * * Common status values from Gemini: * - INVALID_ARGUMENT: Bad request (400) * - UNAUTHENTICATED: Invalid API key (401) * - PERMISSION_DENIED: Forbidden (403) * - NOT_FOUND: Resource not found (404) * - RESOURCE_EXHAUSTED: Rate limited or quota exceeded (429) * - INTERNAL: Internal server error (500) * - UNAVAILABLE: Service unavailable (503) * * @param error - The raw Gemini error * @returns Normalized AgentRouterError */ export declare function translateGeminiError(error: GeminiError): AgentRouterError; /** * Translate any error into an AgentRouterError. * Used when the provider is unknown or for generic error handling. * * @param error - Any error object * @param provider - Provider name for error context * @returns Normalized AgentRouterError */ export declare function translateGenericError(error: unknown, provider: string): AgentRouterError; /** * Translate an error from a specific provider. * Routes to the appropriate provider-specific translator. * * @param error - The raw error from the provider * @param provider - Provider name ('anthropic', 'openai', 'google', etc.) * @returns Normalized AgentRouterError */ export declare function translateProviderError(error: unknown, provider: string): AgentRouterError; /** * Wrap an async function to translate provider errors. * Useful for wrapping provider calls to ensure consistent error handling. * * @param fn - Async function to wrap * @param provider - Provider name for error context * @returns Wrapped function that translates errors */ export declare function withErrorTranslation(fn: (...args: T) => Promise, provider: string): (...args: T) => Promise; /** * Check if an error is retryable (transient error that may succeed on retry). * * @param error - The error to check * @returns True if the error is retryable */ export declare function isRetryableError(error: unknown): boolean; /** * Get recommended retry delay for an error. * * @param error - The error to get retry delay for * @param attempt - Current attempt number (1-based) * @param baseDelayMs - Base delay in milliseconds (default: 1000) * @returns Recommended delay in milliseconds, or undefined if not retryable */ export declare function getRetryDelay(error: unknown, attempt: number, baseDelayMs?: number): number | undefined; //# sourceMappingURL=errors.d.ts.map