@xsai/stream-text 0.0.26 → 0.0.28

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,6 +1,7 @@
1
- import { ChatOptions, FinishReason } from '@xsai/shared-chat';
1
+ import { ChatOptions, FinishReason, Usage } from '@xsai/shared-chat';
2
2
 
3
3
  interface StreamTextOptions extends ChatOptions {
4
+ onChunk?: (chunk: ChunkResult) => Promise<void> | void;
4
5
  /** if you want to disable stream, use `@xsai/generate-{text,object}` */
5
6
  stream?: never;
6
7
  streamOptions?: {
@@ -12,18 +13,13 @@ interface StreamTextOptions extends ChatOptions {
12
13
  usage?: boolean;
13
14
  };
14
15
  }
15
- interface StreamTextResponseUsage {
16
- completion_tokens: number;
17
- prompt_tokens: number;
18
- total_tokens: number;
19
- }
20
16
  interface StreamTextResult {
21
- chunkStream: ReadableStream<StreamTextResponse>;
17
+ chunkStream: ReadableStream<ChunkResult>;
22
18
  finishReason?: FinishReason;
23
19
  textStream: ReadableStream<string>;
24
- usage?: StreamTextResponseUsage;
20
+ usage?: Usage;
25
21
  }
26
- interface StreamTextResponse {
22
+ interface ChunkResult {
27
23
  choices: {
28
24
  delta: {
29
25
  content: string;
@@ -37,11 +33,11 @@ interface StreamTextResponse {
37
33
  model: string;
38
34
  object: 'chat.completion.chunk';
39
35
  system_fingerprint: string;
40
- usage?: StreamTextResponseUsage;
36
+ usage?: Usage;
41
37
  }
42
38
  /**
43
39
  * @experimental WIP, does not support function calling (tools).
44
40
  */
45
41
  declare const streamText: (options: StreamTextOptions) => Promise<StreamTextResult>;
46
42
 
47
- export { type StreamTextOptions, type StreamTextResponse, type StreamTextResponseUsage, type StreamTextResult, streamText as default, streamText };
43
+ export { type ChunkResult, type StreamTextOptions, type StreamTextResult, streamText as default, streamText };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { chat } from '@xsai/shared-chat';
2
2
 
3
- const dataHeaderPrefix = "data: ";
4
- const dataErrorPrefix = `{"error":`;
3
+ const chunkHeaderPrefix = "data: ";
4
+ const chunkErrorPrefix = `{"error":`;
5
5
  const streamText = async (options) => await chat({
6
6
  ...options,
7
7
  stream: true
@@ -11,30 +11,32 @@ const streamText = async (options) => await chat({
11
11
  let usage;
12
12
  let buffer = "";
13
13
  const rawChunkStream = res.body.pipeThrough(new TransformStream({
14
- transform: (chunk, controller) => {
14
+ transform: async (chunk, controller) => {
15
15
  buffer += decoder.decode(chunk);
16
16
  const lines = buffer.split("\n\n");
17
17
  buffer = lines.pop() || "";
18
18
  for (const line of lines) {
19
- if (!line || !line.startsWith(dataHeaderPrefix)) {
19
+ if (!line || !line.startsWith(chunkHeaderPrefix)) {
20
20
  continue;
21
21
  }
22
- if (line.startsWith(dataErrorPrefix)) {
22
+ if (line.startsWith(chunkErrorPrefix)) {
23
23
  controller.error(new Error(`Error from server: ${line}`));
24
24
  break;
25
25
  }
26
- const lineWithoutPrefix = line.slice(dataHeaderPrefix.length);
26
+ const lineWithoutPrefix = line.slice(chunkHeaderPrefix.length);
27
27
  if (lineWithoutPrefix === "[DONE]") {
28
28
  controller.terminate();
29
29
  break;
30
30
  }
31
- const data = JSON.parse(lineWithoutPrefix);
32
- controller.enqueue(data);
33
- if (data.choices[0].finish_reason) {
34
- finishReason = data.choices[0].finish_reason;
31
+ const chunk2 = JSON.parse(lineWithoutPrefix);
32
+ controller.enqueue(chunk2);
33
+ if (options.onChunk)
34
+ await options.onChunk(chunk2);
35
+ if (chunk2.choices[0].finish_reason) {
36
+ finishReason = chunk2.choices[0].finish_reason;
35
37
  }
36
- if (data.usage) {
37
- usage = data.usage;
38
+ if (chunk2.usage) {
39
+ usage = chunk2.usage;
38
40
  }
39
41
  }
40
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xsai/stream-text",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "type": "module",
5
5
  "author": "Moeru AI",
6
6
  "license": "MIT",