Skip to main content

bamboo_tools/tools/
slash_command_tool.rs

1use async_trait::async_trait;
2use bamboo_agent_core::{Tool, ToolError, ToolExecutionContext, ToolResult};
3use serde::Deserialize;
4use serde_json::json;
5
6use super::workspace_state;
7
8#[derive(Debug, Deserialize)]
9struct SlashCommandArgs {
10    command: String,
11}
12
13pub struct SlashCommandTool;
14
15impl SlashCommandTool {
16    pub fn new() -> Self {
17        Self
18    }
19}
20
21impl Default for SlashCommandTool {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27#[async_trait]
28impl Tool for SlashCommandTool {
29    fn name(&self) -> &str {
30        "SlashCommand"
31    }
32
33    fn description(&self) -> &str {
34        "Execute a slash command within the main conversation"
35    }
36
37    fn parameters_schema(&self) -> serde_json::Value {
38        json!({
39            "type": "object",
40            "properties": {
41                "command": {
42                    "type": "string",
43                    "description": "Slash command text, including arguments"
44                }
45            },
46            "required": ["command"],
47            "additionalProperties": false
48        })
49    }
50
51    async fn execute(&self, args: serde_json::Value) -> Result<ToolResult, ToolError> {
52        self.execute_with_context(args, ToolExecutionContext::none("SlashCommand"))
53            .await
54    }
55
56    async fn execute_with_context(
57        &self,
58        args: serde_json::Value,
59        ctx: ToolExecutionContext<'_>,
60    ) -> Result<ToolResult, ToolError> {
61        let parsed: SlashCommandArgs = serde_json::from_value(args).map_err(|e| {
62            ToolError::InvalidArguments(format!("Invalid SlashCommand args: {}", e))
63        })?;
64
65        let raw = parsed.command.trim();
66        if raw.is_empty() {
67            return Err(ToolError::InvalidArguments(
68                "command cannot be empty".to_string(),
69            ));
70        }
71
72        let mut parts = raw.split_whitespace();
73        let head = parts.next().unwrap_or_default();
74        let tail = parts.collect::<Vec<_>>().join(" ");
75
76        let project_path = Some(bamboo_config::paths::path_to_display_string(
77            &workspace_state::workspace_or_process_cwd(ctx.session_id),
78        ));
79
80        let commands = crate::slash_commands::slash_commands_list(project_path)
81            .await
82            .map_err(ToolError::Execution)?;
83
84        if let Some(command) = commands
85            .into_iter()
86            .find(|value| value.full_command == head)
87        {
88            let resolved = if command.accepts_arguments {
89                command.content.replace("$ARGUMENTS", tail.trim())
90            } else {
91                command.content.clone()
92            };
93
94            return Ok(ToolResult {
95                success: true,
96                result: json!({
97                    "command": raw,
98                    "resolved_command": command.full_command,
99                    "content": resolved,
100                })
101                .to_string(),
102                display_preference: Some("Collapsible".to_string()),
103                images: Vec::new(),
104            });
105        }
106
107        let fallback = crate::slash_commands::slash_commands_list(None)
108            .await
109            .unwrap_or_default();
110        let available = fallback
111            .iter()
112            .take(5)
113            .map(|value| value.full_command.clone())
114            .collect::<Vec<_>>();
115
116        Err(ToolError::Execution(format!(
117            "Slash command '{}' not found. Available commands: {}",
118            head,
119            available.join(", ")
120        )))
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127
128    #[tokio::test]
129    async fn slash_command_uses_session_workspace_for_project_commands() {
130        let dir = tempfile::tempdir().unwrap();
131        let commands_dir = dir.path().join(".claude/commands");
132        tokio::fs::create_dir_all(&commands_dir).await.unwrap();
133        tokio::fs::write(commands_dir.join("hello.md"), "Hi $ARGUMENTS")
134            .await
135            .unwrap();
136
137        let session = format!("session_{}", uuid::Uuid::new_v4());
138        super::workspace_state::set_workspace(&session, dir.path().to_path_buf());
139
140        let tool = SlashCommandTool::new();
141        let result = tool
142            .execute_with_context(
143                json!({ "command": "/hello world" }),
144                ToolExecutionContext {
145                    session_id: Some(&session),
146                    tool_call_id: "call_1",
147                    event_tx: None,
148                    available_tool_schemas: None,
149                    bypass_permissions: false,
150                    can_async_resume: false,
151                },
152            )
153            .await
154            .unwrap();
155
156        let payload: serde_json::Value = serde_json::from_str(&result.result).unwrap();
157        assert_eq!(payload["resolved_command"], "/hello");
158        assert_eq!(payload["content"], "Hi world");
159    }
160}