motif64

m64_06d28e7632b2 / agent_gateway.py

384 lines, 15.0 KBraw
1"""Agent gateway for the opencode-qwen4-35b-a3b-strixhalo 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 llama.cpp (llama-server, Vulkan) on 127.0.0.1:9000 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:9000/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")221222223LLM_HEALTH = os.environ.get("LLM_HEALTH", "http://127.0.0.1:9000/health")224225226async def _llm_up() -> bool:227    """llama-server answers /health 200 only once the weights are loaded."""228    try:229        async with httpx.AsyncClient(timeout=5) as c:230            return (await c.get(LLM_HEALTH)).status_code == 200231    except httpx.HTTPError:232        return False233234235async def _report_ready_when_up() -> None:236    # Ready only when both opencode and the model answer: a session opened while llama-server237    # is still loading would fail its first turn.238    while not (await harness.healthy() and await _llm_up()):239        await asyncio.sleep(1)240    _report("ready", f"opencode up, model {MODEL} on local llama.cpp (Vulkan)")241242243@asynccontextmanager244async def _lifespan(app: FastAPI):245    SESSIONS_DIR.mkdir(parents=True, exist_ok=True)246    task = asyncio.create_task(_report_ready_when_up())247    yield248    task.cancel()249250251app = FastAPI(title="opencode-qwen4-35b-a3b-strixhalo agent gateway", docs_url=None, redoc_url=None,252              openapi_url=None, lifespan=_lifespan)253254255@app.get("/health")256async def health():257    ok = await harness.healthy()258    return JSONResponse({"status": "ok" if ok else "starting", "harness": "opencode"},259                        status_code=200 if ok else 503)260261262@app.get("/v1/agent")263def describe():264    return {265        "harness": "opencode", "llm": MODEL, "llm_provider": "llama.cpp (Vulkan, AMD Strix Halo), in this container",266        "sandbox": {267            "network": "internal docker network; the only way out is the m64 gate, which "268                       "allows only what the person serving this run granted (by default "269                       "nothing: the model is local) and logs every connection",270            "filesystem": "each session gets a fresh empty directory under the run's scratch",271            "privileges": "non-root (1000:1000), all capabilities dropped, no-new-privileges",272        },273    }274275276@app.post("/v1/sessions", status_code=201)277async def create_session(request: Request):278    body = await _json(request)279    reg = _load_registry()280    d = SESSIONS_DIR / uuid.uuid4().hex[:12]281    d.mkdir(parents=True)282    sid = await harness.create(str(d), body.get("title"))283    reg[sid] = {"id": sid, "title": body.get("title"), "created": time.time(),284                "directory": str(d)}285    _save_registry(reg)286    log.info("SESSION_CREATE %s dir=%s", sid, d)287    return {k: v for k, v in reg[sid].items() if k != "directory"}288289290@app.post("/v1/sessions/{sid}/messages")291async def send_message(sid: str, request: Request):292    body = await _json(request)293    text = body.get("text") or body.get("prompt")294    if not text or not isinstance(text, str):295        raise HTTPException(400, "body needs 'text'")296    s = _session(_load_registry(), sid)297    if body.get("wait", True):298        t0 = time.time()299        reply = await harness.send(sid, s["directory"], text)300        return {**reply, "elapsed_s": round(time.time() - t0, 1)}301    await harness.send_async(sid, s["directory"], text)302    return JSONResponse({"session_id": sid, "status": "busy",303                         "events": f"GET /v1/sessions/{sid}/events"}, status_code=202)304305306@app.post("/v1/sessions/{sid}/abort")307async def abort(sid: str):308    s = _session(_load_registry(), sid)309    await harness.abort(sid, s["directory"])310    return {"aborted": sid}311312313@app.get("/v1/sessions/{sid}/events")314async def events(sid: str):315    s = _session(_load_registry(), sid)316    return StreamingResponse(harness.events(sid, s["directory"]), media_type="text/event-stream",317                             headers={"Cache-Control": "no-cache"})318319320@app.get("/v1/sessions/{sid}/files")321async def get_files(sid: str, path: str = "."):322    s = _session(_load_registry(), sid)323    root = Path(s["directory"])324    p = _safe_path(root, path)325    kind = _kind(p)326    if kind is None:327        raise HTTPException(404, f"{path} not found")328    if kind == "other":329        raise HTTPException(400, f"{path} is not a regular file or directory")330    if kind == "dir":331        entries = []332        for c in sorted(p.iterdir()):333            k = _kind(c) or "other"334            entries.append({"name": c.name, "type": k,335                            "size": c.stat().st_size if k == "file" else None})336        return {"path": str(p.relative_to(root)) if p != root else ".", "entries": entries}337    # The agent may swap the file for a FIFO after the check above: open without338    # blocking and check what was actually opened.339    f = _open_regular(p, os.O_RDONLY)340    size = os.fstat(f.fileno()).st_size341342    def chunks():343        with f:344            while data := f.read(65536):345                yield data346347    headers = {**_DOWNLOAD_HEADERS, "Content-Length": str(size),348               "Content-Disposition": "attachment; filename*=UTF-8''" + quote(p.name)}349    return StreamingResponse(chunks(), media_type=_download_type(p.name), headers=headers)350351352@app.put("/v1/sessions/{sid}/files", status_code=201)353async def put_file(sid: str, request: Request, path: str = ""):354    if not path.strip("/."):355        raise HTTPException(400, "?path= names the file to write")356    s = _session(_load_registry(), sid)357    p = _safe_path(Path(s["directory"]), path)358    if _kind(p) not in (None, "file"):359        raise HTTPException(400, f"{path} exists and is not a regular file")360    try:361        p.parent.mkdir(parents=True, exist_ok=True)362    except (FileExistsError, NotADirectoryError):363        raise HTTPException(400, f"a parent of {path} is not a directory")364    data = await request.body()365    with _open_regular(p, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) as f:366        f.write(data)367    return {"written": path, "bytes": len(data)}368369370async def _json(request: Request) -> dict:371    raw = await request.body()372    if not raw:373        return {}374    try:375        data = json.loads(raw)376    except ValueError:377        raise HTTPException(400, "body must be JSON")378    if not isinstance(data, dict):379        raise HTTPException(400, "body must be a JSON object")380    return data381382383if __name__ == "__main__":384    uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info")