rawknee-69's picture
Add interactive Logs Explorer tab for viewing raw files
8e45c5a
Raw History Blame Contribute Delete
6.47 kB
from __future__ import annotations
import os
from pathlib import Path
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from backend.api import baseline, battle, metrics, registry, websocket, logs_explorer
from backend.core.task_catalog import list_tasks
try:
from openenv.core.env_server.http_server import create_app as create_openenv_app
except Exception:
create_openenv_app = None
from backend.server.openenv_adapter import (
GOATOpenEnvAction,
GOATOpenEnvEnvironment,
GOATOpenEnvObservation,
)
FRONTEND_DIST = Path(__file__).parent.parent.parent / "frontend" / "dist"
PUBLIC_DIR = Path(__file__).parent.parent.parent / "public"
PUBLIC_INDEX = PUBLIC_DIR / "index.html"
_public_logs_candidate = Path(os.getenv("PUBLIC_LOGS_DIR", "/data/goat-logs"))
try:
_public_logs_candidate.mkdir(parents=True, exist_ok=True)
PUBLIC_LOGS_DIR = _public_logs_candidate
except PermissionError:
# Local/dev fallback when /data is unavailable. HF Spaces with persistent
# storage should set PUBLIC_LOGS_DIR=/data/... and remain on that path.
PUBLIC_LOGS_DIR = Path("logs/public")
PUBLIC_LOGS_DIR.mkdir(parents=True, exist_ok=True)
if create_openenv_app:
app: FastAPI = create_openenv_app(
GOATOpenEnvEnvironment,
GOATOpenEnvAction,
GOATOpenEnvObservation,
env_name="goat_evolution",
max_concurrent_envs=8,
)
else:
app = FastAPI(title="goat_evolution")
@app.get("/health")
async def health():
return {"status": "healthy", "environment": "goat_evolution"}
@app.get("/tasks")
async def tasks() -> list[dict[str, int | str]]:
return list_tasks()
@app.post("/mcp")
async def mcp_endpoint(request: Request) -> dict:
try:
body = await request.json()
except Exception:
body = {}
method = body.get("method", "")
rid = body.get("id", 1)
if method == "tools/list":
return {
"jsonrpc": "2.0",
"id": rid,
"result": {
"tools": [
{
"name": "deploy_attack_strategy",
"description": "Deploy ordered attack components for Red",
"inputSchema": {
"type": "object",
"properties": {
"role": {"type": "string", "enum": ["red"]},
"strategy": {"type": "array", "items": {"type": "string"}},
"thought": {"type": "string"},
},
"required": ["role", "strategy", "thought"],
},
},
{
"name": "deploy_defense",
"description": "Deploy defense components for Blue",
"inputSchema": {
"type": "object",
"properties": {
"role": {"type": "string", "enum": ["blue"]},
"defense": {"type": "array", "items": {"type": "string"}},
"thought": {"type": "string"},
},
"required": ["role", "defense", "thought"],
},
},
]
},
}
return {"jsonrpc": "2.0", "id": rid, "error": {"code": -32601, "message": "Method not found"}}
app.include_router(battle.router, prefix="/api/battle", tags=["Battle"])
app.include_router(registry.router, prefix="/api/registry", tags=["Registry"])
app.include_router(metrics.router, prefix="/api/metrics", tags=["Metrics"])
app.include_router(baseline.router, prefix="/api/baseline", tags=["Baseline"])
app.include_router(websocket.router, tags=["WebSocket"])
app.include_router(logs_explorer.router, prefix="/api/logs", tags=["Logs Explorer"])
@app.get("/raw-logs/baseline_scores.json")
async def serve_baseline_scores():
target = Path("training/baseline_scores.json").resolve()
if not target.is_file():
return JSONResponse({"error": "Not found"}, status_code=404)
return FileResponse(target)
@app.get("/raw-logs/{category}/{filepath:path}")
async def serve_raw_logs(category: str, filepath: str):
from backend.api.logs_explorer import _get_public_logs_dir, _get_app_logs_dir, _get_artifacts_dir
if category == "public":
base = _get_public_logs_dir()
elif category == "app":
base = _get_app_logs_dir()
elif category == "artifacts":
base = _get_artifacts_dir()
else:
return JSONResponse({"error": "Unknown category"}, status_code=404)
try:
target = (base / filepath).resolve()
if not str(target).startswith(str(base.resolve())):
return JSONResponse({"error": "Forbidden"}, status_code=403)
if not target.is_file():
return JSONResponse({"error": "Not found"}, status_code=404)
return FileResponse(target)
except Exception:
return JSONResponse({"error": "Bad path"}, status_code=400)
app.mount("/public-logs", StaticFiles(directory=PUBLIC_LOGS_DIR), name="public-logs")
@app.get("/public-logs-index")
async def public_logs_index() -> dict:
files = sorted(
[
str(path.relative_to(PUBLIC_LOGS_DIR))
for path in PUBLIC_LOGS_DIR.rglob("*")
if path.is_file()
]
)
return {
"logs_dir": str(PUBLIC_LOGS_DIR),
"count": len(files),
"files": files,
"links": [f"/public-logs/{name}" for name in files],
}
@app.get("/{full_path:path}")
async def serve_public_ui(full_path: str = ""):
skip = (
"api/",
"health",
"reset",
"step",
"state",
"ws",
"mcp",
"web",
"docs",
"redoc",
"openapi.json",
"public-logs",
"raw-logs",
)
if any(full_path.startswith(p) for p in skip):
return JSONResponse({"error": "Not found"}, status_code=404)
if PUBLIC_INDEX.exists():
return FileResponse(PUBLIC_INDEX)
if FRONTEND_DIST.exists():
return FileResponse(FRONTEND_DIST / "index.html")
return JSONResponse(
{"error": "UI unavailable: public/index.html not found"},
status_code=404,
)