ai
Version:
AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.
55 lines (51 loc) • 1.36 kB
text/typescript
import { InvalidArgumentError } from '../error/invalid-argument-error';
import type {
RetryFunction,
ShouldRetryFunction,
} from '@ai-sdk/provider-utils';
import { retryWithExponentialBackoffRespectingRetryHeaders } from '../util/retry-with-exponential-backoff';
/**
* Validate and prepare retries.
*/
export function prepareRetries({
maxRetries,
abortSignal,
additionalRetryableError,
parameter = 'maxRetries',
defaultMaxRetries = 2,
}: {
maxRetries: number | undefined;
abortSignal: AbortSignal | undefined;
additionalRetryableError?: ShouldRetryFunction;
parameter?: string;
defaultMaxRetries?: number;
}): {
maxRetries: number;
retry: RetryFunction;
} {
if (maxRetries != null) {
if (!Number.isInteger(maxRetries)) {
throw new InvalidArgumentError({
parameter,
value: maxRetries,
message: `${parameter} must be an integer`,
});
}
if (maxRetries < 0) {
throw new InvalidArgumentError({
parameter,
value: maxRetries,
message: `${parameter} must be >= 0`,
});
}
}
const maxRetriesResult = maxRetries ?? defaultMaxRetries;
return {
maxRetries: maxRetriesResult,
retry: retryWithExponentialBackoffRespectingRetryHeaders({
maxRetries: maxRetriesResult,
abortSignal,
additionalRetryableError,
}),
};
}