Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocAutomation/Intelligence/FoundationModelsEngine.swift3// Description: The on-device engine: Apple's system language model through the FoundationModels framework (macOS 26 or later).4// Only the on-device system model is ever used; the framework's server-backed model types are never referenced.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9#if canImport(FoundationModels)10import FoundationModels11#endif1213public struct FoundationModelsEngine: TextIntelligence {14 public init() {}1516 /// True when this build can use the framework at all (it may still be unavailable at run time).17 public static var isSupportedBySystem: Bool {18 #if canImport(FoundationModels)19 if #available(macOS 26.0, *) { return true }20 #endif21 return false22 }2324 public var availability: IntelligenceAvailability {25 #if canImport(FoundationModels)26 if #available(macOS 26.0, *) {27 switch SystemLanguageModel.default.availability {28 case .available:29 return .available30 case .unavailable(let reason):31 switch reason {32 case .deviceNotEligible: return .deviceNotEligible33 case .appleIntelligenceNotEnabled: return .appleIntelligenceNotEnabled34 case .modelNotReady: return .modelNotReady35 @unknown default: return .unavailable(String(describing: reason))36 }37 }38 }39 #endif40 return .requiresNewerSystem41 }4243 public var contextSize: Int {44 #if canImport(FoundationModels)45 if #available(macOS 26.0, *) { return SystemLanguageModel.default.contextSize }46 #endif47 return 409648 }4950 public var measuresTokens: Bool {51 #if canImport(FoundationModels)52 if #available(macOS 26.4, *) { return true }53 #endif54 return false55 }5657 public func tokenCount(_ text: String) async -> Int {58 #if canImport(FoundationModels)59 if #available(macOS 26.4, *), let n = try? await SystemLanguageModel.default.tokenCount(for: Prompt(text)) { return n }60 #endif61 return TextChunker.estimatedTokens(text)62 }6364 public func supportsLanguage(_ code: String) -> Bool {65 #if canImport(FoundationModels)66 if #available(macOS 26.0, *) { return SystemLanguageModel.default.supportsLocale(Locale(identifier: code)) }67 #endif68 return false69 }7071 /// A short description of the model for diagnostics ("context 8192 tokens").72 public var modelDescription: String {73 #if canImport(FoundationModels)74 if #available(macOS 27.0, *) { return "system language model, variant \(SystemLanguageModel.default.variant), context \(contextSize) tokens" }75 if #available(macOS 26.0, *) { return "system language model, context \(contextSize) tokens" }76 #endif77 return "not available on this system"78 }7980 public func generate(_ request: IntelligenceRequest) -> AsyncThrowingStream<String, Error> {81 AsyncThrowingStream { continuation in82 let state = availability83 guard state.isAvailable else {84 continuation.finish(throwing: IntelligenceError.unavailable(state))85 return86 }87 #if canImport(FoundationModels)88 if #available(macOS 26.0, *) {89 let task = Task {90 do {91 let session = LanguageModelSession(model: SystemLanguageModel.default, instructions: request.instructions)92 var options = GenerationOptions()93 options.temperature = request.temperature94 options.maximumResponseTokens = request.maximumResponseTokens95 for try await snapshot in session.streamResponse(to: request.prompt, options: options) {96 if Task.isCancelled { break }97 continuation.yield(snapshot.content)98 }99 if Task.isCancelled { continuation.finish(throwing: CancellationError()) } else { continuation.finish() }100 } catch is CancellationError {101 continuation.finish(throwing: CancellationError())102 } catch {103 continuation.finish(throwing: FoundationModelsEngine.translate(error))104 }105 }106 continuation.onTermination = { _ in task.cancel() }107 return108 }109 #endif110 continuation.finish(throwing: IntelligenceError.unavailable(.requiresNewerSystem))111 }112 }113114 #if canImport(FoundationModels)115 /// Maps both error families of the framework: `LanguageModelError` / `SystemLanguageModel.Error` / `LanguageModelSession.Error`116 /// (macOS 27 and later) and `LanguageModelSession.GenerationError` (macOS 26, deprecated in 27).117 @available(macOS 26.0, *)118 static func translate(_ error: Error) -> IntelligenceError {119 if let e = error as? IntelligenceError { return e }120 if #available(macOS 27.0, *) {121 if let e = error as? LanguageModelError {122 switch e {123 case .contextSizeExceeded: return .contextExceeded124 case .rateLimited: return .rateLimited125 case .guardrailViolation: return .guardrail126 case .refusal: return .refused(e.errorDescription)127 case .unsupportedLanguageOrLocale: return .unsupportedLanguage128 case .timeout: return .timedOut129 case .unsupportedCapability, .unsupportedTranscriptContent, .unsupportedGenerationGuide: return .other(e.errorDescription ?? String(describing: e))130 @unknown default: return .other(e.errorDescription ?? String(describing: e))131 }132 }133 if let e = error as? SystemLanguageModel.Error {134 switch e {135 case .assetsUnavailable: return .assetsUnavailable136 @unknown default: return .other(e.errorDescription ?? String(describing: e))137 }138 }139 if let e = error as? LanguageModelSession.Error {140 switch e {141 case .concurrentRequests: return .busy142 default: return .other(e.errorDescription ?? String(describing: e))143 }144 }145 }146 if error is LanguageModelSession.GenerationError {147 // The cases of this family are deprecated in macOS 27; they are told apart by name so that this code compiles148 // without deprecation warnings and keeps working on macOS 26, where this is the only family.149 switch Mirror(reflecting: error).children.first?.label ?? String(describing: error) {150 case "exceededContextWindowSize": return .contextExceeded151 case "assetsUnavailable": return .assetsUnavailable152 case "guardrailViolation": return .guardrail153 case "unsupportedLanguageOrLocale": return .unsupportedLanguage154 case "rateLimited": return .rateLimited155 case "concurrentRequests": return .busy156 case "refusal": return .refused(nil)157 default: return .other((error as? LocalizedError)?.errorDescription ?? String(describing: error))158 }159 }160 return .other((error as? LocalizedError)?.errorDescription ?? error.localizedDescription)161 }162 #endif163}164