m64_ca3a28a8569e / agent_gateway.py
370 lines, 14.5 KBraw
1"""Agent gateway for the opencode-qwen38fn-strata-gb10 capsule.23Adapted from gx10 ~/code/vlmtest/agents/gateway/agent_gateway.py, through the4opencode-dsflash-openrouter-gb10 capsule's gateway: the harness-neutral control API over the5local `opencode serve` on 127.0.0.1:4096, whose LLM is Strata on 127.0.0.1:8080 in this container.67Routes (the agent.session kind; the m64 front proxies only these and /health):89 GET /health 200 when opencode answers10 GET /v1/agent what this is11 POST /v1/sessions new session, fresh empty directory12 POST /v1/sessions/{id}/messages {"text": ...}; wait=true (default) blocks for the reply13 GET /v1/sessions/{id}/events SSE stream of this session's events14 GET /v1/sessions/{id}/files[?path=] list a dir / download a file15 PUT /v1/sessions/{id}/files?path= upload a file (raw body)16 POST /v1/sessions/{id}/abort stop the running turn17"""18import asyncio19import json20import logging21import mimetypes22import os23import stat24import time25import uuid26from contextlib import asynccontextmanager27from pathlib import Path28from urllib.parse import quote2930import httpx31import uvicorn32from fastapi import FastAPI, HTTPException, Request33from fastapi.responses import JSONResponse, StreamingResponse3435PORT = int(os.environ.get("M64_PORT", "9500"))36OPENCODE = os.environ.get("OPENCODE_URL", "http://127.0.0.1:4096")37PROVIDER = os.environ.get("AGENT_PROVIDER", "openrouter-compat")38MODEL = os.environ.get("AGENT_MODEL", "deepseek/deepseek-v4.1-flash")39WORK = Path(os.environ.get("WORK_DIR", "/scratch/work"))40SESSIONS_DIR = WORK / "sessions"41REGISTRY = WORK / ".gateway" / "sessions.json"4243logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")44log = logging.getLogger("agent_gateway")454647# ── Session registry (lives in /scratch, so it goes with the run) ────────────────4849def _load_registry() -> dict:50 try:51 return json.loads(REGISTRY.read_text())52 except Exception:53 return {}545556def _save_registry(reg: dict) -> None:57 REGISTRY.parent.mkdir(parents=True, exist_ok=True)58 tmp = REGISTRY.with_suffix(".tmp")59 tmp.write_text(json.dumps(reg, indent=2))60 tmp.replace(REGISTRY)616263def _session(reg: dict, sid: str) -> dict:64 if sid not in reg:65 raise HTTPException(404, f"unknown session '{sid}'")66 return reg[sid]676869def _safe_path(root: Path, rel: str) -> Path:70 if "\x00" in rel:71 raise HTTPException(400, "invalid path")72 try:73 p = (root / rel.lstrip("/")).resolve()74 except (OSError, ValueError, RuntimeError): # e.g. a symlink loop75 raise HTTPException(400, "invalid path")76 if p != root.resolve() and root.resolve() not in p.parents:77 raise HTTPException(400, "path escapes the session directory")78 return p798081def _kind(p: Path) -> str | None:82 """"file", "dir", "other" (FIFO, socket, device...), or None if it does not exist."""83 try:84 mode = p.stat().st_mode85 except (FileNotFoundError, NotADirectoryError):86 return None87 except OSError:88 return "other"89 return "file" if stat.S_ISREG(mode) else "dir" if stat.S_ISDIR(mode) else "other"909192# Files are written by the agent, so their bytes are untrusted. They are only ever served93# as downloads, sandboxed, never sniffed, and typed as one of these inert types or bytes.94_INERT_TYPES = {"text/plain", "image/png", "image/jpeg", "image/gif", "image/webp"}95_DOWNLOAD_HEADERS = {"X-Content-Type-Options": "nosniff", "Content-Security-Policy": "sandbox"}969798def _open_regular(p: Path, flags: int):99 """Open p as a regular file, never blocking on a FIFO or following a final symlink."""100 try:101 fd = os.open(p, flags | os.O_NONBLOCK | os.O_NOFOLLOW, 0o644)102 except OSError:103 raise HTTPException(400, "not a regular file")104 if not stat.S_ISREG(os.fstat(fd).st_mode):105 os.close(fd)106 raise HTTPException(400, "not a regular file")107 os.set_blocking(fd, True)108 return os.fdopen(fd, "rb" if flags == os.O_RDONLY else "wb")109110111def _download_type(name: str) -> str:112 guessed = mimetypes.guess_type(name)[0]113 return guessed if guessed in _INERT_TYPES else "application/octet-stream"114115116# ── opencode translator ─────────────────────────────────────────────────────────117118class Opencode:119 """`opencode serve` HTTP API. Every call carries ?directory= so each session works in120 its own directory under $WORK/sessions."""121122 def __init__(self, base: str):123 self.base = base.rstrip("/")124 # trust_env=False: this is loopback; never route it through HTTPS_PROXY.125 self.http = httpx.AsyncClient(base_url=self.base, timeout=httpx.Timeout(30, read=None),126 trust_env=False)127128 async def healthy(self) -> bool:129 try:130 oc = (await self.http.get("/global/health", timeout=3)).status_code == 200131 llm = (await self.http.get("http://127.0.0.1:8080/health", timeout=3)).status_code == 200132 return oc and llm133 except Exception:134 return False135136 async def create(self, directory: str, title: str | None) -> str:137 r = await self.http.post("/session", params={"directory": directory},138 json={"title": title} if title else {})139 r.raise_for_status()140 return r.json()["id"]141142 @staticmethod143 def _prompt_body(text: str) -> dict:144 return {"parts": [{"type": "text", "text": text}],145 "model": {"providerID": PROVIDER, "modelID": MODEL}}146147 async def send(self, sid: str, directory: str, text: str) -> dict:148 r = await self.http.post(f"/session/{sid}/message", params={"directory": directory},149 json=self._prompt_body(text))150 r.raise_for_status()151 reply = self._normalize(r.json())152 # One turn = several assistant messages (one per tool step); the response is only153 # the last. Gather the tool calls of the whole turn from the history.154 turn = []155 for m in reversed(await self.history(sid, directory)):156 if m["role"] == "user":157 break158 turn.append(m)159 reply["tools"] = [t for m in reversed(turn) for t in m["tools"]]160 reply["steps"] = len(turn)161 return reply162163 async def send_async(self, sid: str, directory: str, text: str) -> None:164 r = await self.http.post(f"/session/{sid}/prompt_async", params={"directory": directory},165 json=self._prompt_body(text))166 r.raise_for_status()167168 async def history(self, sid: str, directory: str) -> list:169 r = await self.http.get(f"/session/{sid}/message", params={"directory": directory})170 r.raise_for_status()171 return [self._normalize(m) for m in r.json()]172173 async def abort(self, sid: str, directory: str) -> None:174 await self.http.post(f"/session/{sid}/abort", params={"directory": directory})175176 async def events(self, sid: str, directory: str):177 """Yield SSE frames for events that belong to `sid`."""178 async with self.http.stream("GET", "/event", params={"directory": directory}) as r:179 async for line in r.aiter_lines():180 if not line.startswith("data:"):181 continue182 try:183 ev = json.loads(line[5:])184 except ValueError:185 continue186 props = ev.get("properties") or {}187 owner = (props.get("sessionID")188 or (props.get("info") or {}).get("sessionID")189 or (props.get("part") or {}).get("sessionID")190 or (props.get("info") or {}).get("id"))191 if owner == sid or ev.get("type") == "server.connected":192 yield f"data: {json.dumps(ev)}\n\n"193194 @staticmethod195 def _normalize(msg: dict) -> dict:196 info = msg.get("info") or {}197 parts = msg.get("parts") or []198 text = "".join(p.get("text", "") for p in parts if p.get("type") == "text")199 tools = [{"tool": p.get("tool"),200 "status": (p.get("state") or {}).get("status"),201 "input": (p.get("state") or {}).get("input"),202 "output": ((p.get("state") or {}).get("output") or "")[:4000]}203 for p in parts if p.get("type") == "tool"]204 out = {"id": info.get("id"), "role": info.get("role"), "text": text, "tools": tools}205 if info.get("role") == "assistant":206 out["tokens"] = info.get("tokens")207 out["finish"] = info.get("finish")208 if info.get("error"):209 out["error"] = info["error"]210 return out211212213harness = Opencode(OPENCODE)214215216def _report(phase: str, detail: str) -> None:217 """Append a phase line to m64's status file (if running under m64)."""218 if path := os.environ.get("M64_STATUS"):219 with open(path, "a") as f:220 f.write(json.dumps({"phase": phase, "detail": detail}) + "\n")221222223async def _report_ready_when_up() -> None:224 while not await harness.healthy():225 await asyncio.sleep(1)226 _report("ready", f"opencode up, model {MODEL} on Strata in this container")227228229@asynccontextmanager230async def _lifespan(app: FastAPI):231 SESSIONS_DIR.mkdir(parents=True, exist_ok=True)232 task = asyncio.create_task(_report_ready_when_up())233 yield234 task.cancel()235236237app = FastAPI(title="opencode-qwen38fn-strata-gb10 agent gateway", docs_url=None, redoc_url=None,238 openapi_url=None, lifespan=_lifespan)239240241@app.get("/health")242async def health():243 ok = await harness.healthy()244 return JSONResponse({"status": "ok" if ok else "starting", "harness": "opencode"},245 status_code=200 if ok else 503)246247248@app.get("/v1/agent")249def describe():250 return {251 "harness": "opencode", "llm": MODEL, "llm_provider": "Strata 0.2.13, in this container",252 "sandbox": {253 "network": "internal docker network; the only way out is the m64 gate, which "254 "allows only what the person serving this run granted (by default "255 "nothing: the model is local) and logs every connection",256 "filesystem": "each session gets a fresh empty directory under the run's scratch",257 "privileges": "non-root (1000:1000), all capabilities dropped, no-new-privileges",258 },259 }260261262@app.post("/v1/sessions", status_code=201)263async def create_session(request: Request):264 body = await _json(request)265 reg = _load_registry()266 d = SESSIONS_DIR / uuid.uuid4().hex[:12]267 d.mkdir(parents=True)268 sid = await harness.create(str(d), body.get("title"))269 reg[sid] = {"id": sid, "title": body.get("title"), "created": time.time(),270 "directory": str(d)}271 _save_registry(reg)272 log.info("SESSION_CREATE %s dir=%s", sid, d)273 return {k: v for k, v in reg[sid].items() if k != "directory"}274275276@app.post("/v1/sessions/{sid}/messages")277async def send_message(sid: str, request: Request):278 body = await _json(request)279 text = body.get("text") or body.get("prompt")280 if not text or not isinstance(text, str):281 raise HTTPException(400, "body needs 'text'")282 s = _session(_load_registry(), sid)283 if body.get("wait", True):284 t0 = time.time()285 reply = await harness.send(sid, s["directory"], text)286 return {**reply, "elapsed_s": round(time.time() - t0, 1)}287 await harness.send_async(sid, s["directory"], text)288 return JSONResponse({"session_id": sid, "status": "busy",289 "events": f"GET /v1/sessions/{sid}/events"}, status_code=202)290291292@app.post("/v1/sessions/{sid}/abort")293async def abort(sid: str):294 s = _session(_load_registry(), sid)295 await harness.abort(sid, s["directory"])296 return {"aborted": sid}297298299@app.get("/v1/sessions/{sid}/events")300async def events(sid: str):301 s = _session(_load_registry(), sid)302 return StreamingResponse(harness.events(sid, s["directory"]), media_type="text/event-stream",303 headers={"Cache-Control": "no-cache"})304305306@app.get("/v1/sessions/{sid}/files")307async def get_files(sid: str, path: str = "."):308 s = _session(_load_registry(), sid)309 root = Path(s["directory"])310 p = _safe_path(root, path)311 kind = _kind(p)312 if kind is None:313 raise HTTPException(404, f"{path} not found")314 if kind == "other":315 raise HTTPException(400, f"{path} is not a regular file or directory")316 if kind == "dir":317 entries = []318 for c in sorted(p.iterdir()):319 k = _kind(c) or "other"320 entries.append({"name": c.name, "type": k,321 "size": c.stat().st_size if k == "file" else None})322 return {"path": str(p.relative_to(root)) if p != root else ".", "entries": entries}323 # The agent may swap the file for a FIFO after the check above: open without324 # blocking and check what was actually opened.325 f = _open_regular(p, os.O_RDONLY)326 size = os.fstat(f.fileno()).st_size327328 def chunks():329 with f:330 while data := f.read(65536):331 yield data332333 headers = {**_DOWNLOAD_HEADERS, "Content-Length": str(size),334 "Content-Disposition": "attachment; filename*=UTF-8''" + quote(p.name)}335 return StreamingResponse(chunks(), media_type=_download_type(p.name), headers=headers)336337338@app.put("/v1/sessions/{sid}/files", status_code=201)339async def put_file(sid: str, request: Request, path: str = ""):340 if not path.strip("/."):341 raise HTTPException(400, "?path= names the file to write")342 s = _session(_load_registry(), sid)343 p = _safe_path(Path(s["directory"]), path)344 if _kind(p) not in (None, "file"):345 raise HTTPException(400, f"{path} exists and is not a regular file")346 try:347 p.parent.mkdir(parents=True, exist_ok=True)348 except (FileExistsError, NotADirectoryError):349 raise HTTPException(400, f"a parent of {path} is not a directory")350 data = await request.body()351 with _open_regular(p, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) as f:352 f.write(data)353 return {"written": path, "bytes": len(data)}354355356async def _json(request: Request) -> dict:357 raw = await request.body()358 if not raw:359 return {}360 try:361 data = json.loads(raw)362 except ValueError:363 raise HTTPException(400, "body must be JSON")364 if not isinstance(data, dict):365 raise HTTPException(400, "body must be a JSON object")366 return data367368369if __name__ == "__main__":370 uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info")