@ai-sdk/provider-utils 3.0.0-canary.9 → 3.0.0
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 +398 -0
- package/dist/index.d.mts +521 -54
- package/dist/index.d.ts +521 -54
- package/dist/index.js +333 -230
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +300 -213
- package/dist/index.mjs.map +1 -1
- package/dist/test/index.d.mts +1 -1
- package/dist/test/index.d.ts +1 -1
- package/dist/test/index.js +8 -2
- package/dist/test/index.js.map +1 -1
- package/dist/test/index.mjs +8 -2
- package/dist/test/index.mjs.map +1 -1
- package/package.json +10 -6
- package/test.d.ts +1 -0
package/dist/index.mjs
CHANGED
@@ -39,134 +39,56 @@ function convertAsyncIteratorToReadableStream(iterator) {
|
|
39
39
|
}
|
40
40
|
|
41
41
|
// src/delay.ts
|
42
|
-
async function delay(delayInMs) {
|
43
|
-
|
44
|
-
|
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
|
-
}
|
42
|
+
async function delay(delayInMs, options) {
|
43
|
+
if (delayInMs == null) {
|
44
|
+
return Promise.resolve();
|
102
45
|
}
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
parseLine(lines[i], controller);
|
109
|
-
}
|
110
|
-
},
|
111
|
-
flush(controller) {
|
112
|
-
parseLine(buffer, controller);
|
113
|
-
dispatchEvent(controller);
|
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;
|
114
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);
|
115
65
|
});
|
116
66
|
}
|
117
|
-
function
|
118
|
-
|
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 + 1] === "\n") {
|
129
|
-
i++;
|
130
|
-
}
|
131
|
-
} else {
|
132
|
-
currentLine += char;
|
133
|
-
}
|
134
|
-
}
|
135
|
-
return { lines, incompleteLine: currentLine };
|
67
|
+
function createAbortError() {
|
68
|
+
return new DOMException("Delay was aborted", "AbortError");
|
136
69
|
}
|
137
70
|
|
138
71
|
// src/extract-response-headers.ts
|
139
72
|
function extractResponseHeaders(response) {
|
140
|
-
|
141
|
-
response.headers.forEach((value, key) => {
|
142
|
-
headers[key] = value;
|
143
|
-
});
|
144
|
-
return headers;
|
73
|
+
return Object.fromEntries([...response.headers]);
|
145
74
|
}
|
146
75
|
|
147
76
|
// src/generate-id.ts
|
148
77
|
import { InvalidArgumentError } from "@ai-sdk/provider";
|
149
|
-
|
150
|
-
// src/generate-id-custom-alphabet.ts
|
151
|
-
function customAlphabet(alphabet, defaultSize) {
|
152
|
-
return (size = defaultSize) => {
|
153
|
-
let id = "";
|
154
|
-
let i = size | 0;
|
155
|
-
while (i--) {
|
156
|
-
id += alphabet[Math.random() * alphabet.length | 0];
|
157
|
-
}
|
158
|
-
return id;
|
159
|
-
};
|
160
|
-
}
|
161
|
-
|
162
|
-
// src/generate-id.ts
|
163
78
|
var createIdGenerator = ({
|
164
79
|
prefix,
|
165
|
-
size
|
80
|
+
size = 16,
|
166
81
|
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
167
82
|
separator = "-"
|
168
83
|
} = {}) => {
|
169
|
-
const generator =
|
84
|
+
const generator = () => {
|
85
|
+
const alphabetLength = alphabet.length;
|
86
|
+
const chars = new Array(size);
|
87
|
+
for (let i = 0; i < size; i++) {
|
88
|
+
chars[i] = alphabet[Math.random() * alphabetLength | 0];
|
89
|
+
}
|
90
|
+
return chars.join("");
|
91
|
+
};
|
170
92
|
if (prefix == null) {
|
171
93
|
return generator;
|
172
94
|
}
|
@@ -176,7 +98,7 @@ var createIdGenerator = ({
|
|
176
98
|
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
177
99
|
});
|
178
100
|
}
|
179
|
-
return (
|
101
|
+
return () => `${prefix}${separator}${generator()}`;
|
180
102
|
};
|
181
103
|
var generateId = createIdGenerator();
|
182
104
|
|
@@ -195,8 +117,43 @@ function getErrorMessage(error) {
|
|
195
117
|
}
|
196
118
|
|
197
119
|
// src/get-from-api.ts
|
120
|
+
import { APICallError as APICallError2 } from "@ai-sdk/provider";
|
121
|
+
|
122
|
+
// src/handle-fetch-error.ts
|
198
123
|
import { APICallError } from "@ai-sdk/provider";
|
199
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
|
+
|
200
157
|
// src/remove-undefined-entries.ts
|
201
158
|
function removeUndefinedEntries(record) {
|
202
159
|
return Object.fromEntries(
|
@@ -204,11 +161,6 @@ function removeUndefinedEntries(record) {
|
|
204
161
|
);
|
205
162
|
}
|
206
163
|
|
207
|
-
// src/is-abort-error.ts
|
208
|
-
function isAbortError(error) {
|
209
|
-
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
210
|
-
}
|
211
|
-
|
212
164
|
// src/get-from-api.ts
|
213
165
|
var getOriginalFetch = () => globalThis.fetch;
|
214
166
|
var getFromApi = async ({
|
@@ -235,10 +187,10 @@ var getFromApi = async ({
|
|
235
187
|
requestBodyValues: {}
|
236
188
|
});
|
237
189
|
} catch (error) {
|
238
|
-
if (isAbortError(error) ||
|
190
|
+
if (isAbortError(error) || APICallError2.isInstance(error)) {
|
239
191
|
throw error;
|
240
192
|
}
|
241
|
-
throw new
|
193
|
+
throw new APICallError2({
|
242
194
|
message: "Failed to process error response",
|
243
195
|
cause: error,
|
244
196
|
statusCode: response.status,
|
@@ -257,11 +209,11 @@ var getFromApi = async ({
|
|
257
209
|
});
|
258
210
|
} catch (error) {
|
259
211
|
if (error instanceof Error) {
|
260
|
-
if (isAbortError(error) ||
|
212
|
+
if (isAbortError(error) || APICallError2.isInstance(error)) {
|
261
213
|
throw error;
|
262
214
|
}
|
263
215
|
}
|
264
|
-
throw new
|
216
|
+
throw new APICallError2({
|
265
217
|
message: "Failed to process successful response",
|
266
218
|
cause: error,
|
267
219
|
statusCode: response.status,
|
@@ -271,22 +223,7 @@ var getFromApi = async ({
|
|
271
223
|
});
|
272
224
|
}
|
273
225
|
} catch (error) {
|
274
|
-
|
275
|
-
throw error;
|
276
|
-
}
|
277
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
278
|
-
const cause = error.cause;
|
279
|
-
if (cause != null) {
|
280
|
-
throw new APICallError({
|
281
|
-
message: `Cannot connect to API: ${cause.message}`,
|
282
|
-
cause,
|
283
|
-
url,
|
284
|
-
isRetryable: true,
|
285
|
-
requestBodyValues: {}
|
286
|
-
});
|
287
|
-
}
|
288
|
-
}
|
289
|
-
throw error;
|
226
|
+
throw handleFetchError({ error, url, requestBodyValues: {} });
|
290
227
|
}
|
291
228
|
};
|
292
229
|
|
@@ -395,7 +332,7 @@ function loadSetting({
|
|
395
332
|
// src/parse-json.ts
|
396
333
|
import {
|
397
334
|
JSONParseError,
|
398
|
-
TypeValidationError as
|
335
|
+
TypeValidationError as TypeValidationError3
|
399
336
|
} from "@ai-sdk/provider";
|
400
337
|
|
401
338
|
// src/secure-json-parse.ts
|
@@ -444,9 +381,10 @@ function secureJsonParse(text) {
|
|
444
381
|
}
|
445
382
|
|
446
383
|
// src/validate-types.ts
|
447
|
-
import { TypeValidationError } from "@ai-sdk/provider";
|
384
|
+
import { TypeValidationError as TypeValidationError2 } from "@ai-sdk/provider";
|
448
385
|
|
449
386
|
// src/validator.ts
|
387
|
+
import { TypeValidationError } from "@ai-sdk/provider";
|
450
388
|
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
451
389
|
function validator(validate) {
|
452
390
|
return { [validatorSymbol]: true, validate };
|
@@ -455,53 +393,61 @@ function isValidator(value) {
|
|
455
393
|
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
456
394
|
}
|
457
395
|
function asValidator(value) {
|
458
|
-
return isValidator(value) ? value :
|
396
|
+
return isValidator(value) ? value : standardSchemaValidator(value);
|
459
397
|
}
|
460
|
-
function
|
461
|
-
return validator((value) => {
|
462
|
-
const result =
|
463
|
-
return result.
|
398
|
+
function standardSchemaValidator(standardSchema) {
|
399
|
+
return validator(async (value) => {
|
400
|
+
const result = await standardSchema["~standard"].validate(value);
|
401
|
+
return result.issues == null ? { success: true, value: result.value } : {
|
402
|
+
success: false,
|
403
|
+
error: new TypeValidationError({
|
404
|
+
value,
|
405
|
+
cause: result.issues
|
406
|
+
})
|
407
|
+
};
|
464
408
|
});
|
465
409
|
}
|
466
410
|
|
467
411
|
// src/validate-types.ts
|
468
|
-
function validateTypes({
|
412
|
+
async function validateTypes({
|
469
413
|
value,
|
470
|
-
schema
|
414
|
+
schema
|
471
415
|
}) {
|
472
|
-
const result = safeValidateTypes({ value, schema
|
416
|
+
const result = await safeValidateTypes({ value, schema });
|
473
417
|
if (!result.success) {
|
474
|
-
throw
|
418
|
+
throw TypeValidationError2.wrap({ value, cause: result.error });
|
475
419
|
}
|
476
420
|
return result.value;
|
477
421
|
}
|
478
|
-
function safeValidateTypes({
|
422
|
+
async function safeValidateTypes({
|
479
423
|
value,
|
480
424
|
schema
|
481
425
|
}) {
|
482
426
|
const validator2 = asValidator(schema);
|
483
427
|
try {
|
484
428
|
if (validator2.validate == null) {
|
485
|
-
return { success: true, value };
|
429
|
+
return { success: true, value, rawValue: value };
|
486
430
|
}
|
487
|
-
const result = validator2.validate(value);
|
431
|
+
const result = await validator2.validate(value);
|
488
432
|
if (result.success) {
|
489
|
-
return result;
|
433
|
+
return { success: true, value: result.value, rawValue: value };
|
490
434
|
}
|
491
435
|
return {
|
492
436
|
success: false,
|
493
|
-
error:
|
437
|
+
error: TypeValidationError2.wrap({ value, cause: result.error }),
|
438
|
+
rawValue: value
|
494
439
|
};
|
495
440
|
} catch (error) {
|
496
441
|
return {
|
497
442
|
success: false,
|
498
|
-
error:
|
443
|
+
error: TypeValidationError2.wrap({ value, cause: error }),
|
444
|
+
rawValue: value
|
499
445
|
};
|
500
446
|
}
|
501
447
|
}
|
502
448
|
|
503
449
|
// src/parse-json.ts
|
504
|
-
function parseJSON({
|
450
|
+
async function parseJSON({
|
505
451
|
text,
|
506
452
|
schema
|
507
453
|
}) {
|
@@ -512,13 +458,13 @@ function parseJSON({
|
|
512
458
|
}
|
513
459
|
return validateTypes({ value, schema });
|
514
460
|
} catch (error) {
|
515
|
-
if (JSONParseError.isInstance(error) ||
|
461
|
+
if (JSONParseError.isInstance(error) || TypeValidationError3.isInstance(error)) {
|
516
462
|
throw error;
|
517
463
|
}
|
518
464
|
throw new JSONParseError({ text, cause: error });
|
519
465
|
}
|
520
466
|
}
|
521
|
-
function safeParseJSON({
|
467
|
+
async function safeParseJSON({
|
522
468
|
text,
|
523
469
|
schema
|
524
470
|
}) {
|
@@ -527,12 +473,12 @@ function safeParseJSON({
|
|
527
473
|
if (schema == null) {
|
528
474
|
return { success: true, value, rawValue: value };
|
529
475
|
}
|
530
|
-
|
531
|
-
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
476
|
+
return await safeValidateTypes({ value, schema });
|
532
477
|
} catch (error) {
|
533
478
|
return {
|
534
479
|
success: false,
|
535
|
-
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error })
|
480
|
+
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error }),
|
481
|
+
rawValue: void 0
|
536
482
|
};
|
537
483
|
}
|
538
484
|
}
|
@@ -545,9 +491,29 @@ function isParsableJson(input) {
|
|
545
491
|
}
|
546
492
|
}
|
547
493
|
|
494
|
+
// src/parse-json-event-stream.ts
|
495
|
+
import {
|
496
|
+
EventSourceParserStream
|
497
|
+
} from "eventsource-parser/stream";
|
498
|
+
function parseJsonEventStream({
|
499
|
+
stream,
|
500
|
+
schema
|
501
|
+
}) {
|
502
|
+
return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(
|
503
|
+
new TransformStream({
|
504
|
+
async transform({ data }, controller) {
|
505
|
+
if (data === "[DONE]") {
|
506
|
+
return;
|
507
|
+
}
|
508
|
+
controller.enqueue(await safeParseJSON({ text: data, schema }));
|
509
|
+
}
|
510
|
+
})
|
511
|
+
);
|
512
|
+
}
|
513
|
+
|
548
514
|
// src/parse-provider-options.ts
|
549
515
|
import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
|
550
|
-
function parseProviderOptions({
|
516
|
+
async function parseProviderOptions({
|
551
517
|
provider,
|
552
518
|
providerOptions,
|
553
519
|
schema
|
@@ -555,7 +521,7 @@ function parseProviderOptions({
|
|
555
521
|
if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
|
556
522
|
return void 0;
|
557
523
|
}
|
558
|
-
const parsedProviderOptions = safeValidateTypes({
|
524
|
+
const parsedProviderOptions = await safeValidateTypes({
|
559
525
|
value: providerOptions[provider],
|
560
526
|
schema
|
561
527
|
});
|
@@ -570,7 +536,7 @@ function parseProviderOptions({
|
|
570
536
|
}
|
571
537
|
|
572
538
|
// src/post-to-api.ts
|
573
|
-
import { APICallError as
|
539
|
+
import { APICallError as APICallError3 } from "@ai-sdk/provider";
|
574
540
|
var getOriginalFetch2 = () => globalThis.fetch;
|
575
541
|
var postJsonToApi = async ({
|
576
542
|
url,
|
@@ -641,10 +607,10 @@ var postToApi = async ({
|
|
641
607
|
requestBodyValues: body.values
|
642
608
|
});
|
643
609
|
} catch (error) {
|
644
|
-
if (isAbortError(error) ||
|
610
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
645
611
|
throw error;
|
646
612
|
}
|
647
|
-
throw new
|
613
|
+
throw new APICallError3({
|
648
614
|
message: "Failed to process error response",
|
649
615
|
cause: error,
|
650
616
|
statusCode: response.status,
|
@@ -663,11 +629,11 @@ var postToApi = async ({
|
|
663
629
|
});
|
664
630
|
} catch (error) {
|
665
631
|
if (error instanceof Error) {
|
666
|
-
if (isAbortError(error) ||
|
632
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
667
633
|
throw error;
|
668
634
|
}
|
669
635
|
}
|
670
|
-
throw new
|
636
|
+
throw new APICallError3({
|
671
637
|
message: "Failed to process successful response",
|
672
638
|
cause: error,
|
673
639
|
statusCode: response.status,
|
@@ -677,26 +643,74 @@ var postToApi = async ({
|
|
677
643
|
});
|
678
644
|
}
|
679
645
|
} catch (error) {
|
680
|
-
|
681
|
-
throw error;
|
682
|
-
}
|
683
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
684
|
-
const cause = error.cause;
|
685
|
-
if (cause != null) {
|
686
|
-
throw new APICallError2({
|
687
|
-
message: `Cannot connect to API: ${cause.message}`,
|
688
|
-
cause,
|
689
|
-
url,
|
690
|
-
requestBodyValues: body.values,
|
691
|
-
isRetryable: true
|
692
|
-
// retry when network error
|
693
|
-
});
|
694
|
-
}
|
695
|
-
}
|
696
|
-
throw error;
|
646
|
+
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
697
647
|
}
|
698
648
|
};
|
699
649
|
|
650
|
+
// src/types/tool.ts
|
651
|
+
function tool(tool2) {
|
652
|
+
return tool2;
|
653
|
+
}
|
654
|
+
function dynamicTool(tool2) {
|
655
|
+
return { ...tool2, type: "dynamic" };
|
656
|
+
}
|
657
|
+
|
658
|
+
// src/provider-defined-tool-factory.ts
|
659
|
+
function createProviderDefinedToolFactory({
|
660
|
+
id,
|
661
|
+
name,
|
662
|
+
inputSchema
|
663
|
+
}) {
|
664
|
+
return ({
|
665
|
+
execute,
|
666
|
+
outputSchema,
|
667
|
+
toModelOutput,
|
668
|
+
onInputStart,
|
669
|
+
onInputDelta,
|
670
|
+
onInputAvailable,
|
671
|
+
...args
|
672
|
+
}) => tool({
|
673
|
+
type: "provider-defined",
|
674
|
+
id,
|
675
|
+
name,
|
676
|
+
args,
|
677
|
+
inputSchema,
|
678
|
+
outputSchema,
|
679
|
+
execute,
|
680
|
+
toModelOutput,
|
681
|
+
onInputStart,
|
682
|
+
onInputDelta,
|
683
|
+
onInputAvailable
|
684
|
+
});
|
685
|
+
}
|
686
|
+
function createProviderDefinedToolFactoryWithOutputSchema({
|
687
|
+
id,
|
688
|
+
name,
|
689
|
+
inputSchema,
|
690
|
+
outputSchema
|
691
|
+
}) {
|
692
|
+
return ({
|
693
|
+
execute,
|
694
|
+
toModelOutput,
|
695
|
+
onInputStart,
|
696
|
+
onInputDelta,
|
697
|
+
onInputAvailable,
|
698
|
+
...args
|
699
|
+
}) => tool({
|
700
|
+
type: "provider-defined",
|
701
|
+
id,
|
702
|
+
name,
|
703
|
+
args,
|
704
|
+
inputSchema,
|
705
|
+
outputSchema,
|
706
|
+
execute,
|
707
|
+
toModelOutput,
|
708
|
+
onInputStart,
|
709
|
+
onInputDelta,
|
710
|
+
onInputAvailable
|
711
|
+
});
|
712
|
+
}
|
713
|
+
|
700
714
|
// src/resolve.ts
|
701
715
|
async function resolve(value) {
|
702
716
|
if (typeof value === "function") {
|
@@ -706,7 +720,7 @@ async function resolve(value) {
|
|
706
720
|
}
|
707
721
|
|
708
722
|
// src/response-handler.ts
|
709
|
-
import { APICallError as
|
723
|
+
import { APICallError as APICallError4, EmptyResponseBodyError } from "@ai-sdk/provider";
|
710
724
|
var createJsonErrorResponseHandler = ({
|
711
725
|
errorSchema,
|
712
726
|
errorToMessage,
|
@@ -717,7 +731,7 @@ var createJsonErrorResponseHandler = ({
|
|
717
731
|
if (responseBody.trim() === "") {
|
718
732
|
return {
|
719
733
|
responseHeaders,
|
720
|
-
value: new
|
734
|
+
value: new APICallError4({
|
721
735
|
message: response.statusText,
|
722
736
|
url,
|
723
737
|
requestBodyValues,
|
@@ -729,13 +743,13 @@ var createJsonErrorResponseHandler = ({
|
|
729
743
|
};
|
730
744
|
}
|
731
745
|
try {
|
732
|
-
const parsedError = parseJSON({
|
746
|
+
const parsedError = await parseJSON({
|
733
747
|
text: responseBody,
|
734
748
|
schema: errorSchema
|
735
749
|
});
|
736
750
|
return {
|
737
751
|
responseHeaders,
|
738
|
-
value: new
|
752
|
+
value: new APICallError4({
|
739
753
|
message: errorToMessage(parsedError),
|
740
754
|
url,
|
741
755
|
requestBodyValues,
|
@@ -749,7 +763,7 @@ var createJsonErrorResponseHandler = ({
|
|
749
763
|
} catch (parseError) {
|
750
764
|
return {
|
751
765
|
responseHeaders,
|
752
|
-
value: new
|
766
|
+
value: new APICallError4({
|
753
767
|
message: response.statusText,
|
754
768
|
url,
|
755
769
|
requestBodyValues,
|
@@ -768,21 +782,10 @@ var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) =>
|
|
768
782
|
}
|
769
783
|
return {
|
770
784
|
responseHeaders,
|
771
|
-
value:
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
return;
|
776
|
-
}
|
777
|
-
controller.enqueue(
|
778
|
-
safeParseJSON({
|
779
|
-
text: data,
|
780
|
-
schema: chunkSchema
|
781
|
-
})
|
782
|
-
);
|
783
|
-
}
|
784
|
-
})
|
785
|
-
)
|
785
|
+
value: parseJsonEventStream({
|
786
|
+
stream: response.body,
|
787
|
+
schema: chunkSchema
|
788
|
+
})
|
786
789
|
};
|
787
790
|
};
|
788
791
|
var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
@@ -795,10 +798,10 @@ var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
795
798
|
responseHeaders,
|
796
799
|
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(
|
797
800
|
new TransformStream({
|
798
|
-
transform(chunkText, controller) {
|
801
|
+
async transform(chunkText, controller) {
|
799
802
|
if (chunkText.endsWith("\n")) {
|
800
803
|
controller.enqueue(
|
801
|
-
safeParseJSON({
|
804
|
+
await safeParseJSON({
|
802
805
|
text: buffer + chunkText,
|
803
806
|
schema: chunkSchema
|
804
807
|
})
|
@@ -814,13 +817,13 @@ var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
814
817
|
};
|
815
818
|
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
816
819
|
const responseBody = await response.text();
|
817
|
-
const parsedResult = safeParseJSON({
|
820
|
+
const parsedResult = await safeParseJSON({
|
818
821
|
text: responseBody,
|
819
822
|
schema: responseSchema
|
820
823
|
});
|
821
824
|
const responseHeaders = extractResponseHeaders(response);
|
822
825
|
if (!parsedResult.success) {
|
823
|
-
throw new
|
826
|
+
throw new APICallError4({
|
824
827
|
message: "Invalid JSON response",
|
825
828
|
cause: parsedResult.error,
|
826
829
|
statusCode: response.status,
|
@@ -839,7 +842,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
839
842
|
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
840
843
|
const responseHeaders = extractResponseHeaders(response);
|
841
844
|
if (!response.body) {
|
842
|
-
throw new
|
845
|
+
throw new APICallError4({
|
843
846
|
message: "Response body is empty",
|
844
847
|
url,
|
845
848
|
requestBodyValues,
|
@@ -855,7 +858,7 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
855
858
|
value: new Uint8Array(buffer)
|
856
859
|
};
|
857
860
|
} catch (error) {
|
858
|
-
throw new
|
861
|
+
throw new APICallError4({
|
859
862
|
message: "Failed to read response as array buffer",
|
860
863
|
url,
|
861
864
|
requestBodyValues,
|
@@ -871,7 +874,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
871
874
|
const responseBody = await response.text();
|
872
875
|
return {
|
873
876
|
responseHeaders,
|
874
|
-
value: new
|
877
|
+
value: new APICallError4({
|
875
878
|
message: response.statusText,
|
876
879
|
url,
|
877
880
|
requestBodyValues,
|
@@ -882,6 +885,76 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
882
885
|
};
|
883
886
|
};
|
884
887
|
|
888
|
+
// src/zod-schema.ts
|
889
|
+
import * as z4 from "zod/v4";
|
890
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
891
|
+
function zod3Schema(zodSchema2, options) {
|
892
|
+
var _a;
|
893
|
+
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
894
|
+
return jsonSchema(
|
895
|
+
zodToJsonSchema(zodSchema2, {
|
896
|
+
$refStrategy: useReferences ? "root" : "none",
|
897
|
+
target: "jsonSchema7"
|
898
|
+
// note: openai mode breaks various gemini conversions
|
899
|
+
}),
|
900
|
+
{
|
901
|
+
validate: async (value) => {
|
902
|
+
const result = await zodSchema2.safeParseAsync(value);
|
903
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
904
|
+
}
|
905
|
+
}
|
906
|
+
);
|
907
|
+
}
|
908
|
+
function zod4Schema(zodSchema2, options) {
|
909
|
+
var _a;
|
910
|
+
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
911
|
+
const z4JSONSchema = z4.toJSONSchema(zodSchema2, {
|
912
|
+
target: "draft-7",
|
913
|
+
io: "output",
|
914
|
+
reused: useReferences ? "ref" : "inline"
|
915
|
+
});
|
916
|
+
return jsonSchema(z4JSONSchema, {
|
917
|
+
validate: async (value) => {
|
918
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
919
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
920
|
+
}
|
921
|
+
});
|
922
|
+
}
|
923
|
+
function isZod4Schema(zodSchema2) {
|
924
|
+
return "_zod" in zodSchema2;
|
925
|
+
}
|
926
|
+
function zodSchema(zodSchema2, options) {
|
927
|
+
if (isZod4Schema(zodSchema2)) {
|
928
|
+
return zod4Schema(zodSchema2, options);
|
929
|
+
} else {
|
930
|
+
return zod3Schema(zodSchema2, options);
|
931
|
+
}
|
932
|
+
}
|
933
|
+
|
934
|
+
// src/schema.ts
|
935
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
936
|
+
function jsonSchema(jsonSchema2, {
|
937
|
+
validate
|
938
|
+
} = {}) {
|
939
|
+
return {
|
940
|
+
[schemaSymbol]: true,
|
941
|
+
_type: void 0,
|
942
|
+
// should never be used directly
|
943
|
+
[validatorSymbol]: true,
|
944
|
+
jsonSchema: jsonSchema2,
|
945
|
+
validate
|
946
|
+
};
|
947
|
+
}
|
948
|
+
function isSchema(value) {
|
949
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
950
|
+
}
|
951
|
+
function asSchema(schema) {
|
952
|
+
return schema == null ? jsonSchema({
|
953
|
+
properties: {},
|
954
|
+
additionalProperties: false
|
955
|
+
}) : isSchema(schema) ? schema : zodSchema(schema);
|
956
|
+
}
|
957
|
+
|
885
958
|
// src/uint8-utils.ts
|
886
959
|
var { btoa, atob } = globalThis;
|
887
960
|
function convertBase64ToUint8Array(base64String) {
|
@@ -904,7 +977,15 @@ function convertToBase64(value) {
|
|
904
977
|
function withoutTrailingSlash(url) {
|
905
978
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
906
979
|
}
|
980
|
+
|
981
|
+
// src/index.ts
|
982
|
+
export * from "@standard-schema/spec";
|
983
|
+
import {
|
984
|
+
EventSourceParserStream as EventSourceParserStream2
|
985
|
+
} from "eventsource-parser/stream";
|
907
986
|
export {
|
987
|
+
EventSourceParserStream2 as EventSourceParserStream,
|
988
|
+
asSchema,
|
908
989
|
asValidator,
|
909
990
|
combineHeaders,
|
910
991
|
convertAsyncIteratorToReadableStream,
|
@@ -912,14 +993,16 @@ export {
|
|
912
993
|
convertToBase64,
|
913
994
|
convertUint8ArrayToBase64,
|
914
995
|
createBinaryResponseHandler,
|
915
|
-
createEventSourceParserStream,
|
916
996
|
createEventSourceResponseHandler,
|
917
997
|
createIdGenerator,
|
918
998
|
createJsonErrorResponseHandler,
|
919
999
|
createJsonResponseHandler,
|
920
1000
|
createJsonStreamResponseHandler,
|
1001
|
+
createProviderDefinedToolFactory,
|
1002
|
+
createProviderDefinedToolFactoryWithOutputSchema,
|
921
1003
|
createStatusCodeErrorResponseHandler,
|
922
1004
|
delay,
|
1005
|
+
dynamicTool,
|
923
1006
|
extractResponseHeaders,
|
924
1007
|
generateId,
|
925
1008
|
getErrorMessage,
|
@@ -928,10 +1011,12 @@ export {
|
|
928
1011
|
isParsableJson,
|
929
1012
|
isUrlSupported,
|
930
1013
|
isValidator,
|
1014
|
+
jsonSchema,
|
931
1015
|
loadApiKey,
|
932
1016
|
loadOptionalSetting,
|
933
1017
|
loadSetting,
|
934
1018
|
parseJSON,
|
1019
|
+
parseJsonEventStream,
|
935
1020
|
parseProviderOptions,
|
936
1021
|
postFormDataToApi,
|
937
1022
|
postJsonToApi,
|
@@ -940,10 +1025,12 @@ export {
|
|
940
1025
|
resolve,
|
941
1026
|
safeParseJSON,
|
942
1027
|
safeValidateTypes,
|
1028
|
+
standardSchemaValidator,
|
1029
|
+
tool,
|
943
1030
|
validateTypes,
|
944
1031
|
validator,
|
945
1032
|
validatorSymbol,
|
946
1033
|
withoutTrailingSlash,
|
947
|
-
|
1034
|
+
zodSchema
|
948
1035
|
};
|
949
1036
|
//# sourceMappingURL=index.mjs.map
|