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 = std::fs::read_to_string(path).map_or(0, |c| count_tokens(&c));
20
21 let ctx = AutoModeContext {
22 path,
23 token_count,
24 task,
25 cache: Some(cache),
26 };
27 auto_mode_resolver::resolve(&ctx).mode
28}
29
30pub fn handle(cache: &mut SessionCache, path: &str, crp_mode: CrpMode) -> String {
31 crate::tools::ctx_read::handle(cache, path, "auto", crp_mode)
32}
33
34pub fn is_code_ext(ext: &str) -> bool {
35 matches!(
36 ext,
37 "rs" | "ts"
38 | "tsx"
39 | "js"
40 | "jsx"
41 | "py"
42 | "go"
43 | "java"
44 | "c"
45 | "cpp"
46 | "cc"
47 | "h"
48 | "hpp"
49 | "rb"
50 | "cs"
51 | "kt"
52 | "swift"
53 | "php"
54 | "zig"
55 | "ex"
56 | "exs"
57 | "scala"
58 | "sc"
59 | "dart"
60 | "sh"
61 | "bash"
62 | "svelte"
63 | "vue"
64 )
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_code_detection() {
73 assert!(is_code_ext("rs"));
74 assert!(is_code_ext("py"));
75 assert!(is_code_ext("tsx"));
76 assert!(!is_code_ext("json"));
77 }
78}