// Voice at the boundary: the `voice.*` names and the shapes they carry // (the voice plan, 17 Nov 2026: "audio in, timed words out"). // // THE CORE DECIDES, THE PHONE LISTENS AND DRAWS. Every rule about what the // words become (settled and tentative text, spoken punctuation, casing, the // vocabulary, reconciling the second pass, command mode, every line the // control says) is the core's. What this side owns is the microphone, running // the engines the plan names, feeding what they heard back, and drawing the // answer. So nothing here interprets text: a field the core adds later is // ignored, a field it leaves out decodes to its empty value, and a shape the // screen draws is exactly what came back. // // THE CONTRACT is the crate's: `crates/cotton-ffi/src/phone/voice.rs`'s // header. Where this file and that header disagree, the header wins. import Foundation extension CottonMethod { /// `voice.plan` — before the microphone opens: which engines, which /// languages, the vocabulary, the downloads a better result needs and the /// words the control says. public static let voicePlan = "voice.plan" /// `voice.heard` — every partial and final from either pass; answers the /// text as it stands and the edits to apply. public static let voiceHeard = "voice.heard" /// `voice.touched` — the person typed or selected inside the dictated text; /// the second pass never changes that range again. public static let voiceTouched = "voice.touched" /// `voice.end` — the session is over; answers what was written. public static let voiceEnd = "voice.end" /// `voice.models` — the catalogue joined with what this phone holds. public static let voiceModels = "voice.models" /// `voice.model` — one model's state changed on this phone. public static let voiceModel = "voice.model" /// `voice.settings` — the person's languages, scripts, passes and the /// mobile-data rule. public static let voiceSettings = "voice.settings" } /// Dictating into text, or speaking a command. public enum VoiceMode: String, Codable, Sendable, Equatable { case dictate case command } /// Which pass a hearing came from. public enum VoicePass: String, Codable, Sendable, Equatable { case live case second } // MARK: - voice.plan public struct VoicePlanRequest: Encodable, Sendable, Equatable { /// Where the microphone is: `deck`, `story.writer`, `note`, `fan`, … public var surface: String public var table: String? public var column: String? public var record: String? public var database: String? public var mode: VoiceMode public init(surface: String, table: String? = nil, column: String? = nil, record: String? = nil, database: String? = nil, mode: VoiceMode) { self.surface = surface self.table = table self.column = column self.record = record self.database = database self.mode = mode } } /// One engine the plan names: `apple.speech`, `android.speech` or /// `sherpa:`, with the model it runs where that is separate. public struct VoiceEngineChoice: Codable, Sendable, Equatable { public var engine: String public var model: String? public init(engine: String, model: String? = nil) { self.engine = engine self.model = model } /// The model id a `sherpa:` engine names, from `model` or the engine id. public var sherpaModel: String? { guard engine.hasPrefix(VoiceEngineChoice.sherpaPrefix) else { return nil } if let model, !model.isEmpty { return model } let id = String(engine.dropFirst(VoiceEngineChoice.sherpaPrefix.count)) return id.isEmpty ? nil : id } public static let appleSpeech = "apple.speech" public static let androidSpeech = "android.speech" public static let sherpaPrefix = "sherpa:" /// The platform's own recogniser, by either platform's name. public static let platformRecognisers: Set = [appleSpeech, androidSpeech] } /// A model the plan wants on this phone and does not find. public struct VoiceDownload: Codable, Sendable, Equatable, Identifiable { public var model: String /// The file's name in the model's folder (one entry per file). public var file: String public var bytes: Int64 public var url: String public var sha256: String /// The core's sentence for why: "Better Telugu", with its size in its own words. public var why: String public var id: String { model } public init(model: String, file: String = "", bytes: Int64, url: String, sha256: String, why: String) { self.model = model self.file = file self.bytes = bytes self.url = url self.sha256 = sha256 self.why = why } enum CodingKeys: String, CodingKey { case model, file, bytes, url, sha256, why } public init(from decoder: any Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) model = try c.decode(String.self, forKey: .model) url = try c.decodeIfPresent(String.self, forKey: .url) ?? "" file = try c.decodeIfPresent(String.self, forKey: .file) ?? URL(string: url)?.lastPathComponent ?? model bytes = try c.decodeIfPresent(Int64.self, forKey: .bytes) ?? 0 sha256 = try c.decodeIfPresent(String.self, forKey: .sha256) ?? "" why = try c.decodeIfPresent(String.self, forKey: .why) ?? "" } } public struct VoicePlan: Decodable, Sendable, Equatable { public var session: String public var mode: VoiceMode public var languages: [String] public var live: VoiceEngineChoice public var second: VoiceEngineChoice? public var vocabulary: [String] public var downloads: [VoiceDownload] /// Each language's script (ISO 15924, lowercase): `{"te": "latn"}`. public var scripts: [String: String] /// The person's rule for downloads off Wi-Fi: `never`, `ask`, `always`. public var mobileData: String /// Every word the control says, keyed by the core (`listening`, /// `settling`, `download`, `denied`, …). The control reads a key and /// falls back to the shell's kit line of the same name. public var lines: [String: String] public init( session: String, mode: VoiceMode, languages: [String], live: VoiceEngineChoice, second: VoiceEngineChoice? = nil, vocabulary: [String] = [], downloads: [VoiceDownload] = [], lines: [String: String] = [:] ) { self.session = session self.mode = mode self.languages = languages self.live = live self.second = second self.vocabulary = vocabulary self.downloads = downloads self.lines = lines scripts = [:] mobileData = "ask" } enum CodingKeys: String, CodingKey { case session, mode, languages, live, second, vocabulary, downloads, lines, scripts case mobileData = "mobile_data" } public init(from decoder: any Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) session = try c.decode(String.self, forKey: .session) mode = try c.decodeIfPresent(VoiceMode.self, forKey: .mode) ?? .dictate languages = try c.decodeIfPresent([String].self, forKey: .languages) ?? [] live = try c.decode(VoiceEngineChoice.self, forKey: .live) second = try c.decodeIfPresent(VoiceEngineChoice.self, forKey: .second) vocabulary = try c.decodeIfPresent([String].self, forKey: .vocabulary) ?? [] downloads = try c.decodeIfPresent([VoiceDownload].self, forKey: .downloads) ?? [] scripts = (try? c.decodeIfPresent([String: String].self, forKey: .scripts)) ?? [:] mobileData = (try? c.decodeIfPresent(String.self, forKey: .mobileData)) ?? "ask" // `lines` is "…words the control says…": strings by key. A value that // is not a string (a nested set) is skipped rather than failing the plan. if let tree = try c.decodeIfPresent(JSONValue.self, forKey: .lines), case .object(let pairs) = tree { var flat: [String: String] = [:] for (key, value) in pairs { if case .string(let s) = value { flat[key] = s } } lines = flat } else { lines = [:] } } } // MARK: - voice.heard /// One timed word as an engine heard it. public struct VoiceWord: Codable, Sendable, Equatable { public var w: String public var startMs: Int public var endMs: Int public var conf: Double? public init(w: String, startMs: Int, endMs: Int, conf: Double? = nil) { self.w = w self.startMs = startMs self.endMs = endMs self.conf = conf } enum CodingKeys: String, CodingKey { case w case startMs = "start_ms" case endMs = "end_ms" case conf } } public struct VoiceHeard: Encodable, Sendable, Equatable { public var session: String public var pass: VoicePass public var final: Bool public var text: String public var words: [VoiceWord]? /// The audio the result covers, from the session's start. public var fromMs: Int? public var toMs: Int? public var language: String? public init(session: String, pass: VoicePass, final: Bool, text: String, words: [VoiceWord]? = nil, fromMs: Int? = nil, toMs: Int? = nil, language: String? = nil) { self.session = session self.pass = pass self.final = final self.text = text self.words = words self.fromMs = fromMs self.toMs = toMs self.language = language } enum CodingKeys: String, CodingKey { case session, pass, final, text, words, language case fromMs = "from_ms" case toMs = "to_ms" } } /// Replace `from.. VoiceModelEntry { var out = self if out.kind.isEmpty { out.kind = known.kind } if out.family.isEmpty { out.family = known.family } if out.role.isEmpty { out.role = known.role } if !known.options.isEmpty { out.options = known.options } if !known.languageCodes.isEmpty { out.languageCodes = known.languageCodes } out.level = known.level ?? out.level out.files = files.map { file in var file = file if file.part.isEmpty, let named = known.files.first(where: { $0.name == file.name }) { file.part = named.part } return file } return out } } public struct VoiceModelsAnswer: Decodable, Sendable, Equatable { public var models: [VoiceModelEntry] /// The catalogue's voice-activity and language-identification models. public var vad: String? public var lid: String? public init(models: [VoiceModelEntry], vad: String? = nil, lid: String? = nil) { self.models = models self.vad = vad self.lid = lid } enum CodingKeys: String, CodingKey { case models, vad, lid } public init(from decoder: any Decoder) throws { // Either `{ models: [...] }` or the bare list. if let c = try? decoder.container(keyedBy: CodingKeys.self), let models = try c.decodeIfPresent([VoiceModelEntry].self, forKey: .models) { self.models = models vad = try c.decodeIfPresent(String.self, forKey: .vad) lid = try c.decodeIfPresent(String.self, forKey: .lid) } else { models = try decoder.singleValueContainer().decode([VoiceModelEntry].self) } } } public struct VoiceModelChange: Encodable, Sendable, Equatable { public var model: String public var state: VoiceModelState public var path: String? public var bytesDone: Int64? public init(model: String, state: VoiceModelState, path: String? = nil, bytesDone: Int64? = nil) { self.model = model self.state = state self.path = path self.bytesDone = bytesDone } enum CodingKeys: String, CodingKey { case model, state, path case bytesDone = "bytes_done" } } // MARK: - voice.settings /// The person's voice settings — read here only for what the PHONE acts on /// (the mobile-data rule for downloads); the rest is the core's. public struct VoiceSettings: Decodable, Sendable, Equatable { public struct Language: Decodable, Sendable, Equatable { public var code: String public var script: String? } public var languages: [Language] /// `never`, `ask` or `always`: may a model download off Wi-Fi. public var mobileData: String public init(languages: [Language] = [], mobileData: String = "ask") { self.languages = languages self.mobileData = mobileData } /// Off Wi-Fi: `always` yes; `ask` yes when the person asked for this /// download (a tap on the offer is the asking); `never` no. public func mobileDataAllowed(asked: Bool) -> Bool { mobileData == "always" || (mobileData == "ask" && asked) } enum CodingKeys: String, CodingKey { case languages case mobileData = "mobile_data" } public init(from decoder: any Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) languages = (try? c.decodeIfPresent([Language].self, forKey: .languages)) ?? [] mobileData = (try? c.decodeIfPresent(String.self, forKey: .mobileData)) ?? "ask" } }