1use super::paths::Paths;
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6#[serde(default)]
7pub struct Config {
8 pub keyword_sufficiency_threshold: usize,
9 pub rerank_top: usize,
10 pub rrf_k: i32,
11 pub max_chunk_tokens: usize,
12 pub chunk_overlap: usize,
13 pub roots: Vec<PathBuf>,
14 pub local_engine: LocalEngineConfig,
17 pub openrouter: OpenRouterConfig,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(default)]
25pub struct LocalEngineConfig {
26 pub base_url: String,
28 pub model: String,
30 pub timeout_secs: u64,
32}
33
34impl Default for LocalEngineConfig {
35 fn default() -> Self {
36 Self {
37 base_url: String::new(),
38 model: String::new(),
39 timeout_secs: 60,
40 }
41 }
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(default)]
47pub struct OpenRouterConfig {
48 pub api_key: String,
50 pub model: String,
52 pub timeout_secs: u64,
54}
55
56impl Default for OpenRouterConfig {
57 fn default() -> Self {
58 Self {
59 api_key: String::new(),
60 model: String::new(),
61 timeout_secs: 120,
62 }
63 }
64}
65
66impl Default for Config {
67 fn default() -> Self {
68 Self {
69 keyword_sufficiency_threshold: 10,
70 rerank_top: 20,
71 rrf_k: 60,
72 max_chunk_tokens: 400,
73 chunk_overlap: 40,
74 roots: vec![],
75 local_engine: LocalEngineConfig::default(),
76 openrouter: OpenRouterConfig::default(),
77 }
78 }
79}
80
81impl Config {
82 pub fn load() -> anyhow::Result<Self> {
83 Self::load_from(&Paths::new().config_path())
84 }
85
86 pub fn load_from(path: &std::path::Path) -> anyhow::Result<Self> {
87 if path.exists() {
88 let content = std::fs::read_to_string(path)?;
89 Ok(toml::from_str(&content)?)
90 } else {
91 Ok(Self::default())
92 }
93 }
94
95 pub fn save(&self) -> anyhow::Result<()> {
96 let path = Paths::new().config_path();
97 if let Some(parent) = path.parent() {
98 std::fs::create_dir_all(parent)?;
99 }
100 std::fs::write(path, toml::to_string_pretty(self)?)?;
101 Ok(())
102 }
103
104 pub fn search(&self) -> SearchConfig {
105 SearchConfig {
106 keyword_sufficiency_threshold: self.keyword_sufficiency_threshold,
107 rerank_top: self.rerank_top,
108 rrf_k: self.rrf_k,
109 ..SearchConfig::default()
110 }
111 }
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115#[serde(default)]
116pub struct SearchConfig {
117 pub keyword_sufficiency_threshold: usize,
118 pub rerank_top: usize,
119 pub rrf_k: i32,
120 pub keyword_limit: usize,
121 pub semantic_limit: usize,
122 pub fused_limit: usize,
123 pub semantic_min_score: f32,
126}
127
128impl Default for SearchConfig {
129 fn default() -> Self {
130 Self {
131 keyword_sufficiency_threshold: 10,
132 rerank_top: 20,
133 rrf_k: 60,
134 keyword_limit: 20,
135 semantic_limit: 20,
136 fused_limit: 50,
137 semantic_min_score: 0.25,
138 }
139 }
140}