Files
comfyui_o1key/web/js/minimaxH3ParameterGuard.js
T

197 lines
7.0 KiB
JavaScript

import { app } from "../../../scripts/app.js";
const NODE_TYPE = "MiniMaxH3Video";
const MODEL_H3 = "MiniMax-H3";
const MODEL_MAX = "MiniMax-H3-MAX";
const MODEL_HIGHSPEED = "MiniMax-H3-Highspeed";
const MODEL_LITE = "MiniMax-H3-Lite";
const MODE_TEXT = "文生视频";
const MODE_REFERENCE = "多模态生视频";
const MODE_LAST = "尾帧图生视频";
const MODE_FIRST_LAST = "首尾帧生视频";
const FIXED_RATIOS = ["16:9", "21:9", "4:3", "1:1", "3:4", "9:16"];
const RESOLUTIONS = {
[MODEL_H3]: ["2K", "768P"],
[MODEL_MAX]: ["768P", "480P"],
[MODEL_HIGHSPEED]: ["768P", "480P"],
[MODEL_LITE]: ["768P", "480P"],
};
const MIN_DURATION = {
[MODEL_H3]: 4,
[MODEL_MAX]: 5,
[MODEL_HIGHSPEED]: 4,
[MODEL_LITE]: 4,
};
function findWidget(node, name) {
for (const widget of node.widgets || node.childWidgets || []) {
if (widget.name === name || widget.name?.endsWith(`.${name}`)) return widget;
const nested = findWidget(widget, name);
if (nested) return nested;
}
}
function selectedModel(node) {
const value = findWidget(node, "模型")?.value;
return Object.hasOwn(RESOLUTIONS, value) ? value : MODEL_H3;
}
function allowedRatios(node) {
return findWidget(node, "生成模式")?.value === MODE_REFERENCE
&& selectedModel(node) !== MODEL_LITE
? ["adaptive", ...FIXED_RATIOS] : FIXED_RATIOS;
}
function updateRatio(node, silent) {
const widget = findWidget(node, "宽高比");
if (!widget) return;
widget.options ??= {};
widget.options.values = allowedRatios(node);
if (!widget.__o1keyMiniMaxRatioGuard) {
widget.__o1keyMiniMaxRatioGuard = true;
const originalCallback = widget.callback;
widget.callback = function (value, ...args) {
const allowed = allowedRatios(node);
const next = allowed.includes(value) ? value : allowed[0];
if (next !== value) {
widget.value = next;
notify(`${selectedModel(node)} 当前模式须使用具体比例,已切换为 ${next}。`);
}
return originalCallback?.call(this, next, ...args);
};
}
if (!widget.options.values.includes(widget.value)) {
setWidgetValue(widget, widget.options.values[0]);
if (!silent) notify(`${selectedModel(node)} 当前模式须使用具体比例,已切换为 ${widget.value}。`);
}
}
function notify(detail) {
app.extensionManager?.toast?.add({
severity: "warn",
summary: "MiniMax H3 参数已调整",
detail,
life: 4000,
});
}
function setWidgetValue(widget, value) {
if (!widget || widget.value === value) return;
widget.value = value;
widget.callback?.(value);
}
function updateParameters(node, silent = false) {
const modelWidget = findWidget(node, "模型");
const modeWidget = findWidget(node, "生成模式");
const resolutionWidget = findWidget(node, "分辨率");
const durationWidget = findWidget(node, "时长");
if (!modelWidget || !modeWidget || !resolutionWidget || !durationWidget) return;
const model = selectedModel(node);
const allowedResolutions = RESOLUTIONS[model];
resolutionWidget.options ??= {};
resolutionWidget.options.values = [...allowedResolutions];
durationWidget.options ??= {};
durationWidget.options.min = MIN_DURATION[model];
durationWidget.options.max = 15;
if (!allowedResolutions.includes(resolutionWidget.value)) {
setWidgetValue(resolutionWidget, "768P");
if (!silent) notify(`${model} 不支持原分辨率,已切换为 768P。`);
}
const duration = Number(durationWidget.value);
if (Number.isFinite(duration) && duration < MIN_DURATION[model]) {
setWidgetValue(durationWidget, MIN_DURATION[model]);
if (!silent) notify(`${model} 最短时长为 ${MIN_DURATION[model]} 秒。`);
}
if (model === MODEL_MAX && modeWidget.value === MODE_REFERENCE) {
setWidgetValue(modeWidget, MODE_TEXT);
if (!silent) notify("MiniMax-H3-MAX 不支持参考素材模式,已切换为文生视频。");
}
if ([MODEL_HIGHSPEED, MODEL_LITE].includes(model) && modeWidget.value === MODE_LAST) {
setWidgetValue(modeWidget, MODE_FIRST_LAST);
if (!silent) notify(`${model} 使用尾帧时须同时提供首帧,已切换为首尾帧生视频。`);
}
updateRatio(node, silent);
node.setDirtyCanvas?.(true, true);
}
function guardNode(node) {
if (node.__o1keyMiniMaxH3ParameterGuard) return;
const modelWidget = findWidget(node, "模型");
const modeWidget = findWidget(node, "生成模式");
const resolutionWidget = findWidget(node, "分辨率");
const durationWidget = findWidget(node, "时长");
if (!modelWidget || !modeWidget || !resolutionWidget || !durationWidget) return;
node.__o1keyMiniMaxH3ParameterGuard = true;
const originalModelCallback = modelWidget.callback;
modelWidget.callback = function () {
const result = originalModelCallback?.apply(this, arguments);
updateParameters(node);
return result;
};
const originalModeCallback = modeWidget.callback;
modeWidget.callback = function (value) {
if (modelWidget.value === MODEL_MAX && value === MODE_REFERENCE) {
modeWidget.value = MODE_TEXT;
notify("MiniMax-H3-MAX 不支持参考素材模式。");
node.setDirtyCanvas?.(true, true);
return originalModeCallback?.call(this, MODE_TEXT);
}
const result = originalModeCallback?.apply(this, arguments);
updateParameters(node);
return result;
};
const originalResolutionCallback = resolutionWidget.callback;
resolutionWidget.callback = function (value) {
const model = selectedModel(node);
if (!RESOLUTIONS[model].includes(value)) {
resolutionWidget.value = "768P";
notify(`${model} 不支持 ${value},已切换为 768P。`);
node.setDirtyCanvas?.(true, true);
return originalResolutionCallback?.call(this, "768P");
}
return originalResolutionCallback?.apply(this, arguments);
};
const originalDurationCallback = durationWidget.callback;
durationWidget.callback = function (value) {
const model = selectedModel(node);
const minimum = MIN_DURATION[model];
const duration = Number(value);
if (Number.isFinite(duration) && duration < minimum) {
durationWidget.value = minimum;
notify(`${model} 最短时长为 ${minimum} 秒。`);
node.setDirtyCanvas?.(true, true);
return originalDurationCallback?.call(this, minimum);
}
return originalDurationCallback?.apply(this, arguments);
};
updateParameters(node, true);
}
app.registerExtension({
name: "o1key.minimaxH3ParameterGuard",
nodeCreated(node) {
if ((node.comfyClass || node.type) === NODE_TYPE) guardNode(node);
},
loadedGraphNode(node) {
if ((node.comfyClass || node.type) !== NODE_TYPE) return;
guardNode(node);
updateParameters(node, true);
},
});