Skip to content

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

feat: added ability to select and download OSS rasoning models from hugging face using llamaCpp

Gabriel Steinffc5362feb66
diff --git a/main.js b/main.js
index 86d3936d3..1ca8d8ff5 100644
--- a/main.js
+++ b/main.js
@@ -16,7 +16,7 @@ process.on("unhandledRejection", (reason, promise) => {
});
// Import helper modules
-const debugLogger = require("./src/helpers/debugLogger");
+const DebugLogger = require("./src/helpers/debugLogger");
const EnvironmentManager = require("./src/helpers/environment");
const WindowManager = require("./src/helpers/windowManager");
const DatabaseManager = require("./src/helpers/database");
@@ -123,8 +123,8 @@ async function startApp() {
// App event handlers
app.whenReady().then(() => {
// Only log debug status in development
- if (debugLogger.isEnabled() && process.env.NODE_ENV === 'development') {
- console.log(`Debug logging enabled: ${debugLogger.getLogPath()}`);
+ if (DebugLogger.isEnabled() && process.env.NODE_ENV === 'development') {
+ console.log(`Debug logging enabled: ${DebugLogger.getLogPath()}`);
}
startApp();
diff --git a/package.json b/package.json
index 3e252e8ba..a37205657 100644
--- a/package.json
+++ b/package.json
@@ -55,7 +55,11 @@
"dependencies": {
"@radix-ui/react-dialog": "^1.1.14",
"@radix-ui/react-dropdown-menu": "^2.1.15",
+ "@radix-ui/react-label": "^2.1.7",
+ "@radix-ui/react-progress": "^1.1.7",
+ "@radix-ui/react-select": "^2.2.5",
"@radix-ui/react-slot": "^1.2.3",
+ "@radix-ui/react-tabs": "^1.1.12",
"better-sqlite3": "^11.10.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
@@ -67,6 +71,8 @@
"react-dom": "^19.1.0",
"shadcn-ui": "^0.9.5",
"tailwind-merge": "^3.3.1",
- "tw-animate-css": "^1.3.4"
+ "tar": "^7.4.3",
+ "tw-animate-css": "^1.3.4",
+ "unzipper": "^0.12.3"
}
}
diff --git a/preload.js b/preload.js
index f01016c90..16090d099 100644
--- a/preload.js
+++ b/preload.js
@@ -92,6 +92,29 @@ contextBridge.exposeInMainWorld("electronAPI", {
// External link opener
openExternal: (url) => ipcRenderer.invoke("open-external", url),
+ // Model management functions
+ modelGetAll: () => ipcRenderer.invoke("model-get-all"),
+ modelCheck: (modelId) => ipcRenderer.invoke("model-check", modelId),
+ modelDownload: (modelId) => ipcRenderer.invoke("model-download", modelId),
+ modelDelete: (modelId) => ipcRenderer.invoke("model-delete", modelId),
+ modelCheckRuntime: () => ipcRenderer.invoke("model-check-runtime"),
+ onModelDownloadProgress: (callback) => ipcRenderer.on("model-download-progress", callback),
+
+ // Anthropic API
+ getAnthropicKey: () => ipcRenderer.invoke("get-anthropic-key"),
+ saveAnthropicKey: (key) => ipcRenderer.invoke("save-anthropic-key", key),
+
+ // Local reasoning
+ processLocalReasoning: (text, modelId, agentName, config) =>
+ ipcRenderer.invoke("process-local-reasoning", text, modelId, agentName, config),
+ checkLocalReasoningAvailable: () =>
+ ipcRenderer.invoke("check-local-reasoning-available"),
+
+ // llama.cpp
+ llamaCppCheck: () => ipcRenderer.invoke("llama-cpp-check"),
+ llamaCppInstall: () => ipcRenderer.invoke("llama-cpp-install"),
+ llamaCppUninstall: () => ipcRenderer.invoke("llama-cpp-uninstall"),
+
// Remove all listeners for a channel
removeAllListeners: (channel) => {
ipcRenderer.removeAllListeners(channel);
diff --git a/src/components/AIModelSelector.tsx b/src/components/AIModelSelector.tsx
new file mode 100644
index 000000000..ec4272587
--- /dev/null
+++ b/src/components/AIModelSelector.tsx
@@ -0,0 +1,211 @@
+import React, { useState, useEffect } from 'react';
+import { Button } from './ui/button';
+import { Input } from './ui/input';
+import { Select } from './ui/select';
+import { LocalModelManager } from './LocalModelManager';
+import ApiKeyInput from './ui/ApiKeyInput';
+import { AI_MODES, detectModeFromModel } from '../config/aiProvidersConfig';
+import { Cloud, HardDrive } from 'lucide-react';
+
+interface AIModelSelectorProps {
+ currentModel: string;
+ onModelChange: (model: string) => void;
+ onProviderChange: (provider: string) => void;
+ openaiApiKey: string;
+ setOpenaiApiKey: (key: string) => void;
+ anthropicApiKey: string;
+ setAnthropicApiKey: (key: string) => void;
+ pasteFromClipboard: (setter: (value: string) => void) => void;
+ showAlertDialog: (dialog: { title: string; description: string }) => void;
+}
+
+export default function AIModelSelector({
+ currentModel,
+ onModelChange,
+ onProviderChange,
+ openaiApiKey,
+ setOpenaiApiKey,
+ anthropicApiKey,
+ setAnthropicApiKey,
+ pasteFromClipboard,
+ showAlertDialog,
+}: AIModelSelectorProps) {
+ // Detect current mode and provider from the selected model
+ const detected = detectModeFromModel(currentModel);
+ const [selectedMode, setSelectedMode] = useState<'cloud' | 'local'>(detected?.mode || 'cloud');
+ const [selectedProvider, setSelectedProvider] = useState(detected?.providerId || 'openai');
+
+ // Get available providers for the selected mode
+ const currentMode = AI_MODES.find(mode => mode.id === selectedMode);
+ const currentProvider = currentMode?.providers.find(p => p.id === selectedProvider);
+
+ useEffect(() => {
+ // Update parent when provider changes
+ onProviderChange(selectedProvider);
+ }, [selectedProvider, onProviderChange]);
+
+ const handleModeChange = (newMode: 'cloud' | 'local') => {
+ setSelectedMode(newMode);
+
+ // Select first provider in the new mode
+ const mode = AI_MODES.find(m => m.id === newMode);
+ if (mode && mode.providers.length > 0) {
+ const firstProvider = mode.providers[0];
+ setSelectedProvider(firstProvider.id);
+
+ // Select first model in the provider
+ if (firstProvider.models.length > 0) {
+ onModelChange(firstProvider.models[0].value);
+ }
+ }
+ };
+
+ const handleProviderChange = (newProvider: string) => {
+ setSelectedProvider(newProvider);
+
+ // Select first model in the new provider
+ const provider = currentMode?.providers.find(p => p.id === newProvider);
+ if (provider && provider.models.length > 0) {
+ onModelChange(provider.models[0].value);
+ }
+ };
+
+ return (
+ <div className="space-y-4">
+ {/* Cloud vs Local Selection */}
+ <div className="space-y-3 p-4 bg-gray-50 border border-gray-200 rounded-xl">
+ <h4 className="font-medium text-gray-900">AI Mode</h4>
+ <div className="grid grid-cols-2 gap-3">
+ <button
+ onClick={() => handleModeChange('cloud')}
+ className={`flex items-center justify-center gap-2 p-3 rounded-lg border-2 transition-all ${
+ selectedMode === 'cloud'
+ ? 'border-blue-500 bg-blue-50 text-blue-700'
+ : 'border-gray-200 bg-white text-gray-600 hover:border-gray-300'
+ }`}
+ >
+ <Cloud size={20} />
+ <span className="font-medium">Cloud AI</span>
+ </button>
+ <button
+ onClick={() => handleModeChange('local')}
+ className={`flex items-center justify-center gap-2 p-3 rounded-lg border-2 transition-all ${
+ selectedMode === 'local'
+ ? 'border-purple-500 bg-purple-50 text-purple-700'
+ : 'border-gray-200 bg-white text-gray-600 hover:border-gray-300'
+ }`}
+ >
+ <HardDrive size={20} />
+ <span className="font-medium">Local AI</span>
+ </button>
+ </div>
+ <p className="text-xs text-gray-600">
+ {selectedMode === 'cloud'
+ ? 'Fast and powerful, requires internet and API keys'
+ : 'Private and offline, runs on your device'}
+ </p>
+ </div>
+
+ {/* Provider Selection */}
+ <div className="space-y-3 p-4 bg-blue-50 border border-blue-200 rounded-xl">
+ <h4 className="font-medium text-blue-900">AI Provider</h4>
+ <select
+ value={selectedProvider}
+ onChange={(e) => handleProviderChange(e.target.value)}
+ className="w-full text-sm border border-blue-300 rounded-md p-2 bg-white focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
+ >
+ {currentMode?.providers.map(provider => (
+ <option key={provider.id} value={provider.id}>
+ {provider.name}
+ </option>
+ ))}
+ </select>
+ </div>
+
+ {/* Model Selection */}
+ {selectedMode === 'cloud' ? (
+ <div className="space-y-3 p-4 bg-indigo-50 border border-indigo-200 rounded-xl">
+ <h4 className="font-medium text-indigo-900">AI Model</h4>
+ <select
+ value={currentModel}
+ onChange={(e) => onModelChange(e.target.value)}
+ className="w-full text-sm border border-indigo-300 rounded-md p-2 bg-white focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
+ >
+ {currentProvider?.models.map(model => (
+ <option key={model.value} value={model.value}>
+ {model.label} - {model.description}
+ </option>
+ ))}
+ </select>
+ <p className="text-xs text-indigo-600">
+ Different models offer varying levels of quality and speed
+ </p>
+ </div>
+ ) : (
+ <div className="space-y-3">
+ <h4 className="font-medium text-purple-900 px-4">Available Models</h4>
+ {/* For local models, we'll show the current provider's models info */}
+ <div className="bg-purple-50 border border-purple-200 rounded-xl p-4">
+ <p className="text-sm text-purple-800 mb-3">
+ {selectedProvider === 'qwen' && 'Qwen models offer excellent multilingual support and reasoning capabilities.'}
+ {selectedProvider === 'mistral' && 'Mistral models are known for their efficiency and strong performance.'}
+ {selectedProvider === 'llama' && 'Meta Llama models provide state-of-the-art open-source AI capabilities.'}
+ </p>
+ <p className="text-xs text-purple-600">
+ Download and manage models below
+ </p>
+ </div>
+ </div>
+ )}
+
+ {/* API Key Input for Cloud Providers */}
+ {selectedMode === 'cloud' && selectedProvider === 'openai' && (
+ <div className="space-y-3 p-4 bg-blue-50 border border-blue-200 rounded-xl">
+ <h4 className="font-medium text-blue-900">OpenAI API Key</h4>
+ <ApiKeyInput
+ apiKey={openaiApiKey}
+ setApiKey={setOpenaiApiKey}
+ helpText="Get your API key from platform.openai.com"
+ />
+ </div>
+ )}
+
+ {selectedMode === 'cloud' && selectedProvider === 'anthropic' && (
+ <div className="space-y-3 p-4 bg-purple-50 border border-purple-200 rounded-xl">
+ <h4 className="font-medium text-purple-900">Anthropic API Key</h4>
+ <div className="flex gap-2">
+ <Input
+ type="password"
+ placeholder="sk-ant-..."
+ value={anthropicApiKey}
+ onChange={(e) => setAnthropicApiKey(e.target.value)}
+ className="flex-1 text-sm border-purple-300 focus:border-purple-500"
+ />
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => pasteFromClipboard(setAnthropicApiKey)}
+ className="border-purple-300 text-purple-700 hover:bg-purple-50"
+ >
+ Paste
+ </Button>
+ </div>
+ <p className="text-xs text-purple-600">
+ Get your API key from console.anthropic.com
+ </p>
+ </div>
+ )}
+
+ {/* Local Model Manager */}
+ {selectedMode === 'local' && (
+ <div className="space-y-4">
+ <LocalModelManager
+ filterProvider={selectedProvider}
+ onModelSelect={onModelChange}
+ selectedModel={currentModel}
+ />
+ </div>
+ )}
+ </div>
+ );
+}
\ No newline at end of file
diff --git a/src/components/LocalModelManager.tsx b/src/components/LocalModelManager.tsx
new file mode 100644
index 000000000..418679cb1
--- /dev/null
+++ b/src/components/LocalModelManager.tsx
@@ -0,0 +1,16 @@
+import React from "react";
+import UnifiedModelPicker from "./UnifiedModelPicker";
+import { useSettings } from "../hooks/useSettings";
+
+export function LocalModelManager() {
+ const { reasoningModel, setReasoningModel } = useSettings();
+
+ return (
+ <UnifiedModelPicker
+ selectedModel={reasoningModel}
+ onModelSelect={setReasoningModel}
+ modelType="llm"
+ variant="settings"
+ />
+ );
+}
\ No newline at end of file
diff --git a/src/components/SettingsPage.tsx b/src/components/SettingsPage.tsx
index 4524df092..16ed9f2a6 100644
--- a/src/components/SettingsPage.tsx
+++ b/src/components/SettingsPage.tsx
@@ -15,6 +15,8 @@ import { useClipboard } from "../hooks/useClipboard";
import { REASONING_PROVIDERS } from "../utils/languages";
import LanguageSelector from "./ui/LanguageSelector";
import PromptStudio from "./ui/PromptStudio";
+import { LocalModelManager } from "./LocalModelManager";
+import AIModelSelector from "./AIModelSelector";
const InteractiveKeyboard = React.lazy(() => import("./ui/Keyboard"));
export type SettingsSectionType =
@@ -92,6 +94,11 @@ export default function SettingsPage({
const { pasteFromClipboardWithFallback } = useClipboard(showAlertDialog);
const { agentName, setAgentName } = useAgentName();
+ // Local state for provider selection (overrides computed value)
+ const [localReasoningProvider, setLocalReasoningProvider] = useState(() => {
+ return localStorage.getItem("reasoningProvider") || reasoningProvider;
+ });
+
// Defer heavy operations for better performance
useEffect(() => {
let mounted = true;
@@ -164,11 +171,14 @@ export default function SettingsPage({
const saveReasoningSettings = useCallback(() => {
updateReasoningSettings({ useReasoningModel, reasoningModel });
updateApiKeys({
- ...(reasoningProvider === "openai" &&
+ ...(localReasoningProvider === "openai" &&
openaiApiKey.trim() && { openaiApiKey }),
- ...(reasoningProvider === "anthropic" &&
+ ...(localReasoningProvider === "anthropic" &&
anthropicApiKey.trim() && { anthropicApiKey }),
});
+
+ // Save the provider separately since it's computed from the model
+ localStorage.setItem("reasoningProvider", localReasoningProvider);
showAlertDialog({
title: "Reasoning Settings Saved",
@@ -176,14 +186,14 @@ export default function SettingsPage({
useReasoningModel ? "enabled" : "disabled"
} with ${
REASONING_PROVIDERS[
- reasoningProvider as keyof typeof REASONING_PROVIDERS
- ]?.name || reasoningProvider
+ localReasoningProvider as keyof typeof REASONING_PROVIDERS
+ ]?.name || localReasoningProvider
} ${reasoningModel}`,
});
}, [
useReasoningModel,
reasoningModel,
- reasoningProvider,
+ localReasoningProvider,
openaiApiKey,
anthropicApiKey,
updateReasoningSettings,
@@ -725,15 +735,6 @@ export default function SettingsPage({
and fixes obvious errors while preserving your natural tone.
</p>
- {useLocalWhisper && (
- <div className="bg-amber-50 border border-amber-200 rounded-lg p-4 mb-6">
- <p className="text-sm text-amber-800">
- <span className="font-medium">Note:</span> AI text
- enhancement requires API access and is currently only
- available when using cloud-based providers.
- </p>
- </div>
- )}
<div className="flex items-center justify-between p-4 bg-green-50 border border-green-200 rounded-xl">
<div>
@@ -775,9 +776,27 @@ export default function SettingsPage({
<div className="space-y-4 p-4 bg-blue-50 border border-blue-200 rounded-xl">
<h4 className="font-medium text-blue-900">AI Provider</h4>
<select
- value={reasoningProvider}
- onChange={(e) => {
- setReasoningProvider(e.target.value);
+ value={localReasoningProvider}
+ onChange={async (e) => {
+ const newProvider = e.target.value;
+ setLocalReasoningProvider(newProvider);
+
+ // Update the model to the first model of the new provider
+ const firstModel = REASONING_PROVIDERS[newProvider as keyof typeof REASONING_PROVIDERS]?.models[0];
+ if (firstModel) {
+ setReasoningModel(firstModel.value);
+ }
+
+ // If switching to local, show a warning if llama.cpp is not available
+ if (newProvider === "local") {
+ const isAvailable = await window.electronAPI?.checkLocalReasoningAvailable?.();
+ if (!isAvailable) {
+ showAlertDialog({
+ title: "Local AI Setup Required",
+ description: "To use local AI models, you need to install llama.cpp and download at least one model. Check the Local AI Models section below for setup instructions.",
+ });
+ }
+ }
}}
className="w-full text-sm border border-blue-300 rounded-md p-2 bg-white focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
>
@@ -799,7 +818,7 @@ export default function SettingsPage({
className="w-full text-sm border border-indigo-300 rounded-md p-2 bg-white focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
>
{REASONING_PROVIDERS[
- reasoningProvider as keyof typeof REASONING_PROVIDERS
+ localReasoningProvider as keyof typeof REASONING_PROVIDERS
]?.models.map((model) => (
<option key={model.value} value={model.value}>
{model.label} - {model.description}
@@ -811,7 +830,7 @@ export default function SettingsPage({
</p>
</div>
- {reasoningProvider === "openai" && (
+ {localReasoningProvider === "openai" && (
<div className="space-y-4 p-4 bg-blue-50 border border-blue-200 rounded-xl">
<h4 className="font-medium text-blue-900">
OpenAI API Key
@@ -824,7 +843,7 @@ export default function SettingsPage({
</div>
)}
- {reasoningProvider === "anthropic" && (
+ {localReasoningProvider === "anthropic" && (
<div className="space-y-4 p-4 bg-purple-50 border border-purple-200 rounded-xl">
<h4 className="font-medium text-purple-900">
Anthropic API Key
@@ -853,6 +872,12 @@ export default function SettingsPage({
</p>
</div>
)}
+
+ {localReasoningProvider === "local" && (
+ <div className="space-y-4">
+ <LocalModelManager />
+ </div>
+ )}
</>
)}
diff --git a/src/components/UnifiedModelPicker.tsx b/src/components/UnifiedModelPicker.tsx
new file mode 100644
index 000000000..8c194b46f
--- /dev/null
+++ b/src/components/UnifiedModelPicker.tsx
@@ -0,0 +1,497 @@
+import { useState, useEffect, useCallback, useMemo } from "react";
+import { Button } from "./ui/button";
+import { Progress } from "./ui/progress";
+import { Alert, AlertDescription } from "./ui/alert";
+import {
+ RefreshCw,
+ Download,
+ Trash2,
+ AlertCircle,
+ ExternalLink
+} from "lucide-react";
+import { useDialogs } from "../hooks/useDialogs";
+import { useToast } from "./ui/Toast";
+import { formatBytes } from "../utils/formatBytes";
+import "../types/electron";
+
+interface Model {
+ id: string;
+ name: string;
+ model?: string; // For Whisper compatibility
+ size: string;
+ size_mb?: number;
+ sizeBytes?: number;
+ description: string;
+ downloaded?: boolean;
+ isDownloaded?: boolean;
+ recommended?: boolean;
+ type: 'whisper' | 'llm';
+}
+
+interface DownloadProgress {
+ percentage: number;
+ downloadedBytes: number;
+ totalBytes: number;
+ speed?: number;
+ eta?: number;
+}
+
+interface UnifiedModelPickerProps {
+ selectedModel: string;
+ onModelSelect: (modelId: string) => void;
+ modelType: 'whisper' | 'llm';
+ className?: string;
+ variant?: "onboarding" | "settings";
+}
+
+const VARIANT_STYLES = {
+ onboarding: {
+ container: "bg-gray-50 p-4 rounded-lg",
+ progress: "bg-blue-50 border-b border-blue-200",
+ progressText: "text-blue-900",
+ progressBar: "bg-blue-200",
+ progressFill: "bg-gradient-to-r from-blue-500 to-blue-600",
+ header: "font-medium text-gray-900 mb-3",
+ modelCard: {
+ selected: "border-blue-500 bg-blue-50",
+ default: "border-gray-200 bg-white hover:border-gray-300",
+ },
+ badges: {
+ selected: "text-xs text-blue-600 bg-blue-100 px-2 py-1 rounded-full font-medium",
+ downloaded: "text-xs text-green-600 bg-green-100 px-2 py-1 rounded",
+ },
+ buttons: {
+ download: "bg-blue-600 hover:bg-blue-700",
+ select: "border-gray-300 text-gray-700 hover:bg-gray-50",
+ delete: "text-red-600 hover:text-red-700 hover:bg-red-50 border-red-200",
+ refresh: "border-gray-300 text-gray-700 hover:bg-gray-50",
+ },
+ },
+ settings: {
+ container: "bg-white border border-purple-200 rounded-lg overflow-hidden",
+ progress: "bg-purple-50 border-b border-purple-200",
+ progressText: "text-purple-900",
+ progressBar: "bg-purple-200",
+ progressFill: "bg-gradient-to-r from-purple-500 to-purple-600",
+ header: "font-medium text-purple-900",
+ modelCard: {
+ selected: "border-purple-500 bg-purple-50",
+ default: "border-purple-200 bg-white hover:border-purple-300",
+ },
+ badges: {
+ selected: "text-xs text-purple-600 bg-purple-100 px-2 py-1 rounded-full font-medium",
+ downloaded: "text-xs text-emerald-600 bg-emerald-100 px-2 py-1 rounded-md",
+ },
+ buttons: {
+ download: "bg-purple-600 hover:bg-purple-700",
+ select: "border-purple-300 text-purple-700 hover:bg-purple-50",
+ delete: "text-red-600 hover:text-red-700 hover:bg-red-50 border-red-200",
+ refresh: "border-purple-300 text-purple-700 hover:bg-purple-50",
+ },
+ },
+};
+
+function formatETA(seconds: number): string {
+ if (seconds < 60) return `${Math.round(seconds)}s`;
+ const minutes = Math.floor(seconds / 60);
+ const remainingSeconds = Math.round(seconds % 60);
+ return `${minutes}m ${remainingSeconds}s`;
+}
+
+export default function UnifiedModelPicker({
+ selectedModel,
+ onModelSelect,
+ modelType,
+ className = "",
+ variant = "settings",
+}: UnifiedModelPickerProps) {
+ const [models, setModels] = useState<Model[]>([]);
+ const [downloadingModel, setDownloadingModel] = useState<string | null>(null);
+ const [downloadProgress, setDownloadProgress] = useState<DownloadProgress>({
+ percentage: 0,
+ downloadedBytes: 0,
+ totalBytes: 0,
+ });
+ const [loadingModels, setLoadingModels] = useState(false);
+ const [llamaCppStatus, setLlamaCppStatus] = useState<{
+ isInstalled: boolean;
+ version?: string;
+ checking: boolean;
+ }>({ isInstalled: true, checking: false }); // Default to true for now
+
+ const {
+ showConfirmDialog,
+ showAlertDialog,
+ } = useDialogs();
+ const { toast } = useToast();
+ const styles = useMemo(() => VARIANT_STYLES[variant], [variant]);
+
+ // Check llama.cpp installation for LLM models
+ // Commented out for now - defaulting to installed
+ /*
+ useEffect(() => {
+ if (modelType === 'llm') {
+ const checkLlamaCpp = async () => {
+ try {
+ const result = await window.electronAPI?.llamaCppCheck();
+ setLlamaCppStatus({
+ isInstalled: result?.isInstalled || false,
+ version: result?.version,
+ checking: false,
+ });
+ } catch {
+ setLlamaCppStatus({ isInstalled: false, checking: false });
+ }
+ };
+ checkLlamaCpp();
+ }
+ }, [modelType]);
+ */
+
+ const loadModels = useCallback(async () => {
+ try {
+ setLoadingModels(true);
+
+ if (modelType === 'whisper') {
+ const result = await window.electronAPI.listWhisperModels();
+ if (result.success) {
+ const whisperModels: Model[] = result.models.map((m: any) => ({
+ ...m,
+ id: m.model,
+ name: m.model.charAt(0).toUpperCase() + m.model.slice(1),
+ size: m.size_mb ? `${m.size_mb}MB` : 'Unknown',
+ description: {
+ tiny: "Fastest, lower quality",
+ base: "Good balance (recommended)",
+ small: "Better quality, slower",
+ medium: "High quality",
+ large: "Best quality, slowest",
+ turbo: "Fast with good quality"
+ }[m.model] || "Model",
+ type: 'whisper' as const,
+ isDownloaded: m.downloaded,
+ recommended: m.model === 'base'
+ }));
+ setModels(whisperModels);
+ }
+ } else {
+ console.log('[UnifiedModelPicker] Loading LLM models...');
+ const result = await window.electronAPI.modelGetAll();
+ console.log('[UnifiedModelPicker] Got result:', result);
+
+ if (!result || !Array.isArray(result)) {
+ console.error('[UnifiedModelPicker] Invalid result format:', result);
+ setModels([]);
+ return;
+ }
+
+ const llmModels: Model[] = result.map((m: any) => ({
+ ...m,
+ type: 'llm' as const,
+ downloaded: m.isDownloaded
+ }));
+ console.log('[UnifiedModelPicker] Mapped models:', llmModels);
+ setModels(llmModels);
+ }
+ } catch (error) {
+ console.error("[UnifiedModelPicker] Failed to load models:", error);
+ setModels([]);
+ } finally {
+ setLoadingModels(false);
+ }
+ }, [modelType]);
+
+ useEffect(() => {
+ loadModels();
+ }, [loadModels]);
+
+ const handleDownloadProgress = useCallback((_event: any, data: any) => {
+ if (modelType === 'whisper') {
+ // Whisper progress format
+ if (data.type === "progress") {
+ const progress: DownloadProgress = {
+ percentage: data.percentage || 0,
+ downloadedBytes: data.downloaded_bytes || 0,
+ totalBytes: data.total_bytes || 0,
+ };
+
+ if (data.speed_mbps && data.speed_mbps > 0) {
+ const remainingBytes = progress.totalBytes - progress.downloadedBytes;
+ progress.eta = (remainingBytes * 8) / (data.speed_mbps * 1_000_000);
+ progress.speed = data.speed_mbps;
+ }
+
+ setDownloadProgress(progress);
+ } else if (data.type === "complete" || data.type === "error") {
+ setDownloadingModel(null);
+ setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });
+ loadModels();
+ }
+ } else {
+ // LLM progress format
+ setDownloadProgress({
+ percentage: data.progress || 0,
+ downloadedBytes: data.downloadedSize || 0,
+ totalBytes: data.totalSize || 0,
+ });
+ }
+ }, [modelType, loadModels]);
+
+ useEffect(() => {
+ if (modelType === 'whisper') {
+ window.electronAPI.onWhisperDownloadProgress(handleDownloadProgress);
+ } else {
+ window.electronAPI.onModelDownloadProgress(handleDownloadProgress);
+ }
+ }, [handleDownloadProgress, modelType]);
+
+ const downloadModel = useCallback(async (modelId: string) => {
+ try {
+ setDownloadingModel(modelId);
+ setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });
+ onModelSelect(modelId);
+
+ if (modelType === 'whisper') {
+ const result = await window.electronAPI.downloadWhisperModel(modelId);
+ if (!result.success && !result.error?.includes("interrupted by user")) {
+ showAlertDialog({
+ title: "Download Failed",
+ description: `Failed to download model: ${result.error}`,
+ });
+ }
+ } else {
+ await window.electronAPI.modelDownload(modelId);
+ }
+
+ await loadModels();
+ } catch (error: any) {
+ if (!error.toString().includes("interrupted by user")) {
+ showAlertDialog({
+ title: "Download Failed",
+ description: `Failed to download model: ${error}`,
+ });
+ }
+ } finally {
+ setDownloadingModel(null);
+ setDownloadProgress({ percentage: 0, downloadedBytes: 0, totalBytes: 0 });
+ }
+ }, [modelType, onModelSelect, loadModels, showAlertDialog]);
+
+ const deleteModel = useCallback(async (modelId: string) => {
+ showConfirmDialog({
+ title: "Delete Model",
+ description: `Are you sure you want to delete this model? You'll need to re-download it if you want to use it again.`,
+ onConfirm: async () => {
+ try {
+ if (modelType === 'whisper') {
+ const result = await window.electronAPI.deleteWhisperModel(modelId);
+ if (result.success) {
+ toast({
+ title: "Model Deleted",
+ description: `Model deleted successfully! Freed ${result.freed_mb}MB of disk space.`,
+ });
+ }
+ } else {
+ await window.electronAPI.modelDelete(modelId);
+ toast({
+ title: "Model Deleted",
+ description: "Model deleted successfully!",
+ });
+ }
+ loadModels();
+ } catch (error) {
+ showAlertDialog({
+ title: "Delete Failed",
+ description: `Failed to delete model: ${error}`,
+ });
+ }
+ },
+ variant: "destructive",
+ });
+ }, [modelType, loadModels, showConfirmDialog, showAlertDialog, toast]);
+
+ const handleInstallLlamaCpp = async () => {
+ try {
+ const result = await window.electronAPI?.llamaCppInstall();
+ if (result?.success) {
+ const status = await window.electronAPI?.llamaCppCheck();
+ setLlamaCppStatus({
+ isInstalled: status?.isInstalled || false,
+ version: status?.version,
+ checking: false,
+ });
+ loadModels();
+ }
+ } catch (error) {
+ console.error("Installation error:", error);
+ }
+ };
+
+ // Show llama.cpp installation prompt for LLM models
+ if (modelType === 'llm' && !llamaCppStatus.isInstalled && !llamaCppStatus.checking) {
+ return (
+ <div className={`${styles.container} ${className}`}>
+ <div className="p-4">
+ <h3 className="text-lg font-semibold mb-4">Local AI Models</h3>
+ <Alert>
+ <AlertCircle className="h-4 w-4" />
+ <AlertDescription className="space-y-3">
+ <p>llama.cpp is required to run local AI models.</p>
+ <div className="flex items-center gap-3">
+ <Button onClick={handleInstallLlamaCpp} size="sm">
+ Install llama.cpp
+ </Button>
+ <Button
+ variant="link"
+ className="p-0 h-auto text-primary"
+ onClick={() => window.electronAPI?.openExternal("https://github.com/ggerganov/llama.cpp#installation")}
+ >
+ Manual installation
+ <ExternalLink className="ml-1 h-3 w-3" />
+ </Button>
+ </div>
+ </AlertDescription>
+ </Alert>
+ </div>
+ </div>
+ );
+ }
+
+ const progressDisplay = useMemo(() => {
+ if (!downloadingModel) return null;
+
+ const { percentage, speed, eta } = downloadProgress;
+ const progressText = `${Math.round(percentage)}%`;
+ const speedText = speed ? ` • ${speed.toFixed(1)} MB/s` : "";
+ const etaText = eta ? ` • ETA: ${formatETA(eta)}` : "";
+
+ return (
+ <div className={`${styles.progress} p-3`}>
+ <div className="flex items-center justify-between mb-2">
+ <span className={`text-sm font-medium ${styles.progressText}`}>
+ Downloading {models.find(m => m.id === downloadingModel)?.name || downloadingModel}...
+ </span>
+ <span className={`text-xs ${styles.progressText}`}>
+ {progressText}{speedText}{etaText}
+ </span>
+ </div>
+ <div className={`w-full ${styles.progressBar} rounded-full h-2`}>
+ <div
+ className={`${styles.progressFill} h-2 rounded-full transition-all duration-300 ease-out`}
+ style={{ width: `${Math.min(percentage, 100)}%` }}
+ />
+ </div>
+ </div>
+ );
+ }, [downloadingModel, downloadProgress, models, styles]);
+
+ return (
+ <div className={`${styles.container} ${className}`}>
+ {progressDisplay}
+
+ <div className="p-4">
+ <div className="flex items-center justify-between mb-3">
+ <h5 className={styles.header}>
+ {modelType === 'whisper' ? 'Whisper Models' : 'Local AI Models'}
+ </h5>
+ <Button
+ onClick={loadModels}
+ variant="outline"
+ size="sm"
+ disabled={loadingModels}
+ className={styles.buttons.refresh}
+ >
+ <RefreshCw size={14} className={loadingModels ? "animate-spin" : ""} />
+ <span className="ml-1">{loadingModels ? "Checking..." : "Refresh"}</span>
+ </Button>
+ </div>
+
+ <div className="space-y-2">
+ {models.map((model) => {
+ const modelId = model.id || model.model || '';
+ const isSelected = modelId === selectedModel;
+ const isDownloading = downloadingModel === modelId;
+ const isDownloaded = model.downloaded || model.isDownloaded;
+
+ return (
+ <div
+ key={modelId}
+ className={`flex items-center justify-between p-3 rounded-lg border-2 transition-all ${
+ isSelected ? styles.modelCard.selected : styles.modelCard.default
+ }`}
+ >
+ <div className="flex-1">
+ <div className="flex items-center gap-2">
+ <span className="font-medium">{model.name}</span>
+ {isSelected && (
+ <span className={styles.badges.selected}>✓ Selected</span>
+ )}
+ {model.recommended && (
+ <span className="text-xs bg-primary/10 text-primary px-2 py-1 rounded">
+ Recommended
+ </span>
+ )}
+ </div>
+ <div className="flex items-center gap-2 mt-1">
+ <p className="text-xs text-gray-600">{model.description}</p>
+ <span className="text-xs text-gray-500">• {model.size}</span>
+ {isDownloaded && (
+ <span className={styles.badges.downloaded}>✓ Downloaded</span>
+ )}
+ </div>
+ </div>
+
+ <div className="flex gap-2">
+ {isDownloaded && (
+ <>
+ {!isSelected && (
+ <Button
+ onClick={() => onModelSelect(modelId)}
+ size="sm"
+ variant="outline"
+ className={styles.buttons.select}
+ >
+ Select
+ </Button>
+ )}
+ <Button
+ onClick={() => deleteModel(modelId)}
+ size="sm"
+ variant="outline"
+ className={styles.buttons.delete}
+ >
+ <Trash2 size={14} />
+ <span className="ml-1">Delete</span>
+ </Button>
+ </>
+ )}
+ {!isDownloaded && !isDownloading && (
+ <Button
+ onClick={() => downloadModel(modelId)}
+ size="sm"
+ className={styles.buttons.download}
+ >
+ <Download size={14} />
+ <span className="ml-1">Download</span>
+ </Button>
+ )}
+ {isDownloading && (
+ <Button disabled size="sm" className={styles.buttons.download}>
+ {`${Math.round(downloadProgress.percentage)}%`}
+ </Button>
+ )}
+ </div>
+ </div>
+ );
+ })}
+ </div>
+
+ {modelType === 'llm' && (
+ <div className="mt-6 text-xs text-muted-foreground">
+ <p>Models are stored in: ~/.cache/openwispr/models/</p>
+ </div>
+ )}
+ </div>
+ </div>
+ );
+}
\ No newline at end of file
diff --git a/src/components/WhisperModelPicker.tsx b/src/components/WhisperModelPicker.tsx
index e72dfc4bb..aadac47d8 100644
--- a/src/components/WhisperModelPicker.tsx
+++ b/src/components/WhisperModelPicker.tsx
@@ -1,23 +1,5 @@
-import React, { useState, useEffect, useCallback, useMemo } from "react";
-import { Button } from "./ui/button";
-import { RefreshCw, Download, Trash2, X } from "lucide-react";
-import { ConfirmDialog, AlertDialog } from "./ui/dialog";
-import { useDialogs } from "../hooks/useDialogs";
-import { useToast } from "./ui/Toast";
-
-interface WhisperModel {
- model: string;
- downloaded: boolean;
- size_mb?: number;
-}
-
-interface DownloadProgress {
- percentage: number;
- downloadedBytes: number;
- totalBytes: number;
- speed?: number;
- eta?: number;
-}
+import React from "react";
+import UnifiedModelPicker from "./UnifiedModelPicker";
interface WhisperModelPickerProps {
selectedModel: string;
@@ -26,479 +8,19 @@ interface WhisperModelPickerProps {
variant?: "onboarding" | "settings";
}
-const MODEL_DESCRIPTIONS = {
- tiny: "Fastest, lower quality",
- base: "Good balance (recommended)",
- small: "Better quality, slower",
- medium: "High quality",
- large: "Best quality, slowest",
-} as const;
-
-const VARIANT_STYLES = {
- onboarding: {
- container: "bg-gray-50 p-4 rounded-lg",
- progress: "bg-blue-50 border-b border-blue-200",
- progressText: "text-blue-900",
- progressBar: "bg-blue-200",
- progressFill: "bg-gradient-to-r from-blue-500 to-blue-600",
- header: "font-medium text-gray-900 mb-3",
- modelCard: {
- selected: "border-blue-500 bg-blue-50",
- default: "border-gray-200 bg-white hover:border-gray-300",
- },
- badges: {
- selected:
- "text-xs text-blue-600 bg-blue-100 px-2 py-1 rounded-full font-medium",
- downloaded: "text-xs text-green-600 bg-green-100 px-2 py-1 rounded",
- },
- buttons: {
- download: "bg-blue-600 hover:bg-blue-700",
- select: "border-gray-300 text-gray-700 hover:bg-gray-50",
- delete: "text-red-600 hover:text-red-700 hover:bg-red-50 border-red-200",
- refresh: "border-gray-300 text-gray-700 hover:bg-gray-50",
- },
- },
- settings: {
- container: "bg-white border border-purple-200 rounded-lg overflow-hidden",
- progress: "bg-purple-50 border-b border-purple-200",
- progressText: "text-purple-900",
- progressBar: "bg-purple-200",
- progressFill: "bg-gradient-to-r from-purple-500 to-purple-600",
- header: "font-medium text-purple-900",
- modelCard: {
- selected: "border-purple-500 bg-purple-50",
- default: "border-purple-200 bg-white hover:border-purple-300",
- },
- badges: {
- selected:
- "text-xs text-purple-600 bg-purple-100 px-2 py-1 rounded-full font-medium",
- downloaded:
- "text-xs text-emerald-600 bg-emerald-100 px-2 py-1 rounded-md",
- },
- buttons: {
- download: "bg-purple-600 hover:bg-purple-700",
- select: "border-purple-300 text-purple-700 hover:bg-purple-50",
- delete: "text-red-600 hover:text-red-700 hover:bg-red-50 border-red-200",
- refresh: "border-purple-300 text-purple-700 hover:bg-purple-50",
- },
- },
-} as const;
-
-function formatETA(seconds: number): string {
- if (seconds < 60) return `${Math.round(seconds)}s`;
- const minutes = Math.floor(seconds / 60);
- const remainingSeconds = Math.round(seconds % 60);
- return `${minutes}m ${remainingSeconds}s`;
-}
-
export default function WhisperModelPicker({
selectedModel,
onModelSelect,
className = "",
variant = "settings",
}: WhisperModelPickerProps) {
- const [modelList, setModelList] = useState<WhisperModel[]>([]);
- const [downloadingModel, setDownloadingModel] = useState<string | null>(null);
- const [downloadProgress, setDownloadProgress] = useState<DownloadProgress>({
- percentage: 0,
- downloadedBytes: 0,
- totalBytes: 0,
- });
- const [loadingModels, setLoadingModels] = useState(false);
-
- // Use custom hooks
- const {
- confirmDialog,
- alertDialog,
- showConfirmDialog,
- showAlertDialog,
- hideConfirmDialog,
- hideAlertDialog,
- } = useDialogs();
- const { toast } = useToast();
- const styles = useMemo(() => VARIANT_STYLES[variant], [variant]);
-
- const loadModelList = useCallback(async () => {
- try {
- setLoadingModels(true);
- const result = await window.electronAPI.listWhisperModels();
- if (result.success) {
- setModelList(result.models);
- }
- } catch (error) {
- console.error("Failed to load model list:", error);
- } finally {
- setLoadingModels(false);
- }
- }, []);
-
- const handleDownloadProgress = useCallback(
- (event: any, data: any) => {
- if (data.type === "progress") {
- const progress: DownloadProgress = {
- percentage: data.percentage || 0,
- downloadedBytes: data.downloaded_bytes || 0,
- totalBytes: data.total_bytes || 0,
- };
-
- // Calculate ETA based on speed
- if (data.speed_mbps && data.speed_mbps > 0) {
- const remainingBytes = progress.totalBytes - progress.downloadedBytes;
- // ETA in seconds: (remainingBytes * 8) / (speed_mbps * 1_000_000)
- progress.eta = (remainingBytes * 8) / (data.speed_mbps * 1_000_000);
- progress.speed = data.speed_mbps;
- }
-
- setDownloadProgress(progress);
- } else if (data.type === "complete") {
- setDownloadingModel(null);
- setDownloadProgress({
- percentage: 0,
- downloadedBytes: 0,
- totalBytes: 0,
- });
- loadModelList();
- } else if (data.type === "error") {
- setDownloadingModel(null);
- setDownloadProgress({
- percentage: 0,
- downloadedBytes: 0,
- totalBytes: 0,
- });
-
- if (data.error?.includes("interrupted by user")) {
- } else if (data.error?.includes("timeout")) {
- showAlertDialog({
- title: "Download Timeout",
- description:
- "Model download timed out. Please check your internet connection and try again.",
- });
- } else {
- showAlertDialog({
- title: "Download Failed",
- description: `Model download failed: ${
- data.error || "Unknown error"
- }`,
- });
- }
- }
- },
- [loadModelList, showAlertDialog]
- );
-
- useEffect(() => {
- loadModelList();
- window.electronAPI.onWhisperDownloadProgress(handleDownloadProgress);
- }, [loadModelList, handleDownloadProgress]);
-
- const downloadModel = useCallback(
- async (modelName: string) => {
- try {
- setDownloadingModel(modelName);
- setDownloadProgress({
- percentage: 0,
- downloadedBytes: 0,
- totalBytes: 0,
- });
-
- // Auto-select the model when starting download
- onModelSelect(modelName);
-
- const result = await window.electronAPI.downloadWhisperModel(modelName);
-
- if (result.success) {
- await loadModelList();
- } else {
- // Only show error if it's not a cancellation
- if (!result.error?.includes("interrupted by user")) {
- showAlertDialog({
- title: "Download Failed",
- description: `Failed to download model "${modelName}": ${result.error}`,
- });
- }
- // Don't show toast here - it's handled by the progress handler
- }
- } catch (error) {
- // Only show error if it's not a cancellation
- if (!error.toString().includes("interrupted by user")) {
- showAlertDialog({
- title: "Download Failed",
- description: `Failed to download model "${modelName}": ${error}`,
- });
- }
- // Don't show toast here - it's handled by the progress handler
- } finally {
- setDownloadingModel(null);
- setDownloadProgress({
- percentage: 0,
- downloadedBytes: 0,
- totalBytes: 0,
- });
- }
- },
- [loadModelList, onModelSelect, showAlertDialog]
- );
-
- const cancelDownload = useCallback(
- async (modelName: string) => {
- showConfirmDialog({
- title: "Cancel Download",
- description: `Are you sure you want to cancel the download of "${modelName}"?`,
- onConfirm: async () => {
- try {
- const result = await window.electronAPI.cancelWhisperDownload();
-
- if (result.success) {
- setDownloadingModel(null);
- setDownloadProgress({
- percentage: 0,
- downloadedBytes: 0,
- totalBytes: 0,
- });
- toast({
- title: "Download Cancelled",
- description: "Model download was cancelled successfully.",
- variant: "default",
- });
- } else {
- // Only show error dialog if it's not a successful cancellation
- if (
- !result.error?.includes("interrupted") &&
- !result.error?.includes("cancelled")
- ) {
- showAlertDialog({
- title: "Cancel Failed",
- description: `Failed to cancel download: ${result.error}`,
- });
- }
- }
- } catch (error) {
- console.error(
- `Failed to cancel download of "${modelName}":`,
- error
- );
- showAlertDialog({
- title: "Cancel Failed",
- description: `Failed to cancel download: ${error}`,
- });
- }
- },
- });
- },
- [showConfirmDialog, showAlertDialog, toast]
- );
-
- const deleteModel = useCallback(
- async (modelName: string) => {
- showConfirmDialog({
- title: "Delete Model",
- description: `Are you sure you want to delete the "${modelName}" model? This will free up disk space but you'll need to re-download it if you want to use it again.`,
- onConfirm: async () => {
- try {
- const result = await window.electronAPI.deleteWhisperModel(
- modelName
- );
-
- if (result.success) {
- showAlertDialog({
- title: "Model Deleted",
- description: `Model "${modelName}" deleted successfully! Freed ${result.freed_mb}MB of disk space.`,
- });
- loadModelList();
- } else {
- showAlertDialog({
- title: "Delete Failed",
- description: `Failed to delete model "${modelName}": ${result.error}`,
- });
- }
- } catch (error) {
- showAlertDialog({
- title: "Delete Failed",
- description: `Failed to delete model "${modelName}": ${error}`,
- });
- }
- },
- variant: "destructive",
- });
- },
- [loadModelList, showConfirmDialog, showAlertDialog]
- );
-
- const progressDisplay = useMemo(() => {
- if (!downloadingModel) return null;
-
- const { percentage, speed, eta } = downloadProgress;
- const progressText = `${Math.round(percentage)}%`;
- const speedText = speed ? ` • ${speed.toFixed(1)} MB/s` : "";
- const etaText = eta ? ` • ETA: ${formatETA(eta)}` : "";
-
- return (
- <div className={`${styles.progress} p-3`}>
- <div className="flex items-center justify-between mb-2">
- <span className={`text-sm font-medium ${styles.progressText}`}>
- Downloading {downloadingModel} model...
- </span>
- <span className={`text-xs ${styles.progressText}`}>
- {progressText}
- {speedText}
- {etaText}
- </span>
- </div>
- <div className={`w-full ${styles.progressBar} rounded-full h-2`}>
- <div
- className={`${styles.progressFill} h-2 rounded-full transition-all duration-300 ease-out`}
- style={{ width: `${Math.min(percentage, 100)}%` }}
- />
- </div>
- </div>
- );
- }, [downloadingModel, downloadProgress, styles]);
-
return (
- <div className={`${styles.container} ${className}`}>
- {progressDisplay}
-
- <div className="p-4">
- <ConfirmDialog
- open={confirmDialog.open}
- onOpenChange={(open) => !open && hideConfirmDialog()}
- title={confirmDialog.title}
- description={confirmDialog.description}
- onConfirm={confirmDialog.onConfirm}
- variant={confirmDialog.variant}
- />
-
- <AlertDialog
- open={alertDialog.open}
- onOpenChange={(open) => !open && hideAlertDialog()}
- title={alertDialog.title}
- description={alertDialog.description}
- onOk={() => {}}
- />
- <div className="flex items-center justify-between mb-3">
- <h5 className={styles.header}>Available Models</h5>
- <Button
- onClick={loadModelList}
- variant="outline"
- size="sm"
- disabled={loadingModels}
- className={styles.buttons.refresh}
- >
- <RefreshCw
- size={14}
- className={loadingModels ? "animate-spin" : ""}
- />
- <span className="ml-1">
- {loadingModels ? "Checking..." : "Refresh"}
- </span>
- </Button>
- </div>
-
- <div className="space-y-2">
- {modelList.map((model) => {
- const isSelected = model.model === selectedModel;
- const isDownloading = downloadingModel === model.model;
-
- return (
- <div
- key={model.model}
- className={`flex items-center justify-between p-3 rounded-lg border-2 transition-all ${
- isSelected
- ? styles.modelCard.selected
- : styles.modelCard.default
- }`}
- >
- <div className="flex-1">
- <div className="flex items-center gap-2">
- <span className="capitalize font-medium">
- {model.model}
- </span>
- {isSelected && (
- <span className={styles.badges.selected}>✓ Selected</span>
- )}
- </div>
- <div className="flex items-center gap-2 mt-1">
- <p className="text-xs text-gray-600">
- {
- MODEL_DESCRIPTIONS[
- model.model as keyof typeof MODEL_DESCRIPTIONS
- ]
- }
- </p>
- {model.downloaded && (
- <span className={styles.badges.downloaded}>
- ✓ Downloaded{" "}
- {model.size_mb ? `(${model.size_mb}MB)` : ""}
- </span>
- )}
- {!model.downloaded && (
- <span className="text-xs text-neutral-500 bg-neutral-100 px-2 py-1 rounded-md">
- Not downloaded
- </span>
- )}
- </div>
- </div>
-
- <div className="flex gap-2">
- {model.downloaded && (
- <>
- {!isSelected && (
- <Button
- onClick={() => onModelSelect(model.model)}
- size="sm"
- variant="outline"
- className={styles.buttons.select}
- >
- Select
- </Button>
- )}
- <Button
- onClick={() => deleteModel(model.model)}
- size="sm"
- variant="outline"
- className={styles.buttons.delete}
- >
- <Trash2 size={14} />
- <span className="ml-1">Delete</span>
- </Button>
- </>
- )}
- {!model.downloaded && !isDownloading && (
- <Button
- onClick={() => downloadModel(model.model)}
- size="sm"
- className={styles.buttons.download}
- >
- <Download size={14} />
- <span className="ml-1">Download</span>
- </Button>
- )}
- {isDownloading && (
- <div className="flex gap-2">
- <Button
- disabled
- size="sm"
- className={styles.buttons.download}
- >
- {`${Math.round(downloadProgress.percentage)}%`}
- </Button>
- <Button
- onClick={() => cancelDownload(model.model)}
- size="sm"
- variant="outline"
- className="text-red-600 hover:text-red-700 hover:bg-red-50 border-red-200"
- >
- <X size={14} />
- <span className="ml-1">Cancel</span>
- </Button>
- </div>
- )}
- </div>
- </div>
- );
- })}
- </div>
- </div>
- </div>
+ <UnifiedModelPicker
+ selectedModel={selectedModel}
+ onModelSelect={onModelSelect}
+ modelType="whisper"
+ className={className}
+ variant={variant}
+ />
);
-}
+}
\ No newline at end of file
diff --git a/src/components/ui/PromptStudio.tsx b/src/components/ui/PromptStudio.tsx
index eecd5cb77..cea064bc2 100644
--- a/src/components/ui/PromptStudio.tsx
+++ b/src/components/ui/PromptStudio.tsx
@@ -90,14 +90,16 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
return;
}
- // Check if we have the required API key
- const apiKey = reasoningProvider === "openai"
- ? localStorage.getItem("openaiApiKey")
- : localStorage.getItem("anthropicApiKey");
-
- if (!apiKey || apiKey.trim() === "") {
- setTestResult(`⚠️ No ${reasoningProvider === "openai" ? "OpenAI" : "Anthropic"} API key found. Add it in AI Models settings.`);
- return;
+ // Check if we have the required API key (only for cloud providers)
+ if (reasoningProvider !== "local") {
+ const apiKey = reasoningProvider === "openai"
+ ? localStorage.getItem("openaiApiKey")
+ : localStorage.getItem("anthropicApiKey");
+
+ if (!apiKey || apiKey.trim() === "") {
+ setTestResult(`⚠️ No ${reasoningProvider === "openai" ? "OpenAI" : "Anthropic"} API key found. Add it in AI Models settings.`);
+ return;
+ }
}
// Save current prompts temporarily so the test uses them
@@ -108,9 +110,31 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
}));
try {
- // Call the AI - ReasoningService will automatically use the custom prompts
- const result = await ReasoningService.processText(testText, reasoningModel);
- setTestResult(result);
+ // For local models, use a different approach
+ if (reasoningProvider === "local") {
+ // Call local reasoning directly
+ const result = await window.electronAPI.processLocalReasoning(testText, reasoningModel, agentName, {
+ customPrompts: {
+ agent: editedAgentPrompt,
+ regular: editedRegularPrompt
+ }
+ });
+
+ if (result.success) {
+ setTestResult(result.text);
+ } else {
+ setTestResult(`❌ Local model error: ${result.error}`);
+ }
+ } else {
+ // Call the AI - ReasoningService will automatically use the custom prompts
+ const result = await ReasoningService.processText(testText, reasoningModel, agentName, {
+ customPrompts: {
+ agent: editedAgentPrompt,
+ regular: editedRegularPrompt
+ }
+ });
+ setTestResult(result);
+ }
} finally {
// Restore original prompts
if (currentCustomPrompts) {
@@ -399,4 +423,4 @@ export default function PromptStudio({ className = "" }: PromptStudioProps) {
{activeTab === "test" && renderTestPlayground()}
</div>
);
-}
+}
\ No newline at end of file
diff --git a/src/components/ui/alert.tsx b/src/components/ui/alert.tsx
new file mode 100644
index 000000000..e40141369
--- /dev/null
+++ b/src/components/ui/alert.tsx
@@ -0,0 +1,59 @@
+import * as React from "react"
+import { cva, type VariantProps } from "class-variance-authority"
+
+import { cn } from "../lib/utils"
+
+const alertVariants = cva(
+ "relative w-full rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7",
+ {
+ variants: {
+ variant: {
+ default: "bg-background text-foreground",
+ destructive:
+ "border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ },
+ }
+)
+
+const Alert = React.forwardRef<
+ HTMLDivElement,
+ React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>
+>(({ className, variant, ...props }, ref) => (
+ <div
+ ref={ref}
+ role="alert"
+ className={cn(alertVariants({ variant }), className)}
+ {...props}
+ />
+))
+Alert.displayName = "Alert"
+
+const AlertTitle = React.forwardRef<
+ HTMLParagraphElement,
+ React.HTMLAttributes<HTMLHeadingElement>
+>(({ className, ...props }, ref) => (
+ <h5
+ ref={ref}
+ className={cn("mb-1 font-medium leading-none tracking-tight", className)}
+ {...props}
+ />
+))
+AlertTitle.displayName = "AlertTitle"
+
+const AlertDescription = React.forwardRef<
+ HTMLParagraphElement,
+ React.HTMLAttributes<HTMLParagraphElement>
+>(({ className, ...props }, ref) => (
+ <div
+ ref={ref}
+ className={cn("text-sm [&_p]:leading-relaxed", className)}
+ {...props}
+ />
+))
+AlertDescription.displayName = "AlertDescription"
+
+export { Alert, AlertTitle, AlertDescription }
\ No newline at end of file
diff --git a/src/components/ui/badge.tsx b/src/components/ui/badge.tsx
new file mode 100644
index 000000000..800d12165
--- /dev/null
+++ b/src/components/ui/badge.tsx
@@ -0,0 +1,36 @@
+import * as React from "react"
+import { cva, type VariantProps } from "class-variance-authority"
+
+import { cn } from "../lib/utils"
+
+const badgeVariants = cva(
+ "inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
+ {
+ variants: {
+ variant: {
+ default:
+ "border-transparent bg-primary text-primary-foreground shadow hover:bg-primary/80",
+ secondary:
+ "border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80",
+ destructive:
+ "border-transparent bg-destructive text-destructive-foreground shadow hover:bg-destructive/80",
+ outline: "text-foreground",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ },
+ }
+)
+
+export interface BadgeProps
+ extends React.HTMLAttributes<HTMLDivElement>,
+ VariantProps<typeof badgeVariants> {}
+
+function Badge({ className, variant, ...props }: BadgeProps) {
+ return (
+ <div className={cn(badgeVariants({ variant }), className)} {...props} />
+ )
+}
+
+export { Badge, badgeVariants }
\ No newline at end of file
diff --git a/src/components/ui/label.tsx b/src/components/ui/label.tsx
new file mode 100644
index 000000000..404b74260
--- /dev/null
+++ b/src/components/ui/label.tsx
@@ -0,0 +1,24 @@
+import * as React from "react"
+import * as LabelPrimitive from "@radix-ui/react-label"
+import { cva, type VariantProps } from "class-variance-authority"
+
+import { cn } from "../lib/utils"
+
+const labelVariants = cva(
+ "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
+)
+
+const Label = React.forwardRef<
+ React.ElementRef<typeof LabelPrimitive.Root>,
+ React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> &
+ VariantProps<typeof labelVariants>
+>(({ className, ...props }, ref) => (
+ <LabelPrimitive.Root
+ ref={ref}
+ className={cn(labelVariants(), className)}
+ {...props}
+ />
+))
+Label.displayName = LabelPrimitive.Root.displayName
+
+export { Label }
\ No newline at end of file
diff --git a/src/components/ui/progress.tsx b/src/components/ui/progress.tsx
new file mode 100644
index 000000000..e71c0a80d
--- /dev/null
+++ b/src/components/ui/progress.tsx
@@ -0,0 +1,26 @@
+import * as React from "react"
+import * as ProgressPrimitive from "@radix-ui/react-progress"
+
+import { cn } from "../lib/utils"
+
+const Progress = React.forwardRef<
+ React.ElementRef<typeof ProgressPrimitive.Root>,
+ React.ComponentPropsWithoutRef<typeof ProgressPrimitive.Root>
+>(({ className, value, ...props }, ref) => (
+ <ProgressPrimitive.Root
+ ref={ref}
+ className={cn(
+ "relative h-2 w-full overflow-hidden rounded-full bg-primary/20",
+ className
+ )}
+ {...props}
+ >
+ <ProgressPrimitive.Indicator
+ className="h-full w-full flex-1 bg-primary transition-all"
+ style={{ transform: `translateX(-${100 - (value || 0)}%)` }}
+ />
+ </ProgressPrimitive.Root>
+))
+Progress.displayName = ProgressPrimitive.Root.displayName
+
+export { Progress }
\ No newline at end of file
diff --git a/src/components/ui/select.tsx b/src/components/ui/select.tsx
new file mode 100644
index 000000000..9b5d27e4a
--- /dev/null
+++ b/src/components/ui/select.tsx
@@ -0,0 +1,157 @@
+import * as React from "react"
+import * as SelectPrimitive from "@radix-ui/react-select"
+import { Check, ChevronDown, ChevronUp } from "lucide-react"
+
+import { cn } from "../lib/utils"
+
+const Select = SelectPrimitive.Root
+
+const SelectGroup = SelectPrimitive.Group
+
+const SelectValue = SelectPrimitive.Value
+
+const SelectTrigger = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Trigger>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
+>(({ className, children, ...props }, ref) => (
+ <SelectPrimitive.Trigger
+ ref={ref}
+ className={cn(
+ "flex h-9 w-full items-center justify-between rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
+ className
+ )}
+ {...props}
+ >
+ {children}
+ <SelectPrimitive.Icon asChild>
+ <ChevronDown className="h-4 w-4 opacity-50" />
+ </SelectPrimitive.Icon>
+ </SelectPrimitive.Trigger>
+))
+SelectTrigger.displayName = SelectPrimitive.Trigger.displayName
+
+const SelectScrollUpButton = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
+>(({ className, ...props }, ref) => (
+ <SelectPrimitive.ScrollUpButton
+ ref={ref}
+ className={cn(
+ "flex cursor-default items-center justify-center py-1",
+ className
+ )}
+ {...props}
+ >
+ <ChevronUp className="h-4 w-4" />
+ </SelectPrimitive.ScrollUpButton>
+))
+SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName
+
+const SelectScrollDownButton = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
+>(({ className, ...props }, ref) => (
+ <SelectPrimitive.ScrollDownButton
+ ref={ref}
+ className={cn(
+ "flex cursor-default items-center justify-center py-1",
+ className
+ )}
+ {...props}
+ >
+ <ChevronDown className="h-4 w-4" />
+ </SelectPrimitive.ScrollDownButton>
+))
+SelectScrollDownButton.displayName =
+ SelectPrimitive.ScrollDownButton.displayName
+
+const SelectContent = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Content>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
+>(({ className, children, position = "popper", ...props }, ref) => (
+ <SelectPrimitive.Portal>
+ <SelectPrimitive.Content
+ ref={ref}
+ className={cn(
+ "relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
+ position === "popper" &&
+ "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
+ className
+ )}
+ position={position}
+ {...props}
+ >
+ <SelectScrollUpButton />
+ <SelectPrimitive.Viewport
+ className={cn(
+ "p-1",
+ position === "popper" &&
+ "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
+ )}
+ >
+ {children}
+ </SelectPrimitive.Viewport>
+ <SelectScrollDownButton />
+ </SelectPrimitive.Content>
+ </SelectPrimitive.Portal>
+))
+SelectContent.displayName = SelectPrimitive.Content.displayName
+
+const SelectLabel = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Label>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
+>(({ className, ...props }, ref) => (
+ <SelectPrimitive.Label
+ ref={ref}
+ className={cn("px-2 py-1.5 text-sm font-semibold", className)}
+ {...props}
+ />
+))
+SelectLabel.displayName = SelectPrimitive.Label.displayName
+
+const SelectItem = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Item>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
+>(({ className, children, ...props }, ref) => (
+ <SelectPrimitive.Item
+ ref={ref}
+ className={cn(
+ "relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
+ className
+ )}
+ {...props}
+ >
+ <span className="absolute right-2 flex h-3.5 w-3.5 items-center justify-center">
+ <SelectPrimitive.ItemIndicator>
+ <Check className="h-4 w-4" />
+ </SelectPrimitive.ItemIndicator>
+ </span>
+ <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
+ </SelectPrimitive.Item>
+))
+SelectItem.displayName = SelectPrimitive.Item.displayName
+
+const SelectSeparator = React.forwardRef<
+ React.ElementRef<typeof SelectPrimitive.Separator>,
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
+>(({ className, ...props }, ref) => (
+ <SelectPrimitive.Separator
+ ref={ref}
+ className={cn("-mx-1 my-1 h-px bg-muted", className)}
+ {...props}
+ />
+))
+SelectSeparator.displayName = SelectPrimitive.Separator.displayName
+
+export {
+ Select,
+ SelectGroup,
+ SelectValue,
+ SelectTrigger,
+ SelectContent,
+ SelectLabel,
+ SelectItem,
+ SelectSeparator,
+ SelectScrollUpButton,
+ SelectScrollDownButton,
+}
\ No newline at end of file
diff --git a/src/components/ui/tabs.tsx b/src/components/ui/tabs.tsx
new file mode 100644
index 000000000..e39fce576
--- /dev/null
+++ b/src/components/ui/tabs.tsx
@@ -0,0 +1,53 @@
+import * as React from "react"
+import * as TabsPrimitive from "@radix-ui/react-tabs"
+
+import { cn } from "../lib/utils"
+
+const Tabs = TabsPrimitive.Root
+
+const TabsList = React.forwardRef<
+ React.ElementRef<typeof TabsPrimitive.List>,
+ React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>
+>(({ className, ...props }, ref) => (
+ <TabsPrimitive.List
+ ref={ref}
+ className={cn(
+ "inline-flex h-9 items-center justify-center rounded-lg bg-muted p-1 text-muted-foreground",
+ className
+ )}
+ {...props}
+ />
+))
+TabsList.displayName = TabsPrimitive.List.displayName
+
+const TabsTrigger = React.forwardRef<
+ React.ElementRef<typeof TabsPrimitive.Trigger>,
+ React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>
+>(({ className, ...props }, ref) => (
+ <TabsPrimitive.Trigger
+ ref={ref}
+ className={cn(
+ "inline-flex items-center justify-center whitespace-nowrap rounded-md px-3 py-1 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-background data-[state=active]:text-foreground data-[state=active]:shadow",
+ className
+ )}
+ {...props}
+ />
+))
+TabsTrigger.displayName = TabsPrimitive.Trigger.displayName
+
+const TabsContent = React.forwardRef<
+ React.ElementRef<typeof TabsPrimitive.Content>,
+ React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content>
+>(({ className, ...props }, ref) => (
+ <TabsPrimitive.Content
+ ref={ref}
+ className={cn(
+ "mt-2 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
+ className
+ )}
+ {...props}
+ />
+))
+TabsContent.displayName = TabsPrimitive.Content.displayName
+
+export { Tabs, TabsList, TabsTrigger, TabsContent }
\ No newline at end of file
diff --git a/src/config/InferenceConfig.ts b/src/config/InferenceConfig.ts
new file mode 100644
index 000000000..ac87decdd
--- /dev/null
+++ b/src/config/InferenceConfig.ts
@@ -0,0 +1,87 @@
+import { cpus } from 'os';
+
+export interface InferenceConfig {
+ temperature: number;
+ maxTokens: number;
+ topK: number;
+ topP: number;
+ repeatPenalty: number;
+ threads: number;
+ contextSize: number;
+ timeout: number;
+}
+
+export class InferenceConfigManager {
+ private static instance: InferenceConfigManager;
+ private config: InferenceConfig;
+
+ private constructor() {
+ this.config = this.getDefaultConfig();
+ }
+
+ static getInstance(): InferenceConfigManager {
+ if (!InferenceConfigManager.instance) {
+ InferenceConfigManager.instance = new InferenceConfigManager();
+ }
+ return InferenceConfigManager.instance;
+ }
+
+ getConfig(): InferenceConfig {
+ return { ...this.config };
+ }
+
+ updateConfig(updates: Partial<InferenceConfig>) {
+ this.config = {
+ ...this.config,
+ ...updates
+ };
+ }
+
+ getOptimalThreadCount(): number {
+ // Use 75% of available CPUs for optimal performance
+ const cpuCount = cpus().length;
+ return Math.max(1, Math.floor(cpuCount * 0.75));
+ }
+
+ private getDefaultConfig(): InferenceConfig {
+ return {
+ temperature: 0.3,
+ maxTokens: 512,
+ topK: 40,
+ topP: 0.9,
+ repeatPenalty: 1.1,
+ threads: this.getOptimalThreadCount(),
+ contextSize: 2048,
+ timeout: 30000
+ };
+ }
+
+ // Get config optimized for specific use cases
+ getConfigForUseCase(useCase: 'reasoning' | 'creative' | 'factual'): Partial<InferenceConfig> {
+ switch (useCase) {
+ case 'reasoning':
+ return {
+ temperature: 0.3,
+ topK: 40,
+ topP: 0.9,
+ repeatPenalty: 1.1
+ };
+ case 'creative':
+ return {
+ temperature: 0.8,
+ topK: 100,
+ topP: 0.95,
+ repeatPenalty: 1.0
+ };
+ case 'factual':
+ return {
+ temperature: 0.1,
+ topK: 20,
+ topP: 0.85,
+ repeatPenalty: 1.2
+ };
+ }
+ }
+}
+
+export const inferenceConfig = InferenceConfigManager.getInstance();
\ No newline at end of file
diff --git a/src/config/aiProvidersConfig.ts b/src/config/aiProvidersConfig.ts
new file mode 100644
index 000000000..b513723ea
--- /dev/null
+++ b/src/config/aiProvidersConfig.ts
@@ -0,0 +1,178 @@
+export interface AIModel {
+ value: string;
+ label: string;
+ description: string;
+ size?: string;
+}
+
+export interface AIProvider {
+ id: string;
+ name: string;
+ models: AIModel[];
+}
+
+export interface AIMode {
+ id: 'cloud' | 'local';
+ name: string;
+ providers: AIProvider[];
+}
+
+export const AI_MODES: AIMode[] = [
+ {
+ id: 'cloud',
+ name: 'Cloud AI',
+ providers: [
+ {
+ id: 'openai',
+ name: 'OpenAI',
+ models: [
+ {
+ value: 'gpt-3.5-turbo',
+ label: 'GPT-3.5 Turbo',
+ description: 'Fast and efficient for most tasks'
+ },
+ {
+ value: 'gpt-4o-mini',
+ label: 'GPT-4o Mini',
+ description: 'Higher quality reasoning'
+ },
+ {
+ value: 'gpt-4-turbo-preview',
+ label: 'GPT-4 Turbo',
+ description: 'Most capable model'
+ }
+ ]
+ },
+ {
+ id: 'anthropic',
+ name: 'Anthropic',
+ models: [
+ {
+ value: 'claude-3-haiku-20240307',
+ label: 'Claude 3 Haiku',
+ description: 'Fast and affordable'
+ },
+ {
+ value: 'claude-3-sonnet-20240229',
+ label: 'Claude 3 Sonnet',
+ description: 'Balanced performance'
+ },
+ {
+ value: 'claude-3-opus-20240229',
+ label: 'Claude 3 Opus',
+ description: 'Most capable Claude model'
+ }
+ ]
+ }
+ ]
+ },
+ {
+ id: 'local',
+ name: 'Local AI',
+ providers: [
+ {
+ id: 'qwen',
+ name: 'Qwen',
+ models: [
+ {
+ value: 'qwen2.5-0.5b-instruct-q5_k_m',
+ label: 'Qwen 2.5 0.5B',
+ description: 'Smallest, fast but limited',
+ size: '0.4GB'
+ },
+ {
+ value: 'qwen2.5-1.5b-instruct-q5_k_m',
+ label: 'Qwen 2.5 1.5B',
+ description: 'Small, good for basic tasks',
+ size: '1.3GB'
+ },
+ {
+ value: 'qwen2.5-3b-instruct-q5_k_m',
+ label: 'Qwen 2.5 3B',
+ description: 'Balanced for general use',
+ size: '2.3GB'
+ },
+ {
+ value: 'qwen2.5-7b-instruct-q4km',
+ label: 'Qwen 2.5 7B (Q4_K_M)',
+ description: 'Large, high quality',
+ size: '4.7GB'
+ },
+ {
+ value: 'qwen2.5-7b-instruct-q5_k_m',
+ label: 'Qwen 2.5 7B (Q5_K_M)',
+ description: 'Large, highest quality',
+ size: '5.4GB'
+ }
+ ]
+ },
+ {
+ id: 'mistral',
+ name: 'Mistral',
+ models: [
+ {
+ value: 'mistral-7b-instruct-v0.2-q4_k_m',
+ label: 'Mistral 7B v0.2',
+ description: 'Popular open model',
+ size: '4.1GB'
+ },
+ {
+ value: 'mixtral-8x7b-instruct-v0.1-q3_k_m',
+ label: 'Mixtral 8x7B',
+ description: 'MoE architecture, very capable',
+ size: '26.4GB'
+ }
+ ]
+ },
+ {
+ id: 'llama',
+ name: 'Meta Llama',
+ models: [
+ {
+ value: 'llama-3.2-1b-instruct-q5_k_m',
+ label: 'Llama 3.2 1B',
+ description: 'Small and efficient',
+ size: '1.0GB'
+ },
+ {
+ value: 'llama-3.2-3b-instruct-q4_k_m',
+ label: 'Llama 3.2 3B',
+ description: 'Good balance',
+ size: '2.0GB'
+ },
+ {
+ value: 'llama-3.1-8b-instruct-q4_k_m',
+ label: 'Llama 3.1 8B',
+ description: 'Large, high quality',
+ size: '4.7GB'
+ }
+ ]
+ }
+ ]
+ }
+];
+
+export function getAIMode(modeId: 'cloud' | 'local'): AIMode | undefined {
+ return AI_MODES.find(mode => mode.id === modeId);
+}
+
+export function getAIProvider(modeId: 'cloud' | 'local', providerId: string): AIProvider | undefined {
+ const mode = getAIMode(modeId);
+ return mode?.providers.find(provider => provider.id === providerId);
+}
+
+export function getAIModel(modeId: 'cloud' | 'local', providerId: string, modelId: string): AIModel | undefined {
+ const provider = getAIProvider(modeId, providerId);
+ return provider?.models.find(model => model.value === modelId);
+}
+
+export function detectModeFromModel(modelId: string): { mode: 'cloud' | 'local', providerId: string } | undefined {
+ for (const mode of AI_MODES) {
+ for (const provider of mode.providers) {
+ if (provider.models.some(model => model.value === modelId)) {
+ return { mode: mode.id, providerId: provider.id };
+ }
+ }
+ }
+ return undefined;
+}
\ No newline at end of file
diff --git a/src/config/constants.ts b/src/config/constants.ts
new file mode 100644
index 000000000..849db8759
--- /dev/null
+++ b/src/config/constants.ts
@@ -0,0 +1,40 @@
+// API Configuration
+export const API_ENDPOINTS = {
+ OPENAI: 'https://api.openai.com/v1/chat/completions',
+ ANTHROPIC: 'https://api.anthropic.com/v1/messages',
+} as const;
+
+export const API_VERSIONS = {
+ ANTHROPIC: '2023-06-01',
+} as const;
+
+// Model Configuration
+export const MODEL_CONSTRAINTS = {
+ MIN_FILE_SIZE: 1_000_000, // 1MB minimum for valid model files
+ MODEL_TEST_TIMEOUT: 5000, // 5 seconds for model validation
+ INFERENCE_TIMEOUT: 30000, // 30 seconds default (configurable)
+} as const;
+
+// Token Limits
+export const TOKEN_LIMITS = {
+ MIN_TOKENS: 100,
+ MAX_TOKENS: 2048,
+ MIN_TOKENS_ANTHROPIC: 100,
+ MAX_TOKENS_ANTHROPIC: 4096,
+ TOKEN_MULTIPLIER: 2, // text.length * multiplier
+ REASONING_CONTEXT_SIZE: 4096,
+} as const;
+
+// Cache Configuration
+export const CACHE_CONFIG = {
+ API_KEY_TTL: 3600000, // 1 hour in milliseconds
+ MODEL_CACHE_SIZE: 3, // Maximum models to keep in memory
+} as const;
+
+// Retry Configuration
+export const RETRY_CONFIG = {
+ MAX_RETRIES: 3,
+ INITIAL_DELAY: 1000, // 1 second
+ MAX_DELAY: 10000, // 10 seconds
+ BACKOFF_MULTIPLIER: 2,
+} as const;
\ No newline at end of file
diff --git a/src/helpers/ModelManager.ts b/src/helpers/ModelManager.ts
new file mode 100644
index 000000000..f1de1d917
--- /dev/null
+++ b/src/helpers/ModelManager.ts
@@ -0,0 +1,330 @@
+import path from "path";
+import { spawn } from "child_process";
+import fs from "fs";
+import { promises as fsPromises } from "fs";
+import https from "https";
+import { app } from "electron";
+import { modelRegistry } from "../models/ModelRegistry";
+import { inferenceConfig } from "../config/InferenceConfig";
+import { MODEL_CONSTRAINTS } from "../config/constants";
+
+// Error types
+export class ModelError extends Error {
+ constructor(
+ message: string,
+ public code: string,
+ public details: Record<string, any> = {}
+ ) {
+ super(message);
+ this.name = "ModelError";
+ }
+}
+
+export class ModelNotFoundError extends ModelError {
+ constructor(modelId: string) {
+ super(`Model ${modelId} not found`, "MODEL_NOT_FOUND", { modelId });
+ }
+}
+
+export interface DownloadProgress {
+ modelId: string;
+ progress: number;
+ downloadedSize: number;
+ totalSize: number;
+}
+
+class ModelManager {
+ private modelsDir: string;
+ private llamaCppPath: string | null = null;
+ private downloadProgress = new Map<string, DownloadProgress>();
+ private activeDownloads = new Map<string, boolean>();
+
+ constructor() {
+ this.modelsDir = this.getModelsDir();
+ }
+
+ private getModelsDir(): string {
+ const homeDir = app.getPath("home");
+ return path.join(homeDir, ".cache", "openwispr", "models");
+ }
+
+ async ensureModelsDir(): Promise<void> {
+ await fsPromises.mkdir(this.modelsDir, { recursive: true });
+ }
+
+ async isModelDownloaded(modelId: string): Promise<boolean> {
+ const modelInfo = modelRegistry.getModel(modelId);
+ if (!modelInfo) return false;
+
+ const modelPath = path.join(this.modelsDir, modelInfo.model.fileName);
+ try {
+ const stats = await fsPromises.stat(modelPath);
+ return stats.size > MODEL_CONSTRAINTS.MIN_FILE_SIZE;
+ } catch {
+ return false;
+ }
+ }
+
+ async downloadModel(
+ modelId: string,
+ onProgress?: (progress: number, downloaded: number, total: number) => void
+ ): Promise<string> {
+ if (this.activeDownloads.has(modelId)) {
+ throw new ModelError("Download already in progress", "DOWNLOAD_IN_PROGRESS", { modelId });
+ }
+
+ await this.ensureModelsDir();
+
+ const modelInfo = modelRegistry.getModel(modelId);
+ if (!modelInfo) {
+ throw new ModelNotFoundError(modelId);
+ }
+
+ const { model, provider } = modelInfo;
+ const modelPath = path.join(this.modelsDir, model.fileName);
+ const tempPath = `${modelPath}.tmp`;
+
+ if (await this.isModelDownloaded(modelId)) {
+ return modelPath;
+ }
+
+ const downloadUrl = provider.getDownloadUrl(model);
+ this.activeDownloads.set(modelId, true);
+
+ try {
+ await this.downloadFile(downloadUrl, tempPath, (downloadedSize, totalSize) => {
+ const progress = (downloadedSize / totalSize) * 100;
+ this.downloadProgress.set(modelId, {
+ modelId,
+ progress,
+ downloadedSize,
+ totalSize,
+ });
+ if (onProgress) {
+ onProgress(progress, downloadedSize, totalSize);
+ }
+ });
+
+ const stats = await fsPromises.stat(tempPath);
+ if (stats.size < MODEL_CONSTRAINTS.MIN_FILE_SIZE) {
+ throw new ModelError(
+ "Downloaded file appears to be corrupted",
+ "DOWNLOAD_CORRUPTED",
+ { size: stats.size }
+ );
+ }
+
+ await fsPromises.rename(tempPath, modelPath);
+
+ this.downloadProgress.delete(modelId);
+ this.activeDownloads.delete(modelId);
+
+ return modelPath;
+ } catch (error) {
+ this.downloadProgress.delete(modelId);
+ this.activeDownloads.delete(modelId);
+
+ try {
+ await fsPromises.unlink(tempPath);
+ } catch {
+ // Ignore cleanup errors
+ }
+
+ throw error;
+ }
+ }
+
+ async deleteModel(modelId: string): Promise<void> {
+ const modelInfo = modelRegistry.getModel(modelId);
+ if (!modelInfo) return;
+
+ const modelPath = path.join(this.modelsDir, modelInfo.model.fileName);
+ try {
+ await fsPromises.unlink(modelPath);
+ } catch (error: any) {
+ if (error.code !== 'ENOENT') {
+ throw new ModelError(`Failed to delete model`, "DELETE_ERROR", {
+ modelId,
+ error: error.message
+ });
+ }
+ }
+ }
+
+ getDownloadProgress(modelId: string): DownloadProgress | undefined {
+ return this.downloadProgress.get(modelId);
+ }
+
+ private async downloadFile(
+ url: string,
+ destPath: string,
+ onProgress: (downloaded: number, total: number) => void
+ ): Promise<void> {
+ return new Promise((resolve, reject) => {
+ const file = fs.createWriteStream(destPath);
+
+ const request = https.get(url, (response) => {
+ if (response.statusCode === 301 || response.statusCode === 302) {
+ const redirectUrl = response.headers.location;
+ if (redirectUrl) {
+ this.downloadFile(redirectUrl, destPath, onProgress)
+ .then(resolve)
+ .catch(reject);
+ return;
+ }
+ }
+
+ if (response.statusCode !== 200) {
+ reject(new Error(`Download failed: ${response.statusCode}`));
+ return;
+ }
+
+ const totalSize = parseInt(response.headers['content-length'] || '0', 10);
+ let downloadedSize = 0;
+
+ response.on('data', (chunk) => {
+ downloadedSize += chunk.length;
+ onProgress(downloadedSize, totalSize);
+ });
+
+ response.pipe(file);
+
+ file.on('finish', () => {
+ file.close();
+ resolve();
+ });
+
+ file.on('error', (err) => {
+ fs.unlink(destPath, () => {});
+ reject(err);
+ });
+ });
+
+ request.on('error', reject);
+ });
+ }
+
+ async ensureLlamaCpp(): Promise<boolean> {
+ if (this.llamaCppPath) return true;
+
+ // Dynamic import to avoid loading in frontend
+ const llamaCppInstaller = require("./llamaCppInstaller").default;
+
+ if (await llamaCppInstaller.isInstalled()) {
+ this.llamaCppPath = llamaCppInstaller.getInstalledBinaryPath();
+ return true;
+ }
+
+ const paths = [
+ "llama-cli",
+ "/usr/local/bin/llama-cli",
+ "/opt/homebrew/bin/llama-cli",
+ path.join(app.getPath("home"), ".local", "bin", "llama-cli"),
+ ];
+
+ for (const p of paths) {
+ try {
+ if (p === "llama-cli") {
+ await new Promise((resolve, reject) => {
+ spawn("which", ["llama-cli"])
+ .on("close", code => code === 0 ? resolve(p) : reject());
+ });
+ this.llamaCppPath = p;
+ return true;
+ } else {
+ await fsPromises.access(p, fs.constants.X_OK);
+ this.llamaCppPath = p;
+ return true;
+ }
+ } catch {
+ // Continue to next path
+ }
+ }
+
+ throw new ModelError(
+ "llama.cpp not found. Please install it from the Local AI Models settings.",
+ "LLAMA_CPP_NOT_FOUND"
+ );
+ }
+
+ async runInference(modelId: string, prompt: string, options: any = {}): Promise<string> {
+ await this.ensureLlamaCpp();
+
+ const modelInfo = modelRegistry.getModel(modelId);
+ if (!modelInfo) {
+ throw new ModelNotFoundError(modelId);
+ }
+
+ const { model, provider } = modelInfo;
+ const modelPath = path.join(this.modelsDir, model.fileName);
+
+ if (!(await this.isModelDownloaded(modelId))) {
+ throw new ModelError(
+ `Model ${modelId} is not downloaded`,
+ "MODEL_NOT_DOWNLOADED"
+ );
+ }
+
+ const config = inferenceConfig.getConfig();
+ const finalOptions = { ...config, ...options };
+ const formattedPrompt = provider.formatPrompt(prompt, "");
+
+ const args = [
+ "-m", modelPath,
+ "-p", formattedPrompt,
+ "-n", finalOptions.maxTokens.toString(),
+ "--temp", finalOptions.temperature.toString(),
+ "--top-k", finalOptions.topK.toString(),
+ "--top-p", finalOptions.topP.toString(),
+ "--repeat-penalty", finalOptions.repeatPenalty.toString(),
+ "--ctx-size", finalOptions.contextSize.toString(),
+ "--no-display-prompt",
+ "-t", finalOptions.threads.toString(),
+ ];
+
+ return new Promise((resolve, reject) => {
+ const llamaProcess = spawn(this.llamaCppPath!, args);
+ let output = "";
+ let error = "";
+
+ llamaProcess.stdout.on("data", (data) => {
+ output += data.toString();
+ });
+
+ llamaProcess.stderr.on("data", (data) => {
+ error += data.toString();
+ });
+
+ llamaProcess.on("close", (code) => {
+ if (code === 0) {
+ resolve(output.trim());
+ } else {
+ reject(new ModelError(
+ `Inference failed: ${error}`,
+ "INFERENCE_ERROR"
+ ));
+ }
+ });
+
+ setTimeout(() => {
+ llamaProcess.kill();
+ reject(new ModelError("Inference timeout", "INFERENCE_TIMEOUT"));
+ }, finalOptions.timeout);
+ });
+ }
+
+ async getModelsWithStatus() {
+ 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)
+ }))
+ );
+ }
+}
+
+export default new ModelManager();
\ No newline at end of file
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index f534693bc..398d4575a 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -365,8 +365,20 @@ class AudioManager {
async processWithReasoningModel(text) {
try {
const model = localStorage.getItem("reasoningModel") || "gpt-3.5-turbo";
- return await ReasoningService.processText(text, model);
+ console.log(`Processing with reasoning model: ${model}`);
+
+ const result = await ReasoningService.processText(text, model);
+ console.log(`Reasoning model processing successful`);
+ return result;
} catch (error) {
+ console.error(`Reasoning model processing failed: ${error.message}`);
+
+ // If it's a local model error, provide more helpful information
+ if (error.message.includes("llama.cpp") || error.message.includes("Local reasoning")) {
+ console.error("Local AI error - make sure llama.cpp is installed and the model is downloaded");
+ }
+
+ // Fall back to basic cleaning
return AudioManager.cleanTranscription(text);
}
}
diff --git a/src/helpers/environment.js b/src/helpers/environment.js
index 724a24578..b52882f3b 100644
--- a/src/helpers/environment.js
+++ b/src/helpers/environment.js
@@ -53,6 +53,22 @@ class EnvironmentManager {
}
}
+ getAnthropicKey() {
+ const apiKey = process.env.ANTHROPIC_API_KEY;
+ return apiKey || "";
+ }
+
+ saveAnthropicKey(key) {
+ try {
+ // Update the environment variable in memory for immediate use
+ process.env.ANTHROPIC_API_KEY = key;
+ return { success: true };
+ } catch (error) {
+ console.error("Error saving Anthropic API key:", error.message);
+ throw error;
+ }
+ }
+
createProductionEnvFile(apiKey) {
try {
const envPath = path.join(app.getPath("userData"), ".env");
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 90d30eb74..38daf4e18 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -9,6 +9,7 @@ class IPCHandlers {
this.clipboardManager = managers.clipboardManager;
this.whisperManager = managers.whisperManager;
this.windowManager = managers.windowManager;
+ this.modelManager = managers.modelManager;
this.setupHandlers();
}
@@ -284,6 +285,141 @@ class IPCHandlers {
return { success: false, error: error.message };
}
});
+
+ // Model management handlers
+ ipcMain.handle("model-get-all", async () => {
+ try {
+ console.log('[IPC] model-get-all called');
+ const modelManager = require("./modelManagerBridge").default;
+ const models = await modelManager.getModelsWithStatus();
+ console.log('[IPC] Returning models:', models.length);
+ return models;
+ } catch (error) {
+ console.error('[IPC] Error in model-get-all:', error);
+ throw error;
+ }
+ });
+
+ ipcMain.handle("model-check", async (_, modelId) => {
+ const modelManager = require("./modelManagerBridge").default;
+ return modelManager.isModelDownloaded(modelId);
+ });
+
+ ipcMain.handle("model-download", async (event, modelId) => {
+ try {
+ const modelManager = require("./modelManagerBridge").default;
+ const result = await modelManager.downloadModel(
+ modelId,
+ (progress, downloadedSize, totalSize) => {
+ event.sender.send("model-download-progress", {
+ modelId,
+ progress,
+ downloadedSize,
+ totalSize,
+ });
+ }
+ );
+ return { success: true, path: result };
+ } catch (error) {
+ return {
+ success: false,
+ error: error.message,
+ code: error.code,
+ details: error.details
+ };
+ }
+ });
+
+ ipcMain.handle("model-delete", async (event, modelId) => {
+ try {
+ const modelManager = require("./modelManagerBridge").default;
+ await modelManager.deleteModel(modelId);
+ return { success: true };
+ } catch (error) {
+ return {
+ success: false,
+ error: error.message,
+ code: error.code,
+ details: error.details
+ };
+ }
+ });
+
+ ipcMain.handle("model-check-runtime", async (event) => {
+ try {
+ const modelManager = require("./modelManagerBridge").default;
+ await modelManager.ensureLlamaCpp();
+ return { available: true };
+ } catch (error) {
+ return {
+ available: false,
+ error: error.message,
+ code: error.code,
+ details: error.details
+ };
+ }
+ });
+
+ ipcMain.handle("get-anthropic-key", async (event) => {
+ return this.environmentManager.getAnthropicKey();
+ });
+
+ ipcMain.handle("save-anthropic-key", async (event, key) => {
+ return this.environmentManager.saveAnthropicKey(key);
+ });
+
+ // Local reasoning handler
+ ipcMain.handle("process-local-reasoning", async (event, text, modelId, agentName, config) => {
+ try {
+ const LocalReasoningService = require("../services/localReasoningBridge").default;
+ const result = await LocalReasoningService.processText(text, modelId, agentName, config);
+ return { success: true, text: result };
+ } catch (error) {
+ return { success: false, error: error.message };
+ }
+ });
+
+ // Check if local reasoning is available
+ ipcMain.handle("check-local-reasoning-available", async () => {
+ try {
+ const LocalReasoningService = require("../services/localReasoningBridge").default;
+ return await LocalReasoningService.isAvailable();
+ } catch (error) {
+ return false;
+ }
+ });
+
+ // llama.cpp installation handlers
+ ipcMain.handle("llama-cpp-check", async () => {
+ try {
+ const llamaCppInstaller = require("./llamaCppInstaller").default;
+ const isInstalled = await llamaCppInstaller.isInstalled() ||
+ await llamaCppInstaller.checkSystemInstallation();
+ const version = isInstalled ? await llamaCppInstaller.getVersion() : null;
+ return { isInstalled, version };
+ } catch (error) {
+ return { isInstalled: false, error: error.message };
+ }
+ });
+
+ ipcMain.handle("llama-cpp-install", async () => {
+ try {
+ const llamaCppInstaller = require("./llamaCppInstaller").default;
+ const result = await llamaCppInstaller.install();
+ return result;
+ } catch (error) {
+ return { success: false, error: error.message };
+ }
+ });
+
+ ipcMain.handle("llama-cpp-uninstall", async () => {
+ try {
+ const result = await llamaCppInstaller.uninstall();
+ return result;
+ } catch (error) {
+ return { success: false, error: error.message };
+ }
+ });
}
}
diff --git a/src/helpers/llamaCppInstaller.ts b/src/helpers/llamaCppInstaller.ts
new file mode 100644
index 000000000..6180eda9d
--- /dev/null
+++ b/src/helpers/llamaCppInstaller.ts
@@ -0,0 +1,215 @@
+import { spawn } from "child_process";
+import { app } from "electron";
+import path from "path";
+import fs from "fs";
+import { promises as fsPromises } from "fs";
+import https from "https";
+import { createWriteStream } from "fs";
+import tar from "tar";
+import os from "os";
+
+// Only import unzipper in main process
+let unzipper: any;
+if (typeof window === 'undefined') {
+ unzipper = require("unzipper");
+}
+
+class LlamaCppInstaller {
+ private installDir: string;
+ private binPath: string | null = null;
+ private platform: string;
+ private arch: string;
+
+ constructor() {
+ this.installDir = path.join(app.getPath("userData"), "llama-cpp");
+ this.platform = process.platform;
+ this.arch = process.arch;
+ }
+
+ async ensureInstallDir(): Promise<void> {
+ await fsPromises.mkdir(this.installDir, { recursive: true });
+ }
+
+ getBinaryName(): string {
+ return this.platform === "win32" ? "llama-cli.exe" : "llama-cli";
+ }
+
+ getInstalledBinaryPath(): string {
+ return path.join(this.installDir, this.getBinaryName());
+ }
+
+ async isInstalled(): Promise<boolean> {
+ try {
+ const binaryPath = this.getInstalledBinaryPath();
+ await fsPromises.access(binaryPath, fs.constants.X_OK);
+ this.binPath = binaryPath;
+ return true;
+ } catch {
+ return false;
+ }
+ }
+
+ async checkSystemInstallation(): Promise<boolean> {
+ return new Promise((resolve) => {
+ const checkCommand = this.platform === "win32" ? "where" : "which";
+ spawn(checkCommand, ["llama-cli"])
+ .on("close", (code) => {
+ resolve(code === 0);
+ })
+ .on("error", () => {
+ resolve(false);
+ });
+ });
+ }
+
+ async getVersion(): Promise<string | null> {
+ if (!await this.isInstalled()) {
+ return null;
+ }
+
+ return new Promise((resolve) => {
+ const proc = spawn(this.binPath!, ["--version"]);
+ let output = "";
+
+ proc.stdout.on("data", (data) => {
+ output += data.toString();
+ });
+
+ proc.on("close", () => {
+ const match = output.match(/version:\s*([^\s]+)/i);
+ resolve(match ? match[1] : "unknown");
+ });
+
+ proc.on("error", () => {
+ resolve(null);
+ });
+ });
+ }
+
+ getDownloadUrl(): string {
+ const baseUrl = "https://github.com/ggerganov/llama.cpp/releases/latest/download";
+
+ if (this.platform === "darwin") {
+ return `${baseUrl}/llama-${this.arch === "arm64" ? "arm64" : "x64"}-apple-darwin.zip`;
+ } else if (this.platform === "linux") {
+ return `${baseUrl}/llama-x64-linux.tar.gz`;
+ } else if (this.platform === "win32") {
+ return `${baseUrl}/llama-x64-windows.zip`;
+ }
+
+ throw new Error(`Unsupported platform: ${this.platform}`);
+ }
+
+ async download(url: string, destPath: string): Promise<void> {
+ return new Promise((resolve, reject) => {
+ const file = createWriteStream(destPath);
+
+ https.get(url, (response) => {
+ if (response.statusCode === 302 || response.statusCode === 301) {
+ const redirectUrl = response.headers.location;
+ if (redirectUrl) {
+ this.download(redirectUrl, destPath).then(resolve).catch(reject);
+ return;
+ }
+ }
+
+ if (response.statusCode !== 200) {
+ reject(new Error(`Download failed with status: ${response.statusCode}`));
+ return;
+ }
+
+ response.pipe(file);
+
+ file.on("finish", () => {
+ file.close();
+ resolve();
+ });
+
+ file.on("error", (err) => {
+ fs.unlink(destPath, () => {});
+ reject(err);
+ });
+ }).on("error", reject);
+ });
+ }
+
+ async extract(archivePath: string): Promise<void> {
+ await this.ensureInstallDir();
+
+ if (archivePath.endsWith(".tar.gz")) {
+ await tar.x({
+ file: archivePath,
+ cwd: this.installDir,
+ });
+ } else if (archivePath.endsWith(".zip")) {
+ await fs.createReadStream(archivePath)
+ .pipe(unzipper.Extract({ path: this.installDir }))
+ .promise();
+ } else {
+ throw new Error("Unsupported archive format");
+ }
+ }
+
+ async install(): Promise<{ success: boolean; error?: string }> {
+ try {
+ // Check if already installed
+ if (await this.isInstalled()) {
+ return { success: true };
+ }
+
+ // Create install directory
+ await this.ensureInstallDir();
+
+ // Download
+ const url = this.getDownloadUrl();
+ const archivePath = path.join(
+ this.installDir,
+ url.endsWith(".zip") ? "llama.zip" : "llama.tar.gz"
+ );
+
+ console.log(`Downloading llama.cpp from ${url}...`);
+ await this.download(url, archivePath);
+
+ // Extract
+ console.log("Extracting archive...");
+ await this.extract(archivePath);
+
+ // Clean up archive
+ await fsPromises.unlink(archivePath);
+
+ // Make binary executable on Unix
+ if (this.platform !== "win32") {
+ const binaryPath = this.getInstalledBinaryPath();
+ await fsPromises.chmod(binaryPath, 0o755);
+ }
+
+ // Verify installation
+ if (await this.isInstalled()) {
+ return { success: true };
+ } else {
+ throw new Error("Installation verification failed");
+ }
+ } catch (error: any) {
+ console.error("llama.cpp installation error:", error);
+ return {
+ success: false,
+ error: error.message || "Installation failed"
+ };
+ }
+ }
+
+ async uninstall(): Promise<{ success: boolean; error?: string }> {
+ try {
+ await fsPromises.rm(this.installDir, { recursive: true, force: true });
+ this.binPath = null;
+ return { success: true };
+ } catch (error: any) {
+ return {
+ success: false,
+ error: error.message || "Uninstall failed"
+ };
+ }
+ }
+}
+
+export default new LlamaCppInstaller();
\ No newline at end of file
diff --git a/src/helpers/modelManagerBridge.js b/src/helpers/modelManagerBridge.js
new file mode 100644
index 000000000..b32fe8fd9
--- /dev/null
+++ b/src/helpers/modelManagerBridge.js
@@ -0,0 +1,414 @@
+const path = require("path");
+const { spawn } = require("child_process");
+const fs = require("fs");
+const { promises: fsPromises } = require("fs");
+const https = require("https");
+const { app } = require("electron");
+
+// Import the model registry data directly
+let modelRegistryData;
+try {
+ modelRegistryData = require("../models/modelRegistryData.json");
+ console.log('[ModelManager] Loaded registry data successfully');
+} catch (error) {
+ console.error('[ModelManager] Failed to load registry data:', error);
+ // Fallback to inline data
+ modelRegistryData = {
+ providers: [
+ {
+ id: "qwen",
+ name: "Qwen",
+ baseUrl: "https://huggingface.co",
+ models: [
+ {
+ id: "qwen2.5-0.5b-instruct-q5_k_m",
+ name: "Qwen2.5 0.5B",
+ size: "0.4GB",
+ sizeBytes: 429496729,
+ description: "Smallest model, fast but limited capabilities",
+ fileName: "qwen2.5-0.5b-instruct-q5_k_m.gguf",
+ quantization: "q5_k_m",
+ contextLength: 32768
+ },
+ {
+ id: "qwen2.5-1.5b-instruct-q5_k_m",
+ name: "Qwen2.5 1.5B",
+ size: "1.3GB",
+ sizeBytes: 1395864371,
+ description: "Small model, good for basic tasks",
+ fileName: "qwen2.5-1.5b-instruct-q5_k_m.gguf",
+ quantization: "q5_k_m",
+ contextLength: 32768
+ },
+ {
+ id: "qwen2.5-3b-instruct-q5_k_m",
+ name: "Qwen2.5 3B",
+ size: "2.3GB",
+ sizeBytes: 2469606195,
+ description: "Balanced model for general use",
+ fileName: "qwen2.5-3b-instruct-q5_k_m.gguf",
+ quantization: "q5_k_m",
+ contextLength: 32768
+ },
+ {
+ id: "qwen2.5-7b-instruct-q4km",
+ name: "Qwen2.5 7B",
+ size: "4.7GB",
+ sizeBytes: 5046586241,
+ description: "Large model with high quality (Q4_K_M quantization)",
+ fileName: "qwen2.5-7b-instruct-q4_k_m.gguf",
+ quantization: "q4_k_m",
+ contextLength: 128000,
+ recommended: true
+ },
+ {
+ id: "qwen2.5-7b-instruct-q5_k_m",
+ name: "Qwen2.5 7B",
+ size: "5.4GB",
+ sizeBytes: 5798205849,
+ description: "Large model, high quality reasoning (Q5_K_M quantization)",
+ fileName: "qwen2.5-7b-instruct-q5_k_m.gguf",
+ quantization: "q5_k_m",
+ contextLength: 128000
+ }
+ ]
+ }
+ ]
+ };
+}
+
+class ModelError extends Error {
+ constructor(message, code, details = {}) {
+ super(message);
+ this.name = "ModelError";
+ this.code = code;
+ this.details = details;
+ }
+}
+
+class ModelNotFoundError extends ModelError {
+ constructor(modelId) {
+ super(`Model ${modelId} not found`, "MODEL_NOT_FOUND", { modelId });
+ }
+}
+
+class ModelManager {
+ constructor() {
+ this.modelsDir = this.getModelsDir();
+ this.downloadProgress = new Map();
+ this.activeDownloads = new Map();
+ this.llamaCppPath = null;
+ this.ensureModelsDirExists();
+ }
+
+ getModelsDir() {
+ const homeDir = app.getPath("home");
+ return path.join(homeDir, ".cache", "openwispr", "models");
+ }
+
+ async ensureModelsDirExists() {
+ try {
+ await fsPromises.mkdir(this.modelsDir, { recursive: true });
+ } catch (error) {
+ console.error("Failed to create models directory:", error);
+ }
+ }
+
+ async getAllModels() {
+ try {
+ const models = [];
+
+ console.log('[ModelManager] Getting all models from registry');
+ console.log('[ModelManager] Registry data:', modelRegistryData);
+
+ // Get all models from registry
+ for (const provider of modelRegistryData.providers) {
+ for (const model of provider.models) {
+ const modelPath = path.join(this.modelsDir, model.fileName);
+ const isDownloaded = await this.checkFileExists(modelPath);
+
+ models.push({
+ ...model,
+ providerId: provider.id,
+ providerName: provider.name,
+ isDownloaded,
+ path: isDownloaded ? modelPath : null
+ });
+ }
+ }
+
+ console.log('[ModelManager] Found models:', models.length);
+ return models;
+ } catch (error) {
+ console.error('[ModelManager] Error getting all models:', error);
+ throw error;
+ }
+ }
+
+ async getModelsWithStatus() {
+ return this.getAllModels();
+ }
+
+ async isModelDownloaded(modelId) {
+ const modelInfo = this.findModelById(modelId);
+ if (!modelInfo) return false;
+
+ const modelPath = path.join(this.modelsDir, modelInfo.model.fileName);
+ return this.checkFileExists(modelPath);
+ }
+
+ async checkFileExists(filePath) {
+ try {
+ await fsPromises.access(filePath, fs.constants.F_OK);
+ return true;
+ } catch {
+ return false;
+ }
+ }
+
+ findModelById(modelId) {
+ for (const provider of modelRegistryData.providers) {
+ const model = provider.models.find(m => m.id === modelId);
+ if (model) {
+ return { model, provider };
+ }
+ }
+ return null;
+ }
+
+ async downloadModel(modelId, onProgress) {
+ const modelInfo = this.findModelById(modelId);
+ if (!modelInfo) {
+ throw new ModelNotFoundError(modelId);
+ }
+
+ const { model, provider } = modelInfo;
+ const modelPath = path.join(this.modelsDir, model.fileName);
+
+ // Check if already downloaded
+ if (await this.checkFileExists(modelPath)) {
+ return modelPath;
+ }
+
+ // Check if already downloading
+ if (this.activeDownloads.get(modelId)) {
+ throw new ModelError("Model is already being downloaded", "DOWNLOAD_IN_PROGRESS", { modelId });
+ }
+
+ this.activeDownloads.set(modelId, true);
+
+ try {
+ // Construct download URL based on provider
+ const downloadUrl = this.getDownloadUrl(provider, model);
+
+ await this.downloadFile(downloadUrl, modelPath, (progress, downloadedSize, totalSize) => {
+ this.downloadProgress.set(modelId, {
+ modelId,
+ progress,
+ downloadedSize,
+ totalSize
+ });
+ if (onProgress) {
+ onProgress(progress, downloadedSize, totalSize);
+ }
+ });
+
+ return modelPath;
+ } finally {
+ this.activeDownloads.delete(modelId);
+ this.downloadProgress.delete(modelId);
+ }
+ }
+
+ getDownloadUrl(provider, model) {
+ // Based on the provider type, construct the download URL
+ if (provider.id === 'qwen') {
+ return `https://huggingface.co/Qwen/Qwen2.5-${model.name.split(' ')[1]}-Instruct-GGUF/resolve/main/${model.fileName}`;
+ }
+ // Add more providers as needed
+ throw new ModelError(`Unknown provider: ${provider.id}`, "UNKNOWN_PROVIDER");
+ }
+
+ async downloadFile(url, destPath, onProgress) {
+ return new Promise((resolve, reject) => {
+ const file = fs.createWriteStream(destPath);
+ let downloadedSize = 0;
+ let totalSize = 0;
+
+ https.get(url, {
+ headers: { 'User-Agent': 'OpenWispr/1.0' },
+ timeout: 30000
+ }, (response) => {
+ if (response.statusCode === 302 || response.statusCode === 301) {
+ // Handle redirect
+ file.close();
+ fs.unlinkSync(destPath);
+ return this.downloadFile(response.headers.location, destPath, onProgress)
+ .then(resolve)
+ .catch(reject);
+ }
+
+ if (response.statusCode !== 200) {
+ file.close();
+ fs.unlinkSync(destPath);
+ reject(new ModelError(
+ `Download failed with status ${response.statusCode}`,
+ "DOWNLOAD_FAILED",
+ { statusCode: response.statusCode }
+ ));
+ return;
+ }
+
+ totalSize = parseInt(response.headers['content-length'], 10);
+
+ response.on('data', (chunk) => {
+ downloadedSize += chunk.length;
+ file.write(chunk);
+
+ if (onProgress && totalSize > 0) {
+ const progress = (downloadedSize / totalSize) * 100;
+ onProgress(progress, downloadedSize, totalSize);
+ }
+ });
+
+ response.on('end', () => {
+ file.close();
+ resolve(destPath);
+ });
+
+ response.on('error', (error) => {
+ file.close();
+ fs.unlinkSync(destPath);
+ reject(new ModelError(
+ `Download error: ${error.message}`,
+ "DOWNLOAD_ERROR",
+ { error: error.message }
+ ));
+ });
+ }).on('error', (error) => {
+ file.close();
+ if (fs.existsSync(destPath)) {
+ fs.unlinkSync(destPath);
+ }
+ reject(new ModelError(
+ `Network error: ${error.message}`,
+ "NETWORK_ERROR",
+ { error: error.message }
+ ));
+ });
+ });
+ }
+
+ async deleteModel(modelId) {
+ const modelInfo = this.findModelById(modelId);
+ if (!modelInfo) {
+ throw new ModelNotFoundError(modelId);
+ }
+
+ const modelPath = path.join(this.modelsDir, modelInfo.model.fileName);
+
+ if (await this.checkFileExists(modelPath)) {
+ await fsPromises.unlink(modelPath);
+ }
+ }
+
+ async ensureLlamaCpp() {
+ // Check if llama.cpp is available
+ const llamaCppInstaller = require("./llamaCppInstaller").default;
+ const isInstalled = await llamaCppInstaller.isInstalled() ||
+ await llamaCppInstaller.checkSystemInstallation();
+
+ if (!isInstalled) {
+ throw new ModelError(
+ "llama.cpp is not installed",
+ "LLAMACPP_NOT_INSTALLED"
+ );
+ }
+
+ this.llamaCppPath = await llamaCppInstaller.getLlamaCppPath();
+ return true;
+ }
+
+ async runInference(modelId, prompt, options = {}) {
+ await this.ensureLlamaCpp();
+
+ const modelInfo = this.findModelById(modelId);
+ if (!modelInfo) {
+ throw new ModelNotFoundError(modelId);
+ }
+
+ const modelPath = path.join(this.modelsDir, modelInfo.model.fileName);
+ if (!await this.checkFileExists(modelPath)) {
+ throw new ModelError(
+ `Model ${modelId} is not downloaded`,
+ "MODEL_NOT_DOWNLOADED",
+ { modelId }
+ );
+ }
+
+ // Format the prompt based on the provider
+ const formattedPrompt = this.formatPrompt(modelInfo.provider, prompt, options.systemPrompt || "");
+
+ // Run inference with llama.cpp
+ return new Promise((resolve, reject) => {
+ const args = [
+ "-m", modelPath,
+ "-p", formattedPrompt,
+ "-n", String(options.maxTokens || 512),
+ "--temp", String(options.temperature || 0.7),
+ "--top-k", String(options.topK || 40),
+ "--top-p", String(options.topP || 0.9),
+ "--repeat-penalty", String(options.repeatPenalty || 1.1),
+ "-c", String(options.contextSize || modelInfo.model.contextLength),
+ "-t", String(options.threads || 4),
+ "--no-display-prompt"
+ ];
+
+ const process = spawn(this.llamaCppPath, args);
+ let output = "";
+ let error = "";
+
+ process.stdout.on('data', (data) => {
+ output += data.toString();
+ });
+
+ process.stderr.on('data', (data) => {
+ error += data.toString();
+ });
+
+ process.on('close', (code) => {
+ if (code !== 0) {
+ reject(new ModelError(
+ `Inference failed with code ${code}: ${error}`,
+ "INFERENCE_FAILED",
+ { code, error }
+ ));
+ } else {
+ resolve(output.trim());
+ }
+ });
+
+ process.on('error', (err) => {
+ reject(new ModelError(
+ `Failed to start inference: ${err.message}`,
+ "INFERENCE_START_FAILED",
+ { error: err.message }
+ ));
+ });
+ });
+ }
+
+ formatPrompt(provider, text, systemPrompt) {
+ if (provider.id === 'qwen') {
+ return `<|im_start|>system\n${systemPrompt}<|im_end|>\n<|im_start|>user\n${text}<|im_end|>\n<|im_start|>assistant\n`;
+ }
+ // Add more providers as needed
+ return `${systemPrompt}\n\n${text}`;
+ }
+}
+
+module.exports = {
+ default: new ModelManager(),
+ ModelError,
+ ModelNotFoundError
+};
\ No newline at end of file
diff --git a/src/hooks/useLocalModels.ts b/src/hooks/useLocalModels.ts
new file mode 100644
index 000000000..240ea0930
--- /dev/null
+++ b/src/hooks/useLocalModels.ts
@@ -0,0 +1,105 @@
+import { useState, useEffect, useCallback } from "react";
+import { ModelDefinition } from "../models/ModelRegistry";
+import "../types/electron";
+
+interface ModelWithStatus extends ModelDefinition {
+ isDownloaded: boolean;
+ isDownloading: boolean;
+ downloadProgress: number;
+}
+
+interface DownloadProgress {
+ 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, DownloadProgress>>(new Map());
+
+ const loadModels = useCallback(async () => {
+ try {
+ setIsLoading(true);
+ setError(null);
+ const modelsData = await window.electronAPI.modelGetAll();
+ setModels(modelsData);
+ } catch (err) {
+ setError("Failed to load models");
+ console.error(err);
+ } finally {
+ setIsLoading(false);
+ }
+ }, []);
+
+ useEffect(() => {
+ loadModels();
+
+ // Set up progress listener
+ const handleProgress = (_event: any, data: DownloadProgress) => {
+ setProgressMap(prev => new Map(prev).set(data.modelId, data));
+ };
+
+ window.electronAPI.onModelDownloadProgress(handleProgress);
+
+ return () => {
+ window.electronAPI.removeAllListeners("model-download-progress");
+ };
+ }, [loadModels]);
+
+ const downloadModel = useCallback(async (modelId: string) => {
+ try {
+ await window.electronAPI.modelDownload(modelId);
+ await loadModels();
+ } catch (err) {
+ setError(`Failed to download model: ${(err as Error).message}`);
+ throw err;
+ }
+ }, [loadModels]);
+
+ const deleteModel = useCallback(async (modelId: string) => {
+ try {
+ await window.electronAPI.modelDelete(modelId);
+ await loadModels();
+ } catch (err) {
+ setError(`Failed to delete model: ${(err as Error).message}`);
+ throw err;
+ }
+ }, [loadModels]);
+
+ const getModelProgress = useCallback((modelId: string) => {
+ return progressMap.get(modelId);
+ }, [progressMap]);
+
+ const checkRuntimeAvailable = useCallback(async () => {
+ try {
+ const result = await window.electronAPI.modelCheckRuntime();
+ return result;
+ } catch {
+ return false;
+ }
+ }, []);
+
+ const modelsByProvider = models.reduce((acc, model) => {
+ const providerId = model.id.split('-')[0] || 'other';
+ if (!acc[providerId]) {
+ acc[providerId] = [];
+ }
+ acc[providerId].push(model);
+ return acc;
+ }, {} as Record<string, ModelWithStatus[]>);
+
+ return {
+ models,
+ modelsByProvider,
+ isLoading,
+ error,
+ downloadModel,
+ deleteModel,
+ getModelProgress,
+ isRuntimeAvailable: checkRuntimeAvailable,
+ };
+}
\ No newline at end of file
diff --git a/src/models/ModelRegistry.ts b/src/models/ModelRegistry.ts
new file mode 100644
index 000000000..96f0aa70f
--- /dev/null
+++ b/src/models/ModelRegistry.ts
@@ -0,0 +1,159 @@
+export interface ModelDefinition {
+ id: string;
+ name: string;
+ size: string;
+ sizeBytes: number;
+ description: string;
+ fileName: string;
+ quantization: string;
+ contextLength: number;
+ recommended?: boolean;
+}
+
+export interface ModelProvider {
+ id: string;
+ name: string;
+ baseUrl: string;
+ models: ModelDefinition[];
+ formatPrompt(text: string, systemPrompt: string): string;
+ getDownloadUrl(model: ModelDefinition): string;
+}
+
+export interface InferenceOptions {
+ maxTokens?: number;
+ temperature?: number;
+ topK?: number;
+ topP?: number;
+ repeatPenalty?: number;
+ contextSize?: number;
+ threads?: number;
+ stream?: boolean;
+}
+
+class ModelRegistry {
+ private static instance: ModelRegistry;
+ private providers = new Map<string, ModelProvider>();
+
+ private constructor() {
+ this.registerDefaultProviders();
+ }
+
+ static getInstance(): ModelRegistry {
+ if (!ModelRegistry.instance) {
+ ModelRegistry.instance = new ModelRegistry();
+ }
+ return ModelRegistry.instance;
+ }
+
+ registerProvider(provider: ModelProvider) {
+ this.providers.set(provider.id, provider);
+ }
+
+ getProvider(providerId: string): ModelProvider | undefined {
+ return this.providers.get(providerId);
+ }
+
+ getAllProviders(): ModelProvider[] {
+ return Array.from(this.providers.values());
+ }
+
+ getModel(modelId: string): { model: ModelDefinition; provider: ModelProvider } | undefined {
+ for (const provider of this.providers.values()) {
+ const model = provider.models.find(m => m.id === modelId);
+ if (model) {
+ return { model, provider };
+ }
+ }
+ return undefined;
+ }
+
+ getAllModels(): Array<ModelDefinition & { providerId: string }> {
+ const models: Array<ModelDefinition & { providerId: string }> = [];
+
+ for (const provider of this.providers.values()) {
+ for (const model of provider.models) {
+ models.push({
+ ...model,
+ providerId: provider.id
+ });
+ }
+ }
+
+ return models;
+ }
+
+ private registerDefaultProviders() {
+ // Qwen Provider
+ this.registerProvider({
+ id: 'qwen',
+ name: 'Qwen',
+ baseUrl: 'https://huggingface.co',
+ models: [
+ {
+ id: 'qwen2.5-0.5b-instruct-q5_k_m',
+ name: 'Qwen2.5 0.5B',
+ size: '0.4GB',
+ sizeBytes: 429496729,
+ description: 'Smallest model, fast but limited capabilities',
+ fileName: 'qwen2.5-0.5b-instruct-q5_k_m.gguf',
+ quantization: 'q5_k_m',
+ contextLength: 32768,
+ },
+ {
+ id: 'qwen2.5-1.5b-instruct-q5_k_m',
+ name: 'Qwen2.5 1.5B',
+ size: '1.3GB',
+ sizeBytes: 1395864371,
+ description: 'Small model, good for basic tasks',
+ fileName: 'qwen2.5-1.5b-instruct-q5_k_m.gguf',
+ quantization: 'q5_k_m',
+ contextLength: 32768,
+ },
+ {
+ id: 'qwen2.5-3b-instruct-q5_k_m',
+ name: 'Qwen2.5 3B',
+ size: '2.3GB',
+ sizeBytes: 2469606195,
+ description: 'Balanced model for general use',
+ fileName: 'qwen2.5-3b-instruct-q5_k_m.gguf',
+ quantization: 'q5_k_m',
+ contextLength: 32768,
+ },
+ {
+ id: 'qwen2.5-7b-instruct-q5_k_m',
+ name: 'Qwen2.5 7B',
+ size: '5.4GB',
+ sizeBytes: 5798205849,
+ description: 'Large model, high quality reasoning',
+ fileName: 'qwen2.5-7b-instruct-q5_k_m.gguf',
+ quantization: 'q5_k_m',
+ contextLength: 128000,
+ recommended: true,
+ },
+ ],
+ formatPrompt(text: string, systemPrompt: string): string {
+ return `<|im_start|>system
+${systemPrompt}<|im_end|>
+<|im_start|>user
+${text}<|im_end|>
+<|im_start|>assistant
+`;
+ },
+ getDownloadUrl(model: ModelDefinition): string {
+ return `${this.baseUrl}/Qwen/Qwen2.5-${model.name.split(' ')[1]}-Instruct-GGUF/resolve/main/${model.fileName}`;
+ }
+ });
+
+ // Future: Add Mistral provider
+ // this.registerProvider({
+ // id: 'mistral',
+ // name: 'Mistral AI',
+ // baseUrl: 'https://huggingface.co',
+ // models: [...],
+ // formatPrompt: ...,
+ // getDownloadUrl: ...
+ // });
+ }
+}
+
+export const modelRegistry = ModelRegistry.getInstance();
\ No newline at end of file
diff --git a/src/models/modelRegistryData.json b/src/models/modelRegistryData.json
new file mode 100644
index 000000000..544032de5
--- /dev/null
+++ b/src/models/modelRegistryData.json
@@ -0,0 +1,62 @@
+{
+ "providers": [
+ {
+ "id": "qwen",
+ "name": "Qwen",
+ "baseUrl": "https://huggingface.co",
+ "models": [
+ {
+ "id": "qwen2.5-0.5b-instruct-q5_k_m",
+ "name": "Qwen2.5 0.5B",
+ "size": "0.4GB",
+ "sizeBytes": 429496729,
+ "description": "Smallest model, fast but limited capabilities",
+ "fileName": "qwen2.5-0.5b-instruct-q5_k_m.gguf",
+ "quantization": "q5_k_m",
+ "contextLength": 32768
+ },
+ {
+ "id": "qwen2.5-1.5b-instruct-q5_k_m",
+ "name": "Qwen2.5 1.5B",
+ "size": "1.3GB",
+ "sizeBytes": 1395864371,
+ "description": "Small model, good for basic tasks",
+ "fileName": "qwen2.5-1.5b-instruct-q5_k_m.gguf",
+ "quantization": "q5_k_m",
+ "contextLength": 32768
+ },
+ {
+ "id": "qwen2.5-3b-instruct-q5_k_m",
+ "name": "Qwen2.5 3B",
+ "size": "2.3GB",
+ "sizeBytes": 2469606195,
+ "description": "Balanced model for general use",
+ "fileName": "qwen2.5-3b-instruct-q5_k_m.gguf",
+ "quantization": "q5_k_m",
+ "contextLength": 32768
+ },
+ {
+ "id": "qwen2.5-7b-instruct-q4km",
+ "name": "Qwen2.5 7B",
+ "size": "4.7GB",
+ "sizeBytes": 5046586241,
+ "description": "Large model with high quality (Q4_K_M quantization)",
+ "fileName": "qwen2.5-7b-instruct-q4_k_m.gguf",
+ "quantization": "q4_k_m",
+ "contextLength": 128000,
+ "recommended": true
+ },
+ {
+ "id": "qwen2.5-7b-instruct-q5_k_m",
+ "name": "Qwen2.5 7B",
+ "size": "5.4GB",
+ "sizeBytes": 5798205849,
+ "description": "Large model, high quality reasoning (Q5_K_M quantization)",
+ "fileName": "qwen2.5-7b-instruct-q5_k_m.gguf",
+ "quantization": "q5_k_m",
+ "contextLength": 128000
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/src/services/BaseReasoningService.ts b/src/services/BaseReasoningService.ts
new file mode 100644
index 000000000..a5d23edd7
--- /dev/null
+++ b/src/services/BaseReasoningService.ts
@@ -0,0 +1,77 @@
+export interface ReasoningConfig {
+ maxTokens?: number;
+ temperature?: number;
+ contextSize?: number;
+}
+
+export abstract class BaseReasoningService {
+ protected isProcessing = false;
+
+ /**
+ * Get reasoning prompt
+ */
+ protected getReasoningPrompt(
+ text: string,
+ agentName: string | null,
+ config: ReasoningConfig = {}
+ ): string {
+ // Default prompts
+ const DEFAULT_AGENT_PROMPT = `You are {{agentName}}, a helpful AI assistant. Process and improve the following text, removing any reference to your name from the output:\n\n{{text}}\n\nImproved text:`;
+ const DEFAULT_REGULAR_PROMPT = `Process and improve the following text:\n\n{{text}}\n\nImproved text:`;
+
+ // Get custom prompts from localStorage if available
+ let agentPrompt = DEFAULT_AGENT_PROMPT;
+ let regularPrompt = DEFAULT_REGULAR_PROMPT;
+
+ if (typeof window !== 'undefined' && window.localStorage) {
+ const customPrompts = window.localStorage.getItem('customPrompts');
+ if (customPrompts) {
+ try {
+ const parsed = JSON.parse(customPrompts);
+ agentPrompt = parsed.agent || DEFAULT_AGENT_PROMPT;
+ regularPrompt = parsed.regular || DEFAULT_REGULAR_PROMPT;
+ } catch (error) {
+ console.error('Failed to parse custom prompts:', error);
+ }
+ }
+ }
+
+ // Simple prompt construction
+ if (agentName && text.toLowerCase().includes(agentName.toLowerCase())) {
+ // Agent-based prompt - replace placeholders
+ return agentPrompt
+ .replace(/\{\{agentName\}\}/g, agentName)
+ .replace(/\{\{text\}\}/g, text);
+ }
+
+ // Regular prompt - replace placeholders
+ return regularPrompt.replace(/\{\{text\}\}/g, text);
+ }
+
+ /**
+ * Calculate optimal max tokens based on input length
+ */
+ protected calculateMaxTokens(
+ textLength: number,
+ minTokens = 100,
+ maxTokens = 2048,
+ multiplier = 2
+ ): number {
+ return Math.max(minTokens, Math.min(textLength * multiplier, maxTokens));
+ }
+
+ /**
+ * Check if service is available
+ */
+ abstract isAvailable(): Promise<boolean>;
+
+ /**
+ * Process text with reasoning
+ */
+ abstract processText(
+ text: string,
+ modelId: string,
+ agentName?: string | null,
+ config?: ReasoningConfig
+ ): Promise<string>;
+}
\ No newline at end of file
diff --git a/src/services/LocalReasoningService.ts b/src/services/LocalReasoningService.ts
new file mode 100644
index 000000000..522cffac5
--- /dev/null
+++ b/src/services/LocalReasoningService.ts
@@ -0,0 +1,80 @@
+import modelManager from "../helpers/ModelManager";
+import { inferenceConfig } from "../config/InferenceConfig";
+import { BaseReasoningService } from "./BaseReasoningService";
+import { TOKEN_LIMITS } from "../config/constants";
+
+interface LocalReasoningConfig {
+ maxTokens?: number;
+ temperature?: number;
+ contextSize?: number;
+}
+
+class LocalReasoningService extends BaseReasoningService {
+ async processText(
+ text: string,
+ modelId: string = "qwen2.5-7b-instruct-q5_k_m",
+ agentName: string | null = null,
+ config: LocalReasoningConfig = {}
+ ): Promise<string> {
+ if (this.isProcessing) {
+ throw new Error("Already processing a request");
+ }
+
+ this.isProcessing = true;
+
+ try {
+ // Get prompt using the base class method
+ const reasoningPrompt = this.getReasoningPrompt(text, agentName, config);
+
+ // Get optimized config for reasoning use case
+ const inferenceOptions = inferenceConfig.getConfigForUseCase('reasoning');
+
+ // Calculate max tokens with configurable limits
+ const maxTokens = config.maxTokens || this.calculateMaxTokens(
+ text.length,
+ TOKEN_LIMITS.MIN_TOKENS,
+ TOKEN_LIMITS.MAX_TOKENS,
+ TOKEN_LIMITS.TOKEN_MULTIPLIER
+ );
+
+ // Run inference
+ const result = await modelManager.runInference(modelId, reasoningPrompt, {
+ ...inferenceOptions,
+ maxTokens,
+ temperature: config.temperature || inferenceOptions.temperature,
+ contextSize: config.contextSize || TOKEN_LIMITS.REASONING_CONTEXT_SIZE,
+ });
+
+ return result;
+ } catch (error) {
+ console.error("LocalReasoningService error:", error);
+ throw error;
+ } finally {
+ this.isProcessing = false;
+ }
+ }
+
+ // Check if local reasoning is available
+ async isAvailable(): Promise<boolean> {
+ try {
+ await modelManager.ensureLlamaCpp();
+ return true;
+ } catch (error) {
+ console.warn("Local reasoning not available:", (error as Error).message);
+ return false;
+ }
+ }
+
+ // Get list of downloaded models
+ async getDownloadedModels() {
+ try {
+ const modelsWithStatus = await modelManager.getModelsWithStatus();
+ return modelsWithStatus.filter(model => model.isDownloaded);
+ } catch (error) {
+ console.error("Failed to get downloaded models:", error);
+ return [];
+ }
+ }
+}
+
+export default new LocalReasoningService();
\ No newline at end of file
diff --git a/src/services/ReasoningService.js b/src/services/ReasoningService.js
deleted file mode 100644
index 72e4e7a93..000000000
--- a/src/services/ReasoningService.js
+++ /dev/null
@@ -1,273 +0,0 @@
-import { getModelProvider } from "../utils/languages.ts";
-import { getAgentName } from "../utils/agentName.ts";
-
-// Default prompts (fallback if no custom prompts are saved)
-export const DEFAULT_PROMPTS = {
- agent: `You are {{agentName}}, an advanced AI assistant specializing in text processing and transformation. The user has mentioned your name "{{agentName}}" somewhere in their message, indicating they want you to perform specific actions.
-
-## Core Understanding
-- Your name "{{agentName}}" may appear ANYWHERE in the text - beginning, middle, or end
-- The user may address you casually ("{{agentName}}", "hey {{agentName}}") or formally
-- Instructions may come before or after your name is mentioned
-- Context is key - understand the full message before processing
-
-## Command Recognition
-Identify and execute these types of requests:
-- **Editing**: "scratch that", "delete", "remove", "ignore the previous", "actually never mind"
-- **Formatting**: "make this professional", "format as list", "clean this up", "organize", "structure"
-- **Transformation**: "rewrite", "convert to", "change to", "make it sound", "turn this into"
-- **Creation**: "write", "create", "draft", "compose", "generate"
-- **Enhancement**: "improve", "expand", "elaborate", "add details", "make it better"
-
-## Processing Rules
-1. **Context Analysis**: Read the ENTIRE message to understand what the user wants
-2. **Intelligent Parsing**: Separate instructions from content to be processed
-3. **Natural Understanding**: Handle incomplete sentences, corrections, and natural speech patterns
-4. **Clean Output**: Remove your name and meta-instructions from the final result
-5. **Preserve Intent**: Maintain the user's voice, tone and meaning while executing their request
-
-## Examples of Natural Usage
-- "So I was telling John about the quarterly projections and then oh wait {{agentName}} scratch all that and just write that we're on track for Q4"
-- "Dear Sarah I hope this email finds you well I wanted to discuss the marketing budget um {{agentName}} actually make this sound more professional and add a proper greeting"
-- "The meeting notes from today first we discussed revenue which is up 12 percent then operations talked about the new warehouse then HR mentioned the hiring freeze {{agentName}} please format this properly with bullet points and sections"
-- "I'm writing to follow up on our conversation about the partnership deal and uh {{agentName}} can you clean this up and make it sound less desperate but still urgent you know what I mean"
-- "List of things to do today buy groceries call mom finish the report wait no {{agentName}} organize this into categories like personal and work tasks"
-- "This is for the client presentation so we've seen significant growth in user engagement particularly in the 18 to 34 demographic our conversion rates have improved by um let me think {{agentName}} just make this sound more polished keep the numbers but make it flow better"
-- "Email to the team about the deadline so basically we need everyone to submit their parts by Friday no exceptions {{agentName}} rewrite this but make it sound encouraging not threatening"
-- "I was gonna write about our Q3 performance but actually {{agentName}} forget that and draft a message about the upcoming team building event make it fun and casual"
-
-## Your Task
-Analyze the following transcript, identify what the user wants you to do, and provide the processed result:
-
-"{{text}}"
-
-## Output
-Provide ONLY the processed text without any meta-commentary or explanations:`,
-
- regular: `You are an intelligent text processing system designed to clean and enhance voice-to-text transcriptions while preserving authentic human communication.
-
-## Core Capabilities
-1. **Error Correction**: Fix speech recognition errors, typos, and grammatical mistakes
-2. **Natural Language Understanding**: Recognize and execute inline editing commands
-3. **Intelligent Formatting**: Apply appropriate structure based on content context
-4. **Tone Preservation**: Maintain the speaker's voice, style, and personality
-
-## Command Recognition
-Process these natural language commands when they appear in the text:
-- **Deletion**: "scratch that", "ignore that", "delete the previous", "never mind", "forget I said that", "no wait delete that part", "remove what I just said", "erase that"
-- **Correction**: "I mean", "actually", "wait no", "correction", "let me rephrase", "sorry I meant", "that should be", "change that to"
-- **Formatting**: "put this in a list", "new paragraph", "quote", "bullet points", "make this a numbered list", "indent that", "break this up"
-- **Emphasis**: "in caps", "bold that", "emphasize", "highlight", "make that stand out", "underline", "put that in quotes"
-- **Structure**: "new section", "add a heading", "start a new topic", "separate these points", "group these together"
-
-## Processing Guidelines
-1. **Context-Aware**: Understand the full message before making changes
-2. **Smart Punctuation**: Add appropriate punctuation based on speech patterns
-3. **Paragraph Detection**: Create natural paragraph breaks based on topic shifts
-4. **List Recognition**: Automatically format sequential items as lists when appropriate
-5. **Preserve Meaning**: Never alter the core message or add information
-
-## Quality Standards
-- Fix obvious errors without being overly prescriptive
-- Maintain conversational flow and natural rhythm
-- Apply consistent formatting throughout
-- Handle interruptions and self-corrections gracefully
-- Recognize and preserve intentional informality
-
-## Special Handling
-- **Numbers**: Spell out or use digits based on context (dates: "May 15th", prices: "$250", quantities: "25 units")
-- **Abbreviations**: Expand or maintain based on formality level (CEO vs Chief Executive Officer)
-- **Filler Words**: Remove excessive "um", "uh", "like", "you know", "basically", "so yeah" unless characteristically important
-- **Repetitions**: Clean up unintentional repetitions while preserving emphasis ("very very important" → "very important" unless emphasis intended)
-- **Run-on Sentences**: Break up long streams of consciousness into logical sentences
-- **Time References**: Convert casual time mentions ("yesterday", "last week") to specific dates when possible
-- **Contractions**: Adjust based on formality ("don't" → "do not" for formal texts)
-
-## Real-World Examples
-- Input: "So um I wanted to talk about the sales figures from last quarter which were pretty good I mean really good actually we exceeded targets by like 15 percent or was it 16 percent anyway the point is"
- Output: "I wanted to discuss the sales figures from last quarter, which exceeded our targets by approximately 15-16 percent."
-
-- Input: "Dear Mr Johnson no wait that's too formal Hi David I hope you're doing well I'm writing to follow up on our meeting yesterday about the new project timeline"
- Output: "Hi David, I hope you're doing well. I'm writing to follow up on our meeting yesterday about the new project timeline."
-
-- Input: "Meeting notes okay so first thing we talked about was budget that's going up by twenty thousand dollars then marketing presented their Q4 campaign then oh I forgot to mention HR announced the new vacation policy put that at the beginning actually"
- Output: "Meeting Notes:\n\n1. HR announced the new vacation policy\n2. Budget increase of $20,000\n3. Marketing presented their Q4 campaign"
-
-## Input
-Process this transcript:
-
-"{{text}}"
-
-## Output
-Provide the cleaned and formatted text:`};
-
-const getReasoningPrompt = (text, agentName) => {
- // Try to load custom prompts from localStorage
- let customPrompts;
- try {
- const saved = localStorage.getItem("customPrompts");
- customPrompts = saved ? JSON.parse(saved) : null;
- } catch (error) {
- console.warn("Failed to load custom prompts, using defaults:", error);
- customPrompts = null;
- }
-
- // Use custom prompts if available, otherwise use defaults
- const prompts = customPrompts || DEFAULT_PROMPTS;
-
- const hasAgentReference =
- agentName &&
- (text.toLowerCase().includes(`hey ${agentName.toLowerCase()}`) ||
- text.toLowerCase().includes(agentName.toLowerCase()));
-
- // Get the appropriate prompt template
- const promptTemplate = hasAgentReference ? prompts.agent : prompts.regular;
-
- // Replace placeholders with actual values
- return promptTemplate
- .replace(/\{\{agentName\}\}/g, agentName)
- .replace(/\{\{text\}\}/g, text);
-};
-
-class ReasoningService {
- constructor() {
- this.apiKeyCache = new Map();
- }
-
- async processText(text, model = "gpt-3.5-turbo") {
- const provider = getModelProvider(model);
-
- try {
- switch (provider) {
- case "openai":
- return await this.processWithOpenAI(text, model);
- case "anthropic":
- return await this.processWithAnthropic(text, model);
- default:
- throw new Error(`Unsupported reasoning provider: ${provider}`);
- }
- } catch (error) {
- console.error(`ReasoningService error (${provider}):`, error.message);
- throw error;
- }
- }
-
- async processWithOpenAI(text, model) {
- const apiKey = await this.getAPIKey("openai");
- const agentName = getAgentName();
- const prompt = getReasoningPrompt(text, agentName).replace(
- "{{text}}",
- text
- );
- const maxTokens = Math.max(100, text.length * 2);
-
- const response = await fetch("https://api.openai.com/v1/chat/completions", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- Authorization: `Bearer ${apiKey}`,
- },
- body: JSON.stringify({
- model,
- messages: [{ role: "user", content: prompt }],
- max_tokens: maxTokens,
- temperature: 0.3,
- }),
- });
-
- if (!response.ok) {
- const errorText = await response.text();
- throw new Error(`OpenAI API Error: ${response.status} - ${errorText}`);
- }
-
- const result = await response.json();
- const reasonedText = result.choices[0]?.message?.content?.trim();
-
- if (!reasonedText) {
- throw new Error("Empty response from OpenAI");
- }
-
- return reasonedText;
- }
-
- async processWithAnthropic(text, model) {
- const apiKey = await this.getAPIKey("anthropic");
- const agentName = getAgentName();
- const prompt = getReasoningPrompt(text, agentName).replace(
- "{{text}}",
- text
- );
- const maxTokens = Math.max(100, text.length * 2);
-
- const response = await fetch("https://api.anthropic.com/v1/messages", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- "x-api-key": apiKey,
- "anthropic-version": "2023-06-01",
- },
- body: JSON.stringify({
- model,
- max_tokens: maxTokens,
- messages: [{ role: "user", content: prompt }],
- temperature: 0.3,
- }),
- });
-
- if (!response.ok) {
- const errorText = await response.text();
- throw new Error(`Anthropic API Error: ${response.status} - ${errorText}`);
- }
-
- const result = await response.json();
- const reasonedText = result.content[0]?.text?.trim();
-
- if (!reasonedText) {
- throw new Error("Empty response from Anthropic");
- }
-
- return reasonedText;
- }
-
- async getAPIKey(provider) {
- // Check cache first
- if (this.apiKeyCache.has(provider)) {
- return this.apiKeyCache.get(provider);
- }
-
- let apiKey;
-
- switch (provider) {
- case "openai":
- apiKey =
- (await window.electronAPI?.getOpenAIKey()) ||
- localStorage.getItem("openaiApiKey");
- break;
- case "anthropic":
- apiKey =
- (await window.electronAPI?.getAnthropicKey()) ||
- localStorage.getItem("anthropicApiKey");
- break;
- default:
- throw new Error(`Unknown provider: ${provider}`);
- }
-
- if (
- !apiKey ||
- apiKey.trim() === "" ||
- apiKey === "your_openai_api_key_here"
- ) {
- throw new Error(`${provider} API key not found`);
- }
-
- // Cache the key
- this.apiKeyCache.set(provider, apiKey);
- return apiKey;
- }
-
- clearCache() {
- this.apiKeyCache.clear();
- }
-}
-
-export default new ReasoningService();
diff --git a/src/services/ReasoningService.ts b/src/services/ReasoningService.ts
new file mode 100644
index 000000000..33f33d395
--- /dev/null
+++ b/src/services/ReasoningService.ts
@@ -0,0 +1,234 @@
+import { getModelProvider } from "../utils/languages";
+import { BaseReasoningService, ReasoningConfig } from "./BaseReasoningService";
+import { SecureCache } from "../utils/SecureCache";
+import { withRetry, createApiRetryStrategy } from "../utils/retry";
+import { API_ENDPOINTS, API_VERSIONS, TOKEN_LIMITS } from "../config/constants";
+
+export const DEFAULT_PROMPTS = {
+ agent: `You are {{agentName}}, a helpful AI assistant. Process and improve the following text, removing any reference to your name from the output:\n\n{{text}}\n\nImproved text:`,
+ regular: `Process and improve the following text:\n\n{{text}}\n\nImproved text:`
+};
+
+class ReasoningService extends BaseReasoningService {
+ private apiKeyCache: SecureCache<string>;
+ private cacheCleanupStop: (() => void) | undefined;
+
+ constructor() {
+ super();
+ this.apiKeyCache = new SecureCache();
+ this.cacheCleanupStop = this.apiKeyCache.startAutoCleanup();
+ }
+
+ async processText(
+ text: string,
+ model: string = "gpt-3.5-turbo",
+ agentName: string | null = null,
+ config: ReasoningConfig = {}
+ ): Promise<string> {
+ const provider = getModelProvider(model);
+
+ try {
+ switch (provider) {
+ case "openai":
+ return await this.processWithOpenAI(text, model, agentName, config);
+ case "anthropic":
+ return await this.processWithAnthropic(text, model, agentName, config);
+ case "local":
+ return await this.processWithLocal(text, model, agentName, config);
+ default:
+ throw new Error(`Unsupported reasoning provider: ${provider}`);
+ }
+ } catch (error) {
+ console.error(`ReasoningService error (${provider}):`, (error as Error).message);
+ throw error;
+ }
+ }
+
+ private async processWithOpenAI(
+ text: string,
+ model: string,
+ agentName: string | null = null,
+ config: ReasoningConfig = {}
+ ): Promise<string> {
+ if (this.isProcessing) {
+ throw new Error("Already processing a request");
+ }
+
+ let apiKey = this.apiKeyCache.get("openai");
+ if (!apiKey) {
+ try {
+ apiKey = await window.electronAPI.getOpenAIKey();
+ if (apiKey) {
+ this.apiKeyCache.set("openai", apiKey);
+ }
+ } catch (error) {
+ console.error("Failed to retrieve OpenAI API key:", error);
+ }
+ }
+
+ if (!apiKey) {
+ throw new Error("OpenAI API key not configured");
+ }
+
+ this.isProcessing = true;
+
+ try {
+ const systemPrompt = "You are a helpful assistant that processes text.";
+ const userPrompt = this.getReasoningPrompt(text, agentName, config);
+
+ const requestBody = {
+ model: model || "gpt-3.5-turbo",
+ messages: [
+ { role: "system", content: systemPrompt },
+ { role: "user", content: userPrompt }
+ ],
+ max_tokens: config.maxTokens || this.calculateMaxTokens(
+ text.length,
+ TOKEN_LIMITS.MIN_TOKENS,
+ TOKEN_LIMITS.MAX_TOKENS,
+ TOKEN_LIMITS.TOKEN_MULTIPLIER
+ ),
+ temperature: config.temperature || 0.3,
+ };
+
+ const response = await withRetry(
+ async () => {
+ const res = await fetch(`${API_ENDPOINTS.OPENAI}/chat/completions`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${apiKey}`,
+ },
+ body: JSON.stringify(requestBody),
+ });
+
+ if (!res.ok) {
+ const errorData = await res.json().catch(() => ({ error: res.statusText }));
+ throw new Error(errorData.error?.message || `OpenAI API error: ${res.status}`);
+ }
+
+ return res.json();
+ },
+ createApiRetryStrategy("OpenAI")
+ );
+
+ return response.choices[0].message.content.trim();
+ } finally {
+ this.isProcessing = false;
+ }
+ }
+
+ private async processWithAnthropic(
+ text: string,
+ model: string,
+ agentName: string | null = null,
+ config: ReasoningConfig = {}
+ ): Promise<string> {
+ if (this.isProcessing) {
+ throw new Error("Already processing a request");
+ }
+
+ let apiKey = this.apiKeyCache.get("anthropic");
+ if (!apiKey) {
+ try {
+ apiKey = await window.electronAPI.getAnthropicKey();
+ if (apiKey) {
+ this.apiKeyCache.set("anthropic", apiKey);
+ }
+ } catch (error) {
+ console.error("Failed to retrieve Anthropic API key:", error);
+ }
+ }
+
+ if (!apiKey) {
+ throw new Error("Anthropic API key not configured");
+ }
+
+ this.isProcessing = true;
+
+ try {
+ const systemPrompt = "You are a helpful assistant that processes text.";
+ const userPrompt = this.getReasoningPrompt(text, agentName, config);
+
+ const requestBody = {
+ model: model || "claude-3-sonnet-20240229",
+ messages: [{ role: "user", content: userPrompt }],
+ system: systemPrompt,
+ max_tokens: config.maxTokens || this.calculateMaxTokens(
+ text.length,
+ TOKEN_LIMITS.MIN_TOKENS_ANTHROPIC,
+ TOKEN_LIMITS.MAX_TOKENS_ANTHROPIC,
+ TOKEN_LIMITS.TOKEN_MULTIPLIER
+ ),
+ temperature: config.temperature || 0.3,
+ };
+
+ const response = await withRetry(
+ async () => {
+ const res = await fetch(`${API_ENDPOINTS.ANTHROPIC}/messages`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "X-API-Key": apiKey!,
+ "anthropic-version": API_VERSIONS.ANTHROPIC,
+ },
+ body: JSON.stringify(requestBody),
+ });
+
+ if (!res.ok) {
+ const errorData = await res.json().catch(() => ({ error: res.statusText }));
+ throw new Error(errorData.error?.message || `Anthropic API error: ${res.status}`);
+ }
+
+ return res.json();
+ },
+ createApiRetryStrategy("Anthropic")
+ );
+
+ return response.content[0].text.trim();
+ } finally {
+ this.isProcessing = false;
+ }
+ }
+
+ private async processWithLocal(
+ text: string,
+ model: string,
+ agentName: string | null = null,
+ config: ReasoningConfig = {}
+ ): Promise<string> {
+ // Instead of importing directly, we'll use IPC to communicate with main process
+ // For local models, we need to use IPC to communicate with the main process
+ if (typeof window !== 'undefined' && window.electronAPI) {
+ const result = await window.electronAPI.processLocalReasoning(text, model, agentName, config);
+ if (result.success) {
+ return result.text;
+ } else {
+ throw new Error(result.error);
+ }
+ } else {
+ throw new Error('Local reasoning is not available in this environment');
+ }
+ }
+
+ async isAvailable(): Promise<boolean> {
+ try {
+ // Check if we have at least one configured API key or local model available
+ const openaiKey = await window.electronAPI?.getOpenAIKey?.();
+ const anthropicKey = await window.electronAPI?.getAnthropicKey?.();
+ const localAvailable = await window.electronAPI?.checkLocalReasoningAvailable?.();
+
+ return !!(openaiKey || anthropicKey || localAvailable);
+ } catch (error) {
+ return false;
+ }
+ }
+
+ destroy(): void {
+ if (this.cacheCleanupStop) {
+ this.cacheCleanupStop();
+ }
+ }
+}
+
+export default new ReasoningService();
\ No newline at end of file
diff --git a/src/services/localReasoningBridge.js b/src/services/localReasoningBridge.js
new file mode 100644
index 000000000..6d4896f02
--- /dev/null
+++ b/src/services/localReasoningBridge.js
@@ -0,0 +1,89 @@
+const modelManager = require("../helpers/modelManagerBridge").default;
+
+class LocalReasoningService {
+ constructor() {
+ this.isProcessing = false;
+ }
+
+ async isAvailable() {
+ try {
+ // Check if llama.cpp is installed
+ await modelManager.ensureLlamaCpp();
+
+ // Check if at least one model is downloaded
+ const models = await modelManager.getAllModels();
+ return models.some(model => model.isDownloaded);
+ } catch (error) {
+ return false;
+ }
+ }
+
+ async processText(text, modelId, agentName = null, config = {}) {
+ if (this.isProcessing) {
+ throw new Error("Already processing a request");
+ }
+
+ this.isProcessing = true;
+
+ try {
+ // Get custom prompts from the request context
+ const customPrompts = config.customPrompts || null;
+
+ // Build the reasoning prompt
+ const reasoningPrompt = this.getReasoningPrompt(text, agentName, customPrompts);
+
+ // Run inference
+ const result = await modelManager.runInference(modelId, reasoningPrompt, {
+ maxTokens: config.maxTokens || this.calculateMaxTokens(text.length),
+ temperature: config.temperature || 0.7,
+ topK: config.topK || 40,
+ topP: config.topP || 0.9,
+ repeatPenalty: config.repeatPenalty || 1.1,
+ contextSize: config.contextSize || 4096,
+ threads: config.threads || 4,
+ systemPrompt: "You are a helpful AI assistant that processes and improves text."
+ });
+
+ return result;
+ } finally {
+ this.isProcessing = false;
+ }
+ }
+
+ getCustomPrompts() {
+ // In main process, we can't access localStorage directly
+ // This should be passed from the renderer process
+ return null;
+ }
+
+ getReasoningPrompt(text, agentName, customPrompts) {
+ // Default prompts
+ const DEFAULT_AGENT_PROMPT = `You are {{agentName}}, a helpful AI assistant. Process and improve the following text, removing any reference to your name from the output:\n\n{{text}}\n\nImproved text:`;
+ const DEFAULT_REGULAR_PROMPT = `Process and improve the following text:\n\n{{text}}\n\nImproved text:`;
+
+ let agentPrompt = DEFAULT_AGENT_PROMPT;
+ let regularPrompt = DEFAULT_REGULAR_PROMPT;
+
+ if (customPrompts) {
+ agentPrompt = customPrompts.agent || DEFAULT_AGENT_PROMPT;
+ regularPrompt = customPrompts.regular || DEFAULT_REGULAR_PROMPT;
+ }
+
+ // Check if agent name is mentioned
+ if (agentName && text.toLowerCase().includes(agentName.toLowerCase())) {
+ return agentPrompt
+ .replace(/\{\{agentName\}\}/g, agentName)
+ .replace(/\{\{text\}\}/g, text);
+ }
+
+ return regularPrompt.replace(/\{\{text\}\}/g, text);
+ }
+
+ calculateMaxTokens(textLength, minTokens = 100, maxTokens = 2048, multiplier = 2) {
+ return Math.max(minTokens, Math.min(textLength * multiplier, maxTokens));
+ }
+}
+
+module.exports = {
+ default: new LocalReasoningService()
+};
\ No newline at end of file
diff --git a/src/types/electron.ts b/src/types/electron.ts
index f2c1e18b4..10c86cc42 100644
--- a/src/types/electron.ts
+++ b/src/types/electron.ts
@@ -125,10 +125,26 @@ declare global {
getOpenAIKey: () => Promise<string>;
saveOpenAIKey: (key: string) => Promise<{ success: boolean }>;
createProductionEnvFile: (key: string) => Promise<void>;
+ getAnthropicKey: () => Promise<string | null>;
+ saveAnthropicKey: (key: string) => Promise<void>;
// Clipboard operations
readClipboard: () => Promise<string>;
writeClipboard: (text: string) => Promise<{ success: boolean }>;
+ pasteFromClipboard: () => Promise<{ success: boolean; error?: string }>;
+ pasteFromClipboardWithFallback: () => Promise<{ success: boolean; error?: string }>;
+
+ // Settings
+ getSettings: () => Promise<any>;
+ updateSettings: (settings: any) => Promise<void>;
+
+ // Audio
+ getAudioDevices: () => Promise<MediaDeviceInfo[]>;
+ transcribeAudio: (audioData: ArrayBuffer) => Promise<{
+ success: boolean;
+ text?: string;
+ error?: string;
+ }>;
// Python operations
checkPythonInstallation: () => Promise<PythonInstallation>;
@@ -162,14 +178,35 @@ declare global {
error?: string;
}>;
+ // Local AI model management
+ modelGetAll: () => Promise<any[]>;
+ modelCheck: (modelId: string) => Promise<boolean>;
+ modelDownload: (modelId: string) => Promise<void>;
+ modelDelete: (modelId: string) => Promise<void>;
+ modelCheckRuntime: () => Promise<boolean>;
+ onModelDownloadProgress: (callback: (event: any, data: any) => void) => void;
+
+ // Local reasoning
+ processLocalReasoning: (text: string, modelId: string, agentName: string | null, config: any) => Promise<{ success: boolean; text?: string; error?: string }>;
+ checkLocalReasoningAvailable: () => Promise<boolean>;
+
+ // llama.cpp management
+ llamaCppCheck: () => Promise<{ isInstalled: boolean; version?: string }>;
+ llamaCppInstall: () => Promise<{ success: boolean; error?: string }>;
+ llamaCppUninstall: () => Promise<{ success: boolean; error?: string }>;
+
// Window control operations
windowMinimize: () => Promise<void>;
windowMaximize: () => Promise<void>;
windowClose: () => Promise<void>;
windowIsMaximized: () => Promise<boolean>;
+ startWindowDrag: () => Promise<void>;
+ stopWindowDrag: () => Promise<void>;
// App management
- cleanupApp: () => Promise<void>;
+ cleanupApp: () => Promise<{ success: boolean; message: string }>;
+ getTranscriptionHistory: () => Promise<any[]>;
+ clearTranscriptionHistory: () => Promise<void>;
// Update operations
checkForUpdates: () => Promise<UpdateCheckResult>;
@@ -193,13 +230,17 @@ declare global {
// External URL operations
openExternal: (
url: string
- ) => Promise<{ success: boolean; error?: string }>;
+ ) => Promise<{ success: boolean; error?: string } | void>;
// Event listener cleanup
- removeAllListeners?: (channel: string) => void;
+ removeAllListeners: (channel: string) => void;
// Hotkey management
- updateHotkey?: (key: string) => Promise<void>;
+ updateHotkey: (key: string) => Promise<void>;
+ };
+
+ api?: {
+ sendDebugLog: (message: string) => void;
};
}
}
diff --git a/src/utils/SecureCache.ts b/src/utils/SecureCache.ts
new file mode 100644
index 000000000..a9fba6c66
--- /dev/null
+++ b/src/utils/SecureCache.ts
@@ -0,0 +1,66 @@
+import { CACHE_CONFIG } from "../config/constants";
+
+interface CacheEntry<T> {
+ value: T;
+ expiresAt: number;
+}
+
+export class SecureCache<T> {
+ private cache = new Map<string, CacheEntry<T>>();
+ private readonly ttl: number;
+
+ constructor(ttlMs: number = CACHE_CONFIG.API_KEY_TTL) {
+ this.ttl = ttlMs;
+ }
+
+ set(key: string, value: T): void {
+ this.cache.set(key, {
+ value,
+ expiresAt: Date.now() + this.ttl,
+ });
+ }
+
+ get(key: string): T | undefined {
+ const entry = this.cache.get(key);
+
+ if (!entry) {
+ return undefined;
+ }
+
+ if (Date.now() > entry.expiresAt) {
+ this.cache.delete(key);
+ return undefined;
+ }
+
+ return entry.value;
+ }
+
+ has(key: string): boolean {
+ return this.get(key) !== undefined;
+ }
+
+ delete(key: string): boolean {
+ return this.cache.delete(key);
+ }
+
+ clear(): void {
+ this.cache.clear();
+ }
+
+ // Clean up expired entries
+ cleanup(): void {
+ const now = Date.now();
+ const entries = Array.from(this.cache.entries());
+ for (const [key, entry] of entries) {
+ if (now > entry.expiresAt) {
+ this.cache.delete(key);
+ }
+ }
+ }
+
+ // Auto cleanup on interval
+ startAutoCleanup(intervalMs: number = 60000): () => void {
+ const interval = setInterval(() => this.cleanup(), intervalMs);
+ return () => clearInterval(interval);
+ }
+}
\ No newline at end of file
diff --git a/src/utils/formatBytes.ts b/src/utils/formatBytes.ts
new file mode 100644
index 000000000..a05006e93
--- /dev/null
+++ b/src/utils/formatBytes.ts
@@ -0,0 +1,11 @@
+export function formatBytes(bytes: number, decimals = 2): string {
+ if (bytes === 0) return "0 Bytes";
+
+ const k = 1024;
+ const dm = decimals < 0 ? 0 : decimals;
+ const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
+
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
+
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
+}
\ No newline at end of file
diff --git a/src/utils/languages.ts b/src/utils/languages.ts
index f168d257a..3d9370210 100644
--- a/src/utils/languages.ts
+++ b/src/utils/languages.ts
@@ -1,3 +1,5 @@
+import { modelRegistry } from "../models/ModelRegistry";
+
export const LANGUAGE_OPTIONS = [
{ value: "auto", label: "Auto-detect" },
{ value: "af", label: "Afrikaans" },
@@ -96,8 +98,20 @@ export const REASONING_PROVIDERS = {
},
],
},
+ local: {
+ name: "Local AI",
+ models: [], // Will be populated dynamically
+ },
};
+// Dynamically populate local models from registry
+const localModels = modelRegistry.getAllModels();
+REASONING_PROVIDERS.local.models = localModels.map(model => ({
+ value: model.id,
+ label: model.name,
+ description: `${model.description} (${model.size})`,
+}));
+
export const getAllReasoningModels = () => {
return Object.entries(REASONING_PROVIDERS).flatMap(([providerId, provider]) =>
provider.models.map((model) => ({
@@ -118,4 +132,4 @@ export const getModelProvider = (modelId: string): string => {
const allModels = getAllReasoningModels();
const model = allModels.find((m) => m.value === modelId);
return model?.provider || "openai";
-};
+};
\ No newline at end of file
diff --git a/src/utils/retry.ts b/src/utils/retry.ts
new file mode 100644
index 000000000..22f4396d0
--- /dev/null
+++ b/src/utils/retry.ts
@@ -0,0 +1,69 @@
+import { RETRY_CONFIG } from "../config/constants";
+
+export interface RetryOptions {
+ maxRetries?: number;
+ initialDelay?: number;
+ maxDelay?: number;
+ backoffMultiplier?: number;
+ shouldRetry?: (error: any) => boolean;
+}
+
+export async function withRetry<T>(
+ fn: () => Promise<T>,
+ options: RetryOptions = {}
+): Promise<T> {
+ const {
+ maxRetries = RETRY_CONFIG.MAX_RETRIES,
+ initialDelay = RETRY_CONFIG.INITIAL_DELAY,
+ maxDelay = RETRY_CONFIG.MAX_DELAY,
+ backoffMultiplier = RETRY_CONFIG.BACKOFF_MULTIPLIER,
+ shouldRetry = () => true,
+ } = options;
+
+ let lastError: any;
+ let delay = initialDelay;
+
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
+ try {
+ return await fn();
+ } catch (error) {
+ lastError = error;
+
+ if (attempt === maxRetries || !shouldRetry(error)) {
+ throw error;
+ }
+
+ // Wait before retrying with exponential backoff
+ await new Promise(resolve => setTimeout(resolve, delay));
+ delay = Math.min(delay * backoffMultiplier, maxDelay);
+ }
+ }
+
+ throw lastError;
+}
+
+// Specific retry strategy for API calls
+export function createApiRetryStrategy() {
+ return {
+ shouldRetry: (error: any) => {
+ // Retry on network errors or 5xx status codes
+ if (!error.response) return true; // Network error
+
+ const status = error.response?.status || error.status;
+ return status >= 500 && status < 600;
+ },
+ };
+}
+
+// Specific retry strategy for file operations
+export function createFileRetryStrategy() {
+ return {
+ shouldRetry: (error: any) => {
+ // Retry on temporary file system errors
+ const retriableErrors = ['EBUSY', 'ENOENT', 'EPERM', 'EAGAIN'];
+ return retriableErrors.includes(error.code);
+ },
+ maxRetries: 2,
+ initialDelay: 500,
+ };
+}
\ No newline at end of file
diff --git a/src/vite.config.mjs b/src/vite.config.mjs
index 1241aee31..8aa51546a 100644
--- a/src/vite.config.mjs
+++ b/src/vite.config.mjs
@@ -22,5 +22,23 @@ export default defineConfig({
build: {
outDir: 'dist',
assetsDir: 'assets',
+ rollupOptions: {
+ external: [
+ 'electron',
+ 'fs',
+ 'path',
+ 'child_process',
+ 'https',
+ 'http',
+ 'crypto',
+ 'os',
+ 'stream',
+ 'util',
+ 'zlib',
+ 'tar',
+ 'unzipper',
+ '@aws-sdk/client-s3'
+ ]
+ }
}
})