The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { msg } from "gt-react-native"; |
| 2 | |
| 3 | import { m } from "../../i18n/messages"; |
| 4 | import { |
| 5 | throwIfVoiceTranscriptionAborted, |
| 6 | type VoiceRecorder, |
| 7 | } from "@t3tools/client-runtime/voice-input"; |
| 8 | |
| 9 | type Subscription = { remove(): void }; |
| 10 | type RecordingDestination = "local" | "environment"; |
| 11 | |
| 12 | export interface VoiceRecordingInput { |
| 13 | readonly uid: string; |
| 14 | readonly name: string; |
| 15 | readonly type: string; |
| 16 | } |
| 17 | |
| 18 | export interface NativeVoiceRecorder<TStatus, TRecorderState> { |
| 19 | readonly uri: string | null; |
| 20 | prepareToRecordAsync(): Promise<void>; |
| 21 | record(options: { readonly forDuration: number }): void; |
| 22 | stop(): Promise<void>; |
| 23 | getStatus(): TRecorderState; |
| 24 | addListener( |
| 25 | eventName: "recordingStatusUpdate", |
| 26 | listener: (status: TStatus) => void, |
| 27 | ): Subscription; |
| 28 | getAvailableInputs(): ReadonlyArray<VoiceRecordingInput>; |
| 29 | setInput(uid: string): void | Promise<void>; |
| 30 | cancelInputSelection?(): void; |
| 31 | release(): void; |
| 32 | } |
| 33 | |
| 34 | export interface LazyVoiceRecorder<TRecorderState> extends VoiceRecorder { |
| 35 | getStatus(): TRecorderState | null; |
| 36 | release(): void; |
| 37 | } |
| 38 | |
| 39 | /** Construct lazily: cold startup blocks JS for tens of milliseconds and most composers never record. */ |
| 40 | export function createLazyVoiceRecorder<TStatus, TRecorderState>(input: { |
| 41 | readonly getDestination: () => RecordingDestination; |
| 42 | readonly create: ( |
| 43 | destination: RecordingDestination, |
| 44 | ) => NativeVoiceRecorder<TStatus, TRecorderState>; |
| 45 | readonly onStatus: (status: TStatus) => void; |
| 46 | readonly selectInput?: (inputs: ReadonlyArray<VoiceRecordingInput>) => VoiceRecordingInput | null; |
| 47 | }): LazyVoiceRecorder<TRecorderState> { |
| 48 | let recorder: NativeVoiceRecorder<TStatus, TRecorderState> | null = null; |
| 49 | let destination: RecordingDestination | null = null; |
| 50 | let subscription: Subscription | null = null; |
| 51 | let retired: NativeVoiceRecorder<TStatus, TRecorderState> | null = null; |
| 52 | let retiredUri: string | null = null; |
| 53 | let pending: Promise<unknown> = Promise.resolve(); |
| 54 | let abandonInputSelection: (() => void) | null = null; |
| 55 | |
| 56 | const track = <T>(promise: Promise<T>): Promise<T> => { |
| 57 | pending = Promise.allSettled([pending, promise]).then(() => undefined); |
| 58 | return promise; |
| 59 | }; |
| 60 | |
| 61 | const release = () => { |
| 62 | const released = recorder; |
| 63 | if (!released) return; |
| 64 | abandonInputSelection?.(); |
| 65 | recorder = null; |
| 66 | subscription?.remove(); |
| 67 | subscription = null; |
| 68 | retired = released; |
| 69 | void pending.then(() => { |
| 70 | if (retired === released) { |
| 71 | retiredUri = released.uri; |
| 72 | retired = null; |
| 73 | } |
| 74 | released.release(); |
| 75 | }); |
| 76 | }; |
| 77 | |
| 78 | return { |
| 79 | get uri() { |
| 80 | if (recorder) return recorder.uri; |
| 81 | return retired ? retired.uri : retiredUri; |
| 82 | }, |
| 83 | prepareToRecordAsync(signal) { |
| 84 | const next = input.getDestination(); |
| 85 | if (next !== destination) release(); |
| 86 | if (!recorder) { |
| 87 | recorder = input.create(next); |
| 88 | destination = next; |
| 89 | subscription = recorder.addListener("recordingStatusUpdate", input.onStatus); |
| 90 | } |
| 91 | const prepared = recorder; |
| 92 | return track( |
| 93 | prepared.prepareToRecordAsync().then(async () => { |
| 94 | if (signal) throwIfVoiceTranscriptionAborted(signal); |
| 95 | if (!input.selectInput) return; |
| 96 | let selected: VoiceRecordingInput | null; |
| 97 | try { |
| 98 | selected = input.selectInput(prepared.getAvailableInputs()); |
| 99 | } catch { |
| 100 | return; |
| 101 | } |
| 102 | if (!selected) return; |
| 103 | const uid = selected.uid; |
| 104 | // Expo never settles a canceled coroutine function's promise, so stop waiting on abort. |
| 105 | const abandoned = Promise.withResolvers<void>(); |
| 106 | const abandon = () => { |
| 107 | prepared.cancelInputSelection?.(); |
| 108 | abandoned.resolve(); |
| 109 | }; |
| 110 | signal?.addEventListener("abort", abandon, { once: true }); |
| 111 | abandonInputSelection = abandon; |
| 112 | try { |
| 113 | await Promise.race([ |
| 114 | new Promise<void>((resolve) => resolve(prepared.setInput(uid))).catch(() => {}), |
| 115 | abandoned.promise, |
| 116 | ]); |
| 117 | if (signal) throwIfVoiceTranscriptionAborted(signal); |
| 118 | } finally { |
| 119 | signal?.removeEventListener("abort", abandon); |
| 120 | if (abandonInputSelection === abandon) abandonInputSelection = null; |
| 121 | } |
| 122 | }), |
| 123 | ); |
| 124 | }, |
| 125 | record(options) { |
| 126 | if (!recorder) throw new Error(m(msg("The voice recorder isn’t ready. Try again."))); |
| 127 | recorder.record(options); |
| 128 | }, |
| 129 | stop() { |
| 130 | return recorder ? track(recorder.stop()) : Promise.resolve(); |
| 131 | }, |
| 132 | getStatus() { |
| 133 | return recorder ? recorder.getStatus() : null; |
| 134 | }, |
| 135 | release, |
| 136 | }; |
| 137 | } |