Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix: support copying Assistant response selections (#2035)
Wu Shuwen22924997c0bb
diff --git a/src/components/dictation/AssistantPanel.tsx b/src/components/dictation/AssistantPanel.tsxindex de2d4735d..286b37d8e 100644--- a/src/components/dictation/AssistantPanel.tsx+++ b/src/components/dictation/AssistantPanel.tsx@@ -27,6 +27,11 @@ import {normalizeAgentSelectionContext,type AgentSelectionContext,} from "../../utils/agentSelectionContext";+import {+ getSelectionForCopyShortcut,+ getSelectionInside,+ isEditableTarget,+} from "../../utils/assistantSelection";import { AssistantEmptyState } from "./AssistantEmptyState";import { useToast } from "../ui/useToast";import {@@ -142,6 +147,7 @@ export function AssistantPanel({const {copied,copy: handleCopy,+ copyText,confirmCopied,} = useCopyFeedback(responseContent, {resetMs: MANUAL_COPY_FEEDBACK_MS,@@ -341,15 +347,11 @@ export function AssistantPanel({if (!open || !isResponseReady || !latestAssistantMessage) return undefined;const captureSelection = () => {- const selection = window.getSelection();- const root = responseSelectionRootRef.current;- if (!selection || selection.isCollapsed || selection.rangeCount === 0 || !root) return;-- const range = selection.getRangeAt(0);- if (!root.contains(range.startContainer) || !root.contains(range.endContainer)) return;+ const selectedText = getSelectionInside(responseSelectionRootRef.current);+ if (!selectedText) return;const context = normalizeAgentSelectionContext({- text: selection.toString(),+ text: selectedText,sourceMessageId: latestAssistantMessage.id,});if (!context) return;@@ -388,9 +390,15 @@ export function AssistantPanel({return;}- const target = e.target as HTMLElement | null;- const isEditable =- target?.isContentEditable || target?.tagName === "INPUT" || target?.tagName === "TEXTAREA";+ if (isResponseReady) {+ const selectedText = getSelectionForCopyShortcut(e, responseSelectionRootRef.current);+ if (selectedText) {+ e.preventDefault();+ void copyText(selectedText);+ return;+ }+ }+ const isEditable = isEditableTarget(e.target);if (isResponseReady &&footerPhase === "actions" &&@@ -406,7 +414,17 @@ export function AssistantPanel({};document.addEventListener("keydown", handleKeyDown);return () => document.removeEventListener("keydown", handleKeyDown);- }, [voiceState, isBusy, streaming, open, onClose, isResponseReady, footerPhase, handleCopy]);+ }, [+ voiceState,+ isBusy,+ streaming,+ open,+ onClose,+ isResponseReady,+ footerPhase,+ handleCopy,+ copyText,+ ]);return (<>diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.jsindex d72a0cd0c..c1ecf8ec2 100644--- a/src/helpers/windowManager.js+++ b/src/helpers/windowManager.js@@ -1,4 +1,4 @@-const { app, screen, BrowserWindow, dialog, ipcMain } = require("electron");+const { app, screen, BrowserWindow, dialog, ipcMain, Menu } = require("electron");const debugLogger = require("./debugLogger");// Aliased: this class has an openExternalUrl method wrapping the helper.const { openExternalUrl: openUrlInExternalBrowser } = require("./externalUrlOpener");@@ -132,6 +132,7 @@ class WindowManager {this.setMainWindowInteractivity(false);this.registerMainWindowEvents();+ this.registerAssistantSelectionContextMenu();// Register load event handlers BEFORE loading to catch all eventsthis.mainWindow.webContents.on(@@ -173,6 +174,14 @@ class WindowManager {MenuManager.setupMainMenu(() => this.openSettings());}+ registerAssistantSelectionContextMenu() {+ this.mainWindow?.webContents.on("context-menu", (_event, params) => {+ if (!this._assistantPanelOpen || !params?.selectionText?.trim()) return;++ Menu.buildFromTemplate([{ role: "copy" }]).popup({ window: this.mainWindow });+ });+ }+_updateMainContentProtection() {if (!this.mainWindow || this.mainWindow.isDestroyed()) return;this.mainWindow.setContentProtection(diff --git a/src/hooks/useCopyFeedback.ts b/src/hooks/useCopyFeedback.tsindex afd251843..82299e08c 100644--- a/src/hooks/useCopyFeedback.ts+++ b/src/hooks/useCopyFeedback.ts@@ -6,6 +6,7 @@ export function useCopyFeedback(): {copied: boolean;copy: () => Promise<void>;+ copyText: (textToCopy: string) => Promise<boolean>;confirmCopied: (copiedText: string, durationMs?: number) => void;} {const [copied, setCopied] = useState(false);@@ -36,24 +37,36 @@ export function useCopyFeedback([resetMs]);- const copy = useCallback(async () => {- const textToCopy = text.trim();- if (!textToCopy) return;+ // Writes without touching the copied state: the shared feedback belongs to+ // the full-text copy, not to a partial selection.+ const copyText = useCallback(async (textToCopy: string) => {+ if (!textToCopy.trim()) return false;try {const result = await window.electronAPI?.writeClipboard?.(textToCopy);if (result?.success === false) throw new Error("clipboard-write-failed");+ return true;} catch {try {await navigator.clipboard.writeText(textToCopy);+ return true;} catch {- setCopied(false);- return;+ return false;}}+ }, []);++ const copy = useCallback(async () => {+ const textToCopy = text.trim();+ if (!textToCopy) return;++ if (!(await copyText(textToCopy))) {+ setCopied(false);+ return;+ }confirmCopied(text, resetMs);- }, [confirmCopied, text, resetMs]);+ }, [confirmCopied, copyText, text, resetMs]);- return { copied, copy, confirmCopied };+ return { copied, copy, copyText, confirmCopied };}diff --git a/src/utils/assistantSelection.ts b/src/utils/assistantSelection.tsnew file mode 100644index 000000000..d078d3b74--- /dev/null+++ b/src/utils/assistantSelection.ts@@ -0,0 +1,33 @@+export function isEditableTarget(target: EventTarget | null): boolean {+ const element = target as HTMLElement | null;+ return Boolean(+ element?.isContentEditable || element?.tagName === "INPUT" || element?.tagName === "TEXTAREA"+ );+}++export function getSelectionInside(root: HTMLElement | null): string | null {+ const selection = window.getSelection();+ if (!selection || selection.isCollapsed || selection.rangeCount === 0 || !root) return null;++ const range = selection.getRangeAt(0);+ if (!root.contains(range.startContainer) || !root.contains(range.endContainer)) return null;++ const text = selection.toString();+ return text.trim() ? text : null;+}++export function getSelectionForCopyShortcut(+ event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "key" | "metaKey" | "target">,+ root: HTMLElement | null+): string | null {+ if (+ isEditableTarget(event.target) ||+ event.key.toLowerCase() !== "c" ||+ event.altKey ||+ (!event.ctrlKey && !event.metaKey)+ ) {+ return null;+ }++ return getSelectionInside(root);+}diff --git a/test/helpers/assistantPanel.test.js b/test/helpers/assistantPanel.test.jsindex 25860e5be..01ace001c 100644--- a/test/helpers/assistantPanel.test.js+++ b/test/helpers/assistantPanel.test.js@@ -641,6 +641,111 @@ test("an automatic clipboard delivery keeps the shared Copy button confirmed forassert.ok(scheduledDelays.includes(6000));});+test("Assistant selection copy preserves the selected text without claiming Copied", async (t) => {+ let root = null;+ t.after(async () => {+ if (root) await React.act(async () => root.unmount());+ });+ installBrowserGlobals(t);+ const container = installInteractiveDom(t);+ const writes = [];+ const originalElectronAPI = globalThis.window.electronAPI;+ t.after(() => {+ globalThis.window.electronAPI = originalElectronAPI;+ });+ globalThis.window.electronAPI = {+ writeClipboard: async (text) => {+ writes.push(text);+ return { success: true };+ },+ };++ const vite = await createRendererServer(t, {+ cachePrefix: "openwhispr-selection-copy-feedback-test-",+ });+ const { useCopyFeedback } = await vite.ssrLoadModule("/hooks/useCopyFeedback.ts");+ const { createRoot } = require("react-dom/client");+ let copyFeedback;++ function Harness() {+ copyFeedback = useCopyFeedback("Full Agent answer");+ return React.createElement("button");+ }++ root = createRoot(container);+ await React.act(async () => root.render(React.createElement(Harness)));+ await React.act(async () => copyFeedback.copyText(" selected answer "));+ assert.equal(copyFeedback.copied, false, "a partial copy never shows the Copied state");+ await React.act(async () => copyFeedback.copy());+ assert.equal(copyFeedback.copied, true);++ assert.deepEqual(writes, [" selected answer ", "Full Agent answer"]);+});++test("Assistant selection must stay entirely inside the response root", async (t) => {+ installBrowserGlobals(t);+ const vite = await createRendererServer(t, {+ cachePrefix: "openwhispr-assistant-selection-test-",+ });+ const { getSelectionForCopyShortcut, getSelectionInside } = await vite.ssrLoadModule(+ "/utils/assistantSelection.ts"+ );+ const insideStart = {};+ const insideEnd = {};+ const outside = {};+ const responseRoot = {+ contains: (node) => node === insideStart || node === insideEnd,+ };+ const originalGetSelection = globalThis.window.getSelection;+ t.after(() => {+ globalThis.window.getSelection = originalGetSelection;+ });++ globalThis.window.getSelection = () => ({+ isCollapsed: false,+ rangeCount: 1,+ getRangeAt: () => ({ startContainer: insideStart, endContainer: insideEnd }),+ toString: () => "selected answer",+ });+ assert.equal(getSelectionInside(responseRoot), "selected answer");+ assert.equal(+ getSelectionForCopyShortcut(+ { key: "c", ctrlKey: true, metaKey: false, altKey: false },+ responseRoot+ ),+ "selected answer"+ );+ assert.equal(+ getSelectionForCopyShortcut(+ { key: "c", ctrlKey: false, metaKey: true, altKey: false },+ responseRoot+ ),+ "selected answer"+ );+ assert.equal(+ getSelectionForCopyShortcut(+ { key: "c", ctrlKey: true, metaKey: false, altKey: true },+ responseRoot+ ),+ null+ );+ assert.equal(+ getSelectionForCopyShortcut(+ { key: "c", ctrlKey: true, metaKey: false, altKey: false, target: { tagName: "TEXTAREA" } },+ responseRoot+ ),+ null+ );++ globalThis.window.getSelection = () => ({+ isCollapsed: false,+ rangeCount: 1,+ getRangeAt: () => ({ startContainer: insideStart, endContainer: outside }),+ toString: () => "mixed selection",+ });+ assert.equal(getSelectionInside(responseRoot), null);+});+test("a failed Assistant resize releases its open claim so opening can retry", async (t) => {installBrowserGlobals(t);const vite = await createRendererServer(t, {diff --git a/test/helpers/windowManagerAssistantPanel.test.js b/test/helpers/windowManagerAssistantPanel.test.jsindex dccf21bcd..15fc7844d 100644--- a/test/helpers/windowManagerAssistantPanel.test.js+++ b/test/helpers/windowManagerAssistantPanel.test.js@@ -4,6 +4,7 @@ const Module = require("node:module");const requestedMainWindowPositions = [];const createdBrowserWindows = [];const screenListeners = [];+const builtMenus = [];// Same stub set as windowManagerMeetingNotification.test.js: WindowManager// pulls in electron + sibling managers at require time.@@ -49,6 +50,16 @@ Module._load = function loadWindowManagerWithStubs(request, parent, isMain) {hide() { this.visible = false; }moveTop() {}},+ Menu: {+ buildFromTemplate: (template) => {+ const menu = {+ popupCalls: [],+ popup(options) { this.popupCalls.push(options); },+ };+ builtMenus.push({ template, menu });+ return menu;+ },+ },shell: {},dialog: {},};@@ -130,6 +141,30 @@ function makeManager(windowState) {return { manager, calls: fake.calls };}+test("the Assistant response context menu exposes native Copy only for selected text", () => {+ builtMenus.length = 0;+ const manager = new WindowManager();+ const listeners = new Map();+ manager.mainWindow = {+ webContents: { on: (event, listener) => listeners.set(event, listener) },+ };+ manager._assistantPanelOpen = true;+ manager.registerAssistantSelectionContextMenu();++ const onContextMenu = listeners.get("context-menu");+ assert.ok(onContextMenu);+ onContextMenu(null, { selectionText: "selected answer" });+ onContextMenu(null, { selectionText: " " });++ assert.equal(builtMenus.length, 1);+ assert.deepEqual(builtMenus[0].template, [{ role: "copy" }]);+ assert.deepEqual(builtMenus[0].menu.popupCalls, [{ window: manager.mainWindow }]);++ manager._assistantPanelOpen = false;+ onContextMenu(null, { selectionText: "outside Assistant" });+ assert.equal(builtMenus.length, 1);+});+test("the Agent companion follows the edge opposite the panel", () => {requestedMainWindowPositions.length = 0;const manager = new WindowManager();