@ai-sdk/provider-utils 3.0.0-canary.8 → 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 +406 -0
- package/dist/index.d.mts +538 -54
- package/dist/index.d.ts +538 -54
- package/dist/index.js +349 -230
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +315 -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 +12 -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,25 +223,24 @@ 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
|
|
230
|
+
// src/is-url-supported.ts
|
231
|
+
function isUrlSupported({
|
232
|
+
mediaType,
|
233
|
+
url,
|
234
|
+
supportedUrls
|
235
|
+
}) {
|
236
|
+
url = url.toLowerCase();
|
237
|
+
mediaType = mediaType.toLowerCase();
|
238
|
+
return Object.entries(supportedUrls).map(([key, value]) => {
|
239
|
+
const mediaType2 = key.toLowerCase();
|
240
|
+
return mediaType2 === "*" || mediaType2 === "*/*" ? { mediaTypePrefix: "", regexes: value } : { mediaTypePrefix: mediaType2.replace(/\*/, ""), regexes: value };
|
241
|
+
}).filter(({ mediaTypePrefix }) => mediaType.startsWith(mediaTypePrefix)).flatMap(({ regexes }) => regexes).some((pattern) => pattern.test(url));
|
242
|
+
}
|
243
|
+
|
293
244
|
// src/load-api-key.ts
|
294
245
|
import { LoadAPIKeyError } from "@ai-sdk/provider";
|
295
246
|
function loadApiKey({
|
@@ -381,7 +332,7 @@ function loadSetting({
|
|
381
332
|
// src/parse-json.ts
|
382
333
|
import {
|
383
334
|
JSONParseError,
|
384
|
-
TypeValidationError as
|
335
|
+
TypeValidationError as TypeValidationError3
|
385
336
|
} from "@ai-sdk/provider";
|
386
337
|
|
387
338
|
// src/secure-json-parse.ts
|
@@ -430,9 +381,10 @@ function secureJsonParse(text) {
|
|
430
381
|
}
|
431
382
|
|
432
383
|
// src/validate-types.ts
|
433
|
-
import { TypeValidationError } from "@ai-sdk/provider";
|
384
|
+
import { TypeValidationError as TypeValidationError2 } from "@ai-sdk/provider";
|
434
385
|
|
435
386
|
// src/validator.ts
|
387
|
+
import { TypeValidationError } from "@ai-sdk/provider";
|
436
388
|
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
437
389
|
function validator(validate) {
|
438
390
|
return { [validatorSymbol]: true, validate };
|
@@ -441,53 +393,61 @@ function isValidator(value) {
|
|
441
393
|
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
442
394
|
}
|
443
395
|
function asValidator(value) {
|
444
|
-
return isValidator(value) ? value :
|
396
|
+
return isValidator(value) ? value : standardSchemaValidator(value);
|
445
397
|
}
|
446
|
-
function
|
447
|
-
return validator((value) => {
|
448
|
-
const result =
|
449
|
-
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
|
+
};
|
450
408
|
});
|
451
409
|
}
|
452
410
|
|
453
411
|
// src/validate-types.ts
|
454
|
-
function validateTypes({
|
412
|
+
async function validateTypes({
|
455
413
|
value,
|
456
|
-
schema
|
414
|
+
schema
|
457
415
|
}) {
|
458
|
-
const result = safeValidateTypes({ value, schema
|
416
|
+
const result = await safeValidateTypes({ value, schema });
|
459
417
|
if (!result.success) {
|
460
|
-
throw
|
418
|
+
throw TypeValidationError2.wrap({ value, cause: result.error });
|
461
419
|
}
|
462
420
|
return result.value;
|
463
421
|
}
|
464
|
-
function safeValidateTypes({
|
422
|
+
async function safeValidateTypes({
|
465
423
|
value,
|
466
424
|
schema
|
467
425
|
}) {
|
468
426
|
const validator2 = asValidator(schema);
|
469
427
|
try {
|
470
428
|
if (validator2.validate == null) {
|
471
|
-
return { success: true, value };
|
429
|
+
return { success: true, value, rawValue: value };
|
472
430
|
}
|
473
|
-
const result = validator2.validate(value);
|
431
|
+
const result = await validator2.validate(value);
|
474
432
|
if (result.success) {
|
475
|
-
return result;
|
433
|
+
return { success: true, value: result.value, rawValue: value };
|
476
434
|
}
|
477
435
|
return {
|
478
436
|
success: false,
|
479
|
-
error:
|
437
|
+
error: TypeValidationError2.wrap({ value, cause: result.error }),
|
438
|
+
rawValue: value
|
480
439
|
};
|
481
440
|
} catch (error) {
|
482
441
|
return {
|
483
442
|
success: false,
|
484
|
-
error:
|
443
|
+
error: TypeValidationError2.wrap({ value, cause: error }),
|
444
|
+
rawValue: value
|
485
445
|
};
|
486
446
|
}
|
487
447
|
}
|
488
448
|
|
489
449
|
// src/parse-json.ts
|
490
|
-
function parseJSON({
|
450
|
+
async function parseJSON({
|
491
451
|
text,
|
492
452
|
schema
|
493
453
|
}) {
|
@@ -498,13 +458,13 @@ function parseJSON({
|
|
498
458
|
}
|
499
459
|
return validateTypes({ value, schema });
|
500
460
|
} catch (error) {
|
501
|
-
if (JSONParseError.isInstance(error) ||
|
461
|
+
if (JSONParseError.isInstance(error) || TypeValidationError3.isInstance(error)) {
|
502
462
|
throw error;
|
503
463
|
}
|
504
464
|
throw new JSONParseError({ text, cause: error });
|
505
465
|
}
|
506
466
|
}
|
507
|
-
function safeParseJSON({
|
467
|
+
async function safeParseJSON({
|
508
468
|
text,
|
509
469
|
schema
|
510
470
|
}) {
|
@@ -513,12 +473,12 @@ function safeParseJSON({
|
|
513
473
|
if (schema == null) {
|
514
474
|
return { success: true, value, rawValue: value };
|
515
475
|
}
|
516
|
-
|
517
|
-
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
476
|
+
return await safeValidateTypes({ value, schema });
|
518
477
|
} catch (error) {
|
519
478
|
return {
|
520
479
|
success: false,
|
521
|
-
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
|
522
482
|
};
|
523
483
|
}
|
524
484
|
}
|
@@ -531,9 +491,29 @@ function isParsableJson(input) {
|
|
531
491
|
}
|
532
492
|
}
|
533
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
|
+
|
534
514
|
// src/parse-provider-options.ts
|
535
515
|
import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
|
536
|
-
function parseProviderOptions({
|
516
|
+
async function parseProviderOptions({
|
537
517
|
provider,
|
538
518
|
providerOptions,
|
539
519
|
schema
|
@@ -541,7 +521,7 @@ function parseProviderOptions({
|
|
541
521
|
if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
|
542
522
|
return void 0;
|
543
523
|
}
|
544
|
-
const parsedProviderOptions = safeValidateTypes({
|
524
|
+
const parsedProviderOptions = await safeValidateTypes({
|
545
525
|
value: providerOptions[provider],
|
546
526
|
schema
|
547
527
|
});
|
@@ -556,7 +536,7 @@ function parseProviderOptions({
|
|
556
536
|
}
|
557
537
|
|
558
538
|
// src/post-to-api.ts
|
559
|
-
import { APICallError as
|
539
|
+
import { APICallError as APICallError3 } from "@ai-sdk/provider";
|
560
540
|
var getOriginalFetch2 = () => globalThis.fetch;
|
561
541
|
var postJsonToApi = async ({
|
562
542
|
url,
|
@@ -627,10 +607,10 @@ var postToApi = async ({
|
|
627
607
|
requestBodyValues: body.values
|
628
608
|
});
|
629
609
|
} catch (error) {
|
630
|
-
if (isAbortError(error) ||
|
610
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
631
611
|
throw error;
|
632
612
|
}
|
633
|
-
throw new
|
613
|
+
throw new APICallError3({
|
634
614
|
message: "Failed to process error response",
|
635
615
|
cause: error,
|
636
616
|
statusCode: response.status,
|
@@ -649,11 +629,11 @@ var postToApi = async ({
|
|
649
629
|
});
|
650
630
|
} catch (error) {
|
651
631
|
if (error instanceof Error) {
|
652
|
-
if (isAbortError(error) ||
|
632
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
653
633
|
throw error;
|
654
634
|
}
|
655
635
|
}
|
656
|
-
throw new
|
636
|
+
throw new APICallError3({
|
657
637
|
message: "Failed to process successful response",
|
658
638
|
cause: error,
|
659
639
|
statusCode: response.status,
|
@@ -663,26 +643,74 @@ var postToApi = async ({
|
|
663
643
|
});
|
664
644
|
}
|
665
645
|
} catch (error) {
|
666
|
-
|
667
|
-
throw error;
|
668
|
-
}
|
669
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
670
|
-
const cause = error.cause;
|
671
|
-
if (cause != null) {
|
672
|
-
throw new APICallError2({
|
673
|
-
message: `Cannot connect to API: ${cause.message}`,
|
674
|
-
cause,
|
675
|
-
url,
|
676
|
-
requestBodyValues: body.values,
|
677
|
-
isRetryable: true
|
678
|
-
// retry when network error
|
679
|
-
});
|
680
|
-
}
|
681
|
-
}
|
682
|
-
throw error;
|
646
|
+
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
683
647
|
}
|
684
648
|
};
|
685
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
|
+
|
686
714
|
// src/resolve.ts
|
687
715
|
async function resolve(value) {
|
688
716
|
if (typeof value === "function") {
|
@@ -692,7 +720,7 @@ async function resolve(value) {
|
|
692
720
|
}
|
693
721
|
|
694
722
|
// src/response-handler.ts
|
695
|
-
import { APICallError as
|
723
|
+
import { APICallError as APICallError4, EmptyResponseBodyError } from "@ai-sdk/provider";
|
696
724
|
var createJsonErrorResponseHandler = ({
|
697
725
|
errorSchema,
|
698
726
|
errorToMessage,
|
@@ -703,7 +731,7 @@ var createJsonErrorResponseHandler = ({
|
|
703
731
|
if (responseBody.trim() === "") {
|
704
732
|
return {
|
705
733
|
responseHeaders,
|
706
|
-
value: new
|
734
|
+
value: new APICallError4({
|
707
735
|
message: response.statusText,
|
708
736
|
url,
|
709
737
|
requestBodyValues,
|
@@ -715,13 +743,13 @@ var createJsonErrorResponseHandler = ({
|
|
715
743
|
};
|
716
744
|
}
|
717
745
|
try {
|
718
|
-
const parsedError = parseJSON({
|
746
|
+
const parsedError = await parseJSON({
|
719
747
|
text: responseBody,
|
720
748
|
schema: errorSchema
|
721
749
|
});
|
722
750
|
return {
|
723
751
|
responseHeaders,
|
724
|
-
value: new
|
752
|
+
value: new APICallError4({
|
725
753
|
message: errorToMessage(parsedError),
|
726
754
|
url,
|
727
755
|
requestBodyValues,
|
@@ -735,7 +763,7 @@ var createJsonErrorResponseHandler = ({
|
|
735
763
|
} catch (parseError) {
|
736
764
|
return {
|
737
765
|
responseHeaders,
|
738
|
-
value: new
|
766
|
+
value: new APICallError4({
|
739
767
|
message: response.statusText,
|
740
768
|
url,
|
741
769
|
requestBodyValues,
|
@@ -754,21 +782,10 @@ var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) =>
|
|
754
782
|
}
|
755
783
|
return {
|
756
784
|
responseHeaders,
|
757
|
-
value:
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
return;
|
762
|
-
}
|
763
|
-
controller.enqueue(
|
764
|
-
safeParseJSON({
|
765
|
-
text: data,
|
766
|
-
schema: chunkSchema
|
767
|
-
})
|
768
|
-
);
|
769
|
-
}
|
770
|
-
})
|
771
|
-
)
|
785
|
+
value: parseJsonEventStream({
|
786
|
+
stream: response.body,
|
787
|
+
schema: chunkSchema
|
788
|
+
})
|
772
789
|
};
|
773
790
|
};
|
774
791
|
var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
@@ -781,10 +798,10 @@ var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
781
798
|
responseHeaders,
|
782
799
|
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(
|
783
800
|
new TransformStream({
|
784
|
-
transform(chunkText, controller) {
|
801
|
+
async transform(chunkText, controller) {
|
785
802
|
if (chunkText.endsWith("\n")) {
|
786
803
|
controller.enqueue(
|
787
|
-
safeParseJSON({
|
804
|
+
await safeParseJSON({
|
788
805
|
text: buffer + chunkText,
|
789
806
|
schema: chunkSchema
|
790
807
|
})
|
@@ -800,13 +817,13 @@ var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
800
817
|
};
|
801
818
|
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
802
819
|
const responseBody = await response.text();
|
803
|
-
const parsedResult = safeParseJSON({
|
820
|
+
const parsedResult = await safeParseJSON({
|
804
821
|
text: responseBody,
|
805
822
|
schema: responseSchema
|
806
823
|
});
|
807
824
|
const responseHeaders = extractResponseHeaders(response);
|
808
825
|
if (!parsedResult.success) {
|
809
|
-
throw new
|
826
|
+
throw new APICallError4({
|
810
827
|
message: "Invalid JSON response",
|
811
828
|
cause: parsedResult.error,
|
812
829
|
statusCode: response.status,
|
@@ -825,7 +842,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
825
842
|
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
826
843
|
const responseHeaders = extractResponseHeaders(response);
|
827
844
|
if (!response.body) {
|
828
|
-
throw new
|
845
|
+
throw new APICallError4({
|
829
846
|
message: "Response body is empty",
|
830
847
|
url,
|
831
848
|
requestBodyValues,
|
@@ -841,7 +858,7 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
841
858
|
value: new Uint8Array(buffer)
|
842
859
|
};
|
843
860
|
} catch (error) {
|
844
|
-
throw new
|
861
|
+
throw new APICallError4({
|
845
862
|
message: "Failed to read response as array buffer",
|
846
863
|
url,
|
847
864
|
requestBodyValues,
|
@@ -857,7 +874,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
857
874
|
const responseBody = await response.text();
|
858
875
|
return {
|
859
876
|
responseHeaders,
|
860
|
-
value: new
|
877
|
+
value: new APICallError4({
|
861
878
|
message: response.statusText,
|
862
879
|
url,
|
863
880
|
requestBodyValues,
|
@@ -868,6 +885,76 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
868
885
|
};
|
869
886
|
};
|
870
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
|
+
|
871
958
|
// src/uint8-utils.ts
|
872
959
|
var { btoa, atob } = globalThis;
|
873
960
|
function convertBase64ToUint8Array(base64String) {
|
@@ -890,7 +977,15 @@ function convertToBase64(value) {
|
|
890
977
|
function withoutTrailingSlash(url) {
|
891
978
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
892
979
|
}
|
980
|
+
|
981
|
+
// src/index.ts
|
982
|
+
export * from "@standard-schema/spec";
|
983
|
+
import {
|
984
|
+
EventSourceParserStream as EventSourceParserStream2
|
985
|
+
} from "eventsource-parser/stream";
|
893
986
|
export {
|
987
|
+
EventSourceParserStream2 as EventSourceParserStream,
|
988
|
+
asSchema,
|
894
989
|
asValidator,
|
895
990
|
combineHeaders,
|
896
991
|
convertAsyncIteratorToReadableStream,
|
@@ -898,25 +993,30 @@ export {
|
|
898
993
|
convertToBase64,
|
899
994
|
convertUint8ArrayToBase64,
|
900
995
|
createBinaryResponseHandler,
|
901
|
-
createEventSourceParserStream,
|
902
996
|
createEventSourceResponseHandler,
|
903
997
|
createIdGenerator,
|
904
998
|
createJsonErrorResponseHandler,
|
905
999
|
createJsonResponseHandler,
|
906
1000
|
createJsonStreamResponseHandler,
|
1001
|
+
createProviderDefinedToolFactory,
|
1002
|
+
createProviderDefinedToolFactoryWithOutputSchema,
|
907
1003
|
createStatusCodeErrorResponseHandler,
|
908
1004
|
delay,
|
1005
|
+
dynamicTool,
|
909
1006
|
extractResponseHeaders,
|
910
1007
|
generateId,
|
911
1008
|
getErrorMessage,
|
912
1009
|
getFromApi,
|
913
1010
|
isAbortError,
|
914
1011
|
isParsableJson,
|
1012
|
+
isUrlSupported,
|
915
1013
|
isValidator,
|
1014
|
+
jsonSchema,
|
916
1015
|
loadApiKey,
|
917
1016
|
loadOptionalSetting,
|
918
1017
|
loadSetting,
|
919
1018
|
parseJSON,
|
1019
|
+
parseJsonEventStream,
|
920
1020
|
parseProviderOptions,
|
921
1021
|
postFormDataToApi,
|
922
1022
|
postJsonToApi,
|
@@ -925,10 +1025,12 @@ export {
|
|
925
1025
|
resolve,
|
926
1026
|
safeParseJSON,
|
927
1027
|
safeValidateTypes,
|
1028
|
+
standardSchemaValidator,
|
1029
|
+
tool,
|
928
1030
|
validateTypes,
|
929
1031
|
validator,
|
930
1032
|
validatorSymbol,
|
931
1033
|
withoutTrailingSlash,
|
932
|
-
|
1034
|
+
zodSchema
|
933
1035
|
};
|
934
1036
|
//# sourceMappingURL=index.mjs.map
|