@ai-sdk/provider-utils 4.0.34 → 4.0.36
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 +12 -0
- package/dist/index.d.mts +30 -1
- package/dist/index.d.ts +30 -1
- package/dist/index.js +84 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +82 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/retry-with-exponential-backoff.ts +143 -0
- package/src/types/tool.ts +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @ai-sdk/provider-utils
|
|
2
2
|
|
|
3
|
+
## 4.0.36
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0952964: Prevent prototype pollution when synchronously parsing provider JSON inputs and expose `secureJsonParse` from provider-utils.
|
|
8
|
+
|
|
9
|
+
## 4.0.35
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- ea1e95b: feat(mcp): add maxRetries option for failed mcp tool calls
|
|
14
|
+
|
|
3
15
|
## 4.0.34
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1189,6 +1189,8 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1189
1189
|
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
1190
1190
|
*
|
|
1191
1191
|
* If not provided, the tool result will be sent as a JSON object.
|
|
1192
|
+
*
|
|
1193
|
+
* This function is invoked on the server by `convertToModelMessages`, so ensure that you pass the same "tools" (ToolSet) to both "convertToModelMessages" and "streamText" (or other generation APIs).
|
|
1192
1194
|
*/
|
|
1193
1195
|
toModelOutput?: (options: {
|
|
1194
1196
|
/**
|
|
@@ -1360,6 +1362,33 @@ type Resolvable<T> = MaybePromiseLike<T> | (() => MaybePromiseLike<T>);
|
|
|
1360
1362
|
*/
|
|
1361
1363
|
declare function resolve<T>(value: Resolvable<T>): Promise<T>;
|
|
1362
1364
|
|
|
1365
|
+
type RetryFunction = <OUTPUT>(fn: () => PromiseLike<OUTPUT>) => PromiseLike<OUTPUT>;
|
|
1366
|
+
type RetryErrorReason = 'maxRetriesExceeded' | 'errorNotRetryable';
|
|
1367
|
+
type RetryErrorFactory = ({ message, reason, errors, }: {
|
|
1368
|
+
message: string;
|
|
1369
|
+
reason: RetryErrorReason;
|
|
1370
|
+
errors: Array<unknown>;
|
|
1371
|
+
}) => unknown;
|
|
1372
|
+
type RetryDelayProvider = ({ error, exponentialBackoffDelay, }: {
|
|
1373
|
+
error: unknown;
|
|
1374
|
+
exponentialBackoffDelay: number;
|
|
1375
|
+
}) => number;
|
|
1376
|
+
type ShouldRetryFunction = (error: unknown) => boolean | Promise<boolean>;
|
|
1377
|
+
/**
|
|
1378
|
+
* Retries a failed operation with exponential backoff.
|
|
1379
|
+
*/
|
|
1380
|
+
declare const retryWithExponentialBackoff: ({ maxRetries, initialDelayInMs, backoffFactor, abortSignal, shouldRetry, getDelayInMs, createRetryError, }: {
|
|
1381
|
+
maxRetries?: number;
|
|
1382
|
+
initialDelayInMs?: number;
|
|
1383
|
+
backoffFactor?: number;
|
|
1384
|
+
abortSignal?: AbortSignal;
|
|
1385
|
+
shouldRetry: ShouldRetryFunction;
|
|
1386
|
+
getDelayInMs?: RetryDelayProvider;
|
|
1387
|
+
createRetryError?: RetryErrorFactory;
|
|
1388
|
+
}) => RetryFunction;
|
|
1389
|
+
|
|
1390
|
+
declare function secureJsonParse(text: string): any;
|
|
1391
|
+
|
|
1363
1392
|
/**
|
|
1364
1393
|
* Strips file extension segments from a filename.
|
|
1365
1394
|
*
|
|
@@ -1518,4 +1547,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
|
|
1518
1547
|
*/
|
|
1519
1548
|
type ToolCallOptions = ToolExecutionOptions;
|
|
1520
1549
|
|
|
1521
|
-
export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
|
1550
|
+
export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -1189,6 +1189,8 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
|
1189
1189
|
* Optional conversion function that maps the tool result to an output that can be used by the language model.
|
|
1190
1190
|
*
|
|
1191
1191
|
* If not provided, the tool result will be sent as a JSON object.
|
|
1192
|
+
*
|
|
1193
|
+
* This function is invoked on the server by `convertToModelMessages`, so ensure that you pass the same "tools" (ToolSet) to both "convertToModelMessages" and "streamText" (or other generation APIs).
|
|
1192
1194
|
*/
|
|
1193
1195
|
toModelOutput?: (options: {
|
|
1194
1196
|
/**
|
|
@@ -1360,6 +1362,33 @@ type Resolvable<T> = MaybePromiseLike<T> | (() => MaybePromiseLike<T>);
|
|
|
1360
1362
|
*/
|
|
1361
1363
|
declare function resolve<T>(value: Resolvable<T>): Promise<T>;
|
|
1362
1364
|
|
|
1365
|
+
type RetryFunction = <OUTPUT>(fn: () => PromiseLike<OUTPUT>) => PromiseLike<OUTPUT>;
|
|
1366
|
+
type RetryErrorReason = 'maxRetriesExceeded' | 'errorNotRetryable';
|
|
1367
|
+
type RetryErrorFactory = ({ message, reason, errors, }: {
|
|
1368
|
+
message: string;
|
|
1369
|
+
reason: RetryErrorReason;
|
|
1370
|
+
errors: Array<unknown>;
|
|
1371
|
+
}) => unknown;
|
|
1372
|
+
type RetryDelayProvider = ({ error, exponentialBackoffDelay, }: {
|
|
1373
|
+
error: unknown;
|
|
1374
|
+
exponentialBackoffDelay: number;
|
|
1375
|
+
}) => number;
|
|
1376
|
+
type ShouldRetryFunction = (error: unknown) => boolean | Promise<boolean>;
|
|
1377
|
+
/**
|
|
1378
|
+
* Retries a failed operation with exponential backoff.
|
|
1379
|
+
*/
|
|
1380
|
+
declare const retryWithExponentialBackoff: ({ maxRetries, initialDelayInMs, backoffFactor, abortSignal, shouldRetry, getDelayInMs, createRetryError, }: {
|
|
1381
|
+
maxRetries?: number;
|
|
1382
|
+
initialDelayInMs?: number;
|
|
1383
|
+
backoffFactor?: number;
|
|
1384
|
+
abortSignal?: AbortSignal;
|
|
1385
|
+
shouldRetry: ShouldRetryFunction;
|
|
1386
|
+
getDelayInMs?: RetryDelayProvider;
|
|
1387
|
+
createRetryError?: RetryErrorFactory;
|
|
1388
|
+
}) => RetryFunction;
|
|
1389
|
+
|
|
1390
|
+
declare function secureJsonParse(text: string): any;
|
|
1391
|
+
|
|
1363
1392
|
/**
|
|
1364
1393
|
* Strips file extension segments from a filename.
|
|
1365
1394
|
*
|
|
@@ -1518,4 +1547,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
|
|
1518
1547
|
*/
|
|
1519
1548
|
type ToolCallOptions = ToolExecutionOptions;
|
|
1520
1549
|
|
|
1521
|
-
export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
|
1550
|
+
export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
package/dist/index.js
CHANGED
|
@@ -86,8 +86,10 @@ __export(index_exports, {
|
|
|
86
86
|
readResponseWithSizeLimit: () => readResponseWithSizeLimit,
|
|
87
87
|
removeUndefinedEntries: () => removeUndefinedEntries,
|
|
88
88
|
resolve: () => resolve,
|
|
89
|
+
retryWithExponentialBackoff: () => retryWithExponentialBackoff,
|
|
89
90
|
safeParseJSON: () => safeParseJSON,
|
|
90
91
|
safeValidateTypes: () => safeValidateTypes,
|
|
92
|
+
secureJsonParse: () => secureJsonParse,
|
|
91
93
|
stripFileExtension: () => stripFileExtension,
|
|
92
94
|
tool: () => tool,
|
|
93
95
|
validateDownloadUrl: () => validateDownloadUrl,
|
|
@@ -777,7 +779,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
777
779
|
}
|
|
778
780
|
|
|
779
781
|
// src/version.ts
|
|
780
|
-
var VERSION = true ? "4.0.
|
|
782
|
+
var VERSION = true ? "4.0.36" : "0.0.0-test";
|
|
781
783
|
|
|
782
784
|
// src/get-from-api.ts
|
|
783
785
|
var getOriginalFetch = () => globalThis.fetch;
|
|
@@ -2715,6 +2717,85 @@ async function resolve(value) {
|
|
|
2715
2717
|
return Promise.resolve(value);
|
|
2716
2718
|
}
|
|
2717
2719
|
|
|
2720
|
+
// src/retry-with-exponential-backoff.ts
|
|
2721
|
+
var retryWithExponentialBackoff = ({
|
|
2722
|
+
maxRetries = 2,
|
|
2723
|
+
initialDelayInMs = 2e3,
|
|
2724
|
+
backoffFactor = 2,
|
|
2725
|
+
abortSignal,
|
|
2726
|
+
shouldRetry,
|
|
2727
|
+
getDelayInMs = ({ exponentialBackoffDelay }) => exponentialBackoffDelay,
|
|
2728
|
+
createRetryError = ({ message }) => new Error(message)
|
|
2729
|
+
}) => async (f) => retryWithExponentialBackoffInternal(f, {
|
|
2730
|
+
maxRetries,
|
|
2731
|
+
delayInMs: initialDelayInMs,
|
|
2732
|
+
backoffFactor,
|
|
2733
|
+
abortSignal,
|
|
2734
|
+
shouldRetry,
|
|
2735
|
+
getDelayInMs,
|
|
2736
|
+
createRetryError
|
|
2737
|
+
});
|
|
2738
|
+
async function retryWithExponentialBackoffInternal(f, {
|
|
2739
|
+
maxRetries,
|
|
2740
|
+
delayInMs,
|
|
2741
|
+
backoffFactor,
|
|
2742
|
+
abortSignal,
|
|
2743
|
+
shouldRetry,
|
|
2744
|
+
getDelayInMs,
|
|
2745
|
+
createRetryError
|
|
2746
|
+
}, errors = []) {
|
|
2747
|
+
try {
|
|
2748
|
+
return await f();
|
|
2749
|
+
} catch (error) {
|
|
2750
|
+
if (isAbortError(error)) {
|
|
2751
|
+
throw error;
|
|
2752
|
+
}
|
|
2753
|
+
if (maxRetries === 0) {
|
|
2754
|
+
throw error;
|
|
2755
|
+
}
|
|
2756
|
+
const errorMessage = getErrorMessage(error);
|
|
2757
|
+
const newErrors = [...errors, error];
|
|
2758
|
+
const tryNumber = newErrors.length;
|
|
2759
|
+
if (tryNumber > maxRetries) {
|
|
2760
|
+
throw createRetryError({
|
|
2761
|
+
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
|
2762
|
+
reason: "maxRetriesExceeded",
|
|
2763
|
+
errors: newErrors
|
|
2764
|
+
});
|
|
2765
|
+
}
|
|
2766
|
+
if (await shouldRetry(error) && tryNumber <= maxRetries) {
|
|
2767
|
+
await delay(
|
|
2768
|
+
getDelayInMs({
|
|
2769
|
+
error,
|
|
2770
|
+
exponentialBackoffDelay: delayInMs
|
|
2771
|
+
}),
|
|
2772
|
+
{ abortSignal }
|
|
2773
|
+
);
|
|
2774
|
+
return retryWithExponentialBackoffInternal(
|
|
2775
|
+
f,
|
|
2776
|
+
{
|
|
2777
|
+
maxRetries,
|
|
2778
|
+
delayInMs: backoffFactor * delayInMs,
|
|
2779
|
+
backoffFactor,
|
|
2780
|
+
abortSignal,
|
|
2781
|
+
shouldRetry,
|
|
2782
|
+
getDelayInMs,
|
|
2783
|
+
createRetryError
|
|
2784
|
+
},
|
|
2785
|
+
newErrors
|
|
2786
|
+
);
|
|
2787
|
+
}
|
|
2788
|
+
if (tryNumber === 1) {
|
|
2789
|
+
throw error;
|
|
2790
|
+
}
|
|
2791
|
+
throw createRetryError({
|
|
2792
|
+
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
|
2793
|
+
reason: "errorNotRetryable",
|
|
2794
|
+
errors: newErrors
|
|
2795
|
+
});
|
|
2796
|
+
}
|
|
2797
|
+
}
|
|
2798
|
+
|
|
2718
2799
|
// src/response-handler.ts
|
|
2719
2800
|
var import_provider12 = require("@ai-sdk/provider");
|
|
2720
2801
|
var textDecoder = new TextDecoder();
|
|
@@ -2961,8 +3042,10 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
|
2961
3042
|
readResponseWithSizeLimit,
|
|
2962
3043
|
removeUndefinedEntries,
|
|
2963
3044
|
resolve,
|
|
3045
|
+
retryWithExponentialBackoff,
|
|
2964
3046
|
safeParseJSON,
|
|
2965
3047
|
safeValidateTypes,
|
|
3048
|
+
secureJsonParse,
|
|
2966
3049
|
stripFileExtension,
|
|
2967
3050
|
tool,
|
|
2968
3051
|
validateDownloadUrl,
|