lean_ctx/tools/
ctx_smart_read.rs1use crate::core::cache::SessionCache;
2use crate::core::mode_predictor::{FileSignature, ModePredictor};
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 {
11 let content = match std::fs::read_to_string(path) {
12 Ok(c) => c,
13 Err(_) => return "full".to_string(),
14 };
15
16 let token_count = count_tokens(&content);
17 let ext = std::path::Path::new(path)
18 .extension()
19 .and_then(|e| e.to_str())
20 .unwrap_or("");
21
22 if cache.get(path).is_some() {
23 let cached = cache.get(path).unwrap();
24 if cached.hash == compute_hash(&content) {
25 return "full".to_string();
26 }
27 return "diff".to_string();
28 }
29
30 if token_count <= 200 {
31 return "full".to_string();
32 }
33
34 if is_config_or_data(ext, path) {
35 return "full".to_string();
36 }
37
38 let sig = FileSignature::from_path(path, token_count);
42 let predictor = ModePredictor::new();
43 if let Some(predicted) = predictor.predict_best_mode(&sig) {
44 return predicted;
45 }
46
47 heuristic_mode(ext, token_count)
48}
49
50fn heuristic_mode(ext: &str, token_count: usize) -> String {
51 if token_count > 8000 {
52 if is_code(ext) {
53 return "map".to_string();
54 }
55 return "aggressive".to_string();
56 }
57 if token_count > 3000 && is_code(ext) {
58 return "map".to_string();
59 }
60 "full".to_string()
61}
62
63pub fn handle(cache: &mut SessionCache, path: &str, crp_mode: CrpMode) -> String {
64 let mode = select_mode(cache, path);
65 let result = crate::tools::ctx_read::handle(cache, path, &mode, crp_mode);
66 format!("[auto:{mode}] {result}")
67}
68
69fn compute_hash(content: &str) -> String {
70 use md5::{Digest, Md5};
71 let mut hasher = Md5::new();
72 hasher.update(content.as_bytes());
73 format!("{:x}", hasher.finalize())
74}
75
76pub fn is_code_ext(ext: &str) -> bool {
77 is_code(ext)
78}
79
80fn is_code(ext: &str) -> bool {
81 matches!(
82 ext,
83 "rs" | "ts"
84 | "tsx"
85 | "js"
86 | "jsx"
87 | "py"
88 | "go"
89 | "java"
90 | "c"
91 | "cpp"
92 | "cc"
93 | "h"
94 | "hpp"
95 | "rb"
96 | "cs"
97 | "kt"
98 | "swift"
99 | "php"
100 | "zig"
101 | "ex"
102 | "exs"
103 | "scala"
104 | "sc"
105 | "dart"
106 | "sh"
107 | "bash"
108 | "svelte"
109 | "vue"
110 )
111}
112
113fn is_config_or_data(ext: &str, path: &str) -> bool {
114 if matches!(
115 ext,
116 "json" | "yaml" | "yml" | "toml" | "xml" | "ini" | "cfg" | "env" | "lock"
117 ) {
118 return true;
119 }
120 let name = std::path::Path::new(path)
121 .file_name()
122 .and_then(|n| n.to_str())
123 .unwrap_or("");
124 matches!(
125 name,
126 "Cargo.toml"
127 | "package.json"
128 | "tsconfig.json"
129 | "Makefile"
130 | "Dockerfile"
131 | "docker-compose.yml"
132 | ".gitignore"
133 | ".env"
134 | "pyproject.toml"
135 | "go.mod"
136 | "build.gradle"
137 | "pom.xml"
138 )
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144
145 #[test]
146 fn test_config_detection() {
147 assert!(is_config_or_data("json", "package.json"));
148 assert!(is_config_or_data("toml", "Cargo.toml"));
149 assert!(!is_config_or_data("rs", "main.rs"));
150 }
151
152 #[test]
153 fn test_code_detection() {
154 assert!(is_code("rs"));
155 assert!(is_code("py"));
156 assert!(is_code("tsx"));
157 assert!(!is_code("json"));
158 }
159}