skills: load: auto|always|manual frontmatter (on-demand vs inlined vs command-only)
This commit is contained in:
1 parent
fe40594deb
commit
8712e5f9ca
2 files changed
+76
-4
No files matched your search
@@ -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
|
`~/.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.
|
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 `/<name>`, never mentioned to the model).
|
||||||
|
|
||||||
Each skill is also a command: `/<name> [args]` sends the skill file plus `args` as the prompt. Builtin
|
Each skill is also a command: `/<name> [args]` sends the skill file plus `args` as the prompt. Builtin
|
||||||
commands win when names collide.
|
commands win when names collide.
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,20 @@ pub struct Skill {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
pub path: PathBuf,
|
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 `/<name>` 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), `<hx home>/skills`,
|
/// Skill dirs, lowest priority first: `~/.agents/skills` (shared with other agents), `<hx home>/skills`,
|
||||||
@@ -78,10 +92,12 @@ pub fn skills_in(dirs: &[PathBuf]) -> Vec<Skill> {
|
|||||||
let Ok(s) = std::fs::read_to_string(&p) else {
|
let Ok(s) = std::fs::read_to_string(&p) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let fm = s
|
let (fm, body) = s
|
||||||
.strip_prefix("---")
|
.strip_prefix("---")
|
||||||
.and_then(|r| r.split_once("\n---"))
|
.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| {
|
let get = |k: &str| {
|
||||||
fm.lines().find_map(|l| {
|
fm.lines().find_map(|l| {
|
||||||
l.strip_prefix(k)?
|
l.strip_prefix(k)?
|
||||||
@@ -96,8 +112,15 @@ pub fn skills_in(dirs: &[PathBuf]) -> Vec<Skill> {
|
|||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.into()
|
.into()
|
||||||
});
|
});
|
||||||
|
let load = match get("load").as_deref() {
|
||||||
|
Some("always") => Load::Always,
|
||||||
|
Some("manual") => Load::Manual,
|
||||||
|
_ => Load::Auto,
|
||||||
|
};
|
||||||
let sk = Skill {
|
let sk = Skill {
|
||||||
description: get("description").unwrap_or_default(),
|
description: get("description").unwrap_or_default(),
|
||||||
|
load,
|
||||||
|
body: body.trim().to_string(),
|
||||||
name,
|
name,
|
||||||
path: p,
|
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();
|
let mut s = base.unwrap_or(DEFAULT_PROMPT).to_string();
|
||||||
s.push_str(&format!("\n\nWorking directory: {}", cwd.display()));
|
s.push_str(&format!("\n\nWorking directory: {}", cwd.display()));
|
||||||
let sk = skills(home, cwd);
|
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");
|
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!(
|
s.push_str(&format!(
|
||||||
"- {}: {} ({})\n",
|
"- {}: {} ({})\n",
|
||||||
k.name,
|
k.name,
|
||||||
@@ -168,6 +195,47 @@ mod tests {
|
|||||||
assert!(system_prompt(Some("CUSTOM"), &home, &proj).starts_with("CUSTOM"));
|
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]
|
#[test]
|
||||||
fn agent_skills_dir() {
|
fn agent_skills_dir() {
|
||||||
let t = tempfile::tempdir().unwrap();
|
let t = tempfile::tempdir().unwrap();
|
||||||
|
|||||||
Reference in new issue
Block a user