Download scripts/lantern-opencode.py from spark-school/school-corpus: direct link, hf CLI and curl.
- Browser
- Download file 2.53 kB
-
https://huggingface.co/datasets/spark-school/school-corpus/resolve/main/scripts/lantern-opencode.py
- Command line
-
hf download hf://datasets/spark-school/school-corpus/scripts/lantern-opencode.py
-
curl -L -o lantern-opencode.py https://huggingface.co/datasets/spark-school/school-corpus/resolve/main/scripts/lantern-opencode.py
2.53 kB
| #!/usr/bin/env python3 | |
| """lantern-opencode.py — talk to the opencode free-tier seats (GLM-5.3-flash etc.) | |
| Route law (measured 2026-10-16): use the v1 legacy routes on `opencode serve`. | |
| - v2 /api/session/{id}/prompt dies: SessionRunnerModel.UnsupportedApiError | |
| "Unsupported API for opencode/glm-5.3-flash-free: native" (server log receipt). | |
| - v1 POST /session + POST /session/{id}/message runs the ai-sdk path and works. | |
| Server start: `opencode serve --port 8767 --hostname 127.0.0.1` (loopback only, no secrets). | |
| Free models list: `opencode models` (opencode/glm-5.3-flash-free, deepseek-v4.1-flash-free, ...). | |
| """ | |
| import json, sys, time, urllib.request | |
| B = "http://127.0.0.1:8767" | |
| def req(path, method="GET", body=None): | |
| data = json.dumps(body).encode() if body is not None else None | |
| r = urllib.request.Request(B + path, data=data, method=method, | |
| headers={"Content-Type": "application/json"}) | |
| raw = urllib.request.urlopen(r, timeout=180).read().decode() | |
| return json.loads(raw) if raw.strip() else {} | |
| def ask(text, model="glm-5.3-flash-free", session=None, poll_s=180): | |
| """Send one prompt; returns (sessionID, reply_text, tokens).""" | |
| if session is None: | |
| s = req("/session", "POST", {"title": text[:40]}) | |
| sid = s.get("id") | |
| else: | |
| sid = session | |
| body = {"model": {"providerID": "opencode", "modelID": model}, | |
| "parts": [{"type": "text", "text": text}]} | |
| req(f"/session/{sid}/message", "POST", body) | |
| deadline = time.time() + poll_s | |
| while time.time() < deadline: | |
| time.sleep(4) | |
| arr = req(f"/session/{sid}/message") | |
| arr = arr if isinstance(arr, list) else arr.get("data", []) | |
| asst = [m for m in arr if m.get("info", m).get("role") == "assistant"] | |
| if asst: | |
| last = asst[-1]; info = last.get("info", last) | |
| if info.get("time", {}).get("completed"): | |
| txt = " ".join(p.get("text", "") for p in last.get("parts", []) | |
| if p.get("type") == "text") | |
| return sid, txt, info.get("tokens") | |
| raise TimeoutError(f"no completed assistant message in {poll_s}s (session {sid})") | |
| if __name__ == "__main__": | |
| model = "glm-5.3-flash-free" | |
| args = sys.argv[1:] | |
| if args and args[0].startswith("--model="): | |
| model = args[0].split("=", 1)[1]; args = args[1:] | |
| sid, txt, tok = ask(" ".join(args), model=model) | |
| print("session:", sid) | |
| print(txt) | |
| print("tokens:", json.dumps(tok)) | |