From cfd568307a88e5eb05c197e9bf8cc7b416ba4da0 Mon Sep 17 00:00:00 2001 From: donnyjang Date: Sun, 4 Jan 2026 13:04:20 -0700 Subject: [PATCH] fixed progress updates to include all event data without a clientid --- __init__.py | 46 +++------------ progress.py | 160 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 133 insertions(+), 73 deletions(-) diff --git a/__init__.py b/__init__.py index b29175b..e6b509f 100644 --- a/__init__.py +++ b/__init__.py @@ -1,50 +1,16 @@ from server import PromptServer -from .progress import add_progress_hook +from .progress import patched_send_sync from .routes import SetupRoutes from .workflows import * from . import resources -from io import BytesIO -import json -# Custom binary event types (use high numbers to avoid conflicts) -IMAGELAB_PROGRESS_EVENT = 100 -IMAGELAB_PREVIEW_EVENT = 101 - -# Your custom progress callback -def my_progress_callback(value, total, preview_image, prompt_id, node_id): - # Send progress data as binary JSON - progress_data = { - 'value': value, - 'total': total, - 'prompt_id': prompt_id, - 'node_id': node_id, - 'progress_percent': round((value / total * 100), 2) if total > 0 else 0 - } - progress_bytes = json.dumps(progress_data).encode('utf-8') - PromptServer.instance.send_sync(IMAGELAB_PROGRESS_EVENT, progress_bytes, sid=None) - - # Send preview image separately if available - if preview_image is not None: - try: - # preview_image is a tuple: (format, PIL.Image, max_size) - if isinstance(preview_image, tuple) and len(preview_image) >= 2: - image_format = preview_image[0] # 'JPEG' or 'PNG' - pil_image = preview_image[1] - - # Convert PIL image to bytes - buffer = BytesIO() - pil_image.save(buffer, format=image_format, quality=95 if image_format == 'JPEG' else None) - image_bytes = buffer.getvalue() - - # Send as binary message with integer event type - PromptServer.instance.send_sync(IMAGELAB_PREVIEW_EVENT, image_bytes, sid=None) - except Exception as e: - print(f"[ImageLab] Error sending preview image: {e}") - -add_progress_hook(my_progress_callback) +# Apply the send_sync patch +PromptServer.instance.send_sync = patched_send_sync +print("[ImageLab] progress patch applied successfully") # Register API routes SetupRoutes(PromptServer.instance) +print("[ImageLab] routes registered successfully") # Optional: Index all new models at startup (set to True to enable) AUTO_INDEX_ON_STARTUP = True @@ -65,5 +31,7 @@ if AUTO_INDEX_ON_STARTUP: # Run in background thread to not block startup threading.Thread(target=index_on_startup, daemon=True).start() +print("[ImageLab] Initialization complete") + NODE_CLASS_MAPPINGS = {} NODE_DISPLAY_NAME_MAPPINGS = {} \ No newline at end of file diff --git a/progress.py b/progress.py index c5930d9..c3970b1 100644 --- a/progress.py +++ b/progress.py @@ -3,41 +3,133 @@ import sys comfyui_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) if comfyui_root not in sys.path: sys.path.insert(0, comfyui_root) -import comfy.utils -from comfy_execution.utils import get_executing_context + +from server import PromptServer +from io import BytesIO +from PIL import Image, ImageOps +import json +import struct +from protocol import BinaryEventTypes -def add_progress_hook(callback, server_instance=None): - """Add a progress callback that chains with ComfyUI's built-in hook. +# Custom binary event types for ImageLab +IMAGELAB_PROGRESS_EVENT = 100 +IMAGELAB_PREVIEW_EVENT = 101 +IMAGELAB_WORKFLOW_EVENT = 102 + +# Intercept send_sync FIRST (before creating handler that uses it) +# This is the KEY fix - hijack_progress in main.py sends "progress" and +# BinaryEventTypes.UNENCODED_PREVIEW_IMAGE directly to client_id, bypassing handlers. +# We need to intercept these and broadcast them to ALL clients. + +_original_send_sync = PromptServer.instance.send_sync +_is_broadcasting = False # Prevent infinite recursion + +def patched_send_sync(event, data, sid=None): + global _is_broadcasting - Args: - callback: Function(value, total, preview_image, prompt_id, node_id) to call on progress updates - server_instance: Optional server instance for fallback prompt_id/node_id - """ - _original_hook = None - _original_set_hook = comfy.utils.set_progress_bar_global_hook + # Debug: Log what events we're seeing + if event == 'progress': + print(f"[ImageLab DEBUG] Intercepted 'progress' event: {data}") + elif event == BinaryEventTypes.UNENCODED_PREVIEW_IMAGE: + print(f"[ImageLab DEBUG] Intercepted UNENCODED_PREVIEW_IMAGE") + elif event == BinaryEventTypes.PREVIEW_IMAGE_WITH_METADATA: + print(f"[ImageLab DEBUG] Intercepted PREVIEW_IMAGE_WITH_METADATA") - def hook(value, total, preview_image, prompt_id=None, node_id=None): - # Populate prompt_id and node_id from executing context if not provided - executing_context = get_executing_context() - if prompt_id is None and executing_context is not None: - prompt_id = executing_context.prompt_id - if node_id is None and executing_context is not None: - node_id = executing_context.node_id - # Fallback to server instance if available - if server_instance is not None: - if prompt_id is None: - prompt_id = getattr(server_instance, 'last_prompt_id', None) - if node_id is None: - node_id = getattr(server_instance, 'last_node_id', None) + # Always call original first + _original_send_sync(event, data, sid) + + # Prevent infinite recursion when we broadcast + if _is_broadcasting: + return + + try: + _is_broadcasting = True - callback(value, total, preview_image, prompt_id, node_id) - if _original_hook is not None: - _original_hook(value, total, preview_image, prompt_id=prompt_id, node_id=node_id) - - def wrapped_set_hook(function): - nonlocal _original_hook - _original_hook = function - _original_set_hook(hook) - - comfy.utils.set_progress_bar_global_hook = wrapped_set_hook - comfy.utils.set_progress_bar_global_hook(hook) \ No newline at end of file + # Broadcast workflow events to ALL clients + workflow_events = {'executing', 'execution_start', 'executed', 'execution_error', 'execution_cached'} + if event in workflow_events: + workflow_data = {'event': event, 'data': data} + _original_send_sync(IMAGELAB_WORKFLOW_EVENT, json.dumps(workflow_data).encode('utf-8'), sid=None) + + # Broadcast "progress" event (sampler steps) to ALL clients + # This is sent by hijack_progress() in main.py only to client_id + elif event == 'progress' and isinstance(data, dict): + progress_data = { + 'event': 'sampler_progress', + 'node_id': data.get('node'), + 'prompt_id': data.get('prompt_id'), + 'value': data.get('value', 0), + 'max': data.get('max', 1), + 'progress_percent': round((data.get('value', 0) / data.get('max', 1) * 100), 2) if data.get('max', 1) > 0 else 0 + } + _original_send_sync(IMAGELAB_PROGRESS_EVENT, json.dumps(progress_data).encode('utf-8'), sid=None) + + # Broadcast preview images to ALL clients + # BinaryEventTypes.UNENCODED_PREVIEW_IMAGE is sent by hijack_progress() only to client_id + elif event == BinaryEventTypes.UNENCODED_PREVIEW_IMAGE: + # data is a tuple: (format, PIL.Image, max_size) + try: + img_format = data[0] + pil_image = data[1] + max_size = data[2] if len(data) > 2 else None + + # Resize if needed + if max_size is not None: + if hasattr(Image, 'Resampling'): + resampling = Image.Resampling.BILINEAR + else: + resampling = Image.BILINEAR + pil_image = ImageOps.contain(pil_image, (max_size, max_size), resampling) + + # Create metadata (we don't have node_id/prompt_id here, but server has last_node_id) + metadata = json.dumps({ + 'node_id': getattr(PromptServer.instance, 'last_node_id', None), + 'prompt_id': getattr(PromptServer.instance, 'last_prompt_id', None) + }).encode('utf-8') + + # Encode image + buffer = BytesIO() + pil_image.save(buffer, format=img_format, quality=95 if img_format == 'JPEG' else None, compress_level=1 if img_format == 'PNG' else None) + image_bytes = buffer.getvalue() + + # Pack: metadata_length (4 bytes) + metadata + image + combined = struct.pack(">I", len(metadata)) + metadata + image_bytes + _original_send_sync(IMAGELAB_PREVIEW_EVENT, combined, sid=None) + except Exception as e: + print(f"[ImageLab] Preview broadcast error: {e}") + + # Also broadcast PREVIEW_IMAGE_WITH_METADATA + elif event == BinaryEventTypes.PREVIEW_IMAGE_WITH_METADATA: + # data is a tuple: ((format, PIL.Image, max_size), metadata_dict) + try: + image_data, metadata = data + img_format = image_data[0] + pil_image = image_data[1] + max_size = image_data[2] if len(image_data) > 2 else None + + # Resize if needed + if max_size is not None: + if hasattr(Image, 'Resampling'): + resampling = Image.Resampling.BILINEAR + else: + resampling = Image.BILINEAR + pil_image = ImageOps.contain(pil_image, (max_size, max_size), resampling) + + # Encode metadata + metadata_bytes = json.dumps(metadata).encode('utf-8') + + # Encode image + buffer = BytesIO() + pil_image.save(buffer, format=img_format, quality=95 if img_format == 'JPEG' else None, compress_level=1 if img_format == 'PNG' else None) + image_bytes = buffer.getvalue() + + # Pack: metadata_length (4 bytes) + metadata + image + combined = struct.pack(">I", len(metadata_bytes)) + metadata_bytes + image_bytes + _original_send_sync(IMAGELAB_PREVIEW_EVENT, combined, sid=None) + except Exception as e: + print(f"[ImageLab] Preview metadata broadcast error: {e}") + + except Exception as e: + print(f"[ImageLab] Broadcast error: {e}") + finally: + _is_broadcasting = False \ No newline at end of file