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