Skip to content

Commit 1be31e9

Browse files
committed
fix(voice): preserve fresh recording retries on mobile
1 parent debaf2a commit 1be31e9

5 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎apps/mobile/src/native/voiceTranscription.android.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ it.each([
4141
["preparation", "quota", /quota|limit/i],
4242
["transcription", "storage", /disk|storage|space/i],
4343
["transcription", "quota", /quota|limit/i],
44+
["transcription", "no-speech", /speak again/i],
45+
["transcription", "audio-overflow", /complete/i],
46+
["transcription", "incomplete-audio", /complete|interrupt/i],
4447
] as const)("preserves an actionable %s error: %s", async (phase, code, expected) => {
4548
vi.resetAllMocks();
4649
resetVoiceInputGlobalsForTests();
@@ -58,11 +61,12 @@ it.each([
5861
selection: { start: 16, end: 16 },
5962
revision: 1,
6063
};
64+
const record = vi.fn();
6165
const controller = new VoiceInputController({
6266
recorder: {
6367
uri: "file:///acceptance.m4a",
6468
prepareToRecordAsync: async () => {},
65-
record: () => {},
69+
record,
6670
stop: async () => {},
6771
},
6872
getTranscriber: () => transcriber,
@@ -81,4 +85,11 @@ it.each([
8185
expect(commit).not.toHaveBeenCalled();
8286
expect(draft.text).toBe("Preserved draft.");
8387
expect(controller.currentState.error).toMatch(expected);
88+
89+
mocks.prepare.mockResolvedValue({ locale: "en-US", mode: 0 });
90+
mocks.transcribe.mockResolvedValue("Complete transcript.");
91+
await controller.start();
92+
expect(controller.currentState.phase).toBe("recording");
93+
expect(record).toHaveBeenCalledTimes(phase === "transcription" ? 2 : 1);
94+
await controller.cancel();
8495
});

‎apps/web/src/components/settings/TranscriptionTest.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function TranscriptionTest({
8080
},
8181
});
8282
const instance = new VoiceInputController<true>({
83+
retryFailedRecording: true,
8384
recorder: createTestRecorder(platform.recorder),
8485
getTranscriber: () => platform.transcriber,
8586
requestPermission: async () => ({ granted: true, canAskAgain: true }),

‎apps/web/src/speech/useEnvironmentSpeechInput.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function createSpeechController(
6363
getPostProcess: () => VoiceInputControllerDependencies<true>["postProcess"],
6464
) {
6565
return new VoiceInputController<true>({
66+
retryFailedRecording: true,
6667
...dependencies,
6768
// Read the preferences captured when recording starts, rather than at controller creation.
6869
get postProcess() {

‎packages/client-runtime/src/voice-input/controller.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe("streaming voice input", () => {
9595
.mockRejectedValueOnce(new Error("inference failed"))
9696
.mockResolvedValueOnce("recovered text");
9797
const harness = createHarness({
98+
retryFailedRecording: true,
9899
getTranscriber: () => ({ prepare: async () => preparedTranscription(transcribe) }),
99100
});
100101
await harness.controller.start();
@@ -114,6 +115,7 @@ describe("streaming voice input", () => {
114115

115116
it("deletes retained failed audio when the user cancels", async () => {
116117
const harness = createHarness({
118+
retryFailedRecording: true,
117119
getTranscriber: () => ({
118120
prepare: async () =>
119121
preparedTranscription(async () => {

‎packages/client-runtime/src/voice-input/controller.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export type VoiceInputControllerDependencies<WithPostProcessing extends boolean
7676
readonly configureRecording: () => Promise<void>;
7777
readonly releaseRecording: () => Promise<void>;
7878
readonly deleteRecording: (uri: string) => void;
79+
readonly retryFailedRecording?: boolean;
7980
readonly postProcess?: WithPostProcessing extends true
8081
?
8182
| ((
@@ -224,7 +225,7 @@ function preparationErrorMessage(error: unknown): string {
224225
if (errorCode(error) === "unsupported-locale") {
225226
return "Voice transcription is not available for this language.";
226227
}
227-
return error instanceof VoiceTranscriptionError
228+
return error instanceof VoiceTranscriptionError && error.message.trim()
228229
? error.message
229230
: "Could not prepare voice transcription.";
230231
}
@@ -508,7 +509,7 @@ export class VoiceInputController<WithPostProcessing extends boolean = false> {
508509
);
509510
} catch (error) {
510511
if (this.isCurrent(operationToken)) {
511-
if (!transcription.streaming && recordingUri) {
512+
if (this.dependencies.retryFailedRecording && !transcription.streaming && recordingUri) {
512513
this.retryRecording = { uri: recordingUri, draft: capturedDraft, transcription };
513514
}
514515
this.setError(transcriptionErrorMessage(error), "retry");

0 commit comments

Comments
 (0)