ai 4.3.0 → 4.3.1
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/CHANGELOG.md +6 -0
- package/dist/index.d.mts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +38 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +38 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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: /\
|
4631
|
-
line:
|
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
|
-
|
4639
|
-
if (
|
4640
|
-
|
4641
|
-
|
4642
|
-
|
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 =
|
4660
|
-
|
4661
|
-
|
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
|
}
|