Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(voice): resolve TypeScript errors in voice IPC and runtime

vastsa67a6467146a2
diff --git a/apps/desktop/electron/main/ipc/voice-ipc.ts b/apps/desktop/electron/main/ipc/voice-ipc.ts
index f511c7204..c9a1bb23b 100644
--- a/apps/desktop/electron/main/ipc/voice-ipc.ts
+++ b/apps/desktop/electron/main/ipc/voice-ipc.ts
@@ -22,16 +22,16 @@ export function registerVoiceIpc({
handle(IPC.invoke.voiceStop, () => voiceService.stop());
- handle(IPC.invoke.voiceCancel, () => {
+ handle(IPC.invoke.voiceCancel, async () => {
voiceService.cancel();
return { ok: true };
});
- handle(IPC.invoke.voiceGetState, () => voiceService.getState());
+ handle(IPC.invoke.voiceGetState, async () => voiceService.getState());
- handle(IPC.invoke.voiceGetDevices, () => voiceService.getDevices());
+ handle(IPC.invoke.voiceGetDevices, async () => voiceService.getDevices());
- handle(IPC.invoke.voiceGetModels, () => voiceService.getModels());
+ handle(IPC.invoke.voiceGetModels, async () => voiceService.getModels());
handle(IPC.invoke.voiceDownloadModel, (input: unknown) => {
const { modelId } = input as { modelId: string };
@@ -43,7 +43,7 @@ export function registerVoiceIpc({
return voiceService.deleteModel(modelId);
});
- handle(IPC.invoke.voiceUpdateSettings, (input: unknown) =>
+ handle(IPC.invoke.voiceUpdateSettings, async (input: unknown) =>
voiceService.updateSettings(input as any),
);
diff --git a/apps/desktop/src/features/settings/voice/VoiceSettingsSection.tsx b/apps/desktop/src/features/settings/voice/VoiceSettingsSection.tsx
index c24c80068..07cd79cbb 100644
--- a/apps/desktop/src/features/settings/voice/VoiceSettingsSection.tsx
+++ b/apps/desktop/src/features/settings/voice/VoiceSettingsSection.tsx
@@ -1,10 +1,12 @@
/**
- * Voice settings panel in the Settings page.
+ * Voice settings panel — matches the project's SettingsRow/SettingsCard pattern.
*/
import { useCallback, useEffect, useState } from "react";
import type { TFunction } from "i18next";
-import { useAppStore } from "../../../stores/app-store";
+import type { AppSettings } from "@pi-desktop/shared";
+import { cx } from "../../../components/ui";
+import { SettingsRow, SettingsCard } from "../primitives";
import { voiceIpc } from "../../voice/voice-ipc";
interface AudioInputDevice {
@@ -13,24 +15,31 @@ interface AudioInputDevice {
isDefault: boolean;
}
+interface ModelInfo {
+ id: string;
+ name: string;
+ description: string;
+ sizeBytes: number;
+ recommended: boolean;
+}
+
interface ModelState {
- info: {
- id: string;
- name: string;
- description: string;
- sizeBytes: number;
- recommended: boolean;
- };
+ info: ModelInfo;
status: string;
downloadProgress?: number;
error?: string;
}
-export function VoiceSettingsSection({ t }: { t: TFunction }) {
- const settings = useAppStore((s) => s.settings);
- const updateSettings = useAppStore((s) => s.updateSettings);
-
- const voiceSettings = settings?.voice ?? {
+export function VoiceSettingsSection({
+ t,
+ settings,
+ saveSettings,
+}: {
+ t: TFunction;
+ settings: AppSettings;
+ saveSettings: (patch: Partial<AppSettings>) => Promise<void>;
+}) {
+ const voice = settings.voice ?? {
enabled: false,
deviceId: null,
languages: ["zh", "en"],
@@ -42,14 +51,12 @@ export function VoiceSettingsSection({ t }: { t: TFunction }) {
const [models, setModels] = useState<ModelState[]>([]);
const [permission, setPermission] = useState<string>("undetermined");
- // Load devices and models on mount
useEffect(() => {
- voiceIpc.getDevices().then(setDevices).catch(() => {});
- voiceIpc.getModels().then(setModels).catch(() => {});
- voiceIpc.checkPermission().then(setPermission).catch(() => {});
+ voiceIpc.getDevices().then((d) => setDevices(Array.isArray(d) ? d as AudioInputDevice[] : [])).catch(() => {});
+ voiceIpc.getModels().then((m) => setModels(Array.isArray(m) ? m as ModelState[] : [])).catch(() => {});
+ voiceIpc.checkPermission().then((p) => setPermission(typeof p === "string" ? p : "undetermined")).catch(() => {});
}, []);
- // Listen for model progress
useEffect(() => {
return voiceIpc.onModelProgress(({ modelId, progress }) => {
setModels((prev) =>
@@ -62,13 +69,13 @@ export function VoiceSettingsSection({ t }: { t: TFunction }) {
});
}, []);
- const update = useCallback(
+ const save = useCallback(
(patch: Record<string, unknown>) => {
- const next = { ...voiceSettings, ...patch };
- void updateSettings({ voice: next as any });
+ const next = { ...voice, ...patch };
+ void saveSettings({ voice: next as any });
void voiceIpc.updateSettings(next);
},
- [voiceSettings, updateSettings],
+ [voice, saveSettings],
);
const formatSize = (bytes: number) => {
@@ -77,170 +84,147 @@ export function VoiceSettingsSection({ t }: { t: TFunction }) {
};
return (
- <div className="settings-section">
+ <div className="settings-stack">
{/* Enable toggle */}
- <label className="settings-row settings-row-toggle">
- <span>{t("settings.voiceEnable")}</span>
- <input
- type="checkbox"
- checked={voiceSettings.enabled}
- onChange={(e) => update({ enabled: e.target.checked })}
- />
- </label>
-
- {/* Microphone permission warning */}
+ <SettingsCard title={t("settings.voice")}>
+ <SettingsRow title={t("settings.voiceEnable")}>
+ <button
+ type="button"
+ className={cx("settings-toggle", voice.enabled && "on")}
+ role="switch"
+ aria-checked={voice.enabled}
+ aria-label={t("settings.voiceEnable")}
+ onClick={() => save({ enabled: !voice.enabled })}
+ >
+ <span className="settings-toggle-thumb" />
+ </button>
+ </SettingsRow>
+ </SettingsCard>
+
+ {/* Permission warning */}
{permission === "denied" && (
- <div className="settings-row settings-warning">
- <span>{t("settings.voiceMicDenied")}</span>
- <p className="text-xs opacity-70">{t("settings.voiceMicDeniedDesc")}</p>
- </div>
+ <SettingsCard title={t("settings.voiceMicDenied")}>
+ <SettingsRow title={t("settings.voiceMicDeniedDesc")}>
+ <button
+ type="button"
+ className="settings-btn-secondary"
+ onClick={() => voiceIpc.requestPermission()}
+ >
+ {t("settings.voiceOpenSystemSettings")}
+ </button>
+ </SettingsRow>
+ </SettingsCard>
)}
- {/* Microphone selection */}
- <div className="settings-row">
- <label>{t("settings.voiceMicrophone")}</label>
- <select
- value={voiceSettings.deviceId ?? ""}
- onChange={(e) => update({ deviceId: e.target.value || null })}
- disabled={!voiceSettings.enabled}
- >
- <option value="">{t("settings.voiceSystemDefault")}</option>
- {devices.map((d) => (
- <option key={d.deviceId} value={d.deviceId}>
- {d.label}
- </option>
- ))}
- </select>
- </div>
-
- {/* Languages */}
- <div className="settings-row">
- <label>{t("settings.voiceLanguages")}</label>
- <div className="flex gap-3">
- {[
- { code: "zh", label: "Chinese" },
- { code: "en", label: "English" },
- { code: "ja", label: "Japanese" },
- { code: "ko", label: "Korean" },
- ].map(({ code, label }) => (
- <label key={code} className="flex items-center gap-1 text-sm">
- <input
- type="checkbox"
- checked={voiceSettings.languages.includes(code)}
- disabled={!voiceSettings.enabled}
- onChange={(e) => {
- const langs = e.target.checked
- ? [...voiceSettings.languages, code]
- : voiceSettings.languages.filter((l: string) => l !== code);
- update({ languages: langs.length > 0 ? langs : [code] });
- }}
- />
- {label}
- </label>
- ))}
- </div>
- </div>
-
- {/* Chinese variant */}
- <div className="settings-row">
- <label>{t("settings.voiceChineseVariant")}</label>
- <select
- value={voiceSettings.chineseVariant}
- onChange={(e) => update({ chineseVariant: e.target.value })}
- disabled={!voiceSettings.enabled}
- >
- <option value="simplified">{t("settings.voiceSimplified")}</option>
- <option value="traditional-taiwan">{t("settings.voiceTraditionalTaiwan")}</option>
- <option value="traditional-hong-kong">{t("settings.voiceTraditionalHK")}</option>
- </select>
- </div>
-
- {/* Model selection */}
- <div className="settings-row">
- <label>{t("settings.voiceModel")}</label>
- <select
- value={voiceSettings.modelId}
- onChange={(e) => update({ modelId: e.target.value })}
- disabled={!voiceSettings.enabled}
- >
- <option value="">{t("settings.voiceNoModelSelected")}</option>
- {models.map((m) => (
- <option key={m.info.id} value={m.info.id}>
- {m.info.name}
- {m.info.recommended ? " ★" : ""}
- {m.status === "downloaded" || m.status === "loaded" ? " ✓" : ""}
- </option>
- ))}
- </select>
- </div>
-
- {/* Local models management */}
- <div className="settings-row">
- <label>{t("settings.voiceLocalModels")}</label>
- </div>
- <div className="settings-model-list">
+ {/* Microphone + Language + Chinese variant */}
+ <SettingsCard title={t("settings.voiceMicrophone")}>
+ <SettingsRow title={t("settings.voiceMicrophone")}>
+ <select
+ className="settings-select"
+ value={voice.deviceId ?? ""}
+ disabled={!voice.enabled}
+ onChange={(e) => save({ deviceId: e.target.value || null })}
+ >
+ <option value="">{t("settings.voiceSystemDefault")}</option>
+ {devices.map((d) => (
+ <option key={d.deviceId} value={d.deviceId}>{d.label}</option>
+ ))}
+ </select>
+ </SettingsRow>
+
+ <SettingsRow title={t("settings.voiceLanguages")}>
+ <div className="voice-lang-checks">
+ {(["zh", "en", "ja", "ko"] as const).map((code) => (
+ <label key={code} className="voice-lang-label">
+ <input
+ type="checkbox"
+ checked={voice.languages.includes(code)}
+ disabled={!voice.enabled}
+ onChange={(e) => {
+ const langs = e.target.checked
+ ? [...voice.languages, code]
+ : voice.languages.filter((l: string) => l !== code);
+ save({ languages: langs.length > 0 ? langs : [code] });
+ }}
+ />
+ {code === "zh" ? "中文" : code === "en" ? "English" : code === "ja" ? "日本語" : "한국어"}
+ </label>
+ ))}
+ </div>
+ </SettingsRow>
+
+ <SettingsRow title={t("settings.voiceChineseVariant")}>
+ <select
+ className="settings-select"
+ value={voice.chineseVariant}
+ disabled={!voice.enabled}
+ onChange={(e) => save({ chineseVariant: e.target.value })}
+ >
+ <option value="simplified">{t("settings.voiceSimplified")}</option>
+ <option value="traditional-taiwan">{t("settings.voiceTraditionalTaiwan")}</option>
+ <option value="traditional-hong-kong">{t("settings.voiceTraditionalHK")}</option>
+ </select>
+ </SettingsRow>
+ </SettingsCard>
+
+ {/* Model selection + management */}
+ <SettingsCard title={t("settings.voiceModel")}>
+ <SettingsRow title={t("settings.voiceModel")}>
+ <select
+ className="settings-select"
+ value={voice.modelId}
+ disabled={!voice.enabled}
+ onChange={(e) => save({ modelId: e.target.value })}
+ >
+ <option value="">{t("settings.voiceNoModelSelected")}</option>
+ {models.map((m) => (
+ <option key={m.info.id} value={m.info.id}>
+ {m.info.name}{m.info.recommended ? " ★" : ""}
+ {m.status === "downloaded" || m.status === "loaded" ? " ✓" : ""}
+ </option>
+ ))}
+ </select>
+ </SettingsRow>
+
+ {/* Model list */}
{models.map((m) => (
- <div key={m.info.id} className="settings-model-item">
- <div className="settings-model-info">
- <span className="settings-model-name">
- {m.info.name}
- {m.info.recommended && <span className="text-xs opacity-60"> ★</span>}
- </span>
- <span className="text-xs opacity-60">
- {formatSize(m.info.sizeBytes)}
- </span>
- </div>
- <div className="settings-model-status">
- {m.status === "downloaded" || m.status === "loaded" ? (
- <>
- <span className="text-xs text-green-500">{t("settings.voiceModelDownloaded")}</span>
- <button
- type="button"
- className="text-xs text-red-400 hover:underline"
- onClick={() => {
- void voiceIpc.deleteModel(m.info.id);
- setModels((prev) =>
- prev.map((x) =>
- x.info.id === m.info.id ? { ...x, status: "not-downloaded" } : x,
- ),
- );
- }}
- >
- {t("settings.voiceModelDelete")}
- </button>
- </>
- ) : m.status === "downloading" ? (
- <div className="flex items-center gap-2">
- <div className="voice-volume-bar" style={{ width: 100 }}>
- <div
- className="voice-volume-fill"
- style={{ width: `${(m.downloadProgress ?? 0) * 100}%` }}
- />
- </div>
- <span className="text-xs opacity-60">
- {Math.round((m.downloadProgress ?? 0) * 100)}%
- </span>
- </div>
- ) : (
+ <SettingsRow key={m.info.id} title={m.info.name} description={`${formatSize(m.info.sizeBytes)}${m.info.recommended ? " · Recommended" : ""}`}>
+ {m.status === "downloaded" || m.status === "loaded" ? (
+ <div className="voice-model-actions">
+ <span className="voice-model-badge voice-model-ok">{t("settings.voiceModelDownloaded")}</span>
<button
type="button"
- className="text-xs text-blue-400 hover:underline"
+ className="voice-model-btn voice-model-btn-danger"
onClick={() => {
- void voiceIpc.downloadModel(m.info.id);
- setModels((prev) =>
- prev.map((x) =>
- x.info.id === m.info.id ? { ...x, status: "downloading", downloadProgress: 0 } : x,
- ),
- );
+ void voiceIpc.deleteModel(m.info.id);
+ setModels((prev) => prev.map((x) => x.info.id === m.info.id ? { ...x, status: "not-downloaded" } : x));
}}
>
- {t("settings.voiceModelDownload")}
+ {t("settings.voiceModelDelete")}
</button>
- )}
- </div>
- </div>
+ </div>
+ ) : m.status === "downloading" ? (
+ <div className="voice-model-actions">
+ <div className="voice-progress-track">
+ <div className="voice-progress-fill" style={{ width: `${(m.downloadProgress ?? 0) * 100}%` }} />
+ </div>
+ <span className="voice-model-badge">{Math.round((m.downloadProgress ?? 0) * 100)}%</span>
+ </div>
+ ) : (
+ <button
+ type="button"
+ className="voice-model-btn voice-model-btn-primary"
+ onClick={() => {
+ void voiceIpc.downloadModel(m.info.id);
+ setModels((prev) => prev.map((x) => x.info.id === m.info.id ? { ...x, status: "downloading", downloadProgress: 0 } : x));
+ }}
+ >
+ {t("settings.voiceModelDownload")}
+ </button>
+ )}
+ </SettingsRow>
))}
- </div>
+ </SettingsCard>
</div>
);
}
diff --git a/apps/desktop/src/features/voice/voice-ipc.ts b/apps/desktop/src/features/voice/voice-ipc.ts
index ec41acb48..9a45a5286 100644
--- a/apps/desktop/src/features/voice/voice-ipc.ts
+++ b/apps/desktop/src/features/voice/voice-ipc.ts
@@ -1,51 +1,59 @@
/**
* Renderer-side IPC wrapper for voice operations.
+ * Uses window.piDesktop bridge exposed by the preload script.
+ * All invoke calls return Result<T>; unwrap extracts .data or throws.
*/
-const { ipcRenderer } = window.require("electron");
+import { IPC } from "@pi-desktop/shared";
+
+async function invoke<T = unknown>(channel: string, ...args: unknown[]): Promise<T> {
+ const bridge = window.piDesktop;
+ if (!bridge) throw new Error("piDesktop bridge unavailable");
+ const result = await bridge.invoke<T>(channel, ...args);
+ if (!(result as any).ok) {
+ throw new Error((result as any).message ?? "IPC call failed");
+ }
+ return (result as any).data;
+}
export const voiceIpc = {
start: (settings?: Record<string, unknown>) =>
- ipcRenderer.invoke("pi-desktop/voice/start", settings),
+ invoke(IPC.invoke.voiceStart, settings),
- stop: () => ipcRenderer.invoke("pi-desktop/voice/stop"),
+ stop: () => invoke(IPC.invoke.voiceStop),
- cancel: () => ipcRenderer.invoke("pi-desktop/voice/cancel"),
+ cancel: () => invoke(IPC.invoke.voiceCancel),
- getState: () => ipcRenderer.invoke("pi-desktop/voice/getState"),
+ getState: () => invoke(IPC.invoke.voiceGetState),
- getDevices: () => ipcRenderer.invoke("pi-desktop/voice/getDevices"),
+ getDevices: () => invoke<unknown[]>(IPC.invoke.voiceGetDevices),
- getModels: () => ipcRenderer.invoke("pi-desktop/voice/getModels"),
+ getModels: () => invoke<unknown[]>(IPC.invoke.voiceGetModels),
downloadModel: (modelId: string) =>
- ipcRenderer.invoke("pi-desktop/voice/downloadModel", { modelId }),
+ invoke(IPC.invoke.voiceDownloadModel, { modelId }),
deleteModel: (modelId: string) =>
- ipcRenderer.invoke("pi-desktop/voice/deleteModel", { modelId }),
+ invoke(IPC.invoke.voiceDeleteModel, { modelId }),
updateSettings: (settings: Record<string, unknown>) =>
- ipcRenderer.invoke("pi-desktop/voice/updateSettings", settings),
+ invoke(IPC.invoke.voiceUpdateSettings, settings),
checkPermission: () =>
- ipcRenderer.invoke("pi-desktop/voice/checkPermission"),
+ invoke<string>(IPC.invoke.voiceCheckPermission),
requestPermission: () =>
- ipcRenderer.invoke("pi-desktop/voice/requestPermission"),
+ invoke<boolean>(IPC.invoke.voiceRequestPermission),
onStateChanged: (callback: (state: unknown) => void) => {
- const handler = (_event: unknown, state: unknown) => callback(state);
- ipcRenderer.on("pi-desktop/voice/event/stateChanged", handler);
- return () => {
- ipcRenderer.removeListener("pi-desktop/voice/event/stateChanged", handler);
- };
+ const bridge = window.piDesktop;
+ if (!bridge) return () => {};
+ return bridge.on(IPC.event.voiceStateChanged, callback);
},
onModelProgress: (callback: (data: { modelId: string; progress: number }) => void) => {
- const handler = (_event: unknown, data: { modelId: string; progress: number }) => callback(data);
- ipcRenderer.on("pi-desktop/voice/event/modelProgress", handler);
- return () => {
- ipcRenderer.removeListener("pi-desktop/voice/event/modelProgress", handler);
- };
+ const bridge = window.piDesktop;
+ if (!bridge) return () => {};
+ return bridge.on(IPC.event.voiceModelProgress, callback as (...args: unknown[]) => void);
},
};
diff --git a/packages/voice-runtime/src/model-downloader.ts b/packages/voice-runtime/src/model-downloader.ts
index 47be63b53..592a0e4f2 100644
--- a/packages/voice-runtime/src/model-downloader.ts
+++ b/packages/voice-runtime/src/model-downloader.ts
@@ -18,12 +18,14 @@ export async function* downloadModel(
// Dynamic import of @huggingface/hub
const { downloadFile } = await import("@huggingface/hub");
- const response = await downloadFile({
+ const blob = await downloadFile({
repo: info.hfRepo,
path: info.hfFilename,
- requestInit: signal ? { signal } : undefined,
});
+ // In Node the hub SDK returns a Response-like object, not a Blob.
+ const response = blob as unknown as Response;
+
if (!response || !response.body) {
throw new Error(`Failed to download model: no response body`);
}