@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.js
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __create = Object.create;
|
2
3
|
var __defProp = Object.defineProperty;
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
8
|
var __export = (target, all) => {
|
7
9
|
for (var name in all)
|
@@ -15,11 +17,22 @@ var __copyProps = (to, from, except, desc) => {
|
|
15
17
|
}
|
16
18
|
return to;
|
17
19
|
};
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
27
|
+
mod
|
28
|
+
));
|
18
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
30
|
|
20
31
|
// src/index.ts
|
21
32
|
var src_exports = {};
|
22
33
|
__export(src_exports, {
|
34
|
+
EventSourceParserStream: () => import_stream2.EventSourceParserStream,
|
35
|
+
asSchema: () => asSchema,
|
23
36
|
asValidator: () => asValidator,
|
24
37
|
combineHeaders: () => combineHeaders,
|
25
38
|
convertAsyncIteratorToReadableStream: () => convertAsyncIteratorToReadableStream,
|
@@ -27,14 +40,16 @@ __export(src_exports, {
|
|
27
40
|
convertToBase64: () => convertToBase64,
|
28
41
|
convertUint8ArrayToBase64: () => convertUint8ArrayToBase64,
|
29
42
|
createBinaryResponseHandler: () => createBinaryResponseHandler,
|
30
|
-
createEventSourceParserStream: () => createEventSourceParserStream,
|
31
43
|
createEventSourceResponseHandler: () => createEventSourceResponseHandler,
|
32
44
|
createIdGenerator: () => createIdGenerator,
|
33
45
|
createJsonErrorResponseHandler: () => createJsonErrorResponseHandler,
|
34
46
|
createJsonResponseHandler: () => createJsonResponseHandler,
|
35
47
|
createJsonStreamResponseHandler: () => createJsonStreamResponseHandler,
|
48
|
+
createProviderDefinedToolFactory: () => createProviderDefinedToolFactory,
|
49
|
+
createProviderDefinedToolFactoryWithOutputSchema: () => createProviderDefinedToolFactoryWithOutputSchema,
|
36
50
|
createStatusCodeErrorResponseHandler: () => createStatusCodeErrorResponseHandler,
|
37
51
|
delay: () => delay,
|
52
|
+
dynamicTool: () => dynamicTool,
|
38
53
|
extractResponseHeaders: () => extractResponseHeaders,
|
39
54
|
generateId: () => generateId,
|
40
55
|
getErrorMessage: () => getErrorMessage,
|
@@ -43,10 +58,12 @@ __export(src_exports, {
|
|
43
58
|
isParsableJson: () => isParsableJson,
|
44
59
|
isUrlSupported: () => isUrlSupported,
|
45
60
|
isValidator: () => isValidator,
|
61
|
+
jsonSchema: () => jsonSchema,
|
46
62
|
loadApiKey: () => loadApiKey,
|
47
63
|
loadOptionalSetting: () => loadOptionalSetting,
|
48
64
|
loadSetting: () => loadSetting,
|
49
65
|
parseJSON: () => parseJSON,
|
66
|
+
parseJsonEventStream: () => parseJsonEventStream,
|
50
67
|
parseProviderOptions: () => parseProviderOptions,
|
51
68
|
postFormDataToApi: () => postFormDataToApi,
|
52
69
|
postJsonToApi: () => postJsonToApi,
|
@@ -55,11 +72,13 @@ __export(src_exports, {
|
|
55
72
|
resolve: () => resolve,
|
56
73
|
safeParseJSON: () => safeParseJSON,
|
57
74
|
safeValidateTypes: () => safeValidateTypes,
|
75
|
+
standardSchemaValidator: () => standardSchemaValidator,
|
76
|
+
tool: () => tool,
|
58
77
|
validateTypes: () => validateTypes,
|
59
78
|
validator: () => validator,
|
60
79
|
validatorSymbol: () => validatorSymbol,
|
61
80
|
withoutTrailingSlash: () => withoutTrailingSlash,
|
62
|
-
|
81
|
+
zodSchema: () => zodSchema
|
63
82
|
});
|
64
83
|
module.exports = __toCommonJS(src_exports);
|
65
84
|
|
@@ -104,134 +123,56 @@ function convertAsyncIteratorToReadableStream(iterator) {
|
|
104
123
|
}
|
105
124
|
|
106
125
|
// src/delay.ts
|
107
|
-
async function delay(delayInMs) {
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
// src/event-source-parser-stream.ts
|
112
|
-
function createEventSourceParserStream() {
|
113
|
-
let buffer = "";
|
114
|
-
let event = void 0;
|
115
|
-
let data = [];
|
116
|
-
let lastEventId = void 0;
|
117
|
-
let retry = void 0;
|
118
|
-
function parseLine(line, controller) {
|
119
|
-
if (line === "") {
|
120
|
-
dispatchEvent(controller);
|
121
|
-
return;
|
122
|
-
}
|
123
|
-
if (line.startsWith(":")) {
|
124
|
-
return;
|
125
|
-
}
|
126
|
-
const colonIndex = line.indexOf(":");
|
127
|
-
if (colonIndex === -1) {
|
128
|
-
handleField(line, "");
|
129
|
-
return;
|
130
|
-
}
|
131
|
-
const field = line.slice(0, colonIndex);
|
132
|
-
const valueStart = colonIndex + 1;
|
133
|
-
const value = valueStart < line.length && line[valueStart] === " " ? line.slice(valueStart + 1) : line.slice(valueStart);
|
134
|
-
handleField(field, value);
|
135
|
-
}
|
136
|
-
function dispatchEvent(controller) {
|
137
|
-
if (data.length > 0) {
|
138
|
-
controller.enqueue({
|
139
|
-
event,
|
140
|
-
data: data.join("\n"),
|
141
|
-
id: lastEventId,
|
142
|
-
retry
|
143
|
-
});
|
144
|
-
data = [];
|
145
|
-
event = void 0;
|
146
|
-
retry = void 0;
|
147
|
-
}
|
148
|
-
}
|
149
|
-
function handleField(field, value) {
|
150
|
-
switch (field) {
|
151
|
-
case "event":
|
152
|
-
event = value;
|
153
|
-
break;
|
154
|
-
case "data":
|
155
|
-
data.push(value);
|
156
|
-
break;
|
157
|
-
case "id":
|
158
|
-
lastEventId = value;
|
159
|
-
break;
|
160
|
-
case "retry":
|
161
|
-
const parsedRetry = parseInt(value, 10);
|
162
|
-
if (!isNaN(parsedRetry)) {
|
163
|
-
retry = parsedRetry;
|
164
|
-
}
|
165
|
-
break;
|
166
|
-
}
|
126
|
+
async function delay(delayInMs, options) {
|
127
|
+
if (delayInMs == null) {
|
128
|
+
return Promise.resolve();
|
167
129
|
}
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
parseLine(lines[i], controller);
|
174
|
-
}
|
175
|
-
},
|
176
|
-
flush(controller) {
|
177
|
-
parseLine(buffer, controller);
|
178
|
-
dispatchEvent(controller);
|
130
|
+
const signal = options == null ? void 0 : options.abortSignal;
|
131
|
+
return new Promise((resolve2, reject) => {
|
132
|
+
if (signal == null ? void 0 : signal.aborted) {
|
133
|
+
reject(createAbortError());
|
134
|
+
return;
|
179
135
|
}
|
136
|
+
const timeoutId = setTimeout(() => {
|
137
|
+
cleanup();
|
138
|
+
resolve2();
|
139
|
+
}, delayInMs);
|
140
|
+
const cleanup = () => {
|
141
|
+
clearTimeout(timeoutId);
|
142
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
143
|
+
};
|
144
|
+
const onAbort = () => {
|
145
|
+
cleanup();
|
146
|
+
reject(createAbortError());
|
147
|
+
};
|
148
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort);
|
180
149
|
});
|
181
150
|
}
|
182
|
-
function
|
183
|
-
|
184
|
-
let currentLine = buffer;
|
185
|
-
for (let i = 0; i < chunk.length; ) {
|
186
|
-
const char = chunk[i++];
|
187
|
-
if (char === "\n") {
|
188
|
-
lines.push(currentLine);
|
189
|
-
currentLine = "";
|
190
|
-
} else if (char === "\r") {
|
191
|
-
lines.push(currentLine);
|
192
|
-
currentLine = "";
|
193
|
-
if (chunk[i + 1] === "\n") {
|
194
|
-
i++;
|
195
|
-
}
|
196
|
-
} else {
|
197
|
-
currentLine += char;
|
198
|
-
}
|
199
|
-
}
|
200
|
-
return { lines, incompleteLine: currentLine };
|
151
|
+
function createAbortError() {
|
152
|
+
return new DOMException("Delay was aborted", "AbortError");
|
201
153
|
}
|
202
154
|
|
203
155
|
// src/extract-response-headers.ts
|
204
156
|
function extractResponseHeaders(response) {
|
205
|
-
|
206
|
-
response.headers.forEach((value, key) => {
|
207
|
-
headers[key] = value;
|
208
|
-
});
|
209
|
-
return headers;
|
157
|
+
return Object.fromEntries([...response.headers]);
|
210
158
|
}
|
211
159
|
|
212
160
|
// src/generate-id.ts
|
213
161
|
var import_provider = require("@ai-sdk/provider");
|
214
|
-
|
215
|
-
// src/generate-id-custom-alphabet.ts
|
216
|
-
function customAlphabet(alphabet, defaultSize) {
|
217
|
-
return (size = defaultSize) => {
|
218
|
-
let id = "";
|
219
|
-
let i = size | 0;
|
220
|
-
while (i--) {
|
221
|
-
id += alphabet[Math.random() * alphabet.length | 0];
|
222
|
-
}
|
223
|
-
return id;
|
224
|
-
};
|
225
|
-
}
|
226
|
-
|
227
|
-
// src/generate-id.ts
|
228
162
|
var createIdGenerator = ({
|
229
163
|
prefix,
|
230
|
-
size
|
164
|
+
size = 16,
|
231
165
|
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
232
166
|
separator = "-"
|
233
167
|
} = {}) => {
|
234
|
-
const generator =
|
168
|
+
const generator = () => {
|
169
|
+
const alphabetLength = alphabet.length;
|
170
|
+
const chars = new Array(size);
|
171
|
+
for (let i = 0; i < size; i++) {
|
172
|
+
chars[i] = alphabet[Math.random() * alphabetLength | 0];
|
173
|
+
}
|
174
|
+
return chars.join("");
|
175
|
+
};
|
235
176
|
if (prefix == null) {
|
236
177
|
return generator;
|
237
178
|
}
|
@@ -241,7 +182,7 @@ var createIdGenerator = ({
|
|
241
182
|
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
242
183
|
});
|
243
184
|
}
|
244
|
-
return (
|
185
|
+
return () => `${prefix}${separator}${generator()}`;
|
245
186
|
};
|
246
187
|
var generateId = createIdGenerator();
|
247
188
|
|
@@ -260,8 +201,43 @@ function getErrorMessage(error) {
|
|
260
201
|
}
|
261
202
|
|
262
203
|
// src/get-from-api.ts
|
204
|
+
var import_provider3 = require("@ai-sdk/provider");
|
205
|
+
|
206
|
+
// src/handle-fetch-error.ts
|
263
207
|
var import_provider2 = require("@ai-sdk/provider");
|
264
208
|
|
209
|
+
// src/is-abort-error.ts
|
210
|
+
function isAbortError(error) {
|
211
|
+
return (error instanceof Error || error instanceof DOMException) && (error.name === "AbortError" || error.name === "ResponseAborted" || // Next.js
|
212
|
+
error.name === "TimeoutError");
|
213
|
+
}
|
214
|
+
|
215
|
+
// src/handle-fetch-error.ts
|
216
|
+
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
217
|
+
function handleFetchError({
|
218
|
+
error,
|
219
|
+
url,
|
220
|
+
requestBodyValues
|
221
|
+
}) {
|
222
|
+
if (isAbortError(error)) {
|
223
|
+
return error;
|
224
|
+
}
|
225
|
+
if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
|
226
|
+
const cause = error.cause;
|
227
|
+
if (cause != null) {
|
228
|
+
return new import_provider2.APICallError({
|
229
|
+
message: `Cannot connect to API: ${cause.message}`,
|
230
|
+
cause,
|
231
|
+
url,
|
232
|
+
requestBodyValues,
|
233
|
+
isRetryable: true
|
234
|
+
// retry when network error
|
235
|
+
});
|
236
|
+
}
|
237
|
+
}
|
238
|
+
return error;
|
239
|
+
}
|
240
|
+
|
265
241
|
// src/remove-undefined-entries.ts
|
266
242
|
function removeUndefinedEntries(record) {
|
267
243
|
return Object.fromEntries(
|
@@ -269,11 +245,6 @@ function removeUndefinedEntries(record) {
|
|
269
245
|
);
|
270
246
|
}
|
271
247
|
|
272
|
-
// src/is-abort-error.ts
|
273
|
-
function isAbortError(error) {
|
274
|
-
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
275
|
-
}
|
276
|
-
|
277
248
|
// src/get-from-api.ts
|
278
249
|
var getOriginalFetch = () => globalThis.fetch;
|
279
250
|
var getFromApi = async ({
|
@@ -300,10 +271,10 @@ var getFromApi = async ({
|
|
300
271
|
requestBodyValues: {}
|
301
272
|
});
|
302
273
|
} catch (error) {
|
303
|
-
if (isAbortError(error) ||
|
274
|
+
if (isAbortError(error) || import_provider3.APICallError.isInstance(error)) {
|
304
275
|
throw error;
|
305
276
|
}
|
306
|
-
throw new
|
277
|
+
throw new import_provider3.APICallError({
|
307
278
|
message: "Failed to process error response",
|
308
279
|
cause: error,
|
309
280
|
statusCode: response.status,
|
@@ -322,11 +293,11 @@ var getFromApi = async ({
|
|
322
293
|
});
|
323
294
|
} catch (error) {
|
324
295
|
if (error instanceof Error) {
|
325
|
-
if (isAbortError(error) ||
|
296
|
+
if (isAbortError(error) || import_provider3.APICallError.isInstance(error)) {
|
326
297
|
throw error;
|
327
298
|
}
|
328
299
|
}
|
329
|
-
throw new
|
300
|
+
throw new import_provider3.APICallError({
|
330
301
|
message: "Failed to process successful response",
|
331
302
|
cause: error,
|
332
303
|
statusCode: response.status,
|
@@ -336,22 +307,7 @@ var getFromApi = async ({
|
|
336
307
|
});
|
337
308
|
}
|
338
309
|
} catch (error) {
|
339
|
-
|
340
|
-
throw error;
|
341
|
-
}
|
342
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
343
|
-
const cause = error.cause;
|
344
|
-
if (cause != null) {
|
345
|
-
throw new import_provider2.APICallError({
|
346
|
-
message: `Cannot connect to API: ${cause.message}`,
|
347
|
-
cause,
|
348
|
-
url,
|
349
|
-
isRetryable: true,
|
350
|
-
requestBodyValues: {}
|
351
|
-
});
|
352
|
-
}
|
353
|
-
}
|
354
|
-
throw error;
|
310
|
+
throw handleFetchError({ error, url, requestBodyValues: {} });
|
355
311
|
}
|
356
312
|
};
|
357
313
|
|
@@ -370,7 +326,7 @@ function isUrlSupported({
|
|
370
326
|
}
|
371
327
|
|
372
328
|
// src/load-api-key.ts
|
373
|
-
var
|
329
|
+
var import_provider4 = require("@ai-sdk/provider");
|
374
330
|
function loadApiKey({
|
375
331
|
apiKey,
|
376
332
|
environmentVariableName,
|
@@ -381,23 +337,23 @@ function loadApiKey({
|
|
381
337
|
return apiKey;
|
382
338
|
}
|
383
339
|
if (apiKey != null) {
|
384
|
-
throw new
|
340
|
+
throw new import_provider4.LoadAPIKeyError({
|
385
341
|
message: `${description} API key must be a string.`
|
386
342
|
});
|
387
343
|
}
|
388
344
|
if (typeof process === "undefined") {
|
389
|
-
throw new
|
345
|
+
throw new import_provider4.LoadAPIKeyError({
|
390
346
|
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
391
347
|
});
|
392
348
|
}
|
393
349
|
apiKey = process.env[environmentVariableName];
|
394
350
|
if (apiKey == null) {
|
395
|
-
throw new
|
351
|
+
throw new import_provider4.LoadAPIKeyError({
|
396
352
|
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
397
353
|
});
|
398
354
|
}
|
399
355
|
if (typeof apiKey !== "string") {
|
400
|
-
throw new
|
356
|
+
throw new import_provider4.LoadAPIKeyError({
|
401
357
|
message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
402
358
|
});
|
403
359
|
}
|
@@ -423,7 +379,7 @@ function loadOptionalSetting({
|
|
423
379
|
}
|
424
380
|
|
425
381
|
// src/load-setting.ts
|
426
|
-
var
|
382
|
+
var import_provider5 = require("@ai-sdk/provider");
|
427
383
|
function loadSetting({
|
428
384
|
settingValue,
|
429
385
|
environmentVariableName,
|
@@ -434,23 +390,23 @@ function loadSetting({
|
|
434
390
|
return settingValue;
|
435
391
|
}
|
436
392
|
if (settingValue != null) {
|
437
|
-
throw new
|
393
|
+
throw new import_provider5.LoadSettingError({
|
438
394
|
message: `${description} setting must be a string.`
|
439
395
|
});
|
440
396
|
}
|
441
397
|
if (typeof process === "undefined") {
|
442
|
-
throw new
|
398
|
+
throw new import_provider5.LoadSettingError({
|
443
399
|
message: `${description} setting is missing. Pass it using the '${settingName}' parameter. Environment variables is not supported in this environment.`
|
444
400
|
});
|
445
401
|
}
|
446
402
|
settingValue = process.env[environmentVariableName];
|
447
403
|
if (settingValue == null) {
|
448
|
-
throw new
|
404
|
+
throw new import_provider5.LoadSettingError({
|
449
405
|
message: `${description} setting is missing. Pass it using the '${settingName}' parameter or the ${environmentVariableName} environment variable.`
|
450
406
|
});
|
451
407
|
}
|
452
408
|
if (typeof settingValue !== "string") {
|
453
|
-
throw new
|
409
|
+
throw new import_provider5.LoadSettingError({
|
454
410
|
message: `${description} setting must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
455
411
|
});
|
456
412
|
}
|
@@ -458,7 +414,7 @@ function loadSetting({
|
|
458
414
|
}
|
459
415
|
|
460
416
|
// src/parse-json.ts
|
461
|
-
var
|
417
|
+
var import_provider8 = require("@ai-sdk/provider");
|
462
418
|
|
463
419
|
// src/secure-json-parse.ts
|
464
420
|
var suspectProtoRx = /"__proto__"\s*:/;
|
@@ -506,9 +462,10 @@ function secureJsonParse(text) {
|
|
506
462
|
}
|
507
463
|
|
508
464
|
// src/validate-types.ts
|
509
|
-
var
|
465
|
+
var import_provider7 = require("@ai-sdk/provider");
|
510
466
|
|
511
467
|
// src/validator.ts
|
468
|
+
var import_provider6 = require("@ai-sdk/provider");
|
512
469
|
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
513
470
|
function validator(validate) {
|
514
471
|
return { [validatorSymbol]: true, validate };
|
@@ -517,53 +474,61 @@ function isValidator(value) {
|
|
517
474
|
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
518
475
|
}
|
519
476
|
function asValidator(value) {
|
520
|
-
return isValidator(value) ? value :
|
477
|
+
return isValidator(value) ? value : standardSchemaValidator(value);
|
521
478
|
}
|
522
|
-
function
|
523
|
-
return validator((value) => {
|
524
|
-
const result =
|
525
|
-
return result.
|
479
|
+
function standardSchemaValidator(standardSchema) {
|
480
|
+
return validator(async (value) => {
|
481
|
+
const result = await standardSchema["~standard"].validate(value);
|
482
|
+
return result.issues == null ? { success: true, value: result.value } : {
|
483
|
+
success: false,
|
484
|
+
error: new import_provider6.TypeValidationError({
|
485
|
+
value,
|
486
|
+
cause: result.issues
|
487
|
+
})
|
488
|
+
};
|
526
489
|
});
|
527
490
|
}
|
528
491
|
|
529
492
|
// src/validate-types.ts
|
530
|
-
function validateTypes({
|
493
|
+
async function validateTypes({
|
531
494
|
value,
|
532
|
-
schema
|
495
|
+
schema
|
533
496
|
}) {
|
534
|
-
const result = safeValidateTypes({ value, schema
|
497
|
+
const result = await safeValidateTypes({ value, schema });
|
535
498
|
if (!result.success) {
|
536
|
-
throw
|
499
|
+
throw import_provider7.TypeValidationError.wrap({ value, cause: result.error });
|
537
500
|
}
|
538
501
|
return result.value;
|
539
502
|
}
|
540
|
-
function safeValidateTypes({
|
503
|
+
async function safeValidateTypes({
|
541
504
|
value,
|
542
505
|
schema
|
543
506
|
}) {
|
544
507
|
const validator2 = asValidator(schema);
|
545
508
|
try {
|
546
509
|
if (validator2.validate == null) {
|
547
|
-
return { success: true, value };
|
510
|
+
return { success: true, value, rawValue: value };
|
548
511
|
}
|
549
|
-
const result = validator2.validate(value);
|
512
|
+
const result = await validator2.validate(value);
|
550
513
|
if (result.success) {
|
551
|
-
return result;
|
514
|
+
return { success: true, value: result.value, rawValue: value };
|
552
515
|
}
|
553
516
|
return {
|
554
517
|
success: false,
|
555
|
-
error:
|
518
|
+
error: import_provider7.TypeValidationError.wrap({ value, cause: result.error }),
|
519
|
+
rawValue: value
|
556
520
|
};
|
557
521
|
} catch (error) {
|
558
522
|
return {
|
559
523
|
success: false,
|
560
|
-
error:
|
524
|
+
error: import_provider7.TypeValidationError.wrap({ value, cause: error }),
|
525
|
+
rawValue: value
|
561
526
|
};
|
562
527
|
}
|
563
528
|
}
|
564
529
|
|
565
530
|
// src/parse-json.ts
|
566
|
-
function parseJSON({
|
531
|
+
async function parseJSON({
|
567
532
|
text,
|
568
533
|
schema
|
569
534
|
}) {
|
@@ -574,13 +539,13 @@ function parseJSON({
|
|
574
539
|
}
|
575
540
|
return validateTypes({ value, schema });
|
576
541
|
} catch (error) {
|
577
|
-
if (
|
542
|
+
if (import_provider8.JSONParseError.isInstance(error) || import_provider8.TypeValidationError.isInstance(error)) {
|
578
543
|
throw error;
|
579
544
|
}
|
580
|
-
throw new
|
545
|
+
throw new import_provider8.JSONParseError({ text, cause: error });
|
581
546
|
}
|
582
547
|
}
|
583
|
-
function safeParseJSON({
|
548
|
+
async function safeParseJSON({
|
584
549
|
text,
|
585
550
|
schema
|
586
551
|
}) {
|
@@ -589,12 +554,12 @@ function safeParseJSON({
|
|
589
554
|
if (schema == null) {
|
590
555
|
return { success: true, value, rawValue: value };
|
591
556
|
}
|
592
|
-
|
593
|
-
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
557
|
+
return await safeValidateTypes({ value, schema });
|
594
558
|
} catch (error) {
|
595
559
|
return {
|
596
560
|
success: false,
|
597
|
-
error:
|
561
|
+
error: import_provider8.JSONParseError.isInstance(error) ? error : new import_provider8.JSONParseError({ text, cause: error }),
|
562
|
+
rawValue: void 0
|
598
563
|
};
|
599
564
|
}
|
600
565
|
}
|
@@ -607,9 +572,27 @@ function isParsableJson(input) {
|
|
607
572
|
}
|
608
573
|
}
|
609
574
|
|
575
|
+
// src/parse-json-event-stream.ts
|
576
|
+
var import_stream = require("eventsource-parser/stream");
|
577
|
+
function parseJsonEventStream({
|
578
|
+
stream,
|
579
|
+
schema
|
580
|
+
}) {
|
581
|
+
return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough(
|
582
|
+
new TransformStream({
|
583
|
+
async transform({ data }, controller) {
|
584
|
+
if (data === "[DONE]") {
|
585
|
+
return;
|
586
|
+
}
|
587
|
+
controller.enqueue(await safeParseJSON({ text: data, schema }));
|
588
|
+
}
|
589
|
+
})
|
590
|
+
);
|
591
|
+
}
|
592
|
+
|
610
593
|
// src/parse-provider-options.ts
|
611
|
-
var
|
612
|
-
function parseProviderOptions({
|
594
|
+
var import_provider9 = require("@ai-sdk/provider");
|
595
|
+
async function parseProviderOptions({
|
613
596
|
provider,
|
614
597
|
providerOptions,
|
615
598
|
schema
|
@@ -617,12 +600,12 @@ function parseProviderOptions({
|
|
617
600
|
if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
|
618
601
|
return void 0;
|
619
602
|
}
|
620
|
-
const parsedProviderOptions = safeValidateTypes({
|
603
|
+
const parsedProviderOptions = await safeValidateTypes({
|
621
604
|
value: providerOptions[provider],
|
622
605
|
schema
|
623
606
|
});
|
624
607
|
if (!parsedProviderOptions.success) {
|
625
|
-
throw new
|
608
|
+
throw new import_provider9.InvalidArgumentError({
|
626
609
|
argument: "providerOptions",
|
627
610
|
message: `invalid ${provider} provider options`,
|
628
611
|
cause: parsedProviderOptions.error
|
@@ -632,7 +615,7 @@ function parseProviderOptions({
|
|
632
615
|
}
|
633
616
|
|
634
617
|
// src/post-to-api.ts
|
635
|
-
var
|
618
|
+
var import_provider10 = require("@ai-sdk/provider");
|
636
619
|
var getOriginalFetch2 = () => globalThis.fetch;
|
637
620
|
var postJsonToApi = async ({
|
638
621
|
url,
|
@@ -703,10 +686,10 @@ var postToApi = async ({
|
|
703
686
|
requestBodyValues: body.values
|
704
687
|
});
|
705
688
|
} catch (error) {
|
706
|
-
if (isAbortError(error) ||
|
689
|
+
if (isAbortError(error) || import_provider10.APICallError.isInstance(error)) {
|
707
690
|
throw error;
|
708
691
|
}
|
709
|
-
throw new
|
692
|
+
throw new import_provider10.APICallError({
|
710
693
|
message: "Failed to process error response",
|
711
694
|
cause: error,
|
712
695
|
statusCode: response.status,
|
@@ -725,11 +708,11 @@ var postToApi = async ({
|
|
725
708
|
});
|
726
709
|
} catch (error) {
|
727
710
|
if (error instanceof Error) {
|
728
|
-
if (isAbortError(error) ||
|
711
|
+
if (isAbortError(error) || import_provider10.APICallError.isInstance(error)) {
|
729
712
|
throw error;
|
730
713
|
}
|
731
714
|
}
|
732
|
-
throw new
|
715
|
+
throw new import_provider10.APICallError({
|
733
716
|
message: "Failed to process successful response",
|
734
717
|
cause: error,
|
735
718
|
statusCode: response.status,
|
@@ -739,26 +722,74 @@ var postToApi = async ({
|
|
739
722
|
});
|
740
723
|
}
|
741
724
|
} catch (error) {
|
742
|
-
|
743
|
-
throw error;
|
744
|
-
}
|
745
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
746
|
-
const cause = error.cause;
|
747
|
-
if (cause != null) {
|
748
|
-
throw new import_provider8.APICallError({
|
749
|
-
message: `Cannot connect to API: ${cause.message}`,
|
750
|
-
cause,
|
751
|
-
url,
|
752
|
-
requestBodyValues: body.values,
|
753
|
-
isRetryable: true
|
754
|
-
// retry when network error
|
755
|
-
});
|
756
|
-
}
|
757
|
-
}
|
758
|
-
throw error;
|
725
|
+
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
759
726
|
}
|
760
727
|
};
|
761
728
|
|
729
|
+
// src/types/tool.ts
|
730
|
+
function tool(tool2) {
|
731
|
+
return tool2;
|
732
|
+
}
|
733
|
+
function dynamicTool(tool2) {
|
734
|
+
return { ...tool2, type: "dynamic" };
|
735
|
+
}
|
736
|
+
|
737
|
+
// src/provider-defined-tool-factory.ts
|
738
|
+
function createProviderDefinedToolFactory({
|
739
|
+
id,
|
740
|
+
name,
|
741
|
+
inputSchema
|
742
|
+
}) {
|
743
|
+
return ({
|
744
|
+
execute,
|
745
|
+
outputSchema,
|
746
|
+
toModelOutput,
|
747
|
+
onInputStart,
|
748
|
+
onInputDelta,
|
749
|
+
onInputAvailable,
|
750
|
+
...args
|
751
|
+
}) => tool({
|
752
|
+
type: "provider-defined",
|
753
|
+
id,
|
754
|
+
name,
|
755
|
+
args,
|
756
|
+
inputSchema,
|
757
|
+
outputSchema,
|
758
|
+
execute,
|
759
|
+
toModelOutput,
|
760
|
+
onInputStart,
|
761
|
+
onInputDelta,
|
762
|
+
onInputAvailable
|
763
|
+
});
|
764
|
+
}
|
765
|
+
function createProviderDefinedToolFactoryWithOutputSchema({
|
766
|
+
id,
|
767
|
+
name,
|
768
|
+
inputSchema,
|
769
|
+
outputSchema
|
770
|
+
}) {
|
771
|
+
return ({
|
772
|
+
execute,
|
773
|
+
toModelOutput,
|
774
|
+
onInputStart,
|
775
|
+
onInputDelta,
|
776
|
+
onInputAvailable,
|
777
|
+
...args
|
778
|
+
}) => tool({
|
779
|
+
type: "provider-defined",
|
780
|
+
id,
|
781
|
+
name,
|
782
|
+
args,
|
783
|
+
inputSchema,
|
784
|
+
outputSchema,
|
785
|
+
execute,
|
786
|
+
toModelOutput,
|
787
|
+
onInputStart,
|
788
|
+
onInputDelta,
|
789
|
+
onInputAvailable
|
790
|
+
});
|
791
|
+
}
|
792
|
+
|
762
793
|
// src/resolve.ts
|
763
794
|
async function resolve(value) {
|
764
795
|
if (typeof value === "function") {
|
@@ -768,7 +799,7 @@ async function resolve(value) {
|
|
768
799
|
}
|
769
800
|
|
770
801
|
// src/response-handler.ts
|
771
|
-
var
|
802
|
+
var import_provider11 = require("@ai-sdk/provider");
|
772
803
|
var createJsonErrorResponseHandler = ({
|
773
804
|
errorSchema,
|
774
805
|
errorToMessage,
|
@@ -779,7 +810,7 @@ var createJsonErrorResponseHandler = ({
|
|
779
810
|
if (responseBody.trim() === "") {
|
780
811
|
return {
|
781
812
|
responseHeaders,
|
782
|
-
value: new
|
813
|
+
value: new import_provider11.APICallError({
|
783
814
|
message: response.statusText,
|
784
815
|
url,
|
785
816
|
requestBodyValues,
|
@@ -791,13 +822,13 @@ var createJsonErrorResponseHandler = ({
|
|
791
822
|
};
|
792
823
|
}
|
793
824
|
try {
|
794
|
-
const parsedError = parseJSON({
|
825
|
+
const parsedError = await parseJSON({
|
795
826
|
text: responseBody,
|
796
827
|
schema: errorSchema
|
797
828
|
});
|
798
829
|
return {
|
799
830
|
responseHeaders,
|
800
|
-
value: new
|
831
|
+
value: new import_provider11.APICallError({
|
801
832
|
message: errorToMessage(parsedError),
|
802
833
|
url,
|
803
834
|
requestBodyValues,
|
@@ -811,7 +842,7 @@ var createJsonErrorResponseHandler = ({
|
|
811
842
|
} catch (parseError) {
|
812
843
|
return {
|
813
844
|
responseHeaders,
|
814
|
-
value: new
|
845
|
+
value: new import_provider11.APICallError({
|
815
846
|
message: response.statusText,
|
816
847
|
url,
|
817
848
|
requestBodyValues,
|
@@ -826,41 +857,30 @@ var createJsonErrorResponseHandler = ({
|
|
826
857
|
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
|
827
858
|
const responseHeaders = extractResponseHeaders(response);
|
828
859
|
if (response.body == null) {
|
829
|
-
throw new
|
860
|
+
throw new import_provider11.EmptyResponseBodyError({});
|
830
861
|
}
|
831
862
|
return {
|
832
863
|
responseHeaders,
|
833
|
-
value:
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
return;
|
838
|
-
}
|
839
|
-
controller.enqueue(
|
840
|
-
safeParseJSON({
|
841
|
-
text: data,
|
842
|
-
schema: chunkSchema
|
843
|
-
})
|
844
|
-
);
|
845
|
-
}
|
846
|
-
})
|
847
|
-
)
|
864
|
+
value: parseJsonEventStream({
|
865
|
+
stream: response.body,
|
866
|
+
schema: chunkSchema
|
867
|
+
})
|
848
868
|
};
|
849
869
|
};
|
850
870
|
var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
851
871
|
const responseHeaders = extractResponseHeaders(response);
|
852
872
|
if (response.body == null) {
|
853
|
-
throw new
|
873
|
+
throw new import_provider11.EmptyResponseBodyError({});
|
854
874
|
}
|
855
875
|
let buffer = "";
|
856
876
|
return {
|
857
877
|
responseHeaders,
|
858
878
|
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(
|
859
879
|
new TransformStream({
|
860
|
-
transform(chunkText, controller) {
|
880
|
+
async transform(chunkText, controller) {
|
861
881
|
if (chunkText.endsWith("\n")) {
|
862
882
|
controller.enqueue(
|
863
|
-
safeParseJSON({
|
883
|
+
await safeParseJSON({
|
864
884
|
text: buffer + chunkText,
|
865
885
|
schema: chunkSchema
|
866
886
|
})
|
@@ -876,13 +896,13 @@ var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
876
896
|
};
|
877
897
|
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
878
898
|
const responseBody = await response.text();
|
879
|
-
const parsedResult = safeParseJSON({
|
899
|
+
const parsedResult = await safeParseJSON({
|
880
900
|
text: responseBody,
|
881
901
|
schema: responseSchema
|
882
902
|
});
|
883
903
|
const responseHeaders = extractResponseHeaders(response);
|
884
904
|
if (!parsedResult.success) {
|
885
|
-
throw new
|
905
|
+
throw new import_provider11.APICallError({
|
886
906
|
message: "Invalid JSON response",
|
887
907
|
cause: parsedResult.error,
|
888
908
|
statusCode: response.status,
|
@@ -901,7 +921,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
901
921
|
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
902
922
|
const responseHeaders = extractResponseHeaders(response);
|
903
923
|
if (!response.body) {
|
904
|
-
throw new
|
924
|
+
throw new import_provider11.APICallError({
|
905
925
|
message: "Response body is empty",
|
906
926
|
url,
|
907
927
|
requestBodyValues,
|
@@ -917,7 +937,7 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
917
937
|
value: new Uint8Array(buffer)
|
918
938
|
};
|
919
939
|
} catch (error) {
|
920
|
-
throw new
|
940
|
+
throw new import_provider11.APICallError({
|
921
941
|
message: "Failed to read response as array buffer",
|
922
942
|
url,
|
923
943
|
requestBodyValues,
|
@@ -933,7 +953,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
933
953
|
const responseBody = await response.text();
|
934
954
|
return {
|
935
955
|
responseHeaders,
|
936
|
-
value: new
|
956
|
+
value: new import_provider11.APICallError({
|
937
957
|
message: response.statusText,
|
938
958
|
url,
|
939
959
|
requestBodyValues,
|
@@ -944,6 +964,76 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
944
964
|
};
|
945
965
|
};
|
946
966
|
|
967
|
+
// src/zod-schema.ts
|
968
|
+
var z4 = __toESM(require("zod/v4"));
|
969
|
+
var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
|
970
|
+
function zod3Schema(zodSchema2, options) {
|
971
|
+
var _a;
|
972
|
+
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
973
|
+
return jsonSchema(
|
974
|
+
(0, import_zod_to_json_schema.default)(zodSchema2, {
|
975
|
+
$refStrategy: useReferences ? "root" : "none",
|
976
|
+
target: "jsonSchema7"
|
977
|
+
// note: openai mode breaks various gemini conversions
|
978
|
+
}),
|
979
|
+
{
|
980
|
+
validate: async (value) => {
|
981
|
+
const result = await zodSchema2.safeParseAsync(value);
|
982
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
983
|
+
}
|
984
|
+
}
|
985
|
+
);
|
986
|
+
}
|
987
|
+
function zod4Schema(zodSchema2, options) {
|
988
|
+
var _a;
|
989
|
+
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
990
|
+
const z4JSONSchema = z4.toJSONSchema(zodSchema2, {
|
991
|
+
target: "draft-7",
|
992
|
+
io: "output",
|
993
|
+
reused: useReferences ? "ref" : "inline"
|
994
|
+
});
|
995
|
+
return jsonSchema(z4JSONSchema, {
|
996
|
+
validate: async (value) => {
|
997
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
998
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
999
|
+
}
|
1000
|
+
});
|
1001
|
+
}
|
1002
|
+
function isZod4Schema(zodSchema2) {
|
1003
|
+
return "_zod" in zodSchema2;
|
1004
|
+
}
|
1005
|
+
function zodSchema(zodSchema2, options) {
|
1006
|
+
if (isZod4Schema(zodSchema2)) {
|
1007
|
+
return zod4Schema(zodSchema2, options);
|
1008
|
+
} else {
|
1009
|
+
return zod3Schema(zodSchema2, options);
|
1010
|
+
}
|
1011
|
+
}
|
1012
|
+
|
1013
|
+
// src/schema.ts
|
1014
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
1015
|
+
function jsonSchema(jsonSchema2, {
|
1016
|
+
validate
|
1017
|
+
} = {}) {
|
1018
|
+
return {
|
1019
|
+
[schemaSymbol]: true,
|
1020
|
+
_type: void 0,
|
1021
|
+
// should never be used directly
|
1022
|
+
[validatorSymbol]: true,
|
1023
|
+
jsonSchema: jsonSchema2,
|
1024
|
+
validate
|
1025
|
+
};
|
1026
|
+
}
|
1027
|
+
function isSchema(value) {
|
1028
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
1029
|
+
}
|
1030
|
+
function asSchema(schema) {
|
1031
|
+
return schema == null ? jsonSchema({
|
1032
|
+
properties: {},
|
1033
|
+
additionalProperties: false
|
1034
|
+
}) : isSchema(schema) ? schema : zodSchema(schema);
|
1035
|
+
}
|
1036
|
+
|
947
1037
|
// src/uint8-utils.ts
|
948
1038
|
var { btoa, atob } = globalThis;
|
949
1039
|
function convertBase64ToUint8Array(base64String) {
|
@@ -966,8 +1056,14 @@ function convertToBase64(value) {
|
|
966
1056
|
function withoutTrailingSlash(url) {
|
967
1057
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
968
1058
|
}
|
1059
|
+
|
1060
|
+
// src/index.ts
|
1061
|
+
__reExport(src_exports, require("@standard-schema/spec"), module.exports);
|
1062
|
+
var import_stream2 = require("eventsource-parser/stream");
|
969
1063
|
// Annotate the CommonJS export names for ESM import in node:
|
970
1064
|
0 && (module.exports = {
|
1065
|
+
EventSourceParserStream,
|
1066
|
+
asSchema,
|
971
1067
|
asValidator,
|
972
1068
|
combineHeaders,
|
973
1069
|
convertAsyncIteratorToReadableStream,
|
@@ -975,14 +1071,16 @@ function withoutTrailingSlash(url) {
|
|
975
1071
|
convertToBase64,
|
976
1072
|
convertUint8ArrayToBase64,
|
977
1073
|
createBinaryResponseHandler,
|
978
|
-
createEventSourceParserStream,
|
979
1074
|
createEventSourceResponseHandler,
|
980
1075
|
createIdGenerator,
|
981
1076
|
createJsonErrorResponseHandler,
|
982
1077
|
createJsonResponseHandler,
|
983
1078
|
createJsonStreamResponseHandler,
|
1079
|
+
createProviderDefinedToolFactory,
|
1080
|
+
createProviderDefinedToolFactoryWithOutputSchema,
|
984
1081
|
createStatusCodeErrorResponseHandler,
|
985
1082
|
delay,
|
1083
|
+
dynamicTool,
|
986
1084
|
extractResponseHeaders,
|
987
1085
|
generateId,
|
988
1086
|
getErrorMessage,
|
@@ -991,10 +1089,12 @@ function withoutTrailingSlash(url) {
|
|
991
1089
|
isParsableJson,
|
992
1090
|
isUrlSupported,
|
993
1091
|
isValidator,
|
1092
|
+
jsonSchema,
|
994
1093
|
loadApiKey,
|
995
1094
|
loadOptionalSetting,
|
996
1095
|
loadSetting,
|
997
1096
|
parseJSON,
|
1097
|
+
parseJsonEventStream,
|
998
1098
|
parseProviderOptions,
|
999
1099
|
postFormDataToApi,
|
1000
1100
|
postJsonToApi,
|
@@ -1003,10 +1103,13 @@ function withoutTrailingSlash(url) {
|
|
1003
1103
|
resolve,
|
1004
1104
|
safeParseJSON,
|
1005
1105
|
safeValidateTypes,
|
1106
|
+
standardSchemaValidator,
|
1107
|
+
tool,
|
1006
1108
|
validateTypes,
|
1007
1109
|
validator,
|
1008
1110
|
validatorSymbol,
|
1009
1111
|
withoutTrailingSlash,
|
1010
|
-
|
1112
|
+
zodSchema,
|
1113
|
+
...require("@standard-schema/spec")
|
1011
1114
|
});
|
1012
1115
|
//# sourceMappingURL=index.js.map
|