import Foundation import SprachmittlerOllama /// A user-facing recovery action. The UI never shows a raw `localizedDescription`. public enum RecoveryAction: Sendable, Equatable { /// Start Ollama. case startOllama /// Copy and run `ollama pull `. case pullModel(String) /// Open the relevant System Settings page. case openSettings /// Copy the translation instead. case copyInstead /// Shorten the selection. case shortenText /// Retry the translation. case retry /// Nothing to do. case none } public extension SprachmittlerError { /// A short, non-technical message for the panel and onboarding. var userMessage: String { switch self { case .noSelection: "No text was selected." case .emptyText: "No text was copied." case let .textTooLong(_, limit): "This selection is too long (limit \(limit) characters). You can raise the limit in Settings." case .pasteboardUnavailable: "The clipboard is unavailable." case .ollamaUnreachable: "Ollama is not running." case let .ollamaTooOld(_, required): "Ollama is too old. Version \(required) or newer is required." case let .endpointRejected(reason): "The endpoint is not allowed: \(reason.userMessage)" case .modelNotInstalled: "The Hy-MT3 model is not installed." case .noAdapter: "This model is not supported." case .badRequest: "Ollama rejected the request." case .server: "Ollama returned an error." case .timeout: "Ollama took too long to answer." case .emptyOutput: "The model returned an empty translation." case .truncated: "The translation was cut off." case .repetitionLoop: "The translation started repeating." case .preambleDetected: "The model added an explanation." case .protectedTokenLost: "A URL, placeholder or code token was lost." case .accessibilityNotTrusted: "Replace needs the Accessibility permission." case .accessibilityUnsupported: "This app did not accept the replacement." case .insertionTargetChanged: "The selection changed before Replace." case .pasteboardRestoreFailed: "The previous clipboard could not be restored." case .cancelled: "Translation cancelled." } } /// A stable non-text code for diagnostics. var diagnosticCode: String { switch self { case .noSelection: "noSelection" case .emptyText: "emptyText" case .textTooLong: "textTooLong" case .pasteboardUnavailable: "pasteboardUnavailable" case .ollamaUnreachable: "ollamaUnreachable" case .ollamaTooOld: "ollamaTooOld" case .endpointRejected: "endpointRejected" case .modelNotInstalled: "modelNotInstalled" case .noAdapter: "noAdapter" case .badRequest: "badRequest" case .server: "server" case .timeout: "timeout" case .emptyOutput: "emptyOutput" case .truncated: "truncated" case .repetitionLoop: "repetitionLoop" case .preambleDetected: "preambleDetected" case .protectedTokenLost: "protectedTokenLost" case .accessibilityNotTrusted: "accessibilityNotTrusted" case .accessibilityUnsupported: "accessibilityUnsupported" case .insertionTargetChanged: "insertionTargetChanged" case .pasteboardRestoreFailed: "pasteboardRestoreFailed" case .cancelled: "cancelled" } } /// The primary recovery action, if any. var recoveryAction: RecoveryAction { switch self { case .ollamaUnreachable: .startOllama case let .modelNotInstalled(model): .pullModel(model) case .ollamaTooOld, .endpointRejected: .openSettings case .textTooLong: .openSettings case .accessibilityNotTrusted: .openSettings case .accessibilityUnsupported, .insertionTargetChanged, .pasteboardRestoreFailed: .copyInstead case .timeout, .server, .emptyOutput, .truncated, .repetitionLoop, .preambleDetected: .retry default: .none } } } private extension EndpointRejection { var userMessage: String { switch self { case .scheme: "only http and https are supported" case .form: "it must not contain credentials, a query or a fragment" case .remoteNotAllowed: "remote endpoints are disabled" case .remoteRequiresHTTPS: "remote endpoints require HTTPS" case .redirect: "redirects are refused" } } }