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 /// There are two lines because the cloud serves two independent families, and a
33 /// tier is only routable when the cloud has that family configured. Which family
34 /// is which, and what runs behind either, is deliberately not represented here:
35 /// the client knows tier names and nothing else.
36 pub const CLOUD_TIERS: &[&str] = &["fast", "balanced", "large", "mini", "air", "pro", "oke"];
37
38 /// Base URL of the siGit Code Cloud inference endpoint. Override with
39 /// `SIGIT_CLOUD_URL` (dev: `http://localhost:8090/v1`).
40 pub fn cloud_base_url() -> String {
41 std::env::var("SIGIT_CLOUD_URL").unwrap_or_else(|_| DEFAULT_CLOUD_URL.to_string())
42 }
43
44 /// Display label for a tier, e.g. `siGit Code Cloud · Balanced`.
45 pub fn cloud_tier_label(tier: &str) -> String {
46 format!("siGit Code Cloud · {}", tier_title(tier))
47 }
48
49 /// Build a siGit Code Cloud provider for `tier`, if the user is signed in.
50 /// Returns `None` when there is no stored session, so the caller can prompt for
51 /// login rather than silently failing.
52 pub fn cloud_tier_provider(tier: &str) -> Option<ProviderConfig> {
53 let token = crate::credentials::load_token()?;
54 Some(ProviderConfig {
55 display_name: cloud_tier_label(tier),
56 base_url: cloud_base_url(),
57 api_key: token,
58 model: tier_to_model(tier),
59 })
60 }
61
62 /// Map a neutral tier name to the model id sent on the wire. Unknown values pass
63 /// through unchanged so an explicit model id still works.
64 fn tier_to_model(tier: &str) -> String {
65 match tier.trim().to_lowercase().as_str() {
66 "fast" => "onde-fast",
67 "balanced" => "onde-balanced",
68 "large" => "onde-large",
69 "mini" => "onde-mini",
70 "air" => "onde-air",
71 "pro" => "onde-pro",
72 "oke" => "onde-kkk",
73 other => other,
74 }
75 .to_string()
76 }
77
78 /// A resolved inference provider: everything needed to build an OpenAI-compatible
79 /// client. Deliberately free of any Onde/smbCloud-specific fields.
80 #[derive(Debug, Clone)]
81 pub struct ProviderConfig {
82 /// Human-facing name shown in the UI (e.g. `siGit Code Cloud · Balanced`).
83 pub display_name: String,
84 /// API root, e.g. `https://api.openai.com/v1`.
85 pub base_url: String,
86 pub api_key: String,
87 /// Model id sent to the endpoint, e.g. `onde-balanced` or `gpt-4o-mini`.
88 pub model: String,
89 }
90
91 /// Title-case a tier name for display (`balanced` → `Balanced`).
92 pub fn tier_title(tier: &str) -> String {
93 let tier = tier.trim();
94 let mut chars = tier.chars();
95 match chars.next() {
96 Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
97 None => "Balanced".to_string(),
98 }
99 }
100
101 /// Default model id used when an environment-configured provider omits one.
102 const DEFAULT_ENV_MODEL: &str = "onde-large";
103
104 /// Resolve the startup provider, or `None` to run on-device.
105 ///
106 /// This is only the explicit override (env or `providers.toml`), for power users
107 /// and BYO endpoints. Being signed in does **not** auto-select the cloud: model
108 /// choice is separate from identity, and the user picks a model or tier in
109 /// `/models`. With no override, siGit Code is local-first.
110 pub fn active_provider() -> Option<ProviderConfig> {
111 if let Some(config) = from_env() {
112 return Some(config);
113 }
114 match from_config_file() {
115 Ok(config) => config,
116 Err(error) => {
117 log::warn!("provider: ignoring providers.toml: {error}");
118 None
119 }
120 }
121 }
122
123 /// Provider from environment variables, if both URL and key are present.
124 fn from_env() -> Option<ProviderConfig> {
125 let base_url = non_empty(std::env::var("OPENAI_BASE_URL").ok())?;
126 // A base URL with no key is almost always a mistake. Warn instead of
127 // silently falling back to on-device, which looks like the cloud failed.
128 let Some(api_key) = non_empty(std::env::var("OPENAI_API_KEY").ok()) else {
129 log::warn!(
130 "provider: OPENAI_BASE_URL is set but OPENAI_API_KEY is empty/missing; \
131 staying on-device. Set OPENAI_API_KEY to use the remote provider."
132 );
133 return None;
134 };
135 let model = non_empty(std::env::var("SIGIT_MODEL").ok())
136 .unwrap_or_else(|| DEFAULT_ENV_MODEL.to_string());
137 Some(ProviderConfig {
138 display_name: format!("{model} (custom endpoint)"),
139 base_url,
140 api_key,
141 model,
142 })
143 }
144
145 // ── providers.toml ────────────────────────────────────────────────────────────────
146
147 #[derive(Debug, Deserialize)]
148 struct ProvidersFile {
149 /// Name of the profile to use.
150 active: Option<String>,
151 #[serde(default)]
152 provider: Vec<ProviderEntry>,
153 }
154
155 #[derive(Debug, Deserialize)]
156 struct ProviderEntry {
157 name: String,
158 base_url: String,
159 api_key: String,
160 model: String,
161 }
162
163 /// Path to the providers file: `$SIGIT_CONFIG_DIR` or `~/.config/sigit/providers.toml`.
164 fn config_path() -> Option<PathBuf> {
165 if let Ok(dir) = std::env::var("SIGIT_CONFIG_DIR") {
166 return Some(PathBuf::from(dir).join("providers.toml"));
167 }
168 let home = std::env::var("HOME").ok()?;
169 Some(PathBuf::from(home).join(".config/sigit/providers.toml"))
170 }
171
172 /// Load the active profile from `providers.toml`, if the file exists and names one.
173 fn from_config_file() -> Result<Option<ProviderConfig>, String> {
174 let Some(path) = config_path() else {
175 return Ok(None);
176 };
177 if !path.exists() {
178 return Ok(None);
179 }
180
181 let contents =
182 std::fs::read_to_string(&path).map_err(|error| format!("read {path:?}: {error}"))?;
183 let parsed: ProvidersFile =
184 toml::from_str(&contents).map_err(|error| format!("parse {path:?}: {error}"))?;
185
186 let Some(active) = parsed.active else {
187 return Ok(None);
188 };
189
190 let entry = parsed
191 .provider
192 .into_iter()
193 .find(|entry| entry.name == active)
194 .ok_or_else(|| format!("active profile {active:?} not found"))?;
195
196 Ok(Some(ProviderConfig {
197 display_name: format!("{} ({})", entry.name, entry.model),
198 base_url: entry.base_url,
199 api_key: entry.api_key,
200 model: entry.model,
201 }))
202 }
203
204 /// Treat an empty/whitespace string as absent.
205 fn non_empty(value: Option<String>) -> Option<String> {
206 value
207 .map(|string| string.trim().to_string())
208 .filter(|string| !string.is_empty())
209 }
210
211 #[cfg(test)]
212 mod tests {
213 use super::*;
214
215 #[test]
216 fn parses_active_profile_from_toml() {
217 let toml = r#"
218 active = "primary"
219
220 [[provider]]
221 name = "primary"
222 base_url = "https://api.example.com/v1"
223 api_key = "sk-test"
224 model = "model-a"
225
226 [[provider]]
227 name = "openai"
228 base_url = "https://api.openai.com/v1"
229 api_key = "sk-other"
230 model = "gpt-4o-mini"
231 "#;
232 let parsed: ProvidersFile = toml::from_str(toml).unwrap();
233 let active = parsed.active.unwrap();
234 let entry = parsed
235 .provider
236 .into_iter()
237 .find(|entry| entry.name == active)
238 .unwrap();
239 assert_eq!(entry.base_url, "https://api.example.com/v1");
240 assert_eq!(entry.model, "model-a");
241 }
242
243 #[test]
244 fn non_empty_filters_blanks() {
245 assert_eq!(non_empty(Some(" ".to_string())), None);
246 assert_eq!(non_empty(Some(" x ".to_string())), Some("x".to_string()));
247 assert_eq!(non_empty(None), None);
248 }
249 }