400 lines
16 KiB
JavaScript
400 lines
16 KiB
JavaScript
import { app } from "../../../scripts/app.js";
|
||
|
||
const NODE_TYPES = new Set(["SeedanceAutoPass", "SeedanceAutoPassBatch"]);
|
||
const MODEL_25 = "seedance 2.5";
|
||
const MODEL_25_FLASH = "seedance 2.5 flash";
|
||
const OVERSEAS_ROUTE = "海外";
|
||
const LEGACY_MODEL_ROUTES = new Set(["海外HC", "海外破限高并发", "海外破限", "海外破限标准", "海外标准"]);
|
||
const ALL_RESOLUTIONS = ["480p", "720p", "1080p", "4k"];
|
||
const LIMITED_RESOLUTIONS = ["480p", "720p"];
|
||
const LIMITED_RESOLUTION_MODELS = new Set([
|
||
"seedance 2.0 fast",
|
||
"seedance 2.0 mini",
|
||
MODEL_25_FLASH,
|
||
]);
|
||
const NO_4K_RESOLUTIONS = ["480p", "720p", "1080p"];
|
||
const RESOLUTION_FALLBACK = "720p";
|
||
const DRAFT_OFF = "关闭";
|
||
const DRAFT_PREVIEW = "生成样片";
|
||
const DRAFT_FINAL = "按样片生成";
|
||
const DRAFT_PREVIEW_RESOLUTION = "480p";
|
||
|
||
function resolutionsForModel(model) {
|
||
if (LIMITED_RESOLUTION_MODELS.has(model)) return LIMITED_RESOLUTIONS;
|
||
if (model === MODEL_25) return NO_4K_RESOLUTIONS;
|
||
return ALL_RESOLUTIONS;
|
||
}
|
||
const ID_WIDGET_GROUPS = [
|
||
{ prefix: "图片素材ID", maximum: 30 },
|
||
{ prefix: "视频素材ID", maximum: 10 },
|
||
{ prefix: "音频素材ID", maximum: 10 },
|
||
];
|
||
const UNAVAILABLE_WIDGETS = ["联网搜索"];
|
||
|
||
const durationOptions = (maximum) => [
|
||
"自动",
|
||
...Array.from({ length: maximum - 3 }, (_, index) => `${index + 4}秒`),
|
||
];
|
||
|
||
const DURATIONS_20 = durationOptions(15);
|
||
const DURATIONS_25 = durationOptions(30);
|
||
|
||
function findWidget(node, name) {
|
||
return node.widgets?.find((widget) => widget.name === name);
|
||
}
|
||
|
||
function findAssetModeWidget(node) {
|
||
return findWidget(node, "素材创建模式") ?? findWidget(node, "素材创建");
|
||
}
|
||
|
||
const REFERENCE_GROUPS = ["参考图片", "参考视频", "参考音频"];
|
||
const FRAME_INPUTS = ["首帧图片", "尾帧图片"];
|
||
|
||
function isReferenceInput(input) {
|
||
return REFERENCE_GROUPS.some((group) => input.name.startsWith(`${group}.`)
|
||
|| new RegExp(`^${group}\\d+$`).test(input.name));
|
||
}
|
||
|
||
function inputOptions(input) {
|
||
// Never reuse a removed socket's link or internal identity.
|
||
return { label: input.label ?? input.name.split(".").at(-1), shape: input.shape };
|
||
}
|
||
|
||
function syncModeInputs(node) {
|
||
const state = node.__o1keyAutoPassModeInputs;
|
||
if (!state || state.syncing) return;
|
||
const mode = findWidget(node, "生成模式")?.value;
|
||
const frameMode = mode === "首尾帧";
|
||
// 按样片生成不再读取任何素材,隐藏全部媒体插座避免误解。
|
||
const draftFinal = findWidget(node, "样片模式")?.value === DRAFT_FINAL;
|
||
const autogrow = node.comfyDynamic.autogrow;
|
||
state.syncing = true;
|
||
try {
|
||
// 官方 2.5 的视频编辑/延长同样接受参考图和参考音频,只有首尾帧模式需要替换插座。
|
||
for (const group of REFERENCE_GROUPS) {
|
||
const active = !frameMode && !draftFinal;
|
||
if (active) autogrow[group] = state.groups[group];
|
||
else delete autogrow[group];
|
||
}
|
||
for (let index = node.inputs.length - 1; index >= 0; index--) {
|
||
const input = node.inputs[index];
|
||
let removable = false;
|
||
if (draftFinal) {
|
||
removable = isReferenceInput(input) || FRAME_INPUTS.includes(input.name);
|
||
} else if (frameMode) {
|
||
removable = isReferenceInput(input);
|
||
} else {
|
||
removable = FRAME_INPUTS.includes(input.name);
|
||
}
|
||
if (removable) node.removeInput(index);
|
||
}
|
||
const templates = draftFinal ? [] : (frameMode ? state.frames : state.references);
|
||
for (const template of templates) {
|
||
const exists = frameMode
|
||
? node.inputs.some((input) => input.name === template.name)
|
||
: node.inputs.some((input) => input.name.startsWith(`${template.name.split(".")[0]}.`));
|
||
if (!exists) node.addInput(template.name, template.type, inputOptions(template));
|
||
}
|
||
} finally {
|
||
state.syncing = false;
|
||
}
|
||
syncIdWidgets(node);
|
||
}
|
||
|
||
function guardModeInputs(node) {
|
||
if (node.comfyClass !== "SeedanceAutoPass" || node.__o1keyAutoPassModeInputs) return;
|
||
const mode = findWidget(node, "生成模式");
|
||
const autogrow = node.comfyDynamic?.autogrow;
|
||
if (!mode || !autogrow || !node.addInput || !node.removeInput) return;
|
||
const references = REFERENCE_GROUPS.map((group) => node.inputs?.find((input) => input.name.startsWith(`${group}.`)));
|
||
if (references.some((input) => !input) || REFERENCE_GROUPS.some((group) => !autogrow[group])) return;
|
||
node.__o1keyAutoPassModeInputs = {
|
||
syncing: false,
|
||
groups: Object.fromEntries(REFERENCE_GROUPS.map((group) => [group, autogrow[group]])),
|
||
references: references.map((input) => ({ ...input })),
|
||
frames: FRAME_INPUTS.map((name) => ({ ...(node.inputs.find((input) => input.name === name) ?? { name, type: "IMAGE" }) })),
|
||
};
|
||
const originalConnectionsChange = node.onConnectionsChange;
|
||
node.onConnectionsChange = function () {
|
||
if (node.__o1keyAutoPassModeInputs.syncing) return;
|
||
return originalConnectionsChange?.apply(this, arguments);
|
||
};
|
||
const originalCallback = mode.callback;
|
||
mode.callback = function () {
|
||
const result = originalCallback?.apply(this, arguments);
|
||
syncModeInputs(node);
|
||
return result;
|
||
};
|
||
syncModeInputs(node);
|
||
}
|
||
|
||
// 与 SeedanceMultiModal 相同:保留控件和值的序列化顺序,仅渐进显隐单行 ID。
|
||
function setWidgetHidden(widget, hidden) {
|
||
if (!widget || (widget.__o1keyAutoPassIdHidden === hidden
|
||
&& widget.hidden === hidden && widget.options?.hidden === hidden)) return;
|
||
widget.__o1keyAutoPassIdHidden = hidden;
|
||
widget.hidden = hidden;
|
||
widget.options ??= {};
|
||
widget.options.hidden = hidden;
|
||
if (!("__o1keyAutoPassOriginalComputeSize" in widget)) {
|
||
widget.__o1keyAutoPassOriginalComputeSize = widget.computeSize ?? null;
|
||
}
|
||
if (hidden) {
|
||
widget.computeSize = () => [0, -4];
|
||
} else if (widget.__o1keyAutoPassOriginalComputeSize) {
|
||
widget.computeSize = widget.__o1keyAutoPassOriginalComputeSize;
|
||
} else {
|
||
delete widget.computeSize;
|
||
}
|
||
}
|
||
|
||
function syncIdWidgets(node) {
|
||
if (node.comfyClass !== "SeedanceAutoPass") return;
|
||
// Temporarily unavailable controls stay serialized for saved workflows.
|
||
for (const name of UNAVAILABLE_WIDGETS) setWidgetHidden(findWidget(node, name), true);
|
||
// 样片任务 ID:只有「按样片生成」且没有连线时才需要手填。
|
||
const draftIdLinked = node.inputs?.some(
|
||
(input) => input.name === "样片任务ID输入"
|
||
&& input.link !== null && input.link !== undefined,
|
||
);
|
||
setWidgetHidden(
|
||
findWidget(node, "样片任务ID"),
|
||
findWidget(node, "样片模式")?.value !== DRAFT_FINAL || Boolean(draftIdLinked),
|
||
);
|
||
const assetMode = findAssetModeWidget(node);
|
||
// 该开关自身始终显示,只控制下方 ID 行。
|
||
if (assetMode) {
|
||
assetMode.hidden = false;
|
||
assetMode.options ??= {};
|
||
assetMode.options.hidden = false;
|
||
}
|
||
const manual = ["打开", "手动", "manual"].includes(assetMode?.value);
|
||
for (const { prefix, maximum } of ID_WIDGET_GROUPS) {
|
||
const widgets = Array.from({ length: maximum }, (_, index) => findWidget(node, `${prefix}${index + 1}`));
|
||
let highestFilled = -1;
|
||
widgets.forEach((widget, index) => {
|
||
if (typeof widget?.value === "string" && widget.value.trim()) highestFilled = index;
|
||
});
|
||
const visibleCount = manual ? Math.min(maximum, Math.max(1, highestFilled + 2)) : 0;
|
||
widgets.forEach((widget, index) => setWidgetHidden(widget, index >= visibleCount));
|
||
}
|
||
if (typeof node.computeSize === "function" && typeof node.setSize === "function") {
|
||
const size = node.computeSize([...node.size]);
|
||
node.setSize([node.size[0], size[1]]);
|
||
}
|
||
node.setDirtyCanvas?.(true, true);
|
||
}
|
||
|
||
function guardIdWidgets(node) {
|
||
if (node.comfyClass !== "SeedanceAutoPass" || node.__o1keyAutoPassIdGuard) return;
|
||
node.__o1keyAutoPassIdGuard = true;
|
||
const widgets = [findAssetModeWidget(node)];
|
||
for (const { prefix, maximum } of ID_WIDGET_GROUPS) {
|
||
for (let index = 1; index <= maximum; index++) widgets.push(findWidget(node, `${prefix}${index}`));
|
||
}
|
||
for (const widget of widgets) {
|
||
if (!widget) continue;
|
||
const originalCallback = widget.callback;
|
||
widget.callback = function () {
|
||
const result = originalCallback?.apply(this, arguments);
|
||
syncIdWidgets(node);
|
||
return result;
|
||
};
|
||
}
|
||
syncIdWidgets(node);
|
||
}
|
||
|
||
// 样片任务 ID 支持连线:连上后隐藏手填框,断开后恢复。
|
||
function guardDraftIdInput(node) {
|
||
if (node.comfyClass !== "SeedanceAutoPass" || node.__o1keyDraftIdGuard) return;
|
||
node.__o1keyDraftIdGuard = true;
|
||
const originalConnectionsChange = node.onConnectionsChange;
|
||
node.onConnectionsChange = function () {
|
||
const result = originalConnectionsChange?.apply(this, arguments);
|
||
syncIdWidgets(node);
|
||
return result;
|
||
};
|
||
}
|
||
|
||
function notify(detail) {
|
||
app.extensionManager?.toast?.add({
|
||
severity: "info",
|
||
summary: "Seedance 全能生成视频参数已调整",
|
||
detail,
|
||
life: 3500,
|
||
});
|
||
}
|
||
|
||
function setWidgetValue(widget, value) {
|
||
if (!widget || widget.value === value) return;
|
||
widget.value = value;
|
||
widget.callback?.(value);
|
||
}
|
||
|
||
function updateDynamicParameters(node, silent = false) {
|
||
const modelWidget = findWidget(node, "主模型");
|
||
const routeWidget = findWidget(node, "模型线路");
|
||
const resolutionWidget = findWidget(node, "分辨率");
|
||
const durationWidget = findWidget(node, "时长");
|
||
const draftWidget = findWidget(node, "样片模式");
|
||
if (!modelWidget || !routeWidget || !resolutionWidget || !durationWidget) return;
|
||
|
||
const is25 = [MODEL_25, MODEL_25_FLASH].includes(modelWidget.value);
|
||
const supportsDraft = modelWidget.value === MODEL_25;
|
||
const routes = modelWidget.value === MODEL_25_FLASH ? ["国内"] : ["海外", "国内"];
|
||
routeWidget.options ??= {};
|
||
routeWidget.options.values = routes;
|
||
|
||
// 样片模式仅 2.5 可用:其他模型直接隐藏,并强制回到「关闭」。
|
||
if (draftWidget) {
|
||
setWidgetHidden(draftWidget, !supportsDraft);
|
||
if (!supportsDraft && draftWidget.value !== DRAFT_OFF) {
|
||
setWidgetValue(draftWidget, DRAFT_OFF);
|
||
if (!silent) notify("样片模式仅支持 Seedance 2.5,已关闭样片模式。");
|
||
}
|
||
}
|
||
const draftValue = draftWidget?.value ?? DRAFT_OFF;
|
||
const draftPreview = draftValue === DRAFT_PREVIEW;
|
||
|
||
const allowedResolutions = draftPreview
|
||
? [DRAFT_PREVIEW_RESOLUTION]
|
||
: resolutionsForModel(modelWidget.value);
|
||
const fallbackResolution = draftPreview ? DRAFT_PREVIEW_RESOLUTION : RESOLUTION_FALLBACK;
|
||
const maximumDuration = is25 ? 30 : 15;
|
||
durationWidget.options ??= {};
|
||
durationWidget.options.values = is25 ? DURATIONS_25 : DURATIONS_20;
|
||
resolutionWidget.options ??= {};
|
||
resolutionWidget.options.values = allowedResolutions;
|
||
|
||
if (LEGACY_MODEL_ROUTES.has(routeWidget.value)) {
|
||
setWidgetValue(routeWidget, OVERSEAS_ROUTE);
|
||
}
|
||
if (!routes.includes(routeWidget.value)) {
|
||
setWidgetValue(routeWidget, "国内");
|
||
if (!silent) notify("Seedance 2.5 Flash 仅支持国内线路,已自动切换。");
|
||
}
|
||
|
||
if (!allowedResolutions.includes(resolutionWidget.value)) {
|
||
setWidgetValue(resolutionWidget, fallbackResolution);
|
||
if (!silent) {
|
||
notify(
|
||
draftPreview
|
||
? "样片模式只输出 480p 预览,已自动切换为 480p。"
|
||
: `${modelWidget.value} 仅支持 ${allowedResolutions.join(" / ")},`
|
||
+ `已自动切换为 ${fallbackResolution}。`,
|
||
);
|
||
}
|
||
}
|
||
|
||
const duration = Number.parseInt(durationWidget.value, 10);
|
||
if (Number.isFinite(duration) && duration > maximumDuration) {
|
||
setWidgetValue(durationWidget, `${maximumDuration}秒`);
|
||
if (!silent) {
|
||
notify(`${modelWidget.value} 的最大时长为 ${maximumDuration} 秒,已自动调整。`);
|
||
}
|
||
}
|
||
|
||
// 样片模式/ID 行隐藏后需要重算节点高度。
|
||
syncIdWidgets(node);
|
||
|
||
node.setDirtyCanvas?.(true, true);
|
||
}
|
||
|
||
function guardNode(node) {
|
||
if (node.__o1keySeedanceAutoPassDynamic) return;
|
||
node.__o1keySeedanceAutoPassDynamic = true;
|
||
|
||
const modelWidget = findWidget(node, "主模型");
|
||
const routeWidget = findWidget(node, "模型线路");
|
||
const resolutionWidget = findWidget(node, "分辨率");
|
||
const durationWidget = findWidget(node, "时长");
|
||
if (!modelWidget || !routeWidget || !resolutionWidget || !durationWidget) return;
|
||
|
||
const originalModelCallback = modelWidget.callback;
|
||
modelWidget.callback = function (value) {
|
||
const result = originalModelCallback?.apply(this, arguments);
|
||
updateDynamicParameters(node);
|
||
return result;
|
||
};
|
||
|
||
const originalRouteCallback = routeWidget.callback;
|
||
routeWidget.callback = function (value) {
|
||
if (modelWidget.value === MODEL_25_FLASH && value !== "国内") {
|
||
routeWidget.value = "国内";
|
||
notify("Seedance 2.5 Flash 仅支持国内线路。");
|
||
return originalRouteCallback?.call(this, "国内");
|
||
}
|
||
if (LEGACY_MODEL_ROUTES.has(value)) {
|
||
routeWidget.value = OVERSEAS_ROUTE;
|
||
node.setDirtyCanvas?.(true, true);
|
||
return originalRouteCallback?.call(this, OVERSEAS_ROUTE);
|
||
}
|
||
return originalRouteCallback?.apply(this, arguments);
|
||
};
|
||
|
||
const originalResolutionCallback = resolutionWidget.callback;
|
||
resolutionWidget.callback = function (value) {
|
||
const allowedResolutions = resolutionsForModel(modelWidget.value);
|
||
if (!allowedResolutions.includes(value)) {
|
||
resolutionWidget.value = RESOLUTION_FALLBACK;
|
||
notify(`${modelWidget.value} 仅支持 ${allowedResolutions.join(" / ")}。`);
|
||
node.setDirtyCanvas?.(true, true);
|
||
return originalResolutionCallback?.call(this, RESOLUTION_FALLBACK);
|
||
}
|
||
return originalResolutionCallback?.apply(this, arguments);
|
||
};
|
||
|
||
const originalDurationCallback = durationWidget.callback;
|
||
durationWidget.callback = function (value) {
|
||
const maximumDuration = [MODEL_25, MODEL_25_FLASH].includes(modelWidget.value) ? 30 : 15;
|
||
const duration = Number.parseInt(value, 10);
|
||
if (Number.isFinite(duration) && duration > maximumDuration) {
|
||
const fallback = `${maximumDuration}秒`;
|
||
durationWidget.value = fallback;
|
||
notify(`${modelWidget.value} 的最大时长为 ${maximumDuration} 秒。`);
|
||
node.setDirtyCanvas?.(true, true);
|
||
return originalDurationCallback?.call(this, fallback);
|
||
}
|
||
return originalDurationCallback?.apply(this, arguments);
|
||
};
|
||
|
||
const draftWidget = findWidget(node, "样片模式");
|
||
if (draftWidget) {
|
||
const originalDraftCallback = draftWidget.callback;
|
||
draftWidget.callback = function () {
|
||
const result = originalDraftCallback?.apply(this, arguments);
|
||
updateDynamicParameters(node);
|
||
syncModeInputs(node);
|
||
syncIdWidgets(node);
|
||
if (draftWidget.value === DRAFT_FINAL) {
|
||
notify("按样片生成只提交样片任务 ID,提示词、素材和参数都会复用样片。");
|
||
}
|
||
return result;
|
||
};
|
||
}
|
||
|
||
updateDynamicParameters(node, true);
|
||
}
|
||
|
||
app.registerExtension({
|
||
name: "o1key.seedanceAutoPassDynamic",
|
||
|
||
nodeCreated(node) {
|
||
if (!NODE_TYPES.has(node.comfyClass)) return;
|
||
guardNode(node);
|
||
guardIdWidgets(node);
|
||
guardDraftIdInput(node);
|
||
guardModeInputs(node);
|
||
},
|
||
|
||
loadedGraphNode(node) {
|
||
if (!NODE_TYPES.has(node.comfyClass)) return;
|
||
updateDynamicParameters(node, true);
|
||
guardIdWidgets(node);
|
||
guardDraftIdInput(node);
|
||
guardModeInputs(node);
|
||
syncModeInputs(node);
|
||
syncIdWidgets(node);
|
||
},
|
||
});
|