/** * Retry logic for LLM client operations * * Provides exponential backoff retry functionality for handling * transient errors like rate limits (429) and server errors (5xx). */ /** * Options for retry behavior */ export interface RetryOptions { /** Maximum number of retry attempts (default: 3) */ maxRetries?: number; /** Base delay in milliseconds before first retry (default: 1000) */ baseDelay?: number; /** Maximum delay in milliseconds between retries (default: 30000) */ maxDelay?: number; /** Optional callback invoked before each retry attempt */ onRetry?: (error: Error, attempt: number, delay: number) => void; } /** * Result of a retry operation */ export interface RetryResult { /** The successful result if operation succeeded */ result: T; /** Number of attempts made (1 = no retries needed) */ attempts: number; } /** * Calculate the backoff delay for a given retry attempt * * Uses exponential backoff: delay = baseDelay * 2^attempt * with a maximum cap to prevent excessive wait times. * * @param attempt - The retry attempt number (0-indexed) * @param baseDelay - Base delay in milliseconds * @param maxDelay - Maximum delay cap in milliseconds * @returns The calculated delay in milliseconds * * @example * ```ts * calculateBackoff(0, 1000, 30000); // 1000ms (1s) * calculateBackoff(1, 1000, 30000); // 2000ms (2s) * calculateBackoff(2, 1000, 30000); // 4000ms (4s) * calculateBackoff(3, 1000, 30000); // 8000ms (8s) * calculateBackoff(5, 1000, 30000); // 30000ms (capped at max) * ``` */ export declare function calculateBackoff(attempt: number, baseDelay?: number, maxDelay?: number): number; /** * Wrap an async function with retry logic using exponential backoff * * Only retries errors where `error.retryable` is true (typically 429 and 5xx errors). * Non-retryable errors (like 401/403) fail fast without retries. * * @param fn - The async function to wrap with retry logic * @param options - Retry configuration options * @returns A new function that retries on transient failures * * @example * ```ts * const fetchWithRetry = withRetry( * () => client.complete(messages, model), * { maxRetries: 3, baseDelay: 1000 } * ); * * const response = await fetchWithRetry(); * ``` * * @example * ```ts * // With retry callback for logging * const response = await withRetry( * () => client.complete(messages, model), * { * maxRetries: 3, * onRetry: (err, attempt, delay) => { * console.log(`Retry ${attempt} after ${delay}ms: ${err.message}`); * } * } * )(); * ``` */ export declare function withRetry(fn: () => Promise, options?: RetryOptions): () => Promise>; /** * Check if an error is retryable * * An error is retryable if: * - It's an LLMClientError with retryable = true * - It has a status of 429 (rate limit) or 5xx (server error) * * Non-retryable errors include: * - 401 (Unauthorized) - invalid API key * - 403 (Forbidden) - access denied * - 400 (Bad Request) - malformed request * * @param error - The error to check * @returns true if the error should trigger a retry */ export declare function isRetryableError(error: unknown): boolean; /** * Check if an error indicates an authentication failure * * These errors should fail fast without retries. * * @param error - The error to check * @returns true if the error is an auth failure */ export declare function isAuthError(error: unknown): boolean; /** * Execute a function with retry logic and return only the result * * This is a convenience wrapper around withRetry that discards * the attempt count information. * * @param fn - The async function to execute with retries * @param options - Retry configuration options * @returns The result of the successful execution * * @example * ```ts * const response = await retry( * () => client.complete(messages, model), * { maxRetries: 3 } * ); * ``` */ export declare function retry(fn: () => Promise, options?: RetryOptions): Promise; /** * Create a retryable version of an async function * * @param fn - The async function to make retryable * @param options - Default retry options * @returns A new function that accepts arguments and retries on failure * * @example * ```ts * const completeWithRetry = makeRetryable( * (messages: ChatMessage[], model: string) => * client.complete(messages, model), * { maxRetries: 3 } * ); * * const response = await completeWithRetry(messages, "glm-4"); * ``` */ export declare function makeRetryable(fn: (...args: TArgs) => Promise, options?: RetryOptions): (...args: TArgs) => Promise; //# sourceMappingURL=retry.d.ts.map