"""Namespaced registrations for the complete vendored H3 upstream packs. The source is kept under ``vendor/`` with its licenses and upstream layout intact. This module is the only integration point: every upstream node gets a DUMAS-prefixed ComfyUI id so the original packs can remain installed without node-id collisions. """ from __future__ import annotations import importlib import logging import copy import dataclasses import types _PACKAGES = ( (".vendor.dumas_h3_continuum", "H3 Continuum", "DumasH3Continuum", "Dumas/MiniMax H3/Continuum"), (".vendor.dumas_minimax_h3_refmod", "MiniMax H3 RefMod", "DumasMiniMaxH3RefMod", "Dumas/MiniMax H3/RefMod"), (".vendor.dumas_h3_refmod_picker", "H3 RefMod Picker", "DumasH3RefModPicker", "Dumas/MiniMax H3/RefMod Picker"), ) def _load_pack(package_name): try: return importlib.import_module(package_name) except Exception: logging.getLogger("DumasNodes").exception( "Could not load vendored upstream pack %s", package_name, ) return None def _patch_schema(schema, *, node_id, category, display_name): """Return an upstream schema with DUMAS identity and category. Comfy's modern schema objects have changed from mutable dataclasses to immutable value objects across API revisions. Support both forms so the vendored nodes remain usable on the same Comfy versions as their source packs. """ changes = {"node_id": node_id, "category": category, "display_name": display_name} if dataclasses.is_dataclass(schema): try: return dataclasses.replace(schema, **changes) except (TypeError, ValueError): pass patched = copy.copy(schema) for name, value in changes.items(): try: setattr(patched, name, value) except (AttributeError, TypeError): try: object.__setattr__(patched, name, value) except (AttributeError, TypeError): raise TypeError( f"Cannot namespace Comfy schema {type(schema).__name__}.{name}" ) from None return patched def _namespace_class(node_class, *, node_id, category, display_name): """Wrap an upstream class without changing its execution implementation.""" define_schema = getattr(node_class, "define_schema", None) if define_schema is None: namespaced = types.new_class(node_class.__name__, (node_class,)) namespaced.CATEGORY = category namespaced.NODE_CATEGORY = category else: def _define_schema(cls): schema = node_class.define_schema() return _patch_schema( schema, node_id=node_id, category=category, display_name=display_name, ) namespaced = types.new_class(node_class.__name__, (node_class,)) namespaced.define_schema = classmethod(_define_schema) # Older Comfy builds still inspect these class attributes while # constructing the node search index. namespaced.CATEGORY = category namespaced.NODE_CATEGORY = category if node_id == "DumasH3Continuum_H3ContinuumSamplerV39": namespaced = _add_direct_refmod_input(namespaced) namespaced.__module__ = __name__ namespaced.__qualname__ = node_class.__qualname__ namespaced.__name__ = node_class.__name__ namespaced.__dumas_upstream_id__ = node_id return namespaced def _add_direct_refmod_input(node_class): """Add the DUMAS direct RefMod path to the vendored V3.9 sampler. Continuum's normal image-reference bundle remains untouched. When a RefMod bundle is connected, the same model-scoped OUTER_SAMPLE bridge used by the maintained RefMod pack is attached to a cloned model for this run. This keeps RefMods on the native H3 ref2va path and avoids global state. """ original_input_types = node_class.INPUT_TYPES @classmethod def INPUT_TYPES(cls): schema = original_input_types() schema = {name: dict(value) for name, value in schema.items()} required = dict(schema.get("required", {})) # V3.8 exposes both its newer resolution preset controls and the # original aspect/size controls. The DUMAS V3.9 node is intentionally # a clean manual-dimension node: width and height are the one source of # truth and can be converted to linked inputs in ComfyUI. for name in ("aspect", "preset", "custom_mp", "size_source"): required.pop(name, None) schema["required"] = required optional = dict(schema.get("optional", {})) # The bundled audio reference socket supersedes the two legacy scalar # audio inputs; exposing both makes it unclear which route is active. for name in ("reference_audio_1", "reference_audio_vae"): optional.pop(name, None) optional["refmods"] = ( "H3_REF_MODS", { "display_name": "RefMods (Optional)", "tooltip": ( "Optional MiniMax H3 RefMod bundle. RefMods are injected directly " "into this sampler for every generated chunk." ), }, ) optional["refmod_retention"] = ( "FLOAT", { "default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "display": "number", "display_name": "RefMod Retention", "tooltip": "Master RefMod strength. 1.0 preserves the selected bundle fully.", }, ) optional["refmod_max_total_tokens"] = ( "INT", { "default": 0, "min": 0, "max": 1048576, "step": 1, "display_name": "RefMod Token Budget", "tooltip": "0 uses the RefMod bundle's normal budget; otherwise enforce this maximum.", }, ) schema["optional"] = optional return schema def run(self, refmods=None, refmod_retention=1.0, refmod_max_total_tokens=0, **kwargs): # The clean DUMAS surface always uses the explicitly supplied width / # height pair. Keep the upstream runtime's legacy arguments internal. kwargs["size_source"] = "Manual" kwargs.pop("aspect", None) kwargs.pop("preset", None) kwargs.pop("custom_mp", None) if refmods: model = kwargs.get("model") if model is None: raise ValueError( "DUMAS H3 Continuum Sampler V3.9 requires a model when RefMods are connected." ) try: pack = importlib.import_module( f"{__package__}.vendor.dumas_minimax_h3_refmod.nodes" if __package__ else "vendor.dumas_minimax_h3_refmod.nodes" ) bridge = importlib.import_module( f"{__package__}.vendor.dumas_minimax_h3_refmod.continuum_bridge" if __package__ else "vendor.dumas_minimax_h3_refmod.continuum_bridge" ) blocks = pack._ref_blocks( refmods, refmod_retention, seed=-1, scramble_mode="shuffle", max_total_tokens=int(refmod_max_total_tokens), ) kwargs["model"] = bridge.attach(model, blocks, enabled=True) print( "[DUMAS H3 Continuum Sampler V3.9] direct RefMods active " f"({len(blocks)} block(s))" ) except Exception as exc: raise RuntimeError( "Unable to attach RefMods directly to DUMAS H3 Continuum " f"Sampler V3.9: {exc}" ) from exc return super(node_class, self).run(**kwargs) node_class.INPUT_TYPES = INPUT_TYPES node_class.run = run return node_class def _namespaced_mappings(): mappings = {} display = {} for relative_name, label, prefix, category in _PACKAGES: package_name = (f"{__package__}{relative_name}" if __package__ else relative_name.lstrip(".")) pack = _load_pack(package_name) if pack is None: continue source_mappings = getattr(pack, "NODE_CLASS_MAPPINGS", {}) or {} source_display = getattr(pack, "NODE_DISPLAY_NAME_MAPPINGS", {}) or {} for node_id, node_class in source_mappings.items(): dumas_id = f"{prefix}_{node_id}" original_name = source_display.get(node_id, node_id) dumas_name = f"DUMAS {label} ยท {original_name}" mappings[dumas_id] = _namespace_class( node_class, node_id=dumas_id, category=category, display_name=dumas_name, ) display[dumas_id] = dumas_name return mappings, display NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS = _namespaced_mappings()