Update js/sequence_prompt_builder.js

This commit is contained in:
2026-11-12 10:51:46 +00:00
parent 8549283b75
commit d5687ec102
+95 -41
View File
@@ -3,7 +3,7 @@ import { api } from "../../scripts/api.js";
const CLASS = "DumasH3SequencePromptBuilder";
// Check the browser console for this line to confirm the patched file loaded.
console.info("[DUMAS H3] Prompt Builder v4 (live reference previews) loaded");
console.info("[DUMAS H3] Prompt Builder v5 (upstream + executed tensor previews) loaded");
const MAX_CLIPS = 16;
const FIXED_TEXT_WIDGETS = [
["shared_directive", 84],
@@ -292,49 +292,80 @@ function imagePreviewCandidates(source) {
};
const className = String(source.comfyClass || source.type || "");
const isOutputLoader = /LoadImageOutput|image.?output/i.test(className);
const imageWidget = get(source, "image");
// File-backed image loaders have an authoritative filename available before
// running the workflow. This takes priority over stale preview images.
if (imageWidget && (typeof imageWidget.value === "string" || imageWidget.value?.filename || imageWidget.value?.value)) {
addRef(imageWidget.value, isOutputLoader ? "output" : "input");
}
// Reuse images that the source node is already drawing on ComfyUI's canvas.
// This works for many other nodes that have no 'image' filename widget.
const images = source.imgs || source.previewImages || [];
if (images?.length) {
const selected = images[source.imageIndex ?? 0] || images[0];
add(selected?.src || selected?.currentSrc);
// A selected filename works even before the source node has been executed.
const imageWidget = get(source, "image") || get(source, "image_file") || get(source, "input_image");
if (imageWidget) addRef(imageWidget.value, isOutputLoader ? "output" : "input");
// Native ComfyUI and many custom nodes expose a ready-to-draw image.
const imgs = source.imgs || source.previewImages || [];
if (Array.isArray(imgs) && imgs.length) {
const selected = imgs[source.imageIndex ?? 0] || imgs[0];
add(selected?.currentSrc || selected?.src);
if (selected?.filename) addRef(selected, "output");
if (typeof selected?.toDataURL === "function") {
try { add(selected.toDataURL("image/png")); } catch (_) { /* canvas may be tainted */ }
}
}
// Results from upstream generation/processing nodes after execution.
for (const item of source.images || []) if (item?.filename) add(imageViewUrl(item));
const result = app.nodeOutputs?.[source.id]
for (const item of source.images || []) {
if (item?.filename) add(imageViewUrl(item));
else add(item?.src || item?.currentSrc);
}
const output = app.nodeOutputs?.[source.id]
|| app.nodeOutputs?.get?.(source.id)
|| app.nodeOutputStore?.getNodeOutputs?.(source);
for (const item of result?.images || []) if (item?.filename) add(imageViewUrl(item));
// Some custom file loader nodes call their selector 'filename'.
if (!candidates.length && /image|photo|picture/i.test(className)) {
const fileWidget = get(source, "filename") || get(source, "file_name");
if (fileWidget) addRef(fileWidget.value, isOutputLoader ? "output" : "input");
for (const item of output?.images || []) if (item?.filename) add(imageViewUrl(item));
for (const item of output?.dumas_reference_previews || []) if (item?.filename) add(imageViewUrl(item));
// Nodes with a custom loader widget may not call it 'image'.
for (const name of ["filename", "file_name", "image_path", "path"]) {
const field = get(source, name);
if (field && /load|image|photo|picture/i.test(className)) addRef(field.value, isOutputLoader ? "output" : "input");
}
return candidates;
}
function connectedReferenceImageCandidates(node, slot) {
const visited = new Set();
let source = linkedNode(node, `reference_image_${slot}`);
for (let depth = 0; source && depth < 16; depth += 1) {
const identity = source.id ?? source;
if (visited.has(identity)) break;
visited.add(identity);
const className = String(source.comfyClass || source.type || "");
if (/reroute/i.test(className)) {
source = linkedNode(source, source.inputs?.[0]?.name || "");
continue;
}
return imagePreviewCandidates(source);
// Resolve both (a) previews returned by THIS builder after execution (exact
// tensor from the socket), and (b) upstream previews before execution. Unlike
// v4, the upstream search continues through resize/crop/switch/relay nodes.
function connectedReferenceImageCandidates(node, slot, trace = []) {
const candidates = [];
const add = (url) => {
if (typeof url === "string" && url && !candidates.includes(url)) candidates.push(url);
};
const ownOutput = app.nodeOutputs?.[node.id]
|| app.nodeOutputs?.get?.(node.id)
|| app.nodeOutputStore?.getNodeOutputs?.(node);
for (const item of ownOutput?.dumas_reference_previews || []) {
if (Number(item.slot) === slot && item.filename) add(imageViewUrl(item));
}
return [];
const visited = new Set();
const first = linkedNode(node, `reference_image_${slot}`);
const queue = first ? [{source:first, depth:0}] : [];
while (queue.length && visited.size < 64) {
const {source, depth} = queue.shift();
const identity = source?.id ?? source;
if (!source || visited.has(identity)) continue;
visited.add(identity);
const className = String(source.comfyClass || source.type || "Unknown");
trace.push({id:source.id, type:className});
for (const url of imagePreviewCandidates(source)) add(url);
if (depth >= 16) continue;
// Follow IMAGE sockets, so a connected resize node can fall through to
// a file-backed LoadImage. Reroute/ANY sockets also participate.
const inputs = (source.inputs || []).filter(input => input?.link != null);
const imageInputs = inputs.filter(input =>
/image|any|\*/i.test(String(input.type || "")) ||
/image|pixels|input|source/i.test(String(input.name || "")) ||
/reroute|switch|pipe|relay/i.test(className)
);
const connectedInputs = imageInputs.length ? imageInputs :
(/image|crop|resize|scale|switch|reroute|pipe/i.test(className) ? inputs : []);
for (const input of connectedInputs) {
const upstream = linkedNode(source, input.name);
if (upstream) queue.push({source:upstream, depth:depth+1});
}
}
return candidates;
}
function refModDescriptorsForNode(node, inputName) {
@@ -482,6 +513,7 @@ function configure(node) {
binding.choices = choices;
binding.index = 0;
binding.failed = false;
binding.lastError = "";
binding.url = null;
}
// New preview may appear after node execution without its graph
@@ -493,7 +525,7 @@ function configure(node) {
binding.image.removeAttribute("src");
binding.image.style.display = "none";
binding.fallback.style.display = "grid";
binding.element.title = `@R${binding.slot}: No browser-visible image preview available`;
binding.element.title = `@R${binding.slot}: No preview available yet (execute builder for tensor preview)`;
continue;
}
binding.lastAttempt = Date.now();
@@ -519,6 +551,7 @@ function configure(node) {
const binding = { slot, element: holder, fallback, image, choices: [], index: 0, url: null, failed: false, signature: "", lastAttempt: 0 };
image.onload = () => {
binding.failed = false;
binding.lastError = "";
image.style.display = "block";
fallback.style.display = "none";
};
@@ -533,6 +566,7 @@ function configure(node) {
return;
}
binding.failed = true;
binding.lastError = "Image URL could not load";
image.style.display = "none";
fallback.style.display = "grid";
holder.title = `@R${slot}: Preview unavailable; see Console for diagnostics`;
@@ -547,11 +581,19 @@ function configure(node) {
const referenceThumbTimer = window.setInterval(refreshReferenceThumbs, 1500);
// Expose read-only diagnostics for unusual custom image loaders, without
// logging image URLs or changing ComfyUI's prompt/output format.
node.__dumasReferencePreviewStatus = () => Array.from({length: 9}, (_, i) => i + 1).map(slot => ({
slot, connected: !!node.inputs?.find(input => input.name === `reference_image_${slot}` && input.link != null),
candidateCount: connectedReferenceImageCandidates(node, slot).length,
sourceType: String(linkedNode(node, `reference_image_${slot}`)?.comfyClass || linkedNode(node, `reference_image_${slot}`)?.type || ""),
}));
node.__dumasReferencePreviewStatus = () => Array.from({length: 9}, (_, i) => i + 1).map(slot => {
const trace = [];
const candidates = connectedReferenceImageCandidates(node, slot, trace);
const binding = [...referenceThumbBindings].find(b => b.slot === slot && b.element.isConnected);
return {
slot,
connected: !!node.inputs?.find(input => input.name === `reference_image_${slot}` && input.link != null),
upstreamNodes: trace.map(item => item.type).join(" -> "),
candidateCount: candidates.length,
displayed: !!binding?.image?.complete && !!binding?.image?.naturalWidth,
previewError: binding?.lastError || "",
};
});
// Selection is visual UI state only; shot timings remain in timeline_json.
const selectedShotByClip = new Map();
// Writing the hidden widget value is safe while a textarea owns focus.
@@ -1368,6 +1410,18 @@ function configure(node) {
refresh();
}
// Browser console: dumasH3ReferenceStatus() shows exactly which image nodes
// were found, without exposing the user's image data.
window.dumasH3ReferenceStatus = () => {
const entries = (app.graph?._nodes || [])
.filter(node => node.comfyClass === CLASS && typeof node.__dumasReferencePreviewStatus === "function")
.flatMap(node => node.__dumasReferencePreviewStatus()
.filter(item => item.connected)
.map(item => ({builderId:node.id, ...item})));
console.table(entries);
return entries;
};
app.registerExtension({
name: "DUMAS.H3.SequencePromptBuilder",
nodeCreated(node) { setTimeout(() => configure(node), 0); },