mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
* iOS keyboard: nothing opens Hex unless the person taps Open Hex
A dictation tap (the Hex key, Dictate, the bar's Hex mark, a hold on the
strip, two-thumb Dictate) opened Hex without saying so in four cases on
iOS 26 and later. Ryan asked that nothing in the keyboard takes people to
Hex unless they tapped something that says it will.
- Multilingual model: removed the hand-off. With Experiments › Multilingual
model on and ready in Hex, every dictation in its 25 languages (English
included) opened Hex unless the keyboard had the model loaded at that
moment (any fresh keyboard process, the first compile, Speech model in
keyboard off, Two-language dictation, a keyboard limit under 131 MB, a
memory warning). The keyboard now dictates those languages itself, with
the model once loaded and Apple's speech recognition until then, as
English already did. Removed KeyboardSelfDictation.Reason.multilingual,
MultilingualParakeetSettings.keyboardOpensHex and isModelReady (only that
read it), KeyboardParakeetSettings.keyboardHasMultilingualModel and
KeyboardParakeet.dictatesMultilingualHere. The Experiments row no longer
says "Once ready, keyboard dictation opens Hex."
- Speech model missing twice, short of memory, Hex not set up: from iOS 26
a dictation tap now shows one line saying why ("Download English in Hex
to dictate", "Not enough memory to dictate here", "Finish setting up Hex
to dictate") with Open Hex and a ✕, in the bar's top row and the full
keyboard's strip (KeyboardDictationBlock), for 8 s. Only Open Hex opens
Hex (to record there, as the tap used to). The second failure to get the
speech model shows the line by itself; it goes once the keyboard can
dictate again. The keyboard's error for that case uses the same words.
- With a warm session the bar's Hex mark dictated by opening Hex; it now
dictates through the warm session as Dictate does, and VoiceOver names it
for what it does (Dictate, Set up Hex, or before iOS 26 Open Hex). The Hex
key's VoiceOver label says "Open Hex to dictate" only where it does.
- The translation note: a tap anywhere on it in the bar opened Hex. Now
only its Open Hex chip opens Hex's Settings, and a ✕ puts it away (both
keyboards); the strip notice's title shrinks to 0.7 to fit beside the ✕.
Before iOS 26 nothing changes for a dictation tap: KeyboardDictationBlock
.resolve is nil there and the tap opens Hex as before (Ryan decides).
A simulator-only App Group key (simulator.keyboardMemoryShort) stands in
for a keyboard short of memory, for screenshots.
Verified: build for the simulator; DictationSSOTTests, KeyboardBridgeTests,
SpeechModelInstallTests and the changed DictationControllerTests case pass
on basebook's iOS 27.2 simulator (new cases in DictationSSOTTests); every
line, both keyboards, light and dark, English and Chinese, on the real
keyboard in a host app on the simulator; code-shape --check passes.
* iOS: Hex gets the keyboard's Apple model ready when it dictates with the multilingual model
With Hex's engine on the multilingual model, KeyboardSpeechPreparation
took the keyboard's speech as following Hex (keyboardSpeechFollowsApp was
true for Parakeet Ultra) because the keyboard used to send those
dictations to Hex. Since the previous commit the keyboard dictates them
itself, with Apple's speech recognition until it has the multilingual
model loaded, so Hex now gets that language's Apple model ready for the
keyboard as it does with Parakeet V2. Without this, the keyboard would
have had to download it on its first dictation.
Verified: DictationSSOTTests' engine case updated to expect it; the full
suite runs on the Studio.
* iOS keyboard: say a language is unavailable instead of "Download it in Hex"; bar labels in Chinese
Two follow-ups the coordinator asked for on this pull request.
- When Hex found that Apple has no speech model for the dictation
language on this iPhone (KeyboardSpeechPreparation's hand-off: Speech
lists nothing, or the language isn't on this iPhone with nothing to fall
back to), a dictation tap said "Download <language> in Hex to dictate",
which downloading can't fix. Hex now marks that case
(LanguageSettings.keyboardLanguageUnavailable, beside keyboardNeedsHex),
the keyboard's reason is .languageUnavailable, and the line says
"<Language> isn't available on this iPhone yet" with only a ✕: no Open
Hex, since Hex can't download it either. The keyboard's own failures to
get a model it should have keep "Download <language> in Hex to dictate"
with Open Hex. A strip notice with no button hides its chip.
- The dictation bar's Dictate, Set up and Set up Hex labels were plain
strings, so the keyboard showed them in English on a Chinese iPhone. They
are localized now (听写, 开始设置, 设置 Hex were already in the catalog).
Verified: DictationSSOTTests (new cases for the unavailable line and
reason), KeyboardBridgeTests, SpeechModelInstallTests and
DictationControllerTests pass on basebook's iOS 27.2 simulator (143 tests).
* iOS docs: the keyboard's line when a language isn't available on the iPhone
Documents the previous commit: where Hex found Apple has no speech model for
the dictation language, the keyboard says so with only a ✕.
* iOS Settings: Dictation › Model picks English or Multilingual for the keyboard
Ryan asked for the keyboard's speech model to be chosen in Settings,
English or multilingual, now that the keyboard dictates the multilingual
model's languages itself (previous pull request).
- Settings › Dictation's speech model card gets Model: English ·
Multilingual (SpeechModelChoice, SpeechModelChoiceRow) under Speech model
in keyboard, on iPhones with 6 GB or more; under that the switch above
already says it needs 6 GB and the row doesn't show. English stays the
default. Multilingual is the existing setting
(MultilingualParakeetSettings, experiments.multilingualParakeet), so Hex's
own dictation follows it as it did from Experiments.
- Each choice says what it means before anything downloads: English, "For
English. A 464.4 MB download, once, on Wi-Fi."; Multilingual, its 25
languages by name in the iPhone's language and "A 632.2 MB download, once,
as soon as you pick it. Hex dictates them with it too."; and both, that
other languages use Apple's speech recognition and a downloaded model
stays.
- Picking English no longer deletes a complete multilingual download, so
switching back is instant (ParakeetUltraModel.removeUnlessComplete, also
at launch); a part-way one still goes. English's model already stayed
while Multilingual was on.
- With Multilingual picked, the card shows its download or failure
(MultilingualModelStatus, whose Turn off is now Use English) and, with
the keyboard switch on, what the keyboard does with it
(KeyboardParakeetStatus.multilingualLine). The switch's detail no longer
says "For English": "The keyboard dictates with the model below."
- Removed Experiments › Multilingual model (Parakeet) and its strings.
- Docs: dictation.md, settings-and-home.md, acknowledgements.
Verified: build for the simulator; DictationSSOTTests,
DictationControllerTests and KeyboardBridgeTests pass on basebook's iOS 27.2
simulator (129 tests, new cases for the multilingual status line, the
language names, and keeping a complete download); the card in English and
Multilingual, English and Simplified Chinese, from the app on the
simulator; code-shape --check passes.
* iOS Settings › Model keeps only the picked model, once the new one is ready
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).
1427 lines
55 KiB
Swift
1427 lines
55 KiB
Swift
import AVFAudio
|
||
import Foundation
|
||
import Observation
|
||
import SwiftUI
|
||
import UIKit
|
||
|
||
@MainActor
|
||
@Observable
|
||
final class DictationController {
|
||
static let shared = DictationController(
|
||
autoPrepareOnInit: ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil,
|
||
cleaner: AppleIntelligenceCleaner()
|
||
)
|
||
|
||
private static let minCaptureDuration: TimeInterval = 0.30
|
||
private static let maxCaptureDuration: TimeInterval = 60.0
|
||
private static let resultDeliveryTTL: TimeInterval = 30.0
|
||
|
||
enum Phase: Equatable {
|
||
case modelRequired
|
||
case preparingModel
|
||
case ready
|
||
case requestingPermission
|
||
case recording
|
||
case transcribing
|
||
case failed(String)
|
||
|
||
var isBusy: Bool {
|
||
switch self {
|
||
case .preparingModel, .requestingPermission, .transcribing:
|
||
true
|
||
default:
|
||
false
|
||
}
|
||
}
|
||
|
||
/// Recording and transcribing keep the screen on, so the iPhone doesn't dim and lock
|
||
/// while you talk or wait for the text.
|
||
var keepsScreenAwake: Bool {
|
||
self == .recording || self == .transcribing
|
||
}
|
||
}
|
||
|
||
struct PendingKeyboardHandoff {
|
||
let jobID: String?
|
||
let style: DictationStyle
|
||
let requestedAt: TimeInterval
|
||
}
|
||
|
||
var transcriber: any LocalTranscribing
|
||
let transcriberFactory: () async -> any LocalTranscribing
|
||
/// Settings › Dictation › Model: Multilingual: its download, beside the engine Hex
|
||
/// dictates with until it's ready.
|
||
@ObservationIgnored let multilingualDownload: ParakeetUltraDownload
|
||
private let cleaner: (any TranscriptCleaning)?
|
||
private let cleanupSettings: AppleIntelligenceCleanupSettings
|
||
private let customVocabularySettings: CustomVocabularySettings
|
||
private let lowercaseSettings: DictationLowercaseSettings
|
||
private let listSettings: DictationListSettings
|
||
private let punctuationSettings: DictationPunctuationSettings
|
||
private let numberSettings: DictationNumberSettings
|
||
private let periodSettings: DictationPeriodSettings
|
||
private let hinglishSettings: HinglishSettings
|
||
private let greeklishSettings: GreeklishSettings
|
||
private let typingJournalSettings: TypingJournalSettings
|
||
private let learnedDictationCorrections: @MainActor (String?) -> [DictationCorrection]
|
||
private let audio: any AudioManaging
|
||
let keyboardBridge: KeyboardBridge
|
||
private let fileManager: FileManager
|
||
private let now: () -> TimeInterval
|
||
private let liveActivity = LiveActivityCoordinator()
|
||
|
||
private var jobGeneration: UInt64 = 0
|
||
var modelPrepGeneration: UInt64 = 0
|
||
/// The preparation now running, cancelled when a newer one replaces it.
|
||
@ObservationIgnored var preparationTask: Task<Void, Never>?
|
||
private var meterTask: Task<Void, Never>?
|
||
private var transcriptionTask: Task<Void, Never>?
|
||
private var keyboardSessionTask: Task<Void, Never>?
|
||
|
||
private var lastKeyboardCommandID: String?
|
||
private var lastHandledLaunchJobID: String?
|
||
private(set) var modelState: ModelState = .required
|
||
private(set) var capture: Capture = .idle
|
||
private(set) var warmSession: Session = .cold
|
||
private var fixturePhase: Phase?
|
||
private var keyboardResult: KeyboardResult?
|
||
private var keyboardMessage: TimedMessage?
|
||
var pendingHandoff: PendingKeyboardHandoff?
|
||
@ObservationIgnored private var keepaliveBackoff = KeepaliveBackoff()
|
||
|
||
/// Derived from `capture` and `modelState` (or `fixturePhase` for `--snap-state` scenes),
|
||
/// so `.recording` without an `ActiveCaptureSession` is unrepresentable.
|
||
var phase: Phase {
|
||
if let fixturePhase { return fixturePhase }
|
||
switch capture {
|
||
case .opening:
|
||
return .requestingPermission
|
||
case .recording:
|
||
return .recording
|
||
case .transcribing:
|
||
return .transcribing
|
||
case .failed(let message, _):
|
||
return .failed(message.text)
|
||
case .idle:
|
||
switch modelState {
|
||
case .required:
|
||
return .modelRequired
|
||
case .preparing:
|
||
return .preparingModel
|
||
case .ready:
|
||
return .ready
|
||
case .failed(let message):
|
||
return .failed(message.text)
|
||
}
|
||
}
|
||
}
|
||
|
||
var isModelReady = false
|
||
var transcript = ""
|
||
@ObservationIgnored var audioLevel: Float = 0
|
||
@ObservationIgnored var loudnessDB: Float = -120
|
||
@ObservationIgnored private var rawRecordingDuration: TimeInterval = 0
|
||
private(set) var recordingWholeSeconds: Int = 0
|
||
var recordingDuration: TimeInterval {
|
||
get { rawRecordingDuration }
|
||
set {
|
||
rawRecordingDuration = newValue
|
||
let whole = max(0, Int(newValue))
|
||
if recordingWholeSeconds != whole {
|
||
recordingWholeSeconds = whole
|
||
}
|
||
}
|
||
}
|
||
var copied = false
|
||
var keyboardSessionEnabled: Bool { warmSession.isWarm }
|
||
@ObservationIgnored private var rawKeyboardSessionRemaining: TimeInterval = 0
|
||
private(set) var keyboardSessionDisplayRemaining = "0:00"
|
||
var keyboardSessionRemaining: TimeInterval { rawKeyboardSessionRemaining }
|
||
@ObservationIgnored private var lastPublishedSnapshotSignature: String?
|
||
@ObservationIgnored private var lastPublishedSnapshotAt: TimeInterval = -100
|
||
var warmDuration: WarmSessionDuration
|
||
private(set) var style: DictationStyle
|
||
private(set) var microphone: MicrophoneChoice
|
||
private(set) var handoffNotice: String?
|
||
var engine: DictationEngine = .undecided
|
||
var engineName: String { engine.displayName }
|
||
var transcriptionLanguage = "English"
|
||
/// The BCP 47 identifier of `transcriptionLanguage`, for the steps that depend on it.
|
||
var transcriptionLanguageID = "en-US"
|
||
/// Settings › Dictation language: a BCP 47 identifier, or nil for Automatic.
|
||
private(set) var dictationLanguage: String?
|
||
/// The keyboard's language Automatic followed when the model was last asked for
|
||
/// (`LanguageSettings.keyboardDictationLanguage`), so a switch on the space bar prepares
|
||
/// the new one.
|
||
private var followedKeyboardLanguage: String?
|
||
/// A language change made while busy, applied once the dictation in flight is done.
|
||
var pendingLanguagePreparation = false
|
||
/// Said once Automatic moved on from a language this iPhone can't dictate yet
|
||
/// ("Russian isn't available on this iPhone yet. Hex switched to English.").
|
||
var languageNotice: String?
|
||
/// The language to offer (BCP 47) when the one chosen can't be dictated here yet.
|
||
var languageOffer: String?
|
||
private let languageSettings: LanguageSettings
|
||
var modelPreparation = ModelPreparationProgress(
|
||
fractionCompleted: 0,
|
||
detail: String(localized: "Checking the speech model")
|
||
)
|
||
/// Whether Hex is in the background, where iOS suspends it and a download's connection
|
||
/// drops.
|
||
var isInBackground = false
|
||
/// A model download or preparation step that failed while Hex was in the background, tried
|
||
/// again on return.
|
||
var preparationFailedInBackground = false
|
||
/// `watchPreparation`: when the preparation last said something new, whether it has said
|
||
/// anything yet, and how long each may last.
|
||
@ObservationIgnored var preparationChangedAt = ContinuousClock.now
|
||
@ObservationIgnored var preparationReported = false
|
||
@ObservationIgnored var preparationLimits = (firstReport: Duration.seconds(30), stall: Duration.seconds(120))
|
||
/// Whether the preparation now running picked Parakeet Ultra, for its failure path.
|
||
@ObservationIgnored var preparingMultilingualParakeet = false
|
||
|
||
init(
|
||
audio: (any AudioManaging)? = nil,
|
||
keyboardBridge: KeyboardBridge? = nil,
|
||
transcriber: (any LocalTranscribing)? = nil,
|
||
transcriberFactory: (() async -> any LocalTranscribing)? = nil,
|
||
fileManager: FileManager = .default,
|
||
now: @escaping () -> TimeInterval = { Date().timeIntervalSince1970 },
|
||
autoPrepareOnInit: Bool = true,
|
||
cleaner: (any TranscriptCleaning)? = nil,
|
||
cleanupSettings: AppleIntelligenceCleanupSettings = AppleIntelligenceCleanupSettings(),
|
||
customVocabularySettings: CustomVocabularySettings = CustomVocabularySettings(),
|
||
lowercaseSettings: DictationLowercaseSettings = DictationLowercaseSettings(),
|
||
listSettings: DictationListSettings = DictationListSettings(),
|
||
punctuationSettings: DictationPunctuationSettings = DictationPunctuationSettings(),
|
||
numberSettings: DictationNumberSettings = DictationNumberSettings(),
|
||
periodSettings: DictationPeriodSettings = DictationPeriodSettings(),
|
||
hinglishSettings: HinglishSettings = HinglishSettings(),
|
||
greeklishSettings: GreeklishSettings = GreeklishSettings(),
|
||
typingJournalSettings: TypingJournalSettings = TypingJournalSettings(),
|
||
learnedDictationCorrections: (@MainActor (String?) -> [DictationCorrection])? = nil,
|
||
languageSettings: LanguageSettings = LanguageSettings(),
|
||
multilingualDownload: ParakeetUltraDownload? = nil
|
||
) {
|
||
// Before anything reads them: Parakeet's files live in the App Group now.
|
||
SpeechModelStore.moveLegacyDownloads()
|
||
let resolvedBridge = keyboardBridge ?? KeyboardBridge()
|
||
let resolvedAudio = audio ?? SystemAudioManager()
|
||
let initialTranscriber = transcriber ?? ParakeetTranscriber()
|
||
|
||
self.audio = resolvedAudio
|
||
self.keyboardBridge = resolvedBridge
|
||
self.transcriber = initialTranscriber
|
||
self.transcriberFactory = transcriberFactory ?? Self.defaultTranscriber
|
||
self.multilingualDownload = multilingualDownload ?? ParakeetUltraDownload()
|
||
self.cleaner = cleaner
|
||
self.cleanupSettings = cleanupSettings
|
||
self.customVocabularySettings = customVocabularySettings
|
||
self.lowercaseSettings = lowercaseSettings
|
||
self.listSettings = listSettings
|
||
self.punctuationSettings = punctuationSettings
|
||
self.numberSettings = numberSettings
|
||
self.periodSettings = periodSettings
|
||
self.hinglishSettings = hinglishSettings
|
||
self.greeklishSettings = greeklishSettings
|
||
self.typingJournalSettings = typingJournalSettings
|
||
self.learnedDictationCorrections = learnedDictationCorrections ?? { language in
|
||
TypingJournal.storedDictationCorrections(settings: typingJournalSettings, language: language)
|
||
}
|
||
self.fileManager = fileManager
|
||
self.now = now
|
||
self.languageSettings = languageSettings
|
||
self.dictationLanguage = languageSettings.dictationLanguage
|
||
self.followedKeyboardLanguage = languageSettings.keyboardDictationLanguage()
|
||
resolvedBridge.reviewWarmDurationForKeyboardDictation()
|
||
self.warmDuration = resolvedBridge.warmDuration
|
||
self.style = resolvedBridge.style
|
||
self.microphone = resolvedBridge.microphone
|
||
|
||
let hasCachedModel = ParakeetTranscriber.hasCachedModel
|
||
resolvedBridge.publish(.offline)
|
||
resolvedBridge.setModelInstalled(hasCachedModel)
|
||
|
||
wireAudioCallbacks()
|
||
wireSystemIntentBus()
|
||
self.multilingualDownload.onDownloaded = { [weak self] in
|
||
self?.syncMultilingualParakeet()
|
||
}
|
||
if autoPrepareOnInit {
|
||
// At launch no dictation is running, so any Hex Live Activity is left over.
|
||
liveActivity.endImmediately()
|
||
}
|
||
|
||
if Self.applyLaunchFixtureIfPresent(to: self) {
|
||
return
|
||
}
|
||
|
||
if autoPrepareOnInit {
|
||
prepareAtLaunch(hasCachedModel: hasCachedModel)
|
||
}
|
||
}
|
||
|
||
var isRecording: Bool {
|
||
phase == .recording
|
||
}
|
||
|
||
var primaryTitle: String {
|
||
switch phase {
|
||
case .modelRequired, .failed:
|
||
"Prepare \(engineName)"
|
||
case .preparingModel:
|
||
"Preparing \(engineName)"
|
||
case .ready:
|
||
"Start dictation"
|
||
case .requestingPermission:
|
||
"Checking microphone"
|
||
case .recording:
|
||
"Finish"
|
||
case .transcribing:
|
||
"Transcribing…"
|
||
}
|
||
}
|
||
|
||
// MARK: - State Transitions
|
||
|
||
func transition(model newState: ModelState) {
|
||
let oldPhase = phase
|
||
modelState = newState
|
||
if case .failed = capture {
|
||
capture = .idle
|
||
}
|
||
didTransitionPhase(from: oldPhase)
|
||
}
|
||
|
||
func failModel(_ message: String) {
|
||
transition(model: .failed(TimedMessage(text: message, at: now())))
|
||
}
|
||
|
||
func transition(capture newCapture: Capture) {
|
||
let oldPhase = phase
|
||
capture = newCapture
|
||
didTransitionPhase(from: oldPhase)
|
||
}
|
||
|
||
func applyFixturePhase(_ fixture: Phase) {
|
||
let oldPhase = phase
|
||
fixturePhase = fixture
|
||
didTransitionPhase(from: oldPhase)
|
||
}
|
||
|
||
func setWarmSession(_ newSession: Session, remaining explicitRemaining: TimeInterval? = nil) {
|
||
warmSession = newSession
|
||
let remaining: TimeInterval
|
||
switch newSession {
|
||
case .cold:
|
||
remaining = 0
|
||
case .warm(let expiresAt, _):
|
||
remaining = explicitRemaining ?? max(0, expiresAt - now())
|
||
}
|
||
updateWarmSessionRemaining(remaining)
|
||
}
|
||
|
||
private func updateWarmSessionRemaining(_ remaining: TimeInterval) {
|
||
rawKeyboardSessionRemaining = remaining
|
||
let formatted = HexTheme.formatRemaining(remaining)
|
||
if keyboardSessionDisplayRemaining != formatted {
|
||
keyboardSessionDisplayRemaining = formatted
|
||
}
|
||
}
|
||
|
||
private func setKeyboardMessage(_ text: String?) {
|
||
keyboardMessage = text.map { TimedMessage(text: $0, at: now()) }
|
||
}
|
||
|
||
private func didTransitionPhase(from oldValue: Phase) {
|
||
let newValue = phase
|
||
// Speech model in keyboard holds back while a preparation runs; it starts once that ends.
|
||
if oldValue == .preparingModel, newValue != .preparingModel {
|
||
startKeyboardParakeetDownloadIfWanted()
|
||
}
|
||
guard newValue.keepsScreenAwake != oldValue.keepsScreenAwake else { return }
|
||
// Only applies while Hex is in front; nothing else in Hex sets it, so ending a
|
||
// dictation hands the screen back to Auto-Lock.
|
||
UIApplication.shared.isIdleTimerDisabled = newValue.keepsScreenAwake
|
||
}
|
||
|
||
// MARK: - Settings
|
||
|
||
func setWarmDuration(_ duration: WarmSessionDuration) {
|
||
warmDuration = duration
|
||
keyboardBridge.warmDuration = duration
|
||
|
||
if warmSession.isWarm {
|
||
if let seconds = duration.seconds {
|
||
setWarmSession(warmSession.refreshing(expiresAt: now() + seconds), remaining: seconds)
|
||
publishKeyboardSnapshot()
|
||
} else if phase == .ready {
|
||
endWarmSession()
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Used by the settings segmented control: picking a duration warms immediately; picking Off ends it.
|
||
func selectWarmDuration(_ duration: WarmSessionDuration) {
|
||
setWarmDuration(duration)
|
||
if duration == .off {
|
||
if keyboardSessionEnabled {
|
||
endWarmSession()
|
||
} else {
|
||
endStaleLiveActivities()
|
||
}
|
||
} else if !keyboardSessionEnabled {
|
||
requestWarmSession()
|
||
}
|
||
}
|
||
|
||
func setStyle(_ newStyle: DictationStyle) {
|
||
style = newStyle
|
||
keyboardBridge.style = newStyle
|
||
}
|
||
|
||
/// Settings › Dictation language. Prepares the new language's model now, or once the
|
||
/// dictation in flight is done.
|
||
func setDictationLanguage(_ identifier: String?) {
|
||
let normalized = (identifier?.isEmpty == true) ? nil : identifier
|
||
guard normalized != dictationLanguage else { return }
|
||
languageSettings.chooseDictationLanguage(normalized)
|
||
applyDictationLanguage(normalized)
|
||
}
|
||
|
||
/// Picks up a language the keyboard switched to in the App Group: a pick in its language
|
||
/// switch, or, on Automatic, the keyboard's own language switched on the space bar.
|
||
func followDictationLanguageSetting() {
|
||
let stored = languageSettings.dictationLanguage
|
||
let following = languageSettings.keyboardDictationLanguage()
|
||
guard stored != dictationLanguage || following != followedKeyboardLanguage else { return }
|
||
applyDictationLanguage(stored)
|
||
}
|
||
|
||
/// Takes the language offered after the chosen one couldn't be dictated here.
|
||
func useOfferedLanguage() {
|
||
guard let languageOffer else { return }
|
||
setDictationLanguage(languageOffer)
|
||
}
|
||
|
||
private func applyDictationLanguage(_ identifier: String?) {
|
||
dictationLanguage = identifier
|
||
followedKeyboardLanguage = languageSettings.keyboardDictationLanguage()
|
||
languageNotice = nil
|
||
languageOffer = nil
|
||
switch phase {
|
||
case .recording, .transcribing, .requestingPermission:
|
||
pendingLanguagePreparation = true
|
||
case .modelRequired, .preparingModel, .ready, .failed:
|
||
prepareModel()
|
||
}
|
||
}
|
||
|
||
/// Runs a language change that waited for the dictation in flight.
|
||
private func prepareDeferredLanguageIfNeeded() {
|
||
guard pendingLanguagePreparation else { return }
|
||
switch phase {
|
||
case .ready, .failed, .modelRequired:
|
||
pendingLanguagePreparation = false
|
||
prepareModel()
|
||
case .recording, .transcribing, .requestingPermission, .preparingModel:
|
||
break
|
||
}
|
||
}
|
||
|
||
/// The next dictation records from the new choice's input; the warm keepalive stays on
|
||
/// the iPhone's microphone whatever the choice.
|
||
func setMicrophone(_ choice: MicrophoneChoice) {
|
||
guard microphone != choice else { return }
|
||
microphone = choice
|
||
keyboardBridge.microphone = choice
|
||
}
|
||
|
||
// MARK: - Standalone & System Actions
|
||
|
||
/// Home's button sets the model up (Set up) rather than dictating: no model yet, or a
|
||
/// failure that left none ready. A microphone or transcription failure with the model
|
||
/// still ready dictates again, as home's Ready says it can.
|
||
var primaryActionPreparesModel: Bool {
|
||
switch phase {
|
||
case .modelRequired: true
|
||
case .failed: !isModelReady
|
||
default: false
|
||
}
|
||
}
|
||
|
||
func performPrimaryAction() {
|
||
if primaryActionPreparesModel {
|
||
prepareModel()
|
||
return
|
||
}
|
||
switch phase {
|
||
case .ready, .failed:
|
||
startRecording(copyToPasteboard: false)
|
||
case .recording:
|
||
finishRecording()
|
||
case .modelRequired, .preparingModel, .requestingPermission, .transcribing:
|
||
break
|
||
}
|
||
}
|
||
|
||
func cancelCurrentJob() {
|
||
jobGeneration &+= 1
|
||
transcriptionTask?.cancel()
|
||
transcriptionTask = nil
|
||
liveActivity.endImmediately()
|
||
|
||
let hadKeyboardJob = capture.keyboardJobID != nil
|
||
if case .recording(let session) = capture {
|
||
stopMetering()
|
||
session.capture.stop()
|
||
try? fileManager.removeItem(at: session.capture.url)
|
||
}
|
||
|
||
if hadKeyboardJob {
|
||
setKeyboardMessage(nil)
|
||
}
|
||
audioLevel = 0
|
||
loudnessDB = -120
|
||
recordingDuration = 0
|
||
switch capture {
|
||
case .opening, .recording, .transcribing:
|
||
transition(capture: .idle)
|
||
case .idle, .failed:
|
||
break
|
||
}
|
||
restoreAudioAfterCapture()
|
||
publishKeyboardSnapshot()
|
||
prepareDeferredLanguageIfNeeded()
|
||
}
|
||
|
||
func clearTranscript() {
|
||
transcript = ""
|
||
copied = false
|
||
}
|
||
|
||
func copyTranscript() {
|
||
guard !transcript.isEmpty else { return }
|
||
UIPasteboard.general.string = transcript
|
||
KeyboardClipboardTracker().markOwnedCopy()
|
||
copied = true
|
||
}
|
||
|
||
func toggleWarmSession() {
|
||
if keyboardSessionEnabled {
|
||
endWarmSession()
|
||
} else {
|
||
if warmDuration == .off {
|
||
setWarmDuration(.fifteenMinutes)
|
||
}
|
||
requestWarmSession()
|
||
}
|
||
}
|
||
|
||
func endWarmSession(message: String? = nil) {
|
||
keyboardSessionTask?.cancel()
|
||
keyboardSessionTask = nil
|
||
pendingHandoff = nil
|
||
|
||
// Cancel an unaccepted in-progress recording, but preserve any already-accepted transcription!
|
||
if case .recording(let session) = capture {
|
||
jobGeneration &+= 1
|
||
transition(capture: .idle)
|
||
stopMetering()
|
||
session.capture.stop()
|
||
try? fileManager.removeItem(at: session.capture.url)
|
||
audioLevel = 0
|
||
loudnessDB = -120
|
||
recordingDuration = 0
|
||
liveActivity.endImmediately()
|
||
}
|
||
|
||
setWarmSession(.cold)
|
||
setKeyboardMessage(message)
|
||
|
||
if !capture.isRecording {
|
||
audio.deactivateSession()
|
||
}
|
||
endStaleLiveActivities()
|
||
|
||
publishKeyboardSnapshot()
|
||
// An accepted transcription the keyboard waits on keeps its heartbeat.
|
||
if keyboardJobInFlight {
|
||
startKeyboardSessionLoop()
|
||
}
|
||
}
|
||
|
||
/// Ends Live Activities no dictation owns, such as one a suspended or killed process left.
|
||
func endStaleLiveActivities() {
|
||
guard !capture.isBusy else { return }
|
||
liveActivity.endImmediately()
|
||
}
|
||
|
||
// MARK: - Keyboard Launch
|
||
|
||
func handleKeyboardLaunch(_ request: KeyboardLaunchRequest) {
|
||
if request.opensPage {
|
||
return
|
||
}
|
||
// Whatever made the keyboard open Hex, it isn't dictating by itself: the warm default is back.
|
||
keyboardBridge.noteKeyboardOpenedHex()
|
||
refreshWarmDuration()
|
||
// A language picked in the keyboard: prepare it before the handoff records.
|
||
followDictationLanguageSetting()
|
||
handoffNotice = nil
|
||
|
||
let currentTime = now()
|
||
if request.isHandoffExpired(at: currentTime) {
|
||
handoffNotice = String(localized: "Timed out")
|
||
pendingHandoff = PendingKeyboardHandoff(
|
||
jobID: nil,
|
||
style: style,
|
||
requestedAt: currentTime
|
||
)
|
||
fulfillPendingHandoffOrPrepare()
|
||
return
|
||
}
|
||
|
||
switch request {
|
||
case .setup, .translationSettings, .pasteSettings:
|
||
break
|
||
|
||
case .startSession:
|
||
pendingHandoff = PendingKeyboardHandoff(
|
||
jobID: nil,
|
||
style: style,
|
||
requestedAt: currentTime
|
||
)
|
||
fulfillPendingHandoffOrPrepare()
|
||
|
||
case .startRecording(let jobID, _, let requestedStyle):
|
||
guard !jobID.isEmpty, jobID != lastHandledLaunchJobID else { return }
|
||
lastHandledLaunchJobID = jobID
|
||
if let requestedStyle {
|
||
setStyle(requestedStyle)
|
||
}
|
||
pendingHandoff = PendingKeyboardHandoff(
|
||
jobID: jobID,
|
||
style: requestedStyle ?? style,
|
||
requestedAt: currentTime
|
||
)
|
||
fulfillPendingHandoffOrPrepare()
|
||
}
|
||
}
|
||
|
||
private func fulfillPendingHandoffOrPrepare() {
|
||
guard let handoff = pendingHandoff else { return }
|
||
|
||
if now() - handoff.requestedAt > KeyboardLaunchRequest.handoffWindow {
|
||
handoffNotice = String(localized: "Timed out")
|
||
pendingHandoff = PendingKeyboardHandoff(
|
||
jobID: nil,
|
||
style: handoff.style,
|
||
requestedAt: now()
|
||
)
|
||
}
|
||
|
||
switch phase {
|
||
case .modelRequired, .failed:
|
||
prepareModel()
|
||
case .ready:
|
||
activateWarmSessionAndRunHandoff()
|
||
case .preparingModel, .requestingPermission, .recording, .transcribing:
|
||
break
|
||
}
|
||
}
|
||
|
||
private func requestWarmSession() {
|
||
pendingHandoff = PendingKeyboardHandoff(
|
||
jobID: nil,
|
||
style: style,
|
||
requestedAt: now()
|
||
)
|
||
fulfillPendingHandoffOrPrepare()
|
||
}
|
||
|
||
func activateWarmSessionAndRunHandoff() {
|
||
guard phase == .ready else { return }
|
||
let handoff = pendingHandoff
|
||
pendingHandoff = nil
|
||
|
||
// If a one-shot recording was requested (`warmDuration == .off` and a jobID is present),
|
||
// go straight into recording and release the mic as soon as capture finishes.
|
||
if warmDuration == .off, let jobID = handoff?.jobID {
|
||
let expired = handoff.map { now() - $0.requestedAt > KeyboardLaunchRequest.handoffWindow } ?? false
|
||
if !expired {
|
||
startRecording(
|
||
keyboardJobID: jobID,
|
||
styleOverride: handoff?.style,
|
||
copyToPasteboard: false
|
||
)
|
||
// The keyboard finishes what it handed off: without the loop its Done and
|
||
// Cancel went unread and the recording ran to its 60 s limit.
|
||
startKeyboardSessionLoop()
|
||
} else {
|
||
handoffNotice = String(localized: "Timed out")
|
||
}
|
||
return
|
||
}
|
||
|
||
if warmSession.isWarm, audio.isKeepaliveRunning {
|
||
refreshWarmExpiry()
|
||
if let jobID = handoff?.jobID {
|
||
let expired = handoff.map { now() - $0.requestedAt > KeyboardLaunchRequest.handoffWindow } ?? false
|
||
if !expired {
|
||
startRecording(
|
||
keyboardJobID: jobID,
|
||
styleOverride: handoff?.style,
|
||
copyToPasteboard: false
|
||
)
|
||
return
|
||
} else {
|
||
handoffNotice = String(localized: "Timed out")
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
jobGeneration &+= 1
|
||
let requestGen = jobGeneration
|
||
transition(capture: .opening(generation: requestGen, keyboardJobID: nil))
|
||
setKeyboardMessage(nil)
|
||
|
||
Task {
|
||
let granted = await audio.requestPermission()
|
||
guard self.jobGeneration == requestGen else { return }
|
||
guard granted else {
|
||
transition(capture: .failed(
|
||
message: TimedMessage(
|
||
text: String(localized: "Allow microphone access in Settings to dictate."),
|
||
at: now()
|
||
),
|
||
retryableWithModelReady: false
|
||
))
|
||
publishKeyboardSnapshot()
|
||
return
|
||
}
|
||
|
||
// If we also have a fresh recording job, begin capture directly so we don't bounce keepalive -> recorder!
|
||
if let jobID = handoff?.jobID,
|
||
let requestedAt = handoff?.requestedAt,
|
||
self.now() - requestedAt <= KeyboardLaunchRequest.handoffWindow {
|
||
self.enableWarmSessionBookkeeping()
|
||
self.transition(capture: .idle)
|
||
self.startKeyboardSessionLoop()
|
||
self.startRecording(
|
||
keyboardJobID: jobID,
|
||
styleOverride: handoff?.style,
|
||
copyToPasteboard: false
|
||
)
|
||
return
|
||
}
|
||
|
||
if let jobID = handoff?.jobID, !jobID.isEmpty {
|
||
self.handoffNotice = String(localized: "Timed out")
|
||
}
|
||
|
||
do {
|
||
try audio.startKeepalive()
|
||
enableWarmSessionBookkeeping()
|
||
transition(capture: .idle)
|
||
startKeyboardSessionLoop()
|
||
publishKeyboardSnapshot()
|
||
} catch {
|
||
audio.deactivateSession()
|
||
transition(capture: .failed(
|
||
message: TimedMessage(
|
||
text: UserFacingError.message(for: error, context: .microphone),
|
||
at: now()
|
||
),
|
||
retryableWithModelReady: false
|
||
))
|
||
publishKeyboardSnapshot()
|
||
}
|
||
}
|
||
}
|
||
|
||
private func enableWarmSessionBookkeeping() {
|
||
let seconds = warmDuration.seconds ?? (15 * 60)
|
||
setWarmSession(.warm(expiresAt: now() + seconds), remaining: seconds)
|
||
}
|
||
|
||
private func refreshWarmExpiry() {
|
||
guard warmSession.isWarm, let seconds = warmDuration.seconds else { return }
|
||
setWarmSession(warmSession.refreshing(expiresAt: now() + seconds), remaining: seconds)
|
||
}
|
||
|
||
// MARK: - Capture & Generation-Scoped Transcription
|
||
|
||
func startRecording(
|
||
keyboardJobID: String? = nil,
|
||
styleOverride: DictationStyle? = nil,
|
||
copyToPasteboard: Bool = false
|
||
) {
|
||
guard !capture.isRecording else { return }
|
||
if let keyboardJobID, keyboardJobID == capture.keyboardJobID {
|
||
return
|
||
}
|
||
if keyboardJobID != nil {
|
||
setKeyboardMessage(nil)
|
||
}
|
||
|
||
// Bumping generation invalidates any stale winding-down transcription task so it can never
|
||
// overwrite our new capture's phase, job state, or temporary WAV file.
|
||
jobGeneration &+= 1
|
||
let captureGeneration = jobGeneration
|
||
transcriptionTask?.cancel()
|
||
transcriptionTask = nil
|
||
let effectiveStyle = styleOverride ?? style
|
||
let usesCleanup = cleanupSettings.isEnabled
|
||
let language = transcriptionLanguageID
|
||
CleanupTagger.prepareIfEnabled(style: effectiveStyle, language: language)
|
||
let preferences = DictationPipeline.Preferences.current(
|
||
style: effectiveStyle,
|
||
vocabulary: customVocabularySettings,
|
||
corrections: learnedDictationCorrections(language),
|
||
lowercase: lowercaseSettings,
|
||
lists: listSettings,
|
||
punctuation: punctuationSettings,
|
||
numbers: numberSettings,
|
||
hinglish: hinglishSettings,
|
||
greeklish: greeklishSettings,
|
||
period: periodSettings
|
||
)
|
||
let sharesHeard = typingJournalSettings.isEnabled
|
||
|
||
// A failed re-preparation left the previously loaded model ready; starting a dictation
|
||
// with it dismisses that stale failure so finishing returns to `.ready`.
|
||
if isModelReady, case .failed = modelState {
|
||
modelState = .ready
|
||
}
|
||
transition(capture: .opening(generation: captureGeneration, keyboardJobID: keyboardJobID))
|
||
copied = false
|
||
|
||
Task {
|
||
let granted = await audio.requestPermission()
|
||
guard self.jobGeneration == captureGeneration else { return }
|
||
guard granted else {
|
||
let message = String(localized: "Allow microphone access in Settings.")
|
||
setKeyboardMessage(keyboardJobID != nil ? message : nil)
|
||
transition(capture: .failed(
|
||
message: TimedMessage(text: message, at: now()),
|
||
retryableWithModelReady: false
|
||
))
|
||
publishKeyboardSnapshot()
|
||
return
|
||
}
|
||
|
||
let captureURL = fileManager.temporaryDirectory
|
||
.appendingPathComponent("hex-dictation-\(captureGeneration)-\(UUID().uuidString)")
|
||
.appendingPathExtension("wav")
|
||
|
||
do {
|
||
let activeAudio = try audio.beginCapture(
|
||
at: captureURL,
|
||
maxDuration: Self.maxCaptureDuration,
|
||
microphone: microphone
|
||
)
|
||
guard self.jobGeneration == captureGeneration else {
|
||
activeAudio.stop()
|
||
try? fileManager.removeItem(at: captureURL)
|
||
return
|
||
}
|
||
|
||
let startedAt = now()
|
||
let session = ActiveCaptureSession(
|
||
generation: captureGeneration,
|
||
capture: activeAudio,
|
||
startedAt: startedAt,
|
||
keyboardJobID: keyboardJobID,
|
||
preferences: preferences,
|
||
usesCleanup: usesCleanup,
|
||
sharesHeard: sharesHeard,
|
||
copyToPasteboard: copyToPasteboard,
|
||
language: language
|
||
)
|
||
refreshWarmExpiry()
|
||
audioLevel = 0
|
||
loudnessDB = -120
|
||
recordingDuration = 0
|
||
transition(capture: .recording(session))
|
||
// Only the Action Button, Control Center, and Siri (the dictations that copy)
|
||
// get a Live Activity: the keyboard and the app show their own recording UI.
|
||
if copyToPasteboard {
|
||
liveActivity.startRecording(startedAt: Date(timeIntervalSince1970: startedAt))
|
||
}
|
||
startMetering(for: captureGeneration)
|
||
publishKeyboardSnapshot()
|
||
} catch {
|
||
try? fileManager.removeItem(at: captureURL)
|
||
let message = UserFacingError.message(for: error, context: .microphone)
|
||
setKeyboardMessage(keyboardJobID != nil ? message : nil)
|
||
transition(capture: .failed(
|
||
message: TimedMessage(text: message, at: now()),
|
||
retryableWithModelReady: isModelReady
|
||
))
|
||
restoreAudioAfterCapture()
|
||
publishKeyboardSnapshot()
|
||
}
|
||
}
|
||
}
|
||
|
||
func finishRecording() {
|
||
guard case .recording(let session) = capture else { return }
|
||
|
||
stopMetering()
|
||
let finalDuration = max(recordingDuration, session.capture.currentTime)
|
||
session.capture.stop()
|
||
audioLevel = 0
|
||
loudnessDB = -120
|
||
|
||
let captureURL = session.capture.url
|
||
let captureGeneration = session.generation
|
||
let keyboardJobID = session.keyboardJobID
|
||
|
||
guard finalDuration >= Self.minCaptureDuration else {
|
||
try? fileManager.removeItem(at: captureURL)
|
||
recordingDuration = 0
|
||
setKeyboardMessage(nil)
|
||
transition(capture: .idle)
|
||
liveActivity.endImmediately()
|
||
restoreAudioAfterCapture()
|
||
publishKeyboardSnapshot()
|
||
prepareDeferredLanguageIfNeeded()
|
||
return
|
||
}
|
||
|
||
transition(capture: .transcribing(generation: captureGeneration, keyboardJobID: keyboardJobID))
|
||
liveActivity.markTranscribing(startedAt: Date(timeIntervalSince1970: session.startedAt))
|
||
restoreAudioAfterCapture()
|
||
publishKeyboardSnapshot()
|
||
|
||
let activeTranscriber = transcriber
|
||
// A keyboard's dictation finishes with Hex in the background, and in One shot nothing
|
||
// keeps Hex running once the microphone closes: iOS would suspend it mid-transcription
|
||
// and the text would never reach the keyboard.
|
||
let backgroundTime = keyboardJobID != nil ? BackgroundTime("Keyboard dictation") : nil
|
||
|
||
transcriptionTask?.cancel()
|
||
transcriptionTask = Task { [weak self] in
|
||
defer {
|
||
try? FileManager.default.removeItem(at: captureURL)
|
||
backgroundTime?.end()
|
||
}
|
||
let transcribeStarted = Date()
|
||
|
||
do {
|
||
let rawText = try await activeTranscriber.transcribe(
|
||
captureURL,
|
||
vocabulary: session.preferences.recognitionHints
|
||
)
|
||
guard let self, self.jobGeneration == captureGeneration, !Task.isCancelled else {
|
||
return
|
||
}
|
||
// The steps in-keyboard dictation runs too, plus Apple Intelligence's cleanup.
|
||
let pipeline = DictationPipeline(
|
||
preferences: session.preferences,
|
||
language: session.language
|
||
)
|
||
let stages = await pipeline.run(rawText) { tagged in
|
||
await self.cleanedTranscriptIfEnabled(tagged, session: session)
|
||
}
|
||
let translated = stages.translated
|
||
guard self.jobGeneration == captureGeneration, !Task.isCancelled else {
|
||
return
|
||
}
|
||
let formatted = stages.formatted
|
||
self.recordingDuration = 0
|
||
self.transcript = formatted
|
||
if session.copyToPasteboard && !formatted.isEmpty {
|
||
UIPasteboard.general.string = formatted
|
||
KeyboardClipboardTracker().markOwnedCopy()
|
||
self.copied = true
|
||
self.liveActivity.finishCopied()
|
||
} else {
|
||
self.liveActivity.endImmediately()
|
||
}
|
||
|
||
if keyboardJobID != nil {
|
||
let dictationStages: DictationStages? = TypingLogSettings().isEnabled
|
||
? DictationStages(
|
||
engine: self.engineName,
|
||
language: self.transcriptionLanguage,
|
||
style: session.preferences.style.rawValue,
|
||
recordedMS: Int((finalDuration * 1000).rounded()),
|
||
transcribeMS: Int((Date().timeIntervalSince(transcribeStarted) * 1000).rounded()),
|
||
raw: rawText,
|
||
fixed: stages.fixed,
|
||
spelled: stages.spelled,
|
||
tagged: CleanupTaggerSettings().isEnabled && session.preferences.style != .verbatim ? stages.tagged : nil,
|
||
cleaned: session.usesCleanup && session.preferences.style != .verbatim ? stages.cleaned : nil,
|
||
translated: translated,
|
||
formatted: formatted
|
||
)
|
||
: nil
|
||
self.keyboardResult = KeyboardResult(
|
||
id: UUID().uuidString,
|
||
transcript: formatted,
|
||
// A translation's words can't be compared with what was heard.
|
||
heard: session.sharesHeard && translated == nil ? stages.heard : nil,
|
||
stages: dictationStages,
|
||
translationFailure: stages.translationFailure,
|
||
expiresAt: self.now() + Self.resultDeliveryTTL
|
||
)
|
||
self.setKeyboardMessage(nil)
|
||
}
|
||
self.refreshWarmExpiry()
|
||
self.transition(capture: .idle)
|
||
self.publishKeyboardSnapshot()
|
||
self.prepareDeferredLanguageIfNeeded()
|
||
} catch {
|
||
guard !Task.isCancelled, let self, self.jobGeneration == captureGeneration else {
|
||
return
|
||
}
|
||
self.recordingDuration = 0
|
||
self.liveActivity.endImmediately()
|
||
// Nothing was said: end quietly, as a capture too short to hold a word does.
|
||
if EmptyCapture.endsQuietly(error) {
|
||
self.setKeyboardMessage(nil)
|
||
self.refreshWarmExpiry()
|
||
self.transition(capture: .idle)
|
||
self.publishKeyboardSnapshot()
|
||
return
|
||
}
|
||
let message = UserFacingError.message(for: error, context: .transcription)
|
||
self.setKeyboardMessage(keyboardJobID != nil ? message : nil)
|
||
self.transition(capture: .failed(
|
||
message: TimedMessage(text: message, at: self.now()),
|
||
retryableWithModelReady: self.isModelReady
|
||
))
|
||
self.publishKeyboardSnapshot()
|
||
self.prepareDeferredLanguageIfNeeded()
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The cleanup experiment's one pass, before formatting. Any failure keeps the raw text.
|
||
private func cleanedTranscriptIfEnabled(
|
||
_ rawText: String,
|
||
session: ActiveCaptureSession
|
||
) async -> String {
|
||
guard session.usesCleanup,
|
||
session.preferences.style != .verbatim,
|
||
!rawText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
|
||
let cleaner,
|
||
cleaner.isAvailable,
|
||
cleaner.supports(language: session.language) else {
|
||
return rawText
|
||
}
|
||
return await cleaner.clean(rawText) ?? rawText
|
||
}
|
||
|
||
private func restoreAudioAfterCapture() {
|
||
guard case .warm(let expiresAt, _) = warmSession,
|
||
let seconds = warmDuration.seconds, seconds > 0,
|
||
expiresAt - now() > 0 else {
|
||
setWarmSession(.cold)
|
||
audio.deactivateSession()
|
||
return
|
||
}
|
||
do {
|
||
try audio.startKeepalive()
|
||
} catch {
|
||
setWarmSession(.cold)
|
||
setKeyboardMessage(UserFacingError.message(for: error, context: .microphone))
|
||
audio.deactivateSession()
|
||
}
|
||
}
|
||
|
||
private func startMetering(for generation: UInt64) {
|
||
stopMetering()
|
||
meterTask = Task { [weak self] in
|
||
while !Task.isCancelled {
|
||
guard let self,
|
||
case .recording(let session) = self.capture,
|
||
session.generation == generation else {
|
||
break
|
||
}
|
||
|
||
// Recorder readiness is a sample, not the capture's lifetime. A transient
|
||
// false must not strand the app meter and keyboard heartbeat until Stop.
|
||
// Completion/interruption callbacks and explicit Stop own capture teardown.
|
||
guard session.capture.isRecording else {
|
||
try? await Task.sleep(for: .milliseconds(33))
|
||
continue
|
||
}
|
||
|
||
let db = session.capture.averagePowerDB()
|
||
self.loudnessDB = db
|
||
self.audioLevel = max(0, min(1, (db + 48) / 48))
|
||
self.recordingDuration = session.capture.currentTime
|
||
self.publishKeyboardSnapshot(minInterval: 0.10)
|
||
|
||
if self.recordingDuration >= Self.maxCaptureDuration - 0.25 {
|
||
self.finishRecording()
|
||
return
|
||
}
|
||
|
||
try? await Task.sleep(for: .milliseconds(33))
|
||
}
|
||
}
|
||
}
|
||
|
||
private func stopMetering() {
|
||
meterTask?.cancel()
|
||
meterTask = nil
|
||
}
|
||
|
||
// MARK: - Warm Session Loop & Snapshot Truth
|
||
|
||
/// A keyboard's dictation is recording or transcribing: Hex reads its commands and
|
||
/// heartbeats its snapshot until the text is in, warm session or not (One shot).
|
||
private var keyboardJobInFlight: Bool {
|
||
capture.keyboardJobID != nil
|
||
}
|
||
|
||
/// Reads the keyboard's commands and keeps its snapshot's heartbeat fresh, for a warm
|
||
/// session and for any keyboard dictation in flight. The keyboard takes a job whose
|
||
/// heartbeat stops as over (`KeyboardSnapshot.isOrphanedJob`).
|
||
private func startKeyboardSessionLoop() {
|
||
keyboardSessionTask?.cancel()
|
||
keepaliveBackoff.reset()
|
||
keyboardSessionTask = Task { [weak self] in
|
||
while !Task.isCancelled {
|
||
guard let self, self.warmSession.isWarm || self.keyboardJobInFlight else { return }
|
||
|
||
// Never expire an in-flight recording or accepted transcription mid-work.
|
||
if case .warm(let expiresAt, let suspended) = self.warmSession, !self.capture.isBusy {
|
||
// The keyboard's language switch writes the App Group; follow it.
|
||
self.followDictationLanguageSetting()
|
||
let remaining = expiresAt - self.now()
|
||
if remaining <= 0 {
|
||
self.endWarmSession()
|
||
return
|
||
}
|
||
self.updateWarmSessionRemaining(remaining)
|
||
|
||
// Verify the keepalive engine is genuinely alive so we never publish a stuck "Ready".
|
||
if self.audio.isKeepaliveRunning {
|
||
self.keepaliveBackoff.noteRunning()
|
||
} else if self.phase == .ready && !suspended,
|
||
self.keepaliveBackoff.shouldRestart(at: self.now()) {
|
||
do {
|
||
try self.audio.startKeepalive()
|
||
} catch {
|
||
self.endWarmSession()
|
||
return
|
||
}
|
||
}
|
||
}
|
||
|
||
self.consumeKeyboardCommand()
|
||
if !self.capture.isRecording {
|
||
self.publishKeyboardSnapshot(minInterval: 0.60)
|
||
}
|
||
try? await Task.sleep(for: .milliseconds(100))
|
||
}
|
||
}
|
||
}
|
||
|
||
func processPendingKeyboardCommandForTesting() {
|
||
consumeKeyboardCommand()
|
||
publishKeyboardSnapshot()
|
||
}
|
||
|
||
func updateRecordingDurationForTesting(_ duration: TimeInterval) {
|
||
recordingDuration = duration
|
||
}
|
||
|
||
private func consumeKeyboardCommand() {
|
||
guard let command = keyboardBridge.latestCommand(),
|
||
command.id != lastKeyboardCommandID else { return }
|
||
lastKeyboardCommandID = command.id
|
||
|
||
// Ignore stale commands left in UserDefaults from >10s ago.
|
||
guard now() - command.sentAt <= KeyboardLaunchRequest.handoffWindow else { return }
|
||
|
||
switch command.kind {
|
||
case .start:
|
||
// A warm session whose microphone failed for the last dictation stays marked failed:
|
||
// the keyboard's next Hex key is its Try again. Without this Hex ignored every
|
||
// start until it was opened, while the keyboard kept sending them.
|
||
guard phase == .ready || capture.allowsKeyboardRetry, capture.keyboardJobID == nil else { return }
|
||
setStyle(command.style)
|
||
startRecording(
|
||
keyboardJobID: command.jobID,
|
||
styleOverride: command.style,
|
||
copyToPasteboard: false
|
||
)
|
||
case .stop:
|
||
guard capture.isRecording, capture.keyboardJobID == command.jobID else { return }
|
||
finishRecording()
|
||
case .cancel:
|
||
guard capture.isBusy, capture.keyboardJobID == command.jobID else { return }
|
||
cancelCurrentJob()
|
||
}
|
||
}
|
||
|
||
func publishKeyboardSnapshot(minInterval: TimeInterval = 0) {
|
||
let currentTime = now()
|
||
|
||
// Past its time to go in by itself the keyboard can still offer it as Paste dictation.
|
||
if let result = keyboardResult, result.isOfferExpired(at: currentTime) {
|
||
keyboardResult = nil
|
||
}
|
||
|
||
let state: KeyboardDictationState
|
||
switch capture {
|
||
case .recording(let session):
|
||
// Hardware truth guard: only claim `.recording` while the recorder sample is live.
|
||
state = session.capture.isRecording ? .recording : (warmSession.isWarm ? .ready : .offline)
|
||
case .transcribing:
|
||
state = .transcribing
|
||
case .failed:
|
||
state = .failed
|
||
case .opening:
|
||
state = .offline
|
||
case .idle:
|
||
switch modelState {
|
||
case .ready:
|
||
state = (warmSession.isWarm && audio.isKeepaliveRunning) ? .ready : .offline
|
||
case .failed:
|
||
state = .failed
|
||
case .required, .preparing:
|
||
state = .offline
|
||
}
|
||
}
|
||
|
||
let displayedMessage: TimedMessage?
|
||
if let captureFailure = capture.failureMessage {
|
||
displayedMessage = captureFailure
|
||
} else if case .idle = capture, let modelFailure = modelState.failureMessage {
|
||
displayedMessage = modelFailure
|
||
} else {
|
||
displayedMessage = keyboardMessage
|
||
}
|
||
|
||
let jobID = capture.keyboardJobID
|
||
let signature = "\(state.rawValue)|\(jobID ?? "")|\(keyboardResult?.id ?? "")|\(displayedMessage?.text ?? "")"
|
||
if minInterval > 0,
|
||
signature == lastPublishedSnapshotSignature,
|
||
currentTime - lastPublishedSnapshotAt < minInterval {
|
||
return
|
||
}
|
||
lastPublishedSnapshotSignature = signature
|
||
lastPublishedSnapshotAt = currentTime
|
||
|
||
let effectiveHeartbeat: TimeInterval
|
||
if state == .recording || state == .transcribing || (warmSession.isWarm && audio.isKeepaliveRunning) {
|
||
effectiveHeartbeat = currentTime
|
||
} else {
|
||
effectiveHeartbeat = 0
|
||
}
|
||
|
||
keyboardBridge.publish(
|
||
KeyboardSnapshot(
|
||
state: state,
|
||
heartbeat: effectiveHeartbeat,
|
||
expiresAt: warmSession.expiresAt,
|
||
recordingStartedAt: capture.activeSession?.startedAt,
|
||
audioLevel: state == .recording ? audioLevel : 0,
|
||
loudnessDB: state == .recording ? loudnessDB : nil,
|
||
jobID: jobID,
|
||
resultID: keyboardResult?.id,
|
||
resultExpiresAt: keyboardResult?.expiresAt,
|
||
transcript: keyboardResult?.transcript,
|
||
message: displayedMessage?.text,
|
||
heard: keyboardResult?.heard,
|
||
stages: keyboardResult?.stages,
|
||
translationFailure: keyboardResult?.translationFailure,
|
||
messageAt: displayedMessage?.at
|
||
)
|
||
)
|
||
}
|
||
|
||
// MARK: - Audio Interruptions, Route Changes, & System Intents
|
||
|
||
private func wireAudioCallbacks() {
|
||
audio.onUnexpectedCaptureEnd = { [weak self] finishedURL, succeeded in
|
||
guard let self,
|
||
case .recording(let session) = self.capture,
|
||
session.capture.url == finishedURL else {
|
||
return
|
||
}
|
||
if succeeded {
|
||
self.finishRecording()
|
||
} else {
|
||
self.cancelCurrentJob()
|
||
self.transition(capture: .failed(
|
||
message: TimedMessage(text: String(localized: "Recording stopped."), at: self.now()),
|
||
retryableWithModelReady: self.isModelReady
|
||
))
|
||
self.publishKeyboardSnapshot()
|
||
}
|
||
}
|
||
|
||
audio.onSystemEvent = { [weak self] event in
|
||
self?.handleAudioSystemEvent(event)
|
||
}
|
||
}
|
||
|
||
func handleAudioSystemEvent(_ event: AudioSystemEvent) {
|
||
switch event {
|
||
case .interruptionBegan:
|
||
setWarmSession(warmSession.suspendingForInterruption())
|
||
if case .recording(let session) = capture {
|
||
// Save spoken audio if >= 300ms; otherwise cancel cleanly so "Listening" never sticks during a call.
|
||
if max(recordingDuration, session.capture.currentTime) >= Self.minCaptureDuration {
|
||
audio.stopKeepalive()
|
||
finishRecording()
|
||
} else {
|
||
cancelCurrentJob()
|
||
setKeyboardMessage(String(localized: "Interrupted"))
|
||
}
|
||
} else {
|
||
audio.stopKeepalive()
|
||
}
|
||
publishKeyboardSnapshot()
|
||
|
||
case .interruptionEnded(let shouldResume):
|
||
guard warmSession.isSuspendedOnInterruption else { return }
|
||
setWarmSession(warmSession.clearingInterruption())
|
||
guard warmSession.isWarm, warmSession.remaining(at: now()) > 0 else {
|
||
endWarmSession()
|
||
return
|
||
}
|
||
if shouldResume || UIApplication.shared.applicationState == .active {
|
||
do {
|
||
try audio.startKeepalive()
|
||
publishKeyboardSnapshot()
|
||
} catch {
|
||
endWarmSession()
|
||
}
|
||
} else {
|
||
endWarmSession()
|
||
}
|
||
|
||
case .routeOldDeviceUnavailable:
|
||
// e.g. AirPods placed in case: finish active speech >= 300ms or rebind keepalive tap to built-in mic.
|
||
if case .recording(let session) = capture {
|
||
if max(recordingDuration, session.capture.currentTime) >= Self.minCaptureDuration {
|
||
finishRecording()
|
||
} else {
|
||
cancelCurrentJob()
|
||
}
|
||
} else if warmSession.isWarm && phase == .ready {
|
||
do {
|
||
try audio.startKeepalive()
|
||
publishKeyboardSnapshot()
|
||
} catch {
|
||
endWarmSession()
|
||
}
|
||
}
|
||
|
||
case .routeNewDeviceAvailable:
|
||
// A headset or car that just connected can take the input: the keepalive goes back
|
||
// to the iPhone's microphone, on a fresh engine for the route's format.
|
||
if warmSession.isWarm && phase == .ready {
|
||
try? audio.startKeepalive()
|
||
publishKeyboardSnapshot()
|
||
}
|
||
|
||
case .mediaServicesReset:
|
||
if capture.isRecording {
|
||
cancelCurrentJob()
|
||
}
|
||
if warmSession.isWarm {
|
||
do {
|
||
try audio.startKeepalive()
|
||
publishKeyboardSnapshot()
|
||
} catch {
|
||
endWarmSession()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private func wireSystemIntentBus() {
|
||
Task {
|
||
await HexSystemCommandBus.shared.register { [weak self] command in
|
||
guard let self else { return nil }
|
||
return await self.performSystemCommand(command)
|
||
}
|
||
}
|
||
}
|
||
|
||
private func performSystemCommand(_ command: HexSystemCommand) async -> String? {
|
||
switch command {
|
||
case .toggleDictation:
|
||
if phase == .recording {
|
||
finishRecording()
|
||
if let task = transcriptionTask {
|
||
await task.value
|
||
}
|
||
return transcript
|
||
}
|
||
followDictationLanguageSetting()
|
||
if phase == .modelRequired || isFailedPhase {
|
||
prepareModel()
|
||
}
|
||
if phase == .preparingModel {
|
||
try? await waitUntilReady(timeoutSeconds: 10)
|
||
}
|
||
guard phase == .ready else { return nil }
|
||
startRecording(copyToPasteboard: true)
|
||
return ""
|
||
|
||
case .stopDictation:
|
||
guard phase == .recording else { return transcript }
|
||
finishRecording()
|
||
if let task = transcriptionTask {
|
||
await task.value
|
||
}
|
||
return transcript
|
||
|
||
case .cancelDictation:
|
||
cancelCurrentJob()
|
||
return nil
|
||
|
||
case .warmSession:
|
||
if !keyboardSessionEnabled {
|
||
if warmDuration == .off {
|
||
setWarmDuration(.fifteenMinutes)
|
||
}
|
||
requestWarmSession()
|
||
}
|
||
return nil
|
||
}
|
||
}
|
||
|
||
private var isFailedPhase: Bool {
|
||
if case .failed = phase { return true }
|
||
return false
|
||
}
|
||
|
||
private func waitUntilReady(timeoutSeconds: TimeInterval) async throws {
|
||
let deadline = now() + timeoutSeconds
|
||
while phase == .preparingModel && now() < deadline {
|
||
try await Task.sleep(for: .milliseconds(100))
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Background time from iOS while Hex finishes a keyboard's dictation, ended when it's done
|
||
/// or when iOS asks for it back.
|
||
@MainActor
|
||
final class BackgroundTime {
|
||
private var id = UIBackgroundTaskIdentifier.invalid
|
||
|
||
init(_ name: String) {
|
||
id = UIApplication.shared.beginBackgroundTask(withName: name) { [weak self] in
|
||
self?.end()
|
||
}
|
||
}
|
||
|
||
func end() {
|
||
guard id != .invalid else { return }
|
||
UIApplication.shared.endBackgroundTask(id)
|
||
id = .invalid
|
||
}
|
||
}
|