Skip to main content

stynx_code_tools/infrastructure/
skill_tool.rs

1use stynx_code_errors::AppResult;
2use stynx_code_types::{PermissionLevel, SearchReadInfo, Tool};
3use serde_json::{Value, json};
4
5pub struct SkillTool;
6
7impl SkillTool {
8    pub fn new() -> Self {
9        Self
10    }
11}
12
13#[async_trait::async_trait]
14impl Tool for SkillTool {
15    fn name(&self) -> &str {
16        "skill"
17    }
18
19    fn description(&self) -> &str {
20        "Invoke a skill by name. Reads the skill definition from ~/.claude/skills/{name}/SKILL.md."
21    }
22
23    fn input_schema(&self) -> Value {
24        json!({
25            "type": "object",
26            "properties": {
27                "skill": {
28                    "type": "string",
29                    "description": "The skill name to invoke"
30                },
31                "args": {
32                    "type": "string",
33                    "description": "Optional arguments for the skill"
34                }
35            },
36            "required": ["skill"]
37        })
38    }
39
40    fn permission_level(&self) -> PermissionLevel {
41        PermissionLevel::ReadOnly
42    }
43
44    fn is_read_only(&self, _input: &Value) -> bool { true }
45    fn is_concurrent_safe(&self, _input: &Value) -> bool { true }
46
47    fn is_search_or_read_command(&self, _input: &Value) -> SearchReadInfo {
48        SearchReadInfo { is_search: false, is_read: true, is_list: false }
49    }
50
51    async fn execute(&self, input: Value) -> AppResult<String> {
52        let skill = input
53            .get("skill")
54            .and_then(|v| v.as_str())
55            .ok_or_else(|| stynx_code_errors::AppError::Tool("missing 'skill' field".into()))?;
56
57        let args = input
58            .get("args")
59            .and_then(|v| v.as_str())
60            .unwrap_or("");
61
62        tracing::info!(skill, args, "invoking skill");
63
64        let home = stynx_code_config::home_dir()
65            .map(|p| p.to_string_lossy().to_string())
66            .ok_or_else(|| stynx_code_errors::AppError::Tool("cannot determine home directory".into()))?;
67
68        let skill_path = format!("{home}/.claude/skills/{skill}/SKILL.md");
69
70        let content = tokio::fs::read_to_string(&skill_path)
71            .await
72            .map_err(|e| stynx_code_errors::AppError::Tool(
73                format!("skill '{skill}' not found at {skill_path}: {e}")
74            ))?;
75
76        if args.is_empty() {
77            Ok(content)
78        } else {
79            Ok(format!("{content}\n\n---\nArgs: {args}"))
80        }
81    }
82}