The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 19:45 UTC
fix(voice): dictation survives draft changes and Escape in the citation box (#32682)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 12, 2026 at 11:35 UTC
1001bc71fcc80ba8dc32c60a0ae4bbe85efea44b
19 files changed
+350
-159
apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx
-4
@@ -87,9 +87,7 @@ import { useScheduledTaskDestinationLabel, useScheduledTaskEditor } from "./sche
87
import {
88
discardScheduledTaskEditor,
89
markScheduledTaskEditorSaved,
90
- scheduledTaskEditorSessionAtom,
90
} from "./scheduled-task-editor-state";
92
-import { appAtomRegistry } from "../../state/atom-registry";
91
import {
92
formatNextScheduledTaskRun,
93
formatScheduledTaskInterval,
@@ -519,8 +517,6 @@ function SettingsScheduledTaskEditorScreen({ title }: { readonly title: string }
517
const voiceInput = useVoiceInputController({
518
ownerKey,
519
label: editor?.draft.title.trim() || title,
522
- subscribeToDraftChanges: (onChange) =>
523
- appAtomRegistry.subscribe(scheduledTaskEditorSessionAtom, onChange),
520
selection,
521
readDraftMessage: () => {
522
const current = readEditor();
apps/mobile/src/features/threads/ThreadComposer.tsx
-3
@@ -57,13 +57,11 @@ import { armAgentAwarenessLiveActivityForLocalWork } from "../agent-awareness/re
57
import { scopedThreadKey } from "../../lib/scopedEntities";
58
import {
59
getComposerDraftSnapshot,
60
- composerDraftsAtom,
60
setComposerDraftText,
61
composerContextImportsAtom,
62
countComposerDraftAttachmentsAfterSelection,
63
useComposerDraft,
64
} from "../../state/use-composer-drafts";
66
-import { appAtomRegistry } from "../../state/atom-registry";
65
import type { ComposerDocumentAttachment } from "../../lib/composerContext";
66
import { useProject, useVisibleThreadShells } from "../../state/entities";
67
import { useMentionableEnvironments } from "../../state/environments";
@@ -579,7 +577,6 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
577
ownerKey: composerDraftKey,
578
label: props.selectedThread.title || "Untitled thread",
579
readDraftMessage: () => getComposerDraftSnapshot(composerDraftKey).text,
582
- subscribeToDraftChanges: (onChange) => appAtomRegistry.subscribe(composerDraftsAtom, onChange),
580
selection: composerMenu.selection,
581
onChangeDraftMessage: (text) => setComposerDraftText(composerDraftKey, text),
582
onChangeSelection: composerMenu.onSelectionChange,
apps/mobile/src/features/voice-input/lazyVoiceRecorder.test.ts
-1
@@ -143,7 +143,6 @@ describe("createLazyVoiceRecorder", () => {
143
ownerKey: "thread",
144
text: "",
145
selection: { start: 0, end: 0 },
146
- revision: 0,
146
}),
147
commitDraft: () => undefined,
148
onStateChange: () => undefined,
apps/mobile/src/features/voice-input/useVoiceInputController.ts
-2
@@ -15,7 +15,6 @@ export function useVoiceInputController(input: {
15
readonly ownerKey: string | null;
16
readonly label: string;
17
readonly readDraftMessage: () => string | null;
18
- readonly subscribeToDraftChanges: (onChange: () => void) => () => void;
18
readonly selection: ComposerEditorSelection;
19
readonly disabled?: boolean;
20
readonly onChangeDraftMessage: (value: string) => void;
@@ -58,7 +57,6 @@ export function useVoiceInputController(input: {
57
}
58
},
59
captured.selection,
61
- captured.subscribeToDraftChanges,
60
),
61
label: captured.label,
62
});
apps/mobile/src/features/voice-input/voiceInputSession.test.ts
+40
-54
@@ -1,6 +1,5 @@
1
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
import type { PreparedVoiceTranscription } from "@t3tools/client-runtime/voice-input";
3
-import { Atom, AtomRegistry } from "effect/reactivity";
3
import { resetVoiceInputGlobalsForTests } from "../../../../../packages/client-runtime/src/voice-input/controller";
4
import {
5
acquireAudioSession,
@@ -23,19 +22,9 @@ import {
22
createVoiceInputTarget,
23
VoiceInputSession,
24
type VoiceInputSessionDependencies,
26
- type VoiceInputTarget,
25
type VoiceRecordingSample,
26
} from "./voiceInputSession";
27
30
-function createTarget(
31
- ownerKey: string,
32
- readText: () => string | null,
33
- commit: VoiceInputTarget["commitDraft"],
34
- selection: { start: number; end: number },
35
-) {
36
- return createVoiceInputTarget(ownerKey, readText, commit, selection, () => () => {});
37
-}
38
-
28
function createSession(
29
overrides?: Partial<
30
Pick<
@@ -116,7 +105,7 @@ describe("global voice input", () => {
105
if (outcome === "activation failure") {
106
audio.setActive.mockRejectedValueOnce(new Error("Audio activation failed"));
107
}
119
- await session.start(createTarget("first", () => "", vi.fn(), { start: 0, end: 0 }));
108
+ await session.start(createVoiceInputTarget("first", () => "", vi.fn(), { start: 0, end: 0 }));
109
expect(pause).toHaveBeenCalledOnce();
110
expect(isAudioSessionOwner(playback)).toBe(false);
111
if (outcome === "complete") {
@@ -154,9 +143,9 @@ describe("global voice input", () => {
143
async ({ selection, expected, cursor }) => {
144
const { session } = createSession();
145
const commit = vi.fn();
157
- await session.start(createTarget("first", () => "hello world", commit, selection));
146
+ await session.start(createVoiceInputTarget("first", () => "hello world", commit, selection));
147
await session.start(
159
- createTarget("second", () => "other prompt", vi.fn(), { start: 12, end: 12 }),
148
+ createVoiceInputTarget("second", () => "other prompt", vi.fn(), { start: 12, end: 12 }),
149
);
150
await session.controller.stop();
151
expect(commit).toHaveBeenCalledWith(expected, {
@@ -175,7 +164,7 @@ describe("global voice input", () => {
164
let visibleDraft = "first";
165
const targetKey = visibleDraft;
166
await session.start(
178
- createTarget(
167
+ createVoiceInputTarget(
168
targetKey,
169
() => drafts.get(targetKey) ?? null,
170
(text) => drafts.set(targetKey, text),
@@ -207,7 +196,7 @@ describe("global voice input", () => {
196
const firstCommit = vi.fn();
197
const secondCommit = vi.fn();
198
const starting = session.start(
210
- createTarget("first", () => "first", firstCommit, { start: 5, end: 5 }),
199
+ createVoiceInputTarget("first", () => "first", firstCommit, { start: 5, end: 5 }),
200
);
201
await preparationEntered.promise;
202
let stopping: Promise<void> | null = null;
@@ -226,7 +215,7 @@ describe("global voice input", () => {
215
await transcriptionEntered.promise;
216
}
217
await session.start(
229
- createTarget("second", () => "second", secondCommit, { start: 6, end: 6 }),
218
+ createVoiceInputTarget("second", () => "second", secondCommit, { start: 6, end: 6 }),
219
);
220
expect(session.ownerKey).toBe("first");
221
expect(session.controller.currentState.phase).toBe(phase);
@@ -247,7 +236,9 @@ describe("global voice input", () => {
236
const { session } = createSession();
237
let text: string | null = "first";
238
const commit = vi.fn();
250
- await session.start(createTarget("first", () => text, commit, { start: 5, end: 5 }));
239
+ await session.start(
240
+ createVoiceInputTarget("first", () => text, commit, { start: 5, end: 5 }),
241
+ );
242
text = change === "removed" ? null : "edited prompt";
243
await session.controller.stop();
244
expect(commit).not.toHaveBeenCalled();
@@ -255,10 +246,26 @@ describe("global voice input", () => {
246
},
247
);
248
249
+ it("retries a failed start against the draft as edited since", async () => {
250
+ const { session, prepare } = createSession();
251
+ prepare.mockRejectedValueOnce(new Error("model unavailable"));
252
+ let text = "first";
253
+ const commit = vi.fn();
254
+ await session.start(createVoiceInputTarget("first", () => text, commit, { start: 5, end: 5 }));
255
+ expect(session.controller.currentState.phase).toBe("error");
256
+ text = "first draft";
257
+ await session.retry();
258
+ await session.controller.stop();
259
+ expect(prepare).toHaveBeenCalledTimes(2);
260
+ expect(commit).toHaveBeenCalledWith("first spoken text draft", { start: 17, end: 17 });
261
+ });
262
+
263
it("finishes the original draft at the recording limit while it is off screen", async () => {
264
const { session, recorder } = createSession();
265
const commit = vi.fn();
261
- await session.start(createTarget("first", () => "first", commit, { start: 5, end: 5 }));
266
+ await session.start(
267
+ createVoiceInputTarget("first", () => "first", commit, { start: 5, end: 5 }),
268
+ );
269
await session.controller.handleRecorderStatus({
270
isFinished: true,
271
hasError: false,
@@ -269,41 +276,14 @@ describe("global voice input", () => {
276
expect(session.controller.currentState.phase).toBe("idle");
277
});
278
272
- it("rejects a transcript when its off-screen draft changes and returns to the original text", async () => {
273
- const registry = AtomRegistry.make();
274
- const draft = Atom.make("hello world");
275
- const unsubscribe = vi.fn();
276
- const { session } = createSession();
277
- const commit = vi.fn();
278
- const target = createVoiceInputTarget(
279
- "first",
280
- () => registry.get(draft),
281
- commit,
282
- { start: 6, end: 6 },
283
- (onChange) => {
284
- const stop = registry.subscribe(draft, onChange);
285
- return () => {
286
- stop();
287
- unsubscribe();
288
- };
289
- },
290
- );
291
- await session.start(target);
292
- registry.set(draft, "changed");
293
- registry.set(draft, "hello world");
294
- await session.controller.stop();
295
- expect(commit).not.toHaveBeenCalled();
296
- expect(session.controller.currentState.error).toContain("draft changed");
297
- expect(unsubscribe).toHaveBeenCalledTimes(1);
298
- registry.dispose();
299
- });
300
-
279
it("stops recording when its queued edit is discarded", async () => {
280
const { session, recorder } = createSession();
281
const commit = vi.fn();
282
let draft: string | null = "queued prompt";
283
const ownerKey = "thread~queued-edit~run";
306
- await session.start(createTarget(ownerKey, () => draft, commit, { start: 13, end: 13 }));
284
+ await session.start(
285
+ createVoiceInputTarget(ownerKey, () => draft, commit, { start: 13, end: 13 }),
286
+ );
287
session.cancel(ownerKey);
288
draft = null;
289
await session.controller.stop();
@@ -319,7 +299,9 @@ describe("global voice input", () => {
299
it("keeps another prompt's recording when a queued edit is discarded", async () => {
300
const { session, recorder } = createSession();
301
const commit = vi.fn();
322
- await session.start(createTarget("other prompt", () => "hello", commit, { start: 5, end: 5 }));
302
+ await session.start(
303
+ createVoiceInputTarget("other prompt", () => "hello", commit, { start: 5, end: 5 }),
304
+ );
305
session.cancel("thread~queued-edit~run");
306
expect(recorder.stop).not.toHaveBeenCalled();
307
expect(session.controller.currentState.phase).toBe("recording");
@@ -338,12 +320,12 @@ describe("global voice input", () => {
320
const oldCommit = vi.fn();
321
const nextCommit = vi.fn();
322
const firstStart = session.start(
341
- createTarget("first", () => "first", oldCommit, { start: 5, end: 5 }),
323
+ createVoiceInputTarget("first", () => "first", oldCommit, { start: 5, end: 5 }),
324
);
325
await preparationEntered.promise;
326
session.cancel("first");
327
const nextStart = session.start(
346
- createTarget("second", () => "second", nextCommit, { start: 6, end: 6 }),
328
+ createVoiceInputTarget("second", () => "second", nextCommit, { start: 6, end: 6 }),
329
);
330
expect(prepare).toHaveBeenCalledTimes(1);
331
expect(recorder.record).not.toHaveBeenCalled();
@@ -366,7 +348,9 @@ describe("global voice input", () => {
348
const snapshots: number[] = [];
349
session.subscribe(() => snapshots.push(session.getSnapshot().elapsedSeconds));
350
369
- await session.start(createTarget("first", () => "first", vi.fn(), { start: 5, end: 5 }));
351
+ await session.start(
352
+ createVoiceInputTarget("first", () => "first", vi.fn(), { start: 5, end: 5 }),
353
+ );
354
expect(session.getSnapshot().state.phase).toBe("recording");
355
expect(keepAwake).toHaveBeenCalledTimes(1);
356
// Silence on a silent history publishes nothing.
@@ -388,7 +372,9 @@ describe("global voice input", () => {
372
expect(session.getSnapshot().elapsedSeconds).toBe(2);
373
374
sample = { durationMillis: 0, metering: -100 };
391
- await session.start(createTarget("first", () => "first", vi.fn(), { start: 5, end: 5 }));
375
+ await session.start(
376
+ createVoiceInputTarget("first", () => "first", vi.fn(), { start: 5, end: 5 }),
377
+ );
378
expect(session.getSnapshot().elapsedSeconds).toBe(0);
379
expect(publishAudioLevels).toHaveBeenLastCalledWith(expect.not.arrayContaining([1]));
380
expect(keepAwake).toHaveBeenCalledTimes(2);
apps/mobile/src/features/voice-input/voiceInputSession.ts
+8
-24
@@ -19,7 +19,6 @@ export type VoiceInputTarget = {
19
readonly label?: string;
20
readonly readDraft: () => VoiceDraftSnapshot | null;
21
readonly commitDraft: VoiceInputControllerDependencies["commitDraft"];
22
- readonly subscribe: () => () => void;
22
};
23
24
export type VoiceInputSnapshot = {
@@ -52,27 +51,14 @@ export function createVoiceInputTarget(
51
readText: () => string | null,
52
commitDraft: VoiceInputTarget["commitDraft"],
53
selection: VoiceDraftSnapshot["selection"],
55
- subscribeToChanges: (onChange: () => void) => () => void,
54
): VoiceInputTarget {
57
- let revision = 0;
55
return {
56
ownerKey,
57
readDraft: () => {
58
const text = readText();
62
- if (text === null) return null;
63
- return { ownerKey, text, selection, revision };
59
+ return text === null ? null : { ownerKey, text, selection };
60
},
61
commitDraft,
66
- subscribe: () => {
67
- let previousText = readText();
68
- return subscribeToChanges(() => {
69
- const text = readText();
70
- if (text !== previousText) {
71
- previousText = text;
72
- revision += 1;
73
- }
74
- });
75
- },
62
};
63
}
64
@@ -84,7 +70,7 @@ export class VoiceInputSession {
70
readonly controller: VoiceInputController;
71
private readonly dependencies: VoiceInputSessionDependencies;
72
private target: VoiceInputTarget | null = null;
87
- private unsubscribe: (() => void) | null = null;
73
+ private startText: string | null = null;
74
private snapshot: VoiceInputSnapshot = {
75
state: IDLE_STATE,
76
ownerKey: null,
@@ -102,7 +88,11 @@ export class VoiceInputSession {
88
this.controller = new VoiceInputController({
89
...dependencies,
90
onStateChange: (state) => this.handleStateChange(state),
105
- readDraft: () => this.target?.readDraft() ?? null,
91
+ // The target's selection is fixed at start, so a draft changed since refuses the transcript.
92
+ readDraft: () => {
93
+ const draft = this.target?.readDraft() ?? null;
94
+ return draft?.text === this.startText ? draft : null;
95
+ },
96
commitDraft: (text, selection) => this.target?.commitDraft(text, selection),
97
});
98
}
@@ -141,23 +131,17 @@ export class VoiceInputSession {
131
start(target: VoiceInputTarget): Promise<void> {
132
if (voiceInputBlocksSubmission(this.controller.currentState)) return Promise.resolve();
133
this.target = target;
144
- this.unsubscribe = target.subscribe();
134
+ this.startText = target.readDraft()?.text ?? null;
135
return this.controller.start();
136
}
137
138
dispose(): void {
139
this.controller.dispose();
140
this.stopRecordingActivity();
151
- this.unsubscribe?.();
152
- this.unsubscribe = null;
141
this.listeners.clear();
142
}
143
144
private handleStateChange(state: VoiceInputState): void {
157
- if (!voiceInputBlocksSubmission(state)) {
158
- this.unsubscribe?.();
159
- this.unsubscribe = null;
160
- }
145
const previousPhase = this.snapshot.state.phase;
146
let elapsedSeconds = this.snapshot.elapsedSeconds;
147
if (state.phase !== previousPhase) {
apps/web/src/components/ComposerPromptEditorTiptap.citationDraft.browser.test.tsx
+26
-1
@@ -31,10 +31,12 @@ const promptBefore = `See ${serializeAssistantCitation(citation)} `;
31
function Composer({
32
question,
33
editorRef,
34
+ disabled = false,
35
send = "clears",
36
}: {
37
question: boolean;
38
editorRef: React.RefObject<ComposerPromptEditorHandle | null>;
39
+ disabled?: boolean;
40
send?: "clears" | "blocked" | "refusedLater";
41
}) {
42
const [prompt, setPrompt] = useState(promptBefore);
@@ -57,7 +59,7 @@ function Composer({
59
cursor={0}
60
contextRecords={new Map()}
61
skills={[]}
60
- disabled={false}
62
+ disabled={disabled}
63
placeholder=""
64
onChange={(next) => (question ? setAnswer(next) : setPrompt(next))}
65
onCitationDraftSave={(replacedValue, savedValue) => {
@@ -159,3 +161,26 @@ it.each(["blocked", "refusedLater"] as const)(
161
.toHaveAttribute("data-prompt", `See ${serializeAssistantCitation(citation)}x `);
162
},
163
);
164
+
165
+it("cancels a new citation while dictation makes the composer read-only", async () => {
166
+ const editorRef = createRef<ComposerPromptEditorHandle>();
167
+ await using screen = await render(<Composer question={false} editorRef={editorRef} disabled />);
168
+
169
+ const sourceAnchor = findAssistantCitationSourceAnchor(document, citation);
170
+ if (!sourceAnchor) throw new Error("Citation source did not resolve");
171
+ await act(async () =>
172
+ editorRef.current?.requestCitationComment({
173
+ previousValue: "See ",
174
+ value: promptBefore,
175
+ citationStart: "See ".length,
176
+ sourceAnchor,
177
+ insertedSpaces: { before: false, after: true },
178
+ }),
179
+ );
180
+ await expect.element(page.getByLabelText("Comment on selected text")).toBeVisible();
181
+
182
+ await page.getByRole("button", { name: "Cancel" }).click();
183
+
184
+ await expect.element(page.getByLabelText("Comment on selected text")).not.toBeInTheDocument();
185
+ expect(screen.container.querySelector("output")?.dataset.prompt).toBe("See ");
186
+});
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+4
-2
@@ -468,7 +468,7 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
468
469
const removeOnCancel = commentTarget?.removeOnCancel;
470
const onRemove = () => {
471
- if (!editor.isEditable || !removeOnCancel) return;
471
+ if (!removeOnCancel) return;
472
const pos = nodePos();
473
if (pos === null) return;
474
const { doc } = editor.state;
@@ -477,7 +477,9 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) {
477
const end = pos + current.nodeSize;
478
const from = removeOnCancel.before && doc.textBetween(pos - 1, pos) === " " ? pos - 1 : pos;
479
const to = removeOnCancel.after && doc.textBetween(end, end + 1) === " " ? end + 1 : end;
480
- editor.chain().focus().deleteRange({ from, to }).run();
480
+ // A read-only composer (dictation) can still receive a new citation, so Cancel must undo it.
481
+ const chain = editor.chain();
482
+ (editor.isEditable ? chain.focus() : chain).deleteRange({ from, to }).run();
483
};
484
485
const sentRef = useRef(false);
apps/web/src/components/chat/ChatComposer.tsx
-1
@@ -1810,7 +1810,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
1810
projectId: activeProjectId,
1811
projectName,
1812
ownerKey: questionAttachmentTarget ?? composerDraftTargetKey,
1813
- draftText: prompt,
1813
disabled: dictationDisabled,
1814
readDraft: () => {
1815
const snapshot = readComposerSnapshot();
apps/web/src/components/preview/PreviewAnnotationSpeech.tsx
+1
-5
@@ -5,7 +5,7 @@ import type {
5
ScopedThreadRef,
6
} from "@t3tools/contracts";
7
import { useNavigate } from "@tanstack/react-router";
8
-import { useEffect, useRef, useState } from "react";
8
+import { useEffect, useRef } from "react";
9
10
import { resolveSpeechPresentation } from "~/components/chat/ComposerSpeechButton";
11
import { VoiceInputSetup } from "~/components/chat/VoiceInputSetup";
@@ -40,7 +40,6 @@ export function PreviewAnnotationSpeech({
40
projectId ? scopeProjectRef(threadRef.environmentId, projectId) : null,
41
);
42
const draft = useRef({ text: "", cursor: 0, selectionEnd: 0 });
43
- const [text, setText] = useState("");
43
const keys = useRef<DictationKeyHandlers | null>(null);
44
const guestFocused = useRef(false);
45
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- notices focus returning from the preview guest to this window
@@ -60,7 +59,6 @@ export function PreviewAnnotationSpeech({
59
projectId,
60
projectName: project?.title,
61
ownerKey: JSON.stringify(["preview-annotation", threadRef, tabId, config.sessionId]),
63
- draftText: text,
62
offerSetup: false,
63
readDraft: () => ({
64
text: draft.current.text,
@@ -68,7 +66,6 @@ export function PreviewAnnotationSpeech({
66
}),
67
commitDraft: (next, selection) => {
68
draft.current = { text: next, cursor: selection.start, selectionEnd: selection.end };
71
- setText(next);
69
void publish(current.current.voiceState, { text: next, cursor: selection.start });
70
},
71
});
@@ -136,7 +133,6 @@ export function PreviewAnnotationSpeech({
133
cursor: Math.min(event.cursor, event.text.length),
134
selectionEnd: Math.min(event.selectionEnd ?? event.cursor, event.text.length),
135
};
139
- setText(event.text);
136
if (event.action === "focus" || event.action === "key" || event.action === "start")
137
guestFocused.current = true;
138
if (event.action === "blur") guestFocused.current = false;
apps/web/src/components/settings/VoiceSettingsPanel.tsx
-1
@@ -1083,7 +1083,6 @@ function TranscriptionTest(props: {
1083
projectId: props.projectId,
1084
projectName: props.projectName,
1085
ownerKey: JSON.stringify([props.environmentId, props.projectId]),
1086
- draftText: draft,
1086
cleanup: false,
1087
readDraft: () => ({
1088
text: draftRef.current,
apps/web/src/speech/useDictationShortcut.browser.test.tsx
+132
-2
@@ -1,17 +1,30 @@
1
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
2
+import { createRef, type RefObject } from "react";
3
import { expect, it, vi } from "vite-plus/test";
4
import { render } from "../../test/render";
4
-import { useDictationShortcut } from "./useDictationShortcut";
5
+import { useDictationShortcut, type DictationKeyHandlers } from "./useDictationShortcut";
6
7
vi.mock("../hooks/useSettings", () => ({ useClientSettings: () => "hold" }));
8
8
-function Probe({ speech }: { speech: Parameters<typeof useDictationShortcut>[0]["speech"] }) {
9
+type ShortcutInput = Parameters<typeof useDictationShortcut>[0];
10
+
11
+function Probe({
12
+ speech,
13
+ targetRef,
14
+ subscribeKeys,
15
+}: {
16
+ speech: ShortcutInput["speech"];
17
+ targetRef?: RefObject<HTMLElement | null>;
18
+ subscribeKeys?: ShortcutInput["subscribeKeys"];
19
+}) {
20
useDictationShortcut({
21
keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
22
speech,
23
disabled: false,
24
terminalOpen: false,
25
modelPickerOpen: false,
26
+ ...(targetRef ? { targetRef } : {}),
27
+ ...(subscribeKeys ? { subscribeKeys, ownsFocus: () => true } : {}),
28
});
29
return null;
30
}
@@ -80,3 +93,120 @@ it("finishes a released hold after startup even if the chord was pressed again w
93
expect(speech.start).toHaveBeenCalledOnce();
94
expect(speech.stop).toHaveBeenCalledOnce();
95
});
96
+
97
+const escape = () =>
98
+ new KeyboardEvent("keydown", { key: "Escape", code: "Escape", bubbles: true, cancelable: true });
99
+
100
+const recording = () => ({
101
+ ...actions(true),
102
+ state: { phase: "recording" as const, error: null, errorAction: null },
103
+});
104
+
105
+it("lets Escape in another dictation editor reach that editor instead of canceling the recording", async () => {
106
+ const speech = recording();
107
+ const commentRef = createRef<HTMLTextAreaElement>();
108
+ const editorEscape = vi.fn();
109
+ await using screen = await render(
110
+ <>
111
+ <Probe speech={speech} />
112
+ <Probe speech={actions(true)} targetRef={commentRef} />
113
+ <div
114
+ role="dialog"
115
+ aria-label="Edit citation comment"
116
+ onKeyDown={(event) => {
117
+ if (event.key === "Escape") editorEscape();
118
+ }}
119
+ >
120
+ <textarea aria-label="Comment" ref={commentRef} />
121
+ <button type="button">Cancel</button>
122
+ </div>
123
+ </>,
124
+ );
125
+ for (const control of [
126
+ screen.getByRole("textbox", { name: "Comment" }).element(),
127
+ screen.getByRole("button", { name: "Cancel" }).element(),
128
+ ]) {
129
+ if (!(control instanceof HTMLElement)) throw new Error("expected a comment editor control");
130
+ control.focus();
131
+ control.dispatchEvent(escape());
132
+ }
133
+ expect(speech.cancel).not.toHaveBeenCalled();
134
+ expect(editorEscape).toHaveBeenCalledTimes(2);
135
+});
136
+
137
+it("lets Escape in another editor of the recording editor's dialog reach that editor", async () => {
138
+ const speech = recording();
139
+ const replyRef = createRef<HTMLTextAreaElement>();
140
+ const otherReplyRef = createRef<HTMLTextAreaElement>();
141
+ await using screen = await render(
142
+ <>
143
+ <Probe speech={speech} targetRef={replyRef} />
144
+ <Probe speech={actions(true)} targetRef={otherReplyRef} />
145
+ <div role="dialog" aria-label="Pull request">
146
+ <textarea aria-label="Reply A" ref={replyRef} />
147
+ <textarea aria-label="Reply B" ref={otherReplyRef} />
148
+ </div>
149
+ </>,
150
+ );
151
+ const other = screen.getByRole("textbox", { name: "Reply B" }).element();
152
+ if (!(other instanceof HTMLElement)) throw new Error("expected a reply editor");
153
+ other.focus();
154
+ other.dispatchEvent(escape());
155
+ expect(speech.cancel).not.toHaveBeenCalled();
156
+});
157
+
158
+it("cancels the recording on Escape outside other shown dictation editors", async () => {
159
+ const speech = recording();
160
+ const reviewRef = createRef<HTMLTextAreaElement>();
161
+ const commentRef = createRef<HTMLTextAreaElement>();
162
+ await using screen = await render(
163
+ <>
164
+ <Probe speech={speech} targetRef={reviewRef} />
165
+ <Probe speech={actions(true)} targetRef={commentRef} />
166
+ <input aria-label="Search" />
167
+ <div role="dialog" aria-label="Pull request">
168
+ <textarea aria-label="Review" ref={reviewRef} />
169
+ <div hidden>
170
+ <textarea aria-label="Comment" ref={commentRef} />
171
+ </div>
172
+ <button type="button">Submit</button>
173
+ </div>
174
+ </>,
175
+ );
176
+ for (const control of [
177
+ screen.getByRole("textbox", { name: "Search" }).element(),
178
+ screen.getByRole("button", { name: "Submit" }).element(),
179
+ ]) {
180
+ if (!(control instanceof HTMLElement)) throw new Error("expected a focusable control");
181
+ control.focus();
182
+ control.dispatchEvent(escape());
183
+ }
184
+ expect(speech.cancel).toHaveBeenCalledTimes(2);
185
+});
186
+
187
+it.each([
188
+ ["another editor is recording", false],
189
+ ["the preview itself is recording", true],
190
+])(
191
+ "routes Escape relayed from the preview guest to the preview when %s",
192
+ async (_name, previewRecords) => {
193
+ const composer = previewRecords ? actions(true) : recording();
194
+ const preview = previewRecords ? recording() : actions(true);
195
+ let relayed: DictationKeyHandlers | undefined;
196
+ await using _screen = await render(
197
+ <>
198
+ <Probe speech={composer} />
199
+ <Probe
200
+ speech={preview}
201
+ subscribeKeys={(handlers) => {
202
+ relayed = handlers;
203
+ return () => {};
204
+ }}
205
+ />
206
+ </>,
207
+ );
208
+ relayed?.keydown(escape());
209
+ expect(composer.cancel).not.toHaveBeenCalled();
210
+ expect(preview.cancel).toHaveBeenCalledTimes(previewRecords ? 1 : 0);
211
+ },
212
+);
apps/web/src/speech/useDictationShortcut.ts
+33
-3
@@ -60,6 +60,31 @@ function ownsShortcut(id: symbol) {
60
return entries.find(([, read]) => !read().targetRef && !read().ownsFocus)?.[0] === id;
61
}
62
63
+const targetScope = (field: HTMLElement | null | undefined) =>
64
+ field?.closest('[role="dialog"]') ?? field;
65
+
66
+// Keys an outside editor (the preview guest) relays arrive on window, so remember their editor.
67
+const relayedFrom = new WeakMap<Event, symbol>();
68
+
69
+// Escape inside another shown dictation editor, or the dialog hosting it, belongs to that editor.
70
+// The editor itself is checked before dialogs, since one dialog can host several editors.
71
+function inOtherShortcutTarget(id: symbol, event: Event) {
72
+ const source = relayedFrom.get(event);
73
+ if (source) return source !== id;
74
+ const target = event.target;
75
+ if (!(target instanceof Node)) return false;
76
+ const own = shortcutTargets.get(id)?.().targetRef?.current;
77
+ const others = [...shortcutTargets]
78
+ .filter(([other]) => other !== id)
79
+ .map(([, read]) => read().targetRef?.current)
80
+ .filter((field): field is HTMLElement => !!field?.getClientRects().length);
81
+ for (const scope of [(field?: HTMLElement | null) => field, targetScope]) {
82
+ if (scope(own)?.contains(target)) return false;
83
+ if (others.some((field) => scope(field)?.contains(target))) return true;
84
+ }
85
+ return false;
86
+}
87
+
88
export function useDictationShortcut(input: ShortcutInput) {
89
const id = useRef(Symbol("dictation-target"));
90
const subscribeKeys = input.subscribeKeys;
@@ -103,7 +128,8 @@ export function useDictationShortcut(input: ShortcutInput) {
128
if (
129
event.key === "Escape" &&
130
(pressed.current !== null ||
106
- (speech.state.phase !== "idle" && speech.state.phase !== "error"))
131
+ (speech.state.phase !== "idle" && speech.state.phase !== "error")) &&
132
+ !inOtherShortcutTarget(id.current, event)
133
) {
134
event.preventDefault();
135
event.stopImmediatePropagation();
@@ -146,9 +172,13 @@ export function useDictationShortcut(input: ShortcutInput) {
172
};
173
};
174
const onBlur = () => finish(null);
175
+ const relay = (event: KeyboardEvent) => {
176
+ relayedFrom.set(event, id.current);
177
+ window.dispatchEvent(event);
178
+ };
179
const unsubscribe = subscribeKeys?.({
150
- keydown: (event) => window.dispatchEvent(event),
151
- keyup: (event) => window.dispatchEvent(event),
180
+ keydown: relay,
181
+ keyup: relay,
182
blur: onBlur,
183
});
184
window.addEventListener("keydown", onKeyDown, true);
apps/web/src/speech/useEnvironmentSpeechInput.browser.test.tsx
-1
@@ -256,7 +256,6 @@ function Probe(props: {
256
projectName: props.projectName,
257
cleanup: props.cleanup,
258
ownerKey: "draft",
259
- draftText: mocks.draft,
259
readDraft: () => ({
260
text: mocks.draft,
261
selection: { start: mocks.draft.length, end: mocks.draft.length },
apps/web/src/speech/useEnvironmentSpeechInput.ts
+1
-28
@@ -92,7 +92,6 @@ type HookInput = {
92
readonly projectId?: ProjectId | null | undefined;
93
readonly projectName?: string | null | undefined;
94
readonly ownerKey: string;
95
- readonly draftText: string;
95
readonly disabled?: boolean | undefined;
96
/**
97
* Until a destination is chosen, voice input is offered and its first gesture opens Voice
@@ -212,19 +211,6 @@ export function useEnvironmentSpeechInput(input: HookInput) {
211
useEffect(() => {
212
latestRef.current = { input, clientSettings, cleanupPrepared, projectWords };
213
});
215
- const draftRevisionRef = useRef({ ownerKey: input.ownerKey, text: input.draftText, revision: 0 });
216
- // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- tracks committed draft revisions so a late transcript cannot overwrite an edited draft
217
- useEffect(() => {
218
- const revision = draftRevisionRef.current;
219
- if (revision.ownerKey !== input.ownerKey || revision.text !== input.draftText) {
220
- draftRevisionRef.current = {
221
- ownerKey: input.ownerKey,
222
- text: input.draftText,
223
- revision: revision.revision + 1,
224
- };
225
- }
226
- }, [input.ownerKey, input.draftText]);
227
-
214
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- owns the recording controller bound to the consented destination; it is disposed with the consent
215
useEffect(() => {
216
if (!prepared || !grantId || typeof navigator === "undefined") return;
@@ -242,20 +228,7 @@ export function useEnvironmentSpeechInput(input: HookInput) {
228
const readDraft = (): VoiceDraftSnapshot => {
229
const current = latestRef.current.input;
230
const draft = current.readDraft();
245
- const revision = draftRevisionRef.current;
246
- if (revision.ownerKey !== current.ownerKey || revision.text !== draft.text) {
247
- draftRevisionRef.current = {
248
- ownerKey: current.ownerKey,
249
- text: draft.text,
250
- revision: revision.revision + 1,
251
- };
252
- }
253
- return {
254
- ownerKey: current.ownerKey,
255
- text: draft.text,
256
- selection: draft.selection,
257
- revision: draftRevisionRef.current.revision,
258
- };
231
+ return { ownerKey: current.ownerKey, text: draft.text, selection: draft.selection };
232
};
233
const platform = createBrowserVoiceInputPlatform({
234
prepared,
apps/web/src/speech/useTextFieldSpeech.tsx
-1
@@ -31,7 +31,6 @@ export function useTextFieldSpeech(input: {
31
environmentId: input.environmentId,
32
projectId: input.projectId,
33
ownerKey: input.ownerKey,
34
- draftText: input.text,
34
disabled: input.disabled,
35
offerSetup: false,
36
readDraft: () => {
docs/user/voice-input.md
+2
@@ -83,6 +83,8 @@ macOS and Ctrl elsewhere). Press again, or the check mark, to finish; `Esc`
83
discards the recording and leaves your draft unchanged. Recordings last up to
84
five minutes. Text is inserted at the cursor or replaces the selection you had
85
when recording started, and the editor is read-only while dictation runs.
86
+Changes that reach the draft meanwhile, such as a cited message, are kept;
87
+the text then goes after your selection instead of replacing it.
88
89
Pressing Send while dictating finishes the recording and sends the draft once
90
the transcript has been inserted. Cancelling also cancels that queued send. In
packages/client-runtime/src/voice-input/controller.test.ts
+63
-9
@@ -49,7 +49,6 @@ function draft(overrides: Partial<VoiceDraftSnapshot> = {}): VoiceDraftSnapshot
49
ownerKey: "environment:thread",
50
text: "hello world",
51
selection: { start: 6, end: 11 },
52
- revision: 1,
52
...overrides,
53
};
54
}
@@ -102,7 +101,7 @@ describe("environment voice input", () => {
101
expect(harness.controller.currentState.phase).toBe("error");
102
expect(harness.deleted).toEqual([]);
103
105
- harness.setDraft(draft({ text: "updated ", revision: 2, selection: { start: 8, end: 8 } }));
104
+ harness.setDraft(draft({ text: "updated ", selection: { start: 8, end: 8 } }));
105
await harness.controller.start();
106
expect(transcribe.mock.calls.map(([uri]) => uri)).toEqual([
107
"file:///voice.m4a",
@@ -280,19 +279,74 @@ describe("resolveTranscriptCommit", () => {
279
});
280
});
281
283
- it("does not replace text after the owner, text, or revision changes", () => {
282
+ it.each([
283
+ [
284
+ "at the start of the selection",
285
+ { start: 6, end: 11 },
286
+ "hello [cite]world",
287
+ "hello [cite]world new text",
288
+ ],
289
+ [
290
+ "inside the selection",
291
+ { start: 6, end: 11 },
292
+ "hello wo[cite]rld",
293
+ "hello wo[cite]rld new text",
294
+ ],
295
+ ["at the caret", { start: 6, end: 6 }, "hello [cite] world", "hello [cite] new text world"],
296
+ [
297
+ "that repeats the selection",
298
+ { start: 0, end: 5 },
299
+ "hello there hello world",
300
+ "hello there hello new text world",
301
+ ],
302
+ [
303
+ "that repeats the selection next to it",
304
+ { start: 0, end: 5 },
305
+ "hello hello world",
306
+ "hello hello new text world",
307
+ ],
308
+ ])("keeps text inserted %s while recording", (_, selection, current, expected) => {
309
+ const captured = draft({ text: "hello world", selection });
310
+ const result = resolveTranscriptCommit(
311
+ captured,
312
+ draft({ text: current, selection: { start: 0, end: 0 } }),
313
+ "new text",
314
+ "en-US",
315
+ );
316
+ expect(result).toMatchObject({ kind: "commit", text: expected });
317
+ });
318
+
319
+ it("keeps every citation when several were inserted while recording", () => {
320
+ const captured = draft({ text: "[todo", selection: { start: 0, end: 1 } });
321
+ const current = "[A](t3-citation://v1/a)[todo[B](t3-citation://v1/b)";
322
+ const result = resolveTranscriptCommit(
323
+ captured,
324
+ draft({ text: current, selection: { start: 0, end: 0 } }),
325
+ "spoken",
326
+ "en-US",
327
+ );
328
+ expect(result).toMatchObject({ kind: "commit", text: `${current} spoken` });
329
+ });
330
+
331
+ it("keeps surrogate pairs whole when an emoji changed while recording", () => {
332
+ const captured = draft({ text: "🔔", selection: { start: 0, end: 0 } });
333
+ const result = resolveTranscriptCommit(
334
+ captured,
335
+ draft({ text: "🤔", selection: { start: 0, end: 0 } }),
336
+ "hello",
337
+ "en-US",
338
+ );
339
+ expect(result).toMatchObject({ kind: "commit", text: "🤔hello" });
340
+ });
341
+
342
+ it("does not insert into a missing draft or another owner's draft", () => {
343
const captured = draft();
344
+ expect(resolveTranscriptCommit(captured, null, "text", "en-US")).toEqual({ kind: "stale" });
345
expect(
346
resolveTranscriptCommit(captured, draft({ ownerKey: "other" }), "text", "en-US"),
347
).toEqual({
348
kind: "stale",
349
});
290
- expect(resolveTranscriptCommit(captured, draft({ text: "newer" }), "text", "en-US")).toEqual({
291
- kind: "stale",
292
- });
293
- expect(resolveTranscriptCommit(captured, draft({ revision: 2 }), "text", "en-US")).toEqual({
294
- kind: "stale",
295
- });
350
});
351
});
352
packages/client-runtime/src/voice-input/controller.ts
+40
-17
@@ -50,7 +50,6 @@ export type VoiceDraftSnapshot = {
50
readonly ownerKey: string;
51
readonly text: string;
52
readonly selection: { readonly start: number; readonly end: number };
53
- readonly revision: number;
53
};
54
55
export type VoiceRecorderStatus = {
@@ -109,18 +108,37 @@ type TranscriptCommitResult =
108
109
const WORD_SPACELESS_LANGUAGES = new Set(["ja", "zh", "yue", "th", "lo", "km", "my"]);
110
111
+const isHighSurrogate = (code: number) => code >= 0xd800 && code <= 0xdbff;
112
+const isLowSurrogate = (code: number) => code >= 0xdc00 && code <= 0xdfff;
113
+
114
+// Where `position` in `before` lands in `text`, placed after anything inserted at it.
115
+function mapPosition(before: string, text: string, position: number, suffixFirst: boolean) {
116
+ const shared = Math.min(before.length, text.length);
117
+ const matchesFromEnd = (offset: number) =>
118
+ before[before.length - 1 - offset] === text[text.length - 1 - offset];
119
+ let prefix = 0;
120
+ let suffix = 0;
121
+ // Matching is per UTF-16 unit; never let a boundary split a surrogate pair.
122
+ const scanSuffix = (limit: number) => {
123
+ while (suffix < limit && matchesFromEnd(suffix)) suffix += 1;
124
+ if (suffix > 0 && isLowSurrogate(before.charCodeAt(before.length - suffix))) suffix -= 1;
125
+ };
126
+ if (suffixFirst) scanSuffix(shared);
127
+ while (prefix < shared - suffix && before[prefix] === text[prefix]) prefix += 1;
128
+ if (prefix > 0 && isHighSurrogate(before.charCodeAt(prefix - 1))) prefix -= 1;
129
+ if (!suffixFirst) scanSuffix(shared - prefix);
130
+ if (position < prefix) return position;
131
+ if (position >= before.length - suffix) return position + text.length - before.length;
132
+ return text.length - suffix;
133
+}
134
+
135
export function resolveTranscriptCommit(
136
captured: VoiceDraftSnapshot,
137
current: VoiceDraftSnapshot | null,
138
transcript: string,
139
locale: string,
140
): TranscriptCommitResult {
118
- if (
119
- !current ||
120
- current.ownerKey !== captured.ownerKey ||
121
- current.text !== captured.text ||
122
- current.revision !== captured.revision
123
- ) {
141
+ if (!current || current.ownerKey !== captured.ownerKey) {
142
return { kind: "stale" };
143
}
144
@@ -129,14 +147,24 @@ export function resolveTranscriptCommit(
147
return { kind: "empty" };
148
}
149
132
- const isEmptySelection = captured.selection.start === captured.selection.end;
150
+ const { text } = current;
151
+ // An edit made while recording (a citation, a paste) is never replaced. The edit's position is
152
+ // ambiguous over repeated text, so insert after the selection under both extreme alignments.
153
+ const unchanged = text === captured.text;
154
+ const end = unchanged
155
+ ? captured.selection.end
156
+ : Math.max(
157
+ mapPosition(captured.text, text, captured.selection.end, false),
158
+ mapPosition(captured.text, text, captured.selection.end, true),
159
+ );
160
+ const start = unchanged ? captured.selection.start : end;
161
+ const isEmptySelection = start === end;
162
const language = locale.split(/[-_]/)[0]!.toLowerCase();
163
let insertion = replacement;
164
if (isEmptySelection && !WORD_SPACELESS_LANGUAGES.has(language)) {
165
const auto = language === "auto";
137
- const caret = captured.selection.start;
138
- const left = captured.text.slice(Math.max(0, caret - 2), caret);
139
- const right = captured.text.slice(caret, caret + 2);
166
+ const left = text.slice(Math.max(0, start - 2), start);
167
+ const right = text.slice(start, start + 2);
168
const leftEdge = auto ? /[A-Za-z0-9.!?,:;)\]}'"]$/ : /(?:\p{L}|\p{M}|\p{N}|[.!?,:;)\]}'"])$/u;
169
const rightEdge = auto ? /^[A-Za-z0-9([{'"]/ : /^[\p{L}\p{M}\p{N}([{'"]/u;
170
const leftNeedsBoundary =
@@ -150,12 +178,7 @@ export function resolveTranscriptCommit(
178
insertion = `${leftNeedsBoundary ? " " : ""}${replacement}${rightNeedsBoundary ? " " : ""}`;
179
}
180
153
- const result = replaceTextRange(
154
- captured.text,
155
- captured.selection.start,
156
- captured.selection.end,
157
- insertion,
158
- );
181
+ const result = replaceTextRange(text, start, end, insertion);
182
return {
183
kind: "commit",
184
text: result.text,