Add MJ image node, GIF save, and Kling/image generator updates

Ship o1key MJ图片生成 with content/style refs and prompt flags, save-GIF with preview, Kling 4.0 helpers, plus GPT/unified image node polish and docs.
This commit is contained in:
Jony
2026-11-09 09:11:12 +08:00
parent ff1cf830fa
commit a181cc1768
40 changed files with 6711 additions and 675 deletions
+240
View File
@@ -0,0 +1,240 @@
import { app } from "../../../scripts/app.js";
import { api } from "../../../scripts/api.js";
/**
* 「保存 GIF」emits custom UI key ``o1key_gif`` (NOT ``images``).
*
* ComfyUI core ``unsafeUpdatePreviews`` only hydrates canvas / video
* previews from ``getNodeOutputs(node).images``. Feeding SavedImages made
* core stack ``$$canvas-image-preview`` (static frame + "W x H") under our
* DOM <img>. Stripping ``message.images`` in onExecuted does not clear the
* live ``app.nodeOutputs`` entry, so the canvas widget kept coming back.
*
* Fix: Python emits ``o1key_gif`` only; this extension mounts one <img>.
* gpt image intentionally emits no GIF node preview.
*/
const NODE_TYPES = new Set(["O1keySaveGIF"]);
const WIDGET_NAME = "o1key_gif_preview_widget";
const UI_KEY = "o1key_gif";
const CANVAS_PREVIEW_NAME = "$$canvas-image-preview";
const EXT_NAME = "comfyui_o1key.gifPreview";
function isGifEntry(entry) {
const name = String(entry?.filename || entry?.name || "");
return /\.gif$/i.test(name);
}
function buildViewUrl(entry) {
if (!entry) return "";
const filename = String(entry.filename || entry.name || "");
if (!filename) return "";
const params = new URLSearchParams();
params.set("filename", filename);
const subfolder = String(entry.subfolder || "");
if (subfolder) params.set("subfolder", subfolder);
params.set("type", String(entry.type || "output"));
return api.apiURL(`/view?${params.toString()}`);
}
function pickGifEntry(message) {
const custom = Array.isArray(message?.[UI_KEY]) ? message[UI_KEY] : [];
const fromCustom = custom.find(isGifEntry) || custom[0];
if (fromCustom) return fromCustom;
// Legacy SavedImages payloads (pre-custom-key) — still show one <img>,
// but clear live nodeOutputs.images so core cannot remount IMAGE_PREVIEW.
const images = Array.isArray(message?.images) ? message.images : [];
return images.find(isGifEntry) || null;
}
/**
* Remove core ``images`` from the LIVE outputs store for this node.
* History snapshots already copied the payload; mutating the live entry
* only stops ``updatePreviews`` from driving ImagePreviewWidget.
*/
function clearLiveCoreImages(node) {
const store = app.nodeOutputs;
if (!store || node?.id == null) return;
const idKeys = new Set([String(node.id), node.id]);
for (const key of Object.keys(store)) {
if (!idKeys.has(key) && !idKeys.has(String(key))) {
// Locator ids are usually the plain node id string; also match
// trailing ":id" / "id" forms used by execution-id helpers.
const asStr = String(key);
if (![...idKeys].some((id) => asStr === String(id) || asStr.endsWith(`:${id}`))) {
continue;
}
}
const entry = store[key];
if (!entry || typeof entry !== "object") continue;
if ("images" in entry) delete entry.images;
if ("animated" in entry) delete entry.animated;
}
}
function removeCoreCanvasPreview(node) {
node.imgs = undefined;
node.images = undefined;
node.imageIndex = undefined;
node.overIndex = undefined;
node.animatedImages = undefined;
if (node.videoContainer) {
try {
node.videoContainer.replaceChildren();
} catch (_err) {
/* ignore */
}
node.videoContainer = undefined;
}
if (!Array.isArray(node.widgets)) return;
for (let i = node.widgets.length - 1; i >= 0; i--) {
const widget = node.widgets[i];
if (!widget) continue;
const name = String(widget.name || "");
const type = String(widget.type || "");
const isOurs = name === WIDGET_NAME;
const isCore =
!isOurs &&
(name === CANVAS_PREVIEW_NAME ||
type === "IMAGE_PREVIEW" ||
type === "image" ||
type === "imagecompare" ||
type === "video" ||
/canvas-image-preview/i.test(name));
if (!isCore) continue;
try {
widget.onRemove?.();
} catch (_err) {
/* ignore */
}
node.widgets.splice(i, 1);
}
}
function ensureGifWidget(node) {
if (node._o1keyGifContainer && node.widgets?.some((w) => w.name === WIDGET_NAME)) {
return node._o1keyGifContainer;
}
const container = document.createElement("div");
container.style.cssText =
"width:100%;display:flex;flex-direction:column;align-items:center;" +
"padding:4px;box-sizing:border-box;";
const img = document.createElement("img");
img.alt = "GIF preview";
img.style.cssText =
"width:100%;display:block;border-radius:4px;background:#111;" +
"object-fit:contain;max-height:480px;";
const label = document.createElement("div");
label.style.cssText =
"font-size:10px;color:#aaa;margin-top:2px;text-align:center;word-break:break-all;";
container.appendChild(img);
container.appendChild(label);
node._o1keyGifContainer = container;
node._o1keyGifImg = img;
node._o1keyGifLabel = label;
if (!node.widgets?.some((w) => w.name === WIDGET_NAME)) {
const widget = node.addDOMWidget(WIDGET_NAME, "img", container, {
serialize: false,
hideOnZoom: false,
});
widget.serializeValue = () => undefined;
widget.computeSize = function (width) {
const w = width ?? node.size?.[0] ?? 320;
const naturalW = img.naturalWidth || 0;
const naturalH = img.naturalHeight || 0;
if (naturalW > 0 && naturalH > 0) {
const innerW = Math.max(w - 16, 10);
const h = Math.round((innerW * naturalH) / naturalW);
return [w, Math.min(h, 480) + 28];
}
return [w, 260];
};
}
img.addEventListener("load", () => {
const w = Math.max(node.size?.[0] || 0, 320);
const naturalW = img.naturalWidth || 1;
const naturalH = img.naturalHeight || 1;
const innerW = Math.max(w - 16, 10);
const h = Math.min(Math.round((innerW * naturalH) / naturalW), 480) + 70;
node.setSize([w, Math.max(node.size?.[1] || 0, h)]);
node.setDirtyCanvas(true, true);
});
return container;
}
function showGifPreview(node, entry) {
const url = buildViewUrl(entry);
if (!url) return;
clearLiveCoreImages(node);
removeCoreCanvasPreview(node);
ensureGifWidget(node);
if (node._o1keyGifImg) {
node._o1keyGifImg.src = url;
}
if (node._o1keyGifLabel) {
node._o1keyGifLabel.textContent = String(entry.filename || entry.name || "");
}
// Keep heuristics sane, but never leave imgs populated for IMAGE_PREVIEW.
node.previewMediaType = "image";
removeCoreCanvasPreview(node);
node.setDirtyCanvas(true, true);
}
app.registerExtension({
name: EXT_NAME,
async beforeRegisterNodeDef(nodeType, nodeData) {
if (!NODE_TYPES.has(nodeData?.name)) return;
const origOnExecuted = nodeType.prototype.onExecuted;
nodeType.prototype.onExecuted = function (message) {
const gif = pickGifEntry(message);
// Always strip live core image previews for this node type —
// new payloads have no ``images``; legacy ones must be cleared
// from app.nodeOutputs so updatePreviews cannot remount.
clearLiveCoreImages(this);
if (gif) {
if (origOnExecuted) {
const rest = Object.assign({}, message);
delete rest.images;
delete rest.animated;
origOnExecuted.call(this, rest);
}
showGifPreview(this, gif);
return;
}
if (origOnExecuted) {
origOnExecuted.apply(this, arguments);
}
};
},
/**
* History / queue restore updates nodeOutputs then may redraw before
* onExecuted; clear any stale ``images`` left from older SavedImages runs.
*/
async nodeCreated(node) {
if (!NODE_TYPES.has(node?.comfyClass || node?.type)) return;
// Light one-shot: if outputs already present without our key, ignore.
// Real preview is applied in onExecuted (also fired on history load).
clearLiveCoreImages(node);
},
});