Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): stop the composer bridge from cancelling IME compositions

Nguyen Thanh Dat8f6272668d35
diff --git a/app/src/components/assistant-ui/thread.tsx b/app/src/components/assistant-ui/thread.tsx
index 66924b0b0b..0f55c3ffeb 100644
--- a/app/src/components/assistant-ui/thread.tsx
+++ b/app/src/components/assistant-ui/thread.tsx
@@ -350,6 +350,14 @@ const Composer: FC<{
};
}, []);
+ // DOM text -> composer store. Deferred by a microtask so the editor has
+ // finished applying the event before the DOM is read.
+ const syncComposerFromDom = (target: EventTarget | null) => {
+ if (!(target instanceof HTMLElement)) return;
+ const text = target.textContent ?? '';
+ globalThis.queueMicrotask(() => aui.composer.setText(text));
+ };
+
return (
<ComposerPrimitive.Unstable_TriggerPopoverRoot>
<ComposerPrimitive.Root
@@ -373,11 +381,23 @@ const Composer: FC<{
ref={inputWrapperRef}
placeholder="Send a message..."
onInputCapture={event => {
- const target = event.target;
- if (target instanceof HTMLElement) {
- const text = target.textContent ?? '';
- globalThis.queueMicrotask(() => aui.composer.setText(text));
+ // An IME fires `input` per keystroke while the candidate window is
+ // still open, and the text on the DOM then is the pre-edit, not the
+ // user's input. Writing it into the store re-renders the editor and
+ // cancels the composition, so `nihao` + Enter committed as
+ // `n ni nihao 你好` (#5763). The keydown guard below already refuses
+ // to act mid-composition; this bridge was the one that did not.
+ if ('isComposing' in event.nativeEvent && event.nativeEvent.isComposing) {
+ return;
}
+ syncComposerFromDom(event.target);
+ }}
+ onCompositionEndCapture={event => {
+ // Chromium emits a trailing `input` with `isComposing === false` that
+ // the handler above picks up; WebKit does not, so on Safari the
+ // committed text exists only here. Running in both is harmless -- the
+ // second write carries the same string.
+ syncComposerFromDom(event.target);
}}
onKeyDownCapture={event => {
if (event.key === 'Escape' && onEscape) {
diff --git a/app/src/pages/__tests__/Conversations.render.test.tsx b/app/src/pages/__tests__/Conversations.render.test.tsx
index 090a836be4..91189a06c0 100644
--- a/app/src/pages/__tests__/Conversations.render.test.tsx
+++ b/app/src/pages/__tests__/Conversations.render.test.tsx
@@ -380,6 +380,31 @@ function setComposerText(textarea: HTMLElement, text: string) {
fireEvent.input(textarea, { data: text, inputType: 'insertText' });
}
+/**
+ * Drive one IME composition the way a browser does: keystrokes arrive as `input`
+ * events carrying the PRE-EDIT text with `isComposing` set, then the commit lands
+ * on `compositionend`.
+ *
+ * `fireEvent.input` builds an `InputEvent` from these props, so `isComposing` is a
+ * real property on the native event rather than something the handler has to be
+ * told about.
+ */
+function typeImePreEdits(textarea: HTMLElement, preEdits: string[]) {
+ for (const preEdit of preEdits) {
+ textarea.textContent = preEdit;
+ fireEvent.input(textarea, {
+ data: preEdit,
+ inputType: 'insertCompositionText',
+ isComposing: true,
+ });
+ }
+}
+
+function commitIme(textarea: HTMLElement, committed: string) {
+ textarea.textContent = committed;
+ fireEvent.compositionEnd(textarea, { data: committed });
+}
+
// ── Tests ──────────────────────────────────────────────────────────────────
describe('Conversations — smoke render (#1123 welcome-lock removal)', () => {
@@ -960,6 +985,75 @@ describe('Conversations — smoke render (#1123 welcome-lock removal)', () => {
resolveSend?.();
});
+ // #5763. The DOM->store bridge on the composer fired on every `input`, and an
+ // IME emits one per keystroke carrying the PRE-EDIT text. In a browser the
+ // resulting store write re-renders the editor and cancels the composition, so
+ // `nihao` + Enter committed as `n ni nihao 你好`. jsdom has no real Lexical
+ // composition to cancel, so what it shows instead is the other half of the same
+ // fault: the committed text never arrives and the last pre-edit stands. Either
+ // way the composer must end up holding what the user committed.
+ it('does not push the pre-edit into the composer while an IME composition runs', async () => {
+ const { textarea } = await renderSelectedConversation();
+
+ await act(async () => {
+ typeImePreEdits(textarea, ['n', 'ni', 'nihao']);
+ });
+
+ // Nothing was committed, so the composer must still be empty. Before the fix
+ // each pre-edit keystroke was written straight into the store, which is the
+ // write that cancels the composition in a real browser (#5763).
+ await waitFor(() => {
+ expect(screen.queryByRole('button', { name: 'Send message' })).toBeNull();
+ });
+ });
+
+ it('takes the committed IME text when the composition ends', async () => {
+ const { textarea, thread } = await renderSelectedConversation();
+
+ await act(async () => {
+ typeImePreEdits(textarea, ['n', 'ni', 'nihao']);
+ commitIme(textarea, '你好');
+ });
+
+ await waitFor(() => {
+ expect(screen.getByRole('button', { name: 'Send message' })).not.toBeDisabled();
+ });
+ await act(async () => {
+ fireEvent.click(screen.getByRole('button', { name: 'Send message' }));
+ });
+
+ await waitFor(() => {
+ expect(chatSend).toHaveBeenCalledTimes(1);
+ });
+ expect(chatSend).toHaveBeenCalledWith(
+ expect.objectContaining({ threadId: thread.id, message: '你好' })
+ );
+ });
+
+ // The gate keys on `isComposing`, so ordinary typing must be untouched. This is
+ // the property the 22 existing composer tests depend on: in jsdom the bridge is
+ // the only path from a synthetic `input` to the store.
+ it('still syncs ordinary typing, which carries no composition flag', async () => {
+ const { textarea, thread } = await renderSelectedConversation();
+
+ await act(async () => {
+ setComposerText(textarea, 'plain text');
+ });
+ await waitFor(() => {
+ expect(screen.getByRole('button', { name: 'Send message' })).not.toBeDisabled();
+ });
+ await act(async () => {
+ fireEvent.click(screen.getByRole('button', { name: 'Send message' }));
+ });
+
+ await waitFor(() => {
+ expect(chatSend).toHaveBeenCalledTimes(1);
+ });
+ expect(chatSend).toHaveBeenCalledWith(
+ expect.objectContaining({ threadId: thread.id, message: 'plain text' })
+ );
+ });
+
it('cancels the in-flight generation when the in-composer Stop button is clicked', async () => {
let resolveSend: (() => void) | undefined;
vi.mocked(chatSend).mockImplementationOnce(