@xsai/stream-text 0.0.11 → 0.0.13

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
@@ -16,6 +16,7 @@ interface StreamTextResponseUsage {
16
16
  total_tokens: number;
17
17
  }
18
18
  interface StreamTextResult {
19
+ chunkStream: ReadableStream<StreamTextResponse>;
19
20
  finishReason?: FinishReason;
20
21
  textStream: ReadableStream<string>;
21
22
  usage?: StreamTextResponseUsage;
package/dist/index.js CHANGED
@@ -10,12 +10,12 @@ const streamText = async (options) => await chatCompletion({
10
10
  const decoder = new TextDecoder();
11
11
  let finishReason;
12
12
  let usage;
13
- const textStream = res.body.pipeThrough(new TransformStream({
13
+ const rawChunkStream = res.body.pipeThrough(new TransformStream({
14
14
  transform: (chunk, controller) => {
15
15
  for (const line of decoder.decode(chunk).split("\n").filter((line2) => line2)) {
16
16
  if (line !== "data: [DONE]") {
17
17
  const data = JSON.parse(line.slice(6));
18
- controller.enqueue(data.choices[0].delta.content);
18
+ controller.enqueue(data);
19
19
  if (data.choices[0].finish_reason) {
20
20
  finishReason = data.choices[0].finish_reason;
21
21
  }
@@ -27,7 +27,11 @@ const streamText = async (options) => await chatCompletion({
27
27
  }
28
28
  }
29
29
  }));
30
- return { finishReason, textStream, usage };
30
+ const [chunkStream, rawTextStream] = rawChunkStream.tee();
31
+ const textStream = rawTextStream.pipeThrough(new TransformStream({
32
+ transform: (chunk, controller) => controller.enqueue(chunk.choices[0].delta.content)
33
+ }));
34
+ return { chunkStream, finishReason, textStream, usage };
31
35
  });
32
36
 
33
37
  export { streamText as default, streamText };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xsai/stream-text",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "type": "module",
5
5
  "author": "Moeru AI",
6
6
  "license": "MIT",
@@ -25,6 +25,10 @@
25
25
  "dependencies": {
26
26
  "@xsai/shared-chat-completion": ""
27
27
  },
28
+ "devDependencies": {
29
+ "@xsai/providers": "",
30
+ "@xsai/shared": ""
31
+ },
28
32
  "scripts": {
29
33
  "build": "pkgroll",
30
34
  "build:watch": "pkgroll --watch",