From 9e9e22d3319678178dba05a76f0ea61064b78ee8 Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Sun, 25 Oct 2026 19:36:56 +0800 Subject: [PATCH] fix(webui): send waiting messages with empty Enter Use the current pending message for explicit empty Enter submissions instead of a transient second-Enter marker. Preserve automatic-send authorization and keyboard/voice guards. Refs NAN-357. --- webui/browser/queued-guidance.spec.ts | 72 ++++++++++++++++- .../src/components/thread/ThreadComposer.tsx | 30 ++------ webui/src/tests/queued-guidance.test.tsx | 12 ++- webui/src/tests/thread-composer.test.tsx | 77 +++++++++++++++---- 4 files changed, 150 insertions(+), 41 deletions(-) diff --git a/webui/browser/queued-guidance.spec.ts b/webui/browser/queued-guidance.spec.ts index 0ac62aba8..ff38ffe13 100644 --- a/webui/browser/queued-guidance.spec.ts +++ b/webui/browser/queued-guidance.spec.ts @@ -18,12 +18,82 @@ for (const width of [1440, 390]) { await page.goto(`${fixture}&complete`); await expect(page.getByRole("group", { name: "Waiting to send" })).toContainText("Confirm this instruction"); await expect(page.getByLabel("Submitted messages")).toHaveText("[]"); - await page.getByRole("button", { name: "Send now", exact: true }).click(); + await page.getByRole("textbox", { name: "Message input", exact: true }).press("Enter"); await expect(page.getByLabel("Submitted messages")).toHaveText('[{"text":"Confirm this instruction"}]'); await expect(page.getByRole("group", { name: "Waiting to send" })).toHaveCount(0); }); + + for (const interaction of ["refocus", "clear edits"] as const) { + test(`empty Enter sends waiting content after ${interaction} (${width}px)`, async ({ page }) => { + await page.setViewportSize({ width, height: 900 }); + await page.goto(fixture); + await queue(page); + const input = page.getByRole("textbox", { name: "Message input", exact: true }); + if (interaction === "refocus") { + await input.press("Shift+Tab"); + await expect(input).not.toBeFocused(); + await page.keyboard.press("Tab"); + } else { + await input.press("x"); + await input.press("Backspace"); + } + await expect(input).toBeFocused(); + await expect(input).toHaveValue(""); + await expect(page.getByLabel("Submitted messages")).toHaveText("[]"); + await input.press("Enter"); + await expect(page.getByLabel("Submitted messages")).toHaveText('[{"text":"Confirm this instruction","options":{"continueActiveTurn":true}}]'); + await expect(page.getByRole("group", { name: "Waiting to send" })).toHaveCount(0); + }); + } } +test("desktop Shift+Enter keeps waiting content until the editor is empty again", async ({ page }) => { + await page.goto(fixture); + await queue(page); + const input = page.getByRole("textbox", { name: "Message input", exact: true }); + await input.press("Shift+Enter"); + await expect(input).toHaveValue("\n"); + await input.press("Enter"); + await expect(page.getByLabel("Submitted messages")).toHaveText("[]"); + await expect(page.getByRole("group", { name: "Waiting to send" })).toContainText("Confirm this instruction"); + await input.press("Backspace"); + await expect(input).toHaveValue(""); + await input.press("Enter"); + await expect(page.getByLabel("Submitted messages")).toHaveText('[{"text":"Confirm this instruction","options":{"continueActiveTurn":true}}]'); +}); + +test("empty Enter sends waiting content after switching away and back", async ({ page }) => { + await page.goto(fixture); + await queue(page); + await page.getByRole("button", { name: "Switch topic" }).click(); + await page.getByRole("button", { name: "Switch topic" }).click(); + await expect(page.getByLabel("Submitted messages")).toHaveText("[]"); + await page.getByRole("textbox", { name: "Message input", exact: true }).press("Enter"); + await expect(page.getByLabel("Submitted messages")).toHaveText('[{"text":"Confirm this instruction","options":{"continueActiveTurn":true}}]'); + await expect(page.getByRole("group", { name: "Waiting to send" })).toHaveCount(0); +}); + +test("touch Enter keeps a restored message waiting until Send now is clicked", async ({ browser, baseURL }) => { + const context = await browser.newContext({ baseURL, viewport: { width: 390, height: 844 }, hasTouch: true, isMobile: true }); + try { + const page = await context.newPage(); + await page.goto(fixture); + await page.evaluate((key) => localStorage.setItem(key, JSON.stringify([ + { id: "touch-draft", text: "Confirm this instruction", autoPaused: true }, + ])), storageKey); + await page.reload(); + const input = page.getByRole("textbox", { name: "Message input", exact: true }); + await input.press("Enter"); + await expect(input).toHaveValue("\n"); + await expect(page.getByLabel("Submitted messages")).toHaveText("[]"); + await expect(page.getByRole("group", { name: "Waiting to send" })).toContainText("Confirm this instruction"); + await page.getByRole("button", { name: "Send now", exact: true }).click(); + await expect(page.getByLabel("Submitted messages")).toHaveText('[{"text":"Confirm this instruction","options":{"continueActiveTurn":true}}]'); + } finally { + await context.close(); + } +}); + test("switching topics in the live page retains one automatic send", async ({ page }) => { await page.goto(fixture); await queue(page); diff --git a/webui/src/components/thread/ThreadComposer.tsx b/webui/src/components/thread/ThreadComposer.tsx index 3322ae865..73a59da2b 100644 --- a/webui/src/components/thread/ThreadComposer.tsx +++ b/webui/src/components/thread/ThreadComposer.tsx @@ -1186,8 +1186,6 @@ export function ThreadComposer({ const fileInputRef = useRef(null); const chipRefs = useRef(new Map()); const queuedPromptCounterRef = useRef(0); - // A fresh queue submission arms Enter for the complete pending message. - const secondEnterPromptIdRef = useRef(null); const previousPendingQueueKeyRef = useRef(pendingQueueKey); const previousQueueRunRef = useRef({ key: pendingQueueKey, isStreaming }); const voiceShortcutDownRef = useRef(false); @@ -1205,10 +1203,6 @@ export function ThreadComposer({ && workspaceControls?.can_change_project !== false; const showProjectPicker = projectPickerAvailable && !workspaceControlsHidden; - useEffect(() => { - secondEnterPromptIdRef.current = null; - }, [pendingQueueKey, queuedPromptStorageKey]); - useEffect(() => { if (!isStreaming || !activeTurnId) return; setQueuedPrompt((prompt) => ( @@ -1274,7 +1268,6 @@ export function ThreadComposer({ const addFiles = useCallback( (files: File[]) => { if (interactionDisabled || files.length === 0) return; - secondEnterPromptIdRef.current = null; const { rejected } = enqueue(files); if (rejected.length > 0) { const { reason, file } = rejected[0]; @@ -1532,7 +1525,6 @@ export function ThreadComposer({ [cliApps, mcpPresets, sessionMentionCatalog, value], ); const editMentionInput = useCallback((next: string, cursor: number) => { - secondEnterPromptIdRef.current = null; setValue(next); setSlashMenuDismissed(false); setCliAppMenuDismissed(false); @@ -1854,7 +1846,6 @@ export function ThreadComposer({ useLayoutEffect(() => { if (previousPendingQueueKeyRef.current === pendingQueueKey) return; previousPendingQueueKeyRef.current = pendingQueueKey; - secondEnterPromptIdRef.current = null; setValue(""); setInlineError(null); setSlashMenuDismissed(false); @@ -1866,7 +1857,6 @@ export function ThreadComposer({ const appendTranscription = useCallback((text: string) => { const transcript = text.trim(); if (!transcript) return; - secondEnterPromptIdRef.current = null; setValue((current) => { if (!current.trim()) return transcript; const separator = /[\s\n]$/.test(current) ? "" : " "; @@ -1917,7 +1907,6 @@ export function ThreadComposer({ function onKeyDown(event: KeyboardEvent): void { if (!isVoiceShortcutDown(event) || event.repeat || voiceShortcutDownRef.current) return; event.preventDefault(); - secondEnterPromptIdRef.current = null; voiceShortcutDownRef.current = true; voiceRecorder.beginShortcutHold(); } @@ -1946,7 +1935,6 @@ export function ThreadComposer({ }, [onTranscribeAudio, voiceRecorder.beginShortcutHold, voiceRecorder.endShortcutHold]); const handleStop = useCallback(() => { - secondEnterPromptIdRef.current = null; setQueuedPrompt((prompt) => prompt ? pauseQueuedPrompt(prompt) : null); onStop?.(); }, [onStop, setQueuedPrompt]); @@ -2116,7 +2104,6 @@ export function ThreadComposer({ setInlineError(error); return; } - secondEnterPromptIdRef.current = prompt.id; setQueuedPrompt(prompt); clear(); clearComposerText(); @@ -2124,14 +2111,12 @@ export function ThreadComposer({ }, [canQueueGuidance, clear, clearComposerText, collectGuidancePrompt, onQuotedContextChange, queuedPromptError, setQueuedPrompt]); const removeQueuedPrompt = useCallback(() => { - secondEnterPromptIdRef.current = null; setQueuedPrompt(null); requestAnimationFrame(() => textareaRef.current?.focus()); }, [setQueuedPrompt]); const editQueuedPrompt = useCallback((pending: QueuedPrompt) => { const prompt = hasComposerContent || normalizedQuotedContext ? collectGuidancePrompt() : pending; - secondEnterPromptIdRef.current = null; setQueuedPrompt(null); replaceMentionInput(prompt.text, prompt.text.length); setBoundSessionMentions(prompt.sessionMentions ?? []); @@ -2150,7 +2135,6 @@ export function ThreadComposer({ const sendQueuedPrompt = useCallback( (prompt: QueuedPrompt, extraOptions?: SendOptions) => { if (interactionDisabled) return; - secondEnterPromptIdRef.current = null; const error = queuedPromptError(prompt); if (error) { setQueuedPrompt(pauseQueuedPrompt(prompt)); @@ -2182,7 +2166,6 @@ export function ThreadComposer({ useEffect(() => { const previous = previousQueueRunRef.current; previousQueueRunRef.current = { key: pendingQueueKey, isStreaming }; - if (!isStreaming) secondEnterPromptIdRef.current = null; // Switching to an idle session is not completion of the previous session's run. if (previous.key !== pendingQueueKey) return; if (isStreaming || interactionDisabled || !queuedPrompt || !canAutoSendQueuedPrompt(queuedPrompt)) return; @@ -2401,20 +2384,20 @@ export function ThreadComposer({ if (!e.repeat) queueGuidancePrompt(); return; } - const secondEnterPrompt = queuedPrompt?.id === secondEnterPromptIdRef.current ? queuedPrompt : null; + // Empty Enter explicitly sends the visible pending message, including restored drafts. if ( - isStreaming + queuedPrompt && value.length === 0 && images.length === 0 && !e.altKey && !e.ctrlKey && !e.metaKey - && secondEnterPrompt ) { - if (!e.repeat) sendQueuedPrompt(secondEnterPrompt); + if (!e.repeat && voiceRecorder.state === "idle" && !voiceShortcutDownRef.current) { + sendQueuedPrompt(queuedPrompt); + } return; } - secondEnterPromptIdRef.current = null; submit(); } }; @@ -2743,9 +2726,6 @@ export function ThreadComposer({ onChange={mentionInput.onChange} onCompositionStart={mentionInput.onCompositionStart} onCompositionEnd={mentionInput.onCompositionEnd} - onBlur={() => { - secondEnterPromptIdRef.current = null; - }} onKeyDown={onKeyDown} onKeyUp={() => setCursorPosition(rawSelection().start)} onSelect={() => setCursorPosition(rawSelection().start)} diff --git a/webui/src/tests/queued-guidance.test.tsx b/webui/src/tests/queued-guidance.test.tsx index 538070880..906ff617d 100644 --- a/webui/src/tests/queued-guidance.test.tsx +++ b/webui/src/tests/queued-guidance.test.tsx @@ -197,6 +197,8 @@ describe("merged waiting messages", () => { view.unmount(); render(); expect(screen.getByRole("button", { name: "Send now" })).toBeDisabled(); + fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); + expect(onSend).toHaveBeenCalledTimes(1); await act(async () => { accept(true); }); expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument(); expect(localStorage.getItem(STORAGE_KEY)).toBeNull(); @@ -283,8 +285,8 @@ describe("merged waiting messages", () => { expect(onSend).not.toHaveBeenCalled(); }); - it("retains the whole message after a rejected immediate send", () => { - const onSend = vi.fn(() => false); + it("retains a rejected message until an explicit Enter retries the whole message", () => { + const onSend = vi.fn().mockReturnValueOnce(false).mockReturnValue(true); const view = render(); enter("1"); enter("2"); fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); @@ -292,6 +294,12 @@ describe("merged waiting messages", () => { expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("1 2"); view.rerender(); expect(onSend).toHaveBeenCalledTimes(1); + fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter", repeat: true }); + expect(onSend).toHaveBeenCalledTimes(1); + fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); + expect(onSend).toHaveBeenCalledTimes(2); + expect(onSend).toHaveBeenLastCalledWith("1\n2"); + expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument(); }); it("does not automatically re-arm stopped content when another input is appended", () => { diff --git a/webui/src/tests/thread-composer.test.tsx b/webui/src/tests/thread-composer.test.tsx index ef666eae4..db1dec50f 100644 --- a/webui/src/tests/thread-composer.test.tsx +++ b/webui/src/tests/thread-composer.test.tsx @@ -3553,10 +3553,11 @@ describe("ThreadComposer", () => { expect(screen.queryByText("send this guidance now")).not.toBeInTheDocument(); }); - it("disarms the second Enter shortcut when keyboard voice recording starts", async () => { + it("does not send waiting guidance during keyboard recording or transcription", async () => { mockVoiceRecorder(); const onSend = vi.fn(); - const onTranscribeAudio = vi.fn(async () => "voice guidance"); + let finishTranscription!: (text: string) => void; + const onTranscribeAudio = vi.fn(() => new Promise((resolve) => { finishTranscription = resolve; })); render( { await waitForVoiceCapture(); fireEvent.keyUp(window, { code: "KeyD", ctrlKey: true, key: "D", shiftKey: true }); await waitFor(() => expect(onTranscribeAudio).toHaveBeenCalled()); + fireEvent.keyDown(input, { key: "Enter" }); + expect(onSend).not.toHaveBeenCalled(); + expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("keep this queued"); + await act(async () => { finishTranscription("voice guidance"); }); + expect(input).toHaveValue("voice guidance"); }); - it("disarms the second Enter shortcut after stopping the active response", () => { + it("keeps stopped guidance paused until an explicit Enter", () => { const onSend = vi.fn(); const onStop = vi.fn(); const { rerender } = render( @@ -3600,7 +3606,6 @@ describe("ThreadComposer", () => { fireEvent.change(input, { target: { value: "keep this queued" } }); fireEvent.keyDown(input, { key: "Enter" }); fireEvent.click(screen.getByRole("button", { name: "Stop response" })); - fireEvent.keyDown(input, { key: "Enter" }); expect(onStop).toHaveBeenCalledTimes(1); expect(onSend).not.toHaveBeenCalled(); @@ -3622,13 +3627,17 @@ describe("ThreadComposer", () => { placeholder="Type your message..." />, ); - fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); - expect(onSend).not.toHaveBeenCalled(); expect(screen.getByText("keep this queued")).toBeInTheDocument(); + fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); + + expect(onSend).toHaveBeenCalledTimes(1); + expect(onSend).toHaveBeenCalledWith( + "keep this queued", undefined, { continueActiveTurn: true }, + ); }); - it("disarms the second Enter shortcut when the composer loses focus", () => { + it.each(["refocusing", "clearing edits"])("sends waiting guidance with Enter after %s", (interaction) => { const onSend = vi.fn(); render( { const input = screen.getByLabelText("Message input"); fireEvent.change(input, { target: { value: "leave this queued" } }); fireEvent.keyDown(input, { key: "Enter" }); - fireEvent.blur(input); - fireEvent.focus(input); + if (interaction === "refocusing") { + fireEvent.blur(input); + fireEvent.focus(input); + } else { + fireEvent.change(input, { target: { value: "x" } }); + fireEvent.change(input, { target: { value: "" } }); + } + expect(onSend).not.toHaveBeenCalled(); fireEvent.keyDown(input, { key: "Enter" }); + expect(onSend).toHaveBeenCalledTimes(1); + expect(onSend).toHaveBeenCalledWith( + "leave this queued", undefined, { continueActiveTurn: true }, + ); + expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument(); + }); + + it.each(["shiftKey", "altKey", "ctrlKey", "metaKey"])("keeps waiting guidance on modified Enter (%s)", (modifier) => { + const onSend = vi.fn(); + render(); + const input = screen.getByLabelText("Message input"); + fireEvent.change(input, { target: { value: "waiting guidance" } }); + fireEvent.keyDown(input, { key: "Enter" }); + fireEvent.keyDown(input, { key: "Enter", [modifier]: true }); expect(onSend).not.toHaveBeenCalled(); - expect(screen.getByText("leave this queued")).toBeInTheDocument(); + expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("waiting guidance"); + + fireEvent.keyDown(input, { key: "Enter" }); + expect(onSend).toHaveBeenCalledTimes(1); + expect(onSend).toHaveBeenCalledWith("waiting guidance", undefined, { continueActiveTurn: true }); + }); + + it("keeps waiting guidance during IME confirmation and sends after composition ends", () => { + const onSend = vi.fn(); + render(); + const input = screen.getByLabelText("Message input"); + fireEvent.change(input, { target: { value: "waiting guidance" } }); + fireEvent.keyDown(input, { key: "Enter" }); + fireEvent.compositionStart(input); + fireEvent.keyDown(input, { key: "Enter", isComposing: true }); + expect(onSend).not.toHaveBeenCalled(); + expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("waiting guidance"); + + fireEvent.compositionEnd(input, { data: "" }); + fireEvent.keyDown(input, { key: "Enter" }); + expect(onSend).toHaveBeenCalledTimes(1); + expect(onSend).toHaveBeenCalledWith( + "waiting guidance", undefined, { continueActiveTurn: true }, + ); }); it("merges consecutive queued inputs and interjects the whole message on a second Enter", () => { @@ -3951,7 +4003,6 @@ describe("ThreadComposer", () => { />, ); expect(await screen.findByText("remember this edited follow-up")).toBeInTheDocument(); - fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); expect(onSend).not.toHaveBeenCalled(); unmount(); @@ -3966,9 +4017,9 @@ describe("ThreadComposer", () => { ); expect(await screen.findByText("remember this edited follow-up")).toBeInTheDocument(); - fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); expect(onSend).not.toHaveBeenCalled(); - fireEvent.click(screen.getByRole("button", { name: "Send now" })); + fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" }); + expect(onSend).toHaveBeenCalledTimes(1); expect(onSend).toHaveBeenCalledWith( "remember this edited follow-up", undefined,