@xsai/stream-transcription 0.4.4 → 0.5.0-beta.2
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.js +9 -52
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -1,49 +1,9 @@
|
|
|
1
1
|
import { DelayedPromise, requestURL, requestHeaders, responseCatch } from '@xsai/shared';
|
|
2
|
-
|
|
3
|
-
const parseChunk = (text) => {
|
|
4
|
-
if (!text || !text.startsWith("data:"))
|
|
5
|
-
return [void 0, false];
|
|
6
|
-
const content = text.slice("data:".length);
|
|
7
|
-
const data = content.startsWith(" ") ? content.slice(1) : content;
|
|
8
|
-
if (data.includes("[DONE]")) {
|
|
9
|
-
return [void 0, true];
|
|
10
|
-
}
|
|
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];
|
|
16
|
-
};
|
|
17
|
-
const transformChunk = () => {
|
|
18
|
-
const decoder = new TextDecoder();
|
|
19
|
-
let buffer = "";
|
|
20
|
-
return new TransformStream({
|
|
21
|
-
transform: async (chunk, controller) => {
|
|
22
|
-
const text = decoder.decode(chunk, { stream: true });
|
|
23
|
-
buffer += text;
|
|
24
|
-
const lines = buffer.split("\n");
|
|
25
|
-
buffer = lines.pop() ?? "";
|
|
26
|
-
for (const line of lines) {
|
|
27
|
-
try {
|
|
28
|
-
const [chunk2, isEnd] = parseChunk(line);
|
|
29
|
-
if (isEnd)
|
|
30
|
-
break;
|
|
31
|
-
if (chunk2) {
|
|
32
|
-
controller.enqueue(chunk2);
|
|
33
|
-
}
|
|
34
|
-
} catch (error) {
|
|
35
|
-
controller.error(error);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
};
|
|
2
|
+
import { createControlledStream, closeControllers, errorControllers, EventSourceParserStream, JsonMessageTransformStream } from '@xsai/shared-stream';
|
|
41
3
|
|
|
42
4
|
const streamTranscription = (options) => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const fullStream = new ReadableStream({ start: (controller) => fullStreamCtrl = controller });
|
|
46
|
-
const textStream = new ReadableStream({ start: (controller) => textStreamCtrl = controller });
|
|
5
|
+
const [textStream, textStreamCtrl] = createControlledStream();
|
|
6
|
+
const [fullStream, fullStreamCtrl] = createControlledStream();
|
|
47
7
|
const fullText = new DelayedPromise();
|
|
48
8
|
let text = "";
|
|
49
9
|
const doStream = async () => {
|
|
@@ -65,18 +25,17 @@ const streamTranscription = (options) => {
|
|
|
65
25
|
});
|
|
66
26
|
await responseCatch(response);
|
|
67
27
|
const { body: stream } = response;
|
|
68
|
-
await stream.pipeThrough(
|
|
28
|
+
await stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(new JsonMessageTransformStream()).pipeTo(new WritableStream({
|
|
69
29
|
abort: (reason) => {
|
|
70
|
-
|
|
71
|
-
textStreamCtrl?.error(reason);
|
|
30
|
+
errorControllers(reason, fullStreamCtrl, textStreamCtrl);
|
|
72
31
|
},
|
|
73
32
|
close: () => {
|
|
74
33
|
},
|
|
75
34
|
write: (chunk) => {
|
|
76
35
|
if (chunk.type === "transcript.text.delta") {
|
|
77
|
-
textStreamCtrl?.enqueue(chunk.delta);
|
|
36
|
+
textStreamCtrl.current?.enqueue(chunk.delta);
|
|
78
37
|
text += chunk.delta;
|
|
79
|
-
fullStreamCtrl?.enqueue(chunk);
|
|
38
|
+
fullStreamCtrl.current?.enqueue(chunk);
|
|
80
39
|
} else if (chunk.type === "transcript.text.done") ;
|
|
81
40
|
}
|
|
82
41
|
}));
|
|
@@ -85,11 +44,9 @@ const streamTranscription = (options) => {
|
|
|
85
44
|
try {
|
|
86
45
|
await doStream();
|
|
87
46
|
fullText.resolve(text);
|
|
88
|
-
fullStreamCtrl
|
|
89
|
-
textStreamCtrl?.close();
|
|
47
|
+
closeControllers(fullStreamCtrl, textStreamCtrl);
|
|
90
48
|
} catch (err) {
|
|
91
|
-
|
|
92
|
-
textStreamCtrl?.error(err);
|
|
49
|
+
errorControllers(err, fullStreamCtrl, textStreamCtrl);
|
|
93
50
|
fullText.reject(err);
|
|
94
51
|
}
|
|
95
52
|
})();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/stream-transcription",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.0-beta.2",
|
|
5
5
|
"description": "extra-small AI SDK.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xsai/shared": "~0.
|
|
32
|
+
"@xsai/shared": "~0.5.0-beta.2",
|
|
33
|
+
"@xsai/shared-stream": "~0.5.0-beta.2"
|
|
33
34
|
},
|
|
34
35
|
"scripts": {
|
|
35
36
|
"build": "pkgroll",
|