@ai-sdk/provider-utils 4.0.33 → 4.0.35
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.mts +26 -1
- package/dist/index.d.ts +26 -1
- package/dist/index.js +82 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +81 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/retry-with-exponential-backoff.ts +143 -0
package/dist/index.mjs
CHANGED
|
@@ -677,7 +677,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
677
677
|
}
|
|
678
678
|
|
|
679
679
|
// src/version.ts
|
|
680
|
-
var VERSION = true ? "4.0.
|
|
680
|
+
var VERSION = true ? "4.0.35" : "0.0.0-test";
|
|
681
681
|
|
|
682
682
|
// src/get-from-api.ts
|
|
683
683
|
var getOriginalFetch = () => globalThis.fetch;
|
|
@@ -2624,6 +2624,85 @@ async function resolve(value) {
|
|
|
2624
2624
|
return Promise.resolve(value);
|
|
2625
2625
|
}
|
|
2626
2626
|
|
|
2627
|
+
// src/retry-with-exponential-backoff.ts
|
|
2628
|
+
var retryWithExponentialBackoff = ({
|
|
2629
|
+
maxRetries = 2,
|
|
2630
|
+
initialDelayInMs = 2e3,
|
|
2631
|
+
backoffFactor = 2,
|
|
2632
|
+
abortSignal,
|
|
2633
|
+
shouldRetry,
|
|
2634
|
+
getDelayInMs = ({ exponentialBackoffDelay }) => exponentialBackoffDelay,
|
|
2635
|
+
createRetryError = ({ message }) => new Error(message)
|
|
2636
|
+
}) => async (f) => retryWithExponentialBackoffInternal(f, {
|
|
2637
|
+
maxRetries,
|
|
2638
|
+
delayInMs: initialDelayInMs,
|
|
2639
|
+
backoffFactor,
|
|
2640
|
+
abortSignal,
|
|
2641
|
+
shouldRetry,
|
|
2642
|
+
getDelayInMs,
|
|
2643
|
+
createRetryError
|
|
2644
|
+
});
|
|
2645
|
+
async function retryWithExponentialBackoffInternal(f, {
|
|
2646
|
+
maxRetries,
|
|
2647
|
+
delayInMs,
|
|
2648
|
+
backoffFactor,
|
|
2649
|
+
abortSignal,
|
|
2650
|
+
shouldRetry,
|
|
2651
|
+
getDelayInMs,
|
|
2652
|
+
createRetryError
|
|
2653
|
+
}, errors = []) {
|
|
2654
|
+
try {
|
|
2655
|
+
return await f();
|
|
2656
|
+
} catch (error) {
|
|
2657
|
+
if (isAbortError(error)) {
|
|
2658
|
+
throw error;
|
|
2659
|
+
}
|
|
2660
|
+
if (maxRetries === 0) {
|
|
2661
|
+
throw error;
|
|
2662
|
+
}
|
|
2663
|
+
const errorMessage = getErrorMessage(error);
|
|
2664
|
+
const newErrors = [...errors, error];
|
|
2665
|
+
const tryNumber = newErrors.length;
|
|
2666
|
+
if (tryNumber > maxRetries) {
|
|
2667
|
+
throw createRetryError({
|
|
2668
|
+
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
|
2669
|
+
reason: "maxRetriesExceeded",
|
|
2670
|
+
errors: newErrors
|
|
2671
|
+
});
|
|
2672
|
+
}
|
|
2673
|
+
if (await shouldRetry(error) && tryNumber <= maxRetries) {
|
|
2674
|
+
await delay(
|
|
2675
|
+
getDelayInMs({
|
|
2676
|
+
error,
|
|
2677
|
+
exponentialBackoffDelay: delayInMs
|
|
2678
|
+
}),
|
|
2679
|
+
{ abortSignal }
|
|
2680
|
+
);
|
|
2681
|
+
return retryWithExponentialBackoffInternal(
|
|
2682
|
+
f,
|
|
2683
|
+
{
|
|
2684
|
+
maxRetries,
|
|
2685
|
+
delayInMs: backoffFactor * delayInMs,
|
|
2686
|
+
backoffFactor,
|
|
2687
|
+
abortSignal,
|
|
2688
|
+
shouldRetry,
|
|
2689
|
+
getDelayInMs,
|
|
2690
|
+
createRetryError
|
|
2691
|
+
},
|
|
2692
|
+
newErrors
|
|
2693
|
+
);
|
|
2694
|
+
}
|
|
2695
|
+
if (tryNumber === 1) {
|
|
2696
|
+
throw error;
|
|
2697
|
+
}
|
|
2698
|
+
throw createRetryError({
|
|
2699
|
+
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
|
2700
|
+
reason: "errorNotRetryable",
|
|
2701
|
+
errors: newErrors
|
|
2702
|
+
});
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
|
|
2627
2706
|
// src/response-handler.ts
|
|
2628
2707
|
import { APICallError as APICallError4, EmptyResponseBodyError } from "@ai-sdk/provider";
|
|
2629
2708
|
var textDecoder = new TextDecoder();
|
|
@@ -2871,6 +2950,7 @@ export {
|
|
|
2871
2950
|
readResponseWithSizeLimit,
|
|
2872
2951
|
removeUndefinedEntries,
|
|
2873
2952
|
resolve,
|
|
2953
|
+
retryWithExponentialBackoff,
|
|
2874
2954
|
safeParseJSON,
|
|
2875
2955
|
safeValidateTypes,
|
|
2876
2956
|
stripFileExtension,
|