Cotton code
37 lines · 1.7 KB · SwiftRaw
// The operating system's on-device speech model, chosen and fetched in ONE// place: which locale a module can listen in, and the language's model, which// the system downloads the first time it is asked for (Apple's download; it// carries no audio).//// Live dictation in the app (`CottonUI/Capture/SystemSpeechRecogniser.swift`,// DictationTranscriber over the microphone) uses it and keeps no copy. A// second copy is how two answers to "does this device have English" drift// apart.import Foundationimport Speech@available(iOS 26.0, macOS 26.0, *)public enum SpeechModel {    /// The locale `module` listens in for `wanted`, or nil when the system has    /// no model for that language at all.    ///    /// The whole locale first (language and region, so an Indian English    /// speaker gets the Indian English model), then the language alone, which    /// lets the system pick the region it has.    public static func locale<Module: LocaleDependentSpeechModule>(        _ module: Module.Type, for wanted: Locale    ) async -> Locale? {        if let found = await module.supportedLocale(equivalentTo: wanted) { return found }        guard let language = wanted.language.languageCode else { return nil }        return await module.supportedLocale(equivalentTo: Locale(languageCode: language))    }    /// Make sure the models `modules` need are on this device, fetching them    /// the first time. Returns at once when they are already installed.    public static func install(_ modules: [any SpeechModule]) async throws {        if let request = try await AssetInventory.assetInstallationRequest(supporting: modules) {            try await request.downloadAndInstall()        }    }}