ai 3.0.21 → 3.0.22

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 (49) hide show
  1. package/dist/index.d.mts +38 -1
  2. package/dist/index.d.ts +38 -1
  3. package/dist/index.js +102 -175
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +63 -136
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +4 -33
  8. package/react/dist/index.js +3 -3
  9. package/react/dist/index.js.map +1 -1
  10. package/react/dist/index.mjs +3 -3
  11. package/react/dist/index.mjs.map +1 -1
  12. package/rsc/dist/rsc-server.mjs +3 -3
  13. package/rsc/dist/rsc-server.mjs.map +1 -1
  14. package/solid/dist/index.js +3 -3
  15. package/solid/dist/index.js.map +1 -1
  16. package/solid/dist/index.mjs +3 -3
  17. package/solid/dist/index.mjs.map +1 -1
  18. package/svelte/dist/index.js +3 -3
  19. package/svelte/dist/index.js.map +1 -1
  20. package/svelte/dist/index.mjs +3 -3
  21. package/svelte/dist/index.mjs.map +1 -1
  22. package/vue/dist/index.js +3 -3
  23. package/vue/dist/index.js.map +1 -1
  24. package/vue/dist/index.mjs +3 -3
  25. package/vue/dist/index.mjs.map +1 -1
  26. package/anthropic/dist/index.d.mts +0 -51
  27. package/anthropic/dist/index.d.ts +0 -51
  28. package/anthropic/dist/index.js +0 -792
  29. package/anthropic/dist/index.js.map +0 -1
  30. package/anthropic/dist/index.mjs +0 -760
  31. package/anthropic/dist/index.mjs.map +0 -1
  32. package/google/dist/index.d.mts +0 -47
  33. package/google/dist/index.d.ts +0 -47
  34. package/google/dist/index.js +0 -796
  35. package/google/dist/index.js.map +0 -1
  36. package/google/dist/index.mjs +0 -764
  37. package/google/dist/index.mjs.map +0 -1
  38. package/mistral/dist/index.d.mts +0 -52
  39. package/mistral/dist/index.d.ts +0 -52
  40. package/mistral/dist/index.js +0 -763
  41. package/mistral/dist/index.js.map +0 -1
  42. package/mistral/dist/index.mjs +0 -731
  43. package/mistral/dist/index.mjs.map +0 -1
  44. package/openai/dist/index.d.mts +0 -116
  45. package/openai/dist/index.d.ts +0 -116
  46. package/openai/dist/index.js +0 -1143
  47. package/openai/dist/index.js.map +0 -1
  48. package/openai/dist/index.mjs +0 -1115
  49. package/openai/dist/index.mjs.map +0 -1
package/dist/index.js CHANGED
@@ -65,9 +65,12 @@ __export(streams_exports, {
65
65
  experimental_generateText: () => experimental_generateText,
66
66
  experimental_streamObject: () => experimental_streamObject,
67
67
  experimental_streamText: () => experimental_streamText,
68
+ formatStreamPart: () => formatStreamPart,
68
69
  generateId: () => generateId,
69
70
  isStreamStringEqualToType: () => isStreamStringEqualToType,
70
71
  nanoid: () => generateId,
72
+ parseStreamPart: () => parseStreamPart,
73
+ readDataStream: () => readDataStream,
71
74
  readableFromAsyncIterable: () => readableFromAsyncIterable,
72
75
  streamToResponse: () => streamToResponse,
73
76
  tool: () => tool,
@@ -76,90 +79,8 @@ __export(streams_exports, {
76
79
  module.exports = __toCommonJS(streams_exports);
77
80
 
78
81
  // core/generate-object/generate-object.ts
79
- var import_provider7 = require("@ai-sdk/provider");
80
-
81
- // spec/util/get-error-message.ts
82
- function getErrorMessage(error) {
83
- if (error == null) {
84
- return "unknown error";
85
- }
86
- if (typeof error === "string") {
87
- return error;
88
- }
89
- if (error instanceof Error) {
90
- return error.message;
91
- }
92
- return JSON.stringify(error);
93
- }
94
-
95
- // spec/util/parse-json.ts
96
- var import_provider2 = require("@ai-sdk/provider");
97
- var import_secure_json_parse = __toESM(require("secure-json-parse"));
98
-
99
- // spec/util/validate-types.ts
100
- var import_provider = require("@ai-sdk/provider");
101
- function safeValidateTypes({
102
- value,
103
- schema
104
- }) {
105
- try {
106
- const validationResult = schema.safeParse(value);
107
- if (validationResult.success) {
108
- return {
109
- success: true,
110
- value: validationResult.data
111
- };
112
- }
113
- return {
114
- success: false,
115
- error: new import_provider.TypeValidationError({
116
- value,
117
- cause: validationResult.error
118
- })
119
- };
120
- } catch (error) {
121
- return {
122
- success: false,
123
- error: import_provider.TypeValidationError.isTypeValidationError(error) ? error : new import_provider.TypeValidationError({ value, cause: error })
124
- };
125
- }
126
- }
127
-
128
- // spec/util/parse-json.ts
129
- function safeParseJSON({
130
- text,
131
- schema
132
- }) {
133
- try {
134
- const value = import_secure_json_parse.default.parse(text);
135
- if (schema == null) {
136
- return {
137
- success: true,
138
- value
139
- };
140
- }
141
- return safeValidateTypes({ value, schema });
142
- } catch (error) {
143
- return {
144
- success: false,
145
- error: import_provider2.JSONParseError.isJSONParseError(error) ? error : new import_provider2.JSONParseError({ text, cause: error })
146
- };
147
- }
148
- }
149
-
150
- // spec/util/uint8-utils.ts
151
- function convertBase64ToUint8Array(base64String) {
152
- const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
153
- const latin1string = globalThis.atob(base64Url);
154
- return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
155
- }
156
- function convertUint8ArrayToBase64(array) {
157
- let latin1string = "";
158
- for (let i = 0; i < array.length; i++) {
159
- latin1string += String.fromCodePoint(array[i]);
160
- }
161
- return globalThis.btoa(latin1string);
162
- }
82
+ var import_provider5 = require("@ai-sdk/provider");
83
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
163
84
 
164
85
  // core/generate-text/token-usage.ts
165
86
  function calculateTokenUsage(usage) {
@@ -187,27 +108,28 @@ function detectImageMimeType(image) {
187
108
  }
188
109
 
189
110
  // core/prompt/data-content.ts
190
- var import_provider3 = require("@ai-sdk/provider");
111
+ var import_provider = require("@ai-sdk/provider");
112
+ var import_provider_utils = require("@ai-sdk/provider-utils");
191
113
  function convertDataContentToBase64String(content) {
192
114
  if (typeof content === "string") {
193
115
  return content;
194
116
  }
195
117
  if (content instanceof ArrayBuffer) {
196
- return convertUint8ArrayToBase64(new Uint8Array(content));
118
+ return (0, import_provider_utils.convertUint8ArrayToBase64)(new Uint8Array(content));
197
119
  }
198
- return convertUint8ArrayToBase64(content);
120
+ return (0, import_provider_utils.convertUint8ArrayToBase64)(content);
199
121
  }
200
122
  function convertDataContentToUint8Array(content) {
201
123
  if (content instanceof Uint8Array) {
202
124
  return content;
203
125
  }
204
126
  if (typeof content === "string") {
205
- return convertBase64ToUint8Array(content);
127
+ return (0, import_provider_utils.convertBase64ToUint8Array)(content);
206
128
  }
207
129
  if (content instanceof ArrayBuffer) {
208
130
  return new Uint8Array(content);
209
131
  }
210
- throw new import_provider3.InvalidDataContentError({ content });
132
+ throw new import_provider.InvalidDataContentError({ content });
211
133
  }
212
134
 
213
135
  // core/prompt/convert-to-language-model-prompt.ts
@@ -292,16 +214,16 @@ function convertToLanguageModelPrompt(prompt) {
292
214
  }
293
215
 
294
216
  // core/prompt/get-validated-prompt.ts
295
- var import_provider4 = require("@ai-sdk/provider");
217
+ var import_provider2 = require("@ai-sdk/provider");
296
218
  function getValidatedPrompt(prompt) {
297
219
  if (prompt.prompt == null && prompt.messages == null) {
298
- throw new import_provider4.InvalidPromptError({
220
+ throw new import_provider2.InvalidPromptError({
299
221
  prompt,
300
222
  message: "prompt or messages must be defined"
301
223
  });
302
224
  }
303
225
  if (prompt.prompt != null && prompt.messages != null) {
304
- throw new import_provider4.InvalidPromptError({
226
+ throw new import_provider2.InvalidPromptError({
305
227
  prompt,
306
228
  message: "prompt and messages cannot be defined at the same time"
307
229
  });
@@ -321,7 +243,7 @@ function getValidatedPrompt(prompt) {
321
243
  }
322
244
 
323
245
  // core/prompt/prepare-call-settings.ts
324
- var import_provider5 = require("@ai-sdk/provider");
246
+ var import_provider3 = require("@ai-sdk/provider");
325
247
  function prepareCallSettings({
326
248
  maxTokens,
327
249
  temperature,
@@ -333,14 +255,14 @@ function prepareCallSettings({
333
255
  }) {
334
256
  if (maxTokens != null) {
335
257
  if (!Number.isInteger(maxTokens)) {
336
- throw new import_provider5.InvalidArgumentError({
258
+ throw new import_provider3.InvalidArgumentError({
337
259
  parameter: "maxTokens",
338
260
  value: maxTokens,
339
261
  message: "maxTokens must be an integer"
340
262
  });
341
263
  }
342
264
  if (maxTokens < 1) {
343
- throw new import_provider5.InvalidArgumentError({
265
+ throw new import_provider3.InvalidArgumentError({
344
266
  parameter: "maxTokens",
345
267
  value: maxTokens,
346
268
  message: "maxTokens must be >= 1"
@@ -349,14 +271,14 @@ function prepareCallSettings({
349
271
  }
350
272
  if (temperature != null) {
351
273
  if (typeof temperature !== "number") {
352
- throw new import_provider5.InvalidArgumentError({
274
+ throw new import_provider3.InvalidArgumentError({
353
275
  parameter: "temperature",
354
276
  value: temperature,
355
277
  message: "temperature must be a number"
356
278
  });
357
279
  }
358
280
  if (temperature < 0 || temperature > 1) {
359
- throw new import_provider5.InvalidArgumentError({
281
+ throw new import_provider3.InvalidArgumentError({
360
282
  parameter: "temperature",
361
283
  value: temperature,
362
284
  message: "temperature must be between 0 and 1 (inclusive)"
@@ -365,14 +287,14 @@ function prepareCallSettings({
365
287
  }
366
288
  if (topP != null) {
367
289
  if (typeof topP !== "number") {
368
- throw new import_provider5.InvalidArgumentError({
290
+ throw new import_provider3.InvalidArgumentError({
369
291
  parameter: "topP",
370
292
  value: topP,
371
293
  message: "topP must be a number"
372
294
  });
373
295
  }
374
296
  if (topP < 0 || topP > 1) {
375
- throw new import_provider5.InvalidArgumentError({
297
+ throw new import_provider3.InvalidArgumentError({
376
298
  parameter: "topP",
377
299
  value: topP,
378
300
  message: "topP must be between 0 and 1 (inclusive)"
@@ -381,14 +303,14 @@ function prepareCallSettings({
381
303
  }
382
304
  if (presencePenalty != null) {
383
305
  if (typeof presencePenalty !== "number") {
384
- throw new import_provider5.InvalidArgumentError({
306
+ throw new import_provider3.InvalidArgumentError({
385
307
  parameter: "presencePenalty",
386
308
  value: presencePenalty,
387
309
  message: "presencePenalty must be a number"
388
310
  });
389
311
  }
390
312
  if (presencePenalty < -1 || presencePenalty > 1) {
391
- throw new import_provider5.InvalidArgumentError({
313
+ throw new import_provider3.InvalidArgumentError({
392
314
  parameter: "presencePenalty",
393
315
  value: presencePenalty,
394
316
  message: "presencePenalty must be between -1 and 1 (inclusive)"
@@ -397,14 +319,14 @@ function prepareCallSettings({
397
319
  }
398
320
  if (frequencyPenalty != null) {
399
321
  if (typeof frequencyPenalty !== "number") {
400
- throw new import_provider5.InvalidArgumentError({
322
+ throw new import_provider3.InvalidArgumentError({
401
323
  parameter: "frequencyPenalty",
402
324
  value: frequencyPenalty,
403
325
  message: "frequencyPenalty must be a number"
404
326
  });
405
327
  }
406
328
  if (frequencyPenalty < -1 || frequencyPenalty > 1) {
407
- throw new import_provider5.InvalidArgumentError({
329
+ throw new import_provider3.InvalidArgumentError({
408
330
  parameter: "frequencyPenalty",
409
331
  value: frequencyPenalty,
410
332
  message: "frequencyPenalty must be between -1 and 1 (inclusive)"
@@ -413,7 +335,7 @@ function prepareCallSettings({
413
335
  }
414
336
  if (seed != null) {
415
337
  if (!Number.isInteger(seed)) {
416
- throw new import_provider5.InvalidArgumentError({
338
+ throw new import_provider3.InvalidArgumentError({
417
339
  parameter: "seed",
418
340
  value: seed,
419
341
  message: "seed must be an integer"
@@ -422,14 +344,14 @@ function prepareCallSettings({
422
344
  }
423
345
  if (maxRetries != null) {
424
346
  if (!Number.isInteger(maxRetries)) {
425
- throw new import_provider5.InvalidArgumentError({
347
+ throw new import_provider3.InvalidArgumentError({
426
348
  parameter: "maxRetries",
427
349
  value: maxRetries,
428
350
  message: "maxRetries must be an integer"
429
351
  });
430
352
  }
431
353
  if (maxRetries < 0) {
432
- throw new import_provider5.InvalidArgumentError({
354
+ throw new import_provider3.InvalidArgumentError({
433
355
  parameter: "maxRetries",
434
356
  value: maxRetries,
435
357
  message: "maxRetries must be >= 0"
@@ -454,7 +376,8 @@ function convertZodToJSONSchema(zodSchema) {
454
376
  }
455
377
 
456
378
  // core/util/retry-with-exponential-backoff.ts
457
- var import_provider6 = require("@ai-sdk/provider");
379
+ var import_provider4 = require("@ai-sdk/provider");
380
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
458
381
 
459
382
  // core/util/delay.ts
460
383
  async function delay(delayInMs) {
@@ -485,17 +408,17 @@ async function _retryWithExponentialBackoff(f, {
485
408
  if (maxRetries === 0) {
486
409
  throw error;
487
410
  }
488
- const errorMessage = getErrorMessage(error);
411
+ const errorMessage = (0, import_provider_utils2.getErrorMessage)(error);
489
412
  const newErrors = [...errors, error];
490
413
  const tryNumber = newErrors.length;
491
414
  if (tryNumber > maxRetries) {
492
- throw new import_provider6.RetryError({
493
- message: `Failed after ${tryNumber} attemps. Last error: ${errorMessage}`,
415
+ throw new import_provider4.RetryError({
416
+ message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
494
417
  reason: "maxRetriesExceeded",
495
418
  errors: newErrors
496
419
  });
497
420
  }
498
- if (error instanceof Error && import_provider6.APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries) {
421
+ if (error instanceof Error && import_provider4.APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries) {
499
422
  await delay(delayInMs);
500
423
  return _retryWithExponentialBackoff(
501
424
  f,
@@ -506,8 +429,8 @@ async function _retryWithExponentialBackoff(f, {
506
429
  if (tryNumber === 1) {
507
430
  throw error;
508
431
  }
509
- throw new import_provider6.RetryError({
510
- message: `Failed after ${tryNumber} attemps with non-retryable error: '${errorMessage}'`,
432
+ throw new import_provider4.RetryError({
433
+ message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
511
434
  reason: "errorNotRetryable",
512
435
  errors: newErrors
513
436
  });
@@ -572,7 +495,7 @@ async function experimental_generateObject({
572
495
  });
573
496
  });
574
497
  if (generateResult.text === void 0) {
575
- throw new import_provider7.NoTextGeneratedError();
498
+ throw new import_provider5.NoTextGeneratedError();
576
499
  }
577
500
  result = generateResult.text;
578
501
  finishReason = generateResult.finishReason;
@@ -596,7 +519,7 @@ async function experimental_generateObject({
596
519
  })
597
520
  );
598
521
  if (generateResult.text === void 0) {
599
- throw new import_provider7.NoTextGeneratedError();
522
+ throw new import_provider5.NoTextGeneratedError();
600
523
  }
601
524
  result = generateResult.text;
602
525
  finishReason = generateResult.finishReason;
@@ -629,7 +552,7 @@ async function experimental_generateObject({
629
552
  );
630
553
  const functionArgs = (_b = (_a = generateResult.toolCalls) == null ? void 0 : _a[0]) == null ? void 0 : _b.args;
631
554
  if (functionArgs === void 0) {
632
- throw new import_provider7.NoTextGeneratedError();
555
+ throw new import_provider5.NoTextGeneratedError();
633
556
  }
634
557
  result = functionArgs;
635
558
  finishReason = generateResult.finishReason;
@@ -645,7 +568,7 @@ async function experimental_generateObject({
645
568
  throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
646
569
  }
647
570
  }
648
- const parseResult = safeParseJSON({ text: result, schema });
571
+ const parseResult = (0, import_provider_utils3.safeParseJSON)({ text: result, schema });
649
572
  if (!parseResult.success) {
650
573
  throw parseResult.error;
651
574
  }
@@ -718,7 +641,7 @@ function isDeepEqualData(obj1, obj2) {
718
641
  }
719
642
 
720
643
  // core/util/parse-partial-json.ts
721
- var import_secure_json_parse2 = __toESM(require("secure-json-parse"));
644
+ var import_secure_json_parse = __toESM(require("secure-json-parse"));
722
645
 
723
646
  // core/util/fix-json.ts
724
647
  function fixJson(input) {
@@ -1043,11 +966,11 @@ function parsePartialJson(jsonText) {
1043
966
  return void 0;
1044
967
  }
1045
968
  try {
1046
- return import_secure_json_parse2.default.parse(jsonText);
969
+ return import_secure_json_parse.default.parse(jsonText);
1047
970
  } catch (ignored) {
1048
971
  try {
1049
972
  const fixedJsonText = fixJson(jsonText);
1050
- return import_secure_json_parse2.default.parse(fixedJsonText);
973
+ return import_secure_json_parse.default.parse(fixedJsonText);
1051
974
  } catch (ignored2) {
1052
975
  }
1053
976
  }
@@ -1209,28 +1132,29 @@ var StreamObjectResult = class {
1209
1132
  };
1210
1133
 
1211
1134
  // core/generate-text/tool-call.ts
1212
- var import_provider8 = require("@ai-sdk/provider");
1135
+ var import_provider6 = require("@ai-sdk/provider");
1136
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
1213
1137
  function parseToolCall({
1214
1138
  toolCall,
1215
1139
  tools
1216
1140
  }) {
1217
1141
  const toolName = toolCall.toolName;
1218
1142
  if (tools == null) {
1219
- throw new import_provider8.NoSuchToolError({ toolName: toolCall.toolName });
1143
+ throw new import_provider6.NoSuchToolError({ toolName: toolCall.toolName });
1220
1144
  }
1221
1145
  const tool2 = tools[toolName];
1222
1146
  if (tool2 == null) {
1223
- throw new import_provider8.NoSuchToolError({
1147
+ throw new import_provider6.NoSuchToolError({
1224
1148
  toolName: toolCall.toolName,
1225
1149
  availableTools: Object.keys(tools)
1226
1150
  });
1227
1151
  }
1228
- const parseResult = safeParseJSON({
1152
+ const parseResult = (0, import_provider_utils4.safeParseJSON)({
1229
1153
  text: toolCall.args,
1230
1154
  schema: tool2.parameters
1231
1155
  });
1232
1156
  if (parseResult.success === false) {
1233
- throw new import_provider8.InvalidToolArgumentsError({
1157
+ throw new import_provider6.InvalidToolArgumentsError({
1234
1158
  toolName,
1235
1159
  toolArgs: toolCall.args,
1236
1160
  cause: parseResult.error
@@ -1327,7 +1251,7 @@ var GenerateTextResult = class {
1327
1251
  };
1328
1252
 
1329
1253
  // core/generate-text/run-tools-transformation.ts
1330
- var import_provider9 = require("@ai-sdk/provider");
1254
+ var import_provider7 = require("@ai-sdk/provider");
1331
1255
 
1332
1256
  // shared/generate-id.ts
1333
1257
  var import_non_secure = require("nanoid/non-secure");
@@ -1363,7 +1287,7 @@ function runToolsTransformation({
1363
1287
  if (tools == null) {
1364
1288
  toolResultsStreamController.enqueue({
1365
1289
  type: "error",
1366
- error: new import_provider9.NoSuchToolError({ toolName: chunk.toolName })
1290
+ error: new import_provider7.NoSuchToolError({ toolName: chunk.toolName })
1367
1291
  });
1368
1292
  break;
1369
1293
  }
@@ -1371,7 +1295,7 @@ function runToolsTransformation({
1371
1295
  if (tool2 == null) {
1372
1296
  toolResultsStreamController.enqueue({
1373
1297
  type: "error",
1374
- error: new import_provider9.NoSuchToolError({
1298
+ error: new import_provider7.NoSuchToolError({
1375
1299
  toolName: chunk.toolName,
1376
1300
  availableTools: Object.keys(tools)
1377
1301
  })
@@ -1565,7 +1489,7 @@ var StreamTextResult = class {
1565
1489
  @returns an `AIStream` object.
1566
1490
  */
1567
1491
  toAIStream(callbacks) {
1568
- return readableFromAsyncIterable(this.textStream).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
1492
+ return this.textStream.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
1569
1493
  }
1570
1494
  /**
1571
1495
  Creates a simple text stream response.
@@ -1699,9 +1623,9 @@ var toolCallStreamPart = {
1699
1623
  code: "7",
1700
1624
  name: "tool_calls",
1701
1625
  parse: (value) => {
1702
- if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some((tc) => {
1703
- 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";
1704
- })) {
1626
+ 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(
1627
+ (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"
1628
+ )) {
1705
1629
  throw new Error(
1706
1630
  '"tool_calls" parts expect an object with a ToolCallPayload.'
1707
1631
  );
@@ -1779,6 +1703,49 @@ function formatStreamPart(type, value) {
1779
1703
  `;
1780
1704
  }
1781
1705
 
1706
+ // shared/read-data-stream.ts
1707
+ var NEWLINE = "\n".charCodeAt(0);
1708
+ function concatChunks(chunks, totalLength) {
1709
+ const concatenatedChunks = new Uint8Array(totalLength);
1710
+ let offset = 0;
1711
+ for (const chunk of chunks) {
1712
+ concatenatedChunks.set(chunk, offset);
1713
+ offset += chunk.length;
1714
+ }
1715
+ chunks.length = 0;
1716
+ return concatenatedChunks;
1717
+ }
1718
+ async function* readDataStream(reader, {
1719
+ isAborted
1720
+ } = {}) {
1721
+ const decoder = new TextDecoder();
1722
+ const chunks = [];
1723
+ let totalLength = 0;
1724
+ while (true) {
1725
+ const { value } = await reader.read();
1726
+ if (value) {
1727
+ chunks.push(value);
1728
+ totalLength += value.length;
1729
+ if (value[value.length - 1] !== NEWLINE) {
1730
+ continue;
1731
+ }
1732
+ }
1733
+ if (chunks.length === 0) {
1734
+ break;
1735
+ }
1736
+ const concatenatedChunks = concatChunks(chunks, totalLength);
1737
+ totalLength = 0;
1738
+ const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
1739
+ for (const streamPart of streamParts2) {
1740
+ yield streamPart;
1741
+ }
1742
+ if (isAborted == null ? void 0 : isAborted()) {
1743
+ reader.cancel();
1744
+ break;
1745
+ }
1746
+ }
1747
+ }
1748
+
1782
1749
  // shared/utils.ts
1783
1750
  function createChunkDecoder(complex) {
1784
1751
  const decoder = new TextDecoder();
@@ -2752,49 +2719,6 @@ async function ReplicateStream(res, cb, options) {
2752
2719
  );
2753
2720
  }
2754
2721
 
2755
- // shared/read-data-stream.ts
2756
- var NEWLINE = "\n".charCodeAt(0);
2757
- function concatChunks(chunks, totalLength) {
2758
- const concatenatedChunks = new Uint8Array(totalLength);
2759
- let offset = 0;
2760
- for (const chunk of chunks) {
2761
- concatenatedChunks.set(chunk, offset);
2762
- offset += chunk.length;
2763
- }
2764
- chunks.length = 0;
2765
- return concatenatedChunks;
2766
- }
2767
- async function* readDataStream(reader, {
2768
- isAborted
2769
- } = {}) {
2770
- const decoder = new TextDecoder();
2771
- const chunks = [];
2772
- let totalLength = 0;
2773
- while (true) {
2774
- const { value } = await reader.read();
2775
- if (value) {
2776
- chunks.push(value);
2777
- totalLength += value.length;
2778
- if (value[value.length - 1] !== NEWLINE) {
2779
- continue;
2780
- }
2781
- }
2782
- if (chunks.length === 0) {
2783
- break;
2784
- }
2785
- const concatenatedChunks = concatChunks(chunks, totalLength);
2786
- totalLength = 0;
2787
- const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
2788
- for (const streamPart of streamParts2) {
2789
- yield streamPart;
2790
- }
2791
- if (isAborted == null ? void 0 : isAborted()) {
2792
- reader.cancel();
2793
- break;
2794
- }
2795
- }
2796
- }
2797
-
2798
2722
  // shared/parse-complex-response.ts
2799
2723
  function assignAnnotationsToMessage(message, annotations) {
2800
2724
  if (!message || !annotations || !annotations.length)
@@ -3020,9 +2944,12 @@ function streamToResponse(res, response, init) {
3020
2944
  experimental_generateText,
3021
2945
  experimental_streamObject,
3022
2946
  experimental_streamText,
2947
+ formatStreamPart,
3023
2948
  generateId,
3024
2949
  isStreamStringEqualToType,
3025
2950
  nanoid,
2951
+ parseStreamPart,
2952
+ readDataStream,
3026
2953
  readableFromAsyncIterable,
3027
2954
  streamToResponse,
3028
2955
  tool,