use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct SkillEntry {
pub name: String,
pub description: String,
pub when_to_use: Option<String>,
pub source: SkillSource,
pub tier: Option<SkillTier>,
pub path: PathBuf,
pub argument_hint: Option<String>,
pub allowed_tools: Option<Vec<String>>,
pub disallowed_tools: Option<Vec<String>>,
pub arguments: Option<Vec<String>>,
pub model: Option<String>,
pub effort: Option<String>,
pub context: Option<String>,
pub agent: Option<String>,
pub background: Option<bool>,
pub disable_model_invocation: bool,
pub user_invocable: bool,
pub paths: Option<Vec<String>>,
pub version: Option<String>,
pub files: Option<Vec<String>>,
pub hooks: Option<Vec<String>>,
}
impl Default for SkillEntry {
fn default() -> Self {
Self {
name: String::new(),
description: String::new(),
when_to_use: None,
source: SkillSource::User,
tier: None,
path: PathBuf::new(),
argument_hint: None,
allowed_tools: None,
disallowed_tools: None,
arguments: None,
model: None,
effort: None,
context: None,
agent: None,
background: None,
disable_model_invocation: false,
user_invocable: true,
paths: None,
version: None,
files: None,
hooks: None,
}
}
}
impl SkillEntry {
pub fn resolve_bundled_files(&self) -> Vec<PathBuf> {
let Some(files) = &self.files else {
return Vec::new();
};
let Some(skill_dir) = self.path.parent() else {
return Vec::new();
};
files.iter().map(|f| skill_dir.join(f)).collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SkillSource {
User,
Project,
Plugin,
Host,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SkillTier {
Global,
Scene,
}
impl SkillTier {
pub fn as_str(&self) -> &'static str {
match self {
SkillTier::Global => "global",
SkillTier::Scene => "scene",
}
}
}
impl SkillSource {
pub fn as_str(&self) -> &'static str {
match self {
SkillSource::User => "user",
SkillSource::Project => "project",
SkillSource::Plugin => "plugin",
SkillSource::Host => "host",
}
}
}
pub fn body_runs_commands(body: &str) -> bool {
body.contains("!`") || body.contains("```!")
}
pub struct InvocationVars<'a> {
pub skill_md: &'a std::path::Path,
pub session_id: Option<&'a str>,
}
pub fn expand_skill_vars_named(
body: &str,
args: &str,
arg_names: &[String],
vars: Option<&InvocationVars<'_>>,
) -> String {
let positions: Vec<&str> = args.split_whitespace().collect();
let mut out = body.to_string();
let mut substituted = false;
for (i, name) in arg_names.iter().enumerate() {
let placeholder = format!("${name}");
if out.contains(&placeholder) {
let value = positions.get(i).copied().unwrap_or("");
out = out.replace(&placeholder, value);
substituted = true;
}
}
if out.contains("$ARGUMENTS") {
out = out.replace("$ARGUMENTS", args);
substituted = true;
}
if out.contains("$@") {
out = out.replace("$@", args);
substituted = true;
}
for i in (1..=9).rev() {
let placeholder = format!("${i}");
if out.contains(&placeholder) {
let value = positions.get(i - 1).copied().unwrap_or("");
out = out.replace(&placeholder, value);
substituted = true;
}
}
let skill_dir = vars
.and_then(|v| v.skill_md.parent())
.filter(|dir| !dir.as_os_str().is_empty());
if let Some(dir) = skill_dir.filter(|_| out.contains("${AGIX_SKILL_DIR}")) {
out = out.replace("${AGIX_SKILL_DIR}", &dir.display().to_string());
substituted = true;
}
if let Some(sid) = vars
.and_then(|v| v.session_id)
.filter(|_| out.contains("${AGIX_SESSION_ID}"))
{
out = out.replace("${AGIX_SESSION_ID}", sid);
substituted = true;
}
if !substituted && !args.is_empty() {
format!("{out}\n\nUser arguments: {args}")
} else {
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_bundled_files_joins_relative_to_skill_dir() {
let skill = SkillEntry {
name: "foo".into(),
path: PathBuf::from("/skills/foo/SKILL.md"),
files: Some(vec!["notes.md".into(), "ref/extra.md".into()]),
..Default::default()
};
let resolved = skill.resolve_bundled_files();
assert_eq!(
resolved,
vec![
PathBuf::from("/skills/foo/notes.md"),
PathBuf::from("/skills/foo/ref/extra.md"),
]
);
}
#[test]
fn resolve_bundled_files_empty_when_files_is_none() {
let skill = SkillEntry {
name: "foo".into(),
path: PathBuf::from("/skills/foo/SKILL.md"),
files: None,
..Default::default()
};
assert!(skill.resolve_bundled_files().is_empty());
}
#[test]
fn resolve_bundled_files_empty_when_files_is_empty_vec() {
let skill = SkillEntry {
name: "foo".into(),
path: PathBuf::from("/skills/foo/SKILL.md"),
files: Some(vec![]),
..Default::default()
};
assert!(skill.resolve_bundled_files().is_empty());
}
#[test]
fn resolve_bundled_files_does_not_reject_traversal_paths() {
let skill = SkillEntry {
name: "foo".into(),
path: PathBuf::from("/skills/foo/SKILL.md"),
files: Some(vec!["../../outside.md".into()]),
..Default::default()
};
assert_eq!(
skill.resolve_bundled_files(),
vec![PathBuf::from("/skills/foo/../../outside.md")]
);
}
#[test]
fn expand_named_args_substitute_by_declared_order() {
let body = "Migrate $component from $from to $to.";
let names = vec![
"component".to_string(),
"from".to_string(),
"to".to_string(),
];
let r = expand_skill_vars_named(body, "SearchBar React Vue", &names, None);
assert_eq!(r, "Migrate SearchBar from React to Vue.");
}
#[test]
fn expand_named_arg_with_no_matching_positional_becomes_empty() {
let body = "value=$only";
let names = vec!["only".to_string()];
let r = expand_skill_vars_named(body, "", &names, None);
assert_eq!(r, "value=");
}
#[test]
fn empty_names_leave_only_the_positional_forms() {
let body = "Run with $ARGUMENTS.";
assert_eq!(
expand_skill_vars_named(body, "x y z", &[], None),
"Run with x y z."
);
}
}