The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { requireOptionalNativeModule } from "expo"; |
| 2 | import type { RecorderState, RecordingStatus } from "expo-audio"; |
| 3 | import { msg } from "gt-react-native"; |
| 4 | import { Platform } from "react-native"; |
| 5 | import type { NativeVoiceRecorder } from "../features/voice-input/lazyVoiceRecorder"; |
| 6 | import { m } from "../i18n/messages"; |
| 7 | |
| 8 | import { |
| 9 | VoiceTranscriptionError, |
| 10 | throwIfVoiceTranscriptionAborted, |
| 11 | type VoiceTranscriber, |
| 12 | } from "@t3tools/client-runtime/voice-input"; |
| 13 | |
| 14 | type PreparedGoogleTranscription = { readonly locale: string; readonly mode: number }; |
| 15 | |
| 16 | type GoogleTranscriptionModule = { |
| 17 | checkAvailability(): Promise<boolean>; |
| 18 | prepare(id: number): Promise<PreparedGoogleTranscription>; |
| 19 | transcribe(id: number, uri: string, locale: string, mode: number): Promise<string>; |
| 20 | cancel(id: number): Promise<void>; |
| 21 | readonly LiveVoiceRecorder?: new () => NativeVoiceRecorder<RecordingStatus, RecorderState>; |
| 22 | startLive?(id: number, uri: string, locale: string, mode: number): Promise<string>; |
| 23 | }; |
| 24 | |
| 25 | const native = requireOptionalNativeModule<GoogleTranscriptionModule>("T3VoiceTranscription"); |
| 26 | |
| 27 | const nativeErrorMessages: Readonly<Record<string, string>> = { |
| 28 | unavailable: msg("On-device voice transcription is not available for this device language."), |
| 29 | storage: msg("Not enough storage for on-device dictation. Free device storage and retry."), |
| 30 | quota: msg("On-device speech quota reached. Let the device rest or charge it, then retry."), |
| 31 | "no-speech": msg("No speech was detected. Speak again and retry dictation."), |
| 32 | busy: msg("On-device speech is busy. Wait for it to finish, then retry dictation."), |
| 33 | "needs-update": msg( |
| 34 | "Update {platform} and its on-device speech services, then retry dictation.", |
| 35 | { platform: "Android" }, |
| 36 | ), |
| 37 | "download-failed": msg("Speech model download failed. Check your network connection and retry."), |
| 38 | "audio-overflow": msg( |
| 39 | "The speech engine could not process the complete recording. No text was added. Retry dictation.", |
| 40 | ), |
| 41 | "incomplete-audio": msg( |
| 42 | "Speech recognition could not process the complete recording. No text was added. Retry dictation.", |
| 43 | ), |
| 44 | }; |
| 45 | |
| 46 | let nextOperationId = 0; |
| 47 | |
| 48 | export function createLiveVoiceRecorder() { |
| 49 | return Number(Platform.Version) >= 31 && native?.LiveVoiceRecorder |
| 50 | ? new native.LiveVoiceRecorder() |
| 51 | : null; |
| 52 | } |
| 53 | |
| 54 | export async function getLocalVoiceTranscriber(): Promise<VoiceTranscriber | null> { |
| 55 | if (!native || !(await native.checkAvailability())) return null; |
| 56 | const module = native; |
| 57 | return { |
| 58 | prepare: async ({ signal }) => { |
| 59 | const prepared = await runOperation(module, signal, "preparation-failed", (id) => |
| 60 | module.prepare(id), |
| 61 | ); |
| 62 | const startLive = module.startLive?.bind(module); |
| 63 | return { |
| 64 | locale: prepared.locale, |
| 65 | ...(module.LiveVoiceRecorder && startLive |
| 66 | ? { |
| 67 | start: (uri: string, { signal }: { signal: AbortSignal }) => |
| 68 | runOperation(module, signal, "transcription-failed", (id) => |
| 69 | startLive(id, uri, prepared.locale, prepared.mode), |
| 70 | ), |
| 71 | } |
| 72 | : {}), |
| 73 | transcribe: (uri, { signal }) => |
| 74 | runOperation(module, signal, "transcription-failed", (id) => |
| 75 | module.transcribe(id, uri, prepared.locale, prepared.mode), |
| 76 | ), |
| 77 | }; |
| 78 | }, |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | async function runOperation<T>( |
| 83 | module: GoogleTranscriptionModule, |
| 84 | signal: AbortSignal, |
| 85 | fallbackCode: "preparation-failed" | "transcription-failed", |
| 86 | operation: (id: number) => Promise<T>, |
| 87 | ): Promise<T> { |
| 88 | throwIfVoiceTranscriptionAborted(signal); |
| 89 | const id = ++nextOperationId; |
| 90 | let cancellation: Promise<void> | undefined; |
| 91 | const abort = () => { |
| 92 | cancellation = module.cancel(id).catch(() => {}); |
| 93 | }; |
| 94 | signal.addEventListener("abort", abort, { once: true }); |
| 95 | try { |
| 96 | const result = await operation(id); |
| 97 | throwIfVoiceTranscriptionAborted(signal); |
| 98 | return result; |
| 99 | } catch (error) { |
| 100 | throwIfVoiceTranscriptionAborted(signal); |
| 101 | const code = |
| 102 | typeof error === "object" && error !== null && "code" in error ? error.code : undefined; |
| 103 | throw new VoiceTranscriptionError({ |
| 104 | code: code === "unavailable" || code === "unsupported-locale" ? code : fallbackCode, |
| 105 | message: |
| 106 | m(typeof code === "string" ? nativeErrorMessages[code] : undefined) ?? |
| 107 | (fallbackCode === "preparation-failed" |
| 108 | ? m( |
| 109 | msg( |
| 110 | "Voice transcription could not prepare this language. Check {service} and try again.", |
| 111 | { service: "Google speech services" }, |
| 112 | ), |
| 113 | ) |
| 114 | : m(msg("Voice transcription failed."))), |
| 115 | cause: error, |
| 116 | }); |
| 117 | } finally { |
| 118 | signal.removeEventListener("abort", abort); |
| 119 | await cancellation; |
| 120 | } |
| 121 | } |