| 1 | /** Cancellation is cooperative: settle only after the underlying work has stopped. */ |
| 2 | export type VoiceTranscriptionOptions = { |
| 3 | readonly signal: AbortSignal; |
| 4 | }; |
| 5 | |
| 6 | /** Binds a recording to its selected implementation and resolved locale. */ |
| 7 | export type PreparedVoiceTranscription = { |
| 8 | readonly locale: string; |
| 9 | readonly transcribe: (uri: string, options: VoiceTranscriptionOptions) => Promise<string>; |
| 10 | }; |
| 11 | |
| 12 | export type VoiceTranscriber = { |
| 13 | readonly prepare: (options: VoiceTranscriptionOptions) => Promise<PreparedVoiceTranscription>; |
| 14 | }; |
| 15 | |
| 16 | export type VoiceTranscriptionErrorCode = |
| 17 | | "unavailable" |
| 18 | | "unsupported-locale" |
| 19 | | "preparation-failed" |
| 20 | | "transcription-failed" |
| 21 | | "cancelled"; |
| 22 | |
| 23 | export class VoiceTranscriptionError extends Error { |
| 24 | readonly code: VoiceTranscriptionErrorCode; |
| 25 | |
| 26 | constructor(code: VoiceTranscriptionErrorCode, message: string, options?: ErrorOptions) { |
| 27 | super(message, options); |
| 28 | this.name = "VoiceTranscriptionError"; |
| 29 | this.code = code; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | export function throwIfVoiceTranscriptionAborted(signal: AbortSignal): void { |
| 34 | if (signal.aborted) { |
| 35 | throw new VoiceTranscriptionError("cancelled", "Voice transcription was cancelled."); |
| 36 | } |
| 37 | } |