package com.cottondb.voiceimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonArrayimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.buildJsonArrayimport kotlinx.serialization.json.buildJsonObjectimport kotlinx.serialization.json.contentOrNullimport kotlinx.serialization.json.put/** * What a calling app hands over to open a dictation: voice.plan's answer as the core gave it, plus `models`, the * catalogue entries (voice.models) of the models it names, so this runtime knows each one's kind and files. */classPlan(val session: String,val languages: List<String>,val live: Engine?,val second: Engine?,val models: Map<String, ModelEntry>,) {val language: Stringget() = languages.firstOrNull() ?: "en"/** The models a dictation runs, after stand-ins: [missing] are not held yet (their files are [downloads]). */classResolved(val live: ModelEntry, val second: ModelEntry?, val vad: ModelEntry?, val language: String, val missing: List<ModelEntry>) {val runnable: Booleanget() = live !in missing && (vad == null || vad !in missing)/** The levelling the microphone gets: the catalogue's, when a model this dictation runs does not level its own input. */val level: Levelling? get() = listOfNotNull(live, vad, second).firstNotNullOfOrNull { it.level }fun downloads(): JsonArray = buildJsonArray { missing.forEach { m -> m.files.forEach { f -> add(buildJsonObject { put("model", m.id); put("file", f.name); put("url", f.url); put("sha256", f.sha256); f.bytes?.let { put("bytes", it) } }) } } } }/** * The planned models, each one that cannot be fetched (no url or sha256 yet) and is not already held replaced by * its stand-in for the language (StandIns). A live engine that is not sherpa's is no engine of this runtime. */fun resolve(ready: (ModelEntry) -> Boolean): Resolved {val liveId = live?.sherpaModel ?: throwNoEngine("engine", live?.engine)fun usable(m: ModelEntry) = ready(m) || m.files.all { it.url != null && it.sha256 != null }fun pick(id: String?, standIn: ModelEntry?): ModelEntry? = id?.let { models[it] }?.takeIf(::usable) ?: standInval liveModel = pick(liveId, StandIns.live(language))!!val secondModel = second?.let { pick(it.sherpaModel, StandIns.second(language)) }?.takeIf { it.id != liveModel.id }val vadModel = if (liveModel.kind.startsWith("online-")) nullelse pick(models.keys.firstOrNull { models[it]?.kind == "vad-silero" }, StandIns.sileroVad)val missing = listOfNotNull(liveModel, secondModel, vadModel).filterNot(ready)returnResolved(liveModel, secondModel, vadModel, language, missing) }companionobject {/** The voice catalogue (kits/shell/voice/models.json, copied in at build time): every model's kind and files. */fun catalogue(json: String): Map<String, ModelEntry> {val catalogue = Json.parseToJsonElement(json) as? JsonObject ?: return emptyMap()val models = catalogue["models"] as? JsonObject ?: return emptyMap()val levelling = Levelling.of(catalogue["levelling"] as? JsonObject)return models.mapNotNull { (id, e) -> (e as? JsonObject)?.let { ModelEntry.of(id, it, levelling) }?.let { id to it } }.toMap() }/** voice.plan's answer; [catalogue] gives each named model its kind and files where the caller's rows do not. */fun parse(session: String, json: String, catalogue: Map<String, ModelEntry> = emptyMap()): Plan {val o = Json.parseToJsonElement(json) as? JsonObject ?: throwIllegalArgumentException("the plan is not a JSON object")val given = (o["models"] as? JsonObject)?.mapNotNull { (id, e) -> (e as? JsonObject)?.let { ModelEntry.of(id, it) }?.let { id to it } }?.toMap() ?: emptyMap()// The plan's downloads carry each file's url and sha256 once uploaded, newer than the catalogue this build carries.val fetched = (o["downloads"] as? JsonArray)?.let { ModelEntry.fromDownloads(it, catalogue + given) }?.associateBy { it.id } ?: emptyMap()val models = catalogue + given + fetched.filterValues { it.kind != "unknown" }.mapValues { (id, m) ->val known = (given[id] ?: catalogue[id])?.files ?: emptyList() m.copy(files = known.map { f -> m.files.firstOrNull { it.name == f.name } ?: f } + m.files.filter { f -> known.none { it.name == f.name } }) }returnPlan( session = session, languages = (o["languages"] as? JsonArray)?.mapNotNull { (it as? JsonPrimitive)?.contentOrNull } ?: emptyList(), live = Engine.of(o["live"]), second = Engine.of(o["second"]), models = models, ) } }}