| 1 | //! OpenCode Go's provider-owned transport roster. |
| 2 | //! |
| 3 | //! Reviewed against <https://opencode.ai/docs/go/#endpoints> on 2026-09-12. |
| 4 | //! Previously accepted Chat ids remain compatible absent explicit deprecation. |
| 5 | //! Membership proves a wire protocol, not live availability, limits or pricing. |
| 6 | |
| 7 | /// Every documented Go wire id, in stable picker order. |
| 8 | #[must_use] |
| 9 | pub fn opencode_go_models() -> Vec<&'static str> { |
| 10 | crate::catalog::reviewed::constants::OPENCODE_GO_MODELS.to_vec() |
| 11 | } |
| 12 | |
| 13 | /// Canonicalize only IDs whose Go protocol is known. Unknown models cannot |
| 14 | /// silently fall through to Chat, another provider, or the default model. |
| 15 | #[must_use] |
| 16 | pub fn opencode_go_model_id(model: &str) -> Option<&'static str> { |
| 17 | let resolved = |
| 18 | crate::route::resolve_model(crate::ProviderKind::OpencodeGo, model, None).ok()?; |
| 19 | crate::catalog::reviewed::constants::OPENCODE_GO_MODELS |
| 20 | .iter() |
| 21 | .copied() |
| 22 | .find(|id| *id == resolved.wire_id) |
| 23 | } |
| 24 | |
| 25 | /// The documented endpoint for a Go model, independent of stale catalog metadata. |
| 26 | #[must_use] |
| 27 | pub fn opencode_go_endpoint_key(model: &str) -> Option<&'static str> { |
| 28 | let canonical = opencode_go_model_id(model)?; |
| 29 | crate::catalog::reviewed::reviewed_transport("opencode-go", canonical) |
| 30 | .map(|row| row.endpoint_key.as_str()) |
| 31 | } |
| 32 | |
| 33 | pub fn opencode_go_listing_base_url(base: &str) -> &str { |
| 34 | let official = url::Url::parse(base).is_ok_and(|url| { |
| 35 | url.scheme() == "https" |
| 36 | && url.host_str() == Some("opencode.ai") |
| 37 | && url.port_or_known_default() == Some(443) |
| 38 | && url.username().is_empty() |
| 39 | && url.password().is_none() |
| 40 | && url.query().is_none() |
| 41 | && url.fragment().is_none() |
| 42 | && matches!( |
| 43 | url.path().trim_end_matches('/'), |
| 44 | "/zen/go/v1" | "/inference/go/v1" |
| 45 | ) |
| 46 | }); |
| 47 | if official { |
| 48 | "https://opencode.ai/inference/go/v1" |
| 49 | } else { |
| 50 | base |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn opencode_go_dispatch_base_url(connection: &str, advertised: &str) -> Option<String> { |
| 55 | let listing = url::Url::parse(opencode_go_listing_base_url(connection)).ok()?; |
| 56 | let endpoint = url::Url::parse(advertised).ok()?; |
| 57 | let path = listing.path().trim_end_matches('/'); |
| 58 | let prefix = path.strip_suffix("/v1").unwrap_or(path); |
| 59 | if listing.origin() != endpoint.origin() |
| 60 | || !matches!(endpoint.scheme(), "http" | "https") |
| 61 | || !endpoint.username().is_empty() |
| 62 | || endpoint.password().is_some() |
| 63 | || endpoint.query().is_some() |
| 64 | || endpoint.fragment().is_some() |
| 65 | || advertised.contains(['%', '\\']) |
| 66 | || !(endpoint.path() == prefix |
| 67 | || endpoint |
| 68 | .path() |
| 69 | .strip_prefix(prefix) |
| 70 | .is_some_and(|path| path.starts_with('/'))) |
| 71 | { |
| 72 | return None; |
| 73 | } |
| 74 | Some(endpoint.as_str().trim_end_matches('/').to_owned()) |
| 75 | } |
| 76 |