grammargem/Sources/GrammarGem/AI/AIEngine.swift
BigMarc 3d749e8139 feat: Implement LiveMonitor for real-time grammar and spelling checks across applications
- Added LiveMonitor class to scan text areas in the frontmost window for grammar and spelling issues.
- Introduced DetectedField struct to represent text areas with issues, including suggestions for corrections.
- Implemented functionality to apply corrections and dismiss suggestions.
- Added Permissions class to manage Accessibility permissions required for monitoring.
- Created SnippetStore for managing text-expansion snippets, including persistence to UserDefaults.
- Developed TextCapture class to read and write text from the frontmost application using Accessibility API and clipboard fallback.
- Implemented TextFieldScanner to traverse the Accessibility tree and collect editable text elements.
- Added TextReplacementCoordinator to orchestrate the text correction process.
- Integrated UpdaterManager for handling application updates using Sparkle.
- Created comprehensive unit tests for various components, including grammar corrections and entitlement checks.
2026-06-18 16:49:55 +02:00

33 lines
1.2 KiB
Swift

import Foundation
/// Layer 2 — local-LLM-powered rewriting / tone / "Ask" / translate.
///
/// Backed in production by **MLX** (mlx-swift) running a small instruct model
/// downloaded once from Hugging Face. The protocol isolates the rest of the app
/// from the runtime so the UI, gating, and tests don't depend on MLX being present.
protocol AIEngine: AnyObject {
/// Whether a model is loaded and ready to run locally.
var isReady: Bool { get }
/// Run an action against `text` and return the rewritten result.
/// Throws on model-not-ready or generation failure. Never performs network I/O.
func run(_ action: AIAction, on text: String) async throws -> String
/// Best-effort local tone classification (used by tone-detection UI).
func detectTone(_ text: String) async -> Tone
}
enum AIEngineError: LocalizedError {
case modelNotReady
case generationFailed(String)
var errorDescription: String? {
switch self {
case .modelNotReady:
return "The on-device model isn't ready yet. Finish the first-run download in Settings."
case .generationFailed(let why):
return "On-device rewrite failed: \(why)"
}
}
}