Skip to main content

agent_engine/skills/
commands.rs

1//! Execution helper for plugin manifest commands.
2
3use std::path::{Component, Path};
4use std::process::Stdio;
5use std::sync::Arc;
6
7use serde_json::Value;
8
9use crate::skills::registry::{RegisteredPluginCommand, RegisteredPluginCommandBackend};
10use crate::tools::{ToolCapabilities, ToolChannels, ToolLimits};
11use crate::{ToolContext, ToolRegistry};
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct PluginCommandOutput {
15    pub status: Option<i32>,
16    pub stdout: String,
17    pub stderr: String,
18}
19
20fn validate_command_path(command: &str) -> crate::Result<()> {
21    let path = Path::new(command);
22    if path.is_absolute() {
23        return Err(crate::RuntimeError::Tool(
24            "plugin command must be relative to plugin root or resolved from PATH".to_string(),
25        ));
26    }
27    if path.components().any(|c| matches!(c, Component::ParentDir)) {
28        return Err(crate::RuntimeError::Tool(
29            "plugin command may not contain '..' path components".to_string(),
30        ));
31    }
32    Ok(())
33}
34
35fn interpolate_slash_args(value: Value, slash_args: &str) -> Value {
36    match value {
37        Value::String(s) if s == "${args}" => Value::String(slash_args.to_string()),
38        Value::String(s) => Value::String(s.replace("${args}", slash_args)),
39        Value::Array(items) => Value::Array(
40            items
41                .into_iter()
42                .map(|v| interpolate_slash_args(v, slash_args))
43                .collect(),
44        ),
45        Value::Object(obj) => Value::Object(
46            obj.into_iter()
47                .map(|(k, v)| (k, interpolate_slash_args(v, slash_args)))
48                .collect(),
49        ),
50        other => other,
51    }
52}
53
54pub async fn execute_plugin_command(
55    command: &RegisteredPluginCommand,
56    slash_args: &str,
57) -> crate::Result<PluginCommandOutput> {
58    match &command.backend {
59        RegisteredPluginCommandBackend::Shell {
60            command: executable,
61            args,
62        } => {
63            validate_command_path(executable)?;
64
65            let mut cmd = tokio::process::Command::new(executable);
66            cmd.current_dir(&command.plugin_root)
67                .args(args)
68                .args(slash_words(slash_args))
69                .stdin(Stdio::null())
70                .stdout(Stdio::piped())
71                .stderr(Stdio::piped());
72
73            let output = cmd.output().await.map_err(|e| {
74                crate::RuntimeError::Tool(format!(
75                    "failed to run plugin command /{}:{}: {}",
76                    command.plugin, command.name, e
77                ))
78            })?;
79
80            Ok(PluginCommandOutput {
81                status: output.status.code(),
82                stdout: String::from_utf8_lossy(&output.stdout).to_string(),
83                stderr: String::from_utf8_lossy(&output.stderr).to_string(),
84            })
85        }
86        RegisteredPluginCommandBackend::SkillPrompt { skill, prompt } => {
87            let mut text = prompt.replace("${args}", slash_args);
88            if !slash_args.trim().is_empty() && !prompt.contains("${args}") {
89                text.push('\n');
90                text.push_str(slash_args);
91            }
92            Ok(PluginCommandOutput {
93                status: Some(0),
94                stdout: format!(
95                    "Skill prompt for /{}:{} (skill: {})\n{}",
96                    command.plugin, command.name, skill, text
97                ),
98                stderr: String::new(),
99            })
100        }
101        RegisteredPluginCommandBackend::ExtensionTool { .. } => Err(crate::RuntimeError::Tool(
102            "extension-backed plugin command requires execute_plugin_command_with_tools"
103                .to_string(),
104        )),
105        RegisteredPluginCommandBackend::Interactive { .. } => Err(crate::RuntimeError::Tool(
106            "interactive plugin command requires ExtensionManager::invoke_command".to_string(),
107        )),
108    }
109}
110
111pub async fn execute_plugin_command_with_tools(
112    command: &RegisteredPluginCommand,
113    slash_args: &str,
114    tools: Arc<tokio::sync::RwLock<ToolRegistry>>,
115) -> crate::Result<PluginCommandOutput> {
116    if let RegisteredPluginCommandBackend::ExtensionTool { tool, input } = &command.backend {
117        let runtime_tool_name = format!("{}:{}", command.plugin, tool);
118        let params = interpolate_slash_args(input.clone(), slash_args);
119        let registry = tools.read().await;
120        let tool = registry
121            .get(&runtime_tool_name)
122            .ok_or_else(|| {
123                crate::RuntimeError::Tool(format!(
124                    "extension tool '{}' is not registered",
125                    runtime_tool_name
126                ))
127            })?
128            .clone();
129        drop(registry);
130        let stdout = tool.execute(params, empty_tool_context()).await?;
131        Ok(PluginCommandOutput {
132            status: Some(0),
133            stdout,
134            stderr: String::new(),
135        })
136    } else {
137        execute_plugin_command(command, slash_args).await
138    }
139}
140
141fn empty_tool_context() -> ToolContext {
142    ToolContext {
143        channels: ToolChannels {
144            tx_delta: None,
145            tx_events: None,
146        },
147        capabilities: ToolCapabilities {
148            watcher_exit_path: None,
149            tool_register_tx: None,
150            session_manager: None,
151            subagent_registry: None,
152            event_queue: None,
153            delegation_parent: None,
154            secret_prompt: None,
155            orchestration: None,
156            tool_activation: None,
157            mcp_leases: None,
158            extension_leases: None,
159            memory_context: None,
160        },
161        limits: ToolLimits {
162            max_tool_output: 30_000,
163            max_tool_buffer: 256 * 1024,
164            bash_timeout: 30,
165            bash_max_timeout: 300,
166            subagent_timeout: 300,
167        },
168    }
169}
170
171fn slash_words(input: &str) -> Vec<String> {
172    input.split_whitespace().map(str::to_string).collect()
173}
174
175#[cfg(test)]
176mod tests {
177    use super::*;
178    use crate::Tool;
179    use async_trait::async_trait;
180    use std::path::PathBuf;
181
182    fn cmd(command: &str, args: Vec<&str>, root: PathBuf) -> RegisteredPluginCommand {
183        RegisteredPluginCommand {
184            plugin: "p".to_string(),
185            name: "hello".to_string(),
186            description: None,
187            backend: RegisteredPluginCommandBackend::Shell {
188                command: command.to_string(),
189                args: args.into_iter().map(str::to_string).collect(),
190            },
191            plugin_root: root,
192        }
193    }
194
195    fn tempdir() -> PathBuf {
196        let dir = std::env::temp_dir().join(format!(
197            "synaps-plugin-cmd-test-{}-{}",
198            std::process::id(),
199            crate::epoch_millis()
200        ));
201        std::fs::create_dir_all(&dir).unwrap();
202        dir
203    }
204
205    #[tokio::test]
206    async fn execute_plugin_command_runs_from_plugin_root_and_appends_slash_args() {
207        let root = tempdir();
208        let command = cmd("printf", vec!["cwd=%s arg=%s", "."], root);
209
210        let output = execute_plugin_command(&command, "extra").await.unwrap();
211
212        assert_eq!(output.status, Some(0));
213        assert_eq!(output.stdout, "cwd=. arg=extra");
214    }
215
216    #[tokio::test]
217    async fn execute_plugin_command_rejects_parent_dir_command_path() {
218        let root = tempdir();
219        let command = cmd("../evil", vec![], root);
220
221        let result = execute_plugin_command(&command, "").await;
222
223        assert!(result.is_err());
224    }
225
226    #[tokio::test]
227    async fn skill_prompt_command_outputs_prompt_with_args() {
228        let command = RegisteredPluginCommand {
229            plugin: "p".to_string(),
230            name: "review".to_string(),
231            description: None,
232            backend: RegisteredPluginCommandBackend::SkillPrompt {
233                skill: "reviewer".to_string(),
234                prompt: "Review: ${args}".to_string(),
235            },
236            plugin_root: PathBuf::from("/tmp/p"),
237        };
238
239        let output = execute_plugin_command(&command, "diff").await.unwrap();
240        assert_eq!(output.status, Some(0));
241        assert!(output.stdout.contains("Review: diff"));
242    }
243
244    struct EchoTool;
245
246    #[async_trait]
247    impl Tool for EchoTool {
248        fn name(&self) -> &str {
249            "p:echo"
250        }
251        fn description(&self) -> &str {
252            "echo"
253        }
254        fn parameters(&self) -> Value {
255            serde_json::json!({"type":"object"})
256        }
257        async fn execute(&self, params: Value, _ctx: ToolContext) -> crate::Result<String> {
258            Ok(format!("echo {}", params["text"].as_str().unwrap()))
259        }
260    }
261
262    #[tokio::test]
263    async fn extension_tool_command_executes_namespaced_registered_tool() {
264        let command = RegisteredPluginCommand {
265            plugin: "p".to_string(),
266            name: "echo".to_string(),
267            description: None,
268            backend: RegisteredPluginCommandBackend::ExtensionTool {
269                tool: "echo".to_string(),
270                input: serde_json::json!({"text":"${args}"}),
271            },
272            plugin_root: PathBuf::from("/tmp/p"),
273        };
274        let registry = Arc::new(tokio::sync::RwLock::new(ToolRegistry::without_subagent()));
275        registry.write().await.register(Arc::new(EchoTool));
276
277        let output = execute_plugin_command_with_tools(&command, "hello", registry)
278            .await
279            .unwrap();
280
281        assert_eq!(output.status, Some(0));
282        assert_eq!(output.stdout, "echo hello");
283    }
284}