@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
tsx 1,906 lines 72.5 KB
Raw
1 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2 import { m } from "~/i18n/messages";
3 import {
4 cancelEnvironmentSpeechModelDownload,
5 downloadEnvironmentSpeechModel,
6 getEnvironmentSpeechModels,
7 getEnvironmentSpeechStatus,
8 linkEnvironmentSpeechModels,
9 removeEnvironmentSpeechModel,
10 selectEnvironmentSpeechModel,
11 updateEnvironmentSpeechAcceleration,
12 updateEnvironmentSpeechModelUnloadTimeout,
13 } from "@t3tools/client-runtime/voice-input";
14 import type { PreparedConnection } from "@t3tools/client-runtime/connection";
15 import { openRouterIcon } from "@t3tools/client-runtime/provider-clients";
16 import {
17 AuthFilesystemReadScope,
18 AuthSettingsWriteScope,
19 CLOUD_SPEECH_LABELS,
20 CloudSpeechProvider,
21 DEFAULT_SPEECH_POST_PROCESSING_PROMPT,
22 type EnvironmentId,
23 type EnvironmentSpeechModel,
24 type EnvironmentSpeechStatus,
25 type ProjectId,
26 ProviderDriverKind,
27 type SpeechAcceleration,
28 type SpeechBenchmarkTier,
29 type SpeechCustomWords,
30 type SpeechModelUnloadTimeout,
31 type VoiceShortcutMode,
32 } from "@t3tools/contracts";
33 import { createModelSelection, pickModelSelection } from "@t3tools/shared/model";
34 import { resolveSpeechPostProcessingModelSelection } from "@t3tools/shared/serverSettings";
35 import {
36 cloudSpeechLanguages,
37 recommendSpeechModels,
38 type SpeechRecommendationRole,
39 } from "@t3tools/shared/speech";
40 import {
41 ArrowDownUpIcon,
42 AudioLinesIcon,
43 CheckIcon,
44 DownloadIcon,
45 LanguagesIcon,
46 KeyRoundIcon,
47 LaptopIcon,
48 MonitorIcon,
49 MicOffIcon,
50 MicIcon,
51 PlusIcon,
52 RefreshCwIcon,
53 SearchIcon,
54 StarIcon,
55 Trash2Icon,
56 UnlinkIcon,
57 XIcon,
58 } from "lucide-react";
59 import { useEffect, useRef, useState, type ReactNode } from "react";
60 import { msg, T, useGT, useLocale, useMessages, Var } from "gt-react";
61
62 import { useStableCallback } from "~/hooks/useStableCallback";
63 import { supportsAppleSpeechLocale, useAppleSpeechLocales } from "../../speech/appleSpeech";
64 import { cn } from "~/lib/utils";
65 import {
66 listAudioInputs,
67 updateMicrophoneOrder,
68 type BrowserMicrophone,
69 } from "../../speech/browserMicrophones";
70 import {
71 microphoneOrder,
72 preferredMicrophoneId,
73 rememberMicrophones,
74 } from "../../speech/microphoneOrder";
75 import { preferredSpeechLanguages } from "../../speech/preferredSpeechLanguages";
76 import {
77 chooseAudioDestination,
78 chooseTranscriptCleanup,
79 useCleanupGrant,
80 } from "../../speech/voiceConsent";
81 import { updateClientSettings, useClientSettings } from "../../hooks/useSettings";
82 import { runEnvironmentSpeech } from "../../speech/runEnvironmentSpeech";
83 import {
84 getTextGenerationModelOptionsByInstance,
85 resolveAppModelSelectionState,
86 } from "../../modelSelection";
87 import { useTextGenerationTargets } from "../../textGenerators";
88 import { useEnvironmentProviders, useEnvironments } from "../../state/environments";
89 import { useEnvironmentScope } from "../../state/session";
90 import { EMPTY_SERVER_PROVIDERS } from "../../state/server";
91 import {
92 useEnvironmentSpeechInput,
93 APPLE_SPEECH_LABEL,
94 useVoiceTranscriptionDestination,
95 } from "../../speech/useEnvironmentSpeechInput";
96 import { ComposerSpeechRecordingPill } from "../chat/ComposerSpeechButton";
97 import { ProviderModelPicker } from "../chat/ProviderModelPicker";
98 import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon";
99 import { ProviderPackageIcon } from "../chat/ProviderPackageIcon";
100 import { TraitsPicker } from "../chat/TraitsPicker";
101 import { VoiceInputSetup } from "../chat/VoiceInputSetup";
102 import { Badge } from "../ui/badge";
103 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
104 import { Button } from "../ui/button";
105 import { Input } from "../ui/input";
106 import { InputGroup, InputGroupAddon, InputGroupInput } from "../ui/input-group";
107 import {
108 Menu,
109 MenuCheckboxItem,
110 MenuGroup,
111 MenuGroupLabel,
112 MenuPopup,
113 MenuRadioGroup,
114 MenuRadioItem,
115 MenuRadioItemIndicator,
116 MenuSearchInput,
117 MenuSeparator,
118 MenuTrigger,
119 } from "../ui/menu";
120 import {
121 Select,
122 SelectGroup,
123 SelectGroupLabel,
124 SelectItem,
125 SelectPopup,
126 SelectTrigger,
127 SelectValue,
128 } from "../ui/select";
129 import { Switch } from "../ui/switch";
130 import { Textarea } from "../ui/textarea";
131 import { toastManager } from "../ui/toast";
132 import { MicrophoneOrderList } from "./MicrophoneOrderList";
133 import { MicrophoneTest } from "./MicrophoneTest";
134 import { CloudVoiceConfiguration } from "./CloudVoiceConfiguration";
135 import { ModelBenchmarks, orderSpeechLanguages } from "./SpeechModelComparison";
136 import { languageLabel, SpeechLanguageSelect } from "./SpeechLanguageSelect";
137 import { filterSpeechModels, type SpeechModelSort } from "./speechModelBrowser";
138 import { AdvancedSettings } from "./AdvancedSettings";
139 import {
140 SETTINGS_PICKER_TRIGGER_CLASSNAME,
141 SettingsPageContainer,
142 SettingsRow,
143 SettingsSearchTarget,
144 SettingsSection,
145 } from "./settingsLayout";
146 import { useSearchableSetting } from "./settingsSearch";
147 import { useOptionalSettingsScope, useSettingsScope } from "./SettingsScopeContext";
148 import { useScopedModelDisabledReason } from "./useScopedModelAvailability";
149 import { useScopedSettings, useUpdateScopedSettings } from "./useScopedSettings";
150
151 const OFF = "off";
152 const DEVICE = "device";
153 function CloudSpeechLogo({ provider }: { provider: CloudSpeechProvider }) {
154 if (provider === "openrouter")
155 return <ProviderPackageIcon icon={openRouterIcon} aria-hidden className="size-4 shrink-0" />;
156 return (
157 <ProviderInstanceIcon
158 driverKind={ProviderDriverKind.make(provider === "openai" ? "codex" : "grok")}
159 displayName={CLOUD_SPEECH_LABELS[provider]}
160 iconClassName="size-4"
161 />
162 );
163 }
164 const formatSize = (bytes: number) => `${Math.round(bytes / 1024 / 1024)} MB`;
165 const SHORTCUT_MODES: Record<VoiceShortcutMode, { label: string; hint: string }> = {
166 auto: {
167 label: msg("Auto", { $context: "dictation shortcut mode" }),
168 hint: msg("Tap to toggle, hold to talk"),
169 },
170 hold: {
171 label: msg("Hold", { $context: "dictation shortcut mode: record while the key is held" }),
172 hint: msg("Record while held"),
173 },
174 toggle: {
175 label: msg("Toggle", { $context: "dictation shortcut mode: tap to start and stop" }),
176 hint: msg("Tap to start, tap to stop"),
177 },
178 };
179 const UNLOAD_LABELS: Record<SpeechModelUnloadTimeout, string> = {
180 never: msg("Never", { $context: "unload the speech model" }),
181 immediately: msg("Immediately"),
182 min_2: msg("After 2 minutes"),
183 min_5: msg("After 5 minutes"),
184 min_10: msg("After 10 minutes"),
185 min_15: msg("After 15 minutes"),
186 hour_1: msg("After 1 hour"),
187 };
188 const normalizeTerm = (value: string) => value.replace(/\s+/g, " ").trim().normalize("NFC");
189 const spellingsOf = (words: SpeechCustomWords) =>
190 new Set(
191 words.flatMap(({ term, aliases }) =>
192 [term, ...aliases].map((spelling) => spelling.toLocaleLowerCase()),
193 ),
194 );
195
196 type ModelCardProps = {
197 readonly model: EnvironmentSpeechModel;
198 readonly comparisonLanguages: readonly string[];
199 readonly recommendation: SpeechRecommendationRole | undefined;
200 readonly benchmarkTier: SpeechBenchmarkTier | undefined;
201 readonly busy: boolean;
202 readonly canCancel: boolean;
203 readonly onDownload: () => void;
204 readonly onSelect: () => void;
205 readonly onCancel: () => void;
206 readonly onDelete: () => void;
207 readonly testControls?: ReactNode;
208 /** Keeps the controls under the text, so a live test never squeezes the model's details. */
209 readonly stacked?: boolean;
210 readonly children?: ReactNode;
211 };
212
213 function ModelBadge(props: { icon: ReactNode; label: string; tooltip: string }) {
214 return (
215 <Tooltip>
216 <TooltipTrigger render={<Badge variant="outline" />}>
217 {props.icon}
218 {props.label}
219 </TooltipTrigger>
220 <TooltipPopup side="top">{props.tooltip}</TooltipPopup>
221 </Tooltip>
222 );
223 }
224
225 function ModelCard(props: ModelCardProps) {
226 const gt = useGT();
227 const locale = useLocale();
228 const { model } = props;
229 const downloading = model.state === "downloading" || model.state === "verifying";
230 const progress = model.downloaded === undefined ? 0 : (model.downloaded / model.size) * 100;
231 return (
232 <div className="@container/model px-3 py-3">
233 <div
234 className={cn(
235 "flex flex-col gap-2.5",
236 !props.stacked &&
237 "@min-[28rem]/model:flex-row @min-[28rem]/model:items-start @min-[28rem]/model:gap-4",
238 )}
239 >
240 <div className="min-w-0 flex-1">
241 <div className="flex flex-wrap items-center gap-1.5">
242 <span className="text-sm font-medium leading-5">{model.name}</span>
243 {props.recommendation ? (
244 <ModelBadge
245 icon={<StarIcon className="mr-1 size-3" />}
246 label={
247 props.recommendation === "recommended"
248 ? gt("Recommended")
249 : props.recommendation === "faster"
250 ? gt("Faster", { $context: "speech model badge, quicker than Recommended" })
251 : gt("More accurate")
252 }
253 tooltip={
254 props.recommendation === "recommended"
255 ? gt(
256 "Balanced accuracy and speed for your selected languages on Handy’s reference benchmarks.",
257 )
258 : props.recommendation === "faster"
259 ? gt("Quicker than Recommended on this reference tier, with useful accuracy.")
260 : gt(
261 "Higher accuracy with a longer reference processing budget for your selected languages.",
262 )
263 }
264 />
265 ) : null}
266 {model.supportsStreaming ? (
267 <ModelBadge
268 icon={<AudioLinesIcon className="mr-1 size-3" />}
269 label={gt("Live preview")}
270 tooltip={gt(
271 "Shows words in your draft as you speak, before the recording finishes.",
272 )}
273 />
274 ) : null}
275 {model.supportsTranslation ? (
276 <ModelBadge
277 icon={<LanguagesIcon className="mr-1 size-3" />}
278 label={gt("Translate")}
279 tooltip={gt(
280 "Can write English text from speech in another language. Turn on Translate to English to use it.",
281 )}
282 />
283 ) : null}
284 </div>
285 {model.description ? (
286 <p className="mt-0.5 text-xs leading-normal text-muted-foreground">
287 {model.description}
288 </p>
289 ) : null}
290 {model.linkedFrom ? (
291 <p className="mt-0.5 break-all text-2xs text-muted-foreground">
292 <T>
293 Using your file at <Var>{model.linkedFrom}</Var>
294 </T>
295 </p>
296 ) : null}
297 <ModelBenchmarks
298 model={model}
299 languages={props.comparisonLanguages}
300 tier={props.benchmarkTier}
301 facts={[
302 ...(model.languages.length > 1
303 ? [
304 gt("{count, plural, one {{count} language} other {{count} languages}}", {
305 count: model.languages.length,
306 }),
307 ]
308 : model.benchmark
309 ? []
310 : [languageLabel(model.languages[0] ?? "", locale)]),
311 formatSize(model.size),
312 ]}
313 />
314 {downloading ? (
315 <div className="mt-2">
316 <div
317 role="progressbar"
318 aria-label={gt("{name} download progress", { name: model.name })}
319 aria-valuemin={0}
320 aria-valuemax={100}
321 aria-valuenow={Math.round(progress)}
322 className="h-1 overflow-hidden rounded-full bg-muted"
323 >
324 <div
325 className="h-full bg-primary transition-[width] duration-200"
326 style={{ width: `${progress}%` }}
327 />
328 </div>
329 <p className="mt-1 text-2xs text-muted-foreground">
330 {model.state === "verifying"
331 ? gt("Verifying download…")
332 : gt("{percent}% downloaded", { percent: Math.round(progress) })}
333 </p>
334 </div>
335 ) : null}
336 </div>
337 <div className="flex max-w-full shrink-0 flex-wrap items-center gap-1.5">
338 {props.testControls}
339 {downloading ? (
340 <Button
341 size="icon-sm"
342 variant="outline"
343 aria-label={gt("Cancel {name} download", { name: model.name })}
344 disabled={!props.canCancel}
345 onClick={props.onCancel}
346 >
347 <XIcon className="size-3.5" />
348 </Button>
349 ) : model.state === "downloadable" ? (
350 <Button size="sm" variant="outline" disabled={props.busy} onClick={props.onDownload}>
351 <DownloadIcon />
352 <T>Download</T>
353 </Button>
354 ) : model.active ? (
355 <Button size="sm" variant="secondary" disabled>
356 <CheckIcon />
357 <T>Selected</T>
358 </Button>
359 ) : (
360 <Button size="sm" variant="outline" disabled={props.busy} onClick={props.onSelect}>
361 <T>Use model</T>
362 </Button>
363 )}
364 {model.state === "installed" ? (
365 <Button
366 size="icon-sm"
367 variant="outline"
368 disabled={props.busy}
369 aria-label={
370 model.linkedFrom
371 ? gt("Stop using your {name} file; the file stays on disk", { name: model.name })
372 : gt("Delete {name}", { name: model.name })
373 }
374 onClick={props.onDelete}
375 >
376 {model.linkedFrom ? <UnlinkIcon /> : <Trash2Icon />}
377 </Button>
378 ) : null}
379 </div>
380 </div>
381 {props.children}
382 </div>
383 );
384 }
385
386 function DictionaryEditor(props: {
387 readonly label: string;
388 readonly words: SpeechCustomWords;
389 readonly disabled?: boolean;
390 readonly onChange: (words: SpeechCustomWords) => void;
391 }) {
392 const gt = useGT();
393 const { words, label, disabled = false, onChange } = props;
394 const [termDraft, setTermDraft] = useState("");
395 const [aliasDraft, setAliasDraft] = useState<{ term: string; value: string } | null>(null);
396 const spellings = spellingsOf(words);
397 const seen = new Set(spellings);
398 const newTerms = termDraft
399 .split(/[,\n]/)
400 .map(normalizeTerm)
401 .filter((term) => {
402 const key = term.toLocaleLowerCase();
403 if (!key || seen.has(key)) return false;
404 seen.add(key);
405 return true;
406 });
407 const canAddTerm =
408 !disabled &&
409 newTerms.length > 0 &&
410 newTerms.every((term) => term.length <= 50) &&
411 words.length + newTerms.length <= 100;
412 const addTerm = () => {
413 if (!canAddTerm) return;
414 onChange([...words, ...newTerms.map((term) => ({ term, aliases: [] }))]);
415 setTermDraft("");
416 };
417 const addAlias = () => {
418 if (!aliasDraft) return;
419 const value = normalizeTerm(aliasDraft.value);
420 if (!value || value.length > 50 || spellings.has(value.toLocaleLowerCase())) return;
421 onChange(
422 words.map((entry) =>
423 entry.term === aliasDraft.term && entry.aliases.length < 8
424 ? { term: entry.term, aliases: [...entry.aliases, value] }
425 : entry,
426 ),
427 );
428 setAliasDraft(null);
429 };
430 return (
431 <>
432 <div className="flex items-start gap-2">
433 {/* One line while empty, so a wrapped placeholder never makes the field taller than Add. */}
434 <div className="min-w-0 flex-1 [&_textarea]:placeholder-shown:whitespace-nowrap">
435 <Textarea
436 value={termDraft}
437 rows={1}
438 maxLength={5_100}
439 disabled={disabled}
440 placeholder={gt("Add words, separated by commas")}
441 aria-label={gt("Add a word or phrase to the {label}", { label })}
442 onChange={(event) => setTermDraft(event.target.value)}
443 onKeyDown={(event) => {
444 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
445 if (event.key !== "Enter" || event.shiftKey) return;
446 event.preventDefault();
447 addTerm();
448 }}
449 />
450 </div>
451 <Button type="button" variant="outline" disabled={!canAddTerm} onClick={addTerm}>
452 <PlusIcon />
453 <T>Add</T>
454 </Button>
455 </div>
456 {words.length > 0 ? (
457 <ul
458 aria-label={`${label} entries`}
459 className="mt-2.5 flex flex-col gap-1.5 @min-[32rem]/settings-row:flex-row @min-[32rem]/settings-row:flex-wrap"
460 >
461 {words.map(({ term, aliases }) => (
462 <li
463 key={term}
464 className="flex items-center gap-1 rounded-md border border-border/60 bg-muted/30 py-0.5 pl-2 pr-0.5 text-xs"
465 >
466 <span className="font-medium">{term}</span>
467 {aliases.length > 0 ? (
468 <span className="text-muted-foreground">← {aliases.join(", ")}</span>
469 ) : null}
470 {aliasDraft?.term === term ? (
471 <Input
472 autoFocus
473 value={aliasDraft.value}
474 maxLength={50}
475 aria-label={gt("Alias for {term}", { term })}
476 placeholder={gt("Misspelling")}
477 className="ms-auto w-32"
478 onChange={(event) => setAliasDraft({ term, value: event.target.value })}
479 onBlur={() => setAliasDraft(null)}
480 onKeyDown={(event) => {
481 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
482 if (event.key === "Escape") setAliasDraft(null);
483 if (event.key !== "Enter") return;
484 event.preventDefault();
485 addAlias();
486 }}
487 />
488 ) : (
489 <Button
490 type="button"
491 size="xs"
492 variant="ghost-muted"
493 className="ms-auto"
494 disabled={disabled || aliases.length >= 8}
495 aria-label={gt("Add an alias for {term}", { term })}
496 onClick={() => setAliasDraft({ term, value: "" })}
497 >
498 <T>Alias</T>
499 </Button>
500 )}
501 <Button
502 type="button"
503 size="icon-xs"
504 variant="ghost-muted"
505 disabled={disabled}
506 aria-label={gt("Remove {term}", { term })}
507 onClick={() => onChange(words.filter((entry) => entry.term !== term))}
508 >
509 <XIcon />
510 </Button>
511 </li>
512 ))}
513 </ul>
514 ) : null}
515 </>
516 );
517 }
518
519 /** Vocabulary stored on the environment with the project, read by everyone who dictates there. */
520 function ProjectDictionaryRow() {
521 const setting = useSearchableSetting();
522 const gt = useGT();
523 const scope = useOptionalSettingsScope();
524 const words = useScopedSettings((settings) => settings.speechProjectCustomWords);
525 const updateScopedSettings = useUpdateScopedSettings();
526 if (scope === null) return null;
527 const isProjectScope = scope.scope.kind === "project" || scope.scope.kind === "checkout";
528 return (
529 <SettingsRow
530 serverScoped
531 settingKeys={["speechProjectCustomWords"]}
532 {...setting("speech-project-dictionary")}
533 title={isProjectScope ? gt("Project dictionary") : gt("Shared dictionary")}
534 description={
535 isProjectScope
536 ? gt("Used by everyone who dictates in {label}.", { label: scope.scope.label })
537 : gt("Used by everyone who dictates on this environment.")
538 }
539 >
540 <div className="mt-3 pb-2">
541 <DictionaryEditor
542 label={isProjectScope ? gt("project dictionary") : gt("shared dictionary")}
543 words={words}
544 disabled={scope.connectedEnvironments.length === 0}
545 onChange={(next) => updateScopedSettings({ speechProjectCustomWords: next })}
546 />
547 </div>
548 </SettingsRow>
549 );
550 }
551
552 /** Cleanup runs on the draft's environment, so its model is an environment setting there. */
553 function CleanupModelRow() {
554 const setting = useSearchableSetting();
555 const gt = useGT();
556 const { environment, connectedEnvironments } = useSettingsScope();
557 const settings = useScopedSettings();
558 const updateSettings = useUpdateScopedSettings();
559 const providersByEnvironment = useEnvironmentProviders();
560 const providers =
561 (environment?.serverConfig
562 ? providersByEnvironment.get(environment.environmentId)
563 : undefined) ?? EMPTY_SERVER_PROVIDERS;
564 const targets = useTextGenerationTargets({
565 environmentId: environment?.environmentId ?? null,
566 settings,
567 providers: providers.filter((provider) => provider.supportsTextGeneration !== false),
568 projectId: null,
569 selections: [
570 settings.speechPostProcessingModelSelection,
571 settings.textGenerationModelSelection,
572 ],
573 });
574 const textGenerationProviders = targets.providers;
575 const instanceEntries = targets.entries;
576 const hasProvider = instanceEntries.some((entry) => entry.enabled && entry.isAvailable);
577 const dedicated = settings.speechPostProcessingModelSelection !== null;
578 const selection = resolveAppModelSelectionState(
579 {
580 ...settings,
581 textGenerationModelSelection: resolveSpeechPostProcessingModelSelection(
582 settings,
583 textGenerationProviders,
584 ),
585 },
586 textGenerationProviders,
587 );
588 const modelOptionsByInstance = getTextGenerationModelOptionsByInstance(
589 settings,
590 textGenerationProviders,
591 selection.instanceId,
592 selection.model,
593 );
594 const instanceEntry = instanceEntries.find((entry) => entry.instanceId === selection.instanceId);
595 const modelDisabledReason = useScopedModelDisabledReason(settings, instanceEntries, true);
596 const setModel = (next: ReturnType<typeof createModelSelection>) => {
597 const reason = modelDisabledReason(next.instanceId, next.model);
598 if (reason) {
599 toastManager.add({
600 type: "error",
601 title: gt("Cleanup model not saved"),
602 description: reason,
603 });
604 return;
605 }
606 updateSettings({ speechPostProcessingModelSelection: next });
607 };
608 return (
609 <SettingsRow
610 serverScoped
611 settingKeys={["speechPostProcessingModelSelection"]}
612 {...setting("speech-post-processing-model")}
613 title={gt("Cleanup model")}
614 description={gt(
615 "Model for transcript cleanup, such as a cheaper or faster one. When disabled, use the text generation model configured for this environment.",
616 )}
617 control={
618 connectedEnvironments.length === 0 ? (
619 <span className="text-sm text-muted-foreground">
620 <T>Connect an environment first.</T>
621 </span>
622 ) : (
623 <div className="flex min-w-0 flex-wrap items-center gap-2 @min-[32rem]/settings-row:justify-end">
624 {dedicated && (!hasProvider || !instanceEntry) ? (
625 <span className="text-sm text-muted-foreground">
626 <T>No text generation providers available.</T>
627 </span>
628 ) : null}
629 {dedicated && hasProvider && instanceEntry ? (
630 <>
631 <ProviderModelPicker
632 environmentId={environment?.environmentId ?? null}
633 activeInstanceId={selection.instanceId}
634 model={selection.model}
635 lockedProvider={null}
636 instanceEntries={instanceEntries}
637 modelOptionsByInstance={modelOptionsByInstance}
638 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
639 getModelDisabledReason={modelDisabledReason}
640 onInstanceModelChange={(instanceId, model) =>
641 setModel(pickModelSelection(selection, instanceId, model))
642 }
643 />
644 <TraitsPicker
645 provider={instanceEntry.driverKind}
646 models={instanceEntry.models}
647 model={selection.model}
648 modelOptions={selection.options ?? []}
649 allowPromptInjectedEffort={false}
650 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
651 onModelOptionsChange={(options) =>
652 setModel(createModelSelection(selection.instanceId, selection.model, options))
653 }
654 />
655 </>
656 ) : null}
657 <Switch
658 checked={dedicated}
659 disabled={!dedicated && !hasProvider}
660 onCheckedChange={(checked) =>
661 updateSettings({
662 speechPostProcessingModelSelection: checked
663 ? createModelSelection(
664 settings.textGenerationModelSelection.instanceId,
665 settings.textGenerationModelSelection.model,
666 settings.textGenerationModelSelection.options,
667 )
668 : null,
669 })
670 }
671 aria-label={gt("Use a separate cleanup model")}
672 />
673 </div>
674 )
675 }
676 />
677 );
678 }
679
680 function useVoiceModels(prepared: PreparedConnection | null) {
681 const gt = useGT();
682 const canManage = useEnvironmentScope(prepared?.environmentId ?? null, AuthSettingsWriteScope);
683 const [models, setModels] = useState<readonly EnvironmentSpeechModel[]>([]);
684 const [status, setStatus] = useState<EnvironmentSpeechStatus | null>(null);
685 const [busy, setBusy] = useState(false);
686 const [requestedDownload, setRequestedDownload] = useState<string | null>(null);
687 const [actionConnection, setActionConnection] = useState(prepared);
688 if (actionConnection !== prepared) {
689 setActionConnection(prepared);
690 setBusy(false);
691 setRequestedDownload(null);
692 setModels([]);
693 setStatus(null);
694 }
695 const epoch = useRef(0);
696 const refreshRevision = useRef(0);
697 const refresh = useStableCallback(
698 async (connection: PreparedConnection, mode: "foreground" | "progress") => {
699 const current = epoch.current;
700 const revision = ++refreshRevision.current;
701 try {
702 const [nextStatus, nextModels] = await Promise.all([
703 runEnvironmentSpeech(getEnvironmentSpeechStatus(connection)),
704 runEnvironmentSpeech(getEnvironmentSpeechModels(connection, mode === "foreground")),
705 ]);
706 if (current !== epoch.current || revision !== refreshRevision.current) return;
707 setStatus(nextStatus);
708 setModels((previous) => {
709 if (mode === "foreground") return nextModels.models;
710 const benchmarks = new Map(previous.map((model) => [model.id, model.benchmark]));
711 return nextModels.models.map((model) => {
712 const benchmark = benchmarks.get(model.id);
713 return benchmark ? { ...model, benchmark } : model;
714 });
715 });
716 } catch (error) {
717 if (mode === "progress") return;
718 if (current !== epoch.current || revision !== refreshRevision.current) return;
719 toastManager.add({
720 type: "error",
721 title: gt("Could not load transcription models"),
722 description:
723 error instanceof Error
724 ? (thrownErrorMessage(error, m) ?? error.message)
725 : String(error),
726 });
727 }
728 },
729 );
730 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- loads the environment's model catalog when its connection becomes available
731 useEffect(() => {
732 epoch.current += 1;
733 if (prepared) void refresh(prepared, "foreground");
734 return () => {
735 epoch.current += 1;
736 };
737 }, [prepared, refresh]);
738 const downloading =
739 requestedDownload !== null ||
740 models.some((model) => model.state === "downloading" || model.state === "verifying");
741 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- polls progress while a local request or the environment reports an active download
742 useEffect(() => {
743 if (!prepared || !downloading) return;
744 let cancelled = false;
745 let timer: number | undefined;
746 const poll = async () => {
747 await refresh(prepared, "progress");
748 if (!cancelled) timer = window.setTimeout(() => void poll(), 1_000);
749 };
750 void poll();
751 return () => {
752 cancelled = true;
753 window.clearTimeout(timer);
754 };
755 }, [downloading, prepared, refresh]);
756
757 const run = async (label: string, work: () => Promise<unknown>, downloadId?: string) => {
758 if (!prepared || !canManage) return;
759 const current = epoch.current;
760 if (downloadId) setRequestedDownload(downloadId);
761 else setBusy(true);
762 try {
763 await work();
764 } catch (error) {
765 if (current === epoch.current)
766 toastManager.add({
767 type: "error",
768 title: label,
769 description:
770 error instanceof Error
771 ? (thrownErrorMessage(error, m) ?? error.message)
772 : String(error),
773 });
774 }
775 if (current === epoch.current) {
776 if (downloadId) setRequestedDownload(null);
777 else setBusy(false);
778 await refresh(prepared, "foreground");
779 }
780 };
781 return {
782 models,
783 status,
784 run,
785 controlsDisabled: busy || downloading || !canManage,
786 canCancel: canManage && !busy,
787 requestedDownload,
788 };
789 }
790
791 type VoiceModels = ReturnType<typeof useVoiceModels>;
792
793 function AdvancedSpeechSettings({
794 prepared,
795 catalog,
796 }: {
797 prepared: PreparedConnection;
798 catalog: VoiceModels;
799 }) {
800 const messages = useMessages();
801 const setting = useSearchableSetting();
802 const gt = useGT();
803 const settings = useClientSettings();
804 const { run, controlsDisabled, status } = catalog;
805 if (!status?.supported) return null;
806 const supported = status;
807 return (
808 <AdvancedSettings targetIds={[setting("speech-streaming").id, setting("speech-hardware").id]}>
809 <SettingsRow
810 {...setting("speech-streaming")}
811 title={gt("Streaming transcription")}
812 description={
813 supported.supportsStreaming
814 ? gt(
815 "Transcribe while recording. Turn off to transcribe after stopping, which may help when live transcription falls behind.",
816 )
817 : gt("This model doesn’t support native streaming.")
818 }
819 control={
820 <Switch
821 aria-label={gt("Streaming transcription")}
822 disabled={!supported.supportsStreaming}
823 checked={settings.speechStreaming}
824 onCheckedChange={(checked) => void updateClientSettings({ speechStreaming: checked })}
825 />
826 }
827 />
828 <SettingsRow
829 {...setting("speech-hardware")}
830 title={gt("Hardware")}
831 description={gt("Auto uses the environment’s GPU when it has one.")}
832 control={
833 <Select
834 value={supported.acceleration}
835 disabled={controlsDisabled}
836 onValueChange={(value) => {
837 if (value)
838 void run(gt("Could not change transcription hardware"), () =>
839 runEnvironmentSpeech(
840 updateEnvironmentSpeechAcceleration(prepared, value as SpeechAcceleration),
841 ),
842 );
843 }}
844 >
845 <SelectTrigger size="sm" aria-label={gt("Transcription hardware")} className="max-w-72">
846 <SelectValue>
847 {supported.acceleration === "auto"
848 ? gt("Auto")
849 : supported.acceleration === "cpu"
850 ? gt("CPU")
851 : (supported.gpuDevices.find(
852 (device) => `gpu:${device.id}` === supported.acceleration,
853 )?.name ?? gt("Selected GPU"))}
854 </SelectValue>
855 </SelectTrigger>
856 <SelectPopup align="end" alignItemWithTrigger={false}>
857 <SelectItem value="auto">
858 <T>Auto</T>
859 </SelectItem>
860 <SelectItem value="cpu">
861 <T>CPU</T>
862 </SelectItem>
863 {supported.gpuDevices.map((device) => (
864 <SelectItem key={device.id} value={`gpu:${device.id}`}>
865 {device.name}
866 </SelectItem>
867 ))}
868 </SelectPopup>
869 </Select>
870 }
871 />
872 <SettingsRow
873 title={gt("Unload model")}
874 description={gt("Release the model’s memory on the environment after it sits idle.")}
875 control={
876 <Select
877 value={supported.modelUnloadTimeout}
878 disabled={controlsDisabled}
879 onValueChange={(value) => {
880 if (value)
881 void run(gt("Could not change when the model unloads"), () =>
882 runEnvironmentSpeech(
883 updateEnvironmentSpeechModelUnloadTimeout(
884 prepared,
885 value as SpeechModelUnloadTimeout,
886 ),
887 ),
888 );
889 }}
890 >
891 <SelectTrigger size="sm" aria-label={gt("Unload model")} className="max-w-72">
892 <SelectValue>{messages(UNLOAD_LABELS[supported.modelUnloadTimeout])}</SelectValue>
893 </SelectTrigger>
894 <SelectPopup align="end" alignItemWithTrigger={false}>
895 {(Object.keys(UNLOAD_LABELS) as SpeechModelUnloadTimeout[]).map((timeout) => (
896 <SelectItem key={timeout} value={timeout}>
897 {messages(UNLOAD_LABELS[timeout])}
898 </SelectItem>
899 ))}
900 </SelectPopup>
901 </Select>
902 }
903 />
904 </AdvancedSettings>
905 );
906 }
907
908 function SpeechModelBrowser(props: {
909 prepared: PreparedConnection;
910 catalog: VoiceModels;
911 testEnvironmentId: EnvironmentId;
912 projectId: ProjectId | null;
913 projectName: string | null;
914 }) {
915 const setting = useSearchableSetting();
916 const gt = useGT();
917 const locale = useLocale();
918 const {
919 prepared,
920 catalog: { status, models, run, controlsDisabled, canCancel, requestedDownload },
921 } = props;
922 const [search, setSearch] = useState("");
923 const [sort, setSort] = useState<SpeechModelSort>("recommended");
924 const [downloadedOnly, setDownloadedOnly] = useState(false);
925 const [existingPath, setExistingPath] = useState("");
926 const canReadFiles = useEnvironmentScope(prepared.environmentId, AuthFilesystemReadScope);
927 const [languageSearch, setLanguageSearch] = useState("");
928 const [selection, setSelection] = useState<string[] | null>(null);
929 const [preferred] = useState(preferredSpeechLanguages);
930 const supportedLanguages = [...new Set(models.flatMap((model) => model.languages))];
931 const defaultLanguage = preferred.find((code) => supportedLanguages.includes(code));
932 const selectedLanguages = selection ?? (defaultLanguage ? [defaultLanguage] : []);
933 const languages = orderSpeechLanguages(supportedLanguages, preferred, locale);
934 const needle = languageSearch.trim().toLocaleLowerCase();
935 const visibleLanguages = languages.filter(
936 (code) =>
937 !needle ||
938 code.includes(needle) ||
939 languageLabel(code, locale).toLocaleLowerCase().includes(needle),
940 );
941 const accuracyLanguage = selectedLanguages[0] ?? "en";
942 const sortLabels: Record<SpeechModelSort, string> = {
943 recommended: gt("Recommended"),
944 size: gt("Smallest"),
945 accuracy: gt("Most accurate"),
946 speed: gt("Fastest"),
947 };
948 const languagesLabel =
949 selectedLanguages.length === 0
950 ? gt("All languages")
951 : selectedLanguages.map((code) => languageLabel(code, locale)).join(", ");
952 const benchmarkTier = status?.supported ? status.benchmarkTier : undefined;
953 const recommendations = recommendSpeechModels(models, selectedLanguages, benchmarkTier);
954 const sortedModels = filterSpeechModels(
955 models,
956 {
957 search,
958 languages: selectedLanguages,
959 downloadedOnly,
960 sort,
961 accuracyLanguage,
962 benchmarkTier,
963 },
964 recommendations,
965 );
966 const activeModel = models.find((model) => model.active);
967 const renderedModels =
968 activeModel && !sortedModels.includes(activeModel)
969 ? [activeModel, ...sortedModels]
970 : sortedModels;
971
972 if (!status) return null;
973 if (!status.supported)
974 return (
975 <SettingsRow
976 {...setting("speech-models")}
977 title={gt("Models")}
978 description={gt("This environment can’t transcribe audio: {reason}.", {
979 reason: status.reason,
980 })}
981 />
982 );
983 return (
984 <SettingsRow
985 {...setting("speech-models")}
986 title={gt("Models")}
987 description={gt(
988 "Grades and seconds per 30 s use Handy’s reference {tier}, not your hardware. Select languages for balanced recommendations.",
989 { tier: status.benchmarkTier === "cpu" ? "CPU" : "GPU" },
990 )}
991 className="@container/voice-models"
992 >
993 <div className="mt-3 flex min-w-0 gap-2">
994 <InputGroup className="min-w-0 flex-1 **:[input]:h-9 sm:**:[input]:h-8">
995 <InputGroupAddon>
996 <SearchIcon aria-hidden />
997 </InputGroupAddon>
998 <InputGroupInput
999 type="search"
1000 aria-label={gt("Search transcription models")}
1001 placeholder={gt("Search models")}
1002 value={search}
1003 onChange={(e) => setSearch(e.target.value)}
1004 />
1005 </InputGroup>
1006 <Menu>
1007 <MenuTrigger
1008 render={<Button variant="outline" className="max-w-56" />}
1009 aria-label={gt("Filter by language: {languagesLabel}", { languagesLabel })}
1010 >
1011 <LanguagesIcon />
1012 <span className="hidden truncate @min-[34rem]/voice-models:inline">
1013 {languagesLabel}
1014 </span>
1015 </MenuTrigger>
1016 <MenuPopup align="end">
1017 <MenuSearchInput
1018 autoFocus
1019 aria-label={gt("Search languages")}
1020 placeholder={gt("Search languages")}
1021 value={languageSearch}
1022 onChange={(e) => setLanguageSearch(e.target.value)}
1023 />
1024 <div
1025 role="group"
1026 aria-label={gt("Browse models by language")}
1027 className="max-h-72 overflow-y-auto"
1028 >
1029 {needle ? null : (
1030 <MenuCheckboxItem
1031 checked={selectedLanguages.length === 0}
1032 onCheckedChange={() => setSelection([])}
1033 >
1034 <T>All languages</T>
1035 </MenuCheckboxItem>
1036 )}
1037 {visibleLanguages.map((code) => (
1038 <MenuCheckboxItem
1039 key={code}
1040 checked={selectedLanguages.includes(code)}
1041 onCheckedChange={() =>
1042 setSelection(
1043 selectedLanguages.includes(code)
1044 ? selectedLanguages.filter((language) => language !== code)
1045 : [...selectedLanguages, code],
1046 )
1047 }
1048 >
1049 {languageLabel(code, locale)}
1050 </MenuCheckboxItem>
1051 ))}
1052 {visibleLanguages.length === 0 && (
1053 <p className="px-2 py-1.5 text-sm text-muted-foreground">
1054 <T>No languages match.</T>
1055 </p>
1056 )}
1057 </div>
1058 </MenuPopup>
1059 </Menu>
1060 <Menu>
1061 <MenuTrigger
1062 render={<Button variant="outline" />}
1063 aria-label={
1064 downloadedOnly
1065 ? gt("Sort and filter models: {sort}, downloaded only", { sort: sortLabels[sort] })
1066 : gt("Sort and filter models: {sort}", { sort: sortLabels[sort] })
1067 }
1068 >
1069 <ArrowDownUpIcon />
1070 <span className="hidden @min-[34rem]/voice-models:inline">{sortLabels[sort]}</span>
1071 </MenuTrigger>
1072 <MenuPopup align="end">
1073 <MenuGroup>
1074 <MenuGroupLabel>
1075 <T>Sort by</T>
1076 </MenuGroupLabel>
1077 <MenuRadioGroup
1078 value={sort}
1079 onValueChange={(value) => setSort(value as SpeechModelSort)}
1080 >
1081 {(Object.keys(sortLabels) as SpeechModelSort[]).map((value) => (
1082 <MenuRadioItem key={value} value={value} closeOnClick>
1083 <span className="flex items-center justify-between gap-3">
1084 {value === "accuracy"
1085 ? gt("Most accurate in {language}", {
1086 language: languageLabel(accuracyLanguage, locale),
1087 })
1088 : sortLabels[value]}
1089 <MenuRadioItemIndicator />
1090 </span>
1091 </MenuRadioItem>
1092 ))}
1093 </MenuRadioGroup>
1094 </MenuGroup>
1095 <MenuSeparator />
1096 <MenuCheckboxItem checked={downloadedOnly} onCheckedChange={setDownloadedOnly}>
1097 <T>Downloaded only</T>
1098 </MenuCheckboxItem>
1099 </MenuPopup>
1100 </Menu>
1101 </div>
1102 <div
1103 className="mt-2 mb-2 max-h-96 divide-y divide-border/60 overflow-y-auto rounded-lg border border-border/60"
1104 role="region"
1105 aria-label={gt("Transcription models")}
1106 tabIndex={0}
1107 >
1108 {sortedModels.length === 0 && (
1109 <p className="px-3 py-3 text-sm text-muted-foreground">
1110 <T>No models match.</T>
1111 </p>
1112 )}
1113 {renderedModels.map((model) => {
1114 const card: ModelCardProps = {
1115 model:
1116 model.id === requestedDownload && model.state === "downloadable"
1117 ? { ...model, state: "downloading" }
1118 : model,
1119 busy: controlsDisabled,
1120 canCancel,
1121 comparisonLanguages: selectedLanguages,
1122 recommendation: recommendations.get(model.id),
1123 benchmarkTier: status.benchmarkTier,
1124 onDownload: () =>
1125 void run(
1126 gt("Could not download the model"),
1127 () => runEnvironmentSpeech(downloadEnvironmentSpeechModel(prepared, model.id)),
1128 model.id,
1129 ),
1130 onSelect: () =>
1131 void run(gt("Could not select the model"), () =>
1132 runEnvironmentSpeech(selectEnvironmentSpeechModel(prepared, model.id)),
1133 ),
1134 onCancel: () =>
1135 void run(gt("Could not cancel the download"), () =>
1136 runEnvironmentSpeech(cancelEnvironmentSpeechModelDownload(prepared, model.id)),
1137 ),
1138 onDelete: () =>
1139 void run(gt("Could not delete the model"), () =>
1140 runEnvironmentSpeech(removeEnvironmentSpeechModel(prepared, model.id)),
1141 ),
1142 };
1143 return model.active ? (
1144 <TranscriptionTest
1145 key={model.id}
1146 card={card}
1147 hidden={!sortedModels.includes(model)}
1148 environmentId={props.testEnvironmentId}
1149 projectId={props.projectId}
1150 projectName={props.projectName}
1151 />
1152 ) : (
1153 <ModelCard key={model.id} {...card} />
1154 );
1155 })}
1156 </div>
1157 {status.supportsModelLinking && canReadFiles ? (
1158 <form
1159 className="mb-2 flex flex-col items-start gap-2 @min-[28rem]/voice-models:flex-row @min-[28rem]/voice-models:items-center"
1160 onSubmit={(event) => {
1161 event.preventDefault();
1162 void run(gt("Could not use a model from that path"), async () => {
1163 await runEnvironmentSpeech(linkEnvironmentSpeechModels(prepared, existingPath));
1164 setExistingPath("");
1165 });
1166 }}
1167 >
1168 <div className="w-full min-w-0 flex-1">
1169 <Input
1170 size="compact"
1171 aria-label={gt("Path to a model file or folder already on this environment")}
1172 placeholder={gt("Already downloaded elsewhere? Paste the model file or folder path")}
1173 value={existingPath}
1174 onChange={(e) => setExistingPath(e.target.value)}
1175 />
1176 </div>
1177 <Button
1178 type="submit"
1179 size="sm"
1180 variant="outline"
1181 disabled={controlsDisabled || !existingPath.trim()}
1182 >
1183 <T>Use existing</T>
1184 </Button>
1185 </form>
1186 ) : null}
1187 </SettingsRow>
1188 );
1189 }
1190
1191 function TranscriptionTest(props: {
1192 readonly card: ModelCardProps;
1193 readonly hidden: boolean;
1194 readonly environmentId: EnvironmentId;
1195 readonly projectId: ProjectId | null;
1196 readonly projectName: string | null;
1197 }) {
1198 const setting = useSearchableSetting();
1199 const gt = useGT();
1200 const [transcript, setTranscript] = useState<string | null>(null);
1201 const draft = transcript ?? "";
1202 const [showTranscript, setShowTranscript] = useState(false);
1203 const draftRef = useRef(draft);
1204 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes the committed draft to the asynchronous transcription controller
1205 useEffect(() => {
1206 draftRef.current = draft;
1207 }, [draft]);
1208 const speech = useEnvironmentSpeechInput({
1209 environmentId: props.environmentId,
1210 projectId: props.projectId,
1211 projectName: props.projectName,
1212 ownerKey: JSON.stringify([props.environmentId, props.projectId]),
1213 cleanup: false,
1214 readDraft: () => ({
1215 text: draftRef.current,
1216 selection: { start: draftRef.current.length, end: draftRef.current.length },
1217 }),
1218 commitDraft: (text) => setTranscript(text),
1219 });
1220 return (
1221 <SettingsSearchTarget {...setting("transcription-test")} hidden={props.hidden}>
1222 <ModelCard
1223 {...props.card}
1224 stacked={showTranscript}
1225 testControls={
1226 <div className="order-last flex min-w-0 max-w-full basis-full items-center gap-1 @min-[28rem]/model:order-none @min-[28rem]/model:basis-auto">
1227 <ComposerSpeechRecordingPill
1228 state={speech.state}
1229 progress={null}
1230 level={speech.level}
1231 onStart={() => void speech.start()}
1232 onStop={() => void speech.stop()}
1233 onCancel={() => speech.cancel()}
1234 onSkipPostProcessing={() => speech.skipPostProcessing()}
1235 />
1236 {speech.available ? (
1237 speech.state.phase === "idle" ? (
1238 <Button
1239 size="sm"
1240 variant="outline"
1241 onClick={() => {
1242 setShowTranscript(true);
1243 void speech.start();
1244 }}
1245 >
1246 <MicIcon />
1247 <T>Test model</T>
1248 </Button>
1249 ) : null
1250 ) : (
1251 <span className="text-xs text-muted-foreground">
1252 {speech.status?.supported === false
1253 ? speech.status.reason
1254 : gt("Waiting for the environment to connect")}
1255 </span>
1256 )}
1257 </div>
1258 }
1259 >
1260 {showTranscript ? (
1261 <div className="mt-3 flex items-start gap-3 rounded-lg border border-border/60 bg-muted/30 py-1 ps-3 pe-1">
1262 <div className="min-w-0 flex-1 py-1.5">
1263 {speech.preview || transcript !== null ? null : (
1264 <p className="text-xs text-muted-foreground">
1265 <T>
1266 Say a sentence. Cleanup is skipped, so you see exactly what the model heard.
1267 </T>
1268 </p>
1269 )}
1270 {speech.preview || transcript !== null ? (
1271 <p
1272 role="status"
1273 aria-label={gt("Test transcript")}
1274 className="select-text whitespace-pre-wrap break-words text-sm leading-relaxed"
1275 >
1276 {speech.preview ? (
1277 <>
1278 {speech.preview.committed}
1279 <span className="text-muted-foreground">{speech.preview.tentative}</span>
1280 </>
1281 ) : (
1282 transcript || gt("No speech recognized. Try again.")
1283 )}
1284 </p>
1285 ) : null}
1286 </div>
1287 <Button
1288 size="icon-sm"
1289 variant="ghost-muted"
1290 aria-label={gt("Close model test")}
1291 onClick={() => {
1292 speech.cancel();
1293 setShowTranscript(false);
1294 }}
1295 >
1296 <XIcon />
1297 </Button>
1298 </div>
1299 ) : null}
1300 </ModelCard>
1301 <VoiceInputSetup
1302 environmentId={speech.transcriptionEnvironmentId}
1303 open={speech.setup.open}
1304 step={speech.setup.step}
1305 status={speech.status}
1306 model={speech.setup.model}
1307 language={speech.setup.language}
1308 downloading={speech.setup.downloading}
1309 error={speech.setup.error}
1310 onOpenChange={speech.setup.setOpen}
1311 onRetryCatalog={speech.setup.retryCatalog}
1312 onDownload={() => void speech.setup.download()}
1313 onCancelDownload={() => void speech.setup.cancelDownload()}
1314 onStartRecording={() => void speech.setup.startRecording()}
1315 />
1316 </SettingsSearchTarget>
1317 );
1318 }
1319
1320 export function VoiceSettingsPanel() {
1321 const messages = useMessages();
1322 const setting = useSearchableSetting();
1323 const gt = useGT();
1324 const cleanupEnabled = useCleanupGrant() !== null;
1325 const settings = useClientSettings();
1326 const settingsScope = useOptionalSettingsScope();
1327 const destination = useVoiceTranscriptionDestination();
1328 const appleSpeechLocales = useAppleSpeechLocales();
1329 const appleSpeechLanguages =
1330 !appleSpeechLocales.includes(settings.speechLanguage) &&
1331 supportsAppleSpeechLocale(appleSpeechLocales, settings.speechLanguage)
1332 ? [...appleSpeechLocales, settings.speechLanguage]
1333 : appleSpeechLocales;
1334 const appleSpeechAvailable = appleSpeechLocales.length > 0;
1335 const appleSpeechLabel = appleSpeechAvailable
1336 ? APPLE_SPEECH_LABEL
1337 : gt("Apple Speech (unavailable)");
1338 const canManageKey = useEnvironmentScope(destination.environmentId, AuthSettingsWriteScope);
1339 const [pendingCloud, setPendingCloud] = useState<{
1340 provider: CloudSpeechProvider;
1341 environmentId: EnvironmentId;
1342 } | null>(null);
1343 const { environments } = useEnvironments();
1344 const [microphones, setMicrophones] = useState<BrowserMicrophone[]>([]);
1345 const prepared = destination.cloudProvider ? null : destination.prepared;
1346 const catalog = useVoiceModels(prepared);
1347
1348 const refreshMicrophones = useStableCallback(async () => {
1349 if (!navigator.mediaDevices?.enumerateDevices) return;
1350 try {
1351 const devices = await listAudioInputs();
1352 setMicrophones(devices);
1353 updateMicrophoneOrder((order) => rememberMicrophones(order, devices));
1354 } catch {
1355 setMicrophones([]);
1356 }
1357 });
1358 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to microphone device changes while settings are open
1359 useEffect(() => {
1360 queueMicrotask(() => void refreshMicrophones());
1361 const mediaDevices = navigator.mediaDevices;
1362 if (!mediaDevices?.addEventListener) return;
1363 const onChange = () => void refreshMicrophones();
1364 mediaDevices.addEventListener("devicechange", onChange);
1365 return () => mediaDevices.removeEventListener("devicechange", onChange);
1366 }, [refreshMicrophones]);
1367 const modelStatus = catalog.status;
1368 const target = settingsScope?.target;
1369 const members = settingsScope?.scope.members;
1370 const project =
1371 members?.find(
1372 (member) => member.environmentId === target?.environmentId && member.id === target.projectId,
1373 ) ?? members?.[0];
1374
1375 const order = microphoneOrder(settings);
1376 const selectedMicrophone = preferredMicrophoneId(order, microphones);
1377 const environmentChoices = environments.map((environment) => ({
1378 environment,
1379 value: JSON.stringify(["environment", environment.environmentId]),
1380 }));
1381 const cloudGroups = environments
1382 .filter(
1383 (environment) => environment.serverConfig?.environment.capabilities.cloudVoiceTranscription,
1384 )
1385 .map((environment) => ({
1386 environment,
1387 choices: CloudSpeechProvider.literals.map((provider) => ({
1388 environment,
1389 provider,
1390 value: JSON.stringify(["cloud", environment.environmentId, provider]),
1391 })),
1392 }));
1393 const cloudChoices = cloudGroups.flatMap((group) => group.choices);
1394 const destinationValue = destination.device
1395 ? DEVICE
1396 : destination.cloudProvider
1397 ? JSON.stringify(["cloud", destination.environmentId, destination.cloudProvider])
1398 : destination.environmentId
1399 ? JSON.stringify(["environment", destination.environmentId])
1400 : OFF;
1401 const cloudEnvironment = environments.find(
1402 (env) => env.environmentId === pendingCloud?.environmentId,
1403 );
1404 // OpenRouter users without settings access still manage their own model choice.
1405 const manageLabel =
1406 destination.cloudProvider === "openrouter" ? gt("Manage OpenRouter") : gt("Manage key");
1407 const chosenEnvironment = environments.find(
1408 (environment) => environment.environmentId === destination.environmentId,
1409 );
1410 const chosenUnsupported =
1411 !destination.cloudProvider &&
1412 chosenEnvironment?.serverConfig != null &&
1413 chosenEnvironment.serverConfig.environment.capabilities.voiceTranscription !== true;
1414 const model = prepared && modelStatus?.supported ? modelStatus : null;
1415
1416 return (
1417 <SettingsPageContainer
1418 searchTargetFallbacks={{
1419 "speech-language":
1420 model || destination.cloudProvider || destination.device
1421 ? undefined
1422 : "voice-transcription-destination",
1423 "speech-models": prepared ? undefined : "voice-transcription-destination",
1424 "speech-hardware": prepared && model ? undefined : "voice-transcription-destination",
1425 "speech-streaming": prepared && model ? undefined : "voice-transcription-destination",
1426 "transcription-test": !prepared
1427 ? "voice-transcription-destination"
1428 : !model || model.state === "missing-model"
1429 ? "speech-models"
1430 : undefined,
1431 "speech-post-processing-model":
1432 cleanupEnabled && settingsScope ? undefined : "speech-post-processing",
1433 "speech-correction-word": cleanupEnabled ? undefined : "speech-post-processing",
1434 }}
1435 >
1436 <SettingsSection id="voice-input" title={gt("Input")}>
1437 <SettingsRow
1438 {...setting("voice-microphone")}
1439 title={gt("Microphone")}
1440 description={gt("Voice input uses the first connected microphone in this list.")}
1441 control={
1442 <Tooltip>
1443 <TooltipTrigger
1444 render={
1445 <Button
1446 type="button"
1447 variant="outline"
1448 size="icon-sm"
1449 aria-label={gt("Refresh microphones")}
1450 onClick={() => void refreshMicrophones()}
1451 >
1452 <RefreshCwIcon />
1453 </Button>
1454 }
1455 />
1456 <TooltipPopup>
1457 <T>Refresh microphones</T>
1458 </TooltipPopup>
1459 </Tooltip>
1460 }
1461 >
1462 <div className="mt-3 pb-2">
1463 <MicrophoneOrderList order={order} devices={microphones} />
1464 </div>
1465 </SettingsRow>
1466 <MicrophoneTest
1467 key={selectedMicrophone}
1468 microphoneId={selectedMicrophone}
1469 onMicrophoneAccess={() => void refreshMicrophones()}
1470 />
1471 <SettingsRow
1472 {...setting("dictation-shortcut-mode")}
1473 title={gt("Dictation shortcut")}
1474 description={`${messages(SHORTCUT_MODES[settings.voiceShortcutMode].hint)}.`}
1475 control={
1476 <Select
1477 value={settings.voiceShortcutMode}
1478 onValueChange={(value) => {
1479 if (value)
1480 void updateClientSettings({ voiceShortcutMode: value as VoiceShortcutMode });
1481 }}
1482 >
1483 <SelectTrigger size="sm" aria-label={gt("Dictation shortcut mode")}>
1484 <SelectValue>
1485 {messages(SHORTCUT_MODES[settings.voiceShortcutMode].label)}
1486 </SelectValue>
1487 </SelectTrigger>
1488 <SelectPopup align="end" alignItemWithTrigger={false}>
1489 {(Object.keys(SHORTCUT_MODES) as VoiceShortcutMode[]).map((mode) => (
1490 <SelectItem key={mode} value={mode}>
1491 <span className="flex flex-col">
1492 {messages(SHORTCUT_MODES[mode].label)}
1493 <span className="text-xs text-muted-foreground">
1494 {messages(SHORTCUT_MODES[mode].hint)}
1495 </span>
1496 </span>
1497 </SelectItem>
1498 ))}
1499 </SelectPopup>
1500 </Select>
1501 }
1502 />
1503 </SettingsSection>
1504
1505 <SettingsSection id="voice-transcription" title={gt("Transcription")}>
1506 <SettingsRow
1507 {...setting("voice-transcription-destination")}
1508 title={gt("Transcribe with")}
1509 description={
1510 destination.environmentId && chosenUnsupported
1511 ? gt("{label} can’t transcribe audio locally. Choose another destination.", {
1512 label: chosenEnvironment.label,
1513 })
1514 : gt("Where your speech is turned into text.")
1515 }
1516 control={
1517 <div className="flex w-full min-w-0 items-center gap-1.5 @min-[32rem]/settings-row:w-auto">
1518 <Select
1519 value={destinationValue}
1520 onValueChange={(value) => {
1521 if (!value) return;
1522 const cloud = cloudChoices.find((choice) => choice.value === value);
1523 if (cloud) {
1524 setPendingCloud({
1525 environmentId: cloud.environment.environmentId,
1526 provider: cloud.provider,
1527 });
1528 return;
1529 }
1530 setPendingCloud(null);
1531 const chosen = environmentChoices.find((choice) => choice.value === value);
1532 if (value === OFF) chooseAudioDestination(null);
1533 else if (value === DEVICE) chooseAudioDestination({ kind: "device" });
1534 else if (chosen)
1535 chooseAudioDestination({
1536 kind: "environment",
1537 environmentId: chosen.environment.environmentId,
1538 });
1539 }}
1540 >
1541 <SelectTrigger
1542 size="sm"
1543 aria-label={gt("Transcribe with")}
1544 className="min-w-0 @min-[32rem]/settings-row:w-auto @min-[32rem]/settings-row:max-w-72"
1545 >
1546 <SelectValue>
1547 <span className="flex min-w-0 items-center gap-2">
1548 {destination.device ? (
1549 <LaptopIcon aria-hidden className="size-4" />
1550 ) : destination.cloudProvider ? (
1551 <CloudSpeechLogo provider={destination.cloudProvider} />
1552 ) : destination.environmentId ? (
1553 <MonitorIcon aria-hidden className="size-4" />
1554 ) : (
1555 <MicOffIcon aria-hidden className="size-4" />
1556 )}
1557 <span className="truncate">
1558 {destination.device
1559 ? appleSpeechLabel
1560 : destination.cloudProvider
1561 ? CLOUD_SPEECH_LABELS[destination.cloudProvider]
1562 : destination.environmentId
1563 ? (chosenEnvironment?.label ?? gt("Chosen environment (unavailable)"))
1564 : gt("Off")}
1565 {destination.cloudProvider && (
1566 <span className="text-muted-foreground">
1567 {" via "}
1568 {chosenEnvironment?.label ?? gt("unavailable environment")}
1569 </span>
1570 )}
1571 </span>
1572 </span>
1573 </SelectValue>
1574 </SelectTrigger>
1575 <SelectPopup align="end" alignItemWithTrigger={false}>
1576 <SelectItem value={OFF}>
1577 <span className="flex items-center gap-2">
1578 <MicOffIcon aria-hidden className="size-4" />
1579 <T>Off</T>
1580 </span>
1581 </SelectItem>
1582 {(appleSpeechAvailable || destination.device) && (
1583 <SelectGroup>
1584 <SelectGroupLabel>
1585 <T>On this Mac</T>
1586 </SelectGroupLabel>
1587 <SelectItem value={DEVICE}>
1588 <span className="flex items-center gap-2">
1589 <LaptopIcon aria-hidden className="size-4" />
1590 {appleSpeechLabel}
1591 </span>
1592 </SelectItem>
1593 </SelectGroup>
1594 )}
1595 {environmentChoices.length > 0 && (
1596 <SelectGroup>
1597 <SelectGroupLabel>
1598 <T>On an environment</T>
1599 </SelectGroupLabel>
1600 {environmentChoices.map(({ environment, value }) => (
1601 <SelectItem key={value} value={value}>
1602 <span className="flex items-center gap-2">
1603 <MonitorIcon aria-hidden className="size-4" />
1604 {environment.label}
1605 </span>
1606 </SelectItem>
1607 ))}
1608 </SelectGroup>
1609 )}
1610 {cloudGroups.map(({ environment, choices }) => (
1611 <SelectGroup key={environment.environmentId}>
1612 <SelectGroupLabel>
1613 <T>
1614 Cloud via <Var>{environment.label}</Var>
1615 </T>
1616 </SelectGroupLabel>
1617 {choices.map(({ value, provider }) => (
1618 <SelectItem
1619 key={value}
1620 value={value}
1621 aria-label={`${CLOUD_SPEECH_LABELS[provider]} via ${environment.label}`}
1622 >
1623 <span className="flex items-center gap-2">
1624 <CloudSpeechLogo provider={provider} />
1625 {CLOUD_SPEECH_LABELS[provider]}
1626 </span>
1627 </SelectItem>
1628 ))}
1629 </SelectGroup>
1630 ))}
1631 {destination.environmentId &&
1632 (!chosenEnvironment ||
1633 (destination.cloudProvider &&
1634 !cloudChoices.some((option) => option.value === destinationValue))) ? (
1635 <SelectItem value={destinationValue}>
1636 <T>Chosen environment (unavailable)</T>
1637 </SelectItem>
1638 ) : null}
1639 </SelectPopup>
1640 </Select>
1641 {destination.cloudProvider &&
1642 chosenEnvironment &&
1643 (canManageKey || destination.cloudProvider === "openrouter") && (
1644 <Tooltip>
1645 <TooltipTrigger
1646 render={
1647 <Button
1648 size="icon-sm"
1649 type="button"
1650 variant="ghost"
1651 aria-label={manageLabel}
1652 onClick={() => {
1653 if (destination.cloudProvider)
1654 setPendingCloud({
1655 provider: destination.cloudProvider,
1656 environmentId: chosenEnvironment.environmentId,
1657 });
1658 }}
1659 >
1660 <KeyRoundIcon aria-hidden className="size-4" />
1661 </Button>
1662 }
1663 />
1664 <TooltipPopup>{manageLabel}</TooltipPopup>
1665 </Tooltip>
1666 )}
1667 </div>
1668 }
1669 />
1670 {pendingCloud && cloudEnvironment && (
1671 <CloudVoiceConfiguration
1672 key={JSON.stringify(pendingCloud)}
1673 provider={pendingCloud.provider}
1674 environment={cloudEnvironment}
1675 active={
1676 destination.environmentId === pendingCloud.environmentId &&
1677 destination.cloudProvider === pendingCloud.provider
1678 }
1679 close={() => setPendingCloud(null)}
1680 />
1681 )}
1682 {(model || destination.cloudProvider || destination.device) && (
1683 <SettingsRow
1684 {...setting("speech-language")}
1685 title={gt("Language")}
1686 description={
1687 destination.device
1688 ? gt(
1689 "Automatic uses your Mac’s language. The first use of a language downloads it.",
1690 )
1691 : destination.cloudProvider
1692 ? gt("The provider detects your language automatically.")
1693 : model?.supportsLanguageDetection
1694 ? gt("{model} detects the language automatically.", { model: model.model })
1695 : model
1696 ? gt("{model} uses one fixed language.", { model: model.model })
1697 : gt("This model uses one fixed language.")
1698 }
1699 control={
1700 <SpeechLanguageSelect
1701 automaticLabel={
1702 destination.device
1703 ? gt("Auto", { $context: "automatic language detection" })
1704 : undefined
1705 }
1706 model={
1707 destination.device
1708 ? { languages: appleSpeechLanguages, supportsLanguageDetection: true }
1709 : destination.cloudProvider
1710 ? cloudSpeechLanguages
1711 : model
1712 }
1713 />
1714 }
1715 />
1716 )}
1717 {model?.supportsTranslation ? (
1718 <SettingsRow
1719 title={gt("Translate to English")}
1720 description={gt("Speak any language and get English text.")}
1721 control={
1722 <Switch
1723 aria-label={gt("Translate to English")}
1724 checked={settings.speechTranslateToEnglish}
1725 onCheckedChange={(checked) =>
1726 void updateClientSettings({ speechTranslateToEnglish: checked })
1727 }
1728 />
1729 }
1730 />
1731 ) : null}
1732 <SettingsRow
1733 {...setting("speech-silence-filtering")}
1734 title={gt("Silence filtering")}
1735 description={
1736 destination.device
1737 ? gt("Apple Speech handles silence itself.")
1738 : destination.cloudProvider === "openai"
1739 ? gt("OpenAI handles silence itself.")
1740 : gt("Skip silence and background noise before transcribing.")
1741 }
1742 control={
1743 <Switch
1744 aria-label={gt("Silence filtering")}
1745 disabled={destination.device || destination.cloudProvider === "openai"}
1746 checked={settings.speechSilenceFiltering}
1747 onCheckedChange={(checked) =>
1748 void updateClientSettings({ speechSilenceFiltering: checked })
1749 }
1750 />
1751 }
1752 />
1753 <SettingsRow
1754 {...setting("remove-filler-words")}
1755 title={gt("Remove filler words")}
1756 description={
1757 destination.device
1758 ? gt("Not available with Apple Speech.")
1759 : gt("Drop “um” and “uh” from transcripts.")
1760 }
1761 control={
1762 <Switch
1763 aria-label={gt("Remove filler words")}
1764 disabled={destination.device}
1765 checked={settings.speechRemoveFillerWords}
1766 onCheckedChange={(checked) =>
1767 void updateClientSettings({ speechRemoveFillerWords: checked })
1768 }
1769 />
1770 }
1771 />
1772 {prepared ? (
1773 <SpeechModelBrowser
1774 key={prepared.environmentId}
1775 prepared={prepared}
1776 catalog={catalog}
1777 testEnvironmentId={
1778 project?.environmentId ?? target?.environmentId ?? prepared.environmentId
1779 }
1780 projectId={project?.id ?? null}
1781 projectName={project?.title ?? null}
1782 />
1783 ) : null}
1784 {prepared && <AdvancedSpeechSettings prepared={prepared} catalog={catalog} />}
1785 </SettingsSection>
1786
1787 <SettingsSection id="voice-dictionary" title={gt("Dictionary")}>
1788 <SettingsRow
1789 {...setting("speech-dictionary")}
1790 title={gt("Your dictionary")}
1791 description={gt("Names and terms to spell your way, saved on this device.")}
1792 >
1793 <div className="mt-3 pb-2">
1794 <DictionaryEditor
1795 label="dictionary"
1796 words={settings.speechCustomWords}
1797 onChange={(words) => void updateClientSettings({ speechCustomWords: words })}
1798 />
1799 </div>
1800 </SettingsRow>
1801 <ProjectDictionaryRow />
1802 </SettingsSection>
1803
1804 <SettingsSection id={setting("speech-post-processing").id} title={gt("Cleanup")}>
1805 <SettingsRow
1806 title={gt("Clean up transcripts")}
1807 description={gt(
1808 "Off by default. Sends the transcript and nearby draft text, never audio, to the cleanup model.",
1809 )}
1810 control={
1811 <Switch
1812 aria-label={gt("Clean up transcripts")}
1813 checked={cleanupEnabled}
1814 onCheckedChange={chooseTranscriptCleanup}
1815 />
1816 }
1817 />
1818 {cleanupEnabled ? (
1819 <SettingsRow
1820 title={gt("Instructions")}
1821 description={
1822 settings.speechPostProcessingPrompt.mode === "custom"
1823 ? gt("Your own instructions for cleanup.")
1824 : gt("Fix punctuation, numbers, lists, and spoken corrections.")
1825 }
1826 control={
1827 <Select
1828 value={settings.speechPostProcessingPrompt.mode}
1829 onValueChange={(value) => {
1830 if (value === "default" || value === "custom")
1831 void updateClientSettings({
1832 speechPostProcessingPrompt: {
1833 mode: value,
1834 customInstructions:
1835 value === "custom" &&
1836 !settings.speechPostProcessingPrompt.customInstructions.trim()
1837 ? DEFAULT_SPEECH_POST_PROCESSING_PROMPT
1838 : settings.speechPostProcessingPrompt.customInstructions,
1839 },
1840 });
1841 }}
1842 >
1843 <SelectTrigger size="sm" aria-label={gt("Cleanup instructions")}>
1844 <SelectValue>
1845 {settings.speechPostProcessingPrompt.mode === "custom"
1846 ? gt("Custom")
1847 : gt("Built-in")}
1848 </SelectValue>
1849 </SelectTrigger>
1850 <SelectPopup align="end" alignItemWithTrigger={false}>
1851 <SelectItem value="default">
1852 <T>Built-in</T>
1853 </SelectItem>
1854 <SelectItem value="custom">
1855 <T>Custom</T>
1856 </SelectItem>
1857 </SelectPopup>
1858 </Select>
1859 }
1860 >
1861 {settings.speechPostProcessingPrompt.mode === "custom" ? (
1862 <div className="mt-3 pb-2">
1863 <Textarea
1864 aria-label={gt("Custom cleanup instructions")}
1865 maxLength={10_000}
1866 value={settings.speechPostProcessingPrompt.customInstructions}
1867 onChange={(event) =>
1868 void updateClientSettings({
1869 speechPostProcessingPrompt: {
1870 mode: "custom",
1871 customInstructions: event.target.value,
1872 },
1873 })
1874 }
1875 />
1876 </div>
1877 ) : null}
1878 </SettingsRow>
1879 ) : null}
1880 {cleanupEnabled && settingsScope !== null ? <CleanupModelRow /> : null}
1881 {cleanupEnabled ? (
1882 <SettingsRow
1883 {...setting("speech-correction-word")}
1884 title={gt("Correction cue")}
1885 description={gt("The phrase you say to fix a mistake, like “scratch that”.")}
1886 control={
1887 <Input
1888 key={settings.speechCorrectionWord}
1889 defaultValue={settings.speechCorrectionWord}
1890 maxLength={50}
1891 placeholder="sorry"
1892 aria-label={gt("Correction cue")}
1893 className="@min-[32rem]/settings-row:w-40"
1894 onBlur={(event) => {
1895 const next = event.target.value.trim();
1896 if (next !== settings.speechCorrectionWord)
1897 void updateClientSettings({ speechCorrectionWord: next });
1898 }}
1899 />
1900 }
1901 />
1902 ) : null}
1903 </SettingsSection>
1904 </SettingsPageContainer>
1905 );
1906 }