ai 5.0.290 → 5.0.292

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 CHANGED
@@ -1,5 +1,23 @@
1
1
  # ai
2
2
 
3
+ ## 5.0.292
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [2bed31c]
8
+ - Updated dependencies [4fb0c6d]
9
+ - Updated dependencies [90ebfd5]
10
+ - @ai-sdk/gateway@2.0.183
11
+
12
+ ## 5.0.291
13
+
14
+ ### Patch Changes
15
+
16
+ - d9bda29: Reduce memory and CPU usage when validating UI messages with deeply nested provider metadata. `validateUIMessages` and `safeValidateUIMessages` validated JSON values in `providerMetadata` and `callProviderMetadata` with a recursive union schema that needed several times the memory of the value. These values are now validated in a single pass and are no longer copied, except to remove `__proto__` keys.
17
+ - Updated dependencies [e88ddaa]
18
+ - Updated dependencies [68bf653]
19
+ - @ai-sdk/gateway@2.0.182
20
+
3
21
  ## 5.0.290
4
22
 
5
23
  ### Patch Changes
package/dist/index.js CHANGED
@@ -847,7 +847,7 @@ function detectMediaType({
847
847
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
848
848
 
849
849
  // src/version.ts
850
- var VERSION = true ? "5.0.290" : "0.0.0-test";
850
+ var VERSION = true ? "5.0.292" : "0.0.0-test";
851
851
 
852
852
  // src/util/download/download.ts
853
853
  var download = async ({
@@ -10828,57 +10828,127 @@ var TextStreamChatTransport = class extends HttpChatTransport {
10828
10828
  var import_provider32 = require("@ai-sdk/provider");
10829
10829
  var import_provider_utils34 = require("@ai-sdk/provider-utils");
10830
10830
  var import_v48 = require("zod/v4");
10831
- function findInvalidJsonValuePath(value, path = [], visited = /* @__PURE__ */ new WeakSet()) {
10831
+
10832
+ // src/util/normalize-json-value.ts
10833
+ var invalid = Symbol("invalid");
10834
+ function normalizeJsonValue(value) {
10835
+ const path = [];
10836
+ const normalized = normalize(value, path, /* @__PURE__ */ new Set());
10837
+ return normalized === invalid ? { success: false, path } : { success: true, value: normalized };
10838
+ }
10839
+ function normalize(value, path, ancestors) {
10832
10840
  switch (typeof value) {
10841
+ case "string":
10842
+ case "boolean":
10843
+ return value;
10833
10844
  case "number":
10834
- return Number.isFinite(value) ? void 0 : [...path];
10835
- case "bigint":
10836
- case "symbol":
10837
- case "function":
10838
- return [...path];
10845
+ return Number.isFinite(value) ? value : invalid;
10839
10846
  case "object": {
10840
- if (value === null || visited.has(value)) {
10841
- return void 0;
10842
- }
10843
- visited.add(value);
10844
- const keys = Array.isArray(value) ? value.keys() : Object.keys(value);
10845
- for (const key of keys) {
10846
- if (key === "__proto__")
10847
- continue;
10848
- path.push(key);
10849
- const invalidPath = findInvalidJsonValuePath(
10850
- value[key],
10851
- path,
10852
- visited
10853
- );
10854
- if (invalidPath != null)
10855
- return invalidPath;
10856
- path.pop();
10857
- }
10858
- return void 0;
10847
+ if (value === null)
10848
+ return value;
10849
+ if (ancestors.has(value))
10850
+ return invalid;
10851
+ ancestors.add(value);
10852
+ const normalized = Array.isArray(value) ? normalizeArray(value, path, ancestors) : normalizeObject(value, path, ancestors);
10853
+ ancestors.delete(value);
10854
+ return normalized;
10859
10855
  }
10860
10856
  default:
10861
- return void 0;
10857
+ return invalid;
10858
+ }
10859
+ }
10860
+ function normalizeArray(array, path, ancestors) {
10861
+ let copy;
10862
+ for (let index = 0; index < array.length; index++) {
10863
+ const element = array[index];
10864
+ path.push(index);
10865
+ const normalized = normalize(element, path, ancestors);
10866
+ if (normalized === invalid)
10867
+ return invalid;
10868
+ path.pop();
10869
+ if (copy != null) {
10870
+ copy.push(normalized);
10871
+ } else if (normalized !== element) {
10872
+ copy = array.slice(0, index);
10873
+ copy.push(normalized);
10874
+ }
10875
+ }
10876
+ return copy != null ? copy : array;
10877
+ }
10878
+ function normalizeObject(object2, path, ancestors) {
10879
+ if (!isPlainObject(object2) || hasEnumerableSymbolKey(object2))
10880
+ return invalid;
10881
+ const keys = Object.keys(object2);
10882
+ let copy;
10883
+ for (let index = 0; index < keys.length; index++) {
10884
+ const key = keys[index];
10885
+ if (key === "__proto__") {
10886
+ copy != null ? copy : copy = copyProperties(object2, keys, index);
10887
+ continue;
10888
+ }
10889
+ const property = object2[key];
10890
+ let normalized = property;
10891
+ if (property !== void 0) {
10892
+ path.push(key);
10893
+ normalized = normalize(property, path, ancestors);
10894
+ if (normalized === invalid)
10895
+ return invalid;
10896
+ path.pop();
10897
+ }
10898
+ if (copy != null) {
10899
+ copy[key] = normalized;
10900
+ } else if (normalized !== property) {
10901
+ copy = copyProperties(object2, keys, index);
10902
+ copy[key] = normalized;
10903
+ }
10862
10904
  }
10905
+ return copy != null ? copy : object2;
10863
10906
  }
10864
- function withJsonValueCheck(schema, jsonValuePath = []) {
10865
- return import_v48.z.unknown().check((context) => {
10866
- let jsonValue = context.value;
10867
- for (const key of jsonValuePath) {
10868
- jsonValue = jsonValue != null && typeof jsonValue === "object" ? jsonValue[key] : void 0;
10869
- }
10870
- const invalidPath = findInvalidJsonValuePath(jsonValue);
10871
- if (invalidPath != null) {
10872
- context.issues.push({
10873
- code: "custom",
10874
- message: "Invalid JSON value. Expected null, a string, a finite number, a boolean, an array, or an object.",
10875
- input: context.value,
10876
- path: [...jsonValuePath, ...invalidPath]
10877
- });
10907
+ function copyProperties(object2, keys, count) {
10908
+ const copy = {};
10909
+ for (let index = 0; index < count; index++) {
10910
+ copy[keys[index]] = object2[keys[index]];
10911
+ }
10912
+ return copy;
10913
+ }
10914
+ function isPlainObject(object2) {
10915
+ const constructor = object2.constructor;
10916
+ if (typeof constructor !== "function")
10917
+ return true;
10918
+ const prototype = constructor.prototype;
10919
+ return prototype !== null && typeof prototype === "object" && Object.prototype.hasOwnProperty.call(prototype, "isPrototypeOf");
10920
+ }
10921
+ function hasEnumerableSymbolKey(object2) {
10922
+ const symbols = Object.getOwnPropertySymbols(object2);
10923
+ for (let index = 0; index < symbols.length; index++) {
10924
+ if (Object.prototype.propertyIsEnumerable.call(object2, symbols[index])) {
10925
+ return true;
10878
10926
  }
10879
- }).pipe(schema);
10927
+ }
10928
+ return false;
10880
10929
  }
10881
- var providerMetadataSchema2 = withJsonValueCheck(providerMetadataSchema);
10930
+
10931
+ // src/ui/validate-ui-messages.ts
10932
+ var jsonValueSchema2 = import_v48.z.unknown().transform((value, context) => {
10933
+ const result = normalizeJsonValue(value);
10934
+ if (!result.success) {
10935
+ context.issues.push({
10936
+ code: "custom",
10937
+ message: "Invalid JSON value. Expected null, a string, a finite number, a boolean, an array, or an object.",
10938
+ input: value,
10939
+ path: result.path
10940
+ });
10941
+ return import_v48.z.NEVER;
10942
+ }
10943
+ return result.value;
10944
+ });
10945
+ var validatedJsonValueSchema = import_v48.z.custom(
10946
+ (value) => value !== void 0,
10947
+ { message: "Invalid JSON value. Expected a defined value." }
10948
+ );
10949
+ var providerMetadataSchema2 = jsonValueSchema2.pipe(
10950
+ import_v48.z.record(import_v48.z.string(), import_v48.z.record(import_v48.z.string(), validatedJsonValueSchema))
10951
+ );
10882
10952
  function isEmptyObject(value) {
10883
10953
  return value != null && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0;
10884
10954
  }