Skip to main content

paper/core/
config.rs

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    /// Optional local, OpenAI-compatible server (Ollama, LM Studio, vLLM).
15    /// Only used by `paper answer --polish --engine local`.
16    pub local_engine: LocalEngineConfig,
17    /// Optional cloud fallback. Only used by `paper answer --polish
18    /// --engine openrouter`, and only when an API key is configured.
19    pub openrouter: OpenRouterConfig,
20}
21
22/// Local OpenAI-compatible server used for optional answer polishing.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(default)]
25pub struct LocalEngineConfig {
26    /// Base URL of the server, e.g. `http://localhost:11434`.
27    pub base_url: String,
28    /// Model name as the server knows it, e.g. `llama3.1`.
29    pub model: String,
30    /// Request timeout in seconds.
31    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/// OpenRouter settings for the opt-in cloud fallback.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(default)]
47pub struct OpenRouterConfig {
48    /// API key. When empty, `OPENROUTER_API_KEY` from the environment is used.
49    pub api_key: String,
50    /// Model slug, e.g. `anthropic/claude-sonnet-4-5`.
51    pub model: String,
52    /// Request timeout in seconds.
53    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    /// Minimum cosine similarity for a semantic hit. Vector search always has a
124    /// nearest neighbour, so without a floor every query would "find" something.
125    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}