Archived
feat: add optional client_id support for WebSocket connections in workflow execution and loading
This commit is contained in:
1 parent
ed0ac8090b
commit
82559292b1
2 files changed
+40
-7
No files matched your search
@@ -96,12 +96,21 @@ def SetupRoutes(server: PromptServer):
|
||||
|
||||
@server.instance.routes.post("/imagelab/workflow/{workflow_name}")
|
||||
async def execute_workflow(request):
|
||||
"""Execute a specific workflow by name."""
|
||||
"""
|
||||
Execute a specific workflow by name.
|
||||
|
||||
Query params:
|
||||
- client_id: Optional client ID for WebSocket connection.
|
||||
Pass this to receive binary preview frames on your WebSocket.
|
||||
"""
|
||||
workflow_name = request.match_info["workflow_name"]
|
||||
|
||||
# Extract client_id from query params (used for WebSocket preview routing)
|
||||
client_id = request.query.get("client_id")
|
||||
|
||||
try:
|
||||
# Get the workflow function
|
||||
workflow_func = get_workflow(workflow_name)
|
||||
# Get the workflow function (passing client_id to configure comfy_script)
|
||||
workflow_func = get_workflow(workflow_name, client_id=client_id)
|
||||
|
||||
# Parse request body for parameters
|
||||
try:
|
||||
|
||||
+28
-4
@@ -40,14 +40,33 @@ _workflow_cache_time: float = 0
|
||||
_yaml_schema_cache: Dict[str, Optional[Dict]] = {}
|
||||
|
||||
|
||||
def load_workflow_module(workflow_name: str):
|
||||
"""Dynamically load/reload a workflow module from the workflows folder."""
|
||||
def load_workflow_module(workflow_name: str, client_id: str = None):
|
||||
"""
|
||||
Dynamically load/reload a workflow module from the workflows folder.
|
||||
|
||||
Args:
|
||||
workflow_name: Name of the workflow to load
|
||||
client_id: Optional client ID to use for comfy_script WebSocket.
|
||||
If provided, this ID will be used instead of a generated one,
|
||||
allowing external clients to receive binary preview frames.
|
||||
"""
|
||||
workflow_path = os.path.join(
|
||||
os.path.dirname(__file__), "workflows", f"{workflow_name}.py"
|
||||
)
|
||||
if not os.path.exists(workflow_path):
|
||||
raise FileNotFoundError(f"Workflow script not found: {workflow_path}")
|
||||
|
||||
# If client_id is provided, set it in comfy_script BEFORE loading the workflow
|
||||
# This ensures comfy_script uses this ID when it creates its WebSocket connection
|
||||
if client_id:
|
||||
try:
|
||||
import comfy_script.runtime as runtime
|
||||
# Set the client_id before the workflow module imports and calls load()
|
||||
runtime._client_id = client_id
|
||||
print(f"[workflows] Set comfy_script client_id to: {client_id}")
|
||||
except ImportError:
|
||||
print("[workflows] Warning: Could not import comfy_script.runtime to set client_id")
|
||||
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
f"workflows.{workflow_name}", workflow_path
|
||||
)
|
||||
@@ -211,10 +230,15 @@ def discover_workflows(force_refresh: bool = False) -> Dict[str, dict]:
|
||||
return discovered
|
||||
|
||||
|
||||
def get_workflow(name: str) -> Callable:
|
||||
def get_workflow(name: str, client_id: str = None) -> Callable:
|
||||
"""
|
||||
Get a workflow function by name.
|
||||
Reloads the module from disk each time for hot reloading.
|
||||
|
||||
Args:
|
||||
name: Workflow name/ID
|
||||
client_id: Optional client ID for comfy_script WebSocket connection.
|
||||
Pass this to receive binary preview frames on your WebSocket.
|
||||
"""
|
||||
workflows = discover_workflows()
|
||||
|
||||
@@ -224,7 +248,7 @@ def get_workflow(name: str) -> Callable:
|
||||
)
|
||||
|
||||
# Reload the module from disk each time
|
||||
module = load_workflow_module(name)
|
||||
module = load_workflow_module(name, client_id=client_id)
|
||||
return module.run_workflow
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user