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