Files
FranckyB-ComfyUI-Prompt-Man…/js/multi_lora_stacker_lm.js

971 lines
33 KiB
JavaScript

import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
import { T } from "./fbui/theme.js";
const NODE_CLASS = "MultiLoraStackerLM";
const LM_PROVIDER_CLASS = "Lora Stacker (LoraManager)";
const STYLE_ID = "pm-multi-lm-style";
const MIN_NODE_WIDTH = 320;
const MIN_NODE_HEIGHT = 320;
const DEFAULT_NODE_WIDTH = 600;
const DEFAULT_NODE_HEIGHT = 430;
const MIN_STACK_COUNT = 1;
const MAX_STACK_COUNT = 4;
const DEFAULT_STACK_COUNT = 2;
// Height constants mirroring LM's loras_widget_utils.js
const LM_LORA_ENTRY_H = 40;
const LM_HEADER_H = 32;
const LM_CONTAINER_PAD = 12;
const LM_EMPTY_H = 100;
const FAST_PREVIEW_DELAY_MS = 20;
// Per-column chrome: title(22) + search textarea(50) + col padding(16)
const COL_CHROME_H = 88;
let loraCodeListenerAttached = false;
let promptSerializationPatched = false;
const SLOT_DEFS = [
{ key: "model_a", label: "Model A", short: "A", state: "loras_state_a" },
{ key: "model_b", label: "Model B", short: "B", state: "loras_state_b" },
{ key: "model_c", label: "Model C", short: "C", state: "loras_state_c" },
{ key: "model_d", label: "Model D", short: "D", state: "loras_state_d" },
];
let lmBridgePromise = null;
// ---------------------------------------------------------------------------
// Styles
// ---------------------------------------------------------------------------
function ensureStyles() {
if (document.getElementById(STYLE_ID)) return;
// The suite's kit colours; the picked stack is outlined in its blue.
const panel = T.card;
const panelBorder = T.plateEdge;
const inputBg = T.card;
const inputBorder = T.plateEdge;
const textHeading = T.inkSoft;
const textPrimary = T.ink;
const textHint = T.faint;
const cardBg = T.plate;
const accentBorder = T.brand;
const accentSoft = T.brandSoft;
const style = document.createElement("style");
style.id = STYLE_ID;
style.textContent = `
.pm-multi-lm-root {
width: 100%;
height: 100%;
box-sizing: border-box;
padding: 6px 6px 6px 6px;
overflow: visible;
display: flex;
flex-direction: column;
position: relative;
}
.pm-multi-lm-root,
.pm-multi-lm-root * {
scrollbar-width: none;
-ms-overflow-style: none;
}
.pm-multi-lm-root::-webkit-scrollbar,
.pm-multi-lm-root *::-webkit-scrollbar {
width: 0;
height: 0;
display: none;
}
.pm-multi-lm-grid {
display: grid;
grid-template-columns: repeat(2, minmax(200px, 1fr));
gap: 8px;
flex: 1;
min-height: 0;
align-items: stretch;
margin-top: -16px;
}
.pm-multi-lm-topbar {
display: inline-flex;
align-items: center;
justify-content: flex-start;
position: absolute;
left: 8px;
top: -48px;
z-index: 2;
margin-top: 0;
margin-bottom: 0;
flex-shrink: 0;
}
.pm-multi-lm-stack-control {
display: inline-flex;
align-items: center;
gap: 8px;
border: 1px solid ${panelBorder};
border-radius: 8px;
background: ${panel};
padding: 5px 8px;
}
.pm-multi-lm-stack-label {
font-size: 14px;
font-weight: 700;
letter-spacing: 0.02em;
color: ${textHeading};
user-select: none;
}
.pm-multi-lm-stepper {
display: inline-flex;
align-items: center;
gap: 4px;
}
.pm-multi-lm-step-btn {
width: 24px;
height: 24px;
border: 1px solid ${inputBorder};
border-radius: 5px;
background: ${inputBg};
color: ${textPrimary};
font-size: 14px;
line-height: 1;
padding: 0;
cursor: pointer;
}
.pm-multi-lm-step-btn:hover {
border-color: ${textHeading};
}
.pm-multi-lm-step-btn:disabled {
opacity: 0.45;
cursor: default;
}
.pm-multi-lm-step-value {
min-width: 16px;
text-align: center;
font-size: 14px;
font-weight: 700;
line-height: 1.1;
color: hsl(0 0% 100%);
font-variant-numeric: tabular-nums;
user-select: none;
}
.pm-multi-lm-col {
border: 1px solid ${panelBorder};
border-radius: 8px;
background: ${panel};
padding: 8px;
min-width: 0;
display: flex;
flex-direction: column;
flex-shrink: 0;
min-height: 0;
height: 100%;
}
.pm-multi-lm-col.active-slot {
border-color: ${accentBorder};
box-shadow: inset 0 0 0 1px ${accentSoft};
}
.pm-multi-lm-col.active-slot .pm-multi-lm-title {
color: ${accentBorder};
}
.pm-multi-lm-title {
font-weight: 700;
font-size: 14px;
letter-spacing: 0.02em;
color: ${textHeading};
margin-bottom: 6px;
flex-shrink: 0;
}
.pm-multi-lm-search {
width: 100%;
min-height: 38px;
resize: vertical;
box-sizing: border-box;
border: 1px solid ${inputBorder};
border-radius: 6px;
padding: 6px 8px;
background: ${inputBg};
color: ${textPrimary};
margin-bottom: 6px;
font-size: 14px;
flex-shrink: 0;
}
.pm-multi-lm-search::placeholder {
color: ${textHint};
}
.pm-multi-lm-list-host {
flex: 1;
min-height: 0;
overflow-y: auto;
overflow-x: hidden;
scrollbar-width: none;
-ms-overflow-style: none;
}
/* Lora Manager's rows read its --lm-* colours: here they take the kit's,
as the rows of LoRA List+. */
.pm-multi-lm-list-host,
.pm-multi-lm-list-host .lm-widget-root,
.pm-multi-lm-list-host .lm-loras-container {
--lm-surface: ${T.plate};
--lm-surface-raised: ${T.fill};
--lm-surface-hover: ${T.fillHover};
--lm-surface-selected: ${T.lit};
--lm-border-soft: ${T.plateEdge};
--lm-border: ${T.switchOff};
--lm-fg: ${T.ink};
--lm-fg-muted: ${T.label};
--lm-fg-subtle: ${T.faint};
--lm-accent: ${T.brand};
--lm-accent-border: ${T.brand};
--lm-accent-soft: ${T.brandSoft};
--lm-accent-text: ${T.brandInk};
--lm-on-accent: ${T.onBrand};
}
.pm-multi-lm-list-host .lm-loras-container {
height: 100%;
max-height: none;
background: ${cardBg};
scrollbar-width: none;
-ms-overflow-style: none;
}
.pm-multi-lm-list-host::-webkit-scrollbar,
.pm-multi-lm-list-host .lm-loras-container::-webkit-scrollbar {
width: 0;
height: 0;
display: none;
}
`;
document.head.appendChild(style);
}
// ---------------------------------------------------------------------------
// LM bridge loader
// ---------------------------------------------------------------------------
async function loadLmBridge() {
if (lmBridgePromise) return lmBridgePromise;
lmBridgePromise = Promise.all([
import("/extensions/ComfyUI-Lora-Manager/autocomplete.js"),
import("/extensions/ComfyUI-Lora-Manager/loras_widget.js"),
import("/extensions/ComfyUI-Lora-Manager/utils.js"),
import("/extensions/ComfyUI-Lora-Manager/lora_syntax_utils.js"),
import("/extensions/ComfyUI-Lora-Manager/preview_tooltip.js"),
]).then(([autocompleteMod, lorasWidgetMod, utilsMod, syntaxMod, previewTooltipMod]) => ({
AutoComplete: autocompleteMod.AutoComplete,
addLorasWidget: lorasWidgetMod.addLorasWidget,
mergeLoras: utilsMod.mergeLoras,
applyLoraValuesToText: syntaxMod.applyLoraValuesToText,
PreviewTooltip: previewTooltipMod.PreviewTooltip,
})).catch((err) => {
console.warn("[PromptManager] Failed to load Lora-Manager bridge modules", err);
lmBridgePromise = null;
return null;
});
return lmBridgePromise;
}
// ---------------------------------------------------------------------------
// Widget helpers
// ---------------------------------------------------------------------------
function getWidgetByName(node, name) {
if (!node || !Array.isArray(node.widgets)) return null;
return node.widgets.find((w) => w && w.name === name) ?? null;
}
function ensureStateWidget(node, name) {
let widget = getWidgetByName(node, name);
if (widget) return widget;
if (!node || typeof node.addWidget !== "function") return null;
return node.addWidget("text", name, "[]", null, { multiline: true });
}
function hideWidget(widget) {
if (!widget || widget.__pm_hidden) return;
widget.__pm_hidden = true;
// Keep original widget type (STRING) so graphToPrompt includes state values.
widget.hidden = true;
widget.computeSize = () => [0, -4];
widget.draw = function () {};
if (widget.element) widget.element.style.display = "none";
}
function hideStateWidgets(node) {
if (!node || !Array.isArray(node.widgets)) return;
for (const widget of node.widgets) {
if (!widget || widget.name === "multi_lora_ui") continue;
hideWidget(widget);
}
}
function parseLorasState(value) {
if (Array.isArray(value)) return value;
if (value && typeof value === "object" && Array.isArray(value.__value__)) {
return value.__value__;
}
if (value && typeof value === "object" && typeof value.__value__ === "string") {
try {
const parsed = JSON.parse(value.__value__);
return Array.isArray(parsed) ? parsed : [];
} catch (_e) {
return [];
}
}
if (typeof value !== "string") return [];
const raw = value.trim();
if (!raw || raw === "[]") return [];
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed : [];
} catch (_e) {
return [];
}
}
function writeLorasState(widget, listValue) {
if (!widget) return;
widget.value = JSON.stringify(Array.isArray(listValue) ? listValue : []);
}
function getSlotByKey(node, slotKey) {
if (!node || !Array.isArray(node.__pmMultiLmSlots)) return null;
return node.__pmMultiLmSlots.find((s) => s && s.key === slotKey) ?? null;
}
function clampStackCount(value) {
const n = Number(value);
if (!Number.isFinite(n)) return DEFAULT_STACK_COUNT;
return Math.max(MIN_STACK_COUNT, Math.min(MAX_STACK_COUNT, Math.round(n)));
}
function getStackCount(node) {
const raw = node?.properties?.pmLoraStackCount;
return clampStackCount(raw ?? DEFAULT_STACK_COUNT);
}
function getVisibleSlotKeys(node) {
const count = getStackCount(node);
return SLOT_DEFS.slice(0, count).map((s) => s.key);
}
function syncStackCountWidget(node, count) {
const widget = getWidgetByName(node, "stack_count");
if (widget) widget.value = count;
}
function ensureActiveSlotVisible(node) {
if (!node || !node.properties) return;
const visible = getVisibleSlotKeys(node);
if (visible.length === 0) return;
if (!visible.includes(node.properties.pmActiveSlot)) {
node.properties.pmActiveSlot = visible[0];
}
}
function updateStackControlUi(node) {
const refs = node?.__pmStackControl;
if (!refs) return;
const count = getStackCount(node);
refs.value.textContent = String(count);
refs.dec.disabled = count <= MIN_STACK_COUNT;
refs.inc.disabled = count >= MAX_STACK_COUNT;
}
function applyVisibleStackCount(node) {
const slots = node?.__pmMultiLmSlots;
const grid = node?.__pmMultiLmGrid;
if (!Array.isArray(slots) || !grid) return;
const visible = new Set(getVisibleSlotKeys(node));
const visibleCount = visible.size || DEFAULT_STACK_COUNT;
grid.style.gridTemplateColumns = `repeat(${visibleCount}, minmax(200px, 1fr))`;
for (const slot of slots) {
if (!slot?.col) continue;
slot.col.style.display = visible.has(slot.key) ? "flex" : "none";
}
}
function setStackCount(node, value) {
if (!node) return;
if (!node.properties) node.properties = {};
node.properties.pmLoraStackCount = clampStackCount(value);
ensureActiveSlotVisible(node);
applyVisibleStackCount(node);
updateStackControlUi(node);
syncStackCountWidget(node, node.properties.pmLoraStackCount);
updateActiveColumnHighlight(node);
notifyHeightChange(node);
applyNodeSizeConstraints(node, false, true);
}
// ---------------------------------------------------------------------------
// Apply incoming lora code to a slot
// ---------------------------------------------------------------------------
function applyIncomingLoraCode(node, slotKey, code, mode) {
if (!node || typeof code !== "string") return;
const slot = getSlotByKey(node, slotKey);
if (!slot || !slot.searchInput) return;
const current = String(slot.searchInput.value || "");
slot.searchInput.value = (mode === "replace" || !current) ? code : `${current}\n${code}`;
slot.searchInput.dispatchEvent(new Event("input", { bubbles: true }));
}
// ---------------------------------------------------------------------------
// lora_code_update listener (attached once, synchronously)
// ---------------------------------------------------------------------------
function attachLoraCodeListener() {
if (loraCodeListenerAttached) return;
loraCodeListenerAttached = true;
api.addEventListener("lora_code_update", (event) => {
const detail = event?.detail;
if (!detail) return;
const nodeId = Number(detail.id);
if (!Number.isFinite(nodeId) || nodeId < 0) return;
const loraCode = typeof detail.lora_code === "string" ? detail.lora_code : "";
const mode = typeof detail.mode === "string" ? detail.mode : "append";
// Find our node by integer ID
const nodes = app.graph?._nodes;
if (!Array.isArray(nodes)) return;
const targetNode = nodes.find((n) => Number(n.id) === nodeId && n.type === NODE_CLASS);
if (!targetNode) return;
// Route to the currently-selected active slot
const activeSlot = targetNode.properties?.pmActiveSlot ?? "model_a";
applyIncomingLoraCode(targetNode, activeSlot, loraCode, mode);
});
}
// ---------------------------------------------------------------------------
// Prompt serialization shim:
// Keep LM comfyClass for manager integration, but serialize this node as its
// real backend class so ComfyUI validates against MultiLoraStackerLM inputs.
// ---------------------------------------------------------------------------
function attachPromptSerializationShim() {
if (promptSerializationPatched) return;
if (!app || typeof app.graphToPrompt !== "function") return;
promptSerializationPatched = true;
const originalGraphToPrompt = app.graphToPrompt.bind(app);
app.graphToPrompt = async function (...args) {
const swapped = [];
const nodes = app.graph?._nodes;
if (Array.isArray(nodes)) {
for (const node of nodes) {
if (!node || node.type !== NODE_CLASS) continue;
if (node.comfyClass !== LM_PROVIDER_CLASS) continue;
swapped.push(node);
node.comfyClass = NODE_CLASS;
}
}
try {
return await originalGraphToPrompt(...args);
} finally {
for (const node of swapped) {
node.comfyClass = LM_PROVIDER_CLASS;
}
}
};
}
// ---------------------------------------------------------------------------
// Active-slot selection (click a section to target LM sends)
// ---------------------------------------------------------------------------
function setActiveSlot(node, slotKey) {
if (!node) return;
if (!node.properties) node.properties = {};
node.properties.pmActiveSlot = slotKey;
updateActiveColumnHighlight(node);
}
function updateActiveColumnHighlight(node) {
if (!Array.isArray(node.__pmMultiLmSlots)) return;
ensureActiveSlotVisible(node);
const activeSlot = node.properties?.pmActiveSlot ?? "model_a";
const visible = new Set(getVisibleSlotKeys(node));
for (const slot of node.__pmMultiLmSlots) {
if (slot?.col) {
const isActive = slot.key === activeSlot && visible.has(slot.key);
slot.col.classList.toggle("active-slot", isActive);
}
}
}
// ---------------------------------------------------------------------------
// Embedded loras widget via fakeNode pattern
// ---------------------------------------------------------------------------
function createEmbeddedLorasWidget(node, lm, widgetName, initialValue, onChange) {
const host = document.createElement("div");
host.className = "pm-multi-lm-list-host";
const fakeNode = {
addDOMWidget(name, type, container, options) {
return {
name,
type,
callback: null,
element: container,
get value() {
return typeof options?.getValue === "function" ? options.getValue() : [];
},
set value(v) {
if (typeof options?.setValue === "function") options.setValue(v);
},
};
},
setDirtyCanvas(fg, bg) { node?.setDirtyCanvas?.(fg, bg); },
};
const out = lm.addLorasWidget(fakeNode, widgetName, {}, onChange);
if (out?.widget?.element) host.appendChild(out.widget.element);
if (out?.widget && Array.isArray(initialValue) && initialValue.length > 0) {
out.widget.value = initialValue;
}
const fastPreviewCleanup = attachFastPreviewHandlers(host, lm);
return { host, widget: out?.widget ?? null, fastPreviewCleanup };
}
function attachFastPreviewHandlers(host, lm) {
if (!host || typeof lm?.PreviewTooltip !== "function") {
return () => {};
}
const tooltip = new lm.PreviewTooltip({ modelType: "loras" });
const processed = new WeakSet();
const bindNameEl = (nameEl) => {
if (!nameEl || processed.has(nameEl)) return;
processed.add(nameEl);
// Replace LM's delayed listeners on this element with a faster local one.
const replacement = nameEl.cloneNode(true);
const triggerEl = nameEl.closest(".lm-lora-entry") || nameEl.parentElement || replacement;
let previewTimer = null;
const clearPreviewTimer = () => {
if (previewTimer) {
clearTimeout(previewTimer);
previewTimer = null;
}
};
triggerEl.addEventListener("mouseenter", () => {
clearPreviewTimer();
previewTimer = setTimeout(async () => {
previewTimer = null;
const name = String(replacement.textContent || "").trim();
if (!name) return;
const rect = triggerEl.getBoundingClientRect();
try {
await tooltip.show(name, rect.right, rect.top);
} catch (_e) {
}
}, FAST_PREVIEW_DELAY_MS);
});
triggerEl.addEventListener("mouseleave", () => {
clearPreviewTimer();
tooltip.hide();
});
nameEl.replaceWith(replacement);
processed.add(replacement);
};
const bindAll = () => {
const nameEls = host.querySelectorAll(".lm-lora-name");
for (const nameEl of nameEls) bindNameEl(nameEl);
};
bindAll();
const observer = new MutationObserver(() => {
bindAll();
});
observer.observe(host, { childList: true, subtree: true });
return () => {
observer.disconnect();
tooltip.hide();
tooltip.cleanup();
};
}
// ---------------------------------------------------------------------------
// Height management — mirrors LM's updateWidgetHeight approach:
// set CSS vars on the root element so ComfyUI's layout system resizes the node,
// never calling node.setSize() (which would prevent user resize).
// ---------------------------------------------------------------------------
function computeContentHeight(node) {
let maxListH = LM_EMPTY_H;
const visible = new Set(getVisibleSlotKeys(node));
if (Array.isArray(node.__pmMultiLmSlots)) {
for (const slot of node.__pmMultiLmSlots) {
if (!visible.has(slot.key)) continue;
const count = Array.isArray(slot.lorasWidget?.value) ? slot.lorasWidget.value.length : 0;
const h = count === 0
? LM_EMPTY_H
: LM_CONTAINER_PAD + LM_HEADER_H + Math.min(count, 12) * LM_LORA_ENTRY_H;
if (h > maxListH) maxListH = h;
}
}
return COL_CHROME_H + maxListH + 56;
}
function notifyHeightChange(node) {
const root = node?.__pmMultiLmRoot;
if (!root) return;
const h = computeContentHeight(node);
root.style.setProperty('--comfy-widget-min-height', `${h}px`);
root.style.setProperty('--comfy-widget-height', `${h}px`);
applyNodeSizeConstraints(node);
setTimeout(() => { node?.setDirtyCanvas?.(true, true); }, 10);
}
function applyNodeSizeConstraints(node, useDefaultSize = false, forceWidth = false) {
if (!node || node.__pmApplyingSizeConstraint) return;
const currentW = Number(node?.size?.[0]) || 0;
const currentH = Number(node?.size?.[1]) || 0;
const minW = MIN_NODE_WIDTH * getStackCount(node);
const targetW = forceWidth
? minW
: (useDefaultSize
? Math.max(DEFAULT_NODE_WIDTH, minW, currentW)
: Math.max(minW, currentW));
const targetH = useDefaultSize
? Math.max(DEFAULT_NODE_HEIGHT, MIN_NODE_HEIGHT, currentH)
: Math.max(MIN_NODE_HEIGHT, currentH);
if (targetW === currentW && targetH === currentH) return;
node.__pmApplyingSizeConstraint = true;
try {
node.setSize([targetW, targetH]);
} finally {
node.__pmApplyingSizeConstraint = false;
}
}
// ---------------------------------------------------------------------------
// Per-slot column builder
// ---------------------------------------------------------------------------
function buildSlotColumn(node, lm, slotDef) {
const stateWidget = ensureStateWidget(node, slotDef.state);
hideWidget(stateWidget);
const col = document.createElement("div");
col.className = "pm-multi-lm-col";
col.title = `Click to make ${slotDef.label} the LoRA Manager target`;
col.addEventListener("pointerdown", () => setActiveSlot(node, slotDef.key));
const title = document.createElement("div");
title.className = "pm-multi-lm-title";
title.textContent = slotDef.label;
const searchInput = document.createElement("textarea");
searchInput.className = "pm-multi-lm-search";
searchInput.placeholder = "Search LoRAs to add\u2026";
searchInput.rows = 1;
searchInput.addEventListener("focus", () => setActiveSlot(node, slotDef.key));
// Attach LM autocomplete to the search textarea
new lm.AutoComplete(searchInput, "loras", { showPreview: false });
const initialState = parseLorasState(stateWidget?.value ?? "[]");
let isSyncing = false;
const embedded = createEmbeddedLorasWidget(
node, lm, `${slotDef.key}_loras`, initialState,
(value) => {
if (isSyncing) return;
isSyncing = true;
try {
const safe = Array.isArray(value) ? value : [];
writeLorasState(stateWidget, safe);
const nextText = lm.applyLoraValuesToText(searchInput.value || "", safe);
if (searchInput.value !== nextText) searchInput.value = nextText;
} finally {
isSyncing = false;
notifyHeightChange(node);
}
},
);
// When user types or autocomplete fires, merge into the loras list
searchInput.addEventListener("input", () => {
if (isSyncing) return;
isSyncing = true;
try {
const existing = Array.isArray(embedded.widget?.value) ? embedded.widget.value : [];
const merged = lm.mergeLoras(searchInput.value || "", existing);
if (embedded.widget) embedded.widget.value = merged;
writeLorasState(stateWidget, merged);
} finally {
isSyncing = false;
notifyHeightChange(node);
}
});
col.appendChild(title);
col.appendChild(searchInput);
col.appendChild(embedded.host);
embedded.host.addEventListener("pointerdown", () => setActiveSlot(node, slotDef.key));
return {
key: slotDef.key,
stateWidget,
searchInput,
lorasWidget: embedded.widget,
fastPreviewCleanup: embedded.fastPreviewCleanup,
col,
};
}
// ---------------------------------------------------------------------------
// Restore widget state from persisted JSON
// ---------------------------------------------------------------------------
function refreshFromStoredValues(node) {
if (!node?.__pmMultiLmSlots || !node.__pmMultiLmBridge) return;
for (const slot of node.__pmMultiLmSlots) {
const state = parseLorasState(slot.stateWidget?.value ?? "[]");
// Restore from persisted JSON directly. Do NOT call mergeLoras here:
// mergeLoras requires syntax in text input, and would drop entries
// during tab-switch/workflow-load when textarea is initially empty.
if (slot.lorasWidget) slot.lorasWidget.value = state;
writeLorasState(slot.stateWidget, state);
// Keep syntax textarea consistent with restored list.
const nextText = node.__pmMultiLmBridge.applyLoraValuesToText(slot.searchInput.value || "", state);
if (slot.searchInput.value !== nextText) slot.searchInput.value = nextText;
}
notifyHeightChange(node);
}
// ---------------------------------------------------------------------------
// Main async UI setup
// ---------------------------------------------------------------------------
async function setupNodeUi(node) {
if (!node || node.__pmMultiLmReady) return;
ensureStyles();
const lm = await loadLmBridge();
if (!lm || node.__pmMultiLmReady) return;
hideStateWidgets(node);
const root = document.createElement("div");
root.className = "pm-multi-lm-root";
// Top-left custom stack count control (1..4)
const topbar = document.createElement("div");
topbar.className = "pm-multi-lm-topbar";
const stackControl = document.createElement("div");
stackControl.className = "pm-multi-lm-stack-control";
const stackLabel = document.createElement("div");
stackLabel.className = "pm-multi-lm-stack-label";
stackLabel.textContent = "Lora Stack";
const stepper = document.createElement("div");
stepper.className = "pm-multi-lm-stepper";
const decBtn = document.createElement("button");
decBtn.className = "pm-multi-lm-step-btn";
decBtn.type = "button";
decBtn.textContent = "<";
const valueEl = document.createElement("div");
valueEl.className = "pm-multi-lm-step-value";
valueEl.textContent = String(getStackCount(node));
const incBtn = document.createElement("button");
incBtn.className = "pm-multi-lm-step-btn";
incBtn.type = "button";
incBtn.textContent = ">";
decBtn.addEventListener("click", () => setStackCount(node, getStackCount(node) - 1));
incBtn.addEventListener("click", () => setStackCount(node, getStackCount(node) + 1));
stepper.appendChild(decBtn);
stepper.appendChild(valueEl);
stepper.appendChild(incBtn);
stackControl.appendChild(stackLabel);
stackControl.appendChild(stepper);
topbar.appendChild(stackControl);
root.appendChild(topbar);
// 4-column LoRA grid
const grid = document.createElement("div");
grid.className = "pm-multi-lm-grid";
root.appendChild(grid);
const slots = SLOT_DEFS.map((slotDef) => {
const slot = buildSlotColumn(node, lm, slotDef);
grid.appendChild(slot.col);
return slot;
});
const uiWidget = node.addDOMWidget("multi_lora_ui", "div", root, {
serialize: false,
hideOnZoom: false,
getMinHeight: () => computeContentHeight(node),
getHeight: () => "100%",
});
node.__pmMultiLmBridge = lm;
node.__pmMultiLmSlots = slots;
node.__pmMultiLmGrid = grid;
node.__pmStackControl = { dec: decBtn, inc: incBtn, value: valueEl };
node.__pmMultiLmRoot = root;
node.__pmMultiLmReady = true;
node.__pmMultiLmRefresh = () => {
refreshFromStoredValues(node);
applyVisibleStackCount(node);
updateStackControlUi(node);
syncStackCountWidget(node, getStackCount(node));
updateActiveColumnHighlight(node);
};
// comfyClass already set synchronously in onNodeCreated — no registry tricks needed
// Restore any lora data that onConfigure may have written before async completed
refreshFromStoredValues(node);
hideStateWidgets(node);
applyVisibleStackCount(node);
updateStackControlUi(node);
updateActiveColumnHighlight(node);
notifyHeightChange(node);
applyNodeSizeConstraints(node);
}
// ---------------------------------------------------------------------------
// Extension registration
// ---------------------------------------------------------------------------
app.registerExtension({
name: "PromptManager.MultiLoraStackerLM",
async beforeRegisterNodeDef(nodeType) {
if (nodeType.comfyClass !== NODE_CLASS) return;
// Attach the send-to-node listener once, immediately (synchronous)
attachLoraCodeListener();
// Ensure prompt uses backend class while keeping LM comfyClass at runtime.
attachPromptSerializationShim();
// ── onNodeCreated ──────────────────────────────────────────────────
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
const result = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined;
this.serialize_widgets = true;
// Set comfyClass SYNCHRONOUSLY so LM WorkflowRegistry includes us
// on the next lora_registry_refresh sweep
this.comfyClass = LM_PROVIDER_CLASS;
if (!this.properties) this.properties = {};
if (!this.properties.pmActiveSlot) this.properties.pmActiveSlot = "model_a";
if (!Number.isFinite(Number(this.properties.pmLoraStackCount))) {
this.properties.pmLoraStackCount = DEFAULT_STACK_COUNT;
} else {
this.properties.pmLoraStackCount = clampStackCount(this.properties.pmLoraStackCount);
}
ensureActiveSlotVisible(this);
syncStackCountWidget(this, this.properties.pmLoraStackCount);
applyNodeSizeConstraints(this, true);
void setupNodeUi(this);
return result;
};
// ── onConfigure (workflow load / tab switch) ──────────────────────
const onConfigure = nodeType.prototype.onConfigure;
nodeType.prototype.onConfigure = function (config) {
const result = onConfigure ? onConfigure.apply(this, arguments) : undefined;
// Re-assert comfyClass in case ComfyUI reset it during configure
this.comfyClass = LM_PROVIDER_CLASS;
hideStateWidgets(this);
if (!Number.isFinite(Number(this.properties?.pmLoraStackCount))) {
if (!this.properties) this.properties = {};
this.properties.pmLoraStackCount = DEFAULT_STACK_COUNT;
} else {
this.properties.pmLoraStackCount = clampStackCount(this.properties.pmLoraStackCount);
}
ensureActiveSlotVisible(this);
syncStackCountWidget(this, this.properties.pmLoraStackCount);
if (typeof this.__pmMultiLmRefresh === "function") {
this.__pmMultiLmRefresh();
}
applyNodeSizeConstraints(this);
return result;
};
const onModeChange = nodeType.prototype.onModeChange;
nodeType.prototype.onModeChange = function () {
const result = onModeChange ? onModeChange.apply(this, arguments) : undefined;
hideStateWidgets(this);
if (typeof this.__pmMultiLmRefresh === "function") {
this.__pmMultiLmRefresh();
}
applyNodeSizeConstraints(this);
return result;
};
const onResize = nodeType.prototype.onResize;
nodeType.prototype.onResize = function () {
const result = onResize ? onResize.apply(this, arguments) : undefined;
applyNodeSizeConstraints(this);
return result;
};
// ── onRemoved ──────────────────────────────────────────────────────
const onRemoved = nodeType.prototype.onRemoved;
nodeType.prototype.onRemoved = function () {
if (Array.isArray(this.__pmMultiLmSlots)) {
for (const slot of this.__pmMultiLmSlots) {
try {
slot?.fastPreviewCleanup?.();
} catch (_e) {
}
}
}
this.__pmMultiLmSlots = null;
this.__pmMultiLmRoot = null;
this.__pmMultiLmBridge = null;
this.__pmMultiLmGrid = null;
this.__pmStackControl = null;
this.__pmMultiLmReady = false;
this.__pmMultiLmRefresh = null;
return onRemoved ? onRemoved.apply(this, arguments) : undefined;
};
},
});