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 effective_mode = if ctx_read::is_instruction_file(path) {
29            "full"
30        } else {
31            mode
32        };
33        let chunk = ctx_read::handle_with_task(cache, path, effective_mode, crp_mode, task);
34        let original = cache.get(path).map_or(0, |e| e.original_tokens);
35        let sent = count_tokens(&chunk);
36        heatmap::record_file_access(path, original, original.saturating_sub(sent));
37        total_original = total_original.saturating_add(original);
38        total_saved = total_saved.saturating_add(original.saturating_sub(sent));
39        sections.push(chunk);
40    }
41
42    let body = sections.join("\n---\n");
43    let summary = format!("Read {n} files | {total_saved} tokens saved");
44    format!("{body}\n---\n{summary}")
45}