desktop: Chinese IME composition loses trailing characters in the prompt composer
Description
Summary
Typing Chinese with 讯飞输入法 (iFlytek IME) in the OpenCode desktop prompt composer intermittently loses the trailing characters that are still in the IME composition buffer. After typing a segment and continuing, the last few pinyin/candidate characters disappear before they are selected or committed. Previously committed text remains. This happens often enough to make Chinese input unusable.
Expected behavior
The IME composition stays active until a candidate is selected/committed; no composing characters are discarded.
Actual behavior
The trailing characters of the active composition vanish. The composition is aborted and the uncommitted pinyin/candidate text is lost; earlier committed text is not affected.
Root cause analysis
This matches the known V2 composer IME issue: the contenteditable editor handles every input/key event during composition and can rewrite its DOM mid-composition.
packages/app/src/composer/editor/editor.tsxrunsonInputfor every input event, including composition updates, and writes the parsed DOM into the composer store.onKeyUpsyncs the cursor on every key, including composition keys.- A
createEffectcompares the DOM with the store and callsrenderComposerEditor()(editor.replaceChildren(...)) when they differ. - Mutating the DOM or selection during an active IME composition makes the browser abort the composition and discard the uncommitted text.
Related
- #39632: IME composition breaks on the first keystroke in the v2 prompt input
- #38674: CJK IME composition aborted mid-input (DOM rewrite during composition)
- #55295: fix(app): keep the composer editor inert during IME composition (closed unmerged; commit f59b00c is not in 2.4.2)
Workaround
Typing Chinese in an external editor and pasting it into the composer works; pasting bypasses IME composition.
Notes
- Hex voice input is not installed/running on this machine, so its Ctrl+Space hotkey is not involved.
- No relevant errors in the logs; this is a DOM/IME lifecycle issue, not a server error.
Plugins
superpowers@git+https://github.com/obra/superpowers.git
OpenCode version
2.4.2 (desktop app; bundled CLI 2.4.2)
Steps to reproduce
- Open a new session in the OpenCode desktop app (2.4.2).
- Focus the prompt composer and switch 讯飞输入法 (iFlytek IME) to Chinese mode.
- Type and commit a short phrase.
- Continue typing another phrase without confirming the IME candidates.
- After a few more keystrokes, the trailing characters in the composition disappear before candidate selection. Earlier committed text remains.
Intermittent but frequent; longer compositions trigger it more often.
Screenshot
No screenshot available (intermittent text loss; textual reproduction above).
Operating System
Windows 10.0.19045 (win32 x64)
Terminal
OpenCode Desktop app (not a terminal client)
Thanks for the detailed report and the steps, @bobcy2015.
I couldn't try this here. It needs the Windows desktop app and a real Chinese IME like 讯飞输入法 composing in the prompt composer, and my sandbox has neither. A maintainer with that setup will need to check it.
I read the composer editor code (packages/app/src/composer/editor/editor.tsx). On every input event it parses the editor's content into its state, and I didn't find any handling of compositionstart or compositionend there. That fits your analysis, but it doesn't confirm the bug by itself.
Related issues and PRs:
- #39632 and #38674: IME composition interrupted in the older prompt input. Both were V1 reports and are closed as not planned.
- #41647: first Chinese character lost with an IME on the macOS desktop app (closed).
- #55295: a V2 change to leave the editor alone during IME composition. It was closed without being merged.
Until this is looked at, typing the text elsewhere and pasting it into the composer is the workaround you already found.
reproduced this at the code level — i can't run the windows desktop app with a real ime here, so no live repro, but the mechanism checks out.
the composer editor has no compositionstart/compositionend handling at all. onInput parses the dom into the store on every input event, including composition updates, and the render effect rewrites the editor dom (replaceChildren) whenever it differs from the store. mutating the dom or the selection mid-composition makes the browser abort it, which is exactly the trailing characters disappearing that you describe. the earlier v2 fix for this (#55295) was closed by automated cleanup without merging, so the bug is still present in dev.
i put up a fix on a branch (can't open the pr from here — github is rejecting pr creation for my account right now, the branch is at https://github.com/anomalyco/opencode/compare/dev...fanyzp:composer-ime-guard): the editor stays inert while a composition is in flight — onInput is gated, keydown dispatch and cursor sync are skipped, the render effect doesn't rewrite the dom, and state is reconciled once from the dom on compositionend. it also covers the safari case where compositionend fires before the confirming enter. regression tests are in packages/app/test-browser/composer-ime.test.ts; they fail on the current code and pass with the fix.
if a maintainer can try it with a real ime on windows, that would confirm the live behavior.