Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix: preserve local model download state

Dustin Persekbdecafdaf3c4
diff --git a/src/helpers/ModelManager.ts b/src/helpers/ModelManager.ts
index 20125e11f..5bd50cff0 100644
--- a/src/helpers/ModelManager.ts
+++ b/src/helpers/ModelManager.ts
@@ -272,12 +272,18 @@ class ModelManager {
const allModels = modelRegistry.getAllModels();
return Promise.all(
- allModels.map(async (model) => ({
- ...model,
- isDownloaded: await this.isModelDownloaded(model.id),
- downloadProgress: this.downloadProgress.get(model.id)?.progress || 0,
- isDownloading: this.activeDownloads.has(model.id),
- }))
+ allModels.map(async (model) => {
+ const progress = this.downloadProgress.get(model.id);
+
+ return {
+ ...model,
+ isDownloaded: await this.isModelDownloaded(model.id),
+ downloadProgress: progress?.progress || 0,
+ downloadedSize: progress?.downloadedSize || 0,
+ totalSize: progress?.totalSize || 0,
+ isDownloading: this.activeDownloads.has(model.id),
+ };
+ })
);
}
}
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 1076096fb..8a7b50658 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -2517,11 +2517,18 @@ class IPCHandlers {
});
ipcMain.handle("model-download", async (event, modelId) => {
+ let lastProgress = {
+ progress: 0,
+ downloadedSize: 0,
+ totalSize: 0,
+ };
+
try {
const modelManager = require("./modelManagerBridge").default;
const result = await modelManager.downloadModel(
modelId,
(progress, downloadedSize, totalSize) => {
+ lastProgress = { progress, downloadedSize, totalSize };
if (!event.sender.isDestroyed()) {
event.sender.send("model-download-progress", {
modelId,
@@ -2532,8 +2539,26 @@ class IPCHandlers {
}
}
);
+ if (!event.sender.isDestroyed()) {
+ event.sender.send("model-download-progress", {
+ type: "complete",
+ modelId,
+ progress: 100,
+ downloadedSize: lastProgress.downloadedSize,
+ totalSize: lastProgress.totalSize,
+ });
+ }
return { success: true, path: result };
} catch (error) {
+ if (error.code !== "DOWNLOAD_IN_PROGRESS" && !event.sender.isDestroyed()) {
+ event.sender.send("model-download-progress", {
+ type: "error",
+ modelId,
+ error: error.message,
+ code: error.code,
+ details: error.details,
+ });
+ }
return {
success: false,
error: error.message,
diff --git a/src/helpers/modelManagerBridge.js b/src/helpers/modelManagerBridge.js
index 5bd3e9d1d..778928580 100644
--- a/src/helpers/modelManagerBridge.js
+++ b/src/helpers/modelManagerBridge.js
@@ -108,12 +108,17 @@ class ModelManager {
for (const model of provider.models) {
const modelPath = path.join(this.modelsDir, model.fileName);
const isDownloaded = await this.checkModelValid(modelPath);
+ const progress = this.downloadProgress.get(model.id);
models.push({
...model,
providerId: provider.id,
providerName: provider.name,
isDownloaded,
+ isDownloading: this.activeDownloads.has(model.id),
+ downloadProgress: progress?.progress || 0,
+ downloadedSize: progress?.downloadedSize || 0,
+ totalSize: progress?.totalSize || 0,
path: isDownloaded ? modelPath : null,
});
}
diff --git a/src/hooks/useLocalModels.ts b/src/hooks/useLocalModels.ts
index cf39a904f..9358d1bce 100644
--- a/src/hooks/useLocalModels.ts
+++ b/src/hooks/useLocalModels.ts
@@ -1,5 +1,6 @@
import { useState, useEffect, useCallback } from "react";
import { ModelDefinition } from "../models/ModelRegistry";
+import type { LocalLLMDownloadProgressEvent } from "../types/electron";
import "../types/electron";
interface ModelWithStatus extends ModelDefinition {
@@ -8,18 +9,13 @@ interface ModelWithStatus extends ModelDefinition {
downloadProgress: number;
}
-interface LLMDownloadProgressEvent {
- modelId: string;
- progress: number;
- downloadedSize: number;
- totalSize: number;
-}
-
export function useLocalModels() {
const [models, setModels] = useState<ModelWithStatus[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
- const [progressMap, setProgressMap] = useState<Map<string, LLMDownloadProgressEvent>>(new Map());
+ const [progressMap, setProgressMap] = useState<Map<string, LocalLLMDownloadProgressEvent>>(
+ new Map()
+ );
const loadModels = useCallback(async () => {
try {
@@ -38,7 +34,28 @@ export function useLocalModels() {
useEffect(() => {
loadModels();
- const handleProgress = (_event: any, data: LLMDownloadProgressEvent) => {
+ const handleProgress = (_event: any, data: LocalLLMDownloadProgressEvent) => {
+ if (data.type === "complete") {
+ setProgressMap((prev) => {
+ const next = new Map(prev);
+ next.delete(data.modelId);
+ return next;
+ });
+ void loadModels();
+ return;
+ }
+
+ if (data.type === "error") {
+ setProgressMap((prev) => {
+ const next = new Map(prev);
+ next.delete(data.modelId);
+ return next;
+ });
+ setError(data.error || "Failed to download model");
+ void loadModels();
+ return;
+ }
+
setProgressMap((prev) => new Map(prev).set(data.modelId, data));
};
diff --git a/src/hooks/useModelDownload.ts b/src/hooks/useModelDownload.ts
index 9e4650eea..f27333c07 100644
--- a/src/hooks/useModelDownload.ts
+++ b/src/hooks/useModelDownload.ts
@@ -3,7 +3,11 @@ import type { TFunction } from "i18next";
import { useTranslation } from "react-i18next";
import { useDialogs } from "./useDialogs";
import { useToast } from "../components/ui/useToast";
-import type { WhisperDownloadProgressData } from "../types/electron";
+import type {
+ LocalLLMDownloadProgressEvent,
+ LocalLLMModelStatus,
+ WhisperDownloadProgressData,
+} from "../types/electron";
import "../types/electron";
const PROGRESS_THROTTLE_MS = 100;
@@ -24,13 +28,6 @@ interface UseModelDownloadOptions {
onModelsCleared?: () => void;
}
-interface LLMDownloadProgressData {
- modelId: string;
- progress: number;
- downloadedSize: number;
- totalSize: number;
-}
-
export function formatETA(seconds: number): string {
if (seconds < 60) return `${Math.round(seconds)}s`;
const minutes = Math.floor(seconds / 60);
@@ -74,6 +71,7 @@ export function useModelDownload({
const [downloadError, setDownloadError] = useState<string | null>(null);
const isCancellingRef = useRef(false);
const lastProgressUpdateRef = useRef(0);
+ const activeDownloadRequestRef = useRef<string | null>(null);
const { showAlertDialog } = useDialogs();
const { toast } = useToast();
@@ -99,6 +97,34 @@ export function useModelDownload({
return () => window.removeEventListener("openwhispr-models-cleared", handleModelsCleared);
}, []);
+ const hydrateActiveLLMDownload = useCallback(
+ async (preferredModelId?: string) => {
+ if (modelType !== "llm") return false;
+
+ let models: LocalLLMModelStatus[] | undefined;
+ try {
+ models = await window.electronAPI?.modelGetAll?.();
+ } catch {
+ return false;
+ }
+
+ const activeModel =
+ models?.find((model) => model.isDownloading && model.id === preferredModelId) ??
+ models?.find((model) => model.isDownloading);
+ if (!activeModel) return false;
+
+ setDownloadingModel(activeModel.id);
+ setDownloadError(null);
+ setDownloadProgress({
+ percentage: activeModel.downloadProgress || 0,
+ downloadedBytes: activeModel.downloadedSize || 0,
+ totalBytes: activeModel.totalSize || 0,
+ });
+ return true;
+ },
+ [modelType]
+ );
+
const handleWhisperProgress = useCallback(
(_event: unknown, data: WhisperDownloadProgressData) => {
if (data.type === "progress") {
@@ -139,22 +165,53 @@ export function useModelDownload({
[t]
);
- const handleLLMProgress = useCallback((_event: unknown, data: LLMDownloadProgressData) => {
- if (isCancellingRef.current) return;
+ const handleLLMProgress = useCallback(
+ (_event: unknown, data: LocalLLMDownloadProgressEvent) => {
+ if (isCancellingRef.current) return;
- const now = Date.now();
- const isComplete = data.progress >= 100;
- if (!isComplete && now - lastProgressUpdateRef.current < PROGRESS_THROTTLE_MS) {
- return;
- }
- lastProgressUpdateRef.current = now;
+ if (data.type === "complete") {
+ if (activeDownloadRequestRef.current === data.modelId) return;
+ setIsInstalling(false);
+ setDownloadingModel(null);
+ setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });
+ void onDownloadCompleteRef.current?.();
+ return;
+ }
- setDownloadProgress({
- percentage: data.progress || 0,
- downloadedBytes: data.downloadedSize || 0,
- totalBytes: data.totalSize || 0,
- });
- }, []);
+ if (data.type === "error") {
+ if (activeDownloadRequestRef.current === data.modelId) return;
+ const msg = getDownloadErrorMessage(
+ t,
+ data.error || t("hooks.modelDownload.errors.unknown"),
+ data.code
+ );
+ setDownloadError(msg);
+ showAlertDialogRef.current({
+ title: t("hooks.modelDownload.downloadFailed.title"),
+ description: msg,
+ });
+ setIsInstalling(false);
+ setDownloadingModel(null);
+ setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });
+ return;
+ }
+
+ const now = Date.now();
+ const isComplete = (data.progress || 0) >= 100;
+ if (!isComplete && now - lastProgressUpdateRef.current < PROGRESS_THROTTLE_MS) {
+ return;
+ }
+ lastProgressUpdateRef.current = now;
+
+ setDownloadingModel(data.modelId);
+ setDownloadProgress({
+ percentage: data.progress || 0,
+ downloadedBytes: data.downloadedSize || 0,
+ totalBytes: data.totalSize || 0,
+ });
+ },
+ [t]
+ );
useEffect(() => {
let dispose: (() => void) | undefined;
@@ -172,6 +229,10 @@ export function useModelDownload({
};
}, [handleWhisperProgress, handleLLMProgress, modelType]);
+ useEffect(() => {
+ void hydrateActiveLLMDownload();
+ }, [hydrateActiveLLMDownload]);
+
const downloadModel = useCallback(
async (modelId: string, onSelectAfterDownload?: (id: string) => void) => {
if (downloadingModel) {
@@ -182,11 +243,14 @@ export function useModelDownload({
return;
}
+ let keepActiveDownloadState = false;
+
try {
setDownloadingModel(modelId);
setDownloadError(null);
setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });
lastProgressUpdateRef.current = 0; // Reset throttle timer
+ activeDownloadRequestRef.current = modelId;
let success = false;
@@ -228,6 +292,19 @@ export function useModelDownload({
| { success: boolean; error?: string; code?: string }
| undefined;
if (result && !result.success && result.error) {
+ if (result.code === "DOWNLOAD_IN_PROGRESS") {
+ keepActiveDownloadState = true;
+ const hydrated = await hydrateActiveLLMDownload(modelId);
+ if (!hydrated) {
+ setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });
+ }
+ toast({
+ title: t("hooks.modelDownload.downloadInProgress.title"),
+ description: t("hooks.modelDownload.downloadInProgress.description"),
+ });
+ return;
+ }
+
const msg = getDownloadErrorMessage(t, result.error, result.code);
setDownloadError(msg);
showAlertDialog({
@@ -268,12 +345,14 @@ export function useModelDownload({
});
}
} finally {
+ activeDownloadRequestRef.current = null;
+ if (keepActiveDownloadState) return;
setIsInstalling(false);
setDownloadingModel(null);
setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });
}
},
- [downloadingModel, modelType, showAlertDialog, toast, t]
+ [downloadingModel, hydrateActiveLLMDownload, modelType, showAlertDialog, toast, t]
);
const deleteModel = useCallback(
diff --git a/src/types/electron.ts b/src/types/electron.ts
index ca1fabc90..3b7b55c0e 100644
--- a/src/types/electron.ts
+++ b/src/types/electron.ts
@@ -1,3 +1,5 @@
+import type { ModelDefinition } from "../models/ModelRegistry";
+
export type LocalTranscriptionProvider = "whisper" | "nvidia";
export type InferenceMode = "openwhispr" | "providers" | "local" | "self-hosted" | "enterprise";
@@ -465,6 +467,40 @@ export interface LlamaVulkanDownloadProgress {
percentage: number;
}
+export interface LocalLLMModelStatus extends ModelDefinition {
+ providerId?: string;
+ providerName?: string;
+ isDownloaded: boolean;
+ isDownloading: boolean;
+ downloadProgress: number;
+ downloadedSize: number;
+ totalSize: number;
+ path: string | null;
+}
+
+export type LocalLLMDownloadProgressEvent =
+ | {
+ type?: "progress";
+ modelId: string;
+ progress: number;
+ downloadedSize: number;
+ totalSize: number;
+ }
+ | {
+ type: "complete";
+ modelId: string;
+ progress: 100;
+ downloadedSize?: number;
+ totalSize?: number;
+ }
+ | {
+ type: "error";
+ modelId: string;
+ error: string;
+ code?: string;
+ details?: unknown;
+ };
+
export interface ConversationPreview {
id: number;
title: string;
@@ -823,7 +859,7 @@ declare global {
getParakeetDiagnostics: () => Promise<ParakeetDiagnosticsResult>;
// Local AI model management
- modelGetAll: () => Promise<any[]>;
+ modelGetAll: () => Promise<LocalLLMModelStatus[]>;
modelCheck: (modelId: string) => Promise<boolean>;
modelDownload: (modelId: string) => Promise<{
success: boolean;
@@ -851,7 +887,9 @@ declare global {
details?: string;
}>;
modelCancelDownload: (modelId: string) => Promise<{ success: boolean; error?: string }>;
- onModelDownloadProgress: (callback: (event: any, data: any) => void) => () => void;
+ onModelDownloadProgress: (
+ callback: (event: any, data: LocalLLMDownloadProgressEvent) => void
+ ) => () => void;
// Local reasoning
processLocalReasoning: (
diff --git a/test/helpers/modelManagerBridgeDownloadStatus.test.js b/test/helpers/modelManagerBridgeDownloadStatus.test.js
new file mode 100644
index 000000000..101f94be4
--- /dev/null
+++ b/test/helpers/modelManagerBridgeDownloadStatus.test.js
@@ -0,0 +1,68 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const Module = require("node:module");
+const fs = require("node:fs/promises");
+const os = require("node:os");
+const path = require("node:path");
+
+const modelRegistryData = require("../../src/models/modelRegistryData.json");
+
+const originalLoad = Module._load;
+const modelManagerModulePath = require.resolve("../../src/helpers/modelManagerBridge.js");
+let electronHome = os.tmpdir();
+
+function loadModelManager() {
+ delete require.cache[modelManagerModulePath];
+
+ Module._load = function loadWithMocks(request, parent, isMain) {
+ if (request === "electron") {
+ return {
+ app: {
+ isReady: () => true,
+ getAppPath: () => process.cwd(),
+ getPath: (name) => (name === "home" ? electronHome : path.join(electronHome, name)),
+ },
+ net: {},
+ };
+ }
+
+ return originalLoad.call(this, request, parent, isMain);
+ };
+
+ try {
+ return require("../../src/helpers/modelManagerBridge.js").default;
+ } finally {
+ Module._load = originalLoad;
+ }
+}
+
+test("getAllModels surfaces in-flight local model download state", async (t) => {
+ const tmpHome = await fs.mkdtemp(path.join(os.tmpdir(), "openwhispr-model-status-"));
+ electronHome = tmpHome;
+ t.after(() => fs.rm(tmpHome, { recursive: true, force: true }));
+
+ const modelManager = loadModelManager();
+ const model = modelRegistryData.localProviders[0].models[0];
+
+ modelManager.activeDownloads.set(model.id, true);
+ modelManager.downloadProgress.set(model.id, {
+ modelId: model.id,
+ progress: 42,
+ downloadedSize: 420,
+ totalSize: 1000,
+ });
+ t.after(() => {
+ modelManager.activeDownloads.clear();
+ modelManager.downloadProgress.clear();
+ });
+
+ const models = await modelManager.getAllModels();
+ const activeModel = models.find((candidate) => candidate.id === model.id);
+
+ assert.equal(activeModel.isDownloaded, false);
+ assert.equal(activeModel.isDownloading, true);
+ assert.equal(activeModel.downloadProgress, 42);
+ assert.equal(activeModel.downloadedSize, 420);
+ assert.equal(activeModel.totalSize, 1000);
+ assert.equal(activeModel.path, null);
+});