@xsai/stream-text 0.0.21 → 0.0.23

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,6 @@
1
- import { ChatCompletionOptions, FinishReason } from '@xsai/shared-chat-completion';
1
+ import { ChatOptions, FinishReason } from '@xsai/shared-chat';
2
2
 
3
- interface StreamTextOptions extends ChatCompletionOptions {
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,13 +1,21 @@
1
- import { chatCompletion } from '@xsai/shared-chat-completion';
1
+ import { chat, ChatError } from '@xsai/shared-chat';
2
2
 
3
3
  const dataHeaderPrefix = "data: ";
4
4
  const dataErrorPrefix = `{"error":`;
5
- const streamText = async (options) => await chatCompletion({
5
+ const streamText = async (options) => await chat({
6
6
  ...options,
7
7
  stream: true
8
- }).then((res) => {
8
+ }).then(async (res) => {
9
+ if (!res.ok) {
10
+ const error = new ChatError(`Remote sent ${res.status} response`, res);
11
+ error.cause = new Error(await res.text());
12
+ }
9
13
  if (!res.body) {
10
- return Promise.reject(res);
14
+ throw new ChatError("Response body is empty from remote server", res);
15
+ }
16
+ if (!(res.body instanceof ReadableStream)) {
17
+ const error = new ChatError(`Expected Response body to be a ReadableStream, but got ${String(res.body)}`, res);
18
+ error.cause = new Error(`Content-Type is ${res.headers.get("Content-Type")}`);
11
19
  }
12
20
  const decoder = new TextDecoder();
13
21
  let finishReason;
@@ -24,12 +32,12 @@ const streamText = async (options) => await chatCompletion({
24
32
  }
25
33
  if (line.startsWith(dataErrorPrefix)) {
26
34
  controller.error(new Error(`Error from server: ${line}`));
27
- continue;
35
+ break;
28
36
  }
29
37
  const lineWithoutPrefix = line.slice(dataHeaderPrefix.length);
30
38
  if (lineWithoutPrefix === "[DONE]") {
31
39
  controller.terminate();
32
- continue;
40
+ break;
33
41
  }
34
42
  const data = JSON.parse(lineWithoutPrefix);
35
43
  controller.enqueue(data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xsai/stream-text",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "type": "module",
5
5
  "author": "Moeru AI",
6
6
  "license": "MIT",
@@ -30,7 +30,7 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "@xsai/shared-chat-completion": ""
33
+ "@xsai/shared-chat": ""
34
34
  },
35
35
  "devDependencies": {
36
36
  "@xsai/providers": "",