pith 0.1.0

Generate optimized codebase context for LLMs
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! Pith CLI - Generate optimized codebase context for LLMs.

use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;

use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use rayon::prelude::*;
use clap_complete::{generate, Shell};
use glob::Pattern;
use ignore::WalkBuilder;
use pith::codemap::{extract_codemap, ExtractOptions};
use pith::errors::{exit_code, PithError};
use pith::filter::{detect_language, passes_extension_filter, Language};
use pith::output::{format_output, OutputFormat, OutputOptions, SelectedFile};
use pith::tokens::{count_tokens, count_tokens_with_encoding, Encoding};
use pith::tree::{render_tree, RenderOptions};
use pith::walker::{build_tree_with_options, WalkOptions};
use serde::Serialize;

#[derive(Parser)]
#[command(name = "pith")]
#[command(about = "Generate optimized codebase context for LLMs")]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Display file tree with metadata
    Tree {
        /// Root directory to scan
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Hide file sizes and line counts
        #[arg(long)]
        no_metadata: bool,

        /// Include hidden files and directories
        #[arg(long)]
        include_hidden: bool,

        /// Maximum directory depth
        #[arg(long)]
        max_depth: Option<usize>,
    },

    /// Extract API signatures from source files
    Codemap {
        /// Root directory to scan
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Include doc comments
        #[arg(long)]
        include_docs: bool,

        /// Include private items
        #[arg(long)]
        include_private: bool,

        /// Filter to specific language(s)
        #[arg(long, value_delimiter = ',')]
        lang: Vec<LanguageArg>,
    },

    /// Generate full context (tree + codemaps)
    Context {
        /// Root directory to scan
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Include doc comments
        #[arg(long)]
        include_docs: bool,

        /// Include private items
        #[arg(long)]
        include_private: bool,

        /// Select files for full content inclusion
        #[arg(long)]
        select: Vec<String>,

        /// Filter to specific language(s)
        #[arg(long, value_delimiter = ',')]
        lang: Vec<LanguageArg>,
    },

    /// Count tokens for files
    Tokens {
        /// Root directory or file to count
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Token encoding
        #[arg(long, default_value = "cl100k")]
        encoding: EncodingArg,

        /// Show per-file breakdown
        #[arg(long)]
        per_file: bool,
    },

    /// Show supported languages
    Languages {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Generate shell completions
    Completions {
        /// Shell to generate completions for
        #[arg(value_enum)]
        shell: Shell,
    },
}

#[derive(Clone, ValueEnum)]
enum LanguageArg {
    Rust,
    Typescript,
    Tsx,
    Javascript,
    Jsx,
    Python,
    Go,
}

#[derive(Clone, ValueEnum)]
enum EncodingArg {
    Cl100k,
    O200k,
}

impl From<EncodingArg> for Encoding {
    fn from(arg: EncodingArg) -> Self {
        match arg {
            EncodingArg::Cl100k => Encoding::Cl100kBase,
            EncodingArg::O200k => Encoding::O200kBase,
        }
    }
}

impl From<LanguageArg> for Language {
    fn from(arg: LanguageArg) -> Self {
        match arg {
            LanguageArg::Rust => Language::Rust,
            LanguageArg::Typescript => Language::TypeScript,
            LanguageArg::Tsx => Language::Tsx,
            LanguageArg::Javascript => Language::JavaScript,
            LanguageArg::Jsx => Language::Jsx,
            LanguageArg::Python => Language::Python,
            LanguageArg::Go => Language::Go,
        }
    }
}

fn main() {
    let cli = Cli::parse();
    let json_output = json_flag(&cli.command);

    let result = match cli.command {
        Commands::Tree {
            path,
            json,
            no_metadata,
            include_hidden,
            max_depth,
        } => run_tree(path, json, no_metadata, include_hidden, max_depth),
        Commands::Codemap {
            path,
            json,
            include_docs,
            include_private,
            lang,
        } => run_codemap(path, json, include_docs, include_private, lang),
        Commands::Context {
            path,
            json,
            include_docs,
            include_private,
            select,
            lang,
        } => run_context(path, json, include_docs, include_private, select, lang),
        Commands::Tokens {
            path,
            json,
            encoding,
            per_file,
        } => run_tokens(path, json, encoding.into(), per_file),
        Commands::Languages { json } => run_languages(json),
        Commands::Completions { shell } => {
            generate(shell, &mut Cli::command(), "pith", &mut std::io::stdout());
            Ok(())
        }
    };

    if let Err(e) = result {
        if json_output {
            eprintln!(r#"{{"error": "{}"}}"#, e);
        } else {
            eprintln!("error: {}", e);
        }
        std::process::exit(exit_code(&e));
    }
}

fn json_flag(cmd: &Commands) -> bool {
    match cmd {
        Commands::Tree { json, .. } => *json,
        Commands::Codemap { json, .. } => *json,
        Commands::Context { json, .. } => *json,
        Commands::Tokens { json, .. } => *json,
        Commands::Languages { json } => *json,
        Commands::Completions { .. } => false,
    }
}

// --- Languages command ---

#[derive(Serialize)]
struct LanguageInfo {
    name: String,
    extensions: Vec<String>,
}

fn run_languages(json: bool) -> Result<(), PithError> {
    let languages: Vec<LanguageInfo> = Language::all()
        .iter()
        .map(|lang| LanguageInfo {
            name: lang.to_string(),
            extensions: lang.extensions().iter().map(|e| format!(".{}", e)).collect(),
        })
        .collect();

    if json {
        #[derive(Serialize)]
        struct Output {
            languages: Vec<LanguageInfo>,
        }
        let output = Output { languages };
        let json = serde_json::to_string_pretty(&output)
            .map_err(|e| PithError::Io(std::io::Error::other(e.to_string())))?;
        println!("{json}");
    } else {
        println!("Supported languages:");
        for lang in &languages {
            println!("  {:12} {}", lang.name, lang.extensions.join(", "));
        }
    }

    Ok(())
}

// --- Tokens command ---

fn run_tokens(
    path: PathBuf,
    json: bool,
    encoding: Encoding,
    per_file: bool,
) -> Result<(), PithError> {
    if !path.exists() {
        return Err(PithError::PathNotFound(path));
    }

    let mut file_tokens: BTreeMap<PathBuf, usize> = BTreeMap::new();

    if path.is_file() {
        let content = fs::read_to_string(&path)?;
        let count = count_tokens_with_encoding(&content, encoding);
        file_tokens.insert(path.clone(), count);
    } else {
        let walker = WalkBuilder::new(&path)
            .hidden(false)
            .git_ignore(true)
            .build();

        // Collect entries for parallel processing
        let entries: Vec<_> = walker
            .flatten()
            .filter(|e| e.path().is_file())
            .filter(|e| passes_extension_filter(e.path()).is_some())
            .collect();

        // Process in parallel
        file_tokens = entries
            .par_iter()
            .filter_map(|entry| {
                let entry_path = entry.path();
                let content = fs::read_to_string(entry_path).ok()?;
                let count = count_tokens_with_encoding(&content, encoding);
                let relative = entry_path
                    .strip_prefix(&path)
                    .unwrap_or(entry_path)
                    .to_path_buf();
                Some((relative, count))
            })
            .collect();
    }

    let total: usize = file_tokens.values().sum();

    if json {
        #[derive(Serialize)]
        struct Output {
            total: usize,
            encoding: String,
            #[serde(skip_serializing_if = "Option::is_none")]
            files: Option<BTreeMap<String, usize>>,
        }

        let files = if per_file {
            Some(
                file_tokens
                    .into_iter()
                    .map(|(k, v)| (k.display().to_string(), v))
                    .collect(),
            )
        } else {
            None
        };

        let output = Output {
            total,
            encoding: encoding.to_string(),
            files,
        };
        let json = serde_json::to_string_pretty(&output)
            .map_err(|e| PithError::Io(std::io::Error::other(e.to_string())))?;
        println!("{json}");
    } else {
        use std::io::{BufWriter, Write};
        let stdout = std::io::stdout();
        let mut out = BufWriter::new(stdout.lock());
        if per_file {
            for (file, count) in &file_tokens {
                writeln!(out, "{}: {} tokens", file.display(), count).ok();
            }
        }
        writeln!(out, "Total: {} tokens", total).ok();
    }

    Ok(())
}

// --- Tree command ---

fn run_tree(
    path: PathBuf,
    json: bool,
    no_metadata: bool,
    include_hidden: bool,
    max_depth: Option<usize>,
) -> Result<(), PithError> {
    if !path.exists() {
        return Err(PithError::PathNotFound(path));
    }

    let walk_opts = WalkOptions {
        max_depth,
        include_hidden,
        ..Default::default()
    };

    let tree = build_tree_with_options(&path, &walk_opts)
        .map_err(|e| PithError::Io(std::io::Error::other(e.to_string())))?;

    if json {
        // Use serde to serialize the tree
        let json = serde_json::to_string_pretty(&tree_to_json(&tree))
            .map_err(|e| PithError::Io(std::io::Error::other(e.to_string())))?;
        println!("{json}");
    } else {
        let render_opts = RenderOptions {
            show_size: !no_metadata,
            show_lines: !no_metadata,
            show_language: !no_metadata,
            ..Default::default()
        };
        print!("{}", render_tree(&tree, &render_opts));
    }

    Ok(())
}

#[derive(Serialize)]
struct JsonTreeNode {
    name: String,
    path: String,
    kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    extension: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    size: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    lines: Option<usize>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    children: Vec<JsonTreeNode>,
}

fn tree_to_json(node: &pith::tree::FileNode) -> JsonTreeNode {
    use pith::tree::NodeKind;

    let (kind, extension, size, lines) = match &node.kind {
        NodeKind::Directory => ("directory".to_string(), None, None, None),
        NodeKind::File { extension, size, lines } => {
            ("file".to_string(), extension.clone(), Some(*size), *lines)
        }
    };

    JsonTreeNode {
        name: node.name.clone(),
        path: node.path.display().to_string(),
        kind,
        extension,
        size,
        lines,
        children: node.children().iter().map(tree_to_json).collect(),
    }
}

// --- Codemap command ---

fn run_codemap(
    path: PathBuf,
    json: bool,
    include_docs: bool,
    include_private: bool,
    lang_filter: Vec<LanguageArg>,
) -> Result<(), PithError> {
    if !path.exists() {
        return Err(PithError::PathNotFound(path));
    }

    let lang_set: Vec<Language> = lang_filter.into_iter().map(|l| l.into()).collect();

    let extract_opts = ExtractOptions {
        include_docs,
        include_private,
    };

    let mut codemaps = Vec::new();

    let walker = WalkBuilder::new(&path)
        .hidden(false)
        .git_ignore(true)
        .build();

    for entry in walker.flatten() {
        let entry_path = entry.path();
        if !entry_path.is_file() {
            continue;
        }

        let lang = match detect_language(entry_path) {
            Some(l) => l,
            None => continue,
        };

        // Apply language filter if specified
        if !lang_set.is_empty() && !lang_set.contains(&lang) {
            continue;
        }

        let content = match fs::read_to_string(entry_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let codemap = extract_codemap(entry_path, &content, lang, &extract_opts);
        codemaps.push(codemap);
    }

    if codemaps.is_empty() {
        return Err(PithError::NoFilesFound(path));
    }

    let output_opts = OutputOptions {
        format: if json { OutputFormat::Json } else { OutputFormat::Xml },
        include_tree: false,
        include_codemaps: true,
        include_selected_files: false,
        include_summary: true,
        public_only: !include_private,
    };

    let output = format_output(None, &codemaps, &[], &output_opts);
    print!("{}", output);

    Ok(())
}

// --- Context command ---

fn run_context(
    path: PathBuf,
    json: bool,
    include_docs: bool,
    include_private: bool,
    select_patterns: Vec<String>,
    lang_filter: Vec<LanguageArg>,
) -> Result<(), PithError> {
    if !path.exists() {
        return Err(PithError::PathNotFound(path));
    }

    let lang_set: Vec<Language> = lang_filter.into_iter().map(|l| l.into()).collect();

    // Build the file tree
    let tree = build_tree_with_options(&path, &WalkOptions::default())
        .map_err(|e| PithError::Io(std::io::Error::other(e.to_string())))?;

    let extract_opts = ExtractOptions {
        include_docs,
        include_private,
    };

    // Compile glob patterns
    let patterns: Vec<Pattern> = select_patterns
        .iter()
        .filter_map(|p| Pattern::new(p).ok())
        .collect();

    let mut codemaps = Vec::new();
    let mut selected_files = Vec::new();

    let walker = WalkBuilder::new(&path)
        .hidden(false)
        .git_ignore(true)
        .build();

    for entry in walker.flatten() {
        let entry_path = entry.path();
        if !entry_path.is_file() {
            continue;
        }

        let relative = entry_path
            .strip_prefix(&path)
            .unwrap_or(entry_path);
        let relative_str = relative.to_string_lossy();

        // Check if file matches any select pattern
        let is_selected = patterns.iter().any(|p| p.matches(&relative_str));

        // Check language
        let lang = detect_language(entry_path);

        // Read content
        let content = match fs::read_to_string(entry_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        // Extract codemap if it's a supported language
        if let Some(lang) = lang {
            // Apply language filter if specified
            if lang_set.is_empty() || lang_set.contains(&lang) {
                let codemap = extract_codemap(entry_path, &content, lang, &extract_opts);
                codemaps.push(codemap);
            }
        }

        // Add to selected files if it matches patterns
        if is_selected {
            let lines = content.lines().count();
            let tokens = count_tokens(&content);
            selected_files.push(SelectedFile {
                path: entry_path.to_path_buf(),
                content,
                lines,
                tokens,
            });
        }
    }

    if codemaps.is_empty() {
        return Err(PithError::NoFilesFound(path));
    }

    let output_opts = OutputOptions {
        format: if json { OutputFormat::Json } else { OutputFormat::Xml },
        include_tree: true,
        include_codemaps: true,
        include_selected_files: !selected_files.is_empty(),
        include_summary: true,
        public_only: !include_private,
    };

    let output = format_output(Some(&tree), &codemaps, &selected_files, &output_opts);
    print!("{}", output);

    Ok(())
}