ai 6.0.331 → 6.0.333

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
+ ## 6.0.333
4
+
5
+ ### Patch Changes
6
+
7
+ - 2771b61: Reduce memory and CPU usage when validating UI messages with deeply nested provider or tool metadata. `validateUIMessages` and `safeValidateUIMessages`, which the agent UI stream helpers also use, validated JSON values in `providerMetadata`, `callProviderMetadata`, `resultProviderMetadata`, and `toolMetadata` 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.
8
+ - Updated dependencies [9faffa5]
9
+ - Updated dependencies [3cf16af]
10
+ - Updated dependencies [d59eee0]
11
+ - @ai-sdk/gateway@3.0.238
12
+ - @ai-sdk/provider-utils@4.0.67
13
+
14
+ ## 6.0.332
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [23ff635]
19
+ - @ai-sdk/gateway@3.0.237
20
+
3
21
  ## 6.0.331
4
22
 
5
23
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1335,7 +1335,7 @@ function detectMediaType({
1335
1335
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
1336
1336
 
1337
1337
  // src/version.ts
1338
- var VERSION = true ? "6.0.331" : "0.0.0-test";
1338
+ var VERSION = true ? "6.0.333" : "0.0.0-test";
1339
1339
 
1340
1340
  // src/util/download/download.ts
1341
1341
  var download = async ({
@@ -3453,7 +3453,7 @@ async function validateApprovedToolApprovals({
3453
3453
  var _a23;
3454
3454
  const approved = [];
3455
3455
  const denied = [];
3456
- const invalid = [];
3456
+ const invalid2 = [];
3457
3457
  for (const approval of approvedToolApprovals) {
3458
3458
  const { toolCall, approvalRequest } = approval;
3459
3459
  const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
@@ -3499,7 +3499,7 @@ async function validateApprovedToolApprovals({
3499
3499
  );
3500
3500
  }
3501
3501
  if (validationError != null) {
3502
- invalid.push({
3502
+ invalid2.push({
3503
3503
  ...approval,
3504
3504
  error: new InvalidToolInputError({
3505
3505
  toolName: toolCall.toolName,
@@ -3532,7 +3532,7 @@ async function validateApprovedToolApprovals({
3532
3532
  return {
3533
3533
  approvedToolApprovals: approved,
3534
3534
  deniedToolApprovals: denied,
3535
- invalidToolApprovals: invalid
3535
+ invalidToolApprovals: invalid2
3536
3536
  };
3537
3537
  }
3538
3538
 
@@ -10235,62 +10235,127 @@ async function convertToModelMessages(messages, options) {
10235
10235
  var import_provider31 = require("@ai-sdk/provider");
10236
10236
  var import_provider_utils25 = require("@ai-sdk/provider-utils");
10237
10237
  var import_v48 = require("zod/v4");
10238
- function findInvalidJsonValuePath(value, path = [], visited = /* @__PURE__ */ new WeakSet()) {
10238
+
10239
+ // src/util/normalize-json-value.ts
10240
+ var invalid = Symbol("invalid");
10241
+ function normalizeJsonValue(value) {
10242
+ const path = [];
10243
+ const normalized = normalize(value, path, /* @__PURE__ */ new Set());
10244
+ return normalized === invalid ? { success: false, path } : { success: true, value: normalized };
10245
+ }
10246
+ function normalize(value, path, ancestors) {
10239
10247
  switch (typeof value) {
10248
+ case "string":
10249
+ case "boolean":
10250
+ return value;
10240
10251
  case "number":
10241
- return Number.isFinite(value) ? void 0 : [...path];
10242
- case "bigint":
10243
- case "symbol":
10244
- case "function":
10245
- return [...path];
10252
+ return Number.isFinite(value) ? value : invalid;
10246
10253
  case "object": {
10247
- if (value === null || visited.has(value)) {
10248
- return void 0;
10249
- }
10250
- visited.add(value);
10251
- const keys = Array.isArray(value) ? value.keys() : Object.keys(value);
10252
- for (const key of keys) {
10253
- if (key === "__proto__")
10254
- continue;
10255
- path.push(key);
10256
- const invalidPath = findInvalidJsonValuePath(
10257
- value[key],
10258
- path,
10259
- visited
10260
- );
10261
- if (invalidPath != null)
10262
- return invalidPath;
10263
- path.pop();
10264
- }
10265
- return void 0;
10254
+ if (value === null)
10255
+ return value;
10256
+ if (ancestors.has(value))
10257
+ return invalid;
10258
+ ancestors.add(value);
10259
+ const normalized = Array.isArray(value) ? normalizeArray(value, path, ancestors) : normalizeObject(value, path, ancestors);
10260
+ ancestors.delete(value);
10261
+ return normalized;
10266
10262
  }
10267
10263
  default:
10268
- return void 0;
10264
+ return invalid;
10265
+ }
10266
+ }
10267
+ function normalizeArray(array2, path, ancestors) {
10268
+ let copy;
10269
+ for (let index = 0; index < array2.length; index++) {
10270
+ const element = array2[index];
10271
+ path.push(index);
10272
+ const normalized = normalize(element, path, ancestors);
10273
+ if (normalized === invalid)
10274
+ return invalid;
10275
+ path.pop();
10276
+ if (copy != null) {
10277
+ copy.push(normalized);
10278
+ } else if (normalized !== element) {
10279
+ copy = array2.slice(0, index);
10280
+ copy.push(normalized);
10281
+ }
10282
+ }
10283
+ return copy != null ? copy : array2;
10284
+ }
10285
+ function normalizeObject(object2, path, ancestors) {
10286
+ if (!isPlainObject(object2) || hasEnumerableSymbolKey(object2))
10287
+ return invalid;
10288
+ const keys = Object.keys(object2);
10289
+ let copy;
10290
+ for (let index = 0; index < keys.length; index++) {
10291
+ const key = keys[index];
10292
+ if (key === "__proto__") {
10293
+ copy != null ? copy : copy = copyProperties(object2, keys, index);
10294
+ continue;
10295
+ }
10296
+ const property = object2[key];
10297
+ let normalized = property;
10298
+ if (property !== void 0) {
10299
+ path.push(key);
10300
+ normalized = normalize(property, path, ancestors);
10301
+ if (normalized === invalid)
10302
+ return invalid;
10303
+ path.pop();
10304
+ }
10305
+ if (copy != null) {
10306
+ copy[key] = normalized;
10307
+ } else if (normalized !== property) {
10308
+ copy = copyProperties(object2, keys, index);
10309
+ copy[key] = normalized;
10310
+ }
10269
10311
  }
10312
+ return copy != null ? copy : object2;
10270
10313
  }
10271
- function withJsonValueCheck(schema, jsonValuePath = []) {
10272
- return import_v48.z.unknown().check((context2) => {
10273
- let jsonValue = context2.value;
10274
- for (const key of jsonValuePath) {
10275
- jsonValue = jsonValue != null && typeof jsonValue === "object" ? jsonValue[key] : void 0;
10276
- }
10277
- const invalidPath = findInvalidJsonValuePath(jsonValue);
10278
- if (invalidPath != null) {
10279
- context2.issues.push({
10280
- code: "custom",
10281
- message: "Invalid JSON value. Expected null, a string, a finite number, a boolean, an array, or an object.",
10282
- input: context2.value,
10283
- path: [...jsonValuePath, ...invalidPath]
10284
- });
10314
+ function copyProperties(object2, keys, count) {
10315
+ const copy = {};
10316
+ for (let index = 0; index < count; index++) {
10317
+ copy[keys[index]] = object2[keys[index]];
10318
+ }
10319
+ return copy;
10320
+ }
10321
+ function isPlainObject(object2) {
10322
+ const constructor = object2.constructor;
10323
+ if (typeof constructor !== "function")
10324
+ return true;
10325
+ const prototype = constructor.prototype;
10326
+ return prototype !== null && typeof prototype === "object" && Object.prototype.hasOwnProperty.call(prototype, "isPrototypeOf");
10327
+ }
10328
+ function hasEnumerableSymbolKey(object2) {
10329
+ const symbols = Object.getOwnPropertySymbols(object2);
10330
+ for (let index = 0; index < symbols.length; index++) {
10331
+ if (Object.prototype.propertyIsEnumerable.call(object2, symbols[index])) {
10332
+ return true;
10285
10333
  }
10286
- }).pipe(schema);
10334
+ }
10335
+ return false;
10287
10336
  }
10288
- var toolMetadataRecordSchema = import_v48.z.record(
10289
- import_v48.z.string(),
10290
- jsonValueSchema.optional()
10337
+
10338
+ // src/ui/validate-ui-messages.ts
10339
+ var jsonValueSchema2 = import_v48.z.unknown().transform((value, context2) => {
10340
+ const result = normalizeJsonValue(value);
10341
+ if (!result.success) {
10342
+ context2.issues.push({
10343
+ code: "custom",
10344
+ message: "Invalid JSON value. Expected null, a string, a finite number, a boolean, an array, or an object.",
10345
+ input: value,
10346
+ path: result.path
10347
+ });
10348
+ return import_v48.z.NEVER;
10349
+ }
10350
+ return result.value;
10351
+ });
10352
+ var validatedJsonValueSchema = import_v48.z.custom();
10353
+ var toolMetadataSchema2 = jsonValueSchema2.pipe(
10354
+ import_v48.z.record(import_v48.z.string(), validatedJsonValueSchema)
10355
+ );
10356
+ var providerMetadataSchema2 = jsonValueSchema2.pipe(
10357
+ import_v48.z.record(import_v48.z.string(), import_v48.z.record(import_v48.z.string(), validatedJsonValueSchema))
10291
10358
  );
10292
- var toolMetadataSchema2 = withJsonValueCheck(toolMetadataRecordSchema);
10293
- var providerMetadataSchema2 = withJsonValueCheck(providerMetadataSchema);
10294
10359
  function isEmptyObject(value) {
10295
10360
  return value != null && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0;
10296
10361
  }