Elffuss BraidHub

kikocis/continue public

a0c996c3d18a · 3.0 KiBRawPermalink
1export async function* toAsyncIterable(
2 nodeReadable: NodeJS.ReadableStream,
3): AsyncGenerator<Uint8Array> {
4 for await (const chunk of nodeReadable) {
5 // @ts-ignore
6 yield chunk as Uint8Array;
7 }
8}
9
10export async function* streamResponse(
11 response: Response,
12): AsyncGenerator<string> {
13 if (response.status !== 200) {
14 throw new Error(await response.text());
15 }
16
17 if (!response.body) {
18 throw new Error("No response body returned.");
19 }
20
21 // Get the major version of Node.js
22 const nodeMajorVersion = parseInt(process.versions.node.split(".")[0], 10);
23
24 if (nodeMajorVersion >= 20) {
25 // Use the new API for Node 20 and above
26 const stream = (ReadableStream as any).from(response.body);
27 for await (const chunk of stream.pipeThrough(
28 new TextDecoderStream("utf-8"),
29 )) {
30 yield chunk;
31 }
32 } else {
33 // Fallback for Node versions below 20
34 // Streaming with this method doesn't work as version 20+ does
35 const decoder = new TextDecoder("utf-8");
36 const nodeStream = response.body as unknown as NodeJS.ReadableStream;
37 for await (const chunk of toAsyncIterable(nodeStream)) {
38 yield decoder.decode(chunk, { stream: true });
39 }
40 }
41}
42
43function parseDataLine(line: string): any {
44 const json = line.startsWith("data: ")
45 ? line.slice("data: ".length)
46 : line.slice("data:".length);
47
48 try {
49 const data = JSON.parse(json);
50 if (data.error) {
51 throw new Error(`Error streaming response: ${data.error}`);
52 }
53
54 return data;
55 } catch (e) {
56 throw new Error(`Malformed JSON sent from server: ${json}`);
57 }
58}
59
60function parseSseLine(line: string): { done: boolean; data: any } {
61 if (line.startsWith("data:[DONE]") || line.startsWith("data: [DONE]")) {
62 return { done: true, data: undefined };
63 }
64 if (line.startsWith("data:")) {
65 return { done: false, data: parseDataLine(line) };
66 }
67 if (line.startsWith(": ping")) {
68 return { done: true, data: undefined };
69 }
70 return { done: false, data: undefined };
71}
72
73export async function* streamSse(response: Response): AsyncGenerator<any> {
74 let buffer = "";
75 for await (const value of streamResponse(response)) {
76 buffer += value;
77
78 let position: number;
79 while ((position = buffer.indexOf("\n")) >= 0) {
80 const line = buffer.slice(0, position);
81 buffer = buffer.slice(position + 1);
82
83 const { done, data } = parseSseLine(line);
84 if (done) {
85 break;
86 }
87 if (data) {
88 yield data;
89 }
90 }
91 }
92
93 if (buffer.length > 0) {
94 const { done, data } = parseSseLine(buffer);
95 if (!done && data) {
96 yield data;
97 }
98 }
99}
100
101export async function* streamJSON(response: Response): AsyncGenerator<any> {
102 let buffer = "";
103 for await (const value of streamResponse(response)) {
104 buffer += value;
105
106 let position;
107 while ((position = buffer.indexOf("\n")) >= 0) {
108 const line = buffer.slice(0, position);
109 const data = JSON.parse(line);
110 yield data;
111 buffer = buffer.slice(position + 1);
112 }
113 }
114}