"""Execution control endpoints.""" from fastapi import APIRouter, HTTPException from backend.models.execution import ExecutionRequest from backend.services import execution_service from backend.services.storage_service import storage router = APIRouter(prefix="/api/execution", tags=["execution"]) @router.post("/run", status_code=201) async def start_run(req: ExecutionRequest): """Start an async workflow execution.""" # Resolve graph data if req.graph: graph_data = req.graph.model_dump() elif req.graph_id: stored = storage.get_graph(req.graph_id) if stored is None: raise HTTPException(status_code=404, detail=f"Graph '{req.graph_id}' not found") graph_data = stored else: raise HTTPException(status_code=400, detail="Either graph_id or graph must be provided") run_id = await execution_service.start_execution( graph_data=graph_data, task_query=req.task_query, config=req.config, llm_provider=req.llm_provider, ) return {"run_id": run_id, "status": "running"} @router.get("/{run_id}") def get_run(run_id: str): """Get execution status and result.""" detail = execution_service.get_run_detail(run_id) if detail is None: raise HTTPException(status_code=404, detail=f"Run '{run_id}' not found") return detail @router.delete("/{run_id}") def cancel_run(run_id: str): """Cancel a running execution.""" if not execution_service.cancel_execution(run_id): raise HTTPException(status_code=404, detail=f"Run '{run_id}' not found or already completed") return {"run_id": run_id, "status": "cancelled"} @router.get("/history/list") def list_runs(): """List all past runs.""" return execution_service.get_run_history()