A desktop coding agent with free access to frontier models.
TypeScript
fix: chat undo key only from the transcript and asked first
ogsamrat68ba638d87b6
diff --git a/apps/app/src/react-app/domains/session/commands/session-command-controls.tsx b/apps/app/src/react-app/domains/session/commands/session-command-controls.tsxindex 1dd4b3c2..a9fa80fe 100644--- a/apps/app/src/react-app/domains/session/commands/session-command-controls.tsx+++ b/apps/app/src/react-app/domains/session/commands/session-command-controls.tsx@@ -76,6 +76,8 @@ export function SessionCommandControls(props: SessionCommandControlsProps) {toast.info("Stop the turn first (Esc), then undo", { duration: 4000 });return;}+ // A key press is easy to make by accident: the undo (the turn and its file changes) asks first.+ if (action === "undo" && !window.confirm("Undo the last turn? Its messages are hidden and its file changes taken back. Redo or Restore brings them back.")) return;void (action === "undo" ? undo() : redo());};window.addEventListener("keydown", onKey, true);diff --git a/apps/app/src/react-app/domains/session/commands/undo-key.ts b/apps/app/src/react-app/domains/session/commands/undo-key.tsindex 8837624e..53db2b2f 100644--- a/apps/app/src/react-app/domains/session/commands/undo-key.ts+++ b/apps/app/src/react-app/domains/session/commands/undo-key.ts@@ -1,35 +1,32 @@-// Cmd/Ctrl+Z for the chat: undo the last turn (Shift: redo), only with focus in the chat itself.+// Cmd/Ctrl+Z for the chat: undo the last turn (Shift: redo), only with focus in the chat transcript itself.type KeyTarget = {tagName?: string;isContentEditable?: boolean;closest?: (selector: string) => unknown;- ownerDocument?: { querySelector?: (selector: string) => unknown } | null;};+/** Anything with a key meaning of its own: fields, editors, terminals, buttons, links, lists, trees, menus. */+const OWN_KEYS = [+ "input", "textarea", "select", "button", "a[href]", "[contenteditable]", ".monaco-editor", ".xterm", ".cm-editor",+ "[role=button]", "[role=textbox]", "[role=tree]", "[role=treeitem]", "[role=listbox]", "[role=option]", "[role=menu]",+ "[role=menuitem]", "[role=searchbox]", "[role=tab]", "[data-composer-shell]", "[data-editor-panel]",+].join(", ");+/**- * A key press Cmd/Ctrl+Z may take for the chat. Never from an editable element (its own undo), never a held- * key's auto-repeat (one press is one turn), and only with focus inside the chat pane, or on the page itself- * while no code editor is on screen: Cmd/Ctrl+Z in the IDE's file tree, tabs, search or problems lists means- * the editor's undo to the person, and taking a chat turn (and its file changes) back from there loses work.+ * A key press Cmd/Ctrl+Z may take for the chat. Only with focus explicitly inside the chat transcript+ * (`[data-chat-transcript]`, focused by a click in it), never from anything with its own keys (a field,+ * the composer, a code editor, the IDE's file tree, tabs, search or problems lists, a button), never from+ * the page itself (nothing focused), and never a held key's auto-repeat: one press is one turn at most.*/export function chatUndoKey(event: Pick<KeyboardEvent, "key" | "metaKey" | "ctrlKey" | "altKey" | "shiftKey" | "target" | "defaultPrevented"> & { repeat?: boolean }, mac: boolean): "undo" | "redo" | null {if (event.defaultPrevented || event.altKey || event.repeat) return null;if (!(mac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey)) return null;if (event.key?.toLowerCase() !== "z") return null;const target = event.target as KeyTarget | null;- if (target && typeof target === "object" && "tagName" in target) {- if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.tagName === "SELECT") return null;- if (target.isContentEditable) return null;- if (target.tagName === "BODY" || target.tagName === "HTML") {- // Nothing focused: the chat's, unless a code editor shares the window (IDE layout).- const editorOnScreen = target.ownerDocument?.querySelector?.("[data-editor-panel]");- if (editorOnScreen) return null;- } else {- if (typeof target.closest !== "function") return null;- if (target.closest("[contenteditable], .monaco-editor, .xterm, .cm-editor")) return null;- if (!target.closest("[data-session-pane]")) return null;- }- }+ if (!target || typeof target !== "object" || typeof target.closest !== "function") return null;+ if (target.isContentEditable) return null;+ if (target.closest(OWN_KEYS)) return null;+ if (!target.closest("[data-chat-transcript]")) return null;return event.shiftKey ? "redo" : "undo";}diff --git a/apps/app/tests/session-commands.test.ts b/apps/app/tests/session-commands.test.tsindex 4a9ac526..b77377e2 100644--- a/apps/app/tests/session-commands.test.ts+++ b/apps/app/tests/session-commands.test.ts@@ -179,43 +179,44 @@ describe("changes", () => {});describe("chatUndoKey", () => {- const key = (init: Partial<KeyboardEvent> & { target?: unknown }) => ({+ const key = (init: Partial<KeyboardEvent> & { target?: unknown; repeat?: boolean }) => ({key: "z", metaKey: false, ctrlKey: false, altKey: false, shiftKey: false, defaultPrevented: false, target: null, ...init,}) as Parameters<typeof chatUndoKey>[0];-- test("Cmd/Ctrl+Z undoes and Shift redoes outside text fields", () => {- expect(chatUndoKey(key({ metaKey: true }), true)).toBe("undo");- expect(chatUndoKey(key({ ctrlKey: true }), false)).toBe("undo");- expect(chatUndoKey(key({ ctrlKey: true, shiftKey: true }), false)).toBe("redo");- expect(chatUndoKey(key({ ctrlKey: true }), true)).toBeNull();- expect(chatUndoKey(key({ metaKey: true, key: "y" }), true)).toBeNull();- });-- test("any editable element keeps its own undo, the empty composer too", () => {- expect(chatUndoKey(key({ metaKey: true, target: { tagName: "TEXTAREA", isContentEditable: false, textContent: "" } }), true)).toBeNull();- expect(chatUndoKey(key({ metaKey: true, target: { tagName: "DIV", isContentEditable: true, textContent: "draft" } }), true)).toBeNull();- expect(chatUndoKey(key({ metaKey: true, target: { tagName: "DIV", isContentEditable: true, textContent: " " } }), true)).toBeNull();- const inChat = (selector: string) => (selector === "[data-session-pane]" ? {} : null);- expect(chatUndoKey(key({ metaKey: true, target: { tagName: "BUTTON", isContentEditable: false, textContent: "Send", closest: inChat } }), true)).toBe("undo");- const inEditor = { tagName: "DIV", isContentEditable: false, textContent: "", closest: (selector: string) => (selector.includes(".monaco-editor") || selector === "[data-session-pane]" ? {} : null) };- expect(chatUndoKey(key({ ctrlKey: true, target: inEditor }), false)).toBeNull();- });-- test("only with focus in the chat: the IDE's file tree, tabs and lists keep the key", () => {- const outsideChat = { tagName: "BUTTON", isContentEditable: false, closest: () => null };- expect(chatUndoKey(key({ metaKey: true, target: outsideChat }), true)).toBeNull();- const noClosest = { tagName: "DIV", isContentEditable: false };- expect(chatUndoKey(key({ metaKey: true, target: noClosest }), true)).toBeNull();- // Nothing focused: the chat's in the Chat layout, nobody's while the code editor is on screen.- const body = (editor: boolean) => ({ tagName: "BODY", isContentEditable: false, ownerDocument: { querySelector: () => (editor ? {} : null) } });- expect(chatUndoKey(key({ metaKey: true, target: body(false) }), true)).toBe("undo");- expect(chatUndoKey(key({ metaKey: true, target: body(true) }), true)).toBeNull();- });-- test("a held key undoes one turn, not one per auto-repeat", () => {- const inChat = { tagName: "BUTTON", isContentEditable: false, closest: (selector: string) => (selector === "[data-session-pane]" ? {} : null) };- expect(chatUndoKey(key({ metaKey: true, target: inChat }), true)).toBe("undo");- expect(chatUndoKey({ ...key({ metaKey: true, target: inChat }), repeat: true }, true)).toBeNull();- expect(chatUndoKey({ ...key({ metaKey: true, shiftKey: true, target: inChat }), repeat: true }, true)).toBeNull();+ // An element whose ancestors match these selectors.+ const el = (tagName: string, ...inside: string[]) => ({+ tagName,+ isContentEditable: false,+ closest: (selector: string) => (selector.split(", ").some((part) => inside.includes(part)) ? {} : null),+ });+ const transcript = el("DIV", "[data-chat-transcript]");++ test("Cmd/Ctrl+Z undoes and Shift redoes with focus in the transcript", () => {+ expect(chatUndoKey(key({ metaKey: true, target: transcript }), true)).toBe("undo");+ expect(chatUndoKey(key({ ctrlKey: true, target: transcript }), false)).toBe("undo");+ expect(chatUndoKey(key({ ctrlKey: true, shiftKey: true, target: transcript }), false)).toBe("redo");+ expect(chatUndoKey(key({ ctrlKey: true, target: transcript }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, key: "y", target: transcript }), true)).toBeNull();+ });++ test("anything with keys of its own keeps them, inside the transcript too", () => {+ expect(chatUndoKey(key({ metaKey: true, target: el("TEXTAREA", "textarea", "[data-chat-transcript]") }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, target: { ...transcript, isContentEditable: true } }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, target: el("BUTTON", "button", "[data-chat-transcript]") }), true)).toBeNull();+ expect(chatUndoKey(key({ ctrlKey: true, target: el("DIV", ".monaco-editor", "[data-chat-transcript]") }), false)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, target: el("DIV", "[data-composer-shell]") }), true)).toBeNull();+ });++ test("never from the IDE, the page itself or anywhere outside the transcript", () => {+ expect(chatUndoKey(key({ metaKey: true, target: el("DIV", "[role=treeitem]", "[data-editor-panel]") }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, target: el("DIV", "[data-editor-panel]") }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, target: el("DIV", "[role=tab]") }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, target: el("BODY") }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, target: el("DIV", "[data-session-pane]") }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, target: null }), true)).toBeNull();+ });++ test("a held key undoes one turn at most, not one per auto-repeat", () => {+ expect(chatUndoKey(key({ metaKey: true, target: transcript, repeat: true }), true)).toBeNull();+ expect(chatUndoKey(key({ metaKey: true, shiftKey: true, target: transcript, repeat: true }), true)).toBeNull();});});