Spaces:
Running
Running
Download src/api.js from DeepThoughtAI/pl_ai_frontend: direct link, hf CLI and curl.
- Browser
- Download file 5.03 kB
-
https://huggingface.co/spaces/DeepThoughtAI/pl_ai_frontend/resolve/main/src/api.js
- Command line
-
hf download hf://spaces/DeepThoughtAI/pl_ai_frontend/src/api.js
-
curl -L -o api.js https://huggingface.co/spaces/DeepThoughtAI/pl_ai_frontend/resolve/main/src/api.js
5.03 kB
| // 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 } | |
| } | |