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 match toml::from_str(&content) {
90 Ok(config) => Ok(config),
91 Err(e) => {
92 tracing::warn!(
93 "Config file at {} is corrupt ({e}); falling back to defaults",
94 path.display()
95 );
96 Ok(Self::default())
97 }
98 }
99 } else {
100 Ok(Self::default())
101 }
102 }
103
104 pub fn save(&self) -> anyhow::Result<()> {
105 let path = Paths::new().config_path();
106 if let Some(parent) = path.parent() {
107 std::fs::create_dir_all(parent)?;
108 }
109 std::fs::write(path, toml::to_string_pretty(self)?)?;
110 Ok(())
111 }
112
113 pub fn search(&self) -> SearchConfig {
114 SearchConfig {
115 keyword_sufficiency_threshold: self.keyword_sufficiency_threshold,
116 rerank_top: self.rerank_top,
117 rrf_k: self.rrf_k,
118 ..SearchConfig::default()
119 }
120 }
121}
122
123#[derive(Debug, Clone, Serialize, Deserialize)]
124#[serde(default)]
125pub struct SearchConfig {
126 pub keyword_sufficiency_threshold: usize,
127 pub rerank_top: usize,
128 pub rrf_k: i32,
129 pub keyword_limit: usize,
130 pub semantic_limit: usize,
131 pub fused_limit: usize,
132 pub semantic_min_score: f32,
135}
136
137impl Default for SearchConfig {
138 fn default() -> Self {
139 Self {
140 keyword_sufficiency_threshold: 10,
141 rerank_top: 20,
142 rrf_k: 60,
143 keyword_limit: 20,
144 semantic_limit: 20,
145 fused_limit: 50,
146 semantic_min_score: 0.25,
147 }
148 }
149}