@xsai/stream-text 0.0.10 → 0.0.12

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/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 藍+85CD
3
+ Copyright (c) 2024 Moeru AI
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { GenerateTextOptions, FinishReason, GenerateTextResponseUsage } from '@xsai/generate-text';
1
+ import { ChatCompletionOptions, FinishReason } from '@xsai/shared-chat-completion';
2
2
 
3
- interface StreamTextOptions extends GenerateTextOptions {
3
+ interface StreamTextOptions extends ChatCompletionOptions {
4
4
  streamOptions?: {
5
5
  /**
6
6
  * Return usage.
@@ -10,10 +10,32 @@ interface StreamTextOptions extends GenerateTextOptions {
10
10
  usage?: boolean;
11
11
  };
12
12
  }
13
+ interface StreamTextResponseUsage {
14
+ completion_tokens: number;
15
+ prompt_tokens: number;
16
+ total_tokens: number;
17
+ }
13
18
  interface StreamTextResult {
19
+ chunkStream: ReadableStream<StreamTextResponse>;
14
20
  finishReason?: FinishReason;
15
21
  textStream: ReadableStream<string>;
16
- usage?: GenerateTextResponseUsage;
22
+ usage?: StreamTextResponseUsage;
23
+ }
24
+ interface StreamTextResponse {
25
+ choices: {
26
+ delta: {
27
+ content: string;
28
+ role: 'assistant';
29
+ };
30
+ finish_reason?: FinishReason;
31
+ index: number;
32
+ }[];
33
+ created: number;
34
+ id: string;
35
+ model: string;
36
+ object: 'chat.completion.chunk';
37
+ system_fingerprint: string;
38
+ usage?: StreamTextResponseUsage;
17
39
  }
18
40
  /**
19
41
  * @experimental
@@ -21,4 +43,4 @@ interface StreamTextResult {
21
43
  */
22
44
  declare const streamText: (options: StreamTextOptions) => Promise<StreamTextResult>;
23
45
 
24
- export { type StreamTextOptions, type StreamTextResult, streamText as default, streamText };
46
+ export { type StreamTextOptions, type StreamTextResponse, type StreamTextResponseUsage, type StreamTextResult, streamText as default, streamText };
package/dist/index.js CHANGED
@@ -1,47 +1,37 @@
1
- import { requestUrl, clean, objCamelToSnake } from '@xsai/shared';
1
+ import { chatCompletion } from '@xsai/shared-chat-completion';
2
2
 
3
- const streamText = async (options) => await fetch(requestUrl(options.path ?? "chat/completions", options.base), {
4
- body: JSON.stringify(clean({
5
- ...objCamelToSnake(options),
6
- abortSignal: void 0,
7
- base: void 0,
8
- headers: void 0,
9
- path: void 0,
10
- stream: true,
11
- tools: options.tools?.map((tool) => ({
12
- function: tool.function,
13
- type: "function"
14
- }))
15
- })),
16
- headers: {
17
- "Content-Type": "application/json",
18
- ...options.headers
19
- },
20
- method: "POST",
21
- signal: options.abortSignal
3
+ const streamText = async (options) => await chatCompletion({
4
+ ...options,
5
+ stream: true
22
6
  }).then((res) => {
7
+ if (!res.body) {
8
+ return Promise.reject(res);
9
+ }
10
+ const decoder = new TextDecoder();
23
11
  let finishReason;
24
12
  let usage;
25
- const decoder = new TextDecoder();
26
- const transformStream = new TransformStream({
13
+ const rawChunkStream = res.body.pipeThrough(new TransformStream({
27
14
  transform: (chunk, controller) => {
28
- for (const line of decoder.decode(chunk).split("\n")) {
29
- if (line.startsWith("data: ") && line !== "data: [DONE]") {
15
+ for (const line of decoder.decode(chunk).split("\n").filter((line2) => line2)) {
16
+ if (line !== "data: [DONE]") {
30
17
  const data = JSON.parse(line.slice(6));
31
- controller.enqueue(data.choices[0].delta.content);
18
+ controller.enqueue(data);
32
19
  if (data.choices[0].finish_reason) {
33
20
  finishReason = data.choices[0].finish_reason;
34
21
  }
35
22
  if (data.usage)
36
23
  usage = data.usage;
37
- } else if (line === "data: [DONE]") {
24
+ } else {
38
25
  controller.terminate();
39
26
  }
40
27
  }
41
28
  }
42
- });
43
- const textStream = res.body.pipeThrough(transformStream);
44
- return { finishReason, textStream, usage };
29
+ }));
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 };
45
35
  });
46
36
 
47
37
  export { streamText as default, streamText };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@xsai/stream-text",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "type": "module",
5
- "author": "藍+85CD",
5
+ "author": "Moeru AI",
6
6
  "license": "MIT",
7
- "homepage": "https://github.com/moeru-ai/xsai",
7
+ "homepage": "https://xsai.js.org",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/moeru-ai/xsai.git",
@@ -23,8 +23,11 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@xsai/generate-text": "^0.0.10",
27
- "@xsai/shared": "^0.0.10"
26
+ "@xsai/shared-chat-completion": ""
27
+ },
28
+ "devDependencies": {
29
+ "@xsai/providers": "",
30
+ "@xsai/shared": ""
28
31
  },
29
32
  "scripts": {
30
33
  "build": "pkgroll",