From 4401a4a416290904ff1aebe12f39c0f9ad0b23e5 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sun, 19 Jul 2026 18:24:22 -0700 Subject: [PATCH] Add a brief delay between terminal voice input text and the trailing newline, so that claude code doesn't interpret the newline as part of a paste --- app/native/g2mirror-client.ts | 20 ++++++++++++++++++++ app/workers/terminal-app.worker.ts | 7 ++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/native/g2mirror-client.ts b/app/native/g2mirror-client.ts index 5b7745e6..d5d48e37 100644 --- a/app/native/g2mirror-client.ts +++ b/app/native/g2mirror-client.ts @@ -13,6 +13,10 @@ declare const java: any; const PROTOCOL_VERSION = 1; const SESSION_LIST_REFRESH_MS = 3_000; +// Wrapper-side pause between submitted text and its trailing "\r". Apps that +// infer pasting from bytes arriving in one read (e.g. Claude Code) would +// otherwise treat the "\r" as a pasted newline instead of the Enter key. +const SUBMIT_DELAY_MS = 150; export type G2MirrorSession = { socket: string; @@ -240,6 +244,22 @@ export class G2MirrorClient { this.send({ type: "input", data: encodeBase64Utf8(text) }); } + /** + * Send `text` followed by Enter ("\r") as one input message, with a + * wrapper-side pause before the "\r" (the protocol's `delays` field) so the + * app reads it as a separate Enter keypress rather than the tail of a + * paste. Older wrappers ignore `delays`, so this degrades to sendInput. + */ + submitInput(text: string): void { + if (this.phase !== "attached") return; + const textByteLength = new java.lang.String(text).getBytes("UTF-8").length; + this.send({ + type: "input", + data: encodeBase64Utf8(`${text}\r`), + delays: [{ at: textByteLength, ms: SUBMIT_DELAY_MS }], + }); + } + unview(): void { if (this.phase !== "attached") return; this.send({ type: "unview" }); diff --git a/app/workers/terminal-app.worker.ts b/app/workers/terminal-app.worker.ts index 6aeee0b2..f47f6d8a 100644 --- a/app/workers/terminal-app.worker.ts +++ b/app/workers/terminal-app.worker.ts @@ -124,10 +124,11 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { } case "text-input": { const window = windows.get(message.windowId); - // Only attached view windows have a terminal to type into. Append "\r" - // (the Enter key) so voice-dictated text is submitted as a command. + // Only attached view windows have a terminal to type into. submitInput + // appends Enter ("\r") after a wrapper-side pause so paste-detecting + // apps (e.g. Claude Code) submit instead of inserting a newline. if (window && window.kind === "view") { - window.client.sendInput(`${message.text}\r`); + window.client.submitInput(message.text); } break; }