ai 3.1.0-canary.1 → 3.1.0-canary.3
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/ai-model-specification/dist/index.d.mts +606 -0
- package/ai-model-specification/dist/index.d.ts +606 -0
- package/ai-model-specification/dist/index.js +617 -0
- package/ai-model-specification/dist/index.js.map +1 -0
- package/ai-model-specification/dist/index.mjs +560 -0
- package/ai-model-specification/dist/index.mjs.map +1 -0
- package/core/dist/index.d.mts +195 -85
- package/core/dist/index.d.ts +195 -85
- package/core/dist/index.js +497 -501
- package/core/dist/index.js.map +1 -1
- package/core/dist/index.mjs +497 -499
- package/core/dist/index.mjs.map +1 -1
- package/dist/index.d.mts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +39 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -7
- package/prompts/dist/index.d.mts +32 -19
- package/prompts/dist/index.d.ts +32 -19
- package/prompts/dist/index.js +0 -1
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs +0 -1
- package/prompts/dist/index.mjs.map +1 -1
- package/provider/dist/index.d.mts +232 -190
- package/provider/dist/index.d.ts +232 -190
- package/provider/dist/index.js +838 -26131
- package/provider/dist/index.js.map +1 -1
- package/provider/dist/index.mjs +806 -7735
- package/provider/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +4 -4
- package/react/dist/index.d.ts +4 -4
- package/react/dist/index.js +16 -1
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +16 -1
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/index.d.ts +11 -0
- package/rsc/dist/rsc-server.d.mts +11 -0
- package/rsc/dist/rsc-server.mjs +21 -21
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/rsc/dist/rsc-shared.mjs +21 -1
- package/rsc/dist/rsc-shared.mjs.map +1 -1
- package/provider/dist/chunk-3DTRVHCT.mjs +0 -5046
- package/provider/dist/chunk-3DTRVHCT.mjs.map +0 -1
- package/provider/dist/chunk-4OUDS3CP.mjs +0 -30
- package/provider/dist/chunk-4OUDS3CP.mjs.map +0 -1
- package/provider/dist/chunk-5IYCPJBV.mjs +0 -56
- package/provider/dist/chunk-5IYCPJBV.mjs.map +0 -1
- package/provider/dist/chunk-VB2TCVQ4.mjs +0 -6746
- package/provider/dist/chunk-VB2TCVQ4.mjs.map +0 -1
- package/provider/dist/chunk-VYIXVZ6L.mjs +0 -317
- package/provider/dist/chunk-VYIXVZ6L.mjs.map +0 -1
- package/provider/dist/chunk-WTOUHN6A.mjs +0 -2251
- package/provider/dist/chunk-WTOUHN6A.mjs.map +0 -1
- package/provider/dist/client-22WAAXR7.mjs +0 -10
- package/provider/dist/client-22WAAXR7.mjs.map +0 -1
- package/provider/dist/fileFromPath-23RINPB2.mjs +0 -115
- package/provider/dist/fileFromPath-23RINPB2.mjs.map +0 -1
- package/provider/dist/lib-BZMMM4HX.mjs +0 -20
- package/provider/dist/lib-BZMMM4HX.mjs.map +0 -1
- package/provider/dist/openai-3YL4AWLI.mjs +0 -3451
- package/provider/dist/openai-3YL4AWLI.mjs.map +0 -1
package/core/dist/index.js
CHANGED
@@ -33,9 +33,7 @@ __export(core_exports, {
|
|
33
33
|
GenerateObjectResult: () => GenerateObjectResult,
|
34
34
|
GenerateTextResult: () => GenerateTextResult,
|
35
35
|
StreamObjectResult: () => StreamObjectResult,
|
36
|
-
StreamTextHttpResponse: () => StreamTextHttpResponse,
|
37
36
|
StreamTextResult: () => StreamTextResult,
|
38
|
-
UnsupportedFunctionalityError: () => UnsupportedFunctionalityError,
|
39
37
|
convertDataContentToBase64String: () => convertDataContentToBase64String,
|
40
38
|
convertDataContentToUint8Array: () => convertDataContentToUint8Array,
|
41
39
|
generateObject: () => generateObject,
|
@@ -46,7 +44,186 @@ __export(core_exports, {
|
|
46
44
|
});
|
47
45
|
module.exports = __toCommonJS(core_exports);
|
48
46
|
|
49
|
-
// core/
|
47
|
+
// core/generate-object/generate-object.ts
|
48
|
+
var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
|
49
|
+
|
50
|
+
// ai-model-specification/errors/ai-invalid-argument-error.ts
|
51
|
+
var AI_InvalidArgumentError = class extends Error {
|
52
|
+
// readonly learnMore =
|
53
|
+
// 'https://sdk.vercel.com/docs/ai/errors/ai_invalid_argument_error';
|
54
|
+
constructor({
|
55
|
+
parameter,
|
56
|
+
value,
|
57
|
+
message
|
58
|
+
}) {
|
59
|
+
super(`Invalid argument for parameter ${parameter}: ${message}`);
|
60
|
+
this.name = "AI_InvalidArgumentError";
|
61
|
+
this.parameter = parameter;
|
62
|
+
this.value = value;
|
63
|
+
}
|
64
|
+
toJSON() {
|
65
|
+
return {
|
66
|
+
name: this.name,
|
67
|
+
message: this.message,
|
68
|
+
stack: this.stack,
|
69
|
+
parameter: this.parameter,
|
70
|
+
value: this.value
|
71
|
+
};
|
72
|
+
}
|
73
|
+
};
|
74
|
+
|
75
|
+
// ai-model-specification/util/get-error-message.ts
|
76
|
+
function getErrorMessage(error) {
|
77
|
+
if (error == null) {
|
78
|
+
return "unknown error";
|
79
|
+
}
|
80
|
+
if (typeof error === "string") {
|
81
|
+
return error;
|
82
|
+
}
|
83
|
+
if (error instanceof Error) {
|
84
|
+
return error.message;
|
85
|
+
}
|
86
|
+
return JSON.stringify(error);
|
87
|
+
}
|
88
|
+
|
89
|
+
// ai-model-specification/errors/json-parse-error.ts
|
90
|
+
var JSONParseError = class extends Error {
|
91
|
+
constructor({ text, cause }) {
|
92
|
+
super(
|
93
|
+
`JSON parsing failed: Text: ${text}.
|
94
|
+
Error message: ${getErrorMessage(cause)}`
|
95
|
+
);
|
96
|
+
this.name = "JSONParseError";
|
97
|
+
this.cause = cause;
|
98
|
+
this.text = text;
|
99
|
+
}
|
100
|
+
toJSON() {
|
101
|
+
return {
|
102
|
+
name: this.name,
|
103
|
+
message: this.message,
|
104
|
+
cause: this.cause,
|
105
|
+
stack: this.stack,
|
106
|
+
valueText: this.text
|
107
|
+
};
|
108
|
+
}
|
109
|
+
};
|
110
|
+
|
111
|
+
// ai-model-specification/errors/no-object-generated-error.ts
|
112
|
+
var NoTextGeneratedError = class extends Error {
|
113
|
+
constructor() {
|
114
|
+
super(`No text generated.`);
|
115
|
+
this.name = "NoTextGeneratedError";
|
116
|
+
}
|
117
|
+
toJSON() {
|
118
|
+
return {
|
119
|
+
name: this.name,
|
120
|
+
cause: this.cause,
|
121
|
+
message: this.message,
|
122
|
+
stack: this.stack
|
123
|
+
};
|
124
|
+
}
|
125
|
+
};
|
126
|
+
|
127
|
+
// ai-model-specification/errors/retry-error.ts
|
128
|
+
var RetryError = class extends Error {
|
129
|
+
constructor({
|
130
|
+
message,
|
131
|
+
reason,
|
132
|
+
errors
|
133
|
+
}) {
|
134
|
+
super(message);
|
135
|
+
this.name = "RetryError";
|
136
|
+
this.reason = reason;
|
137
|
+
this.errors = errors;
|
138
|
+
this.lastError = errors[errors.length - 1];
|
139
|
+
}
|
140
|
+
toJSON() {
|
141
|
+
return {
|
142
|
+
name: this.name,
|
143
|
+
message: this.message,
|
144
|
+
reason: this.reason,
|
145
|
+
lastError: this.lastError,
|
146
|
+
errors: this.errors
|
147
|
+
};
|
148
|
+
}
|
149
|
+
};
|
150
|
+
|
151
|
+
// ai-model-specification/errors/type-validation-error.ts
|
152
|
+
var TypeValidationError = class extends Error {
|
153
|
+
constructor({ value, cause }) {
|
154
|
+
super(
|
155
|
+
`Type validation failed: Value: ${JSON.stringify(value)}.
|
156
|
+
Error message: ${getErrorMessage(cause)}`
|
157
|
+
);
|
158
|
+
this.name = "TypeValidationError";
|
159
|
+
this.cause = cause;
|
160
|
+
this.value = value;
|
161
|
+
}
|
162
|
+
toJSON() {
|
163
|
+
return {
|
164
|
+
name: this.name,
|
165
|
+
message: this.message,
|
166
|
+
cause: this.cause,
|
167
|
+
stack: this.stack,
|
168
|
+
value: this.value
|
169
|
+
};
|
170
|
+
}
|
171
|
+
};
|
172
|
+
|
173
|
+
// ai-model-specification/util/parse-json.ts
|
174
|
+
var import_secure_json_parse = __toESM(require("secure-json-parse"));
|
175
|
+
|
176
|
+
// ai-model-specification/util/validate-types.ts
|
177
|
+
function safeValidateTypes({
|
178
|
+
value,
|
179
|
+
schema
|
180
|
+
}) {
|
181
|
+
try {
|
182
|
+
const validationResult = schema.safeParse(value);
|
183
|
+
if (validationResult.success) {
|
184
|
+
return {
|
185
|
+
success: true,
|
186
|
+
value: validationResult.data
|
187
|
+
};
|
188
|
+
}
|
189
|
+
return {
|
190
|
+
success: false,
|
191
|
+
error: new TypeValidationError({
|
192
|
+
value,
|
193
|
+
cause: validationResult.error
|
194
|
+
})
|
195
|
+
};
|
196
|
+
} catch (error) {
|
197
|
+
return {
|
198
|
+
success: false,
|
199
|
+
error: error instanceof TypeValidationError ? error : new TypeValidationError({ value, cause: error })
|
200
|
+
};
|
201
|
+
}
|
202
|
+
}
|
203
|
+
|
204
|
+
// ai-model-specification/util/parse-json.ts
|
205
|
+
function safeParseJSON({
|
206
|
+
text,
|
207
|
+
schema
|
208
|
+
}) {
|
209
|
+
try {
|
210
|
+
const value = import_secure_json_parse.default.parse(text);
|
211
|
+
if (schema == null) {
|
212
|
+
return {
|
213
|
+
success: true,
|
214
|
+
value
|
215
|
+
};
|
216
|
+
}
|
217
|
+
return safeValidateTypes({ value, schema });
|
218
|
+
} catch (error) {
|
219
|
+
return {
|
220
|
+
success: false,
|
221
|
+
error: error instanceof JSONParseError ? error : new JSONParseError({ text, cause: error })
|
222
|
+
};
|
223
|
+
}
|
224
|
+
}
|
225
|
+
|
226
|
+
// ai-model-specification/util/uint8-utils.ts
|
50
227
|
function convertBase64ToUint8Array(base64String) {
|
51
228
|
const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
|
52
229
|
const latin1string = globalThis.atob(base64Url);
|
@@ -60,6 +237,15 @@ function convertUint8ArrayToBase64(array) {
|
|
60
237
|
return globalThis.btoa(latin1string);
|
61
238
|
}
|
62
239
|
|
240
|
+
// core/generate-text/token-usage.ts
|
241
|
+
function calculateTokenUsage(usage) {
|
242
|
+
return {
|
243
|
+
promptTokens: usage.promptTokens,
|
244
|
+
completionTokens: usage.completionTokens,
|
245
|
+
totalTokens: usage.promptTokens + usage.completionTokens
|
246
|
+
};
|
247
|
+
}
|
248
|
+
|
63
249
|
// core/prompt/data-content.ts
|
64
250
|
function convertDataContentToBase64String(content) {
|
65
251
|
if (typeof content === "string") {
|
@@ -129,7 +315,7 @@ function convertToLanguageModelPrompt({
|
|
129
315
|
case "image": {
|
130
316
|
return {
|
131
317
|
type: "image",
|
132
|
-
image: convertDataContentToUint8Array(part.image),
|
318
|
+
image: part.image instanceof URL ? part.image : convertDataContentToUint8Array(part.image),
|
133
319
|
mimeType: part.mimeType
|
134
320
|
};
|
135
321
|
}
|
@@ -171,129 +357,160 @@ function getInputFormat({
|
|
171
357
|
return prompt != null ? "prompt" : "messages";
|
172
358
|
}
|
173
359
|
|
174
|
-
// core/
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
360
|
+
// core/prompt/validate-call-settings.ts
|
361
|
+
function validateCallSettings(settings) {
|
362
|
+
if (settings.maxTokens != null) {
|
363
|
+
if (!Number.isInteger(settings.maxTokens)) {
|
364
|
+
throw new AI_InvalidArgumentError({
|
365
|
+
parameter: "maxTokens",
|
366
|
+
value: settings.maxTokens,
|
367
|
+
message: "maxTokens must be an integer"
|
368
|
+
});
|
369
|
+
}
|
370
|
+
if (settings.maxTokens < 1) {
|
371
|
+
throw new AI_InvalidArgumentError({
|
372
|
+
parameter: "maxTokens",
|
373
|
+
value: settings.maxTokens,
|
374
|
+
message: "maxTokens must be >= 1"
|
375
|
+
});
|
376
|
+
}
|
187
377
|
}
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
378
|
+
if (settings.temperature != null) {
|
379
|
+
if (typeof settings.temperature !== "number") {
|
380
|
+
throw new AI_InvalidArgumentError({
|
381
|
+
parameter: "temperature",
|
382
|
+
value: settings.temperature,
|
383
|
+
message: "temperature must be a number"
|
384
|
+
});
|
385
|
+
}
|
386
|
+
if (settings.temperature < 0 || settings.temperature > 1) {
|
387
|
+
throw new AI_InvalidArgumentError({
|
388
|
+
parameter: "temperature",
|
389
|
+
value: settings.temperature,
|
390
|
+
message: "temperature must be between 0 and 1 (inclusive)"
|
391
|
+
});
|
392
|
+
}
|
201
393
|
}
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
}
|
394
|
+
if (settings.topP != null) {
|
395
|
+
if (typeof settings.topP !== "number") {
|
396
|
+
throw new AI_InvalidArgumentError({
|
397
|
+
parameter: "topP",
|
398
|
+
value: settings.topP,
|
399
|
+
message: "topP must be a number"
|
400
|
+
});
|
401
|
+
}
|
402
|
+
if (settings.topP < 0 || settings.topP > 1) {
|
403
|
+
throw new AI_InvalidArgumentError({
|
404
|
+
parameter: "topP",
|
405
|
+
value: settings.topP,
|
406
|
+
message: "topP must be between 0 and 1 (inclusive)"
|
407
|
+
});
|
408
|
+
}
|
210
409
|
}
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
)
|
220
|
-
|
221
|
-
|
222
|
-
|
410
|
+
if (settings.presencePenalty != null) {
|
411
|
+
if (typeof settings.presencePenalty !== "number") {
|
412
|
+
throw new AI_InvalidArgumentError({
|
413
|
+
parameter: "presencePenalty",
|
414
|
+
value: settings.presencePenalty,
|
415
|
+
message: "presencePenalty must be a number"
|
416
|
+
});
|
417
|
+
}
|
418
|
+
if (settings.presencePenalty < -1 || settings.presencePenalty > 1) {
|
419
|
+
throw new AI_InvalidArgumentError({
|
420
|
+
parameter: "presencePenalty",
|
421
|
+
value: settings.presencePenalty,
|
422
|
+
message: "presencePenalty must be between -1 and 1 (inclusive)"
|
423
|
+
});
|
424
|
+
}
|
223
425
|
}
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
}
|
426
|
+
if (settings.frequencyPenalty != null) {
|
427
|
+
if (typeof settings.frequencyPenalty !== "number") {
|
428
|
+
throw new AI_InvalidArgumentError({
|
429
|
+
parameter: "frequencyPenalty",
|
430
|
+
value: settings.frequencyPenalty,
|
431
|
+
message: "frequencyPenalty must be a number"
|
432
|
+
});
|
433
|
+
}
|
434
|
+
if (settings.frequencyPenalty < -1 || settings.frequencyPenalty > 1) {
|
435
|
+
throw new AI_InvalidArgumentError({
|
436
|
+
parameter: "frequencyPenalty",
|
437
|
+
value: settings.frequencyPenalty,
|
438
|
+
message: "frequencyPenalty must be between -1 and 1 (inclusive)"
|
439
|
+
});
|
440
|
+
}
|
232
441
|
}
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
})
|
240
|
-
try {
|
241
|
-
const validationResult = schema.validate(value);
|
242
|
-
if (validationResult.success) {
|
243
|
-
return validationResult;
|
442
|
+
if (settings.seed != null) {
|
443
|
+
if (!Number.isInteger(settings.seed)) {
|
444
|
+
throw new AI_InvalidArgumentError({
|
445
|
+
parameter: "seed",
|
446
|
+
value: settings.seed,
|
447
|
+
message: "seed must be an integer"
|
448
|
+
});
|
244
449
|
}
|
245
|
-
return {
|
246
|
-
success: false,
|
247
|
-
error: new TypeValidationError({
|
248
|
-
value,
|
249
|
-
cause: validationResult.error
|
250
|
-
})
|
251
|
-
};
|
252
|
-
} catch (error) {
|
253
|
-
return {
|
254
|
-
success: false,
|
255
|
-
error: error instanceof TypeValidationError ? error : new TypeValidationError({ value, cause: error })
|
256
|
-
};
|
257
450
|
}
|
451
|
+
return settings;
|
258
452
|
}
|
259
453
|
|
260
|
-
// core/
|
261
|
-
function
|
262
|
-
|
263
|
-
|
264
|
-
|
454
|
+
// core/util/delay.ts
|
455
|
+
async function delay(delayInMs) {
|
456
|
+
return new Promise((resolve) => setTimeout(resolve, delayInMs));
|
457
|
+
}
|
458
|
+
|
459
|
+
// core/util/retry-with-exponential-backoff.ts
|
460
|
+
var retryWithExponentialBackoff = ({
|
461
|
+
maxRetries = 2,
|
462
|
+
initialDelayInMs = 2e3,
|
463
|
+
backoffFactor = 2
|
464
|
+
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
465
|
+
maxRetries,
|
466
|
+
delayInMs: initialDelayInMs,
|
467
|
+
backoffFactor
|
468
|
+
});
|
469
|
+
async function _retryWithExponentialBackoff(f, {
|
470
|
+
maxRetries,
|
471
|
+
delayInMs,
|
472
|
+
backoffFactor
|
473
|
+
}, errors = []) {
|
265
474
|
try {
|
266
|
-
|
267
|
-
if (schema == null) {
|
268
|
-
return {
|
269
|
-
success: true,
|
270
|
-
value
|
271
|
-
};
|
272
|
-
}
|
273
|
-
return safeValidateTypes({ value, schema });
|
475
|
+
return await f();
|
274
476
|
} catch (error) {
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
477
|
+
if (maxRetries === 0) {
|
478
|
+
throw error;
|
479
|
+
}
|
480
|
+
const errorMessage = getErrorMessage(error);
|
481
|
+
const newErrors = [...errors, error];
|
482
|
+
const tryNumber = newErrors.length;
|
483
|
+
if (tryNumber > maxRetries) {
|
484
|
+
throw new RetryError({
|
485
|
+
message: `Failed after ${tryNumber} tries. Last error: ${errorMessage}`,
|
486
|
+
reason: "maxRetriesExceeded",
|
487
|
+
errors: newErrors
|
488
|
+
});
|
489
|
+
}
|
490
|
+
if (error instanceof Error) {
|
491
|
+
if (error.name === "AbortError") {
|
492
|
+
throw error;
|
493
|
+
}
|
494
|
+
if (
|
495
|
+
// deal with bundling duplication by using names
|
496
|
+
error.name === "ApiCallError" && error.isRetryable === true && tryNumber <= maxRetries
|
497
|
+
) {
|
498
|
+
await delay(delayInMs);
|
499
|
+
return _retryWithExponentialBackoff(
|
500
|
+
f,
|
501
|
+
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
502
|
+
newErrors
|
503
|
+
);
|
504
|
+
}
|
505
|
+
}
|
506
|
+
throw new RetryError({
|
507
|
+
message: `Failed after ${tryNumber} tries with non-retryable error: '${errorMessage}'`,
|
508
|
+
reason: "errorNotRetryable",
|
509
|
+
errors: newErrors
|
510
|
+
});
|
279
511
|
}
|
280
512
|
}
|
281
513
|
|
282
|
-
// core/schema/zod-schema.ts
|
283
|
-
var import_zod_to_json_schema = require("zod-to-json-schema");
|
284
|
-
var ZodSchema = class {
|
285
|
-
constructor(zodSchema) {
|
286
|
-
this.zodSchema = zodSchema;
|
287
|
-
}
|
288
|
-
validate(value) {
|
289
|
-
const result = this.zodSchema.safeParse(value);
|
290
|
-
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
291
|
-
}
|
292
|
-
getJsonSchema() {
|
293
|
-
return (0, import_zod_to_json_schema.zodToJsonSchema)(this.zodSchema);
|
294
|
-
}
|
295
|
-
};
|
296
|
-
|
297
514
|
// core/generate-object/inject-json-schema-into-system.ts
|
298
515
|
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
299
516
|
var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
|
@@ -313,94 +530,97 @@ function injectJsonSchemaIntoSystem({
|
|
313
530
|
].filter((line) => line != null).join("\n");
|
314
531
|
}
|
315
532
|
|
316
|
-
// core/language-model/errors/no-object-generated-error.ts
|
317
|
-
var NoTextGeneratedError = class extends Error {
|
318
|
-
constructor() {
|
319
|
-
super(`No text generated.`);
|
320
|
-
this.name = "NoTextGeneratedError";
|
321
|
-
}
|
322
|
-
toJSON() {
|
323
|
-
return {
|
324
|
-
name: this.name,
|
325
|
-
cause: this.cause,
|
326
|
-
message: this.message,
|
327
|
-
stack: this.stack
|
328
|
-
};
|
329
|
-
}
|
330
|
-
};
|
331
|
-
|
332
533
|
// core/generate-object/generate-object.ts
|
333
534
|
async function generateObject({
|
334
535
|
model,
|
335
|
-
schema
|
536
|
+
schema,
|
336
537
|
mode,
|
337
538
|
system,
|
338
539
|
prompt,
|
339
540
|
messages,
|
541
|
+
maxRetries,
|
542
|
+
abortSignal,
|
340
543
|
...settings
|
341
544
|
}) {
|
342
545
|
var _a, _b;
|
343
|
-
const
|
344
|
-
const jsonSchema =
|
345
|
-
let result;
|
546
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
547
|
+
const jsonSchema = (0, import_zod_to_json_schema.default)(schema);
|
346
548
|
if (mode === "auto" || mode == null) {
|
347
549
|
mode = model.defaultObjectGenerationMode;
|
348
550
|
}
|
551
|
+
let result;
|
552
|
+
let finishReason;
|
553
|
+
let usage;
|
349
554
|
switch (mode) {
|
350
555
|
case "json": {
|
351
|
-
const generateResult = await
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
556
|
+
const generateResult = await retry(
|
557
|
+
() => model.doGenerate({
|
558
|
+
mode: { type: "object-json" },
|
559
|
+
...validateCallSettings(settings),
|
560
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
561
|
+
prompt: convertToLanguageModelPrompt({
|
562
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
563
|
+
prompt,
|
564
|
+
messages
|
565
|
+
}),
|
566
|
+
abortSignal
|
359
567
|
})
|
360
|
-
|
568
|
+
);
|
361
569
|
if (generateResult.text === void 0) {
|
362
570
|
throw new NoTextGeneratedError();
|
363
571
|
}
|
364
572
|
result = generateResult.text;
|
573
|
+
finishReason = generateResult.finishReason;
|
574
|
+
usage = generateResult.usage;
|
365
575
|
break;
|
366
576
|
}
|
367
577
|
case "grammar": {
|
368
|
-
const generateResult = await
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
578
|
+
const generateResult = await retry(
|
579
|
+
() => model.doGenerate({
|
580
|
+
mode: { type: "object-grammar", schema: jsonSchema },
|
581
|
+
...settings,
|
582
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
583
|
+
prompt: convertToLanguageModelPrompt({
|
584
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
585
|
+
prompt,
|
586
|
+
messages
|
587
|
+
}),
|
588
|
+
abortSignal
|
376
589
|
})
|
377
|
-
|
590
|
+
);
|
378
591
|
if (generateResult.text === void 0) {
|
379
592
|
throw new NoTextGeneratedError();
|
380
593
|
}
|
381
594
|
result = generateResult.text;
|
595
|
+
finishReason = generateResult.finishReason;
|
596
|
+
usage = generateResult.usage;
|
382
597
|
break;
|
383
598
|
}
|
384
599
|
case "tool": {
|
385
|
-
const generateResult = await
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
600
|
+
const generateResult = await retry(
|
601
|
+
() => model.doGenerate({
|
602
|
+
mode: {
|
603
|
+
type: "object-tool",
|
604
|
+
tool: {
|
605
|
+
type: "function",
|
606
|
+
name: "json",
|
607
|
+
description: "Respond with a JSON object.",
|
608
|
+
parameters: jsonSchema
|
609
|
+
}
|
610
|
+
},
|
611
|
+
...settings,
|
612
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
613
|
+
prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
|
614
|
+
abortSignal
|
615
|
+
})
|
616
|
+
);
|
399
617
|
const functionArgs = (_b = (_a = generateResult.toolCalls) == null ? void 0 : _a[0]) == null ? void 0 : _b.args;
|
400
618
|
if (functionArgs === void 0) {
|
401
619
|
throw new NoTextGeneratedError();
|
402
620
|
}
|
403
621
|
result = functionArgs;
|
622
|
+
finishReason = generateResult.finishReason;
|
623
|
+
usage = generateResult.usage;
|
404
624
|
break;
|
405
625
|
}
|
406
626
|
case void 0: {
|
@@ -416,15 +636,22 @@ async function generateObject({
|
|
416
636
|
throw parseResult.error;
|
417
637
|
}
|
418
638
|
return new GenerateObjectResult({
|
419
|
-
object: parseResult.value
|
639
|
+
object: parseResult.value,
|
640
|
+
finishReason,
|
641
|
+
usage: calculateTokenUsage(usage)
|
420
642
|
});
|
421
643
|
}
|
422
644
|
var GenerateObjectResult = class {
|
423
645
|
constructor(options) {
|
424
646
|
this.object = options.object;
|
647
|
+
this.finishReason = options.finishReason;
|
648
|
+
this.usage = options.usage;
|
425
649
|
}
|
426
650
|
};
|
427
651
|
|
652
|
+
// core/generate-object/stream-object.ts
|
653
|
+
var import_zod_to_json_schema2 = __toESM(require("zod-to-json-schema"));
|
654
|
+
|
428
655
|
// core/util/is-deep-equal-data.ts
|
429
656
|
function isDeepEqualData(obj1, obj2) {
|
430
657
|
if (obj1 === obj2)
|
@@ -800,31 +1027,36 @@ function parsePartialJson(jsonText) {
|
|
800
1027
|
// core/generate-object/stream-object.ts
|
801
1028
|
async function streamObject({
|
802
1029
|
model,
|
803
|
-
schema
|
1030
|
+
schema,
|
804
1031
|
mode,
|
805
1032
|
system,
|
806
1033
|
prompt,
|
807
1034
|
messages,
|
1035
|
+
maxRetries,
|
1036
|
+
abortSignal,
|
808
1037
|
...settings
|
809
1038
|
}) {
|
810
|
-
const
|
811
|
-
const jsonSchema =
|
1039
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
1040
|
+
const jsonSchema = (0, import_zod_to_json_schema2.default)(schema);
|
812
1041
|
let modelStream;
|
813
1042
|
if (mode === "auto" || mode == null) {
|
814
1043
|
mode = model.defaultObjectGenerationMode;
|
815
1044
|
}
|
816
1045
|
switch (mode) {
|
817
1046
|
case "json": {
|
818
|
-
const { stream, warnings } = await
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
1047
|
+
const { stream, warnings } = await retry(
|
1048
|
+
() => model.doStream({
|
1049
|
+
mode: { type: "object-json" },
|
1050
|
+
...validateCallSettings(settings),
|
1051
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1052
|
+
prompt: convertToLanguageModelPrompt({
|
1053
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
1054
|
+
prompt,
|
1055
|
+
messages
|
1056
|
+
}),
|
1057
|
+
abortSignal
|
826
1058
|
})
|
827
|
-
|
1059
|
+
);
|
828
1060
|
modelStream = stream.pipeThrough(
|
829
1061
|
new TransformStream({
|
830
1062
|
transform(chunk, controller) {
|
@@ -842,16 +1074,19 @@ async function streamObject({
|
|
842
1074
|
break;
|
843
1075
|
}
|
844
1076
|
case "grammar": {
|
845
|
-
const { stream, warnings } = await
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
1077
|
+
const { stream, warnings } = await retry(
|
1078
|
+
() => model.doStream({
|
1079
|
+
mode: { type: "object-grammar", schema: jsonSchema },
|
1080
|
+
...settings,
|
1081
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1082
|
+
prompt: convertToLanguageModelPrompt({
|
1083
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
1084
|
+
prompt,
|
1085
|
+
messages
|
1086
|
+
}),
|
1087
|
+
abortSignal
|
853
1088
|
})
|
854
|
-
|
1089
|
+
);
|
855
1090
|
modelStream = stream.pipeThrough(
|
856
1091
|
new TransformStream({
|
857
1092
|
transform(chunk, controller) {
|
@@ -869,20 +1104,23 @@ async function streamObject({
|
|
869
1104
|
break;
|
870
1105
|
}
|
871
1106
|
case "tool": {
|
872
|
-
const { stream, warnings } = await
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
1107
|
+
const { stream, warnings } = await retry(
|
1108
|
+
() => model.doStream({
|
1109
|
+
mode: {
|
1110
|
+
type: "object-tool",
|
1111
|
+
tool: {
|
1112
|
+
type: "function",
|
1113
|
+
name: "json",
|
1114
|
+
description: "Respond with a JSON object.",
|
1115
|
+
parameters: jsonSchema
|
1116
|
+
}
|
1117
|
+
},
|
1118
|
+
...settings,
|
1119
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1120
|
+
prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
|
1121
|
+
abortSignal
|
1122
|
+
})
|
1123
|
+
);
|
886
1124
|
modelStream = stream.pipeThrough(
|
887
1125
|
new TransformStream({
|
888
1126
|
transform(chunk, controller) {
|
@@ -942,7 +1180,7 @@ var StreamObjectResult = class {
|
|
942
1180
|
};
|
943
1181
|
|
944
1182
|
// core/generate-text/generate-text.ts
|
945
|
-
var
|
1183
|
+
var import_zod_to_json_schema3 = __toESM(require("zod-to-json-schema"));
|
946
1184
|
|
947
1185
|
// core/generate-text/tool-call.ts
|
948
1186
|
function parseToolCall({
|
@@ -959,7 +1197,7 @@ function parseToolCall({
|
|
959
1197
|
}
|
960
1198
|
const parseResult = safeParseJSON({
|
961
1199
|
text: toolCall.args,
|
962
|
-
schema:
|
1200
|
+
schema: tool2.parameters
|
963
1201
|
});
|
964
1202
|
if (parseResult.success === false) {
|
965
1203
|
throw new Error(
|
@@ -981,27 +1219,33 @@ async function generateText({
|
|
981
1219
|
system,
|
982
1220
|
prompt,
|
983
1221
|
messages,
|
1222
|
+
maxRetries,
|
1223
|
+
abortSignal,
|
984
1224
|
...settings
|
985
1225
|
}) {
|
986
1226
|
var _a, _b;
|
987
|
-
const
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
type: "
|
992
|
-
name,
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
prompt
|
1002
|
-
|
1227
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
1228
|
+
const modelResponse = await retry(
|
1229
|
+
() => model.doGenerate({
|
1230
|
+
mode: {
|
1231
|
+
type: "regular",
|
1232
|
+
tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
|
1233
|
+
type: "function",
|
1234
|
+
name,
|
1235
|
+
description: tool2.description,
|
1236
|
+
parameters: (0, import_zod_to_json_schema3.default)(tool2.parameters)
|
1237
|
+
}))
|
1238
|
+
},
|
1239
|
+
...validateCallSettings(settings),
|
1240
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1241
|
+
prompt: convertToLanguageModelPrompt({
|
1242
|
+
system,
|
1243
|
+
prompt,
|
1244
|
+
messages
|
1245
|
+
}),
|
1246
|
+
abortSignal
|
1003
1247
|
})
|
1004
|
-
|
1248
|
+
);
|
1005
1249
|
const toolCalls = [];
|
1006
1250
|
for (const modelToolCall of (_a = modelResponse.toolCalls) != null ? _a : []) {
|
1007
1251
|
toolCalls.push(parseToolCall({ toolCall: modelToolCall, tools }));
|
@@ -1013,7 +1257,9 @@ async function generateText({
|
|
1013
1257
|
// they can check the length of the string:
|
1014
1258
|
text: (_b = modelResponse.text) != null ? _b : "",
|
1015
1259
|
toolCalls,
|
1016
|
-
toolResults
|
1260
|
+
toolResults,
|
1261
|
+
finishReason: modelResponse.finishReason,
|
1262
|
+
usage: calculateTokenUsage(modelResponse.usage)
|
1017
1263
|
});
|
1018
1264
|
}
|
1019
1265
|
async function executeTools({
|
@@ -1044,11 +1290,13 @@ var GenerateTextResult = class {
|
|
1044
1290
|
this.text = options.text;
|
1045
1291
|
this.toolCalls = options.toolCalls;
|
1046
1292
|
this.toolResults = options.toolResults;
|
1293
|
+
this.finishReason = options.finishReason;
|
1294
|
+
this.usage = options.usage;
|
1047
1295
|
}
|
1048
1296
|
};
|
1049
1297
|
|
1050
1298
|
// core/generate-text/stream-text.ts
|
1051
|
-
var
|
1299
|
+
var import_zod_to_json_schema4 = __toESM(require("zod-to-json-schema"));
|
1052
1300
|
|
1053
1301
|
// core/generate-text/run-tools-transformation.ts
|
1054
1302
|
var import_nanoid = require("nanoid");
|
@@ -1127,6 +1375,7 @@ function runToolsTransformation({
|
|
1127
1375
|
}
|
1128
1376
|
break;
|
1129
1377
|
}
|
1378
|
+
case "finish-metadata":
|
1130
1379
|
case "tool-call-delta": {
|
1131
1380
|
break;
|
1132
1381
|
}
|
@@ -1168,238 +1417,6 @@ function runToolsTransformation({
|
|
1168
1417
|
});
|
1169
1418
|
}
|
1170
1419
|
|
1171
|
-
// shared/stream-parts.ts
|
1172
|
-
var textStreamPart = {
|
1173
|
-
code: "0",
|
1174
|
-
name: "text",
|
1175
|
-
parse: (value) => {
|
1176
|
-
if (typeof value !== "string") {
|
1177
|
-
throw new Error('"text" parts expect a string value.');
|
1178
|
-
}
|
1179
|
-
return { type: "text", value };
|
1180
|
-
}
|
1181
|
-
};
|
1182
|
-
var functionCallStreamPart = {
|
1183
|
-
code: "1",
|
1184
|
-
name: "function_call",
|
1185
|
-
parse: (value) => {
|
1186
|
-
if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
|
1187
|
-
throw new Error(
|
1188
|
-
'"function_call" parts expect an object with a "function_call" property.'
|
1189
|
-
);
|
1190
|
-
}
|
1191
|
-
return {
|
1192
|
-
type: "function_call",
|
1193
|
-
value
|
1194
|
-
};
|
1195
|
-
}
|
1196
|
-
};
|
1197
|
-
var dataStreamPart = {
|
1198
|
-
code: "2",
|
1199
|
-
name: "data",
|
1200
|
-
parse: (value) => {
|
1201
|
-
if (!Array.isArray(value)) {
|
1202
|
-
throw new Error('"data" parts expect an array value.');
|
1203
|
-
}
|
1204
|
-
return { type: "data", value };
|
1205
|
-
}
|
1206
|
-
};
|
1207
|
-
var errorStreamPart = {
|
1208
|
-
code: "3",
|
1209
|
-
name: "error",
|
1210
|
-
parse: (value) => {
|
1211
|
-
if (typeof value !== "string") {
|
1212
|
-
throw new Error('"error" parts expect a string value.');
|
1213
|
-
}
|
1214
|
-
return { type: "error", value };
|
1215
|
-
}
|
1216
|
-
};
|
1217
|
-
var assistantMessageStreamPart = {
|
1218
|
-
code: "4",
|
1219
|
-
name: "assistant_message",
|
1220
|
-
parse: (value) => {
|
1221
|
-
if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
|
1222
|
-
(item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
|
1223
|
-
)) {
|
1224
|
-
throw new Error(
|
1225
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
1226
|
-
);
|
1227
|
-
}
|
1228
|
-
return {
|
1229
|
-
type: "assistant_message",
|
1230
|
-
value
|
1231
|
-
};
|
1232
|
-
}
|
1233
|
-
};
|
1234
|
-
var assistantControlDataStreamPart = {
|
1235
|
-
code: "5",
|
1236
|
-
name: "assistant_control_data",
|
1237
|
-
parse: (value) => {
|
1238
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
1239
|
-
throw new Error(
|
1240
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
1241
|
-
);
|
1242
|
-
}
|
1243
|
-
return {
|
1244
|
-
type: "assistant_control_data",
|
1245
|
-
value: {
|
1246
|
-
threadId: value.threadId,
|
1247
|
-
messageId: value.messageId
|
1248
|
-
}
|
1249
|
-
};
|
1250
|
-
}
|
1251
|
-
};
|
1252
|
-
var dataMessageStreamPart = {
|
1253
|
-
code: "6",
|
1254
|
-
name: "data_message",
|
1255
|
-
parse: (value) => {
|
1256
|
-
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
1257
|
-
throw new Error(
|
1258
|
-
'"data_message" parts expect an object with a "role" and "data" property.'
|
1259
|
-
);
|
1260
|
-
}
|
1261
|
-
return {
|
1262
|
-
type: "data_message",
|
1263
|
-
value
|
1264
|
-
};
|
1265
|
-
}
|
1266
|
-
};
|
1267
|
-
var toolCallStreamPart = {
|
1268
|
-
code: "7",
|
1269
|
-
name: "tool_calls",
|
1270
|
-
parse: (value) => {
|
1271
|
-
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some((tc) => {
|
1272
|
-
tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string";
|
1273
|
-
})) {
|
1274
|
-
throw new Error(
|
1275
|
-
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
1276
|
-
);
|
1277
|
-
}
|
1278
|
-
return {
|
1279
|
-
type: "tool_calls",
|
1280
|
-
value
|
1281
|
-
};
|
1282
|
-
}
|
1283
|
-
};
|
1284
|
-
var messageAnnotationsStreamPart = {
|
1285
|
-
code: "8",
|
1286
|
-
name: "message_annotations",
|
1287
|
-
parse: (value) => {
|
1288
|
-
if (!Array.isArray(value)) {
|
1289
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
1290
|
-
}
|
1291
|
-
return { type: "message_annotations", value };
|
1292
|
-
}
|
1293
|
-
};
|
1294
|
-
var streamParts = [
|
1295
|
-
textStreamPart,
|
1296
|
-
functionCallStreamPart,
|
1297
|
-
dataStreamPart,
|
1298
|
-
errorStreamPart,
|
1299
|
-
assistantMessageStreamPart,
|
1300
|
-
assistantControlDataStreamPart,
|
1301
|
-
dataMessageStreamPart,
|
1302
|
-
toolCallStreamPart,
|
1303
|
-
messageAnnotationsStreamPart
|
1304
|
-
];
|
1305
|
-
var streamPartsByCode = {
|
1306
|
-
[textStreamPart.code]: textStreamPart,
|
1307
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
1308
|
-
[dataStreamPart.code]: dataStreamPart,
|
1309
|
-
[errorStreamPart.code]: errorStreamPart,
|
1310
|
-
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
1311
|
-
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
1312
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
1313
|
-
[toolCallStreamPart.code]: toolCallStreamPart,
|
1314
|
-
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
|
1315
|
-
};
|
1316
|
-
var StreamStringPrefixes = {
|
1317
|
-
[textStreamPart.name]: textStreamPart.code,
|
1318
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
1319
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
1320
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
1321
|
-
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
1322
|
-
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
1323
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
1324
|
-
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
1325
|
-
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
|
1326
|
-
};
|
1327
|
-
var validCodes = streamParts.map((part) => part.code);
|
1328
|
-
function formatStreamPart(type, value) {
|
1329
|
-
const streamPart = streamParts.find((part) => part.name === type);
|
1330
|
-
if (!streamPart) {
|
1331
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
1332
|
-
}
|
1333
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
1334
|
-
`;
|
1335
|
-
}
|
1336
|
-
|
1337
|
-
// shared/utils.ts
|
1338
|
-
var import_non_secure = require("nanoid/non-secure");
|
1339
|
-
var nanoid2 = (0, import_non_secure.customAlphabet)(
|
1340
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
1341
|
-
7
|
1342
|
-
);
|
1343
|
-
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
1344
|
-
|
1345
|
-
// core/generate-text/stream-text-http-response.ts
|
1346
|
-
var StreamTextHttpResponse = class extends Response {
|
1347
|
-
constructor(messageStream) {
|
1348
|
-
super(
|
1349
|
-
messageStream.pipeThrough(
|
1350
|
-
new TransformStream({
|
1351
|
-
transform(chunk, controller) {
|
1352
|
-
var _a;
|
1353
|
-
switch (chunk.type) {
|
1354
|
-
case "error": {
|
1355
|
-
break;
|
1356
|
-
}
|
1357
|
-
case "text-delta": {
|
1358
|
-
controller.enqueue(formatStreamPart("text", chunk.textDelta));
|
1359
|
-
break;
|
1360
|
-
}
|
1361
|
-
case "tool-call": {
|
1362
|
-
controller.enqueue(
|
1363
|
-
formatStreamPart("tool_calls", {
|
1364
|
-
tool_calls: [
|
1365
|
-
{
|
1366
|
-
type: "function",
|
1367
|
-
id: (_a = chunk.toolCallId) != null ? _a : "",
|
1368
|
-
// TODO client need to support null id
|
1369
|
-
function: {
|
1370
|
-
name: chunk.toolName,
|
1371
|
-
arguments: JSON.stringify(chunk.args)
|
1372
|
-
}
|
1373
|
-
}
|
1374
|
-
]
|
1375
|
-
})
|
1376
|
-
);
|
1377
|
-
break;
|
1378
|
-
}
|
1379
|
-
case "tool-result": {
|
1380
|
-
break;
|
1381
|
-
}
|
1382
|
-
default: {
|
1383
|
-
const exhaustiveCheck = chunk;
|
1384
|
-
throw new Error(
|
1385
|
-
`Unhandled stream part type: ${exhaustiveCheck}`
|
1386
|
-
);
|
1387
|
-
}
|
1388
|
-
}
|
1389
|
-
}
|
1390
|
-
})
|
1391
|
-
),
|
1392
|
-
{
|
1393
|
-
status: 200,
|
1394
|
-
headers: {
|
1395
|
-
"Content-Type": "text/plain; charset=utf-8",
|
1396
|
-
[COMPLEX_HEADER]: "true"
|
1397
|
-
}
|
1398
|
-
}
|
1399
|
-
);
|
1400
|
-
}
|
1401
|
-
};
|
1402
|
-
|
1403
1420
|
// core/generate-text/stream-text.ts
|
1404
1421
|
async function streamText({
|
1405
1422
|
model,
|
@@ -1407,26 +1424,32 @@ async function streamText({
|
|
1407
1424
|
system,
|
1408
1425
|
prompt,
|
1409
1426
|
messages,
|
1427
|
+
maxRetries,
|
1428
|
+
abortSignal,
|
1410
1429
|
...settings
|
1411
1430
|
}) {
|
1412
|
-
const
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
type: "
|
1417
|
-
name,
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
prompt
|
1427
|
-
|
1431
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
1432
|
+
const { stream, warnings } = await retry(
|
1433
|
+
() => model.doStream({
|
1434
|
+
mode: {
|
1435
|
+
type: "regular",
|
1436
|
+
tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
|
1437
|
+
type: "function",
|
1438
|
+
name,
|
1439
|
+
description: tool2.description,
|
1440
|
+
parameters: (0, import_zod_to_json_schema4.default)(tool2.parameters)
|
1441
|
+
}))
|
1442
|
+
},
|
1443
|
+
...validateCallSettings(settings),
|
1444
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1445
|
+
prompt: convertToLanguageModelPrompt({
|
1446
|
+
system,
|
1447
|
+
prompt,
|
1448
|
+
messages
|
1449
|
+
}),
|
1450
|
+
abortSignal
|
1428
1451
|
})
|
1429
|
-
|
1452
|
+
);
|
1430
1453
|
const toolStream = runToolsTransformation({
|
1431
1454
|
tools,
|
1432
1455
|
generatorStream: stream
|
@@ -1451,6 +1474,9 @@ var StreamTextResult = class {
|
|
1451
1474
|
return { value: value.textDelta, done: false };
|
1452
1475
|
}
|
1453
1476
|
}
|
1477
|
+
if (value.type === "error") {
|
1478
|
+
console.error("Error:", value.error);
|
1479
|
+
}
|
1454
1480
|
}
|
1455
1481
|
}
|
1456
1482
|
};
|
@@ -1479,48 +1505,18 @@ var StreamTextResult = class {
|
|
1479
1505
|
}
|
1480
1506
|
};
|
1481
1507
|
}
|
1482
|
-
toResponse() {
|
1483
|
-
return new StreamTextHttpResponse(this.rootStream);
|
1484
|
-
}
|
1485
1508
|
};
|
1486
1509
|
|
1487
1510
|
// core/tool/tool.ts
|
1488
1511
|
function tool(tool2) {
|
1489
1512
|
return tool2;
|
1490
1513
|
}
|
1491
|
-
|
1492
|
-
// core/language-model/errors/unsupported-functionality-error.ts
|
1493
|
-
var UnsupportedFunctionalityError = class extends Error {
|
1494
|
-
constructor({
|
1495
|
-
provider,
|
1496
|
-
functionality
|
1497
|
-
}) {
|
1498
|
-
super(
|
1499
|
-
`Functionality not supported by the provider. Provider: ${provider}.
|
1500
|
-
Functionality: ${functionality}`
|
1501
|
-
);
|
1502
|
-
this.name = "UnsupportedFunctionalityError";
|
1503
|
-
this.provider = provider;
|
1504
|
-
this.functionality = functionality;
|
1505
|
-
}
|
1506
|
-
toJSON() {
|
1507
|
-
return {
|
1508
|
-
name: this.name,
|
1509
|
-
message: this.message,
|
1510
|
-
stack: this.stack,
|
1511
|
-
provider: this.provider,
|
1512
|
-
functionality: this.functionality
|
1513
|
-
};
|
1514
|
-
}
|
1515
|
-
};
|
1516
1514
|
// Annotate the CommonJS export names for ESM import in node:
|
1517
1515
|
0 && (module.exports = {
|
1518
1516
|
GenerateObjectResult,
|
1519
1517
|
GenerateTextResult,
|
1520
1518
|
StreamObjectResult,
|
1521
|
-
StreamTextHttpResponse,
|
1522
1519
|
StreamTextResult,
|
1523
|
-
UnsupportedFunctionalityError,
|
1524
1520
|
convertDataContentToBase64String,
|
1525
1521
|
convertDataContentToUint8Array,
|
1526
1522
|
generateObject,
|