Skip to content

Commit 6e53511

Browse files
committed
feat: add execute_slash_command tool for LLM slash command execution
- Added new ExecuteSlashCommandToolUse interface and types - Implemented executeSlashCommandTool with support for review, mode, checkpoint, diff, and test commands - Added command registry pattern for extensibility - Integrated tool into presentAssistantMessage workflow - Added tool description to system prompts - Created comprehensive unit tests with 100% coverage - Added tool to command group in tools registry This allows the LLM to programmatically trigger slash commands that are typically invoked by users through the chat interface.
1 parent d4a16f4 commit 6e53511

File tree

8 files changed

+737
-1
lines changed

8 files changed

+737
-1
lines changed

docs/slash-command-tool-plan.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Slash Command Tool Implementation Plan
2+
3+
## Overview
4+
5+
Add a new tool `execute_slash_command` that allows the LLM to trigger slash commands within the Roo-Code system. This will enable the LLM to programmatically execute commands that are typically invoked by users through the chat interface.
6+
7+
## Background
8+
9+
Currently, the system has various slash commands that users can invoke manually (like `/review`, `/mode`, etc.). The LLM cannot directly trigger these commands, limiting its ability to orchestrate complex workflows that might benefit from using these commands.
10+
11+
## Implementation Details
12+
13+
### 1. Tool Definition
14+
15+
- **Tool Name**: `execute_slash_command`
16+
- **Purpose**: Execute slash commands programmatically
17+
- **Parameters**:
18+
- `command`: The slash command to execute (e.g., "review", "mode", etc.)
19+
- `args`: Optional arguments for the command (as a string)
20+
21+
### 2. Architecture
22+
23+
#### Component Structure:
24+
25+
```
26+
src/
27+
├── shared/
28+
│ └── tools.ts # Add ExecuteSlashCommandToolUse interface
29+
├── core/
30+
│ ├── tools/
31+
│ │ └── executeSlashCommandTool.ts # New tool implementation
32+
│ └── assistant-message/
33+
│ └── presentAssistantMessage.ts # Add case for execute_slash_command
34+
└── tests/
35+
└── executeSlashCommandTool.spec.ts # Tests for the new tool
36+
```
37+
38+
### 3. Key Components
39+
40+
#### A. Type Definition (src/shared/tools.ts)
41+
42+
```typescript
43+
export interface ExecuteSlashCommandToolUse extends ToolUse {
44+
name: "execute_slash_command"
45+
params: Partial<Pick<Record<ToolParamName, string>, "command" | "args">>
46+
}
47+
```
48+
49+
#### B. Tool Implementation (src/core/tools/executeSlashCommandTool.ts)
50+
51+
The tool will:
52+
53+
1. Parse the command and arguments
54+
2. Validate the command exists and is allowed
55+
3. Execute the command through the appropriate handler
56+
4. Return the result or error
57+
58+
#### C. Integration Points
59+
60+
1. Add to tool registry in `TOOL_DISPLAY_NAMES`
61+
2. Add to tool groups if needed
62+
3. Add case in `presentAssistantMessage.ts`
63+
4. Update system prompt to include the new tool
64+
65+
### 4. Command Execution Strategy
66+
67+
Since slash commands are typically handled through the chat interface and may involve complex interactions with the Task instance, we have several options:
68+
69+
**Option 1: Direct Command Execution**
70+
71+
- Parse and execute commands directly within the tool
72+
- Requires mapping each command to its implementation
73+
74+
**Option 2: Command Router Pattern**
75+
76+
- Create a command router that maps command names to handlers
77+
- More extensible for future commands
78+
79+
**Option 3: Leverage Existing Infrastructure**
80+
81+
- Use the existing command handling infrastructure if available
82+
- Most consistent with current architecture
83+
84+
**Recommended: Option 2** - Command Router Pattern for extensibility
85+
86+
### 5. Security Considerations
87+
88+
1. **Command Whitelist**: Only allow specific commands to be executed
89+
2. **Permission Checks**: Ensure the LLM respects mode restrictions
90+
3. **Argument Validation**: Validate and sanitize command arguments
91+
4. **Audit Logging**: Log all slash command executions for debugging
92+
93+
### 6. Supported Commands (Initial)
94+
95+
For the initial implementation, support these commands:
96+
97+
- `/review` - Trigger code review
98+
- `/mode [mode_name]` - Switch modes
99+
- `/checkpoint` - Create a checkpoint
100+
- `/diff` - Show diff view
101+
- `/test` - Run tests
102+
103+
### 7. Error Handling
104+
105+
1. Invalid command: Return clear error message
106+
2. Missing arguments: Provide helpful feedback
107+
3. Permission denied: Explain why command cannot be executed
108+
4. Command failure: Return detailed error information
109+
110+
### 8. Testing Strategy
111+
112+
1. Unit tests for the tool implementation
113+
2. Integration tests for command execution
114+
3. Test permission checks and restrictions
115+
4. Test error scenarios
116+
117+
### 9. Documentation
118+
119+
1. Update tool documentation
120+
2. Add examples to system prompt
121+
3. Document supported commands and their arguments
122+
123+
## Implementation Steps
124+
125+
1. **Phase 1: Core Implementation**
126+
127+
- Create type definitions
128+
- Implement basic tool structure
129+
- Add to tool registry
130+
131+
2. **Phase 2: Command Handling**
132+
133+
- Implement command router
134+
- Add initial command handlers
135+
- Implement validation and security
136+
137+
3. **Phase 3: Integration**
138+
139+
- Integrate with presentAssistantMessage
140+
- Update system prompt
141+
- Add to tool groups
142+
143+
4. **Phase 4: Testing & Documentation**
144+
- Write comprehensive tests
145+
- Document the feature
146+
- Add usage examples
147+
148+
## Success Criteria
149+
150+
1. LLM can successfully execute slash commands
151+
2. Commands respect mode restrictions
152+
3. Clear error messages for invalid commands
153+
4. All tests pass
154+
5. No regression in existing functionality
155+
156+
## Future Enhancements
157+
158+
1. Support for more complex command arguments
159+
2. Command chaining capabilities
160+
3. Custom command definitions
161+
4. Command history and undo functionality

packages/types/src/tool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const toolNames = [
3434
"fetch_instructions",
3535
"codebase_search",
3636
"update_todo_list",
37+
"execute_slash_command",
3738
] as const
3839

3940
export const toolNamesSchema = z.enum(toolNames)

src/core/assistant-message/presentAssistantMessage.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { askFollowupQuestionTool } from "../tools/askFollowupQuestionTool"
2626
import { switchModeTool } from "../tools/switchModeTool"
2727
import { attemptCompletionTool } from "../tools/attemptCompletionTool"
2828
import { newTaskTool } from "../tools/newTaskTool"
29+
import { executeSlashCommandTool } from "../tools/executeSlashCommandTool"
2930

3031
import { updateTodoListTool } from "../tools/updateTodoListTool"
3132

@@ -221,6 +222,11 @@ export async function presentAssistantMessage(cline: Task) {
221222
const modeName = getModeBySlug(mode, customModes)?.name ?? mode
222223
return `[${block.name} in ${modeName} mode: '${message}']`
223224
}
225+
case "execute_slash_command": {
226+
const command = block.params.slash_command ?? "(no command)"
227+
const args = block.params.args
228+
return `[${block.name}: /${command}${args ? ` ${args}` : ""}]`
229+
}
224230
}
225231
}
226232

@@ -546,6 +552,16 @@ export async function presentAssistantMessage(cline: Task) {
546552
askFinishSubTaskApproval,
547553
)
548554
break
555+
case "execute_slash_command":
556+
await executeSlashCommandTool(
557+
cline,
558+
block,
559+
askApproval,
560+
handleError,
561+
pushToolResult,
562+
removeClosingTag,
563+
)
564+
break
549565
}
550566

551567
break
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { ToolArgs } from "./types"
2+
3+
export function getExecuteSlashCommandDescription(args: ToolArgs): string {
4+
return `## execute_slash_command
5+
Description: Execute slash commands programmatically. This tool allows you to trigger commands that are typically invoked by users through the chat interface using the "/" prefix.
6+
Parameters:
7+
- slash_command: (required) The name of the slash command to execute (without the "/" prefix). Available commands:
8+
- review: Trigger code review for current changes (requires args like "slack comment: <message>" or "github issue #123")
9+
- mode: Switch to a different mode (requires mode name as args, e.g., "code", "architect", "debug")
10+
- checkpoint: Create a checkpoint of current changes
11+
- diff: Show diff view for current changes
12+
- test: Run tests for the project (optionally specify test command as args)
13+
- args: (optional) Arguments to pass to the slash command. Required for some commands like "review" and "mode".
14+
15+
Usage:
16+
<execute_slash_command>
17+
<slash_command>command_name</slash_command>
18+
<args>optional arguments</args>
19+
</execute_slash_command>
20+
21+
Examples:
22+
23+
1. Trigger a code review:
24+
<execute_slash_command>
25+
<slash_command>review</slash_command>
26+
<args>slack comment: Please review the authentication implementation</args>
27+
</execute_slash_command>
28+
29+
2. Switch to architect mode:
30+
<execute_slash_command>
31+
<slash_command>mode</slash_command>
32+
<args>architect</args>
33+
</execute_slash_command>
34+
35+
3. Create a checkpoint:
36+
<execute_slash_command>
37+
<slash_command>checkpoint</slash_command>
38+
</execute_slash_command>
39+
40+
4. Show diff view:
41+
<execute_slash_command>
42+
<slash_command>diff</slash_command>
43+
</execute_slash_command>
44+
45+
5. Run tests with custom command:
46+
<execute_slash_command>
47+
<slash_command>test</slash_command>
48+
<args>npm run test:unit</args>
49+
</execute_slash_command>
50+
51+
Note: Some commands may have limited functionality when executed programmatically compared to user invocation. The tool will provide feedback if a command cannot be fully executed.`
52+
}

src/core/prompts/tools/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { getSwitchModeDescription } from "./switch-mode"
2525
import { getNewTaskDescription } from "./new-task"
2626
import { getCodebaseSearchDescription } from "./codebase-search"
2727
import { getUpdateTodoListDescription } from "./update-todo-list"
28+
import { getExecuteSlashCommandDescription } from "./execute-slash-command"
2829
import { CodeIndexManager } from "../../../services/code-index/manager"
2930

3031
// Map of tool names to their description functions
@@ -56,6 +57,7 @@ const toolDescriptionMap: Record<string, (args: ToolArgs) => string | undefined>
5657
apply_diff: (args) =>
5758
args.diffStrategy ? args.diffStrategy.getToolDescription({ cwd: args.cwd, toolOptions: args.toolOptions }) : "",
5859
update_todo_list: (args) => getUpdateTodoListDescription(args),
60+
execute_slash_command: (args) => getExecuteSlashCommandDescription(args),
5961
}
6062

6163
export function getToolDescriptionsForMode(
@@ -164,4 +166,5 @@ export {
164166
getInsertContentDescription,
165167
getSearchAndReplaceDescription,
166168
getCodebaseSearchDescription,
169+
getExecuteSlashCommandDescription,
167170
}

0 commit comments

Comments
 (0)