//! `skill` tool: the model loads a skill body by name (no paths, so no traversal). 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); } }