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.

feat(app): Enhance toggle, live prompt card and the cocoon animation

Gabriele Lozuponee240bf5258ad
diff --git a/src/components/Cocoon.tsx b/src/components/Cocoon.tsx
new file mode 100644
index 0000000..32e18a3
--- /dev/null
+++ b/src/components/Cocoon.tsx
@@ -0,0 +1,435 @@
+// The cocoon: Kokon's mark as motion, ink on paper only (the theme's text colour), like the brand.
+//
+// While an image is made, silk threads wind around a capsule shaped like the logo's stem, loose
+// at first, tighter and denser as the job advances, until the cocoon fills in and is the stem of
+// the K, solid. Just before the image is ready the stem steps left and the two wings grow out
+// of it: the whole logo, centred on the canvas. When the image arrives it appears inside the
+// logo, and the logo grows until it is the image. The canvas (the image's space) stays visible
+// throughout.
+
+import { useEffect, useLayoutEffect, useRef, useState } from "react";
+
+/** The mark in its 1024 grid (assets/brand, variant C): the stem (the cocoon) and two wings. */
+const STEM = "M0.00 242.81 C0.00 112.17 70.23 25.08 175.59 25.08 C280.94 25.08 351.17 112.17 351.17 242.81 L351.17 781.19 C351.17 911.83 280.94 998.92 175.59 998.92 C70.23 998.92 0.00 911.83 0.00 781.19 Z";
+const WING_UP = "M389.53 548.95 C389.53 250.35 662.35 25.08 1024.00 25.08 C1024.00 323.68 751.18 548.95 389.53 548.95 Z";
+const WING_DOWN = "M389.53 587.31 C389.53 821.93 618.71 998.92 922.49 998.92 C922.49 764.30 693.31 587.31 389.53 587.31 Z";
+const STEM_W = 351.17;
+const STEM_TOP = 25.08;
+const STEM_H = 973.84;
+const STEM_RATIO = STEM_W / STEM_H;
+/** Where the wings grow from. */
+const UP_ROOT: [number, number] = [389.5, 549];
+const DOWN_ROOT: [number, number] = [389.5, 587];
+/** How much the logo grows while the image completes around it. */
+const ZOOM_TO = 6;
+
+export const reducedMotion = () => typeof window !== "undefined" && !!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
+
+/** Where the mark sits on a `w`×`h` canvas: the stem ~48 % of the height, a little above the
+ * middle (the percentage sits under it); the whole mark never wider than 80 % of the canvas. */
+interface MarkLayout {
+ /** Mark grid → pixels. */
+ scale: number;
+ top: number;
+ /** Left of the mark's grid with the stem centred (the cocoon), and with the whole mark centred. */
+ leftCocoon: number;
+ leftLogo: number;
+ /** The capsule (stem) with the stem centred. */
+ cx: number;
+ cy: number;
+ w: number;
+ h: number;
+}
+
+function markLayout(w: number, h: number): MarkLayout {
+ let sh = h * 0.48;
+ sh = Math.min(sh, (w * 0.8 * STEM_H) / 1024, (w * 0.5) / STEM_RATIO);
+ const scale = sh / STEM_H;
+ const cy = h * 0.43;
+ return { scale, top: cy - sh / 2 - STEM_TOP * scale, leftCocoon: w / 2 - (STEM_W * scale) / 2, leftLogo: w / 2 - 512 * scale, cx: w / 2, cy, w: STEM_W * scale, h: sh };
+}
+
+/** `d` (absolute M/C/L/Z only) with every point mapped by `f`. */
+function mapPath(d: string, f: (x: number, y: number) => [number, number]): string {
+ const tok = d.match(/[MCLZ]|-?\d*\.?\d+/g) ?? [];
+ let out = "";
+ for (let i = 0; i < tok.length; ) {
+ if (/[MCLZ]/.test(tok[i])) {
+ out += tok[i++] + " ";
+ continue;
+ }
+ const [x, y] = f(Number(tok[i]), Number(tok[i + 1]));
+ out += `${x.toFixed(1)} ${y.toFixed(1)} `;
+ i += 2;
+ }
+ return out;
+}
+
+/** The mark at `left`/`top`/`scale` with its wings grown to `up`/`down` (0..1): one path. */
+function markPath(left: number, top: number, scale: number, up: number, down: number): string {
+ const toPx = (x: number, y: number): [number, number] => [left + x * scale, top + y * scale];
+ const grow = (root: [number, number], k: number) => (x: number, y: number) => toPx(root[0] + (x - root[0]) * k, root[1] + (y - root[1]) * k);
+ return mapPath(STEM, toPx) + (up > 0.001 ? mapPath(WING_UP, grow(UP_ROOT, up)) : "") + (down > 0.001 ? mapPath(WING_DOWN, grow(DOWN_ROOT, down)) : "");
+}
+
+const clamp01 = (v: number) => Math.min(1, Math.max(0, v));
+const easeOut = (v: number) => 1 - Math.pow(1 - v, 3);
+const easeInOut = (v: number) => (v < 0.5 ? 4 * v * v * v : 1 - Math.pow(-2 * v + 2, 3) / 2);
+/** Ease out with a small overshoot, for the wings opening. */
+const easeOutBack = (v: number) => 1 + 2.2 * Math.pow(v - 1, 3) + 1.2 * Math.pow(v - 1, 2);
+
+/** The logo forming: the stem steps left while the wings grow (ms). */
+const FORM_MS = 700;
+/** Shift and wing growth at `ms` after the logo started forming. */
+function formingAt(ms: number) {
+ return {
+ shift: easeInOut(clamp01(ms / FORM_MS)),
+ up: easeOutBack(clamp01((ms - 120) / (FORM_MS - 120))),
+ down: easeOutBack(clamp01((ms - 210) / (FORM_MS - 120))),
+ };
+}
+
+/** When a cocoon last finished forming the logo: the hatching then starts from the logo. */
+let logoFormedAt = -Infinity;
+
+/** Radius of the capsule (a stadium turned around its axis) at height `u` in -1..1. */
+function radiusAt(u: number, m: MarkLayout): number {
+ const r = m.w / 2;
+ const y = Math.abs(u) * (m.h / 2);
+ const straight = m.h / 2 - r;
+ if (y <= straight) return r;
+ const d = y - straight;
+ return Math.sqrt(Math.max(0, r * r - d * d));
+}
+
+interface Thread { turns: number; dir: number; phase: number; spin: number; u0: number; u1: number; birth: number; loose: number; width: number; alpha: number; born: number | null }
+
+/** Same threads on every render: a small seeded generator. */
+function makeThreads(n: number): Thread[] {
+ let seed = 0x4b6f6b6f; // "Koko"
+ const rnd = () => {
+ seed = (seed + 0x6d2b79f5) | 0;
+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
+ };
+ return Array.from({ length: n }, (_, i) => ({
+ // Few turns over a long span: threads cross diagonally, like silk, not like a spring.
+ turns: 0.3 + rnd() * 1.1,
+ dir: rnd() < 0.5 ? -1 : 1,
+ phase: rnd() * Math.PI * 2,
+ spin: 0.18 + rnd() * 0.16,
+ u0: -1 + rnd() * 0.3,
+ u1: 1 - rnd() * 0.3,
+ // A few threads from the start, the rest spun in as the percentage grows.
+ birth: i < 10 ? 0 : (i / n) * 88 + rnd() * 4,
+ loose: 0.35 + rnd() * 0.65,
+ width: 0.4 + rnd() * 0.55,
+ // Mostly faint silk with a few brighter strands.
+ alpha: rnd() < 0.15 ? 0.55 + rnd() * 0.25 : 0.1 + rnd() * 0.22,
+ born: null,
+ }));
+}
+
+const THREADS = 120;
+const POINTS = 72;
+
+/** The cocoon behind a job in progress (`pct` 0..100). For an edit the source shows in black
+ * and white inside it, coming into focus, until the silk fills in. `form` turns the filled
+ * cocoon into the whole logo (set just before the image is ready). */
+export function Cocoon({ src, pct, form, dim }: { src?: string; pct: number; form?: boolean; dim?: boolean }) {
+ const wrap = useRef<HTMLDivElement>(null);
+ const canvas = useRef<HTMLCanvasElement>(null);
+ const [size, setSize] = useState({ w: 0, h: 0 });
+ const live = useRef({ pct, form: !!form });
+ live.current = { pct, form: !!form };
+
+ useLayoutEffect(() => {
+ const el = wrap.current;
+ if (!el) return;
+ const ro = new ResizeObserver(() => setSize({ w: el.clientWidth, h: el.clientHeight }));
+ ro.observe(el);
+ return () => ro.disconnect();
+ }, []);
+
+ useEffect(() => {
+ const c = canvas.current;
+ if (!c || !size.w || !size.h) return;
+ const dpr = Math.min(2, window.devicePixelRatio || 1);
+ c.width = Math.round(size.w * dpr);
+ c.height = Math.round(size.h * dpr);
+ const ctx = c.getContext("2d");
+ if (!ctx) return;
+ const threads = makeThreads(THREADS);
+ const still = reducedMotion();
+ let ink = getComputedStyle(document.documentElement).getPropertyValue("--text").trim() || "#ffffff";
+ let lastInk = 0;
+ let formStart: number | null = null;
+ let raf = 0;
+ const t0 = performance.now();
+
+ const draw = (now: number) => {
+ const t = still ? 0 : (now - t0) / 1000;
+ if (now - lastInk > 1000) {
+ ink = getComputedStyle(document.documentElement).getPropertyValue("--text").trim() || ink;
+ lastInk = now;
+ }
+ const { pct: pctNow, form: formNow } = live.current;
+ const p = clamp01(pctNow / 100);
+ if (formNow && formStart === null) formStart = now;
+ const f = formStart === null ? { shift: 0, up: 0, down: 0 } : still ? { shift: 1, up: 1, down: 1 } : formingAt(now - formStart);
+ if (formStart !== null && now - formStart >= FORM_MS) logoFormedAt = now;
+ // Tightness eases in: loose orbits at the start, a closed cocoon near the end; past half
+ // way the silk fills in until the cocoon is the solid stem (at ~90 %, or when forming).
+ const tight = 1 - Math.pow(1 - p, 1.6);
+ const solid = formStart !== null ? 1 : easeInOut(clamp01((p - 0.5) / 0.4));
+ const m = markLayout(size.w, size.h);
+ const left = m.leftCocoon + (m.leftLogo - m.leftCocoon) * f.shift;
+ const dx = left - m.leftCocoon;
+ const breathe = still ? 1 : 1 + Math.sin(t * 1.3) * 0.008 * (1 - solid);
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
+ ctx.clearRect(0, 0, size.w, size.h);
+ ctx.strokeStyle = ink;
+ ctx.fillStyle = ink;
+ ctx.lineCap = "round";
+
+ // The body: a faint fill that thickens with the silk, solid at the end; then the wings.
+ ctx.globalAlpha = 0.025 + 0.06 * tight + (1 - 0.025 - 0.06 * tight) * solid;
+ ctx.fill(new Path2D(markPath(left, m.top, m.scale, f.up, f.down)));
+
+ const threadAlpha = 1 - solid;
+ if (threadAlpha > 0.01) {
+ for (const th of threads) {
+ if (pctNow < th.birth && !still) continue;
+ if (th.born === null) th.born = t;
+ const grow = still ? 1 : Math.min(1, (t - th.born) / 1.8);
+ const slack = (1 - tight) * th.loose;
+ const rMul = (1 + slack * 1.1) * breathe;
+ const yMul = 1 + slack * 0.35;
+ const front = new Path2D();
+ const back = new Path2D();
+ let side = 0;
+ let hx = 0;
+ let hy = 0;
+ for (let k = 0; k <= POINTS; k++) {
+ const s = (k / POINTS) * grow;
+ const u = th.u0 + (th.u1 - th.u0) * s;
+ const phi = th.phase + th.dir * th.turns * Math.PI * 2 * s + th.spin * t;
+ const r = radiusAt(u, m) * rMul + slack * m.w * 0.15 * Math.sin(phi * 2 + th.phase);
+ const x = m.cx + dx + r * Math.cos(phi);
+ const y = m.cy + u * (m.h / 2) * yMul;
+ const z = Math.sin(phi) >= 0 ? 1 : -1;
+ const path = z > 0 ? front : back;
+ if (k === 0) path.moveTo(x, y);
+ else if (z !== side) path.moveTo(hx, hy); // join front and back across the edge
+ path.lineTo(x, y);
+ side = z;
+ hx = x;
+ hy = y;
+ }
+ ctx.lineWidth = th.width;
+ ctx.globalAlpha = th.alpha * 0.35 * threadAlpha;
+ ctx.stroke(back);
+ ctx.globalAlpha = th.alpha * (0.7 + 0.3 * tight) * threadAlpha;
+ ctx.stroke(front);
+ // The spinning end of a thread still being laid: a small glint.
+ if (grow < 1) {
+ ctx.globalAlpha = 0.8 * threadAlpha;
+ ctx.beginPath();
+ ctx.arc(hx, hy, 1.4, 0, Math.PI * 2);
+ ctx.fill();
+ }
+ }
+ }
+ ctx.globalAlpha = 1;
+ if (!still || formStart === null) raf = requestAnimationFrame(draw);
+ };
+ raf = requestAnimationFrame(draw);
+ return () => cancelAnimationFrame(raf);
+ }, [size.w, size.h]);
+
+ const p = clamp01(pct / 100);
+ const m = markLayout(size.w || 1, size.h || 1);
+ const capsule = `inset(${m.cy - m.h / 2}px ${size.w - m.leftCocoon - m.w}px ${size.h - m.cy - m.h / 2}px ${m.leftCocoon}px round ${m.w / 2}px)`;
+ return (
+ <div className={`cocoon ${dim ? "dim" : ""}`} ref={wrap} aria-hidden>
+ {src && size.w > 0 && !form && (
+ <img
+ className="cocoon-src"
+ src={src}
+ alt=""
+ draggable={false}
+ style={{ clipPath: capsule, filter: `grayscale(1) blur(${(16 - 13 * p).toFixed(1)}px)`, opacity: (0.18 + 0.4 * p) * (1 - easeInOut(clamp01((p - 0.5) / 0.4))) }}
+ />
+ )}
+ <canvas ref={canvas} style={{ width: size.w, height: size.h }} />
+ </div>
+ );
+}
+
+// ---------------------------------------------------------------------------------------
+// Hatching: a result made a moment ago comes out of the logo once, when first shown.
+// ---------------------------------------------------------------------------------------
+
+const fresh = new Map<string, number>();
+
+/** Results of a job that just finished in this window (shown with the hatching once). */
+export function markFresh(ids: string[]) {
+ const now = Date.now();
+ for (const id of ids) fresh.set(id, now);
+}
+
+function takeFresh(id: string): boolean {
+ const at = fresh.get(id);
+ fresh.delete(id);
+ return at !== undefined && Date.now() - at < 8000;
+}
+
+function clearMask(b: HTMLElement) {
+ b.style.clipPath = "";
+ b.style.filter = "";
+ for (const p of ["mask-image", "-webkit-mask-image", "mask-size", "-webkit-mask-size", "mask-repeat", "-webkit-mask-repeat"]) b.style.removeProperty(p);
+}
+
+/** Timeline of the hatching (ms, after the logo has formed). */
+const FILL_MS = 450;
+const HOLD_MS = 350;
+const ZOOM_MS = 950;
+
+/** Wraps a result frame. The first time a fresh result is shown it hatches, on the canvas the
+ * job was made on: from the solid logo (formed here first if the job ended before its card
+ * showed it), the image appears inside the logo, then the logo grows evenly from its centre
+ * while the rest of the image fades in around it, until only the image is left. `aspect` is the image's, to find its canvas. */
+export function Hatch({ id, aspect, children }: { id?: string; aspect: number; children: React.ReactNode }) {
+ const ref = useRef<HTMLDivElement>(null);
+ const body = useRef<HTMLDivElement>(null);
+ const canvasBox = useRef<HTMLDivElement>(null);
+ const ink = useRef<SVGPathElement>(null);
+ const veilBox = useRef<HTMLDivElement>(null);
+ const veil = useRef<SVGSVGElement>(null);
+ const veilPath = useRef<SVGPathElement>(null);
+ const [on] = useState(() => !!id && takeFresh(id) && !reducedMotion());
+ const [running, setRunning] = useState(on);
+
+ useLayoutEffect(() => {
+ const el = ref.current;
+ const b = body.current;
+ if (!on || !el || !b) return;
+ // The canvas: the frame's own image box, measured each frame since the frame sizes itself
+ // after mounting; until then the same fit computed. The mark sits on it like on the card.
+ let iw = 0, ih = 0, ox = 0, oy = 0;
+ let m = markLayout(1, 1);
+ const measure = () => {
+ const W = el.clientWidth;
+ const H = el.clientHeight;
+ let r = { w: Math.min(W, H * aspect), h: 0, x: 0, y: 0 };
+ r = { ...r, h: r.w / aspect, x: (W - r.w) / 2, y: (H - r.w / aspect) / 2 };
+ const shown = b.querySelector<HTMLElement>(".image-box, .compare-box");
+ if (shown && shown.offsetWidth > 0) {
+ const a = shown.getBoundingClientRect();
+ const o = el.getBoundingClientRect();
+ r = { w: a.width, h: a.height, x: a.left - o.left, y: a.top - o.top };
+ }
+ if (r.w === iw && r.h === ih && r.x === ox && r.y === oy) return;
+ [iw, ih, ox, oy] = [r.w, r.h, r.x, r.y];
+ m = markLayout(iw, ih);
+ for (const box of [canvasBox.current, veilBox.current]) if (box) Object.assign(box.style, { left: `${ox}px`, top: `${oy}px`, width: `${iw}px`, height: `${ih}px` });
+ };
+ const formed = performance.now() - logoFormedAt < 4000;
+ const formFrom = formed ? FORM_MS : 0;
+ const fillAt = FORM_MS - formFrom;
+ const zoomAt = fillAt + FILL_MS + HOLD_MS;
+ const start = performance.now();
+ // The solid logo holds until the image inside it is decoded: otherwise the ink fades onto an
+ // empty canvas and the image pops in afterwards (the logo seems to appear twice).
+ let readyAt: number | null = null;
+ const imgs = Array.from(b.querySelectorAll("img"));
+ const ready = () => (readyAt ??= performance.now());
+ Promise.all(imgs.map((i) => i.decode().catch(() => {}))).then(ready);
+ const giveUp = window.setTimeout(ready, 5000);
+ let raf = 0;
+ const frame = (now: number) => {
+ measure();
+ const t = now - start;
+ const f = formingAt(formFrom + t);
+ // The timeline after the logo has formed, paused while the image is not ready.
+ const wait = readyAt === null ? Math.max(0, t - fillAt) : Math.max(0, readyAt - start - fillAt);
+ const tl = t <= fillAt ? t : Math.max(fillAt, t - wait);
+ const z = easeInOut(clamp01((tl - zoomAt) / ZOOM_MS));
+ const left = ox + m.leftCocoon + (m.leftLogo - m.leftCocoon) * f.shift;
+ const top = oy + m.top;
+ let d: string;
+ if (z <= 0) {
+ d = markPath(left, top, m.scale, f.up, f.down);
+ b.style.clipPath = `path('${d}')`;
+ } else {
+ // The logo grows while drifting to the canvas centre, and the canvas around it (a veil
+ // with the logo cut out) softens at the edge and fades away: the image dissolves out of
+ // the logo instead of being cut by it. A path, not a mask image, so no frame waits on a
+ // decode.
+ const k = 1 + (ZOOM_TO - 1) * z;
+ const c0x = left + 512 * m.scale;
+ const c0y = top + 512 * m.scale;
+ const cx = c0x + (ox + iw / 2 - c0x) * z;
+ const cy = c0y + (oy + ih / 2 - c0y) * z;
+ const s = m.scale * k;
+ d = markPath(cx - 512 * s, cy - 512 * s, s, 1, 1);
+ b.style.clipPath = "";
+ const v = veil.current;
+ const vp = veilPath.current;
+ if (v && vp) {
+ const feather = Math.min(iw, ih) * 0.05 * easeOut(z);
+ const pad = feather * 3 + 2;
+ const hole = markPath(cx - ox - 512 * s, cy - oy - 512 * s, s, 1, 1);
+ vp.setAttribute("d", `M${-pad} ${-pad} H${iw + pad} V${ih + pad} H${-pad} Z ` + hole);
+ v.style.display = "block";
+ v.style.opacity = String(1 - easeInOut(clamp01((z - 0.15) / 0.85)));
+ v.style.filter = feather > 0.3 ? `blur(${feather.toFixed(1)}px)` : "";
+ }
+ }
+ // The solid logo gives way to the image inside it.
+ const fill = easeOut(clamp01((tl - fillAt) / FILL_MS));
+ if (ink.current) {
+ ink.current.setAttribute("d", d);
+ ink.current.style.opacity = String(1 - fill);
+ }
+ const blur = 6 * (1 - easeOut(clamp01((tl - fillAt) / (FILL_MS + HOLD_MS + ZOOM_MS * 0.5))));
+ b.style.filter = blur > 0.05 ? `blur(${blur.toFixed(2)}px)` : "";
+ if (tl < zoomAt + ZOOM_MS) raf = requestAnimationFrame(frame);
+ else {
+ clearMask(b);
+ setRunning(false);
+ }
+ };
+ frame(start);
+ raf = requestAnimationFrame(frame);
+ return () => {
+ cancelAnimationFrame(raf);
+ window.clearTimeout(giveUp);
+ clearMask(b);
+ };
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+
+ return (
+ <div className="hatch" ref={ref}>
+ {/* The canvas stays visible while the logo opens on it. */}
+ {running && <div className="hatch-canvas" ref={canvasBox} />}
+ <div className="hatch-body" ref={body}>
+ {children}
+ </div>
+ {running && (
+ <div className="hatch-veil" ref={veilBox} aria-hidden>
+ <svg ref={veil}>
+ <path ref={veilPath} fillRule="evenodd" />
+ </svg>
+ </div>
+ )}
+ {running && (
+ <svg className="hatch-ink" aria-hidden>
+ <path ref={ink} />
+ </svg>
+ )}
+ </div>
+ );
+}
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsx
index 87a640b..39b6d5c 100644
--- a/src/components/Composer.tsx
+++ b/src/components/Composer.tsx
@@ -1,10 +1,10 @@
-import { ArrowUp, Brush, ChevronDown, Expand, FolderOpen, Gauge, ImagePlus, Lightbulb, Maximize, Pencil, SlidersHorizontal, Sparkles, X } from "lucide-react";
+import { ArrowUp, Brush, ChevronDown, Expand, FolderOpen, Gauge, ImagePlus, Lightbulb, Maximize, Pencil, SlidersHorizontal, Sparkles, WandSparkles, X } from "lucide-react";
import { useEffect, useMemo, useRef, useState } from "react";
import { api, urls } from "../lib/api";
-import { activeOverrides, aspectLabel, buildRequest, expansionPixels, formatDuration, hasOutpaint, imageName, moveRef, needsPrompt, parseRatio, type Ref } from "../lib/logic";
+import { activeOverrides, aspectLabel, buildRequest, enhancerApplies, expansionPixels, formatDuration, hasOutpaint, imageName, moveRef, needsPrompt, parseRatio, type Ref } from "../lib/logic";
import { native } from "../lib/native";
import {
- addAsReference, addInputs, editShownImage, getState, itemSource, selectedItem, sessionLabels, setState, startNewImage, submitComposer, toastError, updateComposer, useStore, type SessionItem,
+ addAsReference, addInputs, editShownImage, enhancerAvailable, getState, itemSource, selectedItem, sessionLabels, setState, startNewImage, submitComposer, toastError, updateComposer, useStore, type SessionItem,
} from "../lib/store";
import type { InpaintAction, Plan, Quality } from "../lib/types";
import { AdvancedPanel } from "./Advanced";
@@ -289,6 +289,7 @@ export function Composer() {
const taRef = useRef<HTMLTextAreaElement>(null);
const hasImages = c.refs.length > 0;
const overrides = activeOverrides(c.advanced);
+ const canEnhance = useStore(enhancerAvailable) && enhancerApplies(c);
useEffect(() => {
const ta = taRef.current;
@@ -441,6 +442,23 @@ export function Composer() {
{() => <AdvancedPanel plan={plan} />}
</Popover>
+ {canEnhance && (
+ <button
+ className={`pill enhance ${c.enhance ? "on" : ""}`}
+ role="switch"
+ aria-checked={c.enhance}
+ onClick={() => updateComposer({ enhance: !c.enhance })}
+ title={
+ c.enhance
+ ? "Prompt enhancer on: before making the image, the model rewrites your prompt as a detailed English description (looking at your image for edits). You see it being written; the result keeps both. Click to turn off."
+ : "Prompt enhancer: let the model rewrite a short prompt, in any language, as a detailed English description before making the image. Adds a few seconds."
+ }
+ >
+ <WandSparkles size={15} />
+ Enhance
+ </button>
+ )}
+
<button className="icon-btn" onClick={() => setState({ overlay: "prompt-tips" })} title="Prompt tips" aria-label="Prompt tips">
<Lightbulb size={17} />
</button>
diff --git a/src/components/Settings.tsx b/src/components/Settings.tsx
index 0422542..4f48d89 100644
--- a/src/components/Settings.tsx
+++ b/src/components/Settings.tsx
@@ -3,7 +3,7 @@ import { useEffect, useState } from "react";
import { writeText } from "@tauri-apps/plugin-clipboard-manager";
import { api } from "../lib/api";
import { native } from "../lib/native";
-import { getState, openModelSetup, refreshStatus, setState, startTour, toastError, toastInfo } from "../lib/store";
+import { getState, openModelSetup, refreshStatus, setState, startTour, toastError, toastInfo, useStore } from "../lib/store";
import { formatBytes } from "../lib/logic";
import { setThemeChoice, themeChoice, type ThemeChoice } from "../lib/theme";
import { UpscalerControls } from "./Upscalers";
@@ -34,6 +34,33 @@ function PathField(props: { label: string; value: string; onChange: (v: string)
);
}
+/** One of the prompt enhancer's instructions: the user's text, or the built-in one ("" saved). */
+function EnhancerPrompt(props: { label: string; help: string; value: string; builtin: string | null; max: number; onChange: (v: string) => void }) {
+ const custom = props.value.trim() !== "" && props.value.trim() !== props.builtin?.trim();
+ return (
+ <div className="field">
+ <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
+ <span style={{ flex: 1 }}>
+ {props.label} <span className="muted">· {custom ? "your instructions" : "built-in"}</span>
+ </span>
+ <button className="btn" disabled={!custom} onClick={() => props.onChange("")} title="Go back to Kokon's built-in instructions">
+ <RotateCcw size={13} /> Restore default
+ </button>
+ </div>
+ <textarea
+ className="input"
+ rows={9}
+ spellCheck={false}
+ maxLength={props.max}
+ value={props.value || props.builtin || ""}
+ placeholder={props.builtin === null ? "Loading…" : ""}
+ onChange={(e) => props.onChange(e.target.value.trim() === props.builtin?.trim() ? "" : e.target.value)}
+ />
+ <span className="muted">{props.help}</span>
+ </div>
+ );
+}
+
function Diagnostics() {
const [text, setText] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
@@ -96,9 +123,12 @@ export function Settings() {
const [legacy, setLegacy] = useState<{ files: string[]; bytes: number } | null>(null);
const [dirty, setDirty] = useState(false);
const [theme, setTheme] = useState(themeChoice);
+ const [enhancerDefaults, setEnhancerDefaults] = useState<{ t2i: string; edit: string; max_chars: number } | null>(null);
+ const textGeneration = useStore((s) => s.status?.server.text_generation);
useEffect(() => {
api.config().then(setCfg, toastError);
+ api.enhancerDefaults().then(setEnhancerDefaults, () => {});
api.setup().then(
(r: SetupReport) => {
setDevices(r.devices);
@@ -280,6 +310,35 @@ export function Settings() {
Start
</button>
</div>
+
+ <h4 style={{ margin: "14px 0 0", fontSize: 14, fontWeight: 600 }}>Prompt enhancer</h4>
+ <span className="muted" style={{ marginTop: -8 }}>
+ With Enhance on (next to the prompt), the model's own language model rewrites your prompt before making the image, following these instructions. It
+ always writes in English.
+ {textGeneration === false && " The inference engine in use cannot write prompts: Enhance appears with an engine that can."}
+ </span>
+ <div className="setting-card" style={{ alignItems: "flex-start" }}>
+ <span className="muted">
+ Kokon was tested only with its built-in instructions. Your own can make results worse, or make the model answer with something that is not a prompt; Restore
+ default brings the built-in ones back.
+ </span>
+ </div>
+ <EnhancerPrompt
+ label="New images"
+ help="Used when creating an image from the prompt alone."
+ value={cfg.enhancerT2iPrompt ?? ""}
+ builtin={enhancerDefaults?.t2i ?? null}
+ max={enhancerDefaults?.max_chars ?? 20000}
+ onChange={(v) => set({ enhancerT2iPrompt: v })}
+ />
+ <EnhancerPrompt
+ label="Edits"
+ help="Used when editing or combining images; the model sees the images."
+ value={cfg.enhancerEditPrompt ?? ""}
+ builtin={enhancerDefaults?.edit ?? null}
+ max={enhancerDefaults?.max_chars ?? 20000}
+ onChange={(v) => set({ enhancerEditPrompt: v })}
+ />
</>
)}
diff --git a/src/components/Stage.tsx b/src/components/Stage.tsx
index 28d583e..09b71ff 100644
--- a/src/components/Stage.tsx
+++ b/src/components/Stage.tsx
@@ -1,10 +1,10 @@
import {
- Brush, ClipboardPaste, Combine, Copy, Download, Ellipsis, Expand, FolderOpen, Heart, ImageUpscale, Info, Loader2, MousePointerClick, Palette, Pencil, RefreshCw, RotateCcw, SlidersHorizontal, Square, TextCursorInput, Trash2, Wand2, X,
+ Brush, ClipboardPaste, Combine, Copy, Download, Ellipsis, Expand, FolderOpen, Heart, ImageUpscale, Info, Loader2, MousePointerClick, Palette, Pencil, RefreshCw, RotateCcw, SlidersHorizontal, Square, TextCursorInput, Trash2, Wand2, WandSparkles, X,
} from "lucide-react";
import { Fragment, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { api, urls } from "../lib/api";
import {
- compareBase, composerFromRecord, formatDuration, jobFraction, jobLabel, lutInfo, lutLabel, MAX_UPSCALE_AREA, MAX_UPSCALE_SIDE, MODE_LABEL, pipelineSummary, remainingSeconds, sizeForWidth, upscaleRequest, arcLayout,
+ compareBase, composerFromRecord, formatDuration, jobFraction, jobLabel, jobPercent, 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";
@@ -12,6 +12,7 @@ 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 { Cocoon, Hatch, reducedMotion } from "./Cocoon";
import { CompareFrame, CompareFullscreen } from "./Compare";
import { LutBar, LutButton, LutOverlay } from "./Lut";
import { IconButton, Popover, useFit } from "./ui";
@@ -62,14 +63,13 @@ function Empty() {
);
}
-/** A version in progress: the image it starts from, dimmed, with live progress on top. */
+/** A version in progress. With the prompt enhancer, the prompt it writes first takes the whole
+ * frame, appearing word by word; a couple of seconds after the last word it fades and the
+ * completion percentage takes over, over the cocoon (components/Cocoon.tsx). */
function Pending({ job, item, labels }: { job: JobView; item: SessionItem; labels: Record<string, string> }) {
const o = job.plan?.output;
const { ref, box } = useFit(o ? o.width / o.height : 1);
const session = useStore((s) => s.session);
- const frac = jobFraction(job);
- const remain = remainingSeconds(job);
- const canCancel = !["done", "failed", "cancelled"].includes(job.status);
const src = item.request?.mode === "generate" ? undefined : item.request?.images?.[0];
const from = src ? session.find((i) => itemSource(i) === src) : undefined;
const mode = job.plan?.mode;
@@ -77,32 +77,182 @@ 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 [promptGone, setPromptGone] = useState(false);
+ const promptPhase = job.enhanced_prompt != null && !promptGone;
return (
<div className="stage-frame" ref={ref}>
- <div className="pending" style={{ width: box.w, height: box.h }}>
- {src && !outpaint && <img className="pending-src" src={urls.source(src)} alt="" draggable={false} />}
- <div className="pending-info">
- <span className="pending-title">{title}</span>
- <div className={`progress ${frac === null ? "indeterminate" : ""}`}>
- <i style={frac !== null ? { width: `${Math.round(frac * 100)}%` } : undefined} />
- </div>
- <span>
- {jobLabel(job)}
- {o ? ` · ${o.width}×${o.height}` : ""}
- {remain !== null ? ` · ${formatDuration(remain)} left` : ""}
- </span>
- {job.notes.length > 0 && <span className="notes">{job.notes.join(" ")}</span>}
- {canCancel && (
- <button className="btn" onClick={() => cancelJob(job.id)} title="Cancel">
- <Square size={13} /> Stop
- </button>
- )}
+ <div className={`pending ${promptPhase ? "prompt-phase" : ""}`} style={{ width: box.w, height: box.h }}>
+ <Cocoon src={src && !outpaint ? urls.source(src) : undefined} pct={pct} form={pct >= 92} dim={promptPhase} />
+ {promptPhase ? (
+ <PromptPhase
+ title={title}
+ text={job.enhanced_prompt!}
+ streaming={job.status === "enhancing_prompt"}
+ withImage={!!src}
+ budget={job.plan?.estimated_seconds || null}
+ onCancel={() => cancelJob(job.id)}
+ onGone={() => setPromptGone(true)}
+ />
+ ) : (
+ <ProgressPhase job={job} title={title} pct={pct} />
+ )}
+ </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() });
+ useEffect(() => {
+ let target = 0;
+ let value = 0;
+ const id = window.setInterval(() => {
+ const j = 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));
+ value += (target - value) * (reducedMotion() ? 1 : 0.18);
+ if (target - value < 0.05) value = target;
+ setShown(value);
+ }, 60);
+ return () => window.clearInterval(id);
+ }, []);
+ return shown;
+}
+
+function ProgressPhase({ job, title, pct }: { job: JobView; title: string; pct: number }) {
+ const o = job.plan?.output;
+ const remain = remainingSeconds(job);
+ const canCancel = !["done", "failed", "cancelled"].includes(job.status);
+ return (
+ <div className="progress-phase">
+ <span className="pending-title">{title}</span>
+ {canCancel && (
+ <button className="btn ghost stop-corner" onClick={() => cancelJob(job.id)} title="Cancel">
+ <Square size={12} /> Stop
+ </button>
+ )}
+ {/* The cocoon fills the middle; the percentage sits under it. */}
+ <div className="progress-foot">
+ <span className="job-pct" aria-label={`${Math.floor(pct)} percent`}>
+ {Math.floor(pct)}
+ <small>%</small>
+ </span>
+ <div className="progress">
+ <i style={{ width: `${pct}%` }} />
+ </div>
+ <span>
+ {jobLabel(job)}
+ {o ? ` · ${o.width}×${o.height}` : ""}
+ {remain !== null ? ` · ${formatDuration(remain)} left` : ""}
+ </span>
+ {job.notes.length > 0 && <span className="notes">{job.notes.join(" ")}</span>}
+ </div>
+ </div>
+ );
+}
+
+/** How long the finished prompt stays on screen before the percentage takes over (ms). */
+const PROMPT_HOLD_MS = 2200;
+const PROMPT_FADE_MS = 700;
+
+/** The prompt enhancer's text over the whole frame, revealed word by word at a calm pace. The
+ * model writes it in a second or two and the image starts right away, so the text keeps
+ * appearing while the image is made: reading it fills the wait. It never runs ahead of the text
+ * that has arrived. */
+function PromptPhase(props: { title: string; text: string; streaming: boolean; withImage: boolean; budget: number | null; onCancel: () => void; onGone: () => void }) {
+ const { text, streaming } = props;
+ const shown = useTypewriter(text, streaming, props.budget);
+ const ref = useRef<HTMLDivElement>(null);
+ const words = useMemo(() => text.split(/(\s+)/), [text]);
+ const writing = streaming || shown < text.length;
+ const [leaving, setLeaving] = useState(false);
+ const onGone = useRef(props.onGone);
+ onGone.current = props.onGone;
+ useEffect(() => {
+ if (writing || !text) return;
+ const hold = window.setTimeout(() => setLeaving(true), PROMPT_HOLD_MS);
+ const gone = window.setTimeout(() => onGone.current(), PROMPT_HOLD_MS + PROMPT_FADE_MS);
+ return () => (window.clearTimeout(hold), window.clearTimeout(gone));
+ }, [writing, text]);
+ // Whole words up to the revealed length; each new one fades in once (stable keys).
+ const visible: React.ReactNode[] = [];
+ let at = 0;
+ for (let i = 0; i < words.length && at < shown; i++) {
+ const w = words[i];
+ visible.push(/^\s+$/.test(w) ? w : <span key={i} className="w">{w}</span>);
+ at += w.length;
+ }
+ useEffect(() => {
+ const el = ref.current;
+ if (el && writing) el.scrollTop = el.scrollHeight;
+ }, [visible.length, writing]);
+ return (
+ <div className={`prompt-stage ${leaving ? "leaving" : ""}`} style={{ transitionDuration: `${PROMPT_FADE_MS}ms` }}>
+ <span className={`enhanced-label ${writing ? "shimmer" : ""}`}>
+ <WandSparkles size={13} /> {writing ? "Writing the prompt" : "The prompt"} · {props.title}
+ </span>
+ <div className="prompt-text" ref={ref} aria-live="polite">
+ <div className="prompt-flow">
+ {text ? visible : <span className="enhanced-wait shimmer">{props.withImage ? "Looking at your image…" : "Reading your idea…"}</span>}
+ {writing && text && <i className="caret" />}
</div>
</div>
+ <button className="btn ghost" onClick={props.onCancel} title="Cancel">
+ <Square size={12} /> Stop
+ </button>
</div>
);
}
+/** Calm reading pace of the prompt (characters a second). */
+const TYPE_CHARS_PER_S = 32;
+
+/** How many characters of `text` to show now: ~32 a second, faster when needed to finish in
+ * 4-12 s (and in half the time a quick image takes), never past `text`. */
+function useTypewriter(text: string, streaming: boolean, budget: number | null): number {
+ const [shown, setShown] = useState(() => (reducedMotion() ? text.length : 0));
+ const target = useRef({ text, streaming, budget });
+ target.current = { text, streaming, budget };
+ const start = useRef<number | null>(null);
+ useEffect(() => {
+ if (reducedMotion()) return;
+ let raf = 0;
+ let last = performance.now();
+ let n = 0;
+ const tick = (now: number) => {
+ const { text: t, streaming: s, budget: b } = target.current;
+ const dt = Math.min(0.1, (now - last) / 1000);
+ last = now;
+ if (t.length > 0 && start.current === null) start.current = now;
+ if (n < t.length) {
+ const cap = Math.max(4, Math.min(12, b ? b * 0.5 : 12));
+ const total = Math.min(cap, Math.max(4, t.length / TYPE_CHARS_PER_S));
+ const left = Math.max(0.4, total - (now - (start.current ?? now)) / 1000);
+ const speed = Math.max(s ? 20 : TYPE_CHARS_PER_S, (t.length - n) / left);
+ n = Math.min(t.length, n + speed * dt);
+ setShown(n);
+ }
+ // Done once everything has arrived and is on screen.
+ if (s || n < t.length) raf = requestAnimationFrame(tick);
+ };
+ raf = requestAnimationFrame(tick);
+ return () => cancelAnimationFrame(raf);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+ return reducedMotion() ? text.length : shown;
+}
+
function Failed({ job, item }: { job: JobView; item: SessionItem }) {
const [details, setDetails] = useState(false);
const req = item.request;
@@ -157,7 +307,12 @@ function InfoPanel({ g }: { g: GenerationRecord }) {
];
return (
<div style={{ width: 340, padding: 8, display: "flex", flexDirection: "column", gap: 6 }}>
- <div style={{ fontSize: 13, userSelect: "text", maxHeight: 120, overflow: "auto" }}>{g.prompt}</div>
+ <div style={{ fontSize: 13, userSelect: "text", maxHeight: 160, overflow: "auto" }}>{g.prompt}</div>
+ {g.original_prompt && (
+ <div className="muted" style={{ userSelect: "text", maxHeight: 80, overflow: "auto" }}>
+ <WandSparkles size={11} style={{ verticalAlign: -1 }} /> Written by the prompt enhancer from: “{g.original_prompt}”
+ </div>
+ )}
{g.negative_prompt && <div className="muted">Negative: {g.negative_prompt}</div>}
<div style={{ display: "grid", gridTemplateColumns: "auto 1fr", gap: "4px 12px", fontSize: 12 }}>
{rows.map(([k, v]) => (
@@ -388,9 +543,11 @@ function InputActions({ item, onLut }: { item: SessionItem; onLut: (lut: LutEntr
/** A result or input fitted to the stage (never enlarged past its own size), with the
* full-screen button in its top-right corner. */
-function PlainFrame({ src, alt, w, h, maskSrc, overlay, onFullscreen }: { src: string; alt: string; w: number; h: number; maskSrc?: string; overlay?: React.ReactNode; onFullscreen: () => void }) {
+/** `fill`: enlarged to fill the frame like the card it replaces (a result); otherwise never shown
+ * wider than it is. */
+function PlainFrame({ src, alt, w, h, maskSrc, overlay, fill, onFullscreen }: { src: string; alt: string; w: number; h: number; maskSrc?: string; overlay?: React.ReactNode; fill?: boolean; onFullscreen: () => void }) {
const { ref, box } = useFit(w / h);
- const bw = Math.min(box.w, w);
+ const bw = fill ? box.w : Math.min(box.w, w);
return (
<div className="stage-frame" ref={ref}>
<div className="image-box" style={{ width: bw, height: bw * (h / w) }}>
@@ -417,8 +574,13 @@ function StageBar(props: { item: SessionItem; label: string; showOriginal: boole
<div className="stage-meta">
<span className="ver">{imageName(label)}</span>
{g && <span className="mode">{MODE_LABEL[g.mode]}</span>}
- <span className="stage-prompt" title={g?.prompt ?? name}>
- {g ? g.prompt || (g.mode === "upscale" ? `${g.references[0]?.width ?? "?"}×${g.references[0]?.height ?? "?"} → ${w}×${h}` : lut ? `${lutLabel(lut)} · ${Math.round(lut.intensity * 100)} %` : "") : name}
+ {g?.original_prompt && (
+ <span className="enhanced-mark" title="The prompt enhancer wrote this image's prompt">
+ <WandSparkles size={13} />
+ </span>
+ )}
+ <span className="stage-prompt" title={g?.original_prompt ? `${g.prompt}\n\nYour prompt: ${g.original_prompt}` : g?.prompt ?? name}>
+ {g ? g.original_prompt || g.prompt || (g.mode === "upscale" ? `${g.references[0]?.width ?? "?"}×${g.references[0]?.height ?? "?"} → ${w}×${h}` : lut ? `${lutLabel(lut)} · ${Math.round(lut.intensity * 100)} %` : "") : name}
</span>
{from && !props.lutBar ? (
<span className="seg dims-seg" role="radiogroup" aria-label="Which image to show">
@@ -825,6 +987,7 @@ export function Stage() {
);
view = (
<>
+ <Hatch key={item.key} id={g?.id} aspect={w / h}>
{lutActive ? (
<PlainFrame
key={src}
@@ -840,8 +1003,9 @@ export function Stage() {
) : item.gen && base ? (
<CompareFrame key={`compare-${item.gen.id}`} g={item.gen} base={base} maskSrc={maskSrc} onFullscreen={openFull} />
) : (
- <PlainFrame key={src} src={src} alt={item.gen?.prompt ?? item.input?.original_name ?? ""} w={w} h={h} maskSrc={maskSrc} onFullscreen={openFull} />
+ <PlainFrame key={src} src={src} alt={item.gen?.prompt ?? item.input?.original_name ?? ""} w={w} h={h} maskSrc={maskSrc} fill={!!item.gen} onFullscreen={openFull} />
)}
+ </Hatch>
<StageBar item={item} label={labels[item.key]} showOriginal={showOriginal} setShowOriginal={(on) => setOriginalOf(on && g ? g.id : null)} lutBar={lutBar} onLut={startLut} />
</>
);
diff --git a/src/lib/api.ts b/src/lib/api.ts
index c60efb5..0ef9a3f 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -89,6 +89,8 @@ export const api = {
installStatus: () => call<InstallStatus>("/setup/install"),
cancelInstall: () => call<InstallStatus>("/setup/install/cancel", { method: "POST" }),
config: () => call<AppConfig>("/config"),
+ /** The prompt enhancer's built-in instructions. */
+ enhancerDefaults: () => call<{ t2i: string; edit: string; max_chars: number }>("/enhancer/defaults"),
saveConfig: (c: AppConfig) => call<{ saved: boolean; backend_restart_needed: boolean }>("/config", { method: "PUT", body: json(c) }),
startBackend: () => call<{ ready: boolean }>("/backend/start", { method: "POST" }),
stopBackend: () => call<{ stopped: boolean }>("/backend/stop", { method: "POST" }),
diff --git a/src/lib/logic.test.ts b/src/lib/logic.test.ts
index a61b93d..1d8cb4f 100644
--- a/src/lib/logic.test.ts
+++ b/src/lib/logic.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import {
- activeOverrides, buildRequest, compareBase, diffPixels, emptyComposer, expansionPixels, expansionPreset, jobFraction, jobLabel, lutInfo, moveRef, needsPrompt, normalizeQuality, persistentAdvanced, pipelineSummary,
+ 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,
neighbourAfterRemoval, previewIndex, starPromptDue, STAR_PROMPT_AFTER, formatBytes, formatDuration, installItemShort, installItemText, installProgress, installStep, installTimeLeft, timelineLinks, undoTarget, aspectLabel, parseRatio, sizeForRatio, expandToRatio, expandToSize, arcLayout,
} from "./logic";
@@ -96,6 +96,27 @@ describe("buildRequest", () => {
const c = { ...emptyComposer(), prompt: "x", refs: [ref(1)], mode: "generate" as const };
expect(buildRequest(c).images).toEqual([]);
});
+ it("prompt enhancer: off by default, remembered, only for new images, edits and combines", () => {
+ expect(emptyComposer().enhance).toBe(false);
+ expect(emptyComposer({ enhance: true }).enhance).toBe(true);
+ const off = { ...emptyComposer(), prompt: "un gatto" };
+ expect("enhance" in buildRequest(off)).toBe(false);
+ const on = { ...off, enhance: true };
+ expect(buildRequest(on).enhance).toBe(true);
+ expect(buildRequest({ ...on, refs: [ref(1), ref(2)] }).enhance).toBe(true);
+ const masked = { ...on, refs: [ref(1)], mask: { inputId: "m", refKey: "k1" } };
+ expect(enhancerApplies(masked)).toBe(false);
+ expect("enhance" in buildRequest(masked)).toBe(false);
+ const expanded = { ...on, refs: [ref(1)], outpaint: { left: 10, right: 0, top: 0, bottom: 0, units: "percent" as const } };
+ expect("enhance" in buildRequest(expanded)).toBe(false);
+ });
+ it("reusing an enhanced result's settings gives the user's prompt with the enhancer on", () => {
+ const g = { prompt: "A tabby cat sleeping on a sunlit windowsill.", original_prompt: "un gatto", references: [], mask_input_id: null, outpaint: null, quality: "fast", request: null } as unknown as GenerationRecord;
+ const c = composerFromRecord(g, emptyComposer());
+ expect([c.prompt, c.enhance]).toEqual(["un gatto", true]);
+ const plain = composerFromRecord({ ...g, original_prompt: null }, { ...emptyComposer(), enhance: true });
+ expect([plain.prompt, plain.enhance]).toEqual(["A tabby cat sleeping on a sunlit windowsill.", false]);
+ });
});
describe("expansion", () => {
@@ -124,6 +145,20 @@ describe("job labels are truthful", () => {
expect(jobLabel(noDetail)).toBe("Generating");
expect(jobFraction(noDetail)).toBeNull();
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", () => {
diff --git a/src/lib/logic.ts b/src/lib/logic.ts
index 5a418b6..11e26c7 100644
--- a/src/lib/logic.ts
+++ b/src/lib/logic.ts
@@ -29,6 +29,8 @@ export interface Composer {
customHeight: number;
seed: number | null;
advanced: Advanced;
+ /** Prompt enhancer on (remembered; used only where `enhancerApplies`). */
+ enhance: boolean;
}
/** A preset name from any version: older ones also had "turbo" (now Fast) and "ultra" (now Quality). */
@@ -53,6 +55,7 @@ export const emptyComposer = (prefs?: Partial<Composer>): Composer => ({
customHeight: prefs?.customHeight ?? 1024,
seed: null,
advanced: newChatAdvanced(prefs?.advanced),
+ enhance: prefs?.enhance === true,
});
/** Advanced settings a new chat starts with: everything kept except Reference detail, which
@@ -112,9 +115,17 @@ export function buildRequest(c: Composer, parentId?: string | null): GenerationR
advanced: cleanAdvanced(c.advanced),
client: "ui",
parent_id: parentId ?? null,
+ ...(c.enhance && enhancerApplies(c) ? { enhance: true } : {}),
};
}
+/** The prompt enhancer works on new images, edits and combines; a painted area or an expansion
+ * wraps the prompt in its own instruction. */
+export function enhancerApplies(c: Composer): boolean {
+ const maskValid = c.mask && c.refs[0] && c.mask.refKey === c.refs[0].key;
+ return !maskValid && !hasOutpaint(c.outpaint);
+}
+
/** False only for a removal in a painted area, which needs no description. */
export function needsPrompt(c: Composer): boolean {
const maskValid = c.mask && c.refs[0] && c.mask.refKey === c.refs[0].key;
@@ -157,7 +168,9 @@ export function composerFromRecord(g: GenerationRecord, base: Composer): Compose
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,
- prompt: g.prompt,
+ // 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,
refs,
mask: g.mask_input_id && refs[0] ? { inputId: g.mask_input_id, refKey: refs[0].key } : null,
inpaintAction: req?.inpaint_action ?? "edit",
@@ -317,6 +330,8 @@ export function jobLabel(j: JobView): string {
return "Loading model";
case "preparing":
return "Preparing";
+ case "enhancing_prompt":
+ return "Writing the prompt";
case "saving":
return "Saving";
case "done":
@@ -359,6 +374,49 @@ export function jobFraction(j: JobView): number | null {
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 {
+ switch (j.status) {
+ case "queued":
+ case "failed":
+ case "cancelled":
+ return 0;
+ case "starting_backend":
+ return 2;
+ case "preparing":
+ return 4;
+ case "enhancing_prompt":
+ return 5;
+ case "saving":
+ return 99;
+ case "done":
+ return 100;
+ }
+ switch (j.stage) {
+ case "preparing":
+ return 6;
+ case "encoding_images":
+ case "encoding_prompt":
+ return 8;
+ case "decoding":
+ return 93;
+ case "upscaling":
+ return 95;
+ 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];
+ }
+ 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);
+}
+
export function remainingSeconds(j: JobView): number | null {
if (j.status !== "running" || !j.step || !j.seconds_per_step) return null;
if (j.stage !== "generating") return null;
diff --git a/src/lib/store.ts b/src/lib/store.ts
index 4e59b5f..96729a2 100644
--- a/src/lib/store.ts
+++ b/src/lib/store.ts
@@ -6,6 +6,7 @@
// opened again.
import { useSyncExternalStore } from "react";
+import { markFresh } from "../components/Cocoon";
import { api, ApiError, setPrivateInputs, subscribe } from "./api";
import { buildRequest, emptyComposer, needsPrompt, newKey, persistentAdvanced, refFromGen, refFromInput, starPromptDue, timelineLinks, undoTarget, versionLabels, type CompareMode, type Composer, type Ref } from "./logic";
import type { AppErrorBody, GenerationRecord, GenerationRequest, InputRecord, JobView, ServerState, StatusResponse } from "./types";
@@ -105,9 +106,9 @@ function loadPrefs(): Partial<Composer> {
function savePrefs(c: Composer) {
try {
- const { quality, resolution, aspect, customWidth, customHeight } = c;
+ const { quality, resolution, aspect, customWidth, customHeight, enhance } = c;
const advanced = persistentAdvanced(c.advanced);
- localStorage.setItem(PREFS_KEY, JSON.stringify({ quality, resolution, aspect, customWidth, customHeight, advanced }));
+ localStorage.setItem(PREFS_KEY, JSON.stringify({ quality, resolution, aspect, customWidth, customHeight, advanced, enhance }));
} catch {
/* storage unavailable */
}
@@ -179,7 +180,7 @@ const toStored = (i: SessionItem): StoredItem => ({
function titleOf(items: SessionItem[]): string | null {
for (const i of items) {
- const p = i.gen?.prompt || i.request?.prompt;
+ const p = i.gen?.original_prompt || i.gen?.prompt || i.request?.prompt;
if (p) return p;
if (i.input) return i.input.original_name ? safeDecode(i.input.original_name) : "Imported image";
}
@@ -369,6 +370,15 @@ let unsub: (() => void) | null = null;
export async function refreshStatus() {
try {
const st = await api.status();
+ const tg = st.server.text_generation;
+ if (typeof tg === "boolean" && tg !== knownTextGeneration) {
+ knownTextGeneration = tg;
+ try {
+ localStorage.setItem(TEXT_GEN_KEY, tg ? "1" : "0");
+ } catch {
+ /* storage unavailable */
+ }
+ }
setState({ status: st, server: { state: st.server.state, owned: st.server.owned, error: st.server.error } });
return st;
} catch {
@@ -376,12 +386,32 @@ export async function refreshStatus() {
}
}
+const TEXT_GEN_KEY = "kokon.textGeneration";
+let knownTextGeneration = (() => {
+ try {
+ return localStorage.getItem(TEXT_GEN_KEY) === "1";
+ } catch {
+ return false;
+ }
+})();
+
+/** The engine can write prompts (prompt enhancer): as the loaded model reports, else as it did
+ * last time, so the switch is there before the model loads. */
+export function enhancerAvailable(s: State): boolean {
+ return s.status?.server.text_generation ?? knownTextGeneration;
+}
+
export function startEvents() {
unsub?.();
unsub = subscribe(
(ev) => {
if (ev.type === "job") onJob(ev.job);
- else if (ev.type === "server") setState({ server: { state: ev.state, owned: ev.owned, error: ev.error } });
+ else if (ev.type === "server") {
+ const loaded = ev.state === "ready" && getState().server.state !== "ready";
+ setState({ server: { state: ev.state, owned: ev.owned, error: ev.error } });
+ // What the newly loaded engine can do (the prompt enhancer).
+ if (loaded) refreshStatus();
+ }
else if (ev.type === "gallery") setState((s) => ({ galleryVersion: s.galleryVersion + 1 }));
},
(ok) => {
@@ -402,6 +432,11 @@ function onJob(job: JobView) {
if (job.status === "failed" && prev?.status !== "failed" && isMissingModel(job.error) && job.finished_at && Date.now() - Date.parse(job.finished_at) < 60_000) {
openModelSetup();
}
+ // Results replacing a version in progress of the open project hatch out of the cocoon when
+ // first shown (marked before the state change renders them).
+ if (job.status === "done" && prev?.status !== "done" && getState().session.some((i) => i.jobId === job.id && i.kind === "job")) {
+ markFresh(job.results.map((g) => g.id));
+ }
// Made with this app (it had a pending version), not by another client such as MCP.
let made = false;
setState((s) => {
@@ -749,6 +784,8 @@ export function reusePrompt(g: GenerationRecord) {
composer: {
...c,
prompt: g.prompt,
+ // The enhancer's text is a full prompt already: rewriting it again would drift.
+ enhance: g.original_prompt ? false : c.enhance,
refs,
mask,
inpaintAction: mask ? g.request?.inpaint_action ?? "edit" : c.inpaintAction,
diff --git a/src/lib/types.ts b/src/lib/types.ts
index 98794ba..f50a633 100644
--- a/src/lib/types.ts
+++ b/src/lib/types.ts
@@ -59,6 +59,8 @@ export interface GenerationRequest {
upscale?: UpscaleSpec | null;
/** Private mode: kept out of the gallery and the output folder, deleted when it ends. */
private?: boolean;
+ /** Prompt enhancer: the model's language model rewrites the prompt first (new images, edits, combines). */
+ enhance?: boolean;
}
export interface WorkingCanvas { source: Size; content: Size; canvas: Size; content_offset: [number, number]; scale: number }
@@ -114,7 +116,10 @@ export interface GenerationRecord {
id: string;
created_at: string;
mode: RecordMode;
+ /** The prompt the image was made from (the prompt enhancer's when it ran). */
prompt: string;
+ /** The user's own prompt, when the prompt enhancer rewrote it. */
+ original_prompt?: string | null;
negative_prompt: string;
seed: number;
width: number;
@@ -148,7 +153,7 @@ export interface LutEntry { name: string; title: string | null; kind: "1d" | "3d
/** How a "lut" result was made (in its `request`). */
export interface LutInfo { name: string; title?: string | null; intensity: number }
-export type JobStatus = "queued" | "starting_backend" | "preparing" | "running" | "saving" | "done" | "failed" | "cancelled";
+export type JobStatus = "queued" | "starting_backend" | "preparing" | "enhancing_prompt" | "running" | "saving" | "done" | "failed" | "cancelled";
export type BackendStage = "preparing" | "encoding_images" | "encoding_prompt" | "generating" | "upscaling" | "refining" | "decoding" | "finishing";
export interface JobView {
@@ -163,6 +168,8 @@ export interface JobView {
started_at: string | null;
finished_at: string | null;
prompt: string;
+ /** The prompt enhancer's text: growing while "enhancing_prompt", then the prompt in use. */
+ enhanced_prompt?: string | null;
plan: Plan | null;
seed: number | null;
results: GenerationRecord[];
@@ -188,7 +195,11 @@ export type ServerState = "stopped" | "starting" | "ready" | "error";
export interface StatusResponse {
service: { version: string; pid: number; uptime_s: number };
- server: { state: ServerState; owned: boolean; pid: number | null; port: number; model: string | null; model_loaded: boolean; error: AppErrorBody | null };
+ server: {
+ state: ServerState; owned: boolean; pid: number | null; port: number; model: string | null; model_loaded: boolean; error: AppErrorBody | null;
+ /** The loaded engine can write prompts (prompt enhancer); null while no model is loaded. */
+ text_generation?: boolean | null;
+ };
profile: { id: string; display_name: string; source: ProfileSource };
gpu: { name: string; vendor: string; unified: boolean; memory_total_mib: number; memory_used_mib: number; memory_free_mib: number; driver_version: string } | null;
active_job: JobView | null;
@@ -262,6 +273,9 @@ export interface AppConfig {
hardwarePreset: string;
/** Plan as if the GPU had this much VRAM (MiB); 0 = detected. */
vramOverrideMib: number;
+ /** Prompt enhancer instructions for new images and for edits; "" = the built-in ones. */
+ enhancerT2iPrompt?: string;
+ enhancerEditPrompt?: string;
ui: Record<string, unknown>;
}
diff --git a/src/styles.css b/src/styles.css
index d050a6b..84d4e51 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -142,17 +142,58 @@ svg { display: block; flex: none; }
.empty .hint span { display: flex; gap: 6px; align-items: center; }
/* pending job */
-.pending { position: relative; max-width: 100%; max-height: 100%; border-radius: 6px; background: var(--surface); overflow: hidden; display: grid; place-items: center; }
-.pending::after { content: ""; position: absolute; inset: 0; background: linear-gradient(100deg, transparent 30%, rgba(127,127,127,0.08) 50%, transparent 70%); background-size: 200% 100%; animation: sheen 1.8s linear infinite; }
-@keyframes sheen { from { background-position: 150% 0; } to { background-position: -50% 0; } }
-.pending-src { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0.35; filter: blur(2px) saturate(0.6); }
+.pending { position: relative; max-width: 100%; max-height: 100%; border-radius: 10px; background: var(--surface); overflow: hidden; display: grid; place-items: center; isolation: isolate; }
+/* the cocoon (components/Cocoon.tsx): silk on a canvas, the edit's source inside it */
+.cocoon { position: absolute; inset: 0; z-index: 0; transition: opacity 0.8s; }
+.cocoon.dim { opacity: 0.32; }
+.cocoon canvas { position: absolute; inset: 0; display: block; }
+.cocoon-src { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transition: filter 0.9s ease-out, opacity 0.9s ease-out; }
+/* a fresh result seen through the logo, which grows until it is the image (Hatch) */
+.hatch { position: relative; flex: 1; min-height: 0; width: 100%; display: flex; }
+.hatch-body { position: relative; z-index: 1; flex: 1; min-width: 0; min-height: 0; display: flex; }
+.hatch-canvas { position: absolute; z-index: 0; border-radius: 10px; background: var(--surface); }
+.hatch-veil { position: absolute; z-index: 2; border-radius: 10px; overflow: hidden; pointer-events: none; }
+.hatch-veil svg { display: none; position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; will-change: opacity, filter; }
+.hatch-veil path { fill: var(--surface); }
+.hatch-ink { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 2; pointer-events: none; overflow: visible; }
+.hatch-ink path { fill: var(--text); }
.pending-title { color: var(--text); font-weight: 600; font-size: 14px; }
.pending-info { position: relative; z-index: 1; display: flex; flex-direction: column; align-items: center; gap: 12px; color: var(--text-2); font-size: 13px; padding: 24px; text-align: center; }
+.progress-phase { position: absolute; inset: 0; z-index: 1; display: flex; flex-direction: column; align-items: center; justify-content: space-between; padding: 5% 6% 5.5%; color: var(--text-2); font-size: 13px; text-align: center; animation: phase-in 0.8s cubic-bezier(0.2, 0.7, 0.2, 1) both; }
+@keyframes phase-in { from { opacity: 0; filter: blur(4px); } to { opacity: 1; filter: none; } }
+.progress-phase .stop-corner { position: absolute; top: 3.5%; right: 3%; height: 28px; font-size: 12px; }
+.progress-foot { display: flex; flex-direction: column; align-items: center; gap: 8px; width: 100%; }
+.job-pct { font-size: clamp(30px, 5.5vw, 52px); font-weight: 650; letter-spacing: -0.03em; line-height: 1; color: var(--text); font-variant-numeric: tabular-nums; }
+.job-pct small { font-size: 0.42em; font-weight: 500; margin-left: 2px; color: var(--text-2); letter-spacing: 0; }
+.progress-foot .progress { width: min(240px, 60%); }
+.progress-foot .progress > i { transition: width 0.12s linear; }
.progress { width: 220px; height: 3px; background: var(--surface-3); border-radius: 2px; overflow: hidden; position: relative; }
.progress > i { position: absolute; inset: 0 auto 0 0; background: var(--text); border-radius: 2px; transition: width 0.3s; }
.progress.indeterminate > i { width: 30%; animation: indet 1.3s ease-in-out infinite; }
@keyframes indet { from { left: -30%; } to { left: 100%; } }
.notes { color: var(--text-3); font-size: 12px; max-width: 360px; }
+/* prompt enhancer: its prompt fills the frame while it is written, then fades for the percentage */
+.prompt-stage { position: relative; z-index: 1; display: flex; flex-direction: column; gap: 14px; width: 100%; height: 100%; padding: clamp(18px, 5%, 44px); transition-property: opacity, transform, filter; transition-timing-function: ease-in; }
+.prompt-stage.leaving { opacity: 0; transform: scale(0.97); filter: blur(8px); }
+.prompt-text { flex: 1; min-height: 0; overflow: auto; scrollbar-width: none; display: flex; flex-direction: column; white-space: pre-wrap; user-select: text; color: var(--text); font-size: clamp(15px, 2.1vw, 22px); line-height: 1.5; letter-spacing: -0.005em; text-shadow: 0 1px 18px color-mix(in srgb, var(--bg, #000) 70%, transparent); }
+.prompt-flow { margin: auto 0; }
+.prompt-stage .btn.ghost { align-self: center; }
+.enhanced-label { display: flex; align-items: center; gap: 6px; color: var(--text-3); font-size: 12px; letter-spacing: 0.02em; }
+/* each word arrives out of a soft blur */
+.prompt-text .w { animation: word-in 0.9s cubic-bezier(0.2, 0.7, 0.2, 1) both; }
+@keyframes word-in { from { opacity: 0; filter: blur(6px); } 40% { opacity: 0.55; } to { opacity: 1; filter: blur(0); } }
+.shimmer { background: linear-gradient(90deg, var(--text-3) 0%, var(--text-3) 35%, var(--text) 50%, var(--text-3) 65%, var(--text-3) 100%); background-size: 250% 100%; -webkit-background-clip: text; background-clip: text; color: transparent; animation: shimmer 2s linear infinite; }
+.shimmer svg { color: var(--text-2); }
+@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -150% 0; } }
+.enhanced-wait { display: block; text-align: center; font-size: 0.8em; }
+.prompt-text .caret { display: inline-block; width: 2px; height: 1.05em; margin-left: 3px; vertical-align: -3px; border-radius: 1px; background: var(--text); box-shadow: 0 0 10px var(--text); animation: caret 1.1s ease-in-out infinite; }
+@keyframes caret { 50% { opacity: 0.15; } }
+@media (prefers-reduced-motion: reduce) {
+ .prompt-text .w, .shimmer, .prompt-text .caret, .progress-phase { animation: none; }
+ .shimmer { color: var(--text-3); background: none; }
+}
+.stage-meta .enhanced-mark { flex: none; display: inline-flex; color: var(--text-2); }
+.pill.enhance.on { background: var(--surface-2); }
.failed { display: flex; flex-direction: column; align-items: center; gap: 10px; max-width: 460px; text-align: center; color: var(--text-2); }
.failed strong { color: var(--text); font-weight: 600; }