@ai-sdk/provider-utils 3.0.0-alpha.9 → 3.0.0-beta.2
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 +79 -0
- package/dist/index.d.mts +465 -100
- package/dist/index.d.ts +465 -100
- package/dist/index.js +154 -176
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +130 -151
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
package/dist/index.mjs
CHANGED
@@ -43,98 +43,6 @@ async function delay(delayInMs) {
|
|
43
43
|
return delayInMs == null ? Promise.resolve() : new Promise((resolve2) => setTimeout(resolve2, delayInMs));
|
44
44
|
}
|
45
45
|
|
46
|
-
// src/event-source-parser-stream.ts
|
47
|
-
function createEventSourceParserStream() {
|
48
|
-
let buffer = "";
|
49
|
-
let event = void 0;
|
50
|
-
let data = [];
|
51
|
-
let lastEventId = void 0;
|
52
|
-
let retry = void 0;
|
53
|
-
function parseLine(line, controller) {
|
54
|
-
if (line === "") {
|
55
|
-
dispatchEvent(controller);
|
56
|
-
return;
|
57
|
-
}
|
58
|
-
if (line.startsWith(":")) {
|
59
|
-
return;
|
60
|
-
}
|
61
|
-
const colonIndex = line.indexOf(":");
|
62
|
-
if (colonIndex === -1) {
|
63
|
-
handleField(line, "");
|
64
|
-
return;
|
65
|
-
}
|
66
|
-
const field = line.slice(0, colonIndex);
|
67
|
-
const valueStart = colonIndex + 1;
|
68
|
-
const value = valueStart < line.length && line[valueStart] === " " ? line.slice(valueStart + 1) : line.slice(valueStart);
|
69
|
-
handleField(field, value);
|
70
|
-
}
|
71
|
-
function dispatchEvent(controller) {
|
72
|
-
if (data.length > 0) {
|
73
|
-
controller.enqueue({
|
74
|
-
event,
|
75
|
-
data: data.join("\n"),
|
76
|
-
id: lastEventId,
|
77
|
-
retry
|
78
|
-
});
|
79
|
-
data = [];
|
80
|
-
event = void 0;
|
81
|
-
retry = void 0;
|
82
|
-
}
|
83
|
-
}
|
84
|
-
function handleField(field, value) {
|
85
|
-
switch (field) {
|
86
|
-
case "event":
|
87
|
-
event = value;
|
88
|
-
break;
|
89
|
-
case "data":
|
90
|
-
data.push(value);
|
91
|
-
break;
|
92
|
-
case "id":
|
93
|
-
lastEventId = value;
|
94
|
-
break;
|
95
|
-
case "retry":
|
96
|
-
const parsedRetry = parseInt(value, 10);
|
97
|
-
if (!isNaN(parsedRetry)) {
|
98
|
-
retry = parsedRetry;
|
99
|
-
}
|
100
|
-
break;
|
101
|
-
}
|
102
|
-
}
|
103
|
-
return new TransformStream({
|
104
|
-
transform(chunk, controller) {
|
105
|
-
const { lines, incompleteLine } = splitLines(buffer, chunk);
|
106
|
-
buffer = incompleteLine;
|
107
|
-
for (let i = 0; i < lines.length; i++) {
|
108
|
-
parseLine(lines[i], controller);
|
109
|
-
}
|
110
|
-
},
|
111
|
-
flush(controller) {
|
112
|
-
parseLine(buffer, controller);
|
113
|
-
dispatchEvent(controller);
|
114
|
-
}
|
115
|
-
});
|
116
|
-
}
|
117
|
-
function splitLines(buffer, chunk) {
|
118
|
-
const lines = [];
|
119
|
-
let currentLine = buffer;
|
120
|
-
for (let i = 0; i < chunk.length; ) {
|
121
|
-
const char = chunk[i++];
|
122
|
-
if (char === "\n") {
|
123
|
-
lines.push(currentLine);
|
124
|
-
currentLine = "";
|
125
|
-
} else if (char === "\r") {
|
126
|
-
lines.push(currentLine);
|
127
|
-
currentLine = "";
|
128
|
-
if (chunk[i] === "\n") {
|
129
|
-
i++;
|
130
|
-
}
|
131
|
-
} else {
|
132
|
-
currentLine += char;
|
133
|
-
}
|
134
|
-
}
|
135
|
-
return { lines, incompleteLine: currentLine };
|
136
|
-
}
|
137
|
-
|
138
46
|
// src/extract-response-headers.ts
|
139
47
|
function extractResponseHeaders(response) {
|
140
48
|
return Object.fromEntries([...response.headers]);
|
@@ -184,8 +92,42 @@ function getErrorMessage(error) {
|
|
184
92
|
}
|
185
93
|
|
186
94
|
// src/get-from-api.ts
|
95
|
+
import { APICallError as APICallError2 } from "@ai-sdk/provider";
|
96
|
+
|
97
|
+
// src/handle-fetch-error.ts
|
187
98
|
import { APICallError } from "@ai-sdk/provider";
|
188
99
|
|
100
|
+
// src/is-abort-error.ts
|
101
|
+
function isAbortError(error) {
|
102
|
+
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
103
|
+
}
|
104
|
+
|
105
|
+
// src/handle-fetch-error.ts
|
106
|
+
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
107
|
+
function handleFetchError({
|
108
|
+
error,
|
109
|
+
url,
|
110
|
+
requestBodyValues
|
111
|
+
}) {
|
112
|
+
if (isAbortError(error)) {
|
113
|
+
return error;
|
114
|
+
}
|
115
|
+
if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
|
116
|
+
const cause = error.cause;
|
117
|
+
if (cause != null) {
|
118
|
+
return new APICallError({
|
119
|
+
message: `Cannot connect to API: ${cause.message}`,
|
120
|
+
cause,
|
121
|
+
url,
|
122
|
+
requestBodyValues,
|
123
|
+
isRetryable: true
|
124
|
+
// retry when network error
|
125
|
+
});
|
126
|
+
}
|
127
|
+
}
|
128
|
+
return error;
|
129
|
+
}
|
130
|
+
|
189
131
|
// src/remove-undefined-entries.ts
|
190
132
|
function removeUndefinedEntries(record) {
|
191
133
|
return Object.fromEntries(
|
@@ -193,11 +135,6 @@ function removeUndefinedEntries(record) {
|
|
193
135
|
);
|
194
136
|
}
|
195
137
|
|
196
|
-
// src/is-abort-error.ts
|
197
|
-
function isAbortError(error) {
|
198
|
-
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
199
|
-
}
|
200
|
-
|
201
138
|
// src/get-from-api.ts
|
202
139
|
var getOriginalFetch = () => globalThis.fetch;
|
203
140
|
var getFromApi = async ({
|
@@ -224,10 +161,10 @@ var getFromApi = async ({
|
|
224
161
|
requestBodyValues: {}
|
225
162
|
});
|
226
163
|
} catch (error) {
|
227
|
-
if (isAbortError(error) ||
|
164
|
+
if (isAbortError(error) || APICallError2.isInstance(error)) {
|
228
165
|
throw error;
|
229
166
|
}
|
230
|
-
throw new
|
167
|
+
throw new APICallError2({
|
231
168
|
message: "Failed to process error response",
|
232
169
|
cause: error,
|
233
170
|
statusCode: response.status,
|
@@ -246,11 +183,11 @@ var getFromApi = async ({
|
|
246
183
|
});
|
247
184
|
} catch (error) {
|
248
185
|
if (error instanceof Error) {
|
249
|
-
if (isAbortError(error) ||
|
186
|
+
if (isAbortError(error) || APICallError2.isInstance(error)) {
|
250
187
|
throw error;
|
251
188
|
}
|
252
189
|
}
|
253
|
-
throw new
|
190
|
+
throw new APICallError2({
|
254
191
|
message: "Failed to process successful response",
|
255
192
|
cause: error,
|
256
193
|
statusCode: response.status,
|
@@ -260,22 +197,7 @@ var getFromApi = async ({
|
|
260
197
|
});
|
261
198
|
}
|
262
199
|
} catch (error) {
|
263
|
-
|
264
|
-
throw error;
|
265
|
-
}
|
266
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
267
|
-
const cause = error.cause;
|
268
|
-
if (cause != null) {
|
269
|
-
throw new APICallError({
|
270
|
-
message: `Cannot connect to API: ${cause.message}`,
|
271
|
-
cause,
|
272
|
-
url,
|
273
|
-
isRetryable: true,
|
274
|
-
requestBodyValues: {}
|
275
|
-
});
|
276
|
-
}
|
277
|
-
}
|
278
|
-
throw error;
|
200
|
+
throw handleFetchError({ error, url, requestBodyValues: {} });
|
279
201
|
}
|
280
202
|
};
|
281
203
|
|
@@ -499,7 +421,10 @@ async function safeValidateTypes({
|
|
499
421
|
}
|
500
422
|
|
501
423
|
// src/parse-json.ts
|
502
|
-
async function parseJSON({
|
424
|
+
async function parseJSON({
|
425
|
+
text,
|
426
|
+
schema
|
427
|
+
}) {
|
503
428
|
try {
|
504
429
|
const value = secureJsonParse(text);
|
505
430
|
if (schema == null) {
|
@@ -541,11 +466,14 @@ function isParsableJson(input) {
|
|
541
466
|
}
|
542
467
|
|
543
468
|
// src/parse-json-event-stream.ts
|
469
|
+
import {
|
470
|
+
EventSourceParserStream
|
471
|
+
} from "eventsource-parser/stream";
|
544
472
|
function parseJsonEventStream({
|
545
473
|
stream,
|
546
474
|
schema
|
547
475
|
}) {
|
548
|
-
return stream.pipeThrough(new TextDecoderStream()).pipeThrough(
|
476
|
+
return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(
|
549
477
|
new TransformStream({
|
550
478
|
async transform({ data }, controller) {
|
551
479
|
if (data === "[DONE]") {
|
@@ -582,7 +510,7 @@ async function parseProviderOptions({
|
|
582
510
|
}
|
583
511
|
|
584
512
|
// src/post-to-api.ts
|
585
|
-
import { APICallError as
|
513
|
+
import { APICallError as APICallError3 } from "@ai-sdk/provider";
|
586
514
|
var getOriginalFetch2 = () => globalThis.fetch;
|
587
515
|
var postJsonToApi = async ({
|
588
516
|
url,
|
@@ -653,10 +581,10 @@ var postToApi = async ({
|
|
653
581
|
requestBodyValues: body.values
|
654
582
|
});
|
655
583
|
} catch (error) {
|
656
|
-
if (isAbortError(error) ||
|
584
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
657
585
|
throw error;
|
658
586
|
}
|
659
|
-
throw new
|
587
|
+
throw new APICallError3({
|
660
588
|
message: "Failed to process error response",
|
661
589
|
cause: error,
|
662
590
|
statusCode: response.status,
|
@@ -675,11 +603,11 @@ var postToApi = async ({
|
|
675
603
|
});
|
676
604
|
} catch (error) {
|
677
605
|
if (error instanceof Error) {
|
678
|
-
if (isAbortError(error) ||
|
606
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
679
607
|
throw error;
|
680
608
|
}
|
681
609
|
}
|
682
|
-
throw new
|
610
|
+
throw new APICallError3({
|
683
611
|
message: "Failed to process successful response",
|
684
612
|
cause: error,
|
685
613
|
statusCode: response.status,
|
@@ -689,26 +617,71 @@ var postToApi = async ({
|
|
689
617
|
});
|
690
618
|
}
|
691
619
|
} catch (error) {
|
692
|
-
|
693
|
-
throw error;
|
694
|
-
}
|
695
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
696
|
-
const cause = error.cause;
|
697
|
-
if (cause != null) {
|
698
|
-
throw new APICallError2({
|
699
|
-
message: `Cannot connect to API: ${cause.message}`,
|
700
|
-
cause,
|
701
|
-
url,
|
702
|
-
requestBodyValues: body.values,
|
703
|
-
isRetryable: true
|
704
|
-
// retry when network error
|
705
|
-
});
|
706
|
-
}
|
707
|
-
}
|
708
|
-
throw error;
|
620
|
+
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
709
621
|
}
|
710
622
|
};
|
711
623
|
|
624
|
+
// src/types/tool.ts
|
625
|
+
function tool(tool2) {
|
626
|
+
return tool2;
|
627
|
+
}
|
628
|
+
|
629
|
+
// src/provider-defined-tool-factory.ts
|
630
|
+
function createProviderDefinedToolFactory({
|
631
|
+
id,
|
632
|
+
name,
|
633
|
+
inputSchema
|
634
|
+
}) {
|
635
|
+
return ({
|
636
|
+
execute,
|
637
|
+
outputSchema,
|
638
|
+
toModelOutput,
|
639
|
+
onInputStart,
|
640
|
+
onInputDelta,
|
641
|
+
onInputAvailable,
|
642
|
+
...args
|
643
|
+
}) => tool({
|
644
|
+
type: "provider-defined",
|
645
|
+
id,
|
646
|
+
name,
|
647
|
+
args,
|
648
|
+
inputSchema,
|
649
|
+
outputSchema,
|
650
|
+
execute,
|
651
|
+
toModelOutput,
|
652
|
+
onInputStart,
|
653
|
+
onInputDelta,
|
654
|
+
onInputAvailable
|
655
|
+
});
|
656
|
+
}
|
657
|
+
function createProviderDefinedToolFactoryWithOutputSchema({
|
658
|
+
id,
|
659
|
+
name,
|
660
|
+
inputSchema,
|
661
|
+
outputSchema
|
662
|
+
}) {
|
663
|
+
return ({
|
664
|
+
execute,
|
665
|
+
toModelOutput,
|
666
|
+
onInputStart,
|
667
|
+
onInputDelta,
|
668
|
+
onInputAvailable,
|
669
|
+
...args
|
670
|
+
}) => tool({
|
671
|
+
type: "provider-defined",
|
672
|
+
id,
|
673
|
+
name,
|
674
|
+
args,
|
675
|
+
inputSchema,
|
676
|
+
outputSchema,
|
677
|
+
execute,
|
678
|
+
toModelOutput,
|
679
|
+
onInputStart,
|
680
|
+
onInputDelta,
|
681
|
+
onInputAvailable
|
682
|
+
});
|
683
|
+
}
|
684
|
+
|
712
685
|
// src/resolve.ts
|
713
686
|
async function resolve(value) {
|
714
687
|
if (typeof value === "function") {
|
@@ -718,7 +691,7 @@ async function resolve(value) {
|
|
718
691
|
}
|
719
692
|
|
720
693
|
// src/response-handler.ts
|
721
|
-
import { APICallError as
|
694
|
+
import { APICallError as APICallError4, EmptyResponseBodyError } from "@ai-sdk/provider";
|
722
695
|
var createJsonErrorResponseHandler = ({
|
723
696
|
errorSchema,
|
724
697
|
errorToMessage,
|
@@ -729,7 +702,7 @@ var createJsonErrorResponseHandler = ({
|
|
729
702
|
if (responseBody.trim() === "") {
|
730
703
|
return {
|
731
704
|
responseHeaders,
|
732
|
-
value: new
|
705
|
+
value: new APICallError4({
|
733
706
|
message: response.statusText,
|
734
707
|
url,
|
735
708
|
requestBodyValues,
|
@@ -747,7 +720,7 @@ var createJsonErrorResponseHandler = ({
|
|
747
720
|
});
|
748
721
|
return {
|
749
722
|
responseHeaders,
|
750
|
-
value: new
|
723
|
+
value: new APICallError4({
|
751
724
|
message: errorToMessage(parsedError),
|
752
725
|
url,
|
753
726
|
requestBodyValues,
|
@@ -761,7 +734,7 @@ var createJsonErrorResponseHandler = ({
|
|
761
734
|
} catch (parseError) {
|
762
735
|
return {
|
763
736
|
responseHeaders,
|
764
|
-
value: new
|
737
|
+
value: new APICallError4({
|
765
738
|
message: response.statusText,
|
766
739
|
url,
|
767
740
|
requestBodyValues,
|
@@ -821,7 +794,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
821
794
|
});
|
822
795
|
const responseHeaders = extractResponseHeaders(response);
|
823
796
|
if (!parsedResult.success) {
|
824
|
-
throw new
|
797
|
+
throw new APICallError4({
|
825
798
|
message: "Invalid JSON response",
|
826
799
|
cause: parsedResult.error,
|
827
800
|
statusCode: response.status,
|
@@ -840,7 +813,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
840
813
|
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
841
814
|
const responseHeaders = extractResponseHeaders(response);
|
842
815
|
if (!response.body) {
|
843
|
-
throw new
|
816
|
+
throw new APICallError4({
|
844
817
|
message: "Response body is empty",
|
845
818
|
url,
|
846
819
|
requestBodyValues,
|
@@ -856,7 +829,7 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
856
829
|
value: new Uint8Array(buffer)
|
857
830
|
};
|
858
831
|
} catch (error) {
|
859
|
-
throw new
|
832
|
+
throw new APICallError4({
|
860
833
|
message: "Failed to read response as array buffer",
|
861
834
|
url,
|
862
835
|
requestBodyValues,
|
@@ -872,7 +845,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
872
845
|
const responseBody = await response.text();
|
873
846
|
return {
|
874
847
|
responseHeaders,
|
875
|
-
value: new
|
848
|
+
value: new APICallError4({
|
876
849
|
message: response.statusText,
|
877
850
|
url,
|
878
851
|
requestBodyValues,
|
@@ -884,7 +857,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
884
857
|
};
|
885
858
|
|
886
859
|
// src/zod-schema.ts
|
887
|
-
import * as z4 from "zod/v4
|
860
|
+
import * as z4 from "zod/v4";
|
888
861
|
import zodToJsonSchema from "zod-to-json-schema";
|
889
862
|
function zod3Schema(zodSchema2, options) {
|
890
863
|
var _a;
|
@@ -978,7 +951,11 @@ function withoutTrailingSlash(url) {
|
|
978
951
|
|
979
952
|
// src/index.ts
|
980
953
|
export * from "@standard-schema/spec";
|
954
|
+
import {
|
955
|
+
EventSourceParserStream as EventSourceParserStream2
|
956
|
+
} from "eventsource-parser/stream";
|
981
957
|
export {
|
958
|
+
EventSourceParserStream2 as EventSourceParserStream,
|
982
959
|
asSchema,
|
983
960
|
asValidator,
|
984
961
|
combineHeaders,
|
@@ -987,12 +964,13 @@ export {
|
|
987
964
|
convertToBase64,
|
988
965
|
convertUint8ArrayToBase64,
|
989
966
|
createBinaryResponseHandler,
|
990
|
-
createEventSourceParserStream,
|
991
967
|
createEventSourceResponseHandler,
|
992
968
|
createIdGenerator,
|
993
969
|
createJsonErrorResponseHandler,
|
994
970
|
createJsonResponseHandler,
|
995
971
|
createJsonStreamResponseHandler,
|
972
|
+
createProviderDefinedToolFactory,
|
973
|
+
createProviderDefinedToolFactoryWithOutputSchema,
|
996
974
|
createStatusCodeErrorResponseHandler,
|
997
975
|
delay,
|
998
976
|
extractResponseHeaders,
|
@@ -1018,6 +996,7 @@ export {
|
|
1018
996
|
safeParseJSON,
|
1019
997
|
safeValidateTypes,
|
1020
998
|
standardSchemaValidator,
|
999
|
+
tool,
|
1021
1000
|
validateTypes,
|
1022
1001
|
validator,
|
1023
1002
|
validatorSymbol,
|