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.
Files changed (63) hide show
  1. package/ai-model-specification/dist/index.d.mts +606 -0
  2. package/ai-model-specification/dist/index.d.ts +606 -0
  3. package/ai-model-specification/dist/index.js +617 -0
  4. package/ai-model-specification/dist/index.js.map +1 -0
  5. package/ai-model-specification/dist/index.mjs +560 -0
  6. package/ai-model-specification/dist/index.mjs.map +1 -0
  7. package/core/dist/index.d.mts +195 -85
  8. package/core/dist/index.d.ts +195 -85
  9. package/core/dist/index.js +497 -501
  10. package/core/dist/index.js.map +1 -1
  11. package/core/dist/index.mjs +497 -499
  12. package/core/dist/index.mjs.map +1 -1
  13. package/dist/index.d.mts +5 -2
  14. package/dist/index.d.ts +5 -2
  15. package/dist/index.js +39 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/index.mjs +39 -1
  18. package/dist/index.mjs.map +1 -1
  19. package/package.json +16 -7
  20. package/prompts/dist/index.d.mts +32 -19
  21. package/prompts/dist/index.d.ts +32 -19
  22. package/prompts/dist/index.js +0 -1
  23. package/prompts/dist/index.js.map +1 -1
  24. package/prompts/dist/index.mjs +0 -1
  25. package/prompts/dist/index.mjs.map +1 -1
  26. package/provider/dist/index.d.mts +232 -190
  27. package/provider/dist/index.d.ts +232 -190
  28. package/provider/dist/index.js +838 -26131
  29. package/provider/dist/index.js.map +1 -1
  30. package/provider/dist/index.mjs +806 -7735
  31. package/provider/dist/index.mjs.map +1 -1
  32. package/react/dist/index.d.mts +4 -4
  33. package/react/dist/index.d.ts +4 -4
  34. package/react/dist/index.js +16 -1
  35. package/react/dist/index.js.map +1 -1
  36. package/react/dist/index.mjs +16 -1
  37. package/react/dist/index.mjs.map +1 -1
  38. package/rsc/dist/index.d.ts +11 -0
  39. package/rsc/dist/rsc-server.d.mts +11 -0
  40. package/rsc/dist/rsc-server.mjs +21 -21
  41. package/rsc/dist/rsc-server.mjs.map +1 -1
  42. package/rsc/dist/rsc-shared.mjs +21 -1
  43. package/rsc/dist/rsc-shared.mjs.map +1 -1
  44. package/provider/dist/chunk-3DTRVHCT.mjs +0 -5046
  45. package/provider/dist/chunk-3DTRVHCT.mjs.map +0 -1
  46. package/provider/dist/chunk-4OUDS3CP.mjs +0 -30
  47. package/provider/dist/chunk-4OUDS3CP.mjs.map +0 -1
  48. package/provider/dist/chunk-5IYCPJBV.mjs +0 -56
  49. package/provider/dist/chunk-5IYCPJBV.mjs.map +0 -1
  50. package/provider/dist/chunk-VB2TCVQ4.mjs +0 -6746
  51. package/provider/dist/chunk-VB2TCVQ4.mjs.map +0 -1
  52. package/provider/dist/chunk-VYIXVZ6L.mjs +0 -317
  53. package/provider/dist/chunk-VYIXVZ6L.mjs.map +0 -1
  54. package/provider/dist/chunk-WTOUHN6A.mjs +0 -2251
  55. package/provider/dist/chunk-WTOUHN6A.mjs.map +0 -1
  56. package/provider/dist/client-22WAAXR7.mjs +0 -10
  57. package/provider/dist/client-22WAAXR7.mjs.map +0 -1
  58. package/provider/dist/fileFromPath-23RINPB2.mjs +0 -115
  59. package/provider/dist/fileFromPath-23RINPB2.mjs.map +0 -1
  60. package/provider/dist/lib-BZMMM4HX.mjs +0 -20
  61. package/provider/dist/lib-BZMMM4HX.mjs.map +0 -1
  62. package/provider/dist/openai-3YL4AWLI.mjs +0 -3451
  63. package/provider/dist/openai-3YL4AWLI.mjs.map +0 -1
@@ -1,4 +1,183 @@
1
- // core/util/uint8-utils.ts
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
2
181
  function convertBase64ToUint8Array(base64String) {
3
182
  const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
4
183
  const latin1string = globalThis.atob(base64Url);
@@ -12,6 +191,15 @@ function convertUint8ArrayToBase64(array) {
12
191
  return globalThis.btoa(latin1string);
13
192
  }
14
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
+
15
203
  // core/prompt/data-content.ts
16
204
  function convertDataContentToBase64String(content) {
17
205
  if (typeof content === "string") {
@@ -81,7 +269,7 @@ function convertToLanguageModelPrompt({
81
269
  case "image": {
82
270
  return {
83
271
  type: "image",
84
- image: convertDataContentToUint8Array(part.image),
272
+ image: part.image instanceof URL ? part.image : convertDataContentToUint8Array(part.image),
85
273
  mimeType: part.mimeType
86
274
  };
87
275
  }
@@ -123,129 +311,160 @@ function getInputFormat({
123
311
  return prompt != null ? "prompt" : "messages";
124
312
  }
125
313
 
126
- // core/schema/parse-json.ts
127
- import SecureJSON from "secure-json-parse";
128
-
129
- // core/language-model/errors/get-error-message.ts
130
- function getErrorMessage(error) {
131
- if (error == null) {
132
- return "unknown error";
133
- }
134
- if (typeof error === "string") {
135
- return error;
136
- }
137
- if (error instanceof Error) {
138
- return error.message;
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
+ }
139
331
  }
140
- return JSON.stringify(error);
141
- }
142
-
143
- // core/language-model/errors/json-parse-error.ts
144
- var JSONParseError = class extends Error {
145
- constructor({ text, cause }) {
146
- super(
147
- `JSON parsing failed: Text: ${text}.
148
- Error message: ${getErrorMessage(cause)}`
149
- );
150
- this.name = "JSONParseError";
151
- this.cause = cause;
152
- this.text = text;
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
+ }
153
347
  }
154
- toJSON() {
155
- return {
156
- name: this.name,
157
- message: this.message,
158
- cause: this.cause,
159
- stack: this.stack,
160
- valueText: this.text
161
- };
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
+ }
162
363
  }
163
- };
164
-
165
- // core/language-model/errors/type-validation-error.ts
166
- var TypeValidationError = class extends Error {
167
- constructor({ value, cause }) {
168
- super(
169
- `Type validation failed: Value: ${JSON.stringify(value)}.
170
- Error message: ${getErrorMessage(cause)}`
171
- );
172
- this.name = "TypeValidationError";
173
- this.cause = cause;
174
- this.value = value;
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
+ }
175
379
  }
176
- toJSON() {
177
- return {
178
- name: this.name,
179
- message: this.message,
180
- cause: this.cause,
181
- stack: this.stack,
182
- value: this.value
183
- };
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
+ }
184
395
  }
185
- };
186
-
187
- // core/schema/validate-types.ts
188
- function safeValidateTypes({
189
- value,
190
- schema
191
- }) {
192
- try {
193
- const validationResult = schema.validate(value);
194
- if (validationResult.success) {
195
- return validationResult;
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
+ });
196
403
  }
197
- return {
198
- success: false,
199
- error: new TypeValidationError({
200
- value,
201
- cause: validationResult.error
202
- })
203
- };
204
- } catch (error) {
205
- return {
206
- success: false,
207
- error: error instanceof TypeValidationError ? error : new TypeValidationError({ value, cause: error })
208
- };
209
404
  }
405
+ return settings;
210
406
  }
211
407
 
212
- // core/schema/parse-json.ts
213
- function safeParseJSON({
214
- text,
215
- schema
216
- }) {
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 = []) {
217
428
  try {
218
- const value = SecureJSON.parse(text);
219
- if (schema == null) {
220
- return {
221
- success: true,
222
- value
223
- };
224
- }
225
- return safeValidateTypes({ value, schema });
429
+ return await f();
226
430
  } catch (error) {
227
- return {
228
- success: false,
229
- error: error instanceof JSONParseError ? error : new JSONParseError({ text, cause: error })
230
- };
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
+ });
231
465
  }
232
466
  }
233
467
 
234
- // core/schema/zod-schema.ts
235
- import { zodToJsonSchema } from "zod-to-json-schema";
236
- var ZodSchema = class {
237
- constructor(zodSchema) {
238
- this.zodSchema = zodSchema;
239
- }
240
- validate(value) {
241
- const result = this.zodSchema.safeParse(value);
242
- return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
243
- }
244
- getJsonSchema() {
245
- return zodToJsonSchema(this.zodSchema);
246
- }
247
- };
248
-
249
468
  // core/generate-object/inject-json-schema-into-system.ts
250
469
  var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
251
470
  var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
@@ -265,94 +484,97 @@ function injectJsonSchemaIntoSystem({
265
484
  ].filter((line) => line != null).join("\n");
266
485
  }
267
486
 
268
- // core/language-model/errors/no-object-generated-error.ts
269
- var NoTextGeneratedError = class extends Error {
270
- constructor() {
271
- super(`No text generated.`);
272
- this.name = "NoTextGeneratedError";
273
- }
274
- toJSON() {
275
- return {
276
- name: this.name,
277
- cause: this.cause,
278
- message: this.message,
279
- stack: this.stack
280
- };
281
- }
282
- };
283
-
284
487
  // core/generate-object/generate-object.ts
285
488
  async function generateObject({
286
489
  model,
287
- schema: zodSchema,
490
+ schema,
288
491
  mode,
289
492
  system,
290
493
  prompt,
291
494
  messages,
495
+ maxRetries,
496
+ abortSignal,
292
497
  ...settings
293
498
  }) {
294
499
  var _a, _b;
295
- const schema = new ZodSchema(zodSchema);
296
- const jsonSchema = schema.getJsonSchema();
297
- let result;
500
+ const retry = retryWithExponentialBackoff({ maxRetries });
501
+ const jsonSchema = zodToJsonSchema(schema);
298
502
  if (mode === "auto" || mode == null) {
299
503
  mode = model.defaultObjectGenerationMode;
300
504
  }
505
+ let result;
506
+ let finishReason;
507
+ let usage;
301
508
  switch (mode) {
302
509
  case "json": {
303
- const generateResult = await model.doGenerate({
304
- mode: { type: "object-json" },
305
- ...settings,
306
- inputFormat: getInputFormat({ prompt, messages }),
307
- prompt: convertToLanguageModelPrompt({
308
- system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
309
- prompt,
310
- messages
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
311
521
  })
312
- });
522
+ );
313
523
  if (generateResult.text === void 0) {
314
524
  throw new NoTextGeneratedError();
315
525
  }
316
526
  result = generateResult.text;
527
+ finishReason = generateResult.finishReason;
528
+ usage = generateResult.usage;
317
529
  break;
318
530
  }
319
531
  case "grammar": {
320
- const generateResult = await model.doGenerate({
321
- mode: { type: "object-grammar", schema: jsonSchema },
322
- ...settings,
323
- inputFormat: getInputFormat({ prompt, messages }),
324
- prompt: convertToLanguageModelPrompt({
325
- system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
326
- prompt,
327
- messages
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
328
543
  })
329
- });
544
+ );
330
545
  if (generateResult.text === void 0) {
331
546
  throw new NoTextGeneratedError();
332
547
  }
333
548
  result = generateResult.text;
549
+ finishReason = generateResult.finishReason;
550
+ usage = generateResult.usage;
334
551
  break;
335
552
  }
336
553
  case "tool": {
337
- const generateResult = await model.doGenerate({
338
- mode: {
339
- type: "object-tool",
340
- tool: {
341
- type: "function",
342
- name: "json",
343
- description: "Respond with a JSON object.",
344
- parameters: jsonSchema
345
- }
346
- },
347
- ...settings,
348
- inputFormat: getInputFormat({ prompt, messages }),
349
- prompt: convertToLanguageModelPrompt({ system, prompt, messages })
350
- });
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
+ );
351
571
  const functionArgs = (_b = (_a = generateResult.toolCalls) == null ? void 0 : _a[0]) == null ? void 0 : _b.args;
352
572
  if (functionArgs === void 0) {
353
573
  throw new NoTextGeneratedError();
354
574
  }
355
575
  result = functionArgs;
576
+ finishReason = generateResult.finishReason;
577
+ usage = generateResult.usage;
356
578
  break;
357
579
  }
358
580
  case void 0: {
@@ -368,15 +590,22 @@ async function generateObject({
368
590
  throw parseResult.error;
369
591
  }
370
592
  return new GenerateObjectResult({
371
- object: parseResult.value
593
+ object: parseResult.value,
594
+ finishReason,
595
+ usage: calculateTokenUsage(usage)
372
596
  });
373
597
  }
374
598
  var GenerateObjectResult = class {
375
599
  constructor(options) {
376
600
  this.object = options.object;
601
+ this.finishReason = options.finishReason;
602
+ this.usage = options.usage;
377
603
  }
378
604
  };
379
605
 
606
+ // core/generate-object/stream-object.ts
607
+ import zodToJsonSchema2 from "zod-to-json-schema";
608
+
380
609
  // core/util/is-deep-equal-data.ts
381
610
  function isDeepEqualData(obj1, obj2) {
382
611
  if (obj1 === obj2)
@@ -752,31 +981,36 @@ function parsePartialJson(jsonText) {
752
981
  // core/generate-object/stream-object.ts
753
982
  async function streamObject({
754
983
  model,
755
- schema: zodSchema,
984
+ schema,
756
985
  mode,
757
986
  system,
758
987
  prompt,
759
988
  messages,
989
+ maxRetries,
990
+ abortSignal,
760
991
  ...settings
761
992
  }) {
762
- const schema = new ZodSchema(zodSchema);
763
- const jsonSchema = schema.getJsonSchema();
993
+ const retry = retryWithExponentialBackoff({ maxRetries });
994
+ const jsonSchema = zodToJsonSchema2(schema);
764
995
  let modelStream;
765
996
  if (mode === "auto" || mode == null) {
766
997
  mode = model.defaultObjectGenerationMode;
767
998
  }
768
999
  switch (mode) {
769
1000
  case "json": {
770
- const { stream, warnings } = await model.doStream({
771
- mode: { type: "object-json" },
772
- ...settings,
773
- inputFormat: getInputFormat({ prompt, messages }),
774
- prompt: convertToLanguageModelPrompt({
775
- system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
776
- prompt,
777
- messages
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
778
1012
  })
779
- });
1013
+ );
780
1014
  modelStream = stream.pipeThrough(
781
1015
  new TransformStream({
782
1016
  transform(chunk, controller) {
@@ -794,16 +1028,19 @@ async function streamObject({
794
1028
  break;
795
1029
  }
796
1030
  case "grammar": {
797
- const { stream, warnings } = await model.doStream({
798
- mode: { type: "object-grammar", schema: jsonSchema },
799
- ...settings,
800
- inputFormat: getInputFormat({ prompt, messages }),
801
- prompt: convertToLanguageModelPrompt({
802
- system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
803
- prompt,
804
- messages
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
805
1042
  })
806
- });
1043
+ );
807
1044
  modelStream = stream.pipeThrough(
808
1045
  new TransformStream({
809
1046
  transform(chunk, controller) {
@@ -821,20 +1058,23 @@ async function streamObject({
821
1058
  break;
822
1059
  }
823
1060
  case "tool": {
824
- const { stream, warnings } = await model.doStream({
825
- mode: {
826
- type: "object-tool",
827
- tool: {
828
- type: "function",
829
- name: "json",
830
- description: "Respond with a JSON object.",
831
- parameters: jsonSchema
832
- }
833
- },
834
- ...settings,
835
- inputFormat: getInputFormat({ prompt, messages }),
836
- prompt: convertToLanguageModelPrompt({ system, prompt, messages })
837
- });
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
+ );
838
1078
  modelStream = stream.pipeThrough(
839
1079
  new TransformStream({
840
1080
  transform(chunk, controller) {
@@ -894,7 +1134,7 @@ var StreamObjectResult = class {
894
1134
  };
895
1135
 
896
1136
  // core/generate-text/generate-text.ts
897
- import zodToJsonSchema2 from "zod-to-json-schema";
1137
+ import zodToJsonSchema3 from "zod-to-json-schema";
898
1138
 
899
1139
  // core/generate-text/tool-call.ts
900
1140
  function parseToolCall({
@@ -911,7 +1151,7 @@ function parseToolCall({
911
1151
  }
912
1152
  const parseResult = safeParseJSON({
913
1153
  text: toolCall.args,
914
- schema: new ZodSchema(tool2.parameters)
1154
+ schema: tool2.parameters
915
1155
  });
916
1156
  if (parseResult.success === false) {
917
1157
  throw new Error(
@@ -933,27 +1173,33 @@ async function generateText({
933
1173
  system,
934
1174
  prompt,
935
1175
  messages,
1176
+ maxRetries,
1177
+ abortSignal,
936
1178
  ...settings
937
1179
  }) {
938
1180
  var _a, _b;
939
- const modelResponse = await model.doGenerate({
940
- mode: {
941
- type: "regular",
942
- tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
943
- type: "function",
944
- name,
945
- description: tool2.description,
946
- parameters: zodToJsonSchema2(tool2.parameters)
947
- }))
948
- },
949
- ...settings,
950
- inputFormat: getInputFormat({ prompt, messages }),
951
- prompt: convertToLanguageModelPrompt({
952
- system,
953
- prompt,
954
- messages
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
955
1201
  })
956
- });
1202
+ );
957
1203
  const toolCalls = [];
958
1204
  for (const modelToolCall of (_a = modelResponse.toolCalls) != null ? _a : []) {
959
1205
  toolCalls.push(parseToolCall({ toolCall: modelToolCall, tools }));
@@ -965,7 +1211,9 @@ async function generateText({
965
1211
  // they can check the length of the string:
966
1212
  text: (_b = modelResponse.text) != null ? _b : "",
967
1213
  toolCalls,
968
- toolResults
1214
+ toolResults,
1215
+ finishReason: modelResponse.finishReason,
1216
+ usage: calculateTokenUsage(modelResponse.usage)
969
1217
  });
970
1218
  }
971
1219
  async function executeTools({
@@ -996,11 +1244,13 @@ var GenerateTextResult = class {
996
1244
  this.text = options.text;
997
1245
  this.toolCalls = options.toolCalls;
998
1246
  this.toolResults = options.toolResults;
1247
+ this.finishReason = options.finishReason;
1248
+ this.usage = options.usage;
999
1249
  }
1000
1250
  };
1001
1251
 
1002
1252
  // core/generate-text/stream-text.ts
1003
- import zodToJsonSchema3 from "zod-to-json-schema";
1253
+ import zodToJsonSchema4 from "zod-to-json-schema";
1004
1254
 
1005
1255
  // core/generate-text/run-tools-transformation.ts
1006
1256
  import { nanoid } from "nanoid";
@@ -1079,6 +1329,7 @@ function runToolsTransformation({
1079
1329
  }
1080
1330
  break;
1081
1331
  }
1332
+ case "finish-metadata":
1082
1333
  case "tool-call-delta": {
1083
1334
  break;
1084
1335
  }
@@ -1120,238 +1371,6 @@ function runToolsTransformation({
1120
1371
  });
1121
1372
  }
1122
1373
 
1123
- // shared/stream-parts.ts
1124
- var textStreamPart = {
1125
- code: "0",
1126
- name: "text",
1127
- parse: (value) => {
1128
- if (typeof value !== "string") {
1129
- throw new Error('"text" parts expect a string value.');
1130
- }
1131
- return { type: "text", value };
1132
- }
1133
- };
1134
- var functionCallStreamPart = {
1135
- code: "1",
1136
- name: "function_call",
1137
- parse: (value) => {
1138
- 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") {
1139
- throw new Error(
1140
- '"function_call" parts expect an object with a "function_call" property.'
1141
- );
1142
- }
1143
- return {
1144
- type: "function_call",
1145
- value
1146
- };
1147
- }
1148
- };
1149
- var dataStreamPart = {
1150
- code: "2",
1151
- name: "data",
1152
- parse: (value) => {
1153
- if (!Array.isArray(value)) {
1154
- throw new Error('"data" parts expect an array value.');
1155
- }
1156
- return { type: "data", value };
1157
- }
1158
- };
1159
- var errorStreamPart = {
1160
- code: "3",
1161
- name: "error",
1162
- parse: (value) => {
1163
- if (typeof value !== "string") {
1164
- throw new Error('"error" parts expect a string value.');
1165
- }
1166
- return { type: "error", value };
1167
- }
1168
- };
1169
- var assistantMessageStreamPart = {
1170
- code: "4",
1171
- name: "assistant_message",
1172
- parse: (value) => {
1173
- 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(
1174
- (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"
1175
- )) {
1176
- throw new Error(
1177
- '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
1178
- );
1179
- }
1180
- return {
1181
- type: "assistant_message",
1182
- value
1183
- };
1184
- }
1185
- };
1186
- var assistantControlDataStreamPart = {
1187
- code: "5",
1188
- name: "assistant_control_data",
1189
- parse: (value) => {
1190
- if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
1191
- throw new Error(
1192
- '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
1193
- );
1194
- }
1195
- return {
1196
- type: "assistant_control_data",
1197
- value: {
1198
- threadId: value.threadId,
1199
- messageId: value.messageId
1200
- }
1201
- };
1202
- }
1203
- };
1204
- var dataMessageStreamPart = {
1205
- code: "6",
1206
- name: "data_message",
1207
- parse: (value) => {
1208
- if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
1209
- throw new Error(
1210
- '"data_message" parts expect an object with a "role" and "data" property.'
1211
- );
1212
- }
1213
- return {
1214
- type: "data_message",
1215
- value
1216
- };
1217
- }
1218
- };
1219
- var toolCallStreamPart = {
1220
- code: "7",
1221
- name: "tool_calls",
1222
- parse: (value) => {
1223
- 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) => {
1224
- 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";
1225
- })) {
1226
- throw new Error(
1227
- '"tool_calls" parts expect an object with a ToolCallPayload.'
1228
- );
1229
- }
1230
- return {
1231
- type: "tool_calls",
1232
- value
1233
- };
1234
- }
1235
- };
1236
- var messageAnnotationsStreamPart = {
1237
- code: "8",
1238
- name: "message_annotations",
1239
- parse: (value) => {
1240
- if (!Array.isArray(value)) {
1241
- throw new Error('"message_annotations" parts expect an array value.');
1242
- }
1243
- return { type: "message_annotations", value };
1244
- }
1245
- };
1246
- var streamParts = [
1247
- textStreamPart,
1248
- functionCallStreamPart,
1249
- dataStreamPart,
1250
- errorStreamPart,
1251
- assistantMessageStreamPart,
1252
- assistantControlDataStreamPart,
1253
- dataMessageStreamPart,
1254
- toolCallStreamPart,
1255
- messageAnnotationsStreamPart
1256
- ];
1257
- var streamPartsByCode = {
1258
- [textStreamPart.code]: textStreamPart,
1259
- [functionCallStreamPart.code]: functionCallStreamPart,
1260
- [dataStreamPart.code]: dataStreamPart,
1261
- [errorStreamPart.code]: errorStreamPart,
1262
- [assistantMessageStreamPart.code]: assistantMessageStreamPart,
1263
- [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
1264
- [dataMessageStreamPart.code]: dataMessageStreamPart,
1265
- [toolCallStreamPart.code]: toolCallStreamPart,
1266
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
1267
- };
1268
- var StreamStringPrefixes = {
1269
- [textStreamPart.name]: textStreamPart.code,
1270
- [functionCallStreamPart.name]: functionCallStreamPart.code,
1271
- [dataStreamPart.name]: dataStreamPart.code,
1272
- [errorStreamPart.name]: errorStreamPart.code,
1273
- [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
1274
- [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
1275
- [dataMessageStreamPart.name]: dataMessageStreamPart.code,
1276
- [toolCallStreamPart.name]: toolCallStreamPart.code,
1277
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
1278
- };
1279
- var validCodes = streamParts.map((part) => part.code);
1280
- function formatStreamPart(type, value) {
1281
- const streamPart = streamParts.find((part) => part.name === type);
1282
- if (!streamPart) {
1283
- throw new Error(`Invalid stream part type: ${type}`);
1284
- }
1285
- return `${streamPart.code}:${JSON.stringify(value)}
1286
- `;
1287
- }
1288
-
1289
- // shared/utils.ts
1290
- import { customAlphabet } from "nanoid/non-secure";
1291
- var nanoid2 = customAlphabet(
1292
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1293
- 7
1294
- );
1295
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
1296
-
1297
- // core/generate-text/stream-text-http-response.ts
1298
- var StreamTextHttpResponse = class extends Response {
1299
- constructor(messageStream) {
1300
- super(
1301
- messageStream.pipeThrough(
1302
- new TransformStream({
1303
- transform(chunk, controller) {
1304
- var _a;
1305
- switch (chunk.type) {
1306
- case "error": {
1307
- break;
1308
- }
1309
- case "text-delta": {
1310
- controller.enqueue(formatStreamPart("text", chunk.textDelta));
1311
- break;
1312
- }
1313
- case "tool-call": {
1314
- controller.enqueue(
1315
- formatStreamPart("tool_calls", {
1316
- tool_calls: [
1317
- {
1318
- type: "function",
1319
- id: (_a = chunk.toolCallId) != null ? _a : "",
1320
- // TODO client need to support null id
1321
- function: {
1322
- name: chunk.toolName,
1323
- arguments: JSON.stringify(chunk.args)
1324
- }
1325
- }
1326
- ]
1327
- })
1328
- );
1329
- break;
1330
- }
1331
- case "tool-result": {
1332
- break;
1333
- }
1334
- default: {
1335
- const exhaustiveCheck = chunk;
1336
- throw new Error(
1337
- `Unhandled stream part type: ${exhaustiveCheck}`
1338
- );
1339
- }
1340
- }
1341
- }
1342
- })
1343
- ),
1344
- {
1345
- status: 200,
1346
- headers: {
1347
- "Content-Type": "text/plain; charset=utf-8",
1348
- [COMPLEX_HEADER]: "true"
1349
- }
1350
- }
1351
- );
1352
- }
1353
- };
1354
-
1355
1374
  // core/generate-text/stream-text.ts
1356
1375
  async function streamText({
1357
1376
  model,
@@ -1359,26 +1378,32 @@ async function streamText({
1359
1378
  system,
1360
1379
  prompt,
1361
1380
  messages,
1381
+ maxRetries,
1382
+ abortSignal,
1362
1383
  ...settings
1363
1384
  }) {
1364
- const { stream, warnings } = await model.doStream({
1365
- mode: {
1366
- type: "regular",
1367
- tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1368
- type: "function",
1369
- name,
1370
- description: tool2.description,
1371
- parameters: zodToJsonSchema3(tool2.parameters)
1372
- }))
1373
- },
1374
- ...settings,
1375
- inputFormat: getInputFormat({ prompt, messages }),
1376
- prompt: convertToLanguageModelPrompt({
1377
- system,
1378
- prompt,
1379
- messages
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
1380
1405
  })
1381
- });
1406
+ );
1382
1407
  const toolStream = runToolsTransformation({
1383
1408
  tools,
1384
1409
  generatorStream: stream
@@ -1403,6 +1428,9 @@ var StreamTextResult = class {
1403
1428
  return { value: value.textDelta, done: false };
1404
1429
  }
1405
1430
  }
1431
+ if (value.type === "error") {
1432
+ console.error("Error:", value.error);
1433
+ }
1406
1434
  }
1407
1435
  }
1408
1436
  };
@@ -1431,47 +1459,17 @@ var StreamTextResult = class {
1431
1459
  }
1432
1460
  };
1433
1461
  }
1434
- toResponse() {
1435
- return new StreamTextHttpResponse(this.rootStream);
1436
- }
1437
1462
  };
1438
1463
 
1439
1464
  // core/tool/tool.ts
1440
1465
  function tool(tool2) {
1441
1466
  return tool2;
1442
1467
  }
1443
-
1444
- // core/language-model/errors/unsupported-functionality-error.ts
1445
- var UnsupportedFunctionalityError = class extends Error {
1446
- constructor({
1447
- provider,
1448
- functionality
1449
- }) {
1450
- super(
1451
- `Functionality not supported by the provider. Provider: ${provider}.
1452
- Functionality: ${functionality}`
1453
- );
1454
- this.name = "UnsupportedFunctionalityError";
1455
- this.provider = provider;
1456
- this.functionality = functionality;
1457
- }
1458
- toJSON() {
1459
- return {
1460
- name: this.name,
1461
- message: this.message,
1462
- stack: this.stack,
1463
- provider: this.provider,
1464
- functionality: this.functionality
1465
- };
1466
- }
1467
- };
1468
1468
  export {
1469
1469
  GenerateObjectResult,
1470
1470
  GenerateTextResult,
1471
1471
  StreamObjectResult,
1472
- StreamTextHttpResponse,
1473
1472
  StreamTextResult,
1474
- UnsupportedFunctionalityError,
1475
1473
  convertDataContentToBase64String,
1476
1474
  convertDataContentToUint8Array,
1477
1475
  generateObject,