π OpenHands: AI-Driven Development
fix(chat): build the plan with Ctrl/Cmd+Enter from the empty composer (#18544)
Engel Nyst57c108c38753
diff --git a/.pr/build-plan-shortcut/after-composer-ctrl-enter-1.png b/.pr/build-plan-shortcut/after-composer-ctrl-enter-1.pngnew file mode 100644index 0000000000..f7d44d8542Binary files /dev/null and b/.pr/build-plan-shortcut/after-composer-ctrl-enter-1.png differdiff --git a/.pr/build-plan-shortcut/after-composer-ctrl-enter-2.png b/.pr/build-plan-shortcut/after-composer-ctrl-enter-2.pngnew file mode 100644index 0000000000..d328fe8e20Binary files /dev/null and b/.pr/build-plan-shortcut/after-composer-ctrl-enter-2.png differdiff --git a/.pr/build-plan-shortcut/after-composer-with-text-sends.png b/.pr/build-plan-shortcut/after-composer-with-text-sends.pngnew file mode 100644index 0000000000..730a6d179fBinary files /dev/null and b/.pr/build-plan-shortcut/after-composer-with-text-sends.png differdiff --git a/.pr/build-plan-shortcut/before-composer-ctrl-enter.png b/.pr/build-plan-shortcut/before-composer-ctrl-enter.pngnew file mode 100644index 0000000000..4d5b4aa96bBinary files /dev/null and b/.pr/build-plan-shortcut/before-composer-ctrl-enter.png differdiff --git a/__tests__/components/chat/chat-interface.test.tsx b/__tests__/components/chat/chat-interface.test.tsxindex 7e88bf1818..8e58ac0936 100644--- a/__tests__/components/chat/chat-interface.test.tsx+++ b/__tests__/components/chat/chat-interface.test.tsx@@ -7,6 +7,7 @@ import {waitFor,within,} from "@testing-library/react";+import { flushSync } from "react-dom";import { MemoryRouter, Route, Routes } from "react-router";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import { renderWithProviders, useParamsMock } from "test-utils";@@ -1314,4 +1315,81 @@ describe("ChatInterface - Build plan keyboard shortcut", () => {expect(sentBuildPrompt()).toBe(true);});});++ it("sends the build prompt from the empty composer when the chat re-renders during the keypress", async () => {+ act(() => {+ useConversationStore.setState({+ conversationMode: "plan",+ planContent: "# Plan\n\n- step one",+ });+ });+ // In the app React's root listeners sit on `document` ahead of this+ // shortcut, and the browser commits the composer's keydown update between+ // listeners. Commit a re-render at that point.+ const commitMidKeypress = () => {+ flushSync(() => {+ useConversationStore.setState({+ planContent: "# Plan\n\n- step one\n- step two",+ });+ });+ };+ document.addEventListener("keydown", commitMidKeypress, { once: true });+ renderInterface();+ const composer = screen.getByTestId("chat-input");+ composer.focus();++ composer.dispatchEvent(+ new KeyboardEvent("keydown", {+ key: "Enter",+ ctrlKey: true,+ bubbles: true,+ cancelable: true,+ }),+ );++ await waitFor(() => {+ expect(sentBuildPrompt()).toBe(true);+ });+ });++ it("leaves Ctrl+Enter to a pending confirmation", () => {+ vi.mocked(useAgentState).mockReturnValue({+ curAgentState: AgentState.AWAITING_USER_CONFIRMATION,+ });+ act(() => {+ useConversationStore.setState({+ conversationMode: "plan",+ planContent: "# Plan\n\n- step one",+ });+ });++ try {+ renderInterface();+ pressBuildShortcut();++ expect(sentBuildPrompt()).toBe(false);+ } finally {+ vi.mocked(useAgentState).mockReturnValue({+ curAgentState: AgentState.AWAITING_USER_INPUT,+ });+ }+ });++ it("leaves Ctrl+Enter to the composer when it has text", () => {+ act(() => {+ useConversationStore.setState({+ conversationMode: "plan",+ planContent: "# Plan\n\n- step one",+ });+ });+ renderInterface();+ const composer = screen.getByTestId("chat-input");+ composer.focus();+ composer.textContent = "Refine step one";+ fireEvent.input(composer);++ fireEvent.keyDown(composer, { key: "Enter", ctrlKey: true });++ expect(sentBuildPrompt()).toBe(false);+ });});diff --git a/src/components/features/chat/chat-interface.tsx b/src/components/features/chat/chat-interface.tsxindex f30307656c..ba073a9113 100644--- a/src/components/features/chat/chat-interface.tsx+++ b/src/components/features/chat/chat-interface.tsx@@ -58,6 +58,23 @@ import { useActiveConversation } from "#/hooks/query/use-active-conversation";import { I18nKey } from "#/i18n/declaration";import { hasConversationStarted } from "./components/resolve-picker-kind";import { ComposerDockedProvider } from "#/context/composer-docked-context";+import { isContentEmpty } from "./utils/chat-input.utils";++// Ctrl/Cmd+Enter in a text field that holds text belongs to that field (the+// composer sends it); an empty field or any other target builds the plan.+function isTextFieldWithText(target: EventTarget | null): boolean {+ if (+ target instanceof HTMLInputElement ||+ target instanceof HTMLTextAreaElement+ ) {+ return target.value.trim() !== "";+ }+ if (target instanceof HTMLElement) {+ const editable = target.closest<HTMLDivElement>("[contenteditable='true']");+ return editable !== null && !isContentEmpty(editable);+ }+ return false;+}function getEntryPoint(hasRepository: boolean | null,@@ -164,15 +181,25 @@ export function ChatInterface({// when multiple PlanPreview components exist in the chat.// Gated on the same conditions as the Build button// (ConversationPlannerBuildBar's `isBuildDisabled`) so it cannot fire- // outside the plan flow.+ // outside the plan flow. While an action awaits confirmation the same keys+ // confirm it, so the build shortcut stands aside.React.useEffect(() => {- if (isAgentRunning || conversationMode !== "plan" || !planContent) {+ if (+ isAgentRunning ||+ curAgentState === AgentState.AWAITING_USER_CONFIRMATION ||+ conversationMode !== "plan" ||+ !planContent+ ) {return undefined;}const handleKeyDown = (event: KeyboardEvent) => {// Check for Cmd+Enter (Mac) or Ctrl+Enter (Windows/Linux)- if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {+ if (+ (event.metaKey || event.ctrlKey) &&+ event.key === "Enter" &&+ !isTextFieldWithText(event.target)+ ) {event.preventDefault();event.stopPropagation();handleBuildPlanClick(event);@@ -180,13 +207,18 @@ export function ChatInterface({}};- document.addEventListener("keydown", handleKeyDown);+ // Capture phase, so this runs before React's own `document` listeners+ // hand the key to the focused composer: the re-render its handler commits+ // between listeners re-subscribes this effect, and the browser skips a+ // listener removed or added mid-dispatch.+ document.addEventListener("keydown", handleKeyDown, true);return () => {- document.removeEventListener("keydown", handleKeyDown);+ document.removeEventListener("keydown", handleKeyDown, true);};}, [isAgentRunning,+ curAgentState,conversationMode,planContent,handleBuildPlanClick,