Files
OSGKeyboard/OSGKeyboardShared/Typing/LibrimeEngine.swift
T
rockyandClaude Opus 5 0023741455 feat(app): add crash visibility, memory relief, and clipboard reply work
Squashes the working tree that had accumulated on cloud-agent. Two
strands of work, kept in one commit at the maintainer's request.

Crash and stability (this session):

- Subscribe to MetricKit so crash, hang, CPU, and disk-write diagnostics
  are recorded on device. Keyboard-extension memory kills are the most
  likely way a user loses the keyboard mid-sentence and iOS does not
  classify them as crashes, so they never reach App Store Connect.
- Upload those reports to POST /v1/analytics/diagnostics from internal
  builds only (local Debug and TestFlight), reusing the analytics
  installation identifier and skipping entirely when analytics are off.
  App Store builds keep everything on device.
- Shed the Chinese engine, English lexicon, and in-flight pipeline work
  at the 40 MiB and 48 MiB budget thresholds instead of waiting for a
  system memory warning that jetsam often preempts.
- Install audio taps with the input node's live format through one
  shared adaptive resampler, so an audio-route change mid-recording no
  longer raises an uncatchable format-mismatch exception. Covers the
  dictation preview and the Mac recorder, which still used the pattern
  the Flow capture path had already moved away from.
- Harden the librime bridge: never pass a nil string across a nonnull
  boundary when a user dictionary holds malformed UTF-8, null-check the
  engine API vector, and distrust a candidate count without its array.
- Guard the expanded candidate grid against a list that shrinks while
  the collection view is still laying out against the previous count.
- Resolve localized text without the App Group so the screen shown when
  that container is missing renders its message instead of crashing on
  launch, which is the one situation it exists to handle.
- Gate CI on the App Group and Keychain wiring that the keyboard
  extension hard-depends on, with a self-test proving the check fails.

Clipboard, reply, and semantics (pre-existing working tree):

- Unify the reply center, refresh keyboard AI features, and add native
  event and reminder exporters.
- Expand clipboard semantic routing with authored corpora and a
  product-blind holdout, plus the scripts that build and validate them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:31:29 -07:00

186 lines
5.8 KiB
Swift

// LibrimeEngine.swift
// OSGKeyboard · Shared
//
// Production Chinese IME backed by librime. Runtime access stays on the
// main actor because UIInputViewController and its text proxy are main-only;
// expensive schema deployment is performed by the host app beforehand.
import Foundation
@MainActor
public final class LibrimeEngine: RimeEngineBridging {
public private(set) var composition: TypingComposition = .empty
public private(set) var isReady = false
public private(set) var schema: TypingInputSchema
private var language: TypingInputLanguage = .chinese
private var bridge: OSGRimeBridge?
private let configurationProvider: () -> TypingInputConfigurationSnapshot
private let candidateLimit: Int
public init(
schema: TypingInputSchema = .fullPinyin,
candidateLimit: Int = 160,
configurationProvider: @escaping () -> TypingInputConfigurationSnapshot = {
TypingInputConfiguration.shared.snapshot
}
) {
self.schema = schema
self.candidateLimit = candidateLimit
self.configurationProvider = configurationProvider
}
public func prepare() async throws {
if isReady { return }
guard RimeResourceInstaller.isReady else {
throw RimeResourceError.resourcesNotInstalled
}
let paths = try RimeResourcePaths.resolve()
let runtime = OSGRimeBridge(
sharedDataDirectory: paths.sharedData.path,
userDataDirectory: paths.userData.path,
distributionVersion: RimeResourceInstaller.resourceVersion
)
try runtime.start()
let configured = configurationProvider()
schema = configured.schema
guard runtime.selectSchema(schema.rawValue) else {
runtime.stopSession()
throw LibrimeEngineError.schemaUnavailable(schema.rawValue)
}
_ = runtime.setASCIIMode(language == .english)
bridge = runtime
isReady = true
_ = refresh()
}
public func teardown() {
bridge?.clearComposition()
// Session destroy alone does not flush LevelDB user dictionaries.
bridge?.finalizeRuntime()
bridge = nil
composition = .empty
isReady = false
}
public func setLanguage(_ language: TypingInputLanguage) {
self.language = language
_ = bridge?.setASCIIMode(language == .english)
if language == .english {
bridge?.clearComposition()
composition = .empty
} else {
_ = refresh()
}
}
@discardableResult
public func setSchema(_ schema: TypingInputSchema) -> Bool {
guard let bridge else {
self.schema = schema
return false
}
bridge.clearComposition()
guard bridge.selectSchema(schema.rawValue) else { return false }
self.schema = schema
composition = .empty
return true
}
public func processCharacter(_ character: Character) -> String? {
guard language == .chinese,
let scalar = character.asciiValue,
bridge?.processKeyCode(Int32(scalar), modifiers: 0) == true else {
return nil
}
return refresh()
}
public func processBackspace() -> String? {
guard bridge?.processKeyCode(OSGRimeKeyBackSpace, modifiers: 0) == true else {
return nil
}
return refresh()
}
public func processSpace() -> String? {
guard bridge?.processKeyCode(32, modifiers: 0) == true else {
return " "
}
return refresh()
}
public func processReturn() -> String? {
guard bridge?.processKeyCode(OSGRimeKeyReturn, modifiers: 0) == true else {
return "\n"
}
return refresh()
}
public func selectCandidate(at index: Int) -> String {
guard bridge?.selectCandidate(at: index) == true else { return "" }
return refresh() ?? ""
}
public func flushPreedit() -> String {
let raw = bridge?.rawInput() ?? ""
bridge?.clearComposition()
composition = .empty
return raw
}
public func clearComposition() {
bridge?.clearComposition()
composition = .empty
}
/// Copies librime-owned memory into Sendable Swift value types and returns
/// any commit emitted by the preceding key operation.
@discardableResult
private func refresh() -> String? {
guard let snapshot = bridge?.snapshot(withCandidateLimit: candidateLimit) else {
composition = .empty
return nil
}
let preedit = snapshot.preedit
let raw = bridge?.rawInput() ?? ""
composition = TypingComposition(
preedit: preedit,
rawInput: raw,
candidates: snapshot.candidates.enumerated().map { displayIndex, candidate in
TypingCandidate(
id: "\(preedit)|\(displayIndex)|\(candidate.index)|\(candidate.text)",
text: candidate.text,
annotation: candidate.comment.isEmpty ? nil : candidate.comment,
engineIndex: Int(candidate.index)
)
}
)
return snapshot.commitText.isEmpty ? nil : snapshot.commitText
}
}
public enum LibrimeEngineError: LocalizedError {
case schemaUnavailable(String)
public var errorDescription: String? {
switch self {
case .schemaUnavailable(let id):
return SharedL10n.format("error.rime.schemaUnavailable", id)
}
}
}
private extension Character {
var asciiValue: UInt8? {
guard let scalar = unicodeScalars.first,
unicodeScalars.count == 1,
scalar.value <= UInt8.max else {
return nil
}
return UInt8(scalar.value)
}
}