Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

fix(studio): allow Enter to send with idle macOS Pinyin IME (#12138)

Sourav Rajvi05d2b8dbb0d3
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 34eac6c25c3..8067dd8fa9c 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -63,7 +63,9 @@ import {
composerSubmitIntent,
composerFollowUpBehavior,
composerShortcutLabels,
+ composerKeyEventForImeSubmit,
effectiveSendShortcut,
+ imeKeydownBlocksComposerSubmit,
followUpSubmitIntent,
steeringInsertionIndex,
cancelPreStreamRunForThreadIds,
@@ -5642,6 +5644,8 @@ function useImeComposerInputHandlers({
} = {}) {
const aui = useAui();
const composingRef = useRef(false);
+ const imeSessionOpenRef = useRef(false);
+ const compositionEndedAtRef = useRef(-Infinity);
const [isComposing, setIsComposing] = useState(false);
const stuckTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -5724,6 +5728,7 @@ function useImeComposerInputHandlers({
if (justSentRef) {
justSentRef.current = markSentTextGuardUserInput(justSentRef.current);
}
+ imeSessionOpenRef.current = true;
setCompositionState(true);
}, [justSentRef, setCompositionState]);
@@ -5733,6 +5738,8 @@ function useImeComposerInputHandlers({
const onCompositionEnd = useCallback(
(e: CompositionEvent<HTMLTextAreaElement>) => {
+ imeSessionOpenRef.current = false;
+ compositionEndedAtRef.current = e.timeStamp;
setCompositionState(false);
if (!setComposerText(e.currentTarget.value, e.nativeEvent)) {
e.preventDefault();
@@ -5760,13 +5767,25 @@ function useImeComposerInputHandlers({
// forever and block Send again.
const onKeyDown = useCallback(
(e: KeyboardEvent<HTMLTextAreaElement>) => {
- if (e.nativeEvent.isComposing || e.keyCode === 229) {
- // Deliberately NOT user input: picking a candidate in a composition the
- // send left open is that composition continuing. One begun after the
- // send is marked by compositionstart instead.
- composingRef.current = true;
- refreshStuckTimer();
- return;
+ const msSinceCompositionEnd = e.timeStamp - compositionEndedAtRef.current;
+ compositionEndedAtRef.current = -Infinity;
+ const imeKey = e.nativeEvent.isComposing || e.keyCode === 229;
+ if (imeKey) {
+ if (
+ imeKeydownBlocksComposerSubmit(
+ e,
+ imeSessionOpenRef.current,
+ msSinceCompositionEnd,
+ )
+ ) {
+ // Deliberately NOT user input: picking a candidate in a composition the
+ // send left open is that composition continuing. One begun after the
+ // send is marked by compositionstart instead.
+ composingRef.current = true;
+ refreshStuckTimer();
+ return;
+ }
+ setCompositionState(false);
}
if (justSentRef && isGuardRetiringKey(e)) {
justSentRef.current = markSentTextGuardUserInput(justSentRef.current);
@@ -5788,7 +5807,11 @@ function useImeComposerInputHandlers({
setCompositionState(false);
}
if (submitOnEnter && !skipEnterRef?.current) {
- const intent = composerSubmitIntent(e, sendShortcut, e.currentTarget?.value);
+ const intent = composerSubmitIntent(
+ imeKey ? composerKeyEventForImeSubmit(e) : e,
+ sendShortcut,
+ e.currentTarget?.value,
+ );
if (intent) {
e.preventDefault();
if (onSubmitKey) onSubmitKey(e, intent);
@@ -5813,6 +5836,7 @@ function useImeComposerInputHandlers({
// commits or cancels any in-progress composition before surrendering focus,
// so blur is a safe unconditional reset point.
const onBlur = useCallback(() => {
+ imeSessionOpenRef.current = false;
setCompositionState(false);
}, [setCompositionState]);
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index 197cd59802b..f81bc57314b 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -530,8 +530,10 @@ export {
composerSubmitIntent,
composerFollowUpBehavior,
composerShortcutLabels,
+ composerKeyEventForImeSubmit,
effectiveSendShortcut,
followUpSubmitIntent,
+ imeKeydownBlocksComposerSubmit,
steeringInsertionIndex,
type ComposerSendShortcut,
type ComposerFollowUpBehavior,
diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsx
index b4fe427a3d4..b919c159185 100644
--- a/studio/frontend/src/features/chat/shared-composer.tsx
+++ b/studio/frontend/src/features/chat/shared-composer.tsx
@@ -20,6 +20,8 @@ import { useChatPreferencesStore } from "./stores/chat-preferences-store";
import {
composerSubmitIntent,
composerShortcutLabels,
+ composerKeyEventForImeSubmit,
+ imeKeydownBlocksComposerSubmit,
} from "./utils/composer-preferences";
import { Button } from "@/components/ui/button";
import { Spinner } from "@/components/ui/spinner";
@@ -723,6 +725,8 @@ export function SharedComposer({
window.setTimeout(() => textareaRef.current?.focus(), 0);
}, [pendingFixPrompt, setCurrentText]);
const composingRef = useRef(false);
+ const imeSessionOpenRef = useRef(false);
+ const compositionEndedAtRef = useRef(-Infinity);
const stuckImeTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const audioInputRef = useRef<HTMLInputElement>(null);
@@ -2221,13 +2225,25 @@ export function SharedComposer({
const busy = running || comparing;
function onKeyDown(e: KeyboardEvent) {
+ const msSinceCompositionEnd = e.timeStamp - compositionEndedAtRef.current;
+ compositionEndedAtRef.current = -Infinity;
// IME composition (JP/CN/KR): Enter commits the candidate, so do not hijack it (#5318). Re-pin
// composingRef in case the stuck watchdog (#5546) cleared it during a long candidate-window pause,
// and re-arm the watchdog on the same path, or the WSL+Chrome no-compositionend case pins it forever.
- if (e.nativeEvent.isComposing || e.keyCode === 229) {
- composingRef.current = true;
- refreshStuckImeTimer();
- return;
+ const imeKey = e.nativeEvent.isComposing || e.keyCode === 229;
+ if (imeKey) {
+ if (
+ imeKeydownBlocksComposerSubmit(
+ e,
+ imeSessionOpenRef.current,
+ msSinceCompositionEnd,
+ )
+ ) {
+ composingRef.current = true;
+ refreshStuckImeTimer();
+ return;
+ }
+ setCompositionState(false);
}
// Non-IME key while composingRef is stuck; mirrors the fix in thread.tsx. On macOS, switching
// input methods without composing can leave composingRef pinned.
@@ -2242,7 +2258,13 @@ export function SharedComposer({
}
setCompositionState(false);
}
- if (composerSubmitIntent(e, sendShortcut, text)) {
+ if (
+ composerSubmitIntent(
+ imeKey ? composerKeyEventForImeSubmit(e) : e,
+ sendShortcut,
+ text,
+ )
+ ) {
e.preventDefault();
if (!busy && !isDictating) {
send();
@@ -2671,12 +2693,15 @@ export function SharedComposer({
);
}}
onCompositionStart={() => {
+ imeSessionOpenRef.current = true;
setCompositionState(true);
}}
onCompositionUpdate={() => {
refreshStuckImeTimer();
}}
onCompositionEnd={(e: CompositionEvent<HTMLTextAreaElement>) => {
+ imeSessionOpenRef.current = false;
+ compositionEndedAtRef.current = e.timeStamp;
setCompositionState(false);
setCurrentText(e.currentTarget.value);
}}
@@ -2687,6 +2712,7 @@ export function SharedComposer({
onBlur={() => {
// Mac: switching input methods can fire compositionstart without a matching compositionend,
// leaving composingRef pinned. The OS always commits or cancels before focus is lost.
+ imeSessionOpenRef.current = false;
setCompositionState(false);
skillMentions.close();
diff --git a/studio/frontend/src/features/chat/utils/composer-preferences.ts b/studio/frontend/src/features/chat/utils/composer-preferences.ts
index 75508f71f2e..f6f66093567 100644
--- a/studio/frontend/src/features/chat/utils/composer-preferences.ts
+++ b/studio/frontend/src/features/chat/utils/composer-preferences.ts
@@ -17,6 +17,37 @@ export type ComposerKeyEvent = {
keyCode?: number;
};
+// WebKit fires compositionend before the committing keydown (keyCode 229, WebKit bug 165004); ProseMirror's window.
+const IME_COMMIT_KEYDOWN_MS = 500;
+
+/** False only for a plain IME-marked Enter outside any composition, e.g. idle macOS Pinyin (#12137). */
+export function imeKeydownBlocksComposerSubmit(
+ event: ComposerKeyEvent,
+ imeSessionOpen: boolean,
+ msSinceCompositionEnd: number,
+): boolean {
+ return (
+ event.key !== "Enter" ||
+ event.metaKey ||
+ event.ctrlKey ||
+ imeSessionOpen ||
+ msSinceCompositionEnd < IME_COMMIT_KEYDOWN_MS
+ );
+}
+
+export function composerKeyEventForImeSubmit(
+ event: ComposerKeyEvent,
+): ComposerKeyEvent {
+ return {
+ key: event.key,
+ metaKey: event.metaKey,
+ ctrlKey: event.ctrlKey,
+ shiftKey: event.shiftKey,
+ altKey: event.altKey,
+ repeat: event.repeat,
+ };
+}
+
/** The rule in force for `draft`. */
export function effectiveSendShortcut(
shortcut: ComposerSendShortcut,
diff --git a/studio/frontend/tests/composer-ime-enter.test.ts b/studio/frontend/tests/composer-ime-enter.test.ts
new file mode 100644
index 00000000000..6e82d1518ac
--- /dev/null
+++ b/studio/frontend/tests/composer-ime-enter.test.ts
@@ -0,0 +1,38 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import {
+ composerKeyEventForImeSubmit,
+ composerSubmitIntent,
+ imeKeydownBlocksComposerSubmit,
+} from "../src/features/chat/utils/composer-preferences.ts";
+
+const imeEnter = {
+ key: "Enter",
+ metaKey: false,
+ ctrlKey: false,
+ shiftKey: false,
+ altKey: false,
+ isComposing: false,
+ keyCode: 229,
+};
+
+test("idle macOS Pinyin Enter submits (#12137)", () => {
+ assert.equal(imeKeydownBlocksComposerSubmit(imeEnter, false, Infinity), false);
+ assert.equal(
+ composerSubmitIntent(composerKeyEventForImeSubmit(imeEnter), "enter", "nihao"),
+ "default",
+ );
+});
+
+test("IME-owned keydowns stay blocked", () => {
+ assert.equal(imeKeydownBlocksComposerSubmit(imeEnter, true, Infinity), true);
+ assert.equal(imeKeydownBlocksComposerSubmit(imeEnter, false, 5), true);
+ assert.equal(
+ imeKeydownBlocksComposerSubmit({ ...imeEnter, metaKey: true }, false, Infinity),
+ true,
+ );
+ assert.equal(
+ imeKeydownBlocksComposerSubmit({ ...imeEnter, key: " " }, false, Infinity),
+ true,
+ );
+});
diff --git a/studio/frontend/tests/composer-submit-path.test.ts b/studio/frontend/tests/composer-submit-path.test.ts
index 0ed05feef83..b8429a4ca81 100644
--- a/studio/frontend/tests/composer-submit-path.test.ts
+++ b/studio/frontend/tests/composer-submit-path.test.ts
@@ -2,7 +2,11 @@ import assert from "node:assert/strict";
import test from "node:test";
import ts from "typescript";
import { readSrc } from "./helpers/kit.ts";
-import { composerSubmitIntent } from "../src/features/chat/utils/composer-preferences.ts";
+import {
+ composerKeyEventForImeSubmit,
+ composerSubmitIntent,
+ imeKeydownBlocksComposerSubmit,
+} from "../src/features/chat/utils/composer-preferences.ts";
const text = readSrc("components/assistant-ui/thread.tsx");
const source = ts.createSourceFile(
@@ -60,13 +64,18 @@ test("the shipped main composer key handler protects IME and mention selection",
let submits = 0;
let prevented = 0;
const composingRef = { current: false };
+ const imeSessionOpenRef = { current: false };
const skipEnterRef = { current: false };
const deps = {
composerSubmitIntent,
+ composerKeyEventForImeSubmit,
+ imeKeydownBlocksComposerSubmit,
sendShortcut: "mod-enter",
submitOnEnter: true,
skipEnterRef,
composingRef,
+ imeSessionOpenRef,
+ compositionEndedAtRef: { current: -Infinity },
justSentRef: undefined,
refreshStuckTimer: () => undefined,
setCompositionState: (value: boolean) => {
@@ -106,6 +115,60 @@ test("the shipped main composer key handler protects IME and mention selection",
assert.ok(prevented > 0);
});
+test("idle IME Enter sends, the WebKit candidate-confirming Enter does not (#12137)", () => {
+ let submits = 0;
+ const composingRef = { current: false };
+ const imeSessionOpenRef = { current: false };
+ const compositionEndedAtRef = { current: -Infinity };
+ const deps = {
+ composerSubmitIntent,
+ composerKeyEventForImeSubmit,
+ imeKeydownBlocksComposerSubmit,
+ sendShortcut: "enter",
+ submitOnEnter: true,
+ skipEnterRef: { current: false },
+ composingRef,
+ imeSessionOpenRef,
+ compositionEndedAtRef,
+ justSentRef: undefined,
+ refreshStuckTimer: () => undefined,
+ setCompositionState: (value: boolean) => {
+ composingRef.current = value;
+ },
+ onSubmitKey: () => {
+ submits += 1;
+ },
+ };
+ const onKey = createCallback(keyCallback, deps) as unknown as (
+ event: Record<string, unknown>,
+ ) => void;
+ const imeEnter = (timeStamp: number) => ({
+ key: "Enter",
+ metaKey: false,
+ ctrlKey: false,
+ shiftKey: false,
+ altKey: false,
+ keyCode: 229,
+ timeStamp,
+ nativeEvent: { isComposing: false },
+ preventDefault: () => undefined,
+ });
+ // Chromium: keydown inside the session.
+ imeSessionOpenRef.current = true;
+ onKey({ ...imeEnter(1000), nativeEvent: { isComposing: true } });
+ assert.equal(submits, 0);
+ // WebKit: compositionend, then the committing keydown.
+ imeSessionOpenRef.current = false;
+ composingRef.current = false;
+ compositionEndedAtRef.current = 1995;
+ onKey(imeEnter(2000));
+ assert.equal(submits, 0);
+ assert.equal(composingRef.current, true);
+ onKey(imeEnter(2100));
+ assert.equal(submits, 1);
+ assert.equal(composingRef.current, false);
+});
+
for (const active of ["runtime", "pre-stream", "queue", "idle"]) {
test(`parked send retains the submitted follow-up choice with ${active} state at release`, () => {
const calls: unknown[] = [];
@@ -216,7 +279,10 @@ test("main, edit and comparison composers use the setting and expose settings ac
);
assert.match(text, /scrollTarget: "chat-composer"/);
const compare = readSrc("features/chat/shared-composer.tsx");
- assert.match(compare, /composerSubmitIntent\(e, sendShortcut, text\)/);
+ assert.match(
+ compare,
+ /composerSubmitIntent\(\s*imeKey \? composerKeyEventForImeSubmit\(e\) : e,\s*sendShortcut,\s*text,?\s*\)/,
+ );
assert.match(compare, /scrollTarget: "chat-composer"/);
const page = readSrc("features/chat/chat-page.tsx");
assert.match(page, /showContextWindowUsage &&\s*view.mode === "single"/);
diff --git a/tests/studio/test_composer_rtl_bidi_attribute.py b/tests/studio/test_composer_rtl_bidi_attribute.py
index a0afe421e94..0f2b34b1bb8 100644
--- a/tests/studio/test_composer_rtl_bidi_attribute.py
+++ b/tests/studio/test_composer_rtl_bidi_attribute.py
@@ -190,10 +190,22 @@ def _assert_enter_guard_before_immediate_recovery(block: str, refresh_call: str)
def test_main_composer_stuck_enter_does_not_clear_before_submit():
src = THREAD_TSX.read_text(encoding = "utf-8")
block = _extract_block(src, "const onKeyDown = useCallback")
- _assert_enter_guard_before_immediate_recovery(block, "refreshStuckTimer")
+ stuck = _extract_block(
+ block[block.find("if (composingRef.current)") :],
+ "if (composingRef.current)",
+ opener = "{",
+ closer = "}",
+ )
+ _assert_enter_guard_before_immediate_recovery(stuck, "refreshStuckTimer")
def test_compare_composer_stuck_enter_does_not_clear_before_submit():
src = SHARED_TSX.read_text(encoding = "utf-8")
block = _extract_block(src, "function onKeyDown", opener = "{", closer = "}")
- _assert_enter_guard_before_immediate_recovery(block, "refreshStuckImeTimer")
+ stuck = _extract_block(
+ block[block.find("if (composingRef.current)") :],
+ "if (composingRef.current)",
+ opener = "{",
+ closer = "}",
+ )
+ _assert_enter_guard_before_immediate_recovery(stuck, "refreshStuckImeTimer")