8000 fix(deck): one voice session at a time, so dictation is not repeated by simon-inkie · Pull Request #72 · simon-inkie/inkie-deck · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .specs/voice-single-session/SPEC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Voice dictation repeats text when the mic is clicked while connecting

- Issue: none (reported directly by the operator)
- Branch: fix/voice-single-session
- Started: 2026-10-19

## TL;DR

`toggleMic` in `packages/deck/app/components/Compose.tsx` only refused a new
session when `status === "active"`, which is set after `getUserMedia` and the
socket `open`. The mic is slow to respond, so clicks during that window each
started another session (own stream, socket, recorder). Each overwrote the
refs, so `stopVoice` lost the older ones, and every live socket called
`onFinalTextRef.current`: each Deepgram final was inserted once per session.

Fix: one session at a time. A `liveRef` lock set on the first click makes a
click while connecting cancel the pending start; a `sessionIdRef` bumped on
every release lets the in-flight start notice it was cancelled after
`getUserMedia` resolves and drop its stream. Every socket listener checks
`wsRef.current === ws`, so a superseded socket can neither insert text nor
stop the live session. A new `connecting` voice state shows a spinner with
`aria-busy` and the label "Connecting voice, click to cancel".

## Scope

In scope: `useVoice` and the mic button in `Compose.tsx`; one jsdom test.

Out of scope: the voice proxy, the Deepgram relay, other components.

## Test plan

- `test/compose-voice-single-session.test.tsx` (stubs getUserMedia,
WebSocket, MediaRecorder): two clicks before the socket opens create one
socket and release it; a click during the permission prompt opens no
socket and stops the stream; a stale socket's final, open and close do not
reach the textarea or stop the live session. All three fail on the
pre-fix code.
- Not yet verified in a real browser.

## Follow-ups

- None.
69 changes: 58 additions & 11 deletions packages/deck/app/components/Compose.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
*/
import { useRef, useState, useEffect, useCallback } from "react";
import { useFetcher } from "react-router";
import { FileImage, FileText, Mic, MicOff, Paperclip, Settings2, X } from "lucide-react";
import { FileImage, FileText, Loader2, Mic, MicOff, Paperclip, Settings2, X } from "lucide-react";
import { Button } from "./ui/button";
import { Switch } from "./ui/switch";
import type { BatchPrefs, HoldIndicator } from "~/lib/batch-prefs";
Expand Down Expand Up @@ -189,6 +189,7 @@ interface ComposeProps {

type VoiceState =
| { status: "idle" }
| { status: "connecting" }
| { status: "active"; interim: string }
| { status: "unavailable" };

Expand Down Expand Up @@ -237,6 +238,14 @@ function useVoice(
const wsRef = useRef<WebSocket | null>(null);
const streamRef = useRef<MediaStream | null>(null);

// One session at a time. liveRef is true from the first click until the
// session is released, so a click while connecting (status still idle in
// the render that handled it) cancels instead of starting a second session.
// sessionIdRef is bumped on every release; an in-flight start compares its
// own id after each await and gives up if it has been superseded.
const liveRef = useRef(false);
const sessionIdRef = useRef(0);

// Fix layer 1: the WS message listener is registered ONCE per
// session, so a direct closure over onFinalText goes stale the moment the
// parent re-renders (its captured draft froze at session start - that was
Expand All @@ -262,7 +271,11 @@ function useVoice(
[onDebugEvent]
);

const stopVoice = useCallback(() => {
// Tear down whatever session the refs hold, without touching React state.
const releaseSession = useCallback(() => {
liveRef.current = false;
sessionIdRef.current += 1;

if (silenceTimerRef.current !== null) {
clearTimeout(silenceTimerRef.current);
silenceTimerRef.current = null;
Expand Down Expand Up @@ -293,9 +306,12 @@ function useVoice(

frameCountRef.current = 0;
frameBytesRef.current = 0;
}, []);

const stopVoice = useCallback(() => {
releaseSession();
setVoiceState({ status: "idle" });
}, []);
}, [releaseSession]);

// Stop on unmount - no hot mic after component dies.
useEffect(() => {
Expand All @@ -318,7 +334,9 @@ function useVoice(
}, [emit, stopVoice]);

const toggleMic = useCallback(async () => {
if (voiceState.status === "active") {
// Connecting or active: a click stops (or cancels) the current session.
if (liveRef.current) {
emit("state", "stopped by click");
stopVoice();
return;
}
Expand All F438 @@ -334,19 +352,35 @@ function useVoice(
return;
}

// Release any leftover session before claiming the refs for a new one.
releaseSession();
liveRef.current = true;
const sessionId = sessionIdRef.current;
const isCurrent = () => sessionIdRef.current === sessionId;
setVoiceState({ status: "connecting" });

emit("state", "getUserMedia requested");

let stream: MediaStream;
try {
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
emit("state", "getUserMedia granted");
} catch (err) {
if (!isCurrent()) return;
const name = err instanceof Error ? err.name : "unknown";
emit("error", `getUserMedia denied: ${name}`);
releaseSession();
setVoiceState({ status: "unavailable" });
return;
}

// Cancelled while the permission prompt was open: drop this stream.
if (!isCurrent()) {
stream.getTracks().forEach((t) => t.stop());
emit("state", "start cancelled");
return;
}

streamRef.current = stream;

// Open WS first; start recording once WS is open.
Expand All @@ -355,7 +389,11 @@ function useVoice(
wsRef.current = ws;
emit("ws", "connecting");

// Every listener below belongs to THIS socket's session. Events from a
// socket that is no longer wsRef.current are dropped, so a superseded
// session can neither insert text nor stop the live one.
ws.addEventListener("open", () => {
if (wsRef.current !== ws) return;
emit("ws", "open");

// If debug mode is on, opt in to dg_raw mirroring.
Expand Down Expand Up @@ -404,6 +442,7 @@ function useVoice(
});

ws.addEventListener("message", (e) => {
if (wsRef.current !== ws) return;
if (typeof e.data !== "string") return;

// Emit the raw message (truncated to 300 chars) before parsing.
Expand Down Expand Up @@ -480,25 +519,28 @@ function useVoice(

ws.addEventListener("close", (e) => {
emit("ws", `close code=${e.code} reason=${e.reason || "(none)"}`);
// If we didn't initiate the close ourselves, flip to unavailable.
if (wsRef.current !== null) {
// If we didn't initiate the close ourselves (stopVoice nulls wsRef
// first), and this is still the current socket, flip to unavailable.
if (wsRef.current === ws) {
stopVoice();
setVoiceState({ status: "unavailable" });
}
});

ws.addEventListener("error", () => {
if (wsRef.current !== ws) return;
emit("error", "ws error");
stopVoice();
setVoiceState({ status: "unavailable" });
});
}, [voiceState.status, proxyUrl, stopVoice, emit, debug, resetSilenceTimer]);
}, [proxyUrl, stopVoice, releaseSession, emit, debug, resetSilenceTimer]);

const micAvailable = voiceState.status !== "unavailable";
const isActive = voiceState.status === "active";
const isConnecting = voiceState.status === "connecting";
const interimText = voiceState.status === "active" ? voiceState.interim : "";

return { voiceState, micAvailable, isActive, interimText, toggleMic, stopVoice };
return { voiceState, micAvailable, isActive, isConnecting, interimText, toggleMic, stopVoice };
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -731,7 +773,7 @@ export function Compose({
currentTextRef.current = latestComposeText;
}, [latestComposeText]);

const { micAvailable, isActive, interimText, toggleMic, stopVoice } =
const { micAvailable, isActive, isConnecting, interimText, toggleMic, stopVoice } =
useVoice(
voiceProxyUrl,
handleFinalText,
Expand All @@ -746,7 +788,9 @@ export function Compose({
? "Voice unavailable"
: isActive
? "Stop voice recording"
: "Start voice recording";
: isConnecting
? "Connecting voice, click to cancel"
: "Start voice recording";

// Capture the anchor at the voice-session activation edge; clear on end so
// the next session re-anchors at the then-current cursor. Deliberately NOT
Expand Down Expand Up @@ -1204,13 +1248,16 @@ export function Compose({
title={micLabel}
className="relative h-9 w-9 p-0 min-h-0 min-w-0 text-muted-foreground"
>
aria-busy={isConnecting || undefined}
data-testid="mic-button"
>
{/* Pulse indicator while active */}
{isActive ? (
<span className="absolute top-0.5 right-0.5 size-1.5 rounded-full bg-primary animate-pulse" />
) : null}
{voiceEnabled ? (
{voiceEnabled && isConnecting ? (
<Loader2 className="h-5 w-5 animate-spin" />
) : voiceEnabled ? (
<Mic className={cn("h-5 w-5", isActive ? "text-primary" : "")} />
) : (
<MicOff className="h-5 w-5" />
Expand Down
Loading
Loading
0