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

fix(web): regenerate the last turn with its pictures

kevin93274653c349cbbb
diff --git a/web/src/App.tsx b/web/src/App.tsx
index 30fee3fa..309e2375 100644
--- a/web/src/App.tsx
+++ b/web/src/App.tsx
@@ -30,6 +30,7 @@ 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 { resendFrom } from "@/lib/chat"
import { activeRequests, supportsCacheSlots } from "@/lib/runtime"
import Brio from "./Brio"
import { BrainWorkspace } from "./BrainWorkspace"
@@ -209,14 +210,17 @@ export default function App() {
const canSend = useMemo(() => (draft.trim() || pending.length) && model && !loading, [draft, loading, model, pending])
- const send = async (text = draft, previous = messages, pictures = pending) => {
+ /* consumeDraft is false for regenerate so a follow-up already in the composer is not eaten. */
+ const send = async (text = draft, previous = messages, pictures = pending, consumeDraft = true) => {
const content = text.trim()
if ((!content && !pictures.length) || loading) return
const user = message("user", content, pictures)
const assistant = message("assistant", "")
const history = [...previous, user]
- setDraft("")
- setPending([])
+ if (consumeDraft) {
+ setDraft("")
+ setPending([])
+ }
setError("")
updateMessages([...history, assistant])
setLoading(true)
@@ -427,7 +431,7 @@ export default function App() {
<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 userIndex = messages.map((m, i) => m.role === "user" && i < index ? i : -1).reduce((a, b) => Math.max(a, b), -1); if (userIndex >= 0) void send(messages[userIndex].content, messages.slice(0, userIndex)) }}><RefreshCw /></button>}</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>}</div>}
</article>)}<div ref={bottomRef} /></div>
</div>}
<div className="composer-wrap">
diff --git a/web/src/lib/chat.test.ts b/web/src/lib/chat.test.ts
new file mode 100644
index 00000000..00adbae7
--- /dev/null
+++ b/web/src/lib/chat.test.ts
@@ -0,0 +1,115 @@
+import { afterEach, describe, expect, it, vi } from "vitest"
+
+import { streamChat, type ChatMessage } from "./api"
+import { resendFrom } from "./chat"
+
+afterEach(() => vi.unstubAllGlobals())
+
+const user = (content: string, images?: string[]): ChatMessage =>
+ images?.length
+ ? { id: "u", role: "user", content, images }
+ : { id: "u", role: "user", content }
+
+const assistant = (content: string): ChatMessage => ({ id: "a", role: "assistant", content })
+
+describe("resendFrom", () => {
+ it("keeps the pictures that arrived with the last user turn", () => {
+ const photo = "data:image/png;base64,aaa"
+ const messages = [user("what is this?", [photo]), assistant("a cat")]
+ expect(resendFrom(messages, 1)).toEqual({
+ text: "what is this?",
+ previous: [],
+ pictures: [photo],
+ })
+ })
+
+ it("lets an image-only turn be regenerated", () => {
+ const photo = "data:image/jpeg;base64,bbb"
+ const messages = [user("", [photo]), assistant("a diagram")]
+ const retry = resendFrom(messages, 1)
+ expect(retry).not.toBeNull()
+ expect(retry!.text).toBe("")
+ expect(retry!.pictures).toEqual([photo])
+ })
+
+ it("uses the nearest user turn and leaves earlier history alone", () => {
+ const first: ChatMessage = { id: "u0", role: "user", content: "hi" }
+ const reply: ChatMessage = { id: "a0", role: "assistant", content: "hello" }
+ const photo = "data:image/png;base64,ccc"
+ const second: ChatMessage = { id: "u1", role: "user", content: "and this?", images: [photo] }
+ const last: ChatMessage = { id: "a1", role: "assistant", content: "a bird" }
+ const messages = [first, reply, second, last]
+ expect(resendFrom(messages, 3)).toEqual({
+ text: "and this?",
+ previous: [first, reply],
+ pictures: [photo],
+ })
+ })
+
+ it("returns null when there is no user turn to resend", () => {
+ expect(resendFrom([assistant("orphan")], 0)).toBeNull()
+ })
+
+ it("leaves a text-only turn without a pictures field on the wire", () => {
+ expect(resendFrom([user("hello"), assistant("hi")], 1)).toEqual({
+ text: "hello",
+ previous: [],
+ pictures: [],
+ })
+ })
+})
+
+describe("regenerate request body", () => {
+ it("puts those pictures on the wire as image_url parts", async () => {
+ const photo = "data:image/png;base64,ddd"
+ const retry = resendFrom([user("describe this", [photo]), assistant("skipped")], 1)
+ const fetchMock = vi.fn().mockResolvedValue(new Response("data: [DONE]\n\n", {
+ headers: { "content-type": "text/event-stream" },
+ }))
+ vi.stubGlobal("fetch", fetchMock)
+ await streamChat({
+ baseUrl: "http://localhost:8000/v1",
+ apiKey: "",
+ model: "test-model",
+ messages: [{ id: "u", role: "user", content: retry!.text, images: retry!.pictures }],
+ temperature: 0,
+ maxTokens: 8,
+ enableThinking: false,
+ signal: new AbortController().signal,
+ onDelta: () => undefined,
+ })
+ const body = JSON.parse(fetchMock.mock.calls[0][1].body as string) as {
+ messages: Array<{ content: unknown }>
+ }
+ expect(body.messages[0].content).toEqual([
+ { type: "text", text: "describe this" },
+ { type: "image_url", image_url: { url: photo } },
+ ])
+ })
+
+ it("sends an image-only regenerate as image parts with no empty text", async () => {
+ const photo = "data:image/png;base64,eee"
+ const retry = resendFrom([user("", [photo]), assistant("skipped")], 1)
+ const fetchMock = vi.fn().mockResolvedValue(new Response("data: [DONE]\n\n", {
+ headers: { "content-type": "text/event-stream" },
+ }))
+ vi.stubGlobal("fetch", fetchMock)
+ await streamChat({
+ baseUrl: "http://localhost:8000/v1",
+ apiKey: "",
+ model: "test-model",
+ messages: [{ id: "u", role: "user", content: retry!.text, images: retry!.pictures }],
+ temperature: 0,
+ maxTokens: 8,
+ enableThinking: false,
+ signal: new AbortController().signal,
+ onDelta: () => undefined,
+ })
+ const body = JSON.parse(fetchMock.mock.calls[0][1].body as string) as {
+ messages: Array<{ content: unknown }>
+ }
+ expect(body.messages[0].content).toEqual([
+ { type: "image_url", image_url: { url: photo } },
+ ])
+ })
+})
diff --git a/web/src/lib/chat.ts b/web/src/lib/chat.ts
new file mode 100644
index 00000000..706c536e
--- /dev/null
+++ b/web/src/lib/chat.ts
@@ -0,0 +1,24 @@
+import type { ChatMessage } from "./api"
+
+export interface ResendTurn {
+ text: string
+ previous: ChatMessage[]
+ pictures: string[]
+}
+
+/* Rebuild the last user turn so regenerate can send it again. The pictures
+ live on that message, not in the composer: a follow-up without them would
+ get an answer about nothing, and an image-only turn would not send at all. */
+export function resendFrom(messages: ChatMessage[], assistantIndex: number): ResendTurn | null {
+ let userIndex = -1
+ for (let i = 0; i < assistantIndex; i++) {
+ if (messages[i].role === "user") userIndex = i
+ }
+ if (userIndex < 0) return null
+ const user = messages[userIndex]
+ return {
+ text: user.content,
+ previous: messages.slice(0, userIndex),
+ pictures: user.images ? [...user.images] : [],
+ }
+}