Open History to see the last commit
1import { msg } from "gt-i18n";
2import { MarkedText, translatableMessage } from "../errors/translatable.ts";
3import * as Schema from "effect/Schema";
4
5/** Cancellation is cooperative: settle only after the underlying work has stopped. */
6export type VoiceTranscriptionOptions = {
7 readonly signal: AbortSignal;
8};
9
10export type VoiceStreamingSession = {
11 readonly finish: (options: VoiceTranscriptionOptions) => Promise<string>;
12};
13
14export type PreparedVoiceTranscription = {
15 readonly locale: string;
16 /**
17 * Runs alongside capture once recording starts. Resolves with the transcript when it
18 * transcribed live, or null when `transcribe` still runs after the recording stops.
19 * A rejection stops the recording.
20 */
21 readonly start?: (uri: string, options: VoiceTranscriptionOptions) => Promise<string | null>;
22 readonly transcribe: (uri: string, options: VoiceTranscriptionOptions) => Promise<string>;
23 /** Present when audio streams during capture; `finish` then replaces `transcribe`. */
24 readonly streaming?: VoiceStreamingSession | undefined;
25};
26
27export type VoiceTranscriber = {
28 readonly prepare: (options: VoiceTranscriptionOptions) => Promise<PreparedVoiceTranscription>;
29};
30
31const VoiceTranscriptionErrorCode = Schema.Literals([
32 "unavailable",
33 "unsupported-locale",
34 "preparation-failed",
35 "transcription-failed",
36 "cancelled",
37]);
38export type VoiceTranscriptionErrorCode = typeof VoiceTranscriptionErrorCode.Type;
39
40export class VoiceTranscriptionError extends Schema.TaggedError<VoiceTranscriptionError>()(
41 "VoiceTranscriptionError",
42 {
43 code: VoiceTranscriptionErrorCode,
44 message: Schema.String,
45 i18nMessage: Schema.optionalKey(MarkedText),
46 cause: Schema.optional(Schema.Defect()),
47 },
48) {}
49
50export function throwIfVoiceTranscriptionAborted(signal: AbortSignal): void {
51 if (signal.aborted) {
52 throw new VoiceTranscriptionError({
53 code: "cancelled",
54 ...translatableMessage(msg("Voice transcription was cancelled.")),
55 });
56 }
57}