@xsai/stream-transcription 0.4.4 → 0.5.0-beta.1

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.
Files changed (2) hide show
  1. package/dist/index.js +19 -8
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,5 +1,20 @@
1
- import { DelayedPromise, requestURL, requestHeaders, responseCatch } from '@xsai/shared';
1
+ import { RemoteAPIError, JSONParseError, DelayedPromise, requestURL, requestHeaders, responseCatch } from '@xsai/shared';
2
2
 
3
+ const parseJSONChunk = (data) => {
4
+ if (data.startsWith("{") && data.includes('"error":')) {
5
+ throw new RemoteAPIError(`Error from server: ${data}`, {
6
+ responseBody: data
7
+ });
8
+ }
9
+ try {
10
+ return JSON.parse(data);
11
+ } catch (cause) {
12
+ throw new JSONParseError(`Failed to parse stream chunk JSON: ${data}`, {
13
+ cause,
14
+ text: data
15
+ });
16
+ }
17
+ };
3
18
  const parseChunk = (text) => {
4
19
  if (!text || !text.startsWith("data:"))
5
20
  return [void 0, false];
@@ -8,11 +23,7 @@ const parseChunk = (text) => {
8
23
  if (data.includes("[DONE]")) {
9
24
  return [void 0, true];
10
25
  }
11
- if (data.startsWith("{") && data.includes('"error":')) {
12
- throw new Error(`Error from server: ${data}`);
13
- }
14
- const chunk = JSON.parse(data);
15
- return [chunk, false];
26
+ return [parseJSONChunk(data), false];
16
27
  };
17
28
  const transformChunk = () => {
18
29
  const decoder = new TextDecoder();
@@ -28,11 +39,11 @@ const transformChunk = () => {
28
39
  const [chunk2, isEnd] = parseChunk(line);
29
40
  if (isEnd)
30
41
  break;
31
- if (chunk2) {
42
+ if (chunk2)
32
43
  controller.enqueue(chunk2);
33
- }
34
44
  } catch (error) {
35
45
  controller.error(error);
46
+ return;
36
47
  }
37
48
  }
38
49
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/stream-transcription",
3
3
  "type": "module",
4
- "version": "0.4.4",
4
+ "version": "0.5.0-beta.1",
5
5
  "description": "extra-small AI SDK.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",
@@ -29,7 +29,7 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@xsai/shared": "~0.4.4"
32
+ "@xsai/shared": "~0.5.0-beta.1"
33
33
  },
34
34
  "scripts": {
35
35
  "build": "pkgroll",