104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
import json
|
|
from typing import Optional, List
|
|
|
|
from fastapi import APIRouter, Body, HTTPException, Path
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from workflow_service.comfy import comfy_workflow
|
|
from workflow_service.database.api import (
|
|
save_workflow,
|
|
get_all_workflows,
|
|
delete_workflow,
|
|
get_workflow,
|
|
)
|
|
|
|
|
|
workflow_router = APIRouter(
|
|
prefix="/api/workflow",
|
|
tags=["Workflow"],
|
|
)
|
|
|
|
|
|
@workflow_router.post("", status_code=201)
|
|
async def publish_workflow_endpoint(data: dict = Body(...)):
|
|
"""
|
|
发布工作流
|
|
"""
|
|
try:
|
|
name, wf_json = data.get("name"), data.get("workflow")
|
|
if not name or not wf_json:
|
|
raise HTTPException(
|
|
status_code=400, detail="`name` and `workflow` fields are required."
|
|
)
|
|
await save_workflow(name, json.dumps(wf_json))
|
|
print(f"Workflow '{name}' published.")
|
|
return JSONResponse(
|
|
content={"status": "success", "message": f"Workflow '{name}' published."},
|
|
status_code=201,
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Failed to save workflow: {e}")
|
|
|
|
|
|
@workflow_router.get("", response_model=List[dict])
|
|
async def get_all_workflows_endpoint():
|
|
"""
|
|
获取所有工作流
|
|
"""
|
|
try:
|
|
return await get_all_workflows()
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Failed to get workflows: {e}")
|
|
|
|
|
|
@workflow_router.delete("/{workflow_name:path}")
|
|
async def delete_workflow_endpoint(
|
|
workflow_name: str = Path(
|
|
...,
|
|
description="The full, unique name of the workflow to delete, e.g., 'my_workflow [20250101120000]'",
|
|
)
|
|
):
|
|
"""
|
|
删除工作流
|
|
"""
|
|
try:
|
|
success = await delete_workflow(workflow_name)
|
|
if success:
|
|
return {"status": "deleted", "name": workflow_name}
|
|
else:
|
|
raise HTTPException(
|
|
status_code=404, detail=f"Workflow '{workflow_name}' not found"
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Failed to delete workflow: {e}")
|
|
|
|
|
|
@workflow_router.get("/{base_name:path}")
|
|
async def get_one_workflow_endpoint(base_name: str, version: Optional[str] = None):
|
|
"""
|
|
获取工作流规范
|
|
"""
|
|
workflow_data = await get_workflow(base_name, version)
|
|
|
|
if not workflow_data:
|
|
detail = (
|
|
f"Workflow '{base_name}'"
|
|
+ (f" with version '{version}'" if version else " (latest)")
|
|
+ " not found."
|
|
)
|
|
raise HTTPException(status_code=404, detail=detail)
|
|
|
|
try:
|
|
workflow = json.loads(workflow_data["workflow_json"])
|
|
return {
|
|
"workflow": workflow,
|
|
"api_spec": comfy_workflow.parse_api_spec(workflow),
|
|
"inputs_json_schema": comfy_workflow.parse_inputs_json_schema(workflow),
|
|
}
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=500, detail=f"Failed to parse workflow specification: {e}"
|
|
)
|