ai 3.1.0-canary.3 → 3.1.0
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/README.md +1 -1
- package/dist/index.d.mts +982 -24
- package/dist/index.d.ts +982 -24
- package/dist/index.js +1748 -175
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1723 -174
- package/dist/index.mjs.map +1 -1
- package/package.json +14 -31
- package/prompts/dist/index.d.mts +13 -1
- package/prompts/dist/index.d.ts +13 -1
- package/prompts/dist/index.js +13 -0
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs +12 -0
- package/prompts/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +27 -6
- package/react/dist/index.d.ts +31 -8
- package/react/dist/index.js +155 -141
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +154 -141
- package/react/dist/index.mjs.map +1 -1
- package/react/dist/index.server.d.mts +4 -2
- package/react/dist/index.server.d.ts +4 -2
- package/react/dist/index.server.js.map +1 -1
- package/react/dist/index.server.mjs.map +1 -1
- package/rsc/dist/index.d.ts +385 -20
- package/rsc/dist/rsc-client.d.mts +1 -1
- package/rsc/dist/rsc-client.mjs +2 -0
- package/rsc/dist/rsc-client.mjs.map +1 -1
- package/rsc/dist/rsc-server.d.mts +367 -20
- package/rsc/dist/rsc-server.mjs +676 -35
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/rsc/dist/rsc-shared.d.mts +24 -9
- package/rsc/dist/rsc-shared.mjs +98 -4
- package/rsc/dist/rsc-shared.mjs.map +1 -1
- package/solid/dist/index.d.mts +7 -3
- package/solid/dist/index.d.ts +7 -3
- package/solid/dist/index.js +106 -107
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +106 -107
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +7 -3
- package/svelte/dist/index.d.ts +7 -3
- package/svelte/dist/index.js +109 -109
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +109 -109
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +7 -3
- package/vue/dist/index.d.ts +7 -3
- package/vue/dist/index.js +106 -107
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +106 -107
- package/vue/dist/index.mjs.map +1 -1
- package/ai-model-specification/dist/index.d.mts +0 -606
- package/ai-model-specification/dist/index.d.ts +0 -606
- package/ai-model-specification/dist/index.js +0 -617
- package/ai-model-specification/dist/index.js.map +0 -1
- package/ai-model-specification/dist/index.mjs +0 -560
- package/ai-model-specification/dist/index.mjs.map +0 -1
- package/core/dist/index.d.mts +0 -590
- package/core/dist/index.d.ts +0 -590
- package/core/dist/index.js +0 -1528
- package/core/dist/index.js.map +0 -1
- package/core/dist/index.mjs +0 -1481
- package/core/dist/index.mjs.map +0 -1
- package/provider/dist/index.d.mts +0 -429
- package/provider/dist/index.d.ts +0 -429
- package/provider/dist/index.js +0 -1194
- package/provider/dist/index.js.map +0 -1
- package/provider/dist/index.mjs +0 -1158
- package/provider/dist/index.mjs.map +0 -1
package/core/dist/index.mjs
DELETED
@@ -1,1481 +0,0 @@
|
|
1
|
-
// core/generate-object/generate-object.ts
|
2
|
-
import zodToJsonSchema from "zod-to-json-schema";
|
3
|
-
|
4
|
-
// ai-model-specification/errors/ai-invalid-argument-error.ts
|
5
|
-
var AI_InvalidArgumentError = class extends Error {
|
6
|
-
// readonly learnMore =
|
7
|
-
// 'https://sdk.vercel.com/docs/ai/errors/ai_invalid_argument_error';
|
8
|
-
constructor({
|
9
|
-
parameter,
|
10
|
-
value,
|
11
|
-
message
|
12
|
-
}) {
|
13
|
-
super(`Invalid argument for parameter ${parameter}: ${message}`);
|
14
|
-
this.name = "AI_InvalidArgumentError";
|
15
|
-
this.parameter = parameter;
|
16
|
-
this.value = value;
|
17
|
-
}
|
18
|
-
toJSON() {
|
19
|
-
return {
|
20
|
-
name: this.name,
|
21
|
-
message: this.message,
|
22
|
-
stack: this.stack,
|
23
|
-
parameter: this.parameter,
|
24
|
-
value: this.value
|
25
|
-
};
|
26
|
-
}
|
27
|
-
};
|
28
|
-
|
29
|
-
// ai-model-specification/util/get-error-message.ts
|
30
|
-
function getErrorMessage(error) {
|
31
|
-
if (error == null) {
|
32
|
-
return "unknown error";
|
33
|
-
}
|
34
|
-
if (typeof error === "string") {
|
35
|
-
return error;
|
36
|
-
}
|
37
|
-
if (error instanceof Error) {
|
38
|
-
return error.message;
|
39
|
-
}
|
40
|
-
return JSON.stringify(error);
|
41
|
-
}
|
42
|
-
|
43
|
-
// ai-model-specification/errors/json-parse-error.ts
|
44
|
-
var JSONParseError = class extends Error {
|
45
|
-
constructor({ text, cause }) {
|
46
|
-
super(
|
47
|
-
`JSON parsing failed: Text: ${text}.
|
48
|
-
Error message: ${getErrorMessage(cause)}`
|
49
|
-
);
|
50
|
-
this.name = "JSONParseError";
|
51
|
-
this.cause = cause;
|
52
|
-
this.text = text;
|
53
|
-
}
|
54
|
-
toJSON() {
|
55
|
-
return {
|
56
|
-
name: this.name,
|
57
|
-
message: this.message,
|
58
|
-
cause: this.cause,
|
59
|
-
stack: this.stack,
|
60
|
-
valueText: this.text
|
61
|
-
};
|
62
|
-
}
|
63
|
-
};
|
64
|
-
|
65
|
-
// ai-model-specification/errors/no-object-generated-error.ts
|
66
|
-
var NoTextGeneratedError = class extends Error {
|
67
|
-
constructor() {
|
68
|
-
super(`No text generated.`);
|
69
|
-
this.name = "NoTextGeneratedError";
|
70
|
-
}
|
71
|
-
toJSON() {
|
72
|
-
return {
|
73
|
-
name: this.name,
|
74
|
-
cause: this.cause,
|
75
|
-
message: this.message,
|
76
|
-
stack: this.stack
|
77
|
-
};
|
78
|
-
}
|
79
|
-
};
|
80
|
-
|
81
|
-
// ai-model-specification/errors/retry-error.ts
|
82
|
-
var RetryError = class extends Error {
|
83
|
-
constructor({
|
84
|
-
message,
|
85
|
-
reason,
|
86
|
-
errors
|
87
|
-
}) {
|
88
|
-
super(message);
|
89
|
-
this.name = "RetryError";
|
90
|
-
this.reason = reason;
|
91
|
-
this.errors = errors;
|
92
|
-
this.lastError = errors[errors.length - 1];
|
93
|
-
}
|
94
|
-
toJSON() {
|
95
|
-
return {
|
96
|
-
name: this.name,
|
97
|
-
message: this.message,
|
98
|
-
reason: this.reason,
|
99
|
-
lastError: this.lastError,
|
100
|
-
errors: this.errors
|
101
|
-
};
|
102
|
-
}
|
103
|
-
};
|
104
|
-
|
105
|
-
// ai-model-specification/errors/type-validation-error.ts
|
106
|
-
var TypeValidationError = class extends Error {
|
107
|
-
constructor({ value, cause }) {
|
108
|
-
super(
|
109
|
-
`Type validation failed: Value: ${JSON.stringify(value)}.
|
110
|
-
Error message: ${getErrorMessage(cause)}`
|
111
|
-
);
|
112
|
-
this.name = "TypeValidationError";
|
113
|
-
this.cause = cause;
|
114
|
-
this.value = value;
|
115
|
-
}
|
116
|
-
toJSON() {
|
117
|
-
return {
|
118
|
-
name: this.name,
|
119
|
-
message: this.message,
|
120
|
-
cause: this.cause,
|
121
|
-
stack: this.stack,
|
122
|
-
value: this.value
|
123
|
-
};
|
124
|
-
}
|
125
|
-
};
|
126
|
-
|
127
|
-
// ai-model-specification/util/parse-json.ts
|
128
|
-
import SecureJSON from "secure-json-parse";
|
129
|
-
|
130
|
-
// ai-model-specification/util/validate-types.ts
|
131
|
-
function safeValidateTypes({
|
132
|
-
value,
|
133
|
-
schema
|
134
|
-
}) {
|
135
|
-
try {
|
136
|
-
const validationResult = schema.safeParse(value);
|
137
|
-
if (validationResult.success) {
|
138
|
-
return {
|
139
|
-
success: true,
|
140
|
-
value: validationResult.data
|
141
|
-
};
|
142
|
-
}
|
143
|
-
return {
|
144
|
-
success: false,
|
145
|
-
error: new TypeValidationError({
|
146
|
-
value,
|
147
|
-
cause: validationResult.error
|
148
|
-
})
|
149
|
-
};
|
150
|
-
} catch (error) {
|
151
|
-
return {
|
152
|
-
success: false,
|
153
|
-
error: error instanceof TypeValidationError ? error : new TypeValidationError({ value, cause: error })
|
154
|
-
};
|
155
|
-
}
|
156
|
-
}
|
157
|
-
|
158
|
-
// ai-model-specification/util/parse-json.ts
|
159
|
-
function safeParseJSON({
|
160
|
-
text,
|
161
|
-
schema
|
162
|
-
}) {
|
163
|
-
try {
|
164
|
-
const value = SecureJSON.parse(text);
|
165
|
-
if (schema == null) {
|
166
|
-
return {
|
167
|
-
success: true,
|
168
|
-
value
|
169
|
-
};
|
170
|
-
}
|
171
|
-
return safeValidateTypes({ value, schema });
|
172
|
-
} catch (error) {
|
173
|
-
return {
|
174
|
-
success: false,
|
175
|
-
error: error instanceof JSONParseError ? error : new JSONParseError({ text, cause: error })
|
176
|
-
};
|
177
|
-
}
|
178
|
-
}
|
179
|
-
|
180
|
-
// ai-model-specification/util/uint8-utils.ts
|
181
|
-
function convertBase64ToUint8Array(base64String) {
|
182
|
-
const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
|
183
|
-
const latin1string = globalThis.atob(base64Url);
|
184
|
-
return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
|
185
|
-
}
|
186
|
-
function convertUint8ArrayToBase64(array) {
|
187
|
-
let latin1string = "";
|
188
|
-
for (const value of array) {
|
189
|
-
latin1string += String.fromCodePoint(value);
|
190
|
-
}
|
191
|
-
return globalThis.btoa(latin1string);
|
192
|
-
}
|
193
|
-
|
194
|
-
// core/generate-text/token-usage.ts
|
195
|
-
function calculateTokenUsage(usage) {
|
196
|
-
return {
|
197
|
-
promptTokens: usage.promptTokens,
|
198
|
-
completionTokens: usage.completionTokens,
|
199
|
-
totalTokens: usage.promptTokens + usage.completionTokens
|
200
|
-
};
|
201
|
-
}
|
202
|
-
|
203
|
-
// core/prompt/data-content.ts
|
204
|
-
function convertDataContentToBase64String(content) {
|
205
|
-
if (typeof content === "string") {
|
206
|
-
return content;
|
207
|
-
}
|
208
|
-
if (content instanceof ArrayBuffer) {
|
209
|
-
return convertUint8ArrayToBase64(new Uint8Array(content));
|
210
|
-
}
|
211
|
-
return convertUint8ArrayToBase64(content);
|
212
|
-
}
|
213
|
-
function convertDataContentToUint8Array(content) {
|
214
|
-
if (content instanceof Uint8Array) {
|
215
|
-
return content;
|
216
|
-
}
|
217
|
-
if (typeof content === "string") {
|
218
|
-
return convertBase64ToUint8Array(content);
|
219
|
-
}
|
220
|
-
if (content instanceof ArrayBuffer) {
|
221
|
-
return new Uint8Array(content);
|
222
|
-
}
|
223
|
-
throw new Error(
|
224
|
-
`Invalid data content. Expected a string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.`
|
225
|
-
);
|
226
|
-
}
|
227
|
-
|
228
|
-
// core/prompt/convert-to-language-model-prompt.ts
|
229
|
-
function convertToLanguageModelPrompt({
|
230
|
-
system,
|
231
|
-
prompt,
|
232
|
-
messages
|
233
|
-
}) {
|
234
|
-
if (prompt == null && messages == null) {
|
235
|
-
throw new Error("prompt or messages must be defined");
|
236
|
-
}
|
237
|
-
if (prompt != null && messages != null) {
|
238
|
-
throw new Error("prompt and messages cannot be defined at the same time");
|
239
|
-
}
|
240
|
-
const languageModelMessages = [];
|
241
|
-
if (system != null) {
|
242
|
-
languageModelMessages.push({ role: "system", content: system });
|
243
|
-
}
|
244
|
-
if (typeof prompt === "string") {
|
245
|
-
languageModelMessages.push({
|
246
|
-
role: "user",
|
247
|
-
content: [{ type: "text", text: prompt }]
|
248
|
-
});
|
249
|
-
} else {
|
250
|
-
messages = messages;
|
251
|
-
languageModelMessages.push(
|
252
|
-
...messages.map((message) => {
|
253
|
-
switch (message.role) {
|
254
|
-
case "user": {
|
255
|
-
if (typeof message.content === "string") {
|
256
|
-
return {
|
257
|
-
role: "user",
|
258
|
-
content: [{ type: "text", text: message.content }]
|
259
|
-
};
|
260
|
-
}
|
261
|
-
return {
|
262
|
-
role: "user",
|
263
|
-
content: message.content.map(
|
264
|
-
(part) => {
|
265
|
-
switch (part.type) {
|
266
|
-
case "text": {
|
267
|
-
return part;
|
268
|
-
}
|
269
|
-
case "image": {
|
270
|
-
return {
|
271
|
-
type: "image",
|
272
|
-
image: part.image instanceof URL ? part.image : convertDataContentToUint8Array(part.image),
|
273
|
-
mimeType: part.mimeType
|
274
|
-
};
|
275
|
-
}
|
276
|
-
}
|
277
|
-
}
|
278
|
-
)
|
279
|
-
};
|
280
|
-
}
|
281
|
-
case "assistant": {
|
282
|
-
if (typeof message.content === "string") {
|
283
|
-
return {
|
284
|
-
role: "assistant",
|
285
|
-
content: [{ type: "text", text: message.content }]
|
286
|
-
};
|
287
|
-
}
|
288
|
-
return { role: "assistant", content: message.content };
|
289
|
-
}
|
290
|
-
case "tool": {
|
291
|
-
return message;
|
292
|
-
}
|
293
|
-
}
|
294
|
-
})
|
295
|
-
);
|
296
|
-
}
|
297
|
-
return languageModelMessages;
|
298
|
-
}
|
299
|
-
|
300
|
-
// core/prompt/get-input-format.ts
|
301
|
-
function getInputFormat({
|
302
|
-
prompt,
|
303
|
-
messages
|
304
|
-
}) {
|
305
|
-
if (prompt == null && messages == null) {
|
306
|
-
throw new Error("prompt or messages must be defined");
|
307
|
-
}
|
308
|
-
if (prompt != null && messages != null) {
|
309
|
-
throw new Error("prompt and messages cannot be defined at the same time");
|
310
|
-
}
|
311
|
-
return prompt != null ? "prompt" : "messages";
|
312
|
-
}
|
313
|
-
|
314
|
-
// core/prompt/validate-call-settings.ts
|
315
|
-
function validateCallSettings(settings) {
|
316
|
-
if (settings.maxTokens != null) {
|
317
|
-
if (!Number.isInteger(settings.maxTokens)) {
|
318
|
-
throw new AI_InvalidArgumentError({
|
319
|
-
parameter: "maxTokens",
|
320
|
-
value: settings.maxTokens,
|
321
|
-
message: "maxTokens must be an integer"
|
322
|
-
});
|
323
|
-
}
|
324
|
-
if (settings.maxTokens < 1) {
|
325
|
-
throw new AI_InvalidArgumentError({
|
326
|
-
parameter: "maxTokens",
|
327
|
-
value: settings.maxTokens,
|
328
|
-
message: "maxTokens must be >= 1"
|
329
|
-
});
|
330
|
-
}
|
331
|
-
}
|
332
|
-
if (settings.temperature != null) {
|
333
|
-
if (typeof settings.temperature !== "number") {
|
334
|
-
throw new AI_InvalidArgumentError({
|
335
|
-
parameter: "temperature",
|
336
|
-
value: settings.temperature,
|
337
|
-
message: "temperature must be a number"
|
338
|
-
});
|
339
|
-
}
|
340
|
-
if (settings.temperature < 0 || settings.temperature > 1) {
|
341
|
-
throw new AI_InvalidArgumentError({
|
342
|
-
parameter: "temperature",
|
343
|
-
value: settings.temperature,
|
344
|
-
message: "temperature must be between 0 and 1 (inclusive)"
|
345
|
-
});
|
346
|
-
}
|
347
|
-
}
|
348
|
-
if (settings.topP != null) {
|
349
|
-
if (typeof settings.topP !== "number") {
|
350
|
-
throw new AI_InvalidArgumentError({
|
351
|
-
parameter: "topP",
|
352
|
-
value: settings.topP,
|
353
|
-
message: "topP must be a number"
|
354
|
-
});
|
355
|
-
}
|
356
|
-
if (settings.topP < 0 || settings.topP > 1) {
|
357
|
-
throw new AI_InvalidArgumentError({
|
358
|
-
parameter: "topP",
|
359
|
-
value: settings.topP,
|
360
|
-
message: "topP must be between 0 and 1 (inclusive)"
|
361
|
-
});
|
362
|
-
}
|
363
|
-
}
|
364
|
-
if (settings.presencePenalty != null) {
|
365
|
-
if (typeof settings.presencePenalty !== "number") {
|
366
|
-
throw new AI_InvalidArgumentError({
|
367
|
-
parameter: "presencePenalty",
|
368
|
-
value: settings.presencePenalty,
|
369
|
-
message: "presencePenalty must be a number"
|
370
|
-
});
|
371
|
-
}
|
372
|
-
if (settings.presencePenalty < -1 || settings.presencePenalty > 1) {
|
373
|
-
throw new AI_InvalidArgumentError({
|
374
|
-
parameter: "presencePenalty",
|
375
|
-
value: settings.presencePenalty,
|
376
|
-
message: "presencePenalty must be between -1 and 1 (inclusive)"
|
377
|
-
});
|
378
|
-
}
|
379
|
-
}
|
380
|
-
if (settings.frequencyPenalty != null) {
|
381
|
-
if (typeof settings.frequencyPenalty !== "number") {
|
382
|
-
throw new AI_InvalidArgumentError({
|
383
|
-
parameter: "frequencyPenalty",
|
384
|
-
value: settings.frequencyPenalty,
|
385
|
-
message: "frequencyPenalty must be a number"
|
386
|
-
});
|
387
|
-
}
|
388
|
-
if (settings.frequencyPenalty < -1 || settings.frequencyPenalty > 1) {
|
389
|
-
throw new AI_InvalidArgumentError({
|
390
|
-
parameter: "frequencyPenalty",
|
391
|
-
value: settings.frequencyPenalty,
|
392
|
-
message: "frequencyPenalty must be between -1 and 1 (inclusive)"
|
393
|
-
});
|
394
|
-
}
|
395
|
-
}
|
396
|
-
if (settings.seed != null) {
|
397
|
-
if (!Number.isInteger(settings.seed)) {
|
398
|
-
throw new AI_InvalidArgumentError({
|
399
|
-
parameter: "seed",
|
400
|
-
value: settings.seed,
|
401
|
-
message: "seed must be an integer"
|
402
|
-
});
|
403
|
-
}
|
404
|
-
}
|
405
|
-
return settings;
|
406
|
-
}
|
407
|
-
|
408
|
-
// core/util/delay.ts
|
409
|
-
async function delay(delayInMs) {
|
410
|
-
return new Promise((resolve) => setTimeout(resolve, delayInMs));
|
411
|
-
}
|
412
|
-
|
413
|
-
// core/util/retry-with-exponential-backoff.ts
|
414
|
-
var retryWithExponentialBackoff = ({
|
415
|
-
maxRetries = 2,
|
416
|
-
initialDelayInMs = 2e3,
|
417
|
-
backoffFactor = 2
|
418
|
-
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
419
|
-
maxRetries,
|
420
|
-
delayInMs: initialDelayInMs,
|
421
|
-
backoffFactor
|
422
|
-
});
|
423
|
-
async function _retryWithExponentialBackoff(f, {
|
424
|
-
maxRetries,
|
425
|
-
delayInMs,
|
426
|
-
backoffFactor
|
427
|
-
}, errors = []) {
|
428
|
-
try {
|
429
|
-
return await f();
|
430
|
-
} catch (error) {
|
431
|
-
if (maxRetries === 0) {
|
432
|
-
throw error;
|
433
|
-
}
|
434
|
-
const errorMessage = getErrorMessage(error);
|
435
|
-
const newErrors = [...errors, error];
|
436
|
-
const tryNumber = newErrors.length;
|
437
|
-
if (tryNumber > maxRetries) {
|
438
|
-
throw new RetryError({
|
439
|
-
message: `Failed after ${tryNumber} tries. Last error: ${errorMessage}`,
|
440
|
-
reason: "maxRetriesExceeded",
|
441
|
-
errors: newErrors
|
442
|
-
});
|
443
|
-
}
|
444
|
-
if (error instanceof Error) {
|
445
|
-
if (error.name === "AbortError") {
|
446
|
-
throw error;
|
447
|
-
}
|
448
|
-
if (
|
449
|
-
// deal with bundling duplication by using names
|
450
|
-
error.name === "ApiCallError" && error.isRetryable === true && tryNumber <= maxRetries
|
451
|
-
) {
|
452
|
-
await delay(delayInMs);
|
453
|
-
return _retryWithExponentialBackoff(
|
454
|
-
f,
|
455
|
-
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
456
|
-
newErrors
|
457
|
-
);
|
458
|
-
}
|
459
|
-
}
|
460
|
-
throw new RetryError({
|
461
|
-
message: `Failed after ${tryNumber} tries with non-retryable error: '${errorMessage}'`,
|
462
|
-
reason: "errorNotRetryable",
|
463
|
-
errors: newErrors
|
464
|
-
});
|
465
|
-
}
|
466
|
-
}
|
467
|
-
|
468
|
-
// core/generate-object/inject-json-schema-into-system.ts
|
469
|
-
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
470
|
-
var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
|
471
|
-
function injectJsonSchemaIntoSystem({
|
472
|
-
system,
|
473
|
-
schema,
|
474
|
-
schemaPrefix = DEFAULT_SCHEMA_PREFIX,
|
475
|
-
schemaSuffix = DEFAULT_SCHEMA_SUFFIX
|
476
|
-
}) {
|
477
|
-
return [
|
478
|
-
system,
|
479
|
-
system != null ? "" : null,
|
480
|
-
// add a newline if system is not null
|
481
|
-
schemaPrefix,
|
482
|
-
JSON.stringify(schema),
|
483
|
-
schemaSuffix
|
484
|
-
].filter((line) => line != null).join("\n");
|
485
|
-
}
|
486
|
-
|
487
|
-
// core/generate-object/generate-object.ts
|
488
|
-
async function generateObject({
|
489
|
-
model,
|
490
|
-
schema,
|
491
|
-
mode,
|
492
|
-
system,
|
493
|
-
prompt,
|
494
|
-
messages,
|
495
|
-
maxRetries,
|
496
|
-
abortSignal,
|
497
|
-
...settings
|
498
|
-
}) {
|
499
|
-
var _a, _b;
|
500
|
-
const retry = retryWithExponentialBackoff({ maxRetries });
|
501
|
-
const jsonSchema = zodToJsonSchema(schema);
|
502
|
-
if (mode === "auto" || mode == null) {
|
503
|
-
mode = model.defaultObjectGenerationMode;
|
504
|
-
}
|
505
|
-
let result;
|
506
|
-
let finishReason;
|
507
|
-
let usage;
|
508
|
-
switch (mode) {
|
509
|
-
case "json": {
|
510
|
-
const generateResult = await retry(
|
511
|
-
() => model.doGenerate({
|
512
|
-
mode: { type: "object-json" },
|
513
|
-
...validateCallSettings(settings),
|
514
|
-
inputFormat: getInputFormat({ prompt, messages }),
|
515
|
-
prompt: convertToLanguageModelPrompt({
|
516
|
-
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
517
|
-
prompt,
|
518
|
-
messages
|
519
|
-
}),
|
520
|
-
abortSignal
|
521
|
-
})
|
522
|
-
);
|
523
|
-
if (generateResult.text === void 0) {
|
524
|
-
throw new NoTextGeneratedError();
|
525
|
-
}
|
526
|
-
result = generateResult.text;
|
527
|
-
finishReason = generateResult.finishReason;
|
528
|
-
usage = generateResult.usage;
|
529
|
-
break;
|
530
|
-
}
|
531
|
-
case "grammar": {
|
532
|
-
const generateResult = await retry(
|
533
|
-
() => model.doGenerate({
|
534
|
-
mode: { type: "object-grammar", schema: jsonSchema },
|
535
|
-
...settings,
|
536
|
-
inputFormat: getInputFormat({ prompt, messages }),
|
537
|
-
prompt: convertToLanguageModelPrompt({
|
538
|
-
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
539
|
-
prompt,
|
540
|
-
messages
|
541
|
-
}),
|
542
|
-
abortSignal
|
543
|
-
})
|
544
|
-
);
|
545
|
-
if (generateResult.text === void 0) {
|
546
|
-
throw new NoTextGeneratedError();
|
547
|
-
}
|
548
|
-
result = generateResult.text;
|
549
|
-
finishReason = generateResult.finishReason;
|
550
|
-
usage = generateResult.usage;
|
551
|
-
break;
|
552
|
-
}
|
553
|
-
case "tool": {
|
554
|
-
const generateResult = await retry(
|
555
|
-
() => model.doGenerate({
|
556
|
-
mode: {
|
557
|
-
type: "object-tool",
|
558
|
-
tool: {
|
559
|
-
type: "function",
|
560
|
-
name: "json",
|
561
|
-
description: "Respond with a JSON object.",
|
562
|
-
parameters: jsonSchema
|
563
|
-
}
|
564
|
-
},
|
565
|
-
...settings,
|
566
|
-
inputFormat: getInputFormat({ prompt, messages }),
|
567
|
-
prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
|
568
|
-
abortSignal
|
569
|
-
})
|
570
|
-
);
|
571
|
-
const functionArgs = (_b = (_a = generateResult.toolCalls) == null ? void 0 : _a[0]) == null ? void 0 : _b.args;
|
572
|
-
if (functionArgs === void 0) {
|
573
|
-
throw new NoTextGeneratedError();
|
574
|
-
}
|
575
|
-
result = functionArgs;
|
576
|
-
finishReason = generateResult.finishReason;
|
577
|
-
usage = generateResult.usage;
|
578
|
-
break;
|
579
|
-
}
|
580
|
-
case void 0: {
|
581
|
-
throw new Error("Model does not have a default object generation mode.");
|
582
|
-
}
|
583
|
-
default: {
|
584
|
-
const _exhaustiveCheck = mode;
|
585
|
-
throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
|
586
|
-
}
|
587
|
-
}
|
588
|
-
const parseResult = safeParseJSON({ text: result, schema });
|
589
|
-
if (!parseResult.success) {
|
590
|
-
throw parseResult.error;
|
591
|
-
}
|
592
|
-
return new GenerateObjectResult({
|
593
|
-
object: parseResult.value,
|
594
|
-
finishReason,
|
595
|
-
usage: calculateTokenUsage(usage)
|
596
|
-
});
|
597
|
-
}
|
598
|
-
var GenerateObjectResult = class {
|
599
|
-
constructor(options) {
|
600
|
-
this.object = options.object;
|
601
|
-
this.finishReason = options.finishReason;
|
602
|
-
this.usage = options.usage;
|
603
|
-
}
|
604
|
-
};
|
605
|
-
|
606
|
-
// core/generate-object/stream-object.ts
|
607
|
-
import zodToJsonSchema2 from "zod-to-json-schema";
|
608
|
-
|
609
|
-
// core/util/is-deep-equal-data.ts
|
610
|
-
function isDeepEqualData(obj1, obj2) {
|
611
|
-
if (obj1 === obj2)
|
612
|
-
return true;
|
613
|
-
if (obj1 == null || obj2 == null)
|
614
|
-
return false;
|
615
|
-
if (typeof obj1 !== "object" && typeof obj2 !== "object")
|
616
|
-
return obj1 === obj2;
|
617
|
-
if (obj1.constructor !== obj2.constructor)
|
618
|
-
return false;
|
619
|
-
if (obj1 instanceof Date && obj2 instanceof Date) {
|
620
|
-
return obj1.getTime() === obj2.getTime();
|
621
|
-
}
|
622
|
-
if (Array.isArray(obj1)) {
|
623
|
-
if (obj1.length !== obj2.length)
|
624
|
-
return false;
|
625
|
-
for (let i = 0; i < obj1.length; i++) {
|
626
|
-
if (!isDeepEqualData(obj1[i], obj2[i]))
|
627
|
-
return false;
|
628
|
-
}
|
629
|
-
return true;
|
630
|
-
}
|
631
|
-
const keys1 = Object.keys(obj1);
|
632
|
-
const keys2 = Object.keys(obj2);
|
633
|
-
if (keys1.length !== keys2.length)
|
634
|
-
return false;
|
635
|
-
for (const key of keys1) {
|
636
|
-
if (!keys2.includes(key))
|
637
|
-
return false;
|
638
|
-
if (!isDeepEqualData(obj1[key], obj2[key]))
|
639
|
-
return false;
|
640
|
-
}
|
641
|
-
return true;
|
642
|
-
}
|
643
|
-
|
644
|
-
// core/util/parse-partial-json.ts
|
645
|
-
import SecureJSON2 from "secure-json-parse";
|
646
|
-
|
647
|
-
// core/util/fix-json.ts
|
648
|
-
function fixJson(input) {
|
649
|
-
const stack = ["ROOT"];
|
650
|
-
let lastValidIndex = -1;
|
651
|
-
let literalStart = null;
|
652
|
-
function processValueStart(char, i, swapState) {
|
653
|
-
{
|
654
|
-
switch (char) {
|
655
|
-
case '"': {
|
656
|
-
lastValidIndex = i;
|
657
|
-
stack.pop();
|
658
|
-
stack.push(swapState);
|
659
|
-
stack.push("INSIDE_STRING");
|
660
|
-
break;
|
661
|
-
}
|
662
|
-
case "f":
|
663
|
-
case "t":
|
664
|
-
case "n": {
|
665
|
-
lastValidIndex = i;
|
666
|
-
literalStart = i;
|
667
|
-
stack.pop();
|
668
|
-
stack.push(swapState);
|
669
|
-
stack.push("INSIDE_LITERAL");
|
670
|
-
break;
|
671
|
-
}
|
672
|
-
case "-": {
|
673
|
-
stack.pop();
|
674
|
-
stack.push(swapState);
|
675
|
-
stack.push("INSIDE_NUMBER");
|
676
|
-
break;
|
677
|
-
}
|
678
|
-
case "0":
|
679
|
-
case "1":
|
680
|
-
case "2":
|
681
|
-
case "3":
|
682
|
-
case "4":
|
683
|
-
case "5":
|
684
|
-
case "6":
|
685
|
-
case "7":
|
686
|
-
case "8":
|
687
|
-
case "9": {
|
688
|
-
lastValidIndex = i;
|
689
|
-
stack.pop();
|
690
|
-
stack.push(swapState);
|
691
|
-
stack.push("INSIDE_NUMBER");
|
692
|
-
break;
|
693
|
-
}
|
694
|
-
case "{": {
|
695
|
-
lastValidIndex = i;
|
696
|
-
stack.pop();
|
697
|
-
stack.push(swapState);
|
698
|
-
stack.push("INSIDE_OBJECT_START");
|
699
|
-
break;
|
700
|
-
}
|
701
|
-
case "[": {
|
702
|
-
lastValidIndex = i;
|
703
|
-
stack.pop();
|
704
|
-
stack.push(swapState);
|
705
|
-
stack.push("INSIDE_ARRAY_START");
|
706
|
-
break;
|
707
|
-
}
|
708
|
-
}
|
709
|
-
}
|
710
|
-
}
|
711
|
-
function processAfterObjectValue(char, i) {
|
712
|
-
switch (char) {
|
713
|
-
case ",": {
|
714
|
-
stack.pop();
|
715
|
-
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
716
|
-
break;
|
717
|
-
}
|
718
|
-
case "}": {
|
719
|
-
lastValidIndex = i;
|
720
|
-
stack.pop();
|
721
|
-
break;
|
722
|
-
}
|
723
|
-
}
|
724
|
-
}
|
725
|
-
function processAfterArrayValue(char, i) {
|
726
|
-
switch (char) {
|
727
|
-
case ",": {
|
728
|
-
stack.pop();
|
729
|
-
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
730
|
-
break;
|
731
|
-
}
|
732
|
-
case "]": {
|
733
|
-
lastValidIndex = i;
|
734
|
-
stack.pop();
|
735
|
-
break;
|
736
|
-
}
|
737
|
-
}
|
738
|
-
}
|
739
|
-
for (let i = 0; i < input.length; i++) {
|
740
|
-
const char = input[i];
|
741
|
-
const currentState = stack[stack.length - 1];
|
742
|
-
switch (currentState) {
|
743
|
-
case "ROOT":
|
744
|
-
processValueStart(char, i, "FINISH");
|
745
|
-
break;
|
746
|
-
case "INSIDE_OBJECT_START": {
|
747
|
-
switch (char) {
|
748
|
-
case '"': {
|
749
|
-
stack.pop();
|
750
|
-
stack.push("INSIDE_OBJECT_KEY");
|
751
|
-
break;
|
752
|
-
}
|
753
|
-
case "}": {
|
754
|
-
stack.pop();
|
755
|
-
break;
|
756
|
-
}
|
757
|
-
}
|
758
|
-
break;
|
759
|
-
}
|
760
|
-
case "INSIDE_OBJECT_AFTER_COMMA": {
|
761
|
-
switch (char) {
|
762
|
-
case '"': {
|
763
|
-
stack.pop();
|
764
|
-
stack.push("INSIDE_OBJECT_KEY");
|
765
|
-
break;
|
766
|
-
}
|
767
|
-
}
|
768
|
-
break;
|
769
|
-
}
|
770
|
-
case "INSIDE_OBJECT_KEY": {
|
771
|
-
switch (char) {
|
772
|
-
case '"': {
|
773
|
-
stack.pop();
|
774
|
-
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
775
|
-
break;
|
776
|
-
}
|
777
|
-
}
|
778
|
-
break;
|
779
|
-
}
|
780
|
-
case "INSIDE_OBJECT_AFTER_KEY": {
|
781
|
-
switch (char) {
|
782
|
-
case ":": {
|
783
|
-
stack.pop();
|
784
|
-
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
785
|
-
break;
|
786
|
-
}
|
787
|
-
}
|
788
|
-
break;
|
789
|
-
}
|
790
|
-
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
791
|
-
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
792
|
-
break;
|
793
|
-
}
|
794
|
-
case "INSIDE_OBJECT_AFTER_VALUE": {
|
795
|
-
processAfterObjectValue(char, i);
|
796
|
-
break;
|
797
|
-
}
|
798
|
-
case "INSIDE_STRING": {
|
799
|
-
switch (char) {
|
800
|
-
case '"': {
|
801
|
-
stack.pop();
|
802
|
-
lastValidIndex = i;
|
803
|
-
break;
|
804
|
-
}
|
805
|
-
case "\\": {
|
806
|
-
stack.push("INSIDE_STRING_ESCAPE");
|
807
|
-
break;
|
808
|
-
}
|
809
|
-
default: {
|
810
|
-
lastValidIndex = i;
|
811
|
-
}
|
812
|
-
}
|
813
|
-
break;
|
814
|
-
}
|
815
|
-
case "INSIDE_ARRAY_START": {
|
816
|
-
switch (char) {
|
817
|
-
case "]": {
|
818
|
-
lastValidIndex = i;
|
819
|
-
stack.pop();
|
820
|
-
break;
|
821
|
-
}
|
822
|
-
default: {
|
823
|
-
lastValidIndex = i;
|
824
|
-
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
825
|
-
break;
|
826
|
-
}
|
827
|
-
}
|
828
|
-
break;
|
829
|
-
}
|
830
|
-
case "INSIDE_ARRAY_AFTER_VALUE": {
|
831
|
-
switch (char) {
|
832
|
-
case ",": {
|
833
|
-
stack.pop();
|
834
|
-
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
835
|
-
break;
|
836
|
-
}
|
837
|
-
case "]": {
|
838
|
-
lastValidIndex = i;
|
839
|
-
stack.pop();
|
840
|
-
break;
|
841
|
-
}
|
842
|
-
default: {
|
843
|
-
lastValidIndex = i;
|
844
|
-
break;
|
845
|
-
}
|
846
|
-
}
|
847
|
-
break;
|
848
|
-
}
|
849
|
-
case "INSIDE_ARRAY_AFTER_COMMA": {
|
850
|
-
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
851
|
-
break;
|
852
|
-
}
|
853
|
-
case "INSIDE_STRING_ESCAPE": {
|
854
|
-
stack.pop();
|
855
|
-
lastValidIndex = i;
|
856
|
-
break;
|
857
|
-
}
|
858
|
-
case "INSIDE_NUMBER": {
|
859
|
-
switch (char) {
|
860
|
-
case "0":
|
861
|
-
case "1":
|
862
|
-
case "2":
|
863
|
-
case "3":
|
864
|
-
case "4":
|
865
|
-
case "5":
|
866
|
-
case "6":
|
867
|
-
case "7":
|
868
|
-
case "8":
|
869
|
-
case "9": {
|
870
|
-
lastValidIndex = i;
|
871
|
-
break;
|
872
|
-
}
|
873
|
-
case "e":
|
874
|
-
case "E":
|
875
|
-
case "-":
|
876
|
-
case ".": {
|
877
|
-
break;
|
878
|
-
}
|
879
|
-
case ",": {
|
880
|
-
stack.pop();
|
881
|
-
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
882
|
-
processAfterArrayValue(char, i);
|
883
|
-
}
|
884
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
885
|
-
processAfterObjectValue(char, i);
|
886
|
-
}
|
887
|
-
break;
|
888
|
-
}
|
889
|
-
case "}": {
|
890
|
-
stack.pop();
|
891
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
892
|
-
processAfterObjectValue(char, i);
|
893
|
-
}
|
894
|
-
break;
|
895
|
-
}
|
896
|
-
case "]": {
|
897
|
-
stack.pop();
|
898
|
-
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
899
|
-
processAfterArrayValue(char, i);
|
900
|
-
}
|
901
|
-
break;
|
902
|
-
}
|
903
|
-
default: {
|
904
|
-
stack.pop();
|
905
|
-
break;
|
906
|
-
}
|
907
|
-
}
|
908
|
-
break;
|
909
|
-
}
|
910
|
-
case "INSIDE_LITERAL": {
|
911
|
-
const partialLiteral = input.substring(literalStart, i + 1);
|
912
|
-
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
913
|
-
stack.pop();
|
914
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
915
|
-
processAfterObjectValue(char, i);
|
916
|
-
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
917
|
-
processAfterArrayValue(char, i);
|
918
|
-
}
|
919
|
-
} else {
|
920
|
-
lastValidIndex = i;
|
921
|
-
}
|
922
|
-
break;
|
923
|
-
}
|
924
|
-
}
|
925
|
-
}
|
926
|
-
let result = input.slice(0, lastValidIndex + 1);
|
927
|
-
for (let i = stack.length - 1; i >= 0; i--) {
|
928
|
-
const state = stack[i];
|
929
|
-
switch (state) {
|
930
|
-
case "INSIDE_STRING": {
|
931
|
-
result += '"';
|
932
|
-
break;
|
933
|
-
}
|
934
|
-
case "INSIDE_OBJECT_KEY":
|
935
|
-
case "INSIDE_OBJECT_AFTER_KEY":
|
936
|
-
case "INSIDE_OBJECT_AFTER_COMMA":
|
937
|
-
case "INSIDE_OBJECT_START":
|
938
|
-
case "INSIDE_OBJECT_BEFORE_VALUE":
|
939
|
-
case "INSIDE_OBJECT_AFTER_VALUE": {
|
940
|
-
result += "}";
|
941
|
-
break;
|
942
|
-
}
|
943
|
-
case "INSIDE_ARRAY_START":
|
944
|
-
case "INSIDE_ARRAY_AFTER_COMMA":
|
945
|
-
case "INSIDE_ARRAY_AFTER_VALUE": {
|
946
|
-
result += "]";
|
947
|
-
break;
|
948
|
-
}
|
949
|
-
case "INSIDE_LITERAL": {
|
950
|
-
const partialLiteral = input.substring(literalStart, input.length);
|
951
|
-
if ("true".startsWith(partialLiteral)) {
|
952
|
-
result += "true".slice(partialLiteral.length);
|
953
|
-
} else if ("false".startsWith(partialLiteral)) {
|
954
|
-
result += "false".slice(partialLiteral.length);
|
955
|
-
} else if ("null".startsWith(partialLiteral)) {
|
956
|
-
result += "null".slice(partialLiteral.length);
|
957
|
-
}
|
958
|
-
}
|
959
|
-
}
|
960
|
-
}
|
961
|
-
return result;
|
962
|
-
}
|
963
|
-
|
964
|
-
// core/util/parse-partial-json.ts
|
965
|
-
function parsePartialJson(jsonText) {
|
966
|
-
if (jsonText == null) {
|
967
|
-
return void 0;
|
968
|
-
}
|
969
|
-
try {
|
970
|
-
return SecureJSON2.parse(jsonText);
|
971
|
-
} catch (ignored) {
|
972
|
-
try {
|
973
|
-
const fixedJsonText = fixJson(jsonText);
|
974
|
-
return SecureJSON2.parse(fixedJsonText);
|
975
|
-
} catch (ignored2) {
|
976
|
-
}
|
977
|
-
}
|
978
|
-
return void 0;
|
979
|
-
}
|
980
|
-
|
981
|
-
// core/generate-object/stream-object.ts
|
982
|
-
async function streamObject({
|
983
|
-
model,
|
984
|
-
schema,
|
985
|
-
mode,
|
986
|
-
system,
|
987
|
-
prompt,
|
988
|
-
messages,
|
989
|
-
maxRetries,
|
990
|
-
abortSignal,
|
991
|
-
...settings
|
992
|
-
}) {
|
993
|
-
const retry = retryWithExponentialBackoff({ maxRetries });
|
994
|
-
const jsonSchema = zodToJsonSchema2(schema);
|
995
|
-
let modelStream;
|
996
|
-
if (mode === "auto" || mode == null) {
|
997
|
-
mode = model.defaultObjectGenerationMode;
|
998
|
-
}
|
999
|
-
switch (mode) {
|
1000
|
-
case "json": {
|
1001
|
-
const { stream, warnings } = await retry(
|
1002
|
-
() => model.doStream({
|
1003
|
-
mode: { type: "object-json" },
|
1004
|
-
...validateCallSettings(settings),
|
1005
|
-
inputFormat: getInputFormat({ prompt, messages }),
|
1006
|
-
prompt: convertToLanguageModelPrompt({
|
1007
|
-
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
1008
|
-
prompt,
|
1009
|
-
messages
|
1010
|
-
}),
|
1011
|
-
abortSignal
|
1012
|
-
})
|
1013
|
-
);
|
1014
|
-
modelStream = stream.pipeThrough(
|
1015
|
-
new TransformStream({
|
1016
|
-
transform(chunk, controller) {
|
1017
|
-
switch (chunk.type) {
|
1018
|
-
case "text-delta":
|
1019
|
-
controller.enqueue(chunk.textDelta);
|
1020
|
-
break;
|
1021
|
-
case "error":
|
1022
|
-
controller.enqueue(chunk);
|
1023
|
-
break;
|
1024
|
-
}
|
1025
|
-
}
|
1026
|
-
})
|
1027
|
-
);
|
1028
|
-
break;
|
1029
|
-
}
|
1030
|
-
case "grammar": {
|
1031
|
-
const { stream, warnings } = await retry(
|
1032
|
-
() => model.doStream({
|
1033
|
-
mode: { type: "object-grammar", schema: jsonSchema },
|
1034
|
-
...settings,
|
1035
|
-
inputFormat: getInputFormat({ prompt, messages }),
|
1036
|
-
prompt: convertToLanguageModelPrompt({
|
1037
|
-
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
1038
|
-
prompt,
|
1039
|
-
messages
|
1040
|
-
}),
|
1041
|
-
abortSignal
|
1042
|
-
})
|
1043
|
-
);
|
1044
|
-
modelStream = stream.pipeThrough(
|
1045
|
-
new TransformStream({
|
1046
|
-
transform(chunk, controller) {
|
1047
|
-
switch (chunk.type) {
|
1048
|
-
case "text-delta":
|
1049
|
-
controller.enqueue(chunk.textDelta);
|
1050
|
-
break;
|
1051
|
-
case "error":
|
1052
|
-
controller.enqueue(chunk);
|
1053
|
-
break;
|
1054
|
-
}
|
1055
|
-
}
|
1056
|
-
})
|
1057
|
-
);
|
1058
|
-
break;
|
1059
|
-
}
|
1060
|
-
case "tool": {
|
1061
|
-
const { stream, warnings } = await retry(
|
1062
|
-
() => model.doStream({
|
1063
|
-
mode: {
|
1064
|
-
type: "object-tool",
|
1065
|
-
tool: {
|
1066
|
-
type: "function",
|
1067
|
-
name: "json",
|
1068
|
-
description: "Respond with a JSON object.",
|
1069
|
-
parameters: jsonSchema
|
1070
|
-
}
|
1071
|
-
},
|
1072
|
-
...settings,
|
1073
|
-
inputFormat: getInputFormat({ prompt, messages }),
|
1074
|
-
prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
|
1075
|
-
abortSignal
|
1076
|
-
})
|
1077
|
-
);
|
1078
|
-
modelStream = stream.pipeThrough(
|
1079
|
-
new TransformStream({
|
1080
|
-
transform(chunk, controller) {
|
1081
|
-
switch (chunk.type) {
|
1082
|
-
case "tool-call-delta":
|
1083
|
-
controller.enqueue(chunk.argsTextDelta);
|
1084
|
-
break;
|
1085
|
-
case "error":
|
1086
|
-
controller.enqueue(chunk);
|
1087
|
-
break;
|
1088
|
-
}
|
1089
|
-
}
|
1090
|
-
})
|
1091
|
-
);
|
1092
|
-
break;
|
1093
|
-
}
|
1094
|
-
case void 0: {
|
1095
|
-
throw new Error("Model does not have a default object generation mode.");
|
1096
|
-
}
|
1097
|
-
default: {
|
1098
|
-
const _exhaustiveCheck = mode;
|
1099
|
-
throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
|
1100
|
-
}
|
1101
|
-
}
|
1102
|
-
return new StreamObjectResult(modelStream);
|
1103
|
-
}
|
1104
|
-
var StreamObjectResult = class {
|
1105
|
-
constructor(modelStream) {
|
1106
|
-
let accumulatedText = "";
|
1107
|
-
let latestObject = void 0;
|
1108
|
-
this.objectStream = {
|
1109
|
-
[Symbol.asyncIterator]() {
|
1110
|
-
const reader = modelStream.getReader();
|
1111
|
-
return {
|
1112
|
-
next: async () => {
|
1113
|
-
while (true) {
|
1114
|
-
const { done, value } = await reader.read();
|
1115
|
-
if (done) {
|
1116
|
-
return { value: null, done: true };
|
1117
|
-
}
|
1118
|
-
if (typeof value === "string") {
|
1119
|
-
accumulatedText += value;
|
1120
|
-
const currentObject = parsePartialJson(
|
1121
|
-
accumulatedText
|
1122
|
-
);
|
1123
|
-
if (!isDeepEqualData(latestObject, currentObject)) {
|
1124
|
-
latestObject = currentObject;
|
1125
|
-
return { value: currentObject, done: false };
|
1126
|
-
}
|
1127
|
-
}
|
1128
|
-
}
|
1129
|
-
}
|
1130
|
-
};
|
1131
|
-
}
|
1132
|
-
};
|
1133
|
-
}
|
1134
|
-
};
|
1135
|
-
|
1136
|
-
// core/generate-text/generate-text.ts
|
1137
|
-
import zodToJsonSchema3 from "zod-to-json-schema";
|
1138
|
-
|
1139
|
-
// core/generate-text/tool-call.ts
|
1140
|
-
function parseToolCall({
|
1141
|
-
toolCall,
|
1142
|
-
tools
|
1143
|
-
}) {
|
1144
|
-
const toolName = toolCall.toolName;
|
1145
|
-
if (tools == null) {
|
1146
|
-
throw new Error(`Tool not found: ${toolName}`);
|
1147
|
-
}
|
1148
|
-
const tool2 = tools[toolName];
|
1149
|
-
if (tool2 == null) {
|
1150
|
-
throw new Error(`Tool not found: ${toolName}`);
|
1151
|
-
}
|
1152
|
-
const parseResult = safeParseJSON({
|
1153
|
-
text: toolCall.args,
|
1154
|
-
schema: tool2.parameters
|
1155
|
-
});
|
1156
|
-
if (parseResult.success === false) {
|
1157
|
-
throw new Error(
|
1158
|
-
`Tool call ${toolName} has invalid arguments: ${parseResult.error}`
|
1159
|
-
);
|
1160
|
-
}
|
1161
|
-
const toolArgs = parseResult.value;
|
1162
|
-
return {
|
1163
|
-
toolCallId: toolCall.toolCallId,
|
1164
|
-
toolName,
|
1165
|
-
args: toolArgs
|
1166
|
-
};
|
1167
|
-
}
|
1168
|
-
|
1169
|
-
// core/generate-text/generate-text.ts
|
1170
|
-
async function generateText({
|
1171
|
-
model,
|
1172
|
-
tools,
|
1173
|
-
system,
|
1174
|
-
prompt,
|
1175
|
-
messages,
|
1176
|
-
maxRetries,
|
1177
|
-
abortSignal,
|
1178
|
-
...settings
|
1179
|
-
}) {
|
1180
|
-
var _a, _b;
|
1181
|
-
const retry = retryWithExponentialBackoff({ maxRetries });
|
1182
|
-
const modelResponse = await retry(
|
1183
|
-
() => model.doGenerate({
|
1184
|
-
mode: {
|
1185
|
-
type: "regular",
|
1186
|
-
tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
|
1187
|
-
type: "function",
|
1188
|
-
name,
|
1189
|
-
description: tool2.description,
|
1190
|
-
parameters: zodToJsonSchema3(tool2.parameters)
|
1191
|
-
}))
|
1192
|
-
},
|
1193
|
-
...validateCallSettings(settings),
|
1194
|
-
inputFormat: getInputFormat({ prompt, messages }),
|
1195
|
-
prompt: convertToLanguageModelPrompt({
|
1196
|
-
system,
|
1197
|
-
prompt,
|
1198
|
-
messages
|
1199
|
-
}),
|
1200
|
-
abortSignal
|
1201
|
-
})
|
1202
|
-
);
|
1203
|
-
const toolCalls = [];
|
1204
|
-
for (const modelToolCall of (_a = modelResponse.toolCalls) != null ? _a : []) {
|
1205
|
-
toolCalls.push(parseToolCall({ toolCall: modelToolCall, tools }));
|
1206
|
-
}
|
1207
|
-
const toolResults = tools == null ? [] : await executeTools({ toolCalls, tools });
|
1208
|
-
return new GenerateTextResult({
|
1209
|
-
// Always return a string so that the caller doesn't have to check for undefined.
|
1210
|
-
// If they need to check if the model did not return any text,
|
1211
|
-
// they can check the length of the string:
|
1212
|
-
text: (_b = modelResponse.text) != null ? _b : "",
|
1213
|
-
toolCalls,
|
1214
|
-
toolResults,
|
1215
|
-
finishReason: modelResponse.finishReason,
|
1216
|
-
usage: calculateTokenUsage(modelResponse.usage)
|
1217
|
-
});
|
1218
|
-
}
|
1219
|
-
async function executeTools({
|
1220
|
-
toolCalls,
|
1221
|
-
tools
|
1222
|
-
}) {
|
1223
|
-
const toolResults = await Promise.all(
|
1224
|
-
toolCalls.map(async (toolCall) => {
|
1225
|
-
const tool2 = tools[toolCall.toolName];
|
1226
|
-
if ((tool2 == null ? void 0 : tool2.execute) == null) {
|
1227
|
-
return void 0;
|
1228
|
-
}
|
1229
|
-
const result = await tool2.execute(toolCall.args);
|
1230
|
-
return {
|
1231
|
-
toolCallId: toolCall.toolCallId,
|
1232
|
-
toolName: toolCall.toolName,
|
1233
|
-
args: toolCall.args,
|
1234
|
-
result
|
1235
|
-
};
|
1236
|
-
})
|
1237
|
-
);
|
1238
|
-
return toolResults.filter(
|
1239
|
-
(result) => result != null
|
1240
|
-
);
|
1241
|
-
}
|
1242
|
-
var GenerateTextResult = class {
|
1243
|
-
constructor(options) {
|
1244
|
-
this.text = options.text;
|
1245
|
-
this.toolCalls = options.toolCalls;
|
1246
|
-
this.toolResults = options.toolResults;
|
1247
|
-
this.finishReason = options.finishReason;
|
1248
|
-
this.usage = options.usage;
|
1249
|
-
}
|
1250
|
-
};
|
1251
|
-
|
1252
|
-
// core/generate-text/stream-text.ts
|
1253
|
-
import zodToJsonSchema4 from "zod-to-json-schema";
|
1254
|
-
|
1255
|
-
// core/generate-text/run-tools-transformation.ts
|
1256
|
-
import { nanoid } from "nanoid";
|
1257
|
-
function runToolsTransformation({
|
1258
|
-
tools,
|
1259
|
-
generatorStream
|
1260
|
-
}) {
|
1261
|
-
let canClose = false;
|
1262
|
-
const outstandingToolCalls = /* @__PURE__ */ new Set();
|
1263
|
-
let toolResultsStreamController = null;
|
1264
|
-
const toolResultsStream = new ReadableStream({
|
1265
|
-
start(controller) {
|
1266
|
-
toolResultsStreamController = controller;
|
1267
|
-
}
|
1268
|
-
});
|
1269
|
-
const forwardStream = new TransformStream({
|
1270
|
-
transform(chunk, controller) {
|
1271
|
-
const chunkType = chunk.type;
|
1272
|
-
switch (chunkType) {
|
1273
|
-
case "text-delta":
|
1274
|
-
case "error": {
|
1275
|
-
controller.enqueue(chunk);
|
1276
|
-
break;
|
1277
|
-
}
|
1278
|
-
case "tool-call": {
|
1279
|
-
const toolName = chunk.toolName;
|
1280
|
-
if (tools == null) {
|
1281
|
-
toolResultsStreamController.enqueue({
|
1282
|
-
type: "error",
|
1283
|
-
error: `Tool ${chunk.toolName} not found (no tools provided)`
|
1284
|
-
});
|
1285
|
-
break;
|
1286
|
-
}
|
1287
|
-
const tool2 = tools[toolName];
|
1288
|
-
if (tool2 == null) {
|
1289
|
-
toolResultsStreamController.enqueue({
|
1290
|
-
type: "error",
|
1291
|
-
error: `Tool ${chunk.toolName} not found`
|
1292
|
-
});
|
1293
|
-
break;
|
1294
|
-
}
|
1295
|
-
const toolCall = parseToolCall({
|
1296
|
-
toolCall: chunk,
|
1297
|
-
tools
|
1298
|
-
});
|
1299
|
-
controller.enqueue({
|
1300
|
-
type: "tool-call",
|
1301
|
-
...toolCall
|
1302
|
-
});
|
1303
|
-
if (tool2.execute != null) {
|
1304
|
-
const toolExecutionId = nanoid();
|
1305
|
-
outstandingToolCalls.add(toolExecutionId);
|
1306
|
-
tool2.execute(toolCall.args).then(
|
1307
|
-
(result) => {
|
1308
|
-
toolResultsStreamController.enqueue({
|
1309
|
-
type: "tool-result",
|
1310
|
-
...toolCall,
|
1311
|
-
result
|
1312
|
-
});
|
1313
|
-
outstandingToolCalls.delete(toolExecutionId);
|
1314
|
-
if (canClose && outstandingToolCalls.size === 0) {
|
1315
|
-
toolResultsStreamController.close();
|
1316
|
-
}
|
1317
|
-
},
|
1318
|
-
(error) => {
|
1319
|
-
toolResultsStreamController.enqueue({
|
1320
|
-
type: "error",
|
1321
|
-
error
|
1322
|
-
});
|
1323
|
-
outstandingToolCalls.delete(toolExecutionId);
|
1324
|
-
if (canClose && outstandingToolCalls.size === 0) {
|
1325
|
-
toolResultsStreamController.close();
|
1326
|
-
}
|
1327
|
-
}
|
1328
|
-
);
|
1329
|
-
}
|
1330
|
-
break;
|
1331
|
-
}
|
1332
|
-
case "finish-metadata":
|
1333
|
-
case "tool-call-delta": {
|
1334
|
-
break;
|
1335
|
-
}
|
1336
|
-
default: {
|
1337
|
-
const _exhaustiveCheck = chunkType;
|
1338
|
-
throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
|
1339
|
-
}
|
1340
|
-
}
|
1341
|
-
},
|
1342
|
-
flush() {
|
1343
|
-
canClose = true;
|
1344
|
-
if (outstandingToolCalls.size === 0) {
|
1345
|
-
toolResultsStreamController.close();
|
1346
|
-
}
|
1347
|
-
}
|
1348
|
-
});
|
1349
|
-
return new ReadableStream({
|
1350
|
-
async start(controller) {
|
1351
|
-
generatorStream.pipeThrough(forwardStream).pipeTo(
|
1352
|
-
new WritableStream({
|
1353
|
-
write(chunk) {
|
1354
|
-
controller.enqueue(chunk);
|
1355
|
-
},
|
1356
|
-
close() {
|
1357
|
-
}
|
1358
|
-
})
|
1359
|
-
);
|
1360
|
-
toolResultsStream.pipeTo(
|
1361
|
-
new WritableStream({
|
1362
|
-
write(chunk) {
|
1363
|
-
controller.enqueue(chunk);
|
1364
|
-
},
|
1365
|
-
close() {
|
1366
|
-
controller.close();
|
1367
|
-
}
|
1368
|
-
})
|
1369
|
-
);
|
1370
|
-
}
|
1371
|
-
});
|
1372
|
-
}
|
1373
|
-
|
1374
|
-
// core/generate-text/stream-text.ts
|
1375
|
-
async function streamText({
|
1376
|
-
model,
|
1377
|
-
tools,
|
1378
|
-
system,
|
1379
|
-
prompt,
|
1380
|
-
messages,
|
1381
|
-
maxRetries,
|
1382
|
-
abortSignal,
|
1383
|
-
...settings
|
1384
|
-
}) {
|
1385
|
-
const retry = retryWithExponentialBackoff({ maxRetries });
|
1386
|
-
const { stream, warnings } = await retry(
|
1387
|
-
() => model.doStream({
|
1388
|
-
mode: {
|
1389
|
-
type: "regular",
|
1390
|
-
tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
|
1391
|
-
type: "function",
|
1392
|
-
name,
|
1393
|
-
description: tool2.description,
|
1394
|
-
parameters: zodToJsonSchema4(tool2.parameters)
|
1395
|
-
}))
|
1396
|
-
},
|
1397
|
-
...validateCallSettings(settings),
|
1398
|
-
inputFormat: getInputFormat({ prompt, messages }),
|
1399
|
-
prompt: convertToLanguageModelPrompt({
|
1400
|
-
system,
|
1401
|
-
prompt,
|
1402
|
-
messages
|
1403
|
-
}),
|
1404
|
-
abortSignal
|
1405
|
-
})
|
1406
|
-
);
|
1407
|
-
const toolStream = runToolsTransformation({
|
1408
|
-
tools,
|
1409
|
-
generatorStream: stream
|
1410
|
-
});
|
1411
|
-
return new StreamTextResult(toolStream);
|
1412
|
-
}
|
1413
|
-
var StreamTextResult = class {
|
1414
|
-
constructor(stream) {
|
1415
|
-
this.rootStream = stream;
|
1416
|
-
this.textStream = {
|
1417
|
-
[Symbol.asyncIterator]() {
|
1418
|
-
const reader = stream.getReader();
|
1419
|
-
return {
|
1420
|
-
next: async () => {
|
1421
|
-
while (true) {
|
1422
|
-
const { done, value } = await reader.read();
|
1423
|
-
if (done) {
|
1424
|
-
return { value: null, done: true };
|
1425
|
-
}
|
1426
|
-
if (value.type === "text-delta") {
|
1427
|
-
if (value.textDelta.length > 0) {
|
1428
|
-
return { value: value.textDelta, done: false };
|
1429
|
-
}
|
1430
|
-
}
|
1431
|
-
if (value.type === "error") {
|
1432
|
-
console.error("Error:", value.error);
|
1433
|
-
}
|
1434
|
-
}
|
1435
|
-
}
|
1436
|
-
};
|
1437
|
-
}
|
1438
|
-
};
|
1439
|
-
this.fullStream = {
|
1440
|
-
[Symbol.asyncIterator]() {
|
1441
|
-
const reader = stream.getReader();
|
1442
|
-
return {
|
1443
|
-
next: async () => {
|
1444
|
-
while (true) {
|
1445
|
-
const { done, value } = await reader.read();
|
1446
|
-
if (done) {
|
1447
|
-
return { value: null, done: true };
|
1448
|
-
}
|
1449
|
-
if (value.type === "text-delta") {
|
1450
|
-
if (value.textDelta.length > 0) {
|
1451
|
-
return { value, done: false };
|
1452
|
-
}
|
1453
|
-
} else {
|
1454
|
-
return { value, done: false };
|
1455
|
-
}
|
1456
|
-
}
|
1457
|
-
}
|
1458
|
-
};
|
1459
|
-
}
|
1460
|
-
};
|
1461
|
-
}
|
1462
|
-
};
|
1463
|
-
|
1464
|
-
// core/tool/tool.ts
|
1465
|
-
function tool(tool2) {
|
1466
|
-
return tool2;
|
1467
|
-
}
|
1468
|
-
export {
|
1469
|
-
GenerateObjectResult,
|
1470
|
-
GenerateTextResult,
|
1471
|
-
StreamObjectResult,
|
1472
|
-
StreamTextResult,
|
1473
|
-
convertDataContentToBase64String,
|
1474
|
-
convertDataContentToUint8Array,
|
1475
|
-
generateObject,
|
1476
|
-
generateText,
|
1477
|
-
streamObject,
|
1478
|
-
streamText,
|
1479
|
-
tool
|
1480
|
-
};
|
1481
|
-
//# sourceMappingURL=index.mjs.map
|