spark-rsi
Resilient systems interface and fault-tolerant agent communication
Brought by drakestapleton
| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use std::fs; |
| 3 | use std::path::PathBuf; |
| 4 | use std::process::Command; |
| 5 | |
| 6 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 7 | pub struct OperatorProfile { |
| 8 | #[serde(default = "default_operator_name")] |
| 9 | pub name: String, |
| 10 | #[serde(default = "default_operator_email")] |
| 11 | pub email: String, |
| 12 | #[serde(default = "default_operator_handle")] |
| 13 | pub handle: String, |
| 14 | #[serde(default = "default_sign_commits")] |
| 15 | pub sign_commits: bool, |
| 16 | } |
| 17 | |
| 18 | impl Default for OperatorProfile { |
| 19 | fn default() -> Self { |
| 20 | Self { |
| 21 | name: default_operator_name(), |
| 22 | email: default_operator_email(), |
| 23 | handle: default_operator_handle(), |
| 24 | sign_commits: default_sign_commits(), |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn default_operator_name() -> String { |
| 30 | "Sovereign Operator".to_string() |
| 31 | } |
| 32 | fn default_operator_email() -> String { |
| 33 | "operator@local".to_string() |
| 34 | } |
| 35 | fn default_operator_handle() -> String { |
| 36 | "operator".to_string() |
| 37 | } |
| 38 | fn default_sign_commits() -> bool { |
| 39 | false |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 43 | pub struct EngineConfig { |
| 44 | #[serde(default = "default_engine_mode")] |
| 45 | pub mode: String, // "api" or "max" |
| 46 | #[serde(default = "default_api_base_url")] |
| 47 | pub api_base_url: String, |
| 48 | #[serde(default)] |
| 49 | pub api_key: String, |
| 50 | #[serde(default = "default_model_id")] |
| 51 | pub model_id: String, |
| 52 | #[serde(default = "default_max_port")] |
| 53 | pub max_port: u16, |
| 54 | #[serde(default = "default_context_window")] |
| 55 | pub context_window: usize, |
| 56 | #[serde(default = "default_temperature")] |
| 57 | pub temperature: f32, |
| 58 | } |
| 59 | |
| 60 | impl Default for EngineConfig { |
| 61 | fn default() -> Self { |
| 62 | Self { |
| 63 | mode: default_engine_mode(), |
| 64 | api_base_url: default_api_base_url(), |
| 65 | api_key: String::new(), |
| 66 | model_id: default_model_id(), |
| 67 | max_port: default_max_port(), |
| 68 | context_window: default_context_window(), |
| 69 | temperature: default_temperature(), |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn default_engine_mode() -> String { |
| 75 | "max".to_string() |
| 76 | } |
| 77 | fn default_api_base_url() -> String { |
| 78 | "http://127.0.0.1:18006/v1".to_string() |
| 79 | } |
| 80 | fn default_model_id() -> String { |
| 81 | "nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16".to_string() |
| 82 | } |
| 83 | fn default_max_port() -> u16 { |
| 84 | 18006 |
| 85 | } |
| 86 | fn default_context_window() -> usize { |
| 87 | 32768 |
| 88 | } |
| 89 | fn default_temperature() -> f32 { |
| 90 | 0.7 |
| 91 | } |
| 92 | |
| 93 | #[derive(Debug, Clone, Serialize, Deserialize, Default)] |
| 94 | pub struct SovereignConfig { |
| 95 | #[serde(default)] |
| 96 | pub operator: OperatorProfile, |
| 97 | #[serde(default)] |
| 98 | pub engine: EngineConfig, |
| 99 | } |
| 100 | |
| 101 | impl SovereignConfig { |
| 102 | pub fn config_path() -> PathBuf { |
| 103 | if let Ok(path) = std::env::var("SOVEREIGN_CONFIG_PATH") { |
| 104 | return PathBuf::from(path); |
| 105 | } |
| 106 | let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string()); |
| 107 | PathBuf::from(home).join(".config/sovereign/operator.toml") |
| 108 | } |
| 109 | |
| 110 | pub fn load() -> Self { |
| 111 | let path = Self::config_path(); |
| 112 | let mut config = if path.exists() { |
| 113 | match fs::read_to_string(&path) { |
| 114 | Ok(content) => toml::from_str(&content).unwrap_or_default(), |
| 115 | Err(_) => Self::default(), |
| 116 | } |
| 117 | } else { |
| 118 | Self::default() |
| 119 | }; |
| 120 | |
| 121 | // Environment overrides |
| 122 | if let Ok(name) = std::env::var("SOVEREIGN_OPERATOR_NAME") { |
| 123 | config.operator.name = name; |
| 124 | } |
| 125 | if let Ok(email) = std::env::var("SOVEREIGN_OPERATOR_EMAIL") { |
| 126 | config.operator.email = email; |
| 127 | } |
| 128 | if let Ok(handle) = std::env::var("SOVEREIGN_OPERATOR_HANDLE") { |
| 129 | config.operator.handle = handle; |
| 130 | } |
| 131 | if let Ok(mode) = std::env::var("SOVEREIGN_ENGINE_MODE") { |
| 132 | config.engine.mode = mode; |
| 133 | } |
| 134 | if let Ok(url) = std::env::var("SOVEREIGN_API_BASE_URL") { |
| 135 | config.engine.api_base_url = url; |
| 136 | } |
| 137 | |
| 138 | // Fallback to git config if name or email still default |
| 139 | if config.operator.name == "Sovereign Operator" { |
| 140 | if let Ok(out) = Command::new("git").args(["config", "user.name"]).output() { |
| 141 | let git_name = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 142 | if !git_name.is_empty() { |
| 143 | config.operator.name = git_name; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | if config.operator.email == "operator@local" { |
| 148 | if let Ok(out) = Command::new("git").args(["config", "user.email"]).output() { |
| 149 | let git_email = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 150 | if !git_email.is_empty() { |
| 151 | config.operator.email = git_email; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | config |
| 157 | } |
| 158 | |
| 159 | pub fn save(&self) -> Result<(), String> { |
| 160 | let path = Self::config_path(); |
| 161 | if let Some(parent) = path.parent() { |
| 162 | fs::create_dir_all(parent).map_err(|e| format!("Failed to create config dir: {}", e))?; |
| 163 | } |
| 164 | let serialized = toml::to_string_pretty(self) |
| 165 | .map_err(|e| format!("Failed to serialize config: {}", e))?; |
| 166 | fs::write(&path, serialized) |
| 167 | .map_err(|e| format!("Failed to write config to {:?}: {}", path, e))?; |
| 168 | Ok(()) |
| 169 | } |
| 170 | |
| 171 | pub fn author_string(&self) -> String { |
| 172 | format!("{} <{}>", self.operator.name, self.operator.email) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | #[cfg(test)] |
| 177 | mod tests { |
| 178 | use super::*; |
| 179 | |
| 180 | #[test] |
| 181 | fn test_default_config_and_author_string() { |
| 182 | let mut cfg = SovereignConfig::default(); |
| 183 | cfg.operator.name = "Alice".to_string(); |
| 184 | cfg.operator.email = "alice@domain.org".to_string(); |
| 185 | assert_eq!(cfg.author_string(), "Alice <alice@domain.org>"); |
| 186 | assert_eq!(cfg.engine.mode, "max"); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn test_serialization_roundtrip() { |
| 191 | let cfg = SovereignConfig::default(); |
| 192 | let serialized = toml::to_string_pretty(&cfg).unwrap(); |
| 193 | let deserialized: SovereignConfig = toml::from_str(&serialized).unwrap(); |
| 194 | assert_eq!(deserialized.operator.name, cfg.operator.name); |
| 195 | assert_eq!(deserialized.engine.mode, cfg.engine.mode); |
| 196 | } |
| 197 | } |