// Gradio Server-mode client — used ONLY on the Hugging Face ZeroGPU Space. // // ZeroGPU attributes GPU quota via the HF iframe auth headers, which a plain `fetch` // does NOT forward — so the GPU narration endpoints (overview/advice/chat/project_*) // must be called through @gradio/client, which forwards them. The deterministic // engine endpoints stay on plain `fetch` (see useAnalysis.js). // // Transport is chosen at runtime by `apiMode()`: "local" (the `./her` REST server, // the default) or "space" (this gradio client). useApi() sets it from /api/health. import { Client } from "@gradio/client"; import { clientId } from "./client.js"; let _mode = "local"; // "local" = REST /api/* · "space" = gradio @app.api endpoints export function setApiMode(m) { _mode = m === "space" ? "space" : "local"; } export function apiMode() { return _mode; } let _clientP = null; function getClient() { if (!_clientP) _clientP = Client.connect(window.location.origin); return _clientP; } // Call a Gradio @app.api endpoint by name. A single-output endpoint comes back as // res.data === [value]; unwrap to the value (our endpoints each return one object). // The per-browser `client` token is added to every payload so the GPU routes scope to // the caller's namespace (same isolation as the REST X-Her-Client header). async function call(name, payload) { const gc = await getClient(); const res = await gc.predict("/" + name, { ...(payload || {}), client: clientId() }); const d = res && res.data; return Array.isArray(d) ? d[0] : d; } export const callOverview = (path) => call("overview", { path: path || "" }); export const callAdvice = (path) => call("advice", { path: path || "" }); export const callChat = (question, path) => call("chat", { question, path: path || "" }); export const callProjectChat = (question, cwd) => call("project_chat", { question, cwd }); export const callProjectNarrative = (cwd) => call("project_narrative", { cwd });