Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Runs split one per picture: each run says what it makes itself

Adarsh61014275d3f1
diff --git a/src/components/studio/draft.ts b/src/components/studio/draft.ts
index d60116cf..316a9bd2 100644
--- a/src/components/studio/draft.ts
+++ b/src/components/studio/draft.ts
@@ -3,6 +3,7 @@
* example a page loads into its composer. Plain functions, shared by the studio page and its tests.
*/
import { nanoid } from "nanoid";
+import { reuseOutputs } from "@/lib/split-outputs";
import type { Generation, GenerationRequest, MediaKind, ModelDef, Preset } from "@/lib/catalog/types";
import { handoffFrames, presetModel, presetParams, routeParams, withoutLayout } from "@/lib/composer";
import { presetRoutes } from "@/lib/catalog/preset-recipe-routes";
@@ -126,6 +127,8 @@ export function draftFromRequest(request: GenerationRequest, config: StudioConfi
if (r.inputs?.voiceAudio) slots.voiceAudio = slot(r.inputs.voiceAudio);
if (r.inputs?.voiceAudio2) slots.voiceAudio2 = slot(r.inputs.voiceAudio2);
const params = { ...r.params };
+ // One run of a request split one per picture: Reuse brings back the number asked for, not this run's own.
+ if (request.askedOutputs) params.numOutputs = reuseOutputs({ askedOutputs: request.askedOutputs, params });
// A keyframes run comes back in Frames mode, each still at its place in the clip.
const times = params.keyframeTimes;
const frames =
diff --git a/src/lib/catalog/types.ts b/src/lib/catalog/types.ts
index 6d6b9855..090c45d4 100644
--- a/src/lib/catalog/types.ts
+++ b/src/lib/catalog/types.ts
@@ -1235,6 +1235,12 @@ export type LoraField =
/** What the UI posts to /api/generations. Asset references are local asset ids or URLs. */
export interface GenerationRequest {
+ /**
+ * Server-only (the request schema drops it from what a client sends): one run of a request for more pictures than one
+ * run makes, split one run per picture or group. The number asked for; `params.numOutputs` is this run's own, so its
+ * tile, Retry and Library show and make that many. Reuse brings the number asked for back to the composer.
+ */
+ askedOutputs?: number;
/**
* Public sign-up: the account whose own ComfyUI runs this ("Your GPU"), never the install's providers. Set by the
* server from the session only (the request schema drops it from what a client sends).
diff --git a/src/lib/split-outputs.ts b/src/lib/split-outputs.ts
new file mode 100644
index 00000000..c2caf289
--- /dev/null
+++ b/src/lib/split-outputs.ts
@@ -0,0 +1,19 @@
+import type { GenerationRequest } from "./catalog/types";
+
+/**
+ * The request one run of a split keeps. A request for more pictures than one run of its graph makes is split one run per
+ * picture (or group of `perJob`): each run's request says what that run makes (`params.numOutputs`: its tile's
+ * placeholders, Retry and the Library read it) and keeps the number asked for (`askedOutputs`: Reuse reads it). A run
+ * that isn't split keeps its request as it was.
+ */
+export function splitRunRequest<R extends GenerationRequest>(request: R, jobs: number, perJob: number): R {
+ const asked = request.params?.numOutputs ?? 1;
+ if (jobs <= 1 || asked <= perJob) return request;
+ return { ...request, askedOutputs: asked, params: { ...request.params, numOutputs: perJob } };
+}
+
+/** The number of pictures Reuse brings back to the composer: the number asked for when the run was one of a split. */
+export function reuseOutputs(request: Pick<GenerationRequest, "askedOutputs" | "params">): number | undefined {
+ const own = request.params?.numOutputs;
+ return request.askedOutputs && request.askedOutputs > (own ?? 1) ? request.askedOutputs : own;
+}
diff --git a/src/server/generations.ts b/src/server/generations.ts
index 90d006a4..c0d10bdf 100644
--- a/src/server/generations.ts
+++ b/src/server/generations.ts
@@ -6,6 +6,7 @@ import os from "node:os";
import path from "node:path";
import { nanoid } from "nanoid";
import { getModel, getPreset } from "@/lib/catalog";
+import { splitRunRequest } from "@/lib/split-outputs";
import { ANY_ANGLE } from "@/lib/catalog/any-angle";
import { NO_SUBJECT_MESSAGE, withFrontView } from "./any-angle";
import { applyMentions } from "@/lib/catalog/elements";
@@ -1740,6 +1741,8 @@ export function createGenerations(sent: GenerationRequest, opts: CreateOptions =
if (waits && route.chained) throw new Error(chainedWaitMessage(plan.model.name, plan.op));
const jobs = waits ? 1 : plan.jobs;
const batchId = opts.batchId ?? (jobs > 1 ? nanoid(10) : null);
+ // Split one run per picture (or group): each run's request says what it makes itself, and keeps the number asked for.
+ const stored = splitRunRequest(kept, jobs, plan.perJobOutputs);
const now = Date.now();
const created: Generation[] = [];
for (let i = 0; i < jobs; i++) {
@@ -1760,7 +1763,7 @@ export function createGenerations(sent: GenerationRequest, opts: CreateOptions =
// A step whose prompt comes from an earlier step shows its own words (not the placeholder) until then.
prompt: waits && req.chain?.some((s) => s.slot === "prompt") ? original.prompt?.replaceAll("{text}", "…").trim() || null : plan.prompt,
// An Original remembers the engine it ran on, so a retry or a chained step runs there too.
- request_json: JSON.stringify(dualEngine(plan.model) ? { ...kept, engine: plan.model.engine } : kept),
+ request_json: JSON.stringify(dualEngine(plan.model) ? { ...stored, engine: plan.model.engine } : stored),
provider_input_json: null,
external_id: null,
external_meta_json: null,
diff --git a/tests/split-outputs.test.ts b/tests/split-outputs.test.ts
new file mode 100644
index 00000000..2988c66c
--- /dev/null
+++ b/tests/split-outputs.test.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it } from "vitest";
+import type { GenerationRequest } from "@/lib/catalog/types";
+import { reuseOutputs, splitRunRequest } from "@/lib/split-outputs";
+import { draftFromRequest } from "@/components/studio/draft";
+import { IMAGE_STUDIO } from "@/lib/studios";
+import { getModel, getPreset } from "@/lib/catalog";
+
+// A request for more pictures than one run of its graph makes is split one run per picture. Each run's stored request says
+// what that run makes (its tile's placeholders, Retry and the Library read it), and keeps the number asked for (Reuse).
+const ask: GenerationRequest = { feature: "image", modelId: "oss-minimax-h3-image", prompt: "a red fox in deep snow", engine: "open", params: { numOutputs: 3, seed: 7 } };
+
+describe("splitRunRequest", () => {
+ it("gives each of 3 runs one picture and keeps the 3 asked for", () => {
+ const r = splitRunRequest(ask, 3, 1);
+ expect(r.params?.numOutputs).toBe(1);
+ expect(r.askedOutputs).toBe(3);
+ expect(r.params?.seed).toBe(7);
+ expect(ask.params?.numOutputs).toBe(3);
+ });
+
+ it("splits into groups when a run makes several (8 asked, 4 per run)", () => {
+ const r = splitRunRequest({ ...ask, params: { numOutputs: 8 } }, 2, 4);
+ expect(r.params?.numOutputs).toBe(4);
+ expect(r.askedOutputs).toBe(8);
+ });
+
+ it("leaves a run that isn't split as it was", () => {
+ expect(splitRunRequest(ask, 1, 3)).toBe(ask);
+ const one = { ...ask, params: { numOutputs: 1 } };
+ expect(splitRunRequest(one, 1, 1)).toBe(one);
+ });
+});
+
+describe("Reuse of one run of a split", () => {
+ it("brings back the number asked for", () => {
+ const run = splitRunRequest(ask, 3, 1);
+ expect(reuseOutputs(run)).toBe(3);
+ expect(draftFromRequest(run, IMAGE_STUDIO, getModel, getPreset).params?.numOutputs).toBe(3);
+ });
+
+ it("keeps a run's own number when it wasn't split", () => {
+ expect(reuseOutputs({ params: { numOutputs: 2 } })).toBe(2);
+ expect(draftFromRequest({ ...ask, params: { numOutputs: 2 } }, IMAGE_STUDIO, getModel, getPreset).params?.numOutputs).toBe(2);
+ });
+});