Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

feat(voice): choose the language macOS speech listens in

Alan Buscagliab2b2c79eb110
diff --git a/packages/ui/src/components/SettingsPanel.tsx b/packages/ui/src/components/SettingsPanel.tsx
index d63cc87..0d92666 100644
--- a/packages/ui/src/components/SettingsPanel.tsx
+++ b/packages/ui/src/components/SettingsPanel.tsx
@@ -7,6 +7,7 @@ import {
type ShortcutInfo,
saveShortcut,
} from "../shortcut.ts";
+import { setVoiceLanguage, VOICE_LANGUAGES, voiceLanguage } from "../voice-language.ts";
import "../settings.css";
type Status =
@@ -24,6 +25,7 @@ function reasonOf(error: unknown): string {
/** The desktop panel's settings (S33.1); for now, the shortcut that opens the panel. */
export function SettingsPanel({ close }: { close: () => void }) {
+ const [language, setLanguage] = useState(() => voiceLanguage() ?? "");
const mac = isMac();
const show = (accelerator: string) => formatShortcut(accelerator, mac);
const [info, setInfo] = useState<ShortcutInfo | undefined>(undefined);
@@ -146,6 +148,27 @@ export function SettingsPanel({ close }: { close: () => void }) {
Reset to default
</button>
</section>
+
+ <section className="provider-section" aria-labelledby="settings-voice-language">
+ <h3 id="settings-voice-language">Voice language</h3>
+ <p className="muted">
+ The language macOS speech listens in. The local voice model detects it by itself.
+ </p>
+ <select
+ aria-label="Voice language"
+ value={language}
+ onChange={(event) => {
+ setLanguage(event.target.value);
+ setVoiceLanguage(event.target.value);
+ }}
+ >
+ {VOICE_LANGUAGES.map(([locale, label]) => (
+ <option key={locale || "auto"} value={locale}>
+ {label}
+ </option>
+ ))}
+ </select>
+ </section>
</section>
);
}
diff --git a/packages/ui/src/useVoice.ts b/packages/ui/src/useVoice.ts
index c4728c1..7d9f777 100644
--- a/packages/ui/src/useVoice.ts
+++ b/packages/ui/src/useVoice.ts
@@ -4,6 +4,7 @@ import { newRequestId, type Send } from "./components/types.ts";
import { inDesktop } from "./desktop.ts";
import { guessLanguage, pickVoice, speakableText } from "./speech.ts";
import { type DotState, isBusy } from "./store.ts";
+import { voiceLanguage } from "./voice-language.ts";
import {
cancelNativeVoice,
type NativeVoiceStatus,
@@ -297,7 +298,8 @@ export function useVoice(state: DotState, send: Send): VoiceControls {
const startNative = useCallback(async () => {
try {
- await startNativeVoice(navigator.language);
+ // Apple's recognizer listens in one language: the one chosen in Settings, else the system's.
+ await startNativeVoice(voiceLanguage() ?? navigator.language);
} catch (error) {
finish(startRefusal(error));
return;
diff --git a/packages/ui/src/voice-language.ts b/packages/ui/src/voice-language.ts
new file mode 100644
index 0000000..4883d7e
--- /dev/null
+++ b/packages/ui/src/voice-language.ts
@@ -0,0 +1,35 @@
+/**
+ * The language macOS speech listens in (S30.6). Apple's recognizer does not detect the language
+ * by itself, so a Mac set to English heard Spanish as noise; the local model (Parakeet) detects
+ * it and ignores this. Kept in this window's storage; empty means the system language.
+ */
+const KEY = "gentle-dot-voice-language";
+
+export const VOICE_LANGUAGES: readonly (readonly [locale: string, label: string])[] = [
+ ["", "Automatic (system language)"],
+ ["es-ES", "Español"],
+ ["en-US", "English"],
+ ["pt-BR", "Português"],
+ ["fr-FR", "Français"],
+ ["de-DE", "Deutsch"],
+ ["it-IT", "Italiano"],
+];
+
+/** The chosen locale, or undefined for the system language. */
+export function voiceLanguage(): string | undefined {
+ try {
+ const locale = localStorage.getItem(KEY);
+ return locale && VOICE_LANGUAGES.some(([value]) => value === locale) ? locale : undefined;
+ } catch {
+ return undefined;
+ }
+}
+
+export function setVoiceLanguage(locale: string): void {
+ try {
+ if (locale) localStorage.setItem(KEY, locale);
+ else localStorage.removeItem(KEY);
+ } catch {
+ // Private browsing: the choice lasts for this window only.
+ }
+}
diff --git a/packages/ui/test/settings.test.tsx b/packages/ui/test/settings.test.tsx
index 35fa387..6baeea0 100644
--- a/packages/ui/test/settings.test.tsx
+++ b/packages/ui/test/settings.test.tsx
@@ -162,6 +162,18 @@ describe("the Settings screen (S33.1)", () => {
});
});
+describe("voice language (S30.6)", () => {
+ it("defaults to the system language and remembers the one chosen for macOS speech", async () => {
+ await openSettings();
+ const picker = screen.getByRole("combobox", { name: "Voice language" });
+ expect(picker).toHaveValue("");
+ await userEvent.selectOptions(picker, "es-ES");
+ expect(localStorage.getItem("gentle-dot-voice-language")).toBe("es-ES");
+ await userEvent.selectOptions(picker, "");
+ expect(localStorage.getItem("gentle-dot-voice-language")).toBeNull();
+ });
+});
+
describe("changing the shortcut (S33.2, S33.3)", () => {
it("records the next combination and applies it", async () => {
await startRecording();
diff --git a/packages/ui/test/voice.test.tsx b/packages/ui/test/voice.test.tsx
index fc75718..61d6e8a 100644
--- a/packages/ui/test/voice.test.tsx
+++ b/packages/ui/test/voice.test.tsx
@@ -294,6 +294,15 @@ describe("talking on the desktop", () => {
expect(invoked("voice_stop")).toHaveLength(1);
});
+ it("listens in the language chosen in Settings instead of the system's", async () => {
+ localStorage.setItem("gentle-dot-voice-language", "es-ES");
+ desktop({ available: true });
+ render(<Harness initial={withVoice(false)} />);
+ await waitFor(() => expect(talk()).toBeEnabled());
+ await userEvent.click(talk());
+ expect(invoked("voice_start")[0]?.[1]).toEqual({ locale: "es-ES" });
+ });
+
it("cancels the native recognizer", async () => {
desktop({ available: true });
render(<Harness initial={withVoice(false)} />);