"""Model-scoped RefMod injection before ComfyUI prepares each sampling run. Works with Continuum and standard H3 samplers through the public model patcher OUTER_SAMPLE hook. No process-global bundle or third-party function patch. """ from comfy.patcher_extension import WrappersMP BRIDGE_KEY = "minimax_h3_refmod_bridge" def attach(model, blocks, enabled=True, chunk_blocks=None): model = model.clone() model.remove_wrappers_with_key(WrappersMP.OUTER_SAMPLE, BRIDGE_KEY) if not enabled or not blocks: return model refs = tuple(dict(b) for b in blocks) per_chunk = tuple( tuple(dict(b) for b in chunk) for chunk in (chunk_blocks or ()) ) call_index = 0 def inject(executor, *args, **kwargs): nonlocal call_index # Continuum samples physical groups in sequence order. The DUMAS # prompt builder supplies one block set per logical chunk; use that # set for the corresponding sampling call. A normal RefMod bundle # has no per-chunk map and continues to apply to every chunk. selected = per_chunk[call_index] if call_index < len(per_chunk) else refs call_index += 1 guider = executor.class_obj original = guider.conds guider.conds = { key: [dict(entry, minimax_refs=list(entry.get("minimax_refs") or []) + [dict(ref) for ref in selected]) for entry in entries] for key, entries in original.items() } try: return executor(*args, **kwargs) finally: guider.conds = original model.add_wrapper_with_key(WrappersMP.OUTER_SAMPLE, BRIDGE_KEY, inject) return model def disarm(): # Preserve the old output-node ID in saved workflows. There is no longer # global state to disarm; enable=False on the model line removes the hook. return "Bridge is model-scoped. Bypass it or set enable=False on the model line."