Upload folder using huggingface_hub (part 5)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +1 -0
- ComfyUI/comfy_execution/graph.py +342 -0
- ComfyUI/comfy_execution/graph_utils.py +155 -0
- ComfyUI/comfy_execution/jobs.py +520 -0
- ComfyUI/comfy_execution/progress.py +348 -0
- ComfyUI/comfy_execution/utils.py +46 -0
- ComfyUI/comfy_execution/validation.py +58 -0
- ComfyUI/comfy_extras/__pycache__/color_util.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_ace.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_advanced_samplers.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_align_your_steps.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_apg.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_ar_video.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_attention_multiply.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_audio.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_audio_encoder.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_bernini.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_bg_removal.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_boogu.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_bounding_boxes.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_camera_trajectory.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_canny.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_cfg.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_chroma_radiance.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_clip_sdxl.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_color.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_compositing.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_cond.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_context_windows.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_controlnet.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_cosmos.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_curve.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_custom_sampler.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_dataset.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_depth_anything_3.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_differential_diffusion.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_easycache.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_edit_model.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_eps.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_flux.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_frame_interpolation.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_freelunch.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_fresca.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_gaussian_splat.cpython-312.pyc +3 -0
- ComfyUI/comfy_extras/__pycache__/nodes_gits.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_glsl.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_hidream.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_hidream_o1.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_hooks.cpython-312.pyc +0 -0
- ComfyUI/comfy_extras/__pycache__/nodes_hunyuan.cpython-312.pyc +0 -0
.gitattributes
CHANGED
|
@@ -45,3 +45,4 @@ ComfyUI/comfy_api/latest/__pycache__/_io.cpython-312.pyc filter=lfs diff=lfs mer
|
|
| 45 |
ComfyUI/comfy_api_nodes/__pycache__/nodes_bytedance.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 46 |
ComfyUI/comfy_api_nodes/__pycache__/nodes_kling.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 47 |
ComfyUI/comfy_api_nodes/apis/__pycache__/__init__.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 45 |
ComfyUI/comfy_api_nodes/__pycache__/nodes_bytedance.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 46 |
ComfyUI/comfy_api_nodes/__pycache__/nodes_kling.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 47 |
ComfyUI/comfy_api_nodes/apis/__pycache__/__init__.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
ComfyUI/comfy_extras/__pycache__/nodes_gaussian_splat.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
ComfyUI/comfy_execution/graph.py
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Type, Literal
|
| 2 |
+
|
| 3 |
+
import nodes
|
| 4 |
+
import asyncio
|
| 5 |
+
import inspect
|
| 6 |
+
from comfy_execution.graph_utils import is_link, ExecutionBlocker
|
| 7 |
+
from comfy.comfy_types.node_typing import ComfyNodeABC, InputTypeDict, InputTypeOptions
|
| 8 |
+
|
| 9 |
+
# NOTE: ExecutionBlocker code got moved to graph_utils.py to prevent torch being imported too soon during unit tests
|
| 10 |
+
ExecutionBlocker = ExecutionBlocker
|
| 11 |
+
|
| 12 |
+
class DependencyCycleError(Exception):
|
| 13 |
+
pass
|
| 14 |
+
|
| 15 |
+
class NodeInputError(Exception):
|
| 16 |
+
pass
|
| 17 |
+
|
| 18 |
+
class NodeNotFoundError(Exception):
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
class DynamicPrompt:
|
| 22 |
+
def __init__(self, original_prompt):
|
| 23 |
+
# The original prompt provided by the user
|
| 24 |
+
self.original_prompt = original_prompt
|
| 25 |
+
# Any extra pieces of the graph created during execution
|
| 26 |
+
self.ephemeral_prompt = {}
|
| 27 |
+
self.ephemeral_parents = {}
|
| 28 |
+
self.ephemeral_display = {}
|
| 29 |
+
|
| 30 |
+
def get_node(self, node_id):
|
| 31 |
+
if node_id in self.ephemeral_prompt:
|
| 32 |
+
return self.ephemeral_prompt[node_id]
|
| 33 |
+
if node_id in self.original_prompt:
|
| 34 |
+
return self.original_prompt[node_id]
|
| 35 |
+
raise NodeNotFoundError(f"Node {node_id} not found")
|
| 36 |
+
|
| 37 |
+
def has_node(self, node_id):
|
| 38 |
+
return node_id in self.original_prompt or node_id in self.ephemeral_prompt
|
| 39 |
+
|
| 40 |
+
def add_ephemeral_node(self, node_id, node_info, parent_id, display_id):
|
| 41 |
+
self.ephemeral_prompt[node_id] = node_info
|
| 42 |
+
self.ephemeral_parents[node_id] = parent_id
|
| 43 |
+
self.ephemeral_display[node_id] = display_id
|
| 44 |
+
|
| 45 |
+
def get_real_node_id(self, node_id):
|
| 46 |
+
while node_id in self.ephemeral_parents:
|
| 47 |
+
node_id = self.ephemeral_parents[node_id]
|
| 48 |
+
return node_id
|
| 49 |
+
|
| 50 |
+
def get_parent_node_id(self, node_id):
|
| 51 |
+
return self.ephemeral_parents.get(node_id, None)
|
| 52 |
+
|
| 53 |
+
def get_display_node_id(self, node_id):
|
| 54 |
+
while node_id in self.ephemeral_display:
|
| 55 |
+
node_id = self.ephemeral_display[node_id]
|
| 56 |
+
return node_id
|
| 57 |
+
|
| 58 |
+
def all_node_ids(self):
|
| 59 |
+
return set(self.original_prompt.keys()).union(set(self.ephemeral_prompt.keys()))
|
| 60 |
+
|
| 61 |
+
def get_original_prompt(self):
|
| 62 |
+
return self.original_prompt
|
| 63 |
+
|
| 64 |
+
def get_input_info(
|
| 65 |
+
class_def: Type[ComfyNodeABC],
|
| 66 |
+
input_name: str,
|
| 67 |
+
valid_inputs: InputTypeDict | None = None
|
| 68 |
+
) -> tuple[str, Literal["required", "optional", "hidden"], InputTypeOptions] | tuple[None, None, None]:
|
| 69 |
+
"""Get the input type, category, and extra info for a given input name.
|
| 70 |
+
|
| 71 |
+
Arguments:
|
| 72 |
+
class_def: The class definition of the node.
|
| 73 |
+
input_name: The name of the input to get info for.
|
| 74 |
+
valid_inputs: The valid inputs for the node, or None to use the class_def.INPUT_TYPES().
|
| 75 |
+
|
| 76 |
+
Returns:
|
| 77 |
+
tuple[str, str, dict] | tuple[None, None, None]: The input type, category, and extra info for the input name.
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
valid_inputs = valid_inputs or class_def.INPUT_TYPES()
|
| 81 |
+
input_info = None
|
| 82 |
+
input_category = None
|
| 83 |
+
if "required" in valid_inputs and input_name in valid_inputs["required"]:
|
| 84 |
+
input_category = "required"
|
| 85 |
+
input_info = valid_inputs["required"][input_name]
|
| 86 |
+
elif "optional" in valid_inputs and input_name in valid_inputs["optional"]:
|
| 87 |
+
input_category = "optional"
|
| 88 |
+
input_info = valid_inputs["optional"][input_name]
|
| 89 |
+
elif "hidden" in valid_inputs and input_name in valid_inputs["hidden"]:
|
| 90 |
+
input_category = "hidden"
|
| 91 |
+
input_info = valid_inputs["hidden"][input_name]
|
| 92 |
+
if input_info is None:
|
| 93 |
+
return None, None, None
|
| 94 |
+
input_type = input_info[0]
|
| 95 |
+
if len(input_info) > 1:
|
| 96 |
+
extra_info = input_info[1]
|
| 97 |
+
else:
|
| 98 |
+
extra_info = {}
|
| 99 |
+
# if input_type is a list, it is a Combo defined in outdated format; convert it.
|
| 100 |
+
# NOTE: uncomment this when we are confident old format going away won't cause too much trouble.
|
| 101 |
+
# if isinstance(input_type, list):
|
| 102 |
+
# extra_info["options"] = input_type
|
| 103 |
+
# input_type = IO.Combo.io_type
|
| 104 |
+
return input_type, input_category, extra_info
|
| 105 |
+
|
| 106 |
+
class TopologicalSort:
|
| 107 |
+
def __init__(self, dynprompt):
|
| 108 |
+
self.dynprompt = dynprompt
|
| 109 |
+
self.pendingNodes = {}
|
| 110 |
+
self.blockCount = {} # Number of nodes this node is directly blocked by
|
| 111 |
+
self.blocking = {} # Which nodes are blocked by this node
|
| 112 |
+
self.externalBlocks = 0
|
| 113 |
+
self.unblockedEvent = asyncio.Event()
|
| 114 |
+
|
| 115 |
+
def get_input_info(self, unique_id, input_name):
|
| 116 |
+
class_type = self.dynprompt.get_node(unique_id)["class_type"]
|
| 117 |
+
class_def = nodes.NODE_CLASS_MAPPINGS[class_type]
|
| 118 |
+
return get_input_info(class_def, input_name)
|
| 119 |
+
|
| 120 |
+
def make_input_strong_link(self, to_node_id, to_input):
|
| 121 |
+
inputs = self.dynprompt.get_node(to_node_id)["inputs"]
|
| 122 |
+
if to_input not in inputs:
|
| 123 |
+
raise NodeInputError(f"Node {to_node_id} says it needs input {to_input}, but there is no input to that node at all")
|
| 124 |
+
value = inputs[to_input]
|
| 125 |
+
if not is_link(value):
|
| 126 |
+
raise NodeInputError(f"Node {to_node_id} says it needs input {to_input}, but that value is a constant")
|
| 127 |
+
from_node_id, from_socket = value
|
| 128 |
+
self.add_strong_link(from_node_id, from_socket, to_node_id)
|
| 129 |
+
|
| 130 |
+
def add_strong_link(self, from_node_id, from_socket, to_node_id):
|
| 131 |
+
if not self.is_cached(from_node_id):
|
| 132 |
+
self.add_node(from_node_id)
|
| 133 |
+
if to_node_id not in self.blocking[from_node_id]:
|
| 134 |
+
self.blocking[from_node_id][to_node_id] = {}
|
| 135 |
+
self.blockCount[to_node_id] += 1
|
| 136 |
+
self.blocking[from_node_id][to_node_id][from_socket] = True
|
| 137 |
+
|
| 138 |
+
def add_node(self, node_unique_id, include_lazy=False, subgraph_nodes=None):
|
| 139 |
+
node_ids = [node_unique_id]
|
| 140 |
+
links = []
|
| 141 |
+
|
| 142 |
+
while len(node_ids) > 0:
|
| 143 |
+
unique_id = node_ids.pop()
|
| 144 |
+
if unique_id in self.pendingNodes:
|
| 145 |
+
continue
|
| 146 |
+
|
| 147 |
+
self.pendingNodes[unique_id] = True
|
| 148 |
+
self.blockCount[unique_id] = 0
|
| 149 |
+
self.blocking[unique_id] = {}
|
| 150 |
+
|
| 151 |
+
inputs = self.dynprompt.get_node(unique_id)["inputs"]
|
| 152 |
+
for input_name in inputs:
|
| 153 |
+
value = inputs[input_name]
|
| 154 |
+
if is_link(value):
|
| 155 |
+
from_node_id, from_socket = value
|
| 156 |
+
if subgraph_nodes is not None and from_node_id not in subgraph_nodes:
|
| 157 |
+
continue
|
| 158 |
+
_, _, input_info = self.get_input_info(unique_id, input_name)
|
| 159 |
+
is_lazy = input_info is not None and "lazy" in input_info and input_info["lazy"]
|
| 160 |
+
if (include_lazy or not is_lazy):
|
| 161 |
+
if not self.is_cached(from_node_id):
|
| 162 |
+
node_ids.append(from_node_id)
|
| 163 |
+
links.append((from_node_id, from_socket, unique_id))
|
| 164 |
+
|
| 165 |
+
for link in links:
|
| 166 |
+
self.add_strong_link(*link)
|
| 167 |
+
|
| 168 |
+
def add_external_block(self, node_id):
|
| 169 |
+
assert node_id in self.blockCount, "Can't add external block to a node that isn't pending"
|
| 170 |
+
self.externalBlocks += 1
|
| 171 |
+
self.blockCount[node_id] += 1
|
| 172 |
+
def unblock():
|
| 173 |
+
self.externalBlocks -= 1
|
| 174 |
+
self.blockCount[node_id] -= 1
|
| 175 |
+
self.unblockedEvent.set()
|
| 176 |
+
return unblock
|
| 177 |
+
|
| 178 |
+
def is_cached(self, node_id):
|
| 179 |
+
return False
|
| 180 |
+
|
| 181 |
+
def get_ready_nodes(self):
|
| 182 |
+
return [node_id for node_id in self.pendingNodes if self.blockCount[node_id] == 0]
|
| 183 |
+
|
| 184 |
+
def pop_node(self, unique_id):
|
| 185 |
+
del self.pendingNodes[unique_id]
|
| 186 |
+
for blocked_node_id in self.blocking[unique_id]:
|
| 187 |
+
self.blockCount[blocked_node_id] -= 1
|
| 188 |
+
del self.blocking[unique_id]
|
| 189 |
+
|
| 190 |
+
def is_empty(self):
|
| 191 |
+
return len(self.pendingNodes) == 0
|
| 192 |
+
|
| 193 |
+
class ExecutionList(TopologicalSort):
|
| 194 |
+
"""
|
| 195 |
+
ExecutionList implements a topological dissolve of the graph. After a node is staged for execution,
|
| 196 |
+
it can still be returned to the graph after having further dependencies added.
|
| 197 |
+
"""
|
| 198 |
+
def __init__(self, dynprompt, output_cache, output_link_callback=None):
|
| 199 |
+
super().__init__(dynprompt)
|
| 200 |
+
self.output_cache = output_cache
|
| 201 |
+
self.output_link_callback = output_link_callback
|
| 202 |
+
self.staged_node_id = None
|
| 203 |
+
self.execution_cache = {}
|
| 204 |
+
self.execution_cache_listeners = {}
|
| 205 |
+
|
| 206 |
+
def is_cached(self, node_id):
|
| 207 |
+
return self.output_cache.get_local(node_id) is not None
|
| 208 |
+
|
| 209 |
+
def cache_link(self, from_node_id, to_node_id, from_socket=None):
|
| 210 |
+
if to_node_id not in self.execution_cache:
|
| 211 |
+
self.execution_cache[to_node_id] = {}
|
| 212 |
+
value = self.output_cache.get_local(from_node_id)
|
| 213 |
+
self.execution_cache[to_node_id][from_node_id] = value
|
| 214 |
+
if from_node_id not in self.execution_cache_listeners:
|
| 215 |
+
self.execution_cache_listeners[from_node_id] = set()
|
| 216 |
+
self.execution_cache_listeners[from_node_id].add((to_node_id, from_socket))
|
| 217 |
+
if value is not None and from_socket is not None and self.output_link_callback is not None:
|
| 218 |
+
self.output_link_callback(value.outputs[from_socket])
|
| 219 |
+
|
| 220 |
+
def get_cache(self, from_node_id, to_node_id):
|
| 221 |
+
if to_node_id not in self.execution_cache:
|
| 222 |
+
return None
|
| 223 |
+
value = self.execution_cache[to_node_id].get(from_node_id)
|
| 224 |
+
if value is None:
|
| 225 |
+
return None
|
| 226 |
+
#Write back to the main cache on touch.
|
| 227 |
+
self.output_cache.set_local(from_node_id, value)
|
| 228 |
+
return value
|
| 229 |
+
|
| 230 |
+
def cache_update(self, node_id, value):
|
| 231 |
+
if node_id in self.execution_cache_listeners:
|
| 232 |
+
for to_node_id, from_socket in self.execution_cache_listeners[node_id]:
|
| 233 |
+
if to_node_id in self.execution_cache:
|
| 234 |
+
self.execution_cache[to_node_id][node_id] = value
|
| 235 |
+
if from_socket is not None and self.output_link_callback is not None:
|
| 236 |
+
self.output_link_callback(value.outputs[from_socket])
|
| 237 |
+
|
| 238 |
+
def add_strong_link(self, from_node_id, from_socket, to_node_id):
|
| 239 |
+
super().add_strong_link(from_node_id, from_socket, to_node_id)
|
| 240 |
+
self.cache_link(from_node_id, to_node_id, from_socket)
|
| 241 |
+
|
| 242 |
+
async def stage_node_execution(self):
|
| 243 |
+
assert self.staged_node_id is None
|
| 244 |
+
if self.is_empty():
|
| 245 |
+
return None, None, None
|
| 246 |
+
available = self.get_ready_nodes()
|
| 247 |
+
while len(available) == 0 and self.externalBlocks > 0:
|
| 248 |
+
# Wait for an external block to be released
|
| 249 |
+
await self.unblockedEvent.wait()
|
| 250 |
+
self.unblockedEvent.clear()
|
| 251 |
+
available = self.get_ready_nodes()
|
| 252 |
+
if len(available) == 0:
|
| 253 |
+
cycled_nodes = self.get_nodes_in_cycle()
|
| 254 |
+
# Because cycles composed entirely of static nodes are caught during initial validation,
|
| 255 |
+
# we will 'blame' the first node in the cycle that is not a static node.
|
| 256 |
+
blamed_node = cycled_nodes[0]
|
| 257 |
+
for node_id in cycled_nodes:
|
| 258 |
+
display_node_id = self.dynprompt.get_display_node_id(node_id)
|
| 259 |
+
if display_node_id != node_id:
|
| 260 |
+
blamed_node = display_node_id
|
| 261 |
+
break
|
| 262 |
+
ex = DependencyCycleError("Dependency cycle detected")
|
| 263 |
+
error_details = {
|
| 264 |
+
"node_id": blamed_node,
|
| 265 |
+
"exception_message": str(ex),
|
| 266 |
+
"exception_type": "graph.DependencyCycleError",
|
| 267 |
+
"traceback": [],
|
| 268 |
+
"current_inputs": []
|
| 269 |
+
}
|
| 270 |
+
return None, error_details, ex
|
| 271 |
+
|
| 272 |
+
self.staged_node_id = self.ux_friendly_pick_node(available)
|
| 273 |
+
return self.staged_node_id, None, None
|
| 274 |
+
|
| 275 |
+
def ux_friendly_pick_node(self, node_list):
|
| 276 |
+
# If an output node is available, do that first.
|
| 277 |
+
# Technically this has no effect on the overall length of execution, but it feels better as a user
|
| 278 |
+
# for a PreviewImage to display a result as soon as it can
|
| 279 |
+
# Some other heuristics could probably be used here to improve the UX further.
|
| 280 |
+
def is_output(node_id):
|
| 281 |
+
class_type = self.dynprompt.get_node(node_id)["class_type"]
|
| 282 |
+
class_def = nodes.NODE_CLASS_MAPPINGS[class_type]
|
| 283 |
+
if hasattr(class_def, 'OUTPUT_NODE') and class_def.OUTPUT_NODE == True:
|
| 284 |
+
return True
|
| 285 |
+
return False
|
| 286 |
+
|
| 287 |
+
# If an available node is async, do that first.
|
| 288 |
+
# This will execute the asynchronous function earlier, reducing the overall time.
|
| 289 |
+
def is_async(node_id):
|
| 290 |
+
class_type = self.dynprompt.get_node(node_id)["class_type"]
|
| 291 |
+
class_def = nodes.NODE_CLASS_MAPPINGS[class_type]
|
| 292 |
+
return inspect.iscoroutinefunction(getattr(class_def, class_def.FUNCTION))
|
| 293 |
+
|
| 294 |
+
for node_id in node_list:
|
| 295 |
+
if is_output(node_id) or is_async(node_id):
|
| 296 |
+
return node_id
|
| 297 |
+
|
| 298 |
+
#This should handle the VAEDecode -> preview case
|
| 299 |
+
for node_id in node_list:
|
| 300 |
+
for blocked_node_id in self.blocking[node_id]:
|
| 301 |
+
if is_output(blocked_node_id):
|
| 302 |
+
return node_id
|
| 303 |
+
|
| 304 |
+
#This should handle the VAELoader -> VAEDecode -> preview case
|
| 305 |
+
for node_id in node_list:
|
| 306 |
+
for blocked_node_id in self.blocking[node_id]:
|
| 307 |
+
for blocked_node_id1 in self.blocking[blocked_node_id]:
|
| 308 |
+
if is_output(blocked_node_id1):
|
| 309 |
+
return node_id
|
| 310 |
+
|
| 311 |
+
#TODO: this function should be improved
|
| 312 |
+
return node_list[0]
|
| 313 |
+
|
| 314 |
+
def unstage_node_execution(self):
|
| 315 |
+
assert self.staged_node_id is not None
|
| 316 |
+
self.staged_node_id = None
|
| 317 |
+
|
| 318 |
+
def complete_node_execution(self):
|
| 319 |
+
node_id = self.staged_node_id
|
| 320 |
+
self.pop_node(node_id)
|
| 321 |
+
self.execution_cache.pop(node_id, None)
|
| 322 |
+
self.execution_cache_listeners.pop(node_id, None)
|
| 323 |
+
self.staged_node_id = None
|
| 324 |
+
|
| 325 |
+
def get_nodes_in_cycle(self):
|
| 326 |
+
# We'll dissolve the graph in reverse topological order to leave only the nodes in the cycle.
|
| 327 |
+
# We're skipping some of the performance optimizations from the original TopologicalSort to keep
|
| 328 |
+
# the code simple (and because having a cycle in the first place is a catastrophic error)
|
| 329 |
+
blocked_by = { node_id: {} for node_id in self.pendingNodes }
|
| 330 |
+
for from_node_id in self.blocking:
|
| 331 |
+
for to_node_id in self.blocking[from_node_id]:
|
| 332 |
+
if True in self.blocking[from_node_id][to_node_id].values():
|
| 333 |
+
blocked_by[to_node_id][from_node_id] = True
|
| 334 |
+
to_remove = [node_id for node_id in blocked_by if len(blocked_by[node_id]) == 0]
|
| 335 |
+
while len(to_remove) > 0:
|
| 336 |
+
for node_id in to_remove:
|
| 337 |
+
for to_node_id in blocked_by:
|
| 338 |
+
if node_id in blocked_by[to_node_id]:
|
| 339 |
+
del blocked_by[to_node_id][node_id]
|
| 340 |
+
del blocked_by[node_id]
|
| 341 |
+
to_remove = [node_id for node_id in blocked_by if len(blocked_by[node_id]) == 0]
|
| 342 |
+
return list(blocked_by.keys())
|
ComfyUI/comfy_execution/graph_utils.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|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isinstance(obj[0], str):
|
| 7 |
+
return False
|
| 8 |
+
if not isinstance(obj[1], int) and not isinstance(obj[1], float):
|
| 9 |
+
return False
|
| 10 |
+
return True
|
| 11 |
+
|
| 12 |
+
# The GraphBuilder is just a utility class that outputs graphs in the form expected by the ComfyUI back-end
|
| 13 |
+
class GraphBuilder:
|
| 14 |
+
_default_prefix_root = ""
|
| 15 |
+
_default_prefix_call_index = 0
|
| 16 |
+
_default_prefix_graph_index = 0
|
| 17 |
+
|
| 18 |
+
def __init__(self, prefix = None):
|
| 19 |
+
if prefix is None:
|
| 20 |
+
self.prefix = GraphBuilder.alloc_prefix()
|
| 21 |
+
else:
|
| 22 |
+
self.prefix = prefix
|
| 23 |
+
self.nodes = {}
|
| 24 |
+
self.id_gen = 1
|
| 25 |
+
|
| 26 |
+
@classmethod
|
| 27 |
+
def set_default_prefix(cls, prefix_root, call_index, graph_index = 0):
|
| 28 |
+
cls._default_prefix_root = prefix_root
|
| 29 |
+
cls._default_prefix_call_index = call_index
|
| 30 |
+
cls._default_prefix_graph_index = graph_index
|
| 31 |
+
|
| 32 |
+
@classmethod
|
| 33 |
+
def alloc_prefix(cls, root=None, call_index=None, graph_index=None):
|
| 34 |
+
if root is None:
|
| 35 |
+
root = GraphBuilder._default_prefix_root
|
| 36 |
+
if call_index is None:
|
| 37 |
+
call_index = GraphBuilder._default_prefix_call_index
|
| 38 |
+
if graph_index is None:
|
| 39 |
+
graph_index = GraphBuilder._default_prefix_graph_index
|
| 40 |
+
result = f"{root}.{call_index}.{graph_index}."
|
| 41 |
+
GraphBuilder._default_prefix_graph_index += 1
|
| 42 |
+
return result
|
| 43 |
+
|
| 44 |
+
def node(self, class_type, id=None, **kwargs):
|
| 45 |
+
if id is None:
|
| 46 |
+
id = str(self.id_gen)
|
| 47 |
+
self.id_gen += 1
|
| 48 |
+
id = self.prefix + id
|
| 49 |
+
if id in self.nodes:
|
| 50 |
+
return self.nodes[id]
|
| 51 |
+
|
| 52 |
+
node = Node(id, class_type, kwargs)
|
| 53 |
+
self.nodes[id] = node
|
| 54 |
+
return node
|
| 55 |
+
|
| 56 |
+
def lookup_node(self, id):
|
| 57 |
+
id = self.prefix + id
|
| 58 |
+
return self.nodes.get(id)
|
| 59 |
+
|
| 60 |
+
def finalize(self):
|
| 61 |
+
output = {}
|
| 62 |
+
for node_id, node in self.nodes.items():
|
| 63 |
+
output[node_id] = node.serialize()
|
| 64 |
+
return output
|
| 65 |
+
|
| 66 |
+
def replace_node_output(self, node_id, index, new_value):
|
| 67 |
+
node_id = self.prefix + node_id
|
| 68 |
+
to_remove = []
|
| 69 |
+
for node in self.nodes.values():
|
| 70 |
+
for key, value in node.inputs.items():
|
| 71 |
+
if is_link(value) and value[0] == node_id and value[1] == index:
|
| 72 |
+
if new_value is None:
|
| 73 |
+
to_remove.append((node, key))
|
| 74 |
+
else:
|
| 75 |
+
node.inputs[key] = new_value
|
| 76 |
+
for node, key in to_remove:
|
| 77 |
+
del node.inputs[key]
|
| 78 |
+
|
| 79 |
+
def remove_node(self, id):
|
| 80 |
+
id = self.prefix + id
|
| 81 |
+
del self.nodes[id]
|
| 82 |
+
|
| 83 |
+
class Node:
|
| 84 |
+
def __init__(self, id, class_type, inputs):
|
| 85 |
+
self.id = id
|
| 86 |
+
self.class_type = class_type
|
| 87 |
+
self.inputs = inputs
|
| 88 |
+
self.override_display_id = None
|
| 89 |
+
|
| 90 |
+
def out(self, index):
|
| 91 |
+
return [self.id, index]
|
| 92 |
+
|
| 93 |
+
def set_input(self, key, value):
|
| 94 |
+
if value is None:
|
| 95 |
+
if key in self.inputs:
|
| 96 |
+
del self.inputs[key]
|
| 97 |
+
else:
|
| 98 |
+
self.inputs[key] = value
|
| 99 |
+
|
| 100 |
+
def get_input(self, key):
|
| 101 |
+
return self.inputs.get(key)
|
| 102 |
+
|
| 103 |
+
def set_override_display_id(self, override_display_id):
|
| 104 |
+
self.override_display_id = override_display_id
|
| 105 |
+
|
| 106 |
+
def serialize(self):
|
| 107 |
+
serialized = {
|
| 108 |
+
"class_type": self.class_type,
|
| 109 |
+
"inputs": self.inputs
|
| 110 |
+
}
|
| 111 |
+
if self.override_display_id is not None:
|
| 112 |
+
serialized["override_display_id"] = self.override_display_id
|
| 113 |
+
return serialized
|
| 114 |
+
|
| 115 |
+
def add_graph_prefix(graph, outputs, prefix):
|
| 116 |
+
# Change the node IDs and any internal links
|
| 117 |
+
new_graph = {}
|
| 118 |
+
for node_id, node_info in graph.items():
|
| 119 |
+
# Make sure the added nodes have unique IDs
|
| 120 |
+
new_node_id = prefix + node_id
|
| 121 |
+
new_node = { "class_type": node_info["class_type"], "inputs": {} }
|
| 122 |
+
for input_name, input_value in node_info.get("inputs", {}).items():
|
| 123 |
+
if is_link(input_value):
|
| 124 |
+
new_node["inputs"][input_name] = [prefix + input_value[0], input_value[1]]
|
| 125 |
+
else:
|
| 126 |
+
new_node["inputs"][input_name] = input_value
|
| 127 |
+
new_graph[new_node_id] = new_node
|
| 128 |
+
|
| 129 |
+
# Change the node IDs in the outputs
|
| 130 |
+
new_outputs = []
|
| 131 |
+
for n in range(len(outputs)):
|
| 132 |
+
output = outputs[n]
|
| 133 |
+
if is_link(output):
|
| 134 |
+
new_outputs.append([prefix + output[0], output[1]])
|
| 135 |
+
else:
|
| 136 |
+
new_outputs.append(output)
|
| 137 |
+
|
| 138 |
+
return new_graph, tuple(new_outputs)
|
| 139 |
+
|
| 140 |
+
class ExecutionBlocker:
|
| 141 |
+
"""
|
| 142 |
+
Return this from a node and any users will be blocked with the given error message.
|
| 143 |
+
If the message is None, execution will be blocked silently instead.
|
| 144 |
+
Generally, you should avoid using this functionality unless absolutely necessary. Whenever it's
|
| 145 |
+
possible, a lazy input will be more efficient and have a better user experience.
|
| 146 |
+
This functionality is useful in two cases:
|
| 147 |
+
1. You want to conditionally prevent an output node from executing. (Particularly a built-in node
|
| 148 |
+
like SaveImage. For your own output nodes, I would recommend just adding a BOOL input and using
|
| 149 |
+
lazy evaluation to let it conditionally disable itself.)
|
| 150 |
+
2. You have a node with multiple possible outputs, some of which are invalid and should not be used.
|
| 151 |
+
(I would recommend not making nodes like this in the future -- instead, make multiple nodes with
|
| 152 |
+
different outputs. Unfortunately, there are several popular existing nodes using this pattern.)
|
| 153 |
+
"""
|
| 154 |
+
def __init__(self, message):
|
| 155 |
+
self.message = message
|
ComfyUI/comfy_execution/jobs.py
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Job utilities for the /api/jobs endpoint.
|
| 3 |
+
Provides normalization and helper functions for job status tracking.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import uuid
|
| 7 |
+
from typing import Callable, Optional
|
| 8 |
+
|
| 9 |
+
from comfy_api.internal import prune_dict
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Result of classifying a job for cancellation.
|
| 13 |
+
# 'running' -> job is currently executing (interrupt it)
|
| 14 |
+
# 'pending' -> job is queued but not started (dequeue it)
|
| 15 |
+
# 'terminal' -> job already finished (present in history); cancel is a no-op
|
| 16 |
+
# 'unknown' -> job id is not present anywhere
|
| 17 |
+
CANCEL_RUNNING = 'running'
|
| 18 |
+
CANCEL_PENDING = 'pending'
|
| 19 |
+
CANCEL_TERMINAL = 'terminal'
|
| 20 |
+
CANCEL_UNKNOWN = 'unknown'
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class JobStatus:
|
| 24 |
+
"""Job status constants."""
|
| 25 |
+
PENDING = 'pending'
|
| 26 |
+
IN_PROGRESS = 'in_progress'
|
| 27 |
+
COMPLETED = 'completed'
|
| 28 |
+
FAILED = 'failed'
|
| 29 |
+
CANCELLED = 'cancelled'
|
| 30 |
+
|
| 31 |
+
ALL = [PENDING, IN_PROGRESS, COMPLETED, FAILED, CANCELLED]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def validate_job_id(value) -> str:
|
| 35 |
+
"""Validate a client-supplied job (prompt) id.
|
| 36 |
+
|
| 37 |
+
Job ids must be UUIDs in the canonical lowercase hyphenated form. The id
|
| 38 |
+
is stored and compared verbatim everywhere downstream — history keys,
|
| 39 |
+
websocket events, and /interrupt matching — so accepting another spelling
|
| 40 |
+
would silently rewrite the client's id and then miss every exact-match
|
| 41 |
+
lookup. Rejecting loudly beats that.
|
| 42 |
+
|
| 43 |
+
Returns the id unchanged. Raises ValueError when the value is not a
|
| 44 |
+
string in canonical UUID form.
|
| 45 |
+
"""
|
| 46 |
+
if not isinstance(value, str):
|
| 47 |
+
raise ValueError(f"job id must be a string, got {type(value).__name__}")
|
| 48 |
+
if str(uuid.UUID(value)) != value:
|
| 49 |
+
raise ValueError("job id must be a UUID in canonical lowercase hyphenated form")
|
| 50 |
+
return value
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Media types that can be previewed in the frontend
|
| 54 |
+
PREVIEWABLE_MEDIA_TYPES = frozenset({'images', 'video', 'audio', '3d', 'text'})
|
| 55 |
+
|
| 56 |
+
# 3D file extensions for preview fallback (no dedicated media_type exists)
|
| 57 |
+
THREE_D_EXTENSIONS = frozenset({'.obj', '.fbx', '.gltf', '.glb', '.usdz'})
|
| 58 |
+
|
| 59 |
+
# Text file extensions for preview fallback (the formats SaveText can produce)
|
| 60 |
+
TEXT_EXTENSIONS = frozenset({'.txt', '.md', '.json'})
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def has_3d_extension(filename: str) -> bool:
|
| 64 |
+
lower = filename.lower()
|
| 65 |
+
return any(lower.endswith(ext) for ext in THREE_D_EXTENSIONS)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def normalize_output_item(item):
|
| 69 |
+
"""Normalize a single output list item for the jobs API.
|
| 70 |
+
|
| 71 |
+
Returns the normalized item, or None to exclude it.
|
| 72 |
+
String items with 3D extensions become {filename, type, subfolder} dicts.
|
| 73 |
+
"""
|
| 74 |
+
if item is None:
|
| 75 |
+
return None
|
| 76 |
+
if isinstance(item, str):
|
| 77 |
+
if has_3d_extension(item):
|
| 78 |
+
return {'filename': item, 'type': 'output', 'subfolder': '', 'mediaType': '3d'}
|
| 79 |
+
return None
|
| 80 |
+
if isinstance(item, dict):
|
| 81 |
+
return item
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def normalize_outputs(outputs: dict) -> dict:
|
| 86 |
+
"""Normalize raw node outputs for the jobs API.
|
| 87 |
+
|
| 88 |
+
Transforms string 3D filenames into file output dicts and removes
|
| 89 |
+
None items. All other items (non-3D strings, dicts, etc.) are
|
| 90 |
+
preserved as-is.
|
| 91 |
+
"""
|
| 92 |
+
normalized = {}
|
| 93 |
+
for node_id, node_outputs in outputs.items():
|
| 94 |
+
if not isinstance(node_outputs, dict):
|
| 95 |
+
normalized[node_id] = node_outputs
|
| 96 |
+
continue
|
| 97 |
+
normalized_node = {}
|
| 98 |
+
for media_type, items in node_outputs.items():
|
| 99 |
+
if media_type == 'animated' or not isinstance(items, list):
|
| 100 |
+
normalized_node[media_type] = items
|
| 101 |
+
continue
|
| 102 |
+
normalized_items = []
|
| 103 |
+
for item in items:
|
| 104 |
+
if item is None:
|
| 105 |
+
continue
|
| 106 |
+
norm = normalize_output_item(item)
|
| 107 |
+
normalized_items.append(norm if norm is not None else item)
|
| 108 |
+
normalized_node[media_type] = normalized_items
|
| 109 |
+
normalized[node_id] = normalized_node
|
| 110 |
+
return normalized
|
| 111 |
+
|
| 112 |
+
# Text preview truncation limit (1024 characters) to prevent preview_output bloat
|
| 113 |
+
TEXT_PREVIEW_MAX_LENGTH = 1024
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def _create_text_preview(value: str) -> dict:
|
| 117 |
+
"""Create a text preview dict with optional truncation.
|
| 118 |
+
|
| 119 |
+
Returns:
|
| 120 |
+
dict with 'content' and optionally 'truncated' flag
|
| 121 |
+
"""
|
| 122 |
+
if len(value) <= TEXT_PREVIEW_MAX_LENGTH:
|
| 123 |
+
return {'content': value}
|
| 124 |
+
return {
|
| 125 |
+
'content': value[:TEXT_PREVIEW_MAX_LENGTH],
|
| 126 |
+
'truncated': True
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def _extract_job_metadata(extra_data: dict) -> tuple[Optional[int], Optional[str]]:
|
| 131 |
+
"""Extract create_time and workflow_id from extra_data.
|
| 132 |
+
|
| 133 |
+
Returns:
|
| 134 |
+
tuple: (create_time, workflow_id)
|
| 135 |
+
"""
|
| 136 |
+
create_time = extra_data.get('create_time')
|
| 137 |
+
extra_pnginfo = extra_data.get('extra_pnginfo', {})
|
| 138 |
+
workflow_id = extra_pnginfo.get('workflow', {}).get('id')
|
| 139 |
+
return create_time, workflow_id
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def is_previewable(media_type: str, item: dict) -> bool:
|
| 143 |
+
"""
|
| 144 |
+
Check if an output item is previewable.
|
| 145 |
+
Matches frontend logic in ComfyUI_frontend/src/stores/queueStore.ts
|
| 146 |
+
Maintains backwards compatibility with existing logic.
|
| 147 |
+
|
| 148 |
+
Priority:
|
| 149 |
+
1. media_type is 'images', 'video', 'audio', '3d', or 'text'
|
| 150 |
+
2. format field starts with 'video/' or 'audio/'
|
| 151 |
+
3. filename has a 3D extension (.obj, .fbx, .gltf, .glb, .usdz)
|
| 152 |
+
4. filename has a text extension (.txt, .md, .json, ...)
|
| 153 |
+
"""
|
| 154 |
+
if media_type in PREVIEWABLE_MEDIA_TYPES:
|
| 155 |
+
return True
|
| 156 |
+
|
| 157 |
+
# Check format field (MIME type).
|
| 158 |
+
# Maintains backwards compatibility with how custom node outputs are handled in the frontend.
|
| 159 |
+
fmt = item.get('format', '')
|
| 160 |
+
if fmt and (fmt.startswith('video/') or fmt.startswith('audio/')):
|
| 161 |
+
return True
|
| 162 |
+
|
| 163 |
+
# Check for 3D and text files by extension
|
| 164 |
+
filename = item.get('filename', '').lower()
|
| 165 |
+
if any(filename.endswith(ext) for ext in THREE_D_EXTENSIONS):
|
| 166 |
+
return True
|
| 167 |
+
if any(filename.endswith(ext) for ext in TEXT_EXTENSIONS):
|
| 168 |
+
return True
|
| 169 |
+
|
| 170 |
+
return False
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def is_text_preview(media_type: str, item: dict) -> bool:
|
| 174 |
+
"""
|
| 175 |
+
Check if a previewable output item is textual rather than visual media.
|
| 176 |
+
|
| 177 |
+
Saved text files (SaveText's .txt/.md/.json) are real outputs but must not
|
| 178 |
+
outrank visual media when picking the job preview.
|
| 179 |
+
"""
|
| 180 |
+
if media_type == 'text':
|
| 181 |
+
return True
|
| 182 |
+
filename = item.get('filename', '').lower()
|
| 183 |
+
return any(filename.endswith(ext) for ext in TEXT_EXTENSIONS)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def normalize_queue_item(item: tuple, status: str) -> dict:
|
| 187 |
+
"""Convert queue item tuple to unified job dict.
|
| 188 |
+
|
| 189 |
+
Expects item with sensitive data already removed (5 elements).
|
| 190 |
+
"""
|
| 191 |
+
priority, prompt_id, _, extra_data, _ = item
|
| 192 |
+
create_time, workflow_id = _extract_job_metadata(extra_data)
|
| 193 |
+
|
| 194 |
+
return prune_dict({
|
| 195 |
+
'id': prompt_id,
|
| 196 |
+
'status': status,
|
| 197 |
+
'priority': priority,
|
| 198 |
+
'create_time': create_time,
|
| 199 |
+
'outputs_count': 0,
|
| 200 |
+
'workflow_id': workflow_id,
|
| 201 |
+
})
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def normalize_history_item(prompt_id: str, history_item: dict, include_outputs: bool = False) -> dict:
|
| 205 |
+
"""Convert history item dict to unified job dict.
|
| 206 |
+
|
| 207 |
+
History items have sensitive data already removed (prompt tuple has 5 elements).
|
| 208 |
+
"""
|
| 209 |
+
prompt_tuple = history_item['prompt']
|
| 210 |
+
priority, _, prompt, extra_data, _ = prompt_tuple
|
| 211 |
+
create_time, workflow_id = _extract_job_metadata(extra_data)
|
| 212 |
+
|
| 213 |
+
status_info = history_item.get('status', {})
|
| 214 |
+
status_str = status_info.get('status_str') if status_info else None
|
| 215 |
+
|
| 216 |
+
outputs = history_item.get('outputs', {})
|
| 217 |
+
outputs_count, preview_output = get_outputs_summary(outputs)
|
| 218 |
+
|
| 219 |
+
execution_error = None
|
| 220 |
+
execution_start_time = None
|
| 221 |
+
execution_end_time = None
|
| 222 |
+
was_interrupted = False
|
| 223 |
+
if status_info:
|
| 224 |
+
messages = status_info.get('messages', [])
|
| 225 |
+
for entry in messages:
|
| 226 |
+
if isinstance(entry, (list, tuple)) and len(entry) >= 2:
|
| 227 |
+
event_name, event_data = entry[0], entry[1]
|
| 228 |
+
if isinstance(event_data, dict):
|
| 229 |
+
if event_name == 'execution_start':
|
| 230 |
+
execution_start_time = event_data.get('timestamp')
|
| 231 |
+
elif event_name in ('execution_success', 'execution_error', 'execution_interrupted'):
|
| 232 |
+
execution_end_time = event_data.get('timestamp')
|
| 233 |
+
if event_name == 'execution_error':
|
| 234 |
+
execution_error = event_data
|
| 235 |
+
elif event_name == 'execution_interrupted':
|
| 236 |
+
was_interrupted = True
|
| 237 |
+
|
| 238 |
+
if status_str == 'success':
|
| 239 |
+
status = JobStatus.COMPLETED
|
| 240 |
+
elif status_str == 'error':
|
| 241 |
+
status = JobStatus.CANCELLED if was_interrupted else JobStatus.FAILED
|
| 242 |
+
else:
|
| 243 |
+
status = JobStatus.COMPLETED
|
| 244 |
+
|
| 245 |
+
job = prune_dict({
|
| 246 |
+
'id': prompt_id,
|
| 247 |
+
'status': status,
|
| 248 |
+
'priority': priority,
|
| 249 |
+
'create_time': create_time,
|
| 250 |
+
'execution_start_time': execution_start_time,
|
| 251 |
+
'execution_end_time': execution_end_time,
|
| 252 |
+
'execution_error': execution_error,
|
| 253 |
+
'outputs_count': outputs_count,
|
| 254 |
+
'preview_output': preview_output,
|
| 255 |
+
'workflow_id': workflow_id,
|
| 256 |
+
})
|
| 257 |
+
|
| 258 |
+
if include_outputs:
|
| 259 |
+
job['outputs'] = normalize_outputs(outputs)
|
| 260 |
+
job['execution_status'] = status_info
|
| 261 |
+
job['workflow'] = {
|
| 262 |
+
'prompt': prompt,
|
| 263 |
+
'extra_data': extra_data,
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
return job
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
def get_outputs_summary(outputs: dict) -> tuple[int, Optional[dict]]:
|
| 270 |
+
"""
|
| 271 |
+
Count outputs and find preview in a single pass.
|
| 272 |
+
Returns (outputs_count, preview_output).
|
| 273 |
+
|
| 274 |
+
Preview priority (matching frontend):
|
| 275 |
+
1. type="output" visual media (saved images/video/audio/3d)
|
| 276 |
+
2. any other previewable visual media (e.g. temp/preview images)
|
| 277 |
+
3. saved text file (e.g. SaveText's .txt/.md/.json)
|
| 278 |
+
4. raw text (only when the job produced nothing else previewable)
|
| 279 |
+
|
| 280 |
+
Text is kept in its own slots so node/execution order can't let a text
|
| 281 |
+
output mask a visual one (e.g. a text node that runs before an image).
|
| 282 |
+
|
| 283 |
+
Text content entries (strings under 'text') are preview-only metadata,
|
| 284 |
+
matching the frontend's METADATA_KEYS: they can serve as the fallback
|
| 285 |
+
preview but are not counted as outputs.
|
| 286 |
+
"""
|
| 287 |
+
count = 0
|
| 288 |
+
preview_output = None
|
| 289 |
+
fallback_preview = None
|
| 290 |
+
text_file_fallback = None
|
| 291 |
+
text_fallback = None
|
| 292 |
+
|
| 293 |
+
for node_id, node_outputs in outputs.items():
|
| 294 |
+
if not isinstance(node_outputs, dict):
|
| 295 |
+
continue
|
| 296 |
+
for media_type, items in node_outputs.items():
|
| 297 |
+
# 'animated' is a boolean flag, not actual output items
|
| 298 |
+
if media_type == 'animated' or not isinstance(items, list):
|
| 299 |
+
continue
|
| 300 |
+
|
| 301 |
+
for item in items:
|
| 302 |
+
if not isinstance(item, dict):
|
| 303 |
+
# Handle text outputs (non-dict items like strings or tuples)
|
| 304 |
+
normalized = normalize_output_item(item)
|
| 305 |
+
if normalized is None:
|
| 306 |
+
# Not a 3D file string — check for text preview
|
| 307 |
+
if media_type == 'text':
|
| 308 |
+
if preview_output is None:
|
| 309 |
+
if isinstance(item, tuple):
|
| 310 |
+
text_value = item[0] if item else ''
|
| 311 |
+
else:
|
| 312 |
+
text_value = str(item)
|
| 313 |
+
text_preview = _create_text_preview(text_value)
|
| 314 |
+
enriched = {
|
| 315 |
+
**text_preview,
|
| 316 |
+
'nodeId': node_id,
|
| 317 |
+
'mediaType': media_type
|
| 318 |
+
}
|
| 319 |
+
if text_fallback is None:
|
| 320 |
+
text_fallback = enriched
|
| 321 |
+
continue
|
| 322 |
+
# normalize_output_item returned a dict (e.g. 3D file)
|
| 323 |
+
item = normalized
|
| 324 |
+
|
| 325 |
+
count += 1
|
| 326 |
+
|
| 327 |
+
if preview_output is not None:
|
| 328 |
+
continue
|
| 329 |
+
|
| 330 |
+
if is_previewable(media_type, item):
|
| 331 |
+
enriched = {
|
| 332 |
+
**item,
|
| 333 |
+
'nodeId': node_id,
|
| 334 |
+
}
|
| 335 |
+
if 'mediaType' not in item:
|
| 336 |
+
enriched['mediaType'] = media_type
|
| 337 |
+
if is_text_preview(media_type, item):
|
| 338 |
+
if text_file_fallback is None:
|
| 339 |
+
text_file_fallback = enriched
|
| 340 |
+
elif item.get('type') == 'output':
|
| 341 |
+
preview_output = enriched
|
| 342 |
+
elif fallback_preview is None:
|
| 343 |
+
fallback_preview = enriched
|
| 344 |
+
|
| 345 |
+
return count, preview_output or fallback_preview or text_file_fallback or text_fallback
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
def apply_sorting(jobs: list[dict], sort_by: str, sort_order: str) -> list[dict]:
|
| 349 |
+
"""Sort jobs list by specified field and order."""
|
| 350 |
+
reverse = (sort_order == 'desc')
|
| 351 |
+
|
| 352 |
+
if sort_by == 'execution_duration':
|
| 353 |
+
def get_sort_key(job):
|
| 354 |
+
start = job.get('execution_start_time', 0)
|
| 355 |
+
end = job.get('execution_end_time', 0)
|
| 356 |
+
return end - start if end and start else 0
|
| 357 |
+
else:
|
| 358 |
+
def get_sort_key(job):
|
| 359 |
+
return job.get('create_time', 0)
|
| 360 |
+
|
| 361 |
+
return sorted(jobs, key=get_sort_key, reverse=reverse)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def get_job(prompt_id: str, running: list, queued: list, history: dict) -> Optional[dict]:
|
| 365 |
+
"""
|
| 366 |
+
Get a single job by prompt_id from history or queue.
|
| 367 |
+
|
| 368 |
+
Args:
|
| 369 |
+
prompt_id: The prompt ID to look up
|
| 370 |
+
running: List of currently running queue items
|
| 371 |
+
queued: List of pending queue items
|
| 372 |
+
history: Dict of history items keyed by prompt_id
|
| 373 |
+
|
| 374 |
+
Returns:
|
| 375 |
+
Job dict with full details, or None if not found
|
| 376 |
+
"""
|
| 377 |
+
if prompt_id in history:
|
| 378 |
+
return normalize_history_item(prompt_id, history[prompt_id], include_outputs=True)
|
| 379 |
+
|
| 380 |
+
for item in running:
|
| 381 |
+
if item[1] == prompt_id:
|
| 382 |
+
return normalize_queue_item(item, JobStatus.IN_PROGRESS)
|
| 383 |
+
|
| 384 |
+
for item in queued:
|
| 385 |
+
if item[1] == prompt_id:
|
| 386 |
+
return normalize_queue_item(item, JobStatus.PENDING)
|
| 387 |
+
|
| 388 |
+
return None
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def get_all_jobs(
|
| 392 |
+
running: list,
|
| 393 |
+
queued: list,
|
| 394 |
+
history: dict,
|
| 395 |
+
status_filter: Optional[list[str]] = None,
|
| 396 |
+
workflow_id: Optional[str] = None,
|
| 397 |
+
sort_by: str = "created_at",
|
| 398 |
+
sort_order: str = "desc",
|
| 399 |
+
limit: Optional[int] = None,
|
| 400 |
+
offset: int = 0
|
| 401 |
+
) -> tuple[list[dict], int]:
|
| 402 |
+
"""
|
| 403 |
+
Get all jobs (running, pending, completed) with filtering and sorting.
|
| 404 |
+
|
| 405 |
+
Args:
|
| 406 |
+
running: List of currently running queue items
|
| 407 |
+
queued: List of pending queue items
|
| 408 |
+
history: Dict of history items keyed by prompt_id
|
| 409 |
+
status_filter: List of statuses to include (from JobStatus.ALL)
|
| 410 |
+
workflow_id: Filter by workflow ID
|
| 411 |
+
sort_by: Field to sort by ('created_at', 'execution_duration')
|
| 412 |
+
sort_order: 'asc' or 'desc'
|
| 413 |
+
limit: Maximum number of items to return
|
| 414 |
+
offset: Number of items to skip
|
| 415 |
+
|
| 416 |
+
Returns:
|
| 417 |
+
tuple: (jobs_list, total_count)
|
| 418 |
+
"""
|
| 419 |
+
jobs = []
|
| 420 |
+
|
| 421 |
+
if status_filter is None:
|
| 422 |
+
status_filter = JobStatus.ALL
|
| 423 |
+
|
| 424 |
+
if JobStatus.IN_PROGRESS in status_filter:
|
| 425 |
+
for item in running:
|
| 426 |
+
jobs.append(normalize_queue_item(item, JobStatus.IN_PROGRESS))
|
| 427 |
+
|
| 428 |
+
if JobStatus.PENDING in status_filter:
|
| 429 |
+
for item in queued:
|
| 430 |
+
jobs.append(normalize_queue_item(item, JobStatus.PENDING))
|
| 431 |
+
|
| 432 |
+
history_statuses = {JobStatus.COMPLETED, JobStatus.FAILED, JobStatus.CANCELLED}
|
| 433 |
+
requested_history_statuses = history_statuses & set(status_filter)
|
| 434 |
+
if requested_history_statuses:
|
| 435 |
+
for prompt_id, history_item in history.items():
|
| 436 |
+
job = normalize_history_item(prompt_id, history_item)
|
| 437 |
+
if job.get('status') in requested_history_statuses:
|
| 438 |
+
jobs.append(job)
|
| 439 |
+
|
| 440 |
+
if workflow_id:
|
| 441 |
+
jobs = [j for j in jobs if j.get('workflow_id') == workflow_id]
|
| 442 |
+
|
| 443 |
+
jobs = apply_sorting(jobs, sort_by, sort_order)
|
| 444 |
+
|
| 445 |
+
total_count = len(jobs)
|
| 446 |
+
|
| 447 |
+
if offset > 0:
|
| 448 |
+
jobs = jobs[offset:]
|
| 449 |
+
if limit is not None:
|
| 450 |
+
jobs = jobs[:limit]
|
| 451 |
+
|
| 452 |
+
return (jobs, total_count)
|
| 453 |
+
|
| 454 |
+
|
| 455 |
+
def classify_job_for_cancel(prompt_id: str, running: list, queued: list, history: dict) -> str:
|
| 456 |
+
"""Classify a job id for cancellation.
|
| 457 |
+
|
| 458 |
+
Returns one of CANCEL_RUNNING, CANCEL_PENDING, CANCEL_TERMINAL, CANCEL_UNKNOWN.
|
| 459 |
+
|
| 460 |
+
Queue items are tuples whose second element (index 1) is the prompt_id.
|
| 461 |
+
History is a dict keyed by prompt_id, so a job present there has already
|
| 462 |
+
finished and cancelling it is a no-op.
|
| 463 |
+
"""
|
| 464 |
+
for item in running:
|
| 465 |
+
if item[1] == prompt_id:
|
| 466 |
+
return CANCEL_RUNNING
|
| 467 |
+
for item in queued:
|
| 468 |
+
if item[1] == prompt_id:
|
| 469 |
+
return CANCEL_PENDING
|
| 470 |
+
if prompt_id in history:
|
| 471 |
+
return CANCEL_TERMINAL
|
| 472 |
+
return CANCEL_UNKNOWN
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
def cancel_job(
|
| 476 |
+
prompt_id: str,
|
| 477 |
+
running: list,
|
| 478 |
+
queued: list,
|
| 479 |
+
history: dict,
|
| 480 |
+
interrupt: Callable[[str], bool],
|
| 481 |
+
dequeue: Callable[[str], bool],
|
| 482 |
+
) -> str:
|
| 483 |
+
"""Cancel a single job by id, regardless of state.
|
| 484 |
+
|
| 485 |
+
Maps the cancel onto the runtime's existing mechanics:
|
| 486 |
+
- a running job is interrupted via ``interrupt``
|
| 487 |
+
- a pending job is removed from the queue via ``dequeue``
|
| 488 |
+
- a job that already finished (terminal) is a no-op
|
| 489 |
+
- an unknown id is a no-op (callers that need fail-fast behaviour should
|
| 490 |
+
validate ids up front with ``classify_job_for_cancel``)
|
| 491 |
+
|
| 492 |
+
Both ``interrupt`` and ``dequeue`` take the prompt id and return whether
|
| 493 |
+
they acted on a job that was *actually* in that state, so the value returned
|
| 494 |
+
here reflects what truly happened rather than the (possibly stale)
|
| 495 |
+
classification. This matters around the narrow TOCTOU windows where a job
|
| 496 |
+
changes state between the caller's snapshot and the action:
|
| 497 |
+
|
| 498 |
+
- a job classified RUNNING may have finished before ``interrupt`` fires:
|
| 499 |
+
``interrupt`` returns False and this returns CANCEL_UNKNOWN (no-op).
|
| 500 |
+
- a job classified PENDING may have started executing before ``dequeue``
|
| 501 |
+
fires: ``dequeue`` returns False, ``interrupt`` then catches the now-
|
| 502 |
+
running job and this returns CANCEL_RUNNING. If it had simply finished
|
| 503 |
+
instead, both return False and this returns CANCEL_UNKNOWN.
|
| 504 |
+
|
| 505 |
+
``interrupt`` must be atomic — interrupt the job only if it is still the one
|
| 506 |
+
running — so a cancel can never land on an unrelated prompt that started in
|
| 507 |
+
the meantime (see ``execution.PromptQueue.interrupt_if_running``).
|
| 508 |
+
"""
|
| 509 |
+
classification = classify_job_for_cancel(prompt_id, running, queued, history)
|
| 510 |
+
if classification == CANCEL_RUNNING:
|
| 511 |
+
return CANCEL_RUNNING if interrupt(prompt_id) else CANCEL_UNKNOWN
|
| 512 |
+
if classification == CANCEL_PENDING:
|
| 513 |
+
if dequeue(prompt_id):
|
| 514 |
+
return CANCEL_PENDING
|
| 515 |
+
# Left the pending queue between classification and dequeue: if it
|
| 516 |
+
# started executing, interrupt the now-running job; otherwise it has
|
| 517 |
+
# already finished and the cancel is a genuine no-op.
|
| 518 |
+
return CANCEL_RUNNING if interrupt(prompt_id) else CANCEL_UNKNOWN
|
| 519 |
+
# CANCEL_TERMINAL and CANCEL_UNKNOWN are intentional no-ops.
|
| 520 |
+
return classification
|
ComfyUI/comfy_execution/progress.py
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TypedDict, Dict, Optional, Tuple
|
| 2 |
+
from typing_extensions import override
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from enum import Enum
|
| 5 |
+
from abc import ABC
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
from typing import TYPE_CHECKING
|
| 8 |
+
if TYPE_CHECKING:
|
| 9 |
+
from comfy_execution.graph import DynamicPrompt
|
| 10 |
+
from protocol import BinaryEventTypes
|
| 11 |
+
from comfy_api import feature_flags
|
| 12 |
+
|
| 13 |
+
PreviewImageTuple = Tuple[str, Image.Image, Optional[int]]
|
| 14 |
+
|
| 15 |
+
class NodeState(Enum):
|
| 16 |
+
Pending = "pending"
|
| 17 |
+
Running = "running"
|
| 18 |
+
Finished = "finished"
|
| 19 |
+
Error = "error"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class NodeProgressState(TypedDict):
|
| 23 |
+
"""
|
| 24 |
+
A class to represent the state of a node's progress.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
state: NodeState
|
| 28 |
+
value: float
|
| 29 |
+
max: float
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class ProgressHandler(ABC):
|
| 33 |
+
"""
|
| 34 |
+
Abstract base class for progress handlers.
|
| 35 |
+
Progress handlers receive progress updates and display them in various ways.
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
def __init__(self, name: str):
|
| 39 |
+
self.name = name
|
| 40 |
+
self.enabled = True
|
| 41 |
+
|
| 42 |
+
def set_registry(self, registry: "ProgressRegistry"):
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
def start_handler(self, node_id: str, state: NodeProgressState, prompt_id: str):
|
| 46 |
+
"""Called when a node starts processing"""
|
| 47 |
+
pass
|
| 48 |
+
|
| 49 |
+
def update_handler(
|
| 50 |
+
self,
|
| 51 |
+
node_id: str,
|
| 52 |
+
value: float,
|
| 53 |
+
max_value: float,
|
| 54 |
+
state: NodeProgressState,
|
| 55 |
+
prompt_id: str,
|
| 56 |
+
image: PreviewImageTuple | None = None,
|
| 57 |
+
):
|
| 58 |
+
"""Called when a node's progress is updated"""
|
| 59 |
+
pass
|
| 60 |
+
|
| 61 |
+
def finish_handler(self, node_id: str, state: NodeProgressState, prompt_id: str):
|
| 62 |
+
"""Called when a node finishes processing"""
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
def reset(self):
|
| 66 |
+
"""Called when the progress registry is reset"""
|
| 67 |
+
pass
|
| 68 |
+
|
| 69 |
+
def enable(self):
|
| 70 |
+
"""Enable this handler"""
|
| 71 |
+
self.enabled = True
|
| 72 |
+
|
| 73 |
+
def disable(self):
|
| 74 |
+
"""Disable this handler"""
|
| 75 |
+
self.enabled = False
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class CLIProgressHandler(ProgressHandler):
|
| 79 |
+
"""
|
| 80 |
+
Handler that displays progress using tqdm progress bars in the CLI.
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
def __init__(self):
|
| 84 |
+
super().__init__("cli")
|
| 85 |
+
self.progress_bars: Dict[str, tqdm] = {}
|
| 86 |
+
|
| 87 |
+
@override
|
| 88 |
+
def start_handler(self, node_id: str, state: NodeProgressState, prompt_id: str):
|
| 89 |
+
# Create a new tqdm progress bar
|
| 90 |
+
if node_id not in self.progress_bars:
|
| 91 |
+
self.progress_bars[node_id] = tqdm(
|
| 92 |
+
total=state["max"],
|
| 93 |
+
desc=f"Node {node_id}",
|
| 94 |
+
unit="steps",
|
| 95 |
+
leave=True,
|
| 96 |
+
position=len(self.progress_bars),
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
@override
|
| 100 |
+
def update_handler(
|
| 101 |
+
self,
|
| 102 |
+
node_id: str,
|
| 103 |
+
value: float,
|
| 104 |
+
max_value: float,
|
| 105 |
+
state: NodeProgressState,
|
| 106 |
+
prompt_id: str,
|
| 107 |
+
image: PreviewImageTuple | None = None,
|
| 108 |
+
):
|
| 109 |
+
# Handle case where start_handler wasn't called
|
| 110 |
+
if node_id not in self.progress_bars:
|
| 111 |
+
self.progress_bars[node_id] = tqdm(
|
| 112 |
+
total=max_value,
|
| 113 |
+
desc=f"Node {node_id}",
|
| 114 |
+
unit="steps",
|
| 115 |
+
leave=True,
|
| 116 |
+
position=len(self.progress_bars),
|
| 117 |
+
)
|
| 118 |
+
self.progress_bars[node_id].update(value)
|
| 119 |
+
else:
|
| 120 |
+
# Update existing progress bar
|
| 121 |
+
if max_value != self.progress_bars[node_id].total:
|
| 122 |
+
self.progress_bars[node_id].total = max_value
|
| 123 |
+
# Calculate the update amount (difference from current position)
|
| 124 |
+
current_position = self.progress_bars[node_id].n
|
| 125 |
+
update_amount = value - current_position
|
| 126 |
+
if update_amount > 0:
|
| 127 |
+
self.progress_bars[node_id].update(update_amount)
|
| 128 |
+
|
| 129 |
+
@override
|
| 130 |
+
def finish_handler(self, node_id: str, state: NodeProgressState, prompt_id: str):
|
| 131 |
+
# Complete and close the progress bar if it exists
|
| 132 |
+
if node_id in self.progress_bars:
|
| 133 |
+
# Ensure the bar shows 100% completion
|
| 134 |
+
remaining = state["max"] - self.progress_bars[node_id].n
|
| 135 |
+
if remaining > 0:
|
| 136 |
+
self.progress_bars[node_id].update(remaining)
|
| 137 |
+
self.progress_bars[node_id].close()
|
| 138 |
+
del self.progress_bars[node_id]
|
| 139 |
+
|
| 140 |
+
@override
|
| 141 |
+
def reset(self):
|
| 142 |
+
# Close all progress bars
|
| 143 |
+
for bar in self.progress_bars.values():
|
| 144 |
+
bar.close()
|
| 145 |
+
self.progress_bars.clear()
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
class WebUIProgressHandler(ProgressHandler):
|
| 149 |
+
"""
|
| 150 |
+
Handler that sends progress updates to the WebUI via WebSockets.
|
| 151 |
+
"""
|
| 152 |
+
|
| 153 |
+
def __init__(self, server_instance):
|
| 154 |
+
super().__init__("webui")
|
| 155 |
+
self.server_instance = server_instance
|
| 156 |
+
|
| 157 |
+
def set_registry(self, registry: "ProgressRegistry"):
|
| 158 |
+
self.registry = registry
|
| 159 |
+
|
| 160 |
+
def _send_progress_state(self, prompt_id: str, nodes: Dict[str, NodeProgressState]):
|
| 161 |
+
"""Send the current progress state to the client"""
|
| 162 |
+
if self.server_instance is None:
|
| 163 |
+
return
|
| 164 |
+
|
| 165 |
+
# Only send info for non-pending nodes
|
| 166 |
+
active_nodes = {
|
| 167 |
+
node_id: {
|
| 168 |
+
"value": state["value"],
|
| 169 |
+
"max": state["max"],
|
| 170 |
+
"state": state["state"].value,
|
| 171 |
+
"node_id": node_id,
|
| 172 |
+
"prompt_id": prompt_id,
|
| 173 |
+
"display_node_id": self.registry.dynprompt.get_display_node_id(node_id),
|
| 174 |
+
"parent_node_id": self.registry.dynprompt.get_parent_node_id(node_id),
|
| 175 |
+
"real_node_id": self.registry.dynprompt.get_real_node_id(node_id),
|
| 176 |
+
}
|
| 177 |
+
for node_id, state in nodes.items()
|
| 178 |
+
if state["state"] != NodeState.Pending
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
# Send a combined progress_state message with all node states
|
| 182 |
+
# Include client_id to ensure message is only sent to the initiating client
|
| 183 |
+
self.server_instance.send_sync(
|
| 184 |
+
"progress_state", {"prompt_id": prompt_id, "nodes": active_nodes}, self.server_instance.client_id
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
@override
|
| 188 |
+
def start_handler(self, node_id: str, state: NodeProgressState, prompt_id: str):
|
| 189 |
+
# Send progress state of all nodes
|
| 190 |
+
if self.registry:
|
| 191 |
+
self._send_progress_state(prompt_id, self.registry.nodes)
|
| 192 |
+
|
| 193 |
+
@override
|
| 194 |
+
def update_handler(
|
| 195 |
+
self,
|
| 196 |
+
node_id: str,
|
| 197 |
+
value: float,
|
| 198 |
+
max_value: float,
|
| 199 |
+
state: NodeProgressState,
|
| 200 |
+
prompt_id: str,
|
| 201 |
+
image: PreviewImageTuple | None = None,
|
| 202 |
+
):
|
| 203 |
+
# Send progress state of all nodes
|
| 204 |
+
if self.registry:
|
| 205 |
+
self._send_progress_state(prompt_id, self.registry.nodes)
|
| 206 |
+
if image:
|
| 207 |
+
# Only send new format if client supports it
|
| 208 |
+
if feature_flags.supports_feature(
|
| 209 |
+
self.server_instance.sockets_metadata,
|
| 210 |
+
self.server_instance.client_id,
|
| 211 |
+
"supports_preview_metadata",
|
| 212 |
+
):
|
| 213 |
+
metadata = {
|
| 214 |
+
"node_id": node_id,
|
| 215 |
+
"prompt_id": prompt_id,
|
| 216 |
+
"display_node_id": self.registry.dynprompt.get_display_node_id(
|
| 217 |
+
node_id
|
| 218 |
+
),
|
| 219 |
+
"parent_node_id": self.registry.dynprompt.get_parent_node_id(
|
| 220 |
+
node_id
|
| 221 |
+
),
|
| 222 |
+
"real_node_id": self.registry.dynprompt.get_real_node_id(node_id),
|
| 223 |
+
}
|
| 224 |
+
self.server_instance.send_sync(
|
| 225 |
+
BinaryEventTypes.PREVIEW_IMAGE_WITH_METADATA,
|
| 226 |
+
(image, metadata),
|
| 227 |
+
self.server_instance.client_id,
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
@override
|
| 231 |
+
def finish_handler(self, node_id: str, state: NodeProgressState, prompt_id: str):
|
| 232 |
+
# Send progress state of all nodes
|
| 233 |
+
if self.registry:
|
| 234 |
+
self._send_progress_state(prompt_id, self.registry.nodes)
|
| 235 |
+
|
| 236 |
+
class ProgressRegistry:
|
| 237 |
+
"""
|
| 238 |
+
Registry that maintains node progress state and notifies registered handlers.
|
| 239 |
+
"""
|
| 240 |
+
|
| 241 |
+
def __init__(self, prompt_id: str, dynprompt: "DynamicPrompt"):
|
| 242 |
+
self.prompt_id = prompt_id
|
| 243 |
+
self.dynprompt = dynprompt
|
| 244 |
+
self.nodes: Dict[str, NodeProgressState] = {}
|
| 245 |
+
self.handlers: Dict[str, ProgressHandler] = {}
|
| 246 |
+
|
| 247 |
+
def register_handler(self, handler: ProgressHandler) -> None:
|
| 248 |
+
"""Register a progress handler"""
|
| 249 |
+
self.handlers[handler.name] = handler
|
| 250 |
+
|
| 251 |
+
def unregister_handler(self, handler_name: str) -> None:
|
| 252 |
+
"""Unregister a progress handler"""
|
| 253 |
+
if handler_name in self.handlers:
|
| 254 |
+
# Allow handler to clean up resources
|
| 255 |
+
self.handlers[handler_name].reset()
|
| 256 |
+
del self.handlers[handler_name]
|
| 257 |
+
|
| 258 |
+
def enable_handler(self, handler_name: str) -> None:
|
| 259 |
+
"""Enable a progress handler"""
|
| 260 |
+
if handler_name in self.handlers:
|
| 261 |
+
self.handlers[handler_name].enable()
|
| 262 |
+
|
| 263 |
+
def disable_handler(self, handler_name: str) -> None:
|
| 264 |
+
"""Disable a progress handler"""
|
| 265 |
+
if handler_name in self.handlers:
|
| 266 |
+
self.handlers[handler_name].disable()
|
| 267 |
+
|
| 268 |
+
def ensure_entry(self, node_id: str) -> NodeProgressState:
|
| 269 |
+
"""Ensure a node entry exists"""
|
| 270 |
+
if node_id not in self.nodes:
|
| 271 |
+
self.nodes[node_id] = NodeProgressState(
|
| 272 |
+
state=NodeState.Pending, value=0, max=1
|
| 273 |
+
)
|
| 274 |
+
return self.nodes[node_id]
|
| 275 |
+
|
| 276 |
+
def start_progress(self, node_id: str) -> None:
|
| 277 |
+
"""Start progress tracking for a node"""
|
| 278 |
+
entry = self.ensure_entry(node_id)
|
| 279 |
+
entry["state"] = NodeState.Running
|
| 280 |
+
entry["value"] = 0.0
|
| 281 |
+
entry["max"] = 1.0
|
| 282 |
+
|
| 283 |
+
# Notify all enabled handlers
|
| 284 |
+
for handler in self.handlers.values():
|
| 285 |
+
if handler.enabled:
|
| 286 |
+
handler.start_handler(node_id, entry, self.prompt_id)
|
| 287 |
+
|
| 288 |
+
def update_progress(
|
| 289 |
+
self, node_id: str, value: float, max_value: float, image: PreviewImageTuple | None = None
|
| 290 |
+
) -> None:
|
| 291 |
+
"""Update progress for a node"""
|
| 292 |
+
entry = self.ensure_entry(node_id)
|
| 293 |
+
entry["state"] = NodeState.Running
|
| 294 |
+
entry["value"] = value
|
| 295 |
+
entry["max"] = max_value
|
| 296 |
+
|
| 297 |
+
# Notify all enabled handlers
|
| 298 |
+
for handler in self.handlers.values():
|
| 299 |
+
if handler.enabled:
|
| 300 |
+
handler.update_handler(
|
| 301 |
+
node_id, value, max_value, entry, self.prompt_id, image
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
def finish_progress(self, node_id: str) -> None:
|
| 305 |
+
"""Finish progress tracking for a node"""
|
| 306 |
+
entry = self.ensure_entry(node_id)
|
| 307 |
+
entry["state"] = NodeState.Finished
|
| 308 |
+
entry["value"] = entry["max"]
|
| 309 |
+
|
| 310 |
+
# Notify all enabled handlers
|
| 311 |
+
for handler in self.handlers.values():
|
| 312 |
+
if handler.enabled:
|
| 313 |
+
handler.finish_handler(node_id, entry, self.prompt_id)
|
| 314 |
+
|
| 315 |
+
def reset_handlers(self) -> None:
|
| 316 |
+
"""Reset all handlers"""
|
| 317 |
+
for handler in self.handlers.values():
|
| 318 |
+
handler.reset()
|
| 319 |
+
|
| 320 |
+
# Global registry instance
|
| 321 |
+
global_progress_registry: ProgressRegistry | None = None
|
| 322 |
+
|
| 323 |
+
def reset_progress_state(prompt_id: str, dynprompt: "DynamicPrompt") -> None:
|
| 324 |
+
global global_progress_registry
|
| 325 |
+
|
| 326 |
+
# Reset existing handlers if registry exists
|
| 327 |
+
if global_progress_registry is not None:
|
| 328 |
+
global_progress_registry.reset_handlers()
|
| 329 |
+
|
| 330 |
+
# Create new registry
|
| 331 |
+
global_progress_registry = ProgressRegistry(prompt_id, dynprompt)
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
def add_progress_handler(handler: ProgressHandler) -> None:
|
| 335 |
+
registry = get_progress_state()
|
| 336 |
+
handler.set_registry(registry)
|
| 337 |
+
registry.register_handler(handler)
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def get_progress_state() -> ProgressRegistry:
|
| 341 |
+
global global_progress_registry
|
| 342 |
+
if global_progress_registry is None:
|
| 343 |
+
from comfy_execution.graph import DynamicPrompt
|
| 344 |
+
|
| 345 |
+
global_progress_registry = ProgressRegistry(
|
| 346 |
+
prompt_id="", dynprompt=DynamicPrompt({})
|
| 347 |
+
)
|
| 348 |
+
return global_progress_registry
|
ComfyUI/comfy_execution/utils.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import contextvars
|
| 2 |
+
from typing import Optional, NamedTuple
|
| 3 |
+
|
| 4 |
+
class ExecutionContext(NamedTuple):
|
| 5 |
+
"""
|
| 6 |
+
Context information about the currently executing node.
|
| 7 |
+
|
| 8 |
+
Attributes:
|
| 9 |
+
node_id: The ID of the currently executing node
|
| 10 |
+
list_index: The index in a list being processed (for operations on batches/lists)
|
| 11 |
+
"""
|
| 12 |
+
prompt_id: str
|
| 13 |
+
node_id: str
|
| 14 |
+
list_index: Optional[int]
|
| 15 |
+
|
| 16 |
+
current_executing_context: contextvars.ContextVar[Optional[ExecutionContext]] = contextvars.ContextVar("current_executing_context", default=None)
|
| 17 |
+
|
| 18 |
+
def get_executing_context() -> Optional[ExecutionContext]:
|
| 19 |
+
return current_executing_context.get(None)
|
| 20 |
+
|
| 21 |
+
class CurrentNodeContext:
|
| 22 |
+
"""
|
| 23 |
+
Context manager for setting the current executing node context.
|
| 24 |
+
|
| 25 |
+
Sets the current_executing_context on enter and resets it on exit.
|
| 26 |
+
|
| 27 |
+
Example:
|
| 28 |
+
with CurrentNodeContext(node_id="123", list_index=0):
|
| 29 |
+
# Code that should run with the current node context set
|
| 30 |
+
process_image()
|
| 31 |
+
"""
|
| 32 |
+
def __init__(self, prompt_id: str, node_id: str, list_index: Optional[int] = None):
|
| 33 |
+
self.context = ExecutionContext(
|
| 34 |
+
prompt_id= prompt_id,
|
| 35 |
+
node_id= node_id,
|
| 36 |
+
list_index= list_index
|
| 37 |
+
)
|
| 38 |
+
self.token = None
|
| 39 |
+
|
| 40 |
+
def __enter__(self):
|
| 41 |
+
self.token = current_executing_context.set(self.context)
|
| 42 |
+
return self
|
| 43 |
+
|
| 44 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
| 45 |
+
if self.token is not None:
|
| 46 |
+
current_executing_context.reset(self.token)
|
ComfyUI/comfy_execution/validation.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from comfy_api.latest import IO
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def validate_node_input(
|
| 5 |
+
received_type: str, input_type: str, strict: bool = False
|
| 6 |
+
) -> bool:
|
| 7 |
+
"""
|
| 8 |
+
received_type and input_type are both strings of the form "T1,T2,...".
|
| 9 |
+
|
| 10 |
+
If strict is True, the input_type must contain the received_type.
|
| 11 |
+
For example, if received_type is "STRING" and input_type is "STRING,INT",
|
| 12 |
+
this will return True. But if received_type is "STRING,INT" and input_type is
|
| 13 |
+
"INT", this will return False.
|
| 14 |
+
|
| 15 |
+
If strict is False, the input_type must have overlap with the received_type.
|
| 16 |
+
For example, if received_type is "STRING,BOOLEAN" and input_type is "STRING,INT",
|
| 17 |
+
this will return True.
|
| 18 |
+
|
| 19 |
+
Supports pre-union type extension behaviour of ``__ne__`` overrides.
|
| 20 |
+
"""
|
| 21 |
+
# If the types are exactly the same, we can return immediately
|
| 22 |
+
# Use pre-union behaviour: inverse of `__ne__`
|
| 23 |
+
# NOTE: this lets legacy '*' Any types work that override the __ne__ method of the str class.
|
| 24 |
+
if not received_type != input_type:
|
| 25 |
+
return True
|
| 26 |
+
|
| 27 |
+
# If one of the types is '*', we can return True immediately; this is the 'Any' type.
|
| 28 |
+
if received_type == IO.AnyType.io_type or input_type == IO.AnyType.io_type:
|
| 29 |
+
return True
|
| 30 |
+
|
| 31 |
+
# If the received type or input_type is a MatchType, we can return True immediately;
|
| 32 |
+
# validation for this is handled by the frontend
|
| 33 |
+
if received_type == IO.MatchType.io_type or input_type == IO.MatchType.io_type:
|
| 34 |
+
return True
|
| 35 |
+
|
| 36 |
+
# This accounts for some custom nodes that output lists of options as the type;
|
| 37 |
+
# if we ever want to break them on purpose, this can be removed
|
| 38 |
+
if isinstance(received_type, list) and input_type == IO.Combo.io_type:
|
| 39 |
+
return True
|
| 40 |
+
|
| 41 |
+
# Not equal, and not strings
|
| 42 |
+
if not isinstance(received_type, str) or not isinstance(input_type, str):
|
| 43 |
+
return False
|
| 44 |
+
|
| 45 |
+
# Split the type strings into sets for comparison
|
| 46 |
+
received_types = set(t.strip() for t in received_type.split(","))
|
| 47 |
+
input_types = set(t.strip() for t in input_type.split(","))
|
| 48 |
+
|
| 49 |
+
# If any of the types is '*', we can return True immediately; this is the 'Any' type.
|
| 50 |
+
if IO.AnyType.io_type in received_types or IO.AnyType.io_type in input_types:
|
| 51 |
+
return True
|
| 52 |
+
|
| 53 |
+
if strict:
|
| 54 |
+
# In strict mode, all received types must be in the input types
|
| 55 |
+
return received_types.issubset(input_types)
|
| 56 |
+
else:
|
| 57 |
+
# In non-strict mode, there must be at least one type in common
|
| 58 |
+
return len(received_types.intersection(input_types)) > 0
|
ComfyUI/comfy_extras/__pycache__/color_util.cpython-312.pyc
ADDED
|
Binary file (1.73 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_ace.cpython-312.pyc
ADDED
|
Binary file (11.1 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_advanced_samplers.cpython-312.pyc
ADDED
|
Binary file (9.03 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_align_your_steps.cpython-312.pyc
ADDED
|
Binary file (4.32 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_apg.cpython-312.pyc
ADDED
|
Binary file (4.65 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_ar_video.cpython-312.pyc
ADDED
|
Binary file (7.71 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_attention_multiply.cpython-312.pyc
ADDED
|
Binary file (10 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_audio.cpython-312.pyc
ADDED
|
Binary file (43.8 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_audio_encoder.cpython-312.pyc
ADDED
|
Binary file (3.91 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_bernini.cpython-312.pyc
ADDED
|
Binary file (8.06 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_bg_removal.cpython-312.pyc
ADDED
|
Binary file (3.96 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_boogu.cpython-312.pyc
ADDED
|
Binary file (6.19 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_bounding_boxes.cpython-312.pyc
ADDED
|
Binary file (21 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_camera_trajectory.cpython-312.pyc
ADDED
|
Binary file (14.3 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_canny.cpython-312.pyc
ADDED
|
Binary file (3.44 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_cfg.cpython-312.pyc
ADDED
|
Binary file (6.55 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_chroma_radiance.cpython-312.pyc
ADDED
|
Binary file (7.02 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_clip_sdxl.cpython-312.pyc
ADDED
|
Binary file (5.82 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_color.cpython-312.pyc
ADDED
|
Binary file (2.96 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_compositing.cpython-312.pyc
ADDED
|
Binary file (13.4 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_cond.cpython-312.pyc
ADDED
|
Binary file (4.34 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_context_windows.cpython-312.pyc
ADDED
|
Binary file (14.6 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_controlnet.cpython-312.pyc
ADDED
|
Binary file (5.83 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_cosmos.cpython-312.pyc
ADDED
|
Binary file (10.9 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_curve.cpython-312.pyc
ADDED
|
Binary file (5 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_custom_sampler.cpython-312.pyc
ADDED
|
Binary file (73.2 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_dataset.cpython-312.pyc
ADDED
|
Binary file (90.2 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_depth_anything_3.cpython-312.pyc
ADDED
|
Binary file (36.2 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_differential_diffusion.cpython-312.pyc
ADDED
|
Binary file (3.97 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_easycache.cpython-312.pyc
ADDED
|
Binary file (33.1 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_edit_model.cpython-312.pyc
ADDED
|
Binary file (2.43 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_eps.cpython-312.pyc
ADDED
|
Binary file (7.04 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_flux.cpython-312.pyc
ADDED
|
Binary file (16.8 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_frame_interpolation.cpython-312.pyc
ADDED
|
Binary file (11.4 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_freelunch.cpython-312.pyc
ADDED
|
Binary file (8.93 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_fresca.cpython-312.pyc
ADDED
|
Binary file (5.25 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_gaussian_splat.cpython-312.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:819067e8150f89d64325ffb4548d2b34ba8055fb41dabc9f05afd3f5231abcac
|
| 3 |
+
size 132647
|
ComfyUI/comfy_extras/__pycache__/nodes_gits.cpython-312.pyc
ADDED
|
Binary file (24.9 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_glsl.cpython-312.pyc
ADDED
|
Binary file (43.8 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_hidream.cpython-312.pyc
ADDED
|
Binary file (5.02 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_hidream_o1.cpython-312.pyc
ADDED
|
Binary file (15.8 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_hooks.cpython-312.pyc
ADDED
|
Binary file (28.6 kB). View file
|
|
|
ComfyUI/comfy_extras/__pycache__/nodes_hunyuan.cpython-312.pyc
ADDED
|
Binary file (30.4 kB). View file
|
|
|