systemprompt_loader/config_loader/
mod.rs1mod includes;
12mod merge;
13mod types;
14
15use std::collections::HashSet;
16use std::fs;
17use std::path::{Path, PathBuf};
18
19use systemprompt_config::ProfileBootstrap;
20use systemprompt_models::services::ServicesConfig;
21
22use crate::error::{ConfigLoadError, ConfigLoadResult};
23
24use includes::resolve_includes_recursively;
25use merge::{resolve_skill_instruction_includes, resolve_system_prompt_includes};
26use types::IncludeResolveCtx;
27
28#[derive(Debug)]
29pub struct ConfigLoader {
30 base_path: PathBuf,
31 config_path: PathBuf,
32}
33
34impl ConfigLoader {
35 #[must_use]
36 pub fn new(config_path: PathBuf) -> Self {
37 let base_path = config_path
38 .parent()
39 .unwrap_or_else(|| Path::new("."))
40 .to_path_buf();
41 Self {
42 base_path,
43 config_path,
44 }
45 }
46
47 pub fn for_active_profile() -> ConfigLoadResult<Self> {
48 let profile = ProfileBootstrap::get()?;
49 let config_path = PathBuf::from(profile.paths.config());
50 Ok(Self::new(config_path))
51 }
52
53 pub fn load() -> ConfigLoadResult<ServicesConfig> {
54 Self::for_active_profile()?.run()
55 }
56
57 pub fn load_from_path(path: &Path) -> ConfigLoadResult<ServicesConfig> {
58 Self::new(path.to_path_buf()).run()
59 }
60
61 #[cfg(any(test, feature = "expose-internals"))]
62 pub fn load_from_content(content: &str, path: &Path) -> ConfigLoadResult<ServicesConfig> {
63 Self::new(path.to_path_buf()).run_from_content(content)
64 }
65
66 pub fn validate_file(path: &Path) -> ConfigLoadResult<()> {
67 Self::load_from_path(path).map(|_| ())
68 }
69
70 fn run(&self) -> ConfigLoadResult<ServicesConfig> {
71 let content = fs::read_to_string(&self.config_path).map_err(|e| ConfigLoadError::Io {
72 path: self.config_path.clone(),
73 source: e,
74 })?;
75 self.run_from_content(&content)
76 }
77
78 fn run_from_content(&self, content: &str) -> ConfigLoadResult<ServicesConfig> {
79 let mut merged: ServicesConfig =
80 serde_yaml::from_str(content).map_err(|e| ConfigLoadError::Yaml {
81 path: self.config_path.clone(),
82 source: e,
83 })?;
84
85 let includes = std::mem::take(&mut merged.includes);
86
87 let mut visited: HashSet<PathBuf> = HashSet::new();
88 if let Ok(canonical_root) = fs::canonicalize(&self.config_path) {
89 visited.insert(canonical_root);
90 }
91 {
92 let mut ctx = IncludeResolveCtx {
93 visited: &mut visited,
94 merged: &mut merged,
95 chain: vec![self.config_path.clone()],
96 };
97 for include_path in &includes {
98 resolve_includes_recursively(
99 &self.base_path,
100 include_path,
101 &self.config_path,
102 &mut ctx,
103 )?;
104 }
105 }
106
107 resolve_system_prompt_includes(&self.base_path, &mut merged)?;
108 resolve_skill_instruction_includes(&self.base_path, &mut merged)?;
109
110 merged.settings.apply_env_overrides();
111
112 merged
113 .validate()
114 .map_err(|e| ConfigLoadError::Validation(e.to_string()))?;
115
116 Ok(merged)
117 }
118
119 pub fn get_includes(&self) -> ConfigLoadResult<Vec<String>> {
120 #[derive(serde::Deserialize)]
121 struct IncludesOnly {
122 #[serde(default)]
123 includes: Vec<String>,
124 }
125
126 let content = fs::read_to_string(&self.config_path).map_err(|e| ConfigLoadError::Io {
127 path: self.config_path.clone(),
128 source: e,
129 })?;
130 let parsed: IncludesOnly =
131 serde_yaml::from_str(&content).map_err(|e| ConfigLoadError::Yaml {
132 path: self.config_path.clone(),
133 source: e,
134 })?;
135 Ok(parsed.includes)
136 }
137
138 pub fn list_all_includes(&self) -> ConfigLoadResult<Vec<(String, bool)>> {
139 self.get_includes()?
140 .into_iter()
141 .map(|include| {
142 let exists = self.base_path.join(&include).exists();
143 Ok((include, exists))
144 })
145 .collect()
146 }
147
148 #[must_use]
149 pub fn base_path(&self) -> &Path {
150 &self.base_path
151 }
152}