ai 4.3.0 → 4.3.2

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
@@ -4624,23 +4624,49 @@ var object = ({
4624
4624
  };
4625
4625
 
4626
4626
  // core/generate-text/smooth-stream.ts
4627
- import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
4628
4627
  import { delay as originalDelay } from "@ai-sdk/provider-utils";
4628
+ import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
4629
4629
  var CHUNKING_REGEXPS = {
4630
- word: /\s*\S+\s+/m,
4631
- line: /[^\n]*\n/m
4630
+ word: /\S+\s+/m,
4631
+ line: /\n+/m
4632
4632
  };
4633
4633
  function smoothStream({
4634
4634
  delayInMs = 10,
4635
4635
  chunking = "word",
4636
4636
  _internal: { delay: delay2 = originalDelay } = {}
4637
4637
  } = {}) {
4638
- const chunkingRegexp = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
4639
- if (chunkingRegexp == null) {
4640
- throw new InvalidArgumentError2({
4641
- argument: "chunking",
4642
- message: `Chunking must be "word" or "line" or a RegExp. Received: ${chunking}`
4643
- });
4638
+ let detectChunk;
4639
+ if (typeof chunking === "function") {
4640
+ detectChunk = (buffer) => {
4641
+ const match = chunking(buffer);
4642
+ if (match == null) {
4643
+ return null;
4644
+ }
4645
+ if (!match.length) {
4646
+ throw new Error(`Chunking function must return a non-empty string.`);
4647
+ }
4648
+ if (!buffer.startsWith(match)) {
4649
+ throw new Error(
4650
+ `Chunking function must return a match that is a prefix of the buffer. Received: "${match}" expected to start with "${buffer}"`
4651
+ );
4652
+ }
4653
+ return match;
4654
+ };
4655
+ } else {
4656
+ const chunkingRegex = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
4657
+ if (chunkingRegex == null) {
4658
+ throw new InvalidArgumentError2({
4659
+ argument: "chunking",
4660
+ message: `Chunking must be "word" or "line" or a RegExp. Received: ${chunking}`
4661
+ });
4662
+ }
4663
+ detectChunk = (buffer) => {
4664
+ const match = chunkingRegex.exec(buffer);
4665
+ if (!match) {
4666
+ return null;
4667
+ }
4668
+ return buffer.slice(0, match.index) + (match == null ? void 0 : match[0]);
4669
+ };
4644
4670
  }
4645
4671
  return () => {
4646
4672
  let buffer = "";
@@ -4656,10 +4682,9 @@ function smoothStream({
4656
4682
  }
4657
4683
  buffer += chunk.textDelta;
4658
4684
  let match;
4659
- while ((match = chunkingRegexp.exec(buffer)) != null) {
4660
- const chunk2 = match[0];
4661
- controller.enqueue({ type: "text-delta", textDelta: chunk2 });
4662
- buffer = buffer.slice(chunk2.length);
4685
+ while ((match = detectChunk(buffer)) != null) {
4686
+ controller.enqueue({ type: "text-delta", textDelta: match });
4687
+ buffer = buffer.slice(match.length);
4663
4688
  await delay2(delayInMs);
4664
4689
  }
4665
4690
  }
@@ -4677,6 +4702,25 @@ function asArray(value) {
4677
4702
  return value === void 0 ? [] : Array.isArray(value) ? value : [value];
4678
4703
  }
4679
4704
 
4705
+ // util/consume-stream.ts
4706
+ async function consumeStream({
4707
+ stream,
4708
+ onError
4709
+ }) {
4710
+ const reader = stream.getReader();
4711
+ try {
4712
+ while (true) {
4713
+ const { done } = await reader.read();
4714
+ if (done)
4715
+ break;
4716
+ }
4717
+ } catch (error) {
4718
+ onError == null ? void 0 : onError(error);
4719
+ } finally {
4720
+ reader.releaseLock();
4721
+ }
4722
+ }
4723
+
4680
4724
  // core/util/merge-streams.ts
4681
4725
  function mergeStreams(stream1, stream2) {
4682
4726
  const reader1 = stream1.getReader();
@@ -5918,9 +5962,15 @@ var DefaultStreamTextResult = class {
5918
5962
  )
5919
5963
  );
5920
5964
  }
5921
- async consumeStream() {
5922
- const stream = this.fullStream;
5923
- for await (const part of stream) {
5965
+ async consumeStream(options) {
5966
+ var _a17;
5967
+ try {
5968
+ await consumeStream({
5969
+ stream: this.fullStream,
5970
+ onError: options == null ? void 0 : options.onError
5971
+ });
5972
+ } catch (error) {
5973
+ (_a17 = options == null ? void 0 : options.onError) == null ? void 0 : _a17.call(options, error);
5924
5974
  }
5925
5975
  }
5926
5976
  get experimental_partialOutputStream() {