|
| 1 | +//! OMP-style provider descriptors: how to talk to a host. |
| 2 | +//! |
| 3 | +//! Model ids are **not** compiled here. A descriptor names the wire, URL, env |
| 4 | +//! var, and whether authenticated `GET /v1/models` is the catalog authority |
| 5 | +//! for that host. Offerings come from the Codewhale catalog layers and live |
| 6 | +//! provider `/models` refreshes. |
| 7 | +
|
| 8 | +use std::sync::OnceLock; |
| 9 | + |
| 10 | +use serde::Deserialize; |
| 11 | + |
| 12 | +const DESCRIPTORS_JSON: &str = include_str!("../assets/provider_descriptors.json"); |
| 13 | + |
| 14 | +/// How this host's model list is discovered. |
| 15 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] |
| 16 | +#[serde(rename_all = "snake_case")] |
| 17 | +pub enum DescriptorDiscovery { |
| 18 | + /// Authenticated `GET {base_url}/models` is authoritative for this credential. |
| 19 | + ModelsEndpoint, |
| 20 | + /// No live discovery; only catalog/config rows. |
| 21 | + None, |
| 22 | +} |
| 23 | + |
| 24 | +/// Transport used to send turns. Not a brand enum. |
| 25 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] |
| 26 | +#[serde(rename_all = "kebab-case")] |
| 27 | +pub enum DescriptorWire { |
| 28 | + OpenaiCompatible, |
| 29 | + AnthropicMessages, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Debug, Deserialize)] |
| 33 | +struct DescriptorFile { |
| 34 | + descriptors: Vec<ProviderDescriptor>, |
| 35 | +} |
| 36 | + |
| 37 | +/// Data row describing a hosted OpenAI-compatible (or Anthropic Messages) gateway. |
| 38 | +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] |
| 39 | +pub struct ProviderDescriptor { |
| 40 | + pub id: String, |
| 41 | + pub label: String, |
| 42 | + pub wire: DescriptorWire, |
| 43 | + pub base_url: String, |
| 44 | + pub api_key_env: String, |
| 45 | + pub default_model: String, |
| 46 | + pub discovery: DescriptorDiscovery, |
| 47 | + #[serde(default)] |
| 48 | + pub docs_url: Option<String>, |
| 49 | + #[serde(default)] |
| 50 | + pub credential_url: Option<String>, |
| 51 | + #[serde(default)] |
| 52 | + pub guidance: Option<String>, |
| 53 | + #[serde(default)] |
| 54 | + pub aliases: Vec<String>, |
| 55 | +} |
| 56 | + |
| 57 | +impl ProviderDescriptor { |
| 58 | + #[must_use] |
| 59 | + pub fn matches(&self, needle: &str) -> bool { |
| 60 | + let needle = needle.trim().to_ascii_lowercase().replace('_', "-"); |
| 61 | + if needle.is_empty() { |
| 62 | + return false; |
| 63 | + } |
| 64 | + self.id == needle |
| 65 | + || self |
| 66 | + .aliases |
| 67 | + .iter() |
| 68 | + .any(|alias| alias.eq_ignore_ascii_case(&needle)) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +static DESCRIPTORS: OnceLock<Vec<ProviderDescriptor>> = OnceLock::new(); |
| 73 | + |
| 74 | +/// Bundled compatible-host descriptors. Panics only if the committed JSON is invalid. |
| 75 | +#[must_use] |
| 76 | +pub fn bundled_provider_descriptors() -> &'static [ProviderDescriptor] { |
| 77 | + DESCRIPTORS |
| 78 | + .get_or_init(|| { |
| 79 | + let file: DescriptorFile = serde_json::from_str(DESCRIPTORS_JSON) |
| 80 | + .expect("committed provider_descriptors.json must parse"); |
| 81 | + file.descriptors |
| 82 | + }) |
| 83 | + .as_slice() |
| 84 | +} |
| 85 | + |
| 86 | +#[must_use] |
| 87 | +pub fn provider_descriptor(id: &str) -> Option<&'static ProviderDescriptor> { |
| 88 | + bundled_provider_descriptors() |
| 89 | + .iter() |
| 90 | + .find(|descriptor| descriptor.matches(id)) |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod tests { |
| 95 | + use super::*; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn descriptors_parse_and_command_code_is_a_row_not_a_kind() { |
| 99 | + let rows = bundled_provider_descriptors(); |
| 100 | + assert!(rows.len() >= 5, "expected compatible hosts plus command-code"); |
| 101 | + for row in rows { |
| 102 | + assert!(row.base_url.starts_with("https://"), "{}", row.id); |
| 103 | + assert!(!row.api_key_env.is_empty(), "{}", row.id); |
| 104 | + assert!(!row.default_model.is_empty(), "{}", row.id); |
| 105 | + assert_eq!(row.discovery, DescriptorDiscovery::ModelsEndpoint); |
| 106 | + assert_eq!(row.wire, DescriptorWire::OpenaiCompatible); |
| 107 | + } |
| 108 | + let cmd = provider_descriptor("command-code").expect("command-code"); |
| 109 | + assert_eq!(cmd.base_url, "https://api.commandcode.ai/provider/v1"); |
| 110 | + assert_eq!(cmd.api_key_env, "COMMAND_CODE_API_KEY"); |
| 111 | + assert_eq!( |
| 112 | + provider_descriptor("cmd-code").map(|row| row.id.as_str()), |
| 113 | + Some("command-code") |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn descriptors_do_not_embed_model_rosters() { |
| 119 | + let raw = DESCRIPTORS_JSON; |
| 120 | + assert!( |
| 121 | + !raw.contains("moonshotai/Kimi-K2.7-Code"), |
| 122 | + "do not compile a Baseten/Kimi roster into descriptors" |
| 123 | + ); |
| 124 | + assert!( |
| 125 | + !raw.contains("openai/gpt-oss-120b"), |
| 126 | + "do not compile a Groq roster into descriptors" |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments