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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
feat(ide): take back an unstarted thread message with Escape
rubenluetke10-beep3139f16aa578
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.ts b/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.tsindex 755cc4830d..410750e1df 100644--- a/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.ts+++ b/jarvis/ui/web/frontend/src/components/agentchat/useChatAttachments.ts@@ -57,10 +57,14 @@ export interface ChatAttachments {onPaste: (event: React.ClipboardEvent) => void;remove: (name: string) => void;clear: () => void;+ /** Hand over the held files and empty the draft WITHOUT releasing their pictures. */+ take: () => HeldFiles;+ /** Put files handed over by `take` back into the draft. */+ restore: (files: HeldFiles) => void;}/** The files one draft holds, with their pictures. */-interface HeldFiles {+export interface HeldFiles {attachments: ChatAttachment[];previews: Record<string, string>;}@@ -95,6 +99,11 @@ function updateHeld(owner: object, key: string, update: (current: HeldFiles) =>listeners.forEach((listener) => listener());}+/** Let go of the pictures of files handed over by `take` and never restored. */+export function releaseHeldFiles(files: HeldFiles): void {+ Object.values(files.previews).forEach(revokePreview);+}+/** Let go of every draft's held files for one owner, pictures included. */export function forgetHeldFiles(owner: object): void {const drafts = parked.get(owner);@@ -266,6 +275,22 @@ export function useChatAttachments(});}, [owner, key]);+ const take = useCallback((): HeldFiles => {+ const current = heldFor(owner, key);+ updateHeld(owner, key, () => NOTHING_HELD);+ return current;+ }, [owner, key]);++ const restore = useCallback((files: HeldFiles) => {+ updateHeld(owner, key, (current) => ({+ attachments: [+ ...files.attachments,+ ...current.attachments.filter((item) => !files.attachments.some((back) => back.name === item.name)),+ ],+ previews: { ...files.previews, ...current.previews },+ }));+ }, [owner, key]);+return {attachments,previews,@@ -276,6 +301,8 @@ export function useChatAttachments(onPaste,remove,clear,+ take,+ restore,};}diff --git a/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadComposer.tsx b/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadComposer.tsxindex 9ad7b180de..ab7745dae1 100644--- a/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadComposer.tsx+++ b/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadComposer.tsx@@ -5,7 +5,7 @@ import { ChatMarkdown } from "@/components/agentchat/ChatMarkdown";import { ComposerTypeahead } from "@/components/agentchat/ComposerTypeahead";import { DictationButton } from "@/components/agentchat/DictationButton";import { runningTurn, type QuestionState, type Timeline, type ToolBlock, type TurnItem } from "@/components/agentchat/reduce";-import { useChatAttachments } from "@/components/agentchat/useChatAttachments";+import { releaseHeldFiles, useChatAttachments, type HeldFiles } from "@/components/agentchat/useChatAttachments";import { useComposerDictation } from "@/components/agentchat/useComposerDictation";import { useComposerTypeahead } from "@/components/agentchat/useComposerTypeahead";import { useT } from "@/i18n";@@ -27,6 +27,35 @@ const drafts = new Map<string, string>();/** Queued follow-ups per thread, so switching threads never drops one. */const queues = new Map<string, QueuedMessage[]>();+/**+ * The message sent last, with its files, so Escape can take it back while the+ * agent has not started on it. Module-level: a thread's first message swaps+ * the empty-thread composer for the timeline one.+ */+let lastSent: { text: string; files: HeldFiles } | null = null;++function keepSent(next: { text: string; files: HeldFiles } | null): void {+ if (lastSent && lastSent.files !== next?.files) releaseHeldFiles(lastSent.files);+ lastSent = next;+}++/**+ * True while the running turn answers `sent` and shows no work yet: no+ * reasoning, no tool call, no words. The message right before the turn must be+ * the one sent, so a queued follow-up or another thread's turn never hands back+ * the wrong text.+ */+function untouchedReply(timeline: Timeline, sent: { text: string; files: HeldFiles }): boolean {+ const turn = runningTurn(timeline);+ if (!turn || !turn.blocks.every((block) => block.kind === "text" && !block.text.trim())) return false;+ const index = timeline.items.indexOf(turn);+ const asked = timeline.items.slice(0, index).reverse().find((item) => item.type === "user");+ if (!asked || asked.type !== "user") return false;+ if (sent.text) return asked.text.trim() === sent.text;+ const names = sent.files.attachments.map((item) => item.name).sort().join("|");+ return names !== "" && asked.attachments.map((item) => item.name).sort().join("|") === names;+}+/** The open question card of the newest turn, if any. */function openQuestion(timeline: Timeline): { block: ToolBlock; question: QuestionState } | null {for (let i = timeline.items.length - 1; i >= 0; i--) {@@ -196,6 +225,7 @@ export function ThreadComposer({placeholder = "Ask for changes, send follow-ups, or attach images",autoFocusNonce,strip,+ onScreen = true,}: {/** Which thread the box is typing for — the session id, or `draft:<project>`. */threadKey: string;@@ -204,6 +234,8 @@ export function ThreadComposer({autoFocusNonce: number;/** The strip that hangs under the card — where the agent works and on which branch. */strip?: ReactNode;+ /** False while the IDE section is hidden: its keys belong to whatever is on screen. */+ onScreen?: boolean;}) {const draft = useThreadChatStore((state) => state.draft);const timeline = useThreadChatStore((state) => state.timeline);@@ -348,15 +380,17 @@ export function ThreadComposer({setProblem("Choose a connected coding agent first.");return;}- const attachments = files.attachments;+ const held = files.take();+ const attachments = held.attachments;setValue("");- files.clear();setProblem("");setPaused(false);if (running || busy) {+ releaseHeldFiles(held);setQueue((current) => [...current, { id: ++queueId.current, text, attachments }]);return;}+ keepSent({ text, files: held });const sent = await dispatch(text, attachments);if (!sent && !useThreadChatStore.getState().activeSessionId) {// Nothing was created: give the words back instead of losing them.@@ -364,6 +398,31 @@ export function ThreadComposer({}};+ // Escape before the agent started on the message stops the turn and puts the+ // message, files included, back into the box to edit and send again.+ const recall = useRef<() => boolean>(() => false);+ recall.current = () => {+ if (!lastSent || !untouchedReply(timeline, lastSent)) return false;+ const { text, files: held } = lastSent;+ lastSent = null;+ setPaused(true);+ void useThreadChatStore.getState().cancel();+ const current = drafts.get(threadKey) ?? "";+ setValue(current.trim() ? `${text}\n${current}` : text);+ files.restore(held);+ textareaRef.current?.focus();+ return true;+ };+ useEffect(() => {+ if (!onScreen) return;+ const onEscape = (event: globalThis.KeyboardEvent) => {+ if (event.key !== "Escape" || event.defaultPrevented || event.isComposing) return;+ if (recall.current()) event.preventDefault();+ };+ window.addEventListener("keydown", onEscape);+ return () => window.removeEventListener("keydown", onEscape);+ }, [onScreen]);+const onKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {if (typeahead.onKeyDown(event)) return;if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) {diff --git a/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadRecall.test.tsx b/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadRecall.test.tsxnew file mode 100644index 0000000000..1f8a5b1b12--- /dev/null+++ b/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadRecall.test.tsx@@ -0,0 +1,161 @@+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import type { AgentChatCatalog, AgentChatEvent, AgentChatSession } from "@/lib/agentChatApi";+import type { IdeProject } from "@/lib/agenticIdeApi";+import { forgetHeldFiles } from "@/components/agentchat/useChatAttachments";+import { useIdeProjectsStore } from "@/store/ideProjects";+import { useIdeThreadsStore } from "@/store/ideThreads";++const api = vi.hoisted(() => ({+ fetchAgentChatCatalog: vi.fn(),+ fetchAgentConnections: vi.fn(),+ fetchProviderHealth: vi.fn(),+ fetchAgentChatSessions: vi.fn(),+ sendAgentChatMessage: vi.fn(),+ cancelAgentChatTurn: vi.fn(),+ attachChatFilesIn: vi.fn(),+}));+vi.mock("@/lib/agentChatApi", async (importOriginal) => ({ ...(await importOriginal<typeof import("@/lib/agentChatApi")>()), ...api }));+vi.mock("@/lib/gitApi", async (importOriginal) => ({+ ...(await importOriginal<typeof import("@/lib/gitApi")>()),+ inspectGit: vi.fn().mockResolvedValue({ is_repo: false, branches: [], changes: [], remotes: [] }),+ prepareGit: vi.fn(),+}));+vi.mock("@/lib/chatLibraryApi", async (importOriginal) => ({+ ...(await importOriginal<typeof import("@/lib/chatLibraryApi")>()),+ fetchProjectLaunchers: vi.fn().mockResolvedValue({ file_manager: true, editors: [], remote_url: null, remote_label: null }),+}));++import { ThreadView } from "./ThreadView";+import { useThreadChatStore } from "./threadModel";++/**+ * Escape takes a message back while the agent has not started on it: the turn+ * stops and the words and files return to the composer.+ */++class FakeSocket {+ onopen: (() => void) | null = null;+ onmessage: ((msg: { data: string }) => void) | null = null;+ onclose: (() => void) | null = null;+ onerror: (() => void) | null = null;+ constructor(readonly url: string) {}+ close() { /* frames are driven by hand */ }+}++const CATALOG: AgentChatCatalog = {+ default_cwd: "/home",+ shell: "bash",+ selection: null,+ providers: [{+ id: "claude-api", label: "Anthropic Claude", family: "anthropic", runner: "claude-cli", agent: "claude",+ models_source: "curated", curated_models: [], default_model: "",+ keyless: false, native_resume: true, effort_levels: ["high"], default_effort: "high",+ permission_modes: [{ id: "default", label: "Ask first", description: "" }], default_permission_mode: "default",+ cli_installed: true, typeahead: [],+ }],+} as AgentChatCatalog;++const project = { id: "p1", path: "/repo", name: "App", color: null, pinned: false, archived: false, scratch: false,+ created_at: 0, last_opened_at: 0, exists: true, chats: 0, workspaces: [] } as IdeProject;++const SHOT = { name: "shot.png", reference: '".jarvis/drops/shot.png"', kind: "image" as const,+ detail: "A screenshot.", described_by: "vision" as const, note: "" };++function sessionRow(extra: Partial<AgentChatSession> = {}): AgentChatSession {+ return { session_id: "s1", title: "fix it", provider: "claude-api", model: "", effort: "high", cwd: "/repo",+ permission_mode: "default", surface: "agent", vendor_session: null, created_ms: 1, updated_ms: 2, message_count: 1, preview: "", ...extra };+}++let seq = 0;+function feed(events: [string, Record<string, unknown>][]): void {+ const store = useThreadChatStore.getState();+ act(() => {+ for (const [kind, payload] of events) {+ seq += 1;+ store.ingest({ seq, ts_ms: 1000 + seq, kind, payload } as AgentChatEvent);+ }+ });+}++function transfer(files: File[]) {+ return {+ files,+ items: files.map((file) => ({ kind: "file", type: file.type, getAsFile: () => file })),+ types: files.length ? ["Files"] : [],+ getData: () => "",+ dropEffect: "none",+ };+}++/** Open thread s1, attach a screenshot, type and send. */+async function sendWithShot(text: string): Promise<HTMLTextAreaElement> {+ useIdeThreadsStore.setState({ selection: { projectId: "p1", sessionId: "s1" } });+ api.fetchAgentChatSessions.mockResolvedValue([sessionRow()]);+ render(<ThreadView onScreen />);+ await waitFor(() => expect(useThreadChatStore.getState().activeSessionId).toBe("s1"));+ feed([+ ["user_message", { text: "earlier" }],+ ["turn_started", { turn_id: "t0", provider: "claude-api", model: "", effort: "high", runner: "claude-cli" }],+ ["turn_finished", { turn_id: "t0", status: "done", duration_ms: 10 }],+ ]);+ const box = screen.getByTestId("thread-composer-input") as HTMLTextAreaElement;+ const png = new File([new Uint8Array([1, 2, 3])], "image.png", { type: "image/png" });+ await act(async () => { fireEvent.paste(box, { clipboardData: transfer([png]) }); });+ await waitFor(() => expect(screen.getByTestId("chat-attachment-shot.png")).toBeTruthy());+ fireEvent.change(box, { target: { value: text } });+ await act(async () => { fireEvent.keyDown(box, { key: "Enter" }); });+ await waitFor(() => expect(api.sendAgentChatMessage).toHaveBeenCalled());+ expect(box.value).toBe("");+ expect(screen.queryByTestId("chat-attachment-shot.png")).toBeNull();+ feed([+ ["user_message", { text: `${text}\n\n[file]`, typed: text, attachments: [{ name: "shot.png", kind: "image", described_by: "vision" }] }],+ ["turn_started", { turn_id: "t1", provider: "claude-api", model: "", effort: "high", runner: "claude-cli" }],+ ]);+ return box;+}++beforeEach(() => {+ localStorage.clear();+ vi.stubGlobal("WebSocket", FakeSocket);+ api.fetchAgentChatCatalog.mockReset().mockResolvedValue(CATALOG);+ api.fetchAgentConnections.mockReset().mockResolvedValue([{ jarvis: "claude-api", key_set: true, is_active_brain: true }]);+ api.fetchProviderHealth.mockReset().mockResolvedValue([]);+ api.fetchAgentChatSessions.mockReset().mockResolvedValue([]);+ api.sendAgentChatMessage.mockReset().mockResolvedValue({ turn_id: "t1" });+ api.cancelAgentChatTurn.mockReset().mockResolvedValue(undefined);+ api.attachChatFilesIn.mockReset().mockResolvedValue({ attachments: [SHOT], cwd: "" });+ useIdeProjectsStore.setState({ projects: [project], activeWorkspaceId: null });+ useIdeThreadsStore.setState({ layout: "threads", selection: { projectId: "p1", sessionId: null }, projectOf: {}, seen: {}, archived: {}, order: {}, focusNonce: 0 });+ useThreadChatStore.getState().newChat();+});++afterEach(() => {+ cleanup();+ forgetHeldFiles(useThreadChatStore);+ vi.unstubAllGlobals();+});++describe("Escape takes back a message the agent has not started on", () => {+ it("stops the turn and returns the words and the screenshot to the composer", async () => {+ const box = await sendWithShot("fix the login");+ expect(api.sendAgentChatMessage.mock.calls[0][2]).toEqual([SHOT]);++ await act(async () => { fireEvent.keyDown(window, { key: "Escape" }); });++ expect(api.cancelAgentChatTurn).toHaveBeenCalledWith("s1");+ expect(box.value).toBe("fix the login");+ expect(screen.getByTestId("chat-attachment-shot.png")).toBeTruthy();+ });++ it("leaves the message alone once the agent started thinking", async () => {+ const box = await sendWithShot("fix the login");+ feed([["reasoning_started", { turn_id: "t1" }]]);++ await act(async () => { fireEvent.keyDown(window, { key: "Escape" }); });++ expect(api.cancelAgentChatTurn).not.toHaveBeenCalled();+ expect(box.value).toBe("");+ expect(screen.queryByTestId("chat-attachment-shot.png")).toBeNull();+ });+});diff --git a/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadView.tsx b/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadView.tsxindex 160e808743..a3010518af 100644--- a/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadView.tsx+++ b/jarvis/ui/web/frontend/src/components/agentic/threads/ThreadView.tsx@@ -184,14 +184,14 @@ export function ThreadView({ onScreen }: { onScreen: boolean }) {</div></ThreadPopover><div className="mt-7 w-full">- <ThreadComposer threadKey={threadKey} prepareDraft={prepareDraft} autoFocusNonce={focusNonce + (onScreen ? 1 : 0)} strip={strip} />+ <ThreadComposer threadKey={threadKey} prepareDraft={prepareDraft} autoFocusNonce={focusNonce + (onScreen ? 1 : 0)} strip={strip} onScreen={onScreen} /></div></div>: <><ThreadTimeline items={timeline.items} sessionId={selection.sessionId} bottomInset={composerHeight} folder={folder} /><div ref={composerBox} className="pointer-events-none absolute inset-x-0 bottom-0 bg-gradient-to-t from-background from-70% to-transparent px-5 pb-4 pt-6"><div className="pointer-events-auto">- <ThreadComposer threadKey={threadKey} prepareDraft={prepareDraft} autoFocusNonce={focusNonce} strip={strip} />+ <ThreadComposer threadKey={threadKey} prepareDraft={prepareDraft} autoFocusNonce={focusNonce} strip={strip} onScreen={onScreen} /></div></div></>}