refactor: decouple screen capture from recording start and tie it to AI enhancement toggle with race condition protection

This commit is contained in:
Beingpax 2025-03-02 19:07:43 +05:45
parent 34fada2b26
commit d7ad7c5acd
2 changed files with 12 additions and 11 deletions

View File

@ -22,6 +22,16 @@ class AIEnhancementService: ObservableObject {
// Select the first prompt (default) if none is selected
selectedPromptId = customPrompts.first?.id
}
// Cancel any existing capture task
currentCaptureTask?.cancel()
// Trigger screen capture when enhancement is enabled and screen capture is on
if isEnhancementEnabled && useScreenCaptureContext {
currentCaptureTask = Task {
await captureScreenContext()
}
}
}
}
@Published var useClipboardContext: Bool {
@ -67,6 +77,7 @@ class AIEnhancementService: ObservableObject {
private let aiService: AIService
private let screenCaptureService: ScreenCaptureService
private var currentCaptureTask: Task<Void, Never>?
private let maxRetries = 3
private let baseTimeout: TimeInterval = 4
private let rateLimitInterval: TimeInterval = 1.0 // 1 request per second

View File

@ -292,17 +292,7 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate {
// Step 1: Apply power mode configuration
await ActiveWindowService.shared.applyConfigurationForCurrentApp()
// Step 2: Handle screen capture if enabled by the configuration
if let enhancementService = self.enhancementService,
enhancementService.isEnhancementEnabled &&
enhancementService.useScreenCaptureContext {
await MainActor.run {
self.messageLog += "Capturing screen context...\n"
}
await enhancementService.captureScreenContext()
}
// Step 3: Load model if needed
// Step 2: Load model if needed
if let currentModel = self.currentModel, self.whisperContext == nil {
do {
try await self.loadModel(currentModel)