Update js/sequence_prompt_builder.js
This commit is contained in:
@@ -3,7 +3,7 @@ import { api } from "../../scripts/api.js";
|
|||||||
|
|
||||||
const CLASS = "DumasH3SequencePromptBuilder";
|
const CLASS = "DumasH3SequencePromptBuilder";
|
||||||
// Check the browser console for this line to confirm the patched file loaded.
|
// 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 MAX_CLIPS = 16;
|
||||||
const FIXED_TEXT_WIDGETS = [
|
const FIXED_TEXT_WIDGETS = [
|
||||||
["shared_directive", 84],
|
["shared_directive", 84],
|
||||||
@@ -292,49 +292,80 @@ function imagePreviewCandidates(source) {
|
|||||||
};
|
};
|
||||||
const className = String(source.comfyClass || source.type || "");
|
const className = String(source.comfyClass || source.type || "");
|
||||||
const isOutputLoader = /LoadImageOutput|image.?output/i.test(className);
|
const isOutputLoader = /LoadImageOutput|image.?output/i.test(className);
|
||||||
const imageWidget = get(source, "image");
|
// A selected filename works even before the source node has been executed.
|
||||||
// File-backed image loaders have an authoritative filename available before
|
const imageWidget = get(source, "image") || get(source, "image_file") || get(source, "input_image");
|
||||||
// running the workflow. This takes priority over stale preview images.
|
if (imageWidget) addRef(imageWidget.value, isOutputLoader ? "output" : "input");
|
||||||
if (imageWidget && (typeof imageWidget.value === "string" || imageWidget.value?.filename || imageWidget.value?.value)) {
|
|
||||||
addRef(imageWidget.value, isOutputLoader ? "output" : "input");
|
// Native ComfyUI and many custom nodes expose a ready-to-draw image.
|
||||||
}
|
const imgs = source.imgs || source.previewImages || [];
|
||||||
// Reuse images that the source node is already drawing on ComfyUI's canvas.
|
if (Array.isArray(imgs) && imgs.length) {
|
||||||
// This works for many other nodes that have no 'image' filename widget.
|
const selected = imgs[source.imageIndex ?? 0] || imgs[0];
|
||||||
const images = source.imgs || source.previewImages || [];
|
add(selected?.currentSrc || selected?.src);
|
||||||
if (images?.length) {
|
|
||||||
const selected = images[source.imageIndex ?? 0] || images[0];
|
|
||||||
add(selected?.src || selected?.currentSrc);
|
|
||||||
if (selected?.filename) addRef(selected, "output");
|
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 || []) {
|
||||||
for (const item of source.images || []) if (item?.filename) add(imageViewUrl(item));
|
if (item?.filename) add(imageViewUrl(item));
|
||||||
const result = app.nodeOutputs?.[source.id]
|
else add(item?.src || item?.currentSrc);
|
||||||
|
}
|
||||||
|
const output = app.nodeOutputs?.[source.id]
|
||||||
|| app.nodeOutputs?.get?.(source.id)
|
|| app.nodeOutputs?.get?.(source.id)
|
||||||
|| app.nodeOutputStore?.getNodeOutputs?.(source);
|
|| app.nodeOutputStore?.getNodeOutputs?.(source);
|
||||||
for (const item of result?.images || []) if (item?.filename) add(imageViewUrl(item));
|
for (const item of output?.images || []) if (item?.filename) add(imageViewUrl(item));
|
||||||
// Some custom file loader nodes call their selector 'filename'.
|
for (const item of output?.dumas_reference_previews || []) if (item?.filename) add(imageViewUrl(item));
|
||||||
if (!candidates.length && /image|photo|picture/i.test(className)) {
|
|
||||||
const fileWidget = get(source, "filename") || get(source, "file_name");
|
// Nodes with a custom loader widget may not call it 'image'.
|
||||||
if (fileWidget) addRef(fileWidget.value, isOutputLoader ? "output" : "input");
|
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;
|
return candidates;
|
||||||
}
|
}
|
||||||
|
|
||||||
function connectedReferenceImageCandidates(node, slot) {
|
// Resolve both (a) previews returned by THIS builder after execution (exact
|
||||||
const visited = new Set();
|
// tensor from the socket), and (b) upstream previews before execution. Unlike
|
||||||
let source = linkedNode(node, `reference_image_${slot}`);
|
// v4, the upstream search continues through resize/crop/switch/relay nodes.
|
||||||
for (let depth = 0; source && depth < 16; depth += 1) {
|
function connectedReferenceImageCandidates(node, slot, trace = []) {
|
||||||
const identity = source.id ?? source;
|
const candidates = [];
|
||||||
if (visited.has(identity)) break;
|
const add = (url) => {
|
||||||
visited.add(identity);
|
if (typeof url === "string" && url && !candidates.includes(url)) candidates.push(url);
|
||||||
const className = String(source.comfyClass || source.type || "");
|
};
|
||||||
if (/reroute/i.test(className)) {
|
const ownOutput = app.nodeOutputs?.[node.id]
|
||||||
source = linkedNode(source, source.inputs?.[0]?.name || "");
|
|| app.nodeOutputs?.get?.(node.id)
|
||||||
continue;
|
|| app.nodeOutputStore?.getNodeOutputs?.(node);
|
||||||
}
|
for (const item of ownOutput?.dumas_reference_previews || []) {
|
||||||
return imagePreviewCandidates(source);
|
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) {
|
function refModDescriptorsForNode(node, inputName) {
|
||||||
@@ -482,6 +513,7 @@ function configure(node) {
|
|||||||
binding.choices = choices;
|
binding.choices = choices;
|
||||||
binding.index = 0;
|
binding.index = 0;
|
||||||
binding.failed = false;
|
binding.failed = false;
|
||||||
|
binding.lastError = "";
|
||||||
binding.url = null;
|
binding.url = null;
|
||||||
}
|
}
|
||||||
// New preview may appear after node execution without its graph
|
// New preview may appear after node execution without its graph
|
||||||
@@ -493,7 +525,7 @@ function configure(node) {
|
|||||||
binding.image.removeAttribute("src");
|
binding.image.removeAttribute("src");
|
||||||
binding.image.style.display = "none";
|
binding.image.style.display = "none";
|
||||||
binding.fallback.style.display = "grid";
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
binding.lastAttempt = Date.now();
|
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 };
|
const binding = { slot, element: holder, fallback, image, choices: [], index: 0, url: null, failed: false, signature: "", lastAttempt: 0 };
|
||||||
image.onload = () => {
|
image.onload = () => {
|
||||||
binding.failed = false;
|
binding.failed = false;
|
||||||
|
binding.lastError = "";
|
||||||
image.style.display = "block";
|
image.style.display = "block";
|
||||||
fallback.style.display = "none";
|
fallback.style.display = "none";
|
||||||
};
|
};
|
||||||
@@ -533,6 +566,7 @@ function configure(node) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
binding.failed = true;
|
binding.failed = true;
|
||||||
|
binding.lastError = "Image URL could not load";
|
||||||
image.style.display = "none";
|
image.style.display = "none";
|
||||||
fallback.style.display = "grid";
|
fallback.style.display = "grid";
|
||||||
holder.title = `@R${slot}: Preview unavailable; see Console for diagnostics`;
|
holder.title = `@R${slot}: Preview unavailable; see Console for diagnostics`;
|
||||||
@@ -547,11 +581,19 @@ function configure(node) {
|
|||||||
const referenceThumbTimer = window.setInterval(refreshReferenceThumbs, 1500);
|
const referenceThumbTimer = window.setInterval(refreshReferenceThumbs, 1500);
|
||||||
// Expose read-only diagnostics for unusual custom image loaders, without
|
// Expose read-only diagnostics for unusual custom image loaders, without
|
||||||
// logging image URLs or changing ComfyUI's prompt/output format.
|
// logging image URLs or changing ComfyUI's prompt/output format.
|
||||||
node.__dumasReferencePreviewStatus = () => Array.from({length: 9}, (_, i) => i + 1).map(slot => ({
|
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),
|
const trace = [];
|
||||||
candidateCount: connectedReferenceImageCandidates(node, slot).length,
|
const candidates = connectedReferenceImageCandidates(node, slot, trace);
|
||||||
sourceType: String(linkedNode(node, `reference_image_${slot}`)?.comfyClass || linkedNode(node, `reference_image_${slot}`)?.type || ""),
|
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.
|
// Selection is visual UI state only; shot timings remain in timeline_json.
|
||||||
const selectedShotByClip = new Map();
|
const selectedShotByClip = new Map();
|
||||||
// Writing the hidden widget value is safe while a textarea owns focus.
|
// Writing the hidden widget value is safe while a textarea owns focus.
|
||||||
@@ -1368,6 +1410,18 @@ function configure(node) {
|
|||||||
refresh();
|
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({
|
app.registerExtension({
|
||||||
name: "DUMAS.H3.SequencePromptBuilder",
|
name: "DUMAS.H3.SequencePromptBuilder",
|
||||||
nodeCreated(node) { setTimeout(() => configure(node), 0); },
|
nodeCreated(node) { setTimeout(() => configure(node), 0); },
|
||||||
|
|||||||
Reference in New Issue
Block a user