1use 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 secret_prompt: None,
154 },
155 limits: ToolLimits {
156 max_tool_output: 30_000,
157 max_tool_buffer: 256 * 1024,
158 bash_timeout: 30,
159 bash_max_timeout: 300,
160 subagent_timeout: 300,
161 },
162 }
163}
164
165fn slash_words(input: &str) -> Vec<String> {
166 input.split_whitespace().map(str::to_string).collect()
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172 use crate::Tool;
173 use async_trait::async_trait;
174 use std::path::PathBuf;
175
176 fn cmd(command: &str, args: Vec<&str>, root: PathBuf) -> RegisteredPluginCommand {
177 RegisteredPluginCommand {
178 plugin: "p".to_string(),
179 name: "hello".to_string(),
180 description: None,
181 backend: RegisteredPluginCommandBackend::Shell {
182 command: command.to_string(),
183 args: args.into_iter().map(str::to_string).collect(),
184 },
185 plugin_root: root,
186 }
187 }
188
189 fn tempdir() -> PathBuf {
190 let dir = std::env::temp_dir().join(format!(
191 "synaps-plugin-cmd-test-{}-{}",
192 std::process::id(),
193 crate::epoch_millis()
194 ));
195 std::fs::create_dir_all(&dir).unwrap();
196 dir
197 }
198
199 #[tokio::test]
200 async fn execute_plugin_command_runs_from_plugin_root_and_appends_slash_args() {
201 let root = tempdir();
202 let command = cmd("printf", vec!["cwd=%s arg=%s", "."], root);
203
204 let output = execute_plugin_command(&command, "extra").await.unwrap();
205
206 assert_eq!(output.status, Some(0));
207 assert_eq!(output.stdout, "cwd=. arg=extra");
208 }
209
210 #[tokio::test]
211 async fn execute_plugin_command_rejects_parent_dir_command_path() {
212 let root = tempdir();
213 let command = cmd("../evil", vec![], root);
214
215 let result = execute_plugin_command(&command, "").await;
216
217 assert!(result.is_err());
218 }
219
220 #[tokio::test]
221 async fn skill_prompt_command_outputs_prompt_with_args() {
222 let command = RegisteredPluginCommand {
223 plugin: "p".to_string(),
224 name: "review".to_string(),
225 description: None,
226 backend: RegisteredPluginCommandBackend::SkillPrompt {
227 skill: "reviewer".to_string(),
228 prompt: "Review: ${args}".to_string(),
229 },
230 plugin_root: PathBuf::from("/tmp/p"),
231 };
232
233 let output = execute_plugin_command(&command, "diff").await.unwrap();
234 assert_eq!(output.status, Some(0));
235 assert!(output.stdout.contains("Review: diff"));
236 }
237
238 struct EchoTool;
239
240 #[async_trait]
241 impl Tool for EchoTool {
242 fn name(&self) -> &str {
243 "p:echo"
244 }
245 fn description(&self) -> &str {
246 "echo"
247 }
248 fn parameters(&self) -> Value {
249 serde_json::json!({"type":"object"})
250 }
251 async fn execute(&self, params: Value, _ctx: ToolContext) -> crate::Result<String> {
252 Ok(format!("echo {}", params["text"].as_str().unwrap()))
253 }
254 }
255
256 #[tokio::test]
257 async fn extension_tool_command_executes_namespaced_registered_tool() {
258 let command = RegisteredPluginCommand {
259 plugin: "p".to_string(),
260 name: "echo".to_string(),
261 description: None,
262 backend: RegisteredPluginCommandBackend::ExtensionTool {
263 tool: "echo".to_string(),
264 input: serde_json::json!({"text":"${args}"}),
265 },
266 plugin_root: PathBuf::from("/tmp/p"),
267 };
268 let registry = Arc::new(tokio::sync::RwLock::new(ToolRegistry::without_subagent()));
269 registry.write().await.register(Arc::new(EchoTool));
270
271 let output = execute_plugin_command_with_tools(&command, "hello", registry)
272 .await
273 .unwrap();
274
275 assert_eq!(output.status, Some(0));
276 assert_eq!(output.stdout, "echo hello");
277 }
278}