ai 3.1.0-canary.4 → 3.1.1

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 (69) hide show
  1. package/dist/index.d.mts +982 -24
  2. package/dist/index.d.ts +982 -24
  3. package/dist/index.js +1748 -175
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +1723 -174
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +11 -28
  8. package/prompts/dist/index.d.mts +13 -1
  9. package/prompts/dist/index.d.ts +13 -1
  10. package/prompts/dist/index.js +13 -0
  11. package/prompts/dist/index.js.map +1 -1
  12. package/prompts/dist/index.mjs +12 -0
  13. package/prompts/dist/index.mjs.map +1 -1
  14. package/react/dist/index.d.mts +23 -6
  15. package/react/dist/index.d.ts +27 -8
  16. package/react/dist/index.js +154 -141
  17. package/react/dist/index.js.map +1 -1
  18. package/react/dist/index.mjs +153 -141
  19. package/react/dist/index.mjs.map +1 -1
  20. package/react/dist/index.server.d.mts +4 -2
  21. package/react/dist/index.server.d.ts +4 -2
  22. package/react/dist/index.server.js.map +1 -1
  23. package/react/dist/index.server.mjs.map +1 -1
  24. package/rsc/dist/index.d.ts +388 -21
  25. package/rsc/dist/rsc-client.d.mts +1 -1
  26. package/rsc/dist/rsc-client.mjs +2 -0
  27. package/rsc/dist/rsc-client.mjs.map +1 -1
  28. package/rsc/dist/rsc-server.d.mts +370 -21
  29. package/rsc/dist/rsc-server.mjs +677 -36
  30. package/rsc/dist/rsc-server.mjs.map +1 -1
  31. package/rsc/dist/rsc-shared.d.mts +24 -9
  32. package/rsc/dist/rsc-shared.mjs +98 -4
  33. package/rsc/dist/rsc-shared.mjs.map +1 -1
  34. package/solid/dist/index.d.mts +7 -3
  35. package/solid/dist/index.d.ts +7 -3
  36. package/solid/dist/index.js +106 -107
  37. package/solid/dist/index.js.map +1 -1
  38. package/solid/dist/index.mjs +106 -107
  39. package/solid/dist/index.mjs.map +1 -1
  40. package/svelte/dist/index.d.mts +7 -3
  41. package/svelte/dist/index.d.ts +7 -3
  42. package/svelte/dist/index.js +109 -109
  43. package/svelte/dist/index.js.map +1 -1
  44. package/svelte/dist/index.mjs +109 -109
  45. package/svelte/dist/index.mjs.map +1 -1
  46. package/vue/dist/index.d.mts +7 -3
  47. package/vue/dist/index.d.ts +7 -3
  48. package/vue/dist/index.js +106 -107
  49. package/vue/dist/index.js.map +1 -1
  50. package/vue/dist/index.mjs +106 -107
  51. package/vue/dist/index.mjs.map +1 -1
  52. package/ai-model-specification/dist/index.d.mts +0 -665
  53. package/ai-model-specification/dist/index.d.ts +0 -665
  54. package/ai-model-specification/dist/index.js +0 -716
  55. package/ai-model-specification/dist/index.js.map +0 -1
  56. package/ai-model-specification/dist/index.mjs +0 -656
  57. package/ai-model-specification/dist/index.mjs.map +0 -1
  58. package/core/dist/index.d.mts +0 -626
  59. package/core/dist/index.d.ts +0 -626
  60. package/core/dist/index.js +0 -1918
  61. package/core/dist/index.js.map +0 -1
  62. package/core/dist/index.mjs +0 -1873
  63. package/core/dist/index.mjs.map +0 -1
  64. package/openai/dist/index.d.mts +0 -429
  65. package/openai/dist/index.d.ts +0 -429
  66. package/openai/dist/index.js +0 -1231
  67. package/openai/dist/index.js.map +0 -1
  68. package/openai/dist/index.mjs +0 -1195
  69. package/openai/dist/index.mjs.map +0 -1
@@ -1,1918 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // core/index.ts
31
- var core_exports = {};
32
- __export(core_exports, {
33
- GenerateObjectResult: () => GenerateObjectResult,
34
- GenerateTextResult: () => GenerateTextResult,
35
- StreamObjectResult: () => StreamObjectResult,
36
- StreamTextResult: () => StreamTextResult,
37
- convertDataContentToBase64String: () => convertDataContentToBase64String,
38
- convertDataContentToUint8Array: () => convertDataContentToUint8Array,
39
- generateObject: () => generateObject,
40
- generateText: () => generateText,
41
- streamObject: () => streamObject,
42
- streamText: () => streamText,
43
- tool: () => tool
44
- });
45
- module.exports = __toCommonJS(core_exports);
46
-
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/api-call-error.ts
51
- var APICallError = class extends Error {
52
- constructor({
53
- message,
54
- url,
55
- requestBodyValues,
56
- statusCode,
57
- responseBody,
58
- cause,
59
- isRetryable = statusCode != null && (statusCode === 408 || // request timeout
60
- statusCode === 409 || // conflict
61
- statusCode === 429 || // too many requests
62
- statusCode >= 500),
63
- // server error
64
- data
65
- }) {
66
- super(message);
67
- this.name = "AI_APICallError";
68
- this.url = url;
69
- this.requestBodyValues = requestBodyValues;
70
- this.statusCode = statusCode;
71
- this.responseBody = responseBody;
72
- this.cause = cause;
73
- this.isRetryable = isRetryable;
74
- this.data = data;
75
- }
76
- static isAPICallError(error) {
77
- return error instanceof Error && error.name === "AI_APICallError" && typeof error.url === "string" && typeof error.requestBodyValues === "object" && (error.statusCode == null || typeof error.statusCode === "number") && (error.responseBody == null || typeof error.responseBody === "string") && (error.cause == null || typeof error.cause === "object") && typeof error.isRetryable === "boolean" && (error.data == null || typeof error.data === "object");
78
- }
79
- toJSON() {
80
- return {
81
- name: this.name,
82
- message: this.message,
83
- url: this.url,
84
- requestBodyValues: this.requestBodyValues,
85
- statusCode: this.statusCode,
86
- responseBody: this.responseBody,
87
- cause: this.cause,
88
- isRetryable: this.isRetryable,
89
- data: this.data
90
- };
91
- }
92
- };
93
-
94
- // ai-model-specification/errors/invalid-argument-error.ts
95
- var InvalidArgumentError = class extends Error {
96
- constructor({
97
- parameter,
98
- value,
99
- message
100
- }) {
101
- super(`Invalid argument for parameter ${parameter}: ${message}`);
102
- this.name = "AI_InvalidArgumentError";
103
- this.parameter = parameter;
104
- this.value = value;
105
- }
106
- static isInvalidArgumentError(error) {
107
- return error instanceof Error && error.name === "AI_InvalidArgumentError" && typeof error.parameter === "string" && typeof error.value === "string";
108
- }
109
- toJSON() {
110
- return {
111
- name: this.name,
112
- message: this.message,
113
- stack: this.stack,
114
- parameter: this.parameter,
115
- value: this.value
116
- };
117
- }
118
- };
119
-
120
- // ai-model-specification/util/get-error-message.ts
121
- function getErrorMessage(error) {
122
- if (error == null) {
123
- return "unknown error";
124
- }
125
- if (typeof error === "string") {
126
- return error;
127
- }
128
- if (error instanceof Error) {
129
- return error.message;
130
- }
131
- return JSON.stringify(error);
132
- }
133
-
134
- // ai-model-specification/util/parse-json.ts
135
- var import_secure_json_parse = __toESM(require("secure-json-parse"));
136
-
137
- // ai-model-specification/errors/json-parse-error.ts
138
- var JSONParseError = class extends Error {
139
- constructor({ text, cause }) {
140
- super(
141
- `JSON parsing failed: Text: ${text}.
142
- Error message: ${getErrorMessage(cause)}`
143
- );
144
- this.name = "AI_JSONParseError";
145
- this.cause = cause;
146
- this.text = text;
147
- }
148
- static isJSONParseError(error) {
149
- return error instanceof Error && error.name === "AI_JSONParseError" && typeof error.text === "string" && typeof error.cause === "string";
150
- }
151
- toJSON() {
152
- return {
153
- name: this.name,
154
- message: this.message,
155
- cause: this.cause,
156
- stack: this.stack,
157
- valueText: this.text
158
- };
159
- }
160
- };
161
-
162
- // ai-model-specification/errors/type-validation-error.ts
163
- var TypeValidationError = class extends Error {
164
- constructor({ value, cause }) {
165
- super(
166
- `Type validation failed: Value: ${JSON.stringify(value)}.
167
- Error message: ${getErrorMessage(cause)}`
168
- );
169
- this.name = "AI_TypeValidationError";
170
- this.cause = cause;
171
- this.value = value;
172
- }
173
- static isTypeValidationError(error) {
174
- return error instanceof Error && error.name === "AI_TypeValidationError" && typeof error.value === "string" && typeof error.cause === "string";
175
- }
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
- };
184
- }
185
- };
186
-
187
- // ai-model-specification/util/validate-types.ts
188
- function safeValidateTypes({
189
- value,
190
- schema
191
- }) {
192
- try {
193
- const validationResult = schema.safeParse(value);
194
- if (validationResult.success) {
195
- return {
196
- success: true,
197
- value: validationResult.data
198
- };
199
- }
200
- return {
201
- success: false,
202
- error: new TypeValidationError({
203
- value,
204
- cause: validationResult.error
205
- })
206
- };
207
- } catch (error) {
208
- return {
209
- success: false,
210
- error: TypeValidationError.isTypeValidationError(error) ? error : new TypeValidationError({ value, cause: error })
211
- };
212
- }
213
- }
214
-
215
- // ai-model-specification/util/parse-json.ts
216
- function safeParseJSON({
217
- text,
218
- schema
219
- }) {
220
- try {
221
- const value = import_secure_json_parse.default.parse(text);
222
- if (schema == null) {
223
- return {
224
- success: true,
225
- value
226
- };
227
- }
228
- return safeValidateTypes({ value, schema });
229
- } catch (error) {
230
- return {
231
- success: false,
232
- error: JSONParseError.isJSONParseError(error) ? error : new JSONParseError({ text, cause: error })
233
- };
234
- }
235
- }
236
-
237
- // ai-model-specification/util/uint8-utils.ts
238
- function convertBase64ToUint8Array(base64String) {
239
- const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
240
- const latin1string = globalThis.atob(base64Url);
241
- return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
242
- }
243
- function convertUint8ArrayToBase64(array) {
244
- let latin1string = "";
245
- for (const value of array) {
246
- latin1string += String.fromCodePoint(value);
247
- }
248
- return globalThis.btoa(latin1string);
249
- }
250
-
251
- // ai-model-specification/errors/invalid-tool-arguments-error.ts
252
- var InvalidToolArgumentsError = class extends Error {
253
- constructor({
254
- toolArgs,
255
- toolName,
256
- cause,
257
- message = `Invalid arguments for tool ${toolName}: ${getErrorMessage(
258
- cause
259
- )}`
260
- }) {
261
- super(message);
262
- this.name = "AI_InvalidToolArgumentsError";
263
- this.toolArgs = toolArgs;
264
- this.toolName = toolName;
265
- this.cause = cause;
266
- }
267
- static isInvalidToolArgumentsError(error) {
268
- return error instanceof Error && error.name === "AI_InvalidToolArgumentsError" && typeof error.toolName === "string" && typeof error.toolArgs === "string";
269
- }
270
- toJSON() {
271
- return {
272
- name: this.name,
273
- message: this.message,
274
- cause: this.cause,
275
- stack: this.stack,
276
- toolName: this.toolName,
277
- toolArgs: this.toolArgs
278
- };
279
- }
280
- };
281
-
282
- // ai-model-specification/errors/no-object-generated-error.ts
283
- var NoTextGeneratedError = class extends Error {
284
- constructor() {
285
- super(`No text generated.`);
286
- this.name = "AI_NoTextGeneratedError";
287
- }
288
- static isNoTextGeneratedError(error) {
289
- return error instanceof Error && error.name === "AI_NoTextGeneratedError";
290
- }
291
- toJSON() {
292
- return {
293
- name: this.name,
294
- cause: this.cause,
295
- message: this.message,
296
- stack: this.stack
297
- };
298
- }
299
- };
300
-
301
- // ai-model-specification/errors/no-such-tool-error.ts
302
- var NoSuchToolError = class extends Error {
303
- constructor({ message, toolName }) {
304
- super(message);
305
- this.name = "AI_NoSuchToolError";
306
- this.toolName = toolName;
307
- }
308
- static isNoSuchToolError(error) {
309
- return error instanceof Error && error.name === "AI_NoSuchToolError" && typeof error.toolName === "string";
310
- }
311
- toJSON() {
312
- return {
313
- name: this.name,
314
- message: this.message,
315
- stack: this.stack,
316
- toolName: this.toolName
317
- };
318
- }
319
- };
320
-
321
- // ai-model-specification/errors/retry-error.ts
322
- var RetryError = class extends Error {
323
- constructor({
324
- message,
325
- reason,
326
- errors
327
- }) {
328
- super(message);
329
- this.name = "AI_RetryError";
330
- this.reason = reason;
331
- this.errors = errors;
332
- this.lastError = errors[errors.length - 1];
333
- }
334
- static isRetryError(error) {
335
- return error instanceof Error && error.name === "AI_RetryError" && typeof error.reason === "string" && Array.isArray(error.errors);
336
- }
337
- toJSON() {
338
- return {
339
- name: this.name,
340
- message: this.message,
341
- reason: this.reason,
342
- lastError: this.lastError,
343
- errors: this.errors
344
- };
345
- }
346
- };
347
-
348
- // core/generate-text/token-usage.ts
349
- function calculateTokenUsage(usage) {
350
- return {
351
- promptTokens: usage.promptTokens,
352
- completionTokens: usage.completionTokens,
353
- totalTokens: usage.promptTokens + usage.completionTokens
354
- };
355
- }
356
-
357
- // core/prompt/data-content.ts
358
- function convertDataContentToBase64String(content) {
359
- if (typeof content === "string") {
360
- return content;
361
- }
362
- if (content instanceof ArrayBuffer) {
363
- return convertUint8ArrayToBase64(new Uint8Array(content));
364
- }
365
- return convertUint8ArrayToBase64(content);
366
- }
367
- function convertDataContentToUint8Array(content) {
368
- if (content instanceof Uint8Array) {
369
- return content;
370
- }
371
- if (typeof content === "string") {
372
- return convertBase64ToUint8Array(content);
373
- }
374
- if (content instanceof ArrayBuffer) {
375
- return new Uint8Array(content);
376
- }
377
- throw new Error(
378
- `Invalid data content. Expected a string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.`
379
- );
380
- }
381
-
382
- // core/prompt/convert-to-language-model-prompt.ts
383
- function convertToLanguageModelPrompt({
384
- system,
385
- prompt,
386
- messages
387
- }) {
388
- if (prompt == null && messages == null) {
389
- throw new Error("prompt or messages must be defined");
390
- }
391
- if (prompt != null && messages != null) {
392
- throw new Error("prompt and messages cannot be defined at the same time");
393
- }
394
- const languageModelMessages = [];
395
- if (system != null) {
396
- languageModelMessages.push({ role: "system", content: system });
397
- }
398
- if (typeof prompt === "string") {
399
- languageModelMessages.push({
400
- role: "user",
401
- content: [{ type: "text", text: prompt }]
402
- });
403
- } else {
404
- messages = messages;
405
- languageModelMessages.push(
406
- ...messages.map((message) => {
407
- switch (message.role) {
408
- case "user": {
409
- if (typeof message.content === "string") {
410
- return {
411
- role: "user",
412
- content: [{ type: "text", text: message.content }]
413
- };
414
- }
415
- return {
416
- role: "user",
417
- content: message.content.map(
418
- (part) => {
419
- switch (part.type) {
420
- case "text": {
421
- return part;
422
- }
423
- case "image": {
424
- return {
425
- type: "image",
426
- image: part.image instanceof URL ? part.image : convertDataContentToUint8Array(part.image),
427
- mimeType: part.mimeType
428
- };
429
- }
430
- }
431
- }
432
- )
433
- };
434
- }
435
- case "assistant": {
436
- if (typeof message.content === "string") {
437
- return {
438
- role: "assistant",
439
- content: [{ type: "text", text: message.content }]
440
- };
441
- }
442
- return { role: "assistant", content: message.content };
443
- }
444
- case "tool": {
445
- return message;
446
- }
447
- }
448
- })
449
- );
450
- }
451
- return languageModelMessages;
452
- }
453
-
454
- // core/prompt/get-input-format.ts
455
- function getInputFormat({
456
- prompt,
457
- messages
458
- }) {
459
- if (prompt == null && messages == null) {
460
- throw new Error("prompt or messages must be defined");
461
- }
462
- if (prompt != null && messages != null) {
463
- throw new Error("prompt and messages cannot be defined at the same time");
464
- }
465
- return prompt != null ? "prompt" : "messages";
466
- }
467
-
468
- // core/prompt/prepare-call-settings.ts
469
- function prepareCallSettings({
470
- maxTokens,
471
- temperature,
472
- topP,
473
- presencePenalty,
474
- frequencyPenalty,
475
- seed,
476
- maxRetries
477
- }) {
478
- if (maxTokens != null) {
479
- if (!Number.isInteger(maxTokens)) {
480
- throw new InvalidArgumentError({
481
- parameter: "maxTokens",
482
- value: maxTokens,
483
- message: "maxTokens must be an integer"
484
- });
485
- }
486
- if (maxTokens < 1) {
487
- throw new InvalidArgumentError({
488
- parameter: "maxTokens",
489
- value: maxTokens,
490
- message: "maxTokens must be >= 1"
491
- });
492
- }
493
- }
494
- if (temperature != null) {
495
- if (typeof temperature !== "number") {
496
- throw new InvalidArgumentError({
497
- parameter: "temperature",
498
- value: temperature,
499
- message: "temperature must be a number"
500
- });
501
- }
502
- if (temperature < 0 || temperature > 1) {
503
- throw new InvalidArgumentError({
504
- parameter: "temperature",
505
- value: temperature,
506
- message: "temperature must be between 0 and 1 (inclusive)"
507
- });
508
- }
509
- }
510
- if (topP != null) {
511
- if (typeof topP !== "number") {
512
- throw new InvalidArgumentError({
513
- parameter: "topP",
514
- value: topP,
515
- message: "topP must be a number"
516
- });
517
- }
518
- if (topP < 0 || topP > 1) {
519
- throw new InvalidArgumentError({
520
- parameter: "topP",
521
- value: topP,
522
- message: "topP must be between 0 and 1 (inclusive)"
523
- });
524
- }
525
- }
526
- if (presencePenalty != null) {
527
- if (typeof presencePenalty !== "number") {
528
- throw new InvalidArgumentError({
529
- parameter: "presencePenalty",
530
- value: presencePenalty,
531
- message: "presencePenalty must be a number"
532
- });
533
- }
534
- if (presencePenalty < -1 || presencePenalty > 1) {
535
- throw new InvalidArgumentError({
536
- parameter: "presencePenalty",
537
- value: presencePenalty,
538
- message: "presencePenalty must be between -1 and 1 (inclusive)"
539
- });
540
- }
541
- }
542
- if (frequencyPenalty != null) {
543
- if (typeof frequencyPenalty !== "number") {
544
- throw new InvalidArgumentError({
545
- parameter: "frequencyPenalty",
546
- value: frequencyPenalty,
547
- message: "frequencyPenalty must be a number"
548
- });
549
- }
550
- if (frequencyPenalty < -1 || frequencyPenalty > 1) {
551
- throw new InvalidArgumentError({
552
- parameter: "frequencyPenalty",
553
- value: frequencyPenalty,
554
- message: "frequencyPenalty must be between -1 and 1 (inclusive)"
555
- });
556
- }
557
- }
558
- if (seed != null) {
559
- if (!Number.isInteger(seed)) {
560
- throw new InvalidArgumentError({
561
- parameter: "seed",
562
- value: seed,
563
- message: "seed must be an integer"
564
- });
565
- }
566
- }
567
- if (maxRetries != null) {
568
- if (!Number.isInteger(maxRetries)) {
569
- throw new InvalidArgumentError({
570
- parameter: "maxRetries",
571
- value: maxRetries,
572
- message: "maxRetries must be an integer"
573
- });
574
- }
575
- if (maxRetries < 0) {
576
- throw new InvalidArgumentError({
577
- parameter: "maxRetries",
578
- value: maxRetries,
579
- message: "maxRetries must be >= 0"
580
- });
581
- }
582
- }
583
- return {
584
- maxTokens,
585
- temperature: temperature != null ? temperature : 0,
586
- topP,
587
- presencePenalty: presencePenalty != null ? presencePenalty : 0,
588
- frequencyPenalty: frequencyPenalty != null ? frequencyPenalty : 0,
589
- seed,
590
- maxRetries: maxRetries != null ? maxRetries : 2
591
- };
592
- }
593
-
594
- // core/util/delay.ts
595
- async function delay(delayInMs) {
596
- return new Promise((resolve) => setTimeout(resolve, delayInMs));
597
- }
598
-
599
- // core/util/retry-with-exponential-backoff.ts
600
- var retryWithExponentialBackoff = ({
601
- maxRetries = 2,
602
- initialDelayInMs = 2e3,
603
- backoffFactor = 2
604
- } = {}) => async (f) => _retryWithExponentialBackoff(f, {
605
- maxRetries,
606
- delayInMs: initialDelayInMs,
607
- backoffFactor
608
- });
609
- async function _retryWithExponentialBackoff(f, {
610
- maxRetries,
611
- delayInMs,
612
- backoffFactor
613
- }, errors = []) {
614
- try {
615
- return await f();
616
- } catch (error) {
617
- if (maxRetries === 0) {
618
- throw error;
619
- }
620
- const errorMessage = getErrorMessage(error);
621
- const newErrors = [...errors, error];
622
- const tryNumber = newErrors.length;
623
- if (tryNumber > maxRetries) {
624
- throw new RetryError({
625
- message: `Failed after ${tryNumber} attemps. Last error: ${errorMessage}`,
626
- reason: "maxRetriesExceeded",
627
- errors: newErrors
628
- });
629
- }
630
- if (error instanceof Error) {
631
- if (error.name === "AbortError") {
632
- throw error;
633
- }
634
- if (
635
- // deal with bundling duplication by using names
636
- APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries
637
- ) {
638
- await delay(delayInMs);
639
- return _retryWithExponentialBackoff(
640
- f,
641
- { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
642
- newErrors
643
- );
644
- }
645
- }
646
- if (tryNumber === 1) {
647
- throw error;
648
- }
649
- throw new RetryError({
650
- message: `Failed after ${tryNumber} attemps with non-retryable error: '${errorMessage}'`,
651
- reason: "errorNotRetryable",
652
- errors: newErrors
653
- });
654
- }
655
- }
656
-
657
- // core/generate-object/inject-json-schema-into-system.ts
658
- var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
659
- var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
660
- function injectJsonSchemaIntoSystem({
661
- system,
662
- schema,
663
- schemaPrefix = DEFAULT_SCHEMA_PREFIX,
664
- schemaSuffix = DEFAULT_SCHEMA_SUFFIX
665
- }) {
666
- return [
667
- system,
668
- system != null ? "" : null,
669
- // add a newline if system is not null
670
- schemaPrefix,
671
- JSON.stringify(schema),
672
- schemaSuffix
673
- ].filter((line) => line != null).join("\n");
674
- }
675
-
676
- // core/generate-object/generate-object.ts
677
- async function generateObject({
678
- model,
679
- schema,
680
- mode,
681
- system,
682
- prompt,
683
- messages,
684
- maxRetries,
685
- abortSignal,
686
- ...settings
687
- }) {
688
- var _a, _b;
689
- const retry = retryWithExponentialBackoff({ maxRetries });
690
- const jsonSchema = (0, import_zod_to_json_schema.default)(schema);
691
- if (mode === "auto" || mode == null) {
692
- mode = model.defaultObjectGenerationMode;
693
- }
694
- let result;
695
- let finishReason;
696
- let usage;
697
- switch (mode) {
698
- case "json": {
699
- const generateResult = await retry(
700
- () => model.doGenerate({
701
- mode: { type: "object-json" },
702
- ...prepareCallSettings(settings),
703
- inputFormat: getInputFormat({ prompt, messages }),
704
- prompt: convertToLanguageModelPrompt({
705
- system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
706
- prompt,
707
- messages
708
- }),
709
- abortSignal
710
- })
711
- );
712
- if (generateResult.text === void 0) {
713
- throw new NoTextGeneratedError();
714
- }
715
- result = generateResult.text;
716
- finishReason = generateResult.finishReason;
717
- usage = generateResult.usage;
718
- break;
719
- }
720
- case "grammar": {
721
- const generateResult = await retry(
722
- () => model.doGenerate({
723
- mode: { type: "object-grammar", schema: jsonSchema },
724
- ...settings,
725
- inputFormat: getInputFormat({ prompt, messages }),
726
- prompt: convertToLanguageModelPrompt({
727
- system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
728
- prompt,
729
- messages
730
- }),
731
- abortSignal
732
- })
733
- );
734
- if (generateResult.text === void 0) {
735
- throw new NoTextGeneratedError();
736
- }
737
- result = generateResult.text;
738
- finishReason = generateResult.finishReason;
739
- usage = generateResult.usage;
740
- break;
741
- }
742
- case "tool": {
743
- const generateResult = await retry(
744
- () => model.doGenerate({
745
- mode: {
746
- type: "object-tool",
747
- tool: {
748
- type: "function",
749
- name: "json",
750
- description: "Respond with a JSON object.",
751
- parameters: jsonSchema
752
- }
753
- },
754
- ...settings,
755
- inputFormat: getInputFormat({ prompt, messages }),
756
- prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
757
- abortSignal
758
- })
759
- );
760
- const functionArgs = (_b = (_a = generateResult.toolCalls) == null ? void 0 : _a[0]) == null ? void 0 : _b.args;
761
- if (functionArgs === void 0) {
762
- throw new NoTextGeneratedError();
763
- }
764
- result = functionArgs;
765
- finishReason = generateResult.finishReason;
766
- usage = generateResult.usage;
767
- break;
768
- }
769
- case void 0: {
770
- throw new Error("Model does not have a default object generation mode.");
771
- }
772
- default: {
773
- const _exhaustiveCheck = mode;
774
- throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
775
- }
776
- }
777
- const parseResult = safeParseJSON({ text: result, schema });
778
- if (!parseResult.success) {
779
- throw parseResult.error;
780
- }
781
- return new GenerateObjectResult({
782
- object: parseResult.value,
783
- finishReason,
784
- usage: calculateTokenUsage(usage)
785
- });
786
- }
787
- var GenerateObjectResult = class {
788
- constructor(options) {
789
- this.object = options.object;
790
- this.finishReason = options.finishReason;
791
- this.usage = options.usage;
792
- }
793
- };
794
-
795
- // core/generate-object/stream-object.ts
796
- var import_zod_to_json_schema2 = __toESM(require("zod-to-json-schema"));
797
-
798
- // core/util/async-iterable-stream.ts
799
- function createAsyncIterableStream(source, transformer) {
800
- const transformedStream = source.pipeThrough(
801
- new TransformStream(transformer)
802
- );
803
- transformedStream[Symbol.asyncIterator] = () => {
804
- const reader = transformedStream.getReader();
805
- return {
806
- async next() {
807
- const { done, value } = await reader.read();
808
- return done ? { done: true, value: void 0 } : { done: false, value };
809
- }
810
- };
811
- };
812
- return transformedStream;
813
- }
814
-
815
- // core/util/is-deep-equal-data.ts
816
- function isDeepEqualData(obj1, obj2) {
817
- if (obj1 === obj2)
818
- return true;
819
- if (obj1 == null || obj2 == null)
820
- return false;
821
- if (typeof obj1 !== "object" && typeof obj2 !== "object")
822
- return obj1 === obj2;
823
- if (obj1.constructor !== obj2.constructor)
824
- return false;
825
- if (obj1 instanceof Date && obj2 instanceof Date) {
826
- return obj1.getTime() === obj2.getTime();
827
- }
828
- if (Array.isArray(obj1)) {
829
- if (obj1.length !== obj2.length)
830
- return false;
831
- for (let i = 0; i < obj1.length; i++) {
832
- if (!isDeepEqualData(obj1[i], obj2[i]))
833
- return false;
834
- }
835
- return true;
836
- }
837
- const keys1 = Object.keys(obj1);
838
- const keys2 = Object.keys(obj2);
839
- if (keys1.length !== keys2.length)
840
- return false;
841
- for (const key of keys1) {
842
- if (!keys2.includes(key))
843
- return false;
844
- if (!isDeepEqualData(obj1[key], obj2[key]))
845
- return false;
846
- }
847
- return true;
848
- }
849
-
850
- // core/util/parse-partial-json.ts
851
- var import_secure_json_parse2 = __toESM(require("secure-json-parse"));
852
-
853
- // core/util/fix-json.ts
854
- function fixJson(input) {
855
- const stack = ["ROOT"];
856
- let lastValidIndex = -1;
857
- let literalStart = null;
858
- function processValueStart(char, i, swapState) {
859
- {
860
- switch (char) {
861
- case '"': {
862
- lastValidIndex = i;
863
- stack.pop();
864
- stack.push(swapState);
865
- stack.push("INSIDE_STRING");
866
- break;
867
- }
868
- case "f":
869
- case "t":
870
- case "n": {
871
- lastValidIndex = i;
872
- literalStart = i;
873
- stack.pop();
874
- stack.push(swapState);
875
- stack.push("INSIDE_LITERAL");
876
- break;
877
- }
878
- case "-": {
879
- stack.pop();
880
- stack.push(swapState);
881
- stack.push("INSIDE_NUMBER");
882
- break;
883
- }
884
- case "0":
885
- case "1":
886
- case "2":
887
- case "3":
888
- case "4":
889
- case "5":
890
- case "6":
891
- case "7":
892
- case "8":
893
- case "9": {
894
- lastValidIndex = i;
895
- stack.pop();
896
- stack.push(swapState);
897
- stack.push("INSIDE_NUMBER");
898
- break;
899
- }
900
- case "{": {
901
- lastValidIndex = i;
902
- stack.pop();
903
- stack.push(swapState);
904
- stack.push("INSIDE_OBJECT_START");
905
- break;
906
- }
907
- case "[": {
908
- lastValidIndex = i;
909
- stack.pop();
910
- stack.push(swapState);
911
- stack.push("INSIDE_ARRAY_START");
912
- break;
913
- }
914
- }
915
- }
916
- }
917
- function processAfterObjectValue(char, i) {
918
- switch (char) {
919
- case ",": {
920
- stack.pop();
921
- stack.push("INSIDE_OBJECT_AFTER_COMMA");
922
- break;
923
- }
924
- case "}": {
925
- lastValidIndex = i;
926
- stack.pop();
927
- break;
928
- }
929
- }
930
- }
931
- function processAfterArrayValue(char, i) {
932
- switch (char) {
933
- case ",": {
934
- stack.pop();
935
- stack.push("INSIDE_ARRAY_AFTER_COMMA");
936
- break;
937
- }
938
- case "]": {
939
- lastValidIndex = i;
940
- stack.pop();
941
- break;
942
- }
943
- }
944
- }
945
- for (let i = 0; i < input.length; i++) {
946
- const char = input[i];
947
- const currentState = stack[stack.length - 1];
948
- switch (currentState) {
949
- case "ROOT":
950
- processValueStart(char, i, "FINISH");
951
- break;
952
- case "INSIDE_OBJECT_START": {
953
- switch (char) {
954
- case '"': {
955
- stack.pop();
956
- stack.push("INSIDE_OBJECT_KEY");
957
- break;
958
- }
959
- case "}": {
960
- stack.pop();
961
- break;
962
- }
963
- }
964
- break;
965
- }
966
- case "INSIDE_OBJECT_AFTER_COMMA": {
967
- switch (char) {
968
- case '"': {
969
- stack.pop();
970
- stack.push("INSIDE_OBJECT_KEY");
971
- break;
972
- }
973
- }
974
- break;
975
- }
976
- case "INSIDE_OBJECT_KEY": {
977
- switch (char) {
978
- case '"': {
979
- stack.pop();
980
- stack.push("INSIDE_OBJECT_AFTER_KEY");
981
- break;
982
- }
983
- }
984
- break;
985
- }
986
- case "INSIDE_OBJECT_AFTER_KEY": {
987
- switch (char) {
988
- case ":": {
989
- stack.pop();
990
- stack.push("INSIDE_OBJECT_BEFORE_VALUE");
991
- break;
992
- }
993
- }
994
- break;
995
- }
996
- case "INSIDE_OBJECT_BEFORE_VALUE": {
997
- processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
998
- break;
999
- }
1000
- case "INSIDE_OBJECT_AFTER_VALUE": {
1001
- processAfterObjectValue(char, i);
1002
- break;
1003
- }
1004
- case "INSIDE_STRING": {
1005
- switch (char) {
1006
- case '"': {
1007
- stack.pop();
1008
- lastValidIndex = i;
1009
- break;
1010
- }
1011
- case "\\": {
1012
- stack.push("INSIDE_STRING_ESCAPE");
1013
- break;
1014
- }
1015
- default: {
1016
- lastValidIndex = i;
1017
- }
1018
- }
1019
- break;
1020
- }
1021
- case "INSIDE_ARRAY_START": {
1022
- switch (char) {
1023
- case "]": {
1024
- lastValidIndex = i;
1025
- stack.pop();
1026
- break;
1027
- }
1028
- default: {
1029
- lastValidIndex = i;
1030
- processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
1031
- break;
1032
- }
1033
- }
1034
- break;
1035
- }
1036
- case "INSIDE_ARRAY_AFTER_VALUE": {
1037
- switch (char) {
1038
- case ",": {
1039
- stack.pop();
1040
- stack.push("INSIDE_ARRAY_AFTER_COMMA");
1041
- break;
1042
- }
1043
- case "]": {
1044
- lastValidIndex = i;
1045
- stack.pop();
1046
- break;
1047
- }
1048
- default: {
1049
- lastValidIndex = i;
1050
- break;
1051
- }
1052
- }
1053
- break;
1054
- }
1055
- case "INSIDE_ARRAY_AFTER_COMMA": {
1056
- processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
1057
- break;
1058
- }
1059
- case "INSIDE_STRING_ESCAPE": {
1060
- stack.pop();
1061
- lastValidIndex = i;
1062
- break;
1063
- }
1064
- case "INSIDE_NUMBER": {
1065
- switch (char) {
1066
- case "0":
1067
- case "1":
1068
- case "2":
1069
- case "3":
1070
- case "4":
1071
- case "5":
1072
- case "6":
1073
- case "7":
1074
- case "8":
1075
- case "9": {
1076
- lastValidIndex = i;
1077
- break;
1078
- }
1079
- case "e":
1080
- case "E":
1081
- case "-":
1082
- case ".": {
1083
- break;
1084
- }
1085
- case ",": {
1086
- stack.pop();
1087
- if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
1088
- processAfterArrayValue(char, i);
1089
- }
1090
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
1091
- processAfterObjectValue(char, i);
1092
- }
1093
- break;
1094
- }
1095
- case "}": {
1096
- stack.pop();
1097
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
1098
- processAfterObjectValue(char, i);
1099
- }
1100
- break;
1101
- }
1102
- case "]": {
1103
- stack.pop();
1104
- if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
1105
- processAfterArrayValue(char, i);
1106
- }
1107
- break;
1108
- }
1109
- default: {
1110
- stack.pop();
1111
- break;
1112
- }
1113
- }
1114
- break;
1115
- }
1116
- case "INSIDE_LITERAL": {
1117
- const partialLiteral = input.substring(literalStart, i + 1);
1118
- if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
1119
- stack.pop();
1120
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
1121
- processAfterObjectValue(char, i);
1122
- } else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
1123
- processAfterArrayValue(char, i);
1124
- }
1125
- } else {
1126
- lastValidIndex = i;
1127
- }
1128
- break;
1129
- }
1130
- }
1131
- }
1132
- let result = input.slice(0, lastValidIndex + 1);
1133
- for (let i = stack.length - 1; i >= 0; i--) {
1134
- const state = stack[i];
1135
- switch (state) {
1136
- case "INSIDE_STRING": {
1137
- result += '"';
1138
- break;
1139
- }
1140
- case "INSIDE_OBJECT_KEY":
1141
- case "INSIDE_OBJECT_AFTER_KEY":
1142
- case "INSIDE_OBJECT_AFTER_COMMA":
1143
- case "INSIDE_OBJECT_START":
1144
- case "INSIDE_OBJECT_BEFORE_VALUE":
1145
- case "INSIDE_OBJECT_AFTER_VALUE": {
1146
- result += "}";
1147
- break;
1148
- }
1149
- case "INSIDE_ARRAY_START":
1150
- case "INSIDE_ARRAY_AFTER_COMMA":
1151
- case "INSIDE_ARRAY_AFTER_VALUE": {
1152
- result += "]";
1153
- break;
1154
- }
1155
- case "INSIDE_LITERAL": {
1156
- const partialLiteral = input.substring(literalStart, input.length);
1157
- if ("true".startsWith(partialLiteral)) {
1158
- result += "true".slice(partialLiteral.length);
1159
- } else if ("false".startsWith(partialLiteral)) {
1160
- result += "false".slice(partialLiteral.length);
1161
- } else if ("null".startsWith(partialLiteral)) {
1162
- result += "null".slice(partialLiteral.length);
1163
- }
1164
- }
1165
- }
1166
- }
1167
- return result;
1168
- }
1169
-
1170
- // core/util/parse-partial-json.ts
1171
- function parsePartialJson(jsonText) {
1172
- if (jsonText == null) {
1173
- return void 0;
1174
- }
1175
- try {
1176
- return import_secure_json_parse2.default.parse(jsonText);
1177
- } catch (ignored) {
1178
- try {
1179
- const fixedJsonText = fixJson(jsonText);
1180
- return import_secure_json_parse2.default.parse(fixedJsonText);
1181
- } catch (ignored2) {
1182
- }
1183
- }
1184
- return void 0;
1185
- }
1186
-
1187
- // core/generate-object/stream-object.ts
1188
- async function streamObject({
1189
- model,
1190
- schema,
1191
- mode,
1192
- system,
1193
- prompt,
1194
- messages,
1195
- maxRetries,
1196
- abortSignal,
1197
- ...settings
1198
- }) {
1199
- const retry = retryWithExponentialBackoff({ maxRetries });
1200
- const jsonSchema = (0, import_zod_to_json_schema2.default)(schema);
1201
- if (mode === "auto" || mode == null) {
1202
- mode = model.defaultObjectGenerationMode;
1203
- }
1204
- let callOptions;
1205
- let transformer;
1206
- switch (mode) {
1207
- case "json": {
1208
- callOptions = {
1209
- mode: { type: "object-json" },
1210
- ...prepareCallSettings(settings),
1211
- inputFormat: getInputFormat({ prompt, messages }),
1212
- prompt: convertToLanguageModelPrompt({
1213
- system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
1214
- prompt,
1215
- messages
1216
- }),
1217
- abortSignal
1218
- };
1219
- transformer = {
1220
- transform: (chunk, controller) => {
1221
- switch (chunk.type) {
1222
- case "text-delta":
1223
- controller.enqueue(chunk.textDelta);
1224
- break;
1225
- case "error":
1226
- controller.enqueue(chunk);
1227
- break;
1228
- }
1229
- }
1230
- };
1231
- break;
1232
- }
1233
- case "grammar": {
1234
- callOptions = {
1235
- mode: { type: "object-grammar", schema: jsonSchema },
1236
- ...settings,
1237
- inputFormat: getInputFormat({ prompt, messages }),
1238
- prompt: convertToLanguageModelPrompt({
1239
- system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
1240
- prompt,
1241
- messages
1242
- }),
1243
- abortSignal
1244
- };
1245
- transformer = {
1246
- transform: (chunk, controller) => {
1247
- switch (chunk.type) {
1248
- case "text-delta":
1249
- controller.enqueue(chunk.textDelta);
1250
- break;
1251
- case "error":
1252
- controller.enqueue(chunk);
1253
- break;
1254
- }
1255
- }
1256
- };
1257
- break;
1258
- }
1259
- case "tool": {
1260
- callOptions = {
1261
- mode: {
1262
- type: "object-tool",
1263
- tool: {
1264
- type: "function",
1265
- name: "json",
1266
- description: "Respond with a JSON object.",
1267
- parameters: jsonSchema
1268
- }
1269
- },
1270
- ...settings,
1271
- inputFormat: getInputFormat({ prompt, messages }),
1272
- prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
1273
- abortSignal
1274
- };
1275
- transformer = {
1276
- transform(chunk, controller) {
1277
- switch (chunk.type) {
1278
- case "tool-call-delta":
1279
- controller.enqueue(chunk.argsTextDelta);
1280
- break;
1281
- case "error":
1282
- controller.enqueue(chunk);
1283
- break;
1284
- }
1285
- }
1286
- };
1287
- break;
1288
- }
1289
- case void 0: {
1290
- throw new Error("Model does not have a default object generation mode.");
1291
- }
1292
- default: {
1293
- const _exhaustiveCheck = mode;
1294
- throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
1295
- }
1296
- }
1297
- const { stream, warnings } = await retry(() => model.doStream(callOptions));
1298
- return new StreamObjectResult(
1299
- stream.pipeThrough(new TransformStream(transformer))
1300
- );
1301
- }
1302
- var StreamObjectResult = class {
1303
- constructor(stream) {
1304
- this.originalStream = stream;
1305
- }
1306
- get objectStream() {
1307
- let accumulatedText = "";
1308
- let latestObject = void 0;
1309
- return createAsyncIterableStream(this.originalStream, {
1310
- transform(chunk, controller) {
1311
- if (typeof chunk === "string") {
1312
- accumulatedText += chunk;
1313
- const currentObject = parsePartialJson(
1314
- accumulatedText
1315
- );
1316
- if (!isDeepEqualData(latestObject, currentObject)) {
1317
- latestObject = currentObject;
1318
- controller.enqueue(currentObject);
1319
- }
1320
- }
1321
- if (typeof chunk === "object" && chunk.type === "error") {
1322
- throw chunk.error;
1323
- }
1324
- }
1325
- });
1326
- }
1327
- };
1328
-
1329
- // core/generate-text/generate-text.ts
1330
- var import_zod_to_json_schema3 = __toESM(require("zod-to-json-schema"));
1331
-
1332
- // core/generate-text/tool-call.ts
1333
- function parseToolCall({
1334
- toolCall,
1335
- tools
1336
- }) {
1337
- const toolName = toolCall.toolName;
1338
- if (tools == null) {
1339
- throw new NoSuchToolError({
1340
- message: `Tool ${toolCall.toolName} not found (no tools provided).`,
1341
- toolName: toolCall.toolName
1342
- });
1343
- }
1344
- const tool2 = tools[toolName];
1345
- if (tool2 == null) {
1346
- throw new NoSuchToolError({
1347
- message: `Tool ${toolCall.toolName} not found.`,
1348
- toolName: toolCall.toolName
1349
- });
1350
- }
1351
- const parseResult = safeParseJSON({
1352
- text: toolCall.args,
1353
- schema: tool2.parameters
1354
- });
1355
- if (parseResult.success === false) {
1356
- throw new InvalidToolArgumentsError({
1357
- toolName,
1358
- toolArgs: toolCall.args,
1359
- cause: parseResult.error
1360
- });
1361
- }
1362
- return {
1363
- toolCallId: toolCall.toolCallId,
1364
- toolName,
1365
- args: parseResult.value
1366
- };
1367
- }
1368
-
1369
- // core/generate-text/generate-text.ts
1370
- async function generateText({
1371
- model,
1372
- tools,
1373
- system,
1374
- prompt,
1375
- messages,
1376
- maxRetries,
1377
- abortSignal,
1378
- ...settings
1379
- }) {
1380
- var _a, _b;
1381
- const retry = retryWithExponentialBackoff({ maxRetries });
1382
- const modelResponse = await retry(
1383
- () => model.doGenerate({
1384
- mode: {
1385
- type: "regular",
1386
- tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1387
- type: "function",
1388
- name,
1389
- description: tool2.description,
1390
- parameters: (0, import_zod_to_json_schema3.default)(tool2.parameters)
1391
- }))
1392
- },
1393
- ...prepareCallSettings(settings),
1394
- inputFormat: getInputFormat({ prompt, messages }),
1395
- prompt: convertToLanguageModelPrompt({
1396
- system,
1397
- prompt,
1398
- messages
1399
- }),
1400
- abortSignal
1401
- })
1402
- );
1403
- const toolCalls = [];
1404
- for (const modelToolCall of (_a = modelResponse.toolCalls) != null ? _a : []) {
1405
- toolCalls.push(parseToolCall({ toolCall: modelToolCall, tools }));
1406
- }
1407
- const toolResults = tools == null ? [] : await executeTools({ toolCalls, tools });
1408
- return new GenerateTextResult({
1409
- // Always return a string so that the caller doesn't have to check for undefined.
1410
- // If they need to check if the model did not return any text,
1411
- // they can check the length of the string:
1412
- text: (_b = modelResponse.text) != null ? _b : "",
1413
- toolCalls,
1414
- toolResults,
1415
- finishReason: modelResponse.finishReason,
1416
- usage: calculateTokenUsage(modelResponse.usage)
1417
- });
1418
- }
1419
- async function executeTools({
1420
- toolCalls,
1421
- tools
1422
- }) {
1423
- const toolResults = await Promise.all(
1424
- toolCalls.map(async (toolCall) => {
1425
- const tool2 = tools[toolCall.toolName];
1426
- if ((tool2 == null ? void 0 : tool2.execute) == null) {
1427
- return void 0;
1428
- }
1429
- const result = await tool2.execute(toolCall.args);
1430
- return {
1431
- toolCallId: toolCall.toolCallId,
1432
- toolName: toolCall.toolName,
1433
- args: toolCall.args,
1434
- result
1435
- };
1436
- })
1437
- );
1438
- return toolResults.filter(
1439
- (result) => result != null
1440
- );
1441
- }
1442
- var GenerateTextResult = class {
1443
- constructor(options) {
1444
- this.text = options.text;
1445
- this.toolCalls = options.toolCalls;
1446
- this.toolResults = options.toolResults;
1447
- this.finishReason = options.finishReason;
1448
- this.usage = options.usage;
1449
- }
1450
- };
1451
-
1452
- // core/generate-text/stream-text.ts
1453
- var import_zod_to_json_schema4 = __toESM(require("zod-to-json-schema"));
1454
-
1455
- // shared/stream-parts.ts
1456
- var textStreamPart = {
1457
- code: "0",
1458
- name: "text",
1459
- parse: (value) => {
1460
- if (typeof value !== "string") {
1461
- throw new Error('"text" parts expect a string value.');
1462
- }
1463
- return { type: "text", value };
1464
- }
1465
- };
1466
- var functionCallStreamPart = {
1467
- code: "1",
1468
- name: "function_call",
1469
- parse: (value) => {
1470
- 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") {
1471
- throw new Error(
1472
- '"function_call" parts expect an object with a "function_call" property.'
1473
- );
1474
- }
1475
- return {
1476
- type: "function_call",
1477
- value
1478
- };
1479
- }
1480
- };
1481
- var dataStreamPart = {
1482
- code: "2",
1483
- name: "data",
1484
- parse: (value) => {
1485
- if (!Array.isArray(value)) {
1486
- throw new Error('"data" parts expect an array value.');
1487
- }
1488
- return { type: "data", value };
1489
- }
1490
- };
1491
- var errorStreamPart = {
1492
- code: "3",
1493
- name: "error",
1494
- parse: (value) => {
1495
- if (typeof value !== "string") {
1496
- throw new Error('"error" parts expect a string value.');
1497
- }
1498
- return { type: "error", value };
1499
- }
1500
- };
1501
- var assistantMessageStreamPart = {
1502
- code: "4",
1503
- name: "assistant_message",
1504
- parse: (value) => {
1505
- 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(
1506
- (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"
1507
- )) {
1508
- throw new Error(
1509
- '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
1510
- );
1511
- }
1512
- return {
1513
- type: "assistant_message",
1514
- value
1515
- };
1516
- }
1517
- };
1518
- var assistantControlDataStreamPart = {
1519
- code: "5",
1520
- name: "assistant_control_data",
1521
- parse: (value) => {
1522
- if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
1523
- throw new Error(
1524
- '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
1525
- );
1526
- }
1527
- return {
1528
- type: "assistant_control_data",
1529
- value: {
1530
- threadId: value.threadId,
1531
- messageId: value.messageId
1532
- }
1533
- };
1534
- }
1535
- };
1536
- var dataMessageStreamPart = {
1537
- code: "6",
1538
- name: "data_message",
1539
- parse: (value) => {
1540
- if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
1541
- throw new Error(
1542
- '"data_message" parts expect an object with a "role" and "data" property.'
1543
- );
1544
- }
1545
- return {
1546
- type: "data_message",
1547
- value
1548
- };
1549
- }
1550
- };
1551
- var toolCallStreamPart = {
1552
- code: "7",
1553
- name: "tool_calls",
1554
- parse: (value) => {
1555
- 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) => {
1556
- 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";
1557
- })) {
1558
- throw new Error(
1559
- '"tool_calls" parts expect an object with a ToolCallPayload.'
1560
- );
1561
- }
1562
- return {
1563
- type: "tool_calls",
1564
- value
1565
- };
1566
- }
1567
- };
1568
- var messageAnnotationsStreamPart = {
1569
- code: "8",
1570
- name: "message_annotations",
1571
- parse: (value) => {
1572
- if (!Array.isArray(value)) {
1573
- throw new Error('"message_annotations" parts expect an array value.');
1574
- }
1575
- return { type: "message_annotations", value };
1576
- }
1577
- };
1578
- var streamParts = [
1579
- textStreamPart,
1580
- functionCallStreamPart,
1581
- dataStreamPart,
1582
- errorStreamPart,
1583
- assistantMessageStreamPart,
1584
- assistantControlDataStreamPart,
1585
- dataMessageStreamPart,
1586
- toolCallStreamPart,
1587
- messageAnnotationsStreamPart
1588
- ];
1589
- var streamPartsByCode = {
1590
- [textStreamPart.code]: textStreamPart,
1591
- [functionCallStreamPart.code]: functionCallStreamPart,
1592
- [dataStreamPart.code]: dataStreamPart,
1593
- [errorStreamPart.code]: errorStreamPart,
1594
- [assistantMessageStreamPart.code]: assistantMessageStreamPart,
1595
- [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
1596
- [dataMessageStreamPart.code]: dataMessageStreamPart,
1597
- [toolCallStreamPart.code]: toolCallStreamPart,
1598
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
1599
- };
1600
- var StreamStringPrefixes = {
1601
- [textStreamPart.name]: textStreamPart.code,
1602
- [functionCallStreamPart.name]: functionCallStreamPart.code,
1603
- [dataStreamPart.name]: dataStreamPart.code,
1604
- [errorStreamPart.name]: errorStreamPart.code,
1605
- [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
1606
- [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
1607
- [dataMessageStreamPart.name]: dataMessageStreamPart.code,
1608
- [toolCallStreamPart.name]: toolCallStreamPart.code,
1609
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
1610
- };
1611
- var validCodes = streamParts.map((part) => part.code);
1612
- function formatStreamPart(type, value) {
1613
- const streamPart = streamParts.find((part) => part.name === type);
1614
- if (!streamPart) {
1615
- throw new Error(`Invalid stream part type: ${type}`);
1616
- }
1617
- return `${streamPart.code}:${JSON.stringify(value)}
1618
- `;
1619
- }
1620
-
1621
- // streams/ai-stream.ts
1622
- var import_eventsource_parser = require("eventsource-parser");
1623
- function createCallbacksTransformer(cb) {
1624
- const textEncoder = new TextEncoder();
1625
- let aggregatedResponse = "";
1626
- const callbacks = cb || {};
1627
- return new TransformStream({
1628
- async start() {
1629
- if (callbacks.onStart)
1630
- await callbacks.onStart();
1631
- },
1632
- async transform(message, controller) {
1633
- const content = typeof message === "string" ? message : message.content;
1634
- controller.enqueue(textEncoder.encode(content));
1635
- aggregatedResponse += content;
1636
- if (callbacks.onToken)
1637
- await callbacks.onToken(content);
1638
- if (callbacks.onText && typeof message === "string") {
1639
- await callbacks.onText(message);
1640
- }
1641
- },
1642
- async flush() {
1643
- const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
1644
- if (callbacks.onCompletion) {
1645
- await callbacks.onCompletion(aggregatedResponse);
1646
- }
1647
- if (callbacks.onFinal && !isOpenAICallbacks) {
1648
- await callbacks.onFinal(aggregatedResponse);
1649
- }
1650
- }
1651
- });
1652
- }
1653
- function isOfTypeOpenAIStreamCallbacks(callbacks) {
1654
- return "experimental_onFunctionCall" in callbacks;
1655
- }
1656
- function readableFromAsyncIterable(iterable) {
1657
- let it = iterable[Symbol.asyncIterator]();
1658
- return new ReadableStream({
1659
- async pull(controller) {
1660
- const { done, value } = await it.next();
1661
- if (done)
1662
- controller.close();
1663
- else
1664
- controller.enqueue(value);
1665
- },
1666
- async cancel(reason) {
1667
- var _a;
1668
- await ((_a = it.return) == null ? void 0 : _a.call(it, reason));
1669
- }
1670
- });
1671
- }
1672
-
1673
- // streams/stream-data.ts
1674
- function createStreamDataTransformer(experimental_streamData) {
1675
- if (!experimental_streamData) {
1676
- return new TransformStream({
1677
- transform: async (chunk, controller) => {
1678
- controller.enqueue(chunk);
1679
- }
1680
- });
1681
- }
1682
- const encoder = new TextEncoder();
1683
- const decoder = new TextDecoder();
1684
- return new TransformStream({
1685
- transform: async (chunk, controller) => {
1686
- const message = decoder.decode(chunk);
1687
- controller.enqueue(encoder.encode(formatStreamPart("text", message)));
1688
- }
1689
- });
1690
- }
1691
-
1692
- // core/generate-text/run-tools-transformation.ts
1693
- var import_nanoid = require("nanoid");
1694
- function runToolsTransformation({
1695
- tools,
1696
- generatorStream
1697
- }) {
1698
- let canClose = false;
1699
- const outstandingToolCalls = /* @__PURE__ */ new Set();
1700
- let toolResultsStreamController = null;
1701
- const toolResultsStream = new ReadableStream({
1702
- start(controller) {
1703
- toolResultsStreamController = controller;
1704
- }
1705
- });
1706
- const forwardStream = new TransformStream({
1707
- transform(chunk, controller) {
1708
- const chunkType = chunk.type;
1709
- switch (chunkType) {
1710
- case "text-delta":
1711
- case "error": {
1712
- controller.enqueue(chunk);
1713
- break;
1714
- }
1715
- case "tool-call": {
1716
- const toolName = chunk.toolName;
1717
- if (tools == null) {
1718
- toolResultsStreamController.enqueue({
1719
- type: "error",
1720
- error: new NoSuchToolError({
1721
- message: `Tool ${chunk.toolName} not found (no tools provided).`,
1722
- toolName: chunk.toolName
1723
- })
1724
- });
1725
- break;
1726
- }
1727
- const tool2 = tools[toolName];
1728
- if (tool2 == null) {
1729
- toolResultsStreamController.enqueue({
1730
- type: "error",
1731
- error: new NoSuchToolError({
1732
- message: `Tool ${chunk.toolName} not found.`,
1733
- toolName: chunk.toolName
1734
- })
1735
- });
1736
- break;
1737
- }
1738
- try {
1739
- const toolCall = parseToolCall({
1740
- toolCall: chunk,
1741
- tools
1742
- });
1743
- controller.enqueue({
1744
- type: "tool-call",
1745
- ...toolCall
1746
- });
1747
- if (tool2.execute != null) {
1748
- const toolExecutionId = (0, import_nanoid.nanoid)();
1749
- outstandingToolCalls.add(toolExecutionId);
1750
- tool2.execute(toolCall.args).then(
1751
- (result) => {
1752
- toolResultsStreamController.enqueue({
1753
- type: "tool-result",
1754
- ...toolCall,
1755
- result
1756
- });
1757
- outstandingToolCalls.delete(toolExecutionId);
1758
- if (canClose && outstandingToolCalls.size === 0) {
1759
- toolResultsStreamController.close();
1760
- }
1761
- },
1762
- (error) => {
1763
- toolResultsStreamController.enqueue({
1764
- type: "error",
1765
- error
1766
- });
1767
- outstandingToolCalls.delete(toolExecutionId);
1768
- if (canClose && outstandingToolCalls.size === 0) {
1769
- toolResultsStreamController.close();
1770
- }
1771
- }
1772
- );
1773
- }
1774
- } catch (error) {
1775
- toolResultsStreamController.enqueue({
1776
- type: "error",
1777
- error
1778
- });
1779
- }
1780
- break;
1781
- }
1782
- case "finish-metadata":
1783
- case "tool-call-delta": {
1784
- break;
1785
- }
1786
- default: {
1787
- const _exhaustiveCheck = chunkType;
1788
- throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
1789
- }
1790
- }
1791
- },
1792
- flush() {
1793
- canClose = true;
1794
- if (outstandingToolCalls.size === 0) {
1795
- toolResultsStreamController.close();
1796
- }
1797
- }
1798
- });
1799
- return new ReadableStream({
1800
- async start(controller) {
1801
- generatorStream.pipeThrough(forwardStream).pipeTo(
1802
- new WritableStream({
1803
- write(chunk) {
1804
- controller.enqueue(chunk);
1805
- },
1806
- close() {
1807
- }
1808
- })
1809
- );
1810
- toolResultsStream.pipeTo(
1811
- new WritableStream({
1812
- write(chunk) {
1813
- controller.enqueue(chunk);
1814
- },
1815
- close() {
1816
- controller.close();
1817
- }
1818
- })
1819
- );
1820
- }
1821
- });
1822
- }
1823
-
1824
- // core/generate-text/stream-text.ts
1825
- async function streamText({
1826
- model,
1827
- tools,
1828
- system,
1829
- prompt,
1830
- messages,
1831
- maxRetries,
1832
- abortSignal,
1833
- ...settings
1834
- }) {
1835
- const retry = retryWithExponentialBackoff({ maxRetries });
1836
- const { stream, warnings } = await retry(
1837
- () => model.doStream({
1838
- mode: {
1839
- type: "regular",
1840
- tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1841
- type: "function",
1842
- name,
1843
- description: tool2.description,
1844
- parameters: (0, import_zod_to_json_schema4.default)(tool2.parameters)
1845
- }))
1846
- },
1847
- ...prepareCallSettings(settings),
1848
- inputFormat: getInputFormat({ prompt, messages }),
1849
- prompt: convertToLanguageModelPrompt({
1850
- system,
1851
- prompt,
1852
- messages
1853
- }),
1854
- abortSignal
1855
- })
1856
- );
1857
- const toolStream = runToolsTransformation({
1858
- tools,
1859
- generatorStream: stream
1860
- });
1861
- return new StreamTextResult(toolStream);
1862
- }
1863
- var StreamTextResult = class {
1864
- constructor(stream) {
1865
- this.originalStream = stream;
1866
- }
1867
- get textStream() {
1868
- return createAsyncIterableStream(this.originalStream, {
1869
- transform(chunk, controller) {
1870
- if (chunk.type === "text-delta") {
1871
- if (chunk.textDelta.length > 0) {
1872
- controller.enqueue(chunk.textDelta);
1873
- }
1874
- } else if (chunk.type === "error") {
1875
- throw chunk.error;
1876
- }
1877
- }
1878
- });
1879
- }
1880
- get fullStream() {
1881
- return createAsyncIterableStream(this.originalStream, {
1882
- transform(chunk, controller) {
1883
- if (chunk.type === "text-delta") {
1884
- if (chunk.textDelta.length > 0) {
1885
- controller.enqueue(chunk);
1886
- }
1887
- } else {
1888
- controller.enqueue(chunk);
1889
- }
1890
- }
1891
- });
1892
- }
1893
- toAIStream(callbacks) {
1894
- return readableFromAsyncIterable(this.textStream).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
1895
- createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
1896
- );
1897
- }
1898
- };
1899
-
1900
- // core/tool/tool.ts
1901
- function tool(tool2) {
1902
- return tool2;
1903
- }
1904
- // Annotate the CommonJS export names for ESM import in node:
1905
- 0 && (module.exports = {
1906
- GenerateObjectResult,
1907
- GenerateTextResult,
1908
- StreamObjectResult,
1909
- StreamTextResult,
1910
- convertDataContentToBase64String,
1911
- convertDataContentToUint8Array,
1912
- generateObject,
1913
- generateText,
1914
- streamObject,
1915
- streamText,
1916
- tool
1917
- });
1918
- //# sourceMappingURL=index.js.map