@ai-sdk/provider-utils 3.0.0-beta.1 → 3.0.0-beta.10
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 +58 -0
- package/dist/index.d.mts +89 -61
- package/dist/index.d.ts +89 -61
- package/dist/index.js +119 -87
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +94 -63
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
@@ -39,8 +39,33 @@ function convertAsyncIteratorToReadableStream(iterator) {
|
|
39
39
|
}
|
40
40
|
|
41
41
|
// src/delay.ts
|
42
|
-
async function delay(delayInMs) {
|
43
|
-
|
42
|
+
async function delay(delayInMs, options) {
|
43
|
+
if (delayInMs == null) {
|
44
|
+
return Promise.resolve();
|
45
|
+
}
|
46
|
+
const signal = options == null ? void 0 : options.abortSignal;
|
47
|
+
return new Promise((resolve2, reject) => {
|
48
|
+
if (signal == null ? void 0 : signal.aborted) {
|
49
|
+
reject(createAbortError());
|
50
|
+
return;
|
51
|
+
}
|
52
|
+
const timeoutId = setTimeout(() => {
|
53
|
+
cleanup();
|
54
|
+
resolve2();
|
55
|
+
}, delayInMs);
|
56
|
+
const cleanup = () => {
|
57
|
+
clearTimeout(timeoutId);
|
58
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
59
|
+
};
|
60
|
+
const onAbort = () => {
|
61
|
+
cleanup();
|
62
|
+
reject(createAbortError());
|
63
|
+
};
|
64
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort);
|
65
|
+
});
|
66
|
+
}
|
67
|
+
function createAbortError() {
|
68
|
+
return new DOMException("Delay was aborted", "AbortError");
|
44
69
|
}
|
45
70
|
|
46
71
|
// src/extract-response-headers.ts
|
@@ -92,8 +117,43 @@ function getErrorMessage(error) {
|
|
92
117
|
}
|
93
118
|
|
94
119
|
// src/get-from-api.ts
|
120
|
+
import { APICallError as APICallError2 } from "@ai-sdk/provider";
|
121
|
+
|
122
|
+
// src/handle-fetch-error.ts
|
95
123
|
import { APICallError } from "@ai-sdk/provider";
|
96
124
|
|
125
|
+
// src/is-abort-error.ts
|
126
|
+
function isAbortError(error) {
|
127
|
+
return (error instanceof Error || error instanceof DOMException) && (error.name === "AbortError" || error.name === "ResponseAborted" || // Next.js
|
128
|
+
error.name === "TimeoutError");
|
129
|
+
}
|
130
|
+
|
131
|
+
// src/handle-fetch-error.ts
|
132
|
+
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
133
|
+
function handleFetchError({
|
134
|
+
error,
|
135
|
+
url,
|
136
|
+
requestBodyValues
|
137
|
+
}) {
|
138
|
+
if (isAbortError(error)) {
|
139
|
+
return error;
|
140
|
+
}
|
141
|
+
if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
|
142
|
+
const cause = error.cause;
|
143
|
+
if (cause != null) {
|
144
|
+
return new APICallError({
|
145
|
+
message: `Cannot connect to API: ${cause.message}`,
|
146
|
+
cause,
|
147
|
+
url,
|
148
|
+
requestBodyValues,
|
149
|
+
isRetryable: true
|
150
|
+
// retry when network error
|
151
|
+
});
|
152
|
+
}
|
153
|
+
}
|
154
|
+
return error;
|
155
|
+
}
|
156
|
+
|
97
157
|
// src/remove-undefined-entries.ts
|
98
158
|
function removeUndefinedEntries(record) {
|
99
159
|
return Object.fromEntries(
|
@@ -101,11 +161,6 @@ function removeUndefinedEntries(record) {
|
|
101
161
|
);
|
102
162
|
}
|
103
163
|
|
104
|
-
// src/is-abort-error.ts
|
105
|
-
function isAbortError(error) {
|
106
|
-
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
107
|
-
}
|
108
|
-
|
109
164
|
// src/get-from-api.ts
|
110
165
|
var getOriginalFetch = () => globalThis.fetch;
|
111
166
|
var getFromApi = async ({
|
@@ -132,10 +187,10 @@ var getFromApi = async ({
|
|
132
187
|
requestBodyValues: {}
|
133
188
|
});
|
134
189
|
} catch (error) {
|
135
|
-
if (isAbortError(error) ||
|
190
|
+
if (isAbortError(error) || APICallError2.isInstance(error)) {
|
136
191
|
throw error;
|
137
192
|
}
|
138
|
-
throw new
|
193
|
+
throw new APICallError2({
|
139
194
|
message: "Failed to process error response",
|
140
195
|
cause: error,
|
141
196
|
statusCode: response.status,
|
@@ -154,11 +209,11 @@ var getFromApi = async ({
|
|
154
209
|
});
|
155
210
|
} catch (error) {
|
156
211
|
if (error instanceof Error) {
|
157
|
-
if (isAbortError(error) ||
|
212
|
+
if (isAbortError(error) || APICallError2.isInstance(error)) {
|
158
213
|
throw error;
|
159
214
|
}
|
160
215
|
}
|
161
|
-
throw new
|
216
|
+
throw new APICallError2({
|
162
217
|
message: "Failed to process successful response",
|
163
218
|
cause: error,
|
164
219
|
statusCode: response.status,
|
@@ -168,22 +223,7 @@ var getFromApi = async ({
|
|
168
223
|
});
|
169
224
|
}
|
170
225
|
} catch (error) {
|
171
|
-
|
172
|
-
throw error;
|
173
|
-
}
|
174
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
175
|
-
const cause = error.cause;
|
176
|
-
if (cause != null) {
|
177
|
-
throw new APICallError({
|
178
|
-
message: `Cannot connect to API: ${cause.message}`,
|
179
|
-
cause,
|
180
|
-
url,
|
181
|
-
isRetryable: true,
|
182
|
-
requestBodyValues: {}
|
183
|
-
});
|
184
|
-
}
|
185
|
-
}
|
186
|
-
throw error;
|
226
|
+
throw handleFetchError({ error, url, requestBodyValues: {} });
|
187
227
|
}
|
188
228
|
};
|
189
229
|
|
@@ -407,7 +447,10 @@ async function safeValidateTypes({
|
|
407
447
|
}
|
408
448
|
|
409
449
|
// src/parse-json.ts
|
410
|
-
async function parseJSON({
|
450
|
+
async function parseJSON({
|
451
|
+
text,
|
452
|
+
schema
|
453
|
+
}) {
|
411
454
|
try {
|
412
455
|
const value = secureJsonParse(text);
|
413
456
|
if (schema == null) {
|
@@ -493,7 +536,7 @@ async function parseProviderOptions({
|
|
493
536
|
}
|
494
537
|
|
495
538
|
// src/post-to-api.ts
|
496
|
-
import { APICallError as
|
539
|
+
import { APICallError as APICallError3 } from "@ai-sdk/provider";
|
497
540
|
var getOriginalFetch2 = () => globalThis.fetch;
|
498
541
|
var postJsonToApi = async ({
|
499
542
|
url,
|
@@ -564,10 +607,10 @@ var postToApi = async ({
|
|
564
607
|
requestBodyValues: body.values
|
565
608
|
});
|
566
609
|
} catch (error) {
|
567
|
-
if (isAbortError(error) ||
|
610
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
568
611
|
throw error;
|
569
612
|
}
|
570
|
-
throw new
|
613
|
+
throw new APICallError3({
|
571
614
|
message: "Failed to process error response",
|
572
615
|
cause: error,
|
573
616
|
statusCode: response.status,
|
@@ -586,11 +629,11 @@ var postToApi = async ({
|
|
586
629
|
});
|
587
630
|
} catch (error) {
|
588
631
|
if (error instanceof Error) {
|
589
|
-
if (isAbortError(error) ||
|
632
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
590
633
|
throw error;
|
591
634
|
}
|
592
635
|
}
|
593
|
-
throw new
|
636
|
+
throw new APICallError3({
|
594
637
|
message: "Failed to process successful response",
|
595
638
|
cause: error,
|
596
639
|
statusCode: response.status,
|
@@ -600,23 +643,7 @@ var postToApi = async ({
|
|
600
643
|
});
|
601
644
|
}
|
602
645
|
} catch (error) {
|
603
|
-
|
604
|
-
throw error;
|
605
|
-
}
|
606
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
607
|
-
const cause = error.cause;
|
608
|
-
if (cause != null) {
|
609
|
-
throw new APICallError2({
|
610
|
-
message: `Cannot connect to API: ${cause.message}`,
|
611
|
-
cause,
|
612
|
-
url,
|
613
|
-
requestBodyValues: body.values,
|
614
|
-
isRetryable: true
|
615
|
-
// retry when network error
|
616
|
-
});
|
617
|
-
}
|
618
|
-
}
|
619
|
-
throw error;
|
646
|
+
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
620
647
|
}
|
621
648
|
};
|
622
649
|
|
@@ -624,6 +651,9 @@ var postToApi = async ({
|
|
624
651
|
function tool(tool2) {
|
625
652
|
return tool2;
|
626
653
|
}
|
654
|
+
function dynamicTool(tool2) {
|
655
|
+
return { ...tool2, type: "dynamic" };
|
656
|
+
}
|
627
657
|
|
628
658
|
// src/provider-defined-tool-factory.ts
|
629
659
|
function createProviderDefinedToolFactory({
|
@@ -690,7 +720,7 @@ async function resolve(value) {
|
|
690
720
|
}
|
691
721
|
|
692
722
|
// src/response-handler.ts
|
693
|
-
import { APICallError as
|
723
|
+
import { APICallError as APICallError4, EmptyResponseBodyError } from "@ai-sdk/provider";
|
694
724
|
var createJsonErrorResponseHandler = ({
|
695
725
|
errorSchema,
|
696
726
|
errorToMessage,
|
@@ -701,7 +731,7 @@ var createJsonErrorResponseHandler = ({
|
|
701
731
|
if (responseBody.trim() === "") {
|
702
732
|
return {
|
703
733
|
responseHeaders,
|
704
|
-
value: new
|
734
|
+
value: new APICallError4({
|
705
735
|
message: response.statusText,
|
706
736
|
url,
|
707
737
|
requestBodyValues,
|
@@ -719,7 +749,7 @@ var createJsonErrorResponseHandler = ({
|
|
719
749
|
});
|
720
750
|
return {
|
721
751
|
responseHeaders,
|
722
|
-
value: new
|
752
|
+
value: new APICallError4({
|
723
753
|
message: errorToMessage(parsedError),
|
724
754
|
url,
|
725
755
|
requestBodyValues,
|
@@ -733,7 +763,7 @@ var createJsonErrorResponseHandler = ({
|
|
733
763
|
} catch (parseError) {
|
734
764
|
return {
|
735
765
|
responseHeaders,
|
736
|
-
value: new
|
766
|
+
value: new APICallError4({
|
737
767
|
message: response.statusText,
|
738
768
|
url,
|
739
769
|
requestBodyValues,
|
@@ -793,7 +823,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
793
823
|
});
|
794
824
|
const responseHeaders = extractResponseHeaders(response);
|
795
825
|
if (!parsedResult.success) {
|
796
|
-
throw new
|
826
|
+
throw new APICallError4({
|
797
827
|
message: "Invalid JSON response",
|
798
828
|
cause: parsedResult.error,
|
799
829
|
statusCode: response.status,
|
@@ -812,7 +842,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
812
842
|
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
813
843
|
const responseHeaders = extractResponseHeaders(response);
|
814
844
|
if (!response.body) {
|
815
|
-
throw new
|
845
|
+
throw new APICallError4({
|
816
846
|
message: "Response body is empty",
|
817
847
|
url,
|
818
848
|
requestBodyValues,
|
@@ -828,7 +858,7 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
828
858
|
value: new Uint8Array(buffer)
|
829
859
|
};
|
830
860
|
} catch (error) {
|
831
|
-
throw new
|
861
|
+
throw new APICallError4({
|
832
862
|
message: "Failed to read response as array buffer",
|
833
863
|
url,
|
834
864
|
requestBodyValues,
|
@@ -844,7 +874,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
844
874
|
const responseBody = await response.text();
|
845
875
|
return {
|
846
876
|
responseHeaders,
|
847
|
-
value: new
|
877
|
+
value: new APICallError4({
|
848
878
|
message: response.statusText,
|
849
879
|
url,
|
850
880
|
requestBodyValues,
|
@@ -856,7 +886,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
856
886
|
};
|
857
887
|
|
858
888
|
// src/zod-schema.ts
|
859
|
-
import * as z4 from "zod/v4
|
889
|
+
import * as z4 from "zod/v4";
|
860
890
|
import zodToJsonSchema from "zod-to-json-schema";
|
861
891
|
function zod3Schema(zodSchema2, options) {
|
862
892
|
var _a;
|
@@ -868,8 +898,8 @@ function zod3Schema(zodSchema2, options) {
|
|
868
898
|
// note: openai mode breaks various gemini conversions
|
869
899
|
}),
|
870
900
|
{
|
871
|
-
validate: (value) => {
|
872
|
-
const result = zodSchema2.
|
901
|
+
validate: async (value) => {
|
902
|
+
const result = await zodSchema2.safeParseAsync(value);
|
873
903
|
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
874
904
|
}
|
875
905
|
}
|
@@ -884,8 +914,8 @@ function zod4Schema(zodSchema2, options) {
|
|
884
914
|
reused: useReferences ? "ref" : "inline"
|
885
915
|
});
|
886
916
|
return jsonSchema(z4JSONSchema, {
|
887
|
-
validate: (value) => {
|
888
|
-
const result = z4.
|
917
|
+
validate: async (value) => {
|
918
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
889
919
|
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
890
920
|
}
|
891
921
|
});
|
@@ -972,6 +1002,7 @@ export {
|
|
972
1002
|
createProviderDefinedToolFactoryWithOutputSchema,
|
973
1003
|
createStatusCodeErrorResponseHandler,
|
974
1004
|
delay,
|
1005
|
+
dynamicTool,
|
975
1006
|
extractResponseHeaders,
|
976
1007
|
generateId,
|
977
1008
|
getErrorMessage,
|