Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix: restrict dictation to active text target
Ruben Lütkee175f9607c03
diff --git a/jarvis/ui/web/frontend/src/components/ChatInput.tsx b/jarvis/ui/web/frontend/src/components/ChatInput.tsxindex 0a3268684a..3037271ab3 100644--- a/jarvis/ui/web/frontend/src/components/ChatInput.tsx+++ b/jarvis/ui/web/frontend/src/components/ChatInput.tsx@@ -209,6 +209,7 @@ export function ChatInput() {/><Buttontype="button"+ data-jarvis-dictation-triggeronClick={toggleDictation}disabled={!connected}size="icon"diff --git a/jarvis/ui/web/frontend/src/hooks/useWebSocket.ts b/jarvis/ui/web/frontend/src/hooks/useWebSocket.tsindex 79f6b4ea14..bb2918b27a 100644--- a/jarvis/ui/web/frontend/src/hooks/useWebSocket.ts+++ b/jarvis/ui/web/frontend/src/hooks/useWebSocket.ts@@ -2,7 +2,11 @@ import { useEffect, useRef } from "react";import { useQueryClient } from "@tanstack/react-query";import { WSClient } from "@/lib/ws";-import { deliverDictationText, isForThisWindow } from "@/lib/dictationTarget";+import {+ deliverDictationText,+ documentOwnsDictation,+ isForThisWindow,+} from "@/lib/dictationTarget";import {SECTION_LABELS,isSectionId,@@ -251,6 +255,13 @@ export function useWebSocket(): void {target?: string;};const text = typeof p.text === "string" ? p.text : "";+ // The server fans this event out to every connected UI. Background+ // tabs/windows are observers; letting each one update its own target+ // is how one utterance was pasted into several shared terminals.+ if (!documentOwnsDictation()) {+ if (p.is_final) useEventStore.getState().setDictationInterim("");+ return;+ }if (!p.is_final) {useEventStore.getState().setDictationInterim(text);} else {@@ -260,8 +271,9 @@ export function useWebSocket(): void {? deliverDictationText(text): "none";if (delivered !== "none") {- // Straight into the field or terminal, so no sequence bump for- // the composer — just drop the live tail.+ // Inserted here, or deliberately left to the one foreground UI+ // client that owns this broadcast. Either way, no sequence bump+ // for this client's composer — just drop the live tail.store.setDictationInterim("");} else {// The composer is the historical sink and still the rightdiff --git a/jarvis/ui/web/frontend/src/lib/dictationTarget.test.ts b/jarvis/ui/web/frontend/src/lib/dictationTarget.test.tsindex afcc5f2f72..d6db313599 100644--- a/jarvis/ui/web/frontend/src/lib/dictationTarget.test.ts+++ b/jarvis/ui/web/frontend/src/lib/dictationTarget.test.ts@@ -1,4 +1,4 @@-import { afterEach, beforeEach, describe, expect, it } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { attachTerminalBridge } from "./editActions";import {@@ -22,6 +22,8 @@ import {let removeTracker: (() => void) | null = null;beforeEach(() => {+ vi.spyOn(document, "hasFocus").mockReturnValue(true);+ vi.spyOn(document, "visibilityState", "get").mockReturnValue("visible");resetDictationTarget();removeTracker = installDictationFocusTracker();});@@ -30,6 +32,7 @@ afterEach(() => {removeTracker?.();removeTracker = null;document.body.replaceChildren();+ vi.restoreAllMocks();});function mountField(value = ""): HTMLTextAreaElement {@@ -79,6 +82,7 @@ describe("deliverDictationText", () => {const field = mountField("");field.focus();const button = document.createElement("button");+ button.dataset.jarvisDictationTrigger = "true";document.body.appendChild(button);button.focus();@@ -86,6 +90,17 @@ describe("deliverDictationText", () => {expect(field.value).toBe("still mine");});+ it("does not fall back to a stale field after an unrelated control took focus", () => {+ const field = mountField("");+ field.focus();+ const button = document.createElement("button");+ document.body.appendChild(button);+ button.focus();++ expect(deliverDictationText("not the old field")).toBe("none");+ expect(field.value).toBe("");+ });+it("does not deliver into a field whose view was unmounted", () => {const field = mountField("");field.focus();@@ -125,6 +140,24 @@ describe("deliverDictationText", () => {expect(first.value).toBe("");expect(second.value).toBe("second");});++ it("does not paste a broadcast from a background client", () => {+ const field = mountField("");+ field.focus();+ vi.mocked(document.hasFocus).mockReturnValue(false);++ expect(deliverDictationText("one client only")).toBe("inactive");+ expect(field.value).toBe("");+ });++ it("does not paste a broadcast from a hidden client", () => {+ const field = mountField("");+ field.focus();+ vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");++ expect(deliverDictationText("one tab only")).toBe("inactive");+ expect(field.value).toBe("");+ });});describe("isForThisWindow", () => {diff --git a/jarvis/ui/web/frontend/src/lib/dictationTarget.ts b/jarvis/ui/web/frontend/src/lib/dictationTarget.tsindex 6dc9baff6e..3f3368c1e6 100644--- a/jarvis/ui/web/frontend/src/lib/dictationTarget.ts+++ b/jarvis/ui/web/frontend/src/lib/dictationTarget.ts@@ -24,11 +24,17 @@** ## How the target is chosen*- * The element that has focus when the transcript arrives, and — as a fallback —- * the last editable one that had it. The fallback is load-bearing rather than- * defensive: a dictation started from a button (the composer's microphone, a- * toolbar) moves focus onto that button, and "insert where the caret was" is- * what every dictation tool does and what people expect.+ * First, the document itself must still be visible and focused. WebSocket+ * events are broadcast to every connected UI client; without that ownership+ * check, an old tab or a second desktop window can paste the same transcript+ * into its own remembered field. Within the one owning document, the target is+ * the element that has focus when the transcript arrives, and — as a fallback —+ * the last editable one that had it. That fallback is allowed only while an+ * explicitly marked dictation control has focus. An unrelated toolbar or pane+ * click must invalidate the old field as a destination; otherwise "current+ * field" quietly becomes "whatever field was used earlier." A real dictation+ * button still moves focus onto itself, and "insert where the caret was" remains+ * available for that deliberate case.** A remembered element that has since left the DOM is skipped, so switching* sections cannot deliver into a field that is no longer on screen.@@ -46,10 +52,12 @@ import { captureEditSnapshot, pasteInto } from "./editActions";/*** What became of one transcript.*- * `none` is the honest answer, not a swallowed failure: nothing on screen could- * take the text. The caller says so — see `useWebSocket`.+ * `inactive` means another visible, focused UI client owns this broadcast, so+ * this client must consume it without inserting or falling back to its chat+ * composer. `none` is the honest answer when the owning document has nowhere+ * to put the text. The caller says so — see `useWebSocket`.*/-export type DictationDelivery = "field" | "terminal" | "none";+export type DictationDelivery = "field" | "terminal" | "inactive" | "none";/** The last editable element that had focus, for the button case above. */let lastEditable: HTMLElement | null = null;@@ -72,6 +80,14 @@ function editableTarget(element: Element | null): HTMLElement | null {return null;}+/** Whether focus moved away from a field specifically to start dictation. */+function permitsRememberedTarget(element: Element | null): boolean {+ return (+ element instanceof HTMLElement &&+ element.closest("[data-jarvis-dictation-trigger]") !== null+ );+}+/*** Watch focus so a dictation started from a button still lands in the field.*@@ -83,8 +99,9 @@ export function installDictationFocusTracker(): () => void {const onFocusIn = (event: Event): void => {const found = editableTarget(event.target as Element | null);- // Only ever REPLACED by another editable target, never cleared by one:- // clicking a button must not lose the field the user was just typing in.+ // Keep the field available for an explicitly marked dictation trigger.+ // Ordinary controls cannot use it because `deliverDictationText` checks+ // what currently owns focus before considering this remembered element.if (found) lastEditable = found;};target.addEventListener("focusin", onFocusIn, true);@@ -96,6 +113,11 @@ export function resetDictationTarget(): void {lastEditable = null;}+/** Whether this document is the single UI client allowed to consume dictation. */+export function documentOwnsDictation(target: Document = document): boolean {+ return target.visibilityState === "visible" && target.hasFocus();+}+/** The final-transcript half of a `DictationTranscript` event. */export interface FinalTranscript {text?: string;@@ -141,7 +163,16 @@ export function isForThisWindow(export function deliverDictationText(text: string): DictationDelivery {if (!text.trim()) return "none";if (typeof document === "undefined") return "none";- for (const candidate of [document.activeElement, lastEditable]) {+ // A single server event reaches every open Jarvis tab/window. Only the+ // foreground document may turn that broadcast into an edit; all others are+ // observers. Checking again at the last possible moment closes the race where+ // focus moves after the WebSocket handler starts but before insertion.+ if (!documentOwnsDictation()) return "inactive";+ const active = document.activeElement;+ const candidates = permitsRememberedTarget(active)+ ? [active, lastEditable]+ : [active];+ for (const candidate of candidates) {const element = editableTarget(candidate);if (!element) continue;const snapshot = captureEditSnapshot(element);