mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
The coordinator decided against keeping both models (about 1.3 to 1.6 GB for one the person switched away from). Only the picked model stays. - SpeechModelChoice.unpicked decides what to remove: the other model goes only once the picked one is downloaded and has loaded once, in Hex or the keyboard, and never while Hex dictates with it, so nobody is left with neither and a failed or cancelled download leaves the old one in place. DictationController.keepOnlyThePickedSpeechModel runs it whenever Hex comes to the front (launch included) and once Hex has loaded the multilingual model. - Until then the old model keeps dictating English in the keyboard (KeyboardParakeetPlan): with Multilingual picked and its model not here yet, English's model (readyV2); with English picked and its model not here yet, the multilingual one (new awaitingV2DownloadUsingUltra, which still lets English's model download by itself). Before, the keyboard fell back to Apple's speech recognition in both. - The keyboard records that a model loaded (KeyboardParakeetSettings .hasLoadedOnce, App Group). Removing a model clears that, its compile record (ParakeetUltraModel.remove now forgets Ultra's) and the keyboard's state about it, and sets purgesCompiled: before its next load the keyboard removes its Core ML compiled copies (Library/Caches/<bundle>/com.apple.e5rt.e5bundlecache, where a probe keyboard's syslog on an iPhone 17 Pro showed them; 234 MB for English's model), so the picked one compiles again once (12 to 17 s, Apple's speech recognition meanwhile). The Neural Engine's own cache (com.apple.aned) and Hex's own compiled copy are left to iOS. - The card says so: "Only the picked model stays on this iPhone: switching downloads the new one, and the old one goes once the new one is ready." The choices no longer say "once". Storage: English about 700 MB (464 MB download, 234 MB compiled in the keyboard); Multilingual about 0.9 to 1.2 GB (632 MB download, compiled copies not measured); both for a while during a switch. Verified: build for the simulator; DictationSSOTTests (plan fallbacks and the removal table), DictationControllerTests and KeyboardBridgeTests on basebook's iOS 27.2 simulator; the card in English and Chinese. Untested: the removal and the keyboard's purge on a phone (no Neural Engine or real compile in the simulator).
133 lines
6.3 KiB
Swift
133 lines
6.3 KiB
Swift
import Foundation
|
||
|
||
/// The single derivation of whether and how the keyboard uses Parakeet, shared by:
|
||
/// - `KeyboardParakeet.choice()` (which model the keyboard loads and transcribes with)
|
||
/// - `KeyboardParakeetDownload.hold` (whether Hex auto-downloads Parakeet V2 for the keyboard)
|
||
/// - `FullKeyboardView.offerModelNotice` (whether the keyboard shows the download notice)
|
||
nonisolated enum KeyboardParakeetPlan: Equatable, Sendable {
|
||
enum Model: Equatable, Sendable {
|
||
case parakeetV2
|
||
case parakeetUltra
|
||
|
||
var isMultilingual: Bool { self == .parakeetUltra }
|
||
}
|
||
|
||
/// Switch is off or this iPhone lacks the physical memory (`fitsDevice == false`).
|
||
case off
|
||
/// Two-language dictation is on; Parakeet does not take the keyboard's dictation.
|
||
case twoLanguage
|
||
/// Multilingual Parakeet takes this language.
|
||
/// `fitsKeyboardLimit` is false only when the keyboard measured a limit below `requiredLimit`.
|
||
case multilingual(language: String, downloaded: Bool, fitsKeyboardLimit: Bool)
|
||
/// Dictation language is not English (and not taken by Multilingual Parakeet).
|
||
case notEnglish
|
||
/// English dictation, switch on and iPhone fits, but the keyboard hasn't measured its
|
||
/// memory limit on this iPhone yet.
|
||
case unmeasured(language: String, v2Downloaded: Bool)
|
||
/// English dictation, switch on and iPhone fits, but the keyboard's measured memory limit
|
||
/// is below `KeyboardParakeetBudget.requiredLimit`.
|
||
case tooSmall
|
||
/// English dictation, keyboard fits, waiting for Parakeet V2 to finish downloading.
|
||
case awaitingV2Download(language: String)
|
||
/// As `awaitingV2Download`, with the multilingual model still here from before English was
|
||
/// picked: it keeps dictating English until V2 is ready (Settings › Dictation › Model keeps
|
||
/// the model picked before until the new one has loaded once).
|
||
case awaitingV2DownloadUsingUltra(language: String)
|
||
/// English dictation, keyboard fits, Parakeet V2 is complete in the App Group.
|
||
case readyV2(language: String)
|
||
|
||
static func resolve(
|
||
isEnabled: Bool,
|
||
fitsDevice: Bool,
|
||
twoLanguageEnabled: Bool,
|
||
multilingualLanguage: String?,
|
||
wantedLanguage: String?,
|
||
keyboardLimit: Int?,
|
||
v2Downloaded: Bool,
|
||
ultraDownloaded: Bool
|
||
) -> KeyboardParakeetPlan {
|
||
guard isEnabled, fitsDevice else { return .off }
|
||
if twoLanguageEnabled { return .twoLanguage }
|
||
let fitsLimit = keyboardLimit.map { $0 >= KeyboardParakeetBudget.requiredLimit } ?? true
|
||
if let multilingualLanguage {
|
||
// Until the multilingual model is here, English's, if it still is, keeps dictating
|
||
// English (it goes only once the multilingual one has loaded once).
|
||
if !ultraDownloaded, v2Downloaded, SpeechLanguagePicker.isEnglish(multilingualLanguage),
|
||
let keyboardLimit, keyboardLimit >= KeyboardParakeetBudget.requiredLimit {
|
||
return .readyV2(language: multilingualLanguage)
|
||
}
|
||
return .multilingual(
|
||
language: multilingualLanguage,
|
||
downloaded: ultraDownloaded,
|
||
fitsKeyboardLimit: fitsLimit
|
||
)
|
||
}
|
||
guard let wantedLanguage, SpeechLanguagePicker.isEnglish(wantedLanguage) else {
|
||
return .notEnglish
|
||
}
|
||
guard let keyboardLimit else {
|
||
return .unmeasured(language: wantedLanguage, v2Downloaded: v2Downloaded)
|
||
}
|
||
guard keyboardLimit >= KeyboardParakeetBudget.requiredLimit else {
|
||
return .tooSmall
|
||
}
|
||
if v2Downloaded { return .readyV2(language: wantedLanguage) }
|
||
return ultraDownloaded
|
||
? .awaitingV2DownloadUsingUltra(language: wantedLanguage)
|
||
: .awaitingV2Download(language: wantedLanguage)
|
||
}
|
||
|
||
static func current(
|
||
defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup),
|
||
preferred: [String] = Locale.preferredLanguages,
|
||
physicalMemory: UInt64 = ProcessInfo.processInfo.physicalMemory,
|
||
v2Downloaded: Bool = SpeechModelStore.isComplete(SpeechModelStore.parakeetV2),
|
||
ultraDownloaded: Bool = SpeechModelStore.isComplete(SpeechModelStore.parakeetUltra)
|
||
) -> KeyboardParakeetPlan {
|
||
let parakeet = KeyboardParakeetSettings(defaults: defaults)
|
||
let languages = LanguageSettings(defaults: defaults)
|
||
let wanted = languages.wantedDictationLanguage(preferred: preferred)
|
||
let multilingual = MultilingualParakeetSettings(defaults: defaults).language(
|
||
set: languages.dictationLanguage,
|
||
keyboard: languages.keyboardDictationLanguage(preferred: preferred),
|
||
preferred: preferred,
|
||
physicalMemory: physicalMemory
|
||
)
|
||
return resolve(
|
||
isEnabled: parakeet.isEnabled,
|
||
fitsDevice: KeyboardParakeetSettings.fits(physicalMemory: physicalMemory),
|
||
twoLanguageEnabled: TwoLanguageDictationSettings(defaults: defaults).isEnabled,
|
||
multilingualLanguage: multilingual,
|
||
wantedLanguage: wanted,
|
||
keyboardLimit: parakeet.keyboardLimit,
|
||
v2Downloaded: v2Downloaded,
|
||
ultraDownloaded: ultraDownloaded
|
||
)
|
||
}
|
||
|
||
/// Which model and language the keyboard itself should load/transcribe with, or nil for Apple Speech.
|
||
var modelChoice: (model: Model, language: String)? {
|
||
switch self {
|
||
case .multilingual(let language, downloaded: true, fitsKeyboardLimit: true):
|
||
(.parakeetUltra, language)
|
||
case .readyV2(let language):
|
||
(.parakeetV2, language)
|
||
case .awaitingV2DownloadUsingUltra(let language):
|
||
(.parakeetUltra, language)
|
||
case .unmeasured(let language, v2Downloaded: true):
|
||
// Before a limit is measured (e.g. simulator where available == 0), allow V2 if complete;
|
||
// on device warmUp records the real limit before checking choice().
|
||
(.parakeetV2, language)
|
||
case .off, .twoLanguage, .multilingual, .notEnglish, .unmeasured, .tooSmall, .awaitingV2Download:
|
||
nil
|
||
}
|
||
}
|
||
|
||
/// Whether the keyboard should show its "New speech model" download notice.
|
||
var awaitsV2Download: Bool {
|
||
switch self {
|
||
case .awaitingV2Download, .awaitingV2DownloadUsingUltra: true
|
||
default: false
|
||
}
|
||
}
|
||
}
|