@ai-sdk/provider-utils 5.0.33 → 5.0.35
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 +15 -0
- package/dist/index.d.ts +220 -151
- package/dist/index.js +447 -172
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/convert-inline-file-data-to-uint8-array.ts +13 -1
- package/src/delete-from-api.ts +102 -0
- package/src/index.ts +2 -0
- package/src/post-multipart-stream-to-api.ts +268 -0
- package/src/response-handler.ts +26 -0
package/dist/index.js
CHANGED
|
@@ -204,6 +204,9 @@ function convertAsyncIteratorToReadableStream(iterator) {
|
|
|
204
204
|
});
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
+
// src/convert-inline-file-data-to-uint8-array.ts
|
|
208
|
+
import { UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
209
|
+
|
|
207
210
|
// src/uint8-utils.ts
|
|
208
211
|
var { btoa, atob } = globalThis;
|
|
209
212
|
function convertBase64ToUint8Array(base64String) {
|
|
@@ -224,6 +227,14 @@ function convertToBase64(value) {
|
|
|
224
227
|
|
|
225
228
|
// src/convert-inline-file-data-to-uint8-array.ts
|
|
226
229
|
function convertInlineFileDataToUint8Array(data) {
|
|
230
|
+
if (data.type === "stream") {
|
|
231
|
+
const error = new UnsupportedFunctionalityError({
|
|
232
|
+
functionality: "streaming file upload"
|
|
233
|
+
});
|
|
234
|
+
void data.stream.cancel(error).catch(() => {
|
|
235
|
+
});
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
227
238
|
if (data.type === "text") {
|
|
228
239
|
return new TextEncoder().encode(data.text);
|
|
229
240
|
}
|
|
@@ -395,6 +406,223 @@ var DelayedPromise = class {
|
|
|
395
406
|
}
|
|
396
407
|
};
|
|
397
408
|
|
|
409
|
+
// src/delete-from-api.ts
|
|
410
|
+
import { APICallError as APICallError2 } from "@ai-sdk/provider";
|
|
411
|
+
|
|
412
|
+
// src/extract-response-headers.ts
|
|
413
|
+
function extractResponseHeaders(response) {
|
|
414
|
+
return Object.fromEntries([...response.headers]);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// src/get-runtime-environment-user-agent.ts
|
|
418
|
+
function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
|
|
419
|
+
var _a3, _b3, _c;
|
|
420
|
+
if (globalThisAny.window) {
|
|
421
|
+
return `runtime/browser`;
|
|
422
|
+
}
|
|
423
|
+
if ((_a3 = globalThisAny.navigator) == null ? void 0 : _a3.userAgent) {
|
|
424
|
+
return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
|
|
425
|
+
}
|
|
426
|
+
if ((_c = (_b3 = globalThisAny.process) == null ? void 0 : _b3.versions) == null ? void 0 : _c.node) {
|
|
427
|
+
return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
|
|
428
|
+
}
|
|
429
|
+
if (globalThisAny.EdgeRuntime) {
|
|
430
|
+
return `runtime/vercel-edge`;
|
|
431
|
+
}
|
|
432
|
+
return "runtime/unknown";
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// src/handle-fetch-error.ts
|
|
436
|
+
import { APICallError } from "@ai-sdk/provider";
|
|
437
|
+
|
|
438
|
+
// src/is-abort-error.ts
|
|
439
|
+
function isAbortError(error) {
|
|
440
|
+
return (error instanceof Error || error instanceof DOMException) && (error.name === "AbortError" || error.name === "ResponseAborted" || // Next.js
|
|
441
|
+
error.name === "TimeoutError");
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// src/handle-fetch-error.ts
|
|
445
|
+
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
|
446
|
+
var RETRYABLE_NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
447
|
+
"ConnectionRefused",
|
|
448
|
+
"ConnectionClosed",
|
|
449
|
+
"FailedToOpenSocket",
|
|
450
|
+
"ECONNRESET",
|
|
451
|
+
"ECONNREFUSED",
|
|
452
|
+
"ETIMEDOUT",
|
|
453
|
+
"EPIPE",
|
|
454
|
+
"UND_ERR_SOCKET",
|
|
455
|
+
"UND_ERR_HEADERS_TIMEOUT",
|
|
456
|
+
"UND_ERR_BODY_TIMEOUT",
|
|
457
|
+
"UND_ERR_CONNECT_TIMEOUT"
|
|
458
|
+
]);
|
|
459
|
+
function findNetworkError(error) {
|
|
460
|
+
const visited = /* @__PURE__ */ new Set();
|
|
461
|
+
let current = error;
|
|
462
|
+
while (current instanceof Error && !visited.has(current)) {
|
|
463
|
+
visited.add(current);
|
|
464
|
+
const errorWithCode = current;
|
|
465
|
+
if (typeof errorWithCode.code === "string" && RETRYABLE_NETWORK_ERROR_CODES.has(errorWithCode.code)) {
|
|
466
|
+
return errorWithCode;
|
|
467
|
+
}
|
|
468
|
+
current = current.cause;
|
|
469
|
+
}
|
|
470
|
+
return void 0;
|
|
471
|
+
}
|
|
472
|
+
function handleFetchError({
|
|
473
|
+
error,
|
|
474
|
+
url,
|
|
475
|
+
requestBodyValues
|
|
476
|
+
}) {
|
|
477
|
+
if (isAbortError(error)) {
|
|
478
|
+
return error;
|
|
479
|
+
}
|
|
480
|
+
if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
|
|
481
|
+
const cause = error.cause;
|
|
482
|
+
if (cause != null) {
|
|
483
|
+
return new APICallError({
|
|
484
|
+
message: `Cannot connect to API: ${cause.message}`,
|
|
485
|
+
cause,
|
|
486
|
+
url,
|
|
487
|
+
requestBodyValues,
|
|
488
|
+
isRetryable: true
|
|
489
|
+
// retry when network error
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
const networkError = findNetworkError(error);
|
|
494
|
+
if (networkError != null) {
|
|
495
|
+
if (APICallError.isInstance(error)) {
|
|
496
|
+
return new APICallError({
|
|
497
|
+
message: error.message,
|
|
498
|
+
cause: error.cause,
|
|
499
|
+
url: error.url,
|
|
500
|
+
requestBodyValues: error.requestBodyValues,
|
|
501
|
+
statusCode: error.statusCode,
|
|
502
|
+
responseHeaders: error.responseHeaders,
|
|
503
|
+
responseBody: error.responseBody,
|
|
504
|
+
data: error.data,
|
|
505
|
+
isRetryable: true
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return new APICallError({
|
|
509
|
+
message: `Cannot connect to API: ${error instanceof Error ? error.message : networkError.message}`,
|
|
510
|
+
cause: error,
|
|
511
|
+
url,
|
|
512
|
+
requestBodyValues,
|
|
513
|
+
isRetryable: true
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
return error;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// src/version.ts
|
|
520
|
+
var VERSION = true ? "5.0.35" : "0.0.0-test";
|
|
521
|
+
|
|
522
|
+
// src/normalize-headers.ts
|
|
523
|
+
function normalizeHeaders(headers) {
|
|
524
|
+
if (headers == null) {
|
|
525
|
+
return {};
|
|
526
|
+
}
|
|
527
|
+
const normalized = {};
|
|
528
|
+
if (headers instanceof Headers) {
|
|
529
|
+
headers.forEach((value, key) => {
|
|
530
|
+
normalized[key.toLowerCase()] = value;
|
|
531
|
+
});
|
|
532
|
+
} else {
|
|
533
|
+
if (!Array.isArray(headers)) {
|
|
534
|
+
headers = Object.entries(headers);
|
|
535
|
+
}
|
|
536
|
+
for (const [key, value] of headers) {
|
|
537
|
+
if (value != null) {
|
|
538
|
+
normalized[key.toLowerCase()] = value;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return normalized;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// src/with-user-agent-suffix.ts
|
|
546
|
+
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
547
|
+
const normalizedHeaders = new Headers(normalizeHeaders(headers));
|
|
548
|
+
const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
|
|
549
|
+
normalizedHeaders.set(
|
|
550
|
+
"user-agent",
|
|
551
|
+
[currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
|
|
552
|
+
);
|
|
553
|
+
return Object.fromEntries(normalizedHeaders.entries());
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// src/delete-from-api.ts
|
|
557
|
+
var getOriginalFetch = () => globalThis.fetch;
|
|
558
|
+
var deleteFromApi = async ({
|
|
559
|
+
url,
|
|
560
|
+
headers = {},
|
|
561
|
+
failedResponseHandler,
|
|
562
|
+
successfulResponseHandler,
|
|
563
|
+
abortSignal,
|
|
564
|
+
fetch = getOriginalFetch()
|
|
565
|
+
}) => {
|
|
566
|
+
try {
|
|
567
|
+
const response = await fetch(url, {
|
|
568
|
+
method: "DELETE",
|
|
569
|
+
headers: withUserAgentSuffix(
|
|
570
|
+
headers,
|
|
571
|
+
`ai-sdk/provider-utils/${VERSION}`,
|
|
572
|
+
getRuntimeEnvironmentUserAgent()
|
|
573
|
+
),
|
|
574
|
+
signal: abortSignal
|
|
575
|
+
});
|
|
576
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
577
|
+
if (!response.ok) {
|
|
578
|
+
let errorInformation;
|
|
579
|
+
try {
|
|
580
|
+
errorInformation = await failedResponseHandler({
|
|
581
|
+
response,
|
|
582
|
+
url,
|
|
583
|
+
requestBodyValues: {}
|
|
584
|
+
});
|
|
585
|
+
} catch (error) {
|
|
586
|
+
if (isAbortError(error) || APICallError2.isInstance(error)) {
|
|
587
|
+
throw error;
|
|
588
|
+
}
|
|
589
|
+
throw new APICallError2({
|
|
590
|
+
message: "Failed to process error response",
|
|
591
|
+
cause: error,
|
|
592
|
+
statusCode: response.status,
|
|
593
|
+
url,
|
|
594
|
+
responseHeaders,
|
|
595
|
+
requestBodyValues: {}
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
throw errorInformation.value;
|
|
599
|
+
}
|
|
600
|
+
try {
|
|
601
|
+
return await successfulResponseHandler({
|
|
602
|
+
response,
|
|
603
|
+
url,
|
|
604
|
+
requestBodyValues: {}
|
|
605
|
+
});
|
|
606
|
+
} catch (error) {
|
|
607
|
+
if (error instanceof Error) {
|
|
608
|
+
if (isAbortError(error) || APICallError2.isInstance(error)) {
|
|
609
|
+
throw error;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
throw new APICallError2({
|
|
613
|
+
message: "Failed to process successful response",
|
|
614
|
+
cause: error,
|
|
615
|
+
statusCode: response.status,
|
|
616
|
+
url,
|
|
617
|
+
responseHeaders,
|
|
618
|
+
requestBodyValues: {}
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
} catch (error) {
|
|
622
|
+
throw handleFetchError({ error, url, requestBodyValues: {} });
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
|
|
398
626
|
// src/detect-media-type.ts
|
|
399
627
|
var imageMediaTypeSignatures = [
|
|
400
628
|
{
|
|
@@ -1198,11 +1426,6 @@ function extractLines({
|
|
|
1198
1426
|
return lines.slice(start, end).join(lineEnding);
|
|
1199
1427
|
}
|
|
1200
1428
|
|
|
1201
|
-
// src/extract-response-headers.ts
|
|
1202
|
-
function extractResponseHeaders(response) {
|
|
1203
|
-
return Object.fromEntries([...response.headers]);
|
|
1204
|
-
}
|
|
1205
|
-
|
|
1206
1429
|
// src/filter-nullable.ts
|
|
1207
1430
|
function filterNullable(...values) {
|
|
1208
1431
|
return values.filter((value) => value != null);
|
|
@@ -1241,149 +1464,8 @@ var generateId = createIdGenerator();
|
|
|
1241
1464
|
import { getErrorMessage } from "@ai-sdk/provider";
|
|
1242
1465
|
|
|
1243
1466
|
// src/get-from-api.ts
|
|
1244
|
-
import { APICallError as
|
|
1245
|
-
|
|
1246
|
-
// src/handle-fetch-error.ts
|
|
1247
|
-
import { APICallError } from "@ai-sdk/provider";
|
|
1248
|
-
|
|
1249
|
-
// src/is-abort-error.ts
|
|
1250
|
-
function isAbortError(error) {
|
|
1251
|
-
return (error instanceof Error || error instanceof DOMException) && (error.name === "AbortError" || error.name === "ResponseAborted" || // Next.js
|
|
1252
|
-
error.name === "TimeoutError");
|
|
1253
|
-
}
|
|
1254
|
-
|
|
1255
|
-
// src/handle-fetch-error.ts
|
|
1256
|
-
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
|
1257
|
-
var RETRYABLE_NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
1258
|
-
"ConnectionRefused",
|
|
1259
|
-
"ConnectionClosed",
|
|
1260
|
-
"FailedToOpenSocket",
|
|
1261
|
-
"ECONNRESET",
|
|
1262
|
-
"ECONNREFUSED",
|
|
1263
|
-
"ETIMEDOUT",
|
|
1264
|
-
"EPIPE",
|
|
1265
|
-
"UND_ERR_SOCKET",
|
|
1266
|
-
"UND_ERR_HEADERS_TIMEOUT",
|
|
1267
|
-
"UND_ERR_BODY_TIMEOUT",
|
|
1268
|
-
"UND_ERR_CONNECT_TIMEOUT"
|
|
1269
|
-
]);
|
|
1270
|
-
function findNetworkError(error) {
|
|
1271
|
-
const visited = /* @__PURE__ */ new Set();
|
|
1272
|
-
let current = error;
|
|
1273
|
-
while (current instanceof Error && !visited.has(current)) {
|
|
1274
|
-
visited.add(current);
|
|
1275
|
-
const errorWithCode = current;
|
|
1276
|
-
if (typeof errorWithCode.code === "string" && RETRYABLE_NETWORK_ERROR_CODES.has(errorWithCode.code)) {
|
|
1277
|
-
return errorWithCode;
|
|
1278
|
-
}
|
|
1279
|
-
current = current.cause;
|
|
1280
|
-
}
|
|
1281
|
-
return void 0;
|
|
1282
|
-
}
|
|
1283
|
-
function handleFetchError({
|
|
1284
|
-
error,
|
|
1285
|
-
url,
|
|
1286
|
-
requestBodyValues
|
|
1287
|
-
}) {
|
|
1288
|
-
if (isAbortError(error)) {
|
|
1289
|
-
return error;
|
|
1290
|
-
}
|
|
1291
|
-
if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
|
|
1292
|
-
const cause = error.cause;
|
|
1293
|
-
if (cause != null) {
|
|
1294
|
-
return new APICallError({
|
|
1295
|
-
message: `Cannot connect to API: ${cause.message}`,
|
|
1296
|
-
cause,
|
|
1297
|
-
url,
|
|
1298
|
-
requestBodyValues,
|
|
1299
|
-
isRetryable: true
|
|
1300
|
-
// retry when network error
|
|
1301
|
-
});
|
|
1302
|
-
}
|
|
1303
|
-
}
|
|
1304
|
-
const networkError = findNetworkError(error);
|
|
1305
|
-
if (networkError != null) {
|
|
1306
|
-
if (APICallError.isInstance(error)) {
|
|
1307
|
-
return new APICallError({
|
|
1308
|
-
message: error.message,
|
|
1309
|
-
cause: error.cause,
|
|
1310
|
-
url: error.url,
|
|
1311
|
-
requestBodyValues: error.requestBodyValues,
|
|
1312
|
-
statusCode: error.statusCode,
|
|
1313
|
-
responseHeaders: error.responseHeaders,
|
|
1314
|
-
responseBody: error.responseBody,
|
|
1315
|
-
data: error.data,
|
|
1316
|
-
isRetryable: true
|
|
1317
|
-
});
|
|
1318
|
-
}
|
|
1319
|
-
return new APICallError({
|
|
1320
|
-
message: `Cannot connect to API: ${error instanceof Error ? error.message : networkError.message}`,
|
|
1321
|
-
cause: error,
|
|
1322
|
-
url,
|
|
1323
|
-
requestBodyValues,
|
|
1324
|
-
isRetryable: true
|
|
1325
|
-
});
|
|
1326
|
-
}
|
|
1327
|
-
return error;
|
|
1328
|
-
}
|
|
1329
|
-
|
|
1330
|
-
// src/get-runtime-environment-user-agent.ts
|
|
1331
|
-
function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
|
|
1332
|
-
var _a3, _b3, _c;
|
|
1333
|
-
if (globalThisAny.window) {
|
|
1334
|
-
return `runtime/browser`;
|
|
1335
|
-
}
|
|
1336
|
-
if ((_a3 = globalThisAny.navigator) == null ? void 0 : _a3.userAgent) {
|
|
1337
|
-
return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
|
|
1338
|
-
}
|
|
1339
|
-
if ((_c = (_b3 = globalThisAny.process) == null ? void 0 : _b3.versions) == null ? void 0 : _c.node) {
|
|
1340
|
-
return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
|
|
1341
|
-
}
|
|
1342
|
-
if (globalThisAny.EdgeRuntime) {
|
|
1343
|
-
return `runtime/vercel-edge`;
|
|
1344
|
-
}
|
|
1345
|
-
return "runtime/unknown";
|
|
1346
|
-
}
|
|
1347
|
-
|
|
1348
|
-
// src/normalize-headers.ts
|
|
1349
|
-
function normalizeHeaders(headers) {
|
|
1350
|
-
if (headers == null) {
|
|
1351
|
-
return {};
|
|
1352
|
-
}
|
|
1353
|
-
const normalized = {};
|
|
1354
|
-
if (headers instanceof Headers) {
|
|
1355
|
-
headers.forEach((value, key) => {
|
|
1356
|
-
normalized[key.toLowerCase()] = value;
|
|
1357
|
-
});
|
|
1358
|
-
} else {
|
|
1359
|
-
if (!Array.isArray(headers)) {
|
|
1360
|
-
headers = Object.entries(headers);
|
|
1361
|
-
}
|
|
1362
|
-
for (const [key, value] of headers) {
|
|
1363
|
-
if (value != null) {
|
|
1364
|
-
normalized[key.toLowerCase()] = value;
|
|
1365
|
-
}
|
|
1366
|
-
}
|
|
1367
|
-
}
|
|
1368
|
-
return normalized;
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
// src/with-user-agent-suffix.ts
|
|
1372
|
-
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
1373
|
-
const normalizedHeaders = new Headers(normalizeHeaders(headers));
|
|
1374
|
-
const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
|
|
1375
|
-
normalizedHeaders.set(
|
|
1376
|
-
"user-agent",
|
|
1377
|
-
[currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
|
|
1378
|
-
);
|
|
1379
|
-
return Object.fromEntries(normalizedHeaders.entries());
|
|
1380
|
-
}
|
|
1381
|
-
|
|
1382
|
-
// src/version.ts
|
|
1383
|
-
var VERSION = true ? "5.0.33" : "0.0.0-test";
|
|
1384
|
-
|
|
1385
|
-
// src/get-from-api.ts
|
|
1386
|
-
var getOriginalFetch = () => globalThis.fetch;
|
|
1467
|
+
import { APICallError as APICallError3 } from "@ai-sdk/provider";
|
|
1468
|
+
var getOriginalFetch2 = () => globalThis.fetch;
|
|
1387
1469
|
var getFromApi = async ({
|
|
1388
1470
|
url,
|
|
1389
1471
|
headers = {},
|
|
@@ -1396,7 +1478,7 @@ var getFromApi = async ({
|
|
|
1396
1478
|
trustedOrigin
|
|
1397
1479
|
}) => {
|
|
1398
1480
|
try {
|
|
1399
|
-
const requestFetch = fetch != null ? fetch :
|
|
1481
|
+
const requestFetch = fetch != null ? fetch : getOriginalFetch2();
|
|
1400
1482
|
const outgoingHeaders = credentialedOrigin !== void 0 && !isSameOrigin(url, credentialedOrigin) ? {} : headers;
|
|
1401
1483
|
const requestHeaders = withUserAgentSuffix(
|
|
1402
1484
|
outgoingHeaders,
|
|
@@ -1424,10 +1506,10 @@ var getFromApi = async ({
|
|
|
1424
1506
|
requestBodyValues: {}
|
|
1425
1507
|
});
|
|
1426
1508
|
} catch (error) {
|
|
1427
|
-
if (isAbortError(error) ||
|
|
1509
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
|
1428
1510
|
throw error;
|
|
1429
1511
|
}
|
|
1430
|
-
throw new
|
|
1512
|
+
throw new APICallError3({
|
|
1431
1513
|
message: "Failed to process error response",
|
|
1432
1514
|
cause: error,
|
|
1433
1515
|
statusCode: response.status,
|
|
@@ -1446,11 +1528,11 @@ var getFromApi = async ({
|
|
|
1446
1528
|
});
|
|
1447
1529
|
} catch (error) {
|
|
1448
1530
|
if (error instanceof Error) {
|
|
1449
|
-
if (isAbortError(error) ||
|
|
1531
|
+
if (isAbortError(error) || APICallError3.isInstance(error)) {
|
|
1450
1532
|
throw error;
|
|
1451
1533
|
}
|
|
1452
1534
|
}
|
|
1453
|
-
throw new
|
|
1535
|
+
throw new APICallError3({
|
|
1454
1536
|
message: "Failed to process successful response",
|
|
1455
1537
|
cause: error,
|
|
1456
1538
|
statusCode: response.status,
|
|
@@ -3257,9 +3339,183 @@ async function parseProviderOptions({
|
|
|
3257
3339
|
return parsedProviderOptions.value;
|
|
3258
3340
|
}
|
|
3259
3341
|
|
|
3342
|
+
// src/post-multipart-stream-to-api.ts
|
|
3343
|
+
import { APICallError as APICallError4 } from "@ai-sdk/provider";
|
|
3344
|
+
var getOriginalFetch3 = () => globalThis.fetch;
|
|
3345
|
+
function escapeMultipartHeaderValue(value) {
|
|
3346
|
+
return value.replace(/[\r\n]/g, "").replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
3347
|
+
}
|
|
3348
|
+
function createMultipartBody(parts, boundary) {
|
|
3349
|
+
const encoder = new TextEncoder();
|
|
3350
|
+
let disposed = false;
|
|
3351
|
+
let activeReader;
|
|
3352
|
+
const enteredStreams = /* @__PURE__ */ new Set();
|
|
3353
|
+
async function* emitParts() {
|
|
3354
|
+
var _a3, _b3;
|
|
3355
|
+
for (const part of parts) {
|
|
3356
|
+
if (disposed) return;
|
|
3357
|
+
const disposition = `--${boundary}\r
|
|
3358
|
+
Content-Disposition: form-data; name="${escapeMultipartHeaderValue(part.name)}"`;
|
|
3359
|
+
if (part.type === "field") {
|
|
3360
|
+
yield encoder.encode(`${disposition}\r
|
|
3361
|
+
\r
|
|
3362
|
+
${part.value}\r
|
|
3363
|
+
`);
|
|
3364
|
+
continue;
|
|
3365
|
+
}
|
|
3366
|
+
const filenameParameter = `; filename="${escapeMultipartHeaderValue(
|
|
3367
|
+
(_a3 = part.filename) != null ? _a3 : "blob"
|
|
3368
|
+
)}"`;
|
|
3369
|
+
const mediaType = ((_b3 = part.mediaType) != null ? _b3 : "application/octet-stream").replace(
|
|
3370
|
+
/[\r\n]/g,
|
|
3371
|
+
""
|
|
3372
|
+
);
|
|
3373
|
+
yield encoder.encode(
|
|
3374
|
+
`${disposition}${filenameParameter}\r
|
|
3375
|
+
Content-Type: ${mediaType}\r
|
|
3376
|
+
\r
|
|
3377
|
+
`
|
|
3378
|
+
);
|
|
3379
|
+
if (part.content instanceof Uint8Array) {
|
|
3380
|
+
yield part.content;
|
|
3381
|
+
} else {
|
|
3382
|
+
const reader = part.content.getReader();
|
|
3383
|
+
activeReader = reader;
|
|
3384
|
+
enteredStreams.add(part.content);
|
|
3385
|
+
let finished = false;
|
|
3386
|
+
try {
|
|
3387
|
+
while (true) {
|
|
3388
|
+
const { done, value } = await reader.read();
|
|
3389
|
+
if (done || disposed) {
|
|
3390
|
+
finished = done;
|
|
3391
|
+
break;
|
|
3392
|
+
}
|
|
3393
|
+
yield value;
|
|
3394
|
+
}
|
|
3395
|
+
} finally {
|
|
3396
|
+
activeReader = void 0;
|
|
3397
|
+
if (!finished) {
|
|
3398
|
+
await reader.cancel().catch(() => {
|
|
3399
|
+
});
|
|
3400
|
+
}
|
|
3401
|
+
reader.releaseLock();
|
|
3402
|
+
}
|
|
3403
|
+
if (disposed) return;
|
|
3404
|
+
}
|
|
3405
|
+
yield encoder.encode("\r\n");
|
|
3406
|
+
}
|
|
3407
|
+
yield encoder.encode(`--${boundary}--\r
|
|
3408
|
+
`);
|
|
3409
|
+
}
|
|
3410
|
+
return {
|
|
3411
|
+
stream: convertAsyncIteratorToReadableStream(emitParts()),
|
|
3412
|
+
async dispose(reason) {
|
|
3413
|
+
if (disposed) return;
|
|
3414
|
+
disposed = true;
|
|
3415
|
+
const reader = activeReader;
|
|
3416
|
+
if (reader != null) {
|
|
3417
|
+
await reader.cancel(reason).catch(() => {
|
|
3418
|
+
});
|
|
3419
|
+
}
|
|
3420
|
+
for (const part of parts) {
|
|
3421
|
+
if (part.type === "file" && !(part.content instanceof Uint8Array) && !enteredStreams.has(part.content)) {
|
|
3422
|
+
await part.content.cancel(reason).catch(() => {
|
|
3423
|
+
});
|
|
3424
|
+
}
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
};
|
|
3428
|
+
}
|
|
3429
|
+
var postMultipartStreamToApi = async ({
|
|
3430
|
+
url,
|
|
3431
|
+
headers = {},
|
|
3432
|
+
parts,
|
|
3433
|
+
failedResponseHandler,
|
|
3434
|
+
successfulResponseHandler,
|
|
3435
|
+
abortSignal,
|
|
3436
|
+
fetch = getOriginalFetch3()
|
|
3437
|
+
}) => {
|
|
3438
|
+
const boundary = `ai-sdk-multipart-${generateId()}`;
|
|
3439
|
+
const requestBodyValues = Object.fromEntries(
|
|
3440
|
+
parts.map((part) => {
|
|
3441
|
+
var _a3;
|
|
3442
|
+
return [
|
|
3443
|
+
part.name,
|
|
3444
|
+
part.type === "field" ? part.value : `<file:${(_a3 = part.filename) != null ? _a3 : part.name}>`
|
|
3445
|
+
];
|
|
3446
|
+
})
|
|
3447
|
+
);
|
|
3448
|
+
const body = createMultipartBody(parts, boundary);
|
|
3449
|
+
try {
|
|
3450
|
+
const requestInit = {
|
|
3451
|
+
method: "POST",
|
|
3452
|
+
headers: withUserAgentSuffix(
|
|
3453
|
+
{
|
|
3454
|
+
...headers,
|
|
3455
|
+
"Content-Type": `multipart/form-data; boundary=${boundary}`
|
|
3456
|
+
},
|
|
3457
|
+
`ai-sdk/provider-utils/${VERSION}`,
|
|
3458
|
+
getRuntimeEnvironmentUserAgent()
|
|
3459
|
+
),
|
|
3460
|
+
body: body.stream,
|
|
3461
|
+
duplex: "half",
|
|
3462
|
+
signal: abortSignal
|
|
3463
|
+
};
|
|
3464
|
+
const response = await fetch(url, requestInit);
|
|
3465
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
3466
|
+
if (!response.ok) {
|
|
3467
|
+
let errorInformation;
|
|
3468
|
+
try {
|
|
3469
|
+
errorInformation = await failedResponseHandler({
|
|
3470
|
+
response,
|
|
3471
|
+
url,
|
|
3472
|
+
requestBodyValues
|
|
3473
|
+
});
|
|
3474
|
+
} catch (error) {
|
|
3475
|
+
if (isAbortError(error) || APICallError4.isInstance(error)) {
|
|
3476
|
+
throw error;
|
|
3477
|
+
}
|
|
3478
|
+
throw new APICallError4({
|
|
3479
|
+
message: "Failed to process error response",
|
|
3480
|
+
cause: error,
|
|
3481
|
+
statusCode: response.status,
|
|
3482
|
+
url,
|
|
3483
|
+
responseHeaders,
|
|
3484
|
+
requestBodyValues
|
|
3485
|
+
});
|
|
3486
|
+
}
|
|
3487
|
+
throw errorInformation.value;
|
|
3488
|
+
}
|
|
3489
|
+
try {
|
|
3490
|
+
return await successfulResponseHandler({
|
|
3491
|
+
response,
|
|
3492
|
+
url,
|
|
3493
|
+
requestBodyValues
|
|
3494
|
+
});
|
|
3495
|
+
} catch (error) {
|
|
3496
|
+
if (error instanceof Error) {
|
|
3497
|
+
if (isAbortError(error) || APICallError4.isInstance(error)) {
|
|
3498
|
+
throw error;
|
|
3499
|
+
}
|
|
3500
|
+
}
|
|
3501
|
+
throw new APICallError4({
|
|
3502
|
+
message: "Failed to process successful response",
|
|
3503
|
+
cause: error,
|
|
3504
|
+
statusCode: response.status,
|
|
3505
|
+
url,
|
|
3506
|
+
responseHeaders,
|
|
3507
|
+
requestBodyValues
|
|
3508
|
+
});
|
|
3509
|
+
}
|
|
3510
|
+
} catch (error) {
|
|
3511
|
+
await body.dispose(error);
|
|
3512
|
+
throw handleFetchError({ error, url, requestBodyValues });
|
|
3513
|
+
}
|
|
3514
|
+
};
|
|
3515
|
+
|
|
3260
3516
|
// src/post-to-api.ts
|
|
3261
|
-
import { APICallError as
|
|
3262
|
-
var
|
|
3517
|
+
import { APICallError as APICallError5 } from "@ai-sdk/provider";
|
|
3518
|
+
var getOriginalFetch4 = () => globalThis.fetch;
|
|
3263
3519
|
var postJsonToApi = async ({
|
|
3264
3520
|
url,
|
|
3265
3521
|
headers,
|
|
@@ -3310,7 +3566,7 @@ var postToApi = async ({
|
|
|
3310
3566
|
successfulResponseHandler,
|
|
3311
3567
|
failedResponseHandler,
|
|
3312
3568
|
abortSignal,
|
|
3313
|
-
fetch =
|
|
3569
|
+
fetch = getOriginalFetch4()
|
|
3314
3570
|
}) => {
|
|
3315
3571
|
try {
|
|
3316
3572
|
const response = await fetch(url, {
|
|
@@ -3333,10 +3589,10 @@ var postToApi = async ({
|
|
|
3333
3589
|
requestBodyValues: body.values
|
|
3334
3590
|
});
|
|
3335
3591
|
} catch (error) {
|
|
3336
|
-
if (isAbortError(error) ||
|
|
3592
|
+
if (isAbortError(error) || APICallError5.isInstance(error)) {
|
|
3337
3593
|
throw error;
|
|
3338
3594
|
}
|
|
3339
|
-
throw new
|
|
3595
|
+
throw new APICallError5({
|
|
3340
3596
|
message: "Failed to process error response",
|
|
3341
3597
|
cause: error,
|
|
3342
3598
|
statusCode: response.status,
|
|
@@ -3355,11 +3611,11 @@ var postToApi = async ({
|
|
|
3355
3611
|
});
|
|
3356
3612
|
} catch (error) {
|
|
3357
3613
|
if (error instanceof Error) {
|
|
3358
|
-
if (isAbortError(error) ||
|
|
3614
|
+
if (isAbortError(error) || APICallError5.isInstance(error)) {
|
|
3359
3615
|
throw error;
|
|
3360
3616
|
}
|
|
3361
3617
|
}
|
|
3362
|
-
throw new
|
|
3618
|
+
throw new APICallError5({
|
|
3363
3619
|
message: "Failed to process successful response",
|
|
3364
3620
|
cause: error,
|
|
3365
3621
|
statusCode: response.status,
|
|
@@ -3475,7 +3731,7 @@ async function resolve(value) {
|
|
|
3475
3731
|
|
|
3476
3732
|
// src/resolve-full-media-type.ts
|
|
3477
3733
|
import {
|
|
3478
|
-
UnsupportedFunctionalityError
|
|
3734
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
3479
3735
|
} from "@ai-sdk/provider";
|
|
3480
3736
|
function resolveFullMediaType({
|
|
3481
3737
|
part
|
|
@@ -3491,11 +3747,11 @@ function resolveFullMediaType({
|
|
|
3491
3747
|
if (detected) {
|
|
3492
3748
|
return detected;
|
|
3493
3749
|
}
|
|
3494
|
-
throw new
|
|
3750
|
+
throw new UnsupportedFunctionalityError2({
|
|
3495
3751
|
functionality: `file of media type "${part.mediaType}" must specify subtype since it could not be auto-detected`
|
|
3496
3752
|
});
|
|
3497
3753
|
}
|
|
3498
|
-
throw new
|
|
3754
|
+
throw new UnsupportedFunctionalityError2({
|
|
3499
3755
|
functionality: `file of media type "${part.mediaType}" must specify subtype since it is not passed as inline bytes`
|
|
3500
3756
|
});
|
|
3501
3757
|
}
|
|
@@ -3598,7 +3854,7 @@ async function retryWithExponentialBackoffInternal(f, {
|
|
|
3598
3854
|
}
|
|
3599
3855
|
|
|
3600
3856
|
// src/response-handler.ts
|
|
3601
|
-
import { APICallError as
|
|
3857
|
+
import { APICallError as APICallError6, EmptyResponseBodyError } from "@ai-sdk/provider";
|
|
3602
3858
|
var textDecoder2 = new TextDecoder();
|
|
3603
3859
|
function wrapResponseBodyStream({
|
|
3604
3860
|
stream,
|
|
@@ -3633,7 +3889,7 @@ function wrapResponseBodyStream({
|
|
|
3633
3889
|
}
|
|
3634
3890
|
controller.error(
|
|
3635
3891
|
handleFetchError({
|
|
3636
|
-
error: new
|
|
3892
|
+
error: new APICallError6({
|
|
3637
3893
|
message: "Failed to process successful response",
|
|
3638
3894
|
cause: error,
|
|
3639
3895
|
statusCode,
|
|
@@ -3677,7 +3933,7 @@ var createJsonErrorResponseHandler = ({
|
|
|
3677
3933
|
if (responseBody.trim() === "") {
|
|
3678
3934
|
return {
|
|
3679
3935
|
responseHeaders,
|
|
3680
|
-
value: new
|
|
3936
|
+
value: new APICallError6({
|
|
3681
3937
|
message: response.statusText,
|
|
3682
3938
|
url,
|
|
3683
3939
|
requestBodyValues,
|
|
@@ -3695,7 +3951,7 @@ var createJsonErrorResponseHandler = ({
|
|
|
3695
3951
|
});
|
|
3696
3952
|
return {
|
|
3697
3953
|
responseHeaders,
|
|
3698
|
-
value: new
|
|
3954
|
+
value: new APICallError6({
|
|
3699
3955
|
message: errorToMessage(parsedError),
|
|
3700
3956
|
url,
|
|
3701
3957
|
requestBodyValues,
|
|
@@ -3709,7 +3965,7 @@ var createJsonErrorResponseHandler = ({
|
|
|
3709
3965
|
} catch (e) {
|
|
3710
3966
|
return {
|
|
3711
3967
|
responseHeaders,
|
|
3712
|
-
value: new
|
|
3968
|
+
value: new APICallError6({
|
|
3713
3969
|
message: response.statusText,
|
|
3714
3970
|
url,
|
|
3715
3971
|
requestBodyValues,
|
|
@@ -3748,7 +4004,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
|
3748
4004
|
});
|
|
3749
4005
|
const responseHeaders = extractResponseHeaders(response);
|
|
3750
4006
|
if (!parsedResult.success) {
|
|
3751
|
-
throw new
|
|
4007
|
+
throw new APICallError6({
|
|
3752
4008
|
message: "Invalid JSON response",
|
|
3753
4009
|
cause: parsedResult.error,
|
|
3754
4010
|
statusCode: response.status,
|
|
@@ -3819,7 +4075,7 @@ async function* parseJsonLines({
|
|
|
3819
4075
|
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
|
3820
4076
|
const responseHeaders = extractResponseHeaders(response);
|
|
3821
4077
|
if (!response.body) {
|
|
3822
|
-
throw new
|
|
4078
|
+
throw new APICallError6({
|
|
3823
4079
|
message: "Response body is empty",
|
|
3824
4080
|
url,
|
|
3825
4081
|
requestBodyValues,
|
|
@@ -3835,7 +4091,7 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
|
3835
4091
|
value: new Uint8Array(buffer)
|
|
3836
4092
|
};
|
|
3837
4093
|
} catch (error) {
|
|
3838
|
-
throw new
|
|
4094
|
+
throw new APICallError6({
|
|
3839
4095
|
message: "Failed to read response as array buffer",
|
|
3840
4096
|
url,
|
|
3841
4097
|
requestBodyValues,
|
|
@@ -3846,12 +4102,28 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
|
3846
4102
|
});
|
|
3847
4103
|
}
|
|
3848
4104
|
};
|
|
4105
|
+
var createBinaryStreamResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
|
4106
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
4107
|
+
if (response.body == null) {
|
|
4108
|
+
throw new EmptyResponseBodyError({});
|
|
4109
|
+
}
|
|
4110
|
+
return {
|
|
4111
|
+
responseHeaders,
|
|
4112
|
+
value: wrapResponseBodyStream({
|
|
4113
|
+
stream: response.body,
|
|
4114
|
+
url,
|
|
4115
|
+
requestBodyValues,
|
|
4116
|
+
statusCode: response.status,
|
|
4117
|
+
responseHeaders
|
|
4118
|
+
})
|
|
4119
|
+
};
|
|
4120
|
+
};
|
|
3849
4121
|
var createStatusCodeErrorResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
|
3850
4122
|
const responseHeaders = extractResponseHeaders(response);
|
|
3851
4123
|
const responseBody = await readResponseBodyAsText({ response, url });
|
|
3852
4124
|
return {
|
|
3853
4125
|
responseHeaders,
|
|
3854
|
-
value: new
|
|
4126
|
+
value: new APICallError6({
|
|
3855
4127
|
message: response.statusText,
|
|
3856
4128
|
url,
|
|
3857
4129
|
requestBodyValues,
|
|
@@ -4290,6 +4562,7 @@ export {
|
|
|
4290
4562
|
convertToFormData,
|
|
4291
4563
|
convertUint8ArrayToBase64,
|
|
4292
4564
|
createBinaryResponseHandler,
|
|
4565
|
+
createBinaryStreamResponseHandler,
|
|
4293
4566
|
createEventSourceResponseHandler,
|
|
4294
4567
|
createIdGenerator,
|
|
4295
4568
|
createJsonErrorResponseHandler,
|
|
@@ -4304,6 +4577,7 @@ export {
|
|
|
4304
4577
|
createStatusCodeErrorResponseHandler,
|
|
4305
4578
|
createToolNameMapping,
|
|
4306
4579
|
delay,
|
|
4580
|
+
deleteFromApi,
|
|
4307
4581
|
detectMediaType,
|
|
4308
4582
|
downloadBlob,
|
|
4309
4583
|
dynamicTool,
|
|
@@ -4352,6 +4626,7 @@ export {
|
|
|
4352
4626
|
parseProviderOptions,
|
|
4353
4627
|
postFormDataToApi,
|
|
4354
4628
|
postJsonToApi,
|
|
4629
|
+
postMultipartStreamToApi,
|
|
4355
4630
|
postToApi,
|
|
4356
4631
|
readResponseWithSizeLimit,
|
|
4357
4632
|
readWebSocketMessageText,
|