File size: 2,164 Bytes
6ff51b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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)))