scud-cli 1.67.0

Fast, simple task master for AI-driven development
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    pub llm: LLMConfig,
    #[serde(default)]
    pub swarm: SwarmConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwarmConfig {
    #[serde(default = "default_swarm_harness")]
    pub harness: String,
    /// Default model for swarm agents (e.g., "xai/grok-code-fast-1", "opus").
    /// When unset, inherits from [llm].model.
    #[serde(default)]
    pub model: Option<String>,
    #[serde(default = "default_round_size")]
    pub round_size: usize,
    #[serde(default = "default_default_tag")]
    pub default_tag: Option<String>,
    /// Use direct API instead of CLI harnesses.
    /// Requires `direct-api` Cargo feature.
    #[serde(default)]
    pub use_direct_api: bool,
    /// Provider for direct API mode: anthropic, openai, xai, openrouter, opencode-zen
    #[serde(default = "default_direct_api_provider")]
    pub direct_api_provider: String,
}

fn default_swarm_harness() -> String {
    "rho".to_string()
}

fn default_round_size() -> usize {
    5
}

fn default_default_tag() -> Option<String> {
    None
}

fn default_direct_api_provider() -> String {
    std::env::var("SCUD_DIRECT_API_PROVIDER").unwrap_or_else(|_| "anthropic".to_string())
}

impl Default for SwarmConfig {
    fn default() -> Self {
        SwarmConfig {
            harness: default_swarm_harness(),
            model: std::env::var("SCUD_SWARM_MODEL").ok(),
            round_size: default_round_size(),
            default_tag: default_default_tag(),
            use_direct_api: false,
            direct_api_provider: default_direct_api_provider(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LLMConfig {
    /// Default provider
    #[serde(default = "default_provider")]
    pub provider: String,
    /// Default model (used when no tier specified)
    #[serde(default = "default_model")]
    pub model: String,
    /// Smart provider for validation/analysis tasks
    #[serde(default = "default_smart_provider")]
    pub smart_provider: String,
    /// Smart model for validation/analysis tasks (large context)
    #[serde(default = "default_smart_model")]
    pub smart_model: String,
    /// Fast provider for generation tasks
    #[serde(default = "default_fast_provider")]
    pub fast_provider: String,
    /// Fast model for generation tasks
    #[serde(default = "default_fast_model")]
    pub fast_model: String,
    /// Max tokens for LLM requests
    #[serde(default = "default_max_tokens")]
    pub max_tokens: u32,
}

fn default_provider() -> String {
    std::env::var("SCUD_PROVIDER").unwrap_or_else(|_| "xai".to_string())
}

fn default_model() -> String {
    std::env::var("SCUD_MODEL").unwrap_or_else(|_| "xai/grok-code-fast-1".to_string())
}

fn default_smart_provider() -> String {
    std::env::var("SCUD_SMART_PROVIDER").unwrap_or_else(|_| "claude-cli".to_string())
}

fn default_smart_model() -> String {
    std::env::var("SCUD_SMART_MODEL").unwrap_or_else(|_| "opus".to_string())
}

fn default_fast_provider() -> String {
    std::env::var("SCUD_FAST_PROVIDER").unwrap_or_else(|_| "xai".to_string())
}

fn default_fast_model() -> String {
    std::env::var("SCUD_FAST_MODEL").unwrap_or_else(|_| "xai/grok-code-fast-1".to_string())
}

fn default_max_tokens() -> u32 {
    std::env::var("SCUD_MAX_TOKENS")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(16000)
}

impl Default for Config {
    fn default() -> Self {
        Config {
            llm: LLMConfig {
                provider: default_provider(),
                model: default_model(),
                smart_provider: default_smart_provider(),
                smart_model: default_smart_model(),
                fast_provider: default_fast_provider(),
                fast_model: default_fast_model(),
                max_tokens: default_max_tokens(),
            },
            swarm: SwarmConfig::default(),
        }
    }
}

impl Config {
    /// Resolve the swarm model: swarm.model > llm.model
    pub fn swarm_model(&self) -> &str {
        self.swarm.model.as_deref().unwrap_or(&self.llm.model)
    }

    pub fn load(path: &Path) -> Result<Self> {
        let content = fs::read_to_string(path)
            .with_context(|| format!("Failed to read config file: {}", path.display()))?;

        toml::from_str(&content)
            .with_context(|| format!("Failed to parse config file: {}", path.display()))
    }

    pub fn save(&self, path: &Path) -> Result<()> {
        let content = toml::to_string_pretty(self).context("Failed to serialize config to TOML")?;

        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create config directory: {}", parent.display())
            })?;
        }

        fs::write(path, content)
            .with_context(|| format!("Failed to write config file: {}", path.display()))
    }

    pub fn api_key_env_var(&self) -> &str {
        Self::api_key_env_var_for_provider(&self.llm.provider)
    }

    pub fn api_key_env_var_for_provider(provider: &str) -> &str {
        match provider {
            "anthropic" => "ANTHROPIC_API_KEY",
            "anthropic-oauth" => "NONE", // Uses Claude Code OAuth from Keychain
            "xai" => "XAI_API_KEY",
            "openai" => "OPENAI_API_KEY",
            "openrouter" => "OPENROUTER_API_KEY",
            "opencode-zen" | "opencode" | "zen" => "OPENCODE_API_KEY",
            "claude-cli" => "NONE", // Claude CLI doesn't need API key
            "codex" => "NONE",      // Codex CLI doesn't need API key
            "cursor" => "NONE",     // Cursor Agent CLI doesn't need API key
            _ => "API_KEY",
        }
    }

    pub fn requires_api_key(&self) -> bool {
        let providers = [
            &self.llm.provider,
            &self.llm.smart_provider,
            &self.llm.fast_provider,
        ];
        providers.iter().any(|p| {
            !matches!(
                p.as_str(),
                "claude-cli" | "codex" | "cursor" | "anthropic-oauth"
            )
        })
    }

    pub fn api_endpoint(&self) -> &str {
        match self.llm.provider.as_str() {
            "anthropic" => "https://api.anthropic.com/v1/messages",
            "xai" => "https://api.x.ai/v1/chat/completions",
            "openai" => "https://api.openai.com/v1/chat/completions",
            "openrouter" => "https://openrouter.ai/api/v1/chat/completions",
            _ => "https://api.anthropic.com/v1/messages",
        }
    }

    pub fn default_model_for_provider(provider: &str) -> &str {
        match provider {
            "xai" => "xai/grok-code-fast-1",
            "anthropic" => "claude-sonnet-4-5-20250929",
            "anthropic-oauth" => "claude-opus-4-6",
            "openai" => "o3-mini",
            "openrouter" => "anthropic/claude-sonnet-4.5",
            "claude-cli" => "sonnet", // Claude CLI model names: sonnet, opus, haiku
            "codex" => "gpt-5.1",     // Codex CLI default model
            "cursor" => "claude-4-sonnet", // Cursor Agent default model
            _ => "xai/grok-code-fast-1",
        }
    }

    /// Get suggested models for a provider (for display in init)
    pub fn suggested_models_for_provider(provider: &str) -> Vec<&str> {
        match provider {
            "xai" => vec![
                "xai/grok-code-fast-1",
                "xai/grok-4-1-fast",
                "xai/grok-4.20-experimental-beta-0304-reasoning",
                "xai/grok-4.20-experimental-beta-0304-non-reasoning",
                "xai/grok-4.20-multi-agent-experimental-beta-0304",
                "xai/grok-4-fast",
                "xai/grok-3-fast",
            ],
            "anthropic" => vec![
                "claude-sonnet-4-5-20250929",
                "claude-opus-4-5-20251101",
                "claude-haiku-4-5-20251001",
                "claude-opus-4-1-20250805",
            ],
            "anthropic-oauth" => vec![
                "claude-opus-4-6",
                "claude-sonnet-4-5-20250929",
                "claude-opus-4-5-20251101",
                "claude-haiku-4-5-20251001",
            ],
            "openai" => vec![
                "gpt-5.2-high",
                "gpt-5.1",
                "gpt-5.1-mini",
                "o3-mini",
                "o3",
                "o4-mini",
                "gpt-4.1",
            ],
            "openrouter" => vec![
                "anthropic/claude-sonnet-4.5",
                "anthropic/claude-opus-4.5",
                "openai/o3-mini",
                "openai/gpt-4.1",
                "xai/grok-4-1-fast-reasoning",
            ],
            "claude-cli" => vec![
                "opus",   // Claude Opus 4.5 - smart/reasoning
                "sonnet", // Claude Sonnet - fast/capable
                "haiku",  // Claude Haiku - fastest
            ],
            "codex" => vec![
                "gpt-5.2-high", // Smart/reasoning model
                "gpt-5.1",      // Capable model
                "gpt-5.1-mini", // Fast model
                "o3",           // Reasoning model
                "o3-mini",      // Fast reasoning
            ],
            "cursor" => vec![
                "claude-4-opus",   // Smart/reasoning
                "claude-4-sonnet", // Balanced
                "gpt-5",           // OpenAI model
                "gpt-5.2-high",    // High-capability
            ],
            _ => vec![],
        }
    }

    /// Get the smart provider (for validation/analysis tasks with large context)
    pub fn smart_provider(&self) -> &str {
        &self.llm.smart_provider
    }

    /// Get the smart model (for validation/analysis tasks with large context)
    pub fn smart_model(&self) -> &str {
        &self.llm.smart_model
    }

    /// Get the fast provider (for generation tasks)
    pub fn fast_provider(&self) -> &str {
        &self.llm.fast_provider
    }

    /// Get the fast model (for generation tasks)
    pub fn fast_model(&self) -> &str {
        &self.llm.fast_model
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_default_config() {
        let config = Config::default();
        // Default provider is xai with xai/grok-code-fast-1 for speed
        assert_eq!(config.llm.provider, "xai");
        assert_eq!(config.llm.model, "xai/grok-code-fast-1");
        // Smart tier uses claude-cli with opus
        assert_eq!(config.llm.smart_provider, "claude-cli");
        assert_eq!(config.llm.smart_model, "opus");
        // Fast tier uses xai with xai/grok-code-fast-1
        assert_eq!(config.llm.fast_provider, "xai");
        assert_eq!(config.llm.fast_model, "xai/grok-code-fast-1");
        assert_eq!(config.llm.max_tokens, 16000);
    }

    #[test]
    fn test_model_tiers() {
        let config = Config::default();
        assert_eq!(config.smart_provider(), "claude-cli");
        assert_eq!(config.smart_model(), "opus");
        assert_eq!(config.fast_provider(), "xai");
        assert_eq!(config.fast_model(), "xai/grok-code-fast-1");
    }

    #[test]
    fn test_api_key_env_vars() {
        let mut config = Config::default();

        config.llm.provider = "anthropic".to_string();
        assert_eq!(config.api_key_env_var(), "ANTHROPIC_API_KEY");

        config.llm.provider = "xai".to_string();
        assert_eq!(config.api_key_env_var(), "XAI_API_KEY");

        config.llm.provider = "openai".to_string();
        assert_eq!(config.api_key_env_var(), "OPENAI_API_KEY");

        config.llm.provider = "claude-cli".to_string();
        config.llm.smart_provider = "claude-cli".to_string();
        config.llm.fast_provider = "claude-cli".to_string();
        assert!(!config.requires_api_key());
    }

    #[test]
    fn test_api_endpoints() {
        let mut config = Config::default();

        config.llm.provider = "anthropic".to_string();
        assert_eq!(
            config.api_endpoint(),
            "https://api.anthropic.com/v1/messages"
        );

        config.llm.provider = "xai".to_string();
        assert_eq!(
            config.api_endpoint(),
            "https://api.x.ai/v1/chat/completions"
        );

        config.llm.provider = "openai".to_string();
        assert_eq!(
            config.api_endpoint(),
            "https://api.openai.com/v1/chat/completions"
        );
    }

    #[test]
    fn test_save_and_load_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        let config = Config {
            llm: LLMConfig {
                provider: "claude-cli".to_string(),
                model: "sonnet".to_string(),
                smart_provider: "claude-cli".to_string(),
                smart_model: "opus".to_string(),
                fast_provider: "xai".to_string(),
                fast_model: "haiku".to_string(),
                max_tokens: 8192,
            },
            swarm: SwarmConfig::default(),
        };

        config.save(&config_path).unwrap();
        assert!(config_path.exists());

        let loaded = Config::load(&config_path).unwrap();
        assert_eq!(loaded.llm.provider, "claude-cli");
        assert_eq!(loaded.llm.model, "sonnet");
        assert_eq!(loaded.llm.smart_provider, "claude-cli");
        assert_eq!(loaded.llm.smart_model, "opus");
        assert_eq!(loaded.llm.fast_provider, "xai");
        assert_eq!(loaded.llm.fast_model, "haiku");
        assert_eq!(loaded.llm.max_tokens, 8192);
    }

    #[test]
    fn test_default_models() {
        assert_eq!(
            Config::default_model_for_provider("xai"),
            "xai/grok-code-fast-1"
        );
        assert_eq!(
            Config::default_model_for_provider("anthropic"),
            "claude-sonnet-4-5-20250929"
        );
        assert_eq!(Config::default_model_for_provider("openai"), "o3-mini");
        assert_eq!(Config::default_model_for_provider("claude-cli"), "sonnet");
    }

    #[test]
    fn test_load_config_without_model_tiers() {
        // Test backward compatibility - loading a config without smart/fast models
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        // Write a config without smart_model and fast_model
        std::fs::write(
            &config_path,
            r#"[llm]
provider = "xai"
model = "xai/grok-code-fast-1"
max_tokens = 4096
"#,
        )
        .unwrap();

        let loaded = Config::load(&config_path).unwrap();
        assert_eq!(loaded.llm.provider, "xai");
        assert_eq!(loaded.llm.model, "xai/grok-code-fast-1");
        // Should use defaults for missing fields
        assert_eq!(loaded.llm.smart_provider, "claude-cli");
        assert_eq!(loaded.llm.smart_model, "opus");
        assert_eq!(loaded.llm.fast_provider, "xai");
        assert_eq!(loaded.llm.fast_model, "xai/grok-code-fast-1");
    }
}