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): preserve unsent drafts across section changes

rubenluetke10-beep3585b9669c58
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/AgentComposer.tsx b/jarvis/ui/web/frontend/src/components/agentchat/AgentComposer.tsx
index daf30df602..81dec61edf 100644
--- a/jarvis/ui/web/frontend/src/components/agentchat/AgentComposer.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentchat/AgentComposer.tsx
@@ -1,5 +1,5 @@
import { ChatCommandPanel, useChatCommands } from "./ChatCommands";
-import { useCallback, useEffect, useMemo, useRef, useState } from "react";
+import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import {
ArrowUp,
AudioLines,
@@ -36,6 +36,7 @@ import { DictationButton } from "@/components/agentchat/DictationButton";
import { ComposerAddMenu } from "@/components/agentchat/ComposerAddMenu";
import { ComposerChipField, type ComposerChipFieldHandle } from "@/components/agentchat/ComposerChipField";
import type { ToolChoice } from "@/components/agentchat/toolChoices";
+import { readComposerDraft, useComposerDraft, writeComposerDraft } from "./composerDrafts";
import { useAppshotClaim } from "@/components/agentchat/useAppshotClaim";
import { GigiMark } from "@/components/GigiMark";
import { useVoiceModeSwitch } from "@/components/home/assistantStatus";
@@ -94,19 +95,38 @@ import { cn } from "@/lib/utils";
*/
export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
const t = useT();
- const [value, setValueState] = useState("");
+ const store = useAgentChatApi();
+ const activeSessionId = useAgentChat((s) => s.activeSessionId);
+ const messageDraft = useComposerDraft(store, activeSessionId);
+ const { text: value, choices: selectedTools } = messageDraft;
const textareaRef = useRef<HTMLTextAreaElement>(null);
const fieldRef = useRef<ComposerChipFieldHandle>(null);
+ const setSelectedTools = useCallback((choices: ToolChoice[]) => {
+ writeComposerDraft(store, activeSessionId, { ...readComposerDraft(store, activeSessionId), choices });
+ }, [store, activeSessionId]);
+ const setValueState = useCallback((next: string | ((current: string) => string)) => {
+ const current = readComposerDraft(store, activeSessionId);
+ writeComposerDraft(store, activeSessionId, {
+ ...current,
+ text: typeof next === "function" ? next(current.text) : next,
+ });
+ }, [store, activeSessionId]);
const setValue = useCallback(
(next: string | ((current: string) => string)) => {
- setValueState((current) => {
- const text = typeof next === "function" ? next(current) : next;
- fieldRef.current?.setText(text);
- return text;
- });
+ const current = readComposerDraft(store, activeSessionId).text;
+ const text = typeof next === "function" ? next(current) : next;
+ setValueState(text);
+ fieldRef.current?.setText(text);
},
- [],
+ [store, activeSessionId, setValueState],
);
+ useLayoutEffect(() => {
+ const field = fieldRef.current;
+ const current = field?.getDraft();
+ if (current && (current.text !== messageDraft.text || JSON.stringify(current.choices) !== JSON.stringify(messageDraft.choices))) {
+ field?.hydrate(messageDraft.text, messageDraft.choices);
+ }
+ }, [store, activeSessionId, messageDraft]);
const connected = useEventStore((s) => s.connected);
const wsWarming = useEventStore((s) => s.wsWarming);
const assistantName = useEventStore((s) => s.assistantName);
@@ -114,7 +134,6 @@ export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
const pushToast = useEventStore((s) => s.pushToast);
const [restarting, setRestarting] = useState(false);
- const store = useAgentChatApi();
const surface = useAgentChat((s) => s.surface);
const catalog = useAgentChat((s) => s.catalog);
const catalogError = useAgentChat((s) => s.catalogError);
@@ -123,11 +142,6 @@ export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
const liveModels = useAgentChat((s) => s.liveModels);
const health = useAgentChat((s) => s.health);
const draft = useAgentChat((s) => s.draft);
- const activeSessionId = useAgentChat((s) => s.activeSessionId);
- const [selectedTools, setSelectedTools] = useState<ToolChoice[]>([]);
- useEffect(() => {
- setSelectedTools([]);
- }, [activeSessionId, surface]);
const timeline = useAgentChat((s) => s.timeline);
const busy = useAgentChat((s) => s.busy);
const lastError = useAgentChat((s) => s.lastError);
@@ -236,12 +250,12 @@ export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
if ((!content && files.attachments.length === 0) || (running || busy) && !commands.canSteer) return;
if (files.analyzing > 0) return; // a file still being read would be sent without its contents
if (dictating) stopDictation();
- setValueState("");
+ const selected = selectedTools;
+ writeComposerDraft(store, sessionAtSend, { text: "", choices: [] });
fieldRef.current?.clear();
setAttachError("");
const attached = files.attachments;
files.clear();
- const selected = selectedTools;
if (selected.length)
await send(
content,
@@ -251,10 +265,11 @@ export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
else await send(content, attached);
if (sessionAtSend && store.getState().activeSessionId !== sessionAtSend) return;
if (store.getState().lastError) {
- setSelectedTools(selected);
- setValueState((current) => current || content);
- } else {
- setSelectedTools([]);
+ const target = store.getState().activeSessionId;
+ const current = readComposerDraft(store, target);
+ if (!current.text && !current.choices.length) {
+ writeComposerDraft(store, target, { text: value, choices: selected });
+ }
}
}
@@ -590,8 +605,7 @@ export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
autoFocus={autoFocus}
onSubmit={() => (dictating ? stopDictationAndSend() : void onSend())}
onDraftChange={(draft) => {
- setValueState(draft.text);
- setSelectedTools(draft.choices);
+ writeComposerDraft(store, activeSessionId, { text: draft.text, choices: draft.choices });
}}
onKeyDown={(ev) => {
if (commands.onKeyDown(ev)) return true;
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/composerDrafts.ts b/jarvis/ui/web/frontend/src/components/agentchat/composerDrafts.ts
new file mode 100644
index 0000000000..198ba5f430
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentchat/composerDrafts.ts
@@ -0,0 +1,44 @@
+import { createStore, useStore } from "zustand";
+
+import type { AgentChatStoreHook } from "@/store/agentChat";
+import type { ToolChoice } from "./toolChoices";
+
+export interface MessageDraft {
+ text: string;
+ choices: ToolChoice[];
+}
+
+const EMPTY_DRAFT: MessageDraft = { text: "", choices: [] };
+const createDraftStore = () => createStore<{ drafts: Map<string | null, MessageDraft> }>(() => ({ drafts: new Map() }));
+// Keep unsent text in memory, owned by its chat store, across view unmounts.
+// Separate stores also isolate the front page from IDE panes and other agents.
+const stores = new WeakMap<AgentChatStoreHook, ReturnType<typeof createDraftStore>>();
+
+export function composerDraftsFor(chat: AgentChatStoreHook) {
+ let store = stores.get(chat);
+ if (!store) {
+ store = createDraftStore();
+ stores.set(chat, store);
+ }
+ return store;
+}
+
+export function readComposerDraft(chat: AgentChatStoreHook, sessionId: string | null): MessageDraft {
+ return composerDraftsFor(chat).getState().drafts.get(sessionId) ?? EMPTY_DRAFT;
+}
+
+export function writeComposerDraft(chat: AgentChatStoreHook, sessionId: string | null, draft: MessageDraft) {
+ const store = composerDraftsFor(chat);
+ const previous = readComposerDraft(chat, sessionId);
+ if (previous.text === draft.text && JSON.stringify(previous.choices) === JSON.stringify(draft.choices)) return;
+ store.setState(({ drafts }) => {
+ const next = new Map(drafts);
+ if (draft.text || draft.choices.length) next.set(sessionId, draft);
+ else next.delete(sessionId);
+ return { drafts: next };
+ });
+}
+
+export function useComposerDraft(chat: AgentChatStoreHook, sessionId: string | null): MessageDraft {
+ return useStore(composerDraftsFor(chat), (state) => state.drafts.get(sessionId) ?? EMPTY_DRAFT);
+}
diff --git a/jarvis/ui/web/frontend/src/components/home/ChatStage.test.tsx b/jarvis/ui/web/frontend/src/components/home/ChatStage.test.tsx
index 033ad2b398..a45aa62cbd 100644
--- a/jarvis/ui/web/frontend/src/components/home/ChatStage.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/home/ChatStage.test.tsx
@@ -9,6 +9,7 @@ import { AgentChatStoreProvider } from "@/components/agentchat/AgentChatStoreCon
import { useAgentChatStore, useAgentSessionStore } from "@/store/agentChat";
import { useEventStore } from "@/store/events";
import { useHomeStore } from "@/store/home";
+import { composerDraftsFor } from "@/components/agentchat/composerDrafts";
const CATALOG: AgentChatCatalog = {
default_cwd: "C:\\work",
@@ -181,6 +182,8 @@ describe("ChatStage (agent chat)", () => {
window.localStorage.clear();
useEventStore.setState({ connected: true, wsWarming: false, assistantName: "Jarvis" });
useAgentChatStore.setState(seedState());
+ composerDraftsFor(useAgentChatStore).setState({ drafts: new Map() });
+ composerDraftsFor(useAgentSessionStore).setState({ drafts: new Map() });
});
afterEach(() => {
cleanup();
@@ -223,6 +226,106 @@ describe("ChatStage (agent chat)", () => {
useHomeStore.setState({ surface: "chat" });
});
+ it("restores an unsent multiline draft after leaving the chat section", () => {
+ const text = " Draft to finish later\nKeep this second line too. ";
+ const first = render(<ChatStage />);
+ const box = screen.getByRole("textbox");
+ box.textContent = text;
+ fireEvent.input(box);
+ first.unmount();
+
+ const second = render(<ChatStage />);
+ expect(screen.getByRole("textbox").textContent).toBe(text);
+ expect(screen.getByTestId("composer-send")).toBeTruthy();
+ const restored = screen.getByRole("textbox");
+ restored.textContent = `${text}\nAnd a third line.`;
+ fireEvent.input(restored);
+ second.unmount();
+
+ render(<ChatStage />);
+ expect(screen.getByRole("textbox").textContent).toBe(`${text}\nAnd a third line.`);
+ });
+
+ it("keeps drafts separate when switching conversations", () => {
+ render(<ChatStage />);
+ const box = screen.getByRole("textbox");
+ box.textContent = "New conversation draft";
+ fireEvent.input(box);
+ act(() => useAgentChatStore.setState({ activeSessionId: "other-chat" }));
+ expect(screen.getByRole("textbox").textContent).toBe("");
+ const other = screen.getByRole("textbox");
+ other.textContent = "Existing conversation draft";
+ fireEvent.input(other);
+
+ act(() => useAgentChatStore.setState({ activeSessionId: null }));
+ expect(screen.getByRole("textbox").textContent).toBe("New conversation draft");
+ act(() => useAgentChatStore.setState({ activeSessionId: "other-chat" }));
+ expect(screen.getByRole("textbox").textContent).toBe("Existing conversation draft");
+ });
+
+ it("does not transfer the front page draft into the IDE chat", () => {
+ const first = render(<ChatStage />);
+ const box = screen.getByRole("textbox");
+ box.textContent = "Front page draft";
+ fireEvent.input(box);
+ first.unmount();
+ useAgentSessionStore.setState(seedState());
+ const ide = render(<AgentChatStoreProvider store={useAgentSessionStore}><ChatStage /></AgentChatStoreProvider>);
+ expect(screen.getByRole("textbox").textContent).toBe("");
+ ide.unmount();
+ render(<ChatStage />);
+ expect(screen.getByRole("textbox").textContent).toBe("Front page draft");
+ });
+
+ it("keeps a manually cleared draft empty after remounting", () => {
+ const first = render(<ChatStage />);
+ const box = screen.getByRole("textbox");
+ box.textContent = "Discard this draft";
+ fireEvent.input(box);
+ box.textContent = "";
+ fireEvent.input(box);
+ first.unmount();
+ render(<ChatStage />);
+ expect(screen.getByRole("textbox").textContent).toBe("");
+ });
+
+ it("does not restore a successfully sent draft after remounting", async () => {
+ const originalSend = useAgentChatStore.getState().send;
+ const sent: string[] = [];
+ useAgentChatStore.setState({ send: async (text) => { sent.push(text); } });
+ try {
+ const first = render(<ChatStage />);
+ const box = screen.getByRole("textbox");
+ box.textContent = "Send this once";
+ fireEvent.input(box);
+ await act(async () => fireEvent.click(screen.getByTestId("composer-send")));
+ expect(sent).toEqual(["Send this once"]);
+ first.unmount();
+ render(<ChatStage />);
+ expect(screen.getByRole("textbox").textContent).toBe("");
+ } finally {
+ useAgentChatStore.setState({ send: originalSend });
+ }
+ });
+
+ it("restores a failed send in the field and across remounts", async () => {
+ const originalSend = useAgentChatStore.getState().send;
+ useAgentChatStore.setState({ send: async () => { useAgentChatStore.setState({ lastError: "Offline" }); } });
+ try {
+ const first = render(<ChatStage />);
+ const box = screen.getByRole("textbox");
+ box.textContent = "Keep this if sending fails";
+ fireEvent.input(box);
+ await act(async () => fireEvent.click(screen.getByTestId("composer-send")));
+ expect(screen.getByRole("textbox").textContent).toBe("Keep this if sending fails");
+ first.unmount();
+ render(<ChatStage />);
+ expect(screen.getByRole("textbox").textContent).toBe("Keep this if sending fails");
+ } finally {
+ useAgentChatStore.setState({ send: originalSend });
+ }
+ });
+
it("lists each connected provider with its models in the one brain pick", async () => {
render(<ChatStage />);
fireEvent.click(screen.getByTestId("composer-model"));