@xsai/stream-text 0.0.9 → 0.0.10

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,12 +1,24 @@
1
- import { GenerateTextOptions } from '@xsai/generate-text';
1
+ import { GenerateTextOptions, FinishReason, GenerateTextResponseUsage } from '@xsai/generate-text';
2
2
 
3
+ interface StreamTextOptions extends GenerateTextOptions {
4
+ streamOptions?: {
5
+ /**
6
+ * Return usage.
7
+ * @default `undefined`
8
+ * @remarks Ollama doesn't support this, see {@link https://github.com/ollama/ollama/issues/5200}
9
+ */
10
+ usage?: boolean;
11
+ };
12
+ }
3
13
  interface StreamTextResult {
14
+ finishReason?: FinishReason;
4
15
  textStream: ReadableStream<string>;
16
+ usage?: GenerateTextResponseUsage;
5
17
  }
6
18
  /**
7
19
  * @experimental
8
20
  * WIP, currently only returns `textStream`, does not support function calling (tools).
9
21
  */
10
- declare const streamText: (options: GenerateTextOptions) => Promise<StreamTextResult>;
22
+ declare const streamText: (options: StreamTextOptions) => Promise<StreamTextResult>;
11
23
 
12
- export { type StreamTextResult, streamText as default, streamText };
24
+ export { type StreamTextOptions, type StreamTextResult, streamText as default, streamText };
package/dist/index.js CHANGED
@@ -20,25 +20,28 @@ const streamText = async (options) => await fetch(requestUrl(options.path ?? "ch
20
20
  method: "POST",
21
21
  signal: options.abortSignal
22
22
  }).then((res) => {
23
+ let finishReason;
24
+ let usage;
23
25
  const decoder = new TextDecoder();
24
26
  const transformStream = new TransformStream({
25
27
  transform: (chunk, controller) => {
26
- const text = decoder.decode(chunk);
27
- for (const line of text.split("\n")) {
28
- if (line.includes("[DONE]")) {
29
- controller.terminate();
30
- return;
31
- }
32
- try {
28
+ for (const line of decoder.decode(chunk).split("\n")) {
29
+ if (line.startsWith("data: ") && line !== "data: [DONE]") {
33
30
  const data = JSON.parse(line.slice(6));
34
31
  controller.enqueue(data.choices[0].delta.content);
35
- } catch {
32
+ if (data.choices[0].finish_reason) {
33
+ finishReason = data.choices[0].finish_reason;
34
+ }
35
+ if (data.usage)
36
+ usage = data.usage;
37
+ } else if (line === "data: [DONE]") {
38
+ controller.terminate();
36
39
  }
37
40
  }
38
41
  }
39
42
  });
40
43
  const textStream = res.body.pipeThrough(transformStream);
41
- return { textStream };
44
+ return { finishReason, textStream, usage };
42
45
  });
43
46
 
44
47
  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.9",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "author": "藍+85CD",
6
6
  "license": "MIT",
@@ -23,8 +23,8 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@xsai/generate-text": "^0.0.9",
27
- "@xsai/shared": "^0.0.9"
26
+ "@xsai/generate-text": "^0.0.10",
27
+ "@xsai/shared": "^0.0.10"
28
28
  },
29
29
  "scripts": {
30
30
  "build": "pkgroll",