Skip to content

The agent that grows with you

fix(dashboard): forward committed dead-key input to PTY

Denis H6ece2da7389d
diff --git a/web/src/lib/pty-composition.ts b/web/src/lib/pty-composition.ts
new file mode 100644
index 00000000000..b44758a7970
--- /dev/null
+++ b/web/src/lib/pty-composition.ts
@@ -0,0 +1,25 @@
+/**
+ * Sends committed IME/dead-key text when xterm does not emit onData.
+ *
+ * Some browser/layout combinations leave xterm's CompositionHelper without an
+ * onData callback. The DOM compositionend event is the authoritative commit.
+ * If xterm does emit the same bytes afterwards, consume that one duplicate.
+ */
+export function createPtyCompositionForwarder(send: (data: string) => void) {
+ let pendingDuplicate: string | null = null;
+
+ return {
+ onCompositionEnd(data: string) {
+ if (!data) return;
+ pendingDuplicate = data;
+ send(data);
+ },
+ shouldForwardTerminalData(data: string) {
+ if (data === pendingDuplicate) {
+ pendingDuplicate = null;
+ return false;
+ }
+ return true;
+ },
+ };
+}
diff --git a/web/src/pages/ChatPage.tsx b/web/src/pages/ChatPage.tsx
index c97d247cc23..1078b5fecad 100644
--- a/web/src/pages/ChatPage.tsx
+++ b/web/src/pages/ChatPage.tsx
@@ -36,6 +36,7 @@ import { usePageHeader } from "@/contexts/usePageHeader";
import { useI18n } from "@/i18n";
import { api } from "@/lib/api";
import { latchChatActivation } from "@/lib/chat-activation";
+import { createPtyCompositionForwarder } from "@/lib/pty-composition";
import { normalizeSessionTitle } from "@/lib/chat-title";
import { PtyResumeSanitizer } from "@/lib/pty-resume-sanitizer";
import {
@@ -746,6 +747,12 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
term.loadAddon(new WebLinksAddon());
let mobileInputCleanup: (() => void) | null = null;
+ // xterm occasionally drops committed dead-key/IME text instead of emitting
+ // onData. The compositionend event supplies the authoritative text.
+ let sendComposedText: (data: string) => void = () => undefined;
+ const compositionForwarder = createPtyCompositionForwarder((data) => {
+ sendComposedText(data);
+ });
term.open(host);
// IME composition guard (fixes #52111).
@@ -794,8 +801,9 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
mobileReplacementInputUntilRef.current = Date.now() + MOBILE_REPLACEMENT_WINDOW_MS;
}
};
- const markCompositionEnd = () => {
+ const markCompositionEnd = (ev: Event) => {
mobileReplacementInputUntilRef.current = Date.now() + MOBILE_REPLACEMENT_WINDOW_MS;
+ compositionForwarder.onCompositionEnd((ev as CompositionEvent).data);
};
textarea.addEventListener("beforeinput", markReplacementInput, true);
@@ -1270,7 +1278,7 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
// 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])$/;
- onDataDisposable = term.onData((data) => {
+ const forwardPtyData = (data: string) => {
// 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.
@@ -1301,6 +1309,12 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
mobileReplacementInputUntilRef.current = 0;
}
ws.send(normalized.data);
+ };
+ sendComposedText = forwardPtyData;
+ onDataDisposable = term.onData((data) => {
+ if (compositionForwarder.shouldForwardTerminalData(data)) {
+ forwardPtyData(data);
+ }
});
onResizeDisposable = term.onResize(({ cols, rows }) => {