Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions docs/slash-command-tool-plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Slash Command Tool Implementation Plan
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This planning document is helpful but might get lost here. Consider either:

  1. Moving relevant parts to the main documentation
  2. Adding a reference in the main README
  3. Converting this to implementation comments in the code

What do you think would be most helpful for future maintainers?


## Overview

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.

## Background

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.

## Implementation Details

### 1. Tool Definition

- **Tool Name**: `execute_slash_command`
- **Purpose**: Execute slash commands programmatically
- **Parameters**:
- `command`: The slash command to execute (e.g., "review", "mode", etc.)
- `args`: Optional arguments for the command (as a string)

### 2. Architecture

#### Component Structure:

```
src/
├── shared/
│ └── tools.ts # Add ExecuteSlashCommandToolUse interface
├── core/
│ ├── tools/
│ │ └── executeSlashCommandTool.ts # New tool implementation
│ └── assistant-message/
│ └── presentAssistantMessage.ts # Add case for execute_slash_command
└── tests/
└── executeSlashCommandTool.spec.ts # Tests for the new tool
```

### 3. Key Components

#### A. Type Definition (src/shared/tools.ts)

```typescript
export interface ExecuteSlashCommandToolUse extends ToolUse {
name: "execute_slash_command"
params: Partial<Pick<Record<ToolParamName, string>, "command" | "args">>
}
```

#### B. Tool Implementation (src/core/tools/executeSlashCommandTool.ts)

The tool will:

1. Parse the command and arguments
2. Validate the command exists and is allowed
3. Execute the command through the appropriate handler
4. Return the result or error

#### C. Integration Points

1. Add to tool registry in `TOOL_DISPLAY_NAMES`
2. Add to tool groups if needed
3. Add case in `presentAssistantMessage.ts`
4. Update system prompt to include the new tool

### 4. Command Execution Strategy

Since slash commands are typically handled through the chat interface and may involve complex interactions with the Task instance, we have several options:

**Option 1: Direct Command Execution**

- Parse and execute commands directly within the tool
- Requires mapping each command to its implementation

**Option 2: Command Router Pattern**

- Create a command router that maps command names to handlers
- More extensible for future commands

**Option 3: Leverage Existing Infrastructure**

- Use the existing command handling infrastructure if available
- Most consistent with current architecture

**Recommended: Option 2** - Command Router Pattern for extensibility

### 5. Security Considerations

1. **Command Whitelist**: Only allow specific commands to be executed
2. **Permission Checks**: Ensure the LLM respects mode restrictions
3. **Argument Validation**: Validate and sanitize command arguments
4. **Audit Logging**: Log all slash command executions for debugging

### 6. Supported Commands (Initial)

For the initial implementation, support these commands:

- `/review` - Trigger code review
- `/mode [mode_name]` - Switch modes
- `/checkpoint` - Create a checkpoint
- `/diff` - Show diff view
- `/test` - Run tests

### 7. Error Handling

1. Invalid command: Return clear error message
2. Missing arguments: Provide helpful feedback
3. Permission denied: Explain why command cannot be executed
4. Command failure: Return detailed error information

### 8. Testing Strategy

1. Unit tests for the tool implementation
2. Integration tests for command execution
3. Test permission checks and restrictions
4. Test error scenarios

### 9. Documentation

1. Update tool documentation
2. Add examples to system prompt
3. Document supported commands and their arguments

## Implementation Steps

1. **Phase 1: Core Implementation**

- Create type definitions
- Implement basic tool structure
- Add to tool registry

2. **Phase 2: Command Handling**

- Implement command router
- Add initial command handlers
- Implement validation and security

3. **Phase 3: Integration**

- Integrate with presentAssistantMessage
- Update system prompt
- Add to tool groups

4. **Phase 4: Testing & Documentation**
- Write comprehensive tests
- Document the feature
- Add usage examples

## Success Criteria

1. LLM can successfully execute slash commands
2. Commands respect mode restrictions
3. Clear error messages for invalid commands
4. All tests pass
5. No regression in existing functionality

## Future Enhancements

1. Support for more complex command arguments
2. Command chaining capabilities
3. Custom command definitions
4. Command history and undo functionality
1 change: 1 addition & 0 deletions packages/types/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const toolNames = [
"fetch_instructions",
"codebase_search",
"update_todo_list",
"execute_slash_command",
] as const

export const toolNamesSchema = z.enum(toolNames)
Expand Down
16 changes: 16 additions & 0 deletions src/core/assistant-message/presentAssistantMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { askFollowupQuestionTool } from "../tools/askFollowupQuestionTool"
import { switchModeTool } from "../tools/switchModeTool"
import { attemptCompletionTool } from "../tools/attemptCompletionTool"
import { newTaskTool } from "../tools/newTaskTool"
import { executeSlashCommandTool } from "../tools/executeSlashCommandTool"

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

Expand Down Expand Up @@ -221,6 +222,11 @@ export async function presentAssistantMessage(cline: Task) {
const modeName = getModeBySlug(mode, customModes)?.name ?? mode
return `[${block.name} in ${modeName} mode: '${message}']`
}
case "execute_slash_command": {
const command = block.params.slash_command ?? "(no command)"
const args = block.params.args
return `[${block.name}: /${command}${args ? ` ${args}` : ""}]`
}
}
}

Expand Down Expand Up @@ -546,6 +552,16 @@ export async function presentAssistantMessage(cline: Task) {
askFinishSubTaskApproval,
)
break
case "execute_slash_command":
await executeSlashCommandTool(
cline,
block,
askApproval,
handleError,
pushToolResult,
removeClosingTag,
)
break
}

break
Expand Down
52 changes: 52 additions & 0 deletions src/core/prompts/tools/execute-slash-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ToolArgs } from "./types"

export function getExecuteSlashCommandDescription(args: ToolArgs): string {
return `## execute_slash_command
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.
Parameters:
- slash_command: (required) The name of the slash command to execute (without the "/" prefix). Available commands:
- review: Trigger code review for current changes (requires args like "slack comment: <message>" or "github issue #123")
- mode: Switch to a different mode (requires mode name as args, e.g., "code", "architect", "debug")
- checkpoint: Create a checkpoint of current changes
- diff: Show diff view for current changes
- test: Run tests for the project (optionally specify test command as args)
- args: (optional) Arguments to pass to the slash command. Required for some commands like "review" and "mode".

Usage:
<execute_slash_command>
<slash_command>command_name</slash_command>
<args>optional arguments</args>
</execute_slash_command>

Examples:

1. Trigger a code review:
<execute_slash_command>
<slash_command>review</slash_command>
<args>slack comment: Please review the authentication implementation</args>
</execute_slash_command>

2. Switch to architect mode:
<execute_slash_command>
<slash_command>mode</slash_command>
<args>architect</args>
</execute_slash_command>

3. Create a checkpoint:
<execute_slash_command>
<slash_command>checkpoint</slash_command>
</execute_slash_command>

4. Show diff view:
<execute_slash_command>
<slash_command>diff</slash_command>
</execute_slash_command>

5. Run tests with custom command:
<execute_slash_command>
<slash_command>test</slash_command>
<args>npm run test:unit</args>
</execute_slash_command>

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.`
}
3 changes: 3 additions & 0 deletions src/core/prompts/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getSwitchModeDescription } from "./switch-mode"
import { getNewTaskDescription } from "./new-task"
import { getCodebaseSearchDescription } from "./codebase-search"
import { getUpdateTodoListDescription } from "./update-todo-list"
import { getExecuteSlashCommandDescription } from "./execute-slash-command"
import { CodeIndexManager } from "../../../services/code-index/manager"

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

export function getToolDescriptionsForMode(
Expand Down Expand Up @@ -164,4 +166,5 @@ export {
getInsertContentDescription,
getSearchAndReplaceDescription,
getCodebaseSearchDescription,
getExecuteSlashCommandDescription,
}
Loading
Loading