forked from QuantStack/jupyter-ai-tutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
68 lines (58 loc) · 1.78 KB
/
Copy pathapi.ts
File metadata and controls
68 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { URLExt } from '@jupyterlab/coreutils';
import { ServerConnection } from '@jupyterlab/services';
/**
* Streams the tutor explanation for the given message body via SSE.
* Yields text chunks as they arrive from the backend.
* @param body - The user message (code + question)
* @param signal - Optional abort signal
*/
export async function* streamExplanation(
body: string,
signal?: AbortSignal
): AsyncGenerator<string, void, undefined> {
const settings = ServerConnection.makeSettings();
const url = URLExt.join(settings.baseUrl, 'api/jupyter-ai-tutor/explain');
const response = await ServerConnection.makeRequest(
url,
{
method: 'POST',
body: JSON.stringify({ body }),
headers: { 'Content-Type': 'application/json' },
signal
},
settings
);
if (!response.ok) {
throw new ServerConnection.ResponseError(response);
}
const reader = response.body?.getReader();
if (!reader) {
throw new Error('Response body is not readable');
}
const decoder = new TextDecoder();
let buffer = '';
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() ?? '';
for (const line of lines) {
if (!line.startsWith('data: ')) continue;
const data = line.slice(6).trim();
if (data === '[DONE]') return;
let parsed: { text?: string; error?: string };
try {
parsed = JSON.parse(data) as { text?: string; error?: string };
} catch {
continue;
}
if (parsed.error) throw new Error(parsed.error);
if (parsed.text) yield parsed.text;
}
}
} finally {
reader.releaseLock();
}
}