Spaces:
Running
Running
File size: 3,796 Bytes
13ef204 | 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 | <!doctype html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Baghchal Bot API</title>
<!--
The Bot API, running on a *static* Hugging Face Space.
A static Space serves files and runs no process, so there is nothing here to answer an HTTP
request from another origin. What this page does instead is run the deployed server's OWN
reply builder (bagh_server.botapi.bot_reply) under Pyodide, in the visitor's browser, and
render the exact JSON it produces. Open it with a position and read the response:
api.html?obx=TXXXT/XXXXX/XXXXX/XXXXX/TXXXT g @20 c0 -
Same schema as the hosted API -- {"input", "obx", "move"} plus side/bot/terminal -- because it
is literally the same function, not a re-implementation that could drift from it.
Query params: obx (position), bot (default phantom), ms (think time), pretty=0 for one line.
-->
<style>
:root { color-scheme: light dark; }
body {
margin: 0; padding: 18px;
font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
background: #14151a; color: #e8e4d9;
}
pre { margin: 0; white-space: pre-wrap; word-break: break-word; }
.meta { color: #8c8f9a; margin-bottom: 10px; }
.bad { color: #ffb4a2; }
</style>
<div class="meta" id="meta">loading the Python engine (first call only, ~10s)…</div>
<pre id="out">{}</pre>
<script src="https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.js"></script>
<script>
(async function () {
var START = "TXXXT/XXXXX/XXXXX/XXXXX/TXXXT g @20 c0 -";
var q = new URLSearchParams(location.search);
var obx = (q.get("obx") || START).trim();
var bot = q.get("bot") || "phantom";
var ms = parseInt(q.get("ms") || "1200", 10);
var indent = q.get("pretty") === "0" ? "None" : "2";
var meta = document.getElementById("meta"), out = document.getElementById("out");
function fail(msg) {
meta.className = "meta bad";
meta.textContent = msg;
// Still emit the API's own error shape: an empty "obx" is what makes a caller report the
// problem rather than silently mis-parse a reply it cannot use.
out.textContent = JSON.stringify({ error: msg, obx: "" }, null, 2);
}
try {
var py = await loadPyodide();
var resp = await fetch("bagh_engine.zip");
if (!resp.ok) throw new Error("bagh_engine.zip missing (" + resp.status + ")");
await py.unpackArchive(await resp.arrayBuffer(), "zip");
py.runPython([
"import sys, json",
"sys.path.insert(0, '/home/pyodide')",
"from bagh_engine import roster",
"from bagh_server.botapi import bot_reply",
"from bagh_server.server import BotService",
"_svc = {}",
"def bagh_api(obx, bot, time_ms, indent):",
" svc = _svc.get(bot)",
" if svc is None:",
" svc = _svc[bot] = BotService(roster.build(bot))",
" try:",
" payload = bot_reply(svc, obx, side=None, time_ms=time_ms,",
" tiger_bot=bot, goat_bot=bot)",
" except Exception as exc:",
" return json.dumps({'error': str(exc), 'obx': ''}, indent=indent)",
" return json.dumps(payload, indent=indent)",
].join("\n"));
meta.textContent = "thinking (" + bot + ", " + ms + "ms)…";
var t0 = performance.now();
var body = py.globals.get("bagh_api")(obx, bot, ms, indent === "None" ? null : 2);
out.textContent = body;
meta.textContent = bot + " · " + Math.round(performance.now() - t0) + "ms · in-browser "
+ "(a static Space cannot be called by another site — point baghchal.net at a hosted API)";
// Let an automated check read the result without scraping the DOM.
window.BAGH_RESULT = JSON.parse(body);
} catch (err) {
fail(String(err));
}
})();
</script>
|