diff --git a/packages/remote-protocol/src/voice.ts b/packages/remote-protocol/src/voice.ts index 0f27091d1..eb1f10e96 100644 --- a/packages/remote-protocol/src/voice.ts +++ b/packages/remote-protocol/src/voice.ts @@ -36,7 +36,10 @@ * it lets the device send the session's recording once to POST * /voice/transcriptions for a second pass over the whole file (a more * accurate text and speakers). Only the recording's seconds beyond the - * live session's count against the quota again. + * live session's count against the quota again. An `error` may carry one + * too, when the session had audio worth hearing again; a device that gets + * none from it (or from a server that never sends one) has no second pass + * for that session. * * The second pass (`purpose=note` sessions only), signed in as /voice is: * - POST /voice/transcriptions?v=1&lang=, header @@ -67,7 +70,7 @@ export const VoiceLanguageSchema = z.string().regex(/^([a-z]{2,3}(-[A-Z]{2})?|mu export const VoicePurposeSchema = z.enum(["dictation", "note"]); export type VoicePurpose = z.infer; -/** How long a `done`'s transcribeToken stays good for. */ +/** How long a `done`'s (or `error`'s) transcribeToken stays good for. */ export const TRANSCRIBE_TOKEN_MS = 24 * 60 * 60 * 1000; /** Most bytes of a recording POST /voice/transcriptions takes (about 8 hours of 24 kbps Opus). */ @@ -126,6 +129,12 @@ export const VoiceServerMessageSchema = z.discriminatedUnion("t", [ */ code: z.enum(["bad-frame", "provider-error", "unauthorized"]), message: z.string(), + /** + * A note's session that had audio worth hearing again: as `done`'s, lets + * its recording go once to POST /voice/transcriptions. Missing from a + * server that does not send one. + */ + transcribeToken: z.string().min(1).optional(), }), ]); export type VoiceServerMessage = z.infer; diff --git a/src/renderer/services/notes/recorder.ts b/src/renderer/services/notes/recorder.ts index 62101baf9..12d13427c 100644 --- a/src/renderer/services/notes/recorder.ts +++ b/src/renderer/services/notes/recorder.ts @@ -230,6 +230,11 @@ function onEvent({ id, event }: VoiceEvent): void { endTake(event.reason === "session" ? "chain" : "stop"); return; case "error": + // An error may still hand over the token: the recording can be heard again. + if (event.transcribeToken) { + const token = event.transcribeToken; + setTake((tk) => ({ ...tk, token })); + } if (event.code === "unauthorized") tellFailure("signed-out"); else showToast({ kind: "error", message: t("failure.recognition") }); return; diff --git a/src/shared/notes/transcript.ts b/src/shared/notes/transcript.ts index 4b0bd244a..2f7a89e30 100644 --- a/src/shared/notes/transcript.ts +++ b/src/shared/notes/transcript.ts @@ -47,7 +47,7 @@ export const TranscriptTakeSchema = z.object({ offsetMs: z.number().nonnegative(), /** Its length, as the server counted it once it was done. */ seconds: z.number().nonnegative(), - /** The session's `done.transcribeToken`, until it is used. */ + /** The session's `done` (or `error`) `transcribeToken`, until it is used. */ token: z.string().optional(), /** The second pass on the server, while it is under way. */ transcriptionId: z.string().optional(), diff --git a/tests/unit/main/features/remote/voice.test.ts b/tests/unit/main/features/remote/voice.test.ts index ffedc88e6..102f94d92 100644 --- a/tests/unit/main/features/remote/voice.test.ts +++ b/tests/unit/main/features/remote/voice.test.ts @@ -236,9 +236,11 @@ describe("a note's recording", () => { expect(voice.has(result.id)).toBe(true); expect(voice.has("another")).toBe(false); s.say({ t: "final", text: "안녕하세요", speaker: "2", startMs: 100, endMs: 900 }); + s.say({ t: "error", code: "provider-error", message: "x", transcribeToken: "tok" }); s.say({ t: "done", seconds: 1, transcribeToken: "tok" }); expect(events.map((e) => e.event)).toEqual([ { t: "final", text: "안녕하세요", speaker: "2", startMs: 100, endMs: 900 }, + { t: "error", code: "provider-error", message: "x", transcribeToken: "tok" }, { t: "done", seconds: 1, transcribeToken: "tok" }, ]); }); diff --git a/tests/unit/packages/remote-protocol/protocol.test.ts b/tests/unit/packages/remote-protocol/protocol.test.ts index dfbb3c0f9..95cdbc9d5 100644 --- a/tests/unit/packages/remote-protocol/protocol.test.ts +++ b/tests/unit/packages/remote-protocol/protocol.test.ts @@ -575,6 +575,8 @@ describe("the voice socket", () => { { t: "partial", text: "네, 512", speaker: "2" }, { t: "final", text: "네, 512 KiB면 괜찮아요.", speaker: "2", startMs: 4210, endMs: 6930 }, { t: "done", seconds: 1820.4, transcribeToken: "tt_1" }, + { t: "error", code: "provider-error", message: "x", transcribeToken: "tt_2" }, + { t: "error", code: "provider-error", message: "x" }, ]) { expect(VoiceServerMessageSchema.parse(message)).toEqual(message as never); }