Skip to main content

treesearch/
config.rs

1//! Defines search, traversal, indexing, and optional tokenizer policies.
2
3use std::env;
4use std::path::PathBuf;
5
6/// Unified configuration for TreeSearch Rust CLI.
7///
8/// Priority (high -> low):
9///   1. CLI flags / set_config()
10///   2. Environment variables (TREESEARCH_*)
11///   3. Built-in defaults
12#[derive(Debug, Clone)]
13pub struct TreeSearchConfig {
14    // Search
15    pub max_nodes_per_doc: usize,
16    pub top_k_docs: usize,
17
18    // Index
19    pub max_concurrency: usize,
20    pub max_dir_files: usize,
21    pub max_node_chars: usize,
22    pub max_result_chars: usize,
23
24    // FTS weights
25    pub fts_title_weight: f64,
26    pub fts_summary_weight: f64,
27    pub fts_body_weight: f64,
28    pub fts_code_weight: f64,
29    pub fts_front_matter_weight: f64,
30
31    // Tree search
32    pub search_mode: SearchMode,
33    pub anchor_top_k: usize,
34    pub max_anchor_per_doc: usize,
35    pub max_expansions: usize,
36    pub max_hops: usize,
37    pub max_siblings: usize,
38    pub min_frontier_score: f64,
39    pub early_stop_score: f64,
40    pub path_top_k: usize,
41
42    // Tokenizer
43    pub cjk_tokenizer: CjkTokenizerMode,
44
45    // Jieba custom dictionary (active only with the `cjk-jieba` feature;
46    // dependency-minimal builds use deterministic bigrams for Auto/Jieba).
47    //   - `jieba_user_dict_paths`: file paths in jieba dict format
48    //                              (one entry per line: "word [freq] [tag]")
49    //   - `jieba_user_words`: in-memory entries; each string is parsed as
50    //                        "word [freq] [tag]" with freq/tag optional, so
51    //                        plain `"石墨烯"` works just like `add_word("石墨烯")`
52    //   - `jieba_del_words`: words to suppress. jieba-rs has no `del_word`,
53    //                       so we emulate via `add_word(w, Some(0), None)`
54    //                       which makes the entry weight-zero.
55    pub jieba_user_dict_paths: Vec<PathBuf>,
56    pub jieba_user_words: Vec<String>,
57    pub jieba_del_words: Vec<String>,
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum SearchMode {
62    Auto,
63    Flat,
64    Tree,
65}
66
67impl SearchMode {
68    pub fn as_str(&self) -> &str {
69        match self {
70            Self::Auto => "auto",
71            Self::Flat => "flat",
72            Self::Tree => "tree",
73        }
74    }
75
76    /// Parses a mode name, falling back to [`SearchMode::Auto`].
77    pub fn parse_lossy(s: &str) -> Self {
78        match s.to_lowercase().as_str() {
79            "flat" => Self::Flat,
80            "tree" => Self::Tree,
81            _ => Self::Auto,
82        }
83    }
84
85    /// Parses a mode name with the historical forgiving API.
86    #[allow(clippy::should_implement_trait)]
87    pub fn from_str(s: &str) -> Self {
88        Self::parse_lossy(s)
89    }
90}
91
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub enum CjkTokenizerMode {
94    Auto,
95    Jieba,
96    Bigram,
97    Char,
98}
99
100impl CjkTokenizerMode {
101    /// Parses a tokenizer name, falling back to [`CjkTokenizerMode::Auto`].
102    pub fn parse_lossy(s: &str) -> Self {
103        match s.to_lowercase().as_str() {
104            "jieba" => Self::Jieba,
105            "bigram" => Self::Bigram,
106            "char" => Self::Char,
107            _ => Self::Auto,
108        }
109    }
110
111    /// Parses a tokenizer name with the historical forgiving API.
112    #[allow(clippy::should_implement_trait)]
113    pub fn from_str(s: &str) -> Self {
114        Self::parse_lossy(s)
115    }
116}
117
118impl Default for TreeSearchConfig {
119    fn default() -> Self {
120        let cpus = std::thread::available_parallelism()
121            .map(|n| n.get())
122            .unwrap_or(4)
123            .min(256);
124
125        Self {
126            max_nodes_per_doc: 5,
127            top_k_docs: 3,
128            max_concurrency: cpus,
129            max_dir_files: 10_000,
130            max_node_chars: 8_000,
131            max_result_chars: 32_000,
132
133            fts_title_weight: 5.0,
134            fts_summary_weight: 2.0,
135            fts_body_weight: 10.0,
136            fts_code_weight: 1.0,
137            fts_front_matter_weight: 2.0,
138
139            search_mode: SearchMode::Auto,
140            anchor_top_k: 5,
141            max_anchor_per_doc: 3,
142            max_expansions: 40,
143            max_hops: 3,
144            max_siblings: 2,
145            min_frontier_score: 0.1,
146            early_stop_score: 0.95,
147            path_top_k: 3,
148
149            cjk_tokenizer: CjkTokenizerMode::Auto,
150
151            jieba_user_dict_paths: Vec::new(),
152            jieba_user_words: Vec::new(),
153            jieba_del_words: Vec::new(),
154        }
155    }
156}
157
158impl TreeSearchConfig {
159    /// Create config from environment variables, falling back to defaults.
160    pub fn from_env() -> Self {
161        let mut config = Self::default();
162
163        if let Ok(v) = env::var("TREESEARCH_CJK_TOKENIZER") {
164            config.cjk_tokenizer = CjkTokenizerMode::parse_lossy(&v);
165        }
166        if let Ok(v) = env::var("TREESEARCH_SEARCH_MODE") {
167            config.search_mode = SearchMode::parse_lossy(&v);
168        }
169        if let Ok(v) = env::var("TREESEARCH_MAX_NODES_PER_DOC") {
170            if let Ok(n) = v.parse() {
171                config.max_nodes_per_doc = n;
172            }
173        }
174        if let Ok(v) = env::var("TREESEARCH_TOP_K_DOCS") {
175            if let Ok(n) = v.parse() {
176                config.top_k_docs = n;
177            }
178        }
179
180        // Jieba user dict — paths separated by `:` (POSIX) / `;` (Windows)
181        // via std::env::split_paths; commas are also tolerated for parity
182        // with the Python version.
183        if let Ok(v) = env::var("TREESEARCH_JIEBA_USER_DICT") {
184            let mut paths = Vec::new();
185            for chunk in env::split_paths(&v) {
186                let chunk_str = chunk.to_string_lossy().to_string();
187                for piece in chunk_str.split(',') {
188                    let trimmed = piece.trim();
189                    if !trimmed.is_empty() {
190                        paths.push(PathBuf::from(trimmed));
191                    }
192                }
193            }
194            config.jieba_user_dict_paths = paths;
195        }
196        if let Ok(v) = env::var("TREESEARCH_JIEBA_USER_WORDS") {
197            config.jieba_user_words = v
198                .split(',')
199                .map(|s| s.trim().to_string())
200                .filter(|s| !s.is_empty())
201                .collect();
202        }
203        if let Ok(v) = env::var("TREESEARCH_JIEBA_DEL_WORDS") {
204            config.jieba_del_words = v
205                .split(',')
206                .map(|s| s.trim().to_string())
207                .filter(|s| !s.is_empty())
208                .collect();
209        }
210
211        config
212    }
213
214    /// Column weights as an array for FTS5 bm25() function.
215    /// Order: title, summary, body, code_blocks, front_matter
216    pub fn fts_weights(&self) -> [f64; 5] {
217        [
218            self.fts_title_weight,
219            self.fts_summary_weight,
220            self.fts_body_weight,
221            self.fts_code_weight,
222            self.fts_front_matter_weight,
223        ]
224    }
225}
226
227#[cfg(test)]
228mod tests {
229    use super::*;
230
231    #[test]
232    fn test_default_config() {
233        let config = TreeSearchConfig::default();
234        assert_eq!(config.max_nodes_per_doc, 5);
235        assert_eq!(config.fts_body_weight, 10.0);
236        assert_eq!(config.search_mode, SearchMode::Auto);
237    }
238
239    #[test]
240    fn test_search_mode_parse() {
241        assert_eq!(SearchMode::parse_lossy("flat"), SearchMode::Flat);
242        assert_eq!(SearchMode::parse_lossy("TREE"), SearchMode::Tree);
243        assert_eq!(SearchMode::parse_lossy("blah"), SearchMode::Auto);
244    }
245
246    #[test]
247    fn test_fts_weights() {
248        let config = TreeSearchConfig::default();
249        let w = config.fts_weights();
250        assert_eq!(w, [5.0, 2.0, 10.0, 1.0, 2.0]);
251    }
252}