Spaces:
Running on Zero
Running on Zero
Refactor WorkflowAssembler for cleaner stateless assembly
Browse files12345678910111 unmodified line131415161718192021222 unmodified lines252627282930313233343536373839404142434445464748495051525354555610 unmodified lines676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189import yamlimport osimport importlibfrom copy import deepcopyfrom comfy_integration.nodes import NODE_CLASS_MAPPINGSfrom chain_injectors import discover_injectors, get_registered_featuresfrom core.settings import FEATURES_CONFIGclass WorkflowAssembler:def __init__(self, recipe_path, dynamic_values=None):self.base_path = os.path.dirname(recipe_path)1 unmodified lineself.node_counter = 0self.workflow = {}self.node_map = {}model_type = self.dynamic_values.get('model_type')self._load_injector_config(model_type=model_type)self.recipe = self._load_and_merge_recipe(os.path.basename(recipe_path), self.dynamic_values)def _load_injector_config(self, model_type=None):2 unmodified linesorder = []if model_type and model_type in FEATURES_CONFIG:enabled_features = FEATURES_CONFIG[model_type].get('enabled_chains', [])for feat in enabled_features:if feat in registered_features:chain_key = registered_features[feat]['chain_type']else:chain_key = f"dynamic_{feat}_chains"if chain_key in self.global_injectors and chain_key not in order:order.append(chain_key)for chain_key in self.global_injectors.keys():if chain_key not in order:order.append(chain_key)# An injector may register under a bare CHAIN_TYPE while the recipe uses# the derived "dynamic_<feature>_chains" key (or vice versa). Alias both# directions so either spelling resolves to the same callable.for chain_key in list(order):func = self.global_injectors.get(chain_key)if not func:continueif chain_key.startswith('dynamic_') and chain_key.endswith('_chains'):feature = chain_key[8:-7]alias = f"dynamic_{feature}_chains"else:feature = chain_keyalias = f"dynamic_{feature}_chains"self.global_injectors.setdefault(alias, func)self.global_injectors.setdefault(feature, func)if alias not in order:10 unmodified linesdef _get_node_template(self, class_type):if class_type not in NODE_CLASS_MAPPINGS:raise ValueError(f"Node class '{class_type}' not found. Ensure it's correctly imported in comfy_integration/nodes.py.")node_class = NODE_CLASS_MAPPINGS[class_type]input_types = node_class.INPUT_TYPES()template = {"inputs": {},"class_type": class_type,"_meta": {"title": node_class.NODE_NAME if hasattr(node_class, 'NODE_NAME') else class_type}}all_inputs = {**input_types.get('required', {}), **input_types.get('optional', {})}for name, details in all_inputs.items():config = details[1] if len(details) > 1 and isinstance(details[1], dict) else {}template["inputs"][name] = config.get("default")return templatedef _load_and_merge_recipe(self, recipe_filename, dynamic_values, search_context_dir=None):search_path = search_context_dir or self.base_pathrecipe_path_to_use = os.path.join(search_path, recipe_filename)if not os.path.exists(recipe_path_to_use):raise FileNotFoundError(f"Recipe file not found: {recipe_path_to_use}")with open(recipe_path_to_use, 'r', encoding='utf-8') as f:content = f.read()for key, value in dynamic_values.items():if value is not None:content = content.replace(f"{{{{ {key} }}}}", str(value))main_recipe = yaml.safe_load(content)merged_recipe = {'nodes': {}, 'connections': [], 'ui_map': {}}for key in self.injector_order:# Injector chains may be registered under a bare CHAIN_TYPE# (e.g. "prompt_enhancer") or the derived "dynamic_*_chains" form.# Both must survive the merge, otherwise the assembler has nothing# to look up at injection time.if key:merged_recipe[key] = {}parent_recipe_dir = os.path.dirname(recipe_path_to_use)for import_path_template in main_recipe.get('imports', []):import_path = import_path_templatefor key, value in dynamic_values.items():if value is not None:import_path = import_path.replace(f"{{{{ {key} }}}}", str(value))try:imported_recipe = self._load_and_merge_recipe(import_path, dynamic_values, search_context_dir=parent_recipe_dir)merged_recipe['nodes'].update(imported_recipe.get('nodes', {}))merged_recipe['connections'].extend(imported_recipe.get('connections', []))merged_recipe['ui_map'].update(imported_recipe.get('ui_map', {}))for key in self.injector_order:if key in imported_recipe:merged_recipe.setdefault(key, {}).update(imported_recipe.get(key, {}))except FileNotFoundError:print(f"Warning: Optional recipe partial '{import_path}' not found. Skipping.")merged_recipe['nodes'].update(main_recipe.get('nodes', {}))merged_recipe['connections'].extend(main_recipe.get('connections', []))merged_recipe['ui_map'].update(main_recipe.get('ui_map', {}))for key in self.injector_order:if key in main_recipe:merged_recipe.setdefault(key, {}).update(main_recipe.get(key, {}))return merged_recipedef assemble(self, ui_values):self.ui_values = ui_valuesfor name, details in self.recipe['nodes'].items():class_type = details['class_type']template = self._get_node_template(class_type)node_data = deepcopy(template)unique_id = self._get_unique_id()self.node_map[name] = unique_idif 'params' in details:for param, value in details['params'].items():if param in node_data['inputs']:node_data['inputs'][param] = valueself.workflow[unique_id] = node_datafor ui_key, target in self.recipe.get('ui_map', {}).items():if ui_key in ui_values and ui_values[ui_key] is not None:target_list = target if isinstance(target, list) else [target]for t in target_list:target_name, target_param = t.split(':')if target_name in self.node_map:self.workflow[self.node_map[target_name]]['inputs'][target_param] = ui_values[ui_key]for conn in self.recipe.get('connections', []):from_name, from_output_idx = conn['from'].split(':')to_name, to_input_name = conn['to'].split(':')from_id = self.node_map.get(from_name)to_id = self.node_map.get(to_name)if from_id and to_id:self.workflow[to_id]['inputs'][to_input_name] = [from_id, int(from_output_idx)]print("--- [Assembler] Applying dynamic injectors ---")recipe_chain_types = {key for key in self.recipe if key in self.injector_order}processing_order = [key for key in self.injector_order if key in recipe_chain_types]for chain_type in processing_order:injector_func = self.global_injectors.get(chain_type)if injector_func:for chain_key, chain_def in self.recipe.get(chain_type, {}).items():if chain_key in ui_values and ui_values[chain_key]:print(f" -> Injecting '{chain_type}' for '{chain_key}'...")chain_items = ui_values[chain_key]injector_func(self, chain_def, chain_items)print("--- [Assembler] Finished applying injectors ---")return self.workflow12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849501 unmodified line52535455565758592 unmodified lines6263646566676869707172737475767778798081828384858610 unmodified lines979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194import osimport reimport yamlfrom copy import deepcopyfrom comfy_integration.nodes import NODE_CLASS_MAPPINGSfrom chain_injectors import discover_injectors, get_registered_featuresfrom core.settings import FEATURES_CONFIG_PLACEHOLDER_RE = re.compile(r"\{\{\s*(\w+)\s*\}\}")def _render_placeholders(text: str, values: dict) -> str:for key, val in (values or {}).items():if val is not None:text = text.replace(f"{{{{ {key} }}}}", str(val))return textdef _merge_recipe_dict(target: dict, source: dict) -> dict:for key, val in (source or {}).items():if key == "imports" or val is None:continueif key == "connections" and isinstance(val, list):target.setdefault("connections", []).extend(val)elif isinstance(val, dict):target.setdefault(key, {}).update(val)else:target[key] = valreturn targetdef _iter_ui_bindings(ui_map: dict, ui_values: dict):for ui_key, target in (ui_map or {}).items():if isinstance(target, dict):sub_src = ui_values.get(ui_key) if isinstance(ui_values.get(ui_key), dict) else {}for sub_key, sub_targets in target.items():val = sub_src.get(sub_key) if sub_src.get(sub_key) is not None else ui_values.get(sub_key)if val is not None:for spec in (sub_targets if isinstance(sub_targets, list) else [sub_targets]):if isinstance(spec, str) and ":" in spec:yield (*spec.split(":", 1), val)elif (val := ui_values.get(ui_key)) is not None:for spec in (target if isinstance(target, list) else [target]):if isinstance(spec, str) and ":" in spec:yield (*spec.split(":", 1), val)class WorkflowAssembler:def __init__(self, recipe_path, dynamic_values=None):self.base_path = os.path.dirname(recipe_path)1 unmodified lineself.node_counter = 0self.workflow = {}self.node_map = {}self._load_injector_config(model_type=self.dynamic_values.get("model_type"))self.recipe = self._load_and_merge_recipe(os.path.basename(recipe_path), self.dynamic_values)def _load_injector_config(self, model_type=None):2 unmodified linesorder = []if model_type and model_type in FEATURES_CONFIG:for feat in FEATURES_CONFIG[model_type].get("enabled_chains", []):chain_key = (registered_features[feat]["chain_type"]if feat in registered_featureselse f"dynamic_{feat}_chains")if chain_key in self.global_injectors and chain_key not in order:order.append(chain_key)for chain_key in self.global_injectors:if chain_key not in order:order.append(chain_key)for chain_key in list(order):func = self.global_injectors.get(chain_key)if not func:continuefeature = chain_key[8:-7] if (chain_key.startswith("dynamic_") and chain_key.endswith("_chains")) else chain_keyalias = f"dynamic_{feature}_chains"self.global_injectors.setdefault(alias, func)self.global_injectors.setdefault(feature, func)if alias not in order:10 unmodified linesdef _get_node_template(self, class_type):if class_type not in NODE_CLASS_MAPPINGS:raise ValueError(f"Node class '{class_type}' not found. Ensure it's correctly imported in comfy_integration/nodes.py.")node_class = NODE_CLASS_MAPPINGS[class_type]input_types = node_class.INPUT_TYPES()all_inputs = {**input_types.get("required", {}), **input_types.get("optional", {})}return {"inputs": {name: (details[1].get("default") if len(details) > 1 and isinstance(details[1], dict) else None)for name, details in all_inputs.items()},"class_type": class_type,"_meta": {"title": getattr(node_class, "NODE_NAME", class_type)},}def _load_and_merge_recipe(self, recipe_filename, dynamic_values, search_context_dir=None):recipe_path_to_use = os.path.join(search_context_dir or self.base_path, recipe_filename)if not os.path.exists(recipe_path_to_use):raise FileNotFoundError(f"Recipe file not found: {recipe_path_to_use}")with open(recipe_path_to_use, "r", encoding="utf-8") as f:main_recipe = yaml.safe_load(_render_placeholders(f.read(), dynamic_values)) or {}merged_recipe = {"nodes": {}, "connections": [], "ui_map": {}}for key in self.injector_order:if key:merged_recipe[key] = {}parent_recipe_dir = os.path.dirname(recipe_path_to_use)for import_path_template in main_recipe.get("imports", []):import_path = _render_placeholders(import_path_template, dynamic_values)try:imported_recipe = self._load_and_merge_recipe(import_path, dynamic_values, search_context_dir=parent_recipe_dir)_merge_recipe_dict(merged_recipe, imported_recipe)except FileNotFoundError:print(f"Warning: Optional recipe partial '{import_path}' not found. Skipping.")return _merge_recipe_dict(merged_recipe, main_recipe)def add_node(self, class_type: str, inputs: dict = None, title: str = None, alias: str = None) -> str:node_data = deepcopy(self._get_node_template(class_type))if title:node_data["_meta"]["title"] = titlefor k, v in (inputs or {}).items():if k in node_data["inputs"]:node_data["inputs"][k] = vnode_id = self._get_unique_id()self.workflow[node_id] = node_dataif alias:self.node_map[alias] = node_idreturn node_iddef connect(self, from_node: str, from_output_idx: int, to_node: str, to_input_name: str):from_id = self.node_map.get(from_node, from_node)to_id = self.node_map.get(to_node, to_node)if from_id in self.workflow and to_id in self.workflow:self.workflow[to_id]["inputs"][to_input_name] = [from_id, int(from_output_idx)]def assemble(self, ui_values):self.ui_values = ui_valuesself.node_counter = 0self.workflow = {}self.node_map = {}for name, details in self.recipe.get("nodes", {}).items():class_type = details.get("class_type")if not class_type:continueif match := _PLACEHOLDER_RE.search(class_type):class_type = ui_values.get(match.group(1))if not class_type:continueself.add_node(class_type, inputs=details.get("params"), title=details.get("title"), alias=name)for target_name, target_param, val in _iter_ui_bindings(self.recipe.get("ui_map"), ui_values):if target_id := self.node_map.get(target_name):self.workflow[target_id]["inputs"][target_param] = valfor conn in self.recipe.get("connections", []):if isinstance(conn.get("from"), str) and isinstance(conn.get("to"), str):from_name, from_output_idx = conn["from"].split(":", 1)to_name, to_input_name = conn["to"].split(":", 1)self.connect(from_name, int(from_output_idx), to_name, to_input_name)print("--- [Assembler] Applying dynamic injectors ---")processing_order = [key for key in self.injector_order if key in self.recipe]for chain_type in processing_order:injector_func = self.global_injectors.get(chain_type)if injector_func:for chain_key, chain_def in self.recipe.get(chain_type, {}).items():if chain_key in ui_values and ui_values[chain_key]:print(f" -> Injecting '{chain_type}' for '{chain_key}'...")injector_func(self, chain_def, ui_values[chain_key])print("--- [Assembler] Finished applying injectors ---")return self.workflow
core/workflow_assembler.py+116-111