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    handle_with_task_fresh(cache, paths, mode, false, crp_mode, task)
19}
20
21pub fn handle_with_task_fresh(
22    cache: &mut SessionCache,
23    paths: &[String],
24    mode: &str,
25    fresh: bool,
26    crp_mode: CrpMode,
27    task: Option<&str>,
28) -> String {
29    let n = paths.len();
30    if n == 0 {
31        return "Read 0 files | 0 tokens saved".to_string();
32    }
33
34    let mut sections: Vec<String> = Vec::with_capacity(n);
35    let mut total_saved: usize = 0;
36    let mut total_original: usize = 0;
37
38    for path in paths {
39        let effective_mode = if ctx_read::is_instruction_file(path) {
40            "full"
41        } else {
42            mode
43        };
44        let chunk = if fresh {
45            ctx_read::handle_fresh_with_task(cache, path, effective_mode, crp_mode, task)
46        } else {
47            ctx_read::handle_with_task(cache, path, effective_mode, crp_mode, task)
48        };
49        let original = cache.get(path).map_or(0, |e| e.original_tokens);
50        let sent = count_tokens(&chunk);
51        heatmap::record_file_access(path, original, original.saturating_sub(sent));
52        total_original = total_original.saturating_add(original);
53        total_saved = total_saved.saturating_add(original.saturating_sub(sent));
54        sections.push(chunk);
55    }
56
57    let body = sections.join("\n---\n");
58    let summary = format!("Read {n} files | {total_saved} tokens saved");
59    format!("{body}\n---\n{summary}")
60}