58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
|
|
"""
|
||
|
|
工作流编排器 API
|
||
|
|
|
||
|
|
提供启动自动工作流、查看运行状态的端点
|
||
|
|
"""
|
||
|
|
|
||
|
|
from fastapi import APIRouter, HTTPException
|
||
|
|
from pydantic import BaseModel
|
||
|
|
from typing import Dict, Optional
|
||
|
|
|
||
|
|
from ..services.workflow_orchestrator import get_workflow_orchestrator
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/orchestrator", tags=["orchestrator"])
|
||
|
|
|
||
|
|
|
||
|
|
class StartWorkflowRequest(BaseModel):
|
||
|
|
"""启动工作流请求"""
|
||
|
|
workflow_path: str # YAML 文件名,如 dinner-decision.yaml
|
||
|
|
agent_overrides: Optional[Dict[str, str]] = None # agent_id → model 覆盖
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/start")
|
||
|
|
async def start_workflow(request: StartWorkflowRequest):
|
||
|
|
"""启动一个工作流的自动编排(后台异步执行)"""
|
||
|
|
orchestrator = get_workflow_orchestrator()
|
||
|
|
try:
|
||
|
|
run = await orchestrator.start_workflow(
|
||
|
|
workflow_path=request.workflow_path,
|
||
|
|
agent_overrides=request.agent_overrides,
|
||
|
|
)
|
||
|
|
return {
|
||
|
|
"success": True,
|
||
|
|
"message": f"工作流已启动: {run.workflow_name}",
|
||
|
|
"run_id": run.run_id,
|
||
|
|
"workflow_id": run.workflow_id,
|
||
|
|
}
|
||
|
|
except FileNotFoundError:
|
||
|
|
raise HTTPException(status_code=404, detail=f"工作流文件不存在: {request.workflow_path}")
|
||
|
|
except Exception as e:
|
||
|
|
raise HTTPException(status_code=500, detail=str(e))
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/runs")
|
||
|
|
async def list_runs():
|
||
|
|
"""列出所有编排运行"""
|
||
|
|
orchestrator = get_workflow_orchestrator()
|
||
|
|
return {"runs": orchestrator.list_runs()}
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/runs/{run_id}")
|
||
|
|
async def get_run(run_id: str):
|
||
|
|
"""获取指定运行的详细状态"""
|
||
|
|
orchestrator = get_workflow_orchestrator()
|
||
|
|
run = orchestrator.get_run(run_id)
|
||
|
|
if not run:
|
||
|
|
raise HTTPException(status_code=404, detail=f"运行不存在: {run_id}")
|
||
|
|
return run.to_dict()
|