-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.mjs
More file actions
55 lines (48 loc) · 1.12 KB
/
chat.mjs
File metadata and controls
55 lines (48 loc) · 1.12 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
const baseUrl = "https://__BASE_URL__";
const cors = {
mode: "cors",
credentials: "include",
};
export const auth = {
async isAuthenticated() {
const req = await fetch(new URL("/auth", baseUrl), {
method: "HEAD",
...cors,
});
return req.ok;
},
async getProfile() {
const req = await fetch(new URL("/auth", baseUrl), cors);
return req.ok ? await req.json() : null;
},
};
export const bots = {
async list() {
const req = await fetch(new URL("/bots", baseUrl), cors);
return req.ok ? await req.json() : null;
},
async getBot(name) {
const req = await fetch(new URL("/bots/" + name, baseUrl), cors);
return req.ok ? await req.json() : null;
},
};
export const chat = {
async ask(payload) {
const {
bot = "",
format = "text",
messages = [],
context = {},
model,
} = payload;
const body = JSON.stringify({ bot, format, messages, context, model });
const req = await fetch(
new URL("/chat", baseUrl, {
method: "POST",
body,
...cors,
})
);
return req.ok ? await req.json() : null;
},
};