diff --git a/apps/mobile/src/Stack.tsx b/apps/mobile/src/Stack.tsx index 8a07f8b7a0867..2c17ca4295638 100644 --- a/apps/mobile/src/Stack.tsx +++ b/apps/mobile/src/Stack.tsx @@ -90,6 +90,7 @@ import { SettingsAppearanceRouteScreen } from "./features/settings/SettingsAppea import { SettingsClientStorageRouteScreen } from "./features/settings/SettingsClientStorageRouteScreen"; import { SettingsDiagnosticsRouteScreen } from "./features/diagnostics/SettingsDiagnosticsRouteScreen"; import { SettingsProviderAccountsRouteScreen } from "./features/settings/SettingsProviderAccountsRouteScreen"; +import { SettingsSpeechToTextRouteScreen } from "./features/settings/SettingsSpeechToTextRouteScreen"; import { SettingsAuthRouteScreen } from "./features/settings/SettingsAuthRouteScreen"; import { SettingsEnvironmentDetailRouteScreen } from "./features/settings/SettingsEnvironmentDetailRouteScreen"; import { SettingsEnvironmentsRouteScreen } from "./features/settings/SettingsEnvironmentsRouteScreen"; @@ -281,6 +282,11 @@ const SETTINGS_SCREENS = { linking: "settings/provider-accounts", options: { ...SETTINGS_PAGE_OPTIONS, title: "Provider accounts" }, }), + SettingsSpeechToText: createNativeStackScreen({ + screen: SettingsSpeechToTextRouteScreen, + linking: "settings/speech-to-text", + options: { ...SETTINGS_PAGE_OPTIONS, title: "Speech to text" }, + }), SettingsEnvironmentMaintenance: createNativeStackScreen({ screen: SettingsEnvironmentMaintenanceRouteScreen, linking: "settings/maintenance", diff --git a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx index 2f7406d5b7820..4afce7a6bf99b 100644 --- a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx @@ -268,6 +268,7 @@ function useSettingsIndexSections(): SettingsListSection[] { serverPage("SettingsEnvironmentNewThreads", "text.bubble", "New threads"), serverPage("SettingsEnvironmentSourceControl", "arrow.triangle.branch", "Source control"), serverPage("SettingsEnvironmentAgentBehavior", "text.alignleft", "Agent behavior"), + serverPage("SettingsSpeechToText", "mic", "Speech to text"), serverPage("SettingsEnvironmentMaintenance", "arrow.clockwise", "Maintenance"), ], }, diff --git a/apps/mobile/src/features/settings/SettingsSpeechToTextRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsSpeechToTextRouteScreen.tsx new file mode 100644 index 0000000000000..f17a0bfd6beaf --- /dev/null +++ b/apps/mobile/src/features/settings/SettingsSpeechToTextRouteScreen.tsx @@ -0,0 +1,338 @@ +import { + AuthSettingsWriteScope, + isSpeechToTextConfigured, + resolveSpeechToTextProviderSettings, + SPEECH_TO_TEXT_PROVIDERS, + type SpeechToTextCleanupMode, + type SpeechToTextProvider, + type SpeechToTextSettings, + type SpeechToTextSettingsPatch, +} from "@t3tools/contracts"; +import { useState } from "react"; +import { View } from "react-native"; +import { useSafeAreaInsets } from "react-native-safe-area-context"; + +import { AppText as Text, AppTextInput } from "../../components/ui/AppText"; +import { ScreenScrollView } from "../../components/ui/ScreenScrollView"; +import { useEnvironmentScope } from "../../state/session"; +import { serverEnvironment } from "../../state/server"; +import { useAtomCommand } from "../../state/use-atom-command"; +import { SettingsActionRow } from "./components/SettingsActionRow"; +import { SettingsChoiceRow } from "./components/SettingsChoiceRow"; +import { + AndroidSettingsEnvironmentFilter, + SettingsEnvironmentFilterHeader, +} from "./components/SettingsEnvironmentFilterHeader"; +import { SettingsScreen } from "./components/SettingsScreen"; +import { SettingsSection } from "./components/SettingsSection"; +import { SettingsSwitchRow } from "./components/SettingsSwitchRow"; +import { useSettingsEnvironmentFilter, type SettingsTarget } from "./settings-environment-filter"; + +const PROVIDER_IDS = Object.keys(SPEECH_TO_TEXT_PROVIDERS) as SpeechToTextProvider[]; + +const CLEANUP_CHOICES: ReadonlyArray<{ + readonly mode: SpeechToTextCleanupMode | null; + readonly label: string; + readonly description: string; +}> = [ + { mode: null, label: "Off", description: "Insert the transcript as the service returned it." }, + { + mode: "text-generation", + label: "Text generation model", + description: "Rewrite with the model chosen for commit messages and titles.", + }, + { + mode: "openai-compatible", + label: "OpenAI-compatible", + description: "Rewrite with a chat completions server such as Ollama or LM Studio.", + }, +]; + +export function SettingsSpeechToTextRouteScreen() { + const { selectedTargets } = useSettingsEnvironmentFilter(); + const insets = useSafeAreaInsets(); + return ( + <> + + }> + + {selectedTargets.length === 0 ? ( + Select a connected environment. + ) : ( + selectedTargets.map((environment) => ( + + )) + )} + + + + ); +} + +/** + * Commits on end of editing, like the other text settings, so typing does not round-trip. The + * field owns its draft while focused: a value another device saves only replaces it once the + * field is idle. A secret field never shows the stored key and clears after each commit. + */ +function SettingsTextField(props: { + readonly label: string; + readonly description?: string; + readonly value: string; + readonly placeholder: string; + readonly disabled: boolean; + readonly secure?: boolean; + readonly keyboardType?: "default" | "url"; + readonly onCommit: (value: string) => void; +}) { + const initial = props.secure ? "" : props.value; + const [draft, setDraft] = useState(initial); + const [focused, setFocused] = useState(false); + const [syncedValue, setSyncedValue] = useState(initial); + if (!focused && initial !== syncedValue) { + setSyncedValue(initial); + setDraft(initial); + } + return ( + + {props.label} + {props.description ? ( + {props.description} + ) : null} + setFocused(true)} + onChangeText={setDraft} + onEndEditing={() => { + setFocused(false); + const value = draft.trim(); + if (props.secure) { + // A stored key is replaced only by a new value; removing it is its own action. + if (!props.disabled && value.length > 0) props.onCommit(value); + setDraft(""); + return; + } + if (!props.disabled && value !== props.value) props.onCommit(value); + setDraft(value); + }} + /> + + ); +} + +function EnvironmentSpeechToText({ environment }: { readonly environment: SettingsTarget }) { + const settings: SpeechToTextSettings = environment.serverConfig.settings.speechToText; + const supported = environment.serverConfig.environment.capabilities.speechToText === true; + const canEdit = useEnvironmentScope(environment.environmentId, AuthSettingsWriteScope); + const disabled = !supported || !canEdit; + const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: true }); + const update = (speechToText: SpeechToTextSettingsPatch) => + void updateSettings({ + environmentId: environment.environmentId, + input: { patch: { speechToText } }, + }); + + const provider = settings.provider; + const info = provider ? SPEECH_TO_TEXT_PROVIDERS[provider] : null; + const configured = provider ? resolveSpeechToTextProviderSettings(settings, provider) : null; + const keyStored = (configured?.apiKey.length ?? 0) > 0; + const updateProvider = (fields: { model?: string; baseUrl?: string; apiKey?: string }) => { + if (provider) update({ providers: { [provider]: fields } }); + }; + const cleanup = settings.cleanup; + const cleanupKeyStored = cleanup.apiKey.length > 0; + + const status = !supported + ? `Update ${environment.label} to use speech to text.` + : !canEdit + ? "This connection cannot change this environment's settings." + : provider === null + ? "Off. Dictation uses on-device transcription where the device supports it." + : isSpeechToTextConfigured(settings) + ? "Ready. Use the microphone in the composer to dictate." + : info?.apiKeyRequired && !keyStored + ? `Add your ${info.label} API key to finish setup.` + : "Add the server URL and model to finish setup."; + + return ( + <> + + update({ provider: null })} + /> + {PROVIDER_IDS.map((id) => ( + update({ provider: id })} + /> + ))} + + + {provider && info && configured ? ( + + updateProvider({ apiKey })} + /> + {keyStored ? ( + updateProvider({ apiKey: "" })} + /> + ) : null} + {provider !== "xai" ? ( + 0 + ? `Leave empty for ${info.defaultModel}. Also: ${info.suggestedModels + .filter((model) => model !== info.defaultModel) + .join(", ")}.` + : "The model name your server expects." + } + value={configured.model} + placeholder={info.defaultModel || "parakeet-tdt-0.6b-v3"} + disabled={disabled} + onCommit={(model) => updateProvider({ model })} + /> + ) : null} + updateProvider({ baseUrl })} + /> + + ) : null} + + + update({ language: language.toLowerCase() })} + /> + {!info || info.supportsPrompt || cleanup.mode !== null ? ( + update({ prompt })} + /> + ) : null} + + + + {CLEANUP_CHOICES.map((choice, index) => ( + 0} + disabled={disabled} + onPress={() => update({ cleanup: { mode: choice.mode } })} + /> + ))} + {cleanup.mode === "openai-compatible" ? ( + <> + update({ cleanup: { baseUrl } })} + /> + update({ cleanup: { model } })} + /> + update({ cleanup: { apiKey } })} + /> + {cleanupKeyStored ? ( + update({ cleanup: { apiKey: "" } })} + /> + ) : null} + update({ cleanup: { useThreadContext } })} + /> + + ) : null} + {cleanup.mode !== null ? ( + update({ cleanup: { instructions } })} + /> + ) : null} + + + ); +} diff --git a/apps/mobile/src/features/settings/components/settings-page-targets.ts b/apps/mobile/src/features/settings/components/settings-page-targets.ts index 21267a0871435..91f59db2428f2 100644 --- a/apps/mobile/src/features/settings/components/settings-page-targets.ts +++ b/apps/mobile/src/features/settings/components/settings-page-targets.ts @@ -14,6 +14,7 @@ export const SETTINGS_PAGE_TARGETS = [ "SettingsEnvironmentAgentBehavior", "SettingsEnvironmentMaintenance", "SettingsProviderAccounts", + "SettingsSpeechToText", "SettingsKeyboard", "SettingsMicrophone", "SettingsFollowUp", diff --git a/apps/mobile/src/features/settings/settings-search.ts b/apps/mobile/src/features/settings/settings-search.ts index f82f90dfe8333..93264a8eb5f1d 100644 --- a/apps/mobile/src/features/settings/settings-search.ts +++ b/apps/mobile/src/features/settings/settings-search.ts @@ -45,6 +45,7 @@ const SETTINGS_SEARCH_TERMS = { SettingsFollowUp: ["queue", "steer", "while the agent is running"], SettingsArchive: ["archive", "archived", "restore", "unarchive"], SettingsProviderAccounts: ["codex", "claude", "cursor", "opencode", "sign in", "login", "auth"], + SettingsSpeechToText: ["dictation", "transcription", "voice", "whisper", "parakeet", "api key"], SettingsEnvironmentNewThreads: [ "default workspace", "worktree", diff --git a/docs/user/composer.md b/docs/user/composer.md index 7db8248f00e8c..e5d1266ab2695 100644 --- a/docs/user/composer.md +++ b/docs/user/composer.md @@ -182,7 +182,8 @@ recording and preserves your existing draft. ### Set up a transcription service -Choose a service in **Settings → Speech to text**. The setting belongs to the environment, so every +Choose a service in **Settings → Speech to text** on web and desktop, or **Settings → Server +settings → Speech to text** in the mobile app. The setting belongs to the environment, so every web, desktop, and mobile client connected to it can dictate. API keys stay in that environment's secret store. Recordings go from the environment to the service you choose and are deleted after transcription; only the message text is sent when you submit.