semantic-diff 0.9.0

A terminal diff viewer with AI-powered semantic grouping (Claude CLI / Copilot)
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
use crate::grouper::llm::LlmBackend;
use crate::theme::ThemeMode;
use serde::Deserialize;
use std::path::PathBuf;

/// User configuration loaded from ~/.config/semantic-diff.json (JSONC supported).
#[derive(Debug, Clone)]
pub struct Config {
    pub preferred_ai_cli: Option<AiCli>,
    pub claude_model: String,
    pub copilot_model: String,
    pub theme_mode: ThemeMode,
}

#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AiCli {
    Claude,
    Copilot,
}

/// Raw JSON-serializable config (matches the file format).
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct RawConfig {
    #[serde(rename = "preferred-ai-cli")]
    preferred_ai_cli: Option<AiCli>,
    claude: CliConfig,
    copilot: CliConfig,
    theme: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct CliConfig {
    model: Option<String>,
}


/// Model tier for intelligent cross-backend mapping.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum ModelTier {
    Fast,     // haiku, gemini-flash
    Balanced, // sonnet, gemini-pro
    Power,    // opus
}

impl Config {
    pub fn default_config() -> Self {
        Self {
            preferred_ai_cli: None,
            claude_model: "haiku".to_string(),
            copilot_model: "gemini-flash".to_string(),
            theme_mode: ThemeMode::Auto,
        }
    }

    /// Resolve the model string to pass to the given backend's CLI.
    pub fn model_for_backend(&self, backend: LlmBackend) -> &str {
        match backend {
            LlmBackend::Claude => &self.claude_model,
            LlmBackend::Copilot => &self.copilot_model,
        }
    }

    /// Detect the best available backend, respecting the user's preference.
    pub fn detect_backend(&self) -> Option<LlmBackend> {
        let claude_ok = which::which("claude").is_ok();
        let copilot_ok = which::which("copilot").is_ok();

        match self.preferred_ai_cli {
            Some(AiCli::Claude) => {
                if claude_ok {
                    Some(LlmBackend::Claude)
                } else if copilot_ok {
                    Some(LlmBackend::Copilot)
                } else {
                    None
                }
            }
            Some(AiCli::Copilot) => {
                if copilot_ok {
                    Some(LlmBackend::Copilot)
                } else if claude_ok {
                    Some(LlmBackend::Claude)
                } else {
                    None
                }
            }
            None => {
                // Default: prefer claude, fallback copilot
                if claude_ok {
                    Some(LlmBackend::Claude)
                } else if copilot_ok {
                    Some(LlmBackend::Copilot)
                } else {
                    None
                }
            }
        }
    }
}

/// Config file path: ~/.config/semantic-diff.json
/// Returns None if home directory cannot be determined (refuses to fall back to cwd).
fn config_path() -> Option<PathBuf> {
    // Explicitly refuse to use cwd as home directory fallback.
    // This prevents a malicious repo from injecting config via .config/semantic-diff.json
    let home = dirs::home_dir()?;
    Some(home.join(".config").join("semantic-diff.json"))
}

/// Default config file content with comments explaining each option.
const DEFAULT_CONFIG: &str = r#"{
  // Which AI CLI to prefer: "claude" or "copilot"
  // Falls back to the other if the preferred one is not installed.
  // If unset, defaults to: claude > copilot
  // "preferred-ai-cli": "claude",

  // Claude CLI settings
  "claude": {
    // Model to use: "haiku" (fast, default), "sonnet" (balanced), "opus" (powerful)
    // Cross-backend models are mapped automatically:
    //   gemini-flash -> haiku, gemini-pro -> sonnet
    "model": "haiku"
  },

  // Copilot CLI settings
  "copilot": {
    // Model to use: "gemini-flash" (fast, default), "sonnet", "opus", "haiku", "gemini-pro"
    "model": "gemini-flash"
  }

  // Theme: "dark", "light", or "auto" (detects from terminal)
  // "theme": "auto"
}
"#;

/// Load config from disk. Creates a default commented config if none exists.
/// Returns default config if home directory cannot be determined.
pub fn load() -> Config {
    let path = match config_path() {
        Some(p) => p,
        None => {
            tracing::warn!("Could not determine home directory, using default config");
            return Config::default_config();
        }
    };

    // Create default config if it doesn't exist
    if !path.exists() {
        if let Some(parent) = path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        let _ = std::fs::write(&path, DEFAULT_CONFIG);
        tracing::info!("Created default config at {}", path.display());
    }

    // Read and parse
    let content = match std::fs::read_to_string(&path) {
        Ok(c) => c,
        Err(e) => {
            tracing::warn!("Failed to read config {}: {}", path.display(), e);
            return Config::default_config();
        }
    };

    let stripped = strip_json_comments(&content);
    let raw: RawConfig = match serde_json::from_str(&stripped) {
        Ok(r) => r,
        Err(e) => {
            tracing::warn!("Failed to parse config {}: {}", path.display(), e);
            return Config::default_config();
        }
    };

    Config {
        preferred_ai_cli: raw.preferred_ai_cli,
        claude_model: resolve_model_for_claude(raw.claude.model.as_deref()),
        copilot_model: resolve_model_for_copilot(raw.copilot.model.as_deref()),
        theme_mode: match raw.theme.as_deref() {
            Some("light") => ThemeMode::Light,
            Some("dark") => ThemeMode::Dark,
            _ => ThemeMode::Auto,
        },
    }
}

/// Map any model name to the closest Claude CLI model.
fn resolve_model_for_claude(model: Option<&str>) -> String {
    let tier = model.map(model_tier).unwrap_or(ModelTier::Fast);
    match tier {
        ModelTier::Fast => "haiku",
        ModelTier::Balanced => "sonnet",
        ModelTier::Power => "opus",
    }
    .to_string()
}

/// Map any model name to the closest Copilot CLI model.
/// Copilot passes model names through directly, but we normalize known aliases.
fn resolve_model_for_copilot(model: Option<&str>) -> String {
    match model {
        Some(m) => {
            let tier = model_tier(m);
            // If it's already a recognized copilot model, pass through
            match m {
                "sonnet" | "opus" | "haiku" | "gemini-flash" | "gemini-pro" => m.to_string(),
                // Otherwise map by tier
                _ => match tier {
                    ModelTier::Fast => "gemini-flash",
                    ModelTier::Balanced => "sonnet",
                    ModelTier::Power => "opus",
                }
                .to_string(),
            }
        }
        None => "gemini-flash".to_string(),
    }
}

/// Classify a model name into a performance tier.
fn model_tier(name: &str) -> ModelTier {
    let n = name.to_lowercase();
    if n.contains("flash") || n.contains("haiku") || n == "gpt-4o-mini" || n.ends_with("-mini") {
        ModelTier::Fast
    } else if n.contains("opus") {
        ModelTier::Power
    } else {
        // sonnet, gemini-pro, gpt-4o, etc. → balanced
        ModelTier::Balanced
    }
}

/// Strip // and /* */ comments from JSON text (simple JSONC support).
fn strip_json_comments(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    let mut chars = input.chars().peekable();
    let mut in_string = false;

    while let Some(c) = chars.next() {
        if in_string {
            out.push(c);
            if c == '\\' {
                // Push escaped char as-is
                if let Some(next) = chars.next() {
                    out.push(next);
                }
            } else if c == '"' {
                in_string = false;
            }
            continue;
        }

        match c {
            '"' => {
                in_string = true;
                out.push(c);
            }
            '/' => match chars.peek() {
                Some('/') => {
                    // Line comment — skip to end of line
                    for rest in chars.by_ref() {
                        if rest == '\n' {
                            out.push('\n');
                            break;
                        }
                    }
                }
                Some('*') => {
                    // Block comment — skip to */
                    chars.next(); // consume *
                    let mut prev = ' ';
                    for rest in chars.by_ref() {
                        if prev == '*' && rest == '/' {
                            break;
                        }
                        if rest == '\n' {
                            out.push('\n');
                        }
                        prev = rest;
                    }
                }
                _ => out.push(c),
            },
            _ => out.push(c),
        }
    }
    out
}

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

    #[test]
    fn test_strip_line_comments() {
        let input = r#"{
  // this is a comment
  "key": "value"
}"#;
        let stripped = strip_json_comments(input);
        let parsed: serde_json::Value = serde_json::from_str(&stripped).unwrap();
        assert_eq!(parsed["key"], "value");
    }

    #[test]
    fn test_strip_block_comments() {
        let input = r#"{ /* block */ "key": "value" }"#;
        let stripped = strip_json_comments(input);
        let parsed: serde_json::Value = serde_json::from_str(&stripped).unwrap();
        assert_eq!(parsed["key"], "value");
    }

    #[test]
    fn test_preserves_strings_with_slashes() {
        let input = r#"{ "url": "https://example.com" }"#;
        let stripped = strip_json_comments(input);
        let parsed: serde_json::Value = serde_json::from_str(&stripped).unwrap();
        assert_eq!(parsed["url"], "https://example.com");
    }

    #[test]
    fn test_commented_out_keys_stripped() {
        let input = r#"{
  // "preferred-ai-cli": "claude",
  "claude": { "model": "opus" }
}"#;
        let stripped = strip_json_comments(input);
        let parsed: serde_json::Value = serde_json::from_str(&stripped).unwrap();
        assert!(parsed.get("preferred-ai-cli").is_none());
        assert_eq!(parsed["claude"]["model"], "opus");
    }

    #[test]
    fn test_model_tier_mapping() {
        assert_eq!(model_tier("haiku"), ModelTier::Fast);
        assert_eq!(model_tier("gemini-flash"), ModelTier::Fast);
        assert_eq!(model_tier("gpt-4o-mini"), ModelTier::Fast);
        assert_eq!(model_tier("sonnet"), ModelTier::Balanced);
        assert_eq!(model_tier("gemini-pro"), ModelTier::Balanced);
        assert_eq!(model_tier("opus"), ModelTier::Power);
    }

    #[test]
    fn test_resolve_claude_model() {
        assert_eq!(resolve_model_for_claude(Some("gemini-flash")), "haiku");
        assert_eq!(resolve_model_for_claude(Some("sonnet")), "sonnet");
        assert_eq!(resolve_model_for_claude(Some("opus")), "opus");
        assert_eq!(resolve_model_for_claude(Some("gemini-pro")), "sonnet");
        assert_eq!(resolve_model_for_claude(None), "haiku");
    }

    #[test]
    fn test_resolve_copilot_model() {
        assert_eq!(resolve_model_for_copilot(Some("gemini-flash")), "gemini-flash");
        assert_eq!(resolve_model_for_copilot(Some("sonnet")), "sonnet");
        assert_eq!(resolve_model_for_copilot(Some("haiku")), "haiku");
        assert_eq!(resolve_model_for_copilot(None), "gemini-flash");
    }

    #[test]
    fn test_default_config_parses() {
        let stripped = strip_json_comments(DEFAULT_CONFIG);
        let raw: RawConfig = serde_json::from_str(&stripped).unwrap();
        assert!(raw.preferred_ai_cli.is_none());
        assert_eq!(raw.claude.model.as_deref(), Some("haiku"));
        assert_eq!(raw.copilot.model.as_deref(), Some("gemini-flash"));
    }

    #[test]
    fn test_config_path_returns_option_not_cwd() {
        // config_path() should return Some with a path under home, never "."
        let path = config_path();
        match path {
            Some(p) => {
                let path_str = p.to_string_lossy();
                assert!(
                    !path_str.starts_with("./"),
                    "config_path should not fall back to cwd, got: {}",
                    path_str
                );
                assert!(
                    path_str.contains(".config/semantic-diff.json"),
                    "config_path should end with .config/semantic-diff.json, got: {}",
                    path_str
                );
            }
            None => {
                // None is acceptable if HOME is not set
            }
        }
    }

    #[test]
    fn test_config_path_no_dot_fallback() {
        // Verify config_path never returns a path starting with "."
        let path = config_path();
        if let Some(p) = path {
            assert_ne!(
                p.components().next().map(|c| c.as_os_str().to_string_lossy().to_string()),
                Some(".".to_string()),
                "config_path must not use '.' as base directory"
            );
        }
    }
}