Skip to content

Commit 1001bc7

Browse files
fix(voice): dictation survives draft changes and Escape in the citation box (#32682)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 78ee8a1 commit 1001bc7

19 files changed

Lines changed: 350 additions & 159 deletions

‎apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ import { useScheduledTaskDestinationLabel, useScheduledTaskEditor } from "./sche
8787
import {
8888
discardScheduledTaskEditor,
8989
markScheduledTaskEditorSaved,
90-
scheduledTaskEditorSessionAtom,
9190
} from "./scheduled-task-editor-state";
92-
import { appAtomRegistry } from "../../state/atom-registry";
9391
import {
9492
formatNextScheduledTaskRun,
9593
formatScheduledTaskInterval,
@@ -519,8 +517,6 @@ function SettingsScheduledTaskEditorScreen({ title }: { readonly title: string }
519517
const voiceInput = useVoiceInputController({
520518
ownerKey,
521519
label: editor?.draft.title.trim() || title,
522-
subscribeToDraftChanges: (onChange) =>
523-
appAtomRegistry.subscribe(scheduledTaskEditorSessionAtom, onChange),
524520
selection,
525521
readDraftMessage: () => {
526522
const current = readEditor();

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ import { armAgentAwarenessLiveActivityForLocalWork } from "../agent-awareness/re
5757
import { scopedThreadKey } from "../../lib/scopedEntities";
5858
import {
5959
getComposerDraftSnapshot,
60-
composerDraftsAtom,
6160
setComposerDraftText,
6261
composerContextImportsAtom,
6362
countComposerDraftAttachmentsAfterSelection,
6463
useComposerDraft,
6564
} from "../../state/use-composer-drafts";
66-
import { appAtomRegistry } from "../../state/atom-registry";
6765
import type { ComposerDocumentAttachment } from "../../lib/composerContext";
6866
import { useProject, useVisibleThreadShells } from "../../state/entities";
6967
import { useMentionableEnvironments } from "../../state/environments";
@@ -579,7 +577,6 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
579577
ownerKey: composerDraftKey,
580578
label: props.selectedThread.title || "Untitled thread",
581579
readDraftMessage: () => getComposerDraftSnapshot(composerDraftKey).text,
582-
subscribeToDraftChanges: (onChange) => appAtomRegistry.subscribe(composerDraftsAtom, onChange),
583580
selection: composerMenu.selection,
584581
onChangeDraftMessage: (text) => setComposerDraftText(composerDraftKey, text),
585582
onChangeSelection: composerMenu.onSelectionChange,

‎apps/mobile/src/features/voice-input/lazyVoiceRecorder.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ describe("createLazyVoiceRecorder", () => {
143143
ownerKey: "thread",
144144
text: "",
145145
selection: { start: 0, end: 0 },
146-
revision: 0,
147146
}),
148147
commitDraft: () => undefined,
149148
onStateChange: () => undefined,

‎apps/mobile/src/features/voice-input/useVoiceInputController.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export function useVoiceInputController(input: {
1515
readonly ownerKey: string | null;
1616
readonly label: string;
1717
readonly readDraftMessage: () => string | null;
18-
readonly subscribeToDraftChanges: (onChange: () => void) => () => void;
1918
readonly selection: ComposerEditorSelection;
2019
readonly disabled?: boolean;
2120
readonly onChangeDraftMessage: (value: string) => void;
@@ -58,7 +57,6 @@ export function useVoiceInputController(input: {
5857
}
5958
},
6059
captured.selection,
61-
captured.subscribeToDraftChanges,
6260
),
6361
label: captured.label,
6462
});

‎apps/mobile/src/features/voice-input/voiceInputSession.test.ts‎

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
22
import type { PreparedVoiceTranscription } from "@t3tools/client-runtime/voice-input";
3-
import { Atom, AtomRegistry } from "effect/reactivity";
43
import { resetVoiceInputGlobalsForTests } from "../../../../../packages/client-runtime/src/voice-input/controller";
54
import {
65
acquireAudioSession,
@@ -23,19 +22,9 @@ import {
2322
createVoiceInputTarget,
2423
VoiceInputSession,
2524
type VoiceInputSessionDependencies,
26-
type VoiceInputTarget,
2725
type VoiceRecordingSample,
2826
} from "./voiceInputSession";
2927

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-
3928
function createSession(
4029
overrides?: Partial<
4130
Pick<
@@ -116,7 +105,7 @@ describe("global voice input", () => {
116105
if (outcome === "activation failure") {
117106
audio.setActive.mockRejectedValueOnce(new Error("Audio activation failed"));
118107
}
119-
await session.start(createTarget("first", () => "", vi.fn(), { start: 0, end: 0 }));
108+
await session.start(createVoiceInputTarget("first", () => "", vi.fn(), { start: 0, end: 0 }));
120109
expect(pause).toHaveBeenCalledOnce();
121110
expect(isAudioSessionOwner(playback)).toBe(false);
122111
if (outcome === "complete") {
@@ -154,9 +143,9 @@ describe("global voice input", () => {
154143
async ({ selection, expected, cursor }) => {
155144
const { session } = createSession();
156145
const commit = vi.fn();
157-
await session.start(createTarget("first", () => "hello world", commit, selection));
146+
await session.start(createVoiceInputTarget("first", () => "hello world", commit, selection));
158147
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 }),
160149
);
161150
await session.controller.stop();
162151
expect(commit).toHaveBeenCalledWith(expected, {
@@ -175,7 +164,7 @@ describe("global voice input", () => {
175164
let visibleDraft = "first";
176165
const targetKey = visibleDraft;
177166
await session.start(
178-
createTarget(
167+
createVoiceInputTarget(
179168
targetKey,
180169
() => drafts.get(targetKey) ?? null,
181170
(text) => drafts.set(targetKey, text),
@@ -207,7 +196,7 @@ describe("global voice input", () => {
207196
const firstCommit = vi.fn();
208197
const secondCommit = vi.fn();
209198
const starting = session.start(
210-
createTarget("first", () => "first", firstCommit, { start: 5, end: 5 }),
199+
createVoiceInputTarget("first", () => "first", firstCommit, { start: 5, end: 5 }),
211200
);
212201
await preparationEntered.promise;
213202
let stopping: Promise<void> | null = null;
@@ -226,7 +215,7 @@ describe("global voice input", () => {
226215
await transcriptionEntered.promise;
227216
}
228217
await session.start(
229-
createTarget("second", () => "second", secondCommit, { start: 6, end: 6 }),
218+
createVoiceInputTarget("second", () => "second", secondCommit, { start: 6, end: 6 }),
230219
);
231220
expect(session.ownerKey).toBe("first");
232221
expect(session.controller.currentState.phase).toBe(phase);
@@ -247,18 +236,36 @@ describe("global voice input", () => {
247236
const { session } = createSession();
248237
let text: string | null = "first";
249238
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+
);
251242
text = change === "removed" ? null : "edited prompt";
252243
await session.controller.stop();
253244
expect(commit).not.toHaveBeenCalled();
254245
expect(session.controller.currentState.error).toContain("draft changed");
255246
},
256247
);
257248

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+
258263
it("finishes the original draft at the recording limit while it is off screen", async () => {
259264
const { session, recorder } = createSession();
260265
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+
);
262269
await session.controller.handleRecorderStatus({
263270
isFinished: true,
264271
hasError: false,
@@ -269,41 +276,14 @@ describe("global voice input", () => {
269276
expect(session.controller.currentState.phase).toBe("idle");
270277
});
271278

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-
301279
it("stops recording when its queued edit is discarded", async () => {
302280
const { session, recorder } = createSession();
303281
const commit = vi.fn();
304282
let draft: string | null = "queued prompt";
305283
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+
);
307287
session.cancel(ownerKey);
308288
draft = null;
309289
await session.controller.stop();
@@ -319,7 +299,9 @@ describe("global voice input", () => {
319299
it("keeps another prompt's recording when a queued edit is discarded", async () => {
320300
const { session, recorder } = createSession();
321301
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+
);
323305
session.cancel("thread~queued-edit~run");
324306
expect(recorder.stop).not.toHaveBeenCalled();
325307
expect(session.controller.currentState.phase).toBe("recording");
@@ -338,12 +320,12 @@ describe("global voice input", () => {
338320
const oldCommit = vi.fn();
339321
const nextCommit = vi.fn();
340322
const firstStart = session.start(
341-
createTarget("first", () => "first", oldCommit, { start: 5, end: 5 }),
323+
createVoiceInputTarget("first", () => "first", oldCommit, { start: 5, end: 5 }),
342324
);
343325
await preparationEntered.promise;
344326
session.cancel("first");
345327
const nextStart = session.start(
346-
createTarget("second", () => "second", nextCommit, { start: 6, end: 6 }),
328+
createVoiceInputTarget("second", () => "second", nextCommit, { start: 6, end: 6 }),
347329
);
348330
expect(prepare).toHaveBeenCalledTimes(1);
349331
expect(recorder.record).not.toHaveBeenCalled();
@@ -366,7 +348,9 @@ describe("global voice input", () => {
366348
const snapshots: number[] = [];
367349
session.subscribe(() => snapshots.push(session.getSnapshot().elapsedSeconds));
368350

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+
);
370354
expect(session.getSnapshot().state.phase).toBe("recording");
371355
expect(keepAwake).toHaveBeenCalledTimes(1);
372356
// Silence on a silent history publishes nothing.
@@ -388,7 +372,9 @@ describe("global voice input", () => {
388372
expect(session.getSnapshot().elapsedSeconds).toBe(2);
389373

390374
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+
);
392378
expect(session.getSnapshot().elapsedSeconds).toBe(0);
393379
expect(publishAudioLevels).toHaveBeenLastCalledWith(expect.not.arrayContaining([1]));
394380
expect(keepAwake).toHaveBeenCalledTimes(2);

‎apps/mobile/src/features/voice-input/voiceInputSession.ts‎

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export type VoiceInputTarget = {
1919
readonly label?: string;
2020
readonly readDraft: () => VoiceDraftSnapshot | null;
2121
readonly commitDraft: VoiceInputControllerDependencies["commitDraft"];
22-
readonly subscribe: () => () => void;
2322
};
2423

2524
export type VoiceInputSnapshot = {
@@ -52,27 +51,14 @@ export function createVoiceInputTarget(
5251
readText: () => string | null,
5352
commitDraft: VoiceInputTarget["commitDraft"],
5453
selection: VoiceDraftSnapshot["selection"],
55-
subscribeToChanges: (onChange: () => void) => () => void,
5654
): VoiceInputTarget {
57-
let revision = 0;
5855
return {
5956
ownerKey,
6057
readDraft: () => {
6158
const text = readText();
62-
if (text === null) return null;
63-
return { ownerKey, text, selection, revision };
59+
return text === null ? null : { ownerKey, text, selection };
6460
},
6561
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-
},
7662
};
7763
}
7864

@@ -84,7 +70,7 @@ export class VoiceInputSession {
8470
readonly controller: VoiceInputController;
8571
private readonly dependencies: VoiceInputSessionDependencies;
8672
private target: VoiceInputTarget | null = null;
87-
private unsubscribe: (() => void) | null = null;
73+
private startText: string | null = null;
8874
private snapshot: VoiceInputSnapshot = {
8975
state: IDLE_STATE,
9076
ownerKey: null,
@@ -102,7 +88,11 @@ export class VoiceInputSession {
10288
this.controller = new VoiceInputController({
10389
...dependencies,
10490
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+
},
10696
commitDraft: (text, selection) => this.target?.commitDraft(text, selection),
10797
});
10898
}
@@ -141,23 +131,17 @@ export class VoiceInputSession {
141131
start(target: VoiceInputTarget): Promise<void> {
142132
if (voiceInputBlocksSubmission(this.controller.currentState)) return Promise.resolve();
143133
this.target = target;
144-
this.unsubscribe = target.subscribe();
134+
this.startText = target.readDraft()?.text ?? null;
145135
return this.controller.start();
146136
}
147137

148138
dispose(): void {
149139
this.controller.dispose();
150140
this.stopRecordingActivity();
151-
this.unsubscribe?.();
152-
this.unsubscribe = null;
153141
this.listeners.clear();
154142
}
155143

156144
private handleStateChange(state: VoiceInputState): void {
157-
if (!voiceInputBlocksSubmission(state)) {
158-
this.unsubscribe?.();
159-
this.unsubscribe = null;
160-
}
161145
const previousPhase = this.snapshot.state.phase;
162146
let elapsedSeconds = this.snapshot.elapsedSeconds;
163147
if (state.phase !== previousPhase) {

0 commit comments

Comments
 (0)