Skip to main content

scud/
config.rs

1use anyhow::{Context, Result};
2use serde::{Deserialize, Serialize};
3use std::fs;
4use std::path::Path;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Config {
8    pub llm: LLMConfig,
9    #[serde(default)]
10    pub swarm: SwarmConfig,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct SwarmConfig {
15    #[serde(default = "default_swarm_harness")]
16    pub harness: String,
17    /// Default model for swarm agents (e.g., "xai/grok-code-fast-1", "opus").
18    /// When unset, inherits from [llm].model.
19    #[serde(default)]
20    pub model: Option<String>,
21    #[serde(default = "default_round_size")]
22    pub round_size: usize,
23    #[serde(default = "default_default_tag")]
24    pub default_tag: Option<String>,
25    /// Use direct API instead of CLI harnesses.
26    /// Requires `direct-api` Cargo feature.
27    #[serde(default)]
28    pub use_direct_api: bool,
29    /// Provider for direct API mode: anthropic, openai, xai, openrouter, opencode-zen
30    #[serde(default = "default_direct_api_provider")]
31    pub direct_api_provider: String,
32}
33
34fn default_swarm_harness() -> String {
35    "claude".to_string()
36}
37
38fn default_round_size() -> usize {
39    5
40}
41
42fn default_default_tag() -> Option<String> {
43    None
44}
45
46fn default_direct_api_provider() -> String {
47    std::env::var("SCUD_DIRECT_API_PROVIDER").unwrap_or_else(|_| "anthropic".to_string())
48}
49
50impl Default for SwarmConfig {
51    fn default() -> Self {
52        SwarmConfig {
53            harness: default_swarm_harness(),
54            model: std::env::var("SCUD_SWARM_MODEL").ok(),
55            round_size: default_round_size(),
56            default_tag: default_default_tag(),
57            use_direct_api: false,
58            direct_api_provider: default_direct_api_provider(),
59        }
60    }
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct LLMConfig {
65    /// Default provider
66    #[serde(default = "default_provider")]
67    pub provider: String,
68    /// Default model (used when no tier specified)
69    #[serde(default = "default_model")]
70    pub model: String,
71    /// Smart provider for validation/analysis tasks
72    #[serde(default = "default_smart_provider")]
73    pub smart_provider: String,
74    /// Smart model for validation/analysis tasks (large context)
75    #[serde(default = "default_smart_model")]
76    pub smart_model: String,
77    /// Fast provider for generation tasks
78    #[serde(default = "default_fast_provider")]
79    pub fast_provider: String,
80    /// Fast model for generation tasks
81    #[serde(default = "default_fast_model")]
82    pub fast_model: String,
83    /// Max tokens for LLM requests
84    #[serde(default = "default_max_tokens")]
85    pub max_tokens: u32,
86}
87
88fn default_provider() -> String {
89    std::env::var("SCUD_PROVIDER").unwrap_or_else(|_| "xai".to_string())
90}
91
92fn default_model() -> String {
93    std::env::var("SCUD_MODEL").unwrap_or_else(|_| "xai/grok-code-fast-1".to_string())
94}
95
96fn default_smart_provider() -> String {
97    std::env::var("SCUD_SMART_PROVIDER").unwrap_or_else(|_| "claude-cli".to_string())
98}
99
100fn default_smart_model() -> String {
101    std::env::var("SCUD_SMART_MODEL").unwrap_or_else(|_| "opus".to_string())
102}
103
104fn default_fast_provider() -> String {
105    std::env::var("SCUD_FAST_PROVIDER").unwrap_or_else(|_| "xai".to_string())
106}
107
108fn default_fast_model() -> String {
109    std::env::var("SCUD_FAST_MODEL").unwrap_or_else(|_| "xai/grok-code-fast-1".to_string())
110}
111
112fn default_max_tokens() -> u32 {
113    std::env::var("SCUD_MAX_TOKENS")
114        .ok()
115        .and_then(|s| s.parse().ok())
116        .unwrap_or(16000)
117}
118
119impl Default for Config {
120    fn default() -> Self {
121        Config {
122            llm: LLMConfig {
123                provider: default_provider(),
124                model: default_model(),
125                smart_provider: default_smart_provider(),
126                smart_model: default_smart_model(),
127                fast_provider: default_fast_provider(),
128                fast_model: default_fast_model(),
129                max_tokens: default_max_tokens(),
130            },
131            swarm: SwarmConfig::default(),
132        }
133    }
134}
135
136impl Config {
137    /// Resolve the swarm model: swarm.model > llm.model
138    pub fn swarm_model(&self) -> &str {
139        self.swarm.model.as_deref().unwrap_or(&self.llm.model)
140    }
141
142    pub fn load(path: &Path) -> Result<Self> {
143        let content = fs::read_to_string(path)
144            .with_context(|| format!("Failed to read config file: {}", path.display()))?;
145
146        toml::from_str(&content)
147            .with_context(|| format!("Failed to parse config file: {}", path.display()))
148    }
149
150    pub fn save(&self, path: &Path) -> Result<()> {
151        let content = toml::to_string_pretty(self).context("Failed to serialize config to TOML")?;
152
153        if let Some(parent) = path.parent() {
154            fs::create_dir_all(parent).with_context(|| {
155                format!("Failed to create config directory: {}", parent.display())
156            })?;
157        }
158
159        fs::write(path, content)
160            .with_context(|| format!("Failed to write config file: {}", path.display()))
161    }
162
163    pub fn api_key_env_var(&self) -> &str {
164        Self::api_key_env_var_for_provider(&self.llm.provider)
165    }
166
167    pub fn api_key_env_var_for_provider(provider: &str) -> &str {
168        match provider {
169            "anthropic" => "ANTHROPIC_API_KEY",
170            "anthropic-oauth" => "NONE", // Uses Claude Code OAuth from Keychain
171            "xai" => "XAI_API_KEY",
172            "openai" => "OPENAI_API_KEY",
173            "openrouter" => "OPENROUTER_API_KEY",
174            "opencode-zen" | "opencode" | "zen" => "OPENCODE_API_KEY",
175            "claude-cli" => "NONE", // Claude CLI doesn't need API key
176            "codex" => "NONE",      // Codex CLI doesn't need API key
177            "cursor" => "NONE",     // Cursor Agent CLI doesn't need API key
178            _ => "API_KEY",
179        }
180    }
181
182    pub fn requires_api_key(&self) -> bool {
183        let providers = [
184            &self.llm.provider,
185            &self.llm.smart_provider,
186            &self.llm.fast_provider,
187        ];
188        providers.iter().any(|p| {
189            !matches!(
190                p.as_str(),
191                "claude-cli" | "codex" | "cursor" | "anthropic-oauth"
192            )
193        })
194    }
195
196    pub fn api_endpoint(&self) -> &str {
197        match self.llm.provider.as_str() {
198            "anthropic" => "https://api.anthropic.com/v1/messages",
199            "xai" => "https://api.x.ai/v1/chat/completions",
200            "openai" => "https://api.openai.com/v1/chat/completions",
201            "openrouter" => "https://openrouter.ai/api/v1/chat/completions",
202            _ => "https://api.anthropic.com/v1/messages",
203        }
204    }
205
206    pub fn default_model_for_provider(provider: &str) -> &str {
207        match provider {
208            "xai" => "xai/grok-code-fast-1",
209            "anthropic" => "claude-sonnet-4-5-20250929",
210            "anthropic-oauth" => "claude-opus-4-6",
211            "openai" => "o3-mini",
212            "openrouter" => "anthropic/claude-sonnet-4.5",
213            "claude-cli" => "sonnet", // Claude CLI model names: sonnet, opus, haiku
214            "codex" => "gpt-5.1",         // Codex CLI default model
215            "cursor" => "claude-4-sonnet", // Cursor Agent default model
216            _ => "xai/grok-code-fast-1",
217        }
218    }
219
220    /// Get suggested models for a provider (for display in init)
221    pub fn suggested_models_for_provider(provider: &str) -> Vec<&str> {
222        match provider {
223            "xai" => vec![
224                "xai/grok-code-fast-1",
225                "xai/grok-4-1-fast",
226                "xai/grok-4-fast",
227                "xai/grok-3-fast",
228            ],
229            "anthropic" => vec![
230                "claude-sonnet-4-5-20250929",
231                "claude-opus-4-5-20251101",
232                "claude-haiku-4-5-20251001",
233                "claude-opus-4-1-20250805",
234            ],
235            "anthropic-oauth" => vec![
236                "claude-opus-4-6",
237                "claude-sonnet-4-5-20250929",
238                "claude-opus-4-5-20251101",
239                "claude-haiku-4-5-20251001",
240            ],
241            "openai" => vec![
242                "gpt-5.2-high",
243                "gpt-5.1",
244                "gpt-5.1-mini",
245                "o3-mini",
246                "o3",
247                "o4-mini",
248                "gpt-4.1",
249            ],
250            "openrouter" => vec![
251                "anthropic/claude-sonnet-4.5",
252                "anthropic/claude-opus-4.5",
253                "openai/o3-mini",
254                "openai/gpt-4.1",
255                "xai/grok-4-1-fast-reasoning",
256            ],
257            "claude-cli" => vec![
258                "opus",   // Claude Opus 4.5 - smart/reasoning
259                "sonnet", // Claude Sonnet - fast/capable
260                "haiku",  // Claude Haiku - fastest
261            ],
262            "codex" => vec![
263                "gpt-5.2-high", // Smart/reasoning model
264                "gpt-5.1",      // Capable model
265                "gpt-5.1-mini", // Fast model
266                "o3",           // Reasoning model
267                "o3-mini",      // Fast reasoning
268            ],
269            "cursor" => vec![
270                "claude-4-opus",   // Smart/reasoning
271                "claude-4-sonnet", // Balanced
272                "gpt-5",          // OpenAI model
273                "gpt-5.2-high",   // High-capability
274            ],
275            _ => vec![],
276        }
277    }
278
279    /// Get the smart provider (for validation/analysis tasks with large context)
280    pub fn smart_provider(&self) -> &str {
281        &self.llm.smart_provider
282    }
283
284    /// Get the smart model (for validation/analysis tasks with large context)
285    pub fn smart_model(&self) -> &str {
286        &self.llm.smart_model
287    }
288
289    /// Get the fast provider (for generation tasks)
290    pub fn fast_provider(&self) -> &str {
291        &self.llm.fast_provider
292    }
293
294    /// Get the fast model (for generation tasks)
295    pub fn fast_model(&self) -> &str {
296        &self.llm.fast_model
297    }
298}
299
300#[cfg(test)]
301mod tests {
302    use super::*;
303    use tempfile::TempDir;
304
305    #[test]
306    fn test_default_config() {
307        let config = Config::default();
308        // Default provider is xai with xai/grok-code-fast-1 for speed
309        assert_eq!(config.llm.provider, "xai");
310        assert_eq!(config.llm.model, "xai/grok-code-fast-1");
311        // Smart tier uses claude-cli with opus
312        assert_eq!(config.llm.smart_provider, "claude-cli");
313        assert_eq!(config.llm.smart_model, "opus");
314        // Fast tier uses xai with xai/grok-code-fast-1
315        assert_eq!(config.llm.fast_provider, "xai");
316        assert_eq!(config.llm.fast_model, "xai/grok-code-fast-1");
317        assert_eq!(config.llm.max_tokens, 16000);
318    }
319
320    #[test]
321    fn test_model_tiers() {
322        let config = Config::default();
323        assert_eq!(config.smart_provider(), "claude-cli");
324        assert_eq!(config.smart_model(), "opus");
325        assert_eq!(config.fast_provider(), "xai");
326        assert_eq!(config.fast_model(), "xai/grok-code-fast-1");
327    }
328
329    #[test]
330    fn test_api_key_env_vars() {
331        let mut config = Config::default();
332
333        config.llm.provider = "anthropic".to_string();
334        assert_eq!(config.api_key_env_var(), "ANTHROPIC_API_KEY");
335
336        config.llm.provider = "xai".to_string();
337        assert_eq!(config.api_key_env_var(), "XAI_API_KEY");
338
339        config.llm.provider = "openai".to_string();
340        assert_eq!(config.api_key_env_var(), "OPENAI_API_KEY");
341
342        config.llm.provider = "claude-cli".to_string();
343        config.llm.smart_provider = "claude-cli".to_string();
344        config.llm.fast_provider = "claude-cli".to_string();
345        assert!(!config.requires_api_key());
346    }
347
348    #[test]
349    fn test_api_endpoints() {
350        let mut config = Config::default();
351
352        config.llm.provider = "anthropic".to_string();
353        assert_eq!(
354            config.api_endpoint(),
355            "https://api.anthropic.com/v1/messages"
356        );
357
358        config.llm.provider = "xai".to_string();
359        assert_eq!(
360            config.api_endpoint(),
361            "https://api.x.ai/v1/chat/completions"
362        );
363
364        config.llm.provider = "openai".to_string();
365        assert_eq!(
366            config.api_endpoint(),
367            "https://api.openai.com/v1/chat/completions"
368        );
369    }
370
371    #[test]
372    fn test_save_and_load_config() {
373        let temp_dir = TempDir::new().unwrap();
374        let config_path = temp_dir.path().join("config.toml");
375
376        let config = Config {
377            llm: LLMConfig {
378                provider: "claude-cli".to_string(),
379                model: "sonnet".to_string(),
380                smart_provider: "claude-cli".to_string(),
381                smart_model: "opus".to_string(),
382                fast_provider: "xai".to_string(),
383                fast_model: "haiku".to_string(),
384                max_tokens: 8192,
385            },
386            swarm: SwarmConfig::default(),
387        };
388
389        config.save(&config_path).unwrap();
390        assert!(config_path.exists());
391
392        let loaded = Config::load(&config_path).unwrap();
393        assert_eq!(loaded.llm.provider, "claude-cli");
394        assert_eq!(loaded.llm.model, "sonnet");
395        assert_eq!(loaded.llm.smart_provider, "claude-cli");
396        assert_eq!(loaded.llm.smart_model, "opus");
397        assert_eq!(loaded.llm.fast_provider, "xai");
398        assert_eq!(loaded.llm.fast_model, "haiku");
399        assert_eq!(loaded.llm.max_tokens, 8192);
400    }
401
402    #[test]
403    fn test_default_models() {
404        assert_eq!(
405            Config::default_model_for_provider("xai"),
406            "xai/grok-code-fast-1"
407        );
408        assert_eq!(
409            Config::default_model_for_provider("anthropic"),
410            "claude-sonnet-4-5-20250929"
411        );
412        assert_eq!(Config::default_model_for_provider("openai"), "o3-mini");
413        assert_eq!(Config::default_model_for_provider("claude-cli"), "sonnet");
414    }
415
416    #[test]
417    fn test_load_config_without_model_tiers() {
418        // Test backward compatibility - loading a config without smart/fast models
419        let temp_dir = TempDir::new().unwrap();
420        let config_path = temp_dir.path().join("config.toml");
421
422        // Write a config without smart_model and fast_model
423        std::fs::write(
424            &config_path,
425            r#"[llm]
426provider = "xai"
427model = "xai/grok-code-fast-1"
428max_tokens = 4096
429"#,
430        )
431        .unwrap();
432
433        let loaded = Config::load(&config_path).unwrap();
434        assert_eq!(loaded.llm.provider, "xai");
435        assert_eq!(loaded.llm.model, "xai/grok-code-fast-1");
436        // Should use defaults for missing fields
437        assert_eq!(loaded.llm.smart_provider, "claude-cli");
438        assert_eq!(loaded.llm.smart_model, "opus");
439        assert_eq!(loaded.llm.fast_provider, "xai");
440        assert_eq!(loaded.llm.fast_model, "xai/grok-code-fast-1");
441    }
442}