ai 6.0.217 → 6.0.218

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,6 @@
1
1
  import { InvalidArgumentError } from '../error/invalid-argument-error';
2
- import {
3
- retryWithExponentialBackoffRespectingRetryHeaders,
4
- type RetryFunction,
5
- } from '../util/retry-with-exponential-backoff';
2
+ import type { RetryFunction } from '@ai-sdk/provider-utils';
3
+ import { retryWithExponentialBackoffRespectingRetryHeaders } from '../util/retry-with-exponential-backoff';
6
4
 
7
5
  /**
8
6
  * Validate and prepare retries.
@@ -1,12 +1,11 @@
1
1
  import { APICallError } from '@ai-sdk/provider';
2
2
  import { GatewayError } from '@ai-sdk/gateway';
3
- import { delay, getErrorMessage, isAbortError } from '@ai-sdk/provider-utils';
3
+ import {
4
+ retryWithExponentialBackoff,
5
+ type RetryFunction,
6
+ } from '@ai-sdk/provider-utils';
4
7
  import { RetryError } from './retry-error';
5
8
 
6
- export type RetryFunction = <OUTPUT>(
7
- fn: () => PromiseLike<OUTPUT>,
8
- ) => PromiseLike<OUTPUT>;
9
-
10
9
  function getRetryDelayInMs({
11
10
  error,
12
11
  exponentialBackoffDelay,
@@ -62,98 +61,31 @@ function getRetryDelayInMs({
62
61
  * while respecting rate limit headers (retry-after-ms and retry-after) if they are provided and reasonable (0-60 seconds).
63
62
  * You can configure the maximum number of retries, the initial delay, and the backoff factor.
64
63
  */
65
- export const retryWithExponentialBackoffRespectingRetryHeaders =
66
- ({
67
- maxRetries = 2,
68
- initialDelayInMs = 2000,
69
- backoffFactor = 2,
70
- abortSignal,
71
- }: {
72
- maxRetries?: number;
73
- initialDelayInMs?: number;
74
- backoffFactor?: number;
75
- abortSignal?: AbortSignal;
76
- } = {}): RetryFunction =>
77
- async <OUTPUT>(f: () => PromiseLike<OUTPUT>) =>
78
- _retryWithExponentialBackoff(f, {
79
- maxRetries,
80
- delayInMs: initialDelayInMs,
81
- backoffFactor,
82
- abortSignal,
83
- });
84
-
85
- async function _retryWithExponentialBackoff<OUTPUT>(
86
- f: () => PromiseLike<OUTPUT>,
87
- {
64
+ export const retryWithExponentialBackoffRespectingRetryHeaders = ({
65
+ maxRetries = 2,
66
+ initialDelayInMs = 2000,
67
+ backoffFactor = 2,
68
+ abortSignal,
69
+ }: {
70
+ maxRetries?: number;
71
+ initialDelayInMs?: number;
72
+ backoffFactor?: number;
73
+ abortSignal?: AbortSignal;
74
+ } = {}): RetryFunction =>
75
+ retryWithExponentialBackoff({
88
76
  maxRetries,
89
- delayInMs,
77
+ initialDelayInMs,
90
78
  backoffFactor,
91
79
  abortSignal,
92
- }: {
93
- maxRetries: number;
94
- delayInMs: number;
95
- backoffFactor: number;
96
- abortSignal: AbortSignal | undefined;
97
- },
98
- errors: unknown[] = [],
99
- ): Promise<OUTPUT> {
100
- try {
101
- return await f();
102
- } catch (error) {
103
- if (isAbortError(error)) {
104
- throw error; // don't retry when the request was aborted
105
- }
106
-
107
- if (maxRetries === 0) {
108
- throw error; // don't wrap the error when retries are disabled
109
- }
110
-
111
- const errorMessage = getErrorMessage(error);
112
- const newErrors = [...errors, error];
113
- const tryNumber = newErrors.length;
114
-
115
- if (tryNumber > maxRetries) {
116
- throw new RetryError({
117
- message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
118
- reason: 'maxRetriesExceeded',
119
- errors: newErrors,
120
- });
121
- }
122
-
123
- if (
80
+ shouldRetry: error =>
124
81
  error instanceof Error &&
125
82
  ((APICallError.isInstance(error) && error.isRetryable === true) ||
126
- (GatewayError.isInstance(error) && error.isRetryable === true)) &&
127
- tryNumber <= maxRetries
128
- ) {
129
- await delay(
130
- getRetryDelayInMs({
131
- error: error as APICallError | GatewayError,
132
- exponentialBackoffDelay: delayInMs,
133
- }),
134
- { abortSignal },
135
- );
136
-
137
- return _retryWithExponentialBackoff(
138
- f,
139
- {
140
- maxRetries,
141
- delayInMs: backoffFactor * delayInMs,
142
- backoffFactor,
143
- abortSignal,
144
- },
145
- newErrors,
146
- );
147
- }
148
-
149
- if (tryNumber === 1) {
150
- throw error; // don't wrap the error when a non-retryable error occurs on the first try
151
- }
152
-
153
- throw new RetryError({
154
- message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
155
- reason: 'errorNotRetryable',
156
- errors: newErrors,
157
- });
158
- }
159
- }
83
+ (GatewayError.isInstance(error) && error.isRetryable === true)),
84
+ getDelayInMs: ({ error, exponentialBackoffDelay }) =>
85
+ getRetryDelayInMs({
86
+ error: error as APICallError | GatewayError,
87
+ exponentialBackoffDelay,
88
+ }),
89
+ createRetryError: ({ message, reason, errors }) =>
90
+ new RetryError({ message, reason, errors }),
91
+ });