@xsai/stream-text 0.0.20 → 0.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ChatCompletionOptions, FinishReason } from '@xsai/shared-chat-completion';
1
+ import { ChatCompletionOptions, FinishReason } from '@xsai/shared-chat';
2
2
 
3
3
  interface StreamTextOptions extends ChatCompletionOptions {
4
4
  /** if you want to disable stream, use `@xsai/generate-text` */
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { chatCompletion } from '@xsai/shared-chat-completion';
1
+ import { chatCompletion } from '@xsai/shared-chat';
2
2
 
3
3
  const dataHeaderPrefix = "data: ";
4
4
  const dataErrorPrefix = `{"error":`;
@@ -12,9 +12,13 @@ const streamText = async (options) => await chatCompletion({
12
12
  const decoder = new TextDecoder();
13
13
  let finishReason;
14
14
  let usage;
15
+ let buffer = "";
15
16
  const rawChunkStream = res.body.pipeThrough(new TransformStream({
16
17
  transform: (chunk, controller) => {
17
- for (const line of decoder.decode(chunk).split("\n").filter((line2) => line2)) {
18
+ buffer += decoder.decode(chunk);
19
+ const lines = buffer.split("\n\n");
20
+ buffer = lines.pop() || "";
21
+ for (const line of lines) {
18
22
  if (!line || !line.startsWith(dataHeaderPrefix)) {
19
23
  continue;
20
24
  }
@@ -27,7 +31,7 @@ const streamText = async (options) => await chatCompletion({
27
31
  controller.terminate();
28
32
  continue;
29
33
  }
30
- const data = JSON.parse(line.slice(6));
34
+ const data = JSON.parse(lineWithoutPrefix);
31
35
  controller.enqueue(data);
32
36
  if (data.choices[0].finish_reason) {
33
37
  finishReason = data.choices[0].finish_reason;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xsai/stream-text",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "type": "module",
5
5
  "author": "Moeru AI",
6
6
  "license": "MIT",
@@ -30,7 +30,7 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "@xsai/shared-chat-completion": ""
33
+ "@xsai/shared-chat": ""
34
34
  },
35
35
  "devDependencies": {
36
36
  "@xsai/providers": "",