Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

web: generate images in the conversation when the model can

JustVugg39d4e73bdf8d
diff --git a/web/src/App.tsx b/web/src/App.tsx
index 9badb41b..002f046b 100644
--- a/web/src/App.tsx
+++ b/web/src/App.tsx
@@ -24,18 +24,26 @@ import {
SlidersHorizontal,
Timer,
Trash2,
- Zap, ImagePlus} from "lucide-react"
+ Zap, ImagePlus, Camera, Grid3x3, Palette} from "lucide-react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
-import { getHealth, listModels, streamChat, type ChatMessage, type HealthResponse, type StreamChatResult } from "@/lib/api"
+import {
+ generateImage, generatesImages, getHealth, listModelInfo, streamChat,
+ type ChatMessage, type GeneratedImage, type HealthResponse, type StreamChatResult,
+} from "@/lib/api"
+import {
+ DEFAULT_SIZE, DEFAULT_STEPS, chatTurns, clampSteps, dataUrlBlob, imageFileName, parseSeed, parseSize,
+ presetOf, randomSeed, sizeKey, validSide, type ImageSize,
+} from "@/lib/images"
import { resendFrom } from "@/lib/chat"
import { activeRequests, supportsCacheSlots, supportsContinuation } from "@/lib/runtime"
import Brio from "./Brio"
import { BrainWorkspace } from "./BrainWorkspace"
import { Brand } from "./components/Brand"
+import { ImageControls, ImageProgressCard, ImageResultCard, ImageStoppedCard, type ImageRun } from "./components/ImageTurn"
import { Markdown } from "./components/Markdown"
import { NavigationDock, type View } from "./components/NavigationDock"
import { Profiling } from "./Profiling"
@@ -62,6 +70,9 @@ export default function App() {
})
const [apiKey, setApiKey] = useState("")
const [models, setModels] = useState<string[]>([])
+ /* Models the server marks with the image_generation capability. With one of
+ them selected the conversation generates pictures instead of text. */
+ const [imageModels, setImageModels] = useState<string[]>([])
const [model, setModel] = useState(() => stored(localStorage, "colibri.model", "glm-5.2-colibri"))
const [temperature, setTemperature] = useState(0.7)
const [maxTokens, setMaxTokens] = useState(4096)
@@ -91,6 +102,13 @@ export default function App() {
const fileRef = useRef<HTMLInputElement>(null)
/* Pictures wait here until the turn is sent, then travel on the message. */
const [pending, setPending] = useState<string[]>([])
+ /* Size and steps are remembered like the endpoint; the seed is not, because
+ a remembered seed would silently draw the same picture again. */
+ const [imageSize, setImageSize] = useState<ImageSize>(() => parseSize(stored(localStorage, "colibri.imageSize", "")) || DEFAULT_SIZE)
+ const [customSize, setCustomSize] = useState(() => !presetOf(parseSize(stored(localStorage, "colibri.imageSize", "")) || DEFAULT_SIZE))
+ const [imageSteps, setImageSteps] = useState(() => clampSteps(Number(stored(localStorage, "colibri.imageSteps", String(DEFAULT_STEPS)))))
+ const [seedText, setSeedText] = useState("")
+ const [imageRun, setImageRun] = useState<ImageRun | null>(null)
const historyDialog = useRef<HTMLDialogElement>(null)
useEffect(() => { document.documentElement.dataset.theme = theme; try { localStorage.setItem("colibri.theme", theme) } catch {} }, [theme])
useEffect(() => { if (historyOpen) historyDialog.current?.showModal(); else historyDialog.current?.close() }, [historyOpen])
@@ -104,6 +122,8 @@ export default function App() {
const active = activeRequests(health)
const capacity = health?.scheduler?.capacity || kvSlots
const failures = health?.scheduler ? health.scheduler.rejected + health.scheduler.timed_out + health.scheduler.cancelled : 0
+ const imageMode = imageModels.includes(model)
+ const seed = parseSeed(seedText)
const updateMessages = (next: ChatMessage[] | ((current: ChatMessage[]) => ChatMessage[])) =>
setConversations((current) => ({
@@ -115,6 +135,9 @@ export default function App() {
useEffect(() => {
persistPublicSettings(localStorage, baseUrl, model)
}, [baseUrl, model])
+ useEffect(() => {
+ try { localStorage.setItem("colibri.imageSize", sizeKey(imageSize)); localStorage.setItem("colibri.imageSteps", String(imageSteps)) } catch { /* restricted storage mode */ }
+ }, [imageSize, imageSteps])
// EFFECT #2
useEffect(() => {
@@ -164,8 +187,10 @@ export default function App() {
setConnecting(true)
setError("")
try {
- const found = await listModels(baseUrl, apiKey, controller.signal)
+ const info = await listModelInfo(baseUrl, apiKey, controller.signal)
+ const found = info.map((item) => item.id)
setModels(found)
+ setImageModels(info.filter(generatesImages).map((item) => item.id))
if (found.length && !found.includes(model)) setModel(found[0])
setConnected(true)
try {
@@ -210,7 +235,9 @@ export default function App() {
} catch { setError("status.serverError") }
}
- const canSend = useMemo(() => (draft.trim() || pending.length) && model && !loading, [draft, loading, model, pending])
+ const canSend = useMemo(() => imageMode
+ ? Boolean(draft.trim() && model && !loading && seed !== undefined && validSide(imageSize.width) && validSide(imageSize.height))
+ : (draft.trim() || pending.length) && model && !loading, [draft, imageMode, imageSize, loading, model, pending, seed])
/* The shared streaming core. POSTs `payload` and appends every delta into the
message `targetId`, with the same live metrics and abort/error wiring whether
@@ -235,7 +262,7 @@ export default function App() {
baseUrl,
apiKey,
model,
- messages: payload,
+ messages: chatTurns(payload),
temperature,
maxTokens,
enableThinking: thinking,
@@ -317,6 +344,84 @@ export default function App() {
await runStream(request.history, request.targetId)
}
+ /* The image counterpart of runStream: generates into the assistant turn
+ `targetId`, whose `generated` field already holds the request. Progress and
+ previews live in imageRun, outside the transcript; the finished picture is
+ written onto the turn. Cancel is the same abortRef as the chat's Stop. */
+ const runImage = async (targetId: string, request: GeneratedImage) => {
+ setError("")
+ setLoading(true)
+ const controller = new AbortController()
+ abortRef.current = controller
+ const started = performance.now()
+ setImageRun({ targetId, started: Date.now(), progress: null, preview: null })
+ const update = (change: Partial<ImageRun>) => setImageRun((run) => run && run.targetId === targetId ? { ...run, ...change } : run)
+ try {
+ const image = await generateImage({
+ baseUrl, apiKey, model, ...request, signal: controller.signal,
+ onProgress: (progress) => update({ progress }),
+ onPartial: (preview) => update({ preview }),
+ onPlainRequest: () => update({ plain: true }),
+ })
+ const seconds = (performance.now() - started) / 1000
+ updateMessages((current) => current.map((item) => item.id === targetId
+ ? { ...item, finish: "stop", generated: { ...image, seconds } } : item))
+ setConnected(true)
+ } catch (cause) {
+ const finish = controller.signal.aborted ? "aborted" : "error"
+ if (!controller.signal.aborted) setError(cause instanceof Error ? cause.message : "status.generationFailed")
+ updateMessages((current) => setFinish(current, targetId, finish))
+ } finally {
+ abortRef.current = null
+ setImageRun(null)
+ setLoading(false)
+ }
+ }
+
+ const imageTurn = (request: GeneratedImage): ChatMessage => ({ ...message("assistant", ""), mode: "image", generated: request })
+
+ const sendImage = async () => {
+ const prompt = draft.trim()
+ if (!prompt || loading || seed === undefined) return
+ const request = { prompt, width: imageSize.width, height: imageSize.height, steps: imageSteps, seed: seed ?? randomSeed() }
+ const assistant = imageTurn(request)
+ setDraft("")
+ updateMessages((current) => [...current, { ...message("user", prompt), mode: "image" }, assistant])
+ await runImage(assistant.id, request)
+ }
+
+ /* Same prompt, size and steps, a seed never used before: a new picture
+ under the old one, so the two can be compared. */
+ const imageAgain = async (image: GeneratedImage) => {
+ if (loading) return
+ const { prompt, width, height, steps } = image
+ const assistant = imageTurn({ prompt, width, height, steps, seed: randomSeed() })
+ updateMessages((current) => [...current, assistant])
+ await runImage(assistant.id, assistant.generated!)
+ }
+
+ const imageRetry = async (item: ChatMessage) => {
+ if (loading || !item.generated) return
+ const { prompt, width, height, steps, seed: previous } = item.generated
+ const request = { prompt, width, height, steps, seed: previous }
+ updateMessages((current) => current.map((turn) => turn.id === item.id ? { ...turn, finish: undefined, generated: request } : turn))
+ await runImage(item.id, request)
+ }
+
+ const downloadImage = (image: GeneratedImage) => {
+ if (!image.url) return
+ const url = image.url.startsWith("data:") ? URL.createObjectURL(dataUrlBlob(image.url)) : image.url
+ const anchor = document.createElement("a"); anchor.href = url; anchor.download = imageFileName(image.prompt, image.seed); anchor.click()
+ if (url !== image.url) setTimeout(() => URL.revokeObjectURL(url), 1000)
+ }
+
+ const copySeed = async (item: ChatMessage) => {
+ if (!item.generated) return
+ try { await navigator.clipboard.writeText(String(item.generated.seed)); setCopied(`${item.id}:seed`) } catch { setError(t("ui.copyError")) }
+ }
+
+ const submit = () => { if (imageMode) void sendImage(); else void send() }
+
const openSettings = (page = "general") => { setSettingsPage(page); setView("settings") }
const clearChat = () => { updateMessages([]); setLastRun(null); setTokPerSec(null); setTtft(null); setTokenCount(0); setTotalTokens({ prompt: 0, completion: 0 }); setError("") }
const newChat = () => {
@@ -352,6 +457,7 @@ export default function App() {
const inferenceControls = (<fieldset disabled={loading}><section className="side-section">
<div className="section-title"><SlidersHorizontal className="size-3.5" /> {t("sidebar.inference")}</div>
<label>{t("sidebar.model")}<select id="model-select" value={model} onChange={(event) => setModel(event.target.value)}>{models.length ? models.map((id) => <option key={id}>{id}</option>) : <option>{model}</option>}</select></label>
+ {imageMode ? <p className="field-help">{t("image.modelNote")}</p> : <>
{health?.kv_slots && health.kv_slots > 1 ? <label>{t("sidebar.kvSession")}<select id="kv-slot" value={cacheSlot} onChange={(event) => setCacheSlot(Number(event.target.value))} disabled={loading}>
{Array.from({ length: kvSlots }, (_, slot) => <option key={slot} value={slot}>{t("sidebar.sessionLabel", { slot: slot + 1 })}</option>)}
</select><span className="field-help">{t("sidebar.kvSessionHelp")}</span></label> : null}
@@ -360,6 +466,7 @@ export default function App() {
<button type="button" className={cn("toggle-row", thinking && "active")} aria-pressed={thinking} onClick={() => setThinking((value) => !value)}>
<span><BrainCircuit className="size-4" /> {t("sidebar.reasoning")}</span><i><b /></i>
</button>
+ </>}
</section></fieldset>)
const runtimeControls = (<><section className="side-section runtime-section" aria-live="polite">
<div className="section-title"><Activity className="size-3.5" /> {t("sidebar.runtime")}</div>
@@ -404,9 +511,20 @@ export default function App() {
{!loading && lastRun?.usage ? <Badge><Layers className="size-3" /> {lastRun.usage.prompt_tokens}→{lastRun.usage.completion_tokens}</Badge> : null}
{!loading && lastRun?.finishReason === "length" ? <Badge className="badge-warn" title={t("topbar.truncatedHelp")}><AlertTriangle className="size-3" /> {t("topbar.truncated")}</Badge> : null}
{lastRun?.queueWaitMs != null ? <Badge><Clock className="size-3" /> queue {Math.round(lastRun.queueWaitMs)}ms</Badge> : null}
- <Badge><MonitorDot className="size-3" /> {t("topbar.slot", { n: cacheSlot + 1 })}</Badge>
+ {!imageMode && <Badge><MonitorDot className="size-3" /> {t("topbar.slot", { n: cacheSlot + 1 })}</Badge>}
</div>)
+ const imageCard = (item: ChatMessage) => {
+ const image = item.generated!
+ if (image.url) return <ImageResultCard image={image} copied={copied === `${item.id}:seed`} busy={loading || !imageMode}
+ onDownload={() => downloadImage(image)} onCopySeed={() => void copySeed(item)} onAgain={() => void imageAgain(image)} />
+ if (imageRun?.targetId === item.id) return <ImageProgressCard request={image} run={imageRun} onCancel={() => abortRef.current?.abort()} />
+ return <ImageStoppedCard request={image} finish={item.finish} busy={loading || !imageMode} onRetry={() => void imageRetry(item)} />
+ }
+ const suggestions = imageMode
+ ? [{ key: "image.prompt.photo", Icon: Camera, label: "image.suggest.photo" }, { key: "image.prompt.pixel", Icon: Grid3x3, label: "image.suggest.pixel" }, { key: "image.prompt.poster", Icon: Palette, label: "image.suggest.poster" }]
+ : [{ key: "prompts.routing", Icon: BrainCircuit, label: "ui.idea" }, { key: "prompts.benchmark", Icon: Code2, label: "ui.code" }, { key: "prompts.caching", Icon: Database, label: "ui.analyze" }]
+
return <div className="app-shell redesigned">
<aside className="rail" aria-label={t("ui.sidebar")}>
<button className="rail-brand" title="colibrì" onClick={() => setView("chat")}><Brand /></button>
@@ -446,40 +564,43 @@ export default function App() {
{item.role !== "user" && <div className="assistant-brand"><Brand /><span>colibrì</span></div>}
{item.images?.length ? <div className="message-images">{item.images.map((url, at) =>
<img key={at} src={url} alt={t("ui.attachedImage", { n: at + 1 })} />)}</div> : null}
- <div className="message-body">{item.reasoning
+ {item.role === "assistant" && item.generated ? imageCard(item) : <div className="message-body">{item.reasoning
? <details className="reasoning" open={!item.content}>
<summary>{t("sidebar.reasoning")}</summary>
<div className="reasoning-body">{item.reasoning}</div>
</details>
- : null}{item.content ? (item.role === "assistant" ? <Markdown text={item.content} /> : item.content) : <span className="typing" aria-label={t("ui.generating")}><i /><i /><i /></span>}</div>
- {item.role === "assistant" && item.content && <div className="message-actions"><button className="icon-action" aria-label={t("ui.copy")} title={t("ui.copy")} onClick={() => void copyMessage(item)}><Copy /></button>{copied === item.id && <span role="status">{t("ui.copied")}</span>}{index === messages.length - 1 && !loading && <button className="icon-action" aria-label={t("ui.regenerate")} title={t("ui.regenerate")} onClick={() => { const retry = resendFrom(messages, index); if (retry) void send(retry.text, retry.previous, retry.pictures, false) }}><RefreshCw /></button>}{index === messages.length - 1 && !loading && supportsContinuation(health) && continuable(item) && <button className="icon-action" aria-label={t("ui.continue")} title={t("ui.continue")} onClick={() => void continueTurn()}><StepForward /></button>}</div>}
+ : null}{item.content ? (item.role === "assistant" ? <Markdown text={item.content} /> : item.content) : <span className="typing" aria-label={t("ui.generating")}><i /><i /><i /></span>}</div>}
+ {item.role === "assistant" && item.content && <div className="message-actions"><button className="icon-action" aria-label={t("ui.copy")} title={t("ui.copy")} onClick={() => void copyMessage(item)}><Copy /></button>{copied === item.id && <span role="status">{t("ui.copied")}</span>}{index === messages.length - 1 && !loading && !imageMode && <button className="icon-action" aria-label={t("ui.regenerate")} title={t("ui.regenerate")} onClick={() => { const retry = resendFrom(messages, index); if (retry) void send(retry.text, retry.previous, retry.pictures, false) }}><RefreshCw /></button>}{index === messages.length - 1 && !loading && !imageMode && supportsContinuation(health) && continuable(item) && <button className="icon-action" aria-label={t("ui.continue")} title={t("ui.continue")} onClick={() => void continueTurn()}><StepForward /></button>}</div>}
</article>)}<div ref={bottomRef} /></div>
</div>}
<div className="composer-wrap">
{error && <div className="error-banner" role="alert">{t(error)}</div>}
- <form className="composer" onSubmit={e => { e.preventDefault(); void send() }}
- onDragOver={e => { if (e.dataTransfer.types.includes("Files")) e.preventDefault() }}
- onDrop={e => { if (e.dataTransfer.files.length) { e.preventDefault(); void attach(e.dataTransfer.files) } }}>
- {pending.length > 0 && <div className="composer-attachments">
+ <form className={cn("composer", imageMode && "image-mode")} onSubmit={e => { e.preventDefault(); submit() }}
+ onDragOver={e => { if (!imageMode && e.dataTransfer.types.includes("Files")) e.preventDefault() }}
+ onDrop={e => { if (!imageMode && e.dataTransfer.files.length) { e.preventDefault(); void attach(e.dataTransfer.files) } }}>
+ {!imageMode && pending.length > 0 && <div className="composer-attachments">
{pending.map((url, at) => <span key={at} className="attachment">
<img src={url} alt={t("ui.attachedImage", { n: at + 1 })} />
<button type="button" aria-label={t("ui.removeImage")} title={t("ui.removeImage")}
onClick={() => setPending(list => list.filter((_, index) => index !== at))}><X /></button>
</span>)}
</div>}
- <Textarea ref={draftRef} id="draft" aria-label={t("chat.placeholder")} value={draft} onChange={e => setDraft(e.target.value)} placeholder={t("chat.placeholder")} onPaste={event => { const files = Array.from(event.clipboardData.files || []); if (files.length) { event.preventDefault(); void attach(files) } }} onKeyDown={event => { if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) { event.preventDefault(); void send() } }} />
+ <Textarea ref={draftRef} id="draft" aria-label={imageMode ? t("image.placeholder") : t("chat.placeholder")} value={draft} onChange={e => setDraft(e.target.value)} placeholder={imageMode ? t("image.placeholder") : t("chat.placeholder")} onPaste={event => { if (imageMode) return; const files = Array.from(event.clipboardData.files || []); if (files.length) { event.preventDefault(); void attach(files) } }} onKeyDown={event => { if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) { event.preventDefault(); submit() } }} />
<div className="composer-foot">
+ {imageMode ? <ImageControls size={imageSize} custom={customSize} steps={imageSteps} seed={seedText} seedValid={seed !== undefined}
+ onSize={setImageSize} onCustom={setCustomSize} onSteps={setImageSteps} onSeed={setSeedText} /> : <>
<input ref={fileRef} type="file" accept="image/*" multiple hidden
onChange={e => { void attach(e.target.files); e.target.value = "" }} />
<button type="button" className="attach-chip" aria-label={t("ui.attachImage")} title={t("ui.attachImage")}
onClick={() => fileRef.current?.click()}><ImagePlus /></button>
<button type="button" className="reasoning-chip" aria-pressed={thinking} onClick={() => setThinking(value => !value)}><BrainCircuit />{t("sidebar.reasoning")}</button>
<button type="button" className="slot-chip" onClick={() => openSettings("model")}><Database />{t("topbar.slot", { n: cacheSlot + 1 })}</button>
- {loading ? <button type="button" className="send-button" aria-label={t("chat.stop")} onClick={() => abortRef.current?.abort()}><CircleStop /></button> : <button type="submit" className="send-button" aria-label={t("chat.send")} disabled={!canSend}><ArrowUp /></button>}
+ </>}
+ {loading ? <button type="button" className="send-button" aria-label={imageMode ? t("image.cancel") : t("chat.stop")} onClick={() => abortRef.current?.abort()}><CircleStop /></button> : <button type="submit" className="send-button" aria-label={imageMode ? t("image.generate") : t("chat.send")} disabled={!canSend}><ArrowUp /></button>}
</div>
</form>
- {empty && <div className="suggestions">{[{ key: "routing", Icon: BrainCircuit, label: "idea" }, { key: "benchmark", Icon: Code2, label: "code" }, { key: "caching", Icon: Database, label: "analyze" }].map(({ key, Icon, label }) => <button key={key} onClick={() => { setDraft(t(`prompts.${key}`)); draftRef.current?.focus() }}><Icon />{t(`ui.${label}`)}</button>)}</div>}
- {!empty && lastRun?.finishReason === "length" && <p className="truncation-note">{t("topbar.truncatedHelp")}</p>}
+ {empty && <div className="suggestions">{suggestions.map(({ key, Icon, label }) => <button key={key} onClick={() => { setDraft(t(key)); draftRef.current?.focus() }}><Icon />{t(label)}</button>)}</div>}
+ {!empty && !imageMode && lastRun?.finishReason === "length" && <p className="truncation-note">{t("topbar.truncatedHelp")}</p>}
</div>
</section>}
<section className="brio-workspace" hidden={view !== "brio"}>
diff --git a/web/src/chat-design.css b/web/src/chat-design.css
index a64bd0d4..5bac8a2d 100644
--- a/web/src/chat-design.css
+++ b/web/src/chat-design.css
@@ -148,3 +148,69 @@ button{color:inherit}button:disabled{cursor:default;opacity:.4}svg.lucide{stroke
.brio-add,.brio-run{flex:1 1 100%;justify-content:center}
.brio-card textarea,.brio-card input{font-size:16px}
}
+
+/* ---- text-to-image -------------------------------------------------------
+ An image model's turn is a picture in a frame the size of the request, with
+ the stage and a thin bar under it while it runs, then the parameters and
+ three actions. The composer trades the chat's chips for size, steps and
+ seed chips of the same shape. */
+.image-turn{display:grid;gap:10px;min-width:0;max-width:100%}
+.image-frame{position:relative;width:100%;overflow:hidden;border-radius:16px;background:var(--input)}
+.image-frame>img{position:absolute;inset:0;display:block;width:100%;height:100%;object-fit:cover}
+.image-frame.pending{border:1px solid color-mix(in srgb,var(--foreground) 8%,transparent)}
+/* A preview is a few dozen pixels of an unfinished latent: blown up, pixelated
+ and blurred it reads as "taking shape", not as a broken picture. The scale
+ hides the soft edge the blur leaves at the border. */
+.image-frame>img.image-preview{image-rendering:pixelated;filter:blur(7px) saturate(1.15);transform:scale(1.08);transition:opacity .3s}
+.image-shimmer{position:absolute;inset:0;background:linear-gradient(100deg,transparent 30%,color-mix(in srgb,var(--foreground) 7%,transparent) 50%,transparent 70%);background-size:220% 100%;animation:image-shimmer 1.8s linear infinite}
+@keyframes image-shimmer{from{background-position:130% 0}to{background-position:-130% 0}}
+.image-status{display:flex;align-items:center;gap:10px;min-width:0;font-size:12px;color:var(--muted-foreground)}
+.image-stage{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--foreground)}
+.image-clock{font-variant-numeric:tabular-nums}
+.image-cancel{display:inline-flex;align-items:center;gap:5px;flex-shrink:0;border:1px solid color-mix(in srgb,var(--foreground) 18%,transparent);border-radius:20px;background:none;color:var(--muted-foreground);font-size:11px;padding:5px 10px}
+.image-cancel svg{width:13px;height:13px}
+.image-cancel:hover{color:var(--foreground);background:var(--secondary)}
+.image-bar{height:4px;border-radius:4px;background:var(--secondary);overflow:hidden}
+.image-bar>span{display:block;height:100%;border-radius:4px;background:var(--primary);transition:width .45s ease}
+.image-bar.waiting>span{width:28%!important;animation:image-wait 1.5s ease-in-out infinite}
+@keyframes image-wait{from{transform:translateX(-110%)}to{transform:translateX(370%)}}
+.image-meta{display:flex;flex-wrap:wrap;gap:3px 14px;margin:0;font-size:11px;color:var(--muted-foreground);font-variant-numeric:tabular-nums}
+.image-meta>span[title]{text-decoration:underline dotted color-mix(in srgb,var(--foreground) 30%,transparent);text-underline-offset:3px;cursor:help}
+.image-actions{display:flex;flex-wrap:wrap;align-items:center;gap:6px}
+.image-actions>span{font-size:10px;color:var(--muted-foreground)}
+.image-action{display:inline-flex;align-items:center;gap:6px;border:1px solid color-mix(in srgb,var(--foreground) 18%,transparent);border-radius:20px;background:none;color:var(--muted-foreground);font-size:11px;padding:6px 11px}
+.image-action svg{width:14px;height:14px;flex-shrink:0}
+.image-action:hover:not(:disabled){color:var(--foreground);background:var(--secondary)}
+.image-action.primary{color:var(--foreground)}
+.image-stopped{display:flex;flex-wrap:wrap;align-items:center;gap:8px 12px;padding:11px 12px 11px 15px;border:1px dashed var(--border);border-radius:14px;font-size:12px;color:var(--muted-foreground)}
+.image-stopped>span{color:var(--foreground)}
+.image-stopped>button{margin-left:auto}
+.composer.image-mode .composer-foot{align-items:flex-end}
+.image-controls{display:flex;flex-wrap:wrap;align-items:center;gap:6px;flex:1;min-width:0}
+.image-chip{position:relative;display:inline-flex;align-items:center;gap:6px;height:31px;min-width:0;border:1px solid color-mix(in srgb,var(--foreground) 18%,transparent);border-radius:20px;padding:0 10px;font-size:11px;color:var(--muted-foreground)}
+.image-chip>svg{width:14px;height:14px;flex-shrink:0}
+.image-chip:focus-within{border-color:color-mix(in srgb,var(--foreground) 40%,transparent);color:var(--foreground)}
+.image-chip select,.image-chip input{min-width:0;padding:0;border:0;outline:none;background:none;color:var(--foreground);font:inherit;font-variant-numeric:tabular-nums}
+.image-chip input::placeholder{color:var(--muted-foreground)}
+.image-chip select{appearance:none;padding-right:15px;margin-right:-15px;cursor:pointer}
+.image-chip option,.image-chip optgroup{background:var(--card);color:var(--foreground)}
+.image-chip>.image-caret{width:12px;height:12px;pointer-events:none}
+.image-steps{width:2.6ch;text-align:right}
+.image-seed{width:10ch}
+.image-custom{gap:4px}
+.image-custom input{width:4.4ch;text-align:center}
+.image-chip.invalid,.image-custom input.invalid{border-color:color-mix(in srgb,var(--destructive,#ff766f) 60%,transparent);color:var(--destructive,#ff766f)}
+/* Where the browser can, each field is as wide as what it shows, so the size
+ chip is not stretched to fit "Custom size" while it says 768x512. */
+@supports (field-sizing:content){.image-chip select,.image-chip input{field-sizing:content;width:auto}.image-steps{min-width:1ch}.image-seed{min-width:4ch}.image-custom input{min-width:3ch}}
+.image-clear{display:grid;place-items:center;width:18px;height:18px;margin-right:-5px;padding:0;border:0;border-radius:50%;background:none;color:var(--muted-foreground)}
+.image-clear:hover{background:var(--secondary);color:var(--foreground)}
+.image-clear svg{width:11px;height:11px}
+@media(max-width:760px){
+ /* 16px keeps a phone from zooming into the field it focuses */
+ .image-chip select,.image-chip input{font-size:16px}
+ .image-chip{height:34px;padding:0 9px}
+ .image-seed{width:7.5ch}
+ .image-actions>.image-action:not(.primary)>span{display:none}
+ .image-actions>.image-action:not(.primary){padding:6px 9px}
+}
diff --git a/web/src/components/ImageTurn.tsx b/web/src/components/ImageTurn.tsx
new file mode 100644
index 00000000..f3b1fd9d
--- /dev/null
+++ b/web/src/components/ImageTurn.tsx
@@ -0,0 +1,192 @@
+import { useEffect, useState, type ReactNode } from "react"
+import { ChevronDown, Copy, Dices, Download, Footprints, Proportions, RefreshCw, RotateCcw, X } from "lucide-react"
+
+import type { GeneratedImage, ImageProgress } from "@/lib/api"
+import {
+ MAX_STEPS, SIZE_PRESETS, clampSteps, duration, elapsedClock, presetOf, progressFraction,
+ sizeKey, snapSide, validSide, type ImageSize,
+} from "@/lib/images"
+import { cn } from "@/lib/utils"
+import { useLocale } from "../i18n"
+
+/* The state of the one picture being generated. Kept out of the transcript:
+ it changes on every step, and only the finished picture belongs there. */
+export interface ImageRun {
+ targetId: string
+ started: number
+ progress: ImageProgress | null
+ preview: string | null
+ /* No stream: one plain request, so no stage will ever be reported. */
+ plain?: boolean
+}
+
+/* The room a picture takes, sized from the request before a single pixel
+ exists, so the placeholder, the previews and the result sit in the same
+ place and the conversation does not jump when the image arrives. Never wider
+ than the column or the picture itself, never taller than most of the screen. */
+const turnWidth = (width: number, height: number) => ({
+ width: `min(100%, ${width}px, calc(62dvh * ${(width / height).toFixed(4)}))`,
+})
+
+function Frame({ width, height, className, children }: { width: number; height: number; className?: string; children: ReactNode }) {
+ return <div className={cn("image-frame", className)} style={{ aspectRatio: `${width} / ${height}` }}>{children}</div>
+}
+
+function Elapsed({ since }: { since: number }) {
+ const [now, setNow] = useState(() => Date.now())
+ useEffect(() => {
+ const timer = window.setInterval(() => setNow(Date.now()), 250)
+ return () => window.clearInterval(timer)
+ }, [])
+ return <time className="image-clock">{elapsedClock((now - since) / 1000)}</time>
+}
+
+export function ImageProgressCard({ request, run, onCancel }: { request: GeneratedImage; run: ImageRun; onCancel: () => void }) {
+ const { t } = useLocale()
+ const progress = run.progress
+ const fraction = progressFraction(progress)
+ const stage = !progress ? t(run.plain ? "image.stage.working" : "image.stage.queued")
+ : progress.stage === "denoise" ? t("image.stage.denoise", { step: Math.max(1, progress.step), steps: progress.steps })
+ : progress.stage === "encode" || progress.stage === "decode" ? t(`image.stage.${progress.stage}`)
+ : t("image.stage.working")
+ return <div className="image-turn" style={turnWidth(request.width, request.height)}>
+ <Frame width={request.width} height={request.height} className="pending">
+ {run.preview ? <img className="image-preview" src={run.preview} alt={t("image.previewAlt")} /> : <span className="image-shimmer" />}
+ </Frame>
+ <div className="image-status">
+ <span className="image-stage" aria-live="polite">{stage}</span>
+ <Elapsed since={run.started} />
+ <button type="button" className="image-cancel" onClick={onCancel}><X />{t("image.cancel")}</button>
+ </div>
+ <div className={cn("image-bar", !progress && "waiting")} role="progressbar" aria-label={t("image.progress")}
+ aria-valuemin={0} aria-valuemax={100} aria-valuenow={progress ? Math.round(fraction * 100) : undefined}>
+ <span style={{ width: `${(fraction * 100).toFixed(1)}%` }} />
+ </div>
+ </div>
+}
+
+export function ImageResultCard({ image, copied, busy, onDownload, onCopySeed, onAgain }: {
+ image: GeneratedImage
+ copied: boolean
+ busy: boolean
+ onDownload: () => void
+ onCopySeed: () => void
+ onAgain: () => void
+}) {
+ const { t, locale } = useLocale()
+ const timings = image.timings
+ const breakdown = timings && [timings.encode, timings.denoise, timings.decode].every((value) => typeof value === "number")
+ ? t("image.timings", {
+ encode: duration(timings.encode!, locale), denoise: duration(timings.denoise!, locale), decode: duration(timings.decode!, locale),
+ })
+ : undefined
+ return <div className="image-turn" style={turnWidth(image.width, image.height)}>
+ <Frame width={image.width} height={image.height}>
+ <img src={image.url} alt={t("image.alt", { prompt: image.prompt })} />
+ </Frame>
+ <p className="image-meta">
+ <span>{image.width} × {image.height}</span>
+ <span>{t("image.meta.steps", { n: image.steps })}</span>
+ <span>{t("image.meta.seed", { seed: image.seed })}</span>
+ {image.seconds != null && <span title={breakdown}>{duration(image.seconds, locale)}</span>}
+ </p>
+ <div className="image-actions">
+ {/* aria-label as well as the text: on a phone the first two show only their icon */}
+ <button type="button" className="image-action" onClick={onDownload} aria-label={t("image.download")} title={t("image.download")}><Download /><span>{t("image.download")}</span></button>
+ <button type="button" className="image-action" onClick={onCopySeed} aria-label={t("image.copySeed")} title={t("image.copySeed")}><Copy /><span>{t("image.copySeed")}</span></button>
+ <button type="button" className="image-action primary" onClick={onAgain} disabled={busy} aria-label={t("image.again")} title={t("image.again")}><RefreshCw /><span>{t("image.againShort")}</span></button>
+ {copied && <span role="status">{t("image.seedCopied")}</span>}
+ </div>
+ </div>
+}
+
+export function ImageStoppedCard({ request, finish, busy, onRetry }: { request: GeneratedImage; finish?: string; busy: boolean; onRetry: () => void }) {
+ const { t } = useLocale()
+ return <div className="image-turn">
+ <div className="image-stopped">
+ <span>{finish === "aborted" ? t("image.cancelled") : t("image.failed")}</span>
+ <small>{request.width} × {request.height} · {t("image.meta.steps", { n: request.steps })}</small>
+ <button type="button" className="image-action" onClick={onRetry} disabled={busy}><RotateCcw /><span>{t("image.retry")}</span></button>
+ </div>
+ </div>
+}
+
+/* The presets grouped by shape: the closed select shows only the pixels,
+ the open list says which ratio each one is. */
+const RATIOS = [...new Set(SIZE_PRESETS.map((preset) => preset.ratio))]
+
+/* Size, steps and seed, in the composer where the chat's reasoning and slot
+ chips sit for a text model. A custom side is typed freely and snapped to a
+ multiple of 32 when the field is left; a valid value counts right away, so
+ sending straight from the field uses what is on screen. */
+export function ImageControls({ size, custom, steps, seed, seedValid, onSize, onCustom, onSteps, onSeed }: {
+ size: ImageSize
+ custom: boolean
+ steps: number
+ seed: string
+ seedValid: boolean
+ onSize: (size: ImageSize) => void
+ onCustom: (custom: boolean) => void
+ onSteps: (steps: number) => void
+ onSeed: (seed: string) => void
+}) {
+ const { t } = useLocale()
+ const [width, setWidth] = useState(String(size.width))
+ const [height, setHeight] = useState(String(size.height))
+ const [stepsText, setStepsText] = useState(String(steps))
+ useEffect(() => { setWidth(String(size.width)); setHeight(String(size.height)) }, [size.width, size.height])
+ useEffect(() => { setStepsText(String(steps)) }, [steps])
+
+ const side = (axis: "width" | "height", text: string, commit: boolean) => {
+ const value = Number(text)
+ const next = commit ? snapSide(value) : value
+ if (commit) (axis === "width" ? setWidth : setHeight)(String(next))
+ if (commit || validSide(value)) onSize({ ...size, [axis]: next })
+ }
+ const selected = custom || !presetOf(size) ? "custom" : sizeKey(size)
+ return <div className="image-controls">
+ <label className="image-chip" title={t("image.size")}>
+ <Proportions aria-hidden="true" />
+ <select aria-label={t("image.size")} value={selected} onChange={(event) => {
+ if (event.target.value === "custom") { onCustom(true); return }
+ const preset = SIZE_PRESETS.find((item) => sizeKey(item) === event.target.value)
+ if (preset) { onCustom(false); onSize({ width: preset.width, height: preset.height }) }
+ }}>
+ {RATIOS.map((ratio) => <optgroup key={ratio} label={ratio}>
+ {SIZE_PRESETS.filter((preset) => preset.ratio === ratio).map((preset) =>
+ <option key={sizeKey(preset)} value={sizeKey(preset)}>{preset.width}×{preset.height}</option>)}
+ </optgroup>)}
+ <option value="custom">{t("image.sizeCustom")}</option>
+ </select>
+ <ChevronDown className="image-caret" aria-hidden="true" />
+ </label>
+ {selected === "custom" && <span className="image-chip image-custom" title={t("image.sizeHelp")}>
+ <input aria-label={t("image.width")} inputMode="numeric" value={width}
+ className={cn(!validSide(Number(width)) && "invalid")}
+ onChange={(event) => { setWidth(event.target.value); side("width", event.target.value, false) }}
+ onBlur={(event) => side("width", event.target.value, true)} />
+ <span aria-hidden="true">×</span>
+ <input aria-label={t("image.height")} inputMode="numeric" value={height}
+ className={cn(!validSide(Number(height)) && "invalid")}
+ onChange={(event) => { setHeight(event.target.value); side("height", event.target.value, false) }}
+ onBlur={(event) => side("height", event.target.value, true)} />
+ </span>}
+ <label className="image-chip" title={t("image.steps")}>
+ <Footprints aria-hidden="true" />
+ <input aria-label={t("image.steps")} inputMode="numeric" className="image-steps" value={stepsText}
+ onChange={(event) => {
+ setStepsText(event.target.value)
+ const value = Number(event.target.value)
+ if (event.target.value && Number.isInteger(value) && value >= 1 && value <= MAX_STEPS) onSteps(value)
+ }}
+ onBlur={(event) => { const value = clampSteps(Number(event.target.value || steps)); setStepsText(String(value)); onSteps(value) }} />
+ <span className="image-unit">{t("image.stepsUnit")}</span>
+ </label>
+ <label className={cn("image-chip", !seedValid && "invalid")} title={t("image.seedHelp")}>
+ <Dices aria-hidden="true" />
+ <input aria-label={t("image.seed")} aria-invalid={!seedValid} inputMode="numeric" className="image-seed" value={seed}
+ placeholder={t("image.seedRandom")} onChange={(event) => onSeed(event.target.value)} />
+ {seed && <button type="button" className="image-clear" aria-label={t("image.seedClear")} title={t("image.seedClear")} onClick={() => onSeed("")}><X /></button>}
+ </label>
+ </div>
+}
diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.ts
index 7c1621ff..c365c0cf 100644
--- a/web/src/i18n/en.ts
+++ b/web/src/i18n/en.ts
@@ -229,6 +229,48 @@ const en: Record<string, string> = {
"profile.oneTurn": "1 turn",
"profile.diskNote": "Disk service is time spent reading experts on I/O threads; it overlaps with compute, so only the I/O wait the compute thread felt counts inside the wall-time stack. With multiple KV sessions the shares describe the whole engine over the turn's window.",
+ // text-to-image (a model with the image_generation capability)
+ "image.placeholder": "Describe the image to generate…",
+ "image.generate": "Generate image",
+ "image.size": "Image size",
+ "image.sizeCustom": "Custom size",
+ "image.width": "Width",
+ "image.height": "Height",
+ "image.sizeHelp": "Width and height: multiples of 32, from 256 to 2048",
+ "image.steps": "Denoising steps",
+ "image.stepsUnit": "steps",
+ "image.seed": "Seed",
+ "image.seedRandom": "random",
+ "image.seedHelp": "Seed: leave it empty for a new random seed on every image",
+ "image.seedClear": "Use a random seed",
+ "image.progress": "Image generation progress",
+ "image.stage.queued": "Waiting for the engine",
+ "image.stage.encode": "Encoding the prompt",
+ "image.stage.denoise": "Denoising, step {{step}} of {{steps}}",
+ "image.stage.decode": "Decoding the image",
+ "image.stage.working": "Generating the image",
+ "image.cancel": "Cancel",
+ "image.cancelled": "Generation cancelled",
+ "image.failed": "Generation failed",
+ "image.retry": "Try again",
+ "image.download": "Download PNG",
+ "image.copySeed": "Copy seed",
+ "image.seedCopied": "Seed copied",
+ "image.again": "Again with a new seed",
+ "image.againShort": "New seed",
+ "image.alt": "Generated image: {{prompt}}",
+ "image.previewAlt": "Preview of the image being generated",
+ "image.meta.steps": "{{n}} steps",
+ "image.meta.seed": "seed {{seed}}",
+ "image.timings": "encoding {{encode}}, denoising {{denoise}}, decoding {{decode}}",
+ "image.modelNote": "This model generates images. Size, steps and seed are chosen under the prompt; temperature, output tokens and reasoning do not apply to it.",
+ "image.suggest.photo": "A photo",
+ "image.suggest.pixel": "Pixel art",
+ "image.suggest.poster": "A poster",
+ "image.prompt.photo": "A hummingbird drinking from a red hibiscus flower, macro photo, soft morning light, shallow depth of field",
+ "image.prompt.pixel": "An isometric pixel-art village at dusk, warm lit windows, small canals and wooden bridges",
+ "image.prompt.poster": "A minimalist travel poster of the Dolomites at sunrise, flat colors, the title \"DOLOMITI\" in bold letters",
+
// error boundary
"error.title": "colibrì UI hit an error",
"error.hint": "The engine is unaffected. Try refreshing.",
diff --git a/web/src/i18n/it.ts b/web/src/i18n/it.ts
index 30180763..de0a359e 100644
--- a/web/src/i18n/it.ts
+++ b/web/src/i18n/it.ts
@@ -218,6 +218,47 @@ const it: Record<string, string> = {
"profile.oneTurn": "1 turno",
"profile.diskNote": "Il servizio disco è il tempo speso a leggere gli expert sui thread I/O; si sovrappone al calcolo, quindi solo l'attesa I/O effettivamente percepita dal thread di calcolo conta nella ripartizione del tempo totale. Con più sessioni KV, le quote descrivono l'intero motore nella finestra del turno.",
+ "image.placeholder": "Descrivi l'immagine da generare…",
+ "image.generate": "Genera l'immagine",
+ "image.size": "Dimensioni dell'immagine",
+ "image.sizeCustom": "Su misura",
+ "image.width": "Larghezza",
+ "image.height": "Altezza",
+ "image.sizeHelp": "Larghezza e altezza: multipli di 32, da 256 a 2048",
+ "image.steps": "Passi di denoising",
+ "image.stepsUnit": "passi",
+ "image.seed": "Seed",
+ "image.seedRandom": "casuale",
+ "image.seedHelp": "Seed: lascialo vuoto per averne uno nuovo, casuale, a ogni immagine",
+ "image.seedClear": "Usa un seed casuale",
+ "image.progress": "Avanzamento della generazione",
+ "image.stage.queued": "In attesa del motore",
+ "image.stage.encode": "Lettura del prompt",
+ "image.stage.denoise": "Denoising, passo {{step}} di {{steps}}",
+ "image.stage.decode": "Decodifica dell'immagine",
+ "image.stage.working": "Sto generando l'immagine",
+ "image.cancel": "Annulla",
+ "image.cancelled": "Generazione annullata",
+ "image.failed": "Generazione non riuscita",
+ "image.retry": "Riprova",
+ "image.download": "Scarica il PNG",
+ "image.copySeed": "Copia il seed",
+ "image.seedCopied": "Seed copiato",
+ "image.again": "Rifalla con un altro seed",
+ "image.againShort": "Altro seed",
+ "image.alt": "Immagine generata: {{prompt}}",
+ "image.previewAlt": "Anteprima dell'immagine in corso",
+ "image.meta.steps": "{{n}} passi",
+ "image.meta.seed": "seed {{seed}}",
+ "image.timings": "lettura del prompt {{encode}}, denoising {{denoise}}, decodifica {{decode}}",
+ "image.modelNote": "Questo modello genera immagini. Dimensioni, passi e seed si scelgono sotto il prompt; temperatura, token di output e ragionamento qui non contano.",
+ "image.suggest.photo": "Una foto",
+ "image.suggest.pixel": "Pixel art",
+ "image.suggest.poster": "Un manifesto",
+ "image.prompt.photo": "Un colibrì che beve da un ibisco rosso, foto macro, luce morbida del mattino, sfondo sfocato",
+ "image.prompt.pixel": "Un borgo isometrico in pixel art al tramonto, finestre accese, piccoli canali e ponti di legno",
+ "image.prompt.poster": "Un manifesto turistico minimalista delle Dolomiti all'alba, colori piatti, il titolo \"DOLOMITI\" a lettere grandi",
+
"error.title": "L'interfaccia colibrì ha riscontrato un errore",
"error.hint": "Il motore non è stato coinvolto. Prova a ricaricare la pagina.",
"error.retry": "Riprova",
diff --git a/web/src/lib/api.test.ts b/web/src/lib/api.test.ts
index a5348cda..95b81d07 100644
--- a/web/src/lib/api.test.ts
+++ b/web/src/lib/api.test.ts
@@ -304,7 +304,9 @@ describe("generateImage", () => {
.mockResolvedValueOnce(streamed(""))
.mockResolvedValueOnce(json(answer))
vi.stubGlobal("fetch", fetchMock)
- await expect(generateImage(options())).resolves.toMatchObject({ url: "data:image/png;base64,iVBORw0KGgo=" })
+ const plain = vi.fn()
+ await expect(generateImage(options({ onPlainRequest: plain }))).resolves.toMatchObject({ url: "data:image/png;base64,iVBORw0KGgo=" })
+ expect(plain).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledTimes(2)
expect(bodyOf(fetchMock.mock.calls[1])).toMatchObject({ stream: false, seed: 1234 })
})
diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts
index 0e6f4199..754afd7b 100644
--- a/web/src/lib/api.ts
+++ b/web/src/lib/api.ts
@@ -377,6 +377,9 @@ export interface GenerateImageOptions {
onProgress?: (progress: ImageProgress) => void
/* A preview as a data URI, small and noisy: the latent of the step so far. */
onPartial?: (url: string, index: number) => void
+ /* Streaming was not available and the picture is being made by one plain
+ request: nothing more will arrive until it is done. */
+ onPlainRequest?: () => void
}
/* The stream did not work as a stream (no body, or a close before the first
@@ -493,6 +496,7 @@ export async function generateImage(options: GenerateImageOptions): Promise<Gene
answer = await streamImage(options)
} catch (cause) {
if (!(cause instanceof StreamUnavailable) || options.signal.aborted) throw cause
+ options.onPlainRequest?.()
answer = await requestImage(options)
}
const picture = answer.data?.[0]