/** * Retry Utility with Exponential Backoff * * Provides configurable retry logic with: * - Exponential backoff with jitter * - Configurable retry predicates * - Rate limit awareness (respects retryAfterMs) * - Callbacks for retry events */ /** * Configuration options for retry behavior. */ export interface RetryOptions { /** Maximum number of retry attempts (default: 3) */ maxAttempts?: number; /** Initial delay in milliseconds before first retry (default: 1000) */ initialDelayMs?: number; /** Maximum delay in milliseconds between retries (default: 30000) */ maxDelayMs?: number; /** Multiplier for exponential backoff (default: 2) */ backoffMultiplier?: number; /** Jitter factor to randomize delays (default: 0.1 = 10%) */ jitterFactor?: number; /** * Predicate to determine if an error should be retried. * @param error The error that occurred * @param attempt The current attempt number (1-indexed) * @returns true if the operation should be retried */ shouldRetry?: (error: Error, attempt: number) => boolean; /** * Callback invoked before each retry attempt. * @param error The error that triggered the retry * @param attempt The attempt number that just failed (1-indexed) * @param delayMs The delay before the next attempt */ onRetry?: (error: Error, attempt: number, delayMs: number) => void; } /** * Determines if an error is retryable by default. * * Retryable errors: * - RateLimitError (429 responses) * - TimeoutError (request timeouts) * - ProviderError with 5xx status codes (server errors) * * @param error The error to check * @returns true if the error is retryable */ export declare function isRetryableError(error: Error): boolean; /** * Calculates the delay before the next retry attempt using exponential backoff with jitter. * * Formula: delay = min(initialDelay * (multiplier ^ attempt), maxDelay) * (1 +/- jitter) * * @param attempt The attempt number (0-indexed, where 0 is the first retry) * @param options Retry configuration options * @returns The delay in milliseconds before the next attempt */ export declare function calculateDelay(attempt: number, options: Pick): number; /** * Executes a function with automatic retry on failure using exponential backoff. * * @param fn The async function to execute and potentially retry * @param options Configuration options for retry behavior * @returns The result of the function if successful * @throws The last error encountered if all retry attempts fail * * @example * ```typescript * const result = await retry( * () => provider.complete(request), * { * maxAttempts: 5, * initialDelayMs: 500, * onRetry: (error, attempt, delay) => { * console.log(`Attempt ${attempt} failed, retrying in ${delay}ms`); * } * } * ); * ``` */ export declare function retry(fn: () => Promise, options?: RetryOptions): Promise; /** * Creates a retryable version of an async function. * * This higher-order function wraps any async function with retry logic, * allowing you to create pre-configured retryable operations. * * @param fn The async function to wrap * @param options Configuration options for retry behavior * @returns A new function that executes with retry logic * * @example * ```typescript * const retryableComplete = withRetry( * () => provider.complete(request), * { maxAttempts: 5 } * ); * * // Later, execute with retry * const result = await retryableComplete(); * ``` */ export declare function withRetry(fn: () => Promise, options?: RetryOptions): () => Promise; //# sourceMappingURL=retry.d.ts.map