Open History to see the last commit
1import { useAtomRefresh, useAtomValue } from "@effect/atom-react";
2import { useNavigation } from "@react-navigation/native";
3import {
4 cancelEnvironmentSpeechModelDownload,
5 downloadEnvironmentSpeechModel,
6 selectEnvironmentSpeechModel,
7} from "@t3tools/client-runtime/voice-input";
8import {
9 AuthSettingsWriteScope,
10 type EnvironmentId,
11 type EnvironmentSpeechModel,
12} from "@t3tools/contracts";
13import { AsyncResult } from "effect/reactivity";
14import { type ReactNode, useEffect, useState } from "react";
15import { Platform, Text as NativeText, View } from "react-native";
16
17import { SymbolView } from "../../components/ui/AppSymbol";
18import { AppText as Text } from "../../components/ui/AppText";
19import * as Dialog from "../../components/ui/Dialog";
20import { ListActionRow } from "../../components/ui/ListActionRow";
21import { ListChoiceRow } from "../../components/ui/ListChoiceRow";
22import { ListRow } from "../../components/ui/ListRow";
23import { Progress } from "../../components/ui/Progress";
24import { formatBytes } from "../../lib/formatBytes";
25import { useEnvironmentScope } from "../../state/session";
26import {
27 runSpeech,
28 voiceConnectionAtom,
29 voiceSpeechModelsAtom,
30 voiceSpeechStatusAtom,
31} from "../voice-input/voiceDestination";
32
33const isDownloading = (model: EnvironmentSpeechModel) =>
34 model.state === "downloading" || model.state === "verifying";
35
36export function useVoiceModels(environmentId: EnvironmentId, label: string) {
37 const connection = useAtomValue(voiceConnectionAtom(environmentId));
38 const statusAtom = voiceSpeechStatusAtom(environmentId);
39 const modelsAtom = voiceSpeechModelsAtom(environmentId);
40 const status = AsyncResult.getOrElse(useAtomValue(statusAtom), () => null);
41 const modelsResult = useAtomValue(modelsAtom);
42 const refreshStatus = useAtomRefresh(statusAtom);
43 const refreshModels = useAtomRefresh(modelsAtom);
44 const canManage = useEnvironmentScope(environmentId, AuthSettingsWriteScope);
45 const [requested, setRequested] = useState<string | null>(null);
46 const [busy, setBusy] = useState(false);
47
48 const models = AsyncResult.getOrElse(modelsResult, () => null)?.models ?? [];
49 const downloading = requested !== null || models.some(isDownloading);
50
51 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- polls the environment's download progress while one runs
52 useEffect(() => {
53 if (!downloading) return;
54 const timer = setInterval(refreshModels, 1_000);
55 return () => clearInterval(timer);
56 }, [downloading, refreshModels]);
57
58 const run = async (title: string, work: () => Promise<unknown>, downloadId?: string) => {
59 if (downloadId) setRequested(downloadId);
60 else setBusy(true);
61 try {
62 await work();
63 } catch (error) {
64 Dialog.alert(title, error instanceof Error ? error.message : String(error));
65 }
66 if (downloadId) setRequested(null);
67 else setBusy(false);
68 refreshModels();
69 refreshStatus();
70 };
71 const ready = connection !== null && status?.supported === true;
72 return {
73 connection,
74 status,
75 models,
76 loading: modelsResult._tag === "Initial",
77 canManage,
78 requested,
79 busy,
80 downloading,
81 label,
82 download: (model: EnvironmentSpeechModel) => {
83 if (!ready) return;
84 Dialog.confirm({
85 title: `Download ${model.name}?`,
86 message: `${label} will download ${formatBytes(model.size)} and use it as its transcription model.`,
87 confirmText: "Download",
88 onConfirm: () =>
89 void run(
90 "Could not download the model",
91 async () => {
92 await runSpeech(downloadEnvironmentSpeechModel(connection, model.id));
93 if (model.id !== status.modelId)
94 await runSpeech(selectEnvironmentSpeechModel(connection, model.id));
95 },
96 model.id,
97 ),
98 });
99 },
100 select: (model: EnvironmentSpeechModel) => {
101 if (!ready || model.active) return;
102 void run("Could not select the model", () =>
103 runSpeech(selectEnvironmentSpeechModel(connection, model.id)),
104 );
105 },
106 cancel: (model: EnvironmentSpeechModel) => {
107 if (!ready) return;
108 void run("Could not cancel the download", () =>
109 runSpeech(cancelEnvironmentSpeechModelDownload(connection, model.id)),
110 );
111 },
112 };
113}
114
115export type VoiceModels = ReturnType<typeof useVoiceModels>;
116
117const languageNames =
118 typeof Intl.DisplayNames === "function"
119 ? new Intl.DisplayNames(undefined, { type: "language" })
120 : null;
121const languageLabel = (code: string) => languageNames?.of(code) ?? code.toUpperCase();
122
123function ModelStats(props: { readonly model: EnvironmentSpeechModel; readonly language: string }) {
124 const { model, language } = props;
125 const data = model.benchmark;
126 const code = !data
127 ? undefined
128 : data.accuracy[language]
129 ? language
130 : (model.languages.find((candidate) => data.accuracy[candidate]) ?? model.languages[0]);
131 const score = code ? data?.accuracy[code] : undefined;
132 const parts: ReadonlyArray<readonly [string, ReactNode]> = [
133 ...(code && score
134 ? ([
135 [
136 "score",
137 <>
138 {languageLabel(code)}{" "}
139 <Text className="font-t3-medium text-foreground">{score.grade}</Text> (
140 {score.errorPercent}% error)
141 </>,
142 ],
143 ] as const)
144 : []),
145 ...(data && data.secondsPer30s.accelerated !== null
146 ? ([
147 [
148 "speed",
149 <>
150 <Text className="font-t3-medium text-foreground">
151 {data.secondsPer30s.accelerated} s
152 </Text>{" "}
153 / 30 s
154 </>,
155 ],
156 ] as const)
157 : []),
158 // A single-language model's score already names its language.
159 ...(model.languages.length > 1
160 ? ([
161 [
162 "languages",
163 <>
164 <SymbolView
165 name="globe"
166 size={13}
167 tintColorClassName="accent-icon-muted"
168 type="monochrome"
169 weight="regular"
170 />{" "}
171 {model.languages.length}
172 </>,
173 ],
174 ] as const)
175 : score
176 ? []
177 : ([["languages", languageLabel(model.languages[0] ?? "")]] as const)),
178 ["size", formatBytes(model.size)],
179 ];
180 return (
181 <Text textRole="footnote" className="text-foreground-muted tabular-nums">
182 {parts.map(([key, part], index) => (
183 // Plain Text inherits the muted footnote style; AppText would reset it.
184 <NativeText key={key}>
185 {index > 0 ? " · " : null}
186 {part}
187 </NativeText>
188 ))}
189 </Text>
190 );
191}
192
193function DownloadedGlyph() {
194 return (
195 <View className="size-6">
196 <SymbolView
197 name="tray.and.arrow.down"
198 size={Platform.OS === "android" ? 24 : 22}
199 tintColorClassName="accent-icon"
200 type="monochrome"
201 weight="regular"
202 />
203 <View className="absolute -right-1.5 -bottom-1 size-3.5 items-center justify-center rounded-full bg-primary ring-2 ring-grouped-card">
204 <SymbolView
205 name="checkmark"
206 size={9}
207 tintColorClassName="accent-primary-foreground"
208 type="monochrome"
209 weight="bold"
210 />
211 </View>
212 </View>
213 );
214}
215
216export function VoiceModelRow(props: {
217 readonly model: EnvironmentSpeechModel;
218 readonly voice: VoiceModels;
219 readonly separated: boolean;
220 /** The language whose benchmark score to show, when the model has one. */
221 readonly language: string;
222}) {
223 const { model, voice, separated } = props;
224 const meta = <ModelStats model={model} language={props.language} />;
225 if (model.state === "installed")
226 return (
227 <ListChoiceRow
228 leading={<DownloadedGlyph />}
229 label={model.name}
230 description={model.description}
231 meta={meta}
232 selected={model.active}
233 separated={separated}
234 disabled={!voice.canManage || voice.busy || voice.downloading}
235 onPress={() => voice.select(model)}
236 />
237 );
238 const progress = model.downloaded === undefined ? undefined : model.downloaded / model.size;
239 if (isDownloading(model) || voice.requested === model.id)
240 return (
241 <View className={separated ? "border-t border-border-subtle" : ""}>
242 <ListActionRow
243 icon="xmark"
244 label={model.name}
245 description={
246 model.state === "verifying"
247 ? "Verifying download…"
248 : `${Math.round((progress ?? 0) * 100)}% downloaded · Tap to cancel`
249 }
250 disabled={!voice.canManage || voice.busy}
251 onPress={() => voice.cancel(model)}
252 />
253 <Progress
254 className="bg-grouped-card px-4 pb-3"
255 value={model.state === "verifying" ? undefined : progress}
256 label={`${model.name} download progress`}
257 />
258 </View>
259 );
260 return (
261 <View className={separated ? "border-t border-border-subtle" : ""}>
262 <ListActionRow
263 icon="tray.and.arrow.down"
264 label={model.name}
265 description={model.description}
266 meta={meta}
267 disabled={!voice.canManage || voice.busy || voice.downloading}
268 onPress={() => voice.download(model)}
269 />
270 </View>
271 );
272}
273
274/** The environment's local-model method: chosen by its mark, opens the model list. */
275export function LocalModelChoice(props: {
276 readonly environmentId: EnvironmentId;
277 readonly label: string;
278 readonly selected: boolean;
279 readonly onSelect: () => void;
280}) {
281 const navigation = useNavigation();
282 const voice = useVoiceModels(props.environmentId, props.label);
283 const { status } = voice;
284 const open = () =>
285 navigation.navigate("SettingsVoiceModels", { environmentId: props.environmentId });
286 if (!voice.connection || !status || !status.supported)
287 return (
288 <ListChoiceRow
289 label="Local model"
290 description={
291 !voice.connection
292 ? "Not available on this environment"
293 : status?.supported === false
294 ? status.reason
295 : "Checking…"
296 }
297 selected={props.selected}
298 disabled
299 onPress={() => {}}
300 />
301 );
302
303 const active = status.state === "missing-model" ? null : status;
304 const pending = voice.models.find(isDownloading);
305 const progress =
306 pending?.downloaded === undefined ? undefined : pending.downloaded / pending.size;
307 return (
308 <ListRow
309 label={active?.model ?? "Local model"}
310 description={
311 pending
312 ? pending.state === "verifying"
313 ? `Verifying ${pending.name}…`
314 : `Downloading ${pending.name} · ${Math.round((progress ?? 0) * 100)}%`
315 : active
316 ? `Local model · ${formatBytes(active.size)}`
317 : voice.canManage
318 ? "No model installed · Choose one to download"
319 : `No model installed · Add one using T3 Code on ${props.label}`
320 }
321 selected={props.selected}
322 onPress={() => {
323 if (!props.selected) props.onSelect();
324 // Switching to an installed model needs no further step.
325 if (props.selected || !active) open();
326 }}
327 />
328 );
329}