ai 3.1.0-canary.0 → 3.1.0-canary.2

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 (61) hide show
  1. package/ai-model-specification/dist/index.d.mts +539 -0
  2. package/ai-model-specification/dist/index.d.ts +539 -0
  3. package/ai-model-specification/dist/index.js +581 -0
  4. package/ai-model-specification/dist/index.js.map +1 -0
  5. package/ai-model-specification/dist/index.mjs +526 -0
  6. package/ai-model-specification/dist/index.mjs.map +1 -0
  7. package/core/dist/index.d.mts +122 -77
  8. package/core/dist/index.d.ts +122 -77
  9. package/core/dist/index.js +266 -174
  10. package/core/dist/index.js.map +1 -1
  11. package/core/dist/index.mjs +266 -173
  12. package/core/dist/index.mjs.map +1 -1
  13. package/dist/index.d.mts +3 -1
  14. package/dist/index.d.ts +3 -1
  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 +14 -4
  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 +154 -191
  27. package/provider/dist/index.d.ts +154 -191
  28. package/provider/dist/index.js +800 -26131
  29. package/provider/dist/index.js.map +1 -1
  30. package/provider/dist/index.mjs +770 -7736
  31. package/provider/dist/index.mjs.map +1 -1
  32. package/react/dist/index.js +16 -1
  33. package/react/dist/index.js.map +1 -1
  34. package/react/dist/index.mjs +16 -1
  35. package/react/dist/index.mjs.map +1 -1
  36. package/rsc/dist/index.d.ts +11 -0
  37. package/rsc/dist/rsc-server.d.mts +11 -0
  38. package/rsc/dist/rsc-server.mjs +21 -21
  39. package/rsc/dist/rsc-server.mjs.map +1 -1
  40. package/rsc/dist/rsc-shared.mjs +21 -1
  41. package/rsc/dist/rsc-shared.mjs.map +1 -1
  42. package/provider/dist/chunk-3DTRVHCT.mjs +0 -5046
  43. package/provider/dist/chunk-3DTRVHCT.mjs.map +0 -1
  44. package/provider/dist/chunk-4OUDS3CP.mjs +0 -30
  45. package/provider/dist/chunk-4OUDS3CP.mjs.map +0 -1
  46. package/provider/dist/chunk-5IYCPJBV.mjs +0 -56
  47. package/provider/dist/chunk-5IYCPJBV.mjs.map +0 -1
  48. package/provider/dist/chunk-VB2TCVQ4.mjs +0 -6746
  49. package/provider/dist/chunk-VB2TCVQ4.mjs.map +0 -1
  50. package/provider/dist/chunk-VYIXVZ6L.mjs +0 -317
  51. package/provider/dist/chunk-VYIXVZ6L.mjs.map +0 -1
  52. package/provider/dist/chunk-WTOUHN6A.mjs +0 -2251
  53. package/provider/dist/chunk-WTOUHN6A.mjs.map +0 -1
  54. package/provider/dist/client-22WAAXR7.mjs +0 -10
  55. package/provider/dist/client-22WAAXR7.mjs.map +0 -1
  56. package/provider/dist/fileFromPath-23RINPB2.mjs +0 -115
  57. package/provider/dist/fileFromPath-23RINPB2.mjs.map +0 -1
  58. package/provider/dist/lib-BZMMM4HX.mjs +0 -20
  59. package/provider/dist/lib-BZMMM4HX.mjs.map +0 -1
  60. package/provider/dist/openai-3YL4AWLI.mjs +0 -3451
  61. package/provider/dist/openai-3YL4AWLI.mjs.map +0 -1
@@ -1,4 +1,159 @@
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/type-validation-error.ts
82
+ var TypeValidationError = class extends Error {
83
+ constructor({ value, cause }) {
84
+ super(
85
+ `Type validation failed: Value: ${JSON.stringify(value)}.
86
+ Error message: ${getErrorMessage(cause)}`
87
+ );
88
+ this.name = "TypeValidationError";
89
+ this.cause = cause;
90
+ this.value = value;
91
+ }
92
+ toJSON() {
93
+ return {
94
+ name: this.name,
95
+ message: this.message,
96
+ cause: this.cause,
97
+ stack: this.stack,
98
+ value: this.value
99
+ };
100
+ }
101
+ };
102
+
103
+ // ai-model-specification/util/parse-json.ts
104
+ import SecureJSON from "secure-json-parse";
105
+
106
+ // ai-model-specification/util/validate-types.ts
107
+ function safeValidateTypes({
108
+ value,
109
+ schema
110
+ }) {
111
+ try {
112
+ const validationResult = schema.safeParse(value);
113
+ if (validationResult.success) {
114
+ return {
115
+ success: true,
116
+ value: validationResult.data
117
+ };
118
+ }
119
+ return {
120
+ success: false,
121
+ error: new TypeValidationError({
122
+ value,
123
+ cause: validationResult.error
124
+ })
125
+ };
126
+ } catch (error) {
127
+ return {
128
+ success: false,
129
+ error: error instanceof TypeValidationError ? error : new TypeValidationError({ value, cause: error })
130
+ };
131
+ }
132
+ }
133
+
134
+ // ai-model-specification/util/parse-json.ts
135
+ function safeParseJSON({
136
+ text,
137
+ schema
138
+ }) {
139
+ try {
140
+ const value = SecureJSON.parse(text);
141
+ if (schema == null) {
142
+ return {
143
+ success: true,
144
+ value
145
+ };
146
+ }
147
+ return safeValidateTypes({ value, schema });
148
+ } catch (error) {
149
+ return {
150
+ success: false,
151
+ error: error instanceof JSONParseError ? error : new JSONParseError({ text, cause: error })
152
+ };
153
+ }
154
+ }
155
+
156
+ // ai-model-specification/util/uint8-utils.ts
2
157
  function convertBase64ToUint8Array(base64String) {
3
158
  const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
4
159
  const latin1string = globalThis.atob(base64Url);
@@ -123,129 +278,100 @@ function getInputFormat({
123
278
  return prompt != null ? "prompt" : "messages";
124
279
  }
125
280
 
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;
139
- }
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;
281
+ // core/prompt/validate-call-settings.ts
282
+ function validateCallSettings(settings) {
283
+ if (settings.maxTokens != null) {
284
+ if (!Number.isInteger(settings.maxTokens)) {
285
+ throw new AI_InvalidArgumentError({
286
+ parameter: "maxTokens",
287
+ value: settings.maxTokens,
288
+ message: "maxTokens must be an integer"
289
+ });
290
+ }
291
+ if (settings.maxTokens < 1) {
292
+ throw new AI_InvalidArgumentError({
293
+ parameter: "maxTokens",
294
+ value: settings.maxTokens,
295
+ message: "maxTokens must be >= 1"
296
+ });
297
+ }
153
298
  }
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
- };
299
+ if (settings.temperature != null) {
300
+ if (typeof settings.temperature !== "number") {
301
+ throw new AI_InvalidArgumentError({
302
+ parameter: "temperature",
303
+ value: settings.temperature,
304
+ message: "temperature must be a number"
305
+ });
306
+ }
307
+ if (settings.temperature < 0 || settings.temperature > 1) {
308
+ throw new AI_InvalidArgumentError({
309
+ parameter: "temperature",
310
+ value: settings.temperature,
311
+ message: "temperature must be between 0 and 1 (inclusive)"
312
+ });
313
+ }
162
314
  }
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;
315
+ if (settings.topP != null) {
316
+ if (typeof settings.topP !== "number") {
317
+ throw new AI_InvalidArgumentError({
318
+ parameter: "topP",
319
+ value: settings.topP,
320
+ message: "topP must be a number"
321
+ });
322
+ }
323
+ if (settings.topP < 0 || settings.topP > 1) {
324
+ throw new AI_InvalidArgumentError({
325
+ parameter: "topP",
326
+ value: settings.topP,
327
+ message: "topP must be between 0 and 1 (inclusive)"
328
+ });
329
+ }
175
330
  }
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
- };
331
+ if (settings.presencePenalty != null) {
332
+ if (typeof settings.presencePenalty !== "number") {
333
+ throw new AI_InvalidArgumentError({
334
+ parameter: "presencePenalty",
335
+ value: settings.presencePenalty,
336
+ message: "presencePenalty must be a number"
337
+ });
338
+ }
339
+ if (settings.presencePenalty < -1 || settings.presencePenalty > 1) {
340
+ throw new AI_InvalidArgumentError({
341
+ parameter: "presencePenalty",
342
+ value: settings.presencePenalty,
343
+ message: "presencePenalty must be between -1 and 1 (inclusive)"
344
+ });
345
+ }
184
346
  }
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;
347
+ if (settings.frequencyPenalty != null) {
348
+ if (typeof settings.frequencyPenalty !== "number") {
349
+ throw new AI_InvalidArgumentError({
350
+ parameter: "frequencyPenalty",
351
+ value: settings.frequencyPenalty,
352
+ message: "frequencyPenalty must be a number"
353
+ });
354
+ }
355
+ if (settings.frequencyPenalty < -1 || settings.frequencyPenalty > 1) {
356
+ throw new AI_InvalidArgumentError({
357
+ parameter: "frequencyPenalty",
358
+ value: settings.frequencyPenalty,
359
+ message: "frequencyPenalty must be between -1 and 1 (inclusive)"
360
+ });
196
361
  }
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
362
  }
210
- }
211
-
212
- // core/schema/parse-json.ts
213
- function safeParseJSON({
214
- text,
215
- schema
216
- }) {
217
- try {
218
- const value = SecureJSON.parse(text);
219
- if (schema == null) {
220
- return {
221
- success: true,
222
- value
223
- };
363
+ if (settings.seed != null) {
364
+ if (!Number.isInteger(settings.seed)) {
365
+ throw new AI_InvalidArgumentError({
366
+ parameter: "seed",
367
+ value: settings.seed,
368
+ message: "seed must be an integer"
369
+ });
224
370
  }
225
- return safeValidateTypes({ value, schema });
226
- } catch (error) {
227
- return {
228
- success: false,
229
- error: error instanceof JSONParseError ? error : new JSONParseError({ text, cause: error })
230
- };
231
371
  }
372
+ return settings;
232
373
  }
233
374
 
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
375
  // core/generate-object/inject-json-schema-into-system.ts
250
376
  var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
251
377
  var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
@@ -265,26 +391,10 @@ function injectJsonSchemaIntoSystem({
265
391
  ].filter((line) => line != null).join("\n");
266
392
  }
267
393
 
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
394
  // core/generate-object/generate-object.ts
285
395
  async function generateObject({
286
396
  model,
287
- schema: zodSchema,
397
+ schema,
288
398
  mode,
289
399
  system,
290
400
  prompt,
@@ -292,15 +402,16 @@ async function generateObject({
292
402
  ...settings
293
403
  }) {
294
404
  var _a, _b;
295
- const schema = new ZodSchema(zodSchema);
296
- const jsonSchema = schema.getJsonSchema();
405
+ const jsonSchema = zodToJsonSchema(schema);
406
+ if (mode === "auto" || mode == null) {
407
+ mode = model.defaultObjectGenerationMode;
408
+ }
297
409
  let result;
298
- mode = mode != null ? mode : model.defaultObjectGenerationMode;
299
410
  switch (mode) {
300
411
  case "json": {
301
412
  const generateResult = await model.doGenerate({
302
413
  mode: { type: "object-json" },
303
- ...settings,
414
+ ...validateCallSettings(settings),
304
415
  inputFormat: getInputFormat({ prompt, messages }),
305
416
  prompt: convertToLanguageModelPrompt({
306
417
  system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
@@ -375,6 +486,9 @@ var GenerateObjectResult = class {
375
486
  }
376
487
  };
377
488
 
489
+ // core/generate-object/stream-object.ts
490
+ import zodToJsonSchema2 from "zod-to-json-schema";
491
+
378
492
  // core/util/is-deep-equal-data.ts
379
493
  function isDeepEqualData(obj1, obj2) {
380
494
  if (obj1 === obj2)
@@ -750,22 +864,23 @@ function parsePartialJson(jsonText) {
750
864
  // core/generate-object/stream-object.ts
751
865
  async function streamObject({
752
866
  model,
753
- schema: zodSchema,
867
+ schema,
754
868
  mode,
755
869
  system,
756
870
  prompt,
757
871
  messages,
758
872
  ...settings
759
873
  }) {
760
- const schema = new ZodSchema(zodSchema);
761
- const jsonSchema = schema.getJsonSchema();
874
+ const jsonSchema = zodToJsonSchema2(schema);
762
875
  let modelStream;
763
- mode = mode != null ? mode : model.defaultObjectGenerationMode;
876
+ if (mode === "auto" || mode == null) {
877
+ mode = model.defaultObjectGenerationMode;
878
+ }
764
879
  switch (mode) {
765
880
  case "json": {
766
881
  const { stream, warnings } = await model.doStream({
767
882
  mode: { type: "object-json" },
768
- ...settings,
883
+ ...validateCallSettings(settings),
769
884
  inputFormat: getInputFormat({ prompt, messages }),
770
885
  prompt: convertToLanguageModelPrompt({
771
886
  system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
@@ -890,7 +1005,7 @@ var StreamObjectResult = class {
890
1005
  };
891
1006
 
892
1007
  // core/generate-text/generate-text.ts
893
- import zodToJsonSchema2 from "zod-to-json-schema";
1008
+ import zodToJsonSchema3 from "zod-to-json-schema";
894
1009
 
895
1010
  // core/generate-text/tool-call.ts
896
1011
  function parseToolCall({
@@ -907,7 +1022,7 @@ function parseToolCall({
907
1022
  }
908
1023
  const parseResult = safeParseJSON({
909
1024
  text: toolCall.args,
910
- schema: new ZodSchema(tool2.parameters)
1025
+ schema: tool2.parameters
911
1026
  });
912
1027
  if (parseResult.success === false) {
913
1028
  throw new Error(
@@ -939,10 +1054,10 @@ async function generateText({
939
1054
  type: "function",
940
1055
  name,
941
1056
  description: tool2.description,
942
- parameters: zodToJsonSchema2(tool2.parameters)
1057
+ parameters: zodToJsonSchema3(tool2.parameters)
943
1058
  }))
944
1059
  },
945
- ...settings,
1060
+ ...validateCallSettings(settings),
946
1061
  inputFormat: getInputFormat({ prompt, messages }),
947
1062
  prompt: convertToLanguageModelPrompt({
948
1063
  system,
@@ -996,7 +1111,7 @@ var GenerateTextResult = class {
996
1111
  };
997
1112
 
998
1113
  // core/generate-text/stream-text.ts
999
- import zodToJsonSchema3 from "zod-to-json-schema";
1114
+ import zodToJsonSchema4 from "zod-to-json-schema";
1000
1115
 
1001
1116
  // core/generate-text/run-tools-transformation.ts
1002
1117
  import { nanoid } from "nanoid";
@@ -1075,6 +1190,7 @@ function runToolsTransformation({
1075
1190
  }
1076
1191
  break;
1077
1192
  }
1193
+ case "finish-metadata":
1078
1194
  case "tool-call-delta": {
1079
1195
  break;
1080
1196
  }
@@ -1364,10 +1480,10 @@ async function streamText({
1364
1480
  type: "function",
1365
1481
  name,
1366
1482
  description: tool2.description,
1367
- parameters: zodToJsonSchema3(tool2.parameters)
1483
+ parameters: zodToJsonSchema4(tool2.parameters)
1368
1484
  }))
1369
1485
  },
1370
- ...settings,
1486
+ ...validateCallSettings(settings),
1371
1487
  inputFormat: getInputFormat({ prompt, messages }),
1372
1488
  prompt: convertToLanguageModelPrompt({
1373
1489
  system,
@@ -1399,6 +1515,9 @@ var StreamTextResult = class {
1399
1515
  return { value: value.textDelta, done: false };
1400
1516
  }
1401
1517
  }
1518
+ if (value.type === "error") {
1519
+ console.error("Error:", value.error);
1520
+ }
1402
1521
  }
1403
1522
  }
1404
1523
  };
@@ -1436,38 +1555,12 @@ var StreamTextResult = class {
1436
1555
  function tool(tool2) {
1437
1556
  return tool2;
1438
1557
  }
1439
-
1440
- // core/language-model/errors/unsupported-functionality-error.ts
1441
- var UnsupportedFunctionalityError = class extends Error {
1442
- constructor({
1443
- provider,
1444
- functionality
1445
- }) {
1446
- super(
1447
- `Functionality not supported by the provider. Provider: ${provider}.
1448
- Functionality: ${functionality}`
1449
- );
1450
- this.name = "UnsupportedFunctionalityError";
1451
- this.provider = provider;
1452
- this.functionality = functionality;
1453
- }
1454
- toJSON() {
1455
- return {
1456
- name: this.name,
1457
- message: this.message,
1458
- stack: this.stack,
1459
- provider: this.provider,
1460
- functionality: this.functionality
1461
- };
1462
- }
1463
- };
1464
1558
  export {
1465
1559
  GenerateObjectResult,
1466
1560
  GenerateTextResult,
1467
1561
  StreamObjectResult,
1468
1562
  StreamTextHttpResponse,
1469
1563
  StreamTextResult,
1470
- UnsupportedFunctionalityError,
1471
1564
  convertDataContentToBase64String,
1472
1565
  convertDataContentToUint8Array,
1473
1566
  generateObject,