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 /// Whether the model id sent on the wire takes audio.
83 ///
84 /// No cloud tier does: onde-cloud drops `input_audio` parts when it parses a
85 /// request, so a clip sent to an `onde-*` id would vanish without a word. Any
86 /// other id is the user's own endpoint and, as with images, gets the audio and
87 /// decides for itself.
88 pub fn model_accepts_audio(model: &str) -> bool {
89 !model.starts_with("onde-")
90 }
91
92 /// Base URL of the siGit Code Cloud inference endpoint. Override with
93 /// `SIGIT_CLOUD_URL` (dev: `http://localhost:8090/v1`).
94 pub fn cloud_base_url() -> String {
95 std::env::var("SIGIT_CLOUD_URL").unwrap_or_else(|_| DEFAULT_CLOUD_URL.to_string())
96 }
97
98 /// Display label for a tier, e.g. `siGit Code Cloud · Balanced`.
99 pub fn cloud_tier_label(tier: &str) -> String {
100 format!("siGit Code Cloud · {}", tier_title(tier))
101 }
102
103 /// Build a siGit Code Cloud provider for `tier`, if the user is signed in.
104 /// Returns `None` when there is no stored session, so the caller can prompt for
105 /// login rather than silently failing.
106 pub fn cloud_tier_provider(tier: &str) -> Option<ProviderConfig> {
107 let token = crate::credentials::load_token()?;
108 Some(ProviderConfig {
109 display_name: cloud_tier_label(tier),
110 base_url: cloud_base_url(),
111 api_key: token,
112 model: tier_to_model(tier),
113 })
114 }
115
116 /// Map a neutral tier name to the model id sent on the wire. Unknown values pass
117 /// through unchanged so an explicit model id still works.
118 fn tier_to_model(tier: &str) -> String {
119 match tier.trim().to_lowercase().as_str() {
120 "fast" => "onde-fast",
121 "balanced" => "onde-balanced",
122 "large" => "onde-large",
123 "mini" => "onde-mini",
124 "air" => "onde-air",
125 "pro" => "onde-pro",
126 "oke" => "onde-kkk",
127 "flux" => "onde-flux",
128 "apex" => "onde-apex",
129 "aura" => "onde-aura",
130 "orbit" => "onde-orbit",
131 "nova" => "onde-nova",
132 "flare" => "onde-flare",
133 "zenith" => "onde-zenith",
134 "prism" => "onde-prism",
135 other => other,
136 }
137 .to_string()
138 }
139
140 /// A resolved inference provider: everything needed to build an OpenAI-compatible
141 /// client. Deliberately free of any Onde/smbCloud-specific fields.
142 #[derive(Debug, Clone)]
143 pub struct ProviderConfig {
144 /// Human-facing name shown in the UI (e.g. `siGit Code Cloud · Balanced`).
145 pub display_name: String,
146 /// API root, e.g. `https://api.openai.com/v1`.
147 pub base_url: String,
148 pub api_key: String,
149 /// Model id sent to the endpoint, e.g. `onde-balanced` or `gpt-4o-mini`.
150 pub model: String,
151 }
152
153 /// Title-case a tier name for display (`balanced` → `Balanced`).
154 pub fn tier_title(tier: &str) -> String {
155 let tier = tier.trim();
156 let mut chars = tier.chars();
157 match chars.next() {
158 Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
159 None => "Balanced".to_string(),
160 }
161 }
162
163 /// Default model id used when an environment-configured provider omits one.
164 const DEFAULT_ENV_MODEL: &str = "onde-large";
165
166 /// Resolve the startup provider, or `None` to run on-device.
167 ///
168 /// This is only the explicit override (env or `providers.toml`), for power users
169 /// and BYO endpoints. Being signed in does **not** auto-select the cloud: model
170 /// choice is separate from identity, and the user picks a model or tier in
171 /// `/models`. With no override, siGit Code is local-first.
172 pub fn active_provider() -> Option<ProviderConfig> {
173 if let Some(config) = from_env() {
174 return Some(config);
175 }
176 match from_config_file() {
177 Ok(config) => config,
178 Err(error) => {
179 log::warn!("provider: ignoring providers.toml: {error}");
180 None
181 }
182 }
183 }
184
185 /// Provider from environment variables, if both URL and key are present.
186 fn from_env() -> Option<ProviderConfig> {
187 let base_url = non_empty(std::env::var("OPENAI_BASE_URL").ok())?;
188 // A base URL with no key is almost always a mistake. Warn instead of
189 // silently falling back to on-device, which looks like the cloud failed.
190 let Some(api_key) = non_empty(std::env::var("OPENAI_API_KEY").ok()) else {
191 log::warn!(
192 "provider: OPENAI_BASE_URL is set but OPENAI_API_KEY is empty/missing; \
193 staying on-device. Set OPENAI_API_KEY to use the remote provider."
194 );
195 return None;
196 };
197 let model = non_empty(std::env::var("SIGIT_MODEL").ok())
198 .unwrap_or_else(|| DEFAULT_ENV_MODEL.to_string());
199 Some(ProviderConfig {
200 display_name: format!("{model} (custom endpoint)"),
201 base_url,
202 api_key,
203 model,
204 })
205 }
206
207 // ── providers.toml ────────────────────────────────────────────────────────────────
208
209 #[derive(Debug, Deserialize)]
210 struct ProvidersFile {
211 /// Name of the profile to use.
212 active: Option<String>,
213 #[serde(default)]
214 provider: Vec<ProviderEntry>,
215 }
216
217 #[derive(Debug, Deserialize)]
218 struct ProviderEntry {
219 name: String,
220 base_url: String,
221 api_key: String,
222 model: String,
223 }
224
225 /// Path to the providers file: `$SIGIT_CONFIG_DIR` or `~/.config/sigit/providers.toml`.
226 fn config_path() -> Option<PathBuf> {
227 if let Ok(dir) = std::env::var("SIGIT_CONFIG_DIR") {
228 return Some(PathBuf::from(dir).join("providers.toml"));
229 }
230 let home = std::env::var("HOME").ok()?;
231 Some(PathBuf::from(home).join(".config/sigit/providers.toml"))
232 }
233
234 /// Load the active profile from `providers.toml`, if the file exists and names one.
235 fn from_config_file() -> Result<Option<ProviderConfig>, String> {
236 let Some(path) = config_path() else {
237 return Ok(None);
238 };
239 if !path.exists() {
240 return Ok(None);
241 }
242
243 let contents =
244 std::fs::read_to_string(&path).map_err(|error| format!("read {path:?}: {error}"))?;
245 let parsed: ProvidersFile =
246 toml::from_str(&contents).map_err(|error| format!("parse {path:?}: {error}"))?;
247
248 let Some(active) = parsed.active else {
249 return Ok(None);
250 };
251
252 let entry = parsed
253 .provider
254 .into_iter()
255 .find(|entry| entry.name == active)
256 .ok_or_else(|| format!("active profile {active:?} not found"))?;
257
258 Ok(Some(ProviderConfig {
259 display_name: format!("{} ({})", entry.name, entry.model),
260 base_url: entry.base_url,
261 api_key: entry.api_key,
262 model: entry.model,
263 }))
264 }
265
266 /// Treat an empty/whitespace string as absent.
267 fn non_empty(value: Option<String>) -> Option<String> {
268 value
269 .map(|string| string.trim().to_string())
270 .filter(|string| !string.is_empty())
271 }
272
273 #[cfg(test)]
274 mod tests {
275 use super::*;
276
277 #[test]
278 fn parses_active_profile_from_toml() {
279 let toml = r#"
280 active = "primary"
281
282 [[provider]]
283 name = "primary"
284 base_url = "https://api.example.com/v1"
285 api_key = "sk-test"
286 model = "model-a"
287
288 [[provider]]
289 name = "openai"
290 base_url = "https://api.openai.com/v1"
291 api_key = "sk-other"
292 model = "gpt-4o-mini"
293 "#;
294 let parsed: ProvidersFile = toml::from_str(toml).unwrap();
295 let active = parsed.active.unwrap();
296 let entry = parsed
297 .provider
298 .into_iter()
299 .find(|entry| entry.name == active)
300 .unwrap();
301 assert_eq!(entry.base_url, "https://api.example.com/v1");
302 assert_eq!(entry.model, "model-a");
303 }
304
305 #[test]
306 fn non_empty_filters_blanks() {
307 assert_eq!(non_empty(Some(" ".to_string())), None);
308 assert_eq!(non_empty(Some(" x ".to_string())), Some("x".to_string()));
309 assert_eq!(non_empty(None), None);
310 }
311
312 #[test]
313 fn maps_default_cloud_tier_to_wire_model_id() {
314 assert_eq!(DEFAULT_CLOUD_TIER, "nova");
315 assert_eq!(tier_to_model(DEFAULT_CLOUD_TIER), "onde-nova");
316 }
317
318 #[test]
319 fn maps_every_cloud_tier_to_its_own_onde_model_id() {
320 for tier in CLOUD_TIERS {
321 // `oke` is the one tier whose wire id is not its own name.
322 let expected = match *tier {
323 "oke" => "onde-kkk".to_string(),
324 name => format!("onde-{name}"),
325 };
326 assert_eq!(tier_to_model(tier), expected);
327 }
328 }
329
330 #[test]
331 fn image_support_follows_the_tier_table() {
332 assert!(tier_accepts_images("large"));
333 assert!(tier_accepts_images("oke"));
334 // The default tier is text-only at the source.
335 assert!(!tier_accepts_images(DEFAULT_CLOUD_TIER));
336 assert!(!tier_accepts_images("apex"));
337
338 assert!(model_accepts_images("onde-large"));
339 // `oke` is the one tier whose wire id is not `onde-<tier>`.
340 assert!(model_accepts_images("onde-kkk"));
341 assert!(!model_accepts_images("onde-nova"));
342 // An onde id this client has never heard of is not assumed to work.
343 assert!(!model_accepts_images("onde-something-new"));
344 // A model on the user's own endpoint is theirs to judge.
345 assert!(model_accepts_images("gpt-4o-mini"));
346
347 for tier in IMAGE_TIERS {
348 assert!(CLOUD_TIERS.contains(tier), "{tier} is not a cloud tier");
349 }
350 }
351
352 #[test]
353 fn audio_goes_only_to_the_users_own_endpoint() {
354 for tier in CLOUD_TIERS {
355 assert!(!model_accepts_audio(&tier_to_model(tier)), "{tier}");
356 }
357 assert!(model_accepts_audio("gpt-4o-audio-preview"));
358 }
359 }