File size: 2,358 Bytes
3bae2a5
dd216e6
b4783e7
dd216e6
b4783e7
 
3bae2a5
dd216e6
 
3bae2a5
b4783e7
ed99d21
b4783e7
ed99d21
b4783e7
3bae2a5
db88cf2
3bae2a5
 
 
 
 
b4783e7
3bae2a5
 
 
 
ed99d21
3bae2a5
b4783e7
ed99d21
3bae2a5
ed99d21
3bae2a5
 
 
b4783e7
db88cf2
3bae2a5
 
 
 
 
db88cf2
 
3bae2a5
dd216e6
3bae2a5
 
 
 
 
db88cf2
ed99d21
3bae2a5
 
dd216e6
 
 
3bae2a5
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
import os, subprocess, json
from pathlib import Path
from fastapi import FastAPI, BackgroundTasks, HTTPException
from fastapi.responses import FileResponse, JSONResponse
from pydantic import BaseModel

app = FastAPI(title="Hermes Serverless")
SESSIONS_DIR = os.environ.get("SESSIONS_DIR", "/data/sessions")
Path(SESSIONS_DIR).mkdir(parents=True, exist_ok=True)
task_registry = {}

class AgentTask(BaseModel):
    session_id: str
    task: str

def execute_hermes(session_id: str, task: str):
    task_registry[session_id] = "running"
    workspace = Path(SESSIONS_DIR) / session_id
    workspace.mkdir(parents=True, exist_ok=True)
    log_path = workspace / "output.log"
    env = os.environ.copy()
    env.update({"HOME": "/root", "HERMES_HOME": "/root/.hermes"})
    try:
        with open(log_path, "a") as f:
            p = subprocess.Popen(["hermes", "chat", "-q", task, "--resume", session_id, "--yolo"], cwd=str(workspace), stdout=f, stderr=subprocess.STDOUT, env=env, text=True)
            rc = p.wait(timeout=1800)
        task_registry[session_id] = "completed" if rc == 0 else f"failed:{rc}"
    except Exception as e:
        task_registry[session_id] = f"error:{str(e)}"

@app.post("/agent/execute")
async def run(payload: AgentTask, bg: BackgroundTasks):
    if not payload.session_id or not payload.task:
        raise HTTPException(400, "Missing session_id or task")
    bg.add_task(execute_hermes, payload.session_id, payload.task)
    return {"status": "queued", "session_id": payload.session_id}

@app.get("/agent/status/{session_id}")
def status(session_id: str):
    st = task_registry.get(session_id, "unknown")
    log = Path(SESSIONS_DIR) / session_id / "output.log"
    logs = log.read_text()[-2000:] if log.exists() else ""
    return {"session_id": session_id, "status": st, "logs": logs}

@app.get("/agent/files/{session_id}/{filename}")
def file(session_id: str, filename: str):
    if "/" in filename or ".." in filename:
        raise HTTPException(400, "Invalid filename")
    path = Path(SESSIONS_DIR) / session_id / filename
    if not path.exists():
        raise HTTPException(404, "File not found")
    return FileResponse(str(path))

@app.get("/health")
def health():
    return {"status": "ok", "engine": "hermes-serverless"}

@app.get("/")
def root():
    return {"message": "Hermes Serverless API", "docs": "/docs"}