Skip to content
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
13 changes: 11 additions & 2 deletions packages/remote-protocol/src/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=<language>, header
Expand Down Expand Up @@ -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<typeof VoicePurposeSchema>;

/** 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). */
Expand Down Expand Up @@ -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<typeof VoiceServerMessageSchema>;
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/services/notes/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/notes/transcript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/main/features/remote/voice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
]);
});
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/packages/remote-protocol/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down