Skip to main content

lean_ctx/tools/
ctx_multi_read.rs

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