ai 0.0.0-85f9a635-20240518005312

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 (56) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +37 -0
  3. package/dist/index.d.mts +2287 -0
  4. package/dist/index.d.ts +2287 -0
  5. package/dist/index.js +3531 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +3454 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +178 -0
  10. package/prompts/dist/index.d.mts +324 -0
  11. package/prompts/dist/index.d.ts +324 -0
  12. package/prompts/dist/index.js +178 -0
  13. package/prompts/dist/index.js.map +1 -0
  14. package/prompts/dist/index.mjs +146 -0
  15. package/prompts/dist/index.mjs.map +1 -0
  16. package/react/dist/index.d.mts +565 -0
  17. package/react/dist/index.d.ts +582 -0
  18. package/react/dist/index.js +1430 -0
  19. package/react/dist/index.js.map +1 -0
  20. package/react/dist/index.mjs +1391 -0
  21. package/react/dist/index.mjs.map +1 -0
  22. package/react/dist/index.server.d.mts +17 -0
  23. package/react/dist/index.server.d.ts +17 -0
  24. package/react/dist/index.server.js +50 -0
  25. package/react/dist/index.server.js.map +1 -0
  26. package/react/dist/index.server.mjs +23 -0
  27. package/react/dist/index.server.mjs.map +1 -0
  28. package/rsc/dist/index.d.ts +580 -0
  29. package/rsc/dist/index.mjs +18 -0
  30. package/rsc/dist/rsc-client.d.mts +1 -0
  31. package/rsc/dist/rsc-client.mjs +18 -0
  32. package/rsc/dist/rsc-client.mjs.map +1 -0
  33. package/rsc/dist/rsc-server.d.mts +516 -0
  34. package/rsc/dist/rsc-server.mjs +1900 -0
  35. package/rsc/dist/rsc-server.mjs.map +1 -0
  36. package/rsc/dist/rsc-shared.d.mts +94 -0
  37. package/rsc/dist/rsc-shared.mjs +346 -0
  38. package/rsc/dist/rsc-shared.mjs.map +1 -0
  39. package/solid/dist/index.d.mts +408 -0
  40. package/solid/dist/index.d.ts +408 -0
  41. package/solid/dist/index.js +1072 -0
  42. package/solid/dist/index.js.map +1 -0
  43. package/solid/dist/index.mjs +1044 -0
  44. package/solid/dist/index.mjs.map +1 -0
  45. package/svelte/dist/index.d.mts +484 -0
  46. package/svelte/dist/index.d.ts +484 -0
  47. package/svelte/dist/index.js +1778 -0
  48. package/svelte/dist/index.js.map +1 -0
  49. package/svelte/dist/index.mjs +1749 -0
  50. package/svelte/dist/index.mjs.map +1 -0
  51. package/vue/dist/index.d.mts +402 -0
  52. package/vue/dist/index.d.ts +402 -0
  53. package/vue/dist/index.js +1072 -0
  54. package/vue/dist/index.js.map +1 -0
  55. package/vue/dist/index.mjs +1034 -0
  56. package/vue/dist/index.mjs.map +1 -0
package/dist/index.js ADDED
@@ -0,0 +1,3531 @@
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
+ // streams/index.ts
31
+ var streams_exports = {};
32
+ __export(streams_exports, {
33
+ AIStream: () => AIStream,
34
+ APICallError: () => import_provider8.APICallError,
35
+ AWSBedrockAnthropicMessagesStream: () => AWSBedrockAnthropicMessagesStream,
36
+ AWSBedrockAnthropicStream: () => AWSBedrockAnthropicStream,
37
+ AWSBedrockCohereStream: () => AWSBedrockCohereStream,
38
+ AWSBedrockLlama2Stream: () => AWSBedrockLlama2Stream,
39
+ AWSBedrockStream: () => AWSBedrockStream,
40
+ AnthropicStream: () => AnthropicStream,
41
+ AssistantResponse: () => AssistantResponse,
42
+ CohereStream: () => CohereStream,
43
+ EmbedManyResult: () => EmbedManyResult,
44
+ EmbedResult: () => EmbedResult,
45
+ EmptyResponseBodyError: () => import_provider8.EmptyResponseBodyError,
46
+ GenerateObjectResult: () => GenerateObjectResult,
47
+ GenerateTextResult: () => GenerateTextResult,
48
+ GoogleGenerativeAIStream: () => GoogleGenerativeAIStream,
49
+ HuggingFaceStream: () => HuggingFaceStream,
50
+ InkeepStream: () => InkeepStream,
51
+ InvalidArgumentError: () => import_provider8.InvalidArgumentError,
52
+ InvalidDataContentError: () => import_provider8.InvalidDataContentError,
53
+ InvalidPromptError: () => import_provider8.InvalidPromptError,
54
+ InvalidResponseDataError: () => import_provider8.InvalidResponseDataError,
55
+ InvalidToolArgumentsError: () => import_provider8.InvalidToolArgumentsError,
56
+ JSONParseError: () => import_provider8.JSONParseError,
57
+ LangChainAdapter: () => langchain_adapter_exports,
58
+ LangChainStream: () => LangChainStream,
59
+ LoadAPIKeyError: () => import_provider8.LoadAPIKeyError,
60
+ MistralStream: () => MistralStream,
61
+ NoObjectGeneratedError: () => import_provider8.NoObjectGeneratedError,
62
+ NoSuchToolError: () => import_provider8.NoSuchToolError,
63
+ OpenAIStream: () => OpenAIStream,
64
+ ReplicateStream: () => ReplicateStream,
65
+ RetryError: () => import_provider8.RetryError,
66
+ StreamData: () => StreamData,
67
+ StreamObjectResult: () => StreamObjectResult,
68
+ StreamTextResult: () => StreamTextResult,
69
+ StreamingTextResponse: () => StreamingTextResponse,
70
+ ToolCallParseError: () => import_provider8.ToolCallParseError,
71
+ TypeValidationError: () => import_provider8.TypeValidationError,
72
+ UnsupportedFunctionalityError: () => import_provider8.UnsupportedFunctionalityError,
73
+ UnsupportedJSONSchemaError: () => import_provider8.UnsupportedJSONSchemaError,
74
+ convertDataContentToBase64String: () => convertDataContentToBase64String,
75
+ convertDataContentToUint8Array: () => convertDataContentToUint8Array,
76
+ convertToCoreMessages: () => convertToCoreMessages,
77
+ createCallbacksTransformer: () => createCallbacksTransformer,
78
+ createChunkDecoder: () => createChunkDecoder,
79
+ createEventStreamTransformer: () => createEventStreamTransformer,
80
+ createStreamDataTransformer: () => createStreamDataTransformer,
81
+ embed: () => embed,
82
+ embedMany: () => embedMany,
83
+ experimental_AssistantResponse: () => experimental_AssistantResponse,
84
+ experimental_StreamData: () => experimental_StreamData,
85
+ experimental_StreamingReactResponse: () => experimental_StreamingReactResponse,
86
+ experimental_generateObject: () => experimental_generateObject,
87
+ experimental_generateText: () => experimental_generateText,
88
+ experimental_streamObject: () => experimental_streamObject,
89
+ experimental_streamText: () => experimental_streamText,
90
+ formatStreamPart: () => formatStreamPart,
91
+ generateId: () => generateId,
92
+ generateObject: () => generateObject,
93
+ generateText: () => generateText,
94
+ isStreamStringEqualToType: () => isStreamStringEqualToType,
95
+ nanoid: () => generateId,
96
+ parseStreamPart: () => parseStreamPart,
97
+ readDataStream: () => readDataStream,
98
+ readableFromAsyncIterable: () => readableFromAsyncIterable,
99
+ streamObject: () => streamObject,
100
+ streamResponse: () => streamResponse,
101
+ streamText: () => streamText,
102
+ streamToResponse: () => streamToResponse,
103
+ tool: () => tool,
104
+ trimStartOfStreamHelper: () => trimStartOfStreamHelper
105
+ });
106
+ module.exports = __toCommonJS(streams_exports);
107
+
108
+ // core/util/retry-with-exponential-backoff.ts
109
+ var import_provider = require("@ai-sdk/provider");
110
+ var import_provider_utils = require("@ai-sdk/provider-utils");
111
+
112
+ // core/util/delay.ts
113
+ async function delay(delayInMs) {
114
+ return new Promise((resolve) => setTimeout(resolve, delayInMs));
115
+ }
116
+
117
+ // core/util/retry-with-exponential-backoff.ts
118
+ var retryWithExponentialBackoff = ({
119
+ maxRetries = 2,
120
+ initialDelayInMs = 2e3,
121
+ backoffFactor = 2
122
+ } = {}) => async (f) => _retryWithExponentialBackoff(f, {
123
+ maxRetries,
124
+ delayInMs: initialDelayInMs,
125
+ backoffFactor
126
+ });
127
+ async function _retryWithExponentialBackoff(f, {
128
+ maxRetries,
129
+ delayInMs,
130
+ backoffFactor
131
+ }, errors = []) {
132
+ try {
133
+ return await f();
134
+ } catch (error) {
135
+ if ((0, import_provider_utils.isAbortError)(error)) {
136
+ throw error;
137
+ }
138
+ if (maxRetries === 0) {
139
+ throw error;
140
+ }
141
+ const errorMessage = (0, import_provider_utils.getErrorMessage)(error);
142
+ const newErrors = [...errors, error];
143
+ const tryNumber = newErrors.length;
144
+ if (tryNumber > maxRetries) {
145
+ throw new import_provider.RetryError({
146
+ message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
147
+ reason: "maxRetriesExceeded",
148
+ errors: newErrors
149
+ });
150
+ }
151
+ if (error instanceof Error && import_provider.APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries) {
152
+ await delay(delayInMs);
153
+ return _retryWithExponentialBackoff(
154
+ f,
155
+ { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
156
+ newErrors
157
+ );
158
+ }
159
+ if (tryNumber === 1) {
160
+ throw error;
161
+ }
162
+ throw new import_provider.RetryError({
163
+ message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
164
+ reason: "errorNotRetryable",
165
+ errors: newErrors
166
+ });
167
+ }
168
+ }
169
+
170
+ // core/embed/embed.ts
171
+ async function embed({
172
+ model,
173
+ value,
174
+ maxRetries,
175
+ abortSignal
176
+ }) {
177
+ const retry = retryWithExponentialBackoff({ maxRetries });
178
+ const modelResponse = await retry(
179
+ () => model.doEmbed({
180
+ values: [value],
181
+ abortSignal
182
+ })
183
+ );
184
+ return new EmbedResult({
185
+ value,
186
+ embedding: modelResponse.embeddings[0],
187
+ rawResponse: modelResponse.rawResponse
188
+ });
189
+ }
190
+ var EmbedResult = class {
191
+ constructor(options) {
192
+ this.value = options.value;
193
+ this.embedding = options.embedding;
194
+ this.rawResponse = options.rawResponse;
195
+ }
196
+ };
197
+
198
+ // core/util/split-array.ts
199
+ function splitArray(array, chunkSize) {
200
+ if (chunkSize <= 0) {
201
+ throw new Error("chunkSize must be greater than 0");
202
+ }
203
+ const result = [];
204
+ for (let i = 0; i < array.length; i += chunkSize) {
205
+ result.push(array.slice(i, i + chunkSize));
206
+ }
207
+ return result;
208
+ }
209
+
210
+ // core/embed/embed-many.ts
211
+ async function embedMany({
212
+ model,
213
+ values,
214
+ maxRetries,
215
+ abortSignal
216
+ }) {
217
+ const retry = retryWithExponentialBackoff({ maxRetries });
218
+ const maxEmbeddingsPerCall = model.maxEmbeddingsPerCall;
219
+ if (maxEmbeddingsPerCall == null) {
220
+ const modelResponse = await retry(
221
+ () => model.doEmbed({ values, abortSignal })
222
+ );
223
+ return new EmbedManyResult({
224
+ values,
225
+ embeddings: modelResponse.embeddings
226
+ });
227
+ }
228
+ const valueChunks = splitArray(values, maxEmbeddingsPerCall);
229
+ const embeddings = [];
230
+ for (const chunk of valueChunks) {
231
+ const modelResponse = await retry(
232
+ () => model.doEmbed({ values: chunk, abortSignal })
233
+ );
234
+ embeddings.push(...modelResponse.embeddings);
235
+ }
236
+ return new EmbedManyResult({ values, embeddings });
237
+ }
238
+ var EmbedManyResult = class {
239
+ constructor(options) {
240
+ this.values = options.values;
241
+ this.embeddings = options.embeddings;
242
+ }
243
+ };
244
+
245
+ // core/generate-object/generate-object.ts
246
+ var import_provider5 = require("@ai-sdk/provider");
247
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
248
+
249
+ // core/generate-text/token-usage.ts
250
+ function calculateTokenUsage(usage) {
251
+ return {
252
+ promptTokens: usage.promptTokens,
253
+ completionTokens: usage.completionTokens,
254
+ totalTokens: usage.promptTokens + usage.completionTokens
255
+ };
256
+ }
257
+
258
+ // core/util/detect-image-mimetype.ts
259
+ var mimeTypeSignatures = [
260
+ { mimeType: "image/gif", bytes: [71, 73, 70] },
261
+ { mimeType: "image/png", bytes: [137, 80, 78, 71] },
262
+ { mimeType: "image/jpeg", bytes: [255, 216] },
263
+ { mimeType: "image/webp", bytes: [82, 73, 70, 70] }
264
+ ];
265
+ function detectImageMimeType(image) {
266
+ for (const { bytes, mimeType } of mimeTypeSignatures) {
267
+ if (image.length >= bytes.length && bytes.every((byte, index) => image[index] === byte)) {
268
+ return mimeType;
269
+ }
270
+ }
271
+ return void 0;
272
+ }
273
+
274
+ // core/prompt/data-content.ts
275
+ var import_provider2 = require("@ai-sdk/provider");
276
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
277
+ function convertDataContentToBase64String(content) {
278
+ if (typeof content === "string") {
279
+ return content;
280
+ }
281
+ if (content instanceof ArrayBuffer) {
282
+ return (0, import_provider_utils2.convertUint8ArrayToBase64)(new Uint8Array(content));
283
+ }
284
+ return (0, import_provider_utils2.convertUint8ArrayToBase64)(content);
285
+ }
286
+ function convertDataContentToUint8Array(content) {
287
+ if (content instanceof Uint8Array) {
288
+ return content;
289
+ }
290
+ if (typeof content === "string") {
291
+ try {
292
+ return (0, import_provider_utils2.convertBase64ToUint8Array)(content);
293
+ } catch (error) {
294
+ throw new import_provider2.InvalidDataContentError({
295
+ message: "Invalid data content. Content string is not a base64-encoded image.",
296
+ content,
297
+ cause: error
298
+ });
299
+ }
300
+ }
301
+ if (content instanceof ArrayBuffer) {
302
+ return new Uint8Array(content);
303
+ }
304
+ throw new import_provider2.InvalidDataContentError({ content });
305
+ }
306
+
307
+ // core/prompt/convert-to-language-model-prompt.ts
308
+ function convertToLanguageModelPrompt(prompt) {
309
+ const languageModelMessages = [];
310
+ if (prompt.system != null) {
311
+ languageModelMessages.push({ role: "system", content: prompt.system });
312
+ }
313
+ switch (prompt.type) {
314
+ case "prompt": {
315
+ languageModelMessages.push({
316
+ role: "user",
317
+ content: [{ type: "text", text: prompt.prompt }]
318
+ });
319
+ break;
320
+ }
321
+ case "messages": {
322
+ languageModelMessages.push(
323
+ ...prompt.messages.map((message) => {
324
+ switch (message.role) {
325
+ case "system": {
326
+ return { role: "system", content: message.content };
327
+ }
328
+ case "user": {
329
+ if (typeof message.content === "string") {
330
+ return {
331
+ role: "user",
332
+ content: [{ type: "text", text: message.content }]
333
+ };
334
+ }
335
+ return {
336
+ role: "user",
337
+ content: message.content.map(
338
+ (part) => {
339
+ var _a;
340
+ switch (part.type) {
341
+ case "text": {
342
+ return part;
343
+ }
344
+ case "image": {
345
+ if (part.image instanceof URL) {
346
+ return {
347
+ type: "image",
348
+ image: part.image,
349
+ mimeType: part.mimeType
350
+ };
351
+ }
352
+ const imageUint8 = convertDataContentToUint8Array(
353
+ part.image
354
+ );
355
+ return {
356
+ type: "image",
357
+ image: imageUint8,
358
+ mimeType: (_a = part.mimeType) != null ? _a : detectImageMimeType(imageUint8)
359
+ };
360
+ }
361
+ }
362
+ }
363
+ )
364
+ };
365
+ }
366
+ case "assistant": {
367
+ if (typeof message.content === "string") {
368
+ return {
369
+ role: "assistant",
370
+ content: [{ type: "text", text: message.content }]
371
+ };
372
+ }
373
+ return { role: "assistant", content: message.content };
374
+ }
375
+ case "tool": {
376
+ return message;
377
+ }
378
+ }
379
+ })
380
+ );
381
+ break;
382
+ }
383
+ default: {
384
+ const _exhaustiveCheck = prompt;
385
+ throw new Error(`Unsupported prompt type: ${_exhaustiveCheck}`);
386
+ }
387
+ }
388
+ return languageModelMessages;
389
+ }
390
+
391
+ // core/prompt/get-validated-prompt.ts
392
+ var import_provider3 = require("@ai-sdk/provider");
393
+ function getValidatedPrompt(prompt) {
394
+ if (prompt.prompt == null && prompt.messages == null) {
395
+ throw new import_provider3.InvalidPromptError({
396
+ prompt,
397
+ message: "prompt or messages must be defined"
398
+ });
399
+ }
400
+ if (prompt.prompt != null && prompt.messages != null) {
401
+ throw new import_provider3.InvalidPromptError({
402
+ prompt,
403
+ message: "prompt and messages cannot be defined at the same time"
404
+ });
405
+ }
406
+ return prompt.prompt != null ? {
407
+ type: "prompt",
408
+ prompt: prompt.prompt,
409
+ messages: void 0,
410
+ system: prompt.system
411
+ } : {
412
+ type: "messages",
413
+ prompt: void 0,
414
+ messages: prompt.messages,
415
+ // only possible case bc of checks above
416
+ system: prompt.system
417
+ };
418
+ }
419
+
420
+ // core/prompt/prepare-call-settings.ts
421
+ var import_provider4 = require("@ai-sdk/provider");
422
+ function prepareCallSettings({
423
+ maxTokens,
424
+ temperature,
425
+ topP,
426
+ presencePenalty,
427
+ frequencyPenalty,
428
+ seed,
429
+ maxRetries
430
+ }) {
431
+ if (maxTokens != null) {
432
+ if (!Number.isInteger(maxTokens)) {
433
+ throw new import_provider4.InvalidArgumentError({
434
+ parameter: "maxTokens",
435
+ value: maxTokens,
436
+ message: "maxTokens must be an integer"
437
+ });
438
+ }
439
+ if (maxTokens < 1) {
440
+ throw new import_provider4.InvalidArgumentError({
441
+ parameter: "maxTokens",
442
+ value: maxTokens,
443
+ message: "maxTokens must be >= 1"
444
+ });
445
+ }
446
+ }
447
+ if (temperature != null) {
448
+ if (typeof temperature !== "number") {
449
+ throw new import_provider4.InvalidArgumentError({
450
+ parameter: "temperature",
451
+ value: temperature,
452
+ message: "temperature must be a number"
453
+ });
454
+ }
455
+ }
456
+ if (topP != null) {
457
+ if (typeof topP !== "number") {
458
+ throw new import_provider4.InvalidArgumentError({
459
+ parameter: "topP",
460
+ value: topP,
461
+ message: "topP must be a number"
462
+ });
463
+ }
464
+ }
465
+ if (presencePenalty != null) {
466
+ if (typeof presencePenalty !== "number") {
467
+ throw new import_provider4.InvalidArgumentError({
468
+ parameter: "presencePenalty",
469
+ value: presencePenalty,
470
+ message: "presencePenalty must be a number"
471
+ });
472
+ }
473
+ }
474
+ if (frequencyPenalty != null) {
475
+ if (typeof frequencyPenalty !== "number") {
476
+ throw new import_provider4.InvalidArgumentError({
477
+ parameter: "frequencyPenalty",
478
+ value: frequencyPenalty,
479
+ message: "frequencyPenalty must be a number"
480
+ });
481
+ }
482
+ }
483
+ if (seed != null) {
484
+ if (!Number.isInteger(seed)) {
485
+ throw new import_provider4.InvalidArgumentError({
486
+ parameter: "seed",
487
+ value: seed,
488
+ message: "seed must be an integer"
489
+ });
490
+ }
491
+ }
492
+ if (maxRetries != null) {
493
+ if (!Number.isInteger(maxRetries)) {
494
+ throw new import_provider4.InvalidArgumentError({
495
+ parameter: "maxRetries",
496
+ value: maxRetries,
497
+ message: "maxRetries must be an integer"
498
+ });
499
+ }
500
+ if (maxRetries < 0) {
501
+ throw new import_provider4.InvalidArgumentError({
502
+ parameter: "maxRetries",
503
+ value: maxRetries,
504
+ message: "maxRetries must be >= 0"
505
+ });
506
+ }
507
+ }
508
+ return {
509
+ maxTokens,
510
+ temperature: temperature != null ? temperature : 0,
511
+ topP,
512
+ presencePenalty,
513
+ frequencyPenalty,
514
+ seed,
515
+ maxRetries: maxRetries != null ? maxRetries : 2
516
+ };
517
+ }
518
+
519
+ // core/util/convert-zod-to-json-schema.ts
520
+ var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
521
+ function convertZodToJSONSchema(zodSchema) {
522
+ return (0, import_zod_to_json_schema.default)(zodSchema);
523
+ }
524
+
525
+ // core/generate-object/inject-json-schema-into-system.ts
526
+ var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
527
+ var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
528
+ function injectJsonSchemaIntoSystem({
529
+ system,
530
+ schema,
531
+ schemaPrefix = DEFAULT_SCHEMA_PREFIX,
532
+ schemaSuffix = DEFAULT_SCHEMA_SUFFIX
533
+ }) {
534
+ return [
535
+ system,
536
+ system != null ? "" : null,
537
+ // add a newline if system is not null
538
+ schemaPrefix,
539
+ JSON.stringify(schema),
540
+ schemaSuffix
541
+ ].filter((line) => line != null).join("\n");
542
+ }
543
+
544
+ // core/generate-object/generate-object.ts
545
+ async function generateObject({
546
+ model,
547
+ schema,
548
+ mode,
549
+ system,
550
+ prompt,
551
+ messages,
552
+ maxRetries,
553
+ abortSignal,
554
+ ...settings
555
+ }) {
556
+ var _a, _b;
557
+ const retry = retryWithExponentialBackoff({ maxRetries });
558
+ const jsonSchema = convertZodToJSONSchema(schema);
559
+ if (mode === "auto" || mode == null) {
560
+ mode = model.defaultObjectGenerationMode;
561
+ }
562
+ let result;
563
+ let finishReason;
564
+ let usage;
565
+ let warnings;
566
+ let rawResponse;
567
+ let logprobs;
568
+ switch (mode) {
569
+ case "json": {
570
+ const validatedPrompt = getValidatedPrompt({
571
+ system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
572
+ prompt,
573
+ messages
574
+ });
575
+ const generateResult = await retry(() => {
576
+ return model.doGenerate({
577
+ mode: { type: "object-json" },
578
+ ...prepareCallSettings(settings),
579
+ inputFormat: validatedPrompt.type,
580
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
581
+ abortSignal
582
+ });
583
+ });
584
+ if (generateResult.text === void 0) {
585
+ throw new import_provider5.NoObjectGeneratedError();
586
+ }
587
+ result = generateResult.text;
588
+ finishReason = generateResult.finishReason;
589
+ usage = generateResult.usage;
590
+ warnings = generateResult.warnings;
591
+ rawResponse = generateResult.rawResponse;
592
+ logprobs = generateResult.logprobs;
593
+ break;
594
+ }
595
+ case "grammar": {
596
+ const validatedPrompt = getValidatedPrompt({
597
+ system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
598
+ prompt,
599
+ messages
600
+ });
601
+ const generateResult = await retry(
602
+ () => model.doGenerate({
603
+ mode: { type: "object-grammar", schema: jsonSchema },
604
+ ...settings,
605
+ inputFormat: validatedPrompt.type,
606
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
607
+ abortSignal
608
+ })
609
+ );
610
+ if (generateResult.text === void 0) {
611
+ throw new import_provider5.NoObjectGeneratedError();
612
+ }
613
+ result = generateResult.text;
614
+ finishReason = generateResult.finishReason;
615
+ usage = generateResult.usage;
616
+ warnings = generateResult.warnings;
617
+ rawResponse = generateResult.rawResponse;
618
+ logprobs = generateResult.logprobs;
619
+ break;
620
+ }
621
+ case "tool": {
622
+ const validatedPrompt = getValidatedPrompt({
623
+ system,
624
+ prompt,
625
+ messages
626
+ });
627
+ const generateResult = await retry(
628
+ () => model.doGenerate({
629
+ mode: {
630
+ type: "object-tool",
631
+ tool: {
632
+ type: "function",
633
+ name: "json",
634
+ description: "Respond with a JSON object.",
635
+ parameters: jsonSchema
636
+ }
637
+ },
638
+ ...settings,
639
+ inputFormat: validatedPrompt.type,
640
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
641
+ abortSignal
642
+ })
643
+ );
644
+ const functionArgs = (_b = (_a = generateResult.toolCalls) == null ? void 0 : _a[0]) == null ? void 0 : _b.args;
645
+ if (functionArgs === void 0) {
646
+ throw new import_provider5.NoObjectGeneratedError();
647
+ }
648
+ result = functionArgs;
649
+ finishReason = generateResult.finishReason;
650
+ usage = generateResult.usage;
651
+ warnings = generateResult.warnings;
652
+ rawResponse = generateResult.rawResponse;
653
+ logprobs = generateResult.logprobs;
654
+ break;
655
+ }
656
+ case void 0: {
657
+ throw new Error("Model does not have a default object generation mode.");
658
+ }
659
+ default: {
660
+ const _exhaustiveCheck = mode;
661
+ throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
662
+ }
663
+ }
664
+ const parseResult = (0, import_provider_utils3.safeParseJSON)({ text: result, schema });
665
+ if (!parseResult.success) {
666
+ throw parseResult.error;
667
+ }
668
+ return new GenerateObjectResult({
669
+ object: parseResult.value,
670
+ finishReason,
671
+ usage: calculateTokenUsage(usage),
672
+ warnings,
673
+ rawResponse,
674
+ logprobs
675
+ });
676
+ }
677
+ var GenerateObjectResult = class {
678
+ constructor(options) {
679
+ this.object = options.object;
680
+ this.finishReason = options.finishReason;
681
+ this.usage = options.usage;
682
+ this.warnings = options.warnings;
683
+ this.rawResponse = options.rawResponse;
684
+ this.logprobs = options.logprobs;
685
+ }
686
+ };
687
+ var experimental_generateObject = generateObject;
688
+
689
+ // core/util/async-iterable-stream.ts
690
+ function createAsyncIterableStream(source, transformer) {
691
+ const transformedStream = source.pipeThrough(
692
+ new TransformStream(transformer)
693
+ );
694
+ transformedStream[Symbol.asyncIterator] = () => {
695
+ const reader = transformedStream.getReader();
696
+ return {
697
+ async next() {
698
+ const { done, value } = await reader.read();
699
+ return done ? { done: true, value: void 0 } : { done: false, value };
700
+ }
701
+ };
702
+ };
703
+ return transformedStream;
704
+ }
705
+
706
+ // core/util/is-deep-equal-data.ts
707
+ function isDeepEqualData(obj1, obj2) {
708
+ if (obj1 === obj2)
709
+ return true;
710
+ if (obj1 == null || obj2 == null)
711
+ return false;
712
+ if (typeof obj1 !== "object" && typeof obj2 !== "object")
713
+ return obj1 === obj2;
714
+ if (obj1.constructor !== obj2.constructor)
715
+ return false;
716
+ if (obj1 instanceof Date && obj2 instanceof Date) {
717
+ return obj1.getTime() === obj2.getTime();
718
+ }
719
+ if (Array.isArray(obj1)) {
720
+ if (obj1.length !== obj2.length)
721
+ return false;
722
+ for (let i = 0; i < obj1.length; i++) {
723
+ if (!isDeepEqualData(obj1[i], obj2[i]))
724
+ return false;
725
+ }
726
+ return true;
727
+ }
728
+ const keys1 = Object.keys(obj1);
729
+ const keys2 = Object.keys(obj2);
730
+ if (keys1.length !== keys2.length)
731
+ return false;
732
+ for (const key of keys1) {
733
+ if (!keys2.includes(key))
734
+ return false;
735
+ if (!isDeepEqualData(obj1[key], obj2[key]))
736
+ return false;
737
+ }
738
+ return true;
739
+ }
740
+
741
+ // core/util/parse-partial-json.ts
742
+ var import_secure_json_parse = __toESM(require("secure-json-parse"));
743
+
744
+ // core/util/fix-json.ts
745
+ function fixJson(input) {
746
+ const stack = ["ROOT"];
747
+ let lastValidIndex = -1;
748
+ let literalStart = null;
749
+ function processValueStart(char, i, swapState) {
750
+ {
751
+ switch (char) {
752
+ case '"': {
753
+ lastValidIndex = i;
754
+ stack.pop();
755
+ stack.push(swapState);
756
+ stack.push("INSIDE_STRING");
757
+ break;
758
+ }
759
+ case "f":
760
+ case "t":
761
+ case "n": {
762
+ lastValidIndex = i;
763
+ literalStart = i;
764
+ stack.pop();
765
+ stack.push(swapState);
766
+ stack.push("INSIDE_LITERAL");
767
+ break;
768
+ }
769
+ case "-": {
770
+ stack.pop();
771
+ stack.push(swapState);
772
+ stack.push("INSIDE_NUMBER");
773
+ break;
774
+ }
775
+ case "0":
776
+ case "1":
777
+ case "2":
778
+ case "3":
779
+ case "4":
780
+ case "5":
781
+ case "6":
782
+ case "7":
783
+ case "8":
784
+ case "9": {
785
+ lastValidIndex = i;
786
+ stack.pop();
787
+ stack.push(swapState);
788
+ stack.push("INSIDE_NUMBER");
789
+ break;
790
+ }
791
+ case "{": {
792
+ lastValidIndex = i;
793
+ stack.pop();
794
+ stack.push(swapState);
795
+ stack.push("INSIDE_OBJECT_START");
796
+ break;
797
+ }
798
+ case "[": {
799
+ lastValidIndex = i;
800
+ stack.pop();
801
+ stack.push(swapState);
802
+ stack.push("INSIDE_ARRAY_START");
803
+ break;
804
+ }
805
+ }
806
+ }
807
+ }
808
+ function processAfterObjectValue(char, i) {
809
+ switch (char) {
810
+ case ",": {
811
+ stack.pop();
812
+ stack.push("INSIDE_OBJECT_AFTER_COMMA");
813
+ break;
814
+ }
815
+ case "}": {
816
+ lastValidIndex = i;
817
+ stack.pop();
818
+ break;
819
+ }
820
+ }
821
+ }
822
+ function processAfterArrayValue(char, i) {
823
+ switch (char) {
824
+ case ",": {
825
+ stack.pop();
826
+ stack.push("INSIDE_ARRAY_AFTER_COMMA");
827
+ break;
828
+ }
829
+ case "]": {
830
+ lastValidIndex = i;
831
+ stack.pop();
832
+ break;
833
+ }
834
+ }
835
+ }
836
+ for (let i = 0; i < input.length; i++) {
837
+ const char = input[i];
838
+ const currentState = stack[stack.length - 1];
839
+ switch (currentState) {
840
+ case "ROOT":
841
+ processValueStart(char, i, "FINISH");
842
+ break;
843
+ case "INSIDE_OBJECT_START": {
844
+ switch (char) {
845
+ case '"': {
846
+ stack.pop();
847
+ stack.push("INSIDE_OBJECT_KEY");
848
+ break;
849
+ }
850
+ case "}": {
851
+ lastValidIndex = i;
852
+ stack.pop();
853
+ break;
854
+ }
855
+ }
856
+ break;
857
+ }
858
+ case "INSIDE_OBJECT_AFTER_COMMA": {
859
+ switch (char) {
860
+ case '"': {
861
+ stack.pop();
862
+ stack.push("INSIDE_OBJECT_KEY");
863
+ break;
864
+ }
865
+ }
866
+ break;
867
+ }
868
+ case "INSIDE_OBJECT_KEY": {
869
+ switch (char) {
870
+ case '"': {
871
+ stack.pop();
872
+ stack.push("INSIDE_OBJECT_AFTER_KEY");
873
+ break;
874
+ }
875
+ }
876
+ break;
877
+ }
878
+ case "INSIDE_OBJECT_AFTER_KEY": {
879
+ switch (char) {
880
+ case ":": {
881
+ stack.pop();
882
+ stack.push("INSIDE_OBJECT_BEFORE_VALUE");
883
+ break;
884
+ }
885
+ }
886
+ break;
887
+ }
888
+ case "INSIDE_OBJECT_BEFORE_VALUE": {
889
+ processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
890
+ break;
891
+ }
892
+ case "INSIDE_OBJECT_AFTER_VALUE": {
893
+ processAfterObjectValue(char, i);
894
+ break;
895
+ }
896
+ case "INSIDE_STRING": {
897
+ switch (char) {
898
+ case '"': {
899
+ stack.pop();
900
+ lastValidIndex = i;
901
+ break;
902
+ }
903
+ case "\\": {
904
+ stack.push("INSIDE_STRING_ESCAPE");
905
+ break;
906
+ }
907
+ default: {
908
+ lastValidIndex = i;
909
+ }
910
+ }
911
+ break;
912
+ }
913
+ case "INSIDE_ARRAY_START": {
914
+ switch (char) {
915
+ case "]": {
916
+ lastValidIndex = i;
917
+ stack.pop();
918
+ break;
919
+ }
920
+ default: {
921
+ lastValidIndex = i;
922
+ processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
923
+ break;
924
+ }
925
+ }
926
+ break;
927
+ }
928
+ case "INSIDE_ARRAY_AFTER_VALUE": {
929
+ switch (char) {
930
+ case ",": {
931
+ stack.pop();
932
+ stack.push("INSIDE_ARRAY_AFTER_COMMA");
933
+ break;
934
+ }
935
+ case "]": {
936
+ lastValidIndex = i;
937
+ stack.pop();
938
+ break;
939
+ }
940
+ default: {
941
+ lastValidIndex = i;
942
+ break;
943
+ }
944
+ }
945
+ break;
946
+ }
947
+ case "INSIDE_ARRAY_AFTER_COMMA": {
948
+ processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
949
+ break;
950
+ }
951
+ case "INSIDE_STRING_ESCAPE": {
952
+ stack.pop();
953
+ lastValidIndex = i;
954
+ break;
955
+ }
956
+ case "INSIDE_NUMBER": {
957
+ switch (char) {
958
+ case "0":
959
+ case "1":
960
+ case "2":
961
+ case "3":
962
+ case "4":
963
+ case "5":
964
+ case "6":
965
+ case "7":
966
+ case "8":
967
+ case "9": {
968
+ lastValidIndex = i;
969
+ break;
970
+ }
971
+ case "e":
972
+ case "E":
973
+ case "-":
974
+ case ".": {
975
+ break;
976
+ }
977
+ case ",": {
978
+ stack.pop();
979
+ if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
980
+ processAfterArrayValue(char, i);
981
+ }
982
+ if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
983
+ processAfterObjectValue(char, i);
984
+ }
985
+ break;
986
+ }
987
+ case "}": {
988
+ stack.pop();
989
+ if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
990
+ processAfterObjectValue(char, i);
991
+ }
992
+ break;
993
+ }
994
+ case "]": {
995
+ stack.pop();
996
+ if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
997
+ processAfterArrayValue(char, i);
998
+ }
999
+ break;
1000
+ }
1001
+ default: {
1002
+ stack.pop();
1003
+ break;
1004
+ }
1005
+ }
1006
+ break;
1007
+ }
1008
+ case "INSIDE_LITERAL": {
1009
+ const partialLiteral = input.substring(literalStart, i + 1);
1010
+ if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
1011
+ stack.pop();
1012
+ if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
1013
+ processAfterObjectValue(char, i);
1014
+ } else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
1015
+ processAfterArrayValue(char, i);
1016
+ }
1017
+ } else {
1018
+ lastValidIndex = i;
1019
+ }
1020
+ break;
1021
+ }
1022
+ }
1023
+ }
1024
+ let result = input.slice(0, lastValidIndex + 1);
1025
+ for (let i = stack.length - 1; i >= 0; i--) {
1026
+ const state = stack[i];
1027
+ switch (state) {
1028
+ case "INSIDE_STRING": {
1029
+ result += '"';
1030
+ break;
1031
+ }
1032
+ case "INSIDE_OBJECT_KEY":
1033
+ case "INSIDE_OBJECT_AFTER_KEY":
1034
+ case "INSIDE_OBJECT_AFTER_COMMA":
1035
+ case "INSIDE_OBJECT_START":
1036
+ case "INSIDE_OBJECT_BEFORE_VALUE":
1037
+ case "INSIDE_OBJECT_AFTER_VALUE": {
1038
+ result += "}";
1039
+ break;
1040
+ }
1041
+ case "INSIDE_ARRAY_START":
1042
+ case "INSIDE_ARRAY_AFTER_COMMA":
1043
+ case "INSIDE_ARRAY_AFTER_VALUE": {
1044
+ result += "]";
1045
+ break;
1046
+ }
1047
+ case "INSIDE_LITERAL": {
1048
+ const partialLiteral = input.substring(literalStart, input.length);
1049
+ if ("true".startsWith(partialLiteral)) {
1050
+ result += "true".slice(partialLiteral.length);
1051
+ } else if ("false".startsWith(partialLiteral)) {
1052
+ result += "false".slice(partialLiteral.length);
1053
+ } else if ("null".startsWith(partialLiteral)) {
1054
+ result += "null".slice(partialLiteral.length);
1055
+ }
1056
+ }
1057
+ }
1058
+ }
1059
+ return result;
1060
+ }
1061
+
1062
+ // core/util/parse-partial-json.ts
1063
+ function parsePartialJson(jsonText) {
1064
+ if (jsonText == null) {
1065
+ return void 0;
1066
+ }
1067
+ try {
1068
+ return import_secure_json_parse.default.parse(jsonText);
1069
+ } catch (ignored) {
1070
+ try {
1071
+ const fixedJsonText = fixJson(jsonText);
1072
+ return import_secure_json_parse.default.parse(fixedJsonText);
1073
+ } catch (ignored2) {
1074
+ }
1075
+ }
1076
+ return void 0;
1077
+ }
1078
+
1079
+ // core/generate-object/stream-object.ts
1080
+ async function streamObject({
1081
+ model,
1082
+ schema,
1083
+ mode,
1084
+ system,
1085
+ prompt,
1086
+ messages,
1087
+ maxRetries,
1088
+ abortSignal,
1089
+ ...settings
1090
+ }) {
1091
+ const retry = retryWithExponentialBackoff({ maxRetries });
1092
+ const jsonSchema = convertZodToJSONSchema(schema);
1093
+ if (mode === "auto" || mode == null) {
1094
+ mode = model.defaultObjectGenerationMode;
1095
+ }
1096
+ let callOptions;
1097
+ let transformer;
1098
+ switch (mode) {
1099
+ case "json": {
1100
+ const validatedPrompt = getValidatedPrompt({
1101
+ system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
1102
+ prompt,
1103
+ messages
1104
+ });
1105
+ callOptions = {
1106
+ mode: { type: "object-json" },
1107
+ ...prepareCallSettings(settings),
1108
+ inputFormat: validatedPrompt.type,
1109
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
1110
+ abortSignal
1111
+ };
1112
+ transformer = {
1113
+ transform: (chunk, controller) => {
1114
+ switch (chunk.type) {
1115
+ case "text-delta":
1116
+ controller.enqueue(chunk.textDelta);
1117
+ break;
1118
+ case "finish":
1119
+ case "error":
1120
+ controller.enqueue(chunk);
1121
+ break;
1122
+ }
1123
+ }
1124
+ };
1125
+ break;
1126
+ }
1127
+ case "grammar": {
1128
+ const validatedPrompt = getValidatedPrompt({
1129
+ system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
1130
+ prompt,
1131
+ messages
1132
+ });
1133
+ callOptions = {
1134
+ mode: { type: "object-grammar", schema: jsonSchema },
1135
+ ...settings,
1136
+ inputFormat: validatedPrompt.type,
1137
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
1138
+ abortSignal
1139
+ };
1140
+ transformer = {
1141
+ transform: (chunk, controller) => {
1142
+ switch (chunk.type) {
1143
+ case "text-delta":
1144
+ controller.enqueue(chunk.textDelta);
1145
+ break;
1146
+ case "finish":
1147
+ case "error":
1148
+ controller.enqueue(chunk);
1149
+ break;
1150
+ }
1151
+ }
1152
+ };
1153
+ break;
1154
+ }
1155
+ case "tool": {
1156
+ const validatedPrompt = getValidatedPrompt({
1157
+ system,
1158
+ prompt,
1159
+ messages
1160
+ });
1161
+ callOptions = {
1162
+ mode: {
1163
+ type: "object-tool",
1164
+ tool: {
1165
+ type: "function",
1166
+ name: "json",
1167
+ description: "Respond with a JSON object.",
1168
+ parameters: jsonSchema
1169
+ }
1170
+ },
1171
+ ...settings,
1172
+ inputFormat: validatedPrompt.type,
1173
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
1174
+ abortSignal
1175
+ };
1176
+ transformer = {
1177
+ transform(chunk, controller) {
1178
+ switch (chunk.type) {
1179
+ case "tool-call-delta":
1180
+ controller.enqueue(chunk.argsTextDelta);
1181
+ break;
1182
+ case "finish":
1183
+ case "error":
1184
+ controller.enqueue(chunk);
1185
+ break;
1186
+ }
1187
+ }
1188
+ };
1189
+ break;
1190
+ }
1191
+ case void 0: {
1192
+ throw new Error("Model does not have a default object generation mode.");
1193
+ }
1194
+ default: {
1195
+ const _exhaustiveCheck = mode;
1196
+ throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
1197
+ }
1198
+ }
1199
+ const result = await retry(() => model.doStream(callOptions));
1200
+ return new StreamObjectResult({
1201
+ stream: result.stream.pipeThrough(new TransformStream(transformer)),
1202
+ warnings: result.warnings,
1203
+ rawResponse: result.rawResponse
1204
+ });
1205
+ }
1206
+ var StreamObjectResult = class {
1207
+ constructor({
1208
+ stream,
1209
+ warnings,
1210
+ rawResponse
1211
+ }) {
1212
+ this.originalStream = stream;
1213
+ this.warnings = warnings;
1214
+ this.rawResponse = rawResponse;
1215
+ }
1216
+ get partialObjectStream() {
1217
+ let accumulatedText = "";
1218
+ let latestObject = void 0;
1219
+ return createAsyncIterableStream(this.originalStream, {
1220
+ transform(chunk, controller) {
1221
+ if (typeof chunk === "string") {
1222
+ accumulatedText += chunk;
1223
+ const currentObject = parsePartialJson(
1224
+ accumulatedText
1225
+ );
1226
+ if (!isDeepEqualData(latestObject, currentObject)) {
1227
+ latestObject = currentObject;
1228
+ controller.enqueue(currentObject);
1229
+ }
1230
+ } else if (chunk.type === "error") {
1231
+ throw chunk.error;
1232
+ }
1233
+ }
1234
+ });
1235
+ }
1236
+ get fullStream() {
1237
+ let accumulatedText = "";
1238
+ let latestObject = void 0;
1239
+ return createAsyncIterableStream(this.originalStream, {
1240
+ transform(chunk, controller) {
1241
+ if (typeof chunk === "string") {
1242
+ accumulatedText += chunk;
1243
+ const currentObject = parsePartialJson(
1244
+ accumulatedText
1245
+ );
1246
+ if (!isDeepEqualData(latestObject, currentObject)) {
1247
+ latestObject = currentObject;
1248
+ controller.enqueue({ type: "object", object: currentObject });
1249
+ }
1250
+ } else {
1251
+ switch (chunk.type) {
1252
+ case "finish":
1253
+ controller.enqueue({
1254
+ ...chunk,
1255
+ usage: calculateTokenUsage(chunk.usage)
1256
+ });
1257
+ break;
1258
+ default:
1259
+ controller.enqueue(chunk);
1260
+ break;
1261
+ }
1262
+ }
1263
+ }
1264
+ });
1265
+ }
1266
+ };
1267
+ var experimental_streamObject = streamObject;
1268
+
1269
+ // core/generate-text/tool-call.ts
1270
+ var import_provider6 = require("@ai-sdk/provider");
1271
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
1272
+ function parseToolCall({
1273
+ toolCall,
1274
+ tools
1275
+ }) {
1276
+ const toolName = toolCall.toolName;
1277
+ if (tools == null) {
1278
+ throw new import_provider6.NoSuchToolError({ toolName: toolCall.toolName });
1279
+ }
1280
+ const tool2 = tools[toolName];
1281
+ if (tool2 == null) {
1282
+ throw new import_provider6.NoSuchToolError({
1283
+ toolName: toolCall.toolName,
1284
+ availableTools: Object.keys(tools)
1285
+ });
1286
+ }
1287
+ const parseResult = (0, import_provider_utils4.safeParseJSON)({
1288
+ text: toolCall.args,
1289
+ schema: tool2.parameters
1290
+ });
1291
+ if (parseResult.success === false) {
1292
+ throw new import_provider6.InvalidToolArgumentsError({
1293
+ toolName,
1294
+ toolArgs: toolCall.args,
1295
+ cause: parseResult.error
1296
+ });
1297
+ }
1298
+ return {
1299
+ type: "tool-call",
1300
+ toolCallId: toolCall.toolCallId,
1301
+ toolName,
1302
+ args: parseResult.value
1303
+ };
1304
+ }
1305
+
1306
+ // core/generate-text/generate-text.ts
1307
+ async function generateText({
1308
+ model,
1309
+ tools,
1310
+ system,
1311
+ prompt,
1312
+ messages,
1313
+ maxRetries,
1314
+ abortSignal,
1315
+ ...settings
1316
+ }) {
1317
+ var _a, _b;
1318
+ const retry = retryWithExponentialBackoff({ maxRetries });
1319
+ const validatedPrompt = getValidatedPrompt({ system, prompt, messages });
1320
+ const modelResponse = await retry(() => {
1321
+ return model.doGenerate({
1322
+ mode: {
1323
+ type: "regular",
1324
+ tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1325
+ type: "function",
1326
+ name,
1327
+ description: tool2.description,
1328
+ parameters: convertZodToJSONSchema(tool2.parameters)
1329
+ }))
1330
+ },
1331
+ ...prepareCallSettings(settings),
1332
+ inputFormat: validatedPrompt.type,
1333
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
1334
+ abortSignal
1335
+ });
1336
+ });
1337
+ const toolCalls = [];
1338
+ for (const modelToolCall of (_a = modelResponse.toolCalls) != null ? _a : []) {
1339
+ toolCalls.push(parseToolCall({ toolCall: modelToolCall, tools }));
1340
+ }
1341
+ const toolResults = tools == null ? [] : await executeTools({ toolCalls, tools });
1342
+ return new GenerateTextResult({
1343
+ // Always return a string so that the caller doesn't have to check for undefined.
1344
+ // If they need to check if the model did not return any text,
1345
+ // they can check the length of the string:
1346
+ text: (_b = modelResponse.text) != null ? _b : "",
1347
+ toolCalls,
1348
+ toolResults,
1349
+ finishReason: modelResponse.finishReason,
1350
+ usage: calculateTokenUsage(modelResponse.usage),
1351
+ warnings: modelResponse.warnings,
1352
+ rawResponse: modelResponse.rawResponse,
1353
+ logprobs: modelResponse.logprobs
1354
+ });
1355
+ }
1356
+ async function executeTools({
1357
+ toolCalls,
1358
+ tools
1359
+ }) {
1360
+ const toolResults = await Promise.all(
1361
+ toolCalls.map(async (toolCall) => {
1362
+ const tool2 = tools[toolCall.toolName];
1363
+ if ((tool2 == null ? void 0 : tool2.execute) == null) {
1364
+ return void 0;
1365
+ }
1366
+ const result = await tool2.execute(toolCall.args);
1367
+ return {
1368
+ toolCallId: toolCall.toolCallId,
1369
+ toolName: toolCall.toolName,
1370
+ args: toolCall.args,
1371
+ result
1372
+ };
1373
+ })
1374
+ );
1375
+ return toolResults.filter(
1376
+ (result) => result != null
1377
+ );
1378
+ }
1379
+ var GenerateTextResult = class {
1380
+ constructor(options) {
1381
+ this.text = options.text;
1382
+ this.toolCalls = options.toolCalls;
1383
+ this.toolResults = options.toolResults;
1384
+ this.finishReason = options.finishReason;
1385
+ this.usage = options.usage;
1386
+ this.warnings = options.warnings;
1387
+ this.rawResponse = options.rawResponse;
1388
+ this.logprobs = options.logprobs;
1389
+ }
1390
+ };
1391
+ var experimental_generateText = generateText;
1392
+
1393
+ // core/generate-text/run-tools-transformation.ts
1394
+ var import_provider7 = require("@ai-sdk/provider");
1395
+
1396
+ // shared/generate-id.ts
1397
+ var import_non_secure = require("nanoid/non-secure");
1398
+ var generateId = (0, import_non_secure.customAlphabet)(
1399
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1400
+ 7
1401
+ );
1402
+
1403
+ // core/generate-text/run-tools-transformation.ts
1404
+ function runToolsTransformation({
1405
+ tools,
1406
+ generatorStream
1407
+ }) {
1408
+ let canClose = false;
1409
+ const outstandingToolCalls = /* @__PURE__ */ new Set();
1410
+ let toolResultsStreamController = null;
1411
+ const toolResultsStream = new ReadableStream({
1412
+ start(controller) {
1413
+ toolResultsStreamController = controller;
1414
+ }
1415
+ });
1416
+ const forwardStream = new TransformStream({
1417
+ transform(chunk, controller) {
1418
+ const chunkType = chunk.type;
1419
+ switch (chunkType) {
1420
+ case "text-delta":
1421
+ case "error": {
1422
+ controller.enqueue(chunk);
1423
+ break;
1424
+ }
1425
+ case "tool-call": {
1426
+ const toolName = chunk.toolName;
1427
+ if (tools == null) {
1428
+ toolResultsStreamController.enqueue({
1429
+ type: "error",
1430
+ error: new import_provider7.NoSuchToolError({ toolName: chunk.toolName })
1431
+ });
1432
+ break;
1433
+ }
1434
+ const tool2 = tools[toolName];
1435
+ if (tool2 == null) {
1436
+ toolResultsStreamController.enqueue({
1437
+ type: "error",
1438
+ error: new import_provider7.NoSuchToolError({
1439
+ toolName: chunk.toolName,
1440
+ availableTools: Object.keys(tools)
1441
+ })
1442
+ });
1443
+ break;
1444
+ }
1445
+ try {
1446
+ const toolCall = parseToolCall({
1447
+ toolCall: chunk,
1448
+ tools
1449
+ });
1450
+ controller.enqueue(toolCall);
1451
+ if (tool2.execute != null) {
1452
+ const toolExecutionId = generateId();
1453
+ outstandingToolCalls.add(toolExecutionId);
1454
+ tool2.execute(toolCall.args).then(
1455
+ (result) => {
1456
+ toolResultsStreamController.enqueue({
1457
+ ...toolCall,
1458
+ type: "tool-result",
1459
+ result
1460
+ });
1461
+ outstandingToolCalls.delete(toolExecutionId);
1462
+ if (canClose && outstandingToolCalls.size === 0) {
1463
+ toolResultsStreamController.close();
1464
+ }
1465
+ },
1466
+ (error) => {
1467
+ toolResultsStreamController.enqueue({
1468
+ type: "error",
1469
+ error
1470
+ });
1471
+ outstandingToolCalls.delete(toolExecutionId);
1472
+ if (canClose && outstandingToolCalls.size === 0) {
1473
+ toolResultsStreamController.close();
1474
+ }
1475
+ }
1476
+ );
1477
+ }
1478
+ } catch (error) {
1479
+ toolResultsStreamController.enqueue({
1480
+ type: "error",
1481
+ error
1482
+ });
1483
+ }
1484
+ break;
1485
+ }
1486
+ case "finish": {
1487
+ controller.enqueue({
1488
+ type: "finish",
1489
+ finishReason: chunk.finishReason,
1490
+ logprobs: chunk.logprobs,
1491
+ usage: calculateTokenUsage(chunk.usage)
1492
+ });
1493
+ break;
1494
+ }
1495
+ case "tool-call-delta": {
1496
+ break;
1497
+ }
1498
+ default: {
1499
+ const _exhaustiveCheck = chunkType;
1500
+ throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
1501
+ }
1502
+ }
1503
+ },
1504
+ flush() {
1505
+ canClose = true;
1506
+ if (outstandingToolCalls.size === 0) {
1507
+ toolResultsStreamController.close();
1508
+ }
1509
+ }
1510
+ });
1511
+ return new ReadableStream({
1512
+ async start(controller) {
1513
+ return Promise.all([
1514
+ generatorStream.pipeThrough(forwardStream).pipeTo(
1515
+ new WritableStream({
1516
+ write(chunk) {
1517
+ controller.enqueue(chunk);
1518
+ },
1519
+ close() {
1520
+ }
1521
+ })
1522
+ ),
1523
+ toolResultsStream.pipeTo(
1524
+ new WritableStream({
1525
+ write(chunk) {
1526
+ controller.enqueue(chunk);
1527
+ },
1528
+ close() {
1529
+ controller.close();
1530
+ }
1531
+ })
1532
+ )
1533
+ ]);
1534
+ }
1535
+ });
1536
+ }
1537
+
1538
+ // core/generate-text/stream-text.ts
1539
+ async function streamText({
1540
+ model,
1541
+ tools,
1542
+ system,
1543
+ prompt,
1544
+ messages,
1545
+ maxRetries,
1546
+ abortSignal,
1547
+ ...settings
1548
+ }) {
1549
+ const retry = retryWithExponentialBackoff({ maxRetries });
1550
+ const validatedPrompt = getValidatedPrompt({ system, prompt, messages });
1551
+ const { stream, warnings, rawResponse } = await retry(
1552
+ () => model.doStream({
1553
+ mode: {
1554
+ type: "regular",
1555
+ tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1556
+ type: "function",
1557
+ name,
1558
+ description: tool2.description,
1559
+ parameters: convertZodToJSONSchema(tool2.parameters)
1560
+ }))
1561
+ },
1562
+ ...prepareCallSettings(settings),
1563
+ inputFormat: validatedPrompt.type,
1564
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
1565
+ abortSignal
1566
+ })
1567
+ );
1568
+ return new StreamTextResult({
1569
+ stream: runToolsTransformation({
1570
+ tools,
1571
+ generatorStream: stream
1572
+ }),
1573
+ warnings,
1574
+ rawResponse
1575
+ });
1576
+ }
1577
+ async function streamResponse({
1578
+ model,
1579
+ tools,
1580
+ system,
1581
+ prompt,
1582
+ messages,
1583
+ maxRetries,
1584
+ abortSignal,
1585
+ ...settings
1586
+ }) {
1587
+ const retry = retryWithExponentialBackoff({ maxRetries });
1588
+ const validatedPrompt = getValidatedPrompt({ system, prompt, messages });
1589
+ const { stream, warnings, rawResponse } = await retry(() => {
1590
+ if (!model.doRawStream) {
1591
+ throw new Error("The model does not support raw streaming.");
1592
+ }
1593
+ return model.doRawStream({
1594
+ mode: {
1595
+ type: "regular",
1596
+ tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1597
+ type: "function",
1598
+ name,
1599
+ description: tool2.description,
1600
+ parameters: convertZodToJSONSchema(tool2.parameters)
1601
+ }))
1602
+ },
1603
+ ...prepareCallSettings(settings),
1604
+ inputFormat: validatedPrompt.type,
1605
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
1606
+ abortSignal
1607
+ });
1608
+ });
1609
+ return { stream, warnings, rawResponse };
1610
+ }
1611
+ var StreamTextResult = class {
1612
+ constructor({
1613
+ stream,
1614
+ warnings,
1615
+ rawResponse
1616
+ }) {
1617
+ this.warnings = warnings;
1618
+ this.rawResponse = rawResponse;
1619
+ let resolveUsage;
1620
+ this.usage = new Promise((resolve) => {
1621
+ resolveUsage = resolve;
1622
+ });
1623
+ let resolveFinishReason;
1624
+ this.finishReason = new Promise((resolve) => {
1625
+ resolveFinishReason = resolve;
1626
+ });
1627
+ this.originalStream = stream.pipeThrough(
1628
+ new TransformStream({
1629
+ async transform(chunk, controller) {
1630
+ controller.enqueue(chunk);
1631
+ if (chunk.type === "finish") {
1632
+ resolveUsage(chunk.usage);
1633
+ resolveFinishReason(chunk.finishReason);
1634
+ }
1635
+ }
1636
+ })
1637
+ );
1638
+ }
1639
+ /**
1640
+ Split out a new stream from the original stream.
1641
+ The original stream is replaced to allow for further splitting,
1642
+ since we do not know how many times the stream will be split.
1643
+
1644
+ Note: this leads to buffering the stream content on the server.
1645
+ However, the LLM results are expected to be small enough to not cause issues.
1646
+ */
1647
+ teeStream() {
1648
+ const [stream1, stream2] = this.originalStream.tee();
1649
+ this.originalStream = stream2;
1650
+ return stream1;
1651
+ }
1652
+ /**
1653
+ A text stream that returns only the generated text deltas. You can use it
1654
+ as either an AsyncIterable or a ReadableStream. When an error occurs, the
1655
+ stream will throw the error.
1656
+ */
1657
+ get textStream() {
1658
+ return createAsyncIterableStream(this.teeStream(), {
1659
+ transform(chunk, controller) {
1660
+ if (chunk.type === "text-delta") {
1661
+ if (chunk.textDelta.length > 0) {
1662
+ controller.enqueue(chunk.textDelta);
1663
+ }
1664
+ } else if (chunk.type === "error") {
1665
+ throw chunk.error;
1666
+ }
1667
+ }
1668
+ });
1669
+ }
1670
+ /**
1671
+ A stream with all events, including text deltas, tool calls, tool results, and
1672
+ errors.
1673
+ You can use it as either an AsyncIterable or a ReadableStream. When an error occurs, the
1674
+ stream will throw the error.
1675
+ */
1676
+ get fullStream() {
1677
+ return createAsyncIterableStream(this.teeStream(), {
1678
+ transform(chunk, controller) {
1679
+ if (chunk.type === "text-delta") {
1680
+ if (chunk.textDelta.length > 0) {
1681
+ controller.enqueue(chunk);
1682
+ }
1683
+ } else {
1684
+ controller.enqueue(chunk);
1685
+ }
1686
+ }
1687
+ });
1688
+ }
1689
+ /**
1690
+ Converts the result to an `AIStream` object that is compatible with `StreamingTextResponse`.
1691
+ It can be used with the `useChat` and `useCompletion` hooks.
1692
+
1693
+ @param callbacks
1694
+ Stream callbacks that will be called when the stream emits events.
1695
+
1696
+ @returns an `AIStream` object.
1697
+ */
1698
+ toAIStream(callbacks = {}) {
1699
+ let aggregatedResponse = "";
1700
+ const callbackTransformer = new TransformStream({
1701
+ async start() {
1702
+ if (callbacks.onStart)
1703
+ await callbacks.onStart();
1704
+ },
1705
+ async transform(chunk, controller) {
1706
+ controller.enqueue(chunk);
1707
+ if (chunk.type === "text-delta") {
1708
+ const textDelta = chunk.textDelta;
1709
+ aggregatedResponse += textDelta;
1710
+ if (callbacks.onToken)
1711
+ await callbacks.onToken(textDelta);
1712
+ if (callbacks.onText)
1713
+ await callbacks.onText(textDelta);
1714
+ }
1715
+ },
1716
+ async flush() {
1717
+ if (callbacks.onCompletion)
1718
+ await callbacks.onCompletion(aggregatedResponse);
1719
+ if (callbacks.onFinal)
1720
+ await callbacks.onFinal(aggregatedResponse);
1721
+ }
1722
+ });
1723
+ const streamDataTransformer = new TransformStream({
1724
+ transform: async (chunk, controller) => {
1725
+ switch (chunk.type) {
1726
+ case "text-delta":
1727
+ controller.enqueue(formatStreamPart("text", chunk.textDelta));
1728
+ break;
1729
+ case "tool-call":
1730
+ controller.enqueue(
1731
+ formatStreamPart("tool_call", {
1732
+ toolCallId: chunk.toolCallId,
1733
+ toolName: chunk.toolName,
1734
+ args: chunk.args
1735
+ })
1736
+ );
1737
+ break;
1738
+ case "tool-result":
1739
+ controller.enqueue(
1740
+ formatStreamPart("tool_result", {
1741
+ toolCallId: chunk.toolCallId,
1742
+ toolName: chunk.toolName,
1743
+ args: chunk.args,
1744
+ result: chunk.result
1745
+ })
1746
+ );
1747
+ break;
1748
+ case "error":
1749
+ controller.enqueue(
1750
+ formatStreamPart("error", JSON.stringify(chunk.error))
1751
+ );
1752
+ break;
1753
+ }
1754
+ }
1755
+ });
1756
+ return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamDataTransformer).pipeThrough(new TextEncoderStream());
1757
+ }
1758
+ /**
1759
+ Writes stream data output to a Node.js response-like object.
1760
+ It sets a `Content-Type` header to `text/plain; charset=utf-8` and
1761
+ writes each stream data part as a separate chunk.
1762
+
1763
+ @param response A Node.js response-like object (ServerResponse).
1764
+ @param init Optional headers and status code.
1765
+ */
1766
+ pipeAIStreamToResponse(response, init) {
1767
+ var _a;
1768
+ response.writeHead((_a = init == null ? void 0 : init.status) != null ? _a : 200, {
1769
+ "Content-Type": "text/plain; charset=utf-8",
1770
+ ...init == null ? void 0 : init.headers
1771
+ });
1772
+ const reader = this.toAIStream().getReader();
1773
+ const read = async () => {
1774
+ try {
1775
+ while (true) {
1776
+ const { done, value } = await reader.read();
1777
+ if (done)
1778
+ break;
1779
+ response.write(value);
1780
+ }
1781
+ } catch (error) {
1782
+ throw error;
1783
+ } finally {
1784
+ response.end();
1785
+ }
1786
+ };
1787
+ read();
1788
+ }
1789
+ /**
1790
+ Writes text delta output to a Node.js response-like object.
1791
+ It sets a `Content-Type` header to `text/plain; charset=utf-8` and
1792
+ writes each text delta as a separate chunk.
1793
+
1794
+ @param response A Node.js response-like object (ServerResponse).
1795
+ @param init Optional headers and status code.
1796
+ */
1797
+ pipeTextStreamToResponse(response, init) {
1798
+ var _a;
1799
+ response.writeHead((_a = init == null ? void 0 : init.status) != null ? _a : 200, {
1800
+ "Content-Type": "text/plain; charset=utf-8",
1801
+ ...init == null ? void 0 : init.headers
1802
+ });
1803
+ const reader = this.textStream.pipeThrough(new TextEncoderStream()).getReader();
1804
+ const read = async () => {
1805
+ try {
1806
+ while (true) {
1807
+ const { done, value } = await reader.read();
1808
+ if (done)
1809
+ break;
1810
+ response.write(value);
1811
+ }
1812
+ } catch (error) {
1813
+ throw error;
1814
+ } finally {
1815
+ response.end();
1816
+ }
1817
+ };
1818
+ read();
1819
+ }
1820
+ /**
1821
+ Converts the result to a streamed response object with a stream data part stream.
1822
+ It can be used with the `useChat` and `useCompletion` hooks.
1823
+
1824
+ @param init Optional headers.
1825
+
1826
+ @return A response object.
1827
+ */
1828
+ toAIStreamResponse(init) {
1829
+ return new StreamingTextResponse(this.toAIStream(), init);
1830
+ }
1831
+ /**
1832
+ Creates a simple text stream response.
1833
+ Each text delta is encoded as UTF-8 and sent as a separate chunk.
1834
+ Non-text-delta events are ignored.
1835
+
1836
+ @param init Optional headers and status code.
1837
+ */
1838
+ toTextStreamResponse(init) {
1839
+ var _a;
1840
+ return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
1841
+ status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
1842
+ headers: {
1843
+ "Content-Type": "text/plain; charset=utf-8",
1844
+ ...init == null ? void 0 : init.headers
1845
+ }
1846
+ });
1847
+ }
1848
+ };
1849
+ var experimental_streamText = streamText;
1850
+
1851
+ // core/prompt/convert-to-core-messages.ts
1852
+ function convertToCoreMessages(messages) {
1853
+ const coreMessages = [];
1854
+ for (const { role, content, toolInvocations } of messages) {
1855
+ switch (role) {
1856
+ case "user": {
1857
+ coreMessages.push({ role: "user", content });
1858
+ break;
1859
+ }
1860
+ case "assistant": {
1861
+ if (toolInvocations == null) {
1862
+ coreMessages.push({ role: "assistant", content });
1863
+ break;
1864
+ }
1865
+ coreMessages.push({
1866
+ role: "assistant",
1867
+ content: [
1868
+ { type: "text", text: content },
1869
+ ...toolInvocations.map(({ toolCallId, toolName, args }) => ({
1870
+ type: "tool-call",
1871
+ toolCallId,
1872
+ toolName,
1873
+ args
1874
+ }))
1875
+ ]
1876
+ });
1877
+ coreMessages.push({
1878
+ role: "tool",
1879
+ content: toolInvocations.map(
1880
+ ({ toolCallId, toolName, args, result }) => ({
1881
+ type: "tool-result",
1882
+ toolCallId,
1883
+ toolName,
1884
+ args,
1885
+ result
1886
+ })
1887
+ )
1888
+ });
1889
+ break;
1890
+ }
1891
+ default: {
1892
+ const _exhaustiveCheck = role;
1893
+ throw new Error(`Unhandled role: ${_exhaustiveCheck}`);
1894
+ }
1895
+ }
1896
+ }
1897
+ return coreMessages;
1898
+ }
1899
+
1900
+ // core/tool/tool.ts
1901
+ function tool(tool2) {
1902
+ return tool2;
1903
+ }
1904
+
1905
+ // core/types/errors.ts
1906
+ var import_provider8 = require("@ai-sdk/provider");
1907
+
1908
+ // shared/stream-parts.ts
1909
+ var textStreamPart = {
1910
+ code: "0",
1911
+ name: "text",
1912
+ parse: (value) => {
1913
+ if (typeof value !== "string") {
1914
+ throw new Error('"text" parts expect a string value.');
1915
+ }
1916
+ return { type: "text", value };
1917
+ }
1918
+ };
1919
+ var functionCallStreamPart = {
1920
+ code: "1",
1921
+ name: "function_call",
1922
+ parse: (value) => {
1923
+ 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") {
1924
+ throw new Error(
1925
+ '"function_call" parts expect an object with a "function_call" property.'
1926
+ );
1927
+ }
1928
+ return {
1929
+ type: "function_call",
1930
+ value
1931
+ };
1932
+ }
1933
+ };
1934
+ var dataStreamPart = {
1935
+ code: "2",
1936
+ name: "data",
1937
+ parse: (value) => {
1938
+ if (!Array.isArray(value)) {
1939
+ throw new Error('"data" parts expect an array value.');
1940
+ }
1941
+ return { type: "data", value };
1942
+ }
1943
+ };
1944
+ var errorStreamPart = {
1945
+ code: "3",
1946
+ name: "error",
1947
+ parse: (value) => {
1948
+ if (typeof value !== "string") {
1949
+ throw new Error('"error" parts expect a string value.');
1950
+ }
1951
+ return { type: "error", value };
1952
+ }
1953
+ };
1954
+ var assistantMessageStreamPart = {
1955
+ code: "4",
1956
+ name: "assistant_message",
1957
+ parse: (value) => {
1958
+ 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(
1959
+ (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"
1960
+ )) {
1961
+ throw new Error(
1962
+ '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
1963
+ );
1964
+ }
1965
+ return {
1966
+ type: "assistant_message",
1967
+ value
1968
+ };
1969
+ }
1970
+ };
1971
+ var assistantControlDataStreamPart = {
1972
+ code: "5",
1973
+ name: "assistant_control_data",
1974
+ parse: (value) => {
1975
+ if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
1976
+ throw new Error(
1977
+ '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
1978
+ );
1979
+ }
1980
+ return {
1981
+ type: "assistant_control_data",
1982
+ value: {
1983
+ threadId: value.threadId,
1984
+ messageId: value.messageId
1985
+ }
1986
+ };
1987
+ }
1988
+ };
1989
+ var dataMessageStreamPart = {
1990
+ code: "6",
1991
+ name: "data_message",
1992
+ parse: (value) => {
1993
+ if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
1994
+ throw new Error(
1995
+ '"data_message" parts expect an object with a "role" and "data" property.'
1996
+ );
1997
+ }
1998
+ return {
1999
+ type: "data_message",
2000
+ value
2001
+ };
2002
+ }
2003
+ };
2004
+ var toolCallsStreamPart = {
2005
+ code: "7",
2006
+ name: "tool_calls",
2007
+ parse: (value) => {
2008
+ 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(
2009
+ (tc) => 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"
2010
+ )) {
2011
+ throw new Error(
2012
+ '"tool_calls" parts expect an object with a ToolCallPayload.'
2013
+ );
2014
+ }
2015
+ return {
2016
+ type: "tool_calls",
2017
+ value
2018
+ };
2019
+ }
2020
+ };
2021
+ var messageAnnotationsStreamPart = {
2022
+ code: "8",
2023
+ name: "message_annotations",
2024
+ parse: (value) => {
2025
+ if (!Array.isArray(value)) {
2026
+ throw new Error('"message_annotations" parts expect an array value.');
2027
+ }
2028
+ return { type: "message_annotations", value };
2029
+ }
2030
+ };
2031
+ var toolCallStreamPart = {
2032
+ code: "9",
2033
+ name: "tool_call",
2034
+ parse: (value) => {
2035
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
2036
+ throw new Error(
2037
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
2038
+ );
2039
+ }
2040
+ return {
2041
+ type: "tool_call",
2042
+ value
2043
+ };
2044
+ }
2045
+ };
2046
+ var toolResultStreamPart = {
2047
+ code: "a",
2048
+ name: "tool_result",
2049
+ parse: (value) => {
2050
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
2051
+ throw new Error(
2052
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
2053
+ );
2054
+ }
2055
+ return {
2056
+ type: "tool_result",
2057
+ value
2058
+ };
2059
+ }
2060
+ };
2061
+ var streamParts = [
2062
+ textStreamPart,
2063
+ functionCallStreamPart,
2064
+ dataStreamPart,
2065
+ errorStreamPart,
2066
+ assistantMessageStreamPart,
2067
+ assistantControlDataStreamPart,
2068
+ dataMessageStreamPart,
2069
+ toolCallsStreamPart,
2070
+ messageAnnotationsStreamPart,
2071
+ toolCallStreamPart,
2072
+ toolResultStreamPart
2073
+ ];
2074
+ var streamPartsByCode = {
2075
+ [textStreamPart.code]: textStreamPart,
2076
+ [functionCallStreamPart.code]: functionCallStreamPart,
2077
+ [dataStreamPart.code]: dataStreamPart,
2078
+ [errorStreamPart.code]: errorStreamPart,
2079
+ [assistantMessageStreamPart.code]: assistantMessageStreamPart,
2080
+ [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
2081
+ [dataMessageStreamPart.code]: dataMessageStreamPart,
2082
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
2083
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
2084
+ [toolCallStreamPart.code]: toolCallStreamPart,
2085
+ [toolResultStreamPart.code]: toolResultStreamPart
2086
+ };
2087
+ var StreamStringPrefixes = {
2088
+ [textStreamPart.name]: textStreamPart.code,
2089
+ [functionCallStreamPart.name]: functionCallStreamPart.code,
2090
+ [dataStreamPart.name]: dataStreamPart.code,
2091
+ [errorStreamPart.name]: errorStreamPart.code,
2092
+ [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
2093
+ [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
2094
+ [dataMessageStreamPart.name]: dataMessageStreamPart.code,
2095
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
2096
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
2097
+ [toolCallStreamPart.name]: toolCallStreamPart.code,
2098
+ [toolResultStreamPart.name]: toolResultStreamPart.code
2099
+ };
2100
+ var validCodes = streamParts.map((part) => part.code);
2101
+ var parseStreamPart = (line) => {
2102
+ const firstSeparatorIndex = line.indexOf(":");
2103
+ if (firstSeparatorIndex === -1) {
2104
+ throw new Error("Failed to parse stream string. No separator found.");
2105
+ }
2106
+ const prefix = line.slice(0, firstSeparatorIndex);
2107
+ if (!validCodes.includes(prefix)) {
2108
+ throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
2109
+ }
2110
+ const code = prefix;
2111
+ const textValue = line.slice(firstSeparatorIndex + 1);
2112
+ const jsonValue = JSON.parse(textValue);
2113
+ return streamPartsByCode[code].parse(jsonValue);
2114
+ };
2115
+ function formatStreamPart(type, value) {
2116
+ const streamPart = streamParts.find((part) => part.name === type);
2117
+ if (!streamPart) {
2118
+ throw new Error(`Invalid stream part type: ${type}`);
2119
+ }
2120
+ return `${streamPart.code}:${JSON.stringify(value)}
2121
+ `;
2122
+ }
2123
+
2124
+ // shared/read-data-stream.ts
2125
+ var NEWLINE = "\n".charCodeAt(0);
2126
+ function concatChunks(chunks, totalLength) {
2127
+ const concatenatedChunks = new Uint8Array(totalLength);
2128
+ let offset = 0;
2129
+ for (const chunk of chunks) {
2130
+ concatenatedChunks.set(chunk, offset);
2131
+ offset += chunk.length;
2132
+ }
2133
+ chunks.length = 0;
2134
+ return concatenatedChunks;
2135
+ }
2136
+ async function* readDataStream(reader, {
2137
+ isAborted
2138
+ } = {}) {
2139
+ const decoder = new TextDecoder();
2140
+ const chunks = [];
2141
+ let totalLength = 0;
2142
+ while (true) {
2143
+ const { value } = await reader.read();
2144
+ if (value) {
2145
+ chunks.push(value);
2146
+ totalLength += value.length;
2147
+ if (value[value.length - 1] !== NEWLINE) {
2148
+ continue;
2149
+ }
2150
+ }
2151
+ if (chunks.length === 0) {
2152
+ break;
2153
+ }
2154
+ const concatenatedChunks = concatChunks(chunks, totalLength);
2155
+ totalLength = 0;
2156
+ const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
2157
+ for (const streamPart of streamParts2) {
2158
+ yield streamPart;
2159
+ }
2160
+ if (isAborted == null ? void 0 : isAborted()) {
2161
+ reader.cancel();
2162
+ break;
2163
+ }
2164
+ }
2165
+ }
2166
+
2167
+ // shared/utils.ts
2168
+ function createChunkDecoder(complex) {
2169
+ const decoder = new TextDecoder();
2170
+ if (!complex) {
2171
+ return function(chunk) {
2172
+ if (!chunk)
2173
+ return "";
2174
+ return decoder.decode(chunk, { stream: true });
2175
+ };
2176
+ }
2177
+ return function(chunk) {
2178
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
2179
+ return decoded.map(parseStreamPart).filter(Boolean);
2180
+ };
2181
+ }
2182
+ var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
2183
+
2184
+ // streams/ai-stream.ts
2185
+ var import_eventsource_parser = require("eventsource-parser");
2186
+ function createEventStreamTransformer(customParser) {
2187
+ const textDecoder = new TextDecoder();
2188
+ let eventSourceParser;
2189
+ return new TransformStream({
2190
+ async start(controller) {
2191
+ eventSourceParser = (0, import_eventsource_parser.createParser)(
2192
+ (event) => {
2193
+ if ("data" in event && event.type === "event" && event.data === "[DONE]" || // Replicate doesn't send [DONE] but does send a 'done' event
2194
+ // @see https://replicate.com/docs/streaming
2195
+ event.event === "done") {
2196
+ controller.terminate();
2197
+ return;
2198
+ }
2199
+ if ("data" in event) {
2200
+ const parsedMessage = customParser ? customParser(event.data, {
2201
+ event: event.event
2202
+ }) : event.data;
2203
+ if (parsedMessage)
2204
+ controller.enqueue(parsedMessage);
2205
+ }
2206
+ }
2207
+ );
2208
+ },
2209
+ transform(chunk) {
2210
+ eventSourceParser.feed(textDecoder.decode(chunk));
2211
+ }
2212
+ });
2213
+ }
2214
+ function createCallbacksTransformer(cb) {
2215
+ const textEncoder = new TextEncoder();
2216
+ let aggregatedResponse = "";
2217
+ const callbacks = cb || {};
2218
+ return new TransformStream({
2219
+ async start() {
2220
+ if (callbacks.onStart)
2221
+ await callbacks.onStart();
2222
+ },
2223
+ async transform(message, controller) {
2224
+ const content = typeof message === "string" ? message : message.content;
2225
+ controller.enqueue(textEncoder.encode(content));
2226
+ aggregatedResponse += content;
2227
+ if (callbacks.onToken)
2228
+ await callbacks.onToken(content);
2229
+ if (callbacks.onText && typeof message === "string") {
2230
+ await callbacks.onText(message);
2231
+ }
2232
+ },
2233
+ async flush() {
2234
+ const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
2235
+ if (callbacks.onCompletion) {
2236
+ await callbacks.onCompletion(aggregatedResponse);
2237
+ }
2238
+ if (callbacks.onFinal && !isOpenAICallbacks) {
2239
+ await callbacks.onFinal(aggregatedResponse);
2240
+ }
2241
+ }
2242
+ });
2243
+ }
2244
+ function isOfTypeOpenAIStreamCallbacks(callbacks) {
2245
+ return "experimental_onFunctionCall" in callbacks;
2246
+ }
2247
+ function trimStartOfStreamHelper() {
2248
+ let isStreamStart = true;
2249
+ return (text) => {
2250
+ if (isStreamStart) {
2251
+ text = text.trimStart();
2252
+ if (text)
2253
+ isStreamStart = false;
2254
+ }
2255
+ return text;
2256
+ };
2257
+ }
2258
+ function AIStream(response, customParser, callbacks) {
2259
+ if (!response.ok) {
2260
+ if (response.body) {
2261
+ const reader = response.body.getReader();
2262
+ return new ReadableStream({
2263
+ async start(controller) {
2264
+ const { done, value } = await reader.read();
2265
+ if (!done) {
2266
+ const errorText = new TextDecoder().decode(value);
2267
+ controller.error(new Error(`Response error: ${errorText}`));
2268
+ }
2269
+ }
2270
+ });
2271
+ } else {
2272
+ return new ReadableStream({
2273
+ start(controller) {
2274
+ controller.error(new Error("Response error: No response body"));
2275
+ }
2276
+ });
2277
+ }
2278
+ }
2279
+ const responseBodyStream = response.body || createEmptyReadableStream();
2280
+ return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
2281
+ }
2282
+ function createEmptyReadableStream() {
2283
+ return new ReadableStream({
2284
+ start(controller) {
2285
+ controller.close();
2286
+ }
2287
+ });
2288
+ }
2289
+ function readableFromAsyncIterable(iterable) {
2290
+ let it = iterable[Symbol.asyncIterator]();
2291
+ return new ReadableStream({
2292
+ async pull(controller) {
2293
+ const { done, value } = await it.next();
2294
+ if (done)
2295
+ controller.close();
2296
+ else
2297
+ controller.enqueue(value);
2298
+ },
2299
+ async cancel(reason) {
2300
+ var _a;
2301
+ await ((_a = it.return) == null ? void 0 : _a.call(it, reason));
2302
+ }
2303
+ });
2304
+ }
2305
+
2306
+ // streams/stream-data.ts
2307
+ var StreamData = class {
2308
+ constructor() {
2309
+ this.encoder = new TextEncoder();
2310
+ this.controller = null;
2311
+ this.isClosed = false;
2312
+ this.warningTimeout = null;
2313
+ const self = this;
2314
+ this.stream = new ReadableStream({
2315
+ start: async (controller) => {
2316
+ self.controller = controller;
2317
+ if (process.env.NODE_ENV === "development") {
2318
+ self.warningTimeout = setTimeout(() => {
2319
+ console.warn(
2320
+ "The data stream is hanging. Did you forget to close it with `data.close()`?"
2321
+ );
2322
+ }, 3e3);
2323
+ }
2324
+ },
2325
+ pull: (controller) => {
2326
+ },
2327
+ cancel: (reason) => {
2328
+ this.isClosed = true;
2329
+ }
2330
+ });
2331
+ }
2332
+ async close() {
2333
+ if (this.isClosed) {
2334
+ throw new Error("Data Stream has already been closed.");
2335
+ }
2336
+ if (!this.controller) {
2337
+ throw new Error("Stream controller is not initialized.");
2338
+ }
2339
+ this.controller.close();
2340
+ this.isClosed = true;
2341
+ if (this.warningTimeout) {
2342
+ clearTimeout(this.warningTimeout);
2343
+ }
2344
+ }
2345
+ append(value) {
2346
+ if (this.isClosed) {
2347
+ throw new Error("Data Stream has already been closed.");
2348
+ }
2349
+ if (!this.controller) {
2350
+ throw new Error("Stream controller is not initialized.");
2351
+ }
2352
+ this.controller.enqueue(
2353
+ this.encoder.encode(formatStreamPart("data", [value]))
2354
+ );
2355
+ }
2356
+ appendMessageAnnotation(value) {
2357
+ if (this.isClosed) {
2358
+ throw new Error("Data Stream has already been closed.");
2359
+ }
2360
+ if (!this.controller) {
2361
+ throw new Error("Stream controller is not initialized.");
2362
+ }
2363
+ this.controller.enqueue(
2364
+ this.encoder.encode(formatStreamPart("message_annotations", [value]))
2365
+ );
2366
+ }
2367
+ };
2368
+ function createStreamDataTransformer() {
2369
+ const encoder = new TextEncoder();
2370
+ const decoder = new TextDecoder();
2371
+ return new TransformStream({
2372
+ transform: async (chunk, controller) => {
2373
+ const message = decoder.decode(chunk);
2374
+ controller.enqueue(encoder.encode(formatStreamPart("text", message)));
2375
+ }
2376
+ });
2377
+ }
2378
+ var experimental_StreamData = class extends StreamData {
2379
+ };
2380
+
2381
+ // streams/anthropic-stream.ts
2382
+ function parseAnthropicStream() {
2383
+ let previous = "";
2384
+ return (data) => {
2385
+ const json = JSON.parse(data);
2386
+ if ("error" in json) {
2387
+ throw new Error(`${json.error.type}: ${json.error.message}`);
2388
+ }
2389
+ if (!("completion" in json)) {
2390
+ return;
2391
+ }
2392
+ const text = json.completion;
2393
+ if (!previous || text.length > previous.length && text.startsWith(previous)) {
2394
+ const delta = text.slice(previous.length);
2395
+ previous = text;
2396
+ return delta;
2397
+ }
2398
+ return text;
2399
+ };
2400
+ }
2401
+ async function* streamable(stream) {
2402
+ for await (const chunk of stream) {
2403
+ if ("completion" in chunk) {
2404
+ const text = chunk.completion;
2405
+ if (text)
2406
+ yield text;
2407
+ } else if ("delta" in chunk) {
2408
+ const { delta } = chunk;
2409
+ if ("text" in delta) {
2410
+ const text = delta.text;
2411
+ if (text)
2412
+ yield text;
2413
+ }
2414
+ }
2415
+ }
2416
+ }
2417
+ function AnthropicStream(res, cb) {
2418
+ if (Symbol.asyncIterator in res) {
2419
+ return readableFromAsyncIterable(streamable(res)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer());
2420
+ } else {
2421
+ return AIStream(res, parseAnthropicStream(), cb).pipeThrough(
2422
+ createStreamDataTransformer()
2423
+ );
2424
+ }
2425
+ }
2426
+
2427
+ // streams/assistant-response.ts
2428
+ function AssistantResponse({ threadId, messageId }, process2) {
2429
+ const stream = new ReadableStream({
2430
+ async start(controller) {
2431
+ var _a;
2432
+ const textEncoder = new TextEncoder();
2433
+ const sendMessage = (message) => {
2434
+ controller.enqueue(
2435
+ textEncoder.encode(formatStreamPart("assistant_message", message))
2436
+ );
2437
+ };
2438
+ const sendDataMessage = (message) => {
2439
+ controller.enqueue(
2440
+ textEncoder.encode(formatStreamPart("data_message", message))
2441
+ );
2442
+ };
2443
+ const sendError = (errorMessage) => {
2444
+ controller.enqueue(
2445
+ textEncoder.encode(formatStreamPart("error", errorMessage))
2446
+ );
2447
+ };
2448
+ const forwardStream = async (stream2) => {
2449
+ var _a2, _b;
2450
+ let result = void 0;
2451
+ for await (const value of stream2) {
2452
+ switch (value.event) {
2453
+ case "thread.message.created": {
2454
+ controller.enqueue(
2455
+ textEncoder.encode(
2456
+ formatStreamPart("assistant_message", {
2457
+ id: value.data.id,
2458
+ role: "assistant",
2459
+ content: [{ type: "text", text: { value: "" } }]
2460
+ })
2461
+ )
2462
+ );
2463
+ break;
2464
+ }
2465
+ case "thread.message.delta": {
2466
+ const content = (_a2 = value.data.delta.content) == null ? void 0 : _a2[0];
2467
+ if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
2468
+ controller.enqueue(
2469
+ textEncoder.encode(
2470
+ formatStreamPart("text", content.text.value)
2471
+ )
2472
+ );
2473
+ }
2474
+ break;
2475
+ }
2476
+ case "thread.run.completed":
2477
+ case "thread.run.requires_action": {
2478
+ result = value.data;
2479
+ break;
2480
+ }
2481
+ }
2482
+ }
2483
+ return result;
2484
+ };
2485
+ controller.enqueue(
2486
+ textEncoder.encode(
2487
+ formatStreamPart("assistant_control_data", {
2488
+ threadId,
2489
+ messageId
2490
+ })
2491
+ )
2492
+ );
2493
+ try {
2494
+ await process2({
2495
+ threadId,
2496
+ messageId,
2497
+ sendMessage,
2498
+ sendDataMessage,
2499
+ forwardStream
2500
+ });
2501
+ } catch (error) {
2502
+ sendError((_a = error.message) != null ? _a : `${error}`);
2503
+ } finally {
2504
+ controller.close();
2505
+ }
2506
+ },
2507
+ pull(controller) {
2508
+ },
2509
+ cancel() {
2510
+ }
2511
+ });
2512
+ return new Response(stream, {
2513
+ status: 200,
2514
+ headers: {
2515
+ "Content-Type": "text/plain; charset=utf-8"
2516
+ }
2517
+ });
2518
+ }
2519
+ var experimental_AssistantResponse = AssistantResponse;
2520
+
2521
+ // streams/aws-bedrock-stream.ts
2522
+ async function* asDeltaIterable(response, extractTextDeltaFromChunk) {
2523
+ var _a, _b;
2524
+ const decoder = new TextDecoder();
2525
+ for await (const chunk of (_a = response.body) != null ? _a : []) {
2526
+ const bytes = (_b = chunk.chunk) == null ? void 0 : _b.bytes;
2527
+ if (bytes != null) {
2528
+ const chunkText = decoder.decode(bytes);
2529
+ const chunkJSON = JSON.parse(chunkText);
2530
+ const delta = extractTextDeltaFromChunk(chunkJSON);
2531
+ if (delta != null) {
2532
+ yield delta;
2533
+ }
2534
+ }
2535
+ }
2536
+ }
2537
+ function AWSBedrockAnthropicMessagesStream(response, callbacks) {
2538
+ return AWSBedrockStream(response, callbacks, (chunk) => {
2539
+ var _a;
2540
+ return (_a = chunk.delta) == null ? void 0 : _a.text;
2541
+ });
2542
+ }
2543
+ function AWSBedrockAnthropicStream(response, callbacks) {
2544
+ return AWSBedrockStream(response, callbacks, (chunk) => chunk.completion);
2545
+ }
2546
+ function AWSBedrockCohereStream(response, callbacks) {
2547
+ return AWSBedrockStream(response, callbacks, (chunk) => chunk == null ? void 0 : chunk.text);
2548
+ }
2549
+ function AWSBedrockLlama2Stream(response, callbacks) {
2550
+ return AWSBedrockStream(response, callbacks, (chunk) => chunk.generation);
2551
+ }
2552
+ function AWSBedrockStream(response, callbacks, extractTextDeltaFromChunk) {
2553
+ return readableFromAsyncIterable(
2554
+ asDeltaIterable(response, extractTextDeltaFromChunk)
2555
+ ).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
2556
+ }
2557
+
2558
+ // streams/cohere-stream.ts
2559
+ var utf8Decoder = new TextDecoder("utf-8");
2560
+ async function processLines(lines, controller) {
2561
+ for (const line of lines) {
2562
+ const { text, is_finished } = JSON.parse(line);
2563
+ if (!is_finished) {
2564
+ controller.enqueue(text);
2565
+ }
2566
+ }
2567
+ }
2568
+ async function readAndProcessLines(reader, controller) {
2569
+ let segment = "";
2570
+ while (true) {
2571
+ const { value: chunk, done } = await reader.read();
2572
+ if (done) {
2573
+ break;
2574
+ }
2575
+ segment += utf8Decoder.decode(chunk, { stream: true });
2576
+ const linesArray = segment.split(/\r\n|\n|\r/g);
2577
+ segment = linesArray.pop() || "";
2578
+ await processLines(linesArray, controller);
2579
+ }
2580
+ if (segment) {
2581
+ const linesArray = [segment];
2582
+ await processLines(linesArray, controller);
2583
+ }
2584
+ controller.close();
2585
+ }
2586
+ function createParser2(res) {
2587
+ var _a;
2588
+ const reader = (_a = res.body) == null ? void 0 : _a.getReader();
2589
+ return new ReadableStream({
2590
+ async start(controller) {
2591
+ if (!reader) {
2592
+ controller.close();
2593
+ return;
2594
+ }
2595
+ await readAndProcessLines(reader, controller);
2596
+ }
2597
+ });
2598
+ }
2599
+ async function* streamable2(stream) {
2600
+ for await (const chunk of stream) {
2601
+ if (chunk.eventType === "text-generation") {
2602
+ const text = chunk.text;
2603
+ if (text)
2604
+ yield text;
2605
+ }
2606
+ }
2607
+ }
2608
+ function CohereStream(reader, callbacks) {
2609
+ if (Symbol.asyncIterator in reader) {
2610
+ return readableFromAsyncIterable(streamable2(reader)).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
2611
+ } else {
2612
+ return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
2613
+ }
2614
+ }
2615
+
2616
+ // streams/google-generative-ai-stream.ts
2617
+ async function* streamable3(response) {
2618
+ var _a, _b, _c;
2619
+ for await (const chunk of response.stream) {
2620
+ const parts = (_c = (_b = (_a = chunk.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
2621
+ if (parts === void 0) {
2622
+ continue;
2623
+ }
2624
+ const firstPart = parts[0];
2625
+ if (typeof firstPart.text === "string") {
2626
+ yield firstPart.text;
2627
+ }
2628
+ }
2629
+ }
2630
+ function GoogleGenerativeAIStream(response, cb) {
2631
+ return readableFromAsyncIterable(streamable3(response)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer());
2632
+ }
2633
+
2634
+ // streams/huggingface-stream.ts
2635
+ function createParser3(res) {
2636
+ const trimStartOfStream = trimStartOfStreamHelper();
2637
+ return new ReadableStream({
2638
+ async pull(controller) {
2639
+ var _a, _b;
2640
+ const { value, done } = await res.next();
2641
+ if (done) {
2642
+ controller.close();
2643
+ return;
2644
+ }
2645
+ const text = trimStartOfStream((_b = (_a = value.token) == null ? void 0 : _a.text) != null ? _b : "");
2646
+ if (!text)
2647
+ return;
2648
+ if (value.generated_text != null && value.generated_text.length > 0) {
2649
+ return;
2650
+ }
2651
+ if (text === "</s>" || text === "<|endoftext|>" || text === "<|end|>") {
2652
+ return;
2653
+ }
2654
+ controller.enqueue(text);
2655
+ }
2656
+ });
2657
+ }
2658
+ function HuggingFaceStream(res, callbacks) {
2659
+ return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
2660
+ }
2661
+
2662
+ // streams/inkeep-stream.ts
2663
+ function InkeepStream(res, callbacks) {
2664
+ if (!res.body) {
2665
+ throw new Error("Response body is null");
2666
+ }
2667
+ let chat_session_id = "";
2668
+ let records_cited;
2669
+ const inkeepEventParser = (data, options) => {
2670
+ var _a, _b;
2671
+ const { event } = options;
2672
+ if (event === "records_cited") {
2673
+ records_cited = JSON.parse(data);
2674
+ (_a = callbacks == null ? void 0 : callbacks.onRecordsCited) == null ? void 0 : _a.call(callbacks, records_cited);
2675
+ }
2676
+ if (event === "message_chunk") {
2677
+ const inkeepMessageChunk = JSON.parse(data);
2678
+ chat_session_id = (_b = inkeepMessageChunk.chat_session_id) != null ? _b : chat_session_id;
2679
+ return inkeepMessageChunk.content_chunk;
2680
+ }
2681
+ return;
2682
+ };
2683
+ let { onRecordsCited, ...passThroughCallbacks } = callbacks || {};
2684
+ passThroughCallbacks = {
2685
+ ...passThroughCallbacks,
2686
+ onFinal: (completion) => {
2687
+ var _a;
2688
+ const inkeepOnFinalMetadata = {
2689
+ chat_session_id,
2690
+ records_cited
2691
+ };
2692
+ (_a = callbacks == null ? void 0 : callbacks.onFinal) == null ? void 0 : _a.call(callbacks, completion, inkeepOnFinalMetadata);
2693
+ }
2694
+ };
2695
+ return AIStream(res, inkeepEventParser, passThroughCallbacks).pipeThrough(
2696
+ createStreamDataTransformer()
2697
+ );
2698
+ }
2699
+
2700
+ // streams/langchain-adapter.ts
2701
+ var langchain_adapter_exports = {};
2702
+ __export(langchain_adapter_exports, {
2703
+ toAIStream: () => toAIStream
2704
+ });
2705
+ function toAIStream(stream, callbacks) {
2706
+ return stream.pipeThrough(
2707
+ new TransformStream({
2708
+ transform: async (chunk, controller) => {
2709
+ if (typeof chunk.content === "string") {
2710
+ controller.enqueue(chunk.content);
2711
+ } else {
2712
+ const content = chunk.content;
2713
+ for (const item of content) {
2714
+ if (item.type === "text") {
2715
+ controller.enqueue(item.text);
2716
+ }
2717
+ }
2718
+ }
2719
+ }
2720
+ })
2721
+ ).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
2722
+ }
2723
+
2724
+ // streams/langchain-stream.ts
2725
+ function LangChainStream(callbacks) {
2726
+ const stream = new TransformStream();
2727
+ const writer = stream.writable.getWriter();
2728
+ const runs = /* @__PURE__ */ new Set();
2729
+ const handleError = async (e, runId) => {
2730
+ runs.delete(runId);
2731
+ await writer.ready;
2732
+ await writer.abort(e);
2733
+ };
2734
+ const handleStart = async (runId) => {
2735
+ runs.add(runId);
2736
+ };
2737
+ const handleEnd = async (runId) => {
2738
+ runs.delete(runId);
2739
+ if (runs.size === 0) {
2740
+ await writer.ready;
2741
+ await writer.close();
2742
+ }
2743
+ };
2744
+ return {
2745
+ stream: stream.readable.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer()),
2746
+ writer,
2747
+ handlers: {
2748
+ handleLLMNewToken: async (token) => {
2749
+ await writer.ready;
2750
+ await writer.write(token);
2751
+ },
2752
+ handleLLMStart: async (_llm, _prompts, runId) => {
2753
+ handleStart(runId);
2754
+ },
2755
+ handleLLMEnd: async (_output, runId) => {
2756
+ await handleEnd(runId);
2757
+ },
2758
+ handleLLMError: async (e, runId) => {
2759
+ await handleError(e, runId);
2760
+ },
2761
+ handleChainStart: async (_chain, _inputs, runId) => {
2762
+ handleStart(runId);
2763
+ },
2764
+ handleChainEnd: async (_outputs, runId) => {
2765
+ await handleEnd(runId);
2766
+ },
2767
+ handleChainError: async (e, runId) => {
2768
+ await handleError(e, runId);
2769
+ },
2770
+ handleToolStart: async (_tool, _input, runId) => {
2771
+ handleStart(runId);
2772
+ },
2773
+ handleToolEnd: async (_output, runId) => {
2774
+ await handleEnd(runId);
2775
+ },
2776
+ handleToolError: async (e, runId) => {
2777
+ await handleError(e, runId);
2778
+ }
2779
+ }
2780
+ };
2781
+ }
2782
+
2783
+ // streams/mistral-stream.ts
2784
+ async function* streamable4(stream) {
2785
+ var _a, _b;
2786
+ for await (const chunk of stream) {
2787
+ const content = (_b = (_a = chunk.choices[0]) == null ? void 0 : _a.delta) == null ? void 0 : _b.content;
2788
+ if (content === void 0 || content === "") {
2789
+ continue;
2790
+ }
2791
+ yield content;
2792
+ }
2793
+ }
2794
+ function MistralStream(response, callbacks) {
2795
+ const stream = readableFromAsyncIterable(streamable4(response));
2796
+ return stream.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
2797
+ }
2798
+
2799
+ // streams/openai-stream.ts
2800
+ function parseOpenAIStream() {
2801
+ const extract = chunkToText();
2802
+ return (data) => extract(JSON.parse(data));
2803
+ }
2804
+ async function* streamable5(stream) {
2805
+ const extract = chunkToText();
2806
+ for await (let chunk of stream) {
2807
+ if ("promptFilterResults" in chunk) {
2808
+ chunk = {
2809
+ id: chunk.id,
2810
+ created: chunk.created.getDate(),
2811
+ object: chunk.object,
2812
+ // not exposed by Azure API
2813
+ model: chunk.model,
2814
+ // not exposed by Azure API
2815
+ choices: chunk.choices.map((choice) => {
2816
+ var _a, _b, _c, _d, _e, _f, _g;
2817
+ return {
2818
+ delta: {
2819
+ content: (_a = choice.delta) == null ? void 0 : _a.content,
2820
+ function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
2821
+ role: (_c = choice.delta) == null ? void 0 : _c.role,
2822
+ tool_calls: ((_e = (_d = choice.delta) == null ? void 0 : _d.toolCalls) == null ? void 0 : _e.length) ? (_g = (_f = choice.delta) == null ? void 0 : _f.toolCalls) == null ? void 0 : _g.map((toolCall, index) => ({
2823
+ index,
2824
+ id: toolCall.id,
2825
+ function: toolCall.function,
2826
+ type: toolCall.type
2827
+ })) : void 0
2828
+ },
2829
+ finish_reason: choice.finishReason,
2830
+ index: choice.index
2831
+ };
2832
+ })
2833
+ };
2834
+ }
2835
+ const text = extract(chunk);
2836
+ if (text)
2837
+ yield text;
2838
+ }
2839
+ }
2840
+ function chunkToText() {
2841
+ const trimStartOfStream = trimStartOfStreamHelper();
2842
+ let isFunctionStreamingIn;
2843
+ return (json) => {
2844
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
2845
+ if (isChatCompletionChunk(json)) {
2846
+ const delta = (_a = json.choices[0]) == null ? void 0 : _a.delta;
2847
+ if ((_b = delta.function_call) == null ? void 0 : _b.name) {
2848
+ isFunctionStreamingIn = true;
2849
+ return {
2850
+ isText: false,
2851
+ content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
2852
+ };
2853
+ } else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
2854
+ isFunctionStreamingIn = true;
2855
+ const toolCall = delta.tool_calls[0];
2856
+ if (toolCall.index === 0) {
2857
+ return {
2858
+ isText: false,
2859
+ content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
2860
+ };
2861
+ } else {
2862
+ return {
2863
+ isText: false,
2864
+ content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
2865
+ };
2866
+ }
2867
+ } else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
2868
+ return {
2869
+ isText: false,
2870
+ content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
2871
+ };
2872
+ } else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
2873
+ return {
2874
+ isText: false,
2875
+ content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
2876
+ };
2877
+ } else if (isFunctionStreamingIn && (((_p = json.choices[0]) == null ? void 0 : _p.finish_reason) === "function_call" || ((_q = json.choices[0]) == null ? void 0 : _q.finish_reason) === "stop")) {
2878
+ isFunctionStreamingIn = false;
2879
+ return {
2880
+ isText: false,
2881
+ content: '"}}'
2882
+ };
2883
+ } else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
2884
+ isFunctionStreamingIn = false;
2885
+ return {
2886
+ isText: false,
2887
+ content: '"}}]}'
2888
+ };
2889
+ }
2890
+ }
2891
+ const text = trimStartOfStream(
2892
+ isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
2893
+ );
2894
+ return text;
2895
+ };
2896
+ function cleanupArguments(argumentChunk) {
2897
+ let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
2898
+ return `${escapedPartialJson}`;
2899
+ }
2900
+ }
2901
+ var __internal__OpenAIFnMessagesSymbol = Symbol(
2902
+ "internal_openai_fn_messages"
2903
+ );
2904
+ function isChatCompletionChunk(data) {
2905
+ return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
2906
+ }
2907
+ function isCompletion(data) {
2908
+ return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
2909
+ }
2910
+ function OpenAIStream(res, callbacks) {
2911
+ const cb = callbacks;
2912
+ let stream;
2913
+ if (Symbol.asyncIterator in res) {
2914
+ stream = readableFromAsyncIterable(streamable5(res)).pipeThrough(
2915
+ createCallbacksTransformer(
2916
+ (cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
2917
+ ...cb,
2918
+ onFinal: void 0
2919
+ } : {
2920
+ ...cb
2921
+ }
2922
+ )
2923
+ );
2924
+ } else {
2925
+ stream = AIStream(
2926
+ res,
2927
+ parseOpenAIStream(),
2928
+ (cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
2929
+ ...cb,
2930
+ onFinal: void 0
2931
+ } : {
2932
+ ...cb
2933
+ }
2934
+ );
2935
+ }
2936
+ if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
2937
+ const functionCallTransformer = createFunctionCallTransformer(cb);
2938
+ return stream.pipeThrough(functionCallTransformer);
2939
+ } else {
2940
+ return stream.pipeThrough(createStreamDataTransformer());
2941
+ }
2942
+ }
2943
+ function createFunctionCallTransformer(callbacks) {
2944
+ const textEncoder = new TextEncoder();
2945
+ let isFirstChunk = true;
2946
+ let aggregatedResponse = "";
2947
+ let aggregatedFinalCompletionResponse = "";
2948
+ let isFunctionStreamingIn = false;
2949
+ let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
2950
+ const decode = createChunkDecoder();
2951
+ return new TransformStream({
2952
+ async transform(chunk, controller) {
2953
+ const message = decode(chunk);
2954
+ aggregatedFinalCompletionResponse += message;
2955
+ const shouldHandleAsFunction = isFirstChunk && (message.startsWith('{"function_call":') || message.startsWith('{"tool_calls":'));
2956
+ if (shouldHandleAsFunction) {
2957
+ isFunctionStreamingIn = true;
2958
+ aggregatedResponse += message;
2959
+ isFirstChunk = false;
2960
+ return;
2961
+ }
2962
+ if (!isFunctionStreamingIn) {
2963
+ controller.enqueue(
2964
+ textEncoder.encode(formatStreamPart("text", message))
2965
+ );
2966
+ return;
2967
+ } else {
2968
+ aggregatedResponse += message;
2969
+ }
2970
+ },
2971
+ async flush(controller) {
2972
+ try {
2973
+ if (!isFirstChunk && isFunctionStreamingIn && (callbacks.experimental_onFunctionCall || callbacks.experimental_onToolCall)) {
2974
+ isFunctionStreamingIn = false;
2975
+ const payload = JSON.parse(aggregatedResponse);
2976
+ let newFunctionCallMessages = [
2977
+ ...functionCallMessages
2978
+ ];
2979
+ let functionResponse = void 0;
2980
+ if (callbacks.experimental_onFunctionCall) {
2981
+ if (payload.function_call === void 0) {
2982
+ console.warn(
2983
+ "experimental_onFunctionCall should not be defined when using tools"
2984
+ );
2985
+ }
2986
+ const argumentsPayload = JSON.parse(
2987
+ payload.function_call.arguments
2988
+ );
2989
+ functionResponse = await callbacks.experimental_onFunctionCall(
2990
+ {
2991
+ name: payload.function_call.name,
2992
+ arguments: argumentsPayload
2993
+ },
2994
+ (result) => {
2995
+ newFunctionCallMessages = [
2996
+ ...functionCallMessages,
2997
+ {
2998
+ role: "assistant",
2999
+ content: "",
3000
+ function_call: payload.function_call
3001
+ },
3002
+ {
3003
+ role: "function",
3004
+ name: payload.function_call.name,
3005
+ content: JSON.stringify(result)
3006
+ }
3007
+ ];
3008
+ return newFunctionCallMessages;
3009
+ }
3010
+ );
3011
+ }
3012
+ if (callbacks.experimental_onToolCall) {
3013
+ const toolCalls = {
3014
+ tools: []
3015
+ };
3016
+ for (const tool2 of payload.tool_calls) {
3017
+ toolCalls.tools.push({
3018
+ id: tool2.id,
3019
+ type: "function",
3020
+ func: {
3021
+ name: tool2.function.name,
3022
+ arguments: JSON.parse(tool2.function.arguments)
3023
+ }
3024
+ });
3025
+ }
3026
+ let responseIndex = 0;
3027
+ try {
3028
+ functionResponse = await callbacks.experimental_onToolCall(
3029
+ toolCalls,
3030
+ (result) => {
3031
+ if (result) {
3032
+ const { tool_call_id, function_name, tool_call_result } = result;
3033
+ newFunctionCallMessages = [
3034
+ ...newFunctionCallMessages,
3035
+ // Only append the assistant message if it's the first response
3036
+ ...responseIndex === 0 ? [
3037
+ {
3038
+ role: "assistant",
3039
+ content: "",
3040
+ tool_calls: payload.tool_calls.map(
3041
+ (tc) => ({
3042
+ id: tc.id,
3043
+ type: "function",
3044
+ function: {
3045
+ name: tc.function.name,
3046
+ // we send the arguments an object to the user, but as the API expects a string, we need to stringify it
3047
+ arguments: JSON.stringify(
3048
+ tc.function.arguments
3049
+ )
3050
+ }
3051
+ })
3052
+ )
3053
+ }
3054
+ ] : [],
3055
+ // Append the function call result message
3056
+ {
3057
+ role: "tool",
3058
+ tool_call_id,
3059
+ name: function_name,
3060
+ content: JSON.stringify(tool_call_result)
3061
+ }
3062
+ ];
3063
+ responseIndex++;
3064
+ }
3065
+ return newFunctionCallMessages;
3066
+ }
3067
+ );
3068
+ } catch (e) {
3069
+ console.error("Error calling experimental_onToolCall:", e);
3070
+ }
3071
+ }
3072
+ if (!functionResponse) {
3073
+ controller.enqueue(
3074
+ textEncoder.encode(
3075
+ formatStreamPart(
3076
+ payload.function_call ? "function_call" : "tool_calls",
3077
+ // parse to prevent double-encoding:
3078
+ JSON.parse(aggregatedResponse)
3079
+ )
3080
+ )
3081
+ );
3082
+ return;
3083
+ } else if (typeof functionResponse === "string") {
3084
+ controller.enqueue(
3085
+ textEncoder.encode(formatStreamPart("text", functionResponse))
3086
+ );
3087
+ aggregatedFinalCompletionResponse = functionResponse;
3088
+ return;
3089
+ }
3090
+ const filteredCallbacks = {
3091
+ ...callbacks,
3092
+ onStart: void 0
3093
+ };
3094
+ callbacks.onFinal = void 0;
3095
+ const openAIStream = OpenAIStream(functionResponse, {
3096
+ ...filteredCallbacks,
3097
+ [__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
3098
+ });
3099
+ const reader = openAIStream.getReader();
3100
+ while (true) {
3101
+ const { done, value } = await reader.read();
3102
+ if (done) {
3103
+ break;
3104
+ }
3105
+ controller.enqueue(value);
3106
+ }
3107
+ }
3108
+ } finally {
3109
+ if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
3110
+ await callbacks.onFinal(aggregatedFinalCompletionResponse);
3111
+ }
3112
+ }
3113
+ }
3114
+ });
3115
+ }
3116
+
3117
+ // streams/replicate-stream.ts
3118
+ async function ReplicateStream(res, cb, options) {
3119
+ var _a;
3120
+ const url = (_a = res.urls) == null ? void 0 : _a.stream;
3121
+ if (!url) {
3122
+ if (res.error)
3123
+ throw new Error(res.error);
3124
+ else
3125
+ throw new Error("Missing stream URL in Replicate response");
3126
+ }
3127
+ const eventStream = await fetch(url, {
3128
+ method: "GET",
3129
+ headers: {
3130
+ Accept: "text/event-stream",
3131
+ ...options == null ? void 0 : options.headers
3132
+ }
3133
+ });
3134
+ return AIStream(eventStream, void 0, cb).pipeThrough(
3135
+ createStreamDataTransformer()
3136
+ );
3137
+ }
3138
+
3139
+ // core/util/merge-streams.ts
3140
+ function mergeStreams(stream1, stream2) {
3141
+ const reader1 = stream1.getReader();
3142
+ const reader2 = stream2.getReader();
3143
+ let lastRead1 = void 0;
3144
+ let lastRead2 = void 0;
3145
+ let stream1Done = false;
3146
+ let stream2Done = false;
3147
+ async function readStream1(controller) {
3148
+ try {
3149
+ if (lastRead1 == null) {
3150
+ lastRead1 = reader1.read();
3151
+ }
3152
+ const result = await lastRead1;
3153
+ lastRead1 = void 0;
3154
+ if (!result.done) {
3155
+ controller.enqueue(result.value);
3156
+ } else {
3157
+ controller.close();
3158
+ }
3159
+ } catch (error) {
3160
+ controller.error(error);
3161
+ }
3162
+ }
3163
+ async function readStream2(controller) {
3164
+ try {
3165
+ if (lastRead2 == null) {
3166
+ lastRead2 = reader2.read();
3167
+ }
3168
+ const result = await lastRead2;
3169
+ lastRead2 = void 0;
3170
+ if (!result.done) {
3171
+ controller.enqueue(result.value);
3172
+ } else {
3173
+ controller.close();
3174
+ }
3175
+ } catch (error) {
3176
+ controller.error(error);
3177
+ }
3178
+ }
3179
+ return new ReadableStream({
3180
+ async pull(controller) {
3181
+ try {
3182
+ if (stream1Done) {
3183
+ readStream2(controller);
3184
+ return;
3185
+ }
3186
+ if (stream2Done) {
3187
+ readStream1(controller);
3188
+ return;
3189
+ }
3190
+ if (lastRead1 == null) {
3191
+ lastRead1 = reader1.read();
3192
+ }
3193
+ if (lastRead2 == null) {
3194
+ lastRead2 = reader2.read();
3195
+ }
3196
+ const { result, reader } = await Promise.race([
3197
+ lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
3198
+ lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
3199
+ ]);
3200
+ if (!result.done) {
3201
+ controller.enqueue(result.value);
3202
+ }
3203
+ if (reader === reader1) {
3204
+ lastRead1 = void 0;
3205
+ if (result.done) {
3206
+ readStream2(controller);
3207
+ stream1Done = true;
3208
+ }
3209
+ } else {
3210
+ lastRead2 = void 0;
3211
+ if (result.done) {
3212
+ stream2Done = true;
3213
+ readStream1(controller);
3214
+ }
3215
+ }
3216
+ } catch (error) {
3217
+ controller.error(error);
3218
+ }
3219
+ },
3220
+ cancel() {
3221
+ reader1.cancel();
3222
+ reader2.cancel();
3223
+ }
3224
+ });
3225
+ }
3226
+
3227
+ // streams/stream-to-response.ts
3228
+ function streamToResponse(res, response, init, data) {
3229
+ var _a;
3230
+ response.writeHead((_a = init == null ? void 0 : init.status) != null ? _a : 200, {
3231
+ "Content-Type": "text/plain; charset=utf-8",
3232
+ ...init == null ? void 0 : init.headers
3233
+ });
3234
+ let processedStream = res;
3235
+ if (data) {
3236
+ processedStream = mergeStreams(data.stream, res);
3237
+ }
3238
+ const reader = processedStream.getReader();
3239
+ function read() {
3240
+ reader.read().then(({ done, value }) => {
3241
+ if (done) {
3242
+ response.end();
3243
+ return;
3244
+ }
3245
+ response.write(value);
3246
+ read();
3247
+ });
3248
+ }
3249
+ read();
3250
+ }
3251
+
3252
+ // shared/parse-complex-response.ts
3253
+ function assignAnnotationsToMessage(message, annotations) {
3254
+ if (!message || !annotations || !annotations.length)
3255
+ return message;
3256
+ return { ...message, annotations: [...annotations] };
3257
+ }
3258
+ async function parseComplexResponse({
3259
+ reader,
3260
+ abortControllerRef,
3261
+ update,
3262
+ onFinish,
3263
+ generateId: generateId2 = generateId,
3264
+ getCurrentDate = () => /* @__PURE__ */ new Date()
3265
+ }) {
3266
+ const createdAt = getCurrentDate();
3267
+ const prefixMap = {
3268
+ data: []
3269
+ };
3270
+ let message_annotations = void 0;
3271
+ for await (const { type, value } of readDataStream(reader, {
3272
+ isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
3273
+ })) {
3274
+ if (type === "text") {
3275
+ if (prefixMap["text"]) {
3276
+ prefixMap["text"] = {
3277
+ ...prefixMap["text"],
3278
+ content: (prefixMap["text"].content || "") + value
3279
+ };
3280
+ } else {
3281
+ prefixMap["text"] = {
3282
+ id: generateId2(),
3283
+ role: "assistant",
3284
+ content: value,
3285
+ createdAt
3286
+ };
3287
+ }
3288
+ }
3289
+ if (type === "tool_call") {
3290
+ if (prefixMap.text == null) {
3291
+ prefixMap.text = {
3292
+ id: generateId2(),
3293
+ role: "assistant",
3294
+ content: "",
3295
+ createdAt
3296
+ };
3297
+ }
3298
+ if (prefixMap.text.toolInvocations == null) {
3299
+ prefixMap.text.toolInvocations = [];
3300
+ }
3301
+ prefixMap.text.toolInvocations.push(value);
3302
+ } else if (type === "tool_result") {
3303
+ if (prefixMap.text == null) {
3304
+ prefixMap.text = {
3305
+ id: generateId2(),
3306
+ role: "assistant",
3307
+ content: "",
3308
+ createdAt
3309
+ };
3310
+ }
3311
+ if (prefixMap.text.toolInvocations == null) {
3312
+ prefixMap.text.toolInvocations = [];
3313
+ }
3314
+ const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
3315
+ (invocation) => invocation.toolCallId === value.toolCallId
3316
+ );
3317
+ if (toolInvocationIndex !== -1) {
3318
+ prefixMap.text.toolInvocations[toolInvocationIndex] = value;
3319
+ } else {
3320
+ prefixMap.text.toolInvocations.push(value);
3321
+ }
3322
+ }
3323
+ let functionCallMessage = null;
3324
+ if (type === "function_call") {
3325
+ prefixMap["function_call"] = {
3326
+ id: generateId2(),
3327
+ role: "assistant",
3328
+ content: "",
3329
+ function_call: value.function_call,
3330
+ name: value.function_call.name,
3331
+ createdAt
3332
+ };
3333
+ functionCallMessage = prefixMap["function_call"];
3334
+ }
3335
+ let toolCallMessage = null;
3336
+ if (type === "tool_calls") {
3337
+ prefixMap["tool_calls"] = {
3338
+ id: generateId2(),
3339
+ role: "assistant",
3340
+ content: "",
3341
+ tool_calls: value.tool_calls,
3342
+ createdAt
3343
+ };
3344
+ toolCallMessage = prefixMap["tool_calls"];
3345
+ }
3346
+ if (type === "data") {
3347
+ prefixMap["data"].push(...value);
3348
+ }
3349
+ let responseMessage = prefixMap["text"];
3350
+ if (type === "message_annotations") {
3351
+ if (!message_annotations) {
3352
+ message_annotations = [...value];
3353
+ } else {
3354
+ message_annotations.push(...value);
3355
+ }
3356
+ functionCallMessage = assignAnnotationsToMessage(
3357
+ prefixMap["function_call"],
3358
+ message_annotations
3359
+ );
3360
+ toolCallMessage = assignAnnotationsToMessage(
3361
+ prefixMap["tool_calls"],
3362
+ message_annotations
3363
+ );
3364
+ responseMessage = assignAnnotationsToMessage(
3365
+ prefixMap["text"],
3366
+ message_annotations
3367
+ );
3368
+ }
3369
+ if (message_annotations == null ? void 0 : message_annotations.length) {
3370
+ const messagePrefixKeys = [
3371
+ "text",
3372
+ "function_call",
3373
+ "tool_calls"
3374
+ ];
3375
+ messagePrefixKeys.forEach((key) => {
3376
+ if (prefixMap[key]) {
3377
+ prefixMap[key].annotations = [...message_annotations];
3378
+ }
3379
+ });
3380
+ }
3381
+ const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
3382
+ ...assignAnnotationsToMessage(message, message_annotations)
3383
+ }));
3384
+ update(merged, [...prefixMap["data"]]);
3385
+ }
3386
+ onFinish == null ? void 0 : onFinish(prefixMap);
3387
+ return {
3388
+ messages: [
3389
+ prefixMap.text,
3390
+ prefixMap.function_call,
3391
+ prefixMap.tool_calls
3392
+ ].filter(Boolean),
3393
+ data: prefixMap.data
3394
+ };
3395
+ }
3396
+
3397
+ // streams/streaming-react-response.ts
3398
+ var experimental_StreamingReactResponse = class {
3399
+ constructor(res, options) {
3400
+ var _a, _b;
3401
+ let resolveFunc = () => {
3402
+ };
3403
+ let next = new Promise((resolve) => {
3404
+ resolveFunc = resolve;
3405
+ });
3406
+ const processedStream = (options == null ? void 0 : options.data) != null ? mergeStreams((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream, res) : res;
3407
+ let lastPayload = void 0;
3408
+ parseComplexResponse({
3409
+ reader: processedStream.getReader(),
3410
+ update: (merged, data) => {
3411
+ var _a2, _b2, _c;
3412
+ const content = (_b2 = (_a2 = merged[0]) == null ? void 0 : _a2.content) != null ? _b2 : "";
3413
+ const ui = ((_c = options == null ? void 0 : options.ui) == null ? void 0 : _c.call(options, { content, data })) || content;
3414
+ const payload = { ui, content };
3415
+ const resolvePrevious = resolveFunc;
3416
+ const nextRow = new Promise((resolve) => {
3417
+ resolveFunc = resolve;
3418
+ });
3419
+ resolvePrevious({
3420
+ next: nextRow,
3421
+ ...payload
3422
+ });
3423
+ lastPayload = payload;
3424
+ },
3425
+ generateId: (_b = options == null ? void 0 : options.generateId) != null ? _b : generateId,
3426
+ onFinish: () => {
3427
+ if (lastPayload !== void 0) {
3428
+ resolveFunc({
3429
+ next: null,
3430
+ ...lastPayload
3431
+ });
3432
+ }
3433
+ }
3434
+ });
3435
+ return next;
3436
+ }
3437
+ };
3438
+
3439
+ // streams/streaming-text-response.ts
3440
+ var StreamingTextResponse = class extends Response {
3441
+ constructor(res, init, data) {
3442
+ let processedStream = res;
3443
+ if (data) {
3444
+ processedStream = mergeStreams(data.stream, res);
3445
+ }
3446
+ super(processedStream, {
3447
+ ...init,
3448
+ status: 200,
3449
+ headers: {
3450
+ "Content-Type": "text/plain; charset=utf-8",
3451
+ ...init == null ? void 0 : init.headers
3452
+ }
3453
+ });
3454
+ }
3455
+ };
3456
+ // Annotate the CommonJS export names for ESM import in node:
3457
+ 0 && (module.exports = {
3458
+ AIStream,
3459
+ APICallError,
3460
+ AWSBedrockAnthropicMessagesStream,
3461
+ AWSBedrockAnthropicStream,
3462
+ AWSBedrockCohereStream,
3463
+ AWSBedrockLlama2Stream,
3464
+ AWSBedrockStream,
3465
+ AnthropicStream,
3466
+ AssistantResponse,
3467
+ CohereStream,
3468
+ EmbedManyResult,
3469
+ EmbedResult,
3470
+ EmptyResponseBodyError,
3471
+ GenerateObjectResult,
3472
+ GenerateTextResult,
3473
+ GoogleGenerativeAIStream,
3474
+ HuggingFaceStream,
3475
+ InkeepStream,
3476
+ InvalidArgumentError,
3477
+ InvalidDataContentError,
3478
+ InvalidPromptError,
3479
+ InvalidResponseDataError,
3480
+ InvalidToolArgumentsError,
3481
+ JSONParseError,
3482
+ LangChainAdapter,
3483
+ LangChainStream,
3484
+ LoadAPIKeyError,
3485
+ MistralStream,
3486
+ NoObjectGeneratedError,
3487
+ NoSuchToolError,
3488
+ OpenAIStream,
3489
+ ReplicateStream,
3490
+ RetryError,
3491
+ StreamData,
3492
+ StreamObjectResult,
3493
+ StreamTextResult,
3494
+ StreamingTextResponse,
3495
+ ToolCallParseError,
3496
+ TypeValidationError,
3497
+ UnsupportedFunctionalityError,
3498
+ UnsupportedJSONSchemaError,
3499
+ convertDataContentToBase64String,
3500
+ convertDataContentToUint8Array,
3501
+ convertToCoreMessages,
3502
+ createCallbacksTransformer,
3503
+ createChunkDecoder,
3504
+ createEventStreamTransformer,
3505
+ createStreamDataTransformer,
3506
+ embed,
3507
+ embedMany,
3508
+ experimental_AssistantResponse,
3509
+ experimental_StreamData,
3510
+ experimental_StreamingReactResponse,
3511
+ experimental_generateObject,
3512
+ experimental_generateText,
3513
+ experimental_streamObject,
3514
+ experimental_streamText,
3515
+ formatStreamPart,
3516
+ generateId,
3517
+ generateObject,
3518
+ generateText,
3519
+ isStreamStringEqualToType,
3520
+ nanoid,
3521
+ parseStreamPart,
3522
+ readDataStream,
3523
+ readableFromAsyncIterable,
3524
+ streamObject,
3525
+ streamResponse,
3526
+ streamText,
3527
+ streamToResponse,
3528
+ tool,
3529
+ trimStartOfStreamHelper
3530
+ });
3531
+ //# sourceMappingURL=index.js.map