Skip to content

armgit / kokon

Public Imported from GitHub
0

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.

progress: one 0-100 % for the whole job, upscaler included

Gabriele Lozupone0ed4700ad6e9
diff --git a/crates/qls-core/src/engine.rs b/crates/qls-core/src/engine.rs
index b1cb58b..61a1f37 100644
--- a/crates/qls-core/src/engine.rs
+++ b/crates/qls-core/src/engine.rs
@@ -978,13 +978,14 @@ impl Engine {
let duration_start = started;
let mask_rec = mask.map(|(_, r)| r);
let mut records = Vec::with_capacity(contents.len());
+ let count = contents.len() as u32;
for (i, content) in contents.into_iter().enumerate() {
let before = Size::new(content.width(), content.height());
let target = pipeline::enlarge_target(&post);
// Kept when the upscaler enlarges it, so the user can still see and save it.
let original = target.map(|_| content.clone());
let how = EnlargeOptions { tile: plan.upscale_tile, blend: cfg.upscale_blend, prescale: cfg.upscale_prescale };
- let (content, enlarged_with) = self.enlarge(id, &client, content, target, esrgan.as_deref(), how).await?;
+ let (content, enlarged_with) = self.enlarge(id, &client, content, target, esrgan.as_deref(), how, (i as u32 + 1, count)).await?;
let original = original.filter(|_| enlarged_with.is_some());
self.update(id, |j| {
j.view.status = JobStatus::Saving;
@@ -1109,8 +1110,10 @@ impl Engine {
/// Enlarges `content` with the upscaler model when [`pipeline::finish`] would resize it
/// up to `target` by more than ~1.1x. Returns the (possibly larger) content and how the
- /// enlargement is done; upscaler failures fall back to Lanczos with a note.
- async fn enlarge(&self, id: &str, client: &SdcppClient, content: image::RgbaImage, target: Option<Size>, esrgan: Option<&str>, how: EnlargeOptions) -> AppResult<(image::RgbaImage, Option<String>)> {
+ /// enlargement is done; upscaler failures fall back to Lanczos with a note. `image` is
+ /// (this image, images in the batch), for the progress.
+ #[allow(clippy::too_many_arguments)]
+ async fn enlarge(&self, id: &str, client: &SdcppClient, content: image::RgbaImage, target: Option<Size>, esrgan: Option<&str>, how: EnlargeOptions, image: (u32, u32)) -> AppResult<(image::RgbaImage, Option<String>)> {
let Some(target) = target else { return Ok((content, None)) };
let size = Size::new(content.width(), content.height());
let factor = (target.width as f64 / size.width as f64).max(target.height as f64 / size.height as f64);
@@ -1122,6 +1125,8 @@ impl Engine {
j.view.status = JobStatus::Running;
j.view.stage = Some(BackendStage::Upscaling);
j.view.step = None;
+ j.view.image = Some(image);
+ j.view.seconds_per_step = None;
});
let content = Arc::new(content);
let r = self.esrgan_enlarge(id, client, content.clone(), target, name, how).await;
@@ -1154,13 +1159,23 @@ impl Engine {
let c = content.clone();
let input = Arc::new(blocking(move || Ok(imaging::resize(&c, ep.input.width, ep.input.height))).await?);
let mut stitcher = pipeline::Stitcher::new(ep.input);
- for tile in &ep.tiles {
+ // Tiles are the upscaler's steps: (done, total) and the measured time per tile.
+ let total = ep.tiles.len() as u32;
+ self.update(id, |j| j.view.step = Some((0, total)));
+ let started = Instant::now();
+ for (k, tile) in ep.tiles.iter().enumerate() {
self.check_cancel(id)?;
let (inp, t) = (input.clone(), *tile);
let png = blocking(move || pipeline::tile_png_b64(&inp, &t)).await?;
let resp = client.upscale(&UpscaleRequest { image: png, upscaler: Some(upscaler.to_string()), tile_size: (tile_px > 0).then_some(tile_px), output_format: "png".into() }).await?;
let up = blocking(move || imaging::decode_engine_image(&crate::sdcpp::b64_decode(&resp.images[0].b64_json)?, pipeline::ESRGAN_MAX_OUTPUT_SIDE)).await?;
stitcher.add(tile, &up)?;
+ let done = k as u32 + 1;
+ let per_tile = started.elapsed().as_secs_f32() / done as f32;
+ self.update(id, |j| {
+ j.view.step = Some((done, total));
+ j.view.seconds_per_step = Some(per_tile);
+ });
}
Ok(Some(blocking(move || stitcher.finish(&content)).await?))
}
@@ -1549,6 +1564,79 @@ mod tests {
assert_eq!(e.job(&mcp.id).unwrap().status, JobStatus::Queued);
}
+ /// An upscale endpoint that answers every request with its tile enlarged x2 (nearest).
+ async fn fake_upscaler() -> u16 {
+ use tokio::io::{AsyncReadExt, AsyncWriteExt};
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ tokio::spawn(async move {
+ while let Ok((mut sock, _)) = listener.accept().await {
+ let mut req = Vec::new();
+ let mut buf = [0u8; 65536];
+ let body = loop {
+ let n = sock.read(&mut buf).await.unwrap();
+ req.extend_from_slice(&buf[..n]);
+ let text = String::from_utf8_lossy(&req).to_string();
+ if let Some(end) = text.find("\r\n\r\n") {
+ let len = text.lines().find_map(|l| l.to_ascii_lowercase().strip_prefix("content-length:").map(|v| v.trim().parse::<usize>().unwrap())).unwrap_or(0);
+ if req.len() >= end + 4 + len {
+ break req[end + 4..end + 4 + len].to_vec();
+ }
+ }
+ };
+ let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
+ let tile = image::load_from_memory(&crate::sdcpp::b64_decode(v["image"].as_str().unwrap()).unwrap()).unwrap();
+ let up = tile.resize_exact(tile.width() * 2, tile.height() * 2, image::imageops::FilterType::Nearest);
+ let mut png = std::io::Cursor::new(Vec::new());
+ up.write_to(&mut png, image::ImageFormat::Png).unwrap();
+ let out = serde_json::json!({ "images": [{ "index": 0, "b64_json": crate::sdcpp::b64(png.get_ref()) }] }).to_string();
+ let head = format!("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", out.len());
+ let _ = sock.write_all(head.as_bytes()).await;
+ let _ = sock.write_all(out.as_bytes()).await;
+ }
+ });
+ port
+ }
+
+ #[tokio::test]
+ async fn upscaling_reports_its_tiles_as_steps() {
+ let dir = tempfile::tempdir().unwrap();
+ // SAFETY: test-only env override, single-threaded setup.
+ unsafe { std::env::set_var("QLS_CONFIG_DIR", dir.path().join("cfg")) };
+ let mut cfg = AppConfig::default();
+ cfg.sdcpp_dir = dir.path().join("sdcpp");
+ cfg.normalize();
+ std::fs::create_dir_all(cfg.engine_dir()).unwrap();
+ std::fs::write(cfg.server_exe(), b"").unwrap();
+ for (_, p, _) in cfg.profile().files() {
+ std::fs::create_dir_all(p.parent().unwrap()).unwrap();
+ std::fs::write(p, b"").unwrap();
+ }
+ let e = Engine::new(cfg, dir.path()).unwrap();
+ let id = e.submit(GenerationRequest { prompt: "a long strip".into(), ..Default::default() }, None).unwrap().id;
+ let mut events = e.events.subscribe();
+ // 4200 px wide: three tiles of at most 2048 px for an x4 model.
+ let content = image::RgbaImage::from_pixel(4200, 40, image::Rgba([90, 120, 200, 255]));
+ let how = EnlargeOptions { tile: 0, blend: 1.0, prescale: false };
+ let client = SdcppClient::new(fake_upscaler().await);
+ let (img, with) = e.enlarge(&id, &client, content, Some(Size::new(16800, 160)), Some("4x"), how, (2, 3)).await.unwrap();
+ assert_eq!((img.width(), with.as_deref()), (8400, Some("4x")), "stitched from the x2 answers");
+ let mut steps = Vec::new();
+ while let Ok(ev) = events.try_recv() {
+ if let Event::Job { job } = ev {
+ if job.stage == Some(BackendStage::Upscaling) {
+ assert_eq!((job.status, job.image), (JobStatus::Running, Some((2, 3))));
+ if steps.last() != Some(&(job.step, job.seconds_per_step.is_some())) {
+ steps.push((job.step, job.seconds_per_step.is_some()));
+ }
+ }
+ }
+ }
+ assert_eq!(steps.first(), Some(&(None, false)));
+ let tiles: Vec<_> = steps.iter().filter_map(|(s, timed)| s.map(|s| (s, *timed))).collect();
+ assert_eq!(tiles, vec![((0, 3), false), ((1, 3), true), ((2, 3), true), ((3, 3), true)], "{steps:?}");
+ }
+
#[tokio::test]
async fn prompt_enhancer_streams_falls_back_and_is_kept_for_retries() {
use crate::sdcpp::{fake_server, sse_answer};
diff --git a/crates/qls-core/src/planner.rs b/crates/qls-core/src/planner.rs
index 5c49a70..7830569 100644
--- a/crates/qls-core/src/planner.rs
+++ b/crates/qls-core/src/planner.rs
@@ -329,6 +329,10 @@ pub struct Plan {
pub notes: Vec<String>,
pub optimized_for_vram: bool,
pub estimated_seconds: f32,
+ /// Part of the job's time the upscaler takes (0..1, from the time model; also known when
+ /// `estimated_seconds` is 0): the progress gives each phase its share.
+ #[serde(default)]
+ pub upscale_share: f32,
/// Upscaler model used to enlarge the result; None = Lanczos.
#[serde(default)]
pub upscaler: Option<String>,
@@ -1083,10 +1087,13 @@ pub fn plan(req: &GenerationRequest, inputs: &PlanInputs, rec: &RecommendedSetti
let enlarge = output.area() as f64 > working.content.area() as f64 * 1.21;
let mut estimated_seconds =
if quick_upscale { 2.0 } else { estimate_seconds(working.canvas, &references, steps, batch_count, inputs.sage_attention, cfg) };
+ let mut upscale_seconds = 0.0;
if inputs.upscaler.is_some() && (enlarge || quick_upscale) {
let mosr = inputs.upscaler.as_deref().is_some_and(is_mosr);
- estimated_seconds += esrgan_seconds(working.content.area(), inputs.upscale_tile, inputs.metal, mosr) * batch_count as f32;
+ upscale_seconds = esrgan_seconds(working.content.area(), inputs.upscale_tile, inputs.metal, mosr) * batch_count as f32;
+ estimated_seconds += upscale_seconds;
}
+ let upscale_share = upscale_seconds / estimated_seconds;
estimated_seconds = match inputs.speed_factor {
Some(f) => estimated_seconds * f,
None if inputs.hide_uncalibrated => 0.0,
@@ -1121,6 +1128,7 @@ pub fn plan(req: &GenerationRequest, inputs: &PlanInputs, rec: &RecommendedSetti
notes,
optimized_for_vram: optimized,
estimated_seconds,
+ upscale_share,
upscaler: inputs.upscaler.clone(),
upscale_tile: if inputs.upscale_tile > 0 { inputs.upscale_tile } else { LARGE_UPSCALE_TILE },
upscale,
@@ -1142,8 +1150,10 @@ mod tests {
let s = plan(&r, &slow, &RecommendedSettings::default()).unwrap();
assert!((s.estimated_seconds / raw - 13.0).abs() < 0.01);
assert!((backend_seconds(&s, false) - raw).abs() < 0.5, "the calibration compares against the uncalibrated backend time");
- let unknown = PlanInputs { hide_uncalibrated: true, ..base.clone() };
- assert_eq!(plan(&r, &unknown, &RecommendedSettings::default()).unwrap().estimated_seconds, 0.0, "unknown until measured");
+ let unknown = PlanInputs { hide_uncalibrated: true, upscaler: Some("4xNomos2_hq_mosr".into()), ..base.clone() };
+ let u = plan(&GenerationRequest { quality: Quality::Quality, ..r.clone() }, &unknown, &RecommendedSettings::default()).unwrap();
+ assert_eq!(u.estimated_seconds, 0.0, "unknown until measured");
+ assert!(u.upscale_share >= 0.0 && u.upscale_share < 1.0, "the share is known anyway: {}", u.upscale_share);
let known = PlanInputs { hide_uncalibrated: true, speed_factor: Some(2.0), ..base };
assert!(plan(&r, &known, &RecommendedSettings::default()).unwrap().estimated_seconds > 0.0);
}
@@ -1385,6 +1395,9 @@ mod tests {
let p2 = plan(&r, &PlanInputs { upscaler: None, ..i.clone() }, &rec()).unwrap();
assert!(esrgan > p2.estimated_seconds, "the enlargement is included in the estimate");
assert!(p2.notes.iter().any(|n| n.ends_with("with Lanczos.")));
+ // Its share of the time, for the progress: the part added by the upscaler.
+ assert!((p.upscale_share - (esrgan - p2.estimated_seconds) / esrgan).abs() < 1e-4, "{}", p.upscale_share);
+ assert_eq!(p2.upscale_share, 0.0, "Lanczos takes no time worth showing");
}
#[test]
@@ -1506,6 +1519,7 @@ mod tests {
let i = PlanInputs { image_sizes: vec![Size::new(1472, 1472)], upscale_tile: 768, upscaler: Some("4xNomos2_hq_mosr".into()), ..env(true) };
let (pc, mac) = (plan(&r, &i, &rec()).unwrap(), plan(&r, &PlanInputs { metal: true, ..i.clone() }, &rec()).unwrap());
assert!(mac.estimated_seconds > pc.estimated_seconds * 5.0, "{} vs {}", mac.estimated_seconds, pc.estimated_seconds);
+ assert!(pc.upscale_share > 0.5 && mac.upscale_share > 0.9, "an upscale is mostly upscaler: {} {}", pc.upscale_share, mac.upscale_share);
}
#[test]
diff --git a/src/components/Stage.tsx b/src/components/Stage.tsx
index 09b71ff..f959032 100644
--- a/src/components/Stage.tsx
+++ b/src/components/Stage.tsx
@@ -4,14 +4,14 @@ import {
import { Fragment, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { api, urls } from "../lib/api";
import {
- compareBase, composerFromRecord, formatDuration, jobFraction, jobLabel, jobPercent, lutInfo, lutLabel, MAX_UPSCALE_AREA, MAX_UPSCALE_SIDE, MODE_LABEL, pipelineSummary, remainingSeconds, sizeForWidth, upscaleRequest, arcLayout,
+ compareBase, composerFromRecord, formatDuration, jobLabel, jobPercent, type JobClock, lutInfo, lutLabel, MAX_UPSCALE_AREA, MAX_UPSCALE_SIDE, MODE_LABEL, pipelineSummary, remainingSeconds, sizeForWidth, upscaleRequest, arcLayout,
upscalerLabel, upscaleTargets, variationRequest, imageName, type LinkKind, type TimelineLink,
} from "../lib/logic";
import { native } from "../lib/native";
import {
addAsReference, addResult, cancelJob, focusPrompt, getState, itemSource, removeSessionItem, reusePrompt, selectedItem, selectItem, sessionLabels, sessionLinks, setState, submitRequest, toastError, toastInfo, useStore, type SessionItem,
} from "../lib/store";
-import type { GenerationRecord, JobView, LutEntry, Mode, Size } from "../lib/types";
+import type { GenerationRecord, GenerationRequest, JobView, LutEntry, Mode, Size } from "../lib/types";
import { Cocoon, Hatch, reducedMotion } from "./Cocoon";
import { CompareFrame, CompareFullscreen } from "./Compare";
import { LutBar, LutButton, LutOverlay } from "./Lut";
@@ -77,7 +77,7 @@ function Pending({ job, item, labels }: { job: JobView; item: SessionItem; label
const title = [imageName(labels[item.key]), mode && `${MODE_DOING[mode]}${from && mode !== "multi_reference" ? ` ${imageName(labels[from.key]).toLowerCase()}` : ""}`].filter(Boolean).join(" · ");
// The source keeps its own shape inside an expansion; elsewhere it fills the frame.
const outpaint = item.request?.outpaint;
- const pct = useJobPercent(job);
+ const { pct, clock } = useJobPercent(job, item.request, true);
const [promptGone, setPromptGone] = useState(false);
const promptPhase = job.enhanced_prompt != null && !promptGone;
return (
@@ -95,44 +95,93 @@ function Pending({ job, item, labels }: { job: JobView; item: SessionItem; label
onGone={() => setPromptGone(true)}
/>
) : (
- <ProgressPhase job={job} title={title} pct={pct} />
+ <ProgressPhase job={job} title={title} pct={pct} clock={clock} />
)}
</div>
</div>
);
}
-/** Live percentage of a job: computed by `jobPercent` a few times a second, only ever going up,
- * and eased so it counts up smoothly instead of jumping from step to step. */
-function useJobPercent(job: JobView): number {
- const [shown, setShown] = useState(0);
- const live = useRef(job);
- live.current = job;
- const runningAt = useRef<number | null>(null);
- const stepAt = useRef<{ step: string; at: number }>({ step: "", at: performance.now() });
+/** This machine's pace for the phases the plan cannot estimate (model load, prompt enhancer),
+ * remembered between jobs; a per-viewer convenience, so defaults when storage is unavailable. */
+const PROGRESS_KEY = "kokon.progress";
+interface ProgressMemory { loadSeconds: number; enhancerSecondsPerWord: number }
+function progressMemory(): ProgressMemory {
+ const d: ProgressMemory = { loadSeconds: 20, enhancerSecondsPerWord: 0.015 };
+ try {
+ const v = JSON.parse(localStorage.getItem(PROGRESS_KEY) || "{}");
+ return { loadSeconds: v.loadSeconds > 0 ? v.loadSeconds : d.loadSeconds, enhancerSecondsPerWord: v.enhancerSecondsPerWord > 0 ? v.enhancerSecondsPerWord : d.enhancerSecondsPerWord };
+ } catch {
+ return d;
+ }
+}
+function rememberProgress(m: Partial<ProgressMemory>) {
+ try {
+ localStorage.setItem(PROGRESS_KEY, JSON.stringify({ ...progressMemory(), ...m }));
+ } catch {
+ /* storage unavailable: the defaults stay */
+ }
+}
+
+const ENHANCED_MODES = new Set(["generate", "edit", "multi_reference"]);
+
+/** Live percentage of a whole job (`jobPercent`), computed a few times a second, only ever going
+ * up, and eased so it counts up smoothly. Tracks when the job and each phase started; `learn`
+ * (the main card only) remembers how long the model load and the prompt enhancer took. */
+function useJobPercent(job: JobView, request: GenerationRequest | null | undefined, learn = false): { pct: number; clock: JobClock } {
+ const enhance = !!request?.enhance && ENHANCED_MODES.has(job.plan?.mode ?? request?.mode ?? "");
+ const [shown, setShown] = useState<{ pct: number; clock: JobClock }>(() => ({ pct: 0, clock: { elapsed: 0, inPhase: 0, inRunning: 0, sinceStep: 0, enhance, ...progressMemory() } }));
+ const live = useRef({ job, enhance });
+ live.current = { job, enhance };
useEffect(() => {
+ const mem = progressMemory();
+ let startedAt: number | null = null;
+ let runningAt: number | null = null;
+ let phase = { key: "", at: performance.now() };
+ let step = { key: "", at: performance.now() };
let target = 0;
let value = 0;
const id = window.setInterval(() => {
- const j = live.current;
+ const { job: j, enhance: e } = live.current;
const now = performance.now();
- if (j.status === "running" && runningAt.current === null) runningAt.current = now;
- const step = j.step ? `${j.step[0]}/${j.step[1]}` : "";
- if (step !== stepAt.current.step) stepAt.current = { step, at: now };
- const runningS = runningAt.current === null ? 0 : (now - runningAt.current) / 1000;
- target = Math.max(target, jobPercent(j, runningS, (now - stepAt.current.at) / 1000));
+ if (startedAt === null && j.status !== "queued") startedAt = now;
+ if (runningAt === null && j.status === "running") runningAt = now;
+ const key = `${j.status}|${j.stage ?? ""}`;
+ if (key !== phase.key) {
+ // Learn the phases the plan does not estimate, when they end.
+ const took = (now - phase.at) / 1000;
+ const was = phase.key.split("|")[0];
+ if (learn && was === "starting_backend" && took > 1) rememberProgress({ loadSeconds: took });
+ if (learn && was === "enhancing_prompt" && j.enhanced_prompt) {
+ const words = j.enhanced_prompt.split(/\s+/).filter(Boolean).length;
+ if (words >= 5) rememberProgress({ enhancerSecondsPerWord: took / words });
+ }
+ phase = { key, at: now };
+ }
+ const stepKey = j.step ? `${j.stage}|${j.step[0]}/${j.step[1]}` : "";
+ if (stepKey !== step.key) step = { key: stepKey, at: now };
+ const clock: JobClock = {
+ elapsed: startedAt === null ? 0 : (now - startedAt) / 1000,
+ inPhase: (now - phase.at) / 1000,
+ inRunning: runningAt === null ? 0 : (now - runningAt) / 1000,
+ sinceStep: (now - step.at) / 1000,
+ enhance: e,
+ ...mem,
+ };
+ target = Math.max(target, jobPercent(j, clock));
value += (target - value) * (reducedMotion() ? 1 : 0.18);
if (target - value < 0.05) value = target;
- setShown(value);
+ setShown({ pct: value, clock });
}, 60);
return () => window.clearInterval(id);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return shown;
}
-function ProgressPhase({ job, title, pct }: { job: JobView; title: string; pct: number }) {
+function ProgressPhase({ job, title, pct, clock }: { job: JobView; title: string; pct: number; clock: JobClock }) {
const o = job.plan?.output;
- const remain = remainingSeconds(job);
+ const remain = remainingSeconds(job, clock);
const canCancel = !["done", "failed", "cancelled"].includes(job.status);
return (
<div className="progress-phase">
@@ -730,7 +779,6 @@ function Timeline({ labels }: { labels: Record<string, string> }) {
const src = itemSource(it);
const pending = it.kind === "job";
const failed = pending && job && ["failed", "cancelled"].includes(job.status);
- const frac = pending && job ? jobFraction(job) : null;
const mode: Mode | "lut" | undefined = it.gen?.mode ?? job?.plan?.mode;
const link = links[it.key];
const pendingSrc = pending && it.request?.mode !== "generate" ? it.request?.images?.[0] : undefined;
@@ -783,7 +831,7 @@ function Timeline({ labels }: { labels: Record<string, string> }) {
{pendingSrc && <img className="dim" src={urls.sourceThumb(pendingSrc)} alt="" draggable={false} />}
{pending && !failed && (
<span className="frame-state">
- {frac !== null ? <Ring frac={frac} /> : <Loader2 className="spin" size={16} />}
+ {job && job.status !== "queued" ? <JobRing job={job} request={it.request} /> : <Loader2 className="spin" size={16} />}
</span>
)}
{failed && (
@@ -811,6 +859,12 @@ function Timeline({ labels }: { labels: Record<string, string> }) {
);
}
+/** The whole job's progress in the timeline, the same percentage as its card. */
+function JobRing({ job, request }: { job: JobView; request?: GenerationRequest | null }) {
+ const { pct } = useJobPercent(job, request);
+ return <Ring frac={pct / 100} />;
+}
+
function Ring({ frac }: { frac: number }) {
const r = 8;
const c = 2 * Math.PI * r;
diff --git a/src/lib/logic.test.ts b/src/lib/logic.test.ts
index 1d8cb4f..89169b9 100644
--- a/src/lib/logic.test.ts
+++ b/src/lib/logic.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import {
- activeOverrides, buildRequest, jobPercent, compareBase, composerFromRecord, enhancerApplies, diffPixels, emptyComposer, expansionPixels, expansionPreset, jobFraction, jobLabel, lutInfo, moveRef, needsPrompt, normalizeQuality, persistentAdvanced, pipelineSummary,
- planPipeline, remainingSeconds, sizeForWidth, timeAgo, upscaleRequest, upscaleTargets, versionLabels, type Ref,
+ activeOverrides, buildRequest, jobPercent, compareBase, composerFromRecord, enhancerApplies, diffPixels, emptyComposer, expansionPixels, expansionPreset, jobLabel, jobSecondsLeft, lutInfo, moveRef, needsPrompt, normalizeQuality, persistentAdvanced, pipelineSummary,
+ planPipeline, remainingSeconds, sizeForWidth, timeAgo, upscaleRequest, upscaleTargets, versionLabels, type JobClock, type Ref,
neighbourAfterRemoval, previewIndex, starPromptDue, STAR_PROMPT_AFTER, formatBytes, formatDuration, installItemShort, installItemText, installProgress, installStep, installTimeLeft, timelineLinks, undoTarget, aspectLabel, parseRatio, sizeForRatio, expandToRatio, expandToSize, arcLayout,
} from "./logic";
import type { GenerationRecord, InstallItem, JobView, Plan } from "./types";
@@ -138,34 +138,73 @@ describe("job labels are truthful", () => {
id: "j", status: "running", stage: "generating", step: [12, 30], image: null, seconds_per_step: 2, queue_position: 0, created_at: "",
started_at: null, finished_at: null, prompt: "", plan: null, seed: 1, results: [], error: null, notes: [], detailed_progress: true,
};
- it("shows steps only when reported", () => {
+ it("shows steps and tiles only when reported", () => {
expect(jobLabel(base)).toBe("Generating · 12/30");
- expect(jobFraction(base)).toBeCloseTo(0.4);
- const noDetail = { ...base, step: null };
- expect(jobLabel(noDetail)).toBe("Generating");
- expect(jobFraction(noDetail)).toBeNull();
+ expect(jobLabel({ ...base, step: null })).toBe("Generating");
expect(jobLabel({ ...base, status: "queued", queue_position: 3 })).toBe("Queued · 3");
expect(jobLabel({ ...base, status: "enhancing_prompt", stage: null, step: null })).toBe("Writing the prompt");
- // Percent: steps (moving on within a step), else time against the estimate; 100 only when done.
- expect(jobPercent(base, 0, 0)).toBeCloseTo(10 + 80 * (12 / 30));
- expect(jobPercent(base, 0, 1)).toBeCloseTo(10 + 80 * (12.5 / 30));
- expect(jobPercent(base, 0, 99)).toBeLessThan(10 + 80 * (13 / 30));
- const noSteps = { ...base, step: null, plan: { estimated_seconds: 20 } as Plan };
- expect(jobPercent(noSteps, 10, 0)).toBeCloseTo(8 + 87 * 0.5);
- expect(jobPercent(noSteps, 60, 0)).toBe(95);
- expect(jobPercent({ ...noSteps, plan: null }, 600, 0)).toBeLessThanOrEqual(95);
- expect(jobPercent({ ...base, stage: "decoding" }, 0, 0)).toBe(93);
- expect(jobPercent({ ...base, status: "saving" }, 0, 0)).toBe(99);
- expect(jobPercent({ ...base, status: "done" }, 0, 0)).toBe(100);
- expect(jobPercent({ ...base, status: "enhancing_prompt" }, 0, 0)).toBe(5);
- expect(jobFraction({ ...base, status: "enhancing_prompt" })).toBeNull();
- });
-
- it("one pass fills the bar; enlarging is indeterminate", () => {
- expect(remainingSeconds({ ...base, step: [10, 20] as [number, number] })).toBeCloseTo(10 * 2);
expect(jobLabel({ ...base, stage: "upscaling", step: null })).toBe("Upscaling");
- expect(jobFraction({ ...base, stage: "upscaling", step: null })).toBeNull();
- expect(jobFraction({ ...base, stage: "decoding", step: null })).toBe(1);
+ expect(jobLabel({ ...base, stage: "upscaling", step: [2, 6], image: [1, 2] })).toBe("Upscaling · 2/6 · 1/2");
+ });
+});
+
+describe("one percentage for the whole job", () => {
+ // 20 s planned: 15 s to generate, 5 s of upscaler; 8 steps at 1.5 s; the model loads in 10 s.
+ const plan = { mode: "generate", estimated_seconds: 20, upscale_share: 0.25, batch_count: 1 } as Plan;
+ const job = (o: Partial<JobView>): JobView => ({
+ id: "j", status: "running", stage: null, step: null, image: null, seconds_per_step: null, queue_position: 0, created_at: "",
+ started_at: null, finished_at: null, prompt: "", plan, seed: 1, results: [], error: null, notes: [], detailed_progress: true, ...o,
+ });
+ const clock = (o: Partial<JobClock>): JobClock => ({ elapsed: 0, inPhase: 0, inRunning: 0, sinceStep: 0, enhance: false, loadSeconds: 10, enhancerSecondsPerWord: 0.015, ...o });
+
+ it("counts every phase still to come", () => {
+ // Loading: the rest of the load, then generation, upscaler and saving.
+ expect(jobSecondsLeft(job({ status: "starting_backend" }), clock({ inPhase: 4 }))).toBeCloseTo(6 + 15 + 5 + 1);
+ // With the enhancer on, its time too.
+ expect(jobSecondsLeft(job({ status: "preparing" }), clock({ enhance: true }))).toBeCloseTo(0.5 + (0.5 + 110 * 0.015) + 21);
+ // Sampling: the steps left at the measured pace, decoding, upscaler, saving.
+ expect(jobSecondsLeft(job({ stage: "generating", step: [4, 8], seconds_per_step: 1.5 }), clock({}))).toBeCloseTo(4 * 1.5 + 1.5 + 5 + 1);
+ // Upscaling: the tiles left at the measured pace, then saving.
+ expect(jobSecondsLeft(job({ stage: "upscaling", step: [1, 3], seconds_per_step: 1.5 }), clock({}))).toBeCloseTo(2 * 1.5 + 1);
+ // The enhancer's own pace once it writes: 20 words in 1 s, ~110 asked.
+ const writing = job({ status: "enhancing_prompt", enhanced_prompt: Array(20).fill("word").join(" ") });
+ expect(jobSecondsLeft(writing, clock({ inPhase: 1, enhance: true }))).toBeCloseTo(90 / 20 + 21);
+ });
+
+ it("moves with time, forms the logo only after the upscaler, and reaches 100 only when done", () => {
+ // Accurate estimates: the percentage is the share of the time spent.
+ const loading = jobPercent(job({ status: "starting_backend" }), clock({ elapsed: 5, inPhase: 5 }));
+ expect(loading).toBeCloseTo((100 * 5) / 31, 0);
+ const halfSteps = jobPercent(job({ stage: "generating", step: [4, 8], seconds_per_step: 1.5 }), clock({ elapsed: 10 + 7, inRunning: 7 }));
+ expect(halfSteps).toBeGreaterThan(loading);
+ // Generation done, upscaler not started: well short of the logo (92 %).
+ const decoded = jobPercent(job({ stage: "upscaling", step: [0, 3] }), clock({ elapsed: 25 }));
+ expect(decoded).toBeLessThan(85);
+ const lastTile = jobPercent(job({ stage: "upscaling", step: [3, 3], seconds_per_step: 1.6 }), clock({ elapsed: 30 }));
+ expect(lastTile).toBeGreaterThan(92);
+ expect(jobPercent(job({ status: "saving" }), clock({ elapsed: 31, inPhase: 9 }))).toBeLessThanOrEqual(99);
+ expect(jobPercent(job({ status: "done" }), clock({ elapsed: 31 }))).toBe(100);
+ expect(jobPercent(job({ status: "queued" }), clock({}))).toBe(0);
+ });
+
+ it("an overrun slows the bar instead of stopping it", () => {
+ const a = jobPercent(job({ status: "starting_backend" }), clock({ elapsed: 30, inPhase: 30 }));
+ const b = jobPercent(job({ status: "starting_backend" }), clock({ elapsed: 60, inPhase: 60 }));
+ expect(b).toBeGreaterThan(a);
+ expect(b).toBeLessThan(99);
+ });
+
+ it("an upscale is all upscaler, and an unmeasured machine still gets a curve", () => {
+ const quick = { mode: "upscale", estimated_seconds: 8, upscale_share: 0.8, batch_count: 1 } as Plan;
+ expect(jobSecondsLeft(job({ plan: quick, stage: "upscaling", step: [0, 4] }), clock({}))).toBeCloseTo(8 + 1);
+ const unknown = { ...plan, estimated_seconds: 0 };
+ expect(jobPercent(job({ plan: unknown, stage: "encoding_prompt" }), clock({ elapsed: 3, inRunning: 3 }))).toBeGreaterThan(0);
+ });
+
+ it("shows the time left only once the engine's pace is known", () => {
+ expect(remainingSeconds(job({ stage: "generating", step: [4, 8], seconds_per_step: 1.5 }), clock({}))).toBeCloseTo(4 * 1.5 + 1.5 + 5 + 1);
+ expect(remainingSeconds(job({ stage: "generating", step: [0, 8] }), clock({}))).toBeNull();
+ expect(remainingSeconds(job({ status: "starting_backend" }), clock({}))).toBeNull();
});
});
diff --git a/src/lib/logic.ts b/src/lib/logic.ts
index 11e26c7..b5ae681 100644
--- a/src/lib/logic.ts
+++ b/src/lib/logic.ts
@@ -352,7 +352,7 @@ export function jobLabel(j: JobView): string {
case "generating":
return j.step ? `Generating · ${j.step[0]}/${j.step[1]}${batch}` : `Generating${batch}`;
case "upscaling":
- return "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":
@@ -366,61 +366,111 @@ export function jobLabel(j: JobView): string {
}
}
-/** 0..1 when the backend reports steps, else null (indeterminate). */
-export function jobFraction(j: JobView): number | null {
- if (j.status !== "running") return null;
- if (j.stage === "generating" && j.step) return j.step[0] / Math.max(1, j.step[1]);
- if (j.stage === "decoding" || j.stage === "finishing") return 1;
- return null;
-}
-
-/** Completion (0..100) for the progress card. `runningS`: seconds since the job started running;
- * `stepS`: seconds since the last reported step. The steps when the server reports them (moving
- * on within a step at the measured pace), else time against the plan's estimate (an external
- * server gives no steps). Short of 100 until the job is done. */
-export function jobPercent(j: JobView, runningS: number, stepS: number): number {
+/** 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, 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" ? "generate" : "edit"];
+ const enhanceS = c.enhance ? 0.5 + words * c.enhancerSecondsPerWord : 0;
+ const fromGeneration = genS + upS + SAVE_SECONDS;
switch (j.status) {
case "queued":
- case "failed":
- case "cancelled":
- return 0;
case "starting_backend":
- return 2;
+ return phaseLeft(c.loadSeconds, j.status === "queued" ? 0 : c.inPhase) + enhanceS + fromGeneration;
case "preparing":
- return 4;
- case "enhancing_prompt":
- return 5;
+ 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 99;
+ return phaseLeft(SAVE_SECONDS, c.inPhase);
case "done":
- return 100;
+ case "failed":
+ case "cancelled":
+ return 0;
}
switch (j.stage) {
- case "preparing":
- return 6;
- case "encoding_images":
- case "encoding_prompt":
- return 8;
- case "decoding":
- return 93;
- case "upscaling":
- return 95;
+ 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 97;
- }
- if (j.step && j.step[1] > 0) {
- const within = j.seconds_per_step ? Math.min(0.95, stepS / j.seconds_per_step) : 0;
- return 10 + (80 * Math.min(j.step[1], j.step[0] + within)) / j.step[1];
+ 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;
+ }
}
- const est = j.plan?.estimated_seconds ?? 0;
- const share = est > 0 ? Math.min(1, runningS / est) : 1 - Math.exp(-runningS / 15);
- return Math.min(95, 8 + 87 * share);
+ // 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)));
}
-export function remainingSeconds(j: JobView): number | null {
+/** 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") return null;
- return Math.max(0, (j.step[1] - j.step[0]) * j.seconds_per_step);
+ if (j.stage !== "generating" && j.stage !== "upscaling") return null;
+ return jobSecondsLeft(j, c);
}
// ---------------------------------------------------------------------------------------
diff --git a/src/lib/types.ts b/src/lib/types.ts
index f50a633..170b4c2 100644
--- a/src/lib/types.ts
+++ b/src/lib/types.ts
@@ -87,6 +87,9 @@ export interface Plan {
optimized_for_vram: boolean;
/** Calibrated to this machine after its first image; 0 = not measured yet. */
estimated_seconds: number;
+ /** Part of that time the upscaler takes (0..1; known even when the estimate is 0; absent in
+ * plans of older versions). */
+ upscale_share?: number;
upscaler: string | null;
/** Upscaler tile size in px (0 in plans of older versions). */
upscale_tile?: number;