mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS: the keyboard model's memory lines say their numbers, and Remove where it can't run What (card A213, approved by Ryan): - The two memory causes carry their numbers, whole megabytes rounded down: "This iPhone's keyboard has 118 MB; the model needs 131 MB, so it uses Apple's speech recognition." (这台 iPhone 的键盘有 %1$lld MB 内存,模型需要 %2$lld MB,因此使用 Apple 的语音识别。) from the limit the keyboard already records, and "The keyboard had 41 MB free when it last opened; loading the model needs 58 MB, so it used Apple's speech recognition. It tries again the next time it opens." (键盘上次打开时只有 %1$lld MB 可用内存,载入模型需要 %2$lld MB,因此 使用了 Apple 的语音识别。下次打开时会再试一次。). The keyboard saves what was free with shortOfMemory (KeyboardParakeetSettings.keyboardStateFree, in the App Group it already writes; nothing is sent, Send feedback unchanged). Without a number (after a memory warning, an older keyboard, nothing measured) the lines keep today's words, never 0 MB. - A switch turned on by hand no longer downloads where the keyboard measured under 131 MB (KeyboardParakeetDownload.downloads); Wi-Fi and free space stay skippable, and before the keyboard has ever opened it still downloads at once, as today. - Remove (464 MB) (移除(%@)) where English's model is on the iPhone and the keyboard is too small for it: deleteKeyboardParakeet, what turning the switch off already deletes, with the switch left on, so the page then reads the too-small line, no button, and nothing downloads again. Never automatic; not while Hex dictates with that model; not for Multilingual, which Hex dictates with too. The size is the folder's files on disk. - Snapshot scenes: toosmall now records 118 MB; toosmalldownloaded has the model on disk; short has 41 MB free. Why: TestFlight 153, iPhone 14 (6 GB): the model was downloaded (by hand) but the keyboard could never use it, and the page couldn't say on which numbers. Verified: DictationSSOTTests (numbers, fallbacks, rounding, what the keyboard saves) and DictationControllerTests (downloads by hand); suite and screenshots in a simbox, separately. * iOS: the too-small snapshot scene dictates with Apple's speech recognition What: settings-dictation-model-english-toosmalldownloaded sets Hex's own transcriber to Apple's speech recognition, so English's model is the keyboard's alone there and Remove shows. Why: in snapshots Hex keeps its initial Parakeet transcriber with the model ready, which rightly hides Remove (Hex would dictate with it); an iPhone 14 on iOS 27 dictates with Apple's. Verified: screenshot in a simbox. * iOS: the too-small snapshot scene keeps Hex from preparing its own model What: settings-dictation-model-english-toosmalldownloaded gives the controller the dictation language the fixture stored (applyFixtureDictationLanguage), so Settings › Dictation doesn't prepare a model for it as it appears. Why: on the simulator, which has no Apple English model, that preparation picked Parakeet V2 for Hex itself, which rightly hid Remove; on an iPhone 14 Hex dictates with Apple's. Verified: screenshot in a simbox.
305 lines
15 KiB
Swift
305 lines
15 KiB
Swift
import Foundation
|
||
import SwiftUI
|
||
|
||
extension DictationController {
|
||
// MARK: - Launch Fixtures for Simulator Verification (`--snap-state <name>`)
|
||
|
||
static func applyLaunchFixtureIfPresent(to controller: DictationController) -> Bool {
|
||
let args = ProcessInfo.processInfo.arguments
|
||
guard let flagIndex = args.firstIndex(of: "--snap-state"),
|
||
flagIndex + 1 < args.count else {
|
||
return false
|
||
}
|
||
let scene = args[flagIndex + 1]
|
||
controller.engine = .appleSpeech
|
||
controller.transcriptionLanguage = "English"
|
||
controller.transcriptionLanguageID = "en-US"
|
||
controller.keyboardBridge.setModelInstalled(true)
|
||
controller.isModelReady = true
|
||
|
||
switch scene {
|
||
case "setup", "setup-sheet", "setup-sheet-microphone", "setup-sheet-noaccess", "paste-setting", _ where scene.hasPrefix("onboarding-") || scene.hasPrefix("settings-") || scene.hasPrefix("model-card-"):
|
||
controller.applyFixturePhase(.ready)
|
||
controller.warmDuration = .fifteenMinutes
|
||
controller.setWarmSession(.cold)
|
||
if scene.hasPrefix("model-card-") { KeyboardParakeetDownload.shared.applyFixture(scene) }
|
||
if scene.hasPrefix("settings-dictation-model-") { controller.applySpeechModelFixture(scene) }
|
||
|
||
case "language-unavailable":
|
||
// Russian picked by hand on an iPhone whose Apple Speech can't install it.
|
||
let unavailable = SpeechModelUnavailable(
|
||
language: "ru-RU",
|
||
reason: .notOnThisIPhone,
|
||
fallback: "en-US",
|
||
automatic: false
|
||
)
|
||
controller.isModelReady = false
|
||
controller.engine = .appleSpeech
|
||
controller.applyFixturePhase(.failed(UserFacingError.message(for: unavailable, context: .model)))
|
||
controller.languageOffer = "en-US"
|
||
controller.languageOfferLine = unavailable.lineBesideOffer
|
||
|
||
case "language-switched":
|
||
// Automatic moved on from Russian to English.
|
||
let unavailable = SpeechModelUnavailable(language: "ru-RU", reason: .notOnThisIPhone, fallback: "en-US")
|
||
controller.applyFixturePhase(.ready)
|
||
controller.languageNotice = UserFacingError.message(for: unavailable, context: .model)
|
||
|
||
case "ready":
|
||
controller.applyFixturePhase(.ready)
|
||
controller.warmDuration = .off
|
||
controller.setWarmSession(.cold)
|
||
controller.transcript = "Meet at ten on Thursday and bring the updated design notes."
|
||
|
||
case "warm":
|
||
controller.applyFixturePhase(.ready)
|
||
controller.warmDuration = .fifteenMinutes
|
||
controller.applyFixtureWarmSession(remaining: 14 * 60 + 42)
|
||
controller.transcript = "Meet at ten on Thursday and bring the updated design notes."
|
||
|
||
case "recording":
|
||
controller.applyFixturePhase(.recording)
|
||
controller.warmDuration = .fifteenMinutes
|
||
controller.audioLevel = 0.74
|
||
controller.loudnessDB = -31.0
|
||
controller.recordingDuration = 6.4
|
||
controller.applyFixtureWarmSession(remaining: 14 * 60 + 54)
|
||
|
||
case "transcribing":
|
||
controller.applyFixturePhase(.transcribing)
|
||
controller.warmDuration = .fifteenMinutes
|
||
controller.applyFixtureWarmSession(remaining: 14 * 60 + 52)
|
||
|
||
case "handoff-listening", "handoff-no-home-bar":
|
||
controller.applyFixturePhase(.recording)
|
||
controller.audioLevel = 0.68
|
||
controller.loudnessDB = -32.5
|
||
controller.recordingDuration = 3.2
|
||
controller.applyFixtureWarmSession(remaining: 14 * 60 + 57)
|
||
Task { @MainActor in
|
||
let start = Date().timeIntervalSince1970 - 3.0
|
||
var simTime: Float = 0.0
|
||
while !Task.isCancelled {
|
||
controller.loudnessDB = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
|
||
controller.recordingDuration = Date().timeIntervalSince1970 - start
|
||
simTime += 0.033
|
||
try? await Task.sleep(for: .milliseconds(33))
|
||
}
|
||
}
|
||
|
||
case "handoff-tour":
|
||
controller.applyFixturePhase(.recording)
|
||
controller.audioLevel = 0.68
|
||
controller.loudnessDB = -32.0
|
||
controller.recordingDuration = 0.0
|
||
controller.applyFixtureWarmSession(remaining: 14 * 60 + 58)
|
||
Task { @MainActor in
|
||
let boot = Date().timeIntervalSince1970
|
||
var simTime: Float = 0.0
|
||
while !Task.isCancelled {
|
||
let elapsed = Date().timeIntervalSince1970 - boot
|
||
if elapsed < 3.4 {
|
||
if controller.phase != .recording {
|
||
withAnimation(HexMotion.stage) {
|
||
controller.applyFixturePhase(.recording)
|
||
}
|
||
}
|
||
controller.loudnessDB = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
|
||
controller.recordingDuration = elapsed
|
||
simTime += 0.033
|
||
} else if elapsed < 4.7 {
|
||
if controller.phase != .transcribing {
|
||
withAnimation(HexMotion.stage) {
|
||
controller.applyFixturePhase(.transcribing)
|
||
}
|
||
}
|
||
controller.loudnessDB = -120.0
|
||
} else {
|
||
if controller.phase != .ready {
|
||
withAnimation(HexMotion.stage) {
|
||
controller.applyFixturePhase(.ready)
|
||
}
|
||
}
|
||
}
|
||
try? await Task.sleep(for: .milliseconds(33))
|
||
}
|
||
}
|
||
|
||
case "hosted-keyboard-warm":
|
||
controller.applyFixturePhase(.ready)
|
||
controller.warmDuration = .fifteenMinutes
|
||
controller.applyFixtureWarmSession(remaining: 14 * 60 + 42)
|
||
Task { @MainActor in
|
||
while !Task.isCancelled {
|
||
let t = Date().timeIntervalSince1970
|
||
controller.keyboardBridge.publish(
|
||
KeyboardSnapshot(
|
||
state: .ready,
|
||
heartbeat: t,
|
||
expiresAt: t + 14 * 60 + 42
|
||
)
|
||
)
|
||
try? await Task.sleep(for: .milliseconds(250))
|
||
}
|
||
}
|
||
|
||
case "hosted-keyboard-recording", "hosted-keyboard-light":
|
||
controller.applyFixturePhase(.ready)
|
||
controller.warmDuration = .fifteenMinutes
|
||
controller.applyFixtureWarmSession(remaining: 14 * 60 + 55)
|
||
let startWall = Date().timeIntervalSince1970 - 4.0
|
||
Task { @MainActor in
|
||
var simTime: Float = 0.4
|
||
while !Task.isCancelled {
|
||
let t = Date().timeIntervalSince1970
|
||
let db = -28.5 + 5.2 * sin(simTime * 19.0) + 2.4 * cos(simTime * 7.3)
|
||
controller.keyboardBridge.publish(
|
||
KeyboardSnapshot(
|
||
state: .recording,
|
||
heartbeat: t,
|
||
expiresAt: t + 15 * 60,
|
||
recordingStartedAt: startWall,
|
||
audioLevel: 0.78,
|
||
loudnessDB: db,
|
||
jobID: "hosted-demo-job"
|
||
)
|
||
)
|
||
simTime += 0.033
|
||
try? await Task.sleep(for: .milliseconds(33))
|
||
}
|
||
}
|
||
|
||
case "hosted-keyboard-tour":
|
||
controller.applyFixturePhase(.ready)
|
||
controller.warmDuration = .fifteenMinutes
|
||
controller.applyFixtureWarmSession(remaining: 14 * 60 + 58)
|
||
Task { @MainActor in
|
||
let boot = Date().timeIntervalSince1970
|
||
let freshResultID = "hosted-tour-\(UUID().uuidString)"
|
||
var simTime: Float = 0.0
|
||
while !Task.isCancelled {
|
||
let t = Date().timeIntervalSince1970
|
||
let elapsed = t - boot
|
||
if elapsed < 2.5 {
|
||
controller.keyboardBridge.publish(
|
||
KeyboardSnapshot(
|
||
state: .ready,
|
||
heartbeat: t,
|
||
expiresAt: t + 15 * 60
|
||
)
|
||
)
|
||
} else if elapsed < 5.2 {
|
||
let db = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
|
||
controller.keyboardBridge.publish(
|
||
KeyboardSnapshot(
|
||
state: .recording,
|
||
heartbeat: t,
|
||
expiresAt: t + 15 * 60,
|
||
recordingStartedAt: boot + 2.5,
|
||
audioLevel: 0.78,
|
||
loudnessDB: db,
|
||
jobID: "hosted-tour-job"
|
||
)
|
||
)
|
||
simTime += 0.033
|
||
} else if elapsed < 6.4 {
|
||
controller.keyboardBridge.publish(
|
||
KeyboardSnapshot(
|
||
state: .transcribing,
|
||
heartbeat: t,
|
||
expiresAt: t + 15 * 60,
|
||
jobID: "hosted-tour-job"
|
||
)
|
||
)
|
||
} else {
|
||
controller.keyboardBridge.publish(
|
||
KeyboardSnapshot(
|
||
state: .ready,
|
||
heartbeat: t,
|
||
expiresAt: t + 14 * 60 + 54,
|
||
resultID: freshResultID,
|
||
resultExpiresAt: boot + 30,
|
||
transcript: "See you at ten."
|
||
)
|
||
)
|
||
}
|
||
try? await Task.sleep(for: .milliseconds(33))
|
||
}
|
||
}
|
||
|
||
default:
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
private func applyFixtureWarmSession(remaining: TimeInterval) {
|
||
setWarmSession(.warm(expiresAt: Date().timeIntervalSince1970 + remaining), remaining: remaining)
|
||
}
|
||
|
||
/// `settings-dictation-model-<english|multilingual>-<waiting|downloading|ready>`: Settings ›
|
||
/// Dictation › Model with that model picked and its download in that state. Waiting is the
|
||
/// real rule: pass `--simulate-cellular` and the keyboard has measured room for the model,
|
||
/// so only the connection holds it. The other states leave the keyboard unmeasured, so
|
||
/// nothing really downloads on the simulator's Wi-Fi. English also has what the keyboard
|
||
/// said about a downloaded model, preparing|short|noengine|failed (short with 41 MB free),
|
||
/// and toosmall, a keyboard limit of 118 MB, under KeyboardParakeetBudget.requiredLimit;
|
||
/// toosmalldownloaded has English's model on the iPhone all the same (a sparse file of its
|
||
/// size), as a switch turned on by hand used to download it, for Remove.
|
||
private func applySpeechModelFixture(_ scene: String) {
|
||
let multilingual = scene.contains("-multilingual-")
|
||
let step = scene.split(separator: "-").last.map(String.init) ?? "waiting"
|
||
let keyboardSaid: [String: (KeyboardParakeetSettings.KeyboardState, KeyboardParakeetSettings.UnavailableReason?)] = [
|
||
"preparing": (.preparing, nil),
|
||
"short": (.unavailable, .shortOfMemory),
|
||
"noengine": (.unavailable, .noNeuralEngine),
|
||
"failed": (.unavailable, .loadFailed),
|
||
]
|
||
let said = multilingual ? nil : keyboardSaid[step]
|
||
let card = multilingual ? "waiting" : said == nil && step != "toosmalldownloaded" ? step : "ready"
|
||
KeyboardParakeetDownload.shared.applyFixture("model-card-\(card)")
|
||
let megabyte = KeyboardParakeetBudget.megabyte
|
||
if step == "waiting" {
|
||
KeyboardParakeetSettings().recordKeyboardLimit(KeyboardParakeetBudget.requiredLimit)
|
||
} else if step.hasPrefix("toosmall") {
|
||
KeyboardParakeetSettings().recordKeyboardLimit(118 * megabyte)
|
||
}
|
||
if step == "toosmalldownloaded" {
|
||
// Hex itself dictates with Apple's speech recognition, as on an iPhone 14, so the
|
||
// model is the keyboard's alone and Remove shows.
|
||
if #available(iOS 26.0, *) {
|
||
transcriber = AppleSpeechTranscriber(language: SpeechLanguage(locale: Locale(identifier: "en-US"), module: .speech))
|
||
}
|
||
applyFixtureDictationLanguage()
|
||
let file = ParakeetTranscriber.cacheDirectory.appendingPathComponent("fixture.bin")
|
||
FileManager.default.createFile(atPath: file.path, contents: nil)
|
||
let handle = try? FileHandle(forWritingTo: file)
|
||
try? handle?.truncate(atOffset: UInt64(KeyboardParakeetDownload.bytes))
|
||
try? handle?.close()
|
||
}
|
||
if let said {
|
||
KeyboardParakeetSettings().setKeyboardState(said.0, reason: said.1, free: step == "short" ? 41 * megabyte : nil)
|
||
}
|
||
MultilingualParakeetSettings().isEnabled = multilingual
|
||
downloadNetwork.forgetMobileData()
|
||
guard multilingual else { return }
|
||
ParakeetUltraModel.remove()
|
||
switch step {
|
||
case "downloading":
|
||
multilingualDownload.applyFixture(share: 0.42)
|
||
case "ready":
|
||
// Files of the right sizes (sparse), so it reads as on the iPhone; nothing loads them.
|
||
for file in ParakeetUltraModel.files {
|
||
let url = ParakeetUltraModel.directory.appendingPathComponent(file.path)
|
||
try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||
FileManager.default.createFile(atPath: url.path, contents: nil)
|
||
let handle = try? FileHandle(forWritingTo: url)
|
||
try? handle?.truncate(atOffset: UInt64(file.size))
|
||
try? handle?.close()
|
||
}
|
||
KeyboardParakeetSettings().setKeyboardState(.ready, multilingual: true)
|
||
default:
|
||
startMultilingualDownloadIfWanted()
|
||
}
|
||
}
|
||
}
|