Archived
172 lines
6.0 KiB
Python
172 lines
6.0 KiB
Python
"""
|
|
API routes for external workflow execution.
|
|
"""
|
|
from aiohttp import web
|
|
from server import PromptServer
|
|
|
|
from .workflows import get_workflow, get_workflow_metadata, list_workflows
|
|
from . import resources
|
|
|
|
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/resources")
|
|
async def get_all_resources(request):
|
|
"""Get all available resources (models, samplers, etc.) - fast, no indexing"""
|
|
try:
|
|
models = resources.get_all_models()
|
|
|
|
return web.json_response({
|
|
'success': True,
|
|
'resources': {
|
|
**models,
|
|
'samplers': resources.get_samplers(),
|
|
'schedulers': resources.get_schedulers(),
|
|
'upscale_methods': resources.get_upscale_methods()
|
|
}
|
|
})
|
|
except Exception as e:
|
|
return web.json_response({
|
|
'success': False,
|
|
'error': str(e)
|
|
}, status=500)
|
|
|
|
@server.instance.routes.get("/imagelab/resources/{resource_type}")
|
|
async def get_resource_by_type(request):
|
|
"""Get resources of a specific type - fast, no indexing"""
|
|
resource_type = request.match_info['resource_type']
|
|
|
|
try:
|
|
# Handle special resource types
|
|
if resource_type == 'samplers':
|
|
result = resources.get_samplers()
|
|
elif resource_type == 'schedulers':
|
|
result = resources.get_schedulers()
|
|
elif resource_type == 'upscale_methods':
|
|
result = resources.get_upscale_methods()
|
|
else:
|
|
# Try to get as a model type
|
|
result = resources.get_models_by_type(resource_type)
|
|
|
|
return web.json_response({
|
|
'success': True,
|
|
'resource_type': resource_type,
|
|
'items': result
|
|
})
|
|
except Exception as e:
|
|
return web.json_response({
|
|
'success': False,
|
|
'error': str(e)
|
|
}, status=500)
|
|
|
|
@server.instance.routes.post("/imagelab/resources/{resource_type}/index/{filename}")
|
|
async def index_model_file(request):
|
|
"""Index a specific model file - calculate hash and fetch metadata"""
|
|
resource_type = request.match_info['resource_type']
|
|
filename = request.match_info['filename']
|
|
|
|
try:
|
|
metadata = resources.index_model_by_name(resource_type, filename)
|
|
|
|
return web.json_response({
|
|
'success': True,
|
|
'resource_type': resource_type,
|
|
'filename': filename,
|
|
'metadata': metadata
|
|
})
|
|
except FileNotFoundError as e:
|
|
return web.json_response({
|
|
'success': False,
|
|
'error': str(e)
|
|
}, status=404)
|
|
except Exception as e:
|
|
return web.json_response({
|
|
'success': False,
|
|
'error': str(e)
|
|
}, status=500)
|
|
|
|
@server.instance.routes.post("/imagelab/resources/index-all")
|
|
async def index_all_models(request):
|
|
"""Index all models that don't have index files yet"""
|
|
try:
|
|
indexed_counts = resources.index_all_new_models()
|
|
|
|
return web.json_response({
|
|
'success': True,
|
|
'message': 'Indexing complete',
|
|
'indexed': indexed_counts
|
|
})
|
|
except Exception as e:
|
|
return web.json_response({
|
|
'success': False,
|
|
'error': 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!'
|
|
}) |