1 //! Inference provider configuration.
2 //!
3 //! Decides which backend serves inference. Resolution order, first match wins:
4 //!
5 //! 1. Override: `OPENAI_BASE_URL` + `OPENAI_API_KEY`, or the active profile in
6 //! `~/.config/sigit/providers.toml`.
7 //! 2. siGit Code Cloud: used when the user is logged in (`sigit login`). The
8 //! endpoint and tier are built in, and the session token is the credential.
9 //! 3. On-device: no login and no override, so inference runs locally.
10 //!
11 //! Provider resolution runs at startup in both modes: the interactive client
12 //! picks its whole backend from it, and `run_acp_server` applies the explicit
13 //! override (env or `providers.toml`) the same way. Some items are still wired
14 //! up only through `#[cfg(unix)]` interactive paths, so the dead-code lint
15 //! stays suppressed on non-Unix targets only — Unix builds keep full coverage.
16 #![cfg_attr(not(unix), allow(dead_code))]
17
18 use std::path::PathBuf;
19
20 use serde::Deserialize;
21
22 /// Default siGit Code Cloud inference endpoint. This is the siGit platform API
23 /// (sigit.si), which authenticates the user and forwards to the inference
24 /// backend; the client never talks to the backend directly. Override with
25 /// `SIGIT_CLOUD_URL` (dev: `http://localhost:8088/api/v1`).
26 const DEFAULT_CLOUD_URL: &str = "https://sigit.si/api/v1";
27
28 /// The cloud quality tiers, in display order (each line ordered smallest to
29 /// largest). Always offered in `/models`; selecting one requires a signed-in
30 /// account.
31 ///
32 /// The tiers group into several independent lines, and a tier is only routable
33 /// when the cloud has that line's family configured. Which family is which, and
34 /// what runs behind any of them, is deliberately not represented here: the
35 /// client knows tier names and nothing else.
36 ///
37 /// This list mirrors onde-cloud's router catalogue (`src/router.rs`) and
38 /// sigit-si's `CloudCatalog`. A tier the cloud serves but this list omits is
39 /// simply invisible in `/models` and the editor picker, so all three sides have
40 /// to be updated together when a tier is added.
41 pub const CLOUD_TIERS: &[&str] = &[
42 "fast", "balanced", "large", "mini", "air", "pro", "oke", "flux", "apex", "aura", "orbit",
43 "nova", "flare", "zenith", "prism",
44 ];
45
46 pub const DEFAULT_CLOUD_TIER: &str = "nova";
47
48 /// Cloud tiers that read images attached to a prompt.
49 ///
50 /// This mirrors onde-cloud's `IMAGE_CANDIDATES` table, which is where the
51 /// per-provider facts live and where a request is actually accepted or
52 /// refused. A tier is listed here when at least one of its providers takes
53 /// images. The rest are text-only at the source: `nova` and `orbit` (GLM-5.3
54 /// and GLM-5.2), `apex` and `flux` (DeepSeek V4 Pro and V4 Flash).
55 ///
56 /// The list can be hard-coded because the tier list itself is: a tier this
57 /// client does not know is not offered at all (see [`CLOUD_TIERS`]).
58 const IMAGE_TIERS: &[&str] = &[
59 "fast", "balanced", "large", "mini", "air", "pro", "oke", "aura", "flare", "zenith", "prism",
60 ];
61
62 /// Whether the cloud tier `tier` reads images.
63 pub fn tier_accepts_images(tier: &str) -> bool {
64 IMAGE_TIERS.contains(&tier.trim().to_lowercase().as_str())
65 }
66
67 /// Whether the model id sent on the wire reads images.
68 ///
69 /// An `onde-*` id is one of our tiers and is answered from [`IMAGE_TIERS`]. Any
70 /// other id belongs to an endpoint the user configured themselves
71 /// (`OPENAI_BASE_URL` or a provider profile). Nothing is known about those, so
72 /// the image is passed through and the endpoint gets to decide.
73 pub fn model_accepts_images(model: &str) -> bool {
74 if !model.starts_with("onde-") {
75 return true;
76 }
77 CLOUD_TIERS
78 .iter()
79 .any(|tier| tier_to_model(tier) == model && tier_accepts_images(tier))
80 }
81
82 /// Base URL of the siGit Code Cloud inference endpoint. Override with
83 /// `SIGIT_CLOUD_URL` (dev: `http://localhost:8090/v1`).
84 pub fn cloud_base_url() -> String {
85 std::env::var("SIGIT_CLOUD_URL").unwrap_or_else(|_| DEFAULT_CLOUD_URL.to_string())
86 }
87
88 /// Display label for a tier, e.g. `siGit Code Cloud · Balanced`.
89 pub fn cloud_tier_label(tier: &str) -> String {
90 format!("siGit Code Cloud · {}", tier_title(tier))
91 }
92
93 /// Build a siGit Code Cloud provider for `tier`, if the user is signed in.
94 /// Returns `None` when there is no stored session, so the caller can prompt for
95 /// login rather than silently failing.
96 pub fn cloud_tier_provider(tier: &str) -> Option<ProviderConfig> {
97 let token = crate::credentials::load_token()?;
98 Some(ProviderConfig {
99 display_name: cloud_tier_label(tier),
100 base_url: cloud_base_url(),
101 api_key: token,
102 model: tier_to_model(tier),
103 })
104 }
105
106 /// Map a neutral tier name to the model id sent on the wire. Unknown values pass
107 /// through unchanged so an explicit model id still works.
108 fn tier_to_model(tier: &str) -> String {
109 match tier.trim().to_lowercase().as_str() {
110 "fast" => "onde-fast",
111 "balanced" => "onde-balanced",
112 "large" => "onde-large",
113 "mini" => "onde-mini",
114 "air" => "onde-air",
115 "pro" => "onde-pro",
116 "oke" => "onde-kkk",
117 "flux" => "onde-flux",
118 "apex" => "onde-apex",
119 "aura" => "onde-aura",
120 "orbit" => "onde-orbit",
121 "nova" => "onde-nova",
122 "flare" => "onde-flare",
123 "zenith" => "onde-zenith",
124 "prism" => "onde-prism",
125 other => other,
126 }
127 .to_string()
128 }
129
130 /// A resolved inference provider: everything needed to build an OpenAI-compatible
131 /// client. Deliberately free of any Onde/smbCloud-specific fields.
132 #[derive(Debug, Clone)]
133 pub struct ProviderConfig {
134 /// Human-facing name shown in the UI (e.g. `siGit Code Cloud · Balanced`).
135 pub display_name: String,
136 /// API root, e.g. `https://api.openai.com/v1`.
137 pub base_url: String,
138 pub api_key: String,
139 /// Model id sent to the endpoint, e.g. `onde-balanced` or `gpt-4o-mini`.
140 pub model: String,
141 }
142
143 /// Title-case a tier name for display (`balanced` → `Balanced`).
144 pub fn tier_title(tier: &str) -> String {
145 let tier = tier.trim();
146 let mut chars = tier.chars();
147 match chars.next() {
148 Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
149 None => "Balanced".to_string(),
150 }
151 }
152
153 /// Default model id used when an environment-configured provider omits one.
154 const DEFAULT_ENV_MODEL: &str = "onde-large";
155
156 /// Resolve the startup provider, or `None` to run on-device.
157 ///
158 /// This is only the explicit override (env or `providers.toml`), for power users
159 /// and BYO endpoints. Being signed in does **not** auto-select the cloud: model
160 /// choice is separate from identity, and the user picks a model or tier in
161 /// `/models`. With no override, siGit Code is local-first.
162 pub fn active_provider() -> Option<ProviderConfig> {
163 if let Some(config) = from_env() {
164 return Some(config);
165 }
166 match from_config_file() {
167 Ok(config) => config,
168 Err(error) => {
169 log::warn!("provider: ignoring providers.toml: {error}");
170 None
171 }
172 }
173 }
174
175 /// Provider from environment variables, if both URL and key are present.
176 fn from_env() -> Option<ProviderConfig> {
177 let base_url = non_empty(std::env::var("OPENAI_BASE_URL").ok())?;
178 // A base URL with no key is almost always a mistake. Warn instead of
179 // silently falling back to on-device, which looks like the cloud failed.
180 let Some(api_key) = non_empty(std::env::var("OPENAI_API_KEY").ok()) else {
181 log::warn!(
182 "provider: OPENAI_BASE_URL is set but OPENAI_API_KEY is empty/missing; \
183 staying on-device. Set OPENAI_API_KEY to use the remote provider."
184 );
185 return None;
186 };
187 let model = non_empty(std::env::var("SIGIT_MODEL").ok())
188 .unwrap_or_else(|| DEFAULT_ENV_MODEL.to_string());
189 Some(ProviderConfig {
190 display_name: format!("{model} (custom endpoint)"),
191 base_url,
192 api_key,
193 model,
194 })
195 }
196
197 // ── providers.toml ────────────────────────────────────────────────────────────────
198
199 #[derive(Debug, Deserialize)]
200 struct ProvidersFile {
201 /// Name of the profile to use.
202 active: Option<String>,
203 #[serde(default)]
204 provider: Vec<ProviderEntry>,
205 }
206
207 #[derive(Debug, Deserialize)]
208 struct ProviderEntry {
209 name: String,
210 base_url: String,
211 api_key: String,
212 model: String,
213 }
214
215 /// Path to the providers file: `$SIGIT_CONFIG_DIR` or `~/.config/sigit/providers.toml`.
216 fn config_path() -> Option<PathBuf> {
217 if let Ok(dir) = std::env::var("SIGIT_CONFIG_DIR") {
218 return Some(PathBuf::from(dir).join("providers.toml"));
219 }
220 let home = std::env::var("HOME").ok()?;
221 Some(PathBuf::from(home).join(".config/sigit/providers.toml"))
222 }
223
224 /// Load the active profile from `providers.toml`, if the file exists and names one.
225 fn from_config_file() -> Result<Option<ProviderConfig>, String> {
226 let Some(path) = config_path() else {
227 return Ok(None);
228 };
229 if !path.exists() {
230 return Ok(None);
231 }
232
233 let contents =
234 std::fs::read_to_string(&path).map_err(|error| format!("read {path:?}: {error}"))?;
235 let parsed: ProvidersFile =
236 toml::from_str(&contents).map_err(|error| format!("parse {path:?}: {error}"))?;
237
238 let Some(active) = parsed.active else {
239 return Ok(None);
240 };
241
242 let entry = parsed
243 .provider
244 .into_iter()
245 .find(|entry| entry.name == active)
246 .ok_or_else(|| format!("active profile {active:?} not found"))?;
247
248 Ok(Some(ProviderConfig {
249 display_name: format!("{} ({})", entry.name, entry.model),
250 base_url: entry.base_url,
251 api_key: entry.api_key,
252 model: entry.model,
253 }))
254 }
255
256 /// Treat an empty/whitespace string as absent.
257 fn non_empty(value: Option<String>) -> Option<String> {
258 value
259 .map(|string| string.trim().to_string())
260 .filter(|string| !string.is_empty())
261 }
262
263 #[cfg(test)]
264 mod tests {
265 use super::*;
266
267 #[test]
268 fn parses_active_profile_from_toml() {
269 let toml = r#"
270 active = "primary"
271
272 [[provider]]
273 name = "primary"
274 base_url = "https://api.example.com/v1"
275 api_key = "sk-test"
276 model = "model-a"
277
278 [[provider]]
279 name = "openai"
280 base_url = "https://api.openai.com/v1"
281 api_key = "sk-other"
282 model = "gpt-4o-mini"
283 "#;
284 let parsed: ProvidersFile = toml::from_str(toml).unwrap();
285 let active = parsed.active.unwrap();
286 let entry = parsed
287 .provider
288 .into_iter()
289 .find(|entry| entry.name == active)
290 .unwrap();
291 assert_eq!(entry.base_url, "https://api.example.com/v1");
292 assert_eq!(entry.model, "model-a");
293 }
294
295 #[test]
296 fn non_empty_filters_blanks() {
297 assert_eq!(non_empty(Some(" ".to_string())), None);
298 assert_eq!(non_empty(Some(" x ".to_string())), Some("x".to_string()));
299 assert_eq!(non_empty(None), None);
300 }
301
302 #[test]
303 fn maps_default_cloud_tier_to_wire_model_id() {
304 assert_eq!(DEFAULT_CLOUD_TIER, "nova");
305 assert_eq!(tier_to_model(DEFAULT_CLOUD_TIER), "onde-nova");
306 }
307
308 #[test]
309 fn maps_every_cloud_tier_to_its_own_onde_model_id() {
310 for tier in CLOUD_TIERS {
311 // `oke` is the one tier whose wire id is not its own name.
312 let expected = match *tier {
313 "oke" => "onde-kkk".to_string(),
314 name => format!("onde-{name}"),
315 };
316 assert_eq!(tier_to_model(tier), expected);
317 }
318 }
319
320 #[test]
321 fn image_support_follows_the_tier_table() {
322 assert!(tier_accepts_images("large"));
323 assert!(tier_accepts_images("oke"));
324 // The default tier is text-only at the source.
325 assert!(!tier_accepts_images(DEFAULT_CLOUD_TIER));
326 assert!(!tier_accepts_images("apex"));
327
328 assert!(model_accepts_images("onde-large"));
329 // `oke` is the one tier whose wire id is not `onde-<tier>`.
330 assert!(model_accepts_images("onde-kkk"));
331 assert!(!model_accepts_images("onde-nova"));
332 // An onde id this client has never heard of is not assumed to work.
333 assert!(!model_accepts_images("onde-something-new"));
334 // A model on the user's own endpoint is theirs to judge.
335 assert!(model_accepts_images("gpt-4o-mini"));
336
337 for tier in IMAGE_TIERS {
338 assert!(CLOUD_TIERS.contains(tier), "{tier} is not a cloud tier");
339 }
340 }
341 }