diff --git a/README.md b/README.md index 10b625a..8b5f06a 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,10 @@ description go into the system prompt; the model reads the file when a task matc `~/.agents/skills` (shared with other agents), `~/.hx/skills`, then `.agents/skills` and `.hx/skills` in the project; a later directory wins when names collide. `/skills` lists what loaded and the searched dirs. +A `load:` frontmatter key sets when a skill reaches the model: `auto` (default, listed by description; the +model reads it when the task matches, so write the description as "use when ..."), `always` (body inlined in +the system prompt every turn) or `manual` (only via `/`, never mentioned to the model). + Each skill is also a command: `/ [args]` sends the skill file plus `args` as the prompt. Builtin commands win when names collide. diff --git a/crates/hx-core/src/context.rs b/crates/hx-core/src/context.rs index 3269f4a..3fb97b4 100644 --- a/crates/hx-core/src/context.rs +++ b/crates/hx-core/src/context.rs @@ -40,6 +40,20 @@ pub struct Skill { pub name: String, pub description: String, pub path: PathBuf, + pub load: Load, + /// Text after the frontmatter. + pub body: String, +} + +/// Frontmatter `load:` key. `auto` (default): name + description in the system prompt, the model reads +/// the file when the task matches. `always`: the body is inlined in the system prompt. `manual`: only +/// reachable as a `/` command, never mentioned to the model. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub enum Load { + #[default] + Auto, + Always, + Manual, } /// Skill dirs, lowest priority first: `~/.agents/skills` (shared with other agents), `/skills`, @@ -78,10 +92,12 @@ pub fn skills_in(dirs: &[PathBuf]) -> Vec { let Ok(s) = std::fs::read_to_string(&p) else { continue; }; - let fm = s + let (fm, body) = s .strip_prefix("---") .and_then(|r| r.split_once("\n---")) - .map_or("", |(f, _)| f); + .map_or(("", s.as_str()), |(f, b)| { + (f, b.split_once('\n').map_or("", |x| x.1)) + }); let get = |k: &str| { fm.lines().find_map(|l| { l.strip_prefix(k)? @@ -96,8 +112,15 @@ pub fn skills_in(dirs: &[PathBuf]) -> Vec { .to_string_lossy() .into() }); + let load = match get("load").as_deref() { + Some("always") => Load::Always, + Some("manual") => Load::Manual, + _ => Load::Auto, + }; let sk = Skill { description: get("description").unwrap_or_default(), + load, + body: body.trim().to_string(), name, path: p, }; @@ -112,9 +135,13 @@ pub fn system_prompt(base: Option<&str>, home: &Path, cwd: &Path) -> String { let mut s = base.unwrap_or(DEFAULT_PROMPT).to_string(); s.push_str(&format!("\n\nWorking directory: {}", cwd.display())); let sk = skills(home, cwd); - if !sk.is_empty() { + for k in sk.iter().filter(|k| k.load == Load::Always) { + s.push_str(&format!("\n\n# Skill: {}\n{}", k.name, k.body)); + } + let auto: Vec<_> = sk.iter().filter(|k| k.load == Load::Auto).collect(); + if !auto.is_empty() { s.push_str("\n\n# Skills\nRead a skill's file with `read` when the task matches it.\n"); - for k in sk { + for k in auto { s.push_str(&format!( "- {}: {} ({})\n", k.name, @@ -168,6 +195,47 @@ mod tests { assert!(system_prompt(Some("CUSTOM"), &home, &proj).starts_with("CUSTOM")); } + #[test] + fn skill_load_modes() { + let t = tempfile::tempdir().unwrap(); + for (n, l) in [("a", "always"), ("m", "manual"), ("d", "")] { + let d = t.path().join(n); + std::fs::create_dir_all(&d).unwrap(); + let load = if l.is_empty() { + String::new() + } else { + format!("load: {l}\n") + }; + std::fs::write( + d.join("SKILL.md"), + format!("---\nname: {n}\ndescription: use when {n}\n{load}---\nBODY-{n}\n"), + ) + .unwrap(); + } + let sk = skills_in(&[t.path().to_path_buf()]); + let loads: Vec<_> = sk.iter().map(|s| (s.name.as_str(), s.load)).collect(); + assert_eq!( + loads, + [("a", Load::Always), ("d", Load::Auto), ("m", Load::Manual)] + ); + assert_eq!(sk[0].body, "BODY-a"); + // prompt: always inlined, auto listed only, manual absent + let home = t.path().join("h"); + std::fs::create_dir_all(home.join("skills")).unwrap(); + for n in ["a", "d", "m"] { + std::fs::create_dir_all(home.join("skills").join(n)).unwrap(); + std::fs::copy( + t.path().join(n).join("SKILL.md"), + home.join("skills").join(n).join("SKILL.md"), + ) + .unwrap(); + } + let p = system_prompt(None, &home, &t.path().join("proj")); + assert!(p.contains("BODY-a") && !p.contains("- a:")); + assert!(p.contains("- d: use when d") && !p.contains("BODY-d")); + assert!(!p.contains("BODY-m") && !p.contains("- m:")); + } + #[test] fn agent_skills_dir() { let t = tempfile::tempdir().unwrap();