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.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
/**
* How far a ComfyUI job is, from what its socket reports (each node that starts, and the steps of a node that has
* them), as one share from 0 to 0.99 that every place showing the run reads: its card, the studio feeds, the Library,
* the API and the admin.
*
* Most of a job's time is its sampler's steps, while the sampler is one node of many, so the share of nodes run says
* little: a graph whose loaders and encoders run first reads three quarters done before its first step. Here the bar has
* stages instead:
*
* - getting ready (its models loading, its prompt encoding): a small share that grows slowly with time, at most
* PROGRESS_BANDS.ready;
* - sampling: most of the bar, by the steps of each sampler pass the graph has (in the order they run), and between two
* steps by the time a step takes;
* - finishing (decoding, sound, saving the files on the GPU): the next few percent, by the nodes after the sampler;
* - collecting (the results coming to the studio): the last percent.
*
* The share never passes 0.99 before the run is done; the server keeps the highest share shown in one try (`shown`), so
* it never moves backwards there. A new try starts again from its own first stage.
*
* Pure: the server and the tests share it.
*/
import { JOB_PHRASES } from "./job-phrases";
import { madeNoun, makingWords, POOL_WORDS, type MadeKind } from "./relay";
/** Where each stage ends on the bar. */
export const PROGRESS_BANDS = {
/** Getting ready: up to here, slowly. */
ready: 0.1,
/** The sampler passes the graph has: up to here. */
sampled: 0.9,
/** Passes beyond the graph's own (a loop that runs its sampler again): each takes half of what is left, up to here. */
extra: 0.94,
/** The nodes after the sampler: up to here. */
finished: 0.97,
/** Never more before the run is done. */
most: 0.99,
} as const;
/** The time over which getting ready grows to about two thirds of its band (a cold model load takes minutes). */
const READY_MS = 90_000;
/** Between two steps, the time a step takes fills at most this much of the next one. */
const IN_STEP = 0.9;
/** The step passes kept per try (the newest). */
const KEEP_PASSES = 16;
/** One pass of a node with steps, as its reports said. */
export interface StepPass {
/** The node's id, when a report named it. */
node?: string;
/** How many nodes had started when it ran (the running node included): which node of the job it is. */
at: number;
value: number;
max: number;
/** The first value seen, and when; when the value now was first seen. */
v0: number;
t0: number;
tv: number;
}
/** A job's live progress as the server keeps it (relay_tasks.progress_json, or the socket's own record). */
export interface LiveProgress {
value?: number;
max?: number;
node?: string;
nodesDone?: number;
cold?: boolean;
/** When this try first reported. */
since?: number;
/** The passes with steps of this try, oldest first. */
passes?: StepPass[];
/** The highest share shown in this try. */
shown?: number;
}
/** What a graph says about its stages: its node count, the nodes that sample, and how many nodes come after them. */
export interface ProgressPlan {
nodes: number;
samplers: string[];
after: number;
}
type GraphNode = { class_type?: unknown; inputs?: Record<string, unknown> };
/** A node that runs steps: KSampler, KSamplerAdvanced, SamplerCustom(Advanced), and a pack's own *Sampler nodes. */
const SAMPLER_CLASS = /^(?:SamplerCustom(?:Advanced)?|\w*[A-Za-z0-9]Sampler(?:Advanced)?)$/;
const isLink = (v: unknown): v is [string, number] => Array.isArray(v) && v.length === 2 && typeof v[0] === "string" && typeof v[1] === "number";
/**
* A graph's plan (ComfyUI's API format: node id → { class_type, inputs }). A sampler is a node of a sampler class that
* takes links (a sampler's settings node, such as KSamplerSelect or a pack's sampler choice, takes none, or feeds
* another node's `sampler` input). `after`: the nodes that read a sampler's result, directly or not (the decode, the
* sound, the file).
*/
export function progressPlan(graph: unknown): ProgressPlan {
const g = graph && typeof graph === "object" ? (graph as Record<string, GraphNode | undefined>) : {};
const ids = Object.keys(g).filter((k) => g[k] && typeof g[k] === "object");
const settings = new Set<string>();
const readers = new Map<string, string[]>();
for (const id of ids) {
for (const [name, v] of Object.entries(g[id]!.inputs ?? {})) {
if (!isLink(v)) continue;
if (name === "sampler") settings.add(v[0]);
readers.set(v[0], [...(readers.get(v[0]) ?? []), id]);
}
}
const samplers = ids.filter((id) => {
const n = g[id]!;
return typeof n.class_type === "string" && SAMPLER_CLASS.test(n.class_type) && !settings.has(id) && Object.values(n.inputs ?? {}).some(isLink);
});
const sampler = new Set(samplers);
const after = new Set<string>();
const todo = [...samplers];
while (todo.length) {
for (const r of readers.get(todo.pop()!) ?? []) {
if (after.has(r) || sampler.has(r)) continue;
after.add(r);
todo.push(r);
}
}
return { nodes: ids.length, samplers: samplers.slice(0, 64), after: after.size };
}
/** A plan kept in a run's meta, read back (null: none, or not one). */
export function planOf(v: unknown): ProgressPlan | null {
if (!v || typeof v !== "object") return null;
const p = v as Record<string, unknown>;
const nodes = Number(p.nodes);
const after = Number(p.after);
if (!Number.isFinite(nodes) || !Number.isFinite(after) || !Array.isArray(p.samplers)) return null;
return { nodes, after, samplers: p.samplers.filter((s): s is string => typeof s === "string").slice(0, 64) };
}
const num = (v: unknown) => (typeof v === "number" && Number.isFinite(v) && v >= 0 ? v : undefined);
/**
* A new report (a progress report or a beat) over what was kept: its fields over the old ones, and its steps noted as a
* pass. A node that started since (more nodes started, no step yet) clears the step of the node before, so a finished
* sampler's last step is never read as the step now. Fewer nodes started than before: its ComfyUI runs the job again
* from the start, so its passes start again (the share shown stays).
*/
export function trackReport(prev: LiveProgress | undefined, r: { value?: unknown; max?: unknown; node?: unknown; nodesDone?: unknown }, now: number): LiveProgress {
const value = num(r.value);
const max = num(r.max);
const done = num(r.nodesDone);
const node = typeof r.node === "string" && r.node ? r.node.slice(0, 64) : undefined;
// This try's clock starts with its first word from the running job (a place in a queue isn't one).
const running = value !== undefined || max !== undefined || done !== undefined || node !== undefined;
const next: LiveProgress = { ...prev };
if (next.since === undefined && running) next.since = now;
let passes = prev?.passes ?? [];
if (done !== undefined && prev?.nodesDone !== undefined && done < prev.nodesDone) passes = [];
const moved = done !== undefined && done !== prev?.nodesDone;
if (done !== undefined) next.nodesDone = done;
if (node) next.node = node;
if (value !== undefined && max) {
next.value = value;
next.max = max;
const at = done ?? prev?.nodesDone ?? 0;
// A beat names no node: the node a report named at the same place is the one with these steps.
const named = node ?? (at === prev?.nodesDone ? prev?.node : undefined);
const last = passes.at(-1);
const same = last && last.at === at && last.max === max && (!named || !last.node || last.node === named);
// The same node again from its first steps (a loop that runs it once more): a new pass.
const again = same && last.value >= max / 2 && value <= max / 4 && value < last.value;
if (same && !again) {
if (value > last.value || (named && !last.node)) passes = [...passes.slice(0, -1), { ...last, ...(value > last.value && { value, tv: now }), ...(named && !last.node && { node: named }) }];
} else {
passes = [...passes, { ...(named && { node: named }), at, value, max, v0: value, t0: now, tv: now }].slice(-KEEP_PASSES);
}
} else if (moved || (node && node !== prev?.node)) {
delete next.value;
delete next.max;
}
if (passes.length) next.passes = passes;
else delete next.passes;
return next;
}
/** The stages a running job shows. */
export type ProgressStage = "loading" | "preparing" | "sampling" | "finishing";
export interface ProgressView {
/** 0 to 0.99. */
share: number;
stage: ProgressStage;
/** The sampler's step now, while it samples. */
step?: { value: number; max: number };
}
const clamp01 = (n: number) => Math.min(1, Math.max(0, n));
/**
* How far a running job is (`plan`: its graph's plan, or null when the run has none: then every node with steps counts
* as a sampler pass, and each pass seen so far as one of the graph's). `now`: the server's clock. Not floored: the
* caller keeps the highest share shown (`shown`).
*/
export function progressView(p: LiveProgress | undefined, plan: ProgressPlan | null, now: number): ProgressView {
const B = PROGRESS_BANDS;
const since = p?.since ?? now;
const all = p?.passes ?? (p?.max && p.value !== undefined ? [{ node: p.node, at: p.nodesDone ?? 0, value: p.value, max: p.max, v0: p.value, t0: now, tv: now }] : []);
const named = plan?.samplers.length ? new Set(plan.samplers) : null;
// The sampler passes: the graph's samplers' (a pass with no node name counts), or every pass with steps when the
// graph names none or its names never come up.
const mine = named ? all.filter((x) => !x.node || named.has(x.node)) : all;
const passes = mine.length ? mine : all;
if (!passes.length) {
const ready = B.ready * (1 - Math.exp(-Math.max(0, now - since) / READY_MS));
return { share: ready, stage: p?.cold ? "loading" : "preparing" };
}
const planned = Math.max(1, named && mine.length ? plan!.samplers.length : passes.length);
const last = passes[passes.length - 1];
const done = p?.nodesDone ?? last.at;
// Still in the last pass's node: its steps, and the time since the last one (at the pace of the steps before it).
const inLast = done <= last.at;
const perStep = last.value > last.v0 ? (last.tv - last.t0) / (last.value - last.v0) : 0;
const inStep = inLast && perStep > 0 && last.value < last.max ? Math.min(IN_STEP, Math.max(0, now - last.tv) / perStep) : 0;
const frac = inLast ? clamp01((last.value + inStep) / last.max) : 1;
// Pass k of `planned` (from 0): its share of the sampling band; a pass beyond the graph's own takes half of what is left.
const k = passes.length - 1;
const sampled = k < planned ? B.ready + (B.sampled - B.ready) * ((k + frac) / planned) : B.sampled + (B.extra - B.sampled) * (1 - Math.pow(0.5, k - planned + frac));
if (inLast) return { share: Math.min(B.most, sampled), stage: "sampling", ...(last.value >= 1 && { step: { value: Math.min(last.value, last.max), max: last.max } }) };
// Between two of the graph's passes.
if (passes.length < planned) return { share: Math.min(B.most, sampled), stage: "sampling" };
// After the last pass: the nodes that read its result, each started one counting as half done (or by its own steps).
const after = Math.max(1, plan?.after || Math.max(1, (plan?.nodes ?? 0) - last.at));
const own = p?.max && p.value !== undefined ? clamp01(p.value / p.max) : 0.5;
const ran = clamp01((done - last.at - 1 + own) / after);
const from = Math.max(sampled, B.sampled);
return { share: Math.min(B.most, from + (B.finished - from) * ran), stage: "finishing" };
}
/** The share while the results come to the studio (`received`: the share of their bytes that arrived). */
export const collectingShare = (received: number | undefined) => PROGRESS_BANDS.finished + (PROGRESS_BANDS.most - PROGRESS_BANDS.finished) * clamp01(received ?? 0);
/** The share to show: never below the highest shown in this try, never above 0.99. */
export const floored = (share: number, shown: number | undefined) => Math.min(PROGRESS_BANDS.most, Math.max(share, shown ?? 0));
/** A bar by time never shows more than this while the job runs (the time is a lane's typical one, not this job's). */
export const TIME_SHARE_MOST = 0.95;
/** "About N min left" only while at least this much of the typical time is left (never "about 0 min left"). */
const LEFT_MIN_MS = 60_000;
/** What a person's card shows for a job on the workers: a share, or none, and about how many minutes are left. */
export interface CardProgress {
/** 0 to 0.99, or null: no honest share to show. */
share: number | null;
/** About how many minutes are left (whole, at least 1), only from the lane's typical run time. */
leftMin?: number;
}
/**
* A running job's progress as a person's card shows it (a job on this install's GPUs), honest about what is known:
*
* - a step bar only while its sampler reports its steps (`view.step`);
* - otherwise a bar by time from the lane's typical run (`typicalMs`, against `runMs`, how long it has run), never above
* TIME_SHARE_MOST, and "about N min left" only from that typical time, while a minute or more of it is left;
* - otherwise no share at all (a count of nodes says little about the time a job takes).
*
* A share is never below the highest one shown in this try (`shown`), so the bar never moves back.
*/
export function cardProgress(view: Pick<ProgressView, "stage" | "share" | "step">, o: { runMs: number; typicalMs: number | null; shown?: number }): CardProgress {
if (view.stage === "sampling" && view.step) return { share: floored(view.share, o.shown) };
if (!o.typicalMs || o.typicalMs <= 0) return { share: null };
const ran = Math.max(0, o.runMs);
const left = o.typicalMs - ran;
return { share: floored(Math.min(TIME_SHARE_MOST, ran / o.typicalMs), o.shown), ...(left >= LEFT_MIN_MS && { leftMin: Math.max(1, Math.round(left / 60_000)) }) };
}
/** "about 4 min left" (null: nothing to say). */
export const leftWords = (leftMin: number | undefined): string | null => (leftMin && leftMin >= 1 ? `about ${Math.round(leftMin)} min left` : null);
/** Words with "about N min left" after them, when there is a time to say. */
export const withLeft = (words: string, leftMin: number | undefined): string => {
const left = leftWords(leftMin);
return left ? `${words} · ${left}` : words;
};
/** "Step 3 of 6". */
export const stepWords = (step: { value: number; max: number }) => `Step ${Math.min(step.value, step.max)} of ${step.max}`;
/** "50%": the sampler's step as a whole percent of its steps (0–100). */
export const stepPercent = (step: { value: number; max: number }): string =>
`${step.max > 0 ? Math.min(100, Math.max(0, Math.round((step.value / step.max) * 100))) : 0}%`;
/** A person's words for a running job on this install's GPUs: "Getting ready", "Creating · 50%", "Almost done". */
export function poolRunWords(v: Pick<ProgressView, "stage" | "step">): string {
if (v.stage === "loading") return POOL_WORDS.gettingReady;
if (v.stage === "finishing") return POOL_WORDS.finishing;
return v.step ? `${POOL_WORDS.working} · ${stepPercent(v.step)}` : POOL_WORDS.working;
}
/** "Creating · 50%": POOL_WORDS.working with a percent. */
const POOL_PERCENT = new RegExp(`^${POOL_WORDS.working} · \\d{1,3}%$`);
/** Is this one of poolRunWords' (the words a person's running job may show)? */
export const isPoolRunWords = (m: string | null | undefined): m is string => {
if (!m) return false;
// Each may say about how long is left (withLeft).
const words = m.replace(/ · about \d+ min left$/, "");
if (words === POOL_WORDS.working || words === POOL_WORDS.gettingReady || words === POOL_WORDS.finishing || POOL_PERCENT.test(words)) return true;
// A job's phrase in place of "Creating" (src/lib/job-phrases.ts), with its percent or without.
return JOB_PHRASES.has(words.replace(/ · \d{1,3}%$/, ""));
};
/** Has a person's run on this install's GPUs started, as these words say ("Creating", a percent, "Almost done")? */
export const startedWords = (m: string | null | undefined): boolean => isPoolRunWords(m) && !m.startsWith(POOL_WORDS.gettingReady);
/**
* A person's run on this install's GPUs once it has started (`peak`: the highest share it was shown since; null: not
* started yet), as it is shown: it never goes back. Taken up again after its GPU let it go (in line, getting ready, or
* running below the peak), it shows "Creating" with no percent (the time left may stay) and the peak's share (none at
* 0), until the new attempt passes the peak. Null: shown as it is.
*/
export function heldRun(
r: { status: string; message: string | null; progress: number | null },
peak: number | null | undefined,
ready: boolean,
): { status: "running"; message: string; progress: number | null } | null {
if (peak === null || peak === undefined || (r.status !== "queued" && r.status !== "running")) return null;
const running = r.status === "running" && !ready && startedWords(r.message);
// Running at or past the peak, or finishing with no share to show (a count of nodes is none): as it is.
if (running && (r.progress === null ? r.message!.startsWith(POOL_WORDS.finishing) : r.progress >= peak - 1e-9)) return null;
const left = running ? (/ · about \d+ min left$/.exec(r.message ?? "")?.[0] ?? "") : "";
return { status: "running", message: `${POOL_WORDS.working}${left}`, progress: peak > 0 ? peak : null };
}
/** The owner's words while a run's model loads. */
export const OWNER_LOADING_WORDS = "Loading the model";
/** The owner's words for a running job: its stage, and the sampler's step ("Making your clip · step 3 of 6"). */
export function runWords(kind: MadeKind, v: Pick<ProgressView, "stage" | "step">): string {
if (v.stage === "loading") return OWNER_LOADING_WORDS;
if (v.stage === "finishing") return `Finishing your ${madeNoun(kind)}`;
return makingWords(kind, v.step?.value, v.step?.max);
}