Files
comfyui_o1key/web/js/gptImageQuality.js
T
Jony a181cc1768 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.
2026-11-09 09:11:12 +08:00

154 lines
5.4 KiB
JavaScript

import { app } from "../../../scripts/app.js";
const NODE_TYPES = new Set(["O1keyGPTImage", "O1keyGPTImageBatch"]);
const BASE_QUALITY_OPTIONS = ["高", "中", "低", "自动"];
const GPT_IMAGE_25_QUALITY_OPTIONS = [...BASE_QUALITY_OPTIONS, "超高", "最高"];
const QUALITY_FALLBACK = "自动";
const RASTER_FORMATS = ["png", "jpeg", "webp"];
const FORMAT_26 = [...RASTER_FORMATS, "svg", "gif"];
const BATCH_RASTER = ["原始", "JPEG", "PNG", "WebP"];
const BATCH_26 = [...BATCH_RASTER, "SVG", "GIF"];
function findWidget(node, name) {
return node.widgets?.find((widget) => widget.name === name);
}
function setWidgetHidden(widget, hidden) {
if (!widget) return;
if (!("__o1keyOriginalComputeSize" in widget)) {
widget.__o1keyOriginalComputeSize = widget.computeSize;
}
widget.hidden = hidden;
widget.options ??= {};
widget.options.hidden = hidden;
if (hidden) widget.computeSize = () => [0, -4];
else if (widget.__o1keyOriginalComputeSize) {
widget.computeSize = widget.__o1keyOriginalComputeSize;
} else delete widget.computeSize;
}
function hasReferenceOrMask(node) {
if (findWidget(node, "遮罩")?.value) return true;
const widgets = node.widgets || [];
for (const widget of widgets) {
if (/^参考图/.test(widget.name) && widget.value) return true;
}
// Connected inputs on modern nodes are not always mirrored into widget values.
const inputs = node.inputs || [];
for (const input of inputs) {
if (!input || input.link == null) continue;
if (input.name === "遮罩" || /^参考图/.test(input.name) || input.name === "参考图组") {
return true;
}
}
return false;
}
function normalizeStandaloneFormat(value) {
return String(value || "png").trim().toLowerCase();
}
function normalizeBatchFormat(value) {
const raw = String(value || "原始");
if (["svg", "SVG"].includes(raw)) return "SVG";
if (["gif", "GIF"].includes(raw)) return "GIF";
return raw;
}
function updateQualityOptions(node) {
const modelWidget = findWidget(node, "模型");
const qualityWidget = findWidget(node, "质量");
if (!modelWidget || !qualityWidget) return;
const model = String(modelWidget.value || "");
const supportsExtendedQuality = /^gpt-image-2\.[56]-/.test(model);
const is26 = /^gpt-image-2\.6-/.test(model);
const allowedOptions = supportsExtendedQuality ? GPT_IMAGE_25_QUALITY_OPTIONS : BASE_QUALITY_OPTIONS;
qualityWidget.options ??= {};
qualityWidget.options.values = [...allowedOptions];
if (!allowedOptions.includes(qualityWidget.value)) {
qualityWidget.value = QUALITY_FALLBACK;
qualityWidget.callback?.(QUALITY_FALLBACK);
}
setWidgetHidden(findWidget(node, "复杂度"), !is26);
const formatWidget = findWidget(node, "输出格式") || findWidget(node, "图片输出格式");
const isBatch = (node.comfyClass || node.type) === "O1keyGPTImageBatch";
if (formatWidget) {
const blocked = hasReferenceOrMask(node);
let values = isBatch
? (is26 ? BATCH_26 : BATCH_RASTER)
: (is26 ? FORMAT_26 : RASTER_FORMATS);
if (is26 && blocked) {
values = values.filter((value) => !/^(svg|gif|SVG|GIF)$/i.test(value));
}
formatWidget.options ??= {};
formatWidget.options.values = [...values];
if (!values.includes(formatWidget.value)) {
formatWidget.value = isBatch ? "PNG" : "png";
formatWidget.callback?.(formatWidget.value);
}
}
const formatValue = isBatch
? normalizeBatchFormat(formatWidget?.value)
: normalizeStandaloneFormat(formatWidget?.value);
const showFrames = is26 && (formatValue === "gif" || formatValue === "GIF");
setWidgetHidden(findWidget(node, "帧数"), !showFrames);
setWidgetHidden(findWidget(node, "帧时长ms"), !showFrames);
node.setDirtyCanvas?.(true, true);
}
function guardNode(node) {
if (node.__o1keyGptImageQuality) return;
const modelWidget = findWidget(node, "模型");
const qualityWidget = findWidget(node, "质量");
if (!modelWidget || !qualityWidget) return;
node.__o1keyGptImageQuality = true;
const watch = ["模型", "输出格式", "图片输出格式", "遮罩"];
for (const name of watch) {
const widget = findWidget(node, name);
if (!widget) continue;
const original = widget.callback;
widget.callback = function () {
const result = original?.apply(this, arguments);
updateQualityOptions(node);
return result;
};
}
const originalOnConfigure = node.onConfigure;
node.onConfigure = function () {
const result = originalOnConfigure?.apply(this, arguments);
updateQualityOptions(this);
return result;
};
const originalOnConnectionsChange = node.onConnectionsChange;
node.onConnectionsChange = function () {
const result = originalOnConnectionsChange?.apply(this, arguments);
updateQualityOptions(this);
return result;
};
updateQualityOptions(node);
}
app.registerExtension({
name: "o1key.gptImageQuality",
nodeCreated(node) {
if (NODE_TYPES.has(node.comfyClass || node.type)) guardNode(node);
},
loadedGraphNode(node) {
if (!NODE_TYPES.has(node.comfyClass || node.type)) return;
guardNode(node);
updateQualityOptions(node);
},
});