|
| 1 | +//! Skill Tool - Invoke skills as callable tools with temporary permission grants |
| 2 | +//! |
| 3 | +//! This tool allows agents to invoke skills as first-class tools, with the skill's |
| 4 | +//! allowed-tools temporarily granted during execution. This enforces skill-based |
| 5 | +//! access patterns and prevents agents from bypassing skills to directly access |
| 6 | +//! underlying tools. |
| 7 | +//! |
| 8 | +//! ## Usage |
| 9 | +//! |
| 10 | +//! ```rust |
| 11 | +//! // Agent calls: Skill("data-processor") |
| 12 | +//! // The skill's allowed-tools are temporarily granted |
| 13 | +//! // After execution, permissions are restored |
| 14 | +//! ``` |
| 15 | +
|
| 16 | +use crate::agent::{AgentConfig, AgentLoop}; |
| 17 | +use crate::llm::LlmClient; |
| 18 | +use crate::permissions::{PermissionDecision, PermissionPolicy, PermissionRule}; |
| 19 | +use crate::skills::{Skill, SkillRegistry}; |
| 20 | +use crate::tools::{Tool, ToolContext, ToolOutput, ToolExecutor}; |
| 21 | +use anyhow::{anyhow, Result}; |
| 22 | +use async_trait::async_trait; |
| 23 | +use serde::{Deserialize, Serialize}; |
| 24 | +use serde_json::Value; |
| 25 | +use std::sync::Arc; |
| 26 | + |
| 27 | +/// Arguments for the Skill tool |
| 28 | +#[derive(Debug, Serialize, Deserialize)] |
| 29 | +pub struct SkillArgs { |
| 30 | + /// Name of the skill to invoke |
| 31 | + pub skill_name: String, |
| 32 | + /// Optional prompt/query to pass to the skill |
| 33 | + #[serde(default)] |
| 34 | + pub prompt: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +/// Skill tool - invokes skills with temporary permission grants |
| 38 | +pub struct SkillTool { |
| 39 | + skill_registry: Arc<SkillRegistry>, |
| 40 | + llm_client: Arc<dyn LlmClient>, |
| 41 | + tool_executor: Arc<ToolExecutor>, |
| 42 | + base_config: AgentConfig, |
| 43 | +} |
| 44 | + |
| 45 | +impl SkillTool { |
| 46 | + pub fn new( |
| 47 | + skill_registry: Arc<SkillRegistry>, |
| 48 | + llm_client: Arc<dyn LlmClient>, |
| 49 | + tool_executor: Arc<ToolExecutor>, |
| 50 | + base_config: AgentConfig, |
| 51 | + ) -> Self { |
| 52 | + Self { |
| 53 | + skill_registry, |
| 54 | + llm_client, |
| 55 | + tool_executor, |
| 56 | + base_config, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Create a temporary permission policy that grants the skill's allowed-tools |
| 61 | + fn create_skill_permission_policy(skill: &Skill) -> PermissionPolicy { |
| 62 | + let permissions = skill.parse_allowed_tools(); |
| 63 | + |
| 64 | + // Convert skill permissions to PermissionRules |
| 65 | + let mut allow_rules = Vec::new(); |
| 66 | + for perm in permissions { |
| 67 | + // Create a rule string in the format "Tool(pattern)" |
| 68 | + let rule_str = if perm.pattern == "*" { |
| 69 | + perm.tool.clone() |
| 70 | + } else { |
| 71 | + format!("{}({})", perm.tool, perm.pattern) |
| 72 | + }; |
| 73 | + allow_rules.push(PermissionRule::new(&rule_str)); |
| 74 | + } |
| 75 | + |
| 76 | + PermissionPolicy { |
| 77 | + deny: Vec::new(), |
| 78 | + allow: allow_rules, |
| 79 | + ask: Vec::new(), |
| 80 | + default_decision: PermissionDecision::Deny, // Deny by default - only allow what skill specifies |
| 81 | + enabled: true, |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[async_trait] |
| 87 | +impl Tool for SkillTool { |
| 88 | + fn name(&self) -> &str { |
| 89 | + "Skill" |
| 90 | + } |
| 91 | + |
| 92 | + fn description(&self) -> &str { |
| 93 | + "Invoke a skill with temporary permission grants. The skill's allowed-tools are granted during execution and revoked after completion." |
| 94 | + } |
| 95 | + |
| 96 | + fn parameters(&self) -> Value { |
| 97 | + serde_json::json!({ |
| 98 | + "type": "object", |
| 99 | + "properties": { |
| 100 | + "skill_name": { |
| 101 | + "type": "string", |
| 102 | + "description": "Name of the skill to invoke" |
| 103 | + }, |
| 104 | + "prompt": { |
| 105 | + "type": "string", |
| 106 | + "description": "Optional prompt or query to pass to the skill" |
| 107 | + } |
| 108 | + }, |
| 109 | + "required": ["skill_name"] |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + async fn execute(&self, args: &Value, ctx: &ToolContext) -> Result<ToolOutput> { |
| 114 | + let args: SkillArgs = serde_json::from_value(args.clone())?; |
| 115 | + |
| 116 | + // Get the skill |
| 117 | + let skill = self.skill_registry |
| 118 | + .get(&args.skill_name) |
| 119 | + .ok_or_else(|| anyhow!("Skill '{}' not found", args.skill_name))?; |
| 120 | + |
| 121 | + // Create temporary permission policy with skill's allowed-tools |
| 122 | + let skill_permission_policy = Self::create_skill_permission_policy(&skill); |
| 123 | + |
| 124 | + // Create a modified config with the skill's permissions |
| 125 | + let mut skill_config = self.base_config.clone(); |
| 126 | + |
| 127 | + // Set the skill's permission policy as the permission checker |
| 128 | + skill_config.permission_checker = Some(Arc::new(skill_permission_policy)); |
| 129 | + |
| 130 | + // Create a temporary skill registry with only this skill |
| 131 | + let temp_registry = Arc::new(SkillRegistry::new()); |
| 132 | + temp_registry.register(skill.clone())?; |
| 133 | + skill_config.skill_registry = Some(temp_registry); |
| 134 | + |
| 135 | + // Build the system prompt with skill content |
| 136 | + skill_config.prompt_slots.role = Some(format!( |
| 137 | + "You are executing the '{}' skill.\n\n{}\n\n{}", |
| 138 | + skill.name, |
| 139 | + skill.description, |
| 140 | + skill.content |
| 141 | + )); |
| 142 | + |
| 143 | + // Create agent loop with skill permissions |
| 144 | + let agent_loop = AgentLoop::new( |
| 145 | + self.llm_client.clone(), |
| 146 | + self.tool_executor.clone(), |
| 147 | + ctx.clone(), |
| 148 | + skill_config, |
| 149 | + ); |
| 150 | + |
| 151 | + // Execute the skill with the prompt |
| 152 | + let prompt = args.prompt.unwrap_or_else(|| { |
| 153 | + format!("Execute the '{}' skill", skill.name) |
| 154 | + }); |
| 155 | + |
| 156 | + // Execute the agent loop with skill permissions |
| 157 | + let result = agent_loop.execute(&[], &prompt, None).await?; |
| 158 | + |
| 159 | + // Return the final response as tool output |
| 160 | + Ok(ToolOutput { |
| 161 | + content: result.text, |
| 162 | + success: true, |
| 163 | + metadata: Some(serde_json::json!({ |
| 164 | + "skill_name": skill.name, |
| 165 | + "tool_calls": result.tool_calls_count, |
| 166 | + "usage": result.usage, |
| 167 | + })), |
| 168 | + images: Vec::new(), |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +#[cfg(test)] |
| 174 | +mod tests { |
| 175 | + use super::*; |
| 176 | + use crate::skills::SkillKind; |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn test_skill_permission_policy() { |
| 180 | + let skill = Skill { |
| 181 | + name: "test-skill".to_string(), |
| 182 | + description: "Test".to_string(), |
| 183 | + allowed_tools: Some("read(*), grep(*)".to_string()), |
| 184 | + disable_model_invocation: false, |
| 185 | + kind: SkillKind::Instruction, |
| 186 | + content: String::new(), |
| 187 | + tags: Vec::new(), |
| 188 | + version: None, |
| 189 | + }; |
| 190 | + |
| 191 | + let policy = SkillTool::create_skill_permission_policy(&skill); |
| 192 | + |
| 193 | + // Should allow tools in allowed-tools |
| 194 | + assert_eq!( |
| 195 | + policy.check("read", &serde_json::json!({})), |
| 196 | + PermissionDecision::Allow |
| 197 | + ); |
| 198 | + assert_eq!( |
| 199 | + policy.check("grep", &serde_json::json!({})), |
| 200 | + PermissionDecision::Allow |
| 201 | + ); |
| 202 | + |
| 203 | + // Should deny tools not in allowed-tools |
| 204 | + assert_eq!( |
| 205 | + policy.check("write", &serde_json::json!({})), |
| 206 | + PermissionDecision::Deny |
| 207 | + ); |
| 208 | + } |
| 209 | +} |
0 commit comments