Files
hx/crates/hx-tools/src/skill.rs
T
johannesandClaude Sonnet 5.5 a94632ea3d chore: delete code comments, add ratchet lint and ADRs
Classify every comment as tautology, stale, excuse or decision record.
Tautology/stale/excuse comments are deleted (excuses filed as issues
#196-#236), decisions moved to docs/adr/0001-0034.

- scripts/lint-comments.sh + .comment-baseline: per-file comment count
  may only go down; wired into tests/all.sh
- .claude/skills/delete-comment: repeatable audit workflow
- cargo fmt applied to touched crates

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 13:37:32 +01:00

74 lines
2.4 KiB
Rust

use crate::spec;
use async_trait::async_trait;
use hx_ai::ToolSpec;
use hx_core::context;
use hx_core::tool::{Tool, ToolCtx, ToolResult};
use serde_json::{Value, json};
pub struct Skill;
#[async_trait]
impl Tool for Skill {
fn spec(&self) -> ToolSpec {
spec(
"skill",
"Load a skill by name from the Skills list in the system prompt. Returns its instructions.",
json!({"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}),
)
}
async fn call(&self, a: Value, cx: &ToolCtx) -> ToolResult {
let name = a["name"]
.as_str()
.unwrap_or_default()
.trim_start_matches('/');
let all = context::skills(&context::hx_home(), &cx.cwd);
match all.iter().find(|k| k.name == name) {
None => ToolResult::err(format!("no skill `{name}`")),
Some(k) if !k.model_invocable => {
ToolResult::err(format!("skill {name} is user-invoked only"))
}
Some(k) => ToolResult::ok(k.text()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn loads_by_name_and_refuses_user_only() {
let t = tempfile::tempdir().unwrap();
let cwd = t.path().join("proj");
for (n, extra) in [("a", ""), ("u", "disable-model-invocation: true\n")] {
let d = cwd.join(".hx/skills").join(n);
std::fs::create_dir_all(&d).unwrap();
std::fs::write(
d.join("SKILL.md"),
format!("---\nname: {n}\ndescription: x\n{extra}---\nBODY-{n}\n"),
)
.unwrap();
}
let cx = ToolCtx {
cwd: cwd.clone(),
state: Default::default(),
};
let r = Skill.call(json!({"name": "a"}), &cx).await;
assert!(!r.is_error, "{}", r.content);
let dir = cwd.join(".hx/skills/a");
assert!(
r.content
.starts_with(&format!("Base directory for this skill: {}", dir.display()))
);
assert!(r.content.contains("BODY-a") && !r.content.contains("description:"));
let r = Skill.call(json!({"name": "u"}), &cx).await;
assert!(
r.is_error && r.content.contains("user-invoked only"),
"{}",
r.content
);
let r = Skill.call(json!({"name": "../../etc/passwd"}), &cx).await;
assert!(r.is_error);
}
}