ai 6.0.332 → 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.
@@ -164,7 +164,7 @@ function detectMediaType({
164
164
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
165
165
 
166
166
  // src/version.ts
167
- var VERSION = true ? "6.0.332" : "0.0.0-test";
167
+ var VERSION = true ? "6.0.333" : "0.0.0-test";
168
168
 
169
169
  // src/util/download/download.ts
170
170
  var download = async ({
@@ -144,7 +144,7 @@ import {
144
144
  } from "@ai-sdk/provider-utils";
145
145
 
146
146
  // src/version.ts
147
- var VERSION = true ? "6.0.332" : "0.0.0-test";
147
+ var VERSION = true ? "6.0.333" : "0.0.0-test";
148
148
 
149
149
  // src/util/download/download.ts
150
150
  var download = async ({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "6.0.332",
3
+ "version": "6.0.333",
4
4
  "description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -45,9 +45,9 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@opentelemetry/api": "^1.9.0",
48
- "@ai-sdk/gateway": "3.0.237",
48
+ "@ai-sdk/gateway": "3.0.238",
49
49
  "@ai-sdk/provider": "3.0.19",
50
- "@ai-sdk/provider-utils": "4.0.66"
50
+ "@ai-sdk/provider-utils": "4.0.67"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@edge-runtime/vm": "^5.0.0",
@@ -58,9 +58,9 @@
58
58
  "tsx": "^4.19.2",
59
59
  "typescript": "5.8.3",
60
60
  "zod": "3.25.76",
61
- "@ai-sdk/google": "3.0.146",
62
- "@ai-sdk/openai": "3.0.143",
63
- "@ai-sdk/perplexity": "3.1.12",
61
+ "@ai-sdk/google": "3.0.147",
62
+ "@ai-sdk/openai": "3.0.144",
63
+ "@ai-sdk/perplexity": "3.1.13",
64
64
  "@ai-sdk/test-server": "1.0.6",
65
65
  "@vercel/ai-tsconfig": "0.0.0"
66
66
  },
@@ -2,6 +2,7 @@ import {
2
2
  TypeValidationContext,
3
3
  TypeValidationError,
4
4
  type JSONObject,
5
+ type JSONValue,
5
6
  } from '@ai-sdk/provider';
6
7
  import {
7
8
  lazySchema,
@@ -14,10 +15,10 @@ import {
14
15
  } from '@ai-sdk/provider-utils';
15
16
  import { z } from 'zod/v4';
16
17
  import { InvalidArgumentError } from '../error';
17
- import { jsonValueSchema } from '../types/json-value';
18
+ import type { ProviderMetadata } from '../types/provider-metadata';
18
19
  import { getOwn } from '../util/get-own';
19
20
  import { isDeepEqualData } from '../util/is-deep-equal-data';
20
- import { providerMetadataSchema as providerMetadataRecordSchema } from '../types/provider-metadata';
21
+ import { normalizeJsonValue } from '../util/normalize-json-value';
21
22
  import type {
22
23
  DataUIPart,
23
24
  DynamicToolUIPart,
@@ -29,98 +30,46 @@ import type {
29
30
  import { markToolPartAsUnavailable } from './unavailable-tool';
30
31
 
31
32
  /**
32
- * Returns the path to the first value that can never be part of a JSON value
33
- * (a non-finite number, bigint, symbol, or function), if there is one.
33
+ * Validates a JSON value in a single pass and reports an invalid JSON value
34
+ * with a single issue.
35
+ *
36
+ * UI messages are untrusted input. The recursive JSON value schema tries
37
+ * every alternative for every nested value and returns a copy of the value,
38
+ * so accepting a value would need several times the memory of the value. It
39
+ * also reports the issues of every alternative at every nesting level around
40
+ * an invalid value, so the size of a validation error would grow much faster
41
+ * than the input.
34
42
  */
35
- function findInvalidJsonValuePath(
36
- value: unknown,
37
- path: PropertyKey[] = [],
38
- visited = new WeakSet<object>(),
39
- ): PropertyKey[] | undefined {
40
- switch (typeof value) {
41
- case 'number':
42
- return Number.isFinite(value) ? undefined : [...path];
43
- case 'bigint':
44
- case 'symbol':
45
- case 'function':
46
- return [...path];
47
- case 'object': {
48
- if (value === null || visited.has(value)) {
49
- return undefined;
50
- }
51
- visited.add(value);
52
-
53
- const keys = Array.isArray(value) ? value.keys() : Object.keys(value);
54
- for (const key of keys) {
55
- // record schemas ignore `__proto__` keys:
56
- if (key === '__proto__') continue;
57
-
58
- path.push(key);
59
- const invalidPath = findInvalidJsonValuePath(
60
- (value as Record<PropertyKey, unknown>)[key],
61
- path,
62
- visited,
63
- );
64
- if (invalidPath != null) return invalidPath;
65
- path.pop();
66
- }
67
- return undefined;
68
- }
69
- default:
70
- return undefined;
43
+ const jsonValueSchema = z.unknown().transform((value: unknown, context) => {
44
+ const result = normalizeJsonValue(value);
45
+
46
+ if (!result.success) {
47
+ context.issues.push({
48
+ code: 'custom',
49
+ message:
50
+ 'Invalid JSON value. Expected null, a string, a finite number, ' +
51
+ 'a boolean, an array, or an object.',
52
+ input: value,
53
+ path: result.path,
54
+ });
55
+
56
+ return z.NEVER;
71
57
  }
72
- }
73
58
 
74
- /**
75
- * Rejects values that contain an invalid JSON value with a single issue before
76
- * `schema` validates them.
77
- *
78
- * The JSON value schema is a recursive union. It reports the issues of every
79
- * alternative at every nesting level around an invalid value, so the size of a
80
- * validation error would grow much faster than the (untrusted) input.
81
- *
82
- * @param jsonValuePath - Path from the validated value to the JSON value.
83
- */
84
- function withJsonValueCheck<SCHEMA extends z.ZodType>(
85
- schema: SCHEMA,
86
- jsonValuePath: string[] = [],
87
- ) {
88
- return z
89
- .unknown()
90
- .check(context => {
91
- let jsonValue = context.value;
92
- for (const key of jsonValuePath) {
93
- jsonValue =
94
- jsonValue != null && typeof jsonValue === 'object'
95
- ? (jsonValue as Record<string, unknown>)[key]
96
- : undefined;
97
- }
59
+ return result.value;
60
+ });
98
61
 
99
- const invalidPath = findInvalidJsonValuePath(jsonValue);
100
- if (invalidPath != null) {
101
- // Pushing the issue instead of adding it with `context.addIssue` aborts
102
- // the pipe, so the recursive schema does not run:
103
- context.issues.push({
104
- code: 'custom',
105
- message:
106
- 'Invalid JSON value. Expected null, a string, a finite number, ' +
107
- 'a boolean, an array, or an object.',
108
- input: context.value,
109
- path: [...jsonValuePath, ...invalidPath],
110
- });
111
- }
112
- })
113
- .pipe(schema);
114
- }
62
+ // The values of the records are JSON values that have already been validated.
63
+ const validatedJsonValueSchema = z.custom<JSONValue | undefined>();
115
64
 
116
- const toolMetadataRecordSchema: z.ZodType<JSONObject> = z.record(
117
- z.string(),
118
- jsonValueSchema.optional(),
65
+ const toolMetadataSchema: z.ZodType<JSONObject> = jsonValueSchema.pipe(
66
+ z.record(z.string(), validatedJsonValueSchema),
119
67
  );
120
68
 
121
- const toolMetadataSchema = withJsonValueCheck(toolMetadataRecordSchema);
122
-
123
- const providerMetadataSchema = withJsonValueCheck(providerMetadataRecordSchema);
69
+ const providerMetadataSchema: z.ZodType<ProviderMetadata> =
70
+ jsonValueSchema.pipe(
71
+ z.record(z.string(), z.record(z.string(), validatedJsonValueSchema)),
72
+ );
124
73
 
125
74
  function isEmptyObject(value: unknown): value is Record<string, never> {
126
75
  return (
@@ -0,0 +1,182 @@
1
+ import type { JSONValue } from '@ai-sdk/provider';
2
+
3
+ const invalid = Symbol('invalid');
4
+
5
+ /**
6
+ * Validates that a value is a JSON value: `null`, a string, a finite number, a
7
+ * boolean, an array of JSON values, or a plain object with JSON values.
8
+ *
9
+ * The value is validated in a single pass that does not allocate memory for
10
+ * its valid parts, so that validating untrusted input costs little more than
11
+ * the input itself. A recursive union schema tries (and reports the issues of)
12
+ * every alternative for every nested value and returns a copy of the value.
13
+ *
14
+ * It accepts the same values as a record schema of JSON values, except that a
15
+ * JSON value cannot contain itself:
16
+ *
17
+ * - Object properties can be `undefined`. Array elements cannot.
18
+ * - Objects must be plain objects without enumerable symbol keys.
19
+ * - Own `__proto__` keys are removed. Only the objects with such a key and the
20
+ * arrays and objects that contain them are copied. Everything else is
21
+ * returned as is.
22
+ *
23
+ * @returns The JSON value, or the path to the first invalid value.
24
+ */
25
+ export function normalizeJsonValue(
26
+ value: unknown,
27
+ ):
28
+ | { success: true; value: JSONValue }
29
+ | { success: false; path: PropertyKey[] } {
30
+ const path: PropertyKey[] = [];
31
+ const normalized = normalize(value, path, new Set());
32
+
33
+ return normalized === invalid
34
+ ? { success: false, path }
35
+ : { success: true, value: normalized as JSONValue };
36
+ }
37
+
38
+ /**
39
+ * Returns the normalized value, or `invalid` with the path to the invalid
40
+ * value in `path`.
41
+ *
42
+ * @param ancestors - The arrays and objects that contain `value`. Tracking
43
+ * them instead of all visited values detects cycles with memory proportional
44
+ * to the nesting depth.
45
+ */
46
+ function normalize(
47
+ value: unknown,
48
+ path: PropertyKey[],
49
+ ancestors: Set<object>,
50
+ ): unknown {
51
+ switch (typeof value) {
52
+ case 'string':
53
+ case 'boolean':
54
+ return value;
55
+ case 'number':
56
+ return Number.isFinite(value) ? value : invalid;
57
+ case 'object': {
58
+ if (value === null) return value;
59
+ if (ancestors.has(value)) return invalid;
60
+
61
+ ancestors.add(value);
62
+ const normalized = Array.isArray(value)
63
+ ? normalizeArray(value, path, ancestors)
64
+ : normalizeObject(value as Record<string, unknown>, path, ancestors);
65
+ ancestors.delete(value);
66
+
67
+ return normalized;
68
+ }
69
+ default:
70
+ // undefined, bigint, symbol, function
71
+ return invalid;
72
+ }
73
+ }
74
+
75
+ function normalizeArray(
76
+ array: unknown[],
77
+ path: PropertyKey[],
78
+ ancestors: Set<object>,
79
+ ): unknown {
80
+ let copy: unknown[] | undefined;
81
+
82
+ for (let index = 0; index < array.length; index++) {
83
+ const element = array[index];
84
+
85
+ path.push(index);
86
+ const normalized = normalize(element, path, ancestors);
87
+ if (normalized === invalid) return invalid;
88
+ path.pop();
89
+
90
+ if (copy != null) {
91
+ copy.push(normalized);
92
+ } else if (normalized !== element) {
93
+ copy = array.slice(0, index);
94
+ copy.push(normalized);
95
+ }
96
+ }
97
+
98
+ return copy ?? array;
99
+ }
100
+
101
+ function normalizeObject(
102
+ object: Record<string, unknown>,
103
+ path: PropertyKey[],
104
+ ancestors: Set<object>,
105
+ ): unknown {
106
+ if (!isPlainObject(object) || hasEnumerableSymbolKey(object)) return invalid;
107
+
108
+ const keys = Object.keys(object);
109
+ let copy: Record<string, unknown> | undefined;
110
+
111
+ for (let index = 0; index < keys.length; index++) {
112
+ const key = keys[index];
113
+
114
+ // Assigning the properties of an object with an own `__proto__` key to
115
+ // another object would replace the prototype of that object:
116
+ if (key === '__proto__') {
117
+ copy ??= copyProperties(object, keys, index);
118
+ continue;
119
+ }
120
+
121
+ const property = object[key];
122
+ let normalized = property;
123
+
124
+ if (property !== undefined) {
125
+ path.push(key);
126
+ normalized = normalize(property, path, ancestors);
127
+ if (normalized === invalid) return invalid;
128
+ path.pop();
129
+ }
130
+
131
+ if (copy != null) {
132
+ copy[key] = normalized;
133
+ } else if (normalized !== property) {
134
+ copy = copyProperties(object, keys, index);
135
+ copy[key] = normalized;
136
+ }
137
+ }
138
+
139
+ return copy ?? object;
140
+ }
141
+
142
+ /**
143
+ * Copies the properties of the first `count` keys, which are known to be valid.
144
+ */
145
+ function copyProperties(
146
+ object: Record<string, unknown>,
147
+ keys: string[],
148
+ count: number,
149
+ ): Record<string, unknown> {
150
+ const copy: Record<string, unknown> = {};
151
+ for (let index = 0; index < count; index++) {
152
+ copy[keys[index]] = object[keys[index]];
153
+ }
154
+ return copy;
155
+ }
156
+
157
+ /**
158
+ * Same check as record schemas, which accepts plain objects from other realms
159
+ * and objects without a prototype.
160
+ */
161
+ function isPlainObject(object: object): boolean {
162
+ const constructor: unknown = (object as { constructor?: unknown })
163
+ .constructor;
164
+ if (typeof constructor !== 'function') return true;
165
+
166
+ const prototype: unknown = constructor.prototype;
167
+ return (
168
+ prototype !== null &&
169
+ typeof prototype === 'object' &&
170
+ Object.prototype.hasOwnProperty.call(prototype, 'isPrototypeOf')
171
+ );
172
+ }
173
+
174
+ function hasEnumerableSymbolKey(object: object): boolean {
175
+ const symbols = Object.getOwnPropertySymbols(object);
176
+ for (let index = 0; index < symbols.length; index++) {
177
+ if (Object.prototype.propertyIsEnumerable.call(object, symbols[index])) {
178
+ return true;
179
+ }
180
+ }
181
+ return false;
182
+ }