-
Notifications
You must be signed in to change notification settings - Fork 626
Expand file tree
/
Copy pathapi.ts
More file actions
330 lines (310 loc) · 8.04 KB
/
api.ts
File metadata and controls
330 lines (310 loc) · 8.04 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import {
ChatMessage,
Conversation,
ConversationRequest,
FrontEndSettings,
} from "./models";
export async function callConversationApi(
options: ConversationRequest,
abortSignal: AbortSignal
): Promise<Response> {
const response = await fetch("/api/conversation", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: options.messages,
conversation_id: options.id,
}),
signal: abortSignal,
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(JSON.stringify(errorData.error));
}
return response;
}
export type UserInfo = {
access_token: string;
expires_on: string;
id_token: string;
provider_name: string;
user_claims: any[];
user_id: string;
};
export async function getUserInfo(): Promise<UserInfo[]> {
try {
const response = await fetch("/.auth/me");
if (!response.ok) {
console.log(
"No identity provider found. Access to chat will be blocked."
);
return [];
}
const payload = await response.json();
return payload;
} catch (e) {
return [];
}
}
export async function checkAuthEnforced(): Promise<boolean> {
try {
const response = await fetch("/api/checkauth", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
const config = await response.json(); // Parse JSON response
return config.is_auth_enforced;
} catch (error) {
console.error("Failed to fetch configuration:", error);
return true; // Return true because we need to enforce auth by default
}
}
export async function getAssistantTypeApi() {
try {
const response = await fetch("/api/assistanttype", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
const config = await response.json(); // Parse JSON response
return config;
} catch (error) {
console.error("Failed to fetch configuration:", error);
return null; // Return null or some default value in case of error
}
}
export const historyRead = async (convId: string): Promise<ChatMessage[]> => {
const response = await fetch("/api/history/read", {
method: "POST",
body: JSON.stringify({
conversation_id: convId,
}),
headers: {
"Content-Type": "application/json",
},
})
.then(async (res) => {
if (!res) {
return [];
}
const payload = await res.json();
const messages: ChatMessage[] = [];
if (payload?.messages) {
payload.messages.forEach((msg: any) => {
const message: ChatMessage = {
id: msg.id,
role: msg.role,
date: msg.createdAt,
content: msg.content,
feedback: msg.feedback ?? undefined,
};
messages.push(message);
});
}
return messages;
})
.catch((_err) => {
console.error("There was an issue fetching your data.");
return [];
});
return response;
};
export const historyList = async (
offset = 0
): Promise<Conversation[] | null> => {
let response = await fetch(`/api/history/list?offset=${offset}`, {
method: "GET",
})
.then(async (res) => {
let payload = await res.json();
if (!Array.isArray(payload)) {
console.error("There was an issue fetching your data.");
return null;
}
const conversations: Conversation[] = payload.map((conv: any) => {
const conversation: Conversation = {
id: conv.id,
title: conv.title,
date: conv.createdAt,
updatedAt: conv?.updatedAt,
messages: [],
};
return conversation;
});
return conversations;
})
.catch((_err) => {
console.error("There was an issue fetching your data.", _err);
return null;
});
return response;
};
export const historyUpdate = async (
messages: ChatMessage[],
convId: string
): Promise<Response> => {
const response = await fetch("/api/history/update", {
method: "POST",
body: JSON.stringify({
conversation_id: convId,
messages: messages,
}),
headers: {
"Content-Type": "application/json",
},
})
.then(async (res) => {
return res;
})
.catch((_err) => {
console.error("There was an issue fetching your data.");
const errRes: Response = {
...new Response(),
ok: false,
status: 500,
};
return errRes;
});
return response;
};
export const historyRename = async (
convId: string,
title: string
): Promise<Response> => {
const response = await fetch("/api/history/rename", {
method: "POST",
body: JSON.stringify({
conversation_id: convId,
title: title,
}),
headers: {
"Content-Type": "application/json",
},
})
.then((res) => {
return res;
})
.catch((_err) => {
console.error("There was an issue fetching your data.");
const errRes: Response = {
...new Response(),
ok: false,
status: 500,
};
return errRes;
});
return response;
};
export const historyDelete = async (convId: string): Promise<Response> => {
const response = await fetch("/api/history/delete", {
method: "DELETE",
body: JSON.stringify({
conversation_id: convId,
}),
headers: {
"Content-Type": "application/json",
},
})
.then((res) => {
return res;
})
.catch((_err) => {
console.error("There was an issue fetching your data.");
const errRes: Response = {
...new Response(),
ok: false,
status: 500,
};
return errRes;
});
return response;
};
export const historyDeleteAll = async (): Promise<Response> => {
const response = await fetch("api/history/delete_all", {
method: "DELETE",
body: JSON.stringify({}),
headers: {
"Content-Type": "application/json",
},
})
.then((res) => {
return res;
})
.catch((_err) => {
console.error("There was an issue fetching your data.");
const errRes: Response = {
...new Response(),
ok: false,
status: 500,
};
return errRes;
});
return response;
};
export const historyExport = async (
convId: string,
format: "json" | "markdown" | "text" = "json"
): Promise<void> => {
try {
const response = await fetch("/api/history/export", {
method: "POST",
body: JSON.stringify({
conversation_id: convId,
format: format,
}),
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || "Export failed");
}
const blob = await response.blob();
const contentDisposition = response.headers.get("Content-Disposition");
let filename = `conversation.${format === "markdown" ? "md" : format === "text" ? "txt" : "json"}`;
if (contentDisposition) {
const match = contentDisposition.match(/filename="(.+)"/);
if (match) {
filename = match[1];
}
}
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
} catch (err) {
console.error("Error exporting conversation:", err);
throw err;
}
};
export async function getFrontEndSettings(): Promise<FrontEndSettings> {
try {
const response = await fetch("/api/history/frontend_settings", {
method: "GET",
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
const responseJSON = await response.json();
return responseJSON
} catch (error) {
console.error("Failed to fetch Front End Settings:", error);
return { CHAT_HISTORY_ENABLED: false };
}
}