TraceStudio/agent/main.py

46 lines
1.2 KiB
Python
Raw Permalink Normal View History

2026-01-13 00:29:18 +08:00
import os
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from core.runner import load_config, run_tool
from core.models import RunRequest, RunResult
CONFIG = load_config()
app = FastAPI(title="TraceStudio Agent", version="0.1.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"] ,
allow_headers=["*"],
)
@app.get("/health")
def health():
tools = list((CONFIG.get("tools") or {}).keys())
return {"status": "ok", "tools": tools}
@app.post("/run", response_model=RunResult)
def run(req: RunRequest):
try:
return run_tool(CONFIG, req)
except FileNotFoundError as e:
raise HTTPException(status_code=404, detail=str(e))
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
host = os.environ.get("AGENT_HOST", "0.0.0.0")
port = int(os.environ.get("AGENT_PORT", "8100"))
import uvicorn
uvicorn.run("main:app", host=host, port=port, reload=False)