Repository navigation
Expand file tree
/
Copy pathchatbot.api.js
More file actions
51 lines (45 loc) · 1.73 KB
/
Copy pathchatbot.api.js
File metadata and controls
51 lines (45 loc) · 1.73 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
import client from "./client";
import { API } from "./endpoints";
export const ChatbotAPI = {
sendMessage(message, history = []) {
return client.post(`${API.CHATBOT}/chat`, { message, history });
},
// Streams the reply over server-sent events. axios can't expose a response
// body incrementally in the browser, so this uses fetch. `onDelta` is called
// with each text chunk; the promise resolves when the reply is complete and
// rejects with an Error carrying `.status` on failure.
async streamMessage(message, history = [], { onDelta, signal } = {}) {
const res = await fetch(`${import.meta.env.VITE_API_URL}${API.CHATBOT}/chat/stream`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message, history }),
signal,
});
if (!res.ok || !res.body) {
const err = new Error("Chat request failed");
err.status = res.status;
throw err;
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
let finished = false;
while (!finished) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
let idx;
while ((idx = buffer.indexOf("\n\n")) !== -1) {
const event = buffer.slice(0, idx);
buffer = buffer.slice(idx + 2);
const line = event.split("\n").find((l) => l.startsWith("data: "));
if (!line) continue;
let payload;
try { payload = JSON.parse(line.slice(6)); } catch { continue; }
if (payload.error) throw new Error(payload.error);
if (payload.delta) onDelta?.(payload.delta);
if (payload.done) finished = true;
}
}
},
};