Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 1x 1x 28x 26x 28x 28x 28x 28x 28x 28x 28x 28x 6x 6x 28x 6x 6x 1x 31x 31x 31x 26x 26x 1x 11x 11x 11x 3x 2x 1x 11x 2x 2x 7x 7x 7x 7x 7x 7x 7x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 18x 18x 17x 8x 18x 10x 3x 3x 7x 7x 10x 10x 10x 7x 18x 1x 3x 3x 3x 3x 3x 3x 3x 3x | /**
* AgentKits — Retry with Exponential Backoff
*
* Wraps any async function with configurable retry logic.
* Handles 429 rate limits, timeouts, and network errors.
*
* Usage:
* import { withRetry, RetryConfig } from 'agentkits';
* const result = await withRetry(() => chat.complete('hello'), { maxRetries: 3 });
*/
// ── Types ──────────────────────────────────────────────────────────
export interface RetryConfig {
/** Maximum number of retry attempts (default: 3) */
maxRetries?: number;
/** Base delay in ms (default: 1000) */
baseDelay?: number;
/** Maximum delay in ms (default: 30000) */
maxDelay?: number;
/** Add random jitter to delays (default: true) */
jitter?: boolean;
/** Custom function to determine if error is retryable */
isRetryable?: (error: unknown) => boolean;
/** Called on each retry attempt */
onRetry?: (error: unknown, attempt: number, delay: number) => void;
/** AbortSignal for cancellation */
signal?: AbortSignal;
}
export interface RetryResult<T> {
result: T;
attempts: number;
totalDelay: number;
}
// ── Default retryable check ────────────────────────────────────────
const RETRYABLE_STATUS_CODES = new Set([429, 500, 502, 503, 504]);
export function isRetryableError(error: unknown): boolean {
if (error === null || error === undefined) return false;
const err = error as any;
// Rate limit (429)
if (err.status === 429 || err.statusCode === 429) return true;
// Server errors
if (typeof err.status === 'number' && RETRYABLE_STATUS_CODES.has(err.status)) return true;
if (typeof err.statusCode === 'number' && RETRYABLE_STATUS_CODES.has(err.statusCode)) return true;
// Network errors
const message = String(err.message ?? err).toLowerCase();
if (message.includes('econnreset') || message.includes('econnrefused') ||
message.includes('etimedout') || message.includes('socket hang up') ||
message.includes('network') || message.includes('timeout') ||
message.includes('fetch failed') || message.includes('enotfound')) {
return true;
}
// OpenAI SDK error codes
if (err.code === 'ECONNRESET' || err.code === 'ETIMEDOUT' || err.code === 'ECONNREFUSED') return true;
return false;
}
// ── Delay calculation ──────────────────────────────────────────────
export function calculateDelay(attempt: number, config: Required<Pick<RetryConfig, 'baseDelay' | 'maxDelay' | 'jitter'>>): number {
const exponential = config.baseDelay * Math.pow(2, attempt);
const capped = Math.min(exponential, config.maxDelay);
if (!config.jitter) return capped;
// Full jitter: random between 0 and capped
return Math.floor(Math.random() * capped);
}
/** Extract Retry-After header value in ms from error */
export function getRetryAfterMs(error: unknown): number | undefined {
const err = error as any;
const headers = err.headers ?? err.response?.headers;
if (!headers) return undefined;
const retryAfter = typeof headers.get === 'function'
? headers.get('retry-after')
: headers['retry-after'];
if (!retryAfter) return undefined;
const seconds = Number(retryAfter);
if (!isNaN(seconds)) return seconds * 1000;
const date = Date.parse(retryAfter);
if (!isNaN(date)) return Math.max(0, date - Date.now());
return undefined;
}
// ── Main ──────────────────────────────────────────────────────────
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise((resolve, reject) => {
if (signal?.aborted) { reject(new Error('Aborted')); return; }
const timer = setTimeout(resolve, ms);
signal?.addEventListener('abort', () => { clearTimeout(timer); reject(new Error('Aborted')); }, { once: true });
});
}
/**
* Retry an async function with exponential backoff.
*
* @example
* const reply = await withRetry(() => chat.complete('hello'), { maxRetries: 3 });
*/
export async function withRetry<T>(
fn: () => Promise<T>,
config: RetryConfig = {},
): Promise<RetryResult<T>> {
const {
maxRetries = 3,
baseDelay = 1000,
maxDelay = 30000,
jitter = true,
isRetryable = isRetryableError,
onRetry,
signal,
} = config;
let totalDelay = 0;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
if (signal?.aborted) throw new Error('Aborted');
const result = await fn();
return { result, attempts: attempt + 1, totalDelay };
} catch (error) {
if (attempt === maxRetries || !isRetryable(error)) {
throw error;
}
// Check Retry-After header first
const retryAfterMs = getRetryAfterMs(error);
const delay = retryAfterMs ?? calculateDelay(attempt, { baseDelay, maxDelay, jitter });
onRetry?.(error, attempt + 1, delay);
totalDelay += delay;
await sleep(delay, signal);
}
}
// Unreachable, but TypeScript needs it
throw new Error('Retry exhausted');
}
/**
* Create a retryable version of any async function.
*
* @example
* const retryableComplete = createRetryable(chat.complete.bind(chat), { maxRetries: 3 });
* const reply = await retryableComplete('hello');
*/
export function createRetryable<TArgs extends any[], TReturn>(
fn: (...args: TArgs) => Promise<TReturn>,
config: RetryConfig = {},
): (...args: TArgs) => Promise<TReturn> {
return async (...args: TArgs) => {
const { result } = await withRetry(() => fn(...args), config);
return result;
};
}
|