Add dictionary context to AI enhancement

This commit is contained in:
Beingpax 2025-08-19 21:48:22 +05:45
parent 0bbb2892d9
commit 57b58ec80e
2 changed files with 39 additions and 2 deletions

View File

@ -61,6 +61,7 @@ class AIEnhancementService: ObservableObject {
private let aiService: AIService
private let screenCaptureService: ScreenCaptureService
private let dictionaryContextService: DictionaryContextService
private let baseTimeout: TimeInterval = 30
private let rateLimitInterval: TimeInterval = 1.0
private var lastRequestTime: Date?
@ -70,6 +71,7 @@ class AIEnhancementService: ObservableObject {
self.aiService = aiService
self.modelContext = modelContext
self.screenCaptureService = ScreenCaptureService()
self.dictionaryContextService = DictionaryContextService.shared
self.isEnhancementEnabled = UserDefaults.standard.bool(forKey: "isAIEnhancementEnabled")
self.useClipboardContext = UserDefaults.standard.bool(forKey: "useClipboardContext")
@ -154,8 +156,10 @@ class AIEnhancementService: ObservableObject {
""
}
let contextSection = if !clipboardContext.isEmpty || !screenCaptureContext.isEmpty {
"\n\n<CONTEXT_INFORMATION>\(clipboardContext)\(screenCaptureContext)\n</CONTEXT_INFORMATION>"
let dictionaryContext = dictionaryContextService.getDictionaryContext()
let contextSection = if !clipboardContext.isEmpty || !screenCaptureContext.isEmpty || !dictionaryContext.isEmpty {
"\n\n<CONTEXT_INFORMATION>\(clipboardContext)\(screenCaptureContext)\(dictionaryContext)\n</CONTEXT_INFORMATION>"
} else {
""
}

View File

@ -0,0 +1,33 @@
import Foundation
import SwiftUI
class DictionaryContextService {
static let shared = DictionaryContextService()
private init() {}
/// Gets dictionary context information to be included in AI enhancement
func getDictionaryContext() -> String {
guard let dictionaryWords = getDictionaryWords(), !dictionaryWords.isEmpty else {
return ""
}
let wordsText = dictionaryWords.joined(separator: ", ")
return "\n\nImportant Vocabulary: \(wordsText)"
}
/// Gets enabled custom dictionary words from UserDefaults
private func getDictionaryWords() -> [String]? {
guard let data = UserDefaults.standard.data(forKey: "CustomDictionaryItems") else {
return nil
}
do {
let items = try JSONDecoder().decode([DictionaryItem].self, from: data)
let enabledWords = items.filter { $0.isEnabled }.map { $0.word }
return enabledWords.isEmpty ? nil : enabledWords
} catch {
return nil
}
}
}