modelsdev 0.11.4

A fast TUI and CLI for browsing AI models, benchmarks, and coding agents
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
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::PathBuf;

/// Routing discriminant for symlink aliases -- not a config field, not serde-derived.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AliasKind {
    Agents,
    Benchmarks,
    Status,
}

fn default_agents_alias() -> String {
    "agents".to_string()
}

fn default_benchmarks_alias() -> String {
    "benchmarks".to_string()
}

fn default_status_alias() -> String {
    "mstatus".to_string()
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AliasesConfig {
    #[serde(default = "default_agents_alias")]
    pub agents: String,
    #[serde(default = "default_benchmarks_alias")]
    pub benchmarks: String,
    #[serde(default = "default_status_alias")]
    pub status: String,
}

impl Default for AliasesConfig {
    fn default() -> Self {
        Self {
            agents: default_agents_alias(),
            benchmarks: default_benchmarks_alias(),
            status: default_status_alias(),
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct Config {
    #[serde(default)]
    pub config_version: u32,
    #[serde(default)]
    pub agents: AgentsConfig,
    #[serde(default)]
    pub cache: CacheConfig,
    #[serde(default)]
    pub display: DisplayConfig,
    #[serde(default)]
    pub status: StatusConfig,
    #[serde(default)]
    pub aliases: AliasesConfig,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CustomAgent {
    pub name: String,
    pub repo: String,
    #[serde(default)]
    pub agent_type: Option<String>, // "cli" or "ide"
    #[serde(default)]
    pub binary: Option<String>,
    #[serde(default)]
    pub version_command: Option<Vec<String>>,
}

impl CustomAgent {
    pub fn to_agent(&self) -> crate::agents::Agent {
        crate::agents::Agent {
            name: self.name.clone(),
            repo: self.repo.clone(),
            categories: self
                .agent_type
                .as_ref()
                .map(|t| vec![t.clone()])
                .unwrap_or_default(),
            cli_binary: self.binary.clone(),
            alt_binaries: vec![],
            version_command: self.version_command.clone().unwrap_or_default(),
            installation_method: self.agent_type.clone(),
            pricing: None,
            supported_providers: vec![],
            platform_support: vec![],
            open_source: true,
            version_regex: None,
            config_files: vec![],
            homepage: None,
            docs: None,
        }
    }
}

/// Default starter agents for new users
fn default_tracked_agents() -> HashSet<String> {
    ["claude-code", "codex", "gemini-cli", "opencode"]
        .iter()
        .map(|s| s.to_string())
        .collect()
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AgentsConfig {
    #[serde(default = "default_tracked_agents")]
    pub tracked: HashSet<String>,
    #[serde(default)]
    pub excluded: HashSet<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub custom: Vec<CustomAgent>,
}

impl Default for AgentsConfig {
    fn default() -> Self {
        Self {
            tracked: default_tracked_agents(),
            excluded: HashSet::new(),
            custom: Vec::new(),
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CacheConfig {
    #[serde(default = "default_github_ttl")]
    pub github_ttl_seconds: u64,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            github_ttl_seconds: default_github_ttl(),
        }
    }
}

fn default_github_ttl() -> u64 {
    3600
}

/// Default: all status providers tracked.
fn default_tracked_providers() -> HashSet<String> {
    crate::status::STATUS_REGISTRY
        .iter()
        .map(|e| e.slug.to_string())
        .collect()
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StatusConfig {
    #[serde(default = "default_tracked_providers")]
    pub tracked: HashSet<String>,
}

impl Default for StatusConfig {
    fn default() -> Self {
        Self {
            tracked: default_tracked_providers(),
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct DisplayConfig {
    #[serde(default)]
    pub default_tab: Option<String>,
}

impl Config {
    pub fn config_path() -> Option<PathBuf> {
        dirs::config_dir().map(|p| p.join("models").join("config.toml"))
    }

    pub fn load() -> Result<Self> {
        let path = match Self::config_path() {
            Some(p) => p,
            None => return Ok(Self::default()),
        };

        if !path.exists() {
            return Ok(Self::default());
        }

        let content = std::fs::read_to_string(&path)
            .with_context(|| format!("Failed to read config: {}", path.display()))?;

        // Strip legacy `custom = []` lines that conflict with [[agents.custom]] blocks.
        // Older versions of save() serialized the empty Vec as an inline array, which
        // causes a TOML parse error if the user later adds [[agents.custom]] entries.
        let content: String = content
            .lines()
            .filter(|line| line.trim() != "custom = []")
            .collect::<Vec<_>>()
            .join("\n");

        toml::from_str(&content).context("Failed to parse config.toml")
    }

    pub fn save(&self) -> Result<()> {
        let path = match Self::config_path() {
            Some(p) => p,
            None => anyhow::bail!("Could not determine config directory"),
        };

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

        let mut content = toml::to_string_pretty(self).context("Failed to serialize config")?;

        // Append a commented example for custom agents when none are configured,
        // so users know the syntax without needing to look up docs.
        if self.agents.custom.is_empty() {
            content.push_str(
                "\n# To track custom agents, add [[agents.custom]] blocks:\n\
                 #\n\
                 # [[agents.custom]]\n\
                 # name = \"My Agent\"\n\
                 # repo = \"owner/repo\"\n\
                 # agent_type = \"cli\"       # optional: \"cli\" or \"ide\"\n\
                 # binary = \"myagent\"       # optional: for version detection\n\
                 # version_command = [\"myagent\", \"--version\"]  # optional\n\
                 #\n\
                 # Add multiple agents with additional [[agents.custom]] blocks.\n\
                 # See: https://github.com/arimxyer/models/wiki/Configuration#custom-agents\n",
            );
        }

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

        Ok(())
    }

    pub fn is_tracked(&self, agent_id: &str) -> bool {
        if self.agents.excluded.contains(agent_id) {
            return false;
        }
        self.agents.tracked.contains(agent_id)
    }

    pub fn set_tracked(&mut self, agent_id: &str, tracked: bool) {
        if tracked {
            self.agents.tracked.insert(agent_id.to_string());
            self.agents.excluded.remove(agent_id);
        } else {
            self.agents.tracked.remove(agent_id);
            self.agents.excluded.insert(agent_id.to_string());
        }
    }

    /// Returns the list of (alias_name, alias_kind) tuples for symlink operations.
    pub fn alias_names(&self) -> Vec<(&str, AliasKind)> {
        vec![
            (&self.aliases.agents, AliasKind::Agents),
            (&self.aliases.benchmarks, AliasKind::Benchmarks),
            (&self.aliases.status, AliasKind::Status),
        ]
    }

    /// Given a binary name (from argv[0]), returns which alias it matches, if any.
    pub fn match_alias(&self, binary_name: &str) -> Option<AliasKind> {
        if binary_name == self.aliases.agents {
            return Some(AliasKind::Agents);
        }
        if binary_name == self.aliases.benchmarks {
            return Some(AliasKind::Benchmarks);
        }
        if binary_name == self.aliases.status {
            return Some(AliasKind::Status);
        }
        None
    }

    pub fn is_status_tracked(&self, slug: &str) -> bool {
        self.status.tracked.contains(slug)
    }

    pub fn set_status_tracked(&mut self, slug: &str, tracked: bool) {
        if tracked {
            self.status.tracked.insert(slug.to_string());
        } else {
            self.status.tracked.remove(slug);
        }
    }
}

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

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert_eq!(config.cache.github_ttl_seconds, 3600);
        // Default includes starter agents
        assert_eq!(config.agents.tracked.len(), 4);
        assert!(config.agents.tracked.contains("claude-code"));
        assert!(config.agents.tracked.contains("codex"));
        assert!(config.agents.tracked.contains("gemini-cli"));
        assert!(config.agents.tracked.contains("opencode"));
    }

    #[test]
    fn test_is_tracked_default() {
        let config = Config::default();
        // Default tracked agents
        assert!(config.is_tracked("claude-code"));
        assert!(config.is_tracked("codex"));
        // Not in default list
        assert!(!config.is_tracked("cursor"));
    }

    #[test]
    fn test_is_tracked_excluded() {
        let mut config = Config::default();
        config.agents.excluded.insert("claude-code".to_string());
        // Excluded even though in tracked list
        assert!(!config.is_tracked("claude-code"));
        // Still tracked
        assert!(config.is_tracked("codex"));
    }

    #[test]
    fn test_status_default_tracks_all_providers() {
        use crate::status::STATUS_REGISTRY;
        let config = Config::default();
        assert_eq!(config.status.tracked.len(), STATUS_REGISTRY.len());
        for entry in STATUS_REGISTRY {
            assert!(config.is_status_tracked(entry.slug));
        }
    }

    #[test]
    fn test_set_status_tracked() {
        let mut config = Config::default();
        // Untrack a provider
        config.set_status_tracked("openai", false);
        assert!(!config.is_status_tracked("openai"));
        // Re-track it
        config.set_status_tracked("openai", true);
        assert!(config.is_status_tracked("openai"));
    }

    #[test]
    fn test_default_aliases() {
        let config = Config::default();
        assert_eq!(config.aliases.agents, "agents");
        assert_eq!(config.aliases.benchmarks, "benchmarks");
        assert_eq!(config.aliases.status, "mstatus");
    }

    #[test]
    fn test_match_alias() {
        let config = Config::default();
        assert_eq!(config.match_alias("agents"), Some(AliasKind::Agents));
        assert_eq!(
            config.match_alias("benchmarks"),
            Some(AliasKind::Benchmarks)
        );
        assert_eq!(config.match_alias("mstatus"), Some(AliasKind::Status));
        assert_eq!(config.match_alias("models"), None);
        assert_eq!(config.match_alias("status"), None);
        assert_eq!(config.match_alias(""), None);
    }

    #[test]
    fn test_alias_names_returns_all_three() {
        let config = Config::default();
        let names = config.alias_names();
        assert_eq!(names.len(), 3);
        assert_eq!(names[0], ("agents", AliasKind::Agents));
        assert_eq!(names[1], ("benchmarks", AliasKind::Benchmarks));
        assert_eq!(names[2], ("mstatus", AliasKind::Status));
    }

    #[test]
    fn test_aliases_config_deserializes_with_defaults_when_section_absent() {
        let toml = r#"
config_version = 1
"#;
        let config: Config = toml::from_str(toml).expect("should parse");
        assert_eq!(config.aliases.agents, "agents");
        assert_eq!(config.aliases.benchmarks, "benchmarks");
        assert_eq!(config.aliases.status, "mstatus");
    }

    #[test]
    fn test_aliases_config_custom_values() {
        let toml = r#"
[aliases]
agents = "myagents"
benchmarks = "bench"
status = "mystatus"
"#;
        let config: Config = toml::from_str(toml).expect("should parse");
        assert_eq!(config.aliases.agents, "myagents");
        assert_eq!(config.aliases.benchmarks, "bench");
        assert_eq!(config.aliases.status, "mystatus");
        assert_eq!(config.match_alias("myagents"), Some(AliasKind::Agents));
        assert_eq!(config.match_alias("mystatus"), Some(AliasKind::Status));
        assert_eq!(config.match_alias("agents"), None);
    }
}