Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(chat): keep attached images when the composer unmounts

rubenluetke10-beep0bb0b35bd843
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/AgentComposer.tsx b/jarvis/ui/web/frontend/src/components/agentchat/AgentComposer.tsx
index e7f41141db..d7358ef1d7 100644
--- a/jarvis/ui/web/frontend/src/components/agentchat/AgentComposer.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentchat/AgentComposer.tsx
@@ -204,9 +204,9 @@ export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
stopAndSend: stopDictationAndSend,
} = useComposerDictation(setValue, () => void onSend());
- // Files going in with this message. Held here rather than in the store: they
- // belong to the sentence being typed, and a chat opened elsewhere must not
- // inherit them.
+ // Files going in with this message. Kept per chat store and session, like
+ // the typed text: they belong to the sentence being typed, a chat opened
+ // elsewhere must not inherit them, and a section switch must not drop them.
const [attachError, setAttachError] = useState("");
const onAttachProblem = useCallback(
(message: string, severity: "warning" | "error") => {
@@ -222,6 +222,7 @@ export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
surface,
},
onAttachProblem,
+ { owner: store, key: activeSessionId ?? "" },
);
// A shortcut appshot parked for "the next message" joins this one.
useAppshotClaim(files.attachFiles, surface === "jarvis", activeSessionId ?? "new");
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/chatAttachments.test.tsx b/jarvis/ui/web/frontend/src/components/agentchat/chatAttachments.test.tsx
index 9d01eb8705..adad68b998 100644
--- a/jarvis/ui/web/frontend/src/components/agentchat/chatAttachments.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentchat/chatAttachments.test.tsx
@@ -4,6 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { AgentComposer } from "@/components/agentchat/AgentComposer";
import { AgentChatStoreProvider } from "@/components/agentchat/AgentChatStoreContext";
import { EMPTY_TIMELINE, reduceEvent } from "@/components/agentchat/reduce";
+import { forgetHeldFiles } from "@/components/agentchat/useChatAttachments";
import { useFileDropGuard } from "@/hooks/useFileDropGuard";
import { useAgentChatStore } from "@/store/agentChat";
import { useEventStore } from "@/store/events";
@@ -122,6 +123,8 @@ describe("chat composer attachments", () => {
afterEach(() => {
cleanup();
+ // Held files outlive the composer; a test's picture must not leak into the next.
+ forgetHeldFiles(useAgentChatStore);
vi.unstubAllGlobals();
vi.restoreAllMocks();
});
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.draft.test.tsx b/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.draft.test.tsx
new file mode 100644
index 0000000000..196c96607a
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.draft.test.tsx
@@ -0,0 +1,89 @@
+import { act, cleanup, renderHook, waitFor } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { forgetHeldFiles, useChatAttachments } from "@/components/agentchat/useChatAttachments";
+
+/**
+ * Files held for an unsent message survive the composer unmounting, the way
+ * its typed text does. Switching app sections unmounts the composer; the
+ * reported bug was the text coming back without its pictures.
+ */
+
+const SHOT = {
+ name: "shot.png",
+ reference: '".jarvis/drops/shot.png"',
+ kind: "image" as const,
+ detail: "",
+ described_by: "vision" as const,
+ note: "",
+};
+
+const TARGET = { sessionId: null, cwd: "C:\\work", provider: "claude-api", surface: "agent" as const };
+const owner = {};
+
+function png() {
+ return new File([new Uint8Array([1])], "shot.png", { type: "image/png" });
+}
+
+describe("held files across unmounts", () => {
+ beforeEach(() => {
+ vi.stubGlobal("fetch", vi.fn(async () => ({
+ ok: true,
+ status: 200,
+ json: async () => ({ attachments: [SHOT], cwd: "C:\\work" }),
+ }) as Response));
+ URL.createObjectURL = vi.fn(() => "blob:shot");
+ URL.revokeObjectURL = vi.fn();
+ });
+
+ afterEach(() => {
+ cleanup();
+ forgetHeldFiles(owner);
+ vi.unstubAllGlobals();
+ });
+
+ it("gives a draft its files and pictures back after a remount", async () => {
+ const first = renderHook(() => useChatAttachments(TARGET, () => {}, { owner, key: "draft:a" }));
+ await act(async () => first.result.current.attachFiles([png()]));
+ await waitFor(() => expect(first.result.current.attachments).toEqual([SHOT]));
+ first.unmount();
+
+ // The picture is still alive: a kept draft frees it on send, not on unmount.
+ expect(URL.revokeObjectURL).not.toHaveBeenCalled();
+ const second = renderHook(() => useChatAttachments(TARGET, () => {}, { owner, key: "draft:a" }));
+ expect(second.result.current.attachments).toEqual([SHOT]);
+ expect(second.result.current.previews).toEqual({ "shot.png": "blob:shot" });
+ });
+
+ it("keeps another draft's files out", async () => {
+ const first = renderHook(() => useChatAttachments(TARGET, () => {}, { owner, key: "draft:a" }));
+ await act(async () => first.result.current.attachFiles([png()]));
+ await waitFor(() => expect(first.result.current.attachments).toHaveLength(1));
+
+ const other = renderHook(() => useChatAttachments(TARGET, () => {}, { owner, key: "session-2" }));
+ expect(other.result.current.attachments).toEqual([]);
+ const stranger = renderHook(() => useChatAttachments(TARGET, () => {}, { owner: {}, key: "draft:a" }));
+ expect(stranger.result.current.attachments).toEqual([]);
+ });
+
+ it("frees the pictures once the files are cleared for sending", async () => {
+ const { result } = renderHook(() => useChatAttachments(TARGET, () => {}, { owner, key: "draft:a" }));
+ await act(async () => result.current.attachFiles([png()]));
+ await waitFor(() => expect(result.current.attachments).toHaveLength(1));
+
+ act(() => result.current.clear());
+ expect(result.current.attachments).toEqual([]);
+ expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:shot");
+ });
+
+ it("lets files without a draft go with the composer", async () => {
+ const first = renderHook(() => useChatAttachments(TARGET, () => {}));
+ await act(async () => first.result.current.attachFiles([png()]));
+ await waitFor(() => expect(first.result.current.attachments).toHaveLength(1));
+ first.unmount();
+
+ expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:shot");
+ const second = renderHook(() => useChatAttachments(TARGET, () => {}));
+ expect(second.result.current.attachments).toEqual([]);
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.ts b/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.ts
index 97acf123c5..755cc4830d 100644
--- a/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.ts
+++ b/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.ts
@@ -17,7 +17,7 @@
* differs: a chat has no pane to type a path into, so the files are HELD here
* and travel with the sentence when it is sent.
*/
-import { useCallback, useEffect, useRef, useState } from "react";
+import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react";
import {
extractPaneDrop,
@@ -59,6 +59,56 @@ export interface ChatAttachments {
clear: () => void;
}
+/** The files one draft holds, with their pictures. */
+interface HeldFiles {
+ attachments: ChatAttachment[];
+ previews: Record<string, string>;
+}
+
+const NOTHING_HELD: HeldFiles = { attachments: [], previews: {} };
+
+/**
+ * Held files per draft, outside React. A composer unmounts whenever the
+ * person switches app sections; the typed text already waited in a draft
+ * store, and files that vanished beside it read as the app dropping half the
+ * message (maintainer, 2026-10-05). The outer key is the owner (a chat store),
+ * so two chats with the same session key never share files.
+ */
+const parked = new WeakMap<object, Map<string, HeldFiles>>();
+const listeners = new Set<() => void>();
+
+function heldFor(owner: object, key: string): HeldFiles {
+ return parked.get(owner)?.get(key) ?? NOTHING_HELD;
+}
+
+function updateHeld(owner: object, key: string, update: (current: HeldFiles) => HeldFiles): void {
+ const current = heldFor(owner, key);
+ const next = update(current);
+ if (next === current) return;
+ let drafts = parked.get(owner);
+ if (!drafts) {
+ drafts = new Map();
+ parked.set(owner, drafts);
+ }
+ if (next.attachments.length === 0 && Object.keys(next.previews).length === 0) drafts.delete(key);
+ else drafts.set(key, next);
+ listeners.forEach((listener) => listener());
+}
+
+/** Let go of every draft's held files for one owner, pictures included. */
+export function forgetHeldFiles(owner: object): void {
+ const drafts = parked.get(owner);
+ if (!drafts) return;
+ drafts.forEach((held) => Object.values(held.previews).forEach(revokePreview));
+ parked.delete(owner);
+ listeners.forEach((listener) => listener());
+}
+
+function subscribeHeld(listener: () => void): () => void {
+ listeners.add(listener);
+ return () => listeners.delete(listener);
+}
+
/**
* Hold files for one chat's next message.
*
@@ -67,6 +117,10 @@ export interface ChatAttachments {
* drop that carried nothing usable is a warning about what was dragged, while
* an attach that threw is an error about the app, and collapsing the two
* buries the second.
+ *
+ * `draft` keeps the files across unmounts: the same owner and key get them
+ * back, the way a composer gets its unsent text back. Without it the files
+ * live and die with the composer.
*/
export function useChatAttachments(
target: {
@@ -76,17 +130,27 @@ export function useChatAttachments(
surface: AgentChatSurface;
},
onProblem: (message: string, severity: "warning" | "error") => void,
+ draft?: { owner: object; key: string },
): ChatAttachments {
- const [attachments, setAttachments] = useState<ChatAttachment[]>([]);
+ const ownRef = useRef<object | null>(null);
+ ownRef.current ??= {};
+ const owner = draft?.owner ?? ownRef.current;
+ const key = draft?.key ?? "";
+ const kept = draft !== undefined;
+ const held = useSyncExternalStore(subscribeHeld, () => heldFor(owner, key));
+ const { attachments, previews } = held;
const [analyzing, setAnalyzing] = useState(0);
- const [previews, setPreviews] = useState<Record<string, string>>({});
- const previewsRef = useRef(previews);
- previewsRef.current = previews;
const { sessionId, cwd, provider, surface } = target;
- // Object URLs hold the picture in memory until revoked; let them go with
- // the composer.
- useEffect(() => () => Object.values(previewsRef.current).forEach(revokePreview), []);
+ // Object URLs hold the picture in memory until revoked. Files that are not
+ // kept for a draft go with the composer; kept ones wait for send or remove.
+ useEffect(() => {
+ if (kept) return;
+ return () => updateHeld(owner, key, (current) => {
+ Object.values(current.previews).forEach(revokePreview);
+ return NOTHING_HELD;
+ });
+ }, [kept, owner, key]);
const attach = useCallback(
async (payload: PaneDropPayload) => {
@@ -105,12 +169,6 @@ export function useChatAttachments(
onProblem("That drop carried nothing this chat could use.", "warning");
return;
}
- // Keyed by name so the same file attached twice is held once — a
- // repeated reference has the model read it again for nothing.
- setAttachments((prev) => [
- ...prev,
- ...found.filter((item) => !prev.some((held) => held.name === item.name)),
- ]);
const pictures = matchPreviews(found, payload.files);
if (folder) {
for (const item of found) {
@@ -119,23 +177,31 @@ export function useChatAttachments(
}
}
}
- if (Object.keys(pictures).length > 0) {
- setPreviews((prev) => {
- const next = { ...prev };
- for (const [name, url] of Object.entries(pictures)) {
- if (next[name]) revokePreview(next[name]);
- next[name] = url;
- }
- return next;
- });
- }
+ // Written to the draft this drop was made in, even when the person
+ // switched away while the file was still being read.
+ updateHeld(owner, key, (current) => {
+ const nextPreviews = { ...current.previews };
+ for (const [name, url] of Object.entries(pictures)) {
+ if (nextPreviews[name]) revokePreview(nextPreviews[name]);
+ nextPreviews[name] = url;
+ }
+ return {
+ // Keyed by name so the same file attached twice is held once — a
+ // repeated reference has the model read it again for nothing.
+ attachments: [
+ ...current.attachments,
+ ...found.filter((item) => !current.attachments.some((held) => held.name === item.name)),
+ ],
+ previews: nextPreviews,
+ };
+ });
} catch (e) {
onProblem((e as Error).message, "error");
} finally {
setAnalyzing((n) => Math.max(0, n - 1));
}
},
- [sessionId, cwd, provider, surface, onProblem],
+ [sessionId, cwd, provider, surface, onProblem, owner, key],
);
const { dragging, handlers: dragHandlers } = usePaneFileDrag(
@@ -185,23 +251,20 @@ export function useChatAttachments(
);
const remove = useCallback((name: string) => {
- setAttachments((prev) => prev.filter((a) => a.name !== name));
- setPreviews((prev) => {
- if (!prev[name]) return prev;
- revokePreview(prev[name]);
- const rest = { ...prev };
+ updateHeld(owner, key, (current) => {
+ const rest = { ...current.previews };
+ if (rest[name]) revokePreview(rest[name]);
delete rest[name];
- return rest;
+ return { attachments: current.attachments.filter((a) => a.name !== name), previews: rest };
});
- }, []);
+ }, [owner, key]);
const clear = useCallback(() => {
- setAttachments([]);
- setPreviews((prev) => {
- Object.values(prev).forEach(revokePreview);
- return {};
+ updateHeld(owner, key, (current) => {
+ Object.values(current.previews).forEach(revokePreview);
+ return NOTHING_HELD;
});
- }, []);
+ }, [owner, key]);
return {
attachments,
diff --git a/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadComposer.tsx b/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadComposer.tsx
index bb33b0e354..6d8b850798 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadComposer.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadComposer.tsx
@@ -224,6 +224,7 @@ export function ThreadComposer({
const files = useChatAttachments(
{ sessionId: activeSessionId, cwd: draft.cwd, provider: draft.provider, surface: "agent" },
(message) => setProblem(message),
+ { owner: useThreadChatStore, key: threadKey },
);
const triggers = useMemo(() => provider?.typeahead ?? [], [provider]);
const typeahead = useComposerTypeahead(textareaRef, value, setValue, {