@ai-sdk/provider-utils 5.0.13 → 5.0.15

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/dist/index.js CHANGED
@@ -86,8 +86,8 @@ async function waitForWebSocketBufferDrain(socket, {
86
86
  pollIntervalMs = 20,
87
87
  abortSignal
88
88
  } = {}) {
89
- var _a2;
90
- while (socket.readyState === WEBSOCKET_OPEN_STATE && ((_a2 = socket.bufferedAmount) != null ? _a2 : 0) > highWaterMark) {
89
+ var _a3;
90
+ while (socket.readyState === WEBSOCKET_OPEN_STATE && ((_a3 = socket.bufferedAmount) != null ? _a3 : 0) > highWaterMark) {
91
91
  if ((abortSignal == null ? void 0 : abortSignal.aborted) === true) {
92
92
  return;
93
93
  }
@@ -109,7 +109,7 @@ function connectToWebSocket({
109
109
  onClose,
110
110
  onAbort
111
111
  }) {
112
- var _a2;
112
+ var _a3;
113
113
  let socket;
114
114
  let abortListener;
115
115
  const close = (code) => {
@@ -123,7 +123,7 @@ function connectToWebSocket({
123
123
  }
124
124
  };
125
125
  if (abortSignal == null ? void 0 : abortSignal.aborted) {
126
- onAbort == null ? void 0 : onAbort((_a2 = abortSignal.reason) != null ? _a2 : new Error("Aborted"));
126
+ onAbort == null ? void 0 : onAbort((_a3 = abortSignal.reason) != null ? _a3 : new Error("Aborted"));
127
127
  return { socket: void 0, close };
128
128
  }
129
129
  try {
@@ -137,8 +137,8 @@ function connectToWebSocket({
137
137
  }
138
138
  if (abortSignal != null && onAbort != null) {
139
139
  abortListener = () => {
140
- var _a3;
141
- return onAbort((_a3 = abortSignal.reason) != null ? _a3 : new Error("Aborted"));
140
+ var _a4;
141
+ return onAbort((_a4 = abortSignal.reason) != null ? _a4 : new Error("Aborted"));
142
142
  };
143
143
  abortSignal.addEventListener("abort", abortListener, { once: true });
144
144
  }
@@ -157,8 +157,11 @@ function connectToWebSocket({
157
157
  socket.onerror = () => {
158
158
  tail = tail.then(() => onSocketError == null ? void 0 : onSocketError()).catch(onProcessingError);
159
159
  };
160
- socket.onclose = () => {
161
- tail = tail.then(() => onClose == null ? void 0 : onClose()).catch(onProcessingError);
160
+ socket.onclose = (event) => {
161
+ const closeEvent = event;
162
+ const code = typeof (closeEvent == null ? void 0 : closeEvent.code) === "number" ? closeEvent.code : void 0;
163
+ const reason = typeof (closeEvent == null ? void 0 : closeEvent.reason) === "string" ? closeEvent.reason : void 0;
164
+ tail = tail.then(() => onClose == null ? void 0 : onClose({ code, reason })).catch(onProcessingError);
162
165
  };
163
166
  return { socket, close };
164
167
  }
@@ -279,12 +282,12 @@ function createToolNameMapping({
279
282
  }
280
283
  return {
281
284
  toProviderToolName: (customToolName) => {
282
- var _a2;
283
- return (_a2 = customToolNameToProviderToolName[customToolName]) != null ? _a2 : customToolName;
285
+ var _a3;
286
+ return (_a3 = customToolNameToProviderToolName[customToolName]) != null ? _a3 : customToolName;
284
287
  },
285
288
  toCustomToolName: (providerToolName) => {
286
- var _a2;
287
- return (_a2 = providerToolNameToCustomToolName[providerToolName]) != null ? _a2 : providerToolName;
289
+ var _a3;
290
+ return (_a3 = providerToolNameToCustomToolName[providerToolName]) != null ? _a3 : providerToolName;
288
291
  }
289
292
  };
290
293
  }
@@ -312,17 +315,17 @@ var DelayedPromise = class {
312
315
  return this._promise;
313
316
  }
314
317
  resolve(value) {
315
- var _a2;
318
+ var _a3;
316
319
  this.status = { type: "resolved", value };
317
320
  if (this._promise) {
318
- (_a2 = this._resolve) == null ? void 0 : _a2.call(this, value);
321
+ (_a3 = this._resolve) == null ? void 0 : _a3.call(this, value);
319
322
  }
320
323
  }
321
324
  reject(error) {
322
- var _a2;
325
+ var _a3;
323
326
  this.status = { type: "rejected", error };
324
327
  if (this._promise) {
325
- (_a2 = this._reject) == null ? void 0 : _a2.call(this, error);
328
+ (_a3 = this._reject) == null ? void 0 : _a3.call(this, error);
326
329
  }
327
330
  }
328
331
  isResolved() {
@@ -637,9 +640,9 @@ function isFullMediaType(mediaType) {
637
640
 
638
641
  // src/cancel-response-body.ts
639
642
  async function cancelResponseBody(response) {
640
- var _a2;
643
+ var _a3;
641
644
  try {
642
- await ((_a2 = response.body) == null ? void 0 : _a2.cancel());
645
+ await ((_a3 = response.body) == null ? void 0 : _a3.cancel());
643
646
  } catch (e) {
644
647
  }
645
648
  }
@@ -683,42 +686,6 @@ function isSameOrigin(url, baseUrl) {
683
686
  }
684
687
  }
685
688
 
686
- // src/sanitize-request-headers.ts
687
- var BLOCKED_REQUEST_HEADERS = [
688
- // Hop-by-hop / transport (RFC 7230 §6.1)
689
- "connection",
690
- "keep-alive",
691
- "te",
692
- "trailer",
693
- "transfer-encoding",
694
- "upgrade",
695
- // Host / virtual-host routing
696
- "host",
697
- // Proxy / origin spoofing
698
- "forwarded",
699
- "proxy-authorization",
700
- "via",
701
- "x-forwarded-for",
702
- "x-forwarded-host",
703
- "x-forwarded-proto",
704
- "x-real-ip",
705
- // Cloud metadata (GCP, AWS IMDSv1/v2, Azure, Alibaba, DigitalOcean)
706
- "metadata",
707
- "metadata-flavor",
708
- "x-aws-ec2-metadata-token",
709
- "x-metadata-token",
710
- // Session / cookie
711
- "cookie",
712
- "set-cookie"
713
- ];
714
- function sanitizeRequestHeaders(input) {
715
- const headers = new Headers(input);
716
- for (const name2 of BLOCKED_REQUEST_HEADERS) {
717
- headers.delete(name2);
718
- }
719
- return headers;
720
- }
721
-
722
689
  // src/validate-download-url.ts
723
690
  function validateDownloadUrl(url) {
724
691
  let parsed;
@@ -772,6 +739,19 @@ function validateDownloadUrl(url) {
772
739
  return;
773
740
  }
774
741
  }
742
+ function validateDownloadAddress({
743
+ address,
744
+ family,
745
+ hostname
746
+ }) {
747
+ const isUnsafe = family === 4 ? !isIPv4(address) || isPrivateIPv4(address) : family === 6 ? isPrivateIPv6(address) : true;
748
+ if (isUnsafe) {
749
+ throw new DownloadError({
750
+ url: hostname,
751
+ message: `Hostname ${hostname} resolved to disallowed IP address ${address}`
752
+ });
753
+ }
754
+ }
775
755
  function isIPv4(hostname) {
776
756
  const parts = hostname.split(".");
777
757
  if (parts.length !== 4) return false;
@@ -863,6 +843,138 @@ function isPrivateIPv6(ip) {
863
843
  return false;
864
844
  }
865
845
 
846
+ // src/safe-node-fetch.ts
847
+ function createSafeLookup(lookup) {
848
+ return ((hostname, options, callback) => {
849
+ lookup(hostname, { ...options, all: true }, (error, addresses) => {
850
+ if (error) {
851
+ callback(error);
852
+ return;
853
+ }
854
+ try {
855
+ const [firstAddress] = addresses;
856
+ if (firstAddress == null) {
857
+ throw new Error(`Hostname ${hostname} did not resolve to an address`);
858
+ }
859
+ for (const { address, family } of addresses) {
860
+ validateDownloadAddress({ address, family, hostname });
861
+ }
862
+ if (options.all === true) {
863
+ callback(null, addresses);
864
+ } else {
865
+ callback(
866
+ null,
867
+ firstAddress.address,
868
+ firstAddress.family
869
+ );
870
+ }
871
+ } catch (error2) {
872
+ callback(
873
+ error2 instanceof Error ? error2 : new Error(String(error2))
874
+ );
875
+ }
876
+ });
877
+ });
878
+ }
879
+ var safeNodeFetchPromise;
880
+ var initialGlobalFetch = globalThis.fetch;
881
+ var initialGlobalFetchIsNodeDefault = isNodeDefaultFetch(initialGlobalFetch);
882
+ function isNodeRuntime() {
883
+ var _a3, _b3;
884
+ const runtimeProcess = globalThis.process;
885
+ return ((_a3 = runtimeProcess == null ? void 0 : runtimeProcess.release) == null ? void 0 : _a3.name) === "node" && ((_b3 = runtimeProcess.versions) == null ? void 0 : _b3.bun) == null;
886
+ }
887
+ async function getDefaultDownloadFetch() {
888
+ if (!isNodeRuntime() || !initialGlobalFetchIsNodeDefault || globalThis.fetch !== initialGlobalFetch) {
889
+ return globalThis.fetch;
890
+ }
891
+ return safeNodeFetchPromise != null ? safeNodeFetchPromise : safeNodeFetchPromise = Promise.resolve().then(createSafeNodeFetch);
892
+ }
893
+ function isNodeDefaultFetch(fetch) {
894
+ const source = Function.prototype.toString.call(fetch);
895
+ return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
896
+ }
897
+ function createSafeNodeFetch() {
898
+ const { createRequire } = loadBuiltinModule("node:module");
899
+ const { lookup } = loadBuiltinModule("node:dns");
900
+ const { Agent, fetch } = createRequire(getCurrentModulePath())(
901
+ "undici"
902
+ );
903
+ const dispatcher = new Agent({
904
+ connect: {
905
+ lookup: createSafeLookup(lookup)
906
+ }
907
+ });
908
+ return ((input, init) => fetch(
909
+ input,
910
+ {
911
+ ...init,
912
+ dispatcher
913
+ }
914
+ ));
915
+ }
916
+ function loadBuiltinModule(id) {
917
+ var _a3;
918
+ const processWithBuiltins = globalThis.process;
919
+ const builtinModule = (_a3 = processWithBuiltins == null ? void 0 : processWithBuiltins.getBuiltinModule) == null ? void 0 : _a3.call(processWithBuiltins, id);
920
+ if (builtinModule == null) {
921
+ throw new Error(`Node.js built-in module ${id} is unavailable`);
922
+ }
923
+ return builtinModule;
924
+ }
925
+ function getCurrentModulePath() {
926
+ const originalPrepareStackTrace = Error.prepareStackTrace;
927
+ try {
928
+ Error.prepareStackTrace = (_error, callSites) => callSites;
929
+ const error = new Error("Capture current module path");
930
+ Error.captureStackTrace(error, getCurrentModulePath);
931
+ const [caller] = error.stack;
932
+ const fileName = caller == null ? void 0 : caller.getFileName();
933
+ if (fileName == null) {
934
+ throw new Error("Unable to determine the current module path");
935
+ }
936
+ return fileName;
937
+ } finally {
938
+ Error.prepareStackTrace = originalPrepareStackTrace;
939
+ }
940
+ }
941
+
942
+ // src/sanitize-request-headers.ts
943
+ var BLOCKED_REQUEST_HEADERS = [
944
+ // Hop-by-hop / transport (RFC 7230 §6.1)
945
+ "connection",
946
+ "keep-alive",
947
+ "te",
948
+ "trailer",
949
+ "transfer-encoding",
950
+ "upgrade",
951
+ // Host / virtual-host routing
952
+ "host",
953
+ // Proxy / origin spoofing
954
+ "forwarded",
955
+ "proxy-authorization",
956
+ "via",
957
+ "x-forwarded-for",
958
+ "x-forwarded-host",
959
+ "x-forwarded-proto",
960
+ "x-real-ip",
961
+ // Cloud metadata (GCP, AWS IMDSv1/v2, Azure, Alibaba, DigitalOcean)
962
+ "metadata",
963
+ "metadata-flavor",
964
+ "x-aws-ec2-metadata-token",
965
+ "x-metadata-token",
966
+ // Session / cookie
967
+ "cookie",
968
+ "set-cookie"
969
+ ];
970
+ function sanitizeRequestHeaders(input) {
971
+ const headers = new Headers(input);
972
+ for (const name3 of BLOCKED_REQUEST_HEADERS) {
973
+ headers.delete(name3);
974
+ }
975
+ return headers;
976
+ }
977
+
866
978
  // src/fetch-with-validated-redirects.ts
867
979
  var MAX_DOWNLOAD_REDIRECTS = 10;
868
980
  var REDIRECT_STATUS_CODES = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
@@ -871,7 +983,7 @@ async function fetchWithValidatedRedirects({
871
983
  headers,
872
984
  abortSignal,
873
985
  maxRedirects = MAX_DOWNLOAD_REDIRECTS,
874
- fetch = globalThis.fetch,
986
+ fetch: customFetch,
875
987
  trustedOrigin
876
988
  }) {
877
989
  let currentHeaders = headers === void 0 ? void 0 : sanitizeRequestHeaders(headers);
@@ -884,9 +996,11 @@ async function fetchWithValidatedRedirects({
884
996
  };
885
997
  let currentUrl = url;
886
998
  for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
887
- if (trustedOrigin === void 0 || !isSameOrigin(currentUrl, trustedOrigin)) {
999
+ const isTrustedHop = trustedOrigin !== void 0 && isSameOrigin(currentUrl, trustedOrigin);
1000
+ if (!isTrustedHop) {
888
1001
  validateDownloadUrl(currentUrl);
889
1002
  }
1003
+ const fetch = customFetch != null ? customFetch : isTrustedHop ? globalThis.fetch : await getDefaultDownloadFetch();
890
1004
  const response = await fetch(currentUrl, perHopInit("manual"));
891
1005
  if (response.type === "opaqueredirect") {
892
1006
  if (!isBrowserRuntime()) {
@@ -976,7 +1090,7 @@ async function readResponseWithSizeLimit({
976
1090
 
977
1091
  // src/download-blob.ts
978
1092
  async function downloadBlob(url, options) {
979
- var _a2, _b2;
1093
+ var _a3, _b3;
980
1094
  try {
981
1095
  const response = await fetchWithValidatedRedirects({
982
1096
  url,
@@ -993,9 +1107,9 @@ async function downloadBlob(url, options) {
993
1107
  const data = await readResponseWithSizeLimit({
994
1108
  response,
995
1109
  url,
996
- maxBytes: (_a2 = options == null ? void 0 : options.maxBytes) != null ? _a2 : DEFAULT_MAX_DOWNLOAD_SIZE
1110
+ maxBytes: (_a3 = options == null ? void 0 : options.maxBytes) != null ? _a3 : DEFAULT_MAX_DOWNLOAD_SIZE
997
1111
  });
998
- const contentType = (_b2 = response.headers.get("content-type")) != null ? _b2 : void 0;
1112
+ const contentType = (_b3 = response.headers.get("content-type")) != null ? _b3 : void 0;
999
1113
  return new Blob([data], contentType ? { type: contentType } : void 0);
1000
1114
  } catch (error) {
1001
1115
  if (DownloadError.isInstance(error)) {
@@ -1129,14 +1243,14 @@ function handleFetchError({
1129
1243
 
1130
1244
  // src/get-runtime-environment-user-agent.ts
1131
1245
  function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
1132
- var _a2, _b2, _c;
1246
+ var _a3, _b3, _c;
1133
1247
  if (globalThisAny.window) {
1134
1248
  return `runtime/browser`;
1135
1249
  }
1136
- if ((_a2 = globalThisAny.navigator) == null ? void 0 : _a2.userAgent) {
1250
+ if ((_a3 = globalThisAny.navigator) == null ? void 0 : _a3.userAgent) {
1137
1251
  return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
1138
1252
  }
1139
- if ((_c = (_b2 = globalThisAny.process) == null ? void 0 : _b2.versions) == null ? void 0 : _c.node) {
1253
+ if ((_c = (_b3 = globalThisAny.process) == null ? void 0 : _b3.versions) == null ? void 0 : _c.node) {
1140
1254
  return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
1141
1255
  }
1142
1256
  if (globalThisAny.EdgeRuntime) {
@@ -1180,7 +1294,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
1180
1294
  }
1181
1295
 
1182
1296
  // src/version.ts
1183
- var VERSION = true ? "5.0.13" : "0.0.0-test";
1297
+ var VERSION = true ? "5.0.15" : "0.0.0-test";
1184
1298
 
1185
1299
  // src/get-from-api.ts
1186
1300
  var getOriginalFetch = () => globalThis.fetch;
@@ -1190,12 +1304,13 @@ var getFromApi = async ({
1190
1304
  successfulResponseHandler,
1191
1305
  failedResponseHandler,
1192
1306
  abortSignal,
1193
- fetch = getOriginalFetch(),
1307
+ fetch,
1194
1308
  validateUrl,
1195
1309
  credentialedOrigin,
1196
1310
  trustedOrigin
1197
1311
  }) => {
1198
1312
  try {
1313
+ const requestFetch = fetch != null ? fetch : getOriginalFetch();
1199
1314
  const outgoingHeaders = credentialedOrigin !== void 0 && !isSameOrigin(url, credentialedOrigin) ? {} : headers;
1200
1315
  const requestHeaders = withUserAgentSuffix(
1201
1316
  outgoingHeaders,
@@ -1208,7 +1323,7 @@ var getFromApi = async ({
1208
1323
  abortSignal,
1209
1324
  fetch,
1210
1325
  trustedOrigin
1211
- }) : await fetch(url, {
1326
+ }) : await requestFetch(url, {
1212
1327
  method: "GET",
1213
1328
  headers: requestHeaders,
1214
1329
  signal: abortSignal
@@ -1288,8 +1403,8 @@ function injectJsonInstructionIntoMessages({
1288
1403
  schemaPrefix,
1289
1404
  schemaSuffix
1290
1405
  }) {
1291
- var _a2, _b2;
1292
- const systemMessage = ((_a2 = messages[0]) == null ? void 0 : _a2.role) === "system" ? { ...messages[0] } : { role: "system", content: "" };
1406
+ var _a3, _b3;
1407
+ const systemMessage = ((_a3 = messages[0]) == null ? void 0 : _a3.role) === "system" ? { ...messages[0] } : { role: "system", content: "" };
1293
1408
  systemMessage.content = injectJsonInstruction({
1294
1409
  prompt: systemMessage.content,
1295
1410
  schema,
@@ -1298,14 +1413,14 @@ function injectJsonInstructionIntoMessages({
1298
1413
  });
1299
1414
  return [
1300
1415
  systemMessage,
1301
- ...((_b2 = messages[0]) == null ? void 0 : _b2.role) === "system" ? messages.slice(1) : messages
1416
+ ...((_b3 = messages[0]) == null ? void 0 : _b3.role) === "system" ? messages.slice(1) : messages
1302
1417
  ];
1303
1418
  }
1304
1419
 
1305
1420
  // src/is-buffer.ts
1306
1421
  function isBuffer(value) {
1307
- var _a2, _b2;
1308
- return (_b2 = (_a2 = globalThis.Buffer) == null ? void 0 : _a2.isBuffer(value)) != null ? _b2 : false;
1422
+ var _a3, _b3;
1423
+ return (_b3 = (_a3 = globalThis.Buffer) == null ? void 0 : _a3.isBuffer(value)) != null ? _b3 : false;
1309
1424
  }
1310
1425
 
1311
1426
  // src/is-non-nullable.ts
@@ -1488,15 +1603,15 @@ function mapReasoningToProviderBudget({
1488
1603
 
1489
1604
  // src/media-type-to-extension.ts
1490
1605
  function mediaTypeToExtension(mediaType) {
1491
- var _a2;
1606
+ var _a3;
1492
1607
  const [_type, subtype = ""] = mediaType.toLowerCase().split("/");
1493
- return (_a2 = {
1608
+ return (_a3 = {
1494
1609
  mpeg: "mp3",
1495
1610
  "x-wav": "wav",
1496
1611
  opus: "ogg",
1497
1612
  mp4: "m4a",
1498
1613
  "x-m4a": "m4a"
1499
- }[subtype]) != null ? _a2 : subtype;
1614
+ }[subtype]) != null ? _a3 : subtype;
1500
1615
  }
1501
1616
 
1502
1617
  // src/parse-json.ts
@@ -1643,11 +1758,11 @@ function parseAnyDef() {
1643
1758
  // src/to-json-schema/zod3-to-json-schema/parsers/array.ts
1644
1759
  import { ZodFirstPartyTypeKind } from "zod/v3";
1645
1760
  function parseArrayDef(def, refs) {
1646
- var _a2, _b2, _c;
1761
+ var _a3, _b3, _c;
1647
1762
  const res = {
1648
1763
  type: "array"
1649
1764
  };
1650
- if (((_a2 = def.type) == null ? void 0 : _a2._def) && ((_c = (_b2 = def.type) == null ? void 0 : _b2._def) == null ? void 0 : _c.typeName) !== ZodFirstPartyTypeKind.ZodAny) {
1765
+ if (((_a3 = def.type) == null ? void 0 : _a3._def) && ((_c = (_b3 = def.type) == null ? void 0 : _b3._def) == null ? void 0 : _c.typeName) !== ZodFirstPartyTypeKind.ZodAny) {
1651
1766
  res.items = parseDef(def.type._def, {
1652
1767
  ...refs,
1653
1768
  currentPath: [...refs.currentPath, "items"]
@@ -2040,8 +2155,8 @@ function escapeNonAlphaNumeric(source) {
2040
2155
  return result;
2041
2156
  }
2042
2157
  function addFormat(schema, value, message, refs) {
2043
- var _a2;
2044
- if (schema.format || ((_a2 = schema.anyOf) == null ? void 0 : _a2.some((x) => x.format))) {
2158
+ var _a3;
2159
+ if (schema.format || ((_a3 = schema.anyOf) == null ? void 0 : _a3.some((x) => x.format))) {
2045
2160
  if (!schema.anyOf) {
2046
2161
  schema.anyOf = [];
2047
2162
  }
@@ -2060,8 +2175,8 @@ function addFormat(schema, value, message, refs) {
2060
2175
  }
2061
2176
  }
2062
2177
  function addPattern(schema, regex, message, refs) {
2063
- var _a2;
2064
- if (schema.pattern || ((_a2 = schema.allOf) == null ? void 0 : _a2.some((x) => x.pattern))) {
2178
+ var _a3;
2179
+ if (schema.pattern || ((_a3 = schema.allOf) == null ? void 0 : _a3.some((x) => x.pattern))) {
2065
2180
  if (!schema.allOf) {
2066
2181
  schema.allOf = [];
2067
2182
  }
@@ -2080,7 +2195,7 @@ function addPattern(schema, regex, message, refs) {
2080
2195
  }
2081
2196
  }
2082
2197
  function stringifyRegExpWithFlags(regex, refs) {
2083
- var _a2;
2198
+ var _a3;
2084
2199
  if (!refs.applyRegexFlags || !regex.flags) {
2085
2200
  return regex.source;
2086
2201
  }
@@ -2110,7 +2225,7 @@ function stringifyRegExpWithFlags(regex, refs) {
2110
2225
  pattern += source[i];
2111
2226
  pattern += `${source[i - 2]}-${source[i]}`.toUpperCase();
2112
2227
  inCharRange = false;
2113
- } else if (source[i + 1] === "-" && ((_a2 = source[i + 2]) == null ? void 0 : _a2.match(/[a-z]/))) {
2228
+ } else if (source[i + 1] === "-" && ((_a3 = source[i + 2]) == null ? void 0 : _a3.match(/[a-z]/))) {
2114
2229
  pattern += source[i];
2115
2230
  inCharRange = true;
2116
2231
  } else {
@@ -2164,15 +2279,15 @@ function stringifyRegExpWithFlags(regex, refs) {
2164
2279
 
2165
2280
  // src/to-json-schema/zod3-to-json-schema/parsers/record.ts
2166
2281
  function parseRecordDef(def, refs) {
2167
- var _a2, _b2, _c, _d, _e, _f;
2282
+ var _a3, _b3, _c, _d, _e, _f;
2168
2283
  const schema = {
2169
2284
  type: "object",
2170
- additionalProperties: (_a2 = parseDef(def.valueType._def, {
2285
+ additionalProperties: (_a3 = parseDef(def.valueType._def, {
2171
2286
  ...refs,
2172
2287
  currentPath: [...refs.currentPath, "additionalProperties"]
2173
- })) != null ? _a2 : refs.allowedAdditionalProperties
2288
+ })) != null ? _a3 : refs.allowedAdditionalProperties
2174
2289
  };
2175
- if (((_b2 = def.keyType) == null ? void 0 : _b2._def.typeName) === ZodFirstPartyTypeKind2.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
2290
+ if (((_b3 = def.keyType) == null ? void 0 : _b3._def.typeName) === ZodFirstPartyTypeKind2.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
2176
2291
  const { type: _type, ...keyType } = parseStringDef(def.keyType._def, refs);
2177
2292
  return {
2178
2293
  ...schema,
@@ -2445,8 +2560,8 @@ function safeIsOptional(schema) {
2445
2560
 
2446
2561
  // src/to-json-schema/zod3-to-json-schema/parsers/optional.ts
2447
2562
  var parseOptionalDef = (def, refs) => {
2448
- var _a2;
2449
- if (refs.currentPath.toString() === ((_a2 = refs.propertyPath) == null ? void 0 : _a2.toString())) {
2563
+ var _a3;
2564
+ if (refs.currentPath.toString() === ((_a3 = refs.propertyPath) == null ? void 0 : _a3.toString())) {
2450
2565
  return parseDef(def.innerType._def, refs);
2451
2566
  }
2452
2567
  const innerSchema = parseDef(def.innerType._def, {
@@ -2645,10 +2760,10 @@ var getRelativePath = (pathA, pathB) => {
2645
2760
 
2646
2761
  // src/to-json-schema/zod3-to-json-schema/parse-def.ts
2647
2762
  function parseDef(def, refs, forceResolution = false) {
2648
- var _a2;
2763
+ var _a3;
2649
2764
  const seenItem = refs.seen.get(def);
2650
2765
  if (refs.override) {
2651
- const overrideResult = (_a2 = refs.override) == null ? void 0 : _a2.call(
2766
+ const overrideResult = (_a3 = refs.override) == null ? void 0 : _a3.call(
2652
2767
  refs,
2653
2768
  def,
2654
2769
  refs,
@@ -2716,11 +2831,11 @@ var getRefs = (options) => {
2716
2831
  currentPath,
2717
2832
  propertyPath: void 0,
2718
2833
  seen: new Map(
2719
- Object.entries(_options.definitions).map(([name2, def]) => [
2834
+ Object.entries(_options.definitions).map(([name3, def]) => [
2720
2835
  def._def,
2721
2836
  {
2722
2837
  def: def._def,
2723
- path: [..._options.basePath, _options.definitionPath, name2],
2838
+ path: [..._options.basePath, _options.definitionPath, name3],
2724
2839
  // Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
2725
2840
  jsonSchema: void 0
2726
2841
  }
@@ -2731,50 +2846,50 @@ var getRefs = (options) => {
2731
2846
 
2732
2847
  // src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts
2733
2848
  var zod3ToJsonSchema = (schema, options) => {
2734
- var _a2;
2849
+ var _a3;
2735
2850
  const refs = getRefs(options);
2736
2851
  let definitions = typeof options === "object" && options.definitions ? Object.entries(options.definitions).reduce(
2737
- (acc, [name3, schema2]) => {
2738
- var _a3;
2852
+ (acc, [name4, schema2]) => {
2853
+ var _a4;
2739
2854
  return {
2740
2855
  ...acc,
2741
- [name3]: (_a3 = parseDef(
2856
+ [name4]: (_a4 = parseDef(
2742
2857
  schema2._def,
2743
2858
  {
2744
2859
  ...refs,
2745
- currentPath: [...refs.basePath, refs.definitionPath, name3]
2860
+ currentPath: [...refs.basePath, refs.definitionPath, name4]
2746
2861
  },
2747
2862
  true
2748
- )) != null ? _a3 : parseAnyDef()
2863
+ )) != null ? _a4 : parseAnyDef()
2749
2864
  };
2750
2865
  },
2751
2866
  {}
2752
2867
  ) : void 0;
2753
- const name2 = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
2754
- const main = (_a2 = parseDef(
2868
+ const name3 = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
2869
+ const main = (_a3 = parseDef(
2755
2870
  schema._def,
2756
- name2 === void 0 ? refs : {
2871
+ name3 === void 0 ? refs : {
2757
2872
  ...refs,
2758
- currentPath: [...refs.basePath, refs.definitionPath, name2]
2873
+ currentPath: [...refs.basePath, refs.definitionPath, name3]
2759
2874
  },
2760
2875
  false
2761
- )) != null ? _a2 : parseAnyDef();
2876
+ )) != null ? _a3 : parseAnyDef();
2762
2877
  const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
2763
2878
  if (title !== void 0) {
2764
2879
  main.title = title;
2765
2880
  }
2766
- const combined = name2 === void 0 ? definitions ? {
2881
+ const combined = name3 === void 0 ? definitions ? {
2767
2882
  ...main,
2768
2883
  [refs.definitionPath]: definitions
2769
2884
  } : main : {
2770
2885
  $ref: [
2771
2886
  ...refs.$refStrategy === "relative" ? [] : refs.basePath,
2772
2887
  refs.definitionPath,
2773
- name2
2888
+ name3
2774
2889
  ].join("/"),
2775
2890
  [refs.definitionPath]: {
2776
2891
  ...definitions,
2777
- [name2]: main
2892
+ [name3]: main
2778
2893
  }
2779
2894
  };
2780
2895
  combined.$schema = "http://json-schema.org/draft-07/schema#";
@@ -2850,8 +2965,8 @@ function hasStandardJsonSchema(schema) {
2850
2965
  return schema["~standard"].jsonSchema != null;
2851
2966
  }
2852
2967
  function zod3Schema(zodSchema2, options) {
2853
- var _a2;
2854
- const useReferences = (_a2 = options == null ? void 0 : options.useReferences) != null ? _a2 : false;
2968
+ var _a3;
2969
+ const useReferences = (_a3 = options == null ? void 0 : options.useReferences) != null ? _a3 : false;
2855
2970
  return jsonSchema(
2856
2971
  // defer json schema creation to avoid unnecessary computation when only validation is needed
2857
2972
  () => zod3ToJsonSchema(zodSchema2, {
@@ -2866,8 +2981,8 @@ function zod3Schema(zodSchema2, options) {
2866
2981
  );
2867
2982
  }
2868
2983
  function zod4Schema(zodSchema2, options) {
2869
- var _a2;
2870
- const useReferences = (_a2 = options == null ? void 0 : options.useReferences) != null ? _a2 : false;
2984
+ var _a3;
2985
+ const useReferences = (_a3 = options == null ? void 0 : options.useReferences) != null ? _a3 : false;
2871
2986
  return jsonSchema(
2872
2987
  // defer json schema creation to avoid unnecessary computation when only validation is needed
2873
2988
  () => addAdditionalPropertiesToJsonSchema(
@@ -3534,6 +3649,26 @@ function isJSONSerializable(value) {
3534
3649
  return false;
3535
3650
  }
3536
3651
 
3652
+ // src/serialization-error.ts
3653
+ import { AISDKError as AISDKError2 } from "@ai-sdk/provider";
3654
+ var name2 = "AI_SerializationError";
3655
+ var marker2 = `vercel.ai.error.${name2}`;
3656
+ var symbol2 = Symbol.for(marker2);
3657
+ var _a2, _b2;
3658
+ var SerializationError = class extends (_b2 = AISDKError2, _a2 = symbol2, _b2) {
3659
+ // used in isInstance
3660
+ constructor({
3661
+ message = "Failed to serialize value.",
3662
+ cause
3663
+ } = {}) {
3664
+ super({ name: name2, message, cause });
3665
+ this[_a2] = true;
3666
+ }
3667
+ static isInstance(error) {
3668
+ return AISDKError2.hasMarker(error, marker2);
3669
+ }
3670
+ };
3671
+
3537
3672
  // src/serialize-model-options.ts
3538
3673
  function serializeModelOptions(options) {
3539
3674
  const serializableConfig = {};
@@ -3555,7 +3690,9 @@ function resolveSync(value) {
3555
3690
  next = value();
3556
3691
  }
3557
3692
  if (next instanceof Promise) {
3558
- throw new Error("Promise returned from resolveSync");
3693
+ throw new SerializationError({
3694
+ message: "Cannot serialize asynchronous model options."
3695
+ });
3559
3696
  }
3560
3697
  return next;
3561
3698
  }
@@ -3567,10 +3704,10 @@ import {
3567
3704
  var StreamingToolCallTracker = class {
3568
3705
  constructor(controller, options = {}) {
3569
3706
  this.toolCalls = [];
3570
- var _a2, _b2;
3707
+ var _a3, _b3;
3571
3708
  this.controller = controller;
3572
- this._generateId = (_a2 = options.generateId) != null ? _a2 : generateId;
3573
- this.typeValidation = (_b2 = options.typeValidation) != null ? _b2 : "none";
3709
+ this._generateId = (_a3 = options.generateId) != null ? _a3 : generateId;
3710
+ this.typeValidation = (_b3 = options.typeValidation) != null ? _b3 : "none";
3574
3711
  this.extractMetadata = options.extractMetadata;
3575
3712
  this.buildToolCallProviderMetadata = options.buildToolCallProviderMetadata;
3576
3713
  }
@@ -3580,8 +3717,8 @@ var StreamingToolCallTracker = class {
3580
3717
  * events as appropriate.
3581
3718
  */
3582
3719
  processDelta(toolCallDelta) {
3583
- var _a2;
3584
- const index = (_a2 = toolCallDelta.index) != null ? _a2 : this.toolCalls.length;
3720
+ var _a3;
3721
+ const index = (_a3 = toolCallDelta.index) != null ? _a3 : this.toolCalls.length;
3585
3722
  if (this.toolCalls[index] == null) {
3586
3723
  this.processNewToolCall(index, toolCallDelta);
3587
3724
  } else {
@@ -3600,7 +3737,7 @@ var StreamingToolCallTracker = class {
3600
3737
  }
3601
3738
  }
3602
3739
  processNewToolCall(index, toolCallDelta) {
3603
- var _a2, _b2, _c;
3740
+ var _a3, _b3, _c;
3604
3741
  if (this.typeValidation === "required") {
3605
3742
  if (toolCallDelta.type !== "function") {
3606
3743
  throw new InvalidResponseDataError({
@@ -3622,7 +3759,7 @@ var StreamingToolCallTracker = class {
3622
3759
  message: `Expected 'id' to be a string.`
3623
3760
  });
3624
3761
  }
3625
- if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
3762
+ if (((_a3 = toolCallDelta.function) == null ? void 0 : _a3.name) == null) {
3626
3763
  throw new InvalidResponseDataError({
3627
3764
  data: toolCallDelta,
3628
3765
  message: `Expected 'function.name' to be a string.`
@@ -3633,7 +3770,7 @@ var StreamingToolCallTracker = class {
3633
3770
  id: toolCallDelta.id,
3634
3771
  toolName: toolCallDelta.function.name
3635
3772
  });
3636
- const metadata = (_b2 = this.extractMetadata) == null ? void 0 : _b2.call(this, toolCallDelta);
3773
+ const metadata = (_b3 = this.extractMetadata) == null ? void 0 : _b3.call(this, toolCallDelta);
3637
3774
  this.toolCalls[index] = {
3638
3775
  id: toolCallDelta.id,
3639
3776
  type: "function",
@@ -3654,12 +3791,12 @@ var StreamingToolCallTracker = class {
3654
3791
  }
3655
3792
  }
3656
3793
  processExistingToolCall(index, toolCallDelta) {
3657
- var _a2;
3794
+ var _a3;
3658
3795
  const toolCall = this.toolCalls[index];
3659
3796
  if (toolCall.hasFinished) {
3660
3797
  return;
3661
3798
  }
3662
- if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null) {
3799
+ if (((_a3 = toolCallDelta.function) == null ? void 0 : _a3.arguments) != null) {
3663
3800
  toolCall.function.arguments += toolCallDelta.function.arguments;
3664
3801
  this.controller.enqueue({
3665
3802
  type: "tool-input-delta",
@@ -3669,18 +3806,18 @@ var StreamingToolCallTracker = class {
3669
3806
  }
3670
3807
  }
3671
3808
  finishToolCall(toolCall) {
3672
- var _a2, _b2;
3809
+ var _a3, _b3;
3673
3810
  this.controller.enqueue({
3674
3811
  type: "tool-input-end",
3675
3812
  id: toolCall.id
3676
3813
  });
3677
- const providerMetadata = (_a2 = this.buildToolCallProviderMetadata) == null ? void 0 : _a2.call(
3814
+ const providerMetadata = (_a3 = this.buildToolCallProviderMetadata) == null ? void 0 : _a3.call(
3678
3815
  this,
3679
3816
  toolCall.metadata
3680
3817
  );
3681
3818
  this.controller.enqueue({
3682
3819
  type: "tool-call",
3683
- toolCallId: (_b2 = toolCall.id) != null ? _b2 : this._generateId(),
3820
+ toolCallId: (_b3 = toolCall.id) != null ? _b3 : this._generateId(),
3684
3821
  toolName: toolCall.function.name,
3685
3822
  input: toolCall.function.arguments,
3686
3823
  ...providerMetadata ? { providerMetadata } : {}
@@ -3887,6 +4024,7 @@ export {
3887
4024
  TRANSCRIPTION_STREAM_AUDIO_DONE_FRAME_TYPE as EXPERIMENTAL_TRANSCRIPTION_STREAM_AUDIO_DONE_FRAME_TYPE,
3888
4025
  TRANSCRIPTION_STREAM_START_FRAME_TYPE as EXPERIMENTAL_TRANSCRIPTION_STREAM_START_FRAME_TYPE,
3889
4026
  EventSourceParserStream2 as EventSourceParserStream,
4027
+ SerializationError,
3890
4028
  StreamingToolCallTracker,
3891
4029
  VERSION,
3892
4030
  WORKFLOW_DESERIALIZE,