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
feat: add proactive token refresh and keyterms support for Deepgram streaming
Gabriel Stein00185ffe66ea
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.jsindex ee52031e2..bc2b901c3 100644--- a/src/helpers/audioManager.js+++ b/src/helpers/audioManager.js@@ -1950,6 +1950,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);const res = await window.electronAPI.deepgramStreamingStart({sampleRate: 16000,language: getBaseLanguageCode(localStorage.getItem("preferredLanguage")),+ keyterms: this.getKeyterms(),});if (!res.success) {diff --git a/src/helpers/deepgramStreaming.js b/src/helpers/deepgramStreaming.jsindex c6494acd8..8919a7980 100644--- a/src/helpers/deepgramStreaming.js+++ b/src/helpers/deepgramStreaming.js@@ -42,6 +42,12 @@ class DeepgramStreaming {this.isDisconnecting = false;this.coldStartBuffer = [];this.coldStartBufferSize = 0;+ this.tokenRefreshFn = null;+ this.proactiveRefreshTimer = null;+ }++ setTokenRefreshFn(fn) {+ this.tokenRefreshFn = fn;}buildWebSocketUrl(options) {@@ -51,15 +57,17 @@ class DeepgramStreaming {sample_rate: String(sampleRate),channels: "1",model: "nova-3",- smart_format: "true",punctuate: "true",interim_results: "true",- utterance_end_ms: "1000",- vad_events: "true",});if (options.language && options.language !== "auto") {params.set("language", options.language);}+ if (Array.isArray(options.keyterms)) {+ for (const term of options.keyterms) {+ if (term) params.append("keyterm", term);+ }+ }return `wss://api.deepgram.com/v1/listen?${params.toString()}`;}@@ -158,6 +166,7 @@ class DeepgramStreaming {// Ignore}this.startKeepAlive();+ this.scheduleProactiveRefresh();debugLogger.debug("Deepgram connection warmed up", {sessionId: this.warmSessionId,via: meta.via || "unknown",@@ -235,9 +244,8 @@ class DeepgramStreaming {if (this.isConnected) {return;}- const token = this.getCachedToken();- if (!token || !this.warmConnectionOptions) {- debugLogger.debug("Deepgram cannot re-warm: no valid token or options");+ if (!this.warmConnectionOptions) {+ debugLogger.debug("Deepgram cannot re-warm: no saved options");return;}@@ -248,15 +256,63 @@ class DeepgramStreaming {delayMs: delay,});clearTimeout(this.rewarmTimer);- this.rewarmTimer = setTimeout(() => {+ this.rewarmTimer = setTimeout(async () => {this.rewarmTimer = null;if (this.hasWarmConnection() || this.isConnected) return;++ let token = this.getCachedToken();+ if (!token && this.tokenRefreshFn) {+ try {+ token = await this.tokenRefreshFn();+ if (token) this.cacheToken(token);+ } catch (err) {+ debugLogger.debug("Deepgram token refresh for re-warm failed", {+ error: err.message,+ });+ }+ }+ if (!token) {+ debugLogger.debug("Deepgram cannot re-warm: no valid token");+ return;+ }+this.warmup({ ...this.warmConnectionOptions, token }).catch((err) => {debugLogger.debug("Deepgram auto re-warm failed", { error: err.message });});}, delay);}+ scheduleProactiveRefresh() {+ clearTimeout(this.proactiveRefreshTimer);+ const refreshDelay = TOKEN_EXPIRY_MS - TOKEN_REFRESH_BUFFER_MS * 2;+ this.proactiveRefreshTimer = setTimeout(async () => {+ this.proactiveRefreshTimer = null;+ if (!this.hasWarmConnection() || this.isConnected) return;++ const savedOptions = this.warmConnectionOptions ? { ...this.warmConnectionOptions } : null;+ if (!savedOptions || !this.tokenRefreshFn) return;++ let newToken;+ try {+ newToken = await this.tokenRefreshFn();+ } catch (err) {+ debugLogger.debug("Deepgram proactive token refresh failed", {+ error: err.message,+ });+ return;+ }+ if (!newToken || this.isConnected) return;++ debugLogger.debug("Deepgram rotating warm connection with fresh token");+ this.cleanupWarmConnection();+ this.cacheToken(newToken);+ this.rewarmAttempts = 0;+ this.warmup({ ...savedOptions, token: newToken }).catch((err) => {+ debugLogger.debug("Deepgram proactive re-warm failed", { error: err.message });+ });+ }, refreshDelay);+ }+useWarmConnection() {if (!this.warmConnection || !this.warmConnectionReady) {return false;@@ -271,6 +327,8 @@ class DeepgramStreaming {}this.stopKeepAlive();+ clearTimeout(this.proactiveRefreshTimer);+ this.proactiveRefreshTimer = null;this.ws = this.warmConnection;this.isConnected = true;@@ -315,6 +373,8 @@ class DeepgramStreaming {cleanupWarmConnection() {this.stopKeepAlive();+ clearTimeout(this.proactiveRefreshTimer);+ this.proactiveRefreshTimer = null;if (this.warmConnection) {try {this.warmConnection.close();@@ -599,6 +659,8 @@ class DeepgramStreaming {this.cleanupWarmConnection();clearTimeout(this.rewarmTimer);this.rewarmTimer = null;+ clearTimeout(this.proactiveRefreshTimer);+ this.proactiveRefreshTimer = null;this.cachedToken = null;this.tokenFetchedAt = null;this.warmConnectionOptions = null;diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex c8c5adc06..6f48eaabc 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -1150,10 +1150,7 @@ class IPCHandlers {runtimeEnv.VITE_NEON_AUTH_URL ||"";- const getSessionCookies = async (event) => {- const win = BrowserWindow.fromWebContents(event.sender);- if (!win) return "";-+ const getSessionCookiesFromWindow = async (win) => {const scopedUrls = [getAuthUrl(), getApiUrl()].filter(Boolean);const cookiesByName = new Map();@@ -1199,6 +1196,12 @@ class IPCHandlers {return cookieHeader;};+ const getSessionCookies = async (event) => {+ const win = BrowserWindow.fromWebContents(event.sender);+ if (!win) return "";+ return getSessionCookiesFromWindow(win);+ };+ipcMain.handle("cloud-transcribe", async (event, audioBuffer, opts = {}) => {try {const apiUrl = getApiUrl();@@ -1797,6 +1800,37 @@ class IPCHandlers {// --- Deepgram Streaming handlers ---+ let deepgramTokenWindowId = null;++ const fetchDeepgramStreamingTokenFromWindow = async (windowId) => {+ const apiUrl = getApiUrl();+ if (!apiUrl) throw new Error("OpenWhispr API URL not configured");++ const win = BrowserWindow.fromId(windowId);+ if (!win || win.isDestroyed()) throw new Error("Window not available for token refresh");++ const cookieHeader = await getSessionCookiesFromWindow(win);+ if (!cookieHeader) throw new Error("No session cookies available");++ const tokenResponse = await fetch(`${apiUrl}/api/deepgram-streaming-token`, {+ method: "POST",+ headers: { Cookie: cookieHeader },+ });++ if (!tokenResponse.ok) {+ if (tokenResponse.status === 401) {+ const err = new Error("Session expired");+ err.code = "AUTH_EXPIRED";+ throw err;+ }+ throw new Error(`Failed to get Deepgram streaming token: ${tokenResponse.status}`);+ }++ const { token } = await tokenResponse.json();+ if (!token) throw new Error("No token received from API");+ return token;+ };+const fetchDeepgramStreamingToken = async (event) => {const apiUrl = getApiUrl();if (!apiUrl) {@@ -1842,10 +1876,20 @@ class IPCHandlers {return { success: false, error: "API not configured", code: "NO_API" };}+ const win = BrowserWindow.fromWebContents(event.sender);+ if (win && !win.isDestroyed()) {+ deepgramTokenWindowId = win.id;+ }+if (!this.deepgramStreaming) {this.deepgramStreaming = new DeepgramStreaming();}+ this.deepgramStreaming.setTokenRefreshFn(async () => {+ if (!deepgramTokenWindowId) throw new Error("No window reference");+ return fetchDeepgramStreamingTokenFromWindow(deepgramTokenWindowId);+ });+if (this.deepgramStreaming.hasWarmConnection()) {debugLogger.debug("Deepgram connection already warm", {}, "streaming");return { success: true, alreadyWarm: true };@@ -1890,11 +1934,19 @@ class IPCHandlers {}const win = BrowserWindow.fromWebContents(event.sender);+ if (win && !win.isDestroyed()) {+ deepgramTokenWindowId = win.id;+ }if (!this.deepgramStreaming) {this.deepgramStreaming = new DeepgramStreaming();}+ this.deepgramStreaming.setTokenRefreshFn(async () => {+ if (!deepgramTokenWindowId) throw new Error("No window reference");+ return fetchDeepgramStreamingTokenFromWindow(deepgramTokenWindowId);+ });+if (this.deepgramStreaming.isConnected) {debugLogger.debug("Deepgram cleaning up stale connection before start", {}, "streaming");await this.deepgramStreaming.disconnect(false);