@ai-sdk/provider-utils 5.0.1 → 5.0.3
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.
- package/CHANGELOG.md +13 -0
- package/dist/index.d.ts +26 -1
- package/dist/index.js +81 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/retry-with-exponential-backoff.ts +143 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/provider-utils",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@standard-schema/spec": "^1.1.0",
|
|
36
36
|
"@workflow/serde": "4.1.0",
|
|
37
37
|
"eventsource-parser": "^3.0.8",
|
|
38
|
-
"@ai-sdk/provider": "4.0.
|
|
38
|
+
"@ai-sdk/provider": "4.0.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "22.19.19",
|
package/src/index.ts
CHANGED
|
@@ -70,6 +70,7 @@ export * from './remove-undefined-entries';
|
|
|
70
70
|
export * from './resolve';
|
|
71
71
|
export { resolveFullMediaType } from './resolve-full-media-type';
|
|
72
72
|
export { resolveProviderReference } from './resolve-provider-reference';
|
|
73
|
+
export * from './retry-with-exponential-backoff';
|
|
73
74
|
export * from './response-handler';
|
|
74
75
|
export {
|
|
75
76
|
asSchema,
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { delay } from './delay';
|
|
2
|
+
import { getErrorMessage } from './get-error-message';
|
|
3
|
+
import { isAbortError } from './is-abort-error';
|
|
4
|
+
|
|
5
|
+
export type RetryFunction = <OUTPUT>(
|
|
6
|
+
fn: () => PromiseLike<OUTPUT>,
|
|
7
|
+
) => PromiseLike<OUTPUT>;
|
|
8
|
+
|
|
9
|
+
export type RetryErrorReason = 'maxRetriesExceeded' | 'errorNotRetryable';
|
|
10
|
+
|
|
11
|
+
export type RetryErrorFactory = ({
|
|
12
|
+
message,
|
|
13
|
+
reason,
|
|
14
|
+
errors,
|
|
15
|
+
}: {
|
|
16
|
+
message: string;
|
|
17
|
+
reason: RetryErrorReason;
|
|
18
|
+
errors: Array<unknown>;
|
|
19
|
+
}) => unknown;
|
|
20
|
+
|
|
21
|
+
export type RetryDelayProvider = ({
|
|
22
|
+
error,
|
|
23
|
+
exponentialBackoffDelay,
|
|
24
|
+
}: {
|
|
25
|
+
error: unknown;
|
|
26
|
+
exponentialBackoffDelay: number;
|
|
27
|
+
}) => number;
|
|
28
|
+
|
|
29
|
+
export type ShouldRetryFunction = (
|
|
30
|
+
error: unknown,
|
|
31
|
+
) => boolean | Promise<boolean>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Retries a failed operation with exponential backoff.
|
|
35
|
+
*/
|
|
36
|
+
export const retryWithExponentialBackoff =
|
|
37
|
+
({
|
|
38
|
+
maxRetries = 2,
|
|
39
|
+
initialDelayInMs = 2000,
|
|
40
|
+
backoffFactor = 2,
|
|
41
|
+
abortSignal,
|
|
42
|
+
shouldRetry,
|
|
43
|
+
getDelayInMs = ({ exponentialBackoffDelay }) => exponentialBackoffDelay,
|
|
44
|
+
createRetryError = ({ message }) => new Error(message),
|
|
45
|
+
}: {
|
|
46
|
+
maxRetries?: number;
|
|
47
|
+
initialDelayInMs?: number;
|
|
48
|
+
backoffFactor?: number;
|
|
49
|
+
abortSignal?: AbortSignal;
|
|
50
|
+
shouldRetry: ShouldRetryFunction;
|
|
51
|
+
getDelayInMs?: RetryDelayProvider;
|
|
52
|
+
createRetryError?: RetryErrorFactory;
|
|
53
|
+
}): RetryFunction =>
|
|
54
|
+
async <OUTPUT>(f: () => PromiseLike<OUTPUT>) =>
|
|
55
|
+
retryWithExponentialBackoffInternal(f, {
|
|
56
|
+
maxRetries,
|
|
57
|
+
delayInMs: initialDelayInMs,
|
|
58
|
+
backoffFactor,
|
|
59
|
+
abortSignal,
|
|
60
|
+
shouldRetry,
|
|
61
|
+
getDelayInMs,
|
|
62
|
+
createRetryError,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
async function retryWithExponentialBackoffInternal<OUTPUT>(
|
|
66
|
+
f: () => PromiseLike<OUTPUT>,
|
|
67
|
+
{
|
|
68
|
+
maxRetries,
|
|
69
|
+
delayInMs,
|
|
70
|
+
backoffFactor,
|
|
71
|
+
abortSignal,
|
|
72
|
+
shouldRetry,
|
|
73
|
+
getDelayInMs,
|
|
74
|
+
createRetryError,
|
|
75
|
+
}: {
|
|
76
|
+
maxRetries: number;
|
|
77
|
+
delayInMs: number;
|
|
78
|
+
backoffFactor: number;
|
|
79
|
+
abortSignal: AbortSignal | undefined;
|
|
80
|
+
shouldRetry: ShouldRetryFunction;
|
|
81
|
+
getDelayInMs: RetryDelayProvider;
|
|
82
|
+
createRetryError: RetryErrorFactory;
|
|
83
|
+
},
|
|
84
|
+
errors: unknown[] = [],
|
|
85
|
+
): Promise<OUTPUT> {
|
|
86
|
+
try {
|
|
87
|
+
return await f();
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (isAbortError(error)) {
|
|
90
|
+
throw error; // don't retry when the request was aborted
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (maxRetries === 0) {
|
|
94
|
+
throw error; // don't wrap the error when retries are disabled
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const errorMessage = getErrorMessage(error);
|
|
98
|
+
const newErrors = [...errors, error];
|
|
99
|
+
const tryNumber = newErrors.length;
|
|
100
|
+
|
|
101
|
+
if (tryNumber > maxRetries) {
|
|
102
|
+
throw createRetryError({
|
|
103
|
+
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
|
104
|
+
reason: 'maxRetriesExceeded',
|
|
105
|
+
errors: newErrors,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if ((await shouldRetry(error)) && tryNumber <= maxRetries) {
|
|
110
|
+
await delay(
|
|
111
|
+
getDelayInMs({
|
|
112
|
+
error,
|
|
113
|
+
exponentialBackoffDelay: delayInMs,
|
|
114
|
+
}),
|
|
115
|
+
{ abortSignal },
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
return retryWithExponentialBackoffInternal(
|
|
119
|
+
f,
|
|
120
|
+
{
|
|
121
|
+
maxRetries,
|
|
122
|
+
delayInMs: backoffFactor * delayInMs,
|
|
123
|
+
backoffFactor,
|
|
124
|
+
abortSignal,
|
|
125
|
+
shouldRetry,
|
|
126
|
+
getDelayInMs,
|
|
127
|
+
createRetryError,
|
|
128
|
+
},
|
|
129
|
+
newErrors,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (tryNumber === 1) {
|
|
134
|
+
throw error; // don't wrap the error when a non-retryable error occurs on the first try
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
throw createRetryError({
|
|
138
|
+
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
|
139
|
+
reason: 'errorNotRetryable',
|
|
140
|
+
errors: newErrors,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|