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(agentic-ide): make Ctrl+V paste into the terminal panes

Ruben Lütke518091d13040
diff --git a/jarvis/ui/web/frontend/src/components/agentic/terminalPaste.test.ts b/jarvis/ui/web/frontend/src/components/agentic/terminalPaste.test.ts
new file mode 100644
index 0000000000..8b68f78819
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentic/terminalPaste.test.ts
@@ -0,0 +1,214 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { installPasteBridge, isPasteChord } from "./terminalPaste";
+
+const DELAY = 140;
+
+/** Stand-in for the xterm terminal, recording what the bridge does to it. */
+function fakeTerminal(): {
+ attachCustomKeyEventHandler: (h: (e: KeyboardEvent) => boolean) => void;
+ paste: (text: string) => void;
+ press: (init: KeyboardEventInit) => boolean;
+ pasted: string[];
+} {
+ let handler: (event: KeyboardEvent) => boolean = () => true;
+ const pasted: string[] = [];
+ return {
+ attachCustomKeyEventHandler: (h) => {
+ handler = h;
+ },
+ paste: (text: string) => {
+ pasted.push(text);
+ },
+ press: (init: KeyboardEventInit) =>
+ handler(new KeyboardEvent("keydown", init)),
+ pasted,
+ };
+}
+
+/** jsdom has no ClipboardEvent — hand-build one with the parts we read. */
+function firePaste(
+ container: HTMLElement,
+ payload: { text?: string; file?: boolean },
+): void {
+ const event = new Event("paste", { bubbles: true });
+ Object.defineProperty(event, "clipboardData", {
+ value: {
+ getData: () => payload.text ?? "",
+ items: payload.file ? [{ kind: "file" }] : [],
+ },
+ });
+ container.dispatchEvent(event);
+}
+
+describe("recognising the paste chord", () => {
+ it("takes Ctrl+V away from Windows and Linux", () => {
+ const event = new KeyboardEvent("keydown", { key: "v", ctrlKey: true });
+ expect(isPasteChord(event, false)).toBe(true);
+ });
+
+ it("takes Cmd+V on a Mac, and leaves Ctrl+V there alone", () => {
+ const cmd = new KeyboardEvent("keydown", { key: "v", metaKey: true });
+ const ctrl = new KeyboardEvent("keydown", { key: "v", ctrlKey: true });
+ expect(isPasteChord(cmd, true)).toBe(true);
+ expect(isPasteChord(ctrl, true)).toBe(false);
+ });
+
+ it("also takes the traditional terminal binding Ctrl+Shift+V", () => {
+ const event = new KeyboardEvent("keydown", {
+ key: "V",
+ ctrlKey: true,
+ shiftKey: true,
+ });
+ expect(isPasteChord(event, false)).toBe(true);
+ });
+
+ it("leaves AltGr combinations alone — they type real characters", () => {
+ const event = new KeyboardEvent("keydown", {
+ key: "v",
+ ctrlKey: true,
+ altKey: true,
+ });
+ expect(isPasteChord(event, false)).toBe(false);
+ });
+
+ it("leaves a plain v and other control codes alone", () => {
+ expect(isPasteChord(new KeyboardEvent("keydown", { key: "v" }), false)).toBe(
+ false,
+ );
+ expect(
+ isPasteChord(
+ new KeyboardEvent("keydown", { key: "c", ctrlKey: true }),
+ false,
+ ),
+ ).toBe(false);
+ });
+});
+
+describe("pasting into a terminal pane", () => {
+ let container: HTMLDivElement;
+ let dispose: () => void;
+
+ beforeEach(() => {
+ vi.useFakeTimers();
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ dispose?.();
+ container.remove();
+ vi.useRealTimers();
+ });
+
+ /**
+ * The regression guard for the reported bug: xterm turns Ctrl+V into the
+ * control code ^V and cancels the event, which stops the browser from ever
+ * pasting. Returning false is what keeps xterm out of it.
+ */
+ it("takes the chord away from xterm, and nothing else", () => {
+ const term = fakeTerminal();
+ dispose = installPasteBridge(term, container, {
+ readClipboard: async () => "",
+ isMac: false,
+ });
+
+ expect(term.press({ key: "v", ctrlKey: true })).toBe(false);
+ expect(term.press({ key: "c", ctrlKey: true })).toBe(true);
+ expect(term.press({ key: "a" })).toBe(true);
+ });
+
+ it("pastes the operating system's clipboard when the browser stays silent", async () => {
+ const term = fakeTerminal();
+ dispose = installPasteBridge(term, container, {
+ readClipboard: async () => "npm run build",
+ isMac: false,
+ fallbackDelayMs: DELAY,
+ });
+
+ term.press({ key: "v", ctrlKey: true });
+ expect(term.pasted).toEqual([]); // still giving the browser its chance
+ await vi.advanceTimersByTimeAsync(DELAY);
+
+ expect(term.pasted).toEqual(["npm run build"]);
+ });
+
+ it("does not paste twice when the browser did deliver the text", async () => {
+ const term = fakeTerminal();
+ dispose = installPasteBridge(term, container, {
+ readClipboard: async () => "npm run build",
+ isMac: false,
+ fallbackDelayMs: DELAY,
+ });
+
+ term.press({ key: "v", ctrlKey: true });
+ firePaste(container, { text: "npm run build" });
+ await vi.advanceTimersByTimeAsync(DELAY);
+
+ expect(term.pasted).toEqual([]);
+ });
+
+ it("keeps out of the way when the clipboard carried a screenshot", async () => {
+ const term = fakeTerminal();
+ dispose = installPasteBridge(term, container, {
+ readClipboard: async () => "some unrelated text",
+ isMac: false,
+ fallbackDelayMs: DELAY,
+ });
+
+ term.press({ key: "v", ctrlKey: true });
+ firePaste(container, { file: true });
+ await vi.advanceTimersByTimeAsync(DELAY);
+
+ expect(term.pasted).toEqual([]);
+ });
+
+ it("says so when no path could read the clipboard", async () => {
+ const term = fakeTerminal();
+ const onUnavailable = vi.fn();
+ dispose = installPasteBridge(term, container, {
+ readClipboard: async () => null,
+ isMac: false,
+ fallbackDelayMs: DELAY,
+ onUnavailable,
+ });
+
+ term.press({ key: "v", ctrlKey: true });
+ await vi.advanceTimersByTimeAsync(DELAY);
+
+ expect(onUnavailable).toHaveBeenCalledOnce();
+ expect(term.pasted).toEqual([]);
+ });
+
+ it("treats an empty clipboard as nothing to do, not as a failure", async () => {
+ const term = fakeTerminal();
+ const onUnavailable = vi.fn();
+ dispose = installPasteBridge(term, container, {
+ readClipboard: async () => "",
+ isMac: false,
+ fallbackDelayMs: DELAY,
+ onUnavailable,
+ });
+
+ term.press({ key: "v", ctrlKey: true });
+ await vi.advanceTimersByTimeAsync(DELAY);
+
+ expect(onUnavailable).not.toHaveBeenCalled();
+ expect(term.pasted).toEqual([]);
+ });
+
+ it("stops reading the clipboard once the pane is gone", async () => {
+ const term = fakeTerminal();
+ dispose = installPasteBridge(term, container, {
+ readClipboard: async () => "late",
+ isMac: false,
+ fallbackDelayMs: DELAY,
+ });
+
+ term.press({ key: "v", ctrlKey: true });
+ dispose();
+ await vi.advanceTimersByTimeAsync(DELAY);
+
+ expect(term.pasted).toEqual([]);
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/components/agentic/terminalPaste.ts b/jarvis/ui/web/frontend/src/components/agentic/terminalPaste.ts
new file mode 100644
index 0000000000..37882f149d
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentic/terminalPaste.ts
@@ -0,0 +1,158 @@
+/**
+ * Making Ctrl+V / Cmd+V paste into a terminal pane.
+ *
+ * ## Why this file has to exist
+ *
+ * xterm.js emulates a real terminal, and in a real terminal Ctrl+<letter> is a
+ * CONTROL CODE, not an editing shortcut: `evaluateKeyboardEvent` turns Ctrl+V
+ * into character 22 (`^V`, "quote the next key") and `Terminal._keyDown` then
+ * cancels the browser event. Cancelling is what actually breaks pasting — the
+ * browser never runs its own paste, so no `paste` event is ever fired, and
+ * xterm's own (correct, bracketing) paste handler is never reached. The user
+ * presses Ctrl+V and nothing at all happens.
+ *
+ * That is right for a terminal on a Linux desktop, where paste is bound to
+ * Ctrl+Shift+V. It is wrong here: these panes are part of a desktop
+ * application, and every terminal people actually use on Windows and macOS
+ * (Windows Terminal, iTerm, the VS Code terminal) pastes on Ctrl+V / Cmd+V.
+ *
+ * ## How it is fixed, and why not more simply
+ *
+ * The browser's own paste is still the best path when it works: it needs no
+ * clipboard permission, it carries images as well as text, and it ends up in
+ * xterm's bracketed-paste handler, which is what a coding agent's TUI expects.
+ * So this bridge does not replace it — it stops xterm from swallowing the
+ * chord and lets the browser proceed.
+ *
+ * The embedded desktop WebView, however, does not always deliver that paste
+ * (it withholds clipboard access from the page; measured 2026-07-26:
+ * `navigator.clipboard.readText()` there never settles at all). A fix that
+ * only handed the chord back to the browser would therefore work in a browser
+ * and stay broken in the desktop app — which is where it was reported. Hence
+ * the second half: if no clipboard payload shows up within a short window, the
+ * text is read through the operating system instead and pasted directly.
+ *
+ * The two paths cannot both run for one keystroke. A browser executes the
+ * paste as the key event's default action, in the same task as the keydown, so
+ * the `paste` event always arrives long before the fallback timer — and when
+ * it arrives it disarms the timer.
+ */
+
+/** The slice of an xterm terminal this bridge drives. */
+export interface PasteCapableTerminal {
+ attachCustomKeyEventHandler(handler: (event: KeyboardEvent) => boolean): void;
+ /** Write text into the terminal as a paste (bracketed where supported). */
+ paste(text: string): void;
+}
+
+export interface PasteBridgeOptions {
+ /**
+ * Read the clipboard when the embedded browser would not.
+ *
+ * `null` means no path could read it at all; `""` means the clipboard is
+ * genuinely empty, which is not an error and must stay distinguishable.
+ */
+ readClipboard: () => Promise<string | null>;
+ /** True on macOS, where the paste chord is Cmd+V rather than Ctrl+V. */
+ isMac: boolean;
+ /** How long to wait for the browser's own paste before reading the OS. */
+ fallbackDelayMs?: number;
+ /** The clipboard could not be read on any path — worth telling the user. */
+ onUnavailable?: () => void;
+}
+
+/**
+ * Long enough that a browser paste always wins the race (it happens in the
+ * same task as the keydown), short enough to feel instant.
+ */
+const DEFAULT_FALLBACK_DELAY_MS = 140;
+
+/**
+ * Is this the keystroke the user means as "paste"?
+ *
+ * Matched on `key` rather than the physical `code` so it follows the keyboard
+ * LAYOUT — on AZERTY or Dvorak, paste is the key that prints "v", not the key
+ * sitting where a US keyboard has one.
+ *
+ * Alt is excluded on purpose: on many European layouts AltGr arrives as
+ * Ctrl+Alt, and those combinations type real characters.
+ *
+ * Ctrl+Shift+V (Cmd+Shift+V) counts too — the traditional terminal binding,
+ * which people coming from Linux will reach for first.
+ */
+export function isPasteChord(event: KeyboardEvent, isMac: boolean): boolean {
+ if (event.altKey) return false;
+ if ((event.key || "").toLowerCase() !== "v") return false;
+ return isMac
+ ? event.metaKey && !event.ctrlKey
+ : event.ctrlKey && !event.metaKey;
+}
+
+/**
+ * Wire the paste chord into `term`, returning a cleanup function.
+ *
+ * The clipboard listener is registered in the CAPTURE phase deliberately.
+ * xterm's own paste handler calls `stopPropagation()`, so anything listening
+ * on an ancestor in the normal (bubbling) phase never runs at all — capture
+ * travels downwards and reaches this container before xterm can stop it.
+ */
+export function installPasteBridge(
+ term: PasteCapableTerminal,
+ container: HTMLElement,
+ options: PasteBridgeOptions,
+): () => void {
+ const delay = options.fallbackDelayMs ?? DEFAULT_FALLBACK_DELAY_MS;
+ // Bumped for every chord and every delivered paste, so a timer that fires
+ // after its keystroke was already served can tell that it is stale.
+ let generation = 0;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+
+ const disarm = (): void => {
+ generation += 1;
+ if (timer !== undefined) {
+ clearTimeout(timer);
+ timer = undefined;
+ }
+ };
+
+ const onPasteCapture = (event: ClipboardEvent): void => {
+ const data = event.clipboardData;
+ if (!data) return;
+ // Files count as well as text: a pasted screenshot is handled by the pane's
+ // own attach handler, and reading the OS clipboard afterwards would append
+ // whatever text happened to sit beside the image.
+ const carriesText = data.getData("text/plain") !== "";
+ const carriesFile = Array.from(data.items ?? []).some(
+ (item) => item.kind === "file",
+ );
+ if (carriesText || carriesFile) disarm();
+ };
+
+ term.attachCustomKeyEventHandler((event) => {
+ if (event.type !== "keydown") return true;
+ if (!isPasteChord(event, options.isMac)) return true;
+ // `false` means: xterm, keep your hands off this one. It returns before
+ // both the `^V` data event and the `cancel()` that suppresses the browser's
+ // own paste — which is the whole point.
+ disarm();
+ const mine = generation;
+ timer = setTimeout(() => {
+ timer = undefined;
+ void (async () => {
+ const text = await options.readClipboard();
+ // A browser paste landed while the clipboard was being read — it has
+ // already inserted the text, so inserting it again would double it.
+ if (generation !== mine) return;
+ if (text === null) options.onUnavailable?.();
+ else if (text !== "") term.paste(text);
+ })();
+ }, delay);
+ return false;
+ });
+
+ container.addEventListener("paste", onPasteCapture, true);
+ return () => {
+ disarm();
+ container.removeEventListener("paste", onPasteCapture, true);
+ };
+}