aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-adapters / src / router.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use reqwest::Client; |
| 17 | |
| 18 | use crate::models::{AdapterSpec, ProviderType}; |
| 19 | use crate::vault::{is_secret_present, resolve_secret}; |
| 20 | |
| 21 | pub struct AdapterRouter { |
| 22 | pub client: Client, |
| 23 | pub catalog: Vec<AdapterSpec>, |
| 24 | } |
| 25 | |
| 26 | impl Default for AdapterRouter { |
| 27 | fn default() -> Self { |
| 28 | Self::new() |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl AdapterRouter { |
| 33 | pub fn new() -> Self { |
| 34 | Self { |
| 35 | client: Client::builder().build().unwrap_or_default(), |
| 36 | catalog: Self::default_catalog(), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn default_catalog() -> Vec<AdapterSpec> { |
| 41 | vec![ |
| 42 | // 1. Local Resident Seats |
| 43 | AdapterSpec { |
| 44 | id: "atlas-lightning-omni".to_string(), |
| 45 | name: "Atlas Nemotron-3.5 30B (Resident)".to_string(), |
| 46 | provider: ProviderType::LocalSeat, |
| 47 | endpoint: ProviderType::LocalSeat.default_endpoint().to_string(), |
| 48 | model_id: "atlas-lightning-omni".to_string(), |
| 49 | context_length: 32768, |
| 50 | is_available: true, |
| 51 | requires_key: false, |
| 52 | is_resident: true, |
| 53 | }, |
| 54 | // 2. OpenAI |
| 55 | AdapterSpec { |
| 56 | id: "openai/gpt-4o".to_string(), |
| 57 | name: "OpenAI GPT-4o".to_string(), |
| 58 | provider: ProviderType::OpenAI, |
| 59 | endpoint: ProviderType::OpenAI.default_endpoint().to_string(), |
| 60 | model_id: "gpt-4o".to_string(), |
| 61 | context_length: 128000, |
| 62 | is_available: is_secret_present("OPENAI_API_KEY"), |
| 63 | requires_key: true, |
| 64 | is_resident: false, |
| 65 | }, |
| 66 | AdapterSpec { |
| 67 | id: "openai/o3-mini".to_string(), |
| 68 | name: "OpenAI o3-mini (Reasoning)".to_string(), |
| 69 | provider: ProviderType::OpenAI, |
| 70 | endpoint: ProviderType::OpenAI.default_endpoint().to_string(), |
| 71 | model_id: "o3-mini".to_string(), |
| 72 | context_length: 200000, |
| 73 | is_available: is_secret_present("OPENAI_API_KEY"), |
| 74 | requires_key: true, |
| 75 | is_resident: false, |
| 76 | }, |
| 77 | // 3. Anthropic |
| 78 | AdapterSpec { |
| 79 | id: "anthropic/claude-3-7-sonnet".to_string(), |
| 80 | name: "Claude 3.7 Sonnet (Hybrid Thinking)".to_string(), |
| 81 | provider: ProviderType::Anthropic, |
| 82 | endpoint: ProviderType::Anthropic.default_endpoint().to_string(), |
| 83 | model_id: "claude-3-7-sonnet-20250219".to_string(), |
| 84 | context_length: 200000, |
| 85 | is_available: is_secret_present("ANTHROPIC_API_KEY"), |
| 86 | requires_key: true, |
| 87 | is_resident: false, |
| 88 | }, |
| 89 | // 4. OpenRouter Gateway |
| 90 | AdapterSpec { |
| 91 | id: "openrouter/auto".to_string(), |
| 92 | name: "OpenRouter Auto Gateway".to_string(), |
| 93 | provider: ProviderType::OpenRouter, |
| 94 | endpoint: ProviderType::OpenRouter.default_endpoint().to_string(), |
| 95 | model_id: "openrouter/auto".to_string(), |
| 96 | context_length: 128000, |
| 97 | is_available: is_secret_present("OPENROUTER_API_KEY"), |
| 98 | requires_key: true, |
| 99 | is_resident: false, |
| 100 | }, |
| 101 | // 5. Google Gemini |
| 102 | AdapterSpec { |
| 103 | id: "gemini/gemini-2.5-flash".to_string(), |
| 104 | name: "Google Gemini 2.5 Flash".to_string(), |
| 105 | provider: ProviderType::Gemini, |
| 106 | endpoint: ProviderType::Gemini.default_endpoint().to_string(), |
| 107 | model_id: "gemini-2.5-flash".to_string(), |
| 108 | context_length: 1000000, |
| 109 | is_available: is_secret_present("GEMINI_API_KEY"), |
| 110 | requires_key: true, |
| 111 | is_resident: false, |
| 112 | }, |
| 113 | // 6. Groq Fast LPU |
| 114 | AdapterSpec { |
| 115 | id: "groq/llama-3.3-70b-versatile".to_string(), |
| 116 | name: "Groq Llama 3.3 70B (Sub-100ms LPU)".to_string(), |
| 117 | provider: ProviderType::Groq, |
| 118 | endpoint: ProviderType::Groq.default_endpoint().to_string(), |
| 119 | model_id: "llama-3.3-70b-versatile".to_string(), |
| 120 | context_length: 128000, |
| 121 | is_available: is_secret_present("GROQ_API_KEY"), |
| 122 | requires_key: true, |
| 123 | is_resident: false, |
| 124 | }, |
| 125 | // 7. Ollama Local |
| 126 | AdapterSpec { |
| 127 | id: "ollama/qwen2.5-coder".to_string(), |
| 128 | name: "Ollama Local (Qwen 2.5 Coder)".to_string(), |
| 129 | provider: ProviderType::Ollama, |
| 130 | endpoint: ProviderType::Ollama.default_endpoint().to_string(), |
| 131 | model_id: "qwen2.5-coder:7b".to_string(), |
| 132 | context_length: 32768, |
| 133 | is_available: true, |
| 134 | requires_key: false, |
| 135 | is_resident: false, |
| 136 | }, |
| 137 | // 8. DeepSeek Direct |
| 138 | AdapterSpec { |
| 139 | id: "deepseek/deepseek-chat".to_string(), |
| 140 | name: "DeepSeek V3".to_string(), |
| 141 | provider: ProviderType::DeepSeek, |
| 142 | endpoint: ProviderType::DeepSeek.default_endpoint().to_string(), |
| 143 | model_id: "deepseek-chat".to_string(), |
| 144 | context_length: 64000, |
| 145 | is_available: is_secret_present("DEEPSEEK_API_KEY"), |
| 146 | requires_key: true, |
| 147 | is_resident: false, |
| 148 | }, |
| 149 | // 9. Consumer Web Sessions |
| 150 | AdapterSpec { |
| 151 | id: "chatgpt/web-plus".to_string(), |
| 152 | name: "ChatGPT Plus (Web Session)".to_string(), |
| 153 | provider: ProviderType::ChatGPTWeb, |
| 154 | endpoint: ProviderType::ChatGPTWeb.default_endpoint().to_string(), |
| 155 | model_id: "gpt-4o-web".to_string(), |
| 156 | context_length: 128000, |
| 157 | is_available: is_secret_present("CHATGPT_SESSION_TOKEN") |
| 158 | || is_secret_present("OPENAI_WEB_TOKEN"), |
| 159 | requires_key: true, |
| 160 | is_resident: false, |
| 161 | }, |
| 162 | AdapterSpec { |
| 163 | id: "claude/web-pro".to_string(), |
| 164 | name: "Claude Pro (Web Session)".to_string(), |
| 165 | provider: ProviderType::ClaudeWeb, |
| 166 | endpoint: ProviderType::ClaudeWeb.default_endpoint().to_string(), |
| 167 | model_id: "claude-3-7-sonnet-web".to_string(), |
| 168 | context_length: 200000, |
| 169 | is_available: is_secret_present("CLAUDE_SESSION_KEY") |
| 170 | || is_secret_present("CLAUDE_WEB_SESSION_KEY"), |
| 171 | requires_key: true, |
| 172 | is_resident: false, |
| 173 | }, |
| 174 | ] |
| 175 | } |
| 176 | |
| 177 | pub fn discover_adapters(&self) -> Vec<AdapterSpec> { |
| 178 | let mut list = self.catalog.clone(); |
| 179 | for spec in &mut list { |
| 180 | let aliases = spec.provider.key_aliases(); |
| 181 | if !aliases.is_empty() { |
| 182 | spec.is_available = aliases.iter().any(|k| is_secret_present(k)); |
| 183 | } |
| 184 | } |
| 185 | list |
| 186 | } |
| 187 | |
| 188 | pub fn resolve_route( |
| 189 | &self, |
| 190 | model_query: &str, |
| 191 | ) -> (ProviderType, String, String, Option<String>) { |
| 192 | let trimmed = model_query.trim(); |
| 193 | |
| 194 | // 1. Direct match in catalog |
| 195 | for spec in &self.catalog { |
| 196 | if spec.id == trimmed || spec.model_id == trimmed { |
| 197 | let key = spec |
| 198 | .provider |
| 199 | .key_aliases() |
| 200 | .iter() |
| 201 | .find_map(|&k| resolve_secret(k)); |
| 202 | return ( |
| 203 | spec.provider, |
| 204 | spec.endpoint.clone(), |
| 205 | spec.model_id.clone(), |
| 206 | key, |
| 207 | ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // 2. Prefix based matching |
| 212 | if let Some(rest) = trimmed.strip_prefix("openai/") { |
| 213 | let key = resolve_secret("OPENAI_API_KEY"); |
| 214 | return ( |
| 215 | ProviderType::OpenAI, |
| 216 | ProviderType::OpenAI.default_endpoint().to_string(), |
| 217 | rest.to_string(), |
| 218 | key, |
| 219 | ); |
| 220 | } |
| 221 | if let Some(rest) = trimmed.strip_prefix("anthropic/") { |
| 222 | let key = resolve_secret("ANTHROPIC_API_KEY"); |
| 223 | return ( |
| 224 | ProviderType::Anthropic, |
| 225 | ProviderType::Anthropic.default_endpoint().to_string(), |
| 226 | rest.to_string(), |
| 227 | key, |
| 228 | ); |
| 229 | } |
| 230 | if let Some(rest) = trimmed.strip_prefix("openrouter/") { |
| 231 | let key = resolve_secret("OPENROUTER_API_KEY"); |
| 232 | return ( |
| 233 | ProviderType::OpenRouter, |
| 234 | ProviderType::OpenRouter.default_endpoint().to_string(), |
| 235 | rest.to_string(), |
| 236 | key, |
| 237 | ); |
| 238 | } |
| 239 | if let Some(rest) = trimmed.strip_prefix("gemini/") { |
| 240 | let key = resolve_secret("GEMINI_API_KEY"); |
| 241 | return ( |
| 242 | ProviderType::Gemini, |
| 243 | ProviderType::Gemini.default_endpoint().to_string(), |
| 244 | rest.to_string(), |
| 245 | key, |
| 246 | ); |
| 247 | } |
| 248 | if let Some(rest) = trimmed.strip_prefix("groq/") { |
| 249 | let key = resolve_secret("GROQ_API_KEY"); |
| 250 | return ( |
| 251 | ProviderType::Groq, |
| 252 | ProviderType::Groq.default_endpoint().to_string(), |
| 253 | rest.to_string(), |
| 254 | key, |
| 255 | ); |
| 256 | } |
| 257 | if let Some(rest) = trimmed.strip_prefix("ollama/") { |
| 258 | return ( |
| 259 | ProviderType::Ollama, |
| 260 | ProviderType::Ollama.default_endpoint().to_string(), |
| 261 | rest.to_string(), |
| 262 | None, |
| 263 | ); |
| 264 | } |
| 265 | if let Some(rest) = trimmed.strip_prefix("deepseek/") { |
| 266 | let key = resolve_secret("DEEPSEEK_API_KEY"); |
| 267 | return ( |
| 268 | ProviderType::DeepSeek, |
| 269 | ProviderType::DeepSeek.default_endpoint().to_string(), |
| 270 | rest.to_string(), |
| 271 | key, |
| 272 | ); |
| 273 | } |
| 274 | if let Some(rest) = trimmed.strip_prefix("chatgpt/") { |
| 275 | let key = resolve_secret("CHATGPT_SESSION_TOKEN") |
| 276 | .or_else(|| resolve_secret("OPENAI_WEB_TOKEN")); |
| 277 | return ( |
| 278 | ProviderType::ChatGPTWeb, |
| 279 | ProviderType::ChatGPTWeb.default_endpoint().to_string(), |
| 280 | rest.to_string(), |
| 281 | key, |
| 282 | ); |
| 283 | } |
| 284 | if let Some(rest) = trimmed.strip_prefix("claude-web/") { |
| 285 | let key = resolve_secret("CLAUDE_SESSION_KEY") |
| 286 | .or_else(|| resolve_secret("CLAUDE_WEB_SESSION_KEY")); |
| 287 | return ( |
| 288 | ProviderType::ClaudeWeb, |
| 289 | ProviderType::ClaudeWeb.default_endpoint().to_string(), |
| 290 | rest.to_string(), |
| 291 | key, |
| 292 | ); |
| 293 | } |
| 294 | |
| 295 | // Default fallback to the local model seat |
| 296 | ( |
| 297 | ProviderType::LocalSeat, |
| 298 | ProviderType::LocalSeat.default_endpoint().to_string(), |
| 299 | "atlas-lightning-omni".to_string(), |
| 300 | None, |
| 301 | ) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | #[cfg(test)] |
| 306 | mod tests { |
| 307 | use super::*; |
| 308 | |
| 309 | #[test] |
| 310 | fn test_web_subscription_aliases() { |
| 311 | let chatgpt = ProviderType::ChatGPTWeb; |
| 312 | assert_eq!( |
| 313 | chatgpt.key_aliases(), |
| 314 | &["CHATGPT_SESSION_TOKEN", "OPENAI_WEB_TOKEN"] |
| 315 | ); |
| 316 | |
| 317 | let claude = ProviderType::ClaudeWeb; |
| 318 | assert_eq!( |
| 319 | claude.key_aliases(), |
| 320 | &["CLAUDE_SESSION_KEY", "CLAUDE_WEB_SESSION_KEY"] |
| 321 | ); |
| 322 | } |
| 323 | } |