Files
Netcatty/components/terminal/runtime/terminalPerCharacterInput.ts
T
bincxzandClaude d809b6c3fa fix(terminal): send IME commits and short pastes per character
Strict bastion prompts (QAX/奇安信) treat one SSH channel write as a single keystroke and silently drop every multi-character chunk, so an IME commit of two or more hanzi and a Chinese paste both reach the bastion's search prompt as nothing at all. Single characters work, which is why typing them one at a time is the only path that works today.

Split plain multi-character text at the send boundary instead of adding a setting: the write side of handleTerminalInputData now iterates one chunk per Unicode code point, and all bookkeeping (command buffer, script recorder, autocomplete, sudo autofill, broadcast, scroll) still sees the original payload exactly once. IME commits split whenever the committed text has more than one code point; raw (non-bracketed) onData splits up to 32 characters and longer pastes keep the single write. Anything containing an escape sequence - bracketed paste wrappers, CSI-u composition, forwarded Kitty key sequences, cursor reports - is never split, and broadcast peers still receive the original string once.

Each chunk carries the same writeToSession options as the original single write, so the sensitive marker and automation flags are kept per character. Bridge writes stay one channel write per IPC call, which reproduces what Xshell sends on the same bastion (#3077).

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-27 14:35:27 +08:00

45 lines
1.8 KiB
TypeScript

/**
* Strict bastion prompts (e.g. QAX/奇安信) treat one SSH channel write as a
* single keystroke and silently drop every multi-character chunk (#3077). IME
* commits and short raw pastes therefore have to leave the renderer as
* per-character writes, which is what typing produces and what Xshell does.
*/
/** Raw pastes longer than this keep the single-write behavior. */
export const MAX_RAW_PASTE_PER_CHARACTER_LENGTH = 32;
const ESC = "\x1b";
/** True while the payload only carries plain text and no escape sequence. */
export const isPlainTerminalInputText = (data: string): boolean => !data.includes(ESC);
/** One chunk per Unicode code point so surrogate pairs stay intact. */
export const splitTextIntoCodePointWrites = (data: string): string[] => Array.from(data);
/** IME commits: any plain text with more than one character splits per glyph. */
export const shouldSplitImeTextInputForWire = (text: string): boolean =>
Array.from(text).length > 1 && isPlainTerminalInputText(text);
/**
* Raw (non-bracketed) paste: short plain text goes out as keystrokes, longer
* pastes keep the single write so bulk pastes do not degrade.
*/
export const shouldSplitRawPasteInputForWire = (data: string): boolean => {
if (data.length <= 1 || !isPlainTerminalInputText(data)) return false;
let codePoints = 0;
for (let index = 0; index < data.length; ) {
index += (data.codePointAt(index) ?? 0) > 0xffff ? 2 : 1;
codePoints += 1;
if (codePoints > MAX_RAW_PASTE_PER_CHARACTER_LENGTH) return false;
}
return true;
};
/** Chunks to write for one input payload; escape sequences are never split. */
export const getTextInputWireChunks = (
data: string,
perCharacterWrites: boolean,
): string[] =>
perCharacterWrites && isPlainTerminalInputText(data)
? splitTextIntoCodePointWrites(data)
: [data];