The best way to control Claude Code, Codex and more from any computer or phone

main
ts 163 lines 5.05 KB
Raw
1 import * as Schema from "effect/Schema";
2 import { requireOptionalNativeModule } from "expo";
3 import { msg } from "gt-react-native";
4 import { Settings } from "react-native";
5 import { m } from "../i18n/messages";
6
7 import {
8 VoiceTranscriptionError,
9 throwIfVoiceTranscriptionAborted,
10 type PreparedVoiceTranscription,
11 type VoiceTranscriber,
12 type VoiceTranscriptionOptions,
13 } from "@t3tools/client-runtime/voice-input";
14
15 interface AppleSpeechModule {
16 isAppleSpeechAvailable?: () => boolean;
17 prepareAppleSpeech: (locale: string) => Promise<string | null>;
18 transcribeWithAppleSpeech: (uri: string, locale: string) => Promise<string>;
19 }
20
21 const native = requireOptionalNativeModule<AppleSpeechModule>("T3VoiceTranscription");
22 const isVoiceTranscriptionError = Schema.is(VoiceTranscriptionError);
23
24 /** The user's ordered Language & Region list, then the app locale; the app ships in English only. */
25 function getPreferredLocales(): string[] {
26 const languages: unknown = Settings.get("AppleLanguages");
27 const preferred = Array.isArray(languages)
28 ? languages.filter(
29 (language): language is string => typeof language === "string" && language.length > 0,
30 )
31 : [];
32 return [...new Set([...preferred, Intl.DateTimeFormat().resolvedOptions().locale])];
33 }
34
35 function wrapError(
36 code: "preparation-failed" | "transcription-failed",
37 message: string,
38 cause: unknown,
39 ): VoiceTranscriptionError {
40 if (isVoiceTranscriptionError(cause)) {
41 return cause;
42 }
43
44 return new VoiceTranscriptionError({ code, message, cause });
45 }
46
47 export async function getLocalVoiceTranscriber(): Promise<VoiceTranscriber | null> {
48 const locales = getPreferredLocales();
49 // Availability is a device and OS check; which languages have a model only `prepare` knows.
50 if (!native?.isAppleSpeechAvailable?.()) return null;
51 return { prepare: (options) => prepareVoiceTranscription(native, locales, options) };
52 }
53
54 async function prepareVoiceTranscription(
55 native: AppleSpeechModule,
56 locales: readonly string[],
57 { signal }: VoiceTranscriptionOptions,
58 ): Promise<PreparedVoiceTranscription> {
59 throwIfVoiceTranscriptionAborted(signal);
60 if (!native?.isAppleSpeechAvailable?.()) {
61 throw new VoiceTranscriptionError({
62 code: "unavailable",
63 message: m(
64 msg("Voice transcription requires a supported device with {platform} or later.", {
65 platform: "iOS 26",
66 }),
67 ),
68 });
69 }
70
71 let locale = locales[0]!;
72 const model = prepareLocales(native, locales, signal).then((supported) => {
73 locale = supported;
74 });
75 // Recorder setup can fail before either consumer observes the model.
76 void model.catch(() => {});
77
78 async function ready(signal: AbortSignal) {
79 throwIfVoiceTranscriptionAborted(signal);
80 const cancelled = Promise.withResolvers<void>();
81 const cancel = () => cancelled.resolve();
82 signal.addEventListener("abort", cancel, { once: true });
83 try {
84 await Promise.race([model, cancelled.promise]);
85 throwIfVoiceTranscriptionAborted(signal);
86 return locale;
87 } finally {
88 signal.removeEventListener("abort", cancel);
89 }
90 }
91 return {
92 get locale() {
93 return locale;
94 },
95 start: async (_uri, { signal }) => {
96 await ready(signal);
97 return null;
98 },
99 transcribe: async (uri, options) =>
100 transcribeVoiceRecording(native, uri, await ready(options.signal), options),
101 };
102 }
103
104 async function prepareLocales(
105 native: AppleSpeechModule,
106 locales: readonly string[],
107 signal: AbortSignal,
108 ): Promise<string> {
109 throwIfVoiceTranscriptionAborted(signal);
110 let unsupported: VoiceTranscriptionError | undefined;
111 for (const locale of locales) {
112 try {
113 return await prepareLocale(native, locale, signal);
114 } catch (error) {
115 if (!isVoiceTranscriptionError(error) || error.code !== "unsupported-locale") throw error;
116 unsupported = error;
117 }
118 }
119 throw unsupported!;
120 }
121
122 async function prepareLocale(
123 native: AppleSpeechModule,
124 locale: string,
125 signal: AbortSignal,
126 ): Promise<string> {
127 let supportedLocale: string | null;
128 try {
129 supportedLocale = await native.prepareAppleSpeech(locale);
130 } catch (error) {
131 throwIfVoiceTranscriptionAborted(signal);
132 throw wrapError(
133 "preparation-failed",
134 m(msg("Voice transcription could not prepare this language.")),
135 error,
136 );
137 }
138 throwIfVoiceTranscriptionAborted(signal);
139 if (supportedLocale === null) {
140 throw new VoiceTranscriptionError({
141 code: "unsupported-locale",
142 message: m(msg("Voice transcription does not support any of your languages.")),
143 });
144 }
145 return supportedLocale;
146 }
147
148 async function transcribeVoiceRecording(
149 native: AppleSpeechModule,
150 uri: string,
151 locale: string,
152 { signal }: VoiceTranscriptionOptions,
153 ): Promise<string> {
154 try {
155 throwIfVoiceTranscriptionAborted(signal);
156 const text = await native.transcribeWithAppleSpeech(uri, locale);
157 throwIfVoiceTranscriptionAborted(signal);
158 return text;
159 } catch (error) {
160 throwIfVoiceTranscriptionAborted(signal);
161 throw wrapError("transcription-failed", m(msg("Voice transcription failed.")), error);
162 }
163 }