hex/ios/HEXMobile/TypingConfigs/TypingConfigExport.swift
Ryan Vogel f18790ef43 iOS: typing configs page, history and Export for your agent (experiment)
Steps 3 and 4 of the phone side, on the shared reader's latest (21cdab23).

- Settings › Keyboard › Typing configs (beside Vocabulary, only while the experiment is on):
  Hex defaults first, then every kept config newest first with its status (In use, Best so
  far, applied or opened when), Use on each row (one tap, deletes nothing), and Delete all.
  A config's page shows its facts (based on, made from another iPhone's typing), Use or
  Use Hex defaults instead, Rename, Share (the kept file, unchanged) and Delete, and its
  parts with their switches (TypingConfigParts, shared with the review sheet), applied at
  once while it is the one in use.
- History: never refuses a new config. At 20 the oldest that is neither active nor best so
  far goes, and the review names it before Apply (replaces the old refusal). Not now keeps
  the parts switched off. Records this phone's export ids.
- Experiment off: the sheet's first action is Turn on typing configs, which shows the
  review at once; Open Experiments stays as the quieter second.
- The review now reads the keyboards the Hex keyboard recorded (TypingConfigSettings
  .keyboards) instead of a keyboard the app built (TypingConfigKeyboard.swift removed), and
  each item's own list, position, evidence and cleaned text instead of indexing by path;
  the note is the review's cleaned one. With no keyboard recorded yet the sheet says so
  under For another keyboard. hiddenCharacters has its sentence. The active config is
  compiled again when Hex comes to the foreground and a keyboard was recorded since.
- Delete what Hex learned also deletes every typing config and goes back to Hex defaults;
  its confirmation says so.
- Export for your agent (Typing logs, experiment on): the warning sheet every time (design
  §5's wording, a variant for fewer days), the days to include, then one zip: the bundled
  package (ios/TypingConfig/package as a folder resource), the chosen logs and state/
  (phone.json as state.md defines it, the recorded keyboards' layout; vocabulary.json with
  each word's model cost and spell-checker answer). Deleted once the share sheet closes.
- Hex's own dictation (handed to Hex, Action Button) applies the active config's dictation
  fixes after Learn how I type's, never to a translated dictation (TypingConfigDictation).
- Strings in English and Simplified Chinese.

Verified on the Studio (iOS 27.2): TypingLogTests 15 of 15 (history at the limit, apply and go
back, dictation fixes off for translation); every screen on iPhone 17 Pro and iPhone SE (3rd
gen) in English and 简体中文; the export of two real days (6.6 MB of logs) is a 772 KB zip,
built in about 2.5 s from Continue; its state/ read by the package's overview.py; the share
sheet offers Copy, Save to Files and More (AirDrop and other apps don't show on the
simulator). Through the real
keyboard (Full Access granted in the simulator's TCC database, the keyboard's typing log and
Full Access report confirming it), a tap 3.35 pt inside r types R with Hex defaults and E with
a key-off config applied through the review sheet (e's right edge +0.2, word-rule evidence),
the keyboard closed and opened in between.
2026-11-23 14:18:41 -05:00

232 lines
13 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
import UIKit
/// Export for your agent (Settings › Privacy › Typing logs): one zip holding the agent package
/// (`ios/TypingConfig/package`, bundled as `package`), the chosen days' logs and `state/`
/// (`package/reference/state.md`). It holds everything the logs hold, words included, so it
/// is written to a temporary folder and deleted once shared.
enum TypingConfigExport {
/// The text fields of a log line that hold what was typed or dictated.
nonisolated static let textKeys: Set<String> = ["inserted", "from", "to", "text", "before", "after",
"raw", "fixed", "spelled", "tagged", "cleaned", "translated", "formatted"]
struct Built: Sendable {
let url: URL
let bytes: Int
let seconds: Double
}
enum Failure: Error {
case noPackage
case zip
}
/// Builds the zip for `days`. The state is read on the main actor; copying, the vocabulary's
/// words and zipping run off it.
@MainActor
static func build(days: [TypingLogDay], now: Date = Date()) async throws -> Built {
let start = Date()
guard let package = Bundle.main.url(forResource: "package", withExtension: nil) else { throw Failure.noPackage }
let exportID = "export-\(stamp(now, "yyyyMMdd"))-\(UUID().uuidString.prefix(8).lowercased())"
let keyboards = TypingConfigSettings().keyboards
let phone = try JSONSerialization.data(withJSONObject: phoneState(exportID: exportID, keyboards: keyboards), options: [.prettyPrinted, .sortedKeys])
let language = keyboards.first.flatMap { KeyboardLanguage(rawValue: $0.context.language) }
?? LanguageSettings().keyboardLanguages.first ?? .english
let model = TypingDecoderModel.bundled(language.rawValue)
let folderName = "hex-typing-export-\(stamp(now, "yyyy-MM-dd"))"
let work = FileManager.default.temporaryDirectory.appendingPathComponent("TypingConfigExport-\(exportID)", isDirectory: true)
let root = work.appendingPathComponent(folderName, isDirectory: true)
let logs = days.map(\.url)
let words = try await Task.detached(priority: .userInitiated) { () throws -> [String: Int] in
let fm = FileManager.default
try? fm.removeItem(at: work)
try fm.createDirectory(at: work, withIntermediateDirectories: true)
try fm.copyItem(at: package, to: root)
let logFolder = root.appendingPathComponent("logs", isDirectory: true)
try fm.createDirectory(at: logFolder, withIntermediateDirectories: true)
for url in logs { try fm.copyItem(at: url, to: logFolder.appendingPathComponent(url.lastPathComponent)) }
let state = root.appendingPathComponent("state", isDirectory: true)
try fm.createDirectory(at: state, withIntermediateDirectories: true)
try phone.write(to: state.appendingPathComponent("phone.json"))
return tokens(in: logs)
}.value
// The vocabulary: each word's cost in Hex's model and whether the spell checker takes it.
let checker = UITextChecker()
let checkLanguage = language.spellCheckLanguage(available: UITextChecker.availableLanguages)
var vocabulary: [String: Any] = [:]
for (n, word) in words.keys.sorted().enumerated() {
if n % 400 == 0 { await Task.yield() }
var entry: [String: Any] = ["count": words[word] ?? 0]
entry["model"] = model.flatMap { m in m.index(of: word).map { (m.unigramCost($0) * 100).rounded() / 100 } } ?? NSNull()
if let checkLanguage {
let range = NSRange(location: 0, length: (word as NSString).length)
entry["dictionary"] = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: checkLanguage).location == NSNotFound
} else {
entry["dictionary"] = NSNull()
}
vocabulary[word] = entry
}
let vocabularyFile = try JSONSerialization.data(withJSONObject: ["language": language.rawValue, "words": vocabulary] as [String: Any], options: [.sortedKeys])
let zipName = folderName + ".zip"
let zip = try await Task.detached(priority: .userInitiated) { () throws -> URL in
try vocabularyFile.write(to: root.appendingPathComponent("state/vocabulary.json"))
let out = work.appendingPathComponent(zipName)
var copied = false
var error: NSError?
// A folder read "for uploading" comes back zipped.
NSFileCoordinator().coordinate(readingItemAt: root, options: .forUploading, error: &error) { zipped in
copied = (try? FileManager.default.copyItem(at: zipped, to: out)) != nil
}
try? FileManager.default.removeItem(at: root)
guard copied else { throw Failure.zip }
return out
}.value
TypingConfigHistory().recordExport(exportID)
let bytes = (try? zip.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
return Built(url: zip, bytes: bytes, seconds: Date().timeIntervalSince(start))
}
/// Deletes an export once it has been shared (or not).
nonisolated static func discard(_ built: Built) {
try? FileManager.default.removeItem(at: built.url.deletingLastPathComponent())
}
/// Every word-like token in the logs' text, lowercased, with how often it appears.
nonisolated static func tokens(in logs: [URL]) -> [String: Int] {
var counts: [String: Int] = [:]
func add(_ text: String) {
var word = ""
func flush() {
let trimmed = word.trimmingCharacters(in: CharacterSet(charactersIn: "'"))
if trimmed.count >= 1, trimmed.count <= 40 { counts[trimmed, default: 0] += 1 }
word = ""
}
for ch in text.lowercased() {
if ch.isLetter || ((ch == "'" || ch == "’") && !word.isEmpty) {
word.append(ch == "’" ? "'" : ch)
} else if !word.isEmpty {
flush()
}
}
if !word.isEmpty { flush() }
}
for url in logs {
guard let data = try? Data(contentsOf: url, options: .mappedIfSafe) else { continue }
for line in data.split(separator: UInt8(ascii: "\n")) {
guard let object = (try? JSONSerialization.jsonObject(with: Data(line))) as? [String: Any] else { continue }
for key in textKeys {
if let text = object[key] as? String { add(text) }
}
}
}
return counts
}
// MARK: - state/phone.json
@MainActor
static func phoneState(exportID: String, keyboards: [TypingConfigKeyboard]) -> [String: Any] {
let info = Bundle.main.infoDictionary
let keyboardSettings = FullKeyboardSettings()
let languages = LanguageSettings()
let journal = TypingJournal.load(from: FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: KeyboardBridge.appGroup)?
.appendingPathComponent(TypingJournal.fileName))
let learning = TypingLearning.derive(from: journal.corrections)
let defaults = UserDefaults(suiteName: KeyboardBridge.appGroup)
let learned = LearnedWordsStore(defaults: defaults).words
// Learn how I type's averages as the decoder weighs them (TypingDecoder.offsets), in keycaps.
var offsets: [String: [Double]] = [:]
for (key, stats) in journal.keys where key.count == 1 && key.first?.isLetter == true && stats.count >= 20 {
let weight = Double(stats.count) / Double(stats.count + 20)
offsets[key] = [round2(stats.meanOffset.dx * weight), round2(stats.meanOffset.dy * weight)]
}
let adaptive = AdaptiveTouchStatus.current(fullAccessConfirmed: KeyboardBridge().fullAccessSeenAt != nil)
let history = TypingConfigHistory()
let active: Any = history.active.map { entry -> [String: Any] in
let id = history.data(entry.id).flatMap { try? JSONDecoder().decode(TypingConfigFile.self, from: $0) }?.id
return ["id": id ?? NSNull(), "sha256": entry.id, "name": entry.name]
} ?? NSNull()
return [
"exportId": exportID,
"formats": TypingConfigExportState.formats,
"device": TypingLogSessionInfo.deviceModel,
"system": "\(UIDevice.current.systemName) \(UIDevice.current.systemVersion)",
"hexVersion": info?["CFBundleShortVersionString"] as? String ?? "",
"hexBuild": info?["CFBundleVersion"] as? String ?? "",
"locale": Locale.current.identifier,
"keyboardLanguages": languages.keyboardLanguages.isEmpty
? Array(Set(keyboards.map(\.context.language))).sorted()
: languages.keyboardLanguages.map(\.rawValue),
// As session_start.settings in the logs (KeyboardViewController.typingLogSessionInfo).
"settings": [
"autocorrect": keyboardSettings.autocorrect,
"spatialAutocorrect": keyboardSettings.spatialAutocorrect,
"swipeTyping": keyboardSettings.swipeTyping,
"emoji": keyboardSettings.emoji,
"key_popups": keyboardSettings.keyPopups,
"haptics": keyboardSettings.haptics,
"learnHowIType": TypingJournalSettings().isEnabled,
"cleanupTagger": CleanupTaggerSettings().isEnabled,
"hexKeyPlacement": keyboardSettings.hexKeyPlacement.rawValue,
"keyboardLayout": keyboards.first?.context.language ?? "",
"dictationLanguage": languages.dictationLanguage ?? "automatic",
"numberRow": keyboardSettings.numberRow,
] as [String: Any],
"experiments": [
"adaptiveTouch": AdaptiveTouchSettings().isEnabled,
"learnEachLanguageSeparately": TypingJournalSettings().learnsPerLanguage,
"typingConfigs": TypingConfigSettings().isEnabled,
],
// The keyboard Hex's keys last ran on (TypingConfigSettings.keyboards), and every one
// they have run on; null when the keyboard hasn't been used with the experiment on.
"layout": keyboards.first.map(Self.layout) ?? NSNull(),
"keyboards": keyboards.map { Self.context($0.context) },
"learnedWords": ["count": learned.count, "known": learned, "complete": learned.count < LearnedWordsStore.maxWords],
"learnedFixes": ["pairs": learning.typos.sorted { $0.key < $1.key }.map { [$0.key, $0.value] }, "complete": true],
"neverCorrect": learning.rejected.sorted(),
"tapOffsets": ["keycaps": offsets],
"adaptiveTouch": ["inUse": adaptive == .active, "offsets": NSNull()],
"activeTypingConfig": active,
]
}
private static func round2(_ v: CGFloat) -> Double { (Double(v) * 1000).rounded() / 1000 }
/// `layout` as state.md names it, from a recorded keyboard (`TypingConfigExportState.layout`'s
/// shape, which reads a live layout).
static func layout(_ keyboard: TypingConfigKeyboard) -> [String: Any] {
func rect(_ r: CGRect) -> [Double] { [r.minX, r.minY, r.width, r.height].map { (Double($0) * 100).rounded() / 100 } }
let g = keyboard.geometry
// The gap between two neighbouring letter keycaps in a row.
let row = g.keys.filter { $0.row == 0 }.sorted { $0.frame.minX < $1.frame.minX }
let gap = zip(row, row.dropFirst()).map { $1.frame.minX - $0.frame.maxX }.min() ?? 0
let keys: [[String: Any]] = g.keys.map { key in
var k: [String: Any] = ["id": key.id, "row": key.row, "frame": rect(key.frame), "hitFrame": rect(key.hitFrame)]
if let band = key.gapHitFrame { k["gapHitFrame"] = rect(band) }
return k
}
return [
"context": context(keyboard.context), "letterWidth": Double(g.letterWidth), "keyHeight": Double(g.keyHeight),
"gap": (Double(gap) * 100).rounded() / 100, "rowGap": ((keyboard.context.rowPitch - Double(g.keyHeight)) * 100).rounded() / 100,
"touchArea": rect(g.touchArea), "keys": keys,
]
}
static func context(_ c: TypingConfigFile.Context) -> [String: Any] {
["width": c.width, "orientation": c.orientation, "posture": c.posture, "language": c.language,
"letters": c.letters, "split": c.split, "hexPlacement": c.hexPlacement, "numberRow": c.numberRow,
"keyHeight": c.keyHeight, "rowPitch": c.rowPitch, "letterWidth": c.letterWidth, "hitModel": c.hitModel]
}
private static func stamp(_ date: Date, _ format: String) -> String {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.calendar = Calendar(identifier: .gregorian)
formatter.dateFormat = format
return formatter.string(from: date)
}
}