fix(native-chat): adopt IME deletions at composition end (#12281)

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
Co-authored-by: fengxinzi1814 <55725821+fengxinzi1814@users.noreply.github.com>
This commit is contained in:
OrcaWin
2026-08-03 03:16:01 -07:00
committed by Neil
co-authored by OrcaWin fengxinzi1814
parent c5a027439c
commit 0bd2d463e3
4 changed files with 85 additions and 16 deletions
@@ -14,6 +14,8 @@ const mocks = vi.hoisted(() => ({
fieldProps: null as {
onSend?: () => void
onStop?: () => void
onCompositionStart?: () => void
onCompositionEnd?: (event: { currentTarget: HTMLTextAreaElement }) => void
sessionOptionsSurface?: SessionOptionsSurface | null
sessionOptionsSnapshot?: SessionOptionDescriptor[]
} | null,
@@ -251,6 +253,46 @@ describe('NativeChatComposer', () => {
expect(new Set(mocks.draftScopeKeys)).toEqual(new Set(['tab-1:leaf-1']))
})
it('adopts an IME deletion delivered only by compositionend', () => {
render(
<NativeChatComposer
terminalTabId="tab-1"
paneKey="tab-1:leaf-1"
targetPtyId="pty-1"
agent="codex"
/>
)
const textarea = document.createElement('textarea')
textarea.value = ''
mocks.setDraft.mockClear()
act(() => {
mocks.fieldProps?.onCompositionStart?.()
mocks.fieldProps?.onCompositionEnd?.({ currentTarget: textarea })
})
expect(mocks.setDraft).toHaveBeenCalledOnce()
expect(mocks.setDraft).toHaveBeenCalledWith('')
})
it('does not duplicate a composition value already adopted by onChange', () => {
render(
<NativeChatComposer
terminalTabId="tab-1"
paneKey="tab-1:leaf-1"
targetPtyId="pty-1"
agent="codex"
/>
)
const textarea = document.createElement('textarea')
textarea.value = 'hello'
mocks.setDraft.mockClear()
act(() => mocks.fieldProps?.onCompositionEnd?.({ currentTarget: textarea }))
expect(mocks.setDraft).not.toHaveBeenCalled()
})
it('shows the model already selected in the Claude TUI when chat opens', async () => {
mocks.getMainBufferSnapshot.mockResolvedValue({
data: 'Claude Code v2.1.211\r\nOpus 4.8 with medium effort · API Usage Billing',
@@ -375,6 +375,17 @@ export const NativeChatComposer = forwardRef<NativeChatComposerHandle, NativeCha
setHistory
})
const handleDraftChange = useCallback(
(value: string, element: HTMLTextAreaElement) => {
setDraft(value)
setHistory((prev) => ({ entries: prev.entries, index: null }))
syncCaret(element)
handleDraftOrCaretChange(value, element.selectionStart ?? value.length)
setActiveSuggestion(0)
},
[handleDraftOrCaretChange, setDraft, syncCaret]
)
return (
<NativeChatComposerField
textareaRef={textareaRef}
@@ -392,13 +403,7 @@ export const NativeChatComposer = forwardRef<NativeChatComposerHandle, NativeCha
dictationDisabled={dictationDisabled}
isDictating={isDictating}
isDictationHoldMode={isDictationHoldMode}
onDraftChange={(value, element) => {
setDraft(value)
setHistory((prev) => ({ entries: prev.entries, index: null }))
syncCaret(element)
handleDraftOrCaretChange(value, element.selectionStart ?? value.length)
setActiveSuggestion(0)
}}
onDraftChange={handleDraftChange}
onTextareaSelect={(element) => {
syncCaret(element)
handleDraftOrCaretChange(element.value, element.selectionStart ?? element.value.length)
@@ -408,8 +413,11 @@ export const NativeChatComposer = forwardRef<NativeChatComposerHandle, NativeCha
onCompositionStart={() => {
isComposingRef.current = true
}}
onCompositionEnd={() => {
onCompositionEnd={(event) => {
isComposingRef.current = false
if (event.currentTarget.value !== draft) {
handleDraftChange(event.currentTarget.value, event.currentTarget)
}
}}
onPaste={handlePaste}
pickerListboxId={picker.listboxId}
@@ -14,6 +14,7 @@ function nextEventLoop(): Promise<void> {
}
function openTerminal(TerminalType: typeof EsmTerminal): {
compositionView: HTMLElement
emitted: string[]
textarea: HTMLTextAreaElement
} {
@@ -25,9 +26,13 @@ function openTerminal(TerminalType: typeof EsmTerminal): {
if (!terminal.textarea) {
throw new Error('xterm textarea was not created')
}
const compositionView = container.querySelector<HTMLElement>('.composition-view')
if (!compositionView) {
throw new Error('xterm composition view was not created')
}
const emitted: string[] = []
terminal.onData((data) => emitted.push(data))
return { emitted, textarea: terminal.textarea }
return { compositionView, emitted, textarea: terminal.textarea }
}
function composition(
@@ -124,6 +129,21 @@ describe.each([
expect(emitted.join('')).toBe('한글')
})
it('shows 가나다 before the final macOS composition commits', async () => {
const { compositionView, emitted, textarea } = openTerminal(TerminalType)
composeSyllable(textarea, '', ['ㄱ', '가'])
composition(textarea, 'compositionend', '가')
composeSyllable(textarea, '가', ['ㄴ', '나'])
await nextEventLoop()
composition(textarea, 'compositionend', '나')
composeSyllable(textarea, '가나', ['ㄷ', '다'])
await nextEventLoop()
expect(emitted.join('')).toBe('가나')
expect(compositionView.classList.contains('active')).toBe(true)
expect(compositionView.textContent).toContain('다')
})
it('does not double-send after an IBus insertText commit', async () => {
const { emitted, textarea } = openTerminal(TerminalType)
textarea.setSelectionRange(0, 0)
@@ -96,16 +96,15 @@ describe('xterm IME composition cancellation', () => {
terminal.dispose()
})
it('emits nothing when the preedit is cancelled without observed input events', async () => {
it('drops a Sogou preedit cancelled without a trailing input event', async () => {
const { emitted, terminal, textarea } = openTerminal()
dispatchCompositionEvent(textarea, 'compositionstart')
dispatchCompositionEvent(textarea, 'compositionupdate', 'ce')
textarea.value = 'ce'
await nextEventLoop()
dispatchCompositionEvent(textarea, 'compositionupdate', 'c')
textarea.value = 'c'
await nextEventLoop()
for (const preedit of ['nihao', 'niha', 'nih', 'ni', 'n']) {
dispatchCompositionEvent(textarea, 'compositionupdate', preedit)
textarea.value = preedit
await nextEventLoop()
}
textarea.value = ''
dispatchCompositionEvent(textarea, 'compositionend')
await nextEventLoop()