File size: 8,888 Bytes
2d31d6d
3b8a7df
 
2d31d6d
 
 
 
 
3b8a7df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d31d6d
 
 
 
 
 
 
3b8a7df
 
2d31d6d
 
 
 
 
 
 
 
3b8a7df
 
 
 
 
 
2d31d6d
 
 
3b8a7df
2d31d6d
 
 
7fb97e6
 
 
 
3b8a7df
 
7fb97e6
 
 
 
 
 
 
2d31d6d
 
 
 
 
 
 
 
 
3b8a7df
2d31d6d
 
3b8a7df
 
 
 
 
 
 
2d31d6d
3b8a7df
2d31d6d
 
 
3b8a7df
2d31d6d
 
 
3b8a7df
 
2d31d6d
3b8a7df
2d31d6d
7fb97e6
2d31d6d
 
 
3b8a7df
 
2d31d6d
 
3b8a7df
2d31d6d
 
 
3b8a7df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d31d6d
 
 
3b8a7df
 
 
2d31d6d
3b8a7df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d31d6d
 
 
 
 
 
3b8a7df
 
2d31d6d
3b8a7df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import re
import yaml
from copy import deepcopy
from comfy_integration.nodes import NODE_CLASS_MAPPINGS
from chain_injectors import discover_injectors, get_registered_features
from 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 text


def _merge_recipe_dict(target: dict, source: dict) -> dict:
    for key, val in (source or {}).items():
        if key == "imports" or val is None:
            continue
        if key == "connections" and isinstance(val, list):
            target.setdefault("connections", []).extend(val)
        elif isinstance(val, dict):
            target.setdefault(key, {}).update(val)
        else:
            target[key] = val
    return target


def _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)
        self.dynamic_values = dynamic_values or {}
        self.node_counter = 0
        self.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):
        self.global_injectors = discover_injectors()
        registered_features = get_registered_features()

        order = []
        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_features
                    else 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:
                continue
            feature = chain_key[8:-7] if (chain_key.startswith("dynamic_") and chain_key.endswith("_chains")) else chain_key
            alias = f"dynamic_{feature}_chains"
            self.global_injectors.setdefault(alias, func)
            self.global_injectors.setdefault(feature, func)
            if alias not in order:
                order.append(alias)
            if feature not in order:
                order.append(feature)

        self.injector_order = order

    def _get_unique_id(self):
        self.node_counter += 1
        return str(self.node_counter)

    def _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"] = title
        for k, v in (inputs or {}).items():
            if k in node_data["inputs"]:
                node_data["inputs"][k] = v
        node_id = self._get_unique_id()
        self.workflow[node_id] = node_data
        if alias:
            self.node_map[alias] = node_id
        return node_id

    def 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_values
        self.node_counter = 0
        self.workflow = {}
        self.node_map = {}

        for name, details in self.recipe.get("nodes", {}).items():
            class_type = details.get("class_type")
            if not class_type:
                continue
            if match := _PLACEHOLDER_RE.search(class_type):
                class_type = ui_values.get(match.group(1))
                if not class_type:
                    continue
            self.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] = val

        for 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