From 76f1adc67d5f9927e5660fbd7bc12a21e27d229c Mon Sep 17 00:00:00 2001 From: Beingpax Date: Mon, 26 May 2025 16:10:23 +0545 Subject: [PATCH] Fix loading/unloading of same AI model - skip all operations when model unchanged --- VoiceInk/PowerMode/ActiveWindowService.swift | 43 +++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/VoiceInk/PowerMode/ActiveWindowService.swift b/VoiceInk/PowerMode/ActiveWindowService.swift index f32f45f..4cfee64 100644 --- a/VoiceInk/PowerMode/ActiveWindowService.swift +++ b/VoiceInk/PowerMode/ActiveWindowService.swift @@ -27,14 +27,12 @@ class ActiveWindowService: ObservableObject { func applyConfigurationForCurrentApp() async { // If power mode is disabled, don't do anything guard PowerModeManager.shared.isPowerModeEnabled else { - print("🔌 Power Mode is disabled globally - skipping configuration application") return } guard let frontmostApp = NSWorkspace.shared.frontmostApplication, let bundleIdentifier = frontmostApp.bundleIdentifier else { return } - print("🎯 Active Application: \(frontmostApp.localizedName ?? "Unknown") (\(bundleIdentifier))") await MainActor.run { currentApplication = frontmostApp } @@ -69,7 +67,6 @@ class ActiveWindowService: ObservableObject { // Get configuration for the current app or use default if none exists let config = PowerModeManager.shared.getConfigurationForApp(bundleIdentifier) ?? PowerModeManager.shared.defaultConfig - print("⚡️ Using Configuration: \(config.name) (AI Enhancement: \(config.isAIEnhancementEnabled ? "Enabled" : "Disabled"))") // Set as active configuration in PowerModeManager await MainActor.run { @@ -93,12 +90,10 @@ class ActiveWindowService: ObservableObject { if config.isAIEnhancementEnabled { if let promptId = config.selectedPrompt, let uuid = UUID(uuidString: promptId) { - print("🎯 Applied Prompt: \(promptId)") enhancementService.selectedPromptId = uuid } else { // Auto-select first prompt if none is selected and AI is enabled if let firstPrompt = enhancementService.allPrompts.first { - print("🎯 Auto-selected Prompt: \(firstPrompt.title)") enhancementService.selectedPromptId = firstPrompt.id } } @@ -111,25 +106,18 @@ class ActiveWindowService: ObservableObject { // Apply AI provider if specified, otherwise use current global provider if let providerName = config.selectedAIProvider, let provider = AIProvider(rawValue: providerName) { - print("🤖 Applied AI Provider: \(provider.rawValue)") aiService.selectedProvider = provider // Apply model if specified, otherwise use default model if let model = config.selectedAIModel, !model.isEmpty { - print("🧠 Applied AI Model: \(model)") aiService.selectModel(model) - } else { - print("🧠 Using default model for provider: \(aiService.currentModel)") } - } else { - print("🤖 Using global AI Provider: \(aiService.selectedProvider.rawValue)") } } // Apply language selection if specified if let language = config.selectedLanguage { - print("🌐 Applied Language: \(language)") UserDefaults.standard.set(language, forKey: "SelectedLanguage") // Notify that language has changed to update the prompt NotificationCenter.default.post(name: .languageDidChange, object: nil) @@ -138,18 +126,25 @@ class ActiveWindowService: ObservableObject { // Apply Whisper model selection - do this outside of MainActor to allow async operations if let whisperState = self.whisperState, - let modelName = config.selectedWhisperModel, - let selectedModel = await whisperState.availableModels.first(where: { $0.name == modelName }) { - print("🎤 Applied Whisper Model: \(selectedModel.name)") - await whisperState.setDefaultModel(selectedModel) - - await whisperState.cleanupModelResources() - - do { - try await whisperState.loadModel(selectedModel) - print("🎤 Loaded Whisper Model: \(selectedModel.name)") - } catch { - print("❌ Failed to load Whisper Model: \(error.localizedDescription)") + let modelName = config.selectedWhisperModel { + // Access availableModels on MainActor since it's a published property + let models = await MainActor.run { whisperState.availableModels } + if let selectedModel = models.first(where: { $0.name == modelName }) { + + // Only perform model operations if switching to a different model + let currentModelName = await MainActor.run { whisperState.currentModel?.name } + if currentModelName != modelName { + // Only load/unload if actually changing models + await whisperState.setDefaultModel(selectedModel) + await whisperState.cleanupModelResources() + + do { + try await whisperState.loadModel(selectedModel) + } catch { + // Handle error silently or log if needed + } + } + // If model is the same, do nothing - skip all operations } } }