spark-adapters
Hardware abstraction adapters for local AI accelerators
Brought by drakestapleton
spark-adapters / src / router.rs
| 1 | use reqwest::Client; |
| 2 | |
| 3 | use crate::models::{AdapterSpec, ProviderType}; |
| 4 | use crate::vault::{is_secret_present, resolve_secret}; |
| 5 | |
| 6 | pub struct AdapterRouter { |
| 7 | pub client: Client, |
| 8 | pub catalog: Vec<AdapterSpec>, |
| 9 | } |
| 10 | |
| 11 | impl AdapterRouter { |
| 12 | pub fn new() -> Self { |
| 13 | Self { |
| 14 | client: Client::builder().build().unwrap_or_default(), |
| 15 | catalog: Self::default_catalog(), |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | pub fn default_catalog() -> Vec<AdapterSpec> { |
| 20 | vec![ |
| 21 | // 1. Local Resident Seats |
| 22 | AdapterSpec { |
| 23 | id: "atlas-lightning-omni".to_string(), |
| 24 | name: "Atlas Nemotron-3.5 30B (Resident)".to_string(), |
| 25 | provider: ProviderType::LocalMax, |
| 26 | endpoint: ProviderType::LocalMax.default_endpoint().to_string(), |
| 27 | model_id: "atlas-lightning-omni".to_string(), |
| 28 | context_length: 32768, |
| 29 | is_available: true, |
| 30 | requires_key: false, |
| 31 | is_resident: true, |
| 32 | }, |
| 33 | // 2. OpenAI |
| 34 | AdapterSpec { |
| 35 | id: "openai/gpt-4o".to_string(), |
| 36 | name: "OpenAI GPT-4o".to_string(), |
| 37 | provider: ProviderType::OpenAI, |
| 38 | endpoint: ProviderType::OpenAI.default_endpoint().to_string(), |
| 39 | model_id: "gpt-4o".to_string(), |
| 40 | context_length: 128000, |
| 41 | is_available: is_secret_present("OPENAI_API_KEY"), |
| 42 | requires_key: true, |
| 43 | is_resident: false, |
| 44 | }, |
| 45 | AdapterSpec { |
| 46 | id: "openai/o3-mini".to_string(), |
| 47 | name: "OpenAI o3-mini (Reasoning)".to_string(), |
| 48 | provider: ProviderType::OpenAI, |
| 49 | endpoint: ProviderType::OpenAI.default_endpoint().to_string(), |
| 50 | model_id: "o3-mini".to_string(), |
| 51 | context_length: 200000, |
| 52 | is_available: is_secret_present("OPENAI_API_KEY"), |
| 53 | requires_key: true, |
| 54 | is_resident: false, |
| 55 | }, |
| 56 | // 3. Anthropic |
| 57 | AdapterSpec { |
| 58 | id: "anthropic/claude-3-7-sonnet".to_string(), |
| 59 | name: "Claude 3.7 Sonnet (Hybrid Thinking)".to_string(), |
| 60 | provider: ProviderType::Anthropic, |
| 61 | endpoint: ProviderType::Anthropic.default_endpoint().to_string(), |
| 62 | model_id: "claude-3-7-sonnet-20250219".to_string(), |
| 63 | context_length: 200000, |
| 64 | is_available: is_secret_present("ANTHROPIC_API_KEY"), |
| 65 | requires_key: true, |
| 66 | is_resident: false, |
| 67 | }, |
| 68 | // 4. OpenRouter Gateway |
| 69 | AdapterSpec { |
| 70 | id: "openrouter/auto".to_string(), |
| 71 | name: "OpenRouter Auto Gateway".to_string(), |
| 72 | provider: ProviderType::OpenRouter, |
| 73 | endpoint: ProviderType::OpenRouter.default_endpoint().to_string(), |
| 74 | model_id: "openrouter/auto".to_string(), |
| 75 | context_length: 128000, |
| 76 | is_available: is_secret_present("OPENROUTER_API_KEY"), |
| 77 | requires_key: true, |
| 78 | is_resident: false, |
| 79 | }, |
| 80 | // 5. Google Gemini |
| 81 | AdapterSpec { |
| 82 | id: "gemini/gemini-2.5-flash".to_string(), |
| 83 | name: "Google Gemini 2.5 Flash".to_string(), |
| 84 | provider: ProviderType::Gemini, |
| 85 | endpoint: ProviderType::Gemini.default_endpoint().to_string(), |
| 86 | model_id: "gemini-2.5-flash".to_string(), |
| 87 | context_length: 1000000, |
| 88 | is_available: is_secret_present("GEMINI_API_KEY"), |
| 89 | requires_key: true, |
| 90 | is_resident: false, |
| 91 | }, |
| 92 | // 6. Groq Fast LPU |
| 93 | AdapterSpec { |
| 94 | id: "groq/llama-3.3-70b-versatile".to_string(), |
| 95 | name: "Groq Llama 3.3 70B (Sub-100ms LPU)".to_string(), |
| 96 | provider: ProviderType::Groq, |
| 97 | endpoint: ProviderType::Groq.default_endpoint().to_string(), |
| 98 | model_id: "llama-3.3-70b-versatile".to_string(), |
| 99 | context_length: 128000, |
| 100 | is_available: is_secret_present("GROQ_API_KEY"), |
| 101 | requires_key: true, |
| 102 | is_resident: false, |
| 103 | }, |
| 104 | // 7. Ollama Local |
| 105 | AdapterSpec { |
| 106 | id: "ollama/qwen2.5-coder".to_string(), |
| 107 | name: "Ollama Local (Qwen 2.5 Coder)".to_string(), |
| 108 | provider: ProviderType::Ollama, |
| 109 | endpoint: ProviderType::Ollama.default_endpoint().to_string(), |
| 110 | model_id: "qwen2.5-coder:7b".to_string(), |
| 111 | context_length: 32768, |
| 112 | is_available: true, |
| 113 | requires_key: false, |
| 114 | is_resident: false, |
| 115 | }, |
| 116 | // 8. DeepSeek Direct |
| 117 | AdapterSpec { |
| 118 | id: "deepseek/deepseek-chat".to_string(), |
| 119 | name: "DeepSeek V3".to_string(), |
| 120 | provider: ProviderType::DeepSeek, |
| 121 | endpoint: ProviderType::DeepSeek.default_endpoint().to_string(), |
| 122 | model_id: "deepseek-chat".to_string(), |
| 123 | context_length: 64000, |
| 124 | is_available: is_secret_present("DEEPSEEK_API_KEY"), |
| 125 | requires_key: true, |
| 126 | is_resident: false, |
| 127 | }, |
| 128 | ] |
| 129 | } |
| 130 | |
| 131 | pub fn discover_adapters(&self) -> Vec<AdapterSpec> { |
| 132 | let mut list = self.catalog.clone(); |
| 133 | for spec in &mut list { |
| 134 | if let Some(key_name) = spec.provider.key_name() { |
| 135 | spec.is_available = is_secret_present(key_name); |
| 136 | } |
| 137 | } |
| 138 | list |
| 139 | } |
| 140 | |
| 141 | pub fn resolve_route(&self, model_query: &str) -> (ProviderType, String, String, Option<String>) { |
| 142 | let trimmed = model_query.trim(); |
| 143 | |
| 144 | // 1. Direct match in catalog |
| 145 | for spec in &self.catalog { |
| 146 | if spec.id == trimmed || spec.model_id == trimmed { |
| 147 | let key = spec.provider.key_name().and_then(resolve_secret); |
| 148 | return (spec.provider, spec.endpoint.clone(), spec.model_id.clone(), key); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // 2. Prefix based matching |
| 153 | if let Some(rest) = trimmed.strip_prefix("openai/") { |
| 154 | let key = resolve_secret("OPENAI_API_KEY"); |
| 155 | return (ProviderType::OpenAI, ProviderType::OpenAI.default_endpoint().to_string(), rest.to_string(), key); |
| 156 | } |
| 157 | if let Some(rest) = trimmed.strip_prefix("anthropic/") { |
| 158 | let key = resolve_secret("ANTHROPIC_API_KEY"); |
| 159 | return (ProviderType::Anthropic, ProviderType::Anthropic.default_endpoint().to_string(), rest.to_string(), key); |
| 160 | } |
| 161 | if let Some(rest) = trimmed.strip_prefix("openrouter/") { |
| 162 | let key = resolve_secret("OPENROUTER_API_KEY"); |
| 163 | return (ProviderType::OpenRouter, ProviderType::OpenRouter.default_endpoint().to_string(), rest.to_string(), key); |
| 164 | } |
| 165 | if let Some(rest) = trimmed.strip_prefix("gemini/") { |
| 166 | let key = resolve_secret("GEMINI_API_KEY"); |
| 167 | return (ProviderType::Gemini, ProviderType::Gemini.default_endpoint().to_string(), rest.to_string(), key); |
| 168 | } |
| 169 | if let Some(rest) = trimmed.strip_prefix("groq/") { |
| 170 | let key = resolve_secret("GROQ_API_KEY"); |
| 171 | return (ProviderType::Groq, ProviderType::Groq.default_endpoint().to_string(), rest.to_string(), key); |
| 172 | } |
| 173 | if let Some(rest) = trimmed.strip_prefix("ollama/") { |
| 174 | return (ProviderType::Ollama, ProviderType::Ollama.default_endpoint().to_string(), rest.to_string(), None); |
| 175 | } |
| 176 | if let Some(rest) = trimmed.strip_prefix("deepseek/") { |
| 177 | let key = resolve_secret("DEEPSEEK_API_KEY"); |
| 178 | return (ProviderType::DeepSeek, ProviderType::DeepSeek.default_endpoint().to_string(), rest.to_string(), key); |
| 179 | } |
| 180 | |
| 181 | // Default fallback to local MAX seat |
| 182 | ( |
| 183 | ProviderType::LocalMax, |
| 184 | ProviderType::LocalMax.default_endpoint().to_string(), |
| 185 | "atlas-lightning-omni".to_string(), |
| 186 | None, |
| 187 | ) |
| 188 | } |
| 189 | } |