""" Agent Q3 [HQ] — Orchestrator FastAPI app exposing /v1/chat, /v1/reason, /v1/code, /v1/tandem, /health, /metrics """ from fastapi import FastAPI, Request from fastapi.responses import JSONResponse from compute_router import ComputeRouter from tandem_core import TandemCore from metrics import track_request import uvicorn, os, time app = FastAPI(title="Agent Q3 [HQ]", version="1.0.0") router = ComputeRouter() tandem = TandemCore() def classify(messages: list) -> str: """Auto-classify: reasoner for planning/research/audit, coder for code/debug/file ops.""" content = " ".join(m.get("content", "") for m in messages).lower() code_keywords = ["fix", "debug", "implement", "write code", "function", "class", "solidity", "script"] return "coder" if any(k in content for k in code_keywords) else "reasoner" @app.post("/v1/chat") async def chat(req: Request): body = await req.json() messages = body.get("messages", []) target = classify(messages) start = time.time() result = await router.route(messages, target=target) track_request(target, time.time() - start) return JSONResponse(result) @app.post("/v1/reason") async def reason(req: Request): body = await req.json() start = time.time() result = await router.route(body.get("messages", []), target="reasoner") track_request("reasoner", time.time() - start) return JSONResponse(result) @app.post("/v1/code") async def code(req: Request): body = await req.json() start = time.time() result = await router.route(body.get("messages", []), target="coder") track_request("coder", time.time() - start) return JSONResponse(result) @app.post("/v1/tandem") async def tandem_route(req: Request): body = await req.json() result = await tandem.run(body.get("messages", [])) return JSONResponse(result) @app.get("/health") async def health(): return {"status": "ok", "backends": router.health()} @app.get("/metrics") async def metrics(): from metrics import get_metrics return JSONResponse(get_metrics()) if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 8000)))