ai 3.1.0-canary.1 → 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 +120 -75
  8. package/core/dist/index.d.ts +120 -75
  9. package/core/dist/index.js +261 -173
  10. package/core/dist/index.js.map +1 -1
  11. package/core/dist/index.mjs +261 -172
  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 +13 -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 +795 -26126
  29. package/provider/dist/index.js.map +1 -1
  30. package/provider/dist/index.mjs +763 -7729
  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,17 +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();
297
- let result;
405
+ const jsonSchema = zodToJsonSchema(schema);
298
406
  if (mode === "auto" || mode == null) {
299
407
  mode = model.defaultObjectGenerationMode;
300
408
  }
409
+ let result;
301
410
  switch (mode) {
302
411
  case "json": {
303
412
  const generateResult = await model.doGenerate({
304
413
  mode: { type: "object-json" },
305
- ...settings,
414
+ ...validateCallSettings(settings),
306
415
  inputFormat: getInputFormat({ prompt, messages }),
307
416
  prompt: convertToLanguageModelPrompt({
308
417
  system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
@@ -377,6 +486,9 @@ var GenerateObjectResult = class {
377
486
  }
378
487
  };
379
488
 
489
+ // core/generate-object/stream-object.ts
490
+ import zodToJsonSchema2 from "zod-to-json-schema";
491
+
380
492
  // core/util/is-deep-equal-data.ts
381
493
  function isDeepEqualData(obj1, obj2) {
382
494
  if (obj1 === obj2)
@@ -752,15 +864,14 @@ function parsePartialJson(jsonText) {
752
864
  // core/generate-object/stream-object.ts
753
865
  async function streamObject({
754
866
  model,
755
- schema: zodSchema,
867
+ schema,
756
868
  mode,
757
869
  system,
758
870
  prompt,
759
871
  messages,
760
872
  ...settings
761
873
  }) {
762
- const schema = new ZodSchema(zodSchema);
763
- const jsonSchema = schema.getJsonSchema();
874
+ const jsonSchema = zodToJsonSchema2(schema);
764
875
  let modelStream;
765
876
  if (mode === "auto" || mode == null) {
766
877
  mode = model.defaultObjectGenerationMode;
@@ -769,7 +880,7 @@ async function streamObject({
769
880
  case "json": {
770
881
  const { stream, warnings } = await model.doStream({
771
882
  mode: { type: "object-json" },
772
- ...settings,
883
+ ...validateCallSettings(settings),
773
884
  inputFormat: getInputFormat({ prompt, messages }),
774
885
  prompt: convertToLanguageModelPrompt({
775
886
  system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
@@ -894,7 +1005,7 @@ var StreamObjectResult = class {
894
1005
  };
895
1006
 
896
1007
  // core/generate-text/generate-text.ts
897
- import zodToJsonSchema2 from "zod-to-json-schema";
1008
+ import zodToJsonSchema3 from "zod-to-json-schema";
898
1009
 
899
1010
  // core/generate-text/tool-call.ts
900
1011
  function parseToolCall({
@@ -911,7 +1022,7 @@ function parseToolCall({
911
1022
  }
912
1023
  const parseResult = safeParseJSON({
913
1024
  text: toolCall.args,
914
- schema: new ZodSchema(tool2.parameters)
1025
+ schema: tool2.parameters
915
1026
  });
916
1027
  if (parseResult.success === false) {
917
1028
  throw new Error(
@@ -943,10 +1054,10 @@ async function generateText({
943
1054
  type: "function",
944
1055
  name,
945
1056
  description: tool2.description,
946
- parameters: zodToJsonSchema2(tool2.parameters)
1057
+ parameters: zodToJsonSchema3(tool2.parameters)
947
1058
  }))
948
1059
  },
949
- ...settings,
1060
+ ...validateCallSettings(settings),
950
1061
  inputFormat: getInputFormat({ prompt, messages }),
951
1062
  prompt: convertToLanguageModelPrompt({
952
1063
  system,
@@ -1000,7 +1111,7 @@ var GenerateTextResult = class {
1000
1111
  };
1001
1112
 
1002
1113
  // core/generate-text/stream-text.ts
1003
- import zodToJsonSchema3 from "zod-to-json-schema";
1114
+ import zodToJsonSchema4 from "zod-to-json-schema";
1004
1115
 
1005
1116
  // core/generate-text/run-tools-transformation.ts
1006
1117
  import { nanoid } from "nanoid";
@@ -1079,6 +1190,7 @@ function runToolsTransformation({
1079
1190
  }
1080
1191
  break;
1081
1192
  }
1193
+ case "finish-metadata":
1082
1194
  case "tool-call-delta": {
1083
1195
  break;
1084
1196
  }
@@ -1368,10 +1480,10 @@ async function streamText({
1368
1480
  type: "function",
1369
1481
  name,
1370
1482
  description: tool2.description,
1371
- parameters: zodToJsonSchema3(tool2.parameters)
1483
+ parameters: zodToJsonSchema4(tool2.parameters)
1372
1484
  }))
1373
1485
  },
1374
- ...settings,
1486
+ ...validateCallSettings(settings),
1375
1487
  inputFormat: getInputFormat({ prompt, messages }),
1376
1488
  prompt: convertToLanguageModelPrompt({
1377
1489
  system,
@@ -1403,6 +1515,9 @@ var StreamTextResult = class {
1403
1515
  return { value: value.textDelta, done: false };
1404
1516
  }
1405
1517
  }
1518
+ if (value.type === "error") {
1519
+ console.error("Error:", value.error);
1520
+ }
1406
1521
  }
1407
1522
  }
1408
1523
  };
@@ -1440,38 +1555,12 @@ var StreamTextResult = class {
1440
1555
  function tool(tool2) {
1441
1556
  return tool2;
1442
1557
  }
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
1558
  export {
1469
1559
  GenerateObjectResult,
1470
1560
  GenerateTextResult,
1471
1561
  StreamObjectResult,
1472
1562
  StreamTextHttpResponse,
1473
1563
  StreamTextResult,
1474
- UnsupportedFunctionalityError,
1475
1564
  convertDataContentToBase64String,
1476
1565
  convertDataContentToUint8Array,
1477
1566
  generateObject,