//! # `voice.*` — THE CONTRACT between the core and the platforms' microphones //! //! Audio in, timed words out; the core decides, the platform listens and //! draws. Four jobs share it: dictation into any text, spoken commands, //! meeting transcripts and film subtitles. This comment IS the contract the //! iPhone and Android voice work builds against: every shape here //! is final; a field is only ever ADDED. JSON in and out, like `shell.*`. //! //! ## Offsets //! //! Every `from` / `to` is a UTF-16 code-unit offset (NSString / Kotlin //! `String` indices) into THE SESSION'S TEXT: everything this session has //! written, `settled + tentative`, starting at 0 where the dictation began. //! The platform maps it onto its field (it knows where the session's text //! starts). Ranges are half-open, `from <= to`. //! //! ## `voice.plan { surface, table?, column?, record?, database?, mode, platform? }` //! //! Before the microphone opens. `surface` names the input the person is in //! (`"note"`, `"dialogue"`, `"message"`, `"search"`, …: free text, used for //! ranking); `table` / `column` / `record` the record being written, when //! there is one; `mode` is `"dictate"` (default) or `"command"`; `platform` //! (`ios`, `android`, `mac`) only when the core cannot tell (tests). //! //! ```json //! { "session": "v-…", // pass back on every call below //! "mode": "dictate", //! "languages": ["en", "te"], // the person's, ordered, 1..3 //! "scripts": { "te": "latn" }, // each language's script (ISO 15924, lowercase) //! "live": { "engine": "apple.speech" | "android.speech" | "sherpa:", "model": ""? }, //! "second": { "engine": "sherpa:", "model": "" } | null, //! "vocabulary": ["Ravi", "Hyderabad", …], // bounded (100), ranked: hand to the engines as hotwords / contextual strings //! "downloads": [{ "model", "file", "bytes", "url", "sha256", "why" }], // ONE ENTRY PER FILE this phone lacks for the chosen engines; only files with a url and a sha256; `why` is a kit line //! "mobile_data": "never" | "ask" | "always", // the person's rule for those downloads off Wi-Fi //! "lines": { "idle", "listening", "settling", "stop", "command_hint", "not_heard", //! "no_permission", "open_settings", "waiting_wifi", "failed", //! "download"? (the prompt, with the size), "cotton_voice"? (Android) } //! } //! ``` //! //! `live` is always answered: on an iPhone `apple.speech` where Apple hears //! the language well, else the catalogue's `sherpa:` model (a model not yet //! held: `downloads` says what to fetch, and the platform stands in until it //! is). `second` is null when the person chose live-only, in command mode, or //! when the catalogue has none for the language. The plan is for the first //! of the person's languages; every rule applies all of them. //! //! ## `voice.heard { session, pass, final, text, words?, language? }` //! //! Every engine result, as it comes. `pass` is `"live"` or `"second"`. //! A live partial (`final: false`) is the engine's whole current hypothesis //! for the utterance in progress (cumulative, not a delta); a live //! `final: true` ends the utterance. A second pass is one utterance re-read //! (`final` is true); it is matched to its utterance by `words` timing when //! both passes gave timings, else in order (the n-th second pass is the n-th //! utterance). `words: [{ w, start_ms, end_ms, conf? }]` and `from_ms` / //! `to_ms` (the audio the result covers), times from the session's start. //! `language` is a BCP 47 code when the engine knows it. //! //! ```json //! { "settled": "Ravi said hello. ", // the session's settled text //! "tentative": "and then he", // drawn lighter //! "later": "…"?, // rare: settled text after the tentative words (a second pass ran ahead //! // of the live engine); text = settled + tentative + later //! "edits": [{ "from": 17, "to": 17, "text": "and then he" }], //! "command": { … }?, // command mode only, see below //! "wait_ms": 1200? // a second pass is due: keep the "settling" state at most this long //! } //! ``` //! //! `edits` turn the text as it stood after the previous answer into the text //! now. Each edit's `from` / `to` are offsets in the text BEFORE this answer; //! edits never overlap and come sorted by `from`, LAST FIRST, so applying them //! in the order given never shifts one still to apply. An answer with no //! change has `edits: []`. //! //! ## `voice.touched { session, from, to, text? }` //! //! The person edited the session's text: `[from, to)` in the text as the last //! answer left it now holds `text` (absent: unchanged, the range is only //! marked as the person's — a selection they accepted). That range is the //! person's from now on: no pass ever changes it again. Answers like //! `voice.heard` (with the person's own change already applied, so `edits` //! carries only what the core changed in consequence, usually nothing). //! //! ## `voice.end { session }` //! //! The microphone closed. Whatever is tentative settles as heard; a second //! pass still due is no longer waited for. //! //! ```json //! { "text": "…the whole session's text…", "edits": [ … ], "words": 12, "length": 64, "command": { … }? } //! ``` //! //! ## Command mode //! //! With `mode: "command"`, the settled words go to the universal search the //! ink already uses (`read.find`). A final answer carries //! `command: { "kind": "open", "query", "table", "id", "title", "path"?, //! "database"?, "says" }` when a record resolves (its title is the words, or //! the only hit holding every word, or the only hit), or `{ "kind": "search", //! "query", "says" }` (the shell opens Find with the words) when none does. //! `says` is the line the control shows. The lead words a command starts //! with ("open", "show me", per language) are not searched for. Spoken //! punctuation is not applied to a command. //! //! ## `voice.models {}` //! //! The catalogue (`kits/shell/voice/models.json`) joined //! with what this phone holds: //! `{ "models": [{ "id", "family", "kind", "engine", "languages", "role", //! "bytes", "licence", "files", "fetchable", "state", "path"?, "bytes_done"?, //! "upper_case"?, "measured"?, "options"?, "stream_options"?, //! "language_codes"?, "longest_s"?, "level_input"? }], "languages": { //! "": { "live", "second" } }, "vad", "lid", "held_bytes", "status" }`. //! `state` is `absent`, `downloading`, `ready` or `failed`; `fetchable` is //! false while a file has no url or sha256. `kind` says how sherpa-onnx is //! configured and `files` which file plays which part (by `name`); `options` //! are passed to that configuration as they are (`model_type: //! "nemo_transducer"` for a NeMo transducer). The platform reads all of this //! and decides nothing from a model's id, family or file names. //! `level_input: { below_peak, to_peak, max_gain }` is on a model that does //! not level its own input: while the running peak of the microphone stays //! under `below_peak`, the platform raises it so the peak reads `to_peak` //! (by at most `max_gain`) before that model hears it. `vad` and `lid` name //! the voice-activity and language-identification models. //! //! ## `voice.model { model, state, path?, bytes_done? }` //! //! The platform records a change in what it holds (`path` when `ready`); //! answers that model's row as `voice.models` would. //! //! ## `voice.settings { set? }` //! //! Get (no `set`) or change (`set` holds only the fields to change) the //! person's voice settings, one synced row shared by every phone: //! //! ```json //! { "languages": [{ "code": "te", "script": "telu" | "latn", "name", "native_script" }, …], // 1..3, ordered //! "passes": "two" | "live", //! "mobile_data": "never" | "ask" | "always", // model downloads off Wi-Fi //! "known": [{ "code", "name", "native_script" }], // the languages with spoken punctuation //! "lines": { … } // the settings screen's words //! } //! ``` //! //! ## What a platform owns //! //! The microphone and its permission, running engines and feeding //! `voice.heard`, downloading files into the one shared model store, the mic //! control (idle, listening, settling; tentative words lighter), and never //! keeping audio after a dictation ends. Every word it shows comes from //! `lines`. use serde_json::Value; use crate::handle::CottonHandle; use crate::methods::MethodError; /// Every method this module answers. pub const METHODS: [&str; 7] = [ "voice.end", "voice.heard", "voice.model", "voice.models", "voice.plan", "voice.settings", "voice.touched", ]; pub fn call(handle: &CottonHandle, method: &str, request: &Value) -> Result { use super::voice_methods as body; match method { "voice.plan" => body::plan(handle, request), "voice.heard" => body::heard(handle, request), "voice.touched" => body::touched(handle, request), "voice.end" => body::end(handle, request), "voice.models" => body::models(handle), "voice.model" => body::model(handle, request), "voice.settings" => body::settings(handle, request), other => Err(MethodError::new("unknown_method", format!("no voice method {other}"))), } }