Skip to main content

lean_ctx/tools/
ctx_multi_read.rs

1use crate::core::cache::SessionCache;
2use crate::core::tokens::count_tokens;
3use crate::tools::ctx_read;
4use crate::tools::CrpMode;
5
6pub fn handle(cache: &mut SessionCache, paths: &[String], mode: &str, crp_mode: CrpMode) -> String {
7    let n = paths.len();
8    if n == 0 {
9        return "Read 0 files | 0 tokens saved".to_string();
10    }
11
12    let mut sections: Vec<String> = Vec::with_capacity(n);
13    let mut total_saved: usize = 0;
14    let mut total_original: usize = 0;
15
16    for path in paths {
17        let chunk = ctx_read::handle(cache, path, mode, crp_mode);
18        let original = cache.get(path).map(|e| e.original_tokens).unwrap_or(0);
19        let sent = count_tokens(&chunk);
20        total_original = total_original.saturating_add(original);
21        total_saved = total_saved.saturating_add(original.saturating_sub(sent));
22        sections.push(chunk);
23    }
24
25    let body = sections.join("\n---\n");
26    let summary = format!("Read {n} files | {total_saved} tokens saved");
27    format!("{body}\n---\n{summary}")
28}