Skip to content

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

refactor: consolidate API key management and remove dead code

Gabriel Stein1b40e389ba1b
diff --git a/preload.js b/preload.js
index 02d02e6d9..b3ba8df8d 100644
--- a/preload.js
+++ b/preload.js
@@ -70,9 +70,6 @@ contextBridge.exposeInMainWorld("electronAPI", {
createProductionEnvFile: (key) =>
ipcRenderer.invoke("create-production-env-file", key),
- // Settings management
- saveSettings: (settings) => ipcRenderer.invoke("save-settings", settings),
-
// Clipboard functions
readClipboard: () => ipcRenderer.invoke("read-clipboard"),
writeClipboard: (text) => ipcRenderer.invoke("write-clipboard", text),
diff --git a/src/components/TranscriptionModelPicker.tsx b/src/components/TranscriptionModelPicker.tsx
index e18b1dbac..0bd528c4c 100644
--- a/src/components/TranscriptionModelPicker.tsx
+++ b/src/components/TranscriptionModelPicker.tsx
@@ -493,7 +493,9 @@ export default function TranscriptionModelPicker({
{selectedCloudProvider === "custom" ? (
<div className="space-y-4">
<div className="space-y-3">
- <h4 className="text-sm font-medium text-gray-700">Custom Endpoint Configuration</h4>
+ <h4 className="text-sm font-medium text-gray-700">
+ Custom Endpoint Configuration
+ </h4>
<p className="text-xs text-gray-500">
Enter an OpenAI-compatible transcription endpoint URL.
</p>
@@ -558,7 +560,9 @@ export default function TranscriptionModelPicker({
<h4 className="font-medium text-gray-900">API Configuration</h4>
<ApiKeyInput
apiKey={selectedCloudProvider === "groq" ? groqApiKey : openaiApiKey}
- setApiKey={selectedCloudProvider === "groq" ? setGroqApiKey : setOpenaiApiKey}
+ setApiKey={
+ selectedCloudProvider === "groq" ? setGroqApiKey : setOpenaiApiKey
+ }
helpText={
selectedCloudProvider === "groq" ? (
<>
diff --git a/src/helpers/environment.js b/src/helpers/environment.js
index d6ffb6c12..3bfbe2276 100644
--- a/src/helpers/environment.js
+++ b/src/helpers/environment.js
@@ -20,14 +20,11 @@ class EnvironmentManager {
path.join(process.resourcesPath, "app", ".env"),
];
- let envLoaded = false;
-
for (const envPath of possibleEnvPaths) {
try {
if (fs.existsSync(envPath)) {
const result = require("dotenv").config({ path: envPath });
if (!result.error) {
- envLoaded = true;
break;
}
}
@@ -37,56 +34,48 @@ class EnvironmentManager {
}
}
- getOpenAIKey() {
- const apiKey = process.env.OPENAI_API_KEY;
- return apiKey || "";
+ _getKey(envVarName) {
+ return process.env[envVarName] || "";
}
- saveOpenAIKey(key) {
+ _saveKey(envVarName, key) {
// Update the environment variable in memory for immediate use
- process.env.OPENAI_API_KEY = key;
+ process.env[envVarName] = key;
// Persist all keys to file
this.saveAllKeysToEnvFile();
return { success: true };
}
+ getOpenAIKey() {
+ return this._getKey("OPENAI_API_KEY");
+ }
+
+ saveOpenAIKey(key) {
+ return this._saveKey("OPENAI_API_KEY", key);
+ }
+
getAnthropicKey() {
- const apiKey = process.env.ANTHROPIC_API_KEY;
- return apiKey || "";
+ return this._getKey("ANTHROPIC_API_KEY");
}
saveAnthropicKey(key) {
- // Update the environment variable in memory for immediate use
- process.env.ANTHROPIC_API_KEY = key;
- // Persist all keys to file
- this.saveAllKeysToEnvFile();
- return { success: true };
+ return this._saveKey("ANTHROPIC_API_KEY", key);
}
getGeminiKey() {
- const apiKey = process.env.GEMINI_API_KEY;
- return apiKey || "";
+ return this._getKey("GEMINI_API_KEY");
}
saveGeminiKey(key) {
- // Update the environment variable in memory for immediate use
- process.env.GEMINI_API_KEY = key;
- // Persist all keys to file
- this.saveAllKeysToEnvFile();
- return { success: true };
+ return this._saveKey("GEMINI_API_KEY", key);
}
getGroqKey() {
- const apiKey = process.env.GROQ_API_KEY;
- return apiKey || "";
+ return this._getKey("GROQ_API_KEY");
}
saveGroqKey(key) {
- // Update the environment variable in memory for immediate use
- process.env.GROQ_API_KEY = key;
- // Persist all keys to file
- this.saveAllKeysToEnvFile();
- return { success: true };
+ return this._saveKey("GROQ_API_KEY", key);
}
createProductionEnvFile(apiKey) {
@@ -105,36 +94,32 @@ OPENAI_API_KEY=${apiKey}
}
saveAllKeysToEnvFile() {
- try {
- const envPath = path.join(app.getPath("userData"), ".env");
+ const envPath = path.join(app.getPath("userData"), ".env");
- // Build env content with all current keys
- let envContent = `# OpenWhispr Environment Variables
+ // Build env content with all current keys
+ let envContent = `# OpenWhispr Environment Variables
# This file was created automatically for production use
`;
- if (process.env.OPENAI_API_KEY) {
- envContent += `OPENAI_API_KEY=${process.env.OPENAI_API_KEY}\n`;
- }
- if (process.env.ANTHROPIC_API_KEY) {
- envContent += `ANTHROPIC_API_KEY=${process.env.ANTHROPIC_API_KEY}\n`;
- }
- if (process.env.GEMINI_API_KEY) {
- envContent += `GEMINI_API_KEY=${process.env.GEMINI_API_KEY}\n`;
- }
- if (process.env.GROQ_API_KEY) {
- envContent += `GROQ_API_KEY=${process.env.GROQ_API_KEY}\n`;
- }
+ if (process.env.OPENAI_API_KEY) {
+ envContent += `OPENAI_API_KEY=${process.env.OPENAI_API_KEY}\n`;
+ }
+ if (process.env.ANTHROPIC_API_KEY) {
+ envContent += `ANTHROPIC_API_KEY=${process.env.ANTHROPIC_API_KEY}\n`;
+ }
+ if (process.env.GEMINI_API_KEY) {
+ envContent += `GEMINI_API_KEY=${process.env.GEMINI_API_KEY}\n`;
+ }
+ if (process.env.GROQ_API_KEY) {
+ envContent += `GROQ_API_KEY=${process.env.GROQ_API_KEY}\n`;
+ }
- fs.writeFileSync(envPath, envContent, "utf8");
+ fs.writeFileSync(envPath, envContent, "utf8");
- // Reload the env file
- require("dotenv").config({ path: envPath });
+ // Reload the env file
+ require("dotenv").config({ path: envPath });
- return { success: true, path: envPath };
- } catch (error) {
- throw error;
- }
+ return { success: true, path: envPath };
}
}
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index ec9de4204..ea89a36c2 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -79,19 +79,6 @@ class IPCHandlers {
return this.environmentManager.createProductionEnvFile(apiKey);
});
- ipcMain.handle("save-settings", async (event, settings) => {
- try {
- // Save settings to environment and localStorage
- if (settings.apiKey) {
- await this.environmentManager.saveOpenAIKey(settings.apiKey);
- }
- return { success: true };
- } catch (error) {
- debugLogger.error("Failed to save settings:", error);
- return { success: false, error: error.message };
- }
- });
-
ipcMain.handle("db-save-transcription", async (event, text) => {
const result = this.databaseManager.saveTranscription(text);
if (result?.success && result?.transcription) {
diff --git a/src/types/electron.ts b/src/types/electron.ts
index b14d5d9ad..e3ee923a3 100644
--- a/src/types/electron.ts
+++ b/src/types/electron.ts
@@ -103,14 +103,6 @@ export interface PasteToolsResult {
recommendedInstall?: string;
}
-// Additional interface missing from preload.js
-export interface SaveSettings {
- useLocalWhisper: boolean;
- apiKey: string;
- whisperModel: string;
- hotkey: string;
-}
-
declare global {
interface Window {
electronAPI: {
@@ -248,9 +240,6 @@ declare global {
) => (() => void) | void;
onUpdateError: (callback: (event: any, error: any) => void) => (() => void) | void;
- // Settings management (used by OnboardingFlow but not in preload.js)
- saveSettings?: (settings: SaveSettings) => Promise<void>;
-
// External URL operations
openExternal: (url: string) => Promise<{ success: boolean; error?: string } | void>;