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    handle_with_task(cache, paths, mode, crp_mode, None)
8}
9
10pub fn handle_with_task(
11    cache: &mut SessionCache,
12    paths: &[String],
13    mode: &str,
14    crp_mode: CrpMode,
15    task: Option<&str>,
16) -> String {
17    let n = paths.len();
18    if n == 0 {
19        return "Read 0 files | 0 tokens saved".to_string();
20    }
21
22    let mut sections: Vec<String> = Vec::with_capacity(n);
23    let mut total_saved: usize = 0;
24    let mut total_original: usize = 0;
25
26    for path in paths {
27        let chunk = ctx_read::handle_with_task(cache, path, mode, crp_mode, task);
28        let original = cache.get(path).map(|e| e.original_tokens).unwrap_or(0);
29        let sent = count_tokens(&chunk);
30        total_original = total_original.saturating_add(original);
31        total_saved = total_saved.saturating_add(original.saturating_sub(sent));
32        sections.push(chunk);
33    }
34
35    let body = sections.join("\n---\n");
36    let summary = format!("Read {n} files | {total_saved} tokens saved");
37    format!("{body}\n---\n{summary}")
38}