hex/ios/HEXShared/DictationLanguage.swift
Ryan Vogel 178c16fdc5
iOS: speech model fallbacks for iPhone 11 and unavailable languages; no raw errors in the keyboard (#2048)
* iOS: fall back to DictationTranscriber when a speech model won't install, and never show Apple's error text

A TestFlight tester (iPhone 15, iOS 26.2, build 25, Russian) saw the full
keyboard's strip read "transcription.ru asset not found after attempted
download." Apple Speech lists some languages whose asset never arrives
(Apple's forum shows the same for Arabic), and Hex both stopped there and
showed Apple's sentence: UserFacingError (#2035) kept any description without
an error code in it, and this one has none.

- SpeechModelInstall (HEXShared, used by the app and in-keyboard dictation):
  install the chosen module's model, then the same language on
  DictationTranscriber when SpeechTranscriber's won't install, and go straight
  to DictationTranscriber for three days. When neither installs while online,
  Automatic sets the language aside for three days and moves to the next one
  ("Russian isn't available on this iPhone yet. Using English."); a language
  picked by hand says so and offers that one ("Try English", with Use English
  in the app). Offline or stalled, it says the download failed and keeps the
  language. Every message fits the full keyboard's strip.
- StallWatch: a download whose progress hasn't moved for 60 s is cancelled
  and treated as failed, instead of holding the progress bar forever.
- AppleSpeechAssets.reserve makes room under maximumReservedLocales by
  releasing other reservations first, and a reservation Hex can't get no
  longer fails preparation: the keyboard reserved every language it used and
  never released one, so the app's reserve could throw for good.
- UserFacingError is now an allowlist: only Hex's own errors
  (HexDescribedError) show their text, a lost connection while getting the
  model ready says so, and anything else gets the context's sentence. This
  replaces the marker filter, which let Apple's readable sentences through.
- DictationController re-prepares in the language Automatic moved to
  (at most twice), keeps a notice, and offers a language after an explicit
  choice fails. The dictation bar shows a failure on two smaller lines.
- Snapshot scenes: full-keyboard-language-unavailable[-dark],
  keyboard-language-unavailable, language-unavailable, language-switched.

Verified: xcodebuild build-for-testing on an iPhone 15 / iOS 27.2 simulator,
and the 44 DictationController, picker, SpeechModelInstall and StallWatch
tests pass (fallback, set-aside, offline, unsupported, stall, explicit offer,
Automatic switch, error text). Apple Speech itself can't run in the
simulator, so the install steps are fakes there; which module fails for
Russian on iOS 26.2 is unverified on hardware (no iOS 26.x simulator runtime).

* iOS: dictate English with Apple on iPhone 11, and stop the Parakeet relaunch loop

A TestFlight tester (iPhone 11, iOS 27.3, build 16) reported the model
download sticking at 50% and the app then crashing at every launch, with no
crash log. iPhone 11 (A13, 4 GB) has no SpeechTranscriber
(SpeechTranscriber.isAvailable is false on every A13 iPhone), so Hex used
Parakeet V2 for English. FluidAudio 0.15.5 reports downloads as 0-50% and
Core ML's compile as 50-100%, so 50% is MLModel(contentsOf:) compiling the
encoder, which can't be cancelled; iOS ends the app there. Once the files are
on disk, init saw hasCachedModel and loaded Parakeet again at once, so every
launch ended the same way. iOS ending an app for memory leaves no crash
report, which fits the empty crash list.

- AppleSpeechTranscriber.shouldUse: English without SpeechTranscriber uses
  DictationTranscriber where Parakeet doesn't fit (under 5 GiB of RAM: every
  A13 iPhone) on iOS 26+. Parakeet stays where it fits and before iOS 26.
- ParakeetLoadGuard: a load still marked as running at launch belonged to a
  process that ended, so the next launches don't load Parakeet by themselves
  until a load works; Hex says it closed while loading the model and waits
  for Set up. If Automatic now picks Apple Speech, it prepares that instead.
- Parakeet's download half runs under StallWatch (90 s without progress ends
  it); the compile half isn't watched since it can't be stopped. FluidAudio's
  download errors (Hugging Face paths and HTML in their text) become "Parakeet
  V2 didn't download. Check your connection and try again."

Verified: xcodebuild test on an iPhone 15 / iOS 27.2 simulator, 247 tests,
0 failures, 1 skipped (AppleSpeechClipTests, needs clips), including new
tests for shouldUse on a 4 GB iPhone and the load guard across launches.
Parakeet's memory use, the compile at 50% and the ended launches need a real
iPhone 11; the simulator neither limits memory nor runs SpeechTranscriber.
2026-11-20 15:04:43 +00:00

243 lines
11 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
/// Settings › Dictation language and Experiments › Full keyboard › Keyboard layout, in the
/// App Group so the keyboard reads them and can switch the language.
nonisolated struct LanguageSettings {
private enum Key {
static let dictation = "settings.dictationLanguage"
static let resolvedDictation = "settings.dictationLanguage.resolved"
static let automaticDictation = "settings.dictationLanguage.automatic"
static let recent = "settings.dictationLanguage.recent"
static let supported = "settings.dictationLanguage.supported"
static let keyboardLayout = "settings.keyboardLayout"
static let speechModuleFailed = "settings.dictationLanguage.speechModuleFailed"
static let unavailable = "settings.dictationLanguage.unavailable"
}
static let maxRecent = 5
/// How long a language whose speech model wouldn't install stays set aside before Hex
/// tries it again: Apple's downloads that fail tend to keep failing for days.
static let setAsideDuration: TimeInterval = 3 * 24 * 60 * 60
private let defaults: UserDefaults?
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
self.defaults = defaults
}
/// A BCP 47 identifier such as `nb-NO`, or nil for Automatic.
var dictationLanguage: String? {
get { defaults?.string(forKey: Key.dictation) }
nonmutating set { defaults?.set(newValue, forKey: Key.dictation) }
}
/// The language Apple Speech last resolved for dictation (the app or the keyboard), so
/// translation can follow it without asking Speech again.
var resolvedDictationLanguage: String? {
get { defaults?.string(forKey: Key.resolvedDictation) }
nonmutating set { defaults?.set(newValue, forKey: Key.resolvedDictation) }
}
/// What Automatic picks on this iPhone, when the app or keyboard last worked it out.
var automaticDictationLanguage: String? {
get { defaults?.string(forKey: Key.automaticDictation) }
nonmutating set { defaults?.set(newValue, forKey: Key.automaticDictation) }
}
/// Languages picked lately, newest first, for the keyboard's quick switch.
var recentDictationLanguages: [String] {
get { defaults?.stringArray(forKey: Key.recent) ?? [] }
nonmutating set { defaults?.set(Array(newValue.prefix(Self.maxRecent)), forKey: Key.recent) }
}
/// Every language Apple Speech transcribes here (BCP 47), as last listed, so the
/// keyboard can offer only those without asking Speech.
var supportedDictationLanguages: [String] {
get { defaults?.stringArray(forKey: Key.supported) ?? [] }
nonmutating set { defaults?.set(newValue, forKey: Key.supported) }
}
/// The full keyboard's letters, or nil for Automatic (follow the dictation language).
var keyboardLayout: KeyboardLanguage? {
get { defaults?.string(forKey: Key.keyboardLayout).flatMap(KeyboardLanguage.init(rawValue:)) }
nonmutating set { defaults?.set(newValue?.rawValue, forKey: Key.keyboardLayout) }
}
/// The language dictation uses, when known: the one set, else what Automatic picks,
/// else the last one it ran in.
var effectiveDictationLanguage: String? {
dictationLanguage ?? automaticDictationLanguage ?? resolvedDictationLanguage
}
/// `SpeechTranscriber`'s model for this language (BCP 47) wouldn't install, so dictation
/// uses `DictationTranscriber`'s for a while.
func markSpeechModuleFailed(_ identifier: String, at now: Date = Date()) {
mark(identifier, key: Key.speechModuleFailed, at: now)
}
func speechModuleFailed(_ identifier: String, now: Date = Date()) -> Bool {
isMarked(identifier, key: Key.speechModuleFailed, now: now)
}
/// Neither module's model for this language (BCP 47) would install while online, so
/// Automatic passes over it for a while. An explicit choice still tries it.
func markUnavailable(_ identifier: String, at now: Date = Date()) {
mark(identifier, key: Key.unavailable, at: now)
}
func isUnavailable(_ identifier: String, now: Date = Date()) -> Bool {
isMarked(identifier, key: Key.unavailable, now: now)
}
/// A language that installed after all.
func clearUnavailable(_ identifier: String) {
let key = Locale(identifier: identifier).identifier(.bcp47)
guard var marks = defaults?.dictionary(forKey: Key.unavailable) as? [String: Double],
marks.removeValue(forKey: key) != nil else { return }
defaults?.set(marks, forKey: Key.unavailable)
}
private func mark(_ identifier: String, key: String, at now: Date) {
var marks = (defaults?.dictionary(forKey: key) as? [String: Double]) ?? [:]
marks = marks.filter { now.timeIntervalSince1970 - $0.value < Self.setAsideDuration }
marks[Locale(identifier: identifier).identifier(.bcp47)] = now.timeIntervalSince1970
defaults?.set(marks, forKey: key)
}
private func isMarked(_ identifier: String, key: String, now: Date) -> Bool {
guard let marks = defaults?.dictionary(forKey: key) as? [String: Double],
let at = marks[Locale(identifier: identifier).identifier(.bcp47)] else { return false }
return now.timeIntervalSince1970 - at < Self.setAsideDuration
}
/// A choice made in Hex or the keyboard: the setting, and the front of the recents.
func chooseDictationLanguage(_ identifier: String?) {
dictationLanguage = identifier
if let identifier {
recentDictationLanguages = [identifier] + recentDictationLanguages.filter { $0 != identifier }
}
}
}
/// The Apple Speech module that transcribes a language. `SpeechTranscriber` is Apple's newer
/// model and Hex's default; `DictationTranscriber` (the model behind the iOS keyboard's
/// dictation) covers languages it lacks, such as Norwegian, Danish, and Swedish.
nonisolated enum SpeechModuleKind: String, Codable, Sendable {
case speech
case dictation
}
nonisolated struct SpeechLanguage: Equatable, Sendable {
let locale: Locale
let module: SpeechModuleKind
var identifier: String { locale.identifier(.bcp47) }
var languageCode: String? { locale.language.languageCode?.identifier }
}
/// One entry in Settings › Dictation language.
nonisolated struct SpeechLanguageOption: Equatable, Identifiable, Sendable {
/// BCP 47, such as `nb-NO`.
let id: String
let name: String
}
/// Which language and Apple Speech module a dictation uses.
nonisolated enum SpeechLanguagePicker {
/// What Automatic tries, in order: the iPhone's preferred languages, its own locale, then
/// US English. Norwegian Nynorsk also tries Bokmål, the only Norwegian Apple transcribes.
static func automaticCandidates(preferred: [String], current: String) -> [String] {
var result: [String] = []
var seen = Set<String>()
func add(_ identifier: String) {
let key = Locale(identifier: identifier).identifier(.bcp47)
if seen.insert(key).inserted {
result.append(identifier)
}
}
for identifier in preferred + [current, "en-US"] {
add(identifier)
if Locale(identifier: identifier).language.languageCode?.identifier == "nn" {
add("nb-NO")
}
}
return result
}
/// An explicit choice wins: `SpeechTranscriber` when it has that language, else
/// `DictationTranscriber`. Automatic takes the first candidate either module has,
/// preferring `SpeechTranscriber` for the same language, so a Norwegian iPhone gets
/// Norwegian from `DictationTranscriber` rather than English from `SpeechTranscriber`.
/// `speech` and `dictation` return each module's equivalent locale, or nil. Automatic
/// passes over a language `skip` names (one whose model wouldn't install here), so it
/// lands on the next working one; an explicit choice is tried as it is.
static func pick(
explicit: String?,
candidates: [String],
speech: (Locale) async -> Locale?,
dictation: (Locale) async -> Locale?,
skip: (Locale) -> Bool = { _ in false }
) async -> SpeechLanguage? {
if let explicit, !explicit.isEmpty {
let wanted = Locale(identifier: explicit)
if let found = await speech(wanted), sameLanguage(found, wanted) {
return SpeechLanguage(locale: found, module: .speech)
}
if let found = await dictation(wanted) {
return SpeechLanguage(locale: found, module: .dictation)
}
}
for identifier in candidates {
let candidate = Locale(identifier: identifier)
if let found = await speech(candidate) {
if skip(found) { continue }
return SpeechLanguage(locale: found, module: .speech)
}
if let found = await dictation(candidate), !skip(found) {
return SpeechLanguage(locale: found, module: .dictation)
}
}
return nil
}
/// The same language and, when both name one, the same region.
private static func sameLanguage(_ a: Locale, _ b: Locale) -> Bool {
guard a.language.languageCode == b.language.languageCode else { return false }
guard let ra = a.region, let rb = b.region else { return true }
return ra == rb
}
/// "Norwegian Bokmål (Norway)" in the iPhone's own language.
static func displayName(for identifier: String, in display: Locale = .current) -> String {
display.localizedString(forIdentifier: identifier)
?? Locale(identifier: identifier).localizedString(forIdentifier: identifier)
?? identifier
}
/// Short names for a list of locales: the language alone where it has one region in the
/// list ("Norwegian Bokmål"), the full name where it has several ("English (Canada)").
static func shortNames(for identifiers: [String], in display: Locale = .current) -> [String: String] {
var regionsPerLanguage: [String: Int] = [:]
for identifier in identifiers {
let code = Locale(identifier: identifier).language.languageCode?.identifier ?? identifier
regionsPerLanguage[code, default: 0] += 1
}
var names: [String: String] = [:]
for identifier in identifiers {
let code = Locale(identifier: identifier).language.languageCode?.identifier ?? identifier
if regionsPerLanguage[code] == 1, let language = display.localizedString(forLanguageCode: code) {
names[identifier] = language.prefix(1).uppercased() + language.dropFirst()
} else {
names[identifier] = displayName(for: identifier, in: display)
}
}
return names
}
/// Whether a language identifier is English, for the English-only steps (the cleanup
/// tagger). nil (unknown) counts as English, as before languages existed.
static func isEnglish(_ identifier: String?) -> Bool {
guard let identifier else { return true }
return Locale(identifier: identifier).language.languageCode?.identifier == "en"
}
}