main.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import annotations
  2. from fastapi import FastAPI, HTTPException
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from .models import ExecutionView, RunSummary, TaskDetail
  5. from .real_runs import RunNotFound, agent_data_root, execution_view, list_runs, task_detail
  6. app = FastAPI(title="Script Build Execution Observer", version="2.0.0")
  7. app.add_middleware(
  8. CORSMiddleware,
  9. allow_origins=["http://127.0.0.1:3008", "http://localhost:3008"],
  10. allow_methods=["GET"],
  11. allow_headers=["*"],
  12. )
  13. @app.get("/api/health")
  14. def health() -> dict[str, str]:
  15. root = agent_data_root()
  16. return {
  17. "status": "ok" if root.exists() else "degraded",
  18. "data_mode": "persisted-real-run",
  19. "schema_version": "script-build-execution-view/v1",
  20. "agent_data_root": str(root),
  21. }
  22. @app.get("/api/runs", response_model=list[RunSummary])
  23. def runs() -> list[RunSummary]:
  24. return list_runs()
  25. @app.get("/api/runs/{script_build_id}/execution", response_model=ExecutionView)
  26. def execution(script_build_id: int) -> ExecutionView:
  27. try:
  28. return execution_view(script_build_id)
  29. except RunNotFound as exc:
  30. raise HTTPException(status_code=404, detail=str(exc)) from exc
  31. @app.get("/api/runs/{script_build_id}/tasks/{task_id}", response_model=TaskDetail)
  32. def detail(script_build_id: int, task_id: str) -> TaskDetail:
  33. try:
  34. return task_detail(script_build_id, task_id)
  35. except RunNotFound as exc:
  36. raise HTTPException(status_code=404, detail=str(exc)) from exc