2025-03-17 14:58:05 +08:00
|
|
|
# api/workflow_api.py
|
|
|
|
from typing import Dict, Any, List, Optional
|
|
|
|
from fastapi import APIRouter, HTTPException, Depends, Query
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from services.workflow_service import WorkflowService
|
|
|
|
from core.exceptions import TianfengTaskError
|
2025-03-17 18:31:20 +08:00
|
|
|
from api.models import (
|
|
|
|
ApiResponse, WorkflowInput, WorkflowUpdateInput,
|
|
|
|
WorkflowExecuteInput, WorkflowImportInput
|
|
|
|
)
|
2025-03-17 14:58:05 +08:00
|
|
|
from core.workflow import WorkflowDefinition
|
|
|
|
from config.api_config import ApiResponseCode, ApiResponseMessage
|
|
|
|
|
|
|
|
# 创建路由器
|
|
|
|
router = APIRouter(prefix="/workflow", tags=["工作流管理"])
|
|
|
|
|
|
|
|
# 创建服务实例
|
|
|
|
workflow_service = WorkflowService()
|
|
|
|
|
|
|
|
@router.get("/workflows", response_model=ApiResponse)
|
|
|
|
async def get_workflows(
|
|
|
|
type: Optional[str] = Query(None, description="工作流类型")
|
|
|
|
):
|
|
|
|
"""获取工作流列表"""
|
|
|
|
try:
|
|
|
|
# 获取工作流列表
|
|
|
|
workflows = workflow_service.get_all_workflows(type)
|
|
|
|
|
|
|
|
# 转换为字典列表
|
|
|
|
workflow_dicts = [wf.to_dict() for wf in workflows]
|
|
|
|
|
|
|
|
return {
|
|
|
|
"code": ApiResponseCode.SUCCESS,
|
|
|
|
"message": "获取工作流列表成功",
|
|
|
|
"data": workflow_dicts
|
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.SERVER_ERROR, detail=f"获取工作流列表失败: {str(e)}")
|
|
|
|
|
|
|
|
@router.get("/workflows/{workflow_id}", response_model=ApiResponse)
|
|
|
|
async def get_workflow(workflow_id: str):
|
|
|
|
"""获取工作流详情"""
|
|
|
|
try:
|
|
|
|
# 获取工作流
|
|
|
|
workflow = workflow_service.get_workflow_by_id(workflow_id)
|
|
|
|
|
|
|
|
if not workflow:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.NOT_FOUND, detail=f"找不到工作流: {workflow_id}")
|
|
|
|
|
|
|
|
return {
|
|
|
|
"code": ApiResponseCode.SUCCESS,
|
|
|
|
"message": "获取工作流详情成功",
|
|
|
|
"data": workflow.to_dict()
|
|
|
|
}
|
|
|
|
except HTTPException:
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.SERVER_ERROR, detail=f"获取工作流详情失败: {str(e)}")
|
|
|
|
|
|
|
|
@router.post("/workflows", response_model=ApiResponse)
|
|
|
|
async def create_workflow(workflow_input: WorkflowInput):
|
|
|
|
"""创建工作流"""
|
|
|
|
try:
|
|
|
|
# 创建工作流
|
|
|
|
workflow = workflow_service.create_workflow(
|
|
|
|
name=workflow_input.name,
|
|
|
|
workflow_type=workflow_input.workflow_type,
|
|
|
|
description=workflow_input.description,
|
|
|
|
blocks=workflow_input.blocks,
|
|
|
|
variables=workflow_input.variables,
|
|
|
|
schedule=workflow_input.schedule
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"code": ApiResponseCode.SUCCESS,
|
|
|
|
"message": ApiResponseMessage.Workflow.CREATED,
|
|
|
|
"data": workflow.to_dict()
|
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.SERVER_ERROR, detail=f"创建工作流失败: {str(e)}")
|
|
|
|
|
|
|
|
@router.put("/workflows/{workflow_id}", response_model=ApiResponse)
|
|
|
|
async def update_workflow(workflow_id: str, workflow_input: WorkflowUpdateInput):
|
|
|
|
"""更新工作流"""
|
|
|
|
try:
|
|
|
|
# 更新工作流
|
|
|
|
workflow = workflow_service.update_workflow(
|
|
|
|
workflow_id=workflow_id,
|
|
|
|
name=workflow_input.name,
|
|
|
|
description=workflow_input.description,
|
|
|
|
blocks=workflow_input.blocks,
|
|
|
|
variables=workflow_input.variables,
|
|
|
|
schedule=workflow_input.schedule
|
|
|
|
)
|
|
|
|
|
|
|
|
if not workflow:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.NOT_FOUND, detail=f"找不到工作流: {workflow_id}")
|
|
|
|
|
|
|
|
return {
|
|
|
|
"code": ApiResponseCode.SUCCESS,
|
|
|
|
"message": ApiResponseMessage.Workflow.UPDATED,
|
|
|
|
"data": workflow.to_dict()
|
|
|
|
}
|
|
|
|
except HTTPException:
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.SERVER_ERROR, detail=f"更新工作流失败: {str(e)}")
|
|
|
|
|
|
|
|
@router.delete("/workflows/{workflow_id}", response_model=ApiResponse)
|
|
|
|
async def delete_workflow(workflow_id: str):
|
|
|
|
"""删除工作流"""
|
|
|
|
try:
|
|
|
|
# 删除工作流
|
|
|
|
success = workflow_service.delete_workflow(workflow_id)
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.NOT_FOUND, detail=f"找不到工作流: {workflow_id}")
|
|
|
|
|
|
|
|
return {
|
|
|
|
"code": ApiResponseCode.SUCCESS,
|
|
|
|
"message": ApiResponseMessage.Workflow.DELETED,
|
|
|
|
"data": None
|
|
|
|
}
|
|
|
|
except HTTPException:
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.SERVER_ERROR, detail=f"删除工作流失败: {str(e)}")
|
|
|
|
|
|
|
|
@router.post("/workflows/{workflow_id}/execute", response_model=ApiResponse)
|
|
|
|
async def execute_workflow(workflow_id: str, execute_input: WorkflowExecuteInput):
|
|
|
|
"""执行工作流"""
|
|
|
|
try:
|
|
|
|
# 获取工作流
|
|
|
|
workflow = workflow_service.get_workflow_by_id(workflow_id)
|
|
|
|
|
|
|
|
if not workflow:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.NOT_FOUND, detail=f"找不到工作流: {workflow_id}")
|
|
|
|
|
|
|
|
# 执行工作流
|
|
|
|
result = workflow_service.execute_workflow(
|
|
|
|
workflow=workflow,
|
|
|
|
task_inputs=execute_input.task_inputs
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"code": ApiResponseCode.SUCCESS,
|
|
|
|
"message": ApiResponseMessage.Workflow.EXECUTED,
|
|
|
|
"data": result
|
|
|
|
}
|
|
|
|
except HTTPException:
|
|
|
|
raise
|
|
|
|
except TianfengTaskError as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.BAD_REQUEST, detail=str(e))
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.SERVER_ERROR, detail=f"执行工作流失败: {str(e)}")
|
|
|
|
|
|
|
|
@router.post("/workflows/import", response_model=ApiResponse)
|
|
|
|
async def import_workflow(import_input: WorkflowImportInput):
|
|
|
|
"""导入工作流"""
|
|
|
|
try:
|
|
|
|
# 导入工作流
|
|
|
|
workflow = workflow_service.import_workflow(import_input.workflow_json)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"code": ApiResponseCode.SUCCESS,
|
|
|
|
"message": ApiResponseMessage.Workflow.IMPORTED,
|
|
|
|
"data": workflow.to_dict()
|
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.SERVER_ERROR, detail=f"导入工作流失败: {str(e)}")
|
|
|
|
|
|
|
|
@router.get("/workflows/{workflow_id}/export", response_model=ApiResponse)
|
|
|
|
async def export_workflow(workflow_id: str):
|
|
|
|
"""导出工作流"""
|
|
|
|
try:
|
|
|
|
# 导出工作流
|
|
|
|
workflow_json = workflow_service.export_workflow(workflow_id)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"code": ApiResponseCode.SUCCESS,
|
|
|
|
"message": ApiResponseMessage.Workflow.EXPORTED,
|
|
|
|
"data": {"workflow_json": workflow_json}
|
|
|
|
}
|
|
|
|
except TianfengTaskError as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.BAD_REQUEST, detail=str(e))
|
|
|
|
except Exception as e:
|
|
|
|
raise HTTPException(status_code=ApiResponseCode.SERVER_ERROR, detail=f"导出工作流失败: {str(e)}")
|