bamboo_tools/tools/
slash_command_tool.rs1use 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_infrastructure::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 });
104 }
105
106 let fallback = crate::slash_commands::slash_commands_list(None)
107 .await
108 .unwrap_or_default();
109 let available = fallback
110 .iter()
111 .take(5)
112 .map(|value| value.full_command.clone())
113 .collect::<Vec<_>>();
114
115 Err(ToolError::Execution(format!(
116 "Slash command '{}' not found. Available commands: {}",
117 head,
118 available.join(", ")
119 )))
120 }
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126
127 #[tokio::test]
128 async fn slash_command_uses_session_workspace_for_project_commands() {
129 let dir = tempfile::tempdir().unwrap();
130 let commands_dir = dir.path().join(".claude/commands");
131 tokio::fs::create_dir_all(&commands_dir).await.unwrap();
132 tokio::fs::write(commands_dir.join("hello.md"), "Hi $ARGUMENTS")
133 .await
134 .unwrap();
135
136 let session = format!("session_{}", uuid::Uuid::new_v4());
137 super::workspace_state::set_workspace(&session, dir.path().to_path_buf());
138
139 let tool = SlashCommandTool::new();
140 let result = tool
141 .execute_with_context(
142 json!({ "command": "/hello world" }),
143 ToolExecutionContext {
144 session_id: Some(&session),
145 tool_call_id: "call_1",
146 event_tx: None,
147 available_tool_schemas: None,
148 },
149 )
150 .await
151 .unwrap();
152
153 let payload: serde_json::Value = serde_json::from_str(&result.result).unwrap();
154 assert_eq!(payload["resolved_command"], "/hello");
155 assert_eq!(payload["content"], "Hi world");
156 }
157}