agents
Version:
A home for your AI agents
80 lines (79 loc) • 2.73 kB
TypeScript
//#region src/retries.d.ts
/**
* Retry options for schedule(), scheduleEvery(), queue(), and this.retry().
*/
interface RetryOptions {
/** Max number of attempts (including the first). Default: 3 */
maxAttempts?: number;
/** Base delay in ms for exponential backoff. Default: 100 */
baseDelayMs?: number;
/** Max delay cap in ms. Default: 3000 */
maxDelayMs?: number;
}
/**
* Internal options for tryN -- extends RetryOptions with a shouldRetry predicate.
*/
interface TryNOptions extends RetryOptions {
/**
* Predicate to determine if an error should be retried.
* Receives the error and the next attempt number (so callers can
* make attempt-aware decisions).
* If not provided, all errors are retried.
*/
shouldRetry?: (err: unknown, nextAttempt: number) => boolean;
}
/**
* Validate retry options eagerly so invalid config fails at enqueue/schedule time
* rather than at execution time. Checks individual field ranges, enforces integer
* maxAttempts, and validates cross-field constraints after resolving against
* defaults when provided.
*/
declare function validateRetryOptions(
options: RetryOptions,
defaults?: Required<RetryOptions>
): void;
/**
* Returns the number of milliseconds to wait before retrying a request.
* Uses the "Full Jitter" approach from
* https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
*
* @param attempt The current attempt number (1-indexed).
* @param baseDelayMs Base delay multiplier in ms.
* @param maxDelayMs Maximum delay cap in ms.
* @returns Milliseconds to wait before retrying.
*/
declare function jitterBackoff(
attempt: number,
baseDelayMs: number,
maxDelayMs: number
): number;
/**
* Retry an async function up to `n` total attempts with jittered exponential backoff.
*
* @param n Total number of attempts (must be a finite integer >= 1).
* @param fn The async function to retry. Receives the current attempt number (1-indexed).
* @param options Retry configuration.
* @returns The result of `fn` on success.
* @throws The last error if all attempts fail or `shouldRetry` returns false.
*/
declare function tryN<T>(
n: number,
fn: (attempt: number) => Promise<T>,
options?: TryNOptions
): Promise<T>;
/**
* Returns true if the given error is retryable according to Durable Object error handling.
* See https://developers.cloudflare.com/durable-objects/best-practices/error-handling/
*
* An error is retryable if it has `retryable: true` but is NOT an overloaded error.
*/
declare function isErrorRetryable(err: unknown): boolean;
//#endregion
export {
RetryOptions,
isErrorRetryable,
jitterBackoff,
tryN,
validateRetryOptions
};
//# sourceMappingURL=retries.d.ts.map