- hx-core: Tool trait + registry (schema validation, parallel calls), event bus with Block/Mutate/Inject hooks, agent loop (abort, max turns, AgentEnd inject), JSONL session tree (fork, resume, torn-line tolerant), AGENTS.md/CLAUDE.md hierarchy, skills index (bodies on demand) - hx-tools: read (ranges, binary, images, caps), write/edit (exact match, ambiguity, CRLF, diff, read-guard), bash (merged output, timeout kills process group, tail truncation to temp file) - hx-cli: -p runs full agent loop, --session / --continue, Ctrl-C abort, HX_BASE_URL override - tests/e2e.sh: scripted fake Anthropic server drives write -> bash -> answer Closes #6, closes #7, closes #8, closes #9, closes #10, closes #11, closes #12, closes #13, closes #14
This commit is contained in:
1 parent
da9d5d1ae0
commit
21d4d2bb3d
15 files changed
+1752
-52
No files matched your search
@@ -0,0 +1,151 @@
|
||||
//! System prompt, AGENTS.md discovery, skills.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub const DEFAULT_PROMPT: &str = "You are hx, a coding agent working in the user's repository. \
|
||||
Use the tools to read, edit and run code. Read files before editing them. Keep answers short.";
|
||||
|
||||
const CONTEXT_FILES: &[&str] = &["AGENTS.md", "CLAUDE.md"];
|
||||
|
||||
pub fn hx_home() -> PathBuf {
|
||||
std::env::var_os("HX_HOME")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| {
|
||||
std::env::var_os("HOME")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_default()
|
||||
.join(".hx")
|
||||
})
|
||||
}
|
||||
|
||||
/// Context files from global (`~/.hx`) then root-most to `cwd`. First match per dir.
|
||||
pub fn context_files(home: &Path, cwd: &Path) -> Vec<(PathBuf, String)> {
|
||||
let mut dirs: Vec<&Path> = cwd.ancestors().collect();
|
||||
dirs.reverse();
|
||||
let mut out = vec![];
|
||||
for d in std::iter::once(home).chain(dirs) {
|
||||
for n in CONTEXT_FILES {
|
||||
let p = d.join(n);
|
||||
if let Ok(s) = std::fs::read_to_string(&p) {
|
||||
out.push((p, s));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Skill {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
/// `<dir>/<name>/SKILL.md` with `name:` / `description:` frontmatter. Project skills override global.
|
||||
pub fn skills(home: &Path, cwd: &Path) -> 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 {
|
||||
continue;
|
||||
};
|
||||
let mut paths: Vec<_> = rd
|
||||
.flatten()
|
||||
.map(|e| e.path().join("SKILL.md"))
|
||||
.filter(|p| p.is_file())
|
||||
.collect();
|
||||
paths.sort();
|
||||
for p in paths {
|
||||
let Ok(s) = std::fs::read_to_string(&p) else {
|
||||
continue;
|
||||
};
|
||||
let fm = s
|
||||
.strip_prefix("---")
|
||||
.and_then(|r| r.split_once("\n---"))
|
||||
.map_or("", |(f, _)| f);
|
||||
let get = |k: &str| {
|
||||
fm.lines().find_map(|l| {
|
||||
l.strip_prefix(k)?
|
||||
.strip_prefix(':')
|
||||
.map(|v| v.trim().trim_matches('"').to_string())
|
||||
})
|
||||
};
|
||||
let name = get("name").unwrap_or_else(|| {
|
||||
p.parent()
|
||||
.and_then(|d| d.file_name())
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.into()
|
||||
});
|
||||
let sk = Skill {
|
||||
description: get("description").unwrap_or_default(),
|
||||
name,
|
||||
path: p,
|
||||
};
|
||||
out.retain(|x| x.name != sk.name);
|
||||
out.push(sk);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
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() {
|
||||
s.push_str("\n\n# Skills\nRead a skill's file with `read` when the task matches it.\n");
|
||||
for k in sk {
|
||||
s.push_str(&format!(
|
||||
"- {}: {} ({})\n",
|
||||
k.name,
|
||||
k.description,
|
||||
k.path.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
for (p, c) in context_files(home, cwd) {
|
||||
s.push_str(&format!("\n\n# {}\n{}", p.display(), c.trim()));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn prompt_assembly() {
|
||||
let t = tempfile::tempdir().unwrap();
|
||||
let home = t.path().join("home");
|
||||
let proj = t.path().join("proj/sub");
|
||||
std::fs::create_dir_all(home.join("skills/g")).unwrap();
|
||||
std::fs::create_dir_all(proj.join(".hx/skills/g")).unwrap();
|
||||
std::fs::write(home.join("AGENTS.md"), "GLOBAL").unwrap();
|
||||
std::fs::write(t.path().join("proj/AGENTS.md"), "PROJ").unwrap();
|
||||
std::fs::write(t.path().join("proj/CLAUDE.md"), "IGNORED").unwrap();
|
||||
std::fs::write(proj.join("CLAUDE.md"), "SUB").unwrap();
|
||||
std::fs::write(
|
||||
home.join("skills/g/SKILL.md"),
|
||||
"---\nname: g\ndescription: global\n---\nbody",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
proj.join(".hx/skills/g/SKILL.md"),
|
||||
"---\nname: g\ndescription: \"local\"\n---\nBODY",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let s = system_prompt(None, &home, &proj);
|
||||
let (g, p, sub) = (
|
||||
s.find("GLOBAL").unwrap(),
|
||||
s.find("PROJ").unwrap(),
|
||||
s.find("SUB").unwrap(),
|
||||
);
|
||||
assert!(g < p && p < sub);
|
||||
assert!(!s.contains("IGNORED"));
|
||||
assert!(s.contains("- g: local") && !s.contains("global ("));
|
||||
assert!(!s.contains("BODY"), "skill body must load on demand only");
|
||||
assert!(system_prompt(Some("CUSTOM"), &home, &proj).starts_with("CUSTOM"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user