ai 4.0.25 → 4.0.27

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.
package/dist/index.mjs CHANGED
@@ -225,7 +225,7 @@ import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
225
225
 
226
226
  // util/delay.ts
227
227
  async function delay(delayInMs) {
228
- return delayInMs === void 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delayInMs));
228
+ return delayInMs == null ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delayInMs));
229
229
  }
230
230
 
231
231
  // util/retry-error.ts
@@ -5309,37 +5309,50 @@ var DefaultStreamTextResult = class {
5309
5309
  };
5310
5310
 
5311
5311
  // core/generate-text/smooth-stream.ts
5312
+ import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
5313
+ var CHUNKING_REGEXPS = {
5314
+ word: /\s*\S+\s+/m,
5315
+ line: /[^\n]*\n/m
5316
+ };
5312
5317
  function smoothStream({
5313
5318
  delayInMs = 10,
5319
+ chunking = "word",
5314
5320
  _internal: { delay: delay2 = delay } = {}
5315
5321
  } = {}) {
5316
- let buffer = "";
5317
- return () => new TransformStream({
5318
- async transform(chunk, controller) {
5319
- if (chunk.type === "step-finish") {
5320
- if (buffer.length > 0) {
5321
- controller.enqueue({ type: "text-delta", textDelta: buffer });
5322
- buffer = "";
5322
+ const chunkingRegexp = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
5323
+ if (chunkingRegexp == null) {
5324
+ throw new InvalidArgumentError2({
5325
+ argument: "chunking",
5326
+ message: `Chunking must be "word" or "line" or a RegExp. Received: ${chunking}`
5327
+ });
5328
+ }
5329
+ return () => {
5330
+ let buffer = "";
5331
+ return new TransformStream({
5332
+ async transform(chunk, controller) {
5333
+ if (chunk.type === "step-finish") {
5334
+ if (buffer.length > 0) {
5335
+ controller.enqueue({ type: "text-delta", textDelta: buffer });
5336
+ buffer = "";
5337
+ }
5338
+ controller.enqueue(chunk);
5339
+ return;
5323
5340
  }
5324
- controller.enqueue(chunk);
5325
- return;
5326
- }
5327
- if (chunk.type !== "text-delta") {
5328
- controller.enqueue(chunk);
5329
- return;
5330
- }
5331
- buffer += chunk.textDelta;
5332
- const regexp = /\s*\S+\s+/m;
5333
- while (regexp.test(buffer)) {
5334
- const chunk2 = buffer.match(regexp)[0];
5335
- controller.enqueue({ type: "text-delta", textDelta: chunk2 });
5336
- buffer = buffer.slice(chunk2.length);
5337
- if (delayInMs > 0) {
5341
+ if (chunk.type !== "text-delta") {
5342
+ controller.enqueue(chunk);
5343
+ return;
5344
+ }
5345
+ buffer += chunk.textDelta;
5346
+ let match;
5347
+ while ((match = chunkingRegexp.exec(buffer)) != null) {
5348
+ const chunk2 = match[0];
5349
+ controller.enqueue({ type: "text-delta", textDelta: chunk2 });
5350
+ buffer = buffer.slice(chunk2.length);
5338
5351
  await delay2(delayInMs);
5339
5352
  }
5340
5353
  }
5341
- }
5342
- });
5354
+ });
5355
+ };
5343
5356
  }
5344
5357
 
5345
5358
  // core/middleware/wrap-language-model.ts
@@ -5523,6 +5536,28 @@ function magnitude(vector) {
5523
5536
  return Math.sqrt(dotProduct(vector, vector));
5524
5537
  }
5525
5538
 
5539
+ // core/util/simulate-readable-stream.ts
5540
+ function simulateReadableStream({
5541
+ chunks,
5542
+ initialDelayInMs = 0,
5543
+ chunkDelayInMs = 0,
5544
+ _internal
5545
+ }) {
5546
+ var _a14;
5547
+ const delay2 = (_a14 = _internal == null ? void 0 : _internal.delay) != null ? _a14 : delay;
5548
+ let index = 0;
5549
+ return new ReadableStream({
5550
+ async pull(controller) {
5551
+ if (index < chunks.length) {
5552
+ await delay2(index === 0 ? initialDelayInMs : chunkDelayInMs);
5553
+ controller.enqueue(chunks[index++]);
5554
+ } else {
5555
+ controller.close();
5556
+ }
5557
+ }
5558
+ });
5559
+ }
5560
+
5526
5561
  // streams/assistant-response.ts
5527
5562
  import {
5528
5563
  formatAssistantStreamPart
@@ -5908,6 +5943,7 @@ export {
5908
5943
  pipeDataStreamToResponse,
5909
5944
  processDataStream,
5910
5945
  processTextStream,
5946
+ simulateReadableStream,
5911
5947
  smoothStream,
5912
5948
  streamObject,
5913
5949
  streamText,