Skip to content

Commit 26a58bc

Browse files
vincentkocMino
andauthored
fix(comfy): filter mixed video workflow outputs (#119543)
Punchcard-Session: clear-orchard-lantern-bc Co-authored-by: Mino <[email protected]>
1 parent 287da89 commit 26a58bc

3 files changed

Lines changed: 186 additions & 1 deletion

File tree

‎extensions/comfy/video-generation-provider.test.ts‎

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,67 @@ function fetchGuardParams(call: number): { url?: unknown; auditContext?: unknown
2626
return params as { url?: unknown; auditContext?: unknown };
2727
}
2828

29+
function mockLocalVideoResponses(params: {
30+
promptId: string;
31+
outputs: Record<string, unknown>;
32+
download?: {
33+
body: string;
34+
contentType: string;
35+
};
36+
}) {
37+
fetchWithSsrFGuardMock
38+
.mockResolvedValueOnce({
39+
response: new Response(JSON.stringify({ prompt_id: params.promptId }), {
40+
status: 200,
41+
headers: { "content-type": "application/json" },
42+
}),
43+
release: vi.fn(async () => {}),
44+
})
45+
.mockResolvedValueOnce({
46+
response: new Response(
47+
JSON.stringify({
48+
[params.promptId]: {
49+
outputs: params.outputs,
50+
},
51+
}),
52+
{
53+
status: 200,
54+
headers: { "content-type": "application/json" },
55+
},
56+
),
57+
release: vi.fn(async () => {}),
58+
});
59+
60+
if (params.download) {
61+
fetchWithSsrFGuardMock.mockResolvedValueOnce({
62+
response: new Response(Buffer.from(params.download.body), {
63+
status: 200,
64+
headers: { "content-type": params.download.contentType },
65+
}),
66+
release: vi.fn(async () => {}),
67+
});
68+
}
69+
}
70+
71+
function generateLocalVideo(outputNodeId?: string) {
72+
const provider = buildComfyVideoGenerationProvider();
73+
return provider.generateVideo({
74+
provider: "comfy",
75+
model: "workflow",
76+
prompt: "animate a lobster",
77+
cfg: buildComfyConfig({
78+
video: {
79+
workflow: {
80+
"6": { inputs: { text: "" } },
81+
"9": { inputs: {} },
82+
},
83+
promptNodeId: "6",
84+
...(outputNodeId ? { outputNodeId } : {}),
85+
},
86+
}),
87+
});
88+
}
89+
2990
describe("comfy video-generation provider", () => {
3091
beforeEach(() => {
3192
vi.clearAllMocks();
@@ -143,6 +204,119 @@ describe("comfy video-generation provider", () => {
143204
});
144205
});
145206

207+
it("returns only MP4 video entries from mixed images buckets", async () => {
208+
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
209+
mockLocalVideoResponses({
210+
promptId: "local-video-mixed",
211+
outputs: {
212+
"2": {
213+
images: [{ filename: "generated.png", subfolder: "", type: "output" }],
214+
},
215+
"4": {
216+
images: [{ filename: "generated.mp4", subfolder: "", type: "output" }],
217+
},
218+
},
219+
download: {
220+
body: "mp4-data",
221+
contentType: "video/mp4",
222+
},
223+
});
224+
225+
const result = await generateLocalVideo();
226+
227+
expect(fetchGuardParams(2).url).toBe(
228+
"http://127.0.0.1:8188/view?filename=generated.mp4&subfolder=&type=output",
229+
);
230+
expect(result.videos).toEqual([
231+
expect.objectContaining({
232+
buffer: Buffer.from("mp4-data"),
233+
mimeType: "video/mp4",
234+
fileName: "generated.mp4",
235+
metadata: {
236+
nodeId: "4",
237+
promptId: "local-video-mixed",
238+
},
239+
}),
240+
]);
241+
expect(result.metadata?.outputNodeIds).toEqual(["4"]);
242+
expect(fetchWithSsrFGuardMock).toHaveBeenCalledTimes(3);
243+
});
244+
245+
it("accepts uppercase WEBM names from the images bucket", async () => {
246+
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
247+
mockLocalVideoResponses({
248+
promptId: "local-video-webm",
249+
outputs: {
250+
"9": {
251+
images: [{ name: "generated.WEBM", subfolder: "", type: "output" }],
252+
},
253+
},
254+
download: {
255+
body: "webm-data",
256+
contentType: "video/webm",
257+
},
258+
});
259+
260+
const result = await generateLocalVideo();
261+
262+
expect(fetchGuardParams(2).url).toBe(
263+
"http://127.0.0.1:8188/view?filename=generated.WEBM&subfolder=&type=output",
264+
);
265+
expect(result.videos[0]).toEqual(
266+
expect.objectContaining({
267+
buffer: Buffer.from("webm-data"),
268+
mimeType: "video/webm",
269+
fileName: "generated.WEBM",
270+
}),
271+
);
272+
});
273+
274+
it("rejects images-only workflow output for video generation", async () => {
275+
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
276+
mockLocalVideoResponses({
277+
promptId: "local-video-images-only",
278+
outputs: {
279+
"9": {
280+
images: [
281+
{ filename: "generated.png", subfolder: "", type: "output" },
282+
{ filename: "generated.jpg", subfolder: "", type: "output" },
283+
],
284+
},
285+
},
286+
});
287+
288+
await expect(generateLocalVideo()).rejects.toThrow(
289+
"Comfy workflow local-video-images-only completed without video outputs",
290+
);
291+
expect(fetchWithSsrFGuardMock).toHaveBeenCalledTimes(2);
292+
});
293+
294+
it("preserves legacy videos bucket output without filename filtering", async () => {
295+
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
296+
mockLocalVideoResponses({
297+
promptId: "local-video-legacy",
298+
outputs: {
299+
"9": {
300+
videos: [{ filename: "generated.mov", subfolder: "", type: "output" }],
301+
},
302+
},
303+
download: {
304+
body: "legacy-video-data",
305+
contentType: "video/quicktime",
306+
},
307+
});
308+
309+
const result = await generateLocalVideo("9");
310+
311+
expect(result.videos[0]).toEqual(
312+
expect.objectContaining({
313+
buffer: Buffer.from("legacy-video-data"),
314+
mimeType: "video/quicktime",
315+
fileName: "generated.mov",
316+
}),
317+
);
318+
});
319+
146320
it("rejects generated video downloads that exceed the configured media cap", async () => {
147321
setComfyFetchGuardForTesting(fetchWithSsrFGuardMock);
148322
fetchWithSsrFGuardMock

‎extensions/comfy/video-generation-provider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function buildComfyVideoGenerationProvider(): VideoGenerationProvider {
7575
model: req.model,
7676
timeoutMs: req.timeoutMs,
7777
capability: "video",
78-
outputKinds: ["gifs", "videos"],
78+
outputKinds: ["images", "gifs", "videos"],
7979
inputImage: toComfyInputImage(req.inputImages?.[0]),
8080
});
8181

‎extensions/comfy/workflow-runtime.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ function collectOutputFiles(params: {
527527
history: ComfyHistoryEntry;
528528
outputNodeId?: string;
529529
outputKinds: readonly ComfyOutputKind[];
530+
capability: ComfyCapability;
530531
}): Array<{ nodeId: string; file: ComfyOutputFile }> {
531532
const outputs = params.history.outputs;
532533
if (!outputs) {
@@ -546,6 +547,15 @@ function collectOutputFiles(params: {
546547
continue;
547548
}
548549
for (const file of bucket) {
550+
if (params.capability === "video" && kind === "images") {
551+
// Comfy SaveVideo shares the images bucket with real image outputs.
552+
// Filter before download so mixed workflows cannot return images as videos.
553+
const fileName =
554+
normalizeOptionalString(file.filename) || normalizeOptionalString(file.name);
555+
if (!fileName || !/\.(?:mp4|webm)$/i.test(fileName)) {
556+
continue;
557+
}
558+
}
549559
files.push({ nodeId, file });
550560
}
551561
}
@@ -828,6 +838,7 @@ export async function runComfyWorkflow(params: {
828838
history: historyEntry,
829839
outputNodeId,
830840
outputKinds: params.outputKinds,
841+
capability: params.capability,
831842
});
832843
if (outputFiles.length === 0) {
833844
throw new Error(`Comfy workflow ${promptId} completed without ${params.capability} outputs`);

0 commit comments

Comments
 (0)