runs.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Formal pipeline run API routes."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from uuid import UUID
  5. from fastapi import APIRouter, Depends, HTTPException
  6. from app.dependencies import get_pipeline_repository
  7. router = APIRouter(prefix="/api/pipeline", tags=["pipeline"])
  8. @router.get("/runs/{run_id}")
  9. def pipeline_run(
  10. run_id: UUID,
  11. repo: Any = Depends(get_pipeline_repository),
  12. ) -> dict[str, Any]:
  13. getter = getattr(repo, "get_pipeline_run", None)
  14. if getter is None:
  15. raise HTTPException(status_code=501, detail="pipeline run reader is not implemented")
  16. try:
  17. run = getter(run_id)
  18. except RuntimeError as exc:
  19. raise HTTPException(status_code=404, detail="pipeline run not found") from exc
  20. return {"run": run.model_dump(mode="json") if hasattr(run, "model_dump") else run}
  21. @router.get("/runs/{run_id}/timeline")
  22. def pipeline_run_timeline(
  23. run_id: UUID,
  24. repo: Any = Depends(get_pipeline_repository),
  25. ) -> dict[str, Any]:
  26. getter = getattr(repo, "get_pipeline_run", None)
  27. list_timeline = getattr(repo, "list_timeline", None)
  28. if getter is None or list_timeline is None:
  29. raise HTTPException(status_code=501, detail="pipeline timeline reader is not implemented")
  30. try:
  31. run = getter(run_id)
  32. except RuntimeError as exc:
  33. raise HTTPException(status_code=404, detail="pipeline run not found") from exc
  34. bundle_getter = getattr(repo, "get_timeline_bundle", None)
  35. if bundle_getter is not None:
  36. bundle = bundle_getter(run_id)
  37. else:
  38. bundle = {"events": list_timeline(run_id), "jobs": [], "candidate_hits": [], "llm_call_traces": []}
  39. return {
  40. "run": run.model_dump(mode="json") if hasattr(run, "model_dump") else run,
  41. **bundle,
  42. }
  43. @router.get("/runs/by-acquisition/{acquisition_run_id}")
  44. def pipeline_run_by_acquisition(
  45. acquisition_run_id: UUID,
  46. repo: Any = Depends(get_pipeline_repository),
  47. ) -> dict[str, Any]:
  48. getter = getattr(repo, "get_pipeline_run_by_acquisition_run", None)
  49. if getter is None:
  50. raise HTTPException(status_code=501, detail="pipeline acquisition lookup is not implemented")
  51. try:
  52. run = getter(acquisition_run_id)
  53. except RuntimeError as exc:
  54. raise HTTPException(status_code=404, detail="pipeline run not found") from exc
  55. return {"run": run.model_dump(mode="json") if hasattr(run, "model_dump") else run}