// What every microphone listens for, read from the core just before each // session: `read.voiceContext` ranks the person's manual words and the names // in their databases in Rust, and answers the person's language. A failed // optional read leaves dictation available with no hints and the device's // language. import CottonKit import Foundation public enum VoiceBias { @MainActor public static func hints(core: any CottonCalling) async -> DictationHints { // The device's own language model rides with the hints when this // process has loaded one; the first microphone starts the keeper. let keeper = VoiceModelKeeper.shared keeper.ensure() var hints = (try? await core.callJSON(CottonMethod.readVoiceContext)).map { hints($0) } ?? DictationHints() hints.model = await keeper.model(for: hints.locale) return hints } /// The answer's hints, in the core's order and exact spelling: /// `biasGroups` — `[{tag, terms}]`, the ranked hundred split by why each /// term is there (manual, people, places, projects, screen) — tag by tag. static func hints(_ read: JSONValue) -> DictationHints { let groups = read.list("biasGroups").compactMap { group -> DictationHints.Group? in guard let tag = group["tag"]?.string else { return nil } return DictationHints.Group(tag: tag, terms: terms(group.list("terms"))) } return DictationHints(groups: groups, language: read["language"]?.string) } private static func terms(_ list: [JSONValue]) -> [String] { // Nonblank spaces inside a term are the person's, and kept. list.compactMap(\.string).filter { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } } } @MainActor public extension BodyDictation { /// Read just before opening this session, even while its screen stays up. @discardableResult func start(core: any CottonCalling) async -> DictationOutcome? { await start(hints: VoiceBias.hints(core: core)) } }