π OpenHands: AI-Driven Development
fix: accept Ctrl for confirmation shortcuts off macOS (#18127)
Denis Ermakov1dfb410530a0
diff --git a/__tests__/components/shared/buttons/conversation-confirmation-buttons.test.tsx b/__tests__/components/shared/buttons/conversation-confirmation-buttons.test.tsxindex 6dd63a65bb..e1b54c7789 100644--- a/__tests__/components/shared/buttons/conversation-confirmation-buttons.test.tsx+++ b/__tests__/components/shared/buttons/conversation-confirmation-buttons.test.tsx@@ -1,5 +1,5 @@import { fireEvent, render, screen } from "@testing-library/react";-import { describe, expect, it, vi } from "vitest";+import { afterEach, describe, expect, it, vi } from "vitest";import type { ReactNode } from "react";import { ConversationConfirmationButtons } from "#/components/shared/buttons/conversation-confirmation-buttons";import { AgentState } from "#/types/agent-state";@@ -114,6 +114,10 @@ const setupState = () => {};describe("conversation confirmation controls", () => {+ afterEach(() => {+ vi.restoreAllMocks();+ });+it("stays hidden when the agent is not awaiting confirmation", () => {setupState();eventState.events = [actionEvent("action-1")];@@ -147,16 +151,24 @@ describe("conversation confirmation controls", () => {).not.toBeInTheDocument();});- it("stays hidden after the awaiting event has already been submitted", () => {+ it("stays hidden and ignores shortcuts after the awaiting event has already been submitted", () => {setupState();eventState.events = [actionEvent("action-1")];eventMessageState.submittedEventIds = ["action-1"];render(<ConversationConfirmationButtons />);+ const continueEvent = new KeyboardEvent("keydown", {+ key: "Enter",+ ctrlKey: true,+ cancelable: true,+ });+ document.dispatchEvent(continueEvent);expect(screen.queryByTestId("action-confirm-button"),).not.toBeInTheDocument();+ expect(continueEvent.defaultPrevented).toBe(false);+ expect(respondToConfirmationMock).not.toHaveBeenCalled();});it("uses the latest agent event and submits rejection", () => {@@ -260,56 +272,100 @@ describe("conversation confirmation controls", () => {});});- it("supports cancel and continue keyboard shortcuts and ignores near misses", () => {- setupState();- eventState.events = [actionEvent("action-1")];- const { unmount } = render(<ConversationConfirmationButtons />);-- const wrongCancel = new KeyboardEvent("keydown", {- key: "Backspace",- shiftKey: true,- metaKey: false,- cancelable: true,- });- document.dispatchEvent(wrongCancel);- const otherKey = new KeyboardEvent("keydown", {- key: "Escape",- shiftKey: true,- metaKey: true,- cancelable: true,- });- document.dispatchEvent(otherKey);- const cancel = new KeyboardEvent("keydown", {- key: "Backspace",- shiftKey: true,- metaKey: true,- cancelable: true,- });- document.dispatchEvent(cancel);- const continueEvent = new KeyboardEvent("keydown", {- key: "Enter",- metaKey: true,- cancelable: true,- });- document.dispatchEvent(continueEvent);-- expect(wrongCancel.defaultPrevented).toBe(false);- expect(otherKey.defaultPrevented).toBe(false);- expect(cancel.defaultPrevented).toBe(true);- expect(continueEvent.defaultPrevented).toBe(true);- expect(respondToConfirmationMock).toHaveBeenNthCalledWith(- 1,- expect.objectContaining({ accept: false }),- );- expect(respondToConfirmationMock).toHaveBeenNthCalledWith(- 2,- expect.objectContaining({ accept: true }),- );-- unmount();- document.dispatchEvent(- new KeyboardEvent("keydown", { key: "Enter", metaKey: true }),- );- expect(respondToConfirmationMock).toHaveBeenCalledTimes(2);- });+ it.each(["metaKey", "ctrlKey"] as const)(+ "supports cancel and continue keyboard shortcuts with %s and ignores near misses",+ (modifier) => {+ setupState();+ eventState.events = [actionEvent("action-1")];+ const { unmount } = render(<ConversationConfirmationButtons />);++ const wrongCancel = new KeyboardEvent("keydown", {+ key: "Backspace",+ shiftKey: true,+ [modifier]: false,+ cancelable: true,+ });+ document.dispatchEvent(wrongCancel);+ const otherKey = new KeyboardEvent("keydown", {+ key: "Escape",+ shiftKey: true,+ [modifier]: true,+ cancelable: true,+ });+ document.dispatchEvent(otherKey);+ const heldKey = new KeyboardEvent("keydown", {+ key: "Enter",+ [modifier]: true,+ repeat: true,+ cancelable: true,+ });+ document.dispatchEvent(heldKey);+ const cancel = new KeyboardEvent("keydown", {+ key: "Backspace",+ shiftKey: true,+ [modifier]: true,+ cancelable: true,+ });+ document.dispatchEvent(cancel);+ const continueEvent = new KeyboardEvent("keydown", {+ key: "Enter",+ [modifier]: true,+ cancelable: true,+ });+ document.dispatchEvent(continueEvent);++ expect(wrongCancel.defaultPrevented).toBe(false);+ expect(otherKey.defaultPrevented).toBe(false);+ expect(heldKey.defaultPrevented).toBe(false);+ expect(cancel.defaultPrevented).toBe(true);+ expect(continueEvent.defaultPrevented).toBe(true);+ expect(respondToConfirmationMock).toHaveBeenNthCalledWith(+ 1,+ expect.objectContaining({ accept: false }),+ );+ expect(respondToConfirmationMock).toHaveBeenNthCalledWith(+ 2,+ expect.objectContaining({ accept: true }),+ );++ unmount();+ document.dispatchEvent(+ new KeyboardEvent("keydown", { key: "Enter", [modifier]: true }),+ );+ expect(respondToConfirmationMock).toHaveBeenCalledTimes(2);+ },+ );++ it.each([+ ["macOS", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)", "ββ©", "β§ββ«"],+ ["Linux", "Mozilla/5.0 (X11; Linux x86_64)", "Ctrl+β©", "Ctrl+β§+β«"],+ [+ "Windows",+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",+ "Ctrl+β©",+ "Ctrl+β§+β«",+ ],+ ])(+ "labels the shortcuts with the %s modifier",+ async (_platform, userAgent, continueHint, cancelHint) => {+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue(userAgent);+ const { ActionTooltip } = await vi.importActual<+ typeof import("#/components/shared/action-tooltip")+ >("#/components/shared/action-tooltip");++ render(+ <>+ <ActionTooltip type="confirm" onClick={vi.fn()} />+ <ActionTooltip type="reject" onClick={vi.fn()} />+ </>,+ );++ expect(screen.getByTestId("action-confirm-button")).toHaveTextContent(+ `CHAT_INTERFACE$INPUT_CONTINUE_MESSAGE ${continueHint}`,+ );+ expect(screen.getByTestId("action-reject-button")).toHaveTextContent(+ `BUTTON$CANCEL ${cancelHint}`,+ );+ },+ );});diff --git a/src/components/shared/action-tooltip.tsx b/src/components/shared/action-tooltip.tsxindex 820b12a5c7..54f1587659 100644--- a/src/components/shared/action-tooltip.tsx+++ b/src/components/shared/action-tooltip.tsx@@ -1,8 +1,13 @@import { useTranslation } from "react-i18next";import { I18nKey } from "#/i18n/declaration";-import { cn } from "#/utils/utils";+import { cn, isApplePlatform } from "#/utils/utils";import { StyledTooltip } from "./buttons/styled-tooltip";+const SHORTCUT_HINTS = {+ apple: { confirm: "ββ©", reject: "β§ββ«" },+ other: { confirm: "Ctrl+β©", reject: "Ctrl+β§+β«" },+} as const;+interface ActionTooltipProps {type: "confirm" | "reject";onClick: () => void;@@ -21,9 +26,11 @@ export function ActionTooltip({ type, onClick }: ActionTooltipProps) {? t(I18nKey.CHAT_INTERFACE$USER_CONFIRMED): t(I18nKey.CHAT_INTERFACE$USER_REJECTED);+ const shortcutHint = SHORTCUT_HINTS[isApplePlatform() ? "apple" : "other"];+const buttonLabel = isConfirm- ? `${t(I18nKey.CHAT_INTERFACE$INPUT_CONTINUE_MESSAGE)} ββ©`- : `${t(I18nKey.BUTTON$CANCEL)} β§ββ«`;+ ? `${t(I18nKey.CHAT_INTERFACE$INPUT_CONTINUE_MESSAGE)} ${shortcutHint.confirm}`+ : `${t(I18nKey.BUTTON$CANCEL)} ${shortcutHint.reject}`;return (<StyledTooltip closeDelay={100} content={content}>diff --git a/src/components/shared/buttons/conversation-confirmation-buttons.tsx b/src/components/shared/buttons/conversation-confirmation-buttons.tsxindex 0c6a12cf00..a0ff9ff88c 100644--- a/src/components/shared/buttons/conversation-confirmation-buttons.tsx+++ b/src/components/shared/buttons/conversation-confirmation-buttons.tsx@@ -35,6 +35,10 @@ export function ConversationConfirmationButtons() {return curAgentState === AgentState.AWAITING_USER_CONFIRMATION;});+ const hasSubmitted =+ awaitingAction?.id !== undefined &&+ submittedEventIds.includes(awaitingAction.id);+const handleConfirmation = useCallback((accept: boolean) => {if (!awaitingAction || !conversation) {@@ -59,42 +63,48 @@ export function ConversationConfirmationButtons() {// Handle keyboard shortcutsuseEffect(() => {- if (!awaitingAction) {+ // Ignore shortcuts once a response was sent, so a second press cannot+ // submit another (or the opposite) answer before the server update arrives+ if (!awaitingAction || hasSubmitted) {return undefined;}const handleCancelShortcut = (event: KeyboardEvent) => {- if (event.shiftKey && event.metaKey && event.key === "Backspace") {+ if (+ event.shiftKey &&+ (event.metaKey || event.ctrlKey) &&+ event.key === "Backspace"+ ) {event.preventDefault();handleConfirmation(false);}};const handleContinueShortcut = (event: KeyboardEvent) => {- if (event.metaKey && event.key === "Enter") {+ if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {event.preventDefault();handleConfirmation(true);}};const handleKeyDown = (event: KeyboardEvent) => {- // Cancel: Shift+Cmd+Backspace (β§ββ«)+ if (event.repeat) return;+ // Cancel: Shift+Cmd+Backspace (Mac) or Shift+Ctrl+Backspace (Windows/Linux)handleCancelShortcut(event);- // Continue: Cmd+Enter (ββ©)+ // Continue: Cmd+Enter (Mac) or Ctrl+Enter (Windows/Linux)handleContinueShortcut(event);};document.addEventListener("keydown", handleKeyDown);return () => document.removeEventListener("keydown", handleKeyDown);- }, [awaitingAction, handleConfirmation]);+ }, [awaitingAction, hasSubmitted, handleConfirmation]);// Only show if agent is waiting for confirmation and we haven't already submittedif (curAgentState !== AgentState.AWAITING_USER_CONFIRMATION ||!awaitingAction ||- (awaitingAction.id !== undefined &&- submittedEventIds.includes(awaitingAction.id))+ hasSubmitted) {return null;}