ai 4.0.26 → 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,38 +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,
5314
5319
  chunking = "word",
5315
5320
  _internal: { delay: delay2 = delay } = {}
5316
5321
  } = {}) {
5317
- let buffer = "";
5318
- return () => new TransformStream({
5319
- async transform(chunk, controller) {
5320
- if (chunk.type === "step-finish") {
5321
- if (buffer.length > 0) {
5322
- controller.enqueue({ type: "text-delta", textDelta: buffer });
5323
- 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;
5324
5340
  }
5325
- controller.enqueue(chunk);
5326
- return;
5327
- }
5328
- if (chunk.type !== "text-delta") {
5329
- controller.enqueue(chunk);
5330
- return;
5331
- }
5332
- buffer += chunk.textDelta;
5333
- const regexp = chunking === "line" ? /[^\n]*\n/m : /\s*\S+\s+/m;
5334
- while (regexp.test(buffer)) {
5335
- const chunk2 = buffer.match(regexp)[0];
5336
- controller.enqueue({ type: "text-delta", textDelta: chunk2 });
5337
- buffer = buffer.slice(chunk2.length);
5338
- 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);
5339
5351
  await delay2(delayInMs);
5340
5352
  }
5341
5353
  }
5342
- }
5343
- });
5354
+ });
5355
+ };
5344
5356
  }
5345
5357
 
5346
5358
  // core/middleware/wrap-language-model.ts
@@ -5524,6 +5536,28 @@ function magnitude(vector) {
5524
5536
  return Math.sqrt(dotProduct(vector, vector));
5525
5537
  }
5526
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
+
5527
5561
  // streams/assistant-response.ts
5528
5562
  import {
5529
5563
  formatAssistantStreamPart
@@ -5909,6 +5943,7 @@ export {
5909
5943
  pipeDataStreamToResponse,
5910
5944
  processDataStream,
5911
5945
  processTextStream,
5946
+ simulateReadableStream,
5912
5947
  smoothStream,
5913
5948
  streamObject,
5914
5949
  streamText,