1use crate::RunEnv as Env;
19use crate::local_resources::{LocalResources, PathMetadata};
20use resources::prompt_templates::PromptTemplate;
21use resources::skills::{LoadSkillsOptions, Skill, load_skills};
22use std::path::Path;
23
24pub const BUNDLED_SKILLS_ENV: &str = "ULTRON_BUNDLED_SKILLS";
25
26#[derive(Clone, Debug, Default, PartialEq, Eq)]
29pub struct ResourceSource {
30 pub path: String,
31 pub source: String,
32 pub scope: String,
33 pub origin: String,
34 pub base_dir: Option<String>,
35}
36
37impl ResourceSource {
38 fn of(path: &str, metadata: &PathMetadata) -> ResourceSource {
39 ResourceSource {
40 path: path.to_string(),
41 source: metadata.source.clone(),
42 scope: metadata.scope.name().into(),
43 origin: "top-level".into(),
44 base_dir: metadata.base_dir.clone(),
45 }
46 }
47
48 pub fn source_info(&self, path: &str) -> serde_json::Value {
50 let mut info = serde_json::json!({
51 "path": path,
52 "source": self.source,
53 "scope": self.scope,
54 "origin": self.origin,
55 });
56 if let Some(base) = &self.base_dir {
57 info["baseDir"] = base.as_str().into();
58 }
59 info
60 }
61}
62
63#[derive(Clone, Debug, Default, PartialEq, Eq)]
64pub struct SessionResources {
65 pub skill_paths: Vec<String>,
68 pub prompt_paths: Vec<String>,
70 pub metadata: Vec<ResourceSource>,
73 pub system_prompt: Option<String>,
75 pub append_system_prompt: Vec<String>,
77 pub home: String,
79}
80
81fn merge(cwd: &str, home: &str, paths: impl IntoIterator<Item = String>) -> Vec<String> {
83 let mut out: Vec<String> = Vec::new();
84 let mut seen: Vec<String> = Vec::new();
85 for path in paths {
86 let resolved = resources::paths::resolve_path(&path, cwd, home, true);
87 let canonical = resources::paths::canonicalize(&resolved);
88 if !seen.contains(&canonical) {
89 seen.push(canonical);
90 out.push(resolved);
91 }
92 }
93 out
94}
95
96pub fn resolve(
99 env: &Env,
100 settings: config::settings::SettingsManager,
101 append_system_prompt: Option<&[String]>,
102) -> SessionResources {
103 let home = env.home.display().to_string();
104 let cwd = resources::paths::resolve_path(&env.cwd.display().to_string(), "/", &home, false);
105 let agent_dir = env.agent_dir().display().to_string();
106 let trusted = settings.is_project_trusted();
107 let prompt_files =
108 resources::prompt_files::load_prompt_files(&cwd, &agent_dir, &home, trusted, None, append_system_prompt);
109 let resolved = LocalResources::new(env, settings).resolve();
110 let mut metadata: Vec<ResourceSource> = Vec::new();
111 for list in [&resolved.skills, &resolved.prompts, &resolved.themes] {
112 for r in list {
113 if !metadata.iter().any(|m| m.path == r.path) {
114 metadata.push(ResourceSource::of(&r.path, &r.metadata));
115 }
116 }
117 }
118 let enabled = |list: &[crate::local_resources::ResolvedResource]| -> Vec<String> {
119 list.iter().filter(|r| r.enabled).map(|r| r.path.clone()).collect()
120 };
121 SessionResources {
122 skill_paths: enabled(&resolved.skills),
123 prompt_paths: enabled(&resolved.prompts),
124 metadata,
125 system_prompt: prompt_files.system_prompt,
126 append_system_prompt: prompt_files.append_system_prompt,
127 home,
128 }
129}
130
131pub fn resolve_default(env: &Env) -> SessionResources {
134 let agent_dir = env.agent_dir();
135 let trusted = config::trust::worker_project_trusted(&env.cwd, &agent_dir, &env.home);
136 let settings = config::settings::SettingsManager::create(&env.cwd, &agent_dir, trusted);
137 resolve(env, settings, None)
138}
139
140pub fn skills(
143 cwd: &str,
144 agent_dir: &str,
145 resources: &SessionResources,
146 no_skills: bool,
147 bundled: Option<&str>,
148) -> Vec<Skill> {
149 if no_skills {
150 return Vec::new();
151 }
152 let paths = merge(
153 cwd,
154 &resources.home,
155 resources.skill_paths.iter().cloned().chain(bundled.map(str::to_string)),
156 );
157 if paths.is_empty() {
158 return Vec::new();
159 }
160 load_skills(&LoadSkillsOptions {
161 cwd,
162 agent_dir,
163 home: &resources.home,
164 skill_paths: &paths,
165 include_defaults: false,
166 })
167 .skills
168}
169
170pub fn prompts(cwd: &str, agent_dir: &str, resources: &SessionResources) -> Vec<PromptTemplate> {
172 let paths = merge(cwd, &resources.home, resources.prompt_paths.iter().cloned());
173 let loaded = resources::prompt_templates::load_prompt_templates(cwd, agent_dir, &resources.home, &paths, false);
174 let mut out: Vec<PromptTemplate> = Vec::new();
175 for template in loaded.templates {
176 if !out.iter().any(|t| t.name == template.name) {
177 out.push(template);
178 }
179 }
180 out
181}
182
183pub fn source_info(resources: &SessionResources, file_path: &str) -> Option<serde_json::Value> {
187 let file = jsrt::path::resolve("/", file_path);
188 resources
189 .metadata
190 .iter()
191 .find(|m| {
192 let root = jsrt::path::resolve("/", &m.path);
193 file == root || file.starts_with(&format!("{root}/"))
194 })
195 .map(|m| m.source_info(file_path))
196}
197
198pub fn skill_source_info(resources: &SessionResources, skill: &Skill) -> serde_json::Value {
202 source_info(resources, &skill.file_path).unwrap_or_else(|| {
203 serde_json::json!({
204 "path": skill.file_path, "source": "local", "scope": "temporary", "origin": "top-level",
205 "baseDir": skill.base_dir,
206 })
207 })
208}
209
210pub fn prompt_source_info(
213 resources: &SessionResources,
214 template: &PromptTemplate,
215 cwd: &str,
216 agent_dir: &str,
217) -> serde_json::Value {
218 source_info(resources, &template.file_path).unwrap_or_else(|| {
219 let (scope, base) = match template.scope {
220 "user" => ("user", jsrt::path::join(&[agent_dir, "prompts"])),
221 "project" => (
222 "project",
223 jsrt::path::join(&[cwd, config::paths::CONFIG_DIR_NAME, "prompts"]),
224 ),
225 _ => ("temporary", jsrt::path::dirname(&template.file_path)),
226 };
227 serde_json::json!({
228 "path": template.file_path, "source": "local", "scope": scope, "origin": "top-level", "baseDir": base,
229 })
230 })
231}
232
233pub fn bundled_skills(dir: &Path, var: &dyn Fn(&str) -> Option<String>) -> Option<String> {
236 if crate::flag_off(var(BUNDLED_SKILLS_ENV).as_deref()) {
237 return None;
238 }
239 Some(config::paths::normalize(dir).display().to_string())
240}