Skip to content

Commit 9d7a6b5

Browse files
committed
fix: focus composer when typing (#102210)
1 parent fc05a81 commit 9d7a6b5

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎ui/src/e2e/chat-flow.e2e.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,53 @@ describeControlUiE2e("Control UI mocked Gateway E2E", () => {
617617
}
618618
});
619619

620+
it("routes page typing to the active composer without stealing text input focus", async () => {
621+
const context = await newBrowserContext({
622+
locale: "en-US",
623+
serviceWorkers: "block",
624+
viewport: { height: 900, width: 1280 },
625+
});
626+
const page = await context.newPage();
627+
await installMockGateway(page, {
628+
historyMessages: [
629+
{
630+
content: [{ text: "Type whenever you are ready.", type: "text" }],
631+
role: "assistant",
632+
timestamp: Date.now(),
633+
},
634+
],
635+
});
636+
637+
try {
638+
await page.goto(`${server.baseUrl}chat`);
639+
await page.getByText("Type whenever you are ready.").click();
640+
641+
const composer = page.locator(".agent-chat__composer-combobox textarea");
642+
await expect
643+
.poll(() => composer.evaluate((element) => element === document.activeElement))
644+
.toBe(false);
645+
646+
await page.keyboard.type("first character preserved");
647+
expect(await composer.inputValue()).toBe("first character preserved");
648+
await expect
649+
.poll(() => composer.evaluate((element) => element === document.activeElement))
650+
.toBe(true);
651+
652+
await page.getByRole("button", { name: "Open command palette" }).click();
653+
const paletteInput = page.locator(".cmd-palette__input");
654+
await paletteInput.waitFor({ state: "visible", timeout: 10_000 });
655+
await expect
656+
.poll(() => paletteInput.evaluate((element) => element === document.activeElement))
657+
.toBe(true);
658+
await page.keyboard.type("session search");
659+
660+
expect(await paletteInput.inputValue()).toBe("session search");
661+
expect(await composer.inputValue()).toBe("first character preserved");
662+
} finally {
663+
await closeBrowserContext(context);
664+
}
665+
});
666+
620667
it("keeps stale context visible as approximate without warning or compaction", async () => {
621668
const context = await newBrowserContext({
622669
locale: "en-US",

‎ui/src/pages/chat/chat-pane.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ type PaneSessionChangeOptions = { replace?: boolean };
8888

8989
const CHAT_OPEN_DETAILS_SELECTOR =
9090
".chat-controls__inline-select[open], .context-usage details[open], .agent-chat__talk-select[open], .agent-chat__attach-menu[open]";
91+
const CHAT_COMPOSER_TEXTAREA_SELECTOR = ".agent-chat__composer-combobox > textarea";
92+
const CHAT_TEXT_ENTRY_SELECTOR =
93+
"input, textarea, select, [contenteditable]:not([contenteditable='false']), [role='combobox'], [role='listbox'], [role='textbox']";
94+
const CHAT_SPACE_ACTIVATION_SELECTOR =
95+
"a[href], button, summary, [role='button'], [role='checkbox'], [role='link'], [role='radio'], [role='switch']";
96+
const CHAT_MODAL_SELECTOR = "dialog[open], [aria-modal='true']";
9197

9298
const NEW_SESSION_ACTIVE_RUN_MESSAGE =
9399
"Start a new session after the active run or queued messages finish.";
@@ -96,6 +102,12 @@ const NEW_SESSION_LIST_LOADING_MESSAGE =
96102
const NEW_SESSION_CREATE_FAILED_MESSAGE =
97103
"New Chat could not create a new session. Try again in a moment.";
98104

105+
function keyboardEventPathMatches(event: KeyboardEvent, selector: string): boolean {
106+
return event
107+
.composedPath()
108+
.some((target) => target instanceof Element && target.matches(selector));
109+
}
110+
99111
class ChatPane extends LitElement {
100112
@consume({ context: applicationContext, subscribe: false })
101113
private context!: ChatPageContext;
@@ -341,6 +353,26 @@ class ChatPane extends LitElement {
341353
}
342354

343355
private readonly handleDocumentKeydown = (event: KeyboardEvent) => {
356+
if (
357+
this.active &&
358+
!event.defaultPrevented &&
359+
!event.isComposing &&
360+
!event.metaKey &&
361+
!event.ctrlKey &&
362+
!event.altKey &&
363+
event.key.length === 1 &&
364+
!keyboardEventPathMatches(event, CHAT_TEXT_ENTRY_SELECTOR) &&
365+
!(event.key === " " && keyboardEventPathMatches(event, CHAT_SPACE_ACTIVATION_SELECTOR)) &&
366+
!document.querySelector(CHAT_MODAL_SELECTOR)
367+
) {
368+
const composer = this.querySelector<HTMLTextAreaElement>(CHAT_COMPOSER_TEXTAREA_SELECTOR);
369+
if (composer && !composer.disabled && !composer.readOnly) {
370+
// Focus during keydown capture so the browser delivers beforeinput/input,
371+
// including the first character, through the composer's normal pipeline.
372+
composer.focus({ preventScroll: true });
373+
}
374+
}
375+
344376
if (event.defaultPrevented || event.key !== "Escape") {
345377
return;
346378
}

0 commit comments

Comments
 (0)