"""Formal pipeline run API routes.""" from __future__ import annotations from typing import Any from uuid import UUID from fastapi import APIRouter, Depends, HTTPException from app.dependencies import get_pipeline_repository router = APIRouter(prefix="/api/pipeline", tags=["pipeline"]) @router.get("/runs/{run_id}") def pipeline_run( run_id: UUID, repo: Any = Depends(get_pipeline_repository), ) -> dict[str, Any]: getter = getattr(repo, "get_pipeline_run", None) if getter is None: raise HTTPException(status_code=501, detail="pipeline run reader is not implemented") try: run = getter(run_id) except RuntimeError as exc: raise HTTPException(status_code=404, detail="pipeline run not found") from exc return {"run": run.model_dump(mode="json") if hasattr(run, "model_dump") else run} @router.get("/runs/{run_id}/timeline") def pipeline_run_timeline( run_id: UUID, repo: Any = Depends(get_pipeline_repository), ) -> dict[str, Any]: getter = getattr(repo, "get_pipeline_run", None) list_timeline = getattr(repo, "list_timeline", None) if getter is None or list_timeline is None: raise HTTPException(status_code=501, detail="pipeline timeline reader is not implemented") try: run = getter(run_id) except RuntimeError as exc: raise HTTPException(status_code=404, detail="pipeline run not found") from exc bundle_getter = getattr(repo, "get_timeline_bundle", None) if bundle_getter is not None: bundle = bundle_getter(run_id) else: bundle = {"events": list_timeline(run_id), "jobs": [], "candidate_hits": [], "llm_call_traces": []} return { "run": run.model_dump(mode="json") if hasattr(run, "model_dump") else run, **bundle, } @router.get("/runs/by-acquisition/{acquisition_run_id}") def pipeline_run_by_acquisition( acquisition_run_id: UUID, repo: Any = Depends(get_pipeline_repository), ) -> dict[str, Any]: getter = getattr(repo, "get_pipeline_run_by_acquisition_run", None) if getter is None: raise HTTPException(status_code=501, detail="pipeline acquisition lookup is not implemented") try: run = getter(acquisition_run_id) except RuntimeError as exc: raise HTTPException(status_code=404, detail="pipeline run not found") from exc return {"run": run.model_dump(mode="json") if hasattr(run, "model_dump") else run}