from __future__ import annotations from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from .models import ExecutionView, RunSummary, TaskDetail from .real_runs import RunNotFound, agent_data_root, execution_view, list_runs, task_detail app = FastAPI(title="Script Build Execution Observer", version="2.0.0") app.add_middleware( CORSMiddleware, allow_origins=["http://127.0.0.1:3008", "http://localhost:3008"], allow_methods=["GET"], allow_headers=["*"], ) @app.get("/api/health") def health() -> dict[str, str]: root = agent_data_root() return { "status": "ok" if root.exists() else "degraded", "data_mode": "persisted-real-run", "schema_version": "script-build-execution-view/v1", "agent_data_root": str(root), } @app.get("/api/runs", response_model=list[RunSummary]) def runs() -> list[RunSummary]: return list_runs() @app.get("/api/runs/{script_build_id}/execution", response_model=ExecutionView) def execution(script_build_id: int) -> ExecutionView: try: return execution_view(script_build_id) except RunNotFound as exc: raise HTTPException(status_code=404, detail=str(exc)) from exc @app.get("/api/runs/{script_build_id}/tasks/{task_id}", response_model=TaskDetail) def detail(script_build_id: int, task_id: str) -> TaskDetail: try: return task_detail(script_build_id, task_id) except RunNotFound as exc: raise HTTPException(status_code=404, detail=str(exc)) from exc