Refactor WorkflowAssembler for cleaner stateless assembly
Browse files- core/workflow_assembler.py +116 -111
core/workflow_assembler.py
CHANGED
|
@@ -1,11 +1,50 @@
|
|
| 1 |
-
import yaml
|
| 2 |
import os
|
| 3 |
-
import
|
|
|
|
| 4 |
from copy import deepcopy
|
| 5 |
from comfy_integration.nodes import NODE_CLASS_MAPPINGS
|
| 6 |
from chain_injectors import discover_injectors, get_registered_features
|
| 7 |
from core.settings import FEATURES_CONFIG
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class WorkflowAssembler:
|
| 10 |
def __init__(self, recipe_path, dynamic_values=None):
|
| 11 |
self.base_path = os.path.dirname(recipe_path)
|
|
@@ -13,10 +52,8 @@ class WorkflowAssembler:
|
|
| 13 |
self.node_counter = 0
|
| 14 |
self.workflow = {}
|
| 15 |
self.node_map = {}
|
| 16 |
-
|
| 17 |
-
model_type
|
| 18 |
-
self._load_injector_config(model_type=model_type)
|
| 19 |
-
|
| 20 |
self.recipe = self._load_and_merge_recipe(os.path.basename(recipe_path), self.dynamic_values)
|
| 21 |
|
| 22 |
def _load_injector_config(self, model_type=None):
|
|
@@ -25,32 +62,25 @@ class WorkflowAssembler:
|
|
| 25 |
|
| 26 |
order = []
|
| 27 |
if model_type and model_type in FEATURES_CONFIG:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
if chain_key in self.global_injectors and chain_key not in order:
|
| 35 |
order.append(chain_key)
|
| 36 |
|
| 37 |
-
for chain_key in self.global_injectors
|
| 38 |
if chain_key not in order:
|
| 39 |
order.append(chain_key)
|
| 40 |
|
| 41 |
-
# An injector may register under a bare CHAIN_TYPE while the recipe uses
|
| 42 |
-
# the derived "dynamic_<feature>_chains" key (or vice versa). Alias both
|
| 43 |
-
# directions so either spelling resolves to the same callable.
|
| 44 |
for chain_key in list(order):
|
| 45 |
func = self.global_injectors.get(chain_key)
|
| 46 |
if not func:
|
| 47 |
continue
|
| 48 |
-
if chain_key.startswith(
|
| 49 |
-
|
| 50 |
-
alias = f"dynamic_{feature}_chains"
|
| 51 |
-
else:
|
| 52 |
-
feature = chain_key
|
| 53 |
-
alias = f"dynamic_{feature}_chains"
|
| 54 |
self.global_injectors.setdefault(alias, func)
|
| 55 |
self.global_injectors.setdefault(feature, func)
|
| 56 |
if alias not in order:
|
|
@@ -67,123 +97,98 @@ class WorkflowAssembler:
|
|
| 67 |
def _get_node_template(self, class_type):
|
| 68 |
if class_type not in NODE_CLASS_MAPPINGS:
|
| 69 |
raise ValueError(f"Node class '{class_type}' not found. Ensure it's correctly imported in comfy_integration/nodes.py.")
|
| 70 |
-
|
| 71 |
node_class = NODE_CLASS_MAPPINGS[class_type]
|
| 72 |
input_types = node_class.INPUT_TYPES()
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
"class_type": class_type,
|
| 77 |
-
"_meta": {"title":
|
| 78 |
}
|
| 79 |
-
|
| 80 |
-
all_inputs = {**input_types.get('required', {}), **input_types.get('optional', {})}
|
| 81 |
-
for name, details in all_inputs.items():
|
| 82 |
-
config = details[1] if len(details) > 1 and isinstance(details[1], dict) else {}
|
| 83 |
-
template["inputs"][name] = config.get("default")
|
| 84 |
-
|
| 85 |
-
return template
|
| 86 |
|
| 87 |
def _load_and_merge_recipe(self, recipe_filename, dynamic_values, search_context_dir=None):
|
| 88 |
-
|
| 89 |
-
recipe_path_to_use = os.path.join(search_path, recipe_filename)
|
| 90 |
-
|
| 91 |
if not os.path.exists(recipe_path_to_use):
|
| 92 |
raise FileNotFoundError(f"Recipe file not found: {recipe_path_to_use}")
|
| 93 |
|
| 94 |
-
with open(recipe_path_to_use,
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
if value is not None:
|
| 99 |
-
content = content.replace(f"{{{{ {key} }}}}", str(value))
|
| 100 |
-
|
| 101 |
-
main_recipe = yaml.safe_load(content)
|
| 102 |
-
|
| 103 |
-
merged_recipe = {'nodes': {}, 'connections': [], 'ui_map': {}}
|
| 104 |
for key in self.injector_order:
|
| 105 |
-
# Injector chains may be registered under a bare CHAIN_TYPE
|
| 106 |
-
# (e.g. "prompt_enhancer") or the derived "dynamic_*_chains" form.
|
| 107 |
-
# Both must survive the merge, otherwise the assembler has nothing
|
| 108 |
-
# to look up at injection time.
|
| 109 |
if key:
|
| 110 |
merged_recipe[key] = {}
|
| 111 |
|
| 112 |
parent_recipe_dir = os.path.dirname(recipe_path_to_use)
|
| 113 |
-
for import_path_template in main_recipe.get(
|
| 114 |
-
import_path = import_path_template
|
| 115 |
-
for key, value in dynamic_values.items():
|
| 116 |
-
if value is not None:
|
| 117 |
-
import_path = import_path.replace(f"{{{{ {key} }}}}", str(value))
|
| 118 |
-
|
| 119 |
try:
|
| 120 |
imported_recipe = self._load_and_merge_recipe(import_path, dynamic_values, search_context_dir=parent_recipe_dir)
|
| 121 |
-
|
| 122 |
-
merged_recipe['connections'].extend(imported_recipe.get('connections', []))
|
| 123 |
-
merged_recipe['ui_map'].update(imported_recipe.get('ui_map', {}))
|
| 124 |
-
for key in self.injector_order:
|
| 125 |
-
if key in imported_recipe:
|
| 126 |
-
merged_recipe.setdefault(key, {}).update(imported_recipe.get(key, {}))
|
| 127 |
except FileNotFoundError:
|
| 128 |
print(f"Warning: Optional recipe partial '{import_path}' not found. Skipping.")
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
def assemble(self, ui_values):
|
| 140 |
self.ui_values = ui_values
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
node_data = deepcopy(template)
|
| 145 |
-
|
| 146 |
-
unique_id = self._get_unique_id()
|
| 147 |
-
self.node_map[name] = unique_id
|
| 148 |
-
|
| 149 |
-
if 'params' in details:
|
| 150 |
-
for param, value in details['params'].items():
|
| 151 |
-
if param in node_data['inputs']:
|
| 152 |
-
node_data['inputs'][param] = value
|
| 153 |
-
|
| 154 |
-
self.workflow[unique_id] = node_data
|
| 155 |
-
|
| 156 |
-
for ui_key, target in self.recipe.get('ui_map', {}).items():
|
| 157 |
-
if ui_key in ui_values and ui_values[ui_key] is not None:
|
| 158 |
-
target_list = target if isinstance(target, list) else [target]
|
| 159 |
-
for t in target_list:
|
| 160 |
-
target_name, target_param = t.split(':')
|
| 161 |
-
if target_name in self.node_map:
|
| 162 |
-
self.workflow[self.node_map[target_name]]['inputs'][target_param] = ui_values[ui_key]
|
| 163 |
-
|
| 164 |
-
for conn in self.recipe.get('connections', []):
|
| 165 |
-
from_name, from_output_idx = conn['from'].split(':')
|
| 166 |
-
to_name, to_input_name = conn['to'].split(':')
|
| 167 |
-
|
| 168 |
-
from_id = self.node_map.get(from_name)
|
| 169 |
-
to_id = self.node_map.get(to_name)
|
| 170 |
-
|
| 171 |
-
if from_id and to_id:
|
| 172 |
-
self.workflow[to_id]['inputs'][to_input_name] = [from_id, int(from_output_idx)]
|
| 173 |
-
|
| 174 |
-
print("--- [Assembler] Applying dynamic injectors ---")
|
| 175 |
-
recipe_chain_types = {key for key in self.recipe if key in self.injector_order}
|
| 176 |
-
processing_order = [key for key in self.injector_order if key in recipe_chain_types]
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
for chain_type in processing_order:
|
| 179 |
injector_func = self.global_injectors.get(chain_type)
|
| 180 |
if injector_func:
|
| 181 |
for chain_key, chain_def in self.recipe.get(chain_type, {}).items():
|
| 182 |
if chain_key in ui_values and ui_values[chain_key]:
|
| 183 |
print(f" -> Injecting '{chain_type}' for '{chain_key}'...")
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
print("--- [Assembler] Finished applying injectors ---")
|
| 188 |
-
|
| 189 |
-
return self.workflow
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
+
import yaml
|
| 4 |
from copy import deepcopy
|
| 5 |
from comfy_integration.nodes import NODE_CLASS_MAPPINGS
|
| 6 |
from chain_injectors import discover_injectors, get_registered_features
|
| 7 |
from core.settings import FEATURES_CONFIG
|
| 8 |
|
| 9 |
+
_PLACEHOLDER_RE = re.compile(r"\{\{\s*(\w+)\s*\}\}")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def _render_placeholders(text: str, values: dict) -> str:
|
| 13 |
+
for key, val in (values or {}).items():
|
| 14 |
+
if val is not None:
|
| 15 |
+
text = text.replace(f"{{{{ {key} }}}}", str(val))
|
| 16 |
+
return text
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _merge_recipe_dict(target: dict, source: dict) -> dict:
|
| 20 |
+
for key, val in (source or {}).items():
|
| 21 |
+
if key == "imports" or val is None:
|
| 22 |
+
continue
|
| 23 |
+
if key == "connections" and isinstance(val, list):
|
| 24 |
+
target.setdefault("connections", []).extend(val)
|
| 25 |
+
elif isinstance(val, dict):
|
| 26 |
+
target.setdefault(key, {}).update(val)
|
| 27 |
+
else:
|
| 28 |
+
target[key] = val
|
| 29 |
+
return target
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _iter_ui_bindings(ui_map: dict, ui_values: dict):
|
| 33 |
+
for ui_key, target in (ui_map or {}).items():
|
| 34 |
+
if isinstance(target, dict):
|
| 35 |
+
sub_src = ui_values.get(ui_key) if isinstance(ui_values.get(ui_key), dict) else {}
|
| 36 |
+
for sub_key, sub_targets in target.items():
|
| 37 |
+
val = sub_src.get(sub_key) if sub_src.get(sub_key) is not None else ui_values.get(sub_key)
|
| 38 |
+
if val is not None:
|
| 39 |
+
for spec in (sub_targets if isinstance(sub_targets, list) else [sub_targets]):
|
| 40 |
+
if isinstance(spec, str) and ":" in spec:
|
| 41 |
+
yield (*spec.split(":", 1), val)
|
| 42 |
+
elif (val := ui_values.get(ui_key)) is not None:
|
| 43 |
+
for spec in (target if isinstance(target, list) else [target]):
|
| 44 |
+
if isinstance(spec, str) and ":" in spec:
|
| 45 |
+
yield (*spec.split(":", 1), val)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
class WorkflowAssembler:
|
| 49 |
def __init__(self, recipe_path, dynamic_values=None):
|
| 50 |
self.base_path = os.path.dirname(recipe_path)
|
|
|
|
| 52 |
self.node_counter = 0
|
| 53 |
self.workflow = {}
|
| 54 |
self.node_map = {}
|
| 55 |
+
|
| 56 |
+
self._load_injector_config(model_type=self.dynamic_values.get("model_type"))
|
|
|
|
|
|
|
| 57 |
self.recipe = self._load_and_merge_recipe(os.path.basename(recipe_path), self.dynamic_values)
|
| 58 |
|
| 59 |
def _load_injector_config(self, model_type=None):
|
|
|
|
| 62 |
|
| 63 |
order = []
|
| 64 |
if model_type and model_type in FEATURES_CONFIG:
|
| 65 |
+
for feat in FEATURES_CONFIG[model_type].get("enabled_chains", []):
|
| 66 |
+
chain_key = (
|
| 67 |
+
registered_features[feat]["chain_type"]
|
| 68 |
+
if feat in registered_features
|
| 69 |
+
else f"dynamic_{feat}_chains"
|
| 70 |
+
)
|
| 71 |
if chain_key in self.global_injectors and chain_key not in order:
|
| 72 |
order.append(chain_key)
|
| 73 |
|
| 74 |
+
for chain_key in self.global_injectors:
|
| 75 |
if chain_key not in order:
|
| 76 |
order.append(chain_key)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
for chain_key in list(order):
|
| 79 |
func = self.global_injectors.get(chain_key)
|
| 80 |
if not func:
|
| 81 |
continue
|
| 82 |
+
feature = chain_key[8:-7] if (chain_key.startswith("dynamic_") and chain_key.endswith("_chains")) else chain_key
|
| 83 |
+
alias = f"dynamic_{feature}_chains"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
self.global_injectors.setdefault(alias, func)
|
| 85 |
self.global_injectors.setdefault(feature, func)
|
| 86 |
if alias not in order:
|
|
|
|
| 97 |
def _get_node_template(self, class_type):
|
| 98 |
if class_type not in NODE_CLASS_MAPPINGS:
|
| 99 |
raise ValueError(f"Node class '{class_type}' not found. Ensure it's correctly imported in comfy_integration/nodes.py.")
|
| 100 |
+
|
| 101 |
node_class = NODE_CLASS_MAPPINGS[class_type]
|
| 102 |
input_types = node_class.INPUT_TYPES()
|
| 103 |
+
all_inputs = {**input_types.get("required", {}), **input_types.get("optional", {})}
|
| 104 |
+
|
| 105 |
+
return {
|
| 106 |
+
"inputs": {
|
| 107 |
+
name: (details[1].get("default") if len(details) > 1 and isinstance(details[1], dict) else None)
|
| 108 |
+
for name, details in all_inputs.items()
|
| 109 |
+
},
|
| 110 |
"class_type": class_type,
|
| 111 |
+
"_meta": {"title": getattr(node_class, "NODE_NAME", class_type)},
|
| 112 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
def _load_and_merge_recipe(self, recipe_filename, dynamic_values, search_context_dir=None):
|
| 115 |
+
recipe_path_to_use = os.path.join(search_context_dir or self.base_path, recipe_filename)
|
|
|
|
|
|
|
| 116 |
if not os.path.exists(recipe_path_to_use):
|
| 117 |
raise FileNotFoundError(f"Recipe file not found: {recipe_path_to_use}")
|
| 118 |
|
| 119 |
+
with open(recipe_path_to_use, "r", encoding="utf-8") as f:
|
| 120 |
+
main_recipe = yaml.safe_load(_render_placeholders(f.read(), dynamic_values)) or {}
|
| 121 |
|
| 122 |
+
merged_recipe = {"nodes": {}, "connections": [], "ui_map": {}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
for key in self.injector_order:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
if key:
|
| 125 |
merged_recipe[key] = {}
|
| 126 |
|
| 127 |
parent_recipe_dir = os.path.dirname(recipe_path_to_use)
|
| 128 |
+
for import_path_template in main_recipe.get("imports", []):
|
| 129 |
+
import_path = _render_placeholders(import_path_template, dynamic_values)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
try:
|
| 131 |
imported_recipe = self._load_and_merge_recipe(import_path, dynamic_values, search_context_dir=parent_recipe_dir)
|
| 132 |
+
_merge_recipe_dict(merged_recipe, imported_recipe)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
except FileNotFoundError:
|
| 134 |
print(f"Warning: Optional recipe partial '{import_path}' not found. Skipping.")
|
| 135 |
|
| 136 |
+
return _merge_recipe_dict(merged_recipe, main_recipe)
|
| 137 |
+
|
| 138 |
+
def add_node(self, class_type: str, inputs: dict = None, title: str = None, alias: str = None) -> str:
|
| 139 |
+
node_data = deepcopy(self._get_node_template(class_type))
|
| 140 |
+
if title:
|
| 141 |
+
node_data["_meta"]["title"] = title
|
| 142 |
+
for k, v in (inputs or {}).items():
|
| 143 |
+
if k in node_data["inputs"]:
|
| 144 |
+
node_data["inputs"][k] = v
|
| 145 |
+
node_id = self._get_unique_id()
|
| 146 |
+
self.workflow[node_id] = node_data
|
| 147 |
+
if alias:
|
| 148 |
+
self.node_map[alias] = node_id
|
| 149 |
+
return node_id
|
| 150 |
+
|
| 151 |
+
def connect(self, from_node: str, from_output_idx: int, to_node: str, to_input_name: str):
|
| 152 |
+
from_id = self.node_map.get(from_node, from_node)
|
| 153 |
+
to_id = self.node_map.get(to_node, to_node)
|
| 154 |
+
if from_id in self.workflow and to_id in self.workflow:
|
| 155 |
+
self.workflow[to_id]["inputs"][to_input_name] = [from_id, int(from_output_idx)]
|
| 156 |
|
| 157 |
def assemble(self, ui_values):
|
| 158 |
self.ui_values = ui_values
|
| 159 |
+
self.node_counter = 0
|
| 160 |
+
self.workflow = {}
|
| 161 |
+
self.node_map = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
for name, details in self.recipe.get("nodes", {}).items():
|
| 164 |
+
class_type = details.get("class_type")
|
| 165 |
+
if not class_type:
|
| 166 |
+
continue
|
| 167 |
+
if match := _PLACEHOLDER_RE.search(class_type):
|
| 168 |
+
class_type = ui_values.get(match.group(1))
|
| 169 |
+
if not class_type:
|
| 170 |
+
continue
|
| 171 |
+
self.add_node(class_type, inputs=details.get("params"), title=details.get("title"), alias=name)
|
| 172 |
+
|
| 173 |
+
for target_name, target_param, val in _iter_ui_bindings(self.recipe.get("ui_map"), ui_values):
|
| 174 |
+
if target_id := self.node_map.get(target_name):
|
| 175 |
+
self.workflow[target_id]["inputs"][target_param] = val
|
| 176 |
+
|
| 177 |
+
for conn in self.recipe.get("connections", []):
|
| 178 |
+
if isinstance(conn.get("from"), str) and isinstance(conn.get("to"), str):
|
| 179 |
+
from_name, from_output_idx = conn["from"].split(":", 1)
|
| 180 |
+
to_name, to_input_name = conn["to"].split(":", 1)
|
| 181 |
+
self.connect(from_name, int(from_output_idx), to_name, to_input_name)
|
| 182 |
+
|
| 183 |
+
print("--- [Assembler] Applying dynamic injectors ---")
|
| 184 |
+
processing_order = [key for key in self.injector_order if key in self.recipe]
|
| 185 |
for chain_type in processing_order:
|
| 186 |
injector_func = self.global_injectors.get(chain_type)
|
| 187 |
if injector_func:
|
| 188 |
for chain_key, chain_def in self.recipe.get(chain_type, {}).items():
|
| 189 |
if chain_key in ui_values and ui_values[chain_key]:
|
| 190 |
print(f" -> Injecting '{chain_type}' for '{chain_key}'...")
|
| 191 |
+
injector_func(self, chain_def, ui_values[chain_key])
|
| 192 |
+
|
|
|
|
| 193 |
print("--- [Assembler] Finished applying injectors ---")
|
| 194 |
+
return self.workflow
|
|
|