Release V2.1.5 UI serialization stability fix

This commit is contained in:
ukr8b3g-cmyk
2026-08-10 19:56:07 +09:00
parent adaca9789b
commit 28b5b751a6
20 changed files with 508 additions and 353 deletions
+253 -76
View File
@@ -4,8 +4,23 @@ const NODE_NAME = "H3ContinuumSamplerV2";
const PREVIEW_SETTING = "H3Continuum.ShowLatentPreview";
const PROMPT_INPUT = /^clip_(\d+)_prompt$/;
const SEQUENCE_PROMPT_INPUT = "sequence_prompt";
const OVERRIDE_PROPERTY = "h3_show_clip_overrides";
const CLIP_OVERRIDES_PROPERTY = "h3_show_clip_overrides";
const ADVANCED_PROPERTY = "h3_show_advanced";
const ADVANCED_INPUTS = new Set(["last_frame", "session", "initial_state", "prompt_plan"]);
const ADVANCED_OUTPUTS = new Set(["last_state", "session", "report"]);
const MAX_CHUNKS = 16;
const VISIBILITY_STYLE_ID = "h3-continuum-v215-slot-visibility";
const ADVANCED_WIDGETS = new Set([
"control_after_generate",
"audio_continuity",
"exact_total_duration",
"diagnostics",
"reroll_from_chunk",
"reroll_nonce",
"strict_compatibility",
"debug",
"show_preview",
]);
const LEGACY_PROMPT_FORMATS = new Map([
["Fixed — one prompt", "Fixed"],
["List — split with ---", "List"],
@@ -14,29 +29,66 @@ const LEGACY_PROMPT_FORMATS = new Map([
let showLatentPreview = true;
function hideWidget(widget) {
function setWidgetCollapsed(widget, reason, collapsed) {
if (!widget) return;
if (!widget.__h3ContinuumHidden) {
widget.__h3ContinuumHidden = true;
widget.type = "converted-widget";
widget.computeSize = () => [0, -4];
if (!widget.__h3ContinuumCollapseState) {
widget.__h3ContinuumCollapseState = {
computeSize: widget.computeSize,
hiddenOption: widget.options?.hidden,
reasons: new Set(),
};
}
widget.hidden = true;
const state = widget.__h3ContinuumCollapseState;
if (collapsed) {
state.reasons.add(reason);
} else {
state.reasons.delete(reason);
}
const hidden = state.reasons.size > 0;
widget.options ||= {};
if (hidden) {
widget.options.hidden = true;
} else if (state.hiddenOption === undefined) {
delete widget.options.hidden;
} else {
widget.options.hidden = state.hiddenOption;
}
widget.computeSize = hidden ? () => [0, -4] : state.computeSize;
for (const element of [widget.element, widget.inputEl, widget.el]) {
if (element?.style) element.style.display = "none";
if (element?.style) element.style.display = hidden ? "none" : "";
}
}
function promptInputName(index) {
return `clip_${index}_prompt`;
function serializableWidgets(widgets) {
return widgets.filter((widget) => widget.serialize !== false);
}
function placeTransientAfter(node, transient, anchor) {
const original = node.widgets ?? [];
const serializedBefore = serializableWidgets(original);
const withoutTransient = original.filter((widget) => widget !== transient);
const anchorIndex = withoutTransient.findIndex((widget) =>
typeof anchor === "string" ? widget.name === anchor : widget === anchor,
);
if (anchorIndex < 0) return;
const next = [
...withoutTransient.slice(0, anchorIndex + 1),
transient,
...withoutTransient.slice(anchorIndex + 1),
];
const serializedAfter = serializableWidgets(next);
const stable = serializedBefore.length === serializedAfter.length
&& serializedBefore.every((widget, index) => widget === serializedAfter[index]);
if (!stable) throw new Error("H3 Continuum refused to reorder serialized widgets");
node.widgets = next;
}
function inputIsConnected(input) {
return input?.link != null || (Array.isArray(input?.links) && input.links.length > 0);
}
function overridesExpanded(node) {
return node.properties?.[OVERRIDE_PROPERTY] === true;
function outputIsConnected(output) {
return Array.isArray(output?.links) && output.links.length > 0;
}
function syncPromptFormat(node) {
@@ -53,73 +105,102 @@ function syncSequencePrompt(node) {
if (input) input.label = "Sequence Prompt";
}
function clipOverridesExpanded(node) {
return node.properties?.[CLIP_OVERRIDES_PROPERTY] === true;
}
function ensureClipOverridesButton(node) {
if (node.__h3ContinuumClipOverridesButton) {
return node.__h3ContinuumClipOverridesButton;
}
const button = node.addWidget(
"button",
"Individual Clip Overrides ▸",
null,
() => {
node.properties ||= {};
node.properties[CLIP_OVERRIDES_PROPERTY] = !clipOverridesExpanded(node);
syncNode(node);
},
{ serialize: false },
);
button.serialize = false;
button.__h3ContinuumTransient = true;
node.__h3ContinuumClipOverridesButton = button;
return button;
}
function syncPromptInputs(node) {
node.properties ||= {};
if (typeof node.properties[CLIP_OVERRIDES_PROPERTY] !== "boolean") {
node.properties[CLIP_OVERRIDES_PROPERTY] = false;
}
const chunksWidget = node.widgets?.find((widget) => widget.name === "chunks");
const chunks = Math.max(1, Math.min(MAX_CHUNKS, Number(chunksWidget?.value) || 1));
const expanded = overridesExpanded(node);
for (let index = node.inputs.length - 1; index >= 0; index -= 1) {
const input = node.inputs[index];
const expanded = clipOverridesExpanded(node);
for (const input of node.inputs ?? []) {
const match = PROMPT_INPUT.exec(input.name);
if (!match) continue;
const clipIndex = Number(match[1]);
const active = clipIndex <= chunks;
if ((!expanded || !active) && !inputIsConnected(input)) {
node.removeInput(index);
} else {
input.label = active
? "Clip " + clipIndex + " Prompt Override"
: "Clip " + clipIndex + " Override (inactive)";
}
}
if (!expanded) return;
for (let index = 1; index <= chunks; index += 1) {
const name = promptInputName(index);
if (!node.inputs.some((input) => input.name === name)) {
node.addInput(name, "STRING");
}
const input = node.inputs.find((candidate) => candidate.name === name);
if (input) input.label = "Clip " + index + " Prompt Override";
input.label = clipIndex <= chunks
? "Clip " + clipIndex + " Prompt Override"
: "Clip " + clipIndex + " Override (inactive)";
}
const button = ensureClipOverridesButton(node);
placeTransientAfter(node, button, "prompt_mode");
button.name = expanded
? "Individual Clip Overrides (" + chunks + ") ▾"
: "Individual Clip Overrides (" + chunks + ") ▸";
}
function ensureOverrideToggle(node) {
function advancedExpanded(node) {
return node.properties?.[ADVANCED_PROPERTY] === true;
}
function ensureAdvancedButton(node) {
if (node.__h3ContinuumAdvancedButton) return node.__h3ContinuumAdvancedButton;
const button = node.addWidget(
"button",
"Advanced Settings ▸",
null,
() => {
node.properties ||= {};
node.properties[ADVANCED_PROPERTY] = !advancedExpanded(node);
syncNode(node);
},
{ serialize: false },
);
button.serialize = false;
button.__h3ContinuumTransient = true;
node.__h3ContinuumAdvancedButton = button;
return button;
}
function syncAdvancedWidgets(node) {
node.properties ||= {};
if (typeof node.properties[OVERRIDE_PROPERTY] !== "boolean") {
node.properties[OVERRIDE_PROPERTY] = false;
if (typeof node.properties[ADVANCED_PROPERTY] !== "boolean") {
node.properties[ADVANCED_PROPERTY] = false;
}
let widget = node.widgets?.find((candidate) => candidate.__h3OverrideToggle);
if (!widget) {
widget = node.addWidget(
"button",
"Individual Clip Overrides ▸",
null,
() => {
node.properties[OVERRIDE_PROPERTY] = !overridesExpanded(node);
syncNode(node);
},
{ serialize: false },
);
widget.__h3OverrideToggle = true;
widget.serialize = false;
const widgets = node.widgets || [];
const currentIndex = widgets.indexOf(widget);
const formatIndex = widgets.findIndex((candidate) => candidate.name === "prompt_mode");
if (currentIndex >= 0 && formatIndex >= 0) {
widgets.splice(currentIndex, 1);
widgets.splice(formatIndex + 1, 0, widget);
if (
(node.inputs ?? []).some((input) => ADVANCED_INPUTS.has(input.name) && inputIsConnected(input))
|| (node.outputs ?? []).some((output) => ADVANCED_OUTPUTS.has(output.name) && outputIsConnected(output))
) {
node.properties[ADVANCED_PROPERTY] = true;
}
const expanded = advancedExpanded(node);
for (const widget of node.widgets ?? []) {
if (ADVANCED_WIDGETS.has(widget.name)) {
setWidgetCollapsed(widget, "advanced", !expanded);
}
}
widget.name = overridesExpanded(node)
? "Individual Clip Overrides ▾"
: "Individual Clip Overrides ▸";
const button = ensureAdvancedButton(node);
button.name = expanded ? "Advanced Settings ▾" : "Advanced Settings ▸";
}
function syncPreviewSetting(node) {
const widget = node.widgets?.find((candidate) => candidate.name === "show_preview");
if (!widget) return;
hideWidget(widget);
setWidgetCollapsed(widget, "preview-setting", true);
widget.value = showLatentPreview;
}
@@ -128,20 +209,108 @@ function repairLegacySeamCorrection(node) {
if (widget && typeof widget.value === "boolean") widget.value = "Off";
}
function ensureSeamCorrectionProxy(node) {
const source = node.widgets?.find((widget) => widget.name === "seam_correction");
if (!source) return null;
if (typeof source.value === "boolean") source.value = "Off";
setWidgetCollapsed(source, "seam-proxy", true);
let proxy = node.__h3ContinuumSeamProxy;
if (!proxy) {
proxy = node.addWidget(
"combo",
"Seam Correction",
source.value,
(value) => {
source.value = value;
source.callback?.call(source, value);
},
{ values: source.options?.values ?? ["Auto", "Off", "Basic"], serialize: false },
);
proxy.serialize = false;
proxy.__h3ContinuumTransient = true;
node.__h3ContinuumSeamProxy = proxy;
}
proxy.value = source.value;
placeTransientAfter(node, proxy, "base_seed");
return proxy;
}
function cssAttributeValue(value) {
return String(value).replaceAll("\\", "\\\\").replaceAll('"', '\\"');
}
function shouldHideInput(node, input, chunks) {
const promptMatch = PROMPT_INPUT.exec(input.name);
if (promptMatch) {
const clipIndex = Number(promptMatch[1]);
return !inputIsConnected(input)
&& (!clipOverridesExpanded(node) || clipIndex > chunks);
}
return ADVANCED_INPUTS.has(input.name)
&& !advancedExpanded(node)
&& !inputIsConnected(input);
}
function slotVisibilityRules(node) {
const id = cssAttributeValue(node.id);
const root = `.lg-node[data-node-id="${id}"]`;
const chunksWidget = node.widgets?.find((widget) => widget.name === "chunks");
const chunks = Math.max(1, Math.min(MAX_CHUNKS, Number(chunksWidget?.value) || 1));
const rules = [];
const renderedInputs = (node.inputs ?? []).filter((input) => !input.widget);
renderedInputs.forEach((input, index) => {
if (shouldHideInput(node, input, chunks)) {
rules.push(`${root} .lg-slot--input:nth-child(${index + 1}) { display: none !important; }`);
}
});
(node.outputs ?? []).forEach((output, index) => {
if (
ADVANCED_OUTPUTS.has(output.name)
&& !advancedExpanded(node)
&& !outputIsConnected(output)
) {
rules.push(`${root} .lg-slot--output:nth-child(${index + 1}) { display: none !important; }`);
}
});
return rules;
}
function syncSlotVisibilityStyles() {
let style = document.getElementById(VISIBILITY_STYLE_ID);
if (!style) {
style = document.createElement("style");
style.id = VISIBILITY_STYLE_ID;
document.head.appendChild(style);
}
style.textContent = (app.graph?._nodes ?? [])
.filter((node) => node.comfyClass === NODE_NAME || node.type === NODE_NAME)
.flatMap(slotVisibilityRules)
.join("\n");
}
function resizeNode(node) {
const size = node.computeSize();
node.setSize([Math.max(node.size[0], size[0]), size[1]]);
node.setSize([Math.max(node.size?.[0] ?? 0, 320), 120]);
node.setDirtyCanvas(true, true);
}
function syncNode(node) {
hideWidget(node.widgets?.find((widget) => widget.name === "prompt_script"));
setWidgetCollapsed(
node.widgets?.find((widget) => widget.name === "prompt_script"),
"prompt-script",
true,
);
syncPromptFormat(node);
syncSequencePrompt(node);
ensureOverrideToggle(node);
repairLegacySeamCorrection(node);
syncPromptInputs(node);
const seamProxy = ensureSeamCorrectionProxy(node);
syncAdvancedWidgets(node);
if (seamProxy) {
placeTransientAfter(node, node.__h3ContinuumAdvancedButton, seamProxy);
}
syncPreviewSetting(node);
syncSlotVisibilityStyles();
resizeNode(node);
}
@@ -157,17 +326,24 @@ function wrapChunksWidget(node) {
};
}
function scheduleSync(node) {
clearTimeout(node.__h3ContinuumSyncTimer);
node.__h3ContinuumSyncTimer = setTimeout(() => {
wrapChunksWidget(node);
syncNode(node);
}, 0);
}
function syncAllSamplerNodes() {
for (const node of app.graph?._nodes || []) {
if (node.comfyClass === NODE_NAME || node.type === NODE_NAME) {
syncPreviewSetting(node);
node.setDirtyCanvas(true, true);
syncNode(node);
}
}
}
app.registerExtension({
name: "H3Continuum.V2Interface",
name: "H3Continuum.V215StableInterface",
async setup() {
showLatentPreview = app.ui.settings.getSettingValue(PREVIEW_SETTING) ?? true;
@@ -191,20 +367,21 @@ app.registerExtension({
const originalCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
const result = originalCreated?.apply(this, arguments);
setTimeout(() => {
wrapChunksWidget(this);
syncNode(this);
}, 0);
scheduleSync(this);
return result;
};
const originalConfigure = nodeType.prototype.onConfigure;
nodeType.prototype.onConfigure = function () {
const result = originalConfigure?.apply(this, arguments);
setTimeout(() => {
wrapChunksWidget(this);
syncNode(this);
}, 0);
scheduleSync(this);
return result;
};
const originalConnectionsChange = nodeType.prototype.onConnectionsChange;
nodeType.prototype.onConnectionsChange = function () {
const result = originalConnectionsChange?.apply(this, arguments);
scheduleSync(this);
return result;
};
},
-204
View File
@@ -1,204 +0,0 @@
import { app } from "../../scripts/app.js";
const SAMPLER_NODE = "H3ContinuumSamplerV2";
const ADVANCED_PROPERTY = "h3_show_advanced";
const ADVANCED_INPUTS = [
["last_frame", "IMAGE"],
["session", "H3_CONTINUUM_SESSION"],
["initial_state", "H3_CONTINUUM_STATE"],
["prompt_plan", "H3_CONTINUUM_PROMPT_PLAN"],
];
const ADVANCED_OUTPUTS = [
["last_state", "H3_CONTINUUM_STATE"],
["session", "H3_CONTINUUM_SESSION"],
["report", "STRING"],
];
const ADVANCED_WIDGETS = new Set([
"control_after_generate",
"audio_continuity",
"exact_total_duration",
"diagnostics",
"reroll_from_chunk",
"reroll_nonce",
"strict_compatibility",
"debug",
"show_preview",
]);
function inputByName(node, name) {
return node.inputs?.find((input) => input.name === name);
}
function outputByName(node, name) {
return node.outputs?.find((output) => output.name === name);
}
function hasAdvancedConnection(node) {
const inputLinked = ADVANCED_INPUTS.some(([name]) => inputByName(node, name)?.link != null);
const outputLinked = ADVANCED_OUTPUTS.some(([name]) => {
const links = outputByName(node, name)?.links;
return Array.isArray(links) && links.length > 0;
});
return inputLinked || outputLinked;
}
function ensureAdvancedSlots(node) {
for (const [name, type] of ADVANCED_INPUTS) {
if (!inputByName(node, name)) {
node.addInput(name, type, { shape: 7 });
}
}
for (const [name, type] of ADVANCED_OUTPUTS) {
if (!outputByName(node, name)) {
node.addOutput(name, type);
}
}
}
function removeUnlinkedAdvancedSlots(node) {
if (hasAdvancedConnection(node)) {
return;
}
for (let index = (node.inputs?.length ?? 0) - 1; index >= 0; index -= 1) {
if (ADVANCED_INPUTS.some(([name]) => node.inputs[index].name === name)) {
node.removeInput(index);
}
}
for (let index = (node.outputs?.length ?? 0) - 1; index >= 0; index -= 1) {
if (ADVANCED_OUTPUTS.some(([name]) => node.outputs[index].name === name)) {
node.removeOutput(index);
}
}
}
function setWidgetVisible(widget, visible) {
if (!widget.__h3V214Original) {
widget.__h3V214Original = {
type: widget.type,
computeSize: widget.computeSize,
hidden: widget.hidden,
};
}
if (visible) {
widget.type = widget.__h3V214Original.type;
widget.computeSize = widget.__h3V214Original.computeSize;
widget.hidden = widget.__h3V214Original.hidden;
return;
}
widget.type = "hidden";
widget.computeSize = () => [0, -4];
widget.hidden = true;
}
function ensureAdvancedButton(node) {
if (node.__h3V214AdvancedButton) {
return node.__h3V214AdvancedButton;
}
const button = node.addWidget(
"button",
"Advanced Settings ▸",
null,
() => {
node.properties ||= {};
node.properties[ADVANCED_PROPERTY] = !node.properties[ADVANCED_PROPERTY];
syncAdvancedUi(node);
},
{ serialize: false },
);
button.serialize = false;
node.__h3V214AdvancedButton = button;
return button;
}
function placeAdvancedButton(node, button, expanded) {
const widgets = node.widgets ?? [];
const currentIndex = widgets.indexOf(button);
if (currentIndex >= 0) {
widgets.splice(currentIndex, 1);
}
if (!expanded) {
widgets.push(button);
return;
}
const firstAdvancedIndex = widgets.findIndex((widget) => ADVANCED_WIDGETS.has(widget.name));
widgets.splice(firstAdvancedIndex >= 0 ? firstAdvancedIndex : widgets.length, 0, button);
}
function resizeNode(node) {
const computed = node.computeSize();
const width = Math.max(node.size?.[0] ?? 0, computed[0], 320);
node.setSize([width, computed[1]]);
node.setDirtyCanvas(true, true);
}
function syncAdvancedUi(node) {
node.properties ||= {};
if (typeof node.properties[ADVANCED_PROPERTY] !== "boolean") {
node.properties[ADVANCED_PROPERTY] = false;
}
if (hasAdvancedConnection(node)) {
node.properties[ADVANCED_PROPERTY] = true;
}
const expanded = node.properties[ADVANCED_PROPERTY];
if (expanded) {
ensureAdvancedSlots(node);
} else {
removeUnlinkedAdvancedSlots(node);
}
for (const widget of node.widgets ?? []) {
if (ADVANCED_WIDGETS.has(widget.name)) {
setWidgetVisible(widget, expanded);
}
}
const button = ensureAdvancedButton(node);
button.name = expanded ? "Advanced Settings ▾" : "Advanced Settings ▸";
placeAdvancedButton(node, button, expanded);
resizeNode(node);
}
function scheduleSync(node) {
clearTimeout(node.__h3V214SyncTimer);
node.__h3V214SyncTimer = setTimeout(() => syncAdvancedUi(node), 0);
}
app.registerExtension({
name: "H3Continuum.V214SimplifiedUi",
beforeRegisterNodeDef(nodeType, nodeData) {
if (nodeData.name !== SAMPLER_NODE) {
return;
}
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
const result = onNodeCreated?.apply(this, arguments);
this.properties ||= {};
if (typeof this.properties[ADVANCED_PROPERTY] !== "boolean") {
this.properties[ADVANCED_PROPERTY] = false;
}
ensureAdvancedButton(this);
scheduleSync(this);
return result;
};
const onConfigure = nodeType.prototype.onConfigure;
nodeType.prototype.onConfigure = function () {
ensureAdvancedSlots(this);
const result = onConfigure?.apply(this, arguments);
ensureAdvancedButton(this);
scheduleSync(this);
return result;
};
const onConnectionsChange = nodeType.prototype.onConnectionsChange;
nodeType.prototype.onConnectionsChange = function () {
const result = onConnectionsChange?.apply(this, arguments);
scheduleSync(this);
return result;
};
},
});