mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 21:08:17 +00:00
* ⌨️ 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 <ebtien@gmail.com>
293 lines
11 KiB
TypeScript
293 lines
11 KiB
TypeScript
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';
|
|
import AskUserQuestions from '~/components/Chat/Messages/Content/AskUserQuestions';
|
|
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';
|
|
|
|
/**
|
|
* Composer popover for a live `ask_user_question` pause. Single-select rows
|
|
* submit on click; a digit `1..N` activates its row like a click whether
|
|
* focus is in the composer or on the popover (arrows/Enter highlight+confirm
|
|
* from the empty composer); multi-select rows toggle checks and an explicit
|
|
* Submit confirms —
|
|
* folding in any free-form text typed in the composer, exactly like Enter
|
|
* would. The footer hint is a button that focuses the composer — the
|
|
* free-form answer box. The chevron moves the question to the chat card and
|
|
* releases the composer (the card's chevron moves it back). Pure rendering
|
|
* off {@link useAskAnswerMode}; disappears the moment an answer submits from
|
|
* any surface, and locks while one is in flight. Registers as an open composer
|
|
* panel for as long as it shows, so the thread's scroll-to-bottom control
|
|
* stands down instead of landing on its footer.
|
|
*/
|
|
function AskUserQuestionPopoverContent({
|
|
conversationId,
|
|
textAreaRef,
|
|
}: {
|
|
conversationId: string;
|
|
textAreaRef?: React.RefObject<HTMLTextAreaElement>;
|
|
}) {
|
|
const ask = useAskAnswerMode(conversationId);
|
|
const { collapse, popoverVisible } = ask;
|
|
useComposerOverlay(conversationId, popoverVisible);
|
|
|
|
/**
|
|
* 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} onEscape={escapeToChat} />;
|
|
}
|
|
|
|
return (
|
|
<AskUserQuestionPopoverPanel ask={ask} textAreaRef={textAreaRef} onEscape={escapeToChat} />
|
|
);
|
|
}
|
|
|
|
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) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div className="absolute bottom-full z-10 mb-2 w-full">
|
|
{/* The prompt and answers each scroll within their own caps; this outer cap keeps
|
|
the whole card on screen when both are at their limit on a short viewport. */}
|
|
<div className="popover border-border-light bg-surface-secondary rounded-theme-popover flex max-h-[70vh] flex-col overflow-y-auto border shadow-lg [view-transition-name:ask-question]">
|
|
{/* Kept at full height so the cap above scrolls it rather than clipping its end. */}
|
|
<AskUserQuestions
|
|
actionId={liveAsk.actionId}
|
|
questions={questions}
|
|
className="shrink-0"
|
|
focusOnArrival
|
|
onEscape={onEscape}
|
|
headerAction={<MoveToChatButton onClick={collapse} />}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Moves the question to the chat card and hands the composer back. */
|
|
function MoveToChatButton({ onClick }: { onClick: () => void }) {
|
|
const localize = useLocalize();
|
|
return (
|
|
<TooltipAnchor
|
|
description={localize('com_ui_ask_move_to_chat')}
|
|
side="top"
|
|
render={
|
|
<Button
|
|
variant="row-action"
|
|
size="icon-xs"
|
|
aria-label={localize('com_ui_ask_move_to_chat')}
|
|
onClick={onClick}
|
|
>
|
|
<ChevronDown
|
|
className="size-4 [view-transition-name:ask-question-chevron]"
|
|
aria-hidden="true"
|
|
/>
|
|
</Button>
|
|
}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Split from the gate above so the per-keystroke `useWatch` subscription only
|
|
* exists while the popover is actually visible — the invisible popover was
|
|
* re-rendering (to null) on every composer keystroke.
|
|
*/
|
|
function AskUserQuestionPopoverPanel({
|
|
ask,
|
|
textAreaRef,
|
|
onEscape,
|
|
}: {
|
|
ask: ReturnType<typeof useAskAnswerMode>;
|
|
textAreaRef?: React.RefObject<HTMLTextAreaElement>;
|
|
onEscape: () => void;
|
|
}) {
|
|
const localize = useLocalize();
|
|
const { control } = useChatFormContext();
|
|
/** Reactive composer text so multi-select Submit can include (and enable
|
|
* on) the free-form answer the footer hint invites. */
|
|
const composerText = (useWatch({ control, name: 'text' }) ?? '') as string;
|
|
const {
|
|
liveAsk,
|
|
options,
|
|
selected,
|
|
checked,
|
|
multiSelect,
|
|
locked,
|
|
errored,
|
|
toggleChecked,
|
|
canSubmit,
|
|
submit,
|
|
submitOption,
|
|
skip,
|
|
collapse,
|
|
handlePopoverKeyDown,
|
|
} = ask;
|
|
|
|
/** Keyboard selection only paints a highlight (no focus move), so the
|
|
* scrollable option list has to follow `selected` itself or arrow/digit
|
|
* navigation can land on a row that is scrolled out of view. Manual
|
|
* scrollTop math rather than scrollIntoView: it cannot disturb the page. */
|
|
const listRef = useRef<HTMLDivElement>(null);
|
|
const optionRefs = useRef<(HTMLButtonElement | null)[]>([]);
|
|
useEffect(() => {
|
|
if (typeof selected !== 'number') {
|
|
return;
|
|
}
|
|
const list = listRef.current;
|
|
const row = optionRefs.current[selected];
|
|
if (list == null || row == null) {
|
|
return;
|
|
}
|
|
const rowBottom = row.offsetTop + row.offsetHeight;
|
|
if (row.offsetTop < list.scrollTop) {
|
|
list.scrollTop = row.offsetTop;
|
|
} else if (rowBottom > list.scrollTop + list.clientHeight) {
|
|
list.scrollTop = rowBottom - list.clientHeight;
|
|
}
|
|
}, [selected]);
|
|
|
|
if (!liveAsk) {
|
|
return null;
|
|
}
|
|
|
|
const composerHasText = composerText.trim().length > 0;
|
|
|
|
return (
|
|
<div className="absolute bottom-full z-10 mb-2 w-full">
|
|
{/* Digit shortcuts (1..N) work when focus is inside the popover too, not
|
|
only from the composer — keydown bubbles here from the focused row/
|
|
control. Height is viewport-bounded with the option list as the only
|
|
scroll region: the panel is absolutely positioned, so anything that
|
|
overflows it is unreachable by page scroll. */}
|
|
<div
|
|
className="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={(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">
|
|
<p className="text-text-primary text-sm font-medium [overflow-wrap:anywhere]">
|
|
{liveAsk.question.question}
|
|
</p>
|
|
{liveAsk.question.description != null && liveAsk.question.description.length > 0 && (
|
|
<p className="text-text-secondary mt-1 text-sm [overflow-wrap:anywhere]">
|
|
{liveAsk.question.description}
|
|
</p>
|
|
)}
|
|
</div>
|
|
<MoveToChatButton onClick={collapse} />
|
|
</div>
|
|
<AskOptions
|
|
options={options}
|
|
multiSelect={multiSelect}
|
|
checked={checked}
|
|
selected={selected}
|
|
locked={locked}
|
|
onActivate={(index) => (multiSelect ? toggleChecked(index) : submitOption(index))}
|
|
optionRefs={optionRefs}
|
|
listRef={listRef}
|
|
className="relative min-h-0 flex-1 overflow-y-auto"
|
|
/>
|
|
{/** A failed submission keeps the question answerable (controls stay
|
|
* enabled), but the chat card that would show the error is hidden
|
|
* while the popover is up — so surface it here for retry guidance. */}
|
|
{errored && (
|
|
<div className="text-text-warning flex shrink-0 items-center gap-1.5 px-2 pt-1 text-xs">
|
|
<TriangleAlert className="h-4 w-4 shrink-0" aria-hidden="true" />
|
|
{localize('com_ui_ask_answer_error')}
|
|
</div>
|
|
)}
|
|
<div className="flex shrink-0 items-center justify-between gap-2 p-2">
|
|
{/* Shared primitive with the fill suppressed, the same way Summary's
|
|
quiet text buttons compose it: this reads as a hint, not a
|
|
control with a surface, but the focus ring and disabled handling
|
|
should still come from the recipe. */}
|
|
<Button
|
|
variant="ghost"
|
|
className="text-text-secondary h-auto cursor-text rounded-md p-0 text-xs font-normal hover:bg-transparent"
|
|
onClick={() => textAreaRef?.current?.focus()}
|
|
>
|
|
{options.length === 0
|
|
? localize('com_ui_ask_type_below_only')
|
|
: localize('com_ui_ask_type_below')}
|
|
</Button>
|
|
<div className="flex items-center gap-2">
|
|
<Button size="sm" variant="outline" disabled={locked} onClick={() => skip()}>
|
|
{localize('com_ui_skip')}
|
|
</Button>
|
|
{multiSelect && options.length > 0 && (
|
|
<Button
|
|
size="sm"
|
|
variant="submit"
|
|
disabled={locked || (!canSubmit && !composerHasText)}
|
|
onClick={() => submit(composerText)}
|
|
>
|
|
{localize('com_ui_submit')}
|
|
<CornerDownLeft className="ml-1.5 h-4 w-4" aria-hidden="true" />
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const AskUserQuestionPopover = memo(function AskUserQuestionPopover({
|
|
conversationId,
|
|
textAreaRef,
|
|
}: {
|
|
conversationId?: string | null;
|
|
textAreaRef?: React.RefObject<HTMLTextAreaElement>;
|
|
}) {
|
|
if (conversationId == null || conversationId === 'new') {
|
|
return null;
|
|
}
|
|
return (
|
|
<AskUserQuestionPopoverContent conversationId={conversationId} textAreaRef={textAreaRef} />
|
|
);
|
|
});
|
|
|
|
export default AskUserQuestionPopover;
|