1use crate::ai::providers::ProvidersConfig;
7use crate::types::{OutputFormat, PdsTier, ResourceTier};
8use serde::{Deserialize, Serialize};
9use std::path::PathBuf;
10
11#[derive(Debug, Clone, Serialize, Deserialize, Default)]
13#[serde(default)]
14pub struct FetchiumConfig {
15 pub general: GeneralConfig,
16 pub search: SearchConfig,
17 pub fetch: FetchConfig,
18 pub cache: CacheConfig,
19 pub ai: AiConfig,
20 pub output: OutputConfig,
21 pub ranking: RankingConfig,
22 pub youtube: YouTubeConfig,
23 pub social: SocialConfig,
24 pub headless: HeadlessConfig,
25 pub proxy: ProxyConfig,
26 pub dataimpulse: DataImpulseConfig,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(default)]
36pub struct DataImpulseConfig {
37 pub enabled: bool,
39 pub username: String,
41 pub password: String,
43 pub host: String,
45 pub port: u16,
47 #[serde(default)]
49 pub proxy_domains: Vec<String>,
50}
51
52impl Default for DataImpulseConfig {
53 fn default() -> Self {
54 Self {
55 enabled: false,
56 username: String::new(),
57 password: String::new(),
58 host: "gw.dataimpulse.com".into(),
59 port: 823,
60 proxy_domains: Vec::new(),
61 }
62 }
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize)]
67#[serde(default)]
68pub struct ProxyConfig {
69 pub enabled: bool,
71 pub proxy_file: Option<PathBuf>,
73 pub protocol: String,
75 #[serde(default)]
77 pub proxy_domains: Vec<String>,
78 #[serde(default)]
80 pub bypass_domains: Vec<String>,
81}
82
83impl Default for ProxyConfig {
84 fn default() -> Self {
85 Self {
86 enabled: false,
87 proxy_file: None,
88 protocol: "http".into(),
89 proxy_domains: Vec::new(),
90 bypass_domains: vec!["localhost".into(), "127.0.0.1".into()],
91 }
92 }
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize, Default)]
99#[serde(default)]
100pub struct HeadlessConfig {
101 pub chrome_path: Option<PathBuf>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
109#[serde(default)]
110pub struct GeneralConfig {
111 pub max_results: u32,
113 pub format: OutputFormat,
115 pub verbose: bool,
117 pub data_dir: Option<PathBuf>,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
122#[serde(default)]
123pub struct SearchConfig {
124 pub backends: Vec<String>,
126 pub tavily_api_key: Option<String>,
128 pub tavily_api_keys: Vec<String>,
130 pub serper_api_key: Option<String>,
132 pub serper_api_keys: Vec<String>,
134 pub exa_api_key: Option<String>,
136 pub exa_api_keys: Vec<String>,
138 pub firecrawl_api_key: Option<String>,
140 pub firecrawl_api_keys: Vec<String>,
142 pub default_budget: u32,
144 pub default_tier: PdsTier,
146 pub max_concurrent: u32,
148 pub timeout_secs: u64,
150 pub searxng_url: Option<String>,
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize)]
155#[serde(default)]
156pub struct FetchConfig {
157 pub user_agent: String,
159 pub respect_robots: bool,
161 pub max_page_size: u64,
163 pub timeout_secs: u64,
165 pub max_redirects: u32,
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
170#[serde(default)]
171pub struct CacheConfig {
172 pub enabled: bool,
174 pub memory_max_entries: u64,
176 pub disk_max_mb: u64,
178 pub ttl_secs: u64,
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
183#[serde(default)]
184pub struct AiConfig {
185 pub ollama_host: String,
187 pub default_model: String,
189 pub max_tokens: u32,
191 pub fast_model: Option<String>,
194 pub providers: ProvidersConfig,
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize)]
202#[serde(default)]
203pub struct OutputConfig {
204 pub format: OutputFormat,
206 pub include_sources: bool,
208 pub include_confidence: bool,
210}
211
212#[derive(Debug, Clone, Serialize, Deserialize)]
214#[serde(default)]
215pub struct YouTubeConfig {
216 pub invidious_instances: Vec<String>,
218 pub piped_instances: Vec<String>,
220 pub max_videos: usize,
222 pub timeout_secs: u64,
224 pub fetch_comments: bool,
226}
227
228impl Default for YouTubeConfig {
229 fn default() -> Self {
230 Self {
231 invidious_instances: vec![
232 "https://invidious.nerdvpn.de".into(), "https://yewtu.be".into(), ],
235 piped_instances: vec![
239 "https://pipedapi.kavin.rocks".into(), "https://api.piped.yt".into(), ],
242 max_videos: 5,
243 timeout_secs: 4, fetch_comments: false, }
246 }
247}
248
249#[derive(Debug, Clone, Serialize, Deserialize, Default)]
251#[serde(default)]
252pub struct SocialConfig {
253 pub facebook_graph_token: Option<String>,
262}
263
264impl FetchiumConfig {
265 pub fn facebook_graph_token(&self) -> Option<String> {
269 self.social
270 .facebook_graph_token
271 .clone()
272 .or_else(|| std::env::var("FETCHIUM_FACEBOOK_TOKEN").ok())
273 }
274}
275
276#[derive(Debug, Clone, Serialize, Deserialize)]
278#[serde(default)]
279pub struct RankingConfig {
280 #[serde(default)]
283 pub weight_overrides: std::collections::HashMap<String, f64>,
284 #[serde(default = "RankingConfig::default_freshness")]
286 pub freshness_need: f64,
287 #[serde(default = "RankingConfig::default_simhash_threshold")]
289 pub simhash_threshold: u32,
290}
291
292impl RankingConfig {
293 fn default_freshness() -> f64 {
294 0.5
295 }
296 fn default_simhash_threshold() -> u32 {
297 6
298 }
299}
300
301impl Default for RankingConfig {
302 fn default() -> Self {
303 Self {
304 weight_overrides: std::collections::HashMap::new(),
305 freshness_need: Self::default_freshness(),
306 simhash_threshold: Self::default_simhash_threshold(),
307 }
308 }
309}
310
311impl Default for GeneralConfig {
316 fn default() -> Self {
317 Self {
318 max_results: 10,
319 format: OutputFormat::Markdown,
320 verbose: false,
321 data_dir: None,
322 }
323 }
324}
325
326impl Default for SearchConfig {
327 fn default() -> Self {
328 Self {
329 backends: vec![
334 "searxng".into(),
335 "wikipedia".into(),
336 "hackernews".into(),
337 "reddit".into(),
338 "stackoverflow".into(),
339 "arxiv".into(),
340 "github".into(),
341 ],
342 tavily_api_key: None,
343 tavily_api_keys: Vec::new(),
344 serper_api_key: None,
345 serper_api_keys: Vec::new(),
346 exa_api_key: None,
347 exa_api_keys: Vec::new(),
348 firecrawl_api_key: None,
349 firecrawl_api_keys: Vec::new(),
350 default_budget: 4000,
351 default_tier: PdsTier::Summary,
352 max_concurrent: 100,
353 timeout_secs: 60,
354 searxng_url: Some("http://localhost:4040".into()),
355 }
356 }
357}
358
359impl Default for FetchConfig {
360 fn default() -> Self {
361 Self {
362 user_agent: format!(
363 "Fetchium/{} (https://github.com/fetchium/fetchium)",
364 env!("CARGO_PKG_VERSION")
365 ),
366 respect_robots: true,
367 max_page_size: 10 * 1024 * 1024, timeout_secs: 30,
369 max_redirects: 10,
370 }
371 }
372}
373
374impl Default for CacheConfig {
375 fn default() -> Self {
376 Self {
377 enabled: true,
378 memory_max_entries: 1000,
379 disk_max_mb: 500,
380 ttl_secs: 3600,
381 }
382 }
383}
384
385impl Default for AiConfig {
386 fn default() -> Self {
387 Self {
388 ollama_host: "http://localhost:11434".into(),
389 default_model: "llama3.2".into(),
390 max_tokens: 8192,
391 fast_model: None,
392 providers: ProvidersConfig::default(),
393 }
394 }
395}
396
397impl Default for OutputConfig {
398 fn default() -> Self {
399 Self {
400 format: OutputFormat::Markdown,
401 include_sources: true,
402 include_confidence: true,
403 }
404 }
405}
406
407impl FetchiumConfig {
410 pub fn validate(&self) -> Result<Vec<String>, String> {
413 let mut warnings = Vec::new();
414
415 if self.search.default_budget == 0 {
417 return Err("search.default_budget must be > 0".into());
418 }
419 if self.search.default_budget > 1_000_000 {
420 warnings.push(format!(
421 "search.default_budget={} is very high, may cause slow responses",
422 self.search.default_budget
423 ));
424 }
425 if self.search.max_concurrent == 0 {
426 return Err("search.max_concurrent must be > 0".into());
427 }
428 if self.search.max_concurrent > 100 {
429 warnings.push(format!(
430 "search.max_concurrent={} exceeds recommended maximum of 100",
431 self.search.max_concurrent
432 ));
433 }
434 if self.search.timeout_secs == 0 {
435 return Err("search.timeout_secs must be > 0".into());
436 }
437 if self.search.timeout_secs > 300 {
438 warnings.push(format!(
439 "search.timeout_secs={} is very high (>5min)",
440 self.search.timeout_secs
441 ));
442 }
443
444 if self.fetch.timeout_secs == 0 {
446 return Err("fetch.timeout_secs must be > 0".into());
447 }
448 if self.fetch.max_page_size == 0 {
449 return Err("fetch.max_page_size must be > 0".into());
450 }
451 if self.fetch.max_page_size > 500 * 1024 * 1024 {
452 warnings.push("fetch.max_page_size > 500MB is excessive".into());
453 }
454 if self.fetch.max_redirects > 50 {
455 warnings.push("fetch.max_redirects > 50 may indicate a redirect loop issue".into());
456 }
457
458 if self.cache.enabled && self.cache.ttl_secs == 0 {
460 warnings.push("cache.ttl_secs=0 means entries expire immediately".into());
461 }
462
463 if !(0.0..=1.0).contains(&self.ranking.freshness_need) {
465 return Err(format!(
466 "ranking.freshness_need={} must be in [0.0, 1.0]",
467 self.ranking.freshness_need
468 ));
469 }
470 if self.ranking.simhash_threshold > 64 {
471 return Err(format!(
472 "ranking.simhash_threshold={} must be in [0, 64]",
473 self.ranking.simhash_threshold
474 ));
475 }
476
477 if self.ai.max_tokens == 0 {
479 return Err("ai.max_tokens must be > 0".into());
480 }
481
482 if self.general.max_results == 0 {
484 return Err("general.max_results must be > 0".into());
485 }
486 if self.general.max_results > 10_000 {
487 warnings.push(format!(
488 "general.max_results={} is very high",
489 self.general.max_results
490 ));
491 }
492
493 Ok(warnings)
494 }
495}
496
497impl FetchiumConfig {
500 pub fn data_dir(&self) -> PathBuf {
507 if let Some(ref dir) = self.general.data_dir {
508 return dir.clone();
509 }
510
511 let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
512 home.join(".fetchium")
513 }
514
515 pub fn load() -> Self {
517 Self::load_from(None)
518 }
519
520 pub fn load_from(path: Option<&std::path::Path>) -> Self {
526 let config_path = path.map(|p| p.to_path_buf()).unwrap_or_else(|| {
527 let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
528 home.join(".fetchium").join("config.toml")
529 });
530
531 let mut config = if config_path.exists() {
532 match std::fs::read_to_string(&config_path) {
533 Ok(contents) => match toml::from_str(&contents) {
534 Ok(config) => config,
535 Err(e) => {
536 tracing::warn!("Failed to parse config at {}: {e}", config_path.display());
537 Self::default()
538 }
539 },
540 Err(e) => {
541 tracing::warn!("Failed to read config at {}: {e}", config_path.display());
542 Self::default()
543 }
544 }
545 } else {
546 Self::default()
547 };
548
549 config.apply_env_overrides();
551
552 match config.validate() {
554 Ok(warnings) => {
555 for w in warnings {
556 tracing::warn!("Config warning: {w}");
557 }
558 }
559 Err(e) => {
560 tracing::error!("Config validation failed: {e} — using defaults");
561 return Self::default();
562 }
563 }
564
565 config
566 }
567
568 pub fn detect_resource_tier() -> ResourceTier {
570 let sys = sysinfo::System::new_all();
571 let total_ram_gb = sys.total_memory() / (1024 * 1024 * 1024);
572 let cpu_count = sys.cpus().len();
573
574 match (total_ram_gb, cpu_count) {
575 (ram, cpus) if ram >= 32 && cpus >= 8 => ResourceTier::Server,
576 (ram, cpus) if ram >= 16 && cpus >= 4 => ResourceTier::Performance,
577 (ram, _) if ram >= 4 => ResourceTier::Standard,
578 _ => ResourceTier::Minimal,
579 }
580 }
581
582 pub fn apply_env_overrides(&mut self) {
590 macro_rules! env_str {
592 ($name:expr) => {
593 std::env::var($name).ok()
594 };
595 }
596
597 if let Some(val) = env_str!("FETCHIUM_SEARCH_DEFAULT_BUDGET") {
598 if let Ok(budget) = val.parse::<u32>() {
599 self.search.default_budget = budget;
600 }
601 }
602 if let Some(val) = env_str!("FETCHIUM_SEARCH_MAX_CONCURRENT") {
603 if let Ok(n) = val.parse::<u32>() {
604 self.search.max_concurrent = n;
605 }
606 }
607 if let Some(val) = env_str!("FETCHIUM_SEARCH_TIMEOUT_SECS") {
608 if let Ok(n) = val.parse::<u64>() {
609 self.search.timeout_secs = n;
610 }
611 }
612 if let Some(val) = env_str!("FETCHIUM_CACHE_ENABLED") {
613 if let Ok(b) = val.parse::<bool>() {
614 self.cache.enabled = b;
615 }
616 }
617 if let Some(val) = env_str!("FETCHIUM_CACHE_TTL_SECS") {
618 if let Ok(n) = val.parse::<u64>() {
619 self.cache.ttl_secs = n;
620 }
621 }
622 if let Some(val) = env_str!("FETCHIUM_AI_OLLAMA_HOST") {
623 self.ai.ollama_host = val;
624 }
625 if let Some(val) = env_str!("FETCHIUM_AI_DEFAULT_MODEL") {
626 self.ai.default_model = val;
627 }
628 if let Some(val) = env_str!("FETCHIUM_GENERAL_VERBOSE") {
629 if let Ok(b) = val.parse::<bool>() {
630 self.general.verbose = b;
631 }
632 }
633 if let Some(val) = env_str!("FETCHIUM_FETCH_USER_AGENT") {
634 self.fetch.user_agent = val;
635 }
636 if let Some(val) = env_str!("FETCHIUM_FETCH_RESPECT_ROBOTS") {
637 if let Ok(b) = val.parse::<bool>() {
638 self.fetch.respect_robots = b;
639 }
640 }
641 if let Ok(val) = std::env::var("OPENAI_API_KEY") {
643 if !val.is_empty() {
644 self.ai.providers.openai.api_key = Some(val);
645 }
646 }
647 if let Ok(val) = std::env::var("ANTHROPIC_API_KEY") {
648 if !val.is_empty() {
649 self.ai.providers.anthropic.api_key = Some(val);
650 }
651 }
652 if let Ok(val) = std::env::var("GEMINI_API_KEY") {
653 if !val.is_empty() {
654 self.ai.providers.gemini.api_key = Some(val);
655 }
656 }
657 if let Ok(val) = std::env::var("OPENROUTER_API_KEY") {
658 if !val.is_empty() {
659 self.ai.providers.openrouter.api_key = Some(val);
660 }
661 }
662 if let Some(val) = env_str!("FETCHIUM_AI_PROVIDER") {
664 if !val.is_empty() {
665 self.ai.providers.fallback_chain.insert(0, val);
666 }
667 }
668 if let Some(val) = env_str!("FETCHIUM_CHROME_PATH") {
670 if !val.is_empty() {
671 self.headless.chrome_path = Some(PathBuf::from(val));
672 }
673 }
674 }
675
676 pub fn ensure_data_dir(&self) -> std::io::Result<PathBuf> {
678 let dir = self.data_dir();
679 if !dir.exists() {
680 std::fs::create_dir_all(&dir)?;
681 }
682 Ok(dir)
683 }
684
685 pub fn config_file_path() -> PathBuf {
689 let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
690 home.join(".fetchium").join("config.toml")
691 }
692
693 pub fn save(&self) -> std::io::Result<()> {
697 let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
698 let path = home.join(".fetchium").join("config.toml");
699 if let Some(parent) = path.parent() {
700 std::fs::create_dir_all(parent)?;
701 }
702 let toml_str = toml::to_string_pretty(self).map_err(std::io::Error::other)?;
703 std::fs::write(&path, toml_str)
704 }
705}
706
707#[cfg(test)]
708mod tests {
709 use super::*;
710
711 #[test]
712 fn default_config_is_valid() {
713 let config = FetchiumConfig::default();
714 assert_eq!(config.search.default_budget, 4000);
715 assert!(config.fetch.respect_robots);
716 assert!(config.cache.enabled);
717 }
718
719 #[test]
720 fn config_roundtrip_toml() {
721 let config = FetchiumConfig::default();
722 let toml_str = toml::to_string_pretty(&config).unwrap();
723 let back: FetchiumConfig = toml::from_str(&toml_str).unwrap();
724 assert_eq!(back.search.default_budget, config.search.default_budget);
725 }
726
727 #[test]
728 fn data_dir_default() {
729 let config = FetchiumConfig::default();
730 let dir = config.data_dir();
731 let s = dir.to_string_lossy();
733 assert!(s.contains(".fetchium"));
734 }
735
736 #[test]
737 fn env_override_budget_new_prefix() {
738 std::env::set_var("FETCHIUM_SEARCH_DEFAULT_BUDGET", "8000");
739 let mut config = FetchiumConfig::default();
740 config.apply_env_overrides();
741 assert_eq!(config.search.default_budget, 8000);
742 std::env::remove_var("FETCHIUM_SEARCH_DEFAULT_BUDGET");
743 }
744
745 #[test]
746 fn env_override_cache_disabled() {
747 std::env::set_var("FETCHIUM_CACHE_ENABLED", "false");
748 let mut config = FetchiumConfig::default();
749 config.apply_env_overrides();
750 assert!(!config.cache.enabled);
751 std::env::remove_var("FETCHIUM_CACHE_ENABLED");
752 }
753
754 #[test]
755 fn save_and_reload() {
756 let dir = tempfile::tempdir().unwrap();
757 let config_path = dir.path().join("config.toml");
758
759 let mut config = FetchiumConfig::default();
760 config.search.default_budget = 9999;
761
762 let toml_str = toml::to_string_pretty(&config).unwrap();
764 std::fs::write(&config_path, &toml_str).unwrap();
765
766 let loaded = FetchiumConfig::load_from(Some(&config_path));
768 assert_eq!(loaded.search.default_budget, 9999);
769 }
770
771 #[test]
772 fn config_file_path_is_fetchium() {
773 let path = FetchiumConfig::config_file_path();
774 let s = path.to_string_lossy();
775 assert!(s.contains(".fetchium"));
776 assert!(s.ends_with("config.toml"));
777 }
778}