1use std::path::PathBuf;
5
6#[derive(Debug, Clone)]
8pub struct SkillEntry {
9 pub name: String,
11 pub description: String,
13 pub when_to_use: Option<String>,
15 pub source: SkillSource,
16 pub tier: Option<SkillTier>,
18 pub path: PathBuf,
20 pub argument_hint: Option<String>,
22 pub allowed_tools: Option<Vec<String>>,
24 pub disallowed_tools: Option<Vec<String>>,
28 pub arguments: Option<Vec<String>>,
32 pub model: Option<String>,
34 pub effort: Option<String>,
36 pub context: Option<String>,
38 pub agent: Option<String>,
41 pub background: Option<bool>,
45 pub disable_model_invocation: bool,
47 pub user_invocable: bool,
49 pub paths: Option<Vec<String>>,
51 pub version: Option<String>,
53 pub files: Option<Vec<String>>,
57}
58
59impl Default for SkillEntry {
60 fn default() -> Self {
61 Self {
62 name: String::new(),
63 description: String::new(),
64 when_to_use: None,
65 source: SkillSource::User,
66 tier: None,
67 path: PathBuf::new(),
68 argument_hint: None,
69 allowed_tools: None,
70 disallowed_tools: None,
71 arguments: None,
72 model: None,
73 effort: None,
74 context: None,
75 agent: None,
76 background: None,
77 disable_model_invocation: false,
78 user_invocable: true,
79 paths: None,
80 version: None,
81 files: None,
82 }
83 }
84}
85
86impl SkillEntry {
87 pub fn resolve_bundled_files(&self) -> Vec<PathBuf> {
99 let Some(files) = &self.files else {
100 return Vec::new();
101 };
102 let Some(skill_dir) = self.path.parent() else {
103 return Vec::new();
104 };
105 files.iter().map(|f| skill_dir.join(f)).collect()
106 }
107}
108
109#[derive(Debug, Clone, Copy, PartialEq, Eq)]
111pub enum SkillSource {
112 User,
114 Project,
116 Plugin,
119 Host,
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127#[non_exhaustive]
128pub enum SkillTier {
129 Global,
130 Scene,
131}
132
133impl SkillTier {
134 pub fn as_str(&self) -> &'static str {
135 match self {
136 SkillTier::Global => "global",
137 SkillTier::Scene => "scene",
138 }
139 }
140}
141
142impl SkillSource {
143 pub fn as_str(&self) -> &'static str {
144 match self {
145 SkillSource::User => "user",
146 SkillSource::Project => "project",
147 SkillSource::Plugin => "plugin",
148 SkillSource::Host => "host",
149 }
150 }
151}
152
153pub fn body_runs_commands(body: &str) -> bool {
156 body.contains("!`") || body.contains("```!")
157}
158
159pub struct InvocationVars<'a> {
162 pub skill_md: &'a std::path::Path,
164 pub session_id: Option<&'a str>,
165}
166
167pub fn expand_skill_vars_named(
179 body: &str,
180 args: &str,
181 arg_names: &[String],
182 vars: Option<&InvocationVars<'_>>,
183) -> String {
184 let positions: Vec<&str> = args.split_whitespace().collect();
185 let mut out = body.to_string();
186 let mut substituted = false;
187
188 for (i, name) in arg_names.iter().enumerate() {
190 let placeholder = format!("${name}");
191 if out.contains(&placeholder) {
192 let value = positions.get(i).copied().unwrap_or("");
193 out = out.replace(&placeholder, value);
194 substituted = true;
195 }
196 }
197
198 if out.contains("$ARGUMENTS") {
199 out = out.replace("$ARGUMENTS", args);
200 substituted = true;
201 }
202 if out.contains("$@") {
204 out = out.replace("$@", args);
205 substituted = true;
206 }
207 for i in (1..=9).rev() {
209 let placeholder = format!("${i}");
210 if out.contains(&placeholder) {
211 let value = positions.get(i - 1).copied().unwrap_or("");
212 out = out.replace(&placeholder, value);
213 substituted = true;
214 }
215 }
216
217 let skill_dir = vars
218 .and_then(|v| v.skill_md.parent())
219 .filter(|dir| !dir.as_os_str().is_empty());
220 if let Some(dir) = skill_dir.filter(|_| out.contains("${AGIX_SKILL_DIR}")) {
221 out = out.replace("${AGIX_SKILL_DIR}", &dir.display().to_string());
222 substituted = true;
223 }
224 if let Some(sid) = vars
225 .and_then(|v| v.session_id)
226 .filter(|_| out.contains("${AGIX_SESSION_ID}"))
227 {
228 out = out.replace("${AGIX_SESSION_ID}", sid);
229 substituted = true;
230 }
231
232 if !substituted && !args.is_empty() {
233 format!("{out}\n\nUser arguments: {args}")
234 } else {
235 out
236 }
237}
238
239#[cfg(test)]
240mod tests {
241 use super::*;
242
243 #[test]
244 fn resolve_bundled_files_joins_relative_to_skill_dir() {
245 let skill = SkillEntry {
246 name: "foo".into(),
247 path: PathBuf::from("/skills/foo/SKILL.md"),
248 files: Some(vec!["notes.md".into(), "ref/extra.md".into()]),
249 ..Default::default()
250 };
251 let resolved = skill.resolve_bundled_files();
252 assert_eq!(
253 resolved,
254 vec![
255 PathBuf::from("/skills/foo/notes.md"),
256 PathBuf::from("/skills/foo/ref/extra.md"),
257 ]
258 );
259 }
260
261 #[test]
262 fn resolve_bundled_files_empty_when_files_is_none() {
263 let skill = SkillEntry {
264 name: "foo".into(),
265 path: PathBuf::from("/skills/foo/SKILL.md"),
266 files: None,
267 ..Default::default()
268 };
269 assert!(skill.resolve_bundled_files().is_empty());
270 }
271
272 #[test]
273 fn resolve_bundled_files_empty_when_files_is_empty_vec() {
274 let skill = SkillEntry {
275 name: "foo".into(),
276 path: PathBuf::from("/skills/foo/SKILL.md"),
277 files: Some(vec![]),
278 ..Default::default()
279 };
280 assert!(skill.resolve_bundled_files().is_empty());
281 }
282
283 #[test]
284 fn resolve_bundled_files_does_not_reject_traversal_paths() {
285 let skill = SkillEntry {
288 name: "foo".into(),
289 path: PathBuf::from("/skills/foo/SKILL.md"),
290 files: Some(vec!["../../outside.md".into()]),
291 ..Default::default()
292 };
293 assert_eq!(
294 skill.resolve_bundled_files(),
295 vec![PathBuf::from("/skills/foo/../../outside.md")]
296 );
297 }
298
299 #[test]
300 fn expand_named_args_substitute_by_declared_order() {
301 let body = "Migrate $component from $from to $to.";
302 let names = vec![
303 "component".to_string(),
304 "from".to_string(),
305 "to".to_string(),
306 ];
307 let r = expand_skill_vars_named(body, "SearchBar React Vue", &names, None);
308 assert_eq!(r, "Migrate SearchBar from React to Vue.");
309 }
310
311 #[test]
312 fn expand_named_arg_with_no_matching_positional_becomes_empty() {
313 let body = "value=$only";
314 let names = vec!["only".to_string()];
315 let r = expand_skill_vars_named(body, "", &names, None);
316 assert_eq!(r, "value=");
317 }
318
319 #[test]
320 fn empty_names_leave_only_the_positional_forms() {
321 let body = "Run with $ARGUMENTS.";
322 assert_eq!(
323 expand_skill_vars_named(body, "x y z", &[], None),
324 "Run with x y z."
325 );
326 }
327}