A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
feat(voice): leave a dictation in the input for review by default
Alan Buscagliae0010ebdb1a2
diff --git a/packages/ui/src/components/Composer.tsx b/packages/ui/src/components/Composer.tsxindex 2b52ec1..49b1e5b 100644--- a/packages/ui/src/components/Composer.tsx+++ b/packages/ui/src/components/Composer.tsx@@ -33,6 +33,21 @@ export function Composer({const recording = voice?.phase === "recording";const voiceStatus = voice?.phase === "transcribing" ? "Transcribing…" : voice?.note;+ // A dictation to review joins what was already typed, and the cursor waits at its end.+ const draft = voice?.draft;+ const draftTaken = voice?.draftTaken;+ useEffect(() => {+ if (!draft || !draftTaken) return;+ setText((current) => (current.trim() ? `${current.trimEnd()} ${draft.text}` : draft.text));+ draftTaken(draft.id);+ requestAnimationFrame(() => {+ const input = box.current;+ if (!input) return;+ input.focus();+ input.setSelectionRange(input.value.length, input.value.length);+ });+ }, [draft, draftTaken]);+useEffect(() => {if (focusKey !== undefined) box.current?.focus();}, [focusKey]);@@ -49,7 +64,7 @@ export function Composer({if (files.length > 0 && !refs) return;send({ type: "send", text: value, requestId: newRequestId(), ...(refs ? { attachments: refs } : {}) });attachments?.clear();- voice?.typed();+ voice?.sent();setText("");}@@ -125,6 +140,15 @@ export function Composer({value={voice.level}/><span className="voice-partial">{voice.partial || "Listening…"}</span>+ <button+ type="button"+ role="switch"+ aria-checked={voice.sendOnStop}+ className={`voice-send-option${voice.sendOnStop ? " on" : ""}`}+ onClick={() => voice.setSendOnStop(!voice.sendOnStop)}+ >+ Send when I stop+ </button><buttontype="button"className="voice-cancel"@@ -167,8 +191,14 @@ export function Composer({<buttontype="button"className={`composer-mic${recording ? " composer-mic-on" : ""}`}- aria-label={recording ? "Stop and send" : "Talk"}- title={voice.mic.available ? "Tap to talk, tap again to send" : voice.mic.reason}+ aria-label={recording ? (voice.sendOnStop ? "Stop and send" : "Stop and review") : "Talk"}+ title={+ voice.mic.available+ ? voice.sendOnStop+ ? "Tap to talk, tap again to send"+ : "Tap to talk, tap again to review before sending"+ : voice.mic.reason+ }disabled={disabled || !voice.mic.available || voice.phase === "transcribing"}onClick={recording ? voice.stop : voice.start}>diff --git a/packages/ui/src/styles.css b/packages/ui/src/styles.cssindex 72d36ab..68d5092 100644--- a/packages/ui/src/styles.css+++ b/packages/ui/src/styles.css@@ -1813,3 +1813,20 @@ html[data-surface="glow"] body {.voice-model-error {color: var(--red, #ff6b8a);}++/* Send a dictation on stop, or leave it in the input for review (the default). */+.voice-send-option {+ flex: none;+ border: 1px solid var(--line, rgba(255, 255, 255, 0.14));+ border-radius: 999px;+ background: transparent;+ color: var(--muted, inherit);+ font-size: 11px;+ padding: 2px 8px;+ cursor: pointer;+}++.voice-send-option.on {+ border-color: var(--accent);+ color: var(--accent);+}diff --git a/packages/ui/src/useVoice.ts b/packages/ui/src/useVoice.tsindex fd369d1..c4728c1 100644--- a/packages/ui/src/useVoice.ts+++ b/packages/ui/src/useVoice.ts@@ -41,11 +41,22 @@ export interface VoiceControls {setMuted(muted: boolean): void;/** The user sent a typed message: its reply is not read aloud. */typed(): void;+ /** Whether a dictation is sent when recording stops, or left in the input for review. */+ sendOnStop: boolean;+ setSendOnStop(send: boolean): void;+ /** A dictation waiting in the input for review; `id` changes with each one. */+ draft?: { id: number; text: string };+ /** The input took the draft. */+ draftTaken(id: number): void;+ /** The user sent the input: its reply is read aloud when it included a dictation. */+ sent(): void;}/** Recordings stop on their own so they fit the daemon's WebSocket limit (S30.2). */const MAX_RECORDING_MS = 60_000;const MUTED_KEY = "gentle-dot-voice-muted";+/** Set when a dictation is sent on stop; otherwise it stays in the input for review. */+const SEND_KEY = "gentle-dot-voice-send";const RECORDING_TYPES = ["audio/webm;codecs=opus", "audio/webm", "audio/mp4", "audio/ogg;codecs=opus"];const REASONS = {insecure: "Voice needs a secure page (HTTPS or localhost).",@@ -100,7 +111,7 @@ export function useVoice(state: DotState, send: Send): VoiceControls {const [partial, setPartial] = useState("");const [note, setNote] = useState<string | undefined>(undefined);const [speaking, setSpeaking] = useState(false);- const [muted, setMutedState] = useState(() => readMuted());+ const [muted, setMutedState] = useState(() => readFlag(MUTED_KEY));const phaseNow = useRef<VoicePhase>("idle");const recording = useRef<Recording | undefined>(undefined);@@ -172,6 +183,13 @@ export function useVoice(state: DotState, send: Send): VoiceControls {[send, speakWithSystem],);+ const [sendOnStop, setSendOnStopState] = useState(readFlag(SEND_KEY));+ const sendOnStopNow = useRef(sendOnStop);+ sendOnStopNow.current = sendOnStop;+ const [draft, setDraft] = useState<{ id: number; text: string }>();+ /** The input holds dictated text that has not been sent yet. */+ const dictated = useRef(false);+const sendTranscript = useCallback((transcript: string) => {const text = transcript.trim();@@ -179,13 +197,37 @@ export function useVoice(state: DotState, send: Send): VoiceControls {finish(REASONS.empty);return;}- voiceTurn.current = new Set(latest.current.messages.map((m) => m.id));- send({ type: "send", text, requestId: newRequestId() });+ if (sendOnStopNow.current) {+ voiceTurn.current = new Set(latest.current.messages.map((m) => m.id));+ send({ type: "send", text, requestId: newRequestId() });+ } else {+ dictated.current = true;+ setDraft((previous) => ({ id: (previous?.id ?? 0) + 1, text }));+ }finish();},[send, finish],);+ const setSendOnStop = useCallback((next: boolean) => {+ setSendOnStopState(next);+ try {+ if (next) localStorage.setItem(SEND_KEY, "1");+ else localStorage.removeItem(SEND_KEY);+ } catch {+ // Private browsing: the choice lasts for this window only.+ }+ }, []);++ const draftTaken = useCallback((id: number) => {+ setDraft((current) => (current?.id === id ? undefined : current));+ }, []);++ const sent = useCallback(() => {+ voiceTurn.current = dictated.current ? new Set(latest.current.messages.map((m) => m.id)) : undefined;+ dictated.current = false;+ }, []);+const begin = useCallback((entry: Recording) => {const started = Date.now();@@ -406,12 +448,17 @@ export function useVoice(state: DotState, send: Send): VoiceControls {stopSpeaking,setMuted,typed,+ sendOnStop,+ setSendOnStop,+ ...(draft ? { draft } : {}),+ draftTaken,+ sent,};}-function readMuted(): boolean {+function readFlag(key: string): boolean {try {- return localStorage.getItem(MUTED_KEY) === "1";+ return localStorage.getItem(key) === "1";} catch {return false;}diff --git a/packages/ui/test/voice.test.tsx b/packages/ui/test/voice.test.tsxindex 4296140..fc75718 100644--- a/packages/ui/test/voice.test.tsx+++ b/packages/ui/test/voice.test.tsx@@ -135,6 +135,8 @@ beforeEach(() => {getUserMedia.mockClear();track.stop.mockClear();localStorage.clear();+ // These tests cover sending on stop; "review before sending" (the default) has its own below.+ localStorage.setItem("gentle-dot-voice-send", "1");setSecure(true);vi.stubGlobal("MediaRecorder", FakeRecorder);vi.stubGlobal("SpeechSynthesisUtterance", FakeUtterance);@@ -430,3 +432,68 @@ describe("the composer with the mic present", () => {expect(sentOf("abort")).toEqual([{ type: "abort" }]);});});++describe("reviewing a dictation before sending (the default)", () => {+ function desktop(stopText: string) {+ h.tauri = true;+ h.invoke = vi.fn(async (command: string) => {+ if (command === "voice_status") return { available: true };+ if (command === "voice_stop") return { text: stopText };+ return undefined;+ });+ }+ const box = () => screen.getByRole("textbox") as HTMLTextAreaElement;++ beforeEach(() => localStorage.removeItem("gentle-dot-voice-send"));++ it("leaves the transcript in the input, added to what was typed, and sends it on Enter", async () => {+ desktop("hola mundo");+ render(<Harness initial={withVoice(false)} />);+ await waitFor(() => expect(talk()).toBeEnabled());+ await userEvent.type(box(), "dijo:");+ await userEvent.click(talk());+ await userEvent.click(await screen.findByRole("button", { name: "Stop and review" }));+ await waitFor(() => expect(box().value).toBe("dijo: hola mundo"));+ expect(sentOf("send")).toEqual([]);+ await waitFor(() => expect(box()).toHaveFocus());++ await userEvent.type(box(), "{Enter}");+ expect(sentOf("send")).toEqual([+ { type: "send", text: "dijo: hola mundo", requestId: expect.any(String) },+ ]);+ });++ it("still reads the reply aloud when the message sent included a dictation", async () => {+ desktop("hi");+ render(<Harness initial={withVoice(false, false)} />);+ await waitFor(() => expect(talk()).toBeEnabled());+ await userEvent.click(talk());+ await userEvent.click(await screen.findByRole("button", { name: "Stop and review" }));+ await waitFor(() => expect(box().value).toBe("hi"));+ await userEvent.type(box(), "{Enter}");+ server({ type: "user_message", messageId: "u1", text: "hi" });+ server(+ { type: "agent_state", state: "thinking" },+ { type: "message_done", messageId: "a1", text: "Hello there" },+ { type: "agent_state", state: "idle" },+ );+ await waitFor(() => expect(synth.speak).toHaveBeenCalledTimes(1));+ });++ it("offers sending on stop as a remembered option while recording", async () => {+ desktop("hola");+ render(<Harness initial={withVoice(false)} />);+ await waitFor(() => expect(talk()).toBeEnabled());+ await userEvent.click(talk());+ const option = await screen.findByRole("switch", { name: "Send when I stop" });+ expect(option).toHaveAttribute("aria-checked", "false");+ await userEvent.click(option);+ expect(option).toHaveAttribute("aria-checked", "true");+ expect(localStorage.getItem("gentle-dot-voice-send")).toBe("1");+ await userEvent.click(screen.getByRole("button", { name: "Stop and send" }));+ await waitFor(() =>+ expect(sentOf("send")).toEqual([{ type: "send", text: "hola", requestId: expect.any(String) }]),+ );+ expect(box().value).toBe("");+ });+});