@ai-sdk/provider-utils 3.0.0-beta.1 → 3.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/index.d.mts +57 -57
- package/dist/index.d.ts +57 -57
- package/dist/index.js +82 -81
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +58 -57
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
@@ -175,8 +175,42 @@ function getErrorMessage(error) {
|
|
175
175
|
}
|
176
176
|
|
177
177
|
// src/get-from-api.ts
|
178
|
+
var import_provider3 = require("@ai-sdk/provider");
|
179
|
+
|
180
|
+
// src/handle-fetch-error.ts
|
178
181
|
var import_provider2 = require("@ai-sdk/provider");
|
179
182
|
|
183
|
+
// src/is-abort-error.ts
|
184
|
+
function isAbortError(error) {
|
185
|
+
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
186
|
+
}
|
187
|
+
|
188
|
+
// src/handle-fetch-error.ts
|
189
|
+
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
190
|
+
function handleFetchError({
|
191
|
+
error,
|
192
|
+
url,
|
193
|
+
requestBodyValues
|
194
|
+
}) {
|
195
|
+
if (isAbortError(error)) {
|
196
|
+
return error;
|
197
|
+
}
|
198
|
+
if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
|
199
|
+
const cause = error.cause;
|
200
|
+
if (cause != null) {
|
201
|
+
return new import_provider2.APICallError({
|
202
|
+
message: `Cannot connect to API: ${cause.message}`,
|
203
|
+
cause,
|
204
|
+
url,
|
205
|
+
requestBodyValues,
|
206
|
+
isRetryable: true
|
207
|
+
// retry when network error
|
208
|
+
});
|
209
|
+
}
|
210
|
+
}
|
211
|
+
return error;
|
212
|
+
}
|
213
|
+
|
180
214
|
// src/remove-undefined-entries.ts
|
181
215
|
function removeUndefinedEntries(record) {
|
182
216
|
return Object.fromEntries(
|
@@ -184,11 +218,6 @@ function removeUndefinedEntries(record) {
|
|
184
218
|
);
|
185
219
|
}
|
186
220
|
|
187
|
-
// src/is-abort-error.ts
|
188
|
-
function isAbortError(error) {
|
189
|
-
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
190
|
-
}
|
191
|
-
|
192
221
|
// src/get-from-api.ts
|
193
222
|
var getOriginalFetch = () => globalThis.fetch;
|
194
223
|
var getFromApi = async ({
|
@@ -215,10 +244,10 @@ var getFromApi = async ({
|
|
215
244
|
requestBodyValues: {}
|
216
245
|
});
|
217
246
|
} catch (error) {
|
218
|
-
if (isAbortError(error) ||
|
247
|
+
if (isAbortError(error) || import_provider3.APICallError.isInstance(error)) {
|
219
248
|
throw error;
|
220
249
|
}
|
221
|
-
throw new
|
250
|
+
throw new import_provider3.APICallError({
|
222
251
|
message: "Failed to process error response",
|
223
252
|
cause: error,
|
224
253
|
statusCode: response.status,
|
@@ -237,11 +266,11 @@ var getFromApi = async ({
|
|
237
266
|
});
|
238
267
|
} catch (error) {
|
239
268
|
if (error instanceof Error) {
|
240
|
-
if (isAbortError(error) ||
|
269
|
+
if (isAbortError(error) || import_provider3.APICallError.isInstance(error)) {
|
241
270
|
throw error;
|
242
271
|
}
|
243
272
|
}
|
244
|
-
throw new
|
273
|
+
throw new import_provider3.APICallError({
|
245
274
|
message: "Failed to process successful response",
|
246
275
|
cause: error,
|
247
276
|
statusCode: response.status,
|
@@ -251,22 +280,7 @@ var getFromApi = async ({
|
|
251
280
|
});
|
252
281
|
}
|
253
282
|
} catch (error) {
|
254
|
-
|
255
|
-
throw error;
|
256
|
-
}
|
257
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
258
|
-
const cause = error.cause;
|
259
|
-
if (cause != null) {
|
260
|
-
throw new import_provider2.APICallError({
|
261
|
-
message: `Cannot connect to API: ${cause.message}`,
|
262
|
-
cause,
|
263
|
-
url,
|
264
|
-
isRetryable: true,
|
265
|
-
requestBodyValues: {}
|
266
|
-
});
|
267
|
-
}
|
268
|
-
}
|
269
|
-
throw error;
|
283
|
+
throw handleFetchError({ error, url, requestBodyValues: {} });
|
270
284
|
}
|
271
285
|
};
|
272
286
|
|
@@ -285,7 +299,7 @@ function isUrlSupported({
|
|
285
299
|
}
|
286
300
|
|
287
301
|
// src/load-api-key.ts
|
288
|
-
var
|
302
|
+
var import_provider4 = require("@ai-sdk/provider");
|
289
303
|
function loadApiKey({
|
290
304
|
apiKey,
|
291
305
|
environmentVariableName,
|
@@ -296,23 +310,23 @@ function loadApiKey({
|
|
296
310
|
return apiKey;
|
297
311
|
}
|
298
312
|
if (apiKey != null) {
|
299
|
-
throw new
|
313
|
+
throw new import_provider4.LoadAPIKeyError({
|
300
314
|
message: `${description} API key must be a string.`
|
301
315
|
});
|
302
316
|
}
|
303
317
|
if (typeof process === "undefined") {
|
304
|
-
throw new
|
318
|
+
throw new import_provider4.LoadAPIKeyError({
|
305
319
|
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
306
320
|
});
|
307
321
|
}
|
308
322
|
apiKey = process.env[environmentVariableName];
|
309
323
|
if (apiKey == null) {
|
310
|
-
throw new
|
324
|
+
throw new import_provider4.LoadAPIKeyError({
|
311
325
|
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
312
326
|
});
|
313
327
|
}
|
314
328
|
if (typeof apiKey !== "string") {
|
315
|
-
throw new
|
329
|
+
throw new import_provider4.LoadAPIKeyError({
|
316
330
|
message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
317
331
|
});
|
318
332
|
}
|
@@ -338,7 +352,7 @@ function loadOptionalSetting({
|
|
338
352
|
}
|
339
353
|
|
340
354
|
// src/load-setting.ts
|
341
|
-
var
|
355
|
+
var import_provider5 = require("@ai-sdk/provider");
|
342
356
|
function loadSetting({
|
343
357
|
settingValue,
|
344
358
|
environmentVariableName,
|
@@ -349,23 +363,23 @@ function loadSetting({
|
|
349
363
|
return settingValue;
|
350
364
|
}
|
351
365
|
if (settingValue != null) {
|
352
|
-
throw new
|
366
|
+
throw new import_provider5.LoadSettingError({
|
353
367
|
message: `${description} setting must be a string.`
|
354
368
|
});
|
355
369
|
}
|
356
370
|
if (typeof process === "undefined") {
|
357
|
-
throw new
|
371
|
+
throw new import_provider5.LoadSettingError({
|
358
372
|
message: `${description} setting is missing. Pass it using the '${settingName}' parameter. Environment variables is not supported in this environment.`
|
359
373
|
});
|
360
374
|
}
|
361
375
|
settingValue = process.env[environmentVariableName];
|
362
376
|
if (settingValue == null) {
|
363
|
-
throw new
|
377
|
+
throw new import_provider5.LoadSettingError({
|
364
378
|
message: `${description} setting is missing. Pass it using the '${settingName}' parameter or the ${environmentVariableName} environment variable.`
|
365
379
|
});
|
366
380
|
}
|
367
381
|
if (typeof settingValue !== "string") {
|
368
|
-
throw new
|
382
|
+
throw new import_provider5.LoadSettingError({
|
369
383
|
message: `${description} setting must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
370
384
|
});
|
371
385
|
}
|
@@ -373,7 +387,7 @@ function loadSetting({
|
|
373
387
|
}
|
374
388
|
|
375
389
|
// src/parse-json.ts
|
376
|
-
var
|
390
|
+
var import_provider8 = require("@ai-sdk/provider");
|
377
391
|
|
378
392
|
// src/secure-json-parse.ts
|
379
393
|
var suspectProtoRx = /"__proto__"\s*:/;
|
@@ -421,10 +435,10 @@ function secureJsonParse(text) {
|
|
421
435
|
}
|
422
436
|
|
423
437
|
// src/validate-types.ts
|
424
|
-
var
|
438
|
+
var import_provider7 = require("@ai-sdk/provider");
|
425
439
|
|
426
440
|
// src/validator.ts
|
427
|
-
var
|
441
|
+
var import_provider6 = require("@ai-sdk/provider");
|
428
442
|
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
429
443
|
function validator(validate) {
|
430
444
|
return { [validatorSymbol]: true, validate };
|
@@ -440,7 +454,7 @@ function standardSchemaValidator(standardSchema) {
|
|
440
454
|
const result = await standardSchema["~standard"].validate(value);
|
441
455
|
return result.issues == null ? { success: true, value: result.value } : {
|
442
456
|
success: false,
|
443
|
-
error: new
|
457
|
+
error: new import_provider6.TypeValidationError({
|
444
458
|
value,
|
445
459
|
cause: result.issues
|
446
460
|
})
|
@@ -455,7 +469,7 @@ async function validateTypes({
|
|
455
469
|
}) {
|
456
470
|
const result = await safeValidateTypes({ value, schema });
|
457
471
|
if (!result.success) {
|
458
|
-
throw
|
472
|
+
throw import_provider7.TypeValidationError.wrap({ value, cause: result.error });
|
459
473
|
}
|
460
474
|
return result.value;
|
461
475
|
}
|
@@ -474,20 +488,23 @@ async function safeValidateTypes({
|
|
474
488
|
}
|
475
489
|
return {
|
476
490
|
success: false,
|
477
|
-
error:
|
491
|
+
error: import_provider7.TypeValidationError.wrap({ value, cause: result.error }),
|
478
492
|
rawValue: value
|
479
493
|
};
|
480
494
|
} catch (error) {
|
481
495
|
return {
|
482
496
|
success: false,
|
483
|
-
error:
|
497
|
+
error: import_provider7.TypeValidationError.wrap({ value, cause: error }),
|
484
498
|
rawValue: value
|
485
499
|
};
|
486
500
|
}
|
487
501
|
}
|
488
502
|
|
489
503
|
// src/parse-json.ts
|
490
|
-
async function parseJSON({
|
504
|
+
async function parseJSON({
|
505
|
+
text,
|
506
|
+
schema
|
507
|
+
}) {
|
491
508
|
try {
|
492
509
|
const value = secureJsonParse(text);
|
493
510
|
if (schema == null) {
|
@@ -495,10 +512,10 @@ async function parseJSON({ text, schema }) {
|
|
495
512
|
}
|
496
513
|
return validateTypes({ value, schema });
|
497
514
|
} catch (error) {
|
498
|
-
if (
|
515
|
+
if (import_provider8.JSONParseError.isInstance(error) || import_provider8.TypeValidationError.isInstance(error)) {
|
499
516
|
throw error;
|
500
517
|
}
|
501
|
-
throw new
|
518
|
+
throw new import_provider8.JSONParseError({ text, cause: error });
|
502
519
|
}
|
503
520
|
}
|
504
521
|
async function safeParseJSON({
|
@@ -514,7 +531,7 @@ async function safeParseJSON({
|
|
514
531
|
} catch (error) {
|
515
532
|
return {
|
516
533
|
success: false,
|
517
|
-
error:
|
534
|
+
error: import_provider8.JSONParseError.isInstance(error) ? error : new import_provider8.JSONParseError({ text, cause: error }),
|
518
535
|
rawValue: void 0
|
519
536
|
};
|
520
537
|
}
|
@@ -547,7 +564,7 @@ function parseJsonEventStream({
|
|
547
564
|
}
|
548
565
|
|
549
566
|
// src/parse-provider-options.ts
|
550
|
-
var
|
567
|
+
var import_provider9 = require("@ai-sdk/provider");
|
551
568
|
async function parseProviderOptions({
|
552
569
|
provider,
|
553
570
|
providerOptions,
|
@@ -561,7 +578,7 @@ async function parseProviderOptions({
|
|
561
578
|
schema
|
562
579
|
});
|
563
580
|
if (!parsedProviderOptions.success) {
|
564
|
-
throw new
|
581
|
+
throw new import_provider9.InvalidArgumentError({
|
565
582
|
argument: "providerOptions",
|
566
583
|
message: `invalid ${provider} provider options`,
|
567
584
|
cause: parsedProviderOptions.error
|
@@ -571,7 +588,7 @@ async function parseProviderOptions({
|
|
571
588
|
}
|
572
589
|
|
573
590
|
// src/post-to-api.ts
|
574
|
-
var
|
591
|
+
var import_provider10 = require("@ai-sdk/provider");
|
575
592
|
var getOriginalFetch2 = () => globalThis.fetch;
|
576
593
|
var postJsonToApi = async ({
|
577
594
|
url,
|
@@ -642,10 +659,10 @@ var postToApi = async ({
|
|
642
659
|
requestBodyValues: body.values
|
643
660
|
});
|
644
661
|
} catch (error) {
|
645
|
-
if (isAbortError(error) ||
|
662
|
+
if (isAbortError(error) || import_provider10.APICallError.isInstance(error)) {
|
646
663
|
throw error;
|
647
664
|
}
|
648
|
-
throw new
|
665
|
+
throw new import_provider10.APICallError({
|
649
666
|
message: "Failed to process error response",
|
650
667
|
cause: error,
|
651
668
|
statusCode: response.status,
|
@@ -664,11 +681,11 @@ var postToApi = async ({
|
|
664
681
|
});
|
665
682
|
} catch (error) {
|
666
683
|
if (error instanceof Error) {
|
667
|
-
if (isAbortError(error) ||
|
684
|
+
if (isAbortError(error) || import_provider10.APICallError.isInstance(error)) {
|
668
685
|
throw error;
|
669
686
|
}
|
670
687
|
}
|
671
|
-
throw new
|
688
|
+
throw new import_provider10.APICallError({
|
672
689
|
message: "Failed to process successful response",
|
673
690
|
cause: error,
|
674
691
|
statusCode: response.status,
|
@@ -678,23 +695,7 @@ var postToApi = async ({
|
|
678
695
|
});
|
679
696
|
}
|
680
697
|
} catch (error) {
|
681
|
-
|
682
|
-
throw error;
|
683
|
-
}
|
684
|
-
if (error instanceof TypeError && error.message === "fetch failed") {
|
685
|
-
const cause = error.cause;
|
686
|
-
if (cause != null) {
|
687
|
-
throw new import_provider9.APICallError({
|
688
|
-
message: `Cannot connect to API: ${cause.message}`,
|
689
|
-
cause,
|
690
|
-
url,
|
691
|
-
requestBodyValues: body.values,
|
692
|
-
isRetryable: true
|
693
|
-
// retry when network error
|
694
|
-
});
|
695
|
-
}
|
696
|
-
}
|
697
|
-
throw error;
|
698
|
+
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
698
699
|
}
|
699
700
|
};
|
700
701
|
|
@@ -768,7 +769,7 @@ async function resolve(value) {
|
|
768
769
|
}
|
769
770
|
|
770
771
|
// src/response-handler.ts
|
771
|
-
var
|
772
|
+
var import_provider11 = require("@ai-sdk/provider");
|
772
773
|
var createJsonErrorResponseHandler = ({
|
773
774
|
errorSchema,
|
774
775
|
errorToMessage,
|
@@ -779,7 +780,7 @@ var createJsonErrorResponseHandler = ({
|
|
779
780
|
if (responseBody.trim() === "") {
|
780
781
|
return {
|
781
782
|
responseHeaders,
|
782
|
-
value: new
|
783
|
+
value: new import_provider11.APICallError({
|
783
784
|
message: response.statusText,
|
784
785
|
url,
|
785
786
|
requestBodyValues,
|
@@ -797,7 +798,7 @@ var createJsonErrorResponseHandler = ({
|
|
797
798
|
});
|
798
799
|
return {
|
799
800
|
responseHeaders,
|
800
|
-
value: new
|
801
|
+
value: new import_provider11.APICallError({
|
801
802
|
message: errorToMessage(parsedError),
|
802
803
|
url,
|
803
804
|
requestBodyValues,
|
@@ -811,7 +812,7 @@ var createJsonErrorResponseHandler = ({
|
|
811
812
|
} catch (parseError) {
|
812
813
|
return {
|
813
814
|
responseHeaders,
|
814
|
-
value: new
|
815
|
+
value: new import_provider11.APICallError({
|
815
816
|
message: response.statusText,
|
816
817
|
url,
|
817
818
|
requestBodyValues,
|
@@ -826,7 +827,7 @@ var createJsonErrorResponseHandler = ({
|
|
826
827
|
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
|
827
828
|
const responseHeaders = extractResponseHeaders(response);
|
828
829
|
if (response.body == null) {
|
829
|
-
throw new
|
830
|
+
throw new import_provider11.EmptyResponseBodyError({});
|
830
831
|
}
|
831
832
|
return {
|
832
833
|
responseHeaders,
|
@@ -839,7 +840,7 @@ var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) =>
|
|
839
840
|
var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
840
841
|
const responseHeaders = extractResponseHeaders(response);
|
841
842
|
if (response.body == null) {
|
842
|
-
throw new
|
843
|
+
throw new import_provider11.EmptyResponseBodyError({});
|
843
844
|
}
|
844
845
|
let buffer = "";
|
845
846
|
return {
|
@@ -871,7 +872,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
871
872
|
});
|
872
873
|
const responseHeaders = extractResponseHeaders(response);
|
873
874
|
if (!parsedResult.success) {
|
874
|
-
throw new
|
875
|
+
throw new import_provider11.APICallError({
|
875
876
|
message: "Invalid JSON response",
|
876
877
|
cause: parsedResult.error,
|
877
878
|
statusCode: response.status,
|
@@ -890,7 +891,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
890
891
|
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
891
892
|
const responseHeaders = extractResponseHeaders(response);
|
892
893
|
if (!response.body) {
|
893
|
-
throw new
|
894
|
+
throw new import_provider11.APICallError({
|
894
895
|
message: "Response body is empty",
|
895
896
|
url,
|
896
897
|
requestBodyValues,
|
@@ -906,7 +907,7 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
906
907
|
value: new Uint8Array(buffer)
|
907
908
|
};
|
908
909
|
} catch (error) {
|
909
|
-
throw new
|
910
|
+
throw new import_provider11.APICallError({
|
910
911
|
message: "Failed to read response as array buffer",
|
911
912
|
url,
|
912
913
|
requestBodyValues,
|
@@ -922,7 +923,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
922
923
|
const responseBody = await response.text();
|
923
924
|
return {
|
924
925
|
responseHeaders,
|
925
|
-
value: new
|
926
|
+
value: new import_provider11.APICallError({
|
926
927
|
message: response.statusText,
|
927
928
|
url,
|
928
929
|
requestBodyValues,
|
@@ -934,7 +935,7 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
934
935
|
};
|
935
936
|
|
936
937
|
// src/zod-schema.ts
|
937
|
-
var z4 = __toESM(require("zod/v4
|
938
|
+
var z4 = __toESM(require("zod/v4"));
|
938
939
|
var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
|
939
940
|
function zod3Schema(zodSchema2, options) {
|
940
941
|
var _a;
|