use super::paths::{create_private_dir, Paths};
use super::remote_config::RemoteConfig;
use serde::{Deserialize, Serialize};
use std::io::Write;
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
#[serde(skip_serializing_if = "Option::is_none")]
pub embedder: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reranker: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keyword_sufficiency_threshold: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rerank_top: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rrf_k: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub semantic_min_score: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub auto_update: Option<bool>,
pub max_chunk_tokens: usize,
pub chunk_overlap: usize,
pub roots: Vec<PathBuf>,
pub local_engine: LocalEngineConfig,
pub openrouter: OpenRouterConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct LocalEngineConfig {
pub base_url: String,
pub model: String,
pub timeout_secs: u64,
}
impl Default for LocalEngineConfig {
fn default() -> Self {
Self {
base_url: String::new(),
model: String::new(),
timeout_secs: 60,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct OpenRouterConfig {
pub api_key: String,
pub model: String,
pub timeout_secs: u64,
}
impl Default for OpenRouterConfig {
fn default() -> Self {
Self {
api_key: String::new(),
model: String::new(),
timeout_secs: 120,
}
}
}
impl Default for Config {
fn default() -> Self {
Self {
embedder: None,
reranker: None,
keyword_sufficiency_threshold: None,
rerank_top: None,
rrf_k: None,
semantic_min_score: None,
auto_update: None,
max_chunk_tokens: 400,
chunk_overlap: 40,
roots: vec![],
local_engine: LocalEngineConfig::default(),
openrouter: OpenRouterConfig::default(),
}
}
}
impl Config {
pub fn load() -> anyhow::Result<Self> {
Self::load_from(&Paths::new().config_path())
}
pub fn load_from(path: &std::path::Path) -> anyhow::Result<Self> {
if path.exists() {
let content = std::fs::read_to_string(path)?;
match toml::from_str(&content) {
Ok(config) => Ok(config),
Err(e) => {
tracing::warn!(
"Config file at {} is corrupt ({e:#}); falling back to defaults",
path.display()
);
Ok(Self::default())
}
}
} else {
Ok(Self::default())
}
}
pub fn save(&self) -> anyhow::Result<()> {
self.save_to(&Paths::new().config_path())
}
pub fn save_to(&self, path: &std::path::Path) -> anyhow::Result<()> {
let path = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
let parent = path.parent().unwrap_or(std::path::Path::new("."));
create_private_dir(parent)?;
let contents = toml::to_string_pretty(self)?;
let mut temp = tempfile::Builder::new()
.prefix(".config-")
.suffix(".tmp")
.tempfile_in(parent)?;
temp.write_all(contents.as_bytes())?;
temp.as_file().sync_all()?;
let mut attempt = 0;
loop {
match temp.persist(&path) {
Ok(_) => return Ok(()),
Err(e)
if cfg!(windows)
&& e.error.kind() == std::io::ErrorKind::PermissionDenied
&& attempt < 50 =>
{
temp = e.file;
attempt += 1;
std::thread::sleep(std::time::Duration::from_millis(10));
}
Err(e) => return Err(e.error.into()),
}
}
}
pub fn search(&self, remote: Option<&RemoteConfig>) -> SearchConfig {
let d = SearchConfig::default();
SearchConfig {
keyword_sufficiency_threshold: self
.keyword_sufficiency_threshold
.unwrap_or(d.keyword_sufficiency_threshold),
rerank_top: self
.rerank_top
.or(remote.and_then(|r| r.search.rerank_top))
.unwrap_or(d.rerank_top),
rrf_k: self.rrf_k.unwrap_or(d.rrf_k),
semantic_min_score: self
.semantic_min_score
.or(remote.and_then(|r| r.search.semantic_min_score))
.unwrap_or(d.semantic_min_score),
..d
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SearchConfig {
pub keyword_sufficiency_threshold: usize,
pub rerank_top: usize,
pub rrf_k: i32,
pub keyword_limit: usize,
pub semantic_limit: usize,
pub fused_limit: usize,
pub semantic_min_score: f32,
}
impl Default for SearchConfig {
fn default() -> Self {
Self {
keyword_sufficiency_threshold: 10,
rerank_top: 20,
rrf_k: 60,
keyword_limit: 20,
semantic_limit: 20,
fused_limit: 50,
semantic_min_score: 0.25,
}
}
}