The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { msg } from "gt-i18n"; |
| 2 | import { MarkedText, translatableMessage } from "../errors/translatable.ts"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | /** Cancellation is cooperative: settle only after the underlying work has stopped. */ |
| 6 | export type VoiceTranscriptionOptions = { |
| 7 | readonly signal: AbortSignal; |
| 8 | }; |
| 9 | |
| 10 | export type VoiceStreamingSession = { |
| 11 | readonly finish: (options: VoiceTranscriptionOptions) => Promise<string>; |
| 12 | }; |
| 13 | |
| 14 | export 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 | |
| 27 | export type VoiceTranscriber = { |
| 28 | readonly prepare: (options: VoiceTranscriptionOptions) => Promise<PreparedVoiceTranscription>; |
| 29 | }; |
| 30 | |
| 31 | const VoiceTranscriptionErrorCode = Schema.Literals([ |
| 32 | "unavailable", |
| 33 | "unsupported-locale", |
| 34 | "preparation-failed", |
| 35 | "transcription-failed", |
| 36 | "cancelled", |
| 37 | ]); |
| 38 | export type VoiceTranscriptionErrorCode = typeof VoiceTranscriptionErrorCode.Type; |
| 39 | |
| 40 | export 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 | |
| 50 | export 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 | } |