ai 7.0.173 → 7.0.175
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 +26 -0
- package/dist/index.d.ts +68 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +292 -80
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +11 -9
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +16 -3
- package/dist/internal/index.js.map +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +3 -0
- package/docs/03-agents/06-memory.mdx +28 -1
- package/docs/03-ai-sdk-core/33-extractions.mdx +58 -1
- package/docs/03-ai-sdk-core/42-batch.mdx +54 -0
- package/docs/03-ai-sdk-core/45-provider-management.mdx +8 -0
- package/docs/07-reference/01-ai-sdk-core/14-extract.mdx +13 -13
- package/docs/07-reference/01-ai-sdk-core/20-start-batch.mdx +7 -0
- package/docs/07-reference/01-ai-sdk-core/22-get-batch-results.mdx +7 -0
- package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +14 -0
- package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +13 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +3 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +3 -0
- package/package.json +12 -12
- package/src/batch/batch-types.ts +70 -18
- package/src/batch/batch.ts +93 -7
- package/src/extract/extract.ts +23 -4
- package/src/extract/extraction-provider.ts +5 -0
- package/src/extract/extraction-result.ts +1 -1
- package/src/generate-text/stream-text.ts +1 -1
- package/src/global.ts +1 -0
- package/src/model/resolve-model.ts +25 -0
- package/src/registry/custom-provider.ts +37 -0
- package/src/registry/index.ts +1 -0
- package/src/registry/no-such-provider-error.ts +1 -0
- package/src/registry/provider-registry.ts +59 -5
- package/src/telemetry/redact-telemetry-credentials.ts +22 -4
- package/src/types/citation.ts +41 -25
- package/src/ui/validate-ui-messages.ts +46 -91
- package/src/util/normalize-json-value.ts +182 -0
package/src/types/citation.ts
CHANGED
|
@@ -1,31 +1,47 @@
|
|
|
1
1
|
import type { LanguageModelV4Citation } from '@ai-sdk/provider';
|
|
2
|
+
import type { $ZodType } from 'zod/v4/core';
|
|
2
3
|
import { z, type ZodType } from '../util/zod';
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
providerMetadataSchema,
|
|
6
|
+
type ProviderMetadata,
|
|
7
|
+
} from './provider-metadata';
|
|
4
8
|
|
|
5
9
|
/** A reference supporting a generated text part. */
|
|
6
10
|
export type Citation = LanguageModelV4Citation;
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Creates a citation schema that validates the provider metadata of the
|
|
14
|
+
* source with the given schema.
|
|
15
|
+
*/
|
|
16
|
+
export function createCitationSchema(
|
|
17
|
+
providerMetadataSchema: $ZodType<ProviderMetadata>,
|
|
18
|
+
): ZodType<Citation> {
|
|
19
|
+
return z.object({
|
|
20
|
+
source: z.discriminatedUnion('sourceType', [
|
|
21
|
+
z.object({
|
|
22
|
+
type: z.literal('source'),
|
|
23
|
+
sourceType: z.literal('url'),
|
|
24
|
+
id: z.string(),
|
|
25
|
+
url: z.string(),
|
|
26
|
+
title: z.optional(z.string()),
|
|
27
|
+
providerMetadata: z.optional(providerMetadataSchema),
|
|
28
|
+
}),
|
|
29
|
+
z.object({
|
|
30
|
+
type: z.literal('source'),
|
|
31
|
+
sourceType: z.literal('document'),
|
|
32
|
+
id: z.string(),
|
|
33
|
+
mediaType: z.string(),
|
|
34
|
+
title: z.string(),
|
|
35
|
+
filename: z.optional(z.string()),
|
|
36
|
+
providerMetadata: z.optional(providerMetadataSchema),
|
|
37
|
+
}),
|
|
38
|
+
]),
|
|
39
|
+
startIndex: z.optional(z.number().check(z.int()).check(z.nonnegative())),
|
|
40
|
+
endIndex: z.optional(z.number().check(z.int()).check(z.nonnegative())),
|
|
41
|
+
citedText: z.optional(z.string()),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const citationSchema: ZodType<Citation> = createCitationSchema(
|
|
46
|
+
providerMetadataSchema,
|
|
47
|
+
);
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { createCitationSchema } from '../types/citation';
|
|
2
|
+
import {
|
|
3
|
+
TypeValidationError,
|
|
4
|
+
type JSONObject,
|
|
5
|
+
type JSONValue,
|
|
6
|
+
} from '@ai-sdk/provider';
|
|
3
7
|
import {
|
|
4
8
|
lazySchema,
|
|
5
9
|
safeValidateTypes,
|
|
@@ -10,10 +14,10 @@ import {
|
|
|
10
14
|
type Tool,
|
|
11
15
|
} from '@ai-sdk/provider-utils';
|
|
12
16
|
import { InvalidArgumentError } from '../error';
|
|
13
|
-
import {
|
|
17
|
+
import type { ProviderMetadata } from '../types/provider-metadata';
|
|
14
18
|
import { getOwn } from '../util/get-own';
|
|
15
19
|
import { isDeepEqualData } from '../util/is-deep-equal-data';
|
|
16
|
-
import {
|
|
20
|
+
import { normalizeJsonValue } from '../util/normalize-json-value';
|
|
17
21
|
import { z, type ZodOutput, type ZodType } from '../util/zod';
|
|
18
22
|
import type {
|
|
19
23
|
DataUIPart,
|
|
@@ -27,103 +31,54 @@ import { markToolPartAsUnavailable } from './unavailable-tool';
|
|
|
27
31
|
import { warnIfUIMessageHasDeprecatedRawInput } from './warn-if-ui-message-has-deprecated-raw-input';
|
|
28
32
|
|
|
29
33
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
34
|
+
* Validates a JSON value in a single pass and reports an invalid JSON value
|
|
35
|
+
* with a single issue.
|
|
36
|
+
*
|
|
37
|
+
* UI messages are untrusted input. The recursive JSON value schema tries
|
|
38
|
+
* every alternative for every nested value and returns a copy of the value,
|
|
39
|
+
* so accepting a value would need several times the memory of the value. It
|
|
40
|
+
* also reports the issues of every alternative at every nesting level around
|
|
41
|
+
* an invalid value, so the size of a validation error would grow much faster
|
|
42
|
+
* than the input.
|
|
32
43
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
visited.add(value);
|
|
50
|
-
|
|
51
|
-
const keys = Array.isArray(value) ? value.keys() : Object.keys(value);
|
|
52
|
-
for (const key of keys) {
|
|
53
|
-
// record schemas ignore `__proto__` keys:
|
|
54
|
-
if (key === '__proto__') continue;
|
|
55
|
-
|
|
56
|
-
path.push(key);
|
|
57
|
-
const invalidPath = findInvalidJsonValuePath(
|
|
58
|
-
(value as Record<PropertyKey, unknown>)[key],
|
|
59
|
-
path,
|
|
60
|
-
visited,
|
|
61
|
-
);
|
|
62
|
-
if (invalidPath != null) return invalidPath;
|
|
63
|
-
path.pop();
|
|
64
|
-
}
|
|
65
|
-
return undefined;
|
|
44
|
+
const jsonValueSchema = z.pipe(
|
|
45
|
+
z.unknown(),
|
|
46
|
+
z.transform((value: unknown, context) => {
|
|
47
|
+
const result = normalizeJsonValue(value);
|
|
48
|
+
|
|
49
|
+
if (!result.success) {
|
|
50
|
+
context.issues.push({
|
|
51
|
+
code: 'custom',
|
|
52
|
+
message:
|
|
53
|
+
'Invalid JSON value. Expected null, a string, a finite number, ' +
|
|
54
|
+
'a boolean, an array, or an object.',
|
|
55
|
+
input: value,
|
|
56
|
+
path: result.path,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return z.NEVER;
|
|
66
60
|
}
|
|
67
|
-
default:
|
|
68
|
-
return undefined;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
61
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
*
|
|
76
|
-
* The JSON value schema is a recursive union. It reports the issues of every
|
|
77
|
-
* alternative at every nesting level around an invalid value, so the size of a
|
|
78
|
-
* validation error would grow much faster than the (untrusted) input.
|
|
79
|
-
*
|
|
80
|
-
* @param jsonValuePath - Path from the validated value to the JSON value.
|
|
81
|
-
*/
|
|
82
|
-
function withJsonValueCheck<SCHEMA extends ZodType>(
|
|
83
|
-
schema: SCHEMA,
|
|
84
|
-
jsonValuePath: string[] = [],
|
|
85
|
-
) {
|
|
86
|
-
return z.pipe(
|
|
87
|
-
z.unknown().check(context => {
|
|
88
|
-
let jsonValue = context.value;
|
|
89
|
-
for (const key of jsonValuePath) {
|
|
90
|
-
jsonValue =
|
|
91
|
-
jsonValue != null && typeof jsonValue === 'object'
|
|
92
|
-
? (jsonValue as Record<string, unknown>)[key]
|
|
93
|
-
: undefined;
|
|
94
|
-
}
|
|
62
|
+
return result.value;
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
95
65
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
context.issues.push({
|
|
99
|
-
code: 'custom',
|
|
100
|
-
message:
|
|
101
|
-
'Invalid JSON value. Expected null, a string, a finite number, ' +
|
|
102
|
-
'a boolean, an array, or an object.',
|
|
103
|
-
input: context.value,
|
|
104
|
-
path: [...jsonValuePath, ...invalidPath],
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
}),
|
|
108
|
-
schema,
|
|
109
|
-
);
|
|
110
|
-
}
|
|
66
|
+
// The values of the records are JSON values that have already been validated.
|
|
67
|
+
const validatedJsonValueSchema = z.custom<JSONValue | undefined>();
|
|
111
68
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
z.
|
|
69
|
+
const toolMetadataSchema: ZodType<JSONObject> = z.pipe(
|
|
70
|
+
jsonValueSchema,
|
|
71
|
+
z.record(z.string(), validatedJsonValueSchema),
|
|
115
72
|
);
|
|
116
73
|
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
74
|
+
const providerMetadataSchema: ZodType<ProviderMetadata> = z.pipe(
|
|
75
|
+
jsonValueSchema,
|
|
76
|
+
z.record(z.string(), z.record(z.string(), validatedJsonValueSchema)),
|
|
77
|
+
);
|
|
120
78
|
|
|
121
79
|
const providerReferenceSchema = z.record(z.string(), z.string());
|
|
122
80
|
|
|
123
|
-
const uiCitationSchema =
|
|
124
|
-
'source',
|
|
125
|
-
'providerMetadata',
|
|
126
|
-
]);
|
|
81
|
+
const uiCitationSchema = createCitationSchema(providerMetadataSchema);
|
|
127
82
|
|
|
128
83
|
function isEmptyObject(value: unknown): value is Record<string, never> {
|
|
129
84
|
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
|
+
}
|