Spaces:
Sleeping
Sleeping
Update src/api.js
Browse files- src/api.js +17 -5
src/api.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
// src/api.js
|
| 2 |
-
|
| 3 |
// src/api.js
|
| 4 |
export const API_BASE = "https://deepthoughtai-prolog-gateway.hf.space";
|
| 5 |
|
|
@@ -10,12 +8,25 @@ export async function login(username, password) {
|
|
| 10 |
body: JSON.stringify({ username, password }),
|
| 11 |
});
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if (!res.ok) {
|
| 14 |
-
const
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
-
|
| 19 |
if (!json.ok || !json.token) {
|
| 20 |
throw new Error(json.error || "Login failed");
|
| 21 |
}
|
|
@@ -23,6 +34,7 @@ export async function login(username, password) {
|
|
| 23 |
return json; // { ok, token }
|
| 24 |
}
|
| 25 |
|
|
|
|
| 26 |
export async function sendChat(message, strictMode = true, history = [], token) {
|
| 27 |
const res = await fetch(`${API_BASE}/chat`, {
|
| 28 |
method: "POST",
|
|
|
|
|
|
|
|
|
|
| 1 |
// src/api.js
|
| 2 |
export const API_BASE = "https://deepthoughtai-prolog-gateway.hf.space";
|
| 3 |
|
|
|
|
| 8 |
body: JSON.stringify({ username, password }),
|
| 9 |
});
|
| 10 |
|
| 11 |
+
let json;
|
| 12 |
+
const text = await res.text().catch(() => "");
|
| 13 |
+
|
| 14 |
+
try {
|
| 15 |
+
json = text ? JSON.parse(text) : {};
|
| 16 |
+
} catch {
|
| 17 |
+
json = {};
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
// HTTP not OK
|
| 21 |
if (!res.ok) {
|
| 22 |
+
const msg =
|
| 23 |
+
(json && json.error) ||
|
| 24 |
+
text ||
|
| 25 |
+
`Login failed with status ${res.status}`;
|
| 26 |
+
throw new Error(msg);
|
| 27 |
}
|
| 28 |
|
| 29 |
+
// HTTP OK but backend says ok: false
|
| 30 |
if (!json.ok || !json.token) {
|
| 31 |
throw new Error(json.error || "Login failed");
|
| 32 |
}
|
|
|
|
| 34 |
return json; // { ok, token }
|
| 35 |
}
|
| 36 |
|
| 37 |
+
|
| 38 |
export async function sendChat(message, strictMode = true, history = [], token) {
|
| 39 |
const res = await fetch(`${API_BASE}/chat`, {
|
| 40 |
method: "POST",
|