The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): route native text paste through term.paste to stop last-char duplication
Austin Pickett35f0b4b5696c
diff --git a/web/src/lib/chat-image-drop.ts b/web/src/lib/chat-image-drop.tsindex 7c904c784c8..853999b85b6 100644--- a/web/src/lib/chat-image-drop.ts+++ b/web/src/lib/chat-image-drop.ts@@ -6,17 +6,33 @@ import { imageFilesFromTransfer, transferMayContainImage } from "./chatImagePast* paste / dragover / drop listeners (capture phase) that pull image files out* of the transfer data and hand them to the caller's upload+attach pipeline.* `attachChatImageDropListeners` returns a detach function for effect cleanup.+ *+ * Plain-text pastes go through `pasteText` (#52471): xterm's own paste listener+ * clears the hidden textarea, but the browser's default insert runs AFTER event+ * dispatch, so the stale value survives and the next typed character gets+ * duplicated ("when" → "whenn"). Cancelling the native paste and delivering the+ * text through `term.paste()` exactly once is the same route the Ctrl/Cmd+V+ * keydown interception uses.*/export function attachChatImageDropListeners(host: HTMLElement,uploadAndAttachImages: (files: File[]) => void,+ pasteText: (text: string) => void,): () => void {const handleBrowserPaste = (ev: ClipboardEvent) => {const files = imageFilesFromTransfer(ev.clipboardData);- if (!files.length) return;- ev.preventDefault();- ev.stopPropagation();- uploadAndAttachImages(files);+ if (files.length) {+ ev.preventDefault();+ ev.stopPropagation();+ uploadAndAttachImages(files);+ return;+ }+ const text = ev.clipboardData?.getData("text/plain");+ if (text) {+ ev.preventDefault();+ ev.stopPropagation();+ pasteText(text);+ }};const handleBrowserDragOver = (ev: DragEvent) => {if (!transferMayContainImage(ev.dataTransfer)) return;diff --git a/web/src/lib/pty-mouse-report.ts b/web/src/lib/pty-mouse-report.tsnew file mode 100644index 00000000000..cd213ef17f9--- /dev/null+++ b/web/src/lib/pty-mouse-report.ts@@ -0,0 +1,7 @@+// xterm.js SGR mouse tracking emits raw CSI reports (`\x1b[<...`) that look+// like ordinary bytes to the backend. The embedded web chat prefers input+// stability over terminal-style mouse reporting, so ChatPage drops these+// reports entirely instead of forwarding them into Hermes (see ChatPage.tsx,+// "Keystrokes → PTY").+// eslint-disable-next-line no-control-regex -- intentional ESC byte in xterm SGR mouse report parser+export const SGR_MOUSE_RE = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])$/;diff --git a/web/src/pages/ChatPage.test.tsx b/web/src/pages/ChatPage.test.tsxindex 3f82b398766..9d0615870e8 100644--- a/web/src/pages/ChatPage.test.tsx+++ b/web/src/pages/ChatPage.test.tsx@@ -337,6 +337,80 @@ describe("ChatPage", () => {}});+ // #52471: a native/DOM paste (context-menu paste, middle-click, or any+ // Ctrl+V route that bypasses the keydown interception) must deliver its+ // text through term.paste() exactly once — never through the browser's+ // default insertion into xterm's hidden textarea, which leaves a stale+ // value there and duplicates the next typed character.+ async function renderChat() {+ const { default: ChatPage } = await import("./ChatPage");+ await render(+ <MemoryRouter initialEntries={["/chat"]}>+ <ChatPage isActive />+ </MemoryRouter>,+ );+ await vi.waitFor(() => expect(FakeWebSocket.instances).toHaveLength(1));+ return container.querySelector(".hermes-chat-xterm-host")!;+ }++ function textPasteEvent(text: string) {+ const paste = new Event("paste", { bubbles: true, cancelable: true });+ Object.defineProperty(paste, "clipboardData", {+ value: {+ files: [],+ getData: (type: string) => (type === "text/plain" ? text : ""),+ items: [],+ },+ });+ return paste;+ }++ it("delivers a native text paste through term.paste exactly once (#52471)", async () => {+ const host = await renderChat();+ const pasteSpy = vi.spyOn(FakeTerminal.prototype, "paste");+ try {+ const paste = textPasteEvent("abc");+ await act(async () => {+ host.dispatchEvent(paste);+ });+ // Exactly one delivery, via the terminal paste path that resets the+ // hidden textarea — a paste of "abc" yields "abc", never a stale-value+ // double send of the last character on the next keystroke.+ expect(pasteSpy).toHaveBeenCalledTimes(1);+ expect(pasteSpy).toHaveBeenCalledWith("abc");+ expect(paste.defaultPrevented).toBe(true);+ expect(uploadChatImage).not.toHaveBeenCalled();+ } finally {+ pasteSpy.mockRestore();+ }+ });++ it("keeps the image-paste route working: image pastes upload instead of pasting text", async () => {+ const host = await renderChat();+ const pasteSpy = vi.spyOn(FakeTerminal.prototype, "paste");+ try {+ const paste = new Event("paste", { bubbles: true, cancelable: true });+ const file = new File([new Uint8Array([1, 2, 3])], "shot.png", {+ type: "image/png",+ });+ Object.defineProperty(paste, "clipboardData", {+ value: {+ files: [file],+ getData: () => "",+ items: [{ getAsFile: () => file, kind: "file", type: "image/png" }],+ },+ });+ await act(async () => {+ host.dispatchEvent(paste);+ });+ await vi.waitFor(() => expect(uploadChatImage).toHaveBeenCalledTimes(1));+ expect(pasteSpy).not.toHaveBeenCalled();+ expect(paste.defaultPrevented).toBe(true);+ } finally {+ pasteSpy.mockRestore();+ }+ });+it("reconnects on tab return after a hidden-tab close even when a stale upload banner is showing", async () => {const { default: ChatPage } = await import("./ChatPage");await render(diff --git a/web/src/pages/ChatPage.tsx b/web/src/pages/ChatPage.tsxindex f5a931ffe5d..f876c366d1a 100644--- a/web/src/pages/ChatPage.tsx+++ b/web/src/pages/ChatPage.tsx@@ -115,6 +115,7 @@ import { PluginSlot } from "@/plugins";import { useTheme } from "@/themes";import { useProfileScope } from "@/contexts/useProfileScope";import { errorMessage } from "@/lib/api-error";+import { SGR_MOUSE_RE } from "@/lib/pty-mouse-report";// Per-tab keep-alive identity (`?attach=`): lives in pty-attach-token.ts so a// second tab — including a Chrome "Duplicate tab" — gets its own PTY instead of@@ -649,6 +650,7 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {const handleBrowserDropCleanup = attachChatImageDropListeners(host,uploadAndAttachImages,+ (text) => term.paste(text),);term.attachCustomKeyEventHandler((ev) => {@@ -1478,9 +1480,7 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {// mouse reporting, so we drop SGR mouse reports entirely instead of// forwarding them into Hermes. Keyboard input, paste, and resize still// behave normally.- // eslint-disable-next-line no-control-regex -- intentional ESC byte in xterm SGR mouse report parser- const SGR_MOUSE_RE = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])$/;- const forwardPtyData = (data: string, useMobileReplacement = true) => {+ const forwardPtyData = (data: string, useMobileReplacement = true) => {// Mouse reports (scroll wheel etc.) are not typed input — swallow// them before the blocked-input check so scrolling a disconnected// terminal doesn't trip the "reconnecting" notice.