fix(webui): preserve mentions when IME composition is canceled

This commit is contained in:
Xubin Ren committed 2026-09-13 13:53:20 +08:00
1 parent 902e2b87b4
commit 78ae5ff94f
3 files changed
+30

No files matched your search

+4
View File
@@ -102,6 +102,10 @@ export function editComposerMentionText(
caret: number,
selection?: { start: number; end: number },
): { value: string; cursor: number } {
// Canceling IME composition is not an edit, even when its caret is inside a token.
if (nextDisplay === text.display) {
return { value: text.raw, cursor: mentionTextOffset(text, caret, "toRaw") };
}
let start = 0;
// The caret disambiguates repeated text (e.g. inserting a space before an existing space).
while (start < Math.min(text.display.length, nextDisplay.length, caret, selection?.start ?? Infinity)
@@ -12,6 +12,13 @@ const apps = [
const project = (raw: string) => composerMentionText(splitCapabilityMentionSegments(raw, apps));
describe("composer mention display text", () => {
it("preserves the identifier when an IME edit is canceled inside a mention", () => {
const text = project("@drive next");
expect(editComposerMentionText(text, text.display, 4, { start: 4, end: 4 }).value)
.toBe("@drive next");
expect(editComposerMentionText(text, text.display, 13, { start: 0, end: 13 }).value)
.toBe("@drive next");
});
it.each([
["n @Linear @Google Drive", 0, 0, ["linear", "drive"]],
["@Linear n @Google Drive", 8, 8, ["linear", "drive"]],
+19
View File
@@ -2384,6 +2384,25 @@ describe("ThreadComposer", () => {
});
});
it("does not remove a mention when composition is canceled inside its label", () => {
const onSend = vi.fn();
render(<ThreadComposer onSend={onSend} mcpPresets={[
{ ...MCP_PRESETS[0], name: "drive", display_name: "Google Drive" },
]} />);
const input = screen.getByLabelText("Message input") as HTMLTextAreaElement;
fireEvent.change(input, { target: { value: "@drive next" } });
input.setSelectionRange(4, 4);
fireEvent.compositionStart(input);
fireEvent.change(input, { target: { value: "@\u00a0Gonogle Drive next", selectionStart: 5 } });
fireEvent.change(input, { target: { value: "@\u00a0Google Drive next", selectionStart: 4 } });
fireEvent.compositionEnd(input, { data: "" });
expect(input).toHaveValue("@\u00a0Google Drive next");
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).toHaveBeenCalledWith("@drive next", undefined, {
mcpPresets: [expect.objectContaining({ name: "drive" })],
});
});
it("clears mention editing and undo state when switching sessions during composition", () => {
const props = { onSend: vi.fn(), cliApps: CLI_APPS };
const { rerender } = render(<ThreadComposer {...props} pendingQueueKey="chat-a" />);