File size: 1,792 Bytes
3193174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""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()