Skip to content

The agent that grows with you

fix(tui): preserve IME text before return submit

InphinitiZ834a9fc89d49
diff --git a/ui-tui/src/__tests__/textInputReturnBurst.test.ts b/ui-tui/src/__tests__/textInputReturnBurst.test.ts
new file mode 100644
index 00000000000..8470623b821
--- /dev/null
+++ b/ui-tui/src/__tests__/textInputReturnBurst.test.ts
@@ -0,0 +1,25 @@
+import { describe, expect, it } from 'vitest'
+
+import { valueForReturnSubmit } from '../components/textInput.js'
+
+describe('valueForReturnSubmit', () => {
+ it('includes printable input that arrives in the same keypress as return', () => {
+ expect(valueForReturnSubmit('为什么打字上屏,', 8, '会丢失内容')).toEqual({
+ cursor: 13,
+ value: '为什么打字上屏,会丢失内容'
+ })
+ })
+
+ it('keeps IME commit text when it arrives in the same burst as return', () => {
+ expect(valueForReturnSubmit('为什么打字上屏,', 8, '会丢失内容\r')).toEqual({
+ cursor: 13,
+ value: '为什么打字上屏,会丢失内容'
+ })
+ })
+
+ it('leaves the draft unchanged when return carries no printable input', () => {
+ expect(valueForReturnSubmit('hello', 5, '')).toEqual({ cursor: 5, value: 'hello' })
+ expect(valueForReturnSubmit('hello', 5, '\r')).toEqual({ cursor: 5, value: 'hello' })
+ expect(valueForReturnSubmit('hello', 5, '\n')).toEqual({ cursor: 5, value: 'hello' })
+ })
+})
diff --git a/ui-tui/src/components/textInput.tsx b/ui-tui/src/components/textInput.tsx
index dc8cf3fac0a..423547ca8f9 100644
--- a/ui-tui/src/components/textInput.tsx
+++ b/ui-tui/src/components/textInput.tsx
@@ -157,6 +157,33 @@ export function applyPrintableInsert(
export const shouldRouteMultiCharInputAsPaste = (text: string): boolean => text.includes('\n')
+export function valueForReturnSubmit(
+ value: string,
+ cursor: number,
+ input: string,
+ range?: { end: number; start: number } | null
+): TextInsertResult {
+ const pending = input.replace(BRACKET_PASTE, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n')
+
+ if (!pending) {
+ return { cursor, value }
+ }
+
+ // Browser/xterm IME commits can arrive as one burst immediately followed by
+ // Return (for example "会丢失内容\r"). The Return keypath is already about to
+ // submit, but the committed text has not passed through the ordinary
+ // printable-input branch yet. Preserve the printable prefix before the first
+ // newline so the visible, just-committed IME text is part of the submitted
+ // prompt instead of being silently dropped.
+ const [beforeReturn] = pending.split('\n', 1)
+
+ if (!beforeReturn) {
+ return { cursor, value }
+ }
+
+ return applyPrintableInsert(value, cursor, beforeReturn, range) ?? { cursor, value }
+}
+
export function shouldPreserveCtrlJNewline(env: MinimalEnv = process.env): boolean {
if (env.WT_SESSION) {
return true
@@ -1163,13 +1190,15 @@ export function TextInput({
if (k.return) {
flushKeyBurst()
+ const range = selRange()
+ const pending = valueForReturnSubmit(vRef.current, curRef.current, inp, range)
const sequence = (event.keypress as { sequence?: string }).sequence
const preserveBareLineFeed = shouldPreserveCtrlJNewline() && sequence === '\n'
if (k.shift || k.ctrl || preserveBareLineFeed || (isMac ? isActionMod(k) : k.meta)) {
- commit(ins(vRef.current, curRef.current, '\n'), curRef.current + 1)
+ commit(ins(pending.value, pending.cursor, '\n'), pending.cursor + 1)
} else {
- cbSubmit.current?.(vRef.current)
+ cbSubmit.current?.(pending.value)
}
return