@xsai/stream-text 0.0.22 → 0.0.24
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 +2 -2
- package/dist/index.js +5 -8
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatOptions, FinishReason } from '@xsai/shared-chat';
|
|
2
2
|
|
|
3
|
-
interface StreamTextOptions extends
|
|
3
|
+
interface StreamTextOptions extends ChatOptions {
|
|
4
4
|
/** if you want to disable stream, use `@xsai/generate-text` */
|
|
5
5
|
stream?: never;
|
|
6
6
|
streamOptions?: {
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { chat } from '@xsai/shared-chat';
|
|
2
2
|
|
|
3
3
|
const dataHeaderPrefix = "data: ";
|
|
4
4
|
const dataErrorPrefix = `{"error":`;
|
|
5
|
-
const streamText = async (options) => await
|
|
5
|
+
const streamText = async (options) => await chat({
|
|
6
6
|
...options,
|
|
7
7
|
stream: true
|
|
8
|
-
}).then((res) => {
|
|
9
|
-
if (!res.body) {
|
|
10
|
-
return Promise.reject(res);
|
|
11
|
-
}
|
|
8
|
+
}).then(async (res) => {
|
|
12
9
|
const decoder = new TextDecoder();
|
|
13
10
|
let finishReason;
|
|
14
11
|
let usage;
|
|
@@ -24,12 +21,12 @@ const streamText = async (options) => await chatCompletion({
|
|
|
24
21
|
}
|
|
25
22
|
if (line.startsWith(dataErrorPrefix)) {
|
|
26
23
|
controller.error(new Error(`Error from server: ${line}`));
|
|
27
|
-
|
|
24
|
+
break;
|
|
28
25
|
}
|
|
29
26
|
const lineWithoutPrefix = line.slice(dataHeaderPrefix.length);
|
|
30
27
|
if (lineWithoutPrefix === "[DONE]") {
|
|
31
28
|
controller.terminate();
|
|
32
|
-
|
|
29
|
+
break;
|
|
33
30
|
}
|
|
34
31
|
const data = JSON.parse(lineWithoutPrefix);
|
|
35
32
|
controller.enqueue(data);
|