ai 7.0.169 → 7.0.170
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 +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +159 -38
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +1 -1
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +9 -0
- package/docs/03-ai-sdk-core/32-decisions.mdx +14 -10
- package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +9 -0
- package/package.json +5 -5
- package/src/generate-object/output-strategy.ts +8 -0
- package/src/generate-object/stream-object.ts +22 -0
- package/src/generate-text/output.ts +22 -28
- package/src/generate-text/stream-text.ts +23 -0
- package/src/ui/chat.ts +6 -0
- package/src/util/array-output-stream.ts +161 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import {
|
|
2
|
+
safeParseJSON,
|
|
3
|
+
safeValidateTypes,
|
|
4
|
+
type Schema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import type { Output } from '../generate-text/output';
|
|
7
|
+
|
|
8
|
+
export type ArrayOutputStreamParser<ELEMENT> = (
|
|
9
|
+
textDelta: string,
|
|
10
|
+
) => Promise<
|
|
11
|
+
{ partial: ELEMENT[]; textDelta: string } | undefined | 'fallback'
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
// Parser state belongs to a stream, not to the reusable output specification.
|
|
15
|
+
export const arrayStreamParserFactories = new WeakMap<
|
|
16
|
+
Output,
|
|
17
|
+
() => ArrayOutputStreamParser<any> | undefined
|
|
18
|
+
>();
|
|
19
|
+
|
|
20
|
+
export function createArrayStreamParser(output: Output) {
|
|
21
|
+
return arrayStreamParserFactories.get(output)?.();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
enum Phase {
|
|
25
|
+
Prefix,
|
|
26
|
+
Elements,
|
|
27
|
+
Suffix,
|
|
28
|
+
Done,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Scan only new text and parse/validate each delimited element once. Keep the
|
|
33
|
+
* last element pending until another starts or the enclosing object closes,
|
|
34
|
+
* matching the partial JSON parser's treatment of an incomplete response.
|
|
35
|
+
* Nonstandard envelopes and malformed JSON use the existing partial parser.
|
|
36
|
+
*/
|
|
37
|
+
export function createArrayOutputStreamParser<ELEMENT>(
|
|
38
|
+
schema: Schema<ELEMENT>,
|
|
39
|
+
skipInvalidElements: boolean,
|
|
40
|
+
): ArrayOutputStreamParser<ELEMENT> {
|
|
41
|
+
let phase = Phase.Prefix;
|
|
42
|
+
let prefix = '';
|
|
43
|
+
let elementText = '';
|
|
44
|
+
let depth = 0;
|
|
45
|
+
let inString = false;
|
|
46
|
+
let escaped = false;
|
|
47
|
+
let afterComma = false;
|
|
48
|
+
let pending: [] | [ELEMENT] = [];
|
|
49
|
+
const elements: ELEMENT[] = [];
|
|
50
|
+
let publishedCount: number | undefined;
|
|
51
|
+
|
|
52
|
+
async function finishElement() {
|
|
53
|
+
const parsed = await safeParseJSON({ text: elementText });
|
|
54
|
+
if (!parsed.success) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const validated = await safeValidateTypes({ value: parsed.value, schema });
|
|
59
|
+
if (validated.success) {
|
|
60
|
+
pending = [validated.value];
|
|
61
|
+
} else if (!skipInvalidElements) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
elementText = '';
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function publishPending() {
|
|
69
|
+
elements.push(...pending);
|
|
70
|
+
pending = [];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return async textDelta => {
|
|
74
|
+
for (const char of textDelta) {
|
|
75
|
+
if (phase === Phase.Prefix) {
|
|
76
|
+
prefix += char;
|
|
77
|
+
if (char === '[') {
|
|
78
|
+
// Bound prefix scanning; other valid object shapes retain their
|
|
79
|
+
// existing behavior through the stateless parser.
|
|
80
|
+
if (!/^\s*\{\s*"elements"\s*:\s*\[$/.test(prefix)) {
|
|
81
|
+
return 'fallback';
|
|
82
|
+
}
|
|
83
|
+
phase = Phase.Elements;
|
|
84
|
+
} else if (prefix.length > 256) {
|
|
85
|
+
return 'fallback';
|
|
86
|
+
}
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (phase === Phase.Suffix || phase === Phase.Done) {
|
|
91
|
+
if (/\s/.test(char)) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (phase === Phase.Suffix && char === '}') {
|
|
95
|
+
publishPending();
|
|
96
|
+
phase = Phase.Done;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
return 'fallback';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!inString && depth === 0 && (char === ',' || char === ']')) {
|
|
103
|
+
if (elementText.length > 0) {
|
|
104
|
+
if (!(await finishElement())) {
|
|
105
|
+
return 'fallback';
|
|
106
|
+
}
|
|
107
|
+
} else if (char === ',' || afterComma) {
|
|
108
|
+
return 'fallback';
|
|
109
|
+
}
|
|
110
|
+
afterComma = char === ',';
|
|
111
|
+
if (char === ']') {
|
|
112
|
+
phase = Phase.Suffix;
|
|
113
|
+
}
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (elementText.length === 0) {
|
|
118
|
+
if (/\s/.test(char)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
publishPending();
|
|
122
|
+
afterComma = false;
|
|
123
|
+
}
|
|
124
|
+
elementText += char;
|
|
125
|
+
|
|
126
|
+
if (inString) {
|
|
127
|
+
if (escaped) {
|
|
128
|
+
escaped = false;
|
|
129
|
+
} else if (char === '\\') {
|
|
130
|
+
escaped = true;
|
|
131
|
+
} else if (char === '"') {
|
|
132
|
+
inString = false;
|
|
133
|
+
}
|
|
134
|
+
} else if (char === '"') {
|
|
135
|
+
inString = true;
|
|
136
|
+
} else if (char === '{' || char === '[') {
|
|
137
|
+
depth++;
|
|
138
|
+
} else if (char === '}' || char === ']') {
|
|
139
|
+
depth--;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (phase === Phase.Prefix || publishedCount === elements.length) {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const previousCount = publishedCount ?? 0;
|
|
148
|
+
const delta =
|
|
149
|
+
(publishedCount == null ? '[' : '') +
|
|
150
|
+
(previousCount > 0 && elements.length > previousCount ? ',' : '') +
|
|
151
|
+
elements
|
|
152
|
+
.slice(previousCount)
|
|
153
|
+
.map(value => JSON.stringify(value))
|
|
154
|
+
.join(',') +
|
|
155
|
+
(phase === Phase.Done ? ']' : '');
|
|
156
|
+
publishedCount = elements.length;
|
|
157
|
+
|
|
158
|
+
// A snapshot prevents subsequent chunks from mutating already emitted arrays.
|
|
159
|
+
return { partial: elements.slice(), textDelta: delta };
|
|
160
|
+
};
|
|
161
|
+
}
|