Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix: add LLM download cancellation support and throttle progress updates to prevent UI flashing
Gabriel Steinbc673b605efd
diff --git a/preload.js b/preload.jsindex 3ab869748..02d02e6d9 100644--- a/preload.js+++ b/preload.js@@ -148,6 +148,7 @@ contextBridge.exposeInMainWorld("electronAPI", {modelDelete: (modelId) => ipcRenderer.invoke("model-delete", modelId),modelDeleteAll: () => ipcRenderer.invoke("model-delete-all"),modelCheckRuntime: () => ipcRenderer.invoke("model-check-runtime"),+ modelCancelDownload: (modelId) => ipcRenderer.invoke("model-cancel-download", modelId),onModelDownloadProgress: registerListener("model-download-progress"),// Anthropic APIdiff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex 431bf4a6c..ec9de4204 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -407,6 +407,19 @@ class IPCHandlers {}});+ ipcMain.handle("model-cancel-download", async (event, modelId) => {+ try {+ const modelManager = require("./modelManagerBridge").default;+ const cancelled = modelManager.cancelDownload(modelId);+ return { success: cancelled };+ } catch (error) {+ return {+ success: false,+ error: error.message,+ };+ }+ });+ipcMain.handle("model-check-runtime", async (event) => {try {const modelManager = require("./modelManagerBridge").default;diff --git a/src/helpers/modelManagerBridge.js b/src/helpers/modelManagerBridge.jsindex 83e651450..6ffcac405 100644--- a/src/helpers/modelManagerBridge.js+++ b/src/helpers/modelManagerBridge.js@@ -34,6 +34,7 @@ class ModelManager {this.modelsDir = this.getModelsDir();this.downloadProgress = new Map();this.activeDownloads = new Map();+ this.activeRequests = new Map(); // Track HTTP requests for cancellationthis.llamaCppPath = null;this.ensureModelsDirExists();}@@ -153,7 +154,7 @@ class ModelManager {if (onProgress) {onProgress(progress, downloadedSize, totalSize);}- });+ }, modelId);const stats = await fsPromises.stat(tempPath);if (stats.size < MIN_FILE_SIZE) {@@ -192,11 +193,13 @@ class ModelManager {return `${baseUrl}/${model.hfRepo}/resolve/main/${model.fileName}`;}- async downloadFile(url, destPath, onProgress) {+ async downloadFile(url, destPath, onProgress, modelId) {return new Promise((resolve, reject) => {const file = fs.createWriteStream(destPath);let downloadedSize = 0;let totalSize = 0;+ let lastProgressUpdate = 0;+ const PROGRESS_THROTTLE_MS = 100; // Throttle progress updates to prevent UI flashingconst cleanup = (callback) => {file.close(() => {@@ -207,7 +210,7 @@ class ModelManager {});};- https+ const request = https.get(url,{@@ -232,7 +235,7 @@ class ModelManager {return;}cleanup(() => {- this.downloadFile(redirectUrl, destPath, onProgress)+ this.downloadFile(redirectUrl, destPath, onProgress, modelId).then(resolve).catch(reject);});@@ -257,14 +260,33 @@ class ModelManager {response.on("data", (chunk) => {downloadedSize += chunk.length;- if (onProgress && totalSize > 0) {+ // Throttle progress updates to prevent UI flashing+ const now = Date.now();+ if (onProgress && totalSize > 0 && (now - lastProgressUpdate >= PROGRESS_THROTTLE_MS || downloadedSize >= totalSize)) {+ lastProgressUpdate = now;const progress = (downloadedSize / totalSize) * 100;onProgress(progress, downloadedSize, totalSize);}});pipeline(response, file, (error) => {+ // Clean up request tracking+ if (modelId) {+ this.activeRequests.delete(modelId);+ }+if (error) {+ // Check if this was a cancellation+ if (error.code === "ERR_STREAM_PREMATURE_CLOSE" && modelId && !this.activeDownloads.has(modelId)) {+ cleanup(() => {+ reject(+ new ModelError("Download cancelled by user", "DOWNLOAD_CANCELLED", {+ modelId,+ })+ );+ });+ return;+ }cleanup(() => {reject(new ModelError(`Download error: ${error.message}`, "DOWNLOAD_ERROR", {@@ -279,7 +301,21 @@ class ModelManager {}).on("error", (error) => {+ // Clean up request tracking+ if (modelId) {+ this.activeRequests.delete(modelId);+ }+cleanup(() => {+ // Check if this was a cancellation+ if (error.code === "ECONNRESET" && modelId && !this.activeDownloads.has(modelId)) {+ reject(+ new ModelError("Download cancelled by user", "DOWNLOAD_CANCELLED", {+ modelId,+ })+ );+ return;+ }reject(new ModelError(`Network error: ${error.message}`, "NETWORK_ERROR", {error: error.message,@@ -287,9 +323,35 @@ class ModelManager {);});});++ // Store the request for potential cancellation+ if (modelId) {+ this.activeRequests.set(modelId, { request, file, destPath });+ }});}+ cancelDownload(modelId) {+ const activeRequest = this.activeRequests.get(modelId);+ if (activeRequest) {+ const { request, file, destPath } = activeRequest;++ // Mark as no longer active before destroying+ this.activeDownloads.delete(modelId);+ this.activeRequests.delete(modelId);+ this.downloadProgress.delete(modelId);++ // Destroy the request and close the file+ request.destroy();+ file.close(() => {+ fsPromises.unlink(destPath).catch(() => {});+ });++ return true;+ }+ return false;+ }+async deleteModel(modelId) {const modelInfo = this.findModelById(modelId);if (!modelInfo) {diff --git a/src/hooks/useModelDownload.ts b/src/hooks/useModelDownload.tsindex 587bc7778..0399ab21d 100644--- a/src/hooks/useModelDownload.ts+++ b/src/hooks/useModelDownload.ts@@ -4,6 +4,8 @@ import { useToast } from "../components/ui/Toast";import type { WhisperDownloadProgressData } from "../types/electron";import "../types/electron";+const PROGRESS_THROTTLE_MS = 100; // Throttle UI updates to prevent flashing+export interface DownloadProgress {percentage: number;downloadedBytes: number;@@ -47,6 +49,7 @@ export function useModelDownload({});const [isCancelling, setIsCancelling] = useState(false);const isCancellingRef = useRef(false);+ const lastProgressUpdateRef = useRef(0);const { showAlertDialog } = useDialogs();const { toast } = useToast();@@ -87,6 +90,17 @@ export function useModelDownload({);const handleLLMProgress = useCallback((_event: unknown, data: LLMDownloadProgressData) => {+ // Skip if cancellation is in progress+ if (isCancellingRef.current) return;++ // Throttle UI updates to prevent flashing (server-side throttling is primary, this is backup)+ const now = Date.now();+ const isComplete = data.progress >= 100;+ if (!isComplete && now - lastProgressUpdateRef.current < PROGRESS_THROTTLE_MS) {+ return;+ }+ lastProgressUpdateRef.current = now;+setDownloadProgress({percentage: data.progress || 0,downloadedBytes: data.downloadedSize || 0,@@ -107,9 +121,19 @@ export function useModelDownload({const downloadModel = useCallback(async (modelId: string, onSelectAfterDownload?: (id: string) => void) => {+ // Prevent starting a new download if one is already in progress+ if (downloadingModel) {+ toast({+ title: "Download in Progress",+ description: "Please wait for the current download to complete or cancel it first.",+ });+ return;+ }+try {setDownloadingModel(modelId);setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });+ lastProgressUpdateRef.current = 0; // Reset throttle timerlet success = false;@@ -143,8 +167,15 @@ export function useModelDownload({onDownloadCompleteRef.current?.();} catch (error: unknown) {+ // Skip error display if cancellation is in progress+ if (isCancellingRef.current) return;+const errorMessage = error instanceof Error ? error.message : String(error);- if (!errorMessage.includes("interrupted by user")) {+ if (+ !errorMessage.includes("interrupted by user") &&+ !errorMessage.includes("cancelled by user") &&+ !errorMessage.includes("DOWNLOAD_CANCELLED")+ ) {showAlertDialog({title: "Download Failed",description: `Failed to download model: ${errorMessage}`,@@ -155,7 +186,7 @@ export function useModelDownload({setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });}},- [modelType, showAlertDialog]+ [downloadingModel, modelType, showAlertDialog, toast]);const deleteModel = useCallback(@@ -189,13 +220,15 @@ export function useModelDownload({);const cancelDownload = useCallback(async () => {- if (!downloadingModel) return;+ if (!downloadingModel || isCancelling) return;setIsCancelling(true);isCancellingRef.current = true;try {if (modelType === "whisper") {await window.electronAPI?.cancelWhisperDownload();+ } else {+ await window.electronAPI?.modelCancelDownload?.(downloadingModel);}toast({title: "Download Cancelled",@@ -210,7 +243,7 @@ export function useModelDownload({setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });onDownloadCompleteRef.current?.();}- }, [downloadingModel, modelType, toast]);+ }, [downloadingModel, isCancelling, modelType, toast]);const isDownloading = downloadingModel !== null;const isDownloadingModel = useCallback(diff --git a/src/types/electron.ts b/src/types/electron.tsindex c68d0a837..f39df893d 100644--- a/src/types/electron.ts+++ b/src/types/electron.ts@@ -189,6 +189,7 @@ declare global {modelDelete: (modelId: string) => Promise<void>;modelDeleteAll: () => Promise<{ success: boolean; error?: string; code?: string }>;modelCheckRuntime: () => Promise<boolean>;+ modelCancelDownload: (modelId: string) => Promise<{ success: boolean; error?: string }>;onModelDownloadProgress: (callback: (event: any, data: any) => void) => (() => void) | void;// Local reasoning