""" API routes for external workflow execution. """ from aiohttp import web from server import PromptServer from .workflows import get_workflow, get_workflow_metadata, list_workflows def SetupRoutes(server: PromptServer): """Register API routes with the ComfyUI server.""" @server.instance.routes.get("/imagelab/workflows") async def list_available_workflows(request): """List all available workflows with metadata.""" return web.json_response({ 'workflows': list_workflows() }) @server.instance.routes.get("/imagelab/workflow/{workflow_name}/metadata") async def get_workflow_info(request): """Get metadata for a specific workflow.""" workflow_name = request.match_info['workflow_name'] try: metadata = get_workflow_metadata(workflow_name) return web.json_response({ 'success': True, 'workflow': workflow_name, 'metadata': metadata }) except ValueError as e: return web.json_response({ 'success': False, 'error': str(e) }, status=404) @server.instance.routes.post("/imagelab/workflow/{workflow_name}") async def execute_workflow(request): """Execute a specific workflow by name.""" workflow_name = request.match_info['workflow_name'] try: # Get the workflow function workflow_func = get_workflow(workflow_name) # Parse request body for parameters try: params = await request.json() except: params = {} # Execute workflow result = workflow_func(**params) return web.json_response({ 'success': True, 'workflow': workflow_name, 'result': result }) except ValueError as e: return web.json_response({ 'success': False, 'type': 'ValueError', 'error': str(e) }, status=404) except Exception as e: return web.json_response({ 'success': False, 'type': 'Exception', 'error': f"Workflow execution failed: {str(e)}" }, status=500) @server.instance.routes.get("/imagelab/hello") async def get_hello(request): """Test endpoint.""" return web.json_response({ 'message': 'Hello from external API!' })