skills: also load ~/.agent/skills
This commit is contained in:
1 parent
3cc2192af7
commit
d993292c99
2 files changed
+53
-3
No files matched your search
@@ -42,11 +42,27 @@ pub struct Skill {
|
||||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
/// `<dir>/<name>/SKILL.md` with `name:` / `description:` frontmatter. Project skills override global.
|
||||
/// Skill dirs, lowest priority first: `~/.agent/skills` (shared with other agents), `<hx home>/skills`,
|
||||
/// `<project>/.hx/skills`.
|
||||
pub fn skill_dirs(home: &Path, cwd: &Path) -> Vec<PathBuf> {
|
||||
let mut v: Vec<PathBuf> = std::env::var_os("HOME")
|
||||
.map(|h| PathBuf::from(h).join(".agent/skills"))
|
||||
.into_iter()
|
||||
.collect();
|
||||
v.push(home.join("skills"));
|
||||
v.push(cwd.join(".hx/skills"));
|
||||
v
|
||||
}
|
||||
|
||||
pub fn skills(home: &Path, cwd: &Path) -> Vec<Skill> {
|
||||
skills_in(&skill_dirs(home, cwd))
|
||||
}
|
||||
|
||||
/// `<dir>/<name>/SKILL.md` with `name:` / `description:` frontmatter. Later dirs override earlier ones by name.
|
||||
pub fn skills_in(dirs: &[PathBuf]) -> Vec<Skill> {
|
||||
let mut out: Vec<Skill> = vec![];
|
||||
for dir in [home.join("skills"), cwd.join(".hx/skills")] {
|
||||
let Ok(rd) = std::fs::read_dir(&dir) else {
|
||||
for dir in dirs {
|
||||
let Ok(rd) = std::fs::read_dir(dir) else {
|
||||
continue;
|
||||
};
|
||||
let mut paths: Vec<_> = rd
|
||||
@@ -148,4 +164,31 @@ mod tests {
|
||||
assert!(!s.contains("BODY"), "skill body must load on demand only");
|
||||
assert!(system_prompt(Some("CUSTOM"), &home, &proj).starts_with("CUSTOM"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_skills_dir() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let w = |p: &str, s: &str| {
|
||||
let p = t.path().join(p);
|
||||
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
|
||||
std::fs::write(p, s).unwrap();
|
||||
};
|
||||
w(
|
||||
"agent/a/SKILL.md",
|
||||
"---\nname: a\ndescription: shared\n---\n",
|
||||
);
|
||||
w(
|
||||
"agent/g/SKILL.md",
|
||||
"---\nname: g\ndescription: shared-g\n---\n",
|
||||
);
|
||||
w("hx/g/SKILL.md", "---\nname: g\ndescription: hx-g\n---\n");
|
||||
let sk = skills_in(&[t.path().join("agent"), t.path().join("hx")]);
|
||||
let got: Vec<(&str, &str)> = sk
|
||||
.iter()
|
||||
.map(|s| (s.name.as_str(), s.description.as_str()))
|
||||
.collect();
|
||||
assert_eq!(got, [("a", "shared"), ("g", "hx-g")]);
|
||||
let d = skill_dirs(Path::new("/h"), Path::new("/p"));
|
||||
assert!(d[0].ends_with(".agent/skills") && d[1] == Path::new("/h/skills"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user