斜杠命令
使用简单的 Markdown 文件创建自定义斜杠命令,以自动化重复性任务并扩展 Roo Code 的功能。
在聊天中输入
/ 以查看所有可用命令,或通过在 .roo/commands/ 或 ~/.roo/commands/ 中添加 Markdown 文件来创建你自己的命令!概述
斜杠命令允许你创建可重用的提示和工作流程,并可即时触发。使用简单的 Markdown 文件,将复杂的多步流程转变为单个命令,标准化团队实践,并自动化重复性任务。
主要优点:
- 工作流程自动化: 将复杂的多步流程转变为单个命令。
- 团队标准化: 在你的团队中共享命令,以实现一致的实践。
- 上下文保留: 在每个命令中包含特定于项目的上下文。
- 快速访问: 模糊搜索和自动完成,实现即时命令发现。
创建自定义命令
自定义命令通过在特定目录中添加 Markdown 文件来扩展 Roo Code 的功能:
- 项目特定: 你工作区根目录中的
.roo/commands/ - 全局: 你主目录中的
~/.roo/commands/
文件名将成为命令名称。例如:
review.md→/reviewtest-api.md→/test-apideploy-check.md→/deploy-check
通过 UI 创建命令时,命令名称会自动处理:
- 转换为小写
- 空格替换为短横线
- 特殊字符被移除(短横线除外)
- 多个连续的短横线替换为单个短横线
- 移除前导/尾随短横线
“My Cool Command!” 变为 my-cool-command。基本命令格式
通过添加 Markdown 文件来创建简单命令:
# review.md
Please review this code for:
- Performance issues
- Security vulnerabilities
- Code style violations
- Potential bugs
带 Frontmatter 的高级命令
使用 Frontmatter 添加元数据以增强功能:
---
description: Comprehensive code review focusing on security and performance
argument-hint: <file or directory to review>
---
# Security-First Code Review
Please perform a thorough security review of the selected code:
1. **Authentication & Authorization**
- Check for proper access controls
- Verify token validation
- Review permission checks
2. **Input Validation**
- Identify potential injection points
- Check for proper sanitization
- Review data type validation
3. **Security Best Practices**
- Look for hardcoded secrets
- Check for secure communication
- Review error handling for information leakage
Frontmatter 字段:
description:出现在命令菜单中,帮助用户理解命令的目的。argument-hint:(可选) 提供有关使用命令时预期参数的提示。有关详细信息,请参阅 参数提示。
命令管理
Roo Code 提供了多种管理自定义命令的方式。
创建新命令:
- 在聊天中输入
/以打开斜杠命令菜单。 - 在输入字段中输入你的命令名称(例如,“
Sample command name”)。 - 点击
+按钮以创建命令。 - 新文件将自动创建并打开(例如,
sample-command-name.md)。 - 文件将包含模板内容,以帮助你入门。
使用斜杠命令
在聊天中输入 / 以查看包含所有可用命令的统一菜单。菜单在同一界面中显示自定义工作流程命令、模式切换命令和内置命令。
- 统一菜单: 所有命令类型一起出现。
- 自动完成: 开始输入以筛选命令(例如,
/sam显示sample-command-name)。 - 基本筛选: 查找与文本匹配的命令。
- 描述预览: 在菜单中查看命令描述。
- 命令优先级: 项目命令覆盖同名的全局命令。
参数提示
参数提示为斜杠命令提供即时帮助,当命令需要额外输入时,向你展示应提供哪种信息。
当你输入 / 调出命令菜单时,需要参数的命令旁边会显示一个浅灰色提示。此提示会告诉你命令需要哪种参数。
例如:
/mode <mode_slug>- 提示<mode_slug>表示你应该提供一个模式名称,如code或debug。/api-endpoint <endpoint-name> <http-method>- 显示你需要端点名称和 HTTP 方法。
选择命令后,它将插入到聊天输入中,后面跟着一个空格。提示不会被插入;它只是一个视觉指南,帮助你了解接下来要输入什么。然后你必须在命令后手动输入参数。
向自定义命令添加参数提示
你可以使用 frontmatter 中的 argument-hint 字段向自定义命令添加参数提示:
---
description: Generate a new REST API endpoint with best practices
argument-hint: <endpoint-name> <http-method>
---
这将在命令菜单中显示为 /api-endpoint <endpoint-name> <http-method>。
参数提示的最佳实践:
- 具体: 使用描述性占位符,如
<file-path>,而不是通用的,如<arg>。 - 显示多个参数: 如果你的命令需要多个输入,请全部显示:
<source> <destination>。 - 使用一致的格式: 始终将占位符用尖括号括起来:
<placeholder>。 - 保持简洁: 提示应简短而清晰。
常见问题:
- “如果我不提供参数怎么办?” 命令可能无法按预期工作,或者它可能会提示你提供更多信息。提示是为了帮助你第一次就做对。
- “所有命令都有提示吗?” 不,只有设计为接受参数的命令才会有提示。不需要额外输入的命令不会显示提示。
- “我可以使用命令而不替换提示吗?” 提示文本(如
<mode_slug>)需要用实际值替换。保留提示文本可能会导致命令失败或行为异常。
示例和用例
开发工作流程
API 端点生成器
---
description: Generate a new REST API endpoint with best practices
argument-hint: <endpoint-name> <http-method>
---
Create a new REST API endpoint with the following specifications:
- Proper error handling
- Input validation
- Authentication middleware
- OpenAPI documentation
- Unit tests
- Integration tests
Follow our project's API conventions and patterns.
数据库迁移助手
---
description: Create a database migration with rollback support
---
Generate a database migration that:
1. Includes both up and down migrations
2. Has proper transaction handling
3. Includes data validation
4. Provides clear migration descriptions
5. Follows our naming conventions
Remember to check for dependent migrations and data integrity.
代码质量
性能分析器
---
description: Analyze code for performance bottlenecks
---
Analyze the selected code for performance issues:
- Identify O(n²) or worse algorithms
- Find unnecessary database queries
- Detect memory leaks
- Suggest caching opportunities
- Recommend async/await optimizations
- Check for proper resource cleanup
重构助手
---
description: Suggest refactoring improvements for cleaner code
---
Review this code and suggest refactoring improvements:
- Extract repeated code into functions
- Improve variable and function names
- Simplify complex conditionals
- Apply SOLID principles
- Reduce coupling between components
- Improve testability
文档
README 生成器
---
description: Create a comprehensive README for the current project
---
Generate a README.md file that includes:
1. Project title and description
2. Installation instructions
3. Usage examples
4. API documentation
5. Configuration options
6. Contributing guidelines
7. License information
Base it on the current project structure and existing code.
API 文档
---
description: Generate OpenAPI/Swagger documentation
---
Create OpenAPI 3.0 documentation for the API endpoints in this file:
- Include all HTTP methods
- Document request/response schemas
- Add example requests and responses
- Include authentication requirements
- Document error responses
- Add descriptive summaries
测试
测试生成器
---
description: Generate comprehensive test suites
---
Create tests for the selected code:
1. Unit tests for all public methods
2. Edge case testing
3. Error handling tests
4. Mock external dependencies
5. Performance benchmarks
6. Integration tests where applicable
Use our project's testing framework and conventions.
测试覆盖率分析器
---
description: Identify missing test coverage
---
Analyze the current test coverage and:
- Identify untested code paths
- Suggest additional test cases
- Find edge cases not covered
- Recommend integration tests
- Check for proper error testing
最佳实践
命令命名:
- 使用描述性、面向动作的名称。
- 保持名称简洁但清晰。
- 对于多词命令使用连字符。
- 避免使用
help或test等通用名称。 - 注意: 名称会自动转化为
slug(小写,移除特殊字符)。 md扩展名会根据需要自动添加/移除。
命令内容:
- 以清晰的指令开头。
- 使用结构化格式(列表、部分)。
- 包含具体要求。
- 引用项目约定。
- 保持命令专注于单个任务。
组织:
- 在子目录中对相关命令进行分组。
- 使用一致的命名模式。
- 记录复杂命令。
- 对你的命令进行版本控制。
- 在项目仓库中共享团队命令。
内置命令
Roo Code 包含强大的内置命令,提供专业功能:
init 命令
/init 命令是一个全面的 AI 助手设置工具,它会分析你的代码库并创建定制的配置文件。这个强大的命令:
执行多阶段分析:
- 发现阶段: 扫描你的项目结构并识别关键技术。
- 项目识别: 确定项目类型、框架和依赖项。
- 架构映射: 分析代码组织和模式。
- 构建/测试检测: 识别构建工具、测试框架和脚本。
- 代码风格提取: 捕获编码约定和模式。
创建 AI 助手配置:
- 在
.roo/rules-*目录中生成特定于模式的AGENTS.md文件。 - 为不同的 AI 助手模式(代码、架构师、调试等)创建详细规则。
- 遵循“只关注非显而易见之处”的原则,生成简洁、高信号的文档。
- 支持多种 AI 助手格式(Claude、Cursor、Copilot)。
管理项目设置:
- 为项目初始化创建全面的待办事项列表。
- 识别安全和性能考虑因素。
- 记录特定于项目的约定和模式。
- 对生成的文档强制执行质量标准。
用法:
只需在聊天中输入 /init,即可分析你的代码库并设置根据你的项目定制的 AI 助手配置文件。
init 命令在开始一个新项目或当你想在你的团队中建立一致的 AI 助手行为时特别有用。故障排除
命令未出现:
- 检查文件位置: 确保自定义命令文件位于
.roo/commands/或~/.roo/commands/中。 - 验证文件扩展名: 自定义命令必须是
.md文件。 - 重新加载窗口: 有时需要重新加载 VS Code 才能检测到新的命令文件。
命令未找到: 当找不到斜杠命令时,LLM 将看到一条错误消息,指示命令应位于何处。这有助于指导你在正确的位置创建命令。
命令模板内容: 通过 UI 创建的新命令会收到模板内容,以帮助你入门。此模板包含你可以自定义的基本结构和示例。
命令冲突:
- 项目命令(
.roo/commands/)会覆盖同名的全局命令(~/.roo/commands/)。 - 内置命令无法被覆盖。
- 通过 UI 创建重复名称时,会自动附加数字(例如,
new-command-1,new-command-2)。
文件系统错误:
- 权限问题: 确保你对
.roo/commands/目录有写入权限。 - 目录创建: 如果命令目录不存在,系统将尝试创建它。
- 符号链接: 命令目录支持符号链接,用于跨项目共享命令。
斜杠菜单包含模式切换命令(如
/code、/ask),它们从根本上改变了 AI 的操作模式——它们不仅仅是注入文本,而是切换整个 AI 上下文。你创建的自定义模式也会显示为斜杠命令(例如,一个 slug 为 reviewer 的模式会变为 /reviewer)。这些模式命令不能被自定义工作流程命令覆盖。在 使用模式 和 自定义模式 中了解更多信息。