fix model loading on power mode change
This commit is contained in:
parent
9704a1ba79
commit
ce5f509a68
@ -52,6 +52,10 @@ class ActiveWindowService: ObservableObject {
|
||||
// Check for URL-specific configuration
|
||||
if let config = PowerModeManager.shared.getConfigurationForURL(currentURL) {
|
||||
logger.debug("⚙️ Found URL Configuration: \(config.name) for URL: \(currentURL)")
|
||||
// Set as active configuration in PowerModeManager
|
||||
await MainActor.run {
|
||||
PowerModeManager.shared.setActiveConfiguration(config)
|
||||
}
|
||||
// Apply URL-specific configuration
|
||||
await applyConfiguration(config)
|
||||
return
|
||||
@ -66,78 +70,87 @@ 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 {
|
||||
PowerModeManager.shared.setActiveConfiguration(config)
|
||||
}
|
||||
|
||||
await applyConfiguration(config)
|
||||
}
|
||||
|
||||
private func applyConfiguration(_ config: PowerModeConfig) async {
|
||||
/// Applies a specific configuration
|
||||
func applyConfiguration(_ config: PowerModeConfig) async {
|
||||
guard let enhancementService = enhancementService else { return }
|
||||
|
||||
|
||||
await MainActor.run {
|
||||
// Only apply settings if power mode is enabled globally
|
||||
if PowerModeManager.shared.isPowerModeEnabled {
|
||||
// Apply AI enhancement settings
|
||||
enhancementService.isEnhancementEnabled = config.isAIEnhancementEnabled
|
||||
enhancementService.useScreenCaptureContext = config.useScreenCapture
|
||||
|
||||
// Handle prompt selection
|
||||
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
|
||||
}
|
||||
// Apply AI enhancement settings
|
||||
enhancementService.isEnhancementEnabled = config.isAIEnhancementEnabled
|
||||
enhancementService.useScreenCaptureContext = config.useScreenCapture
|
||||
|
||||
// Handle prompt selection
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply AI provider and model if specified
|
||||
if config.isAIEnhancementEnabled,
|
||||
let aiService = enhancementService.getAIService() {
|
||||
|
||||
// Apply AI provider and model if specified
|
||||
if config.isAIEnhancementEnabled,
|
||||
let aiService = enhancementService.getAIService() {
|
||||
// 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 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)")
|
||||
}
|
||||
// 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 global AI Provider: \(aiService.selectedProvider.rawValue)")
|
||||
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)
|
||||
}
|
||||
} else {
|
||||
print("🔌 Power Mode is disabled globally - skipping configuration application")
|
||||
return
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
// Apply Whisper model selection - do this outside of MainActor to allow async operations
|
||||
if PowerModeManager.shared.isPowerModeEnabled,
|
||||
let whisperState = self.whisperState,
|
||||
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)")
|
||||
// Apply the model selection immediately
|
||||
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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,6 +25,8 @@ struct PowerModePopover: View {
|
||||
action: {
|
||||
powerModeManager.setActiveConfiguration(powerModeManager.defaultConfig)
|
||||
selectedConfig = powerModeManager.defaultConfig
|
||||
// Apply configuration immediately
|
||||
applySelectedConfiguration()
|
||||
}
|
||||
)
|
||||
|
||||
@ -36,6 +38,8 @@ struct PowerModePopover: View {
|
||||
action: {
|
||||
powerModeManager.setActiveConfiguration(config)
|
||||
selectedConfig = config
|
||||
// Apply configuration immediately
|
||||
applySelectedConfiguration()
|
||||
}
|
||||
)
|
||||
}
|
||||
@ -53,6 +57,15 @@ struct PowerModePopover: View {
|
||||
selectedConfig = powerModeManager.activeConfiguration
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to apply the selected configuration
|
||||
private func applySelectedConfiguration() {
|
||||
Task {
|
||||
if let config = selectedConfig {
|
||||
await ActiveWindowService.shared.applyConfiguration(config)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Row view for each power mode in the popover
|
||||
|
||||
@ -293,7 +293,7 @@ extension WhisperState {
|
||||
// MARK: - Resource Management
|
||||
|
||||
func cleanupModelResources() async {
|
||||
try? await Task.sleep(nanoseconds: 500_000_000)
|
||||
try? await Task.sleep(nanoseconds: 300_000_000)
|
||||
|
||||
await whisperContext?.releaseResources()
|
||||
whisperContext = nil
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user