tldr-cli 0.4.0

CLI binary for TLDR code analysis tool
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
//! Similar command - Find similar code fragments
//!
//! Finds code that is semantically similar to a given file or function.
//! Uses dense embeddings to compute similarity scores.

use std::path::PathBuf;

use anyhow::Result;
use clap::Args;

use tldr_core::semantic::{
    BuildOptions, CacheConfig, ChunkGranularity, EmbeddingModel, IndexSearchOptions, SemanticIndex,
};

use crate::output::{OutputFormat, OutputWriter};

/// Find similar code fragments
#[derive(Debug, Args)]
pub struct SimilarArgs {
    /// Source file to find similar code for
    pub file: PathBuf,

    /// Specific function name (optional, searches whole file if not specified)
    #[arg(short = 'F', long)]
    pub function: Option<String>,

    /// Maximum number of results
    #[arg(short = 'n', long, default_value = "5")]
    pub top: usize,

    /// Minimum similarity threshold
    #[arg(short = 't', long, default_value = "0.7")]
    pub threshold: f64,

    /// Path to search for similar code (default: current directory)
    #[arg(short, long, default_value = ".")]
    pub path: PathBuf,

    /// Embedding model: arctic-xs, arctic-s, arctic-m, arctic-m-long, arctic-l
    #[arg(short, long, default_value = "arctic-m")]
    pub model: String,

    /// Include self in results (by default, the query is excluded)
    #[arg(long)]
    pub include_self: bool,

    /// Disable embedding cache
    #[arg(long)]
    pub no_cache: bool,

    /// M16 (med-cleanup-bundle-v1): emit one row per matching chunk
    /// (legacy behavior). The default — when no `--function` is given
    /// and the target is a whole file — aggregates chunk matches per
    /// destination file and ranks by total similarity, since per-chunk
    /// scoring on a 600-LOC file made the user wade through 5 unrelated
    /// 4-9 line helpers.
    #[arg(long)]
    pub by_chunk: bool,
}

impl SimilarArgs {
    /// Run the similar command
    pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
        let writer = OutputWriter::new(format, quiet);

        // Parse model
        let model = parse_model(&self.model)?;

        // Canonicalize file path for matching
        let canonical_file = self
            .file
            .canonicalize()
            .unwrap_or_else(|_| self.file.clone());
        let file_str = canonical_file.display().to_string();

        // Smart search path: if --path is the default "." and the input file is
        // an absolute path, use the file's parent directory to avoid indexing the
        // entire cwd (which may be an enormous repo).
        let effective_path =
            if self.path == std::path::Path::new(".") && canonical_file.is_absolute() {
                canonical_file
                    .parent()
                    .map(|p| p.to_path_buf())
                    .unwrap_or_else(|| self.path.clone())
            } else {
                self.path.clone()
            };

        writer.progress(&format!(
            "Finding code similar to {}{}...",
            self.file.display(),
            self.function
                .as_ref()
                .map(|f| format!("::{}", f))
                .unwrap_or_default()
        ));

        // Build options
        let build_opts = BuildOptions {
            model,
            granularity: ChunkGranularity::Function,
            languages: None,
            show_progress: !quiet,
            use_cache: !self.no_cache,
        };

        // Cache config
        let cache_config = if self.no_cache {
            None
        } else {
            Some(CacheConfig::default())
        };

        // Build index using effective path
        let index = SemanticIndex::build(&effective_path, build_opts, cache_config)?;

        writer.progress(&format!(
            "Searching {} chunks for similar code...",
            index.len()
        ));

        // Search options
        let search_opts = IndexSearchOptions {
            top_k: self.top,
            threshold: self.threshold,
            include_snippet: true,
            snippet_lines: 5,
        };

        // M16 (med-cleanup-bundle-v1): when the user passed a whole
        // file (no `--function`) and did not opt into the legacy
        // per-chunk view via `--by-chunk`, aggregate matches per
        // destination FILE and rank by total similarity. The chunk
        // granularity made `tldr similar lib/application.js` (~600
        // LOC) return five unrelated 4-9 line helpers — useless.
        if self.function.is_none() && !self.by_chunk {
            let report = aggregate_similar_by_file(
                &index,
                &file_str,
                self.top,
                self.threshold,
            )?;
            if writer.is_text() {
                let text = format_aggregated_similar_text(&report);
                writer.write_text(&text)?;
            } else {
                writer.write(&report)?;
            }
            return Ok(());
        }

        // Find similar (legacy per-chunk path: explicit --function or
        // explicit --by-chunk).
        let report = index.find_similar(&file_str, self.function.as_deref(), &search_opts)?;

        // Output based on format
        if writer.is_text() {
            let text = format_similar_text(&report);
            writer.write_text(&text)?;
        } else {
            writer.write(&report)?;
        }

        Ok(())
    }
}

// =============================================================================
// M16 — File-level aggregation
// =============================================================================

/// File-level similarity result. One row per destination file, with
/// total similarity (sum of per-chunk best scores) and chunk count.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FileSimilarityResult {
    pub file_path: std::path::PathBuf,
    /// Sum of per-chunk best similarity scores against the source's chunks.
    pub total_score: f64,
    /// Number of source-chunk to dest-chunk pairs that contributed.
    pub matched_chunks: usize,
    /// Average score across matched_chunks (total_score / matched_chunks).
    pub avg_score: f64,
}

/// Aggregated similarity report keyed by destination file.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AggregatedSimilarityReport {
    pub source_file: std::path::PathBuf,
    pub source_chunks: usize,
    pub model: tldr_core::semantic::EmbeddingModel,
    pub similar_files: Vec<FileSimilarityResult>,
    pub total_compared_chunks: usize,
}

/// Build a file-level aggregation: for every chunk in the source file,
/// find each candidate chunk's similarity, group by destination file,
/// keep only the best contribution per (source_chunk, dest_file) pair,
/// then sum.
fn aggregate_similar_by_file(
    index: &SemanticIndex,
    file_str: &str,
    top: usize,
    threshold: f64,
) -> Result<AggregatedSimilarityReport> {
    use std::collections::HashMap;

    // Source chunks (every chunk whose file_path matches `file_str`).
    let source_chunks: Vec<&tldr_core::semantic::EmbeddedChunk> = index
        .chunks()
        .iter()
        .filter(|c| c.chunk.file_path.to_string_lossy() == file_str)
        .collect();

    if source_chunks.is_empty() {
        return Err(anyhow::anyhow!(
            "no indexed chunks found for source file: {}",
            file_str
        ));
    }

    // (dest_file -> (score, count)) accumulator. For each source chunk
    // and each dest chunk we keep the per-(src_chunk, dest_file) best.
    let mut per_src_dest_best: HashMap<(usize, std::path::PathBuf), f64> = HashMap::new();
    let mut total_compared: usize = 0;

    for (src_idx, src) in source_chunks.iter().enumerate() {
        for dest in index.chunks().iter() {
            // Skip self-file: do not recommend the source's own chunks.
            if dest.chunk.file_path == src.chunk.file_path {
                continue;
            }
            total_compared += 1;
            // Use core's similarity helper to stay consistent with the
            // rest of the semantic stack.
            let score =
                tldr_core::semantic::cosine_similarity(&src.embedding, &dest.embedding);
            if score < threshold {
                continue;
            }
            let key = (src_idx, dest.chunk.file_path.clone());
            let entry = per_src_dest_best.entry(key).or_insert(0.0);
            if score > *entry {
                *entry = score;
            }
        }
    }

    // Now sum per dest_file across source chunks, also count contributors.
    let mut per_file: HashMap<std::path::PathBuf, (f64, usize)> = HashMap::new();
    for ((_src_idx, dest_file), score) in per_src_dest_best {
        let entry = per_file.entry(dest_file).or_insert((0.0, 0));
        entry.0 += score;
        entry.1 += 1;
    }

    let mut similar_files: Vec<FileSimilarityResult> = per_file
        .into_iter()
        .map(|(file_path, (total_score, matched_chunks))| {
            let avg_score = if matched_chunks > 0 {
                total_score / matched_chunks as f64
            } else {
                0.0
            };
            FileSimilarityResult {
                file_path,
                total_score,
                matched_chunks,
                avg_score,
            }
        })
        .collect();

    similar_files.sort_by(|a, b| {
        b.total_score
            .partial_cmp(&a.total_score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    similar_files.truncate(top);

    Ok(AggregatedSimilarityReport {
        source_file: std::path::PathBuf::from(file_str),
        source_chunks: source_chunks.len(),
        model: index.model(),
        similar_files,
        total_compared_chunks: total_compared,
    })
}

/// Format an aggregated (file-level) similarity report.
fn format_aggregated_similar_text(report: &AggregatedSimilarityReport) -> String {
    use colored::Colorize;
    let mut output = String::new();
    output.push_str(&format!(
        "{}: {} ({} source chunks)\n",
        "Finding files similar to".bold(),
        report.source_file.display().to_string().green(),
        report.source_chunks,
    ));
    output.push_str(&format!(
        "Model: {} | Compared: {} chunks\n\n",
        format!("{:?}", report.model).yellow(),
        report.total_compared_chunks,
    ));

    if report.similar_files.is_empty() {
        output.push_str("No similar files found above threshold.\n");
    } else {
        output.push_str(&format!(
            "{} ({} found):\n\n",
            "Similar files".bold(),
            report.similar_files.len()
        ));
        for (i, f) in report.similar_files.iter().enumerate() {
            output.push_str(&format!(
                "{}. {} (total: {:.2}, avg: {:.2}, chunks: {})\n",
                i + 1,
                f.file_path.display().to_string().green(),
                f.total_score,
                f.avg_score,
                f.matched_chunks,
            ));
        }
    }
    output
}

/// Parse model string into EmbeddingModel
fn parse_model(model_str: &str) -> Result<EmbeddingModel> {
    match model_str {
        "arctic-xs" | "xs" => Ok(EmbeddingModel::ArcticXS),
        "arctic-s" | "s" => Ok(EmbeddingModel::ArcticS),
        "arctic-m" | "m" => Ok(EmbeddingModel::ArcticM),
        "arctic-m-long" | "m-long" => Ok(EmbeddingModel::ArcticMLong),
        "arctic-l" | "l" => Ok(EmbeddingModel::ArcticL),
        _ => Err(anyhow::anyhow!(
            "Invalid model '{}'. Options: arctic-xs, arctic-s, arctic-m, arctic-m-long, arctic-l",
            model_str
        )),
    }
}

/// Format similarity report for text output
fn format_similar_text(report: &tldr_core::semantic::SimilarityReport) -> String {
    use colored::Colorize;

    let mut output = String::new();

    // Source info
    let source_name = report.source.function_name.as_deref().unwrap_or("<file>");
    let source_class = report
        .source
        .class_name
        .as_ref()
        .map(|c| format!("{}::", c))
        .unwrap_or_default();

    output.push_str(&format!(
        "{}: {}:{}{}\n",
        "Finding similar to".bold(),
        report.source.file_path.display().to_string().green(),
        source_class,
        source_name.blue()
    ));
    output.push_str(&format!(
        "Model: {} | Compared: {} chunks | Exclude self: {}\n\n",
        format!("{:?}", report.model).yellow(),
        report.total_compared,
        report.exclude_self
    ));

    if report.similar.is_empty() {
        output.push_str("No similar code found above threshold.\n");
    } else {
        output.push_str(&format!(
            "{} ({} found):\n\n",
            "Similar code".bold(),
            report.similar.len()
        ));

        for (i, result) in report.similar.iter().enumerate() {
            let func_name = result.function_name.as_deref().unwrap_or("<file>");
            let class_prefix = result
                .class_name
                .as_ref()
                .map(|c| format!("{}::", c))
                .unwrap_or_default();

            output.push_str(&format!(
                "{}. {}:{}{} (score: {:.2})\n",
                i + 1,
                result.file_path.display().to_string().green(),
                class_prefix,
                func_name.blue(),
                result.score
            ));
            output.push_str(&format!(
                "   Lines {}-{}\n",
                result.line_start, result.line_end
            ));

            if !result.snippet.is_empty() {
                output.push_str(&format!("   {}\n", result.snippet.dimmed()));
            }
            output.push('\n');
        }
    }

    output
}