Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
⌨️ feat: Keyboard Submit, Focus and Escape for Ask User Cards (#17231)
Marco Berettaf51ea67ab428
diff --git a/client/src/components/Chat/Input/AskUserQuestionPopover.tsx b/client/src/components/Chat/Input/AskUserQuestionPopover.tsxindex 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/AskUserQuestionimport 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<HTMLTextAreaElement>;}) {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 <AskUserQuestionsPopoverPanel ask={ask} />;+ return <AskUserQuestionsPopoverPanel ask={ask} onEscape={escapeToChat} />;}- return <AskUserQuestionPopoverPanel ask={ask} textAreaRef={textAreaRef} />;+ return (+ <AskUserQuestionPopoverPanel ask={ask} textAreaRef={textAreaRef} onEscape={escapeToChat} />+ );}-function AskUserQuestionsPopoverPanel({ ask }: { ask: ReturnType<typeof useAskAnswerMode> }) {+function AskUserQuestionsPopoverPanel({+ ask,+ onEscape,+}: {+ ask: ReturnType<typeof useAskAnswerMode>;+ 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<typeof useAskAnactionId={liveAsk.actionId}questions={questions}className="shrink-0"+ focusOnArrival+ onEscape={onEscape}headerAction={<MoveToChatButton onClick={collapse} />}/></div>@@ -101,9 +131,11 @@ function MoveToChatButton({ onClick }: { onClick: () => void }) {function AskUserQuestionPopoverPanel({ask,textAreaRef,+ onEscape,}: {ask: ReturnType<typeof useAskAnswerMode>;textAreaRef?: React.RefObject<HTMLTextAreaElement>;+ onEscape: () => void;}) {const localize = useLocalize();const { control } = useChatFormContext();@@ -165,7 +197,14 @@ function AskUserQuestionPopoverPanel({overflows it is unreachable by page scroll. */}<divclassName="popover border-border-light bg-surface-secondary rounded-theme-popover flex max-h-[60vh] flex-col border p-2 shadow-lg [view-transition-name:ask-question]"- onKeyDown={handlePopoverKeyDown}+ onKeyDown={(e) => {+ if (isCancelKey(e.nativeEvent) && !e.nativeEvent.isComposing) {+ e.preventDefault();+ onEscape();+ return;+ }+ handlePopoverKeyDown(e);+ }}><div className="text-text-secondary flex shrink-0 items-start justify-between gap-2 p-1 pl-2"><div className="max-h-[24vh] min-w-0 overflow-y-auto pt-1">diff --git a/client/src/components/Chat/Input/__tests__/AskUserQuestionPopover.test.tsx b/client/src/components/Chat/Input/__tests__/AskUserQuestionPopover.test.tsxnew file mode 100644index 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<HTMLTextAreaElement> }) {+ const methods = useForm({ defaultValues: { text: '' } });+ return (+ <RecoilRoot>+ <FormProvider {...methods}>+ <textarea ref={textAreaRef} aria-label="Composer" />+ <AskUserQuestionPopover conversationId="conversation-1" textAreaRef={textAreaRef} />+ </FormProvider>+ </RecoilRoot>+ );+}++describe('AskUserQuestionPopover Escape', () => {+ beforeEach(() => {+ jest.clearAllMocks();+ mockVisibility.visible = true;+ mockQuestions = undefined;+ });++ /** Escape on `target`, then the popover closes; returns the composer. */+ const escapeThenClose = (target: () => HTMLElement) => {+ const textAreaRef = createRef<HTMLTextAreaElement>();+ render(<Host textAreaRef={textAreaRef} />);+ fireEvent.keyDown(target(), { key: 'Escape' });+ act(() => mockVisibility.set(false));+ return textAreaRef.current;+ };++ test('moves a single question to the chat and returns focus to the composer', () => {+ const composer = escapeThenClose(() => screen.getByRole('button', { name: /Staging/ }));+ expect(mockCollapse).toHaveBeenCalledTimes(1);+ expect(mockHandlePopoverKeyDown).not.toHaveBeenCalled();+ expect(composer).toHaveFocus();+ });++ test('moves a batch to the chat and returns focus to the composer', () => {+ mockQuestions = [+ { id: 'window', question: 'Which time window?' },+ { id: 'region', question: 'Which region?' },+ ];+ const composer = escapeThenClose(() =>+ screen.getByRole('textbox', { name: /Which time window/ }),+ );+ expect(mockCollapse).toHaveBeenCalledTimes(1);+ expect(composer).toHaveFocus();+ });++ test('leaves an Escape that cancels an IME composition alone', () => {+ render(<Host textAreaRef={createRef<HTMLTextAreaElement>()} />);+ fireEvent.keyDown(screen.getByRole('button', { name: /Staging/ }), {+ key: 'Escape',+ isComposing: true,+ });+ expect(mockCollapse).not.toHaveBeenCalled();+ });++ test('passes other keys through to the popover shortcuts', () => {+ render(<Host textAreaRef={createRef<HTMLTextAreaElement>()} />);+ fireEvent.keyDown(screen.getByRole('button', { name: /Staging/ }), { key: '1' });+ expect(mockHandlePopoverKeyDown).toHaveBeenCalledTimes(1);+ expect(mockCollapse).not.toHaveBeenCalled();+ });+});diff --git a/client/src/components/Chat/Messages/Content/AskUserQuestion.tsx b/client/src/components/Chat/Messages/Content/AskUserQuestion.tsxindex 13af9061a1..c645a6bcd6 100644--- a/client/src/components/Chat/Messages/Content/AskUserQuestion.tsx+++ b/client/src/components/Chat/Messages/Content/AskUserQuestion.tsx@@ -2,14 +2,25 @@ import { useContext, useMemo, useState } from 'react';import { Button, TextareaAutosize, TooltipAnchor } from '@librechat/client';import { ChevronDown, MessageCircleQuestion, TriangleAlert } from 'lucide-react';import type { Agents } from 'librechat-data-provider';+import type { KeyboardEvent } from 'react';+import type { ComposerBindings } from '~/hooks/Input/useComposerBindings';+import { isCancelKey, isReboundSubmitChord, resolveComposerKeyDown } from '~/utils/shortcuts';import { useApprovalContext, useAskSubmitStatus, useResumeSubmit } from './ApprovalContext';import { splitOtherOption, ASK_USER_DECLINED_ANSWER } from '~/utils/approval';+import { useOptionalChatSurface } from '~/components/Chat/Subagents/surface';+import { cn, forceResize, insertTextAtCursor } from '~/utils';import useAskAnswerMode from '~/hooks/Input/useAskAnswerMode';import AskOptions from '~/components/Chat/ask/options';import { ChatContext } from '~/Providers/ChatContext';import AskUserQuestions from './AskUserQuestions';import { useLocalize } from '~/hooks';-import { cn } from '~/utils';++/** Stock preferences for a card rendered outside a chat surface. */+const NO_HOST_BINDINGS: ComposerBindings = {+ shortcutsEnabled: true,+ submitOverride: undefined,+ yieldedChords: new Set(),+};/*** Renders an `ask_user_question` pause: the prompt, optional description, any@@ -81,6 +92,7 @@ function AskUserQuestionsCard({<AskUserQuestionsactionId={actionId}questions={questions}+ focusOnArrival={live}headerAction={onExpand != null && (<TooltipAnchor@@ -122,6 +134,10 @@ function AskUserQuestionSingle({const { submitAskAnswer } = useResumeSubmit();const [answer, setAnswer] = useState(() => getAskAnswerDraft(actionId));const [localChecked, setLocalChecked] = useState<number[]>([]);+ const surface = useOptionalChatSurface();+ const enterToSend = surface?.enterToSend ?? false;+ const { shortcutsEnabled, submitOverride, yieldedChords } =+ surface?.composerBindings ?? NO_HOST_BINDINGS;/*** The composer popover is the primary answer surface — while it's VISIBLE* for this pause, rendering the card too duplicates the question. The card@@ -216,6 +232,59 @@ function AskUserQuestionSingle({submitAskAnswer(actionId, values.join(', '));};+ /**+ * The answer box follows the composer's Enter table, so the user's+ * Enter-to-send preference and any rebound submit chord apply here too. The+ * run is paused on this question rather than generating, so it resolves as+ * idle: Ctrl/Cmd+Enter must submit the answer, not reach the during-run+ * steer/queue actions.+ */+ const handleAnswerKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {+ const isComposing = e.nativeEvent.isComposing || e.key === 'Process' || e.keyCode === 229;+ if (!isComposing && isReboundSubmitChord(e.nativeEvent, { shortcutsEnabled, submitOverride })) {+ e.preventDefault();+ if (canSubmit && !locked) {+ submitCombined();+ }+ return;+ }+ const action = resolveComposerKeyDown(e.nativeEvent, {+ isComposing,+ isSubmitting: false,+ allowSubmitWhileGenerating: false,+ hasDuringRunModifier: false,+ shortcutsEnabled,+ enterToSend,+ submitOverride,+ yieldedChords,+ });+ if (action === 'none') {+ return;+ }+ e.preventDefault();+ if (action === 'newline') {+ insertTextAtCursor(e.currentTarget, '\n');+ forceResize(e.currentTarget);+ return;+ }+ if (action === 'submit' && canSubmit && !locked) {+ submitCombined();+ }+ };++ /** Escape skips, but only before anything is entered, so a stray Escape+ * cannot discard an answer and resume the run. */+ const handleCardKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {+ if (!isCancelKey(e.nativeEvent) || e.nativeEvent.isComposing) {+ return;+ }+ if (locked || trimmed.length > 0 || (multiSelect && checkedIndices.length > 0)) {+ return;+ }+ e.preventDefault();+ handleSkip();+ };+/*** The live card shares its view-transition-name with the popover panel, so* collapse/expand morphs one surface into the other. The placeholder copy@@ -233,6 +302,7 @@ function AskUserQuestionSingle({isLivePause && !showPlaceholder && '[view-transition-name:ask-question]',)}aria-hidden={showPlaceholder || undefined}+ onKeyDown={handleCardKeyDown}><div className="text-text-secondary flex items-start justify-between gap-2"><div className="min-w-0 pt-1">@@ -284,6 +354,7 @@ function AskUserQuestionSingle({setAnswerValue(e.target.value);setAskAnswerDraft(actionId, e.target.value);}}+ onKeyDown={handleAnswerKeyDown}minRows={2}maxRows={12}placeholder={otherLabel ?? localize('com_ui_your_answer')}diff --git a/client/src/components/Chat/Messages/Content/AskUserQuestions.tsx b/client/src/components/Chat/Messages/Content/AskUserQuestions.tsxindex 2ffeaac254..5eb2f3ac3b 100644--- a/client/src/components/Chat/Messages/Content/AskUserQuestions.tsx+++ b/client/src/components/Chat/Messages/Content/AskUserQuestions.tsx@@ -2,11 +2,14 @@ import { useCallback, useEffect, useId, useMemo, useRef } from 'react';import { TriangleAlert } from 'lucide-react';import { Input, Button } from '@librechat/client';import type { Agents } from 'librechat-data-provider';-import type { ReactNode } from 'react';+import type { KeyboardEvent, ReactNode } from 'react';+import { useOptionalChatSurface } from '~/components/Chat/Subagents/surface';+import { isCancelKey, isReboundSubmitChord } from '~/utils/shortcuts';import useAskQuestionsForm from '~/hooks/Input/useAskQuestionsForm';import AskOptions from '~/components/Chat/ask/options';import { splitOtherOption } from '~/utils/approval';import { AutoHeight } from '~/components/ui';+import { mainTextareaId } from '~/common';import { useLocalize } from '~/hooks';import { cn } from '~/utils';@@ -21,23 +24,32 @@ export default function AskUserQuestions({questions,className,headerAction,+ focusOnArrival = false,+ onEscape,}: {actionId: string;questions: Agents.AskUserQuestionBatchItem[];className?: string;+ /** This surface is the live pause's visible one, so the batch arriving may+ * take focus. A Share view or a hidden footprint copy leaves it alone. */+ focusOnArrival?: boolean;/** The surface's own control (move to chat, move back), set in the* question's header row so it shares the form's inset. */headerAction?: ReactNode;+ /** Escape on this surface. Without one, Escape skips the batch. */+ onEscape?: () => void;}) {const localize = useLocalize();const promptId = useId();const form = useAskQuestionsForm(actionId, questions);+ const composerBindings = useOptionalChatSurface()?.composerBindings;const { goToStep, selectOption } = form;const scrollRef = useRef<HTMLDivElement>(null);const stepRef = useRef<HTMLFieldSetElement>(null);- /** Set only when a choice click is about to unmount the button that owns- * focus, which would otherwise drop focus to <body> mid-batch. */+ const answerRef = useRef<HTMLInputElement>(null);+ /** Set only when a choice click or an Enter is about to swap the step out+ * from under focus, which would otherwise drop focus to <body> mid-batch. */const refocusRef = useRef(false);const total = questions.length;@@ -69,6 +81,33 @@ export default function AskUserQuestions({[activeIndex, goToStep, selectOption, total],);+ /**+ * Focus the step's control that is ready to answer: its picked option, else+ * the answer field. Each is labelled within the step, so a screen reader+ * still announces the new question. The step itself is the fallback when+ * every control is locked.+ *+ * An unpicked option is only a target on arrival (`firstOption`). After a+ * step change the key that caused it may still be down: a held or+ * double-tapped Enter on a focused option is its click, which would pick it,+ * advance again, and on the last step confirm the pick and submit the batch.+ */+ const focusStep = useCallback(+ ({ firstOption = false, ...options }: FocusOptions & { firstOption?: boolean } = {}) => {+ const step = stepRef.current;+ if (step == null) {+ return;+ }+ const target =+ step.querySelector<HTMLElement>('button[data-selected]:not(:disabled)') ??+ (firstOption ? step.querySelector<HTMLElement>('button:not(:disabled)') : null) ??+ (answerRef.current?.disabled === false ? answerRef.current : null) ??+ step;+ target.focus(options);+ },+ [],+ );+useEffect(() => {if (scrollRef.current != null) {scrollRef.current.scrollTop = 0;@@ -77,8 +116,36 @@ export default function AskUserQuestions({return;}refocusRef.current = false;- stepRef.current?.focus();- }, [activeIndex]);+ focusStep();+ }, [activeIndex, focusStep]);++ /**+ * An arriving batch puts the user on the first question's controls, but+ * only when focus is idle (nowhere, or the empty composer that just sent+ * the turn), so a draft or any other control the user is in keeps focus.+ * It happens once per batch: moving it between the popover and the chat+ * remounts it, and that is not a new arrival.+ */+ useEffect(() => {+ if (!focusOnArrival || form.locked || form.arrivalFocused) {+ return;+ }+ const active = document.activeElement;+ const idle =+ active == null ||+ active === document.body ||+ (active instanceof HTMLTextAreaElement &&+ active.id === mainTextareaId &&+ active.value.trim().length === 0);+ if (!idle) {+ return;+ }+ focusStep({ firstOption: true, preventScroll: true });+ if (stepRef.current?.contains(document.activeElement) === true) {+ form.markArrivalFocused();+ }+ // eslint-disable-next-line react-hooks/exhaustive-deps -- arrival only+ }, []);if (form.status === 'submitted') {return null;@@ -108,8 +175,41 @@ export default function AskUserQuestions({);const text = Object.hasOwn(form.state.text, question.id) ? form.state.text[question.id] : '';+ /** Confirm the current answer: the next step, or the batch on the last one. */+ const confirmStep = () => {+ if (!isLastStep) {+ if (!navLocked) {+ refocusRef.current = true;+ goToStep(activeIndex + 1);+ }+ return;+ }+ form.submit();+ };++ /**+ * Escape is the surface's way out when it has one (the popover moves the+ * batch to the chat), else Skip. Skip only fires before anything is+ * answered, so a stray Escape cannot discard answers and resume the run.+ */+ const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {+ if (!isCancelKey(event.nativeEvent) || event.nativeEvent.isComposing) {+ return;+ }+ if (onEscape != null) {+ event.preventDefault();+ onEscape();+ return;+ }+ if (form.locked || Object.keys(form.answers).length > 0) {+ return;+ }+ event.preventDefault();+ form.skip();+ };+return (- <div className={cn('flex min-h-0 flex-col', className)}>+ <div className={cn('flex min-h-0 flex-col', className)} onKeyDown={handleKeyDown}>{/* Only the active step renders, laid out at its final size at once.One `AutoHeight` holds everything above the buttons and clips only atits bottom edge, so the header and title stay put and the footer@@ -188,6 +288,27 @@ export default function AskUserQuestions({tabIndex={-1}aria-labelledby={promptId}className="flex flex-col gap-2 pt-3 outline-hidden"+ onKeyDown={(event) => {+ /* Picking and confirming stay separate (WCAG 3.2.2): Enter on an+ option is its click, so a pick never submits by itself. Enter+ on the single-select option that is ALREADY the answer confirms+ that answer, like Enter in the field: the next step, or the+ batch. */+ const target = event.target;+ if (+ event.key !== 'Enter' ||+ event.repeat ||+ event.shiftKey ||+ event.altKey ||+ question.multiSelect === true ||+ !(target instanceof HTMLButtonElement) ||+ target.getAttribute('aria-pressed') !== 'true'+ ) {+ return;+ }+ event.preventDefault();+ confirmStep();+ }}>{choices.length > 0 && (<AskOptions@@ -204,31 +325,41 @@ export default function AskUserQuestions({/>)}<Input+ ref={answerRef}value={text}disabled={form.locked}onChange={(event) => form.setText(question, event.target.value)}onKeyDown={(event) => {/* The composer popover sits inside the chat form, where Enter in asingle-line field would submit the composer draft instead.- Enter confirms this answer: the next step, or the batch. An Enter that- confirms an IME composition is left alone, with the same Safari- fallback as the composer. */+ Enter (or Ctrl/Cmd+Enter) confirms this answer: the next step,+ or the batch, and so does a submit shortcut rebound off Enter.+ Shift+Enter and Alt+Enter never submit, as in the+ composer, and neither does a held Enter repeating into the next+ step; those are still kept from submitting the form. An Enter+ that confirms an IME composition is left alone, with the same+ Safari fallback as the composer. */+ const composing =+ event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229;if (- event.key !== 'Enter' ||- event.nativeEvent.isComposing ||- event.nativeEvent.keyCode === 229+ !composing &&+ composerBindings != null &&+ isReboundSubmitChord(event.nativeEvent, composerBindings)) {+ event.preventDefault();+ if (!event.repeat) {+ confirmStep();+ }+ return;+ }+ if (event.key !== 'Enter' || composing) {return;}event.preventDefault();- if (!isLastStep) {- if (!navLocked) {- refocusRef.current = true;- goToStep(activeIndex + 1);- }+ if (event.repeat || event.shiftKey || event.altKey) {return;}- form.submit();+ confirmStep();}}placeholder={otherLabel ?? localize('com_ui_your_answer')}aria-label={`${question.question} ${localize('com_ui_your_answer')}`}diff --git a/client/src/components/Chat/Messages/Content/__tests__/AskUserQuestion.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/AskUserQuestion.test.tsxindex ea47135432..19f10beea2 100644--- a/client/src/components/Chat/Messages/Content/__tests__/AskUserQuestion.test.tsx+++ b/client/src/components/Chat/Messages/Content/__tests__/AskUserQuestion.test.tsx@@ -2,17 +2,28 @@ import React from 'react';import { RecoilRoot } from 'recoil';import { fireEvent, render, screen } from '@testing-library/react';import type { Agents } from 'librechat-data-provider';+import type { ShortcutBinding } from '~/utils/shortcuts';+import { ChatSurfaceProvider } from '~/components/Chat/Subagents/surface';+import { ASK_USER_DECLINED_ANSWER } from '~/utils/approval';import ApprovalProvider from '../ApprovalContext';import AskUserQuestion from '../AskUserQuestion';const mockSubmitAnswer = jest.fn();const mockSetAnswerText = jest.fn();+const mockInsertTextAtCursor = jest.fn();let mockPopoverVisible = false;let mockCollapsed = false;let mockLiveActionId: string | null = null;let mockChecked: number[] = [];let mockAnswerText = '';+/** jsdom has no execCommand, so the newline path is observed through the mock. */+jest.mock('~/utils/textarea', () => ({+ ...jest.requireActual('~/utils/textarea'),+ forceResize: jest.fn(),+ insertTextAtCursor: (...args: unknown[]) => mockInsertTextAtCursor(...args),+}));+jest.mock('~/hooks', () => ({useLocalize: () => (key: string) => {const labels: Record<string, string> = {@@ -56,14 +67,29 @@ jest.mock('~/Providers/ChatContext', () => ({}),}));+const yieldedChords = new Set<string>();+let submitOverride: ShortcutBinding | undefined;+const tree = (key: string,question: Agents.AskUserQuestionRequest = { question: 'Which environment?' },+ enterToSend = true,) => (<RecoilRoot>- <ApprovalProvider>- <AskUserQuestion key={key} actionId="ask-1" question={question} />- </ApprovalProvider>+ <ChatSurfaceProvider+ value={{+ enterToSend,+ maximizeChatSpace: false,+ showScrollButton: false,+ composerBindings: { shortcutsEnabled: true, submitOverride, yieldedChords },+ claimForeground: jest.fn(),+ handOffComposerText: jest.fn(),+ }}+ >+ <ApprovalProvider>+ <AskUserQuestion key={key} actionId="ask-1" question={question} />+ </ApprovalProvider>+ </ChatSurfaceProvider></RecoilRoot>);@@ -75,6 +101,7 @@ describe('AskUserQuestion', () => {mockLiveActionId = null;mockChecked = [];mockAnswerText = '';+ submitOverride = undefined;});test('restores a typed answer after the card remounts inside the same message', () => {@@ -109,4 +136,103 @@ describe('AskUserQuestion', () => {expect(mockSubmitAnswer).toHaveBeenCalledWith(['public', 'carried free-form answer']);});++ describe('answer box keyboard submit', () => {+ const liveTree = (enterToSend: boolean, answerText = 'Use staging first') => {+ mockCollapsed = true;+ mockLiveActionId = 'ask-1';+ mockAnswerText = answerText;+ return tree('live', { question: 'Which environment?' }, enterToSend);+ };+ const answerBox = () => screen.getByRole('textbox', { name: 'Your answer' });++ test('Enter submits when Enter-to-send is on', () => {+ render(liveTree(true));+ fireEvent.keyDown(answerBox(), { key: 'Enter' });+ expect(mockSubmitAnswer).toHaveBeenCalledWith(['Use staging first']);+ });++ test('Ctrl+Enter and Cmd+Enter submit when Enter-to-send is off', () => {+ render(liveTree(false));+ fireEvent.keyDown(answerBox(), { key: 'Enter', ctrlKey: true });+ fireEvent.keyDown(answerBox(), { key: 'Enter', metaKey: true });+ expect(mockSubmitAnswer).toHaveBeenCalledTimes(2);+ });++ test('plain Enter writes a newline instead of submitting when Enter-to-send is off', () => {+ render(liveTree(false));+ fireEvent.keyDown(answerBox(), { key: 'Enter' });+ expect(mockSubmitAnswer).not.toHaveBeenCalled();+ expect(mockInsertTextAtCursor).toHaveBeenCalledWith(answerBox(), '\n');+ });++ test('Shift+Enter never submits', () => {+ render(liveTree(true));+ fireEvent.keyDown(answerBox(), { key: 'Enter', shiftKey: true });+ expect(mockSubmitAnswer).not.toHaveBeenCalled();+ });++ test('Enter confirming an IME composition does not submit', () => {+ render(liveTree(true));+ fireEvent.keyDown(answerBox(), { key: 'Enter', keyCode: 229 });+ expect(mockSubmitAnswer).not.toHaveBeenCalled();+ });++ test('a submit shortcut rebound off Enter submits the answer', () => {+ submitOverride = { meta: false, ctrl: true, alt: false, shift: false, key: 'J' };+ render(liveTree(true));+ fireEvent.keyDown(answerBox(), { key: 'j', ctrlKey: true });+ expect(mockSubmitAnswer).toHaveBeenCalledWith(['Use staging first']);+ });++ test('does not submit an empty answer', () => {+ render(liveTree(true, ' '));+ fireEvent.keyDown(answerBox(), { key: 'Enter' });+ expect(mockSubmitAnswer).not.toHaveBeenCalled();+ });+ });++ describe('Escape', () => {+ const liveTree = (answerText = '') => {+ mockCollapsed = true;+ mockLiveActionId = 'ask-1';+ mockAnswerText = answerText;+ return tree('live', { question: 'Which environment?' });+ };+ const answerBox = () => screen.getByRole('textbox', { name: 'Your answer' });++ test('skips the question when nothing is entered', () => {+ render(liveTree());+ fireEvent.keyDown(answerBox(), { key: 'Escape' });+ expect(mockSubmitAnswer).toHaveBeenCalledWith([ASK_USER_DECLINED_ANSWER]);+ });++ test('keeps a typed answer instead of skipping', () => {+ render(liveTree('Use staging first'));+ fireEvent.keyDown(answerBox(), { key: 'Escape' });+ expect(mockSubmitAnswer).not.toHaveBeenCalled();+ });++ test('keeps checked options instead of skipping', () => {+ mockCollapsed = true;+ mockLiveActionId = 'ask-1';+ mockChecked = [0];+ render(+ tree('live', {+ question: 'Choose a source',+ multiSelect: true,+ options: [{ label: 'Public data', value: 'public' }],+ }),+ );+ fireEvent.keyDown(answerBox(), { key: 'Escape' });+ expect(mockSubmitAnswer).not.toHaveBeenCalled();+ });++ test('ignores Escape that cancels an IME composition or carries a modifier', () => {+ render(liveTree());+ fireEvent.keyDown(answerBox(), { key: 'Escape', isComposing: true });+ fireEvent.keyDown(answerBox(), { key: 'Escape', shiftKey: true });+ expect(mockSubmitAnswer).not.toHaveBeenCalled();+ });+ });});diff --git a/client/src/components/Chat/Messages/Content/__tests__/AskUserQuestions.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/AskUserQuestions.test.tsxindex f6a8dc0788..31e433e44d 100644--- a/client/src/components/Chat/Messages/Content/__tests__/AskUserQuestions.test.tsx+++ b/client/src/components/Chat/Messages/Content/__tests__/AskUserQuestions.test.tsx@@ -2,6 +2,7 @@ import React from 'react';import { RecoilRoot } from 'recoil';import { fireEvent, render, screen } from '@testing-library/react';import type { Agents } from 'librechat-data-provider';+import { ChatSurfaceProvider } from '~/components/Chat/Subagents/surface';import { ASK_USER_DECLINED_ANSWER } from '~/utils/approval';import AskUserQuestions from '../AskUserQuestions';@@ -47,13 +48,37 @@ const questions: Agents.AskUserQuestionBatchItem[] = [{ id: 'window', question: 'Which time window?' },];-const renderBatch = (actionId: string, batch: Agents.AskUserQuestionBatchItem[] = questions) =>+const renderBatch = (+ actionId: string,+ batch: Agents.AskUserQuestionBatchItem[] = questions,+ focusOnArrival = true,+) =>render(<RecoilRoot>- <AskUserQuestions actionId={actionId} questions={batch} />+ <AskUserQuestions actionId={actionId} questions={batch} focusOnArrival={focusOnArrival} /></RecoilRoot>,);+const optionBatch: Agents.AskUserQuestionBatchItem[] = [+ { id: 'window', question: 'Which time window?' },+ {+ id: 'environment',+ question: 'Where should this run?',+ options: [+ { label: 'Staging', value: 'staging' },+ { label: 'Production', value: 'production' },+ ],+ },+ {+ id: 'region',+ question: 'Which region?',+ options: [+ { label: 'us-east', value: 'us-east' },+ { label: 'eu-west', value: 'eu-west' },+ ],+ },+];+/** Only the active step renders. */const isShown = (text: string) => screen.queryByText(text) != null;@@ -255,14 +280,250 @@ describe('AskUserQuestions', () => {expect(mockSubmitAskAnswer).not.toHaveBeenCalled();});- test('Enter moves focus to the next question', () => {+ test('Enter moves focus into the next question, ready to answer', () => {renderBatch('ask-enter-focus');const first = screen.getByRole('textbox', { name: /Where should this run/ });fireEvent.change(first, { target: { value: 'Locally' } });fireEvent.keyDown(first, { key: 'Enter' });expect(isShown('Which time window?')).toBe(true);- expect(screen.getByRole('group', { name: 'Which time window?' })).toHaveFocus();+ expect(screen.getByRole('textbox', { name: /Which time window/ })).toHaveFocus();+ });++ test('Ctrl/Cmd+Enter confirm like Enter; Shift+Enter never does', () => {+ renderBatch('ask-modifiers');+ const first = screen.getByRole('textbox', { name: /Where should this run/ });+ fireEvent.change(first, { target: { value: 'Locally' } });++ fireEvent.keyDown(first, { key: 'Enter', shiftKey: true });+ expect(isShown('Where should this run?')).toBe(true);++ fireEvent.keyDown(first, { key: 'Enter', ctrlKey: true });+ expect(isShown('Which time window?')).toBe(true);++ const last = screen.getByRole('textbox', { name: /Which time window/ });+ fireEvent.change(last, { target: { value: 'Today' } });+ fireEvent.keyDown(last, { key: 'Enter', metaKey: true });+ expect(mockSubmitAskAnswer).toHaveBeenCalledTimes(1);+ });++ test('modified or repeated Enter in an answer field keeps the enclosing form from submitting', () => {+ const onSubmit = jest.fn((event: React.FormEvent) => event.preventDefault());+ render(+ <RecoilRoot>+ <form onSubmit={onSubmit}>+ <AskUserQuestions actionId="ask-implicit-submit" questions={questions} />+ </form>+ </RecoilRoot>,+ );+ const first = screen.getByRole('textbox', { name: /Where should this run/ });+ fireEvent.change(first, { target: { value: 'Locally' } });++ for (const init of [{ shiftKey: true }, { altKey: true }, { repeat: true }]) {+ /* fireEvent returns false when the default (implicit submit) was prevented. */+ expect(fireEvent.keyDown(first, { key: 'Enter', ...init })).toBe(false);+ }+ expect(isShown('Where should this run?')).toBe(true);+ expect(onSubmit).not.toHaveBeenCalled();+ });++ test('a submit shortcut rebound off Enter confirms the answer like Enter', () => {+ render(+ <RecoilRoot>+ <ChatSurfaceProvider+ value={{+ enterToSend: false,+ maximizeChatSpace: false,+ showScrollButton: false,+ composerBindings: {+ shortcutsEnabled: true,+ submitOverride: { meta: false, ctrl: true, alt: false, shift: false, key: 'J' },+ yieldedChords: new Set(),+ },+ claimForeground: jest.fn(),+ handOffComposerText: jest.fn(),+ }}+ >+ <AskUserQuestions actionId="ask-rebound-submit" questions={questions} />+ </ChatSurfaceProvider>+ </RecoilRoot>,+ );+ const first = screen.getByRole('textbox', { name: /Where should this run/ });+ fireEvent.change(first, { target: { value: 'Locally' } });+ fireEvent.keyDown(first, { key: 'j', ctrlKey: true });+ expect(isShown('Which time window?')).toBe(true);++ const last = screen.getByRole('textbox', { name: /Which time window/ });+ fireEvent.change(last, { target: { value: 'Today' } });+ fireEvent.keyDown(last, { key: 'j', ctrlKey: true });+ expect(mockSubmitAskAnswer).toHaveBeenCalledTimes(1);+ });++ describe('focus on arrival', () => {+ afterEach(() => {+ document.getElementById('prompt-textarea')?.remove();+ });++ test('lands on the first option when the question offers choices', () => {+ renderBatch('ask-focus-option');+ expect(screen.getByRole('button', { name: /Staging/ })).toHaveFocus();+ });++ test('lands in the answer field when the question has no choices', () => {+ renderBatch('ask-focus-field', [{ id: 'window', question: 'Which time window?' }]);+ expect(screen.getByRole('textbox', { name: /Which time window/ })).toHaveFocus();+ });++ test('takes focus from the empty composer that sent the turn', () => {+ const composer = document.createElement('textarea');+ composer.id = 'prompt-textarea';+ document.body.appendChild(composer);+ composer.focus();++ renderBatch('ask-focus-composer');+ expect(screen.getByRole('button', { name: /Staging/ })).toHaveFocus();+ });++ test('stays put on a surface that is not the live pause', () => {+ renderBatch('ask-focus-not-live', questions, false);+ expect(document.body).toHaveFocus();+ });++ test('happens once per batch, not again when it moves between surfaces', () => {+ const view = renderBatch('ask-focus-once');+ expect(screen.getByRole('button', { name: /Staging/ })).toHaveFocus();+ view.unmount();++ renderBatch('ask-focus-once');+ expect(document.body).toHaveFocus();+ });++ test('leaves focus alone while the user is drafting in the composer', () => {+ const composer = document.createElement('textarea');+ composer.id = 'prompt-textarea';+ composer.value = 'half a thought';+ document.body.appendChild(composer);+ composer.focus();++ renderBatch('ask-focus-draft');+ expect(composer).toHaveFocus();+ });+ });++ describe('option keyboard', () => {+ /** A keyboard Enter on a button is its click; jsdom does not synthesize+ * that, so the click fires only when the handler leaves Enter alone. */+ const pressEnter = (option: HTMLElement) => {+ if (fireEvent.keyDown(option, { key: 'Enter' })) {+ fireEvent.click(option);+ }+ };++ test('Enter on an option mid-batch picks it and moves to the next question', () => {+ renderBatch('ask-option-enter');++ pressEnter(screen.getByRole('button', { name: /Production/ }));++ expect(isShown('Which time window?')).toBe(true);+ expect(screen.getByRole('textbox', { name: /Which time window/ })).toHaveFocus();+ });++ test('on the last question, Enter picks an option and a second Enter submits', () => {+ renderBatch('ask-option-submit', [+ { id: 'window', question: 'Which time window?' },+ {+ id: 'environment',+ question: 'Where should this run?',+ options: [+ { label: 'Staging', value: 'staging' },+ { label: 'Production', value: 'production' },+ ],+ },+ ]);+ fireEvent.change(screen.getByRole('textbox', { name: /Which time window/ }), {+ target: { value: 'Today' },+ });+ fireEvent.click(screen.getByRole('button', { name: 'Next' }));++ const staging = screen.getByRole('button', { name: /Staging/ });+ pressEnter(staging);+ expect(staging).toHaveAttribute('aria-pressed', 'true');+ expect(mockSubmitAskAnswer).not.toHaveBeenCalled();++ pressEnter(staging);+ expect(mockSubmitAskAnswer).toHaveBeenCalledWith(+ 'ask-option-submit',+ { window: 'Today', environment: 'staging' },+ expect.objectContaining({ onSuccess: expect.any(Function) }),+ );+ });++ test('a held Enter that picks an option on the last question does not also submit', () => {+ renderBatch('ask-option-held-last', [+ {+ id: 'environment',+ question: 'Where should this run?',+ options: [+ { label: 'Staging', value: 'staging' },+ { label: 'Production', value: 'production' },+ ],+ },+ ]);+ const staging = screen.getByRole('button', { name: /Staging/ });+ pressEnter(staging);+ expect(staging).toHaveAttribute('aria-pressed', 'true');++ fireEvent.keyDown(staging, { key: 'Enter', repeat: true });+ fireEvent.keyDown(staging, { key: 'Enter', repeat: true });+ expect(mockSubmitAskAnswer).not.toHaveBeenCalled();++ pressEnter(staging);+ expect(mockSubmitAskAnswer).toHaveBeenCalledTimes(1);+ });++ test('a held or double-tapped Enter from the answer field never picks the next options', () => {+ renderBatch('ask-option-held', optionBatch);+ const first = screen.getByRole('textbox', { name: /Which time window/ });+ fireEvent.change(first, { target: { value: 'Today' } });+ fireEvent.keyDown(first, { key: 'Enter' });++ expect(isShown('Where should this run?')).toBe(true);+ /* Focus waits in the answer field, not on an option, so the repeats+ reach a field: they can page on, but never pick an option for the user. */+ expect(screen.getByRole('textbox', { name: /Where should this run/ })).toHaveFocus();+ for (let i = 0; i < 3; i++) {+ pressEnter(document.activeElement as HTMLElement);+ }++ expect(screen.queryByRole('button', { pressed: true })).not.toBeInTheDocument();+ expect(mockSubmitAskAnswer).not.toHaveBeenCalled();+ });++ test('Enter on a multi-select option toggles it and never submits', () => {+ renderBatch('ask-option-multi', [+ {+ id: 'regions',+ question: 'Which regions?',+ multiSelect: true,+ options: [+ { label: 'us-east', value: 'us-east' },+ { label: 'eu-west', value: 'eu-west' },+ ],+ },+ ]);+ const usEast = screen.getByRole('checkbox', { name: /us-east/ });+ fireEvent.click(usEast);++ pressEnter(usEast);+ pressEnter(screen.getByRole('checkbox', { name: /eu-west/ }));++ expect(usEast).toHaveAttribute('aria-checked', 'false');+ expect(screen.getByRole('checkbox', { name: /eu-west/ })).toHaveAttribute(+ 'aria-checked',+ 'true',+ );+ expect(mockSubmitAskAnswer).not.toHaveBeenCalled();+ });});test('retains partial answers and the current step across surface remounts', () => {@@ -348,4 +609,47 @@ describe('AskUserQuestions', () => {expect.objectContaining({ onSuccess: expect.any(Function) }),);});++ describe('Escape', () => {+ test('skips the batch when nothing is answered yet', () => {+ renderBatch('ask-escape-skip');+ fireEvent.keyDown(screen.getByRole('button', { name: /Staging/ }), { key: 'Escape' });+ expect(mockSubmitAskAnswer).toHaveBeenCalledWith(+ 'ask-escape-skip',+ { environment: ASK_USER_DECLINED_ANSWER, window: ASK_USER_DECLINED_ANSWER },+ expect.objectContaining({ onSuccess: expect.any(Function) }),+ );+ });++ test('keeps partial answers instead of skipping', () => {+ renderBatch('ask-escape-partial');+ fireEvent.click(screen.getByRole('button', { name: /Staging/ }));+ fireEvent.keyDown(screen.getByRole('textbox', { name: /Your answer/ }), { key: 'Escape' });+ expect(mockSubmitAskAnswer).not.toHaveBeenCalled();+ });++ test('ignores Escape that cancels an IME composition or carries a modifier', () => {+ renderBatch('ask-escape-ime', [{ id: 'window', question: 'Which time window?' }]);+ const field = screen.getByRole('textbox', { name: /Your answer/ });+ fireEvent.keyDown(field, { key: 'Escape', isComposing: true });+ fireEvent.keyDown(field, { key: 'Escape', shiftKey: true });+ expect(mockSubmitAskAnswer).not.toHaveBeenCalled();+ });++ test('hands Escape to the surface instead of skipping when it supplies one', () => {+ const onEscape = jest.fn();+ render(+ <RecoilRoot>+ <AskUserQuestions+ actionId="ask-escape-surface"+ questions={questions}+ onEscape={onEscape}+ />+ </RecoilRoot>,+ );+ fireEvent.keyDown(screen.getByRole('button', { name: /Staging/ }), { key: 'Escape' });+ expect(onEscape).toHaveBeenCalledTimes(1);+ expect(mockSubmitAskAnswer).not.toHaveBeenCalled();+ });+ });});diff --git a/client/src/hooks/Input/useAskQuestionsForm.ts b/client/src/hooks/Input/useAskQuestionsForm.tsindex 5357831f9b..f03cc1fcf9 100644--- a/client/src/hooks/Input/useAskQuestionsForm.ts+++ b/client/src/hooks/Input/useAskQuestionsForm.ts@@ -15,9 +15,17 @@ interface AskQuestionsFormState {step: number;text: Record<string, string>;selected: Record<string, string[]>;+ /** The batch's arrival already placed focus. Moving it between the composer+ * popover and the chat card remounts it, and that is not a new arrival. */+ arrivalFocused: boolean;}-const EMPTY_FORM: AskQuestionsFormState = { step: 0, text: {}, selected: {} };+const EMPTY_FORM: AskQuestionsFormState = {+ step: 0,+ text: {},+ selected: {},+ arrivalFocused: false,+};/** Keyed by action id, so the composer popover and the chat card share one form. */const askQuestionsFormFamily = atomFamily((_actionId: string) =>@@ -86,6 +94,14 @@ export default function useAskQuestionsForm([questions.length, setState],);+ const markArrivalFocused = useCallback(+ () =>+ setState((previous) =>+ previous.arrivalFocused ? previous : { ...previous, arrivalFocused: true },+ ),+ [setState],+ );+const answers = useMemo(() => {const resolved = Object.create(null) as Record<string, string>;for (const question of questions) {@@ -138,5 +154,7 @@ export default function useAskQuestionsForm(goToStep,submit,skip,+ arrivalFocused: state.arrivalFocused,+ markArrivalFocused,};}diff --git a/client/src/utils/shortcuts.spec.ts b/client/src/utils/shortcuts.spec.tsindex eff456ed6e..3cfc880901 100644--- a/client/src/utils/shortcuts.spec.ts+++ b/client/src/utils/shortcuts.spec.ts@@ -2,6 +2,7 @@ import type { ShortcutBinding, ComposerKeyContext } from './shortcuts';import {hasModifier,isCancelKey,+ isReboundSubmitChord,bindingHash,normalizeKey,parseBinding,@@ -222,6 +223,46 @@ describe('resolveSubmitOverrideAction', () => {});});+describe('isReboundSubmitChord', () => {+ const ctrlJ = makeBinding({ ctrl: true, key: 'J' });+ const press = (init: KeyboardEventInit) => new KeyboardEvent('keydown', init);++ it('matches a submit chord rebound off Enter', () => {+ expect(+ isReboundSubmitChord(press({ key: 'j', ctrlKey: true }), {+ shortcutsEnabled: true,+ submitOverride: ctrlJ,+ }),+ ).toBe(true);+ });++ it('leaves Enter chords, other keys, unset or unbound overrides and disabled shortcuts alone', () => {+ const ctrlEnter = makeBinding({ ctrl: true, key: 'Enter' });+ const event = press({ key: 'j', ctrlKey: true });+ expect(+ isReboundSubmitChord(press({ key: 'Enter', ctrlKey: true }), {+ shortcutsEnabled: true,+ submitOverride: ctrlEnter,+ }),+ ).toBe(false);+ expect(+ isReboundSubmitChord(press({ key: 'k', ctrlKey: true }), {+ shortcutsEnabled: true,+ submitOverride: ctrlJ,+ }),+ ).toBe(false);+ expect(isReboundSubmitChord(event, { shortcutsEnabled: true, submitOverride: undefined })).toBe(+ false,+ );+ expect(isReboundSubmitChord(event, { shortcutsEnabled: true, submitOverride: null })).toBe(+ false,+ );+ expect(isReboundSubmitChord(event, { shortcutsEnabled: false, submitOverride: ctrlJ })).toBe(+ false,+ );+ });+});+describe('isCancelKey', () => {it('is true for a bare Escape press', () => {expect(isCancelKey(new KeyboardEvent('keydown', { key: 'Escape' }))).toBe(true);diff --git a/client/src/utils/shortcuts.ts b/client/src/utils/shortcuts.tsindex 0da06e3547..effebb71c0 100644--- a/client/src/utils/shortcuts.ts+++ b/client/src/utils/shortcuts.ts@@ -321,6 +321,23 @@ export function resolveComposerKeyDown(return 'none';}+/**+ * A `submitMessage` chord rebound off Enter. The window-level handler yields+ * it in every editing field but the composer, so an answer field that submits+ * like the composer has to act on it itself; Enter chords go through+ * `resolveComposerKeyDown`.+ */+export function isReboundSubmitChord(+ e: KeyChordSource,+ ctx: Pick<ComposerKeyContext, 'shortcutsEnabled' | 'submitOverride'>,+): boolean {+ const { submitOverride } = ctx;+ if (!ctx.shortcutsEnabled || submitOverride == null || submitOverride.key === 'Enter') {+ return false;+ }+ return bindingsMatch(bindingFromEvent(e), submitOverride);+}+export function isCancelKey(e: KeyboardEvent): boolean {return e.key === 'Escape' && !e.metaKey && !e.ctrlKey && !e.altKey && !e.shiftKey;}diff --git a/e2e/specs/mock/scenarios/ask-keyboard.spec.ts b/e2e/specs/mock/scenarios/ask-keyboard.spec.tsnew file mode 100644index 0000000000..d761e1e438--- /dev/null+++ b/e2e/specs/mock/scenarios/ask-keyboard.spec.ts@@ -0,0 +1,227 @@+import { expect, test } from '@playwright/test';+import type { Page, Request } from '@playwright/test';+import type { AgentDetail } from '../agents.helpers';+import { cleanupAgent, openAgentBuilder, uniqueAgentName } from '../agents.helpers';+import {+ MOCK_ENDPOINTS,+ NEW_CHAT_PATH,+ escapeRegExp,+ getAccessToken,+ messagesView,+ requestJson,+ sendMessage,+} from '../helpers';++/**+ * Keyboard control of a live `ask_user_question` batch. The fake model+ * (e2e/setup/fake-model.js) pauses a real agent run with three questions for+ * `E2E_ASK_USER_QUESTIONS:`, each with two options; popover, card, form state+ * and resume request are the production ones.+ */++test.use({ viewport: { width: 1280, height: 800 }, isMobile: false, hasTouch: false });++type AskResumeBody = { actionId?: string; answers?: Record<string, string> };++const ASK_TOOL_ID = 'ask_user_question';+const MOVE_TO_CHAT = 'Answer later in the chat';+const MOVE_TO_COMPOSER = 'Answer from the message box';+const DECLINED = 'The user chose not to answer this question.';++const isResumeRequest = (request: Request) =>+ request.method() === 'POST' && new URL(request.url()).pathname === '/api/agents/chat/resume';++const questionsFor = (label: string) => ({+ environment: `Which environment for ${label}?`,+ region: `Which region for ${label}?`,+ notes: `Anything else for ${label}?`,+});++async function createAgent(page: Page): Promise<AgentDetail> {+ await page.goto(NEW_CHAT_PATH, { timeout: 15000 });+ const token = await getAccessToken(page);+ const agent = await requestJson<AgentDetail>(page, {+ path: '/api/agents',+ token,+ method: 'POST',+ body: {+ name: uniqueAgentName('E2E Ask Keyboard Agent'),+ description: 'Pauses at ask_user_question for the keyboard scenarios.',+ instructions: 'Ask the user the question the test requests.',+ provider: MOCK_ENDPOINTS[0].label,+ model: MOCK_ENDPOINTS[0].model,+ tools: [ASK_TOOL_ID],+ },+ });+ expect(agent.tools).toEqual(expect.arrayContaining([ASK_TOOL_ID]));+ return agent;+}++async function openBatch(page: Page, agent: AgentDetail, label: string) {+ const form = await openAgentBuilder(page);+ await form.getByRole('combobox', { name: 'Agent', exact: true }).click();+ await page.getByRole('option', { name: agent.name }).click();+ await expect(form.getByLabel('Agent name')).toHaveValue(agent.name ?? '');+ await form.getByRole('button', { name: 'Select Agent' }).click();++ const response = await sendMessage(page, `E2E_ASK_USER_QUESTIONS:${label}`);+ expect(response.ok()).toBeTruthy();+ await expect(page).toHaveURL(/\/c\/(?!new)/, { timeout: 15000 });+ const popover = page+ .locator('.popover')+ .filter({ has: page.getByRole('button', { name: MOVE_TO_CHAT }) });+ await expect(popover).toBeVisible({ timeout: 30000 });+ return popover;+}++type Scope = Page | ReturnType<Page['locator']>;++const answerField = (scope: Scope, question: string) =>+ scope.getByRole('textbox', { name: new RegExp(`^${escapeRegExp(question)}`) });++const optionRow = (scope: Scope, name: string) =>+ scope.getByRole('button', { name: new RegExp(`${escapeRegExp(name)}$`) });++const uniqueLabel = () => `${Date.now()}-${Math.floor(Math.random() * 1e4)}`;++test.describe('ask question keyboard control', () => {+ test('a batch is answered and submitted from the keyboard alone @scenario:ask-batch-completes-from-keyboard', async ({+ page,+ }) => {+ test.setTimeout(120000);+ const label = uniqueLabel();+ const questions = questionsFor(label);+ let agentId: string | undefined;++ try {+ const agent = await createAgent(page);+ agentId = agent.id;+ const popover = await openBatch(page, agent, label);++ /* The batch arrives while focus sits in the empty composer that sent it. */+ await expect(optionRow(popover, 'Staging')).toBeFocused();+ await page.keyboard.press('Enter');++ /* The next question has no pick yet, so focus lands in its answer field,+ never on an unpicked option a held Enter could choose. */+ await expect(popover.getByText(questions.region, { exact: true })).toBeVisible();+ await expect(answerField(popover, questions.region)).toBeFocused();+ await page.keyboard.type('typed-region');+ await page.keyboard.press('Enter');++ await expect(popover.getByText(questions.notes, { exact: true })).toBeVisible();+ await expect(answerField(popover, questions.notes)).toBeFocused();+ await page.keyboard.press('Shift+Tab');+ await expect(optionRow(popover, 'Call me first')).toBeFocused();++ const resumes: Request[] = [];+ page.on('request', (request) => {+ if (isResumeRequest(request)) {+ resumes.push(request);+ }+ });++ /* On the last question the first Enter only picks. */+ await page.keyboard.press('Enter');+ await expect(optionRow(popover, 'Call me first')).toHaveAttribute('aria-pressed', 'true');+ expect(resumes).toHaveLength(0);++ const [resume] = await Promise.all([+ page.waitForRequest(isResumeRequest),+ page.keyboard.press('Enter'),+ ]);+ expect((resume.postDataJSON() as AskResumeBody).answers).toEqual({+ environment: 'staging',+ region: 'typed-region',+ notes: 'call',+ });+ await expect(popover).toBeHidden({ timeout: 30000 });+ } finally {+ await cleanupAgent(page, agentId);+ }+ });++ test('Escape in the composer popover moves the batch to the chat and returns to the composer @scenario:ask-popover-escape-moves-to-chat', async ({+ page,+ }) => {+ test.setTimeout(120000);+ const label = uniqueLabel();+ const questions = questionsFor(label);+ let agentId: string | undefined;++ try {+ const agent = await createAgent(page);+ agentId = agent.id;+ const popover = await openBatch(page, agent, label);++ const resumes: string[] = [];+ page.on('request', (request) => {+ if (isResumeRequest(request)) {+ resumes.push(request.url());+ }+ });++ await expect(optionRow(popover, 'Staging')).toBeFocused();+ await page.keyboard.press('Escape');++ await expect(popover).toBeHidden();+ const chat = messagesView(page);+ await expect(chat.getByText(questions.environment, { exact: true })).toBeVisible();+ await expect(chat.getByRole('button', { name: MOVE_TO_COMPOSER })).toBeVisible();+ const composer = page.getByRole('textbox', { name: 'Message input' });+ await expect(composer).toBeEnabled();+ await expect(composer).toBeFocused();+ expect(resumes).toEqual([]);+ } finally {+ await cleanupAgent(page, agentId);+ }+ });++ test('Escape in the chat card skips the batch only while nothing is answered @scenario:ask-card-escape-skips-only-when-empty', async ({+ page,+ }) => {+ test.setTimeout(120000);+ const label = uniqueLabel();+ const questions = questionsFor(label);+ let agentId: string | undefined;++ try {+ const agent = await createAgent(page);+ agentId = agent.id;+ const popover = await openBatch(page, agent, label);+ await popover.getByRole('button', { name: MOVE_TO_CHAT }).click();+ await expect(popover).toBeHidden();++ const chat = messagesView(page);+ const field = answerField(chat, questions.environment);+ await expect(field).toBeVisible();++ const resumes: Request[] = [];+ page.on('request', (request) => {+ if (isResumeRequest(request)) {+ resumes.push(request);+ }+ });++ /* A typed answer is kept: Escape neither clears it nor resumes the run. */+ await field.fill('half-typed');+ await field.press('Escape');+ await expect(field).toHaveValue('half-typed');+ await expect(chat.getByRole('button', { name: 'Skip', exact: true })).toBeEnabled();++ await field.fill('');+ const [resume] = await Promise.all([+ page.waitForRequest(isResumeRequest),+ field.press('Escape'),+ ]);+ expect((resume.postDataJSON() as AskResumeBody).answers).toEqual({+ environment: DECLINED,+ region: DECLINED,+ notes: DECLINED,+ });+ expect(resumes).toHaveLength(1);+ } finally {+ await cleanupAgent(page, agentId);+ }+ });+});