|
| 1 | +package ai.openclaw.app |
| 2 | + |
| 3 | +import ai.openclaw.app.node.asObjectOrNull |
| 4 | +import ai.openclaw.app.node.asStringOrNull |
| 5 | +import kotlinx.serialization.json.JsonArray |
| 6 | +import kotlinx.serialization.json.JsonElement |
| 7 | +import kotlinx.serialization.json.JsonObject |
| 8 | +import kotlinx.serialization.json.JsonPrimitive |
| 9 | +import kotlinx.serialization.json.booleanOrNull |
| 10 | + |
| 11 | +data class GatewayTalkSetupReadiness( |
| 12 | + val realtimeTalk: GatewayTalkSetupState, |
| 13 | + val dictation: GatewayTalkSetupState, |
| 14 | +) { |
| 15 | + companion object { |
| 16 | + fun unverified( |
| 17 | + issue: GatewayTalkSetupIssue = GatewayTalkSetupIssue.CatalogNotLoaded, |
| 18 | + ): GatewayTalkSetupReadiness = |
| 19 | + GatewayTalkSetupReadiness( |
| 20 | + realtimeTalk = GatewayTalkSetupState.Unverified(issue), |
| 21 | + dictation = GatewayTalkSetupState.Unverified(issue), |
| 22 | + ) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +sealed interface GatewayTalkSetupState { |
| 27 | + data class Ready( |
| 28 | + val provider: GatewayTalkProvider, |
| 29 | + ) : GatewayTalkSetupState |
| 30 | + |
| 31 | + data class NeedsSetup( |
| 32 | + val issue: GatewayTalkSetupIssue, |
| 33 | + val provider: GatewayTalkProvider? = null, |
| 34 | + ) : GatewayTalkSetupState |
| 35 | + |
| 36 | + /** Catalog failures must not disable a startup path that the Gateway still validates. */ |
| 37 | + data class Unverified( |
| 38 | + val issue: GatewayTalkSetupIssue, |
| 39 | + ) : GatewayTalkSetupState |
| 40 | +} |
| 41 | + |
| 42 | +enum class GatewayTalkSetupTarget( |
| 43 | + val title: String, |
| 44 | +) { |
| 45 | + REALTIME_TALK("Realtime Talk"), |
| 46 | + DICTATION("Dictation"), |
| 47 | +} |
| 48 | + |
| 49 | +sealed interface GatewayTalkSetupIssue { |
| 50 | + data object CatalogNotLoaded : GatewayTalkSetupIssue |
| 51 | + |
| 52 | + data object CatalogLoadFailed : GatewayTalkSetupIssue |
| 53 | + |
| 54 | + data class GroupMissing( |
| 55 | + val target: GatewayTalkSetupTarget, |
| 56 | + ) : GatewayTalkSetupIssue |
| 57 | + |
| 58 | + data class NoProvider( |
| 59 | + val target: GatewayTalkSetupTarget, |
| 60 | + ) : GatewayTalkSetupIssue |
| 61 | + |
| 62 | + data class UnknownProvider( |
| 63 | + val target: GatewayTalkSetupTarget, |
| 64 | + val providerId: String, |
| 65 | + ) : GatewayTalkSetupIssue |
| 66 | + |
| 67 | + data class MissingReadiness( |
| 68 | + val target: GatewayTalkSetupTarget, |
| 69 | + ) : GatewayTalkSetupIssue |
| 70 | + |
| 71 | + data class ConfigureProvider( |
| 72 | + val target: GatewayTalkSetupTarget, |
| 73 | + ) : GatewayTalkSetupIssue |
| 74 | + |
| 75 | + data class MissingActiveProvider( |
| 76 | + val target: GatewayTalkSetupTarget, |
| 77 | + ) : GatewayTalkSetupIssue |
| 78 | + |
| 79 | + data class UnsupportedProvider( |
| 80 | + val target: GatewayTalkSetupTarget, |
| 81 | + ) : GatewayTalkSetupIssue |
| 82 | + |
| 83 | + data class ConfigureSelectedProvider( |
| 84 | + val providerLabel: String, |
| 85 | + ) : GatewayTalkSetupIssue |
| 86 | +} |
| 87 | + |
| 88 | +data class GatewayTalkProvider( |
| 89 | + val id: String, |
| 90 | + val label: String, |
| 91 | +) |
| 92 | + |
| 93 | +val GatewayTalkSetupState.isReady: Boolean |
| 94 | + get() = this is GatewayTalkSetupState.Ready |
| 95 | + |
| 96 | +val GatewayTalkSetupState.requiresSetup: Boolean |
| 97 | + get() = this is GatewayTalkSetupState.NeedsSetup |
| 98 | + |
| 99 | +fun gatewayTalkSetupStatusText(state: GatewayTalkSetupState): String = |
| 100 | + when (state) { |
| 101 | + is GatewayTalkSetupState.Ready -> "Ready" |
| 102 | + is GatewayTalkSetupState.NeedsSetup -> "Needs setup" |
| 103 | + is GatewayTalkSetupState.Unverified -> "Unverified" |
| 104 | + } |
| 105 | + |
| 106 | +fun gatewayTalkSetupDescription(state: GatewayTalkSetupState): String = |
| 107 | + when (state) { |
| 108 | + is GatewayTalkSetupState.Ready -> "${state.provider.label} via Gateway relay" |
| 109 | + is GatewayTalkSetupState.NeedsSetup -> gatewayTalkSetupIssueDescription(state.issue) |
| 110 | + is GatewayTalkSetupState.Unverified -> gatewayTalkSetupIssueDescription(state.issue) |
| 111 | + } |
| 112 | + |
| 113 | +private fun gatewayTalkSetupIssueDescription(issue: GatewayTalkSetupIssue): String = |
| 114 | + when (issue) { |
| 115 | + GatewayTalkSetupIssue.CatalogNotLoaded -> "Gateway talk catalog not loaded" |
| 116 | + GatewayTalkSetupIssue.CatalogLoadFailed -> "Could not load Gateway talk catalog" |
| 117 | + is GatewayTalkSetupIssue.GroupMissing -> "Gateway did not return ${issue.target.title} setup" |
| 118 | + is GatewayTalkSetupIssue.NoProvider -> "No ${issue.target.title} provider is configured on the Gateway" |
| 119 | + is GatewayTalkSetupIssue.UnknownProvider -> "Gateway selected unknown provider ${issue.providerId}" |
| 120 | + is GatewayTalkSetupIssue.MissingReadiness -> "Gateway did not return ${issue.target.title} readiness" |
| 121 | + is GatewayTalkSetupIssue.ConfigureProvider -> "Configure a ${issue.target.title} provider on the Gateway" |
| 122 | + is GatewayTalkSetupIssue.MissingActiveProvider -> |
| 123 | + "Gateway did not identify the active ${issue.target.title} provider" |
| 124 | + is GatewayTalkSetupIssue.UnsupportedProvider -> |
| 125 | + "Choose a supported ${issue.target.title} provider on the Gateway" |
| 126 | + is GatewayTalkSetupIssue.ConfigureSelectedProvider -> "Configure ${issue.providerLabel} on the Gateway" |
| 127 | + } |
| 128 | + |
| 129 | +internal fun parseGatewayTalkSetupReadiness(catalog: JsonObject?): GatewayTalkSetupReadiness { |
| 130 | + if (catalog == null) return GatewayTalkSetupReadiness.unverified() |
| 131 | + return GatewayTalkSetupReadiness( |
| 132 | + realtimeTalk = |
| 133 | + parseTalkCatalogGroup(catalog = catalog, key = "realtime", target = GatewayTalkSetupTarget.REALTIME_TALK), |
| 134 | + dictation = |
| 135 | + parseTalkCatalogGroup(catalog = catalog, key = "transcription", target = GatewayTalkSetupTarget.DICTATION), |
| 136 | + ) |
| 137 | +} |
| 138 | + |
| 139 | +private fun parseTalkCatalogGroup( |
| 140 | + catalog: JsonObject, |
| 141 | + key: String, |
| 142 | + target: GatewayTalkSetupTarget, |
| 143 | +): GatewayTalkSetupState { |
| 144 | + val group = |
| 145 | + catalog[key].asObjectOrNull() |
| 146 | + ?: return GatewayTalkSetupState.Unverified(GatewayTalkSetupIssue.GroupMissing(target)) |
| 147 | + val providers = |
| 148 | + (group["providers"] as? JsonArray) |
| 149 | + ?.mapNotNull(::parseTalkCatalogProvider) |
| 150 | + .orEmpty() |
| 151 | + val ready = (group["ready"] as? JsonPrimitive)?.booleanOrNull |
| 152 | + val activeProviderId = group["activeProvider"].asStringOrNull()?.trim()?.takeIf(String::isNotEmpty) |
| 153 | + if (providers.isEmpty()) { |
| 154 | + return when { |
| 155 | + ready == false -> GatewayTalkSetupState.NeedsSetup(GatewayTalkSetupIssue.NoProvider(target)) |
| 156 | + activeProviderId != null -> |
| 157 | + GatewayTalkSetupState.Unverified(GatewayTalkSetupIssue.UnknownProvider(target, activeProviderId)) |
| 158 | + else -> GatewayTalkSetupState.Unverified(GatewayTalkSetupIssue.MissingReadiness(target)) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if (activeProviderId == null) { |
| 163 | + if (ready == false) { |
| 164 | + return GatewayTalkSetupState.NeedsSetup(GatewayTalkSetupIssue.ConfigureProvider(target)) |
| 165 | + } |
| 166 | + // Older Gateways can omit the selected provider and report alias-backed rows as unconfigured |
| 167 | + // even though session startup resolves them. Only an explicit readiness result is authoritative. |
| 168 | + return GatewayTalkSetupState.Unverified(GatewayTalkSetupIssue.MissingActiveProvider(target)) |
| 169 | + } |
| 170 | + val selected = |
| 171 | + // Match Gateway registry precedence: canonical ids win before alias fallback. |
| 172 | + providers.firstOrNull { it.matchesId(activeProviderId) } |
| 173 | + ?: providers.firstOrNull { it.matchesAlias(activeProviderId) } |
| 174 | + ?: return if (ready == false) { |
| 175 | + GatewayTalkSetupState.NeedsSetup(GatewayTalkSetupIssue.UnsupportedProvider(target)) |
| 176 | + } else { |
| 177 | + GatewayTalkSetupState.Unverified(GatewayTalkSetupIssue.UnknownProvider(target, activeProviderId)) |
| 178 | + } |
| 179 | + val provider = GatewayTalkProvider(id = selected.id, label = selected.label) |
| 180 | + return when (ready) { |
| 181 | + true -> GatewayTalkSetupState.Ready(provider) |
| 182 | + false -> |
| 183 | + GatewayTalkSetupState.NeedsSetup( |
| 184 | + issue = GatewayTalkSetupIssue.ConfigureSelectedProvider(selected.label), |
| 185 | + provider = provider, |
| 186 | + ) |
| 187 | + null -> GatewayTalkSetupState.Unverified(GatewayTalkSetupIssue.MissingReadiness(target)) |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +private data class TalkCatalogProvider( |
| 192 | + val id: String, |
| 193 | + val label: String, |
| 194 | + val configured: Boolean, |
| 195 | + val aliases: List<String>, |
| 196 | +) { |
| 197 | + fun matchesId(candidate: String): Boolean = id.equals(candidate, ignoreCase = true) |
| 198 | + |
| 199 | + fun matchesAlias(candidate: String): Boolean = aliases.any { it.equals(candidate, ignoreCase = true) } |
| 200 | +} |
| 201 | + |
| 202 | +private fun parseTalkCatalogProvider(item: JsonElement): TalkCatalogProvider? { |
| 203 | + val value = item.asObjectOrNull() ?: return null |
| 204 | + val id = value["id"].asStringOrNull()?.trim()?.takeIf(String::isNotEmpty) ?: return null |
| 205 | + val label = value["label"].asStringOrNull()?.trim()?.takeIf(String::isNotEmpty) ?: id |
| 206 | + val aliases = |
| 207 | + (value["aliases"] as? JsonArray) |
| 208 | + ?.mapNotNull { it.asStringOrNull()?.trim()?.takeIf(String::isNotEmpty) } |
| 209 | + .orEmpty() |
| 210 | + return TalkCatalogProvider( |
| 211 | + id = id, |
| 212 | + label = label, |
| 213 | + configured = (value["configured"] as? JsonPrimitive)?.booleanOrNull == true, |
| 214 | + aliases = aliases, |
| 215 | + ) |
| 216 | +} |
0 commit comments