Spaces:
Running
Running
File size: 5,033 Bytes
c87f4ee 00e1c63 c87f4ee | 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 | // src/api.js
export const API_BASE = "https://deepthoughtai-pl-ai-gateway.hf.space";
export async function login(username, password) {
const res = await fetch(`${API_BASE}/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
});
let json;
const text = await res.text().catch(() => "");
try {
json = text ? JSON.parse(text) : {};
} catch {
json = {};
}
// HTTP not OK
if (!res.ok) {
const msg =
(json && json.error) ||
text ||
`Login failed with status ${res.status}`;
throw new Error(msg);
}
// HTTP OK but backend says ok: false
if (!json.ok || !json.token) {
throw new Error(json.error || "Login failed");
}
return json; // { ok, token }
}
export async function sendChat(message, strictMode = true, history = [], token) {
const res = await fetch(`${API_BASE}/chat`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`, // 🟢 send JWT
},
body: JSON.stringify({
message,
strict_mode: strictMode,
history,
}),
});
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`);
}
const json = await res.json();
if (!json.ok) {
throw new Error(json.error || "Chat failed");
}
return json;
}
export async function streamChat(message, strictMode, history, token, onToken) {
const res = await fetch(`${API_BASE}/chat/stream`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify({
message,
strict_mode: strictMode,
history
}),
});
if (!res.body) {
throw new Error("Streaming not supported by server");
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { value, done } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
onToken(chunk);
}
}
export async function uploadPrologFile(prologCode, token) {
const res = await fetch(`${API_BASE}/prolog/upload`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ prolog_code: prologCode }),
});
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(`HTTP ${res.status}: ${text || "Upload failed"}`);
}
const json = await res.json();
if (!json.ok) {
throw new Error(json.error || "Upload failed");
}
return json;
}
export async function getKBMetadata(token) {
const res = await fetch(`${API_BASE}/prolog/metadata`, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
});
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(`HTTP ${res.status}: ${text || "Metadata fetch failed"}`);
}
const json = await res.json();
if (!json.ok) {
throw new Error(json.error || "Metadata fetch failed");
}
return json;
}
export async function queryProlog(query, token, maxSolutions = 10) {
const res = await fetch(`${API_BASE}/prolog/query`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ query, max_solutions: maxSolutions }),
});
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(`HTTP ${res.status}: ${text || "Query failed"}`);
}
const json = await res.json();
if (!json.ok) {
throw new Error(json.error || "Query failed");
}
return json;
}
export async function getMe(token) {
const res = await fetch(`${API_BASE}/me`, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
});
const text = await res.text().catch(() => "");
let json;
try {
json = text ? JSON.parse(text) : {};
} catch {
json = {};
}
if (!res.ok) {
const msg = (json && json.error) || text || `HTTP ${res.status}`;
throw new Error(msg);
}
if (!json.ok) {
throw new Error(json.error || "Failed to fetch user info");
}
return json; // { ok, username, is_admin }
}
export async function registerUser(username, password, token, overwrite = false) {
const res = await fetch(`${API_BASE}/register`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ username, password, overwrite }),
});
const text = await res.text().catch(() => "");
let json;
try {
json = text ? JSON.parse(text) : {};
} catch {
json = {};
}
if (!res.ok) {
const msg = (json && json.error) || text || `HTTP ${res.status}`;
throw new Error(msg);
}
if (!json.ok) {
throw new Error(json.error || "Register failed");
}
return json; // { ok, username }
}
|