From f51ea67ab428ffc6bfd5d11ed3f3ca7d5e8d8fee Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:17:36 +0200 Subject: [PATCH] =?UTF-8?q?=E2=8C=A8=EF=B8=8F=20feat:=20Keyboard=20Submit,?= =?UTF-8?q?=20Focus=20and=20Escape=20for=20Ask=20User=20Cards=20(#17231)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ⌨️ feat: Keyboard submit for ask_user_question answer box The interactive ask_user_question card's answer textarea had no key handling, so the answer could only be submitted by clicking. Route its keydown through resolveComposerKeyDown so it follows the composer's Enter-to-send preference, a rebound submit chord, and IME composition. The run is paused rather than generating, so it resolves as idle and Ctrl/Cmd+Enter submits instead of reaching the during-run actions. * ⌨️ feat: Keyboard focus and confirm for batched ask_user_question - Focus the first question's controls when a batch arrives, only when focus is idle (body, or the empty composer that sent the turn), and focus into the next step on advance instead of the fieldset: the picked or first option, else the answer field. - Enter on the already-picked single-select option confirms the step; Enter on an unpicked option stays its click, so a pick never submits by itself (WCAG 3.2.2). Multi-select rows only toggle. - Ctrl/Cmd+Enter confirm from the answer field like Enter; Shift+Enter never does. * 🐛 fix: Keep Held Enter and Remounts From Picking Batched Answers Review findings on the batched ask_user_question card, and what this commit does about each. 1. Held or double-tapped Enter could pick answers and submit the batch. After Enter confirmed a step, focus moved to the next step's first option. A keyboard Enter on a focused button is its click, so the key repeat of a held Enter (or a quick double-tap) picked that option and advanced again. On the last step the next repeat landed on the now-pressed option, confirmed it, and submitted the whole batch with answers the user never chose. Fix: after a step change, focus goes to the step's picked option, else its answer field. An unpicked option is only a focus target when a batch first arrives. Repeats now reach a text field, which can page on but never picks an option or submits on the user's behalf. 2. Focus-on-arrival ran on every mount, not only on arrival. Its effect has empty deps, so moving the batch between the composer popover and the chat card, or remounting a message that still holds an unanswered batch (reopening a conversation, a Share view), pulled focus onto an option of that batch. The next Enter or Space would then answer it. Fix: `AskUserQuestions` takes a `focusOnArrival` prop, set only by the surface showing the live pause (the popover, or the chat card while it is live and visible), so other mounts never take focus. Focus is placed at most once per batch, tracked by action id, so a move between surfaces is not treated as a new arrival. Tests: a held Enter from an answer field picks nothing and never submits; a surface that isn't the live pause leaves focus alone; a remount of the same batch doesn't take focus again. All three fail without this change. The Chat/Messages/Content, Chat/Input, Chat/ask and hooks/Input suites pass, apart from TokenUsage/index.spec.tsx, which fails the same way without this change. tsc, ESLint and Prettier are clean on the changed files. * feat: Escape to Skip Ask Cards and Move the Popover to Chat Escape in the chat card skips the question or batch, but only before anything is entered, so a stray Escape cannot discard an answer and resume the run. Escape with focus inside the composer popover moves the question to the chat, as it already did from the composer, and returns focus to the composer once the popover closes. Also drops the em dashes from the batched card's comments. * fix: Read Enter-to-Send From the Chat Surface Host in the Ask Card * test: Cover Ask Batch Keyboard Control in E2E Scenarios * fix: Keep Held and Modified Enter From Submitting Ask Answers Ignore auto-repeated Enter before confirming a picked option, so a held Enter cannot both pick and submit the last question. In the answer field, Shift, Alt and repeated Enter now prevent the enclosing chat form's implicit submit instead of returning early. Escape that cancels an IME composition no longer moves the popover to the chat, and the arrival-focus flag lives in the batch's Jotai form state. * fix: Honor a Submit Shortcut Rebound Off Enter in Ask Answer Fields The window shortcut handler yields submitMessage in every editing field except the composer, so a chord such as Ctrl+J did nothing in the ask answer fields. Both the single-question answer box and the batched answer field now act on it, through a shared isReboundSubmitChord. --------- Co-authored-by: elie605 --- .../Chat/Input/AskUserQuestionPopover.tsx | 53 ++- .../__tests__/AskUserQuestionPopover.test.tsx | 132 ++++++++ .../Chat/Messages/Content/AskUserQuestion.tsx | 73 +++- .../Messages/Content/AskUserQuestions.tsx | 171 ++++++++-- .../__tests__/AskUserQuestion.test.tsx | 132 +++++++- .../__tests__/AskUserQuestions.test.tsx | 312 +++++++++++++++++- client/src/hooks/Input/useAskQuestionsForm.ts | 20 +- client/src/utils/shortcuts.spec.ts | 41 +++ client/src/utils/shortcuts.ts | 17 + e2e/specs/mock/scenarios/ask-keyboard.spec.ts | 227 +++++++++++++ 10 files changed, 1142 insertions(+), 36 deletions(-) create mode 100644 client/src/components/Chat/Input/__tests__/AskUserQuestionPopover.test.tsx create mode 100644 e2e/specs/mock/scenarios/ask-keyboard.spec.ts diff --git a/client/src/components/Chat/Input/AskUserQuestionPopover.tsx b/client/src/components/Chat/Input/AskUserQuestionPopover.tsx index c8d7c25565..a8341ae27e 100644 --- a/client/src/components/Chat/Input/AskUserQuestionPopover.tsx +++ b/client/src/components/Chat/Input/AskUserQuestionPopover.tsx @@ -1,4 +1,4 @@ -import { memo, useEffect, useRef } from 'react'; +import { memo, useCallback, useEffect, useRef } from 'react'; import { useWatch } from 'react-hook-form'; import { Button, TooltipAnchor } from '@librechat/client'; import { ChevronDown, CornerDownLeft, TriangleAlert } from 'lucide-react'; @@ -6,6 +6,7 @@ import AskUserQuestions from '~/components/Chat/Messages/Content/AskUserQuestion import useAskAnswerMode from '~/hooks/Input/useAskAnswerMode'; import AskOptions from '~/components/Chat/ask/options'; import { useChatFormContext } from '~/Providers'; +import { isCancelKey } from '~/utils/shortcuts'; import { useComposerOverlay } from './overlay'; import { useLocalize } from '~/hooks'; @@ -32,20 +33,47 @@ function AskUserQuestionPopoverContent({ textAreaRef?: React.RefObject; }) { const ask = useAskAnswerMode(conversationId); - useComposerOverlay(conversationId, ask.popoverVisible); + const { collapse, popoverVisible } = ask; + useComposerOverlay(conversationId, popoverVisible); - if (!ask.popoverVisible || !ask.liveAsk) { + /** + * Escape from inside the popover does what it does from the composer: moves + * the question to the chat. Focus then returns to the composer, which a + * batch keeps disabled until the popover is gone, hence after the commit. + */ + const refocusComposer = useRef(false); + const escapeToChat = useCallback(() => { + refocusComposer.current = true; + collapse(); + }, [collapse]); + useEffect(() => { + if (popoverVisible || !refocusComposer.current) { + return; + } + refocusComposer.current = false; + textAreaRef?.current?.focus(); + }, [popoverVisible, textAreaRef]); + + if (!popoverVisible || !ask.liveAsk) { return null; } if (ask.liveAsk.questions != null && ask.liveAsk.questions.length > 0) { - return ; + return ; } - return ; + return ( + + ); } -function AskUserQuestionsPopoverPanel({ ask }: { ask: ReturnType }) { +function AskUserQuestionsPopoverPanel({ + ask, + onEscape, +}: { + ask: ReturnType; + onEscape: () => void; +}) { const { liveAsk, collapse } = ask; const questions = liveAsk?.questions; if (liveAsk == null || questions == null || questions.length === 0) { @@ -62,6 +90,8 @@ function AskUserQuestionsPopoverPanel({ ask }: { ask: ReturnType} /> @@ -101,9 +131,11 @@ function MoveToChatButton({ onClick }: { onClick: () => void }) { function AskUserQuestionPopoverPanel({ ask, textAreaRef, + onEscape, }: { ask: ReturnType; textAreaRef?: React.RefObject; + onEscape: () => void; }) { const localize = useLocalize(); const { control } = useChatFormContext(); @@ -165,7 +197,14 @@ function AskUserQuestionPopoverPanel({ overflows it is unreachable by page scroll. */}
{ + if (isCancelKey(e.nativeEvent) && !e.nativeEvent.isComposing) { + e.preventDefault(); + onEscape(); + return; + } + handlePopoverKeyDown(e); + }} >
diff --git a/client/src/components/Chat/Input/__tests__/AskUserQuestionPopover.test.tsx b/client/src/components/Chat/Input/__tests__/AskUserQuestionPopover.test.tsx new file mode 100644 index 0000000000..2f73186a26 --- /dev/null +++ b/client/src/components/Chat/Input/__tests__/AskUserQuestionPopover.test.tsx @@ -0,0 +1,132 @@ +import React, { createRef } from 'react'; +import { RecoilRoot } from 'recoil'; +import { useForm, FormProvider } from 'react-hook-form'; +import { act, fireEvent, render, screen } from '@testing-library/react'; +import type { Agents } from 'librechat-data-provider'; +import AskUserQuestionPopover from '../AskUserQuestionPopover'; + +const mockCollapse = jest.fn(); +const mockHandlePopoverKeyDown = jest.fn(); +/** The popover is memoized, so visibility changes reach it the way the real + * hook's state does: through a store it subscribes to. */ +const mockVisibility = { + visible: true, + listeners: new Set<() => void>(), + set(visible: boolean) { + this.visible = visible; + this.listeners.forEach((listener) => listener()); + }, + subscribe(listener: () => void) { + mockVisibility.listeners.add(listener); + return () => mockVisibility.listeners.delete(listener); + }, +}; +let mockQuestions: Agents.AskUserQuestionBatchItem[] | undefined; + +jest.mock('~/hooks', () => ({ + useLocalize: () => (key: string) => key, +})); + +jest.mock('~/components/Chat/Messages/Content/ApprovalContext', () => ({ + useAskSubmitStatus: () => ({ getAskStatus: () => 'idle' }), + useResumeSubmit: () => ({ submitAskAnswer: jest.fn() }), +})); + +jest.mock('../overlay', () => ({ + useComposerOverlay: jest.fn(), +})); + +jest.mock('~/Providers', () => ({ + useChatFormContext: () => jest.requireActual('react-hook-form').useFormContext(), +})); + +jest.mock('~/hooks/Input/useAskAnswerMode', () => ({ + __esModule: true, + default: () => ({ + popoverVisible: jest + .requireActual('react') + .useSyncExternalStore(mockVisibility.subscribe, () => mockVisibility.visible), + liveAsk: { + actionId: 'ask-1', + question: { question: 'Which environment?', options: [{ label: 'Staging', value: 's' }] }, + questions: mockQuestions, + }, + options: [{ label: 'Staging', value: 's' }], + selected: null, + checked: [], + multiSelect: false, + locked: false, + errored: false, + toggleChecked: jest.fn(), + canSubmit: false, + submit: jest.fn(), + submitOption: jest.fn(), + skip: jest.fn(), + collapse: mockCollapse, + handlePopoverKeyDown: mockHandlePopoverKeyDown, + }), +})); + +function Host({ textAreaRef }: { textAreaRef: React.RefObject }) { + const methods = useForm({ defaultValues: { text: '' } }); + return ( + + +