Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.
TypeScript ai-photo-editorcomfyui-alternativegenerative-fillimage-editingimage-generationinpaintinglinuxlocal-aimacosmcpmcp-servernano-banana-alternativeofflinephoto-editorprivacyqwen-imageqwen-image-2-1stable-diffusion-cppvulkanwindows
// Pure UI logic (unit-tested in logic.test.ts).
import type { Advanced, GenerationRecord, GenerationRequest, InpaintAction, InputRecord, InstallItem, InstallStatus, JobView, LoraItem, LutInfo, MacBenchState, Mode, ModelsStatus, StatusResponse, OutpaintSpec, Plan, Quality, ResolutionMode, Size } from "./types";
export interface Ref {
key: string;
/** "input:<id>" or "gen:<id>" */
source: string;
width: number;
height: number;
name: string;
/** HEIC source with an HDR gain map. */
hdr?: boolean;
}
export interface Composer {
prompt: string;
refs: Ref[];
/** Mask painted on refs[0] (input id of the mask PNG); `marks`: the coloured marks drawn there
* (input id of a transparent PNG), which the prompt names by colour. */
mask: { inputId: string; refKey: string; marks?: string } | null;
/** What to do in the painted area. */
inpaintAction: InpaintAction;
outpaint: OutpaintSpec | null;
mode: Mode;
aspect: string;
quality: Quality;
resolution: ResolutionMode;
customWidth: number;
customHeight: number;
seed: number | null;
advanced: Advanced;
/** Prompt enhancer on (remembered). */
enhance: boolean;
/** Prompt+ writes its prompt into the box first, to read and change before making the image
* (remembered); otherwise it writes and the image follows. */
enhanceReview: boolean;
/** Prompt+ "Detailed": longer prompts with more context (remembered). */
enhanceDetail: boolean;
/** The user's own words when the prompt in the box was written by Prompt+ (a review, or a
* reused enhanced prompt): sent as is, without rewriting it again. */
enhancedFrom: string | null;
/** "New image" chosen: images added to the prompt are references for a new image (people,
* products, a style), not the image to edit. */
create: boolean;
/** LoRAs switched on in this project; null = the ones on by default (`activeLoras`). */
loras: string[] | null;
}
/** A preset name from any version: older ones also had "turbo" (now Fast) and "ultra" (now Quality). */
export function normalizeQuality(q: unknown, fallback: Quality = "fast"): Quality {
if (q === "fast" || q === "balanced" || q === "quality") return q;
if (q === "turbo") return "fast";
if (q === "ultra") return "quality";
return fallback;
}
export const emptyComposer = (prefs?: Partial<Composer>): Composer => ({
prompt: "",
refs: [],
mask: null,
// Remove first: the most common brushed edit, and the one that needs no words.
inpaintAction: "remove",
outpaint: null,
mode: "auto",
aspect: prefs?.aspect ?? "auto",
quality: normalizeQuality(prefs?.quality),
resolution: prefs?.resolution ?? "auto",
customWidth: prefs?.customWidth ?? 1024,
customHeight: prefs?.customHeight ?? 1024,
seed: null,
advanced: newChatAdvanced(prefs?.advanced),
enhance: prefs?.enhance === true,
enhanceReview: prefs?.enhanceReview === true,
enhanceDetail: prefs?.enhanceDetail === true,
enhancedFrom: null,
create: false,
loras: null,
});
/** The prompt makes a new image from the reference images (rather than editing image 1). */
export const composing = (c: Pick<Composer, "create" | "refs">): boolean => c.create && c.refs.length > 0;
/** The prompt edits image 1 (with any other images as references). */
export const editing = (c: Pick<Composer, "create" | "refs">): boolean => !c.create && c.refs.length > 0;
/** LoRAs one image can use at once (`lora::MAX_ACTIVE`). */
export const MAX_LORAS = 3;
/** The LoRAs a request from this composer uses: the project's choice, else the ones on by
* default; only loaded ones whose copy is there, at most `MAX_LORAS`. */
export function activeLoras(c: Pick<Composer, "loras">, all: LoraItem[]): string[] {
const usable = new Set(all.filter((l) => l.available).map((l) => l.id));
const chosen = c.loras ?? all.filter((l) => l.onByDefault).map((l) => l.id);
return chosen.filter((id) => usable.has(id)).slice(0, MAX_LORAS);
}
/** The project's LoRA choice after switching `id` on or off. */
export function toggleLora(c: Pick<Composer, "loras">, all: LoraItem[], id: string): string[] {
const now = activeLoras(c, all);
if (now.includes(id)) return now.filter((x) => x !== id);
return now.length < MAX_LORAS ? [...now, id] : now;
}
/** Advanced settings a new chat starts with: everything kept except Reference detail, which
* every chat starts at the standard 1 MP. */
function newChatAdvanced(a: Advanced | undefined): Advanced {
const { reference_detail: _, ...rest } = a ?? {};
return rest;
}
let counter = 0;
export const newKey = () => `k${Date.now().toString(36)}${(counter++).toString(36)}`;
export const refFromInput = (r: InputRecord): Ref => ({ key: newKey(), source: `input:${r.id}`, width: r.width, height: r.height, name: r.original_name ? decodeSafe(r.original_name) : "Image", hdr: r.hdr || undefined });
export const refFromGen = (g: GenerationRecord): Ref => ({ key: newKey(), source: `gen:${g.id}`, width: g.width, height: g.height, name: "Result" });
function decodeSafe(s: string) {
try {
return decodeURIComponent(s);
} catch {
return s;
}
}
/** Moves a reference, keeping all others in order. */
export function moveRef(refs: Ref[], from: number, to: number): Ref[] {
if (from === to || from < 0 || from >= refs.length) return refs;
const out = refs.slice();
const [r] = out.splice(from, 1);
out.splice(Math.max(0, Math.min(to, out.length)), 0, r);
return out;
}
export function hasOutpaint(o: OutpaintSpec | null): boolean {
return !!o && (o.left > 0 || o.right > 0 || o.top > 0 || o.bottom > 0);
}
/** A mask painted on image 1 that the request uses (not for a new image from references). */
const maskInUse = (c: Composer) => !composing(c) && !!c.mask && !!c.refs[0] && c.mask.refKey === c.refs[0].key;
const outpaintInUse = (c: Composer) => !composing(c) && hasOutpaint(c.outpaint);
/** Composer state -> stable service request. */
export function buildRequest(c: Composer, parentId?: string | null): GenerationRequest {
const maskValid = maskInUse(c);
const custom = c.resolution === "custom";
// Inpaint and expand keep the source's shape; create, edit and combine take the chosen ratio.
const shaped = !maskValid && !outpaintInUse(c);
// Written by Prompt+ already (from the user's words, or from none: "" for a removal or Expand).
const reviewed = c.enhancedFrom != null && !!c.prompt.trim();
return {
prompt: c.prompt.trim(),
mode: composing(c) ? "compose" : c.mode,
images: c.mode === "generate" && !composing(c) ? [] : c.refs.map((r) => r.source),
mask: maskValid ? `input:${c.mask!.inputId}` : null,
...(maskValid ? { inpaint_action: c.inpaintAction } : {}),
...(maskValid && c.mask!.marks ? { marks: `input:${c.mask!.marks}` } : {}),
outpaint: outpaintInUse(c) ? c.outpaint : null,
quality: c.quality,
resolution: c.resolution,
aspect_ratio: shaped && c.aspect !== "auto" && !custom ? c.aspect : null,
width: custom ? c.customWidth : null,
height: custom ? c.customHeight : null,
seed: c.seed,
preserve_dimensions: true,
advanced: cleanAdvanced(c.advanced),
client: "ui",
parent_id: parentId ?? null,
// A prompt Prompt+ wrote already is sent as it is, with the user's words kept beside it.
...(reviewed ? { enhanced_from: c.enhancedFrom } : c.enhance ? { enhance: true, ...(c.enhanceDetail ? { enhance_detail: true } : {}) } : {}),
};
}
/** Prompt+ in review mode: Enter first has the prompt written into the box (no image yet). A
* removal or an expansion with nothing typed is always written into the box first: Prompt+ names
* what is removed, or what the new areas show, for the user to approve or change. Prompt+ works on
* everything the prompt box makes: new images, edits, combines, coloured marks, a painted area
* (crates/qls-core/src/enhancer.rs AREA_SYSTEM_PROMPT) and Expand (EXPAND_SYSTEM_PROMPT). */
export function wantsPromptReview(c: Composer): boolean {
if (!c.enhance || c.enhancedFrom != null) return false;
if (!typedWords(c)) return !needsPrompt(c) && (maskInUse(c) || outpaintInUse(c));
return c.enhanceReview;
}
/** The request that only writes the prompt (Prompt+ review). */
export function reviewRequest(c: Composer): GenerationRequest {
return { ...buildRequest(c), enhance: true, enhance_only: true };
}
/** The painted area in use is coloured marks (the prompt names them by colour). */
export const marksInUse = (c: Composer) => maskInUse(c) && !!c.mask!.marks;
/** The words each action of a painted area puts at the start of the prompt (the service takes
* them off again: planner.rs InpaintAction::subject). */
export const ACTION_VERB: Record<InpaintAction, string> = { remove: "Remove", edit: "Change", replace: "Replace with", add: "Add" };
const VERB_RE = /^\s*(?:remove|change|replace with|replace|add)\b[ \t]*/i;
/** The prompt with `action`'s words in front, in place of another action's. */
export function withActionVerb(prompt: string, action: InpaintAction): string {
return `${ACTION_VERB[action]} ${prompt.replace(VERB_RE, "")}`;
}
/** What the user typed: for a painted area, without the action's own words ("Remove " alone is nothing). */
export function typedWords(c: Composer): string {
return maskInUse(c) && !c.mask!.marks ? c.prompt.replace(VERB_RE, "").trim() : c.prompt.trim();
}
/** False for a removal in a painted area, which needs no description, and for Expand, which
* continues the scene (Prompt+ can write what the new areas show). */
export function needsPrompt(c: Composer): boolean {
return !((maskInUse(c) && !c.mask!.marks && c.inpaintAction === "remove") || outpaintInUse(c));
}
/** Advanced fields that change how the model samples. They last for the session only: a value
* typed once must not silently override the presets after a restart. */
const SAMPLING_OVERRIDES = ["sampler", "strength", "reference_detail"];
/** Fields of older versions that Turbo's fixed schedule no longer uses (may sit in saved
* preferences, projects and gallery records): dropped everywhere. */
const REMOVED_ADVANCED = ["steps", "cfg", "scheduler", "flow_shift", "refine"];
/** Labels of the sampling overrides currently set, e.g. ["Sampler heun", "Strength 0.8"]. */
export function activeOverrides(a: Advanced): string[] {
const label: Record<string, string> = { sampler: "Sampler", strength: "Strength", reference_detail: "Reference" };
return Object.entries(cleanAdvanced(a))
.filter(([k]) => SAMPLING_OVERRIDES.includes(k))
.map(([k, v]) => `${label[k]} ${v}`);
}
/** `a` without the session-only sampling overrides (what is kept in saved preferences). */
export function persistentAdvanced(a: Advanced | undefined): Advanced {
const out = cleanAdvanced(a ?? {});
for (const k of SAMPLING_OVERRIDES) delete (out as Record<string, unknown>)[k];
return out;
}
function cleanAdvanced(a: Advanced): Advanced {
const out: Advanced = {};
for (const [k, v] of Object.entries(a)) {
if (v !== null && v !== undefined && v !== "" && !REMOVED_ADVANCED.includes(k)) (out as Record<string, unknown>)[k] = v;
}
return out;
}
/** Restores composer fields from a stored generation (gallery "reopen"). */
export function composerFromRecord(g: GenerationRecord, base: Composer): Composer {
const req = g.request;
const refs: Ref[] = g.references.map((r) => ({ key: newKey(), source: `input:${r.input_id}`, width: r.width, height: r.height, name: `Image ${r.index + 1}` }));
const c: Composer = {
...base,
// An enhanced result is the user's prompt with the enhancer on (Reuse prompt takes its text).
prompt: g.original_prompt ?? g.prompt,
enhance: !!g.original_prompt,
enhancedFrom: null,
create: g.mode === "compose" && refs.length > 0,
refs,
mask: g.mask_input_id && refs[0] ? { inputId: g.mask_input_id, refKey: refs[0].key } : null,
inpaintAction: req?.inpaint_action ?? "edit",
outpaint: g.outpaint ? { ...g.outpaint, units: "pixels" } : null,
// Upscales are started from the image toolbar, not the composer.
mode: req?.mode && req.mode !== "upscale" && req.mode !== "compose" && g.mode !== "lut" && g.mode !== "layers" ? req.mode : "auto",
quality: normalizeQuality(g.quality, base.quality),
resolution: req?.resolution ?? base.resolution,
aspect: req?.aspect_ratio ?? "auto",
// A seed the user fixed is a setting too; a random one stays random.
seed: req?.seed ?? null,
advanced: cleanAdvanced(req?.advanced ?? {}),
// The LoRAs it was made with (none for images made before LoRAs or without them).
loras: (g.loras ?? []).map((l) => l.id),
};
if (req?.width && req?.height) {
c.customWidth = req.width;
c.customHeight = req.height;
}
return c;
}
/** A variation: same recipe, new random seed. */
export function variationRequest(g: GenerationRecord): GenerationRequest | null {
if (!g.request) return null;
return { ...g.request, seed: null, client: "ui", parent_id: g.id };
}
export const MODE_LABEL: Record<Mode | "lut" | "layers", string> = {
auto: "Auto",
generate: "Create",
edit: "Edit",
inpaint: "Inpaint",
outpaint: "Expand",
multi_reference: "Combine",
compose: "From references",
upscale: "Upscale",
lut: "LUT",
layers: "Layers",
};
/** How a LUT is shown: a preset ("kokon:<key>") by its title, a user's LUT by its file name. */
export function lutLabel(l: { name: string; title?: string | null }): string {
return l.name.startsWith("kokon:") ? l.title || l.name.slice(6) : l.name;
}
/** The LUT a "lut" result was made with. */
export function lutInfo(g: GenerationRecord): LutInfo | null {
return g.mode === "lut" ? ((g.request as unknown as { lut?: LutInfo } | null)?.lut ?? null) : null;
}
/** Short names within a project. Every image that starts something new (a new image, a new
* seed of one, an import) gets the next number: "1", "2"… Everything made from it (edits,
* edits of edits, combinations led by it, upscales) is numbered within it: "1.1", "1.2"…
* `from`/`variant` as in timelineLinks. */
export function versionLabels(items: { key: string; source: string | null; from: string[]; variant?: boolean }[]): Record<string, string> {
const links = timelineLinks(items);
const out: Record<string, string> = {};
const family: Record<string, string> = {};
const count: Record<string, number> = {};
let images = 0;
for (const it of items) {
const parent = links[it.key].parents[0];
if (!parent || it.variant) {
const n = String(++images);
family[it.key] = n;
out[it.key] = n;
} else {
const f = family[parent];
family[it.key] = f;
count[f] = (count[f] ?? 0) + 1;
out[it.key] = `${f}.${count[f]}`;
}
}
return out;
}
export interface ArcPlace {
/** 0 = lowest level; an arc sits above every shorter arc it spans or overlaps. */
lane: number;
/** Horizontal shift of each end from its thumbnail's centre, so arcs meeting at one
* thumbnail do not share a point. */
dx1: number;
dx2: number;
}
/** Places arcs between items at positions `a` -> `b` (indexes along the timeline). */
export function arcLayout(arcs: { a: number; b: number }[], spread = 8, maxShift = 16): ArcPlace[] {
const lanes: { lo: number; hi: number }[][] = [];
const order = arcs.map((_, i) => i).sort((i, j) => Math.abs(arcs[i].b - arcs[i].a) - Math.abs(arcs[j].b - arcs[j].a) || Math.min(arcs[i].a, arcs[i].b) - Math.min(arcs[j].a, arcs[j].b));
const out: ArcPlace[] = arcs.map(() => ({ lane: 0, dx1: 0, dx2: 0 }));
for (const i of order) {
const lo = Math.min(arcs[i].a, arcs[i].b);
const hi = Math.max(arcs[i].a, arcs[i].b);
let lane = 0;
while (lanes[lane]?.some((s) => lo <= s.hi && s.lo <= hi)) lane++;
(lanes[lane] ??= []).push({ lo, hi });
out[i].lane = lane;
}
// Ends at the same item, ordered by where the other end lies (left to right), spread out.
const ends = new Map<number, { arc: number; end: "dx1" | "dx2"; other: number }[]>();
arcs.forEach((r, i) => {
for (const [at, end, other] of [[r.a, "dx1", r.b], [r.b, "dx2", r.a]] as const) {
if (!ends.has(at)) ends.set(at, []);
ends.get(at)!.push({ arc: i, end, other });
}
});
for (const list of ends.values()) {
list.sort((p, q) => p.other - q.other);
const step = list.length > 1 ? Math.min(spread, (2 * maxShift) / (list.length - 1)) : 0;
list.forEach((e, k) => (out[e.arc][e.end] = (k - (list.length - 1) / 2) * step));
}
return out;
}
/** "Image 1.2" for a short name, for sentences and headings. */
export const imageName = (label: string | undefined) => (label ? `Image ${label}` : "Image");
/** How a version joins the timeline: "start" begins a new line (a new image or an import),
* "next" continues from the version just before it, "variant" is the version before made
* again with a new seed, "branch" comes from an earlier version, "combine" from several. */
export type LinkKind = "start" | "next" | "variant" | "branch" | "combine";
export interface TimelineLink {
kind: LinkKind;
/** Keys of the versions this one was made from (in reference order). */
parents: string[];
}
/** Links between versions. `from` are the "gen:<id>"/"input:<id>" sources an item was made
* from; `variant` marks a new seed of `from[0]` rather than an edit of it. Sources that are
* not in the project are ignored. */
export function timelineLinks(items: { key: string; source: string | null; from: string[]; variant?: boolean }[]): Record<string, TimelineLink> {
const bySource = new Map<string, string>();
const out: Record<string, TimelineLink> = {};
items.forEach((it, i) => {
const parents = [...new Set(it.from.map((f) => bySource.get(f)).filter((k): k is string => !!k && k !== it.key))];
const prev = items[i - 1]?.key;
const kind: LinkKind = !parents.length ? "start" : parents.length > 1 ? "combine" : parents[0] !== prev ? "branch" : it.variant ? "variant" : "next";
out[it.key] = { kind, parents };
if (it.source) bySource.set(it.source, it.key);
});
return out;
}
/** "1m ago"-style age for the project list. */
export function timeAgo(ms: number, now = Date.now()): string {
const s = Math.max(0, (now - ms) / 1000);
if (s < 60) return "just now";
if (s < 3600) return `${Math.floor(s / 60)}m ago`;
if (s < 86400) return `${Math.floor(s / 3600)}h ago`;
if (s < 86400 * 7) return `${Math.floor(s / 86400)}d ago`;
return new Date(ms).toLocaleDateString();
}
/** A project's title as the list shows it: an image file's name without its extension. */
export function projectTitle(title: string): string {
return title.replace(/\.(png|jpe?g|webp|heic|heif|avif|gif|bmp|tiff?)$/i, "").trim() || title;
}
/** A file name that says nothing about the picture ("image.png", "4.png", "IMG_1234.JPG",
* "Screenshot 2026-10-01 at 10.00.00"): a project named after it takes its first prompt instead. */
export function isGenericName(name: string): boolean {
// "Screenshot 2026-10-01 at 10.00.00": the "at" of a date says nothing either.
const n = projectTitle(name).toLowerCase().replace(/ (at|alle) /g, " ");
return (
/^[\d\s_.()-]*$/.test(n) ||
/^(image|img|photo|pic|picture|untitled|unnamed|download|file|imported image|whatsapp image|screenshot|screen shot|schermata|immagine|foto)[\s\d_.()\-:,]*$/.test(n) ||
/^(img|dsc|dscn|pxl|mvimg)[_-]?\d[\d_.-]*$/.test(n)
);
}
/** The heading a project goes under in the list, by when it was last changed. */
export function dayGroup(ms: number, now = Date.now()): string {
const day = (t: number) => {
const d = new Date(t);
return new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
};
const days = Math.round((day(now) - day(ms)) / 86400000);
if (days <= 0) return "Today";
if (days === 1) return "Yesterday";
if (days < 7) return "Previous 7 days";
if (days < 30) return "Previous 30 days";
return "Older";
}
/** Short truthful status text for a job. */
export function jobLabel(j: JobView): string {
switch (j.status) {
case "queued":
return j.queue_position > 1 ? `Queued · ${j.queue_position}` : "Queued";
case "starting_backend":
return "Loading model";
case "preparing":
return "Preparing";
case "enhancing_prompt":
return "Writing the prompt";
case "saving":
return "Saving";
case "done":
return "Done";
case "failed":
return "Failed";
case "cancelled":
return "Cancelled";
case "running": {
const batch = j.image && j.image[1] > 1 ? ` · ${j.image[0]}/${j.image[1]}` : "";
switch (j.stage) {
case "preparing":
return "Preparing";
case "encoding_images":
return "Encoding references";
case "encoding_prompt":
return "Reading prompt";
case "generating":
return j.step ? `Generating · ${j.step[0]}/${j.step[1]}${batch}` : `Generating${batch}`;
case "upscaling":
return j.step ? `Upscaling · ${j.step[0]}/${j.step[1]}${batch}` : `Upscaling${batch}`;
case "refining":
return j.step ? `Refining · ${j.step[0]}/${j.step[1]}${batch}` : `Refining${batch}`;
case "decoding":
return "Decoding";
case "finishing":
return "Finishing";
default:
return "Working";
}
}
}
}
/** What the progress knows besides the job (seconds): time since the job left the queue,
* time in the current phase (status and stage), since the job started running and since the
* last step or tile; whether the prompt enhancer runs for it; and this machine's remembered
* speeds (`progressMemory`). */
export interface JobClock {
elapsed: number;
inPhase: number;
inRunning: number;
sinceStep: number;
enhance: boolean;
/** Seconds the model took to load the last time. */
loadSeconds: number;
/** Seconds the prompt enhancer took per word the last time. */
enhancerSecondsPerWord: number;
}
/** About how many words the built-in enhancer prompts ask for. */
const ENHANCER_WORDS = { generate: 110, compose: 150, edit: 35 };
const SAVE_SECONDS = 1;
/** Time left of a phase estimated at `est` seconds, `t` seconds in. Past ~80 % of the
* estimate it keeps shrinking without reaching 0, so an overrun slows the bar instead of
* stopping it. */
function phaseLeft(est: number, t: number): number {
if (est <= 0) return 0;
return t < 0.8 * est ? est - t : (0.16 * est * est) / t;
}
/** Estimated seconds until the job is done: what is left of the current phase, measured where
* the engine reports progress (steps, tiles, the enhancer's words), plus every phase still to
* come (model load, enhancer, generation, decoding, upscaler, saving). */
export function jobSecondsLeft(j: JobView, c: JobClock): number {
const p = j.plan;
const quick = p?.mode === "upscale";
// The plan's split of the time; without a measured speed, a typical job (corrected live by the
// steps and tiles as they come).
const total = p && p.estimated_seconds > 0 ? p.estimated_seconds : quick ? 8 : 30;
const share = quick ? 1 : Math.min(0.95, Math.max(0, p?.upscale_share ?? 0));
const upS = total * share;
const genS = total - upS;
const decodeS = genS * 0.1;
const batch = Math.max(1, p?.batch_count ?? 1);
const imagesAfter = j.image && j.image[1] > 1 ? j.image[1] - j.image[0] : 0;
const words = ENHANCER_WORDS[p?.mode === "generate" || p?.mode === "compose" ? p.mode : "edit"];
const enhanceS = c.enhance ? 0.5 + words * c.enhancerSecondsPerWord : 0;
const fromGeneration = genS + upS + SAVE_SECONDS;
switch (j.status) {
case "queued":
case "starting_backend":
return phaseLeft(c.loadSeconds, j.status === "queued" ? 0 : c.inPhase) + enhanceS + fromGeneration;
case "preparing":
return phaseLeft(0.5, c.inPhase) + enhanceS + fromGeneration;
case "enhancing_prompt": {
const written = (j.enhanced_prompt ?? "").split(/\s+/).filter(Boolean).length;
// Once it writes, its own pace says how long the rest takes.
const left = written >= 8 ? (Math.max(words - written, words * 0.05) * c.inPhase) / written : phaseLeft(enhanceS, c.inPhase);
return left + fromGeneration;
}
case "saving":
return phaseLeft(SAVE_SECONDS, c.inPhase);
case "done":
case "failed":
case "cancelled":
return 0;
}
switch (j.stage) {
case "upscaling": {
const perImage = upS / batch;
const after = (imagesAfter * upS) / batch + SAVE_SECONDS;
if (j.step && j.step[1] > 0) {
const perTile = j.seconds_per_step ?? perImage / j.step[1];
const within = Math.min(0.95, c.sinceStep / perTile);
return Math.max(0, j.step[1] - j.step[0] - within) * perTile + after;
}
return phaseLeft(perImage, c.inPhase) + after;
}
case "finishing":
return phaseLeft(0.5, c.inPhase) + upS + SAVE_SECONDS;
case "decoding":
return phaseLeft(decodeS / batch, c.inPhase) + (imagesAfter * genS) / batch + upS + SAVE_SECONDS;
case "generating":
case "refining":
if (j.step && j.step[1] > 0) {
const perStep = j.seconds_per_step ?? (0.85 * genS) / batch / j.step[1];
const within = Math.min(0.95, c.sinceStep / perStep);
return Math.max(0, j.step[1] - j.step[0] - within) * perStep + decodeS / batch + (imagesAfter * genS) / batch + upS + SAVE_SECONDS;
}
}
// Encoding, or a server that reports no steps: time against the generation estimate.
return phaseLeft(genS, c.inRunning) + upS + SAVE_SECONDS;
}
/** Completion (0..100) of the whole job, model load to saved file: the time it has taken over
* that time plus the time left (`jobSecondsLeft`). Short of 100 until the job is done. */
export function jobPercent(j: JobView, c: JobClock): number {
if (j.status === "done") return 100;
if (j.status === "queued" || j.status === "failed" || j.status === "cancelled" || c.elapsed <= 0) return 0;
return Math.min(99, (100 * c.elapsed) / (c.elapsed + jobSecondsLeft(j, c)));
}
/** Time left to show, only once the engine's own pace is known (steps or tiles running). */
export function remainingSeconds(j: JobView, c: JobClock): number | null {
if (j.status !== "running" || !j.step || !j.seconds_per_step) return null;
if (j.stage !== "generating" && j.stage !== "upscaling") return null;
return jobSecondsLeft(j, c);
}
// ---------------------------------------------------------------------------------------
// Upscaling
// ---------------------------------------------------------------------------------------
/** Server limits for upscale results (8192 px per side, ~40 MP). */
export const MAX_UPSCALE_SIDE = 8192;
export const MAX_UPSCALE_AREA = 40_000_000;
export interface UpscaleTarget { key: string; label: string; width: number; height: number }
const fits = (s: Size) => s.width >= 64 && s.height >= 64 && s.width <= MAX_UPSCALE_SIDE && s.height <= MAX_UPSCALE_SIDE && s.width * s.height <= MAX_UPSCALE_AREA;
/** Size with the given width and the source's aspect ratio. */
export function sizeForWidth(w: number, h: number, width: number): Size {
return { width: Math.round(width), height: Math.max(1, Math.round((width * h) / w)) };
}
/** Upscale presets for a w×h image: 2x, 4K (3840 long side) and the original source
* size of an edit, when each is a real enlargement within the limits. */
export function upscaleTargets(w: number, h: number, source?: Size | null): UpscaleTarget[] {
const out: UpscaleTarget[] = [];
const add = (key: string, label: string, s: Size) => {
if (fits(s) && s.width * s.height > w * h * 1.05 && !out.some((o) => o.width === s.width && o.height === s.height)) out.push({ key, label, ...s });
};
add("2x", "2×", { width: w * 2, height: h * 2 });
const k4 = w >= h ? sizeForWidth(w, h, 3840) : { width: Math.round((3840 * w) / h), height: 3840 };
add("4k", "4K", k4);
if (source) add("source", "Source size", source);
return out;
}
export function upscaleRequest(source: string, target: Size, parentId?: string | null): GenerationRequest {
return {
prompt: "",
mode: "upscale",
images: [source],
quality: "fast",
resolution: "auto",
preserve_dimensions: true,
advanced: {},
client: "ui",
parent_id: parentId ?? null,
upscale: { method: "quick", width: target.width, height: target.height },
};
}
const dims = (s: Size) => `${s.width}×${s.height}`;
/** Upscaler models offered in Settings: [file stem, label]. Mirrors UPSCALER_CHOICES in config.rs. */
export const UPSCALERS: [string, string][] = [
["4x_NMKD-Superscale-SP_178000_G", "Superscale-SP"],
["4xNomos2_hq_mosr", "MoSR"],
["4x-UltraSharp", "UltraSharp"],
["lanczos", "Lanczos"],
];
const UPSCALER_LABELS: Record<string, string> = Object.fromEntries(UPSCALERS);
/** Short name of the upscaler that ran or will run: "Superscale-SP", "MoSR", "Lanczos"... */
export const upscalerLabel = (name: string | null | undefined) => (!name || name === "Lanczos" ? "Lanczos" : UPSCALER_LABELS[name] ?? name);
/** "1376×768 → 5120×2880 (MoSR)" for the Details panel; two-pass records from
* older versions read "1376×768 → refined 2048×1152 (Fast, 6 steps, UltraSharp) → ...". */
export function pipelineSummary(g: GenerationRecord): string | null {
const p = g.pipeline;
if (!p) return null;
const parts: string[] = [];
if (p.base) {
const mode = p.refine === "best" ? "Best" : "Fast";
parts.push(dims(p.base), `refined ${g.working_width}×${g.working_height} (${mode}, ${p.refine_steps ?? "?"} steps, ${upscalerLabel(p.hires_upscaler)})`);
}
if (p.enlarged_with && p.enlarged_from) {
if (!parts.length) parts.push(dims(p.enlarged_from));
parts.push(`${g.width}×${g.height} (${upscalerLabel(p.enlarged_with)})`);
}
return parts.length ? parts.join(" → ") : null;
}
/** Short description of what a plan will do, for the Advanced panel. */
export function planPipeline(plan: Plan): string {
const out = dims(plan.output);
const enlarge = plan.output.width * plan.output.height > plan.content.width * plan.content.height * 1.21;
const via = upscalerLabel(plan.upscaler);
if (plan.upscale?.method === "quick") return `${dims(plan.upscale.source)} → ${out} (${via})`;
const work = dims(plan.working);
return enlarge ? `${work} render → ${out} output (${via})` : `${work} render → ${out} output`;
}
export function formatDuration(s: number): string {
const t = Math.round(s);
if (t < 60) return `${Math.max(1, t)}s`;
const m = Math.floor(t / 60);
return t % 60 ? `${m}m ${t % 60}s` : `${m}m`;
}
/** Undo target for version `key`: the version it was made from (`from` is that image's
* "gen:<id>"/"input:<id>"), or the version before it when that one is not in the project. */
export function undoTarget(items: { key: string; source: string | null; from: string | null }[], key: string): string | null {
const i = items.findIndex((x) => x.key === key);
if (i < 0) return null;
const from = items[i].from;
const parent = from ? items.find((x) => x.source === from && x.key !== key) : undefined;
if (parent) return parent.key;
return i > 0 ? items[i - 1].key : null;
}
/** 1.2 GB, 340 MB, 12 KB (decimal units, like download sizes on Hugging Face and GitHub). */
export function formatBytes(n: number): string {
if (n >= 1e9) return `${(n / 1e9).toFixed(n >= 1e10 ? 0 : 1)} GB`;
if (n >= 1e6) return `${Math.round(n / 1e6)} MB`;
return `${Math.max(0, Math.round(n / 1e3))} KB`;
}
/** Whether What's new shows at start: once per release `version` after an update (not on a new install, which gets the
* welcome screen), and again while a needed engine update was not postponed. */
export function whatsNewDue(ui: Record<string, unknown>, engine: { release: string; update_needed: boolean } | undefined, version: string): boolean {
if (!ui.welcomeSeen) return false;
return ui.whatsNewSeen !== version || (!!engine?.update_needed && ui.engineUpdateSkipped !== engine.release);
}
/** The image-model step of What's new (the Q4_K_M, 0.11.1): offered to the official files still on the Q4_K and, as a
* suggestion, to own files that are that Q4_K. "Later" waits for the next release `version`; "Keep using my file" is
* never asked again. */
export function modelOfferDue(dit: ModelsStatus["dit"] | undefined, ui: Record<string, unknown>, version: string): boolean {
if (!dit?.update) return false;
if (dit.update === "own" && ui.ownModelKept) return false;
return ui.modelUpdateSkipped !== version;
}
/** What closing What's new remembers about a model step it offered and that did not happen: "keep" (own files) for
* good, otherwise postponed until the next release `version`. */
export function modelChoiceUi(choice: "later" | "keep", version: string): Record<string, unknown> {
return choice === "keep" ? { ownModelKept: true } : { modelUpdateSkipped: version };
}
/** Kokon's Mac engine is the default on Macs since 0.12: switch it on once, at start, when set up, the engine is in
* the app, it is off and nobody chose yet (`macEngineOffered`: the 0.11 offer's "Not now" or this switch). Not below
* 16 GB: the engine peaks at ~9 GB during a 1024² decode (M2 Pro, Benchmark button 2026-10-26) and was never run on 8 GB. */
export function macEngineDefaultDue(st: Pick<StatusResponse, "gpu" | "mac_engine"> | null, ui: Record<string, unknown>, setupComplete: boolean): boolean {
const engine = st?.mac_engine;
return setupComplete && !!engine?.available && !engine.on && !ui.macEngineOffered && (st?.gpu?.memory_total_mib ?? 0) >= 15 * 1024;
}
/** Benchmark this Mac while it runs: the bar's fill and the line under it ("Step 2 of 7 · Default: accuracy at
* 512² · 3 min"). The engine names a run by the setting it changes; its first run is the default paths. */
export function macBenchProgress(b: MacBenchState, now: number): { frac: number; text: string } | null {
if (b.state !== "running") return null;
const minutes = Math.max(0, Math.floor((now - Date.parse(b.started_at)) / 60000));
const elapsed = Number.isFinite(minutes) ? ` · ${minutes} min` : "";
if (b.n <= 0) return { frac: 0, text: `Starting${elapsed}` };
const label = b.label.replace(/^default \(paths above\)/, "Default");
// A step is done when the next one starts: k − 1 of n.
return { frac: (b.k - 1) / b.n, text: `Step ${b.k} of ${b.n}${label ? ` · ${label}` : ""}${elapsed}` };
}
/** Overall download progress over the files that were not installed already. */
export function installProgress(items: InstallItem[]): { received: number; total: number; frac: number } {
const todo = items.filter((i) => i.state !== "present");
const total = todo.reduce((a, i) => a + i.size, 0);
const received = todo.reduce((a, i) => a + Math.min(i.received, i.size), 0);
return { received, total, frac: total ? received / total : 1 };
}
/** One line under a file in the setup wizard. */
export function installItemText(i: InstallItem, bytesPerSec: number): string {
switch (i.state) {
case "present":
case "done":
return `Installed · ${formatBytes(i.size)}`;
case "pending":
return `To download · ${formatBytes(i.size - i.received)}`;
case "downloading": {
const rate = bytesPerSec > 0 ? ` · ${formatBytes(bytesPerSec)}/s` : "";
const left = bytesPerSec > 0 ? ` · ${formatDuration((i.size - i.received) / bytesPerSec)} left` : "";
return `${formatBytes(i.received)} of ${formatBytes(i.size)}${rate}${left}`;
}
case "verifying":
return "Checking the file…";
case "extracting":
return "Extracting…";
case "cancelled":
return i.received ? `Paused at ${formatBytes(i.received)}; continues where it stopped` : "Cancelled";
case "failed":
return i.error ?? "Failed";
}
}
/** Right-hand status of a file in the setup list. */
export function installItemShort(i: InstallItem): string {
switch (i.state) {
case "present":
case "done":
return "Installed";
case "downloading":
return `${Math.floor((i.received / i.size) * 100)}%`;
case "verifying":
return "Checking";
case "extracting":
return "Extracting";
case "failed":
return "Failed";
case "cancelled":
return i.received ? `Paused · ${formatBytes(i.size - i.received)} left` : formatBytes(i.size);
case "pending":
return formatBytes(i.size - i.received);
}
}
/** What the installer is doing right now, for the progress panel. */
export function installStep(items: InstallItem[]): string | null {
const it = items.find((i) => i.state === "downloading" || i.state === "verifying" || i.state === "extracting");
if (!it) return null;
if (it.state === "verifying") return `Checking ${it.label}`;
if (it.state === "extracting") return `Unpacking ${it.label}`;
return `Downloading ${it.label}`;
}
/** "about 2 min left" once the speed is known. */
export function installTimeLeft(remaining: number, bytesPerSec: number): string | null {
if (bytesPerSec < 100e3 || remaining <= 0) return null;
const s = remaining / bytesPerSec;
return s < 60 ? "less than a minute left" : `about ${Math.ceil(s / 60)} min left`;
}
/** Aspect ratios offered in the menus (landscape, then its portrait twin). */
export const ASPECTS = ["1:1", "5:4", "4:5", "4:3", "3:4", "3:2", "2:3", "16:9", "9:16", "2:1", "1:2", "21:9", "9:21"] as const;
/** "16:9" -> 1.777…; null when it is not a usable ratio (the service accepts 1:8 to 8:1). */
export function parseRatio(s: string): number | null {
const m = /^\s*(\d+(?:\.\d+)?)\s*[:x×/]\s*(\d+(?:\.\d+)?)\s*$/i.exec(s);
if (!m) return null;
const r = Number(m[1]) / Number(m[2]);
return Number.isFinite(r) && r >= 0.125 && r <= 8 ? r : null;
}
const gcd = (a: number, b: number): number => (b ? gcd(b, a % b) : a);
/** Readable ratio of a size: "16:9" when it is (within 1 %) a common ratio, the reduced
* fraction when that is short ("7:5"), else "1.85:1". */
export function aspectLabel(w: number, h: number): string {
if (w <= 0 || h <= 0) return "";
const r = w / h;
const known = ASPECTS.find((a) => Math.abs(parseRatio(a)! / r - 1) < 0.01);
if (known) return known;
const g = gcd(Math.round(w), Math.round(h));
const a = Math.round(w) / g;
const b = Math.round(h) / g;
if (a <= 32 && b <= 32) return `${a}:${b}`;
return r >= 1 ? `${r.toFixed(2)}:1` : `1:${(1 / r).toFixed(2)}`;
}
/** Width × height with ratio `r` and about the given pixel area, rounded to multiples of 16. */
export function sizeForRatio(r: number, area: number, max = 4096): Size {
const r16 = (v: number) => Math.max(64, Math.min(max, Math.round(v / 16) * 16));
return { width: r16(Math.sqrt(area * r)), height: r16(Math.sqrt(area / r)) };
}
/** Common screen sizes by their long side (16:9 gives 1920×1080, 2560×1440, 3840×2160). */
export const LONG_SIDE_PRESETS = [
{ label: "1080p", long: 1920 },
{ label: "1440p", long: 2560 },
{ label: "4K", long: 3840 },
] as const;
/** Width × height with ratio `r` and the given long side; the short side is rounded. */
export function sizeForLongSide(r: number, long: number): Size {
return r >= 1 ? { width: long, height: Math.max(1, Math.round(long / r)) } : { width: Math.max(1, Math.round(long * r)), height: long };
}
/** Expansion (source pixels, centred) that turns a w×h image into ratio `r` without cropping. */
export function expandToRatio(w: number, h: number, r: number): OutpaintSpec {
if (w / h < r) {
const extra = Math.max(0, Math.round(h * r) - w);
return { left: Math.floor(extra / 2), right: extra - Math.floor(extra / 2), top: 0, bottom: 0, units: "pixels" };
}
const extra = Math.max(0, Math.round(w / r) - h);
return { left: 0, right: 0, top: Math.floor(extra / 2), bottom: extra - Math.floor(extra / 2), units: "pixels" };
}
/** Expansion (centred) to an exact final size; a side smaller than the source stays as it is. */
export function expandToSize(w: number, h: number, W: number, H: number): OutpaintSpec {
const ex = Math.max(0, Math.round(W) - w);
const ey = Math.max(0, Math.round(H) - h);
return { left: Math.floor(ex / 2), right: ex - Math.floor(ex / 2), top: Math.floor(ey / 2), bottom: ey - Math.floor(ey / 2), units: "pixels" };
}
/** The image moved to `left`, `top` (source px) inside the same expanded canvas: the extra space
* shifts between opposite sides, the final size stays. Snapped to 8 px; within 4 px of an edge, on it. */
export function placeInCanvas(p: { left: number; right: number; top: number; bottom: number }, left: number, top: number): OutpaintSpec {
const snap = (v: number, max: number) => (v >= max - 4 ? max : Math.min(max, Math.max(0, Math.round(v / 8) * 8)));
const ex = p.left + p.right;
const ey = p.top + p.bottom;
const l = snap(left, ex);
const t = snap(top, ey);
return { left: l, right: ex - l, top: t, bottom: ey - t, units: "pixels" };
}
/** Expansion presets in source pixels. */
export function expansionPreset(kind: "left" | "right" | "top" | "bottom" | "landscape" | "portrait" | "square", w: number, h: number, cur: OutpaintSpec): OutpaintSpec {
const base: OutpaintSpec = { left: cur.left, right: cur.right, top: cur.top, bottom: cur.bottom, units: "pixels" };
const qx = Math.round(w * 0.25);
const qy = Math.round(h * 0.25);
const toAspect = (a: number): OutpaintSpec => {
if (w / h < a) {
const extra = Math.max(0, Math.round(h * a) - w);
return { left: Math.floor(extra / 2), right: extra - Math.floor(extra / 2), top: 0, bottom: 0, units: "pixels" };
}
const extra = Math.max(0, Math.round(w / a) - h);
return { left: 0, right: 0, top: Math.floor(extra / 2), bottom: extra - Math.floor(extra / 2), units: "pixels" };
};
switch (kind) {
case "left":
return { ...base, left: base.left + qx };
case "right":
return { ...base, right: base.right + qx };
case "top":
return { ...base, top: base.top + qy };
case "bottom":
return { ...base, bottom: base.bottom + qy };
case "landscape":
return toAspect(16 / 9);
case "portrait":
return toAspect(9 / 16);
case "square":
return toAspect(1);
}
}
/** Expansion in pixels regardless of units. */
export function expansionPixels(o: OutpaintSpec, w: number, h: number) {
const f = (v: number, b: number) => (o.units === "percent" ? Math.round((b * v) / 100) : Math.round(v));
return { left: f(o.left, w), right: f(o.right, w), top: f(o.top, h), bottom: f(o.bottom, h) };
}
// ---------------------------------------------------------------------------------------
// Before / after comparison
// ---------------------------------------------------------------------------------------
export type CompareMode = "off" | "slider" | "flicker" | "diff";
/** The image a result was made from and where it sits inside the result, as fractions of the
* result's width/height (an expanded image sits inside a larger canvas). */
export interface CompareBase {
inputId: string;
rect: { x: number; y: number; w: number; h: number };
}
export function compareBase(g: GenerationRecord): CompareBase | null {
const r = g.references[0];
// A new image or a combination of several has no single "before".
if (!r || !["edit", "inpaint", "outpaint", "upscale", "lut", "layers"].includes(g.mode)) return null;
const o = g.outpaint;
if (g.mode === "outpaint" && o) {
// Expansion is in source pixels; the result is the source plus the expansion.
const W = r.width + o.left + o.right;
const H = r.height + o.top + o.bottom;
return { inputId: r.input_id, rect: { x: o.left / W, y: o.top / H, w: r.width / W, h: r.height / H } };
}
return { inputId: r.input_id, rect: { x: 0, y: 0, w: 1, h: 1 } };
}
/** Long side of the screen-sized copy kokon-service makes of an image for a browser's stage
* (`PREVIEW_SIDE` in crates/qls-core/src/store.rs). */
export const PREVIEW_SIDE = 1280;
/** The stage may show the preview instead of the full image: drawn at `w`×`h` CSS px, the image
* needs no more device pixels than the preview has (an iPhone: 390 CSS px × 3 = 1170). */
export function stageFitsPreview(w: number, h: number, dpr: number): boolean {
return w > 0 && h > 0 && Math.max(w, h) * (dpr > 0 ? dpr : 1) <= PREVIEW_SIDE;
}
/** Full screen in a browser: an image of `size` drawn at `scale` needs the full file, not the
* preview. Fitted to a phone it doesn't; zoomed in past ~1280 device px it does. */
export function viewerWantsFull(size: { width: number; height: number }, scale: number, dpr: number): boolean {
return size.width > 0 && size.height > 0 && !stageFitsPreview(size.width * scale, size.height * scale, dpr);
}
/** Versions to drop after loading a project's records in one answer: the ones the service knew
* (their key was in its copy, `known`) but sent no record for, i.e. deleted from the gallery.
* A version it did not know yet (added here, not saved) is looked up on its own instead. */
export function goneVersions(
items: { key: string; kind: string; genId?: string; inputId?: string }[],
records: { generations: Record<string, unknown>; inputs: Record<string, unknown> },
known: string[],
): Set<string> {
const knew = new Set(known);
const gone = new Set<string>();
for (const i of items) {
const has = i.kind === "gen" && i.genId ? i.genId in records.generations : i.kind === "input" && i.inputId ? i.inputId in records.inputs : true;
if (!has && knew.has(i.key)) gone.add(i.key);
}
return gone;
}
/** Per-pixel change between two equally sized RGBA buffers: the dimmed result in grey with
* changed pixels in red (stronger = bigger change). Pixels the "before" does not cover
* (alpha 0, e.g. expanded areas) count as changed. Returns the changed share of the image. */
export function diffPixels(after: Uint8ClampedArray, before: Uint8ClampedArray, out: Uint8ClampedArray, threshold = 24): number {
let changed = 0;
const n = after.length / 4;
for (let i = 0; i < after.length; i += 4) {
const grey = 0.3 * (0.299 * after[i] + 0.587 * after[i + 1] + 0.114 * after[i + 2]);
const d = before[i + 3] < 128 ? 255 : Math.max(Math.abs(after[i] - before[i]), Math.abs(after[i + 1] - before[i + 1]), Math.abs(after[i + 2] - before[i + 2]));
const a = d <= threshold ? 0 : Math.min(1, 0.35 + (d - threshold) / 96);
if (a > 0) changed++;
out[i] = grey + (255 - grey) * a;
out[i + 1] = grey + (64 - grey) * a;
out[i + 2] = grey + (64 - grey) * a;
out[i + 3] = 255;
}
return n ? changed / n : 0;
}
/** Where the gallery preview stands in `ids`: on the image it shows (`id`) wherever the list
* moved it, else at its last position `at` (clamped); null when the list is empty. */
export function previewIndex(ids: string[], id: string, at: number): number | null {
if (!ids.length) return null;
const i = ids.indexOf(id);
return i >= 0 ? i : Math.min(Math.max(at, 0), ids.length - 1);
}
/** The image to show once `id` leaves the list: the next one, the previous at the end, or
* null when it was the only one (or not in the list). */
export function neighbourAfterRemoval(ids: string[], id: string): string | null {
const i = ids.indexOf(id);
if (i < 0) return null;
return ids[i + 1] ?? ids[i - 1] ?? null;
}
/** Images made with the app before the one-time "star on GitHub" card. */
export const STAR_PROMPT_AFTER = 5;
/** Whether to show the star card now: enough images made, and nothing else on screen (a dialog,
* the tour or setup), else it waits for the next finished image. */
export function starPromptDue(made: number, busy: boolean): boolean {
return made >= STAR_PROMPT_AFTER && !busy;
}
/** Browser on another device: whether to reload because the page was built for another Kokon
* than the service now runs (the app on the server computer updated). `reloadedFor`: the service
* version this tab already reloaded for, so a page that still differs afterwards (an old copy
* in a cache) does not reload again and again. */
export function pageOutdated(pageVersion: string, serviceVersion: string | undefined, reloadedFor: string | null): boolean {
return !!pageVersion && !!serviceVersion && pageVersion !== serviceVersion && reloadedFor !== serviceVersion;
}
// ---- Layers lab: what is being done, for the status lines and their Stop buttons ----
/** A quick cut-out's step, for "Vase · model at work · 4 s". */
export type CutStep = "model" | "preparing" | "sending" | "matting" | "edges" | "saving";
/** The status line of a quick cut-out of `name`: its step, the download's % (`pct`) while the model
* is fetched, and the seconds the model has worked so far (a run is ~4-8 s, 11-04). */
export function cutoutText(name: string, step: CutStep, seconds: number, pct: number | null): string {
const what =
step === "model"
? `getting the sticker model (once, 224 MB)${pct != null ? ` · ${pct}%` : ""}`
: step === "preparing"
? "preparing"
: step === "sending"
? "sending to the model"
: step === "matting"
? `model at work${seconds >= 1 ? ` · ${Math.floor(seconds)} s` : ""}`
: step === "edges"
? "refining the edge"
: "saving";
return `${name} · ${what}`;
}
/** "step 3 of 8" once the engine reports steps, else "". */
export function jobStepText(j: Pick<JobView, "status" | "step">): string {
if (j.status !== "running" || !j.step || !j.step[1]) return "";
return `step ${Math.min(j.step[0], j.step[1])} of ${j.step[1]}`;
}
/** The download running in the service is the one for `preset` (Cancel stops only that one: the
* installer is shared with Settings and the first setup). */
export function downloadIsFor(st: Pick<InstallStatus, "running" | "preset"> | null | undefined, preset: string): boolean {
return !!st?.running && st.preset === preset;
}