Squashed 'vendor/dumas_minimax_h3_refmod/' content from commit 517e8c4
git-subtree-dir: vendor/dumas_minimax_h3_refmod git-subtree-split: 517e8c419a8584fbf69fceb487bc39d83e6a6ec8
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
export function openRefModLibrary(node, fetchEntries) {
|
||||
const previousFocus = document.activeElement;
|
||||
const dialog = document.createElement("dialog");
|
||||
dialog.className = "refmod-library";
|
||||
dialog.setAttribute("aria-label", "RefMod library");
|
||||
const style = document.createElement("style");
|
||||
style.textContent = `
|
||||
.refmod-library {box-sizing:border-box;width:min(1100px,96vw);height:90vh;max-height:90vh;padding:24px;border:1px solid #455266;border-radius:14px;background:#111820;color:#e8edf5;font:14px system-ui;}
|
||||
.refmod-library::backdrop {background:#000b}
|
||||
.refmod-library .layout {height:100%;display:flex;flex-direction:column;gap:16px;min-height:0}
|
||||
.refmod-library header {display:flex;gap:12px;align-items:center;justify-content:space-between}
|
||||
.refmod-library h2 {margin:0;font-size:23px}
|
||||
.refmod-library .controls {display:flex;gap:10px;flex-wrap:wrap}
|
||||
.refmod-library input,.refmod-library select,.refmod-library button {font:inherit;color:inherit;background:#222f3e;border:1px solid #455266;border-radius:7px;padding:9px 12px}
|
||||
.refmod-library input {flex:1;min-width:180px}
|
||||
.refmod-library button {cursor:pointer}
|
||||
.refmod-library button:focus-visible,.refmod-library input:focus-visible,.refmod-library select:focus-visible {outline:2px solid #ffb257;outline-offset:2px}
|
||||
.refmod-library .results {overflow:auto;min-height:0;flex:1;display:flex;flex-direction:column;gap:10px}
|
||||
.refmod-library article {padding:16px;border:1px solid #334254;border-radius:9px;background:#18222d;display:grid;grid-template-columns:1fr auto;gap:8px 18px}
|
||||
.refmod-library .description,.refmod-library .path {grid-column:1/-1;color:#afbed0;overflow-wrap:anywhere}
|
||||
.refmod-library .path {font-size:12px}
|
||||
.refmod-library .use {background:#ffb257;border-color:#ffb257;color:#171c22;font-weight:650}
|
||||
.refmod-library .status {color:#afbed0;min-height:20px}
|
||||
@media(max-width:600px){.refmod-library{padding:14px}.refmod-library article{grid-template-columns:1fr}.refmod-library header{flex-wrap:wrap}}
|
||||
`;
|
||||
const make = (tag, text, className) => {
|
||||
const el = document.createElement(tag);
|
||||
if (text !== undefined) el.textContent = text;
|
||||
if (className) el.className = className;
|
||||
return el;
|
||||
};
|
||||
const layout = make("div", undefined, "layout");
|
||||
const header = make("header");
|
||||
const close = make("button", "Close");
|
||||
header.append(make("h2", "RefMod library"), close);
|
||||
const controls = make("div", undefined, "controls");
|
||||
const search = make("input");
|
||||
search.placeholder = "Search name, folder or description";
|
||||
search.setAttribute("aria-label", "Search RefMods");
|
||||
const kind = make("select");
|
||||
kind.setAttribute("aria-label", "Reference type");
|
||||
for (const value of ["all", "image", "video", "audio", "bundle"]) {
|
||||
const option = make("option", value === "all" ? "All reference types" : value);
|
||||
option.value = value;
|
||||
kind.append(option);
|
||||
}
|
||||
const slot = make("select");
|
||||
slot.setAttribute("aria-label", "Loader slot");
|
||||
const widgets = (node.widgets || []).filter(w => /^mod_(?:[ab]_)?\d+$/.test(w.name));
|
||||
for (const widget of widgets) {
|
||||
const option = make("option", widget.name);
|
||||
option.value = widget.name;
|
||||
slot.append(option);
|
||||
}
|
||||
const refresh = make("button", "Refresh");
|
||||
controls.append(search, kind, slot, refresh);
|
||||
const status = make("div", "Loading library…", "status");
|
||||
status.setAttribute("role", "status");
|
||||
const results = make("div", undefined, "results");
|
||||
layout.append(header, controls, status, results);
|
||||
dialog.append(style, layout);
|
||||
document.body.append(dialog);
|
||||
let entries = [], disposed = false, controller;
|
||||
const cleanup = () => {
|
||||
if (disposed) return;
|
||||
disposed = true;
|
||||
controller?.abort();
|
||||
dialog.remove();
|
||||
previousFocus?.focus();
|
||||
};
|
||||
close.onclick = () => dialog.close();
|
||||
dialog.addEventListener("close", cleanup, {once:true});
|
||||
const render = () => {
|
||||
const query = search.value.trim().toLocaleLowerCase();
|
||||
const filtered = entries.filter(e => (kind.value === "all" || e.kind === kind.value)
|
||||
&& [e.name,e.concept,e.description].join(" ").toLocaleLowerCase().includes(query));
|
||||
status.textContent = `${filtered.length} of ${entries.length} RefMods · choose a loader slot, then Use`;
|
||||
results.replaceChildren();
|
||||
for (const entry of filtered) {
|
||||
const row = make("article");
|
||||
const title = make("strong", entry.name);
|
||||
const use = make("button", "Use", "use");
|
||||
use.disabled = widgets.length === 0;
|
||||
use.onclick = () => {
|
||||
const widget = widgets.find(w => w.name === slot.value);
|
||||
const linked = node.inputs?.find(i => i.name === slot.value)?.link;
|
||||
if (linked != null) {
|
||||
status.textContent = `${slot.value} is connected. Select an unconnected slot.`;
|
||||
return;
|
||||
}
|
||||
if (!widget) return;
|
||||
widget.value = entry.name;
|
||||
widget.callback?.(entry.name);
|
||||
node.setDirtyCanvas?.(true, true);
|
||||
dialog.close();
|
||||
};
|
||||
const tokens = entry.tokens == null ? "tokens available after loading" : `${entry.tokens.toLocaleString()} tokens / copy`;
|
||||
row.append(title, use, make("div", `${entry.kind} · ${entry.concept} · ${tokens}`, "description"),
|
||||
make("div", entry.description || "No description saved", "description"),
|
||||
make("div", entry.path, "path"));
|
||||
results.append(row);
|
||||
}
|
||||
if (!filtered.length) results.append(make("p", "No matching RefMods. Try another search or Refresh after adding files."));
|
||||
};
|
||||
const load = async () => {
|
||||
controller?.abort();
|
||||
controller = new AbortController();
|
||||
const signal = controller.signal;
|
||||
refresh.disabled = true;
|
||||
try {
|
||||
const data = await fetchEntries(signal);
|
||||
if (disposed || signal.aborted) return;
|
||||
entries = data;
|
||||
render();
|
||||
} catch (error) {
|
||||
if (!disposed && !signal.aborted) status.textContent = `Could not load library: ${error.message}`;
|
||||
} finally {
|
||||
if (!disposed && !signal.aborted) refresh.disabled = false;
|
||||
}
|
||||
};
|
||||
search.oninput = render;
|
||||
kind.onchange = render;
|
||||
refresh.onclick = load;
|
||||
dialog.showModal();
|
||||
search.focus();
|
||||
load();
|
||||
return dialog;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { app } from "../../scripts/app.js";
|
||||
|
||||
export function installRefModApply(nodeType) {
|
||||
const created = nodeType.prototype.onNodeCreated;
|
||||
nodeType.prototype.onNodeCreated = function(...args) {
|
||||
const result = created?.apply(this, args);
|
||||
// MatchType installs its callback on the instance during construction.
|
||||
const connections = this.onConnectionsChange;
|
||||
this.onConnectionsChange = function(...args) {
|
||||
const index = this.inputs?.findIndex(input => input.name === "conditioning") ?? -1;
|
||||
if (index >= 0) {
|
||||
const input = this.inputs[index];
|
||||
try {
|
||||
// Frontend 1.39.2 proxies MatchType slots, but NodeSlot.node
|
||||
// reads a #private field. That throws during canvas drawing.
|
||||
void input.node;
|
||||
} catch (error) {
|
||||
if (!(error instanceof TypeError)) throw error;
|
||||
this.inputs[index] = new input.constructor({...input}, this);
|
||||
if (args[4] === input) args[4] = this.inputs[index];
|
||||
}
|
||||
}
|
||||
return connections?.apply(this, args);
|
||||
};
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: "MiniMaxH3.ApplyCanvasCompatibility",
|
||||
beforeRegisterNodeDef(nodeType, nodeData) {
|
||||
if (nodeData.name === "MiniMaxH3RefModApply") installRefModApply(nodeType);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import { app } from "../../scripts/app.js";
|
||||
|
||||
const modes = { training: "Compressed Reference", encode: "Full Reference" };
|
||||
|
||||
function updateLabels(node) {
|
||||
const mode = node.widgets?.find(widget => widget.name === "mode");
|
||||
if (mode) {
|
||||
mode.options.values = Object.values(modes);
|
||||
mode.value = modes[mode.value] ?? mode.value;
|
||||
}
|
||||
const steps = node.widgets?.find(widget => widget.name === "identity");
|
||||
if (steps) steps.label = "Refinement Steps";
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: "MiniMaxH3.CreationLabels",
|
||||
beforeRegisterNodeDef(nodeType, nodeData) {
|
||||
if (!["MiniMaxH3RefModExtract", "MiniMaxH3RefModMasterExtract"].includes(nodeData.name)) return;
|
||||
for (const hook of ["onNodeCreated", "onConfigure"]) {
|
||||
const previous = nodeType.prototype[hook];
|
||||
nodeType.prototype[hook] = function(...args) {
|
||||
const result = previous?.apply(this, args);
|
||||
updateLabels(this);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import { app } from "../../scripts/app.js";
|
||||
import { api } from "../../scripts/api.js";
|
||||
import { openRefModLibrary } from "./library_dialog.js";
|
||||
|
||||
export function updateRefModChoices(node, entries) {
|
||||
const names = ["(none)", ...new Set(entries.map(entry => entry.name))];
|
||||
for (const widget of node.widgets || []) {
|
||||
if (!/^mod_(?:[ab]_)?\d+$/.test(widget.name)) continue;
|
||||
// Preserve selections, including missing files, for an explicit load
|
||||
// error instead of silently switching the subject after a refresh.
|
||||
widget.options.values = names.includes(widget.value) || !widget.value
|
||||
? [...names] : [...names, widget.value];
|
||||
}
|
||||
node.setDirtyCanvas?.(true, true);
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: "MiniMaxH3.RefModLibrary",
|
||||
beforeRegisterNodeDef(nodeType, nodeData) {
|
||||
if (!["MiniMaxH3RefModsLoader", "MiniMaxH3RefModsAxis"].includes(nodeData.name)) return;
|
||||
const removed = nodeType.prototype.onRemoved;
|
||||
nodeType.prototype.onRemoved = function(...args) {
|
||||
this._refmodRefresh?.abort();
|
||||
this._refmodDialog?.close();
|
||||
return removed?.apply(this, args);
|
||||
};
|
||||
const previous = nodeType.prototype.onNodeCreated;
|
||||
nodeType.prototype.onNodeCreated = function(...args) {
|
||||
const result = previous?.apply(this, args);
|
||||
const refresh = this.addWidget("button", "Refresh RefMods", null, async () => {
|
||||
this._refmodRefresh?.abort();
|
||||
const controller = new AbortController();
|
||||
this._refmodRefresh = controller;
|
||||
refresh.name = "Refreshing RefMods…";
|
||||
this.setDirtyCanvas?.(true, true);
|
||||
try {
|
||||
const response = await api.fetchApi("/refmods/library", {signal: controller.signal, cache: "no-store"});
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
const entries = await response.json();
|
||||
if (controller.signal.aborted) return;
|
||||
updateRefModChoices(this, entries);
|
||||
refresh.name = "Refresh RefMods";
|
||||
} catch (error) {
|
||||
if (controller.signal.aborted) return;
|
||||
refresh.name = "Refresh failed — click to retry";
|
||||
console.error("RefMod refresh failed:", error);
|
||||
} finally {
|
||||
if (!controller.signal.aborted) {
|
||||
this._refmodRefresh = null;
|
||||
this.setDirtyCanvas?.(true, true);
|
||||
}
|
||||
}
|
||||
}, {serialize:false});
|
||||
this.addWidget("button", "RefMod library", null, () => {
|
||||
this._refmodDialog?.close();
|
||||
this._refmodDialog = openRefModLibrary(this, async signal => {
|
||||
const response = await api.fetchApi("/refmods/library", {signal});
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
const entries = await response.json();
|
||||
if (!signal.aborted) updateRefModChoices(this, entries);
|
||||
return entries;
|
||||
});
|
||||
}, {serialize:false});
|
||||
return result;
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,158 @@
|
||||
import { app } from "../../scripts/app.js";
|
||||
|
||||
const SLOT = /^(?:mod_(?:[ab]_)?|strength_|copies_|value_|components_|visual_strength_|audio_strength_)(\d+)$/;
|
||||
const NONE = new Set(["", "None", "(none)"]);
|
||||
|
||||
export function installRefModSlots(nodeType) {
|
||||
const created = nodeType.prototype.onNodeCreated;
|
||||
nodeType.prototype.onNodeCreated = function(...args) {
|
||||
const result = created?.apply(this, args);
|
||||
setupSlots(this);
|
||||
return result;
|
||||
};
|
||||
// LiteGraph stores widget values by position. Keep the original schema
|
||||
// order for saving/loading even though the canvas groups them by slot.
|
||||
// ComfyUI frontend >= 1.5x saves via serializeFromStoreState() and never
|
||||
// calls serialize(), so that path has to be covered as well or workflows
|
||||
// get written in display order and shift on the next load.
|
||||
for (const method of ["serialize", "serializeFromStoreState", "configure"]) {
|
||||
const original = nodeType.prototype[method];
|
||||
nodeType.prototype[method] = function(...args) {
|
||||
const state = this._refmodSlots;
|
||||
if (!state) return original?.apply(this, args);
|
||||
// Nodes 2.0 exposes a stable array view: assigning widgets mutates
|
||||
// that same view, so retain a snapshot rather than its reference.
|
||||
const displayOrder = [...this.widgets];
|
||||
state.schemaOrderDepth++;
|
||||
this.widgets = [...state.originals, ...displayOrder.filter(w => !state.originals.includes(w))];
|
||||
try {
|
||||
return original?.apply(this, args);
|
||||
} finally {
|
||||
this.widgets = displayOrder;
|
||||
state.schemaOrderDepth--;
|
||||
if (method === "configure") state.sync();
|
||||
}
|
||||
};
|
||||
}
|
||||
const connections = nodeType.prototype.onConnectionsChange;
|
||||
nodeType.prototype.onConnectionsChange = function(...args) {
|
||||
const result = connections?.apply(this, args);
|
||||
this._refmodSlots?.sync();
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
function setupSlots(node) {
|
||||
if (node._refmodSlots) return;
|
||||
const originals = [...(node.widgets || [])];
|
||||
const groups = new Map();
|
||||
for (const widget of originals) {
|
||||
const match = widget.name.match(SLOT);
|
||||
if (!match) continue;
|
||||
const slot = Number(match[1]);
|
||||
if (!groups.has(slot)) groups.set(slot, []);
|
||||
groups.get(slot).push(widget);
|
||||
}
|
||||
const slots = [...groups.keys()].sort((a, b) => a - b);
|
||||
if (!slots.length) return;
|
||||
const defaults = new Map(originals.map(w => [w, w.value]));
|
||||
const hidden = new Map();
|
||||
const removers = new Map();
|
||||
node.properties ??= {};
|
||||
node.properties.refmod_visible_slots ??= [slots[0]];
|
||||
const linked = slot => node.inputs?.some(input => Number(input.name.match(SLOT)?.[1]) === slot && input.link != null);
|
||||
const used = slot => groups.get(slot).some(w => w.name.startsWith("mod_")
|
||||
? w.value != null && !NONE.has(String(w.value).trim())
|
||||
: w.value !== defaults.get(w));
|
||||
|
||||
function show(widget, visible) {
|
||||
if (!visible && !hidden.has(widget)) {
|
||||
hidden.set(widget, {computeSize: widget.computeSize,
|
||||
draw: widget.draw, mouse: widget.mouse, hidden: widget.hidden,
|
||||
optionsHidden: widget.options?.hidden});
|
||||
// converted-widget means a socket row in Nodes 2.0, not a hidden
|
||||
// widget. Its empty row was responsible for the large gaps.
|
||||
widget.options = {...widget.options, hidden: true};
|
||||
widget.computeSize = () => [0, -4];
|
||||
widget.draw = () => {};
|
||||
widget.mouse = () => false;
|
||||
widget.hidden = true;
|
||||
} else if (visible && hidden.has(widget)) {
|
||||
const saved = hidden.get(widget);
|
||||
widget.options = {...widget.options, hidden: saved.optionsHidden};
|
||||
widget.computeSize = saved.computeSize;
|
||||
widget.draw = saved.draw;
|
||||
widget.mouse = saved.mouse;
|
||||
widget.hidden = saved.hidden;
|
||||
hidden.delete(widget);
|
||||
}
|
||||
}
|
||||
|
||||
function sync() {
|
||||
// configure fires connection callbacks before assigning widget values.
|
||||
// Keep schema order until the entire positional restore has finished.
|
||||
if (node._refmodSlots.schemaOrderDepth) return;
|
||||
const requested = node.properties.refmod_visible_slots;
|
||||
// A saved workflow can bring this back as {0: 1, 1: 2, ...} rather than
|
||||
// an array, so accept either shape before falling back to slot one.
|
||||
const requestedSlots = Array.isArray(requested)
|
||||
? requested
|
||||
: requested && typeof requested === "object"
|
||||
? Object.values(requested)
|
||||
: [slots[0]];
|
||||
const visible = new Set(requestedSlots.filter(slot => groups.has(slot)));
|
||||
for (const slot of slots) if (used(slot) || linked(slot)) visible.add(slot);
|
||||
node.properties.refmod_visible_slots = [...visible].sort((a, b) => a - b);
|
||||
const grouped = [...groups.values()].flat();
|
||||
const extras = node.widgets.filter(w => !originals.includes(w) && ![...removers.values(), add].includes(w));
|
||||
const displayOrder = [];
|
||||
for (const slot of slots) {
|
||||
for (const widget of groups.get(slot)) {
|
||||
show(widget, visible.has(slot));
|
||||
displayOrder.push(widget);
|
||||
}
|
||||
const remove = removers.get(slot);
|
||||
remove.disabled = !!linked(slot);
|
||||
show(remove, visible.has(slot));
|
||||
displayOrder.push(remove);
|
||||
}
|
||||
show(add, visible.size < slots.length);
|
||||
displayOrder.push(add, ...originals.filter(w => !grouped.includes(w)), ...extras);
|
||||
node.widgets = displayOrder;
|
||||
node.setSize?.([node.size[0], node.computeSize()[1]]);
|
||||
node.setDirtyCanvas?.(true, true);
|
||||
}
|
||||
|
||||
for (const slot of slots) {
|
||||
removers.set(slot, node.addWidget("button", `Remove RefMod ${slot}`, null, () => {
|
||||
if (linked(slot)) return;
|
||||
for (const widget of groups.get(slot)) widget.value = defaults.get(widget);
|
||||
node.properties.refmod_visible_slots = node.properties.refmod_visible_slots.filter(i => i !== slot);
|
||||
sync();
|
||||
}, {serialize: false}));
|
||||
for (const widget of groups.get(slot)) {
|
||||
const callback = widget.callback;
|
||||
widget.callback = function(...args) {
|
||||
const result = callback?.apply(this, args);
|
||||
sync();
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}
|
||||
const add = node.addWidget("button", "+ Add RefMod", null, () => {
|
||||
const next = slots.find(slot => !node.properties.refmod_visible_slots.includes(slot));
|
||||
if (next != null) node.properties.refmod_visible_slots.push(next);
|
||||
sync();
|
||||
}, {serialize: false});
|
||||
node._refmodSlots = {originals, sync, schemaOrderDepth: 0};
|
||||
sync();
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: "MiniMaxH3.AdditiveSlots",
|
||||
beforeRegisterNodeDef(nodeType, nodeData) {
|
||||
if (["MiniMaxH3RefModsLoader", "MiniMaxH3RefModsAxis"].includes(nodeData.name)) {
|
||||
installRefModSlots(nodeType);
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user