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

This commit is contained in:
James Babcock committed 2026-07-19 18:24:22 -07:00
1 parent 9b05c20c1b
commit 4401a4a416
2 files changed
+24 -3

No files matched your search

+20
View File
@@ -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" });
+4 -3
View File
@@ -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;
}