Kling 节点接入官方 4.0 / Flash(文生/图生/Omni)
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
import { app } from "../../../scripts/app.js";
|
||||
|
||||
// Kling 4.0 / Flash: display names stay as labels, stored values remain
|
||||
// `v4` / `v4-flash` so older workflows that saved `v4` keep working.
|
||||
// Switching to Flash clamps 1080p back to 720p and caps duration at 20s.
|
||||
|
||||
const NODE_TYPE = "K3Video";
|
||||
const MODEL_LABELS = Object.freeze({
|
||||
v4: "Kling 4.0",
|
||||
"v4-flash": "Kling 4.0 Flash",
|
||||
});
|
||||
const FLASH_MODELS = new Set(["v4-flash", "Kling 4.0 Flash"]);
|
||||
const STANDARD_V4_MODELS = new Set(["v4", "Kling 4.0"]);
|
||||
const FLASH_RESOLUTIONS = ["720p"];
|
||||
const STANDARD_RESOLUTIONS = ["720p", "1080p"];
|
||||
const RESOLUTION_FALLBACK = "720p";
|
||||
const FLASH_MAX_DURATION = 20;
|
||||
const STANDARD_MAX_DURATION = 30;
|
||||
const INSTALLED_FLAG = "o1keyKlingV4ParameterGuardInstalled";
|
||||
const LABEL_FLAG = "o1keyKlingV4ModelLabelsInstalled";
|
||||
|
||||
function notify(detail) {
|
||||
app.extensionManager?.toast?.add({
|
||||
severity: "warn",
|
||||
summary: "Kling 参数已调整",
|
||||
detail,
|
||||
life: 4000,
|
||||
});
|
||||
}
|
||||
|
||||
function collectWidgets(root, found = []) {
|
||||
const widgets = root?.widgets || root?.childWidgets || [];
|
||||
for (const widget of widgets) {
|
||||
found.push(widget);
|
||||
if (widget?.widgets || widget?.childWidgets) {
|
||||
collectWidgets(widget, found);
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
function findWidget(node, name) {
|
||||
return collectWidgets(node).find((widget) => widget?.name === name);
|
||||
}
|
||||
|
||||
function modelValueFrom(node, modelWidget) {
|
||||
const value = modelWidget?.value;
|
||||
if (value && typeof value === "object") {
|
||||
return value.模型 ?? value.model ?? "";
|
||||
}
|
||||
return value ?? "";
|
||||
}
|
||||
|
||||
function isFlashModel(model) {
|
||||
return FLASH_MODELS.has(String(model || ""));
|
||||
}
|
||||
|
||||
function isKlingV4Family(model) {
|
||||
const raw = String(model || "");
|
||||
return FLASH_MODELS.has(raw) || STANDARD_V4_MODELS.has(raw);
|
||||
}
|
||||
|
||||
function setWidgetValue(widget, value) {
|
||||
if (!widget || widget.value === value) return;
|
||||
widget.value = value;
|
||||
widget.callback?.(value);
|
||||
}
|
||||
|
||||
function clampDuration(value, maximum) {
|
||||
const duration = Number.parseInt(value, 10);
|
||||
if (Number.isFinite(duration) && duration > maximum) return maximum;
|
||||
return value;
|
||||
}
|
||||
|
||||
function applyModelLabels(widget) {
|
||||
if (!widget?.options || widget.options[LABEL_FLAG]) return;
|
||||
const previousGetOptionLabel = widget.options.getOptionLabel;
|
||||
widget.options.getOptionLabel = (value) => {
|
||||
const normalized = value == null ? "" : String(value);
|
||||
if (MODEL_LABELS[normalized]) return MODEL_LABELS[normalized];
|
||||
return previousGetOptionLabel?.(value) ?? normalized;
|
||||
};
|
||||
widget.options[LABEL_FLAG] = true;
|
||||
}
|
||||
|
||||
function updateDynamicParameters(node, silent = false) {
|
||||
const modelWidget = findWidget(node, "模型");
|
||||
if (!modelWidget) return;
|
||||
applyModelLabels(modelWidget);
|
||||
|
||||
const model = modelValueFrom(node, modelWidget);
|
||||
if (!isKlingV4Family(model)) return;
|
||||
|
||||
const resWidget = findWidget(node, "模式");
|
||||
const durationWidget = findWidget(node, "时长");
|
||||
const flash = isFlashModel(model);
|
||||
const allowedResolutions = flash ? FLASH_RESOLUTIONS : STANDARD_RESOLUTIONS;
|
||||
const maximumDuration = flash ? FLASH_MAX_DURATION : STANDARD_MAX_DURATION;
|
||||
|
||||
if (resWidget) {
|
||||
resWidget.options ??= {};
|
||||
resWidget.options.values = allowedResolutions;
|
||||
const current = String(resWidget.value ?? "");
|
||||
if (!allowedResolutions.includes(current)) {
|
||||
setWidgetValue(resWidget, RESOLUTION_FALLBACK);
|
||||
if (!silent) {
|
||||
notify(`${MODEL_LABELS[model] || model} 仅支持 ${allowedResolutions.join(" / ")},已自动切换为 ${RESOLUTION_FALLBACK}。`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (durationWidget) {
|
||||
durationWidget.options ??= {};
|
||||
if (durationWidget.options.max != null) durationWidget.options.max = maximumDuration;
|
||||
if (durationWidget.max != null) durationWidget.max = maximumDuration;
|
||||
const clamped = clampDuration(durationWidget.value, maximumDuration);
|
||||
if (clamped !== durationWidget.value) {
|
||||
setWidgetValue(durationWidget, clamped);
|
||||
if (!silent) {
|
||||
notify(`${MODEL_LABELS[model] || model} 的最大时长为 ${maximumDuration} 秒,已自动调整。`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (modelWidget.value && typeof modelWidget.value === "object") {
|
||||
const current = modelWidget.value;
|
||||
let changed = false;
|
||||
if (resWidget && current.模式 && !allowedResolutions.includes(String(current.模式))) {
|
||||
current.模式 = RESOLUTION_FALLBACK;
|
||||
changed = true;
|
||||
}
|
||||
if (current.时长 != null) {
|
||||
const next = clampDuration(current.时长, maximumDuration);
|
||||
if (next !== current.时长) {
|
||||
current.时长 = next;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed && !silent) {
|
||||
notify(`${MODEL_LABELS[model] || model} 的分辨率或时长已按官方范围调整。`);
|
||||
}
|
||||
}
|
||||
|
||||
node.setDirtyCanvas?.(true, true);
|
||||
}
|
||||
|
||||
function guardNode(node) {
|
||||
if (!node || node.comfyClass !== NODE_TYPE || node[INSTALLED_FLAG]) return;
|
||||
node[INSTALLED_FLAG] = true;
|
||||
|
||||
const modelWidget = findWidget(node, "模型");
|
||||
if (!modelWidget) return;
|
||||
applyModelLabels(modelWidget);
|
||||
|
||||
const origModelCb = modelWidget.callback;
|
||||
modelWidget.callback = function () {
|
||||
const ret = origModelCb?.apply(this, arguments);
|
||||
updateDynamicParameters(node);
|
||||
return ret;
|
||||
};
|
||||
|
||||
const resWidget = findWidget(node, "模式");
|
||||
if (resWidget) {
|
||||
const origResCb = resWidget.callback;
|
||||
resWidget.callback = function (value) {
|
||||
const model = modelValueFrom(node, modelWidget);
|
||||
if (isFlashModel(model) && value === "1080p") {
|
||||
resWidget.value = RESOLUTION_FALLBACK;
|
||||
notify("Kling 4.0 Flash 仅支持 720p,已自动切换为 720p。");
|
||||
node.setDirtyCanvas?.(true, true);
|
||||
return origResCb?.call(this, RESOLUTION_FALLBACK);
|
||||
}
|
||||
return origResCb?.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
const durationWidget = findWidget(node, "时长");
|
||||
if (durationWidget) {
|
||||
const origDurationCb = durationWidget.callback;
|
||||
durationWidget.callback = function (value) {
|
||||
const model = modelValueFrom(node, modelWidget);
|
||||
const maximum = isFlashModel(model) ? FLASH_MAX_DURATION : STANDARD_MAX_DURATION;
|
||||
const clamped = clampDuration(value, maximum);
|
||||
if (clamped !== value) {
|
||||
durationWidget.value = clamped;
|
||||
notify(`${MODEL_LABELS[model] || model} 的最大时长为 ${maximum} 秒。`);
|
||||
node.setDirtyCanvas?.(true, true);
|
||||
return origDurationCb?.call(this, clamped);
|
||||
}
|
||||
return origDurationCb?.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
updateDynamicParameters(node, true);
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: "o1key.klingV4ParameterGuard",
|
||||
nodeCreated(node) {
|
||||
if (node?.comfyClass === NODE_TYPE) guardNode(node);
|
||||
},
|
||||
loadedGraphNode(node) {
|
||||
if (node?.comfyClass !== NODE_TYPE) return;
|
||||
guardNode(node);
|
||||
updateDynamicParameters(node, true);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user