diff --git a/README.md b/README.md index aa5bc34..ea3061b 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,13 @@ tests = "turn_end" # affected tests: turn_end | agent_end | off # install = ["brew", "install", "zls"] # offered by the lsp tool when `zls` is missing ``` +## Skills + +Skills are `//SKILL.md` files with `name:` / `description:` frontmatter. Only the name and +description go into the system prompt; the model reads the file when a task matches. Looked up in +`~/.agent/skills` (shared with other agents), `~/.hx/skills`, then `.hx/skills` in the project; a later +directory wins when names collide. + ## Lens After every tool round, hx asks the language server about files the agent wrote or edited and appends diff --git a/crates/hx-core/src/context.rs b/crates/hx-core/src/context.rs index b5faa89..77daf4d 100644 --- a/crates/hx-core/src/context.rs +++ b/crates/hx-core/src/context.rs @@ -42,11 +42,27 @@ pub struct Skill { pub path: PathBuf, } -/// `//SKILL.md` with `name:` / `description:` frontmatter. Project skills override global. +/// Skill dirs, lowest priority first: `~/.agent/skills` (shared with other agents), `/skills`, +/// `/.hx/skills`. +pub fn skill_dirs(home: &Path, cwd: &Path) -> Vec { + let mut v: Vec = 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 { + skills_in(&skill_dirs(home, cwd)) +} + +/// `//SKILL.md` with `name:` / `description:` frontmatter. Later dirs override earlier ones by name. +pub fn skills_in(dirs: &[PathBuf]) -> Vec { let mut out: Vec = 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")); + } }