lean_ctx/tools/
ctx_smart_read.rs1use crate::core::auto_mode_resolver::{self, AutoModeContext};
2use crate::core::cache::SessionCache;
3use crate::core::tokens::count_tokens;
4use crate::tools::CrpMode;
5
6pub fn select_mode(cache: &SessionCache, path: &str) -> String {
7 select_mode_with_task(cache, path, None)
8}
9
10pub fn select_mode_with_task(cache: &SessionCache, path: &str, task: Option<&str>) -> String {
12 if let Ok(meta) = std::fs::metadata(path) {
13 let cap = crate::core::limits::max_read_bytes() as u64;
14 if meta.len() > cap {
15 return "full".to_string();
16 }
17 }
18
19 let token_count = cache
24 .get(path)
25 .filter(|e| !crate::core::cache::is_cache_entry_stale(path, e.stored_mtime))
26 .map(|e| e.original_tokens)
27 .filter(|t| *t > 0)
28 .unwrap_or_else(|| std::fs::read_to_string(path).map_or(0, |c| count_tokens(&c)));
29
30 let ctx = AutoModeContext {
31 path,
32 token_count,
33 task,
34 cache: Some(cache),
35 };
36 auto_mode_resolver::resolve(&ctx).mode
37}
38
39pub fn handle(cache: &mut SessionCache, path: &str, crp_mode: CrpMode) -> String {
40 crate::tools::ctx_read::handle(cache, path, "auto", crp_mode)
41}
42
43pub fn is_code_ext(ext: &str) -> bool {
44 matches!(
45 ext,
46 "rs" | "ts"
47 | "tsx"
48 | "js"
49 | "jsx"
50 | "py"
51 | "go"
52 | "java"
53 | "c"
54 | "cpp"
55 | "cc"
56 | "h"
57 | "hpp"
58 | "rb"
59 | "cs"
60 | "kt"
61 | "swift"
62 | "php"
63 | "zig"
64 | "ex"
65 | "exs"
66 | "scala"
67 | "sc"
68 | "dart"
69 | "sh"
70 | "bash"
71 | "svelte"
72 | "vue"
73 )
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn test_code_detection() {
82 assert!(is_code_ext("rs"));
83 assert!(is_code_ext("py"));
84 assert!(is_code_ext("tsx"));
85 assert!(!is_code_ext("json"));
86 }
87}