Files
claude-code/docs/command-system.md
arno 9074aabe1c Add project documentation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 09:33:11 +08:00

235 lines
6.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 命令系统分析
## 架构概述
命令系统通过 `/` 前缀提供用户交互功能,共约 82 个命令目录、189 个文件。
```mermaid
classDiagram
class Command {
+string name
+string description
+boolean hidden
+execute(args) void
}
class InteractiveCommand {
+React 组件渲染
+用户交互
}
class NonInteractiveCommand {
+纯逻辑执行
+无 UI
}
Command <|-- InteractiveCommand
Command <|-- NonInteractiveCommand
InteractiveCommand <|-- CommitCommand
InteractiveCommand <|-- DoctorCommand
InteractiveCommand <|-- ReviewCommand
NonInteractiveCommand <|-- ClearCommand
NonInteractiveCommand <|-- CostCommand
NonInteractiveCommand <|-- CompactCommand
```
## 命令类型系统
所有命令统一使用三种基本类型:
| 类型 | 说明 | 适用场景 |
|------|------|----------|
| `local` | 执行本地操作,返回文本结果 | 简单操作(/clear、/cost |
| `local-jsx` | 执行本地操作并渲染 React 组件 | 需要用户交互(/help、/theme |
| `prompt` | 扩展为提示文本,由模型执行 | 需要 AI 能力(/commit、/review |
## 命令来源
```mermaid
flowchart LR
A[命令来源] --> B[builtin<br/>内置命令]
A --> C[skills/<br/>技能目录]
A --> D[bundled<br/>打包技能]
A --> E[plugin<br/>插件命令]
A --> F[managed<br/>托管命令]
A --> G[mcp<br/>MCP 技能]
B --> H[commands.ts<br/>统一注册]
C --> H
D --> H
E --> H
F --> H
G --> H
```
## 命令注册机制
命令在 `src/commands.ts` 中注册,使用 `memoize` 缓存,支持延迟加载(`load()` 按需加载实现)。使用 Fuse.js 实现模糊搜索,支持命令名称、描述和别名匹配。
```mermaid
flowchart TD
A[COMMANDS memoize] --> B[核心命令注册]
B --> C[loadSkillsDir<br/>用户技能]
B --> D[initBundledSkills<br/>打包技能]
B --> E[插件加载器<br/>插件命令]
F{可用性检查} --> G[meetsAvailabilityRequirement]
G --> H[isCommandEnabled<br/>功能标志/平台/环境]
G --> I[availability<br/>claude-ai/console]
```
## 命令分类
### 交互管理类
| 命令 | 功能 |
|------|------|
| `/compact` | 压缩对话上下文 |
| `/clear` | 清除对话/缓存 |
| `/rewind` | 回退对话状态 |
| `/resume` | 恢复之前的会话 |
| `/exit` | 退出程序 |
| `/rename` | 重命名会话 |
### 代码工作流类
| 命令 | 功能 |
|------|------|
| `/commit` | 创建 Git 提交 |
| `/review` | 代码审查 |
| `/diff` | 查看变更 |
| `/autofix-pr` | 自动修复 PR |
| `/pr_comments` | 查看 PR 评论 |
| `/bughunter` | Bug 搜索 |
| `/plan` | 计划模式 |
### 环境和配置类
| 命令 | 功能 |
|------|------|
| `/config` | 设置管理 |
| `/doctor` | 环境诊断 |
| `/env` | 环境变量管理 |
| `/permissions` | 权限管理 |
| `/sandbox-toggle` | 沙盒开关 |
| `/keybindings` | 快捷键配置 |
| `/vim` | Vim 模式切换 |
| `/theme` | 主题切换 |
| `/color` | 颜色配置 |
| `/output-style` | 输出样式 |
| `/effort` | 思考力度设置 |
| `/model` | 模型选择 |
### 认证和账户类
| 命令 | 功能 |
|------|------|
| `/login` | 登录 |
| `/logout` | 登出 |
| `/cost` | 使用成本查看 |
| `/usage` | 使用量查看 |
| `/extra-usage` | 额外用量 |
| `/rate-limit-options` | 速率限制选项 |
| `/reset-limits` | 重置限制 |
| `/passes` | 使用通行证 |
### 工具集成类
| 命令 | 功能 |
|------|------|
| `/mcp` | MCP 服务器管理 |
| `/skills` | 技能管理 |
| `/plugin` | 插件管理 |
| `/reload-plugins` | 重新加载插件 |
| `/hooks` | 钩子管理 |
| `/lsp` | LSP 管理 |
### IDE 和远程类
| 命令 | 功能 |
|------|------|
| `/bridge` | IDE 桥接模式 |
| `/ide` | IDE 集成 |
| `/desktop` | 桌面应用切换 |
| `/mobile` | 移动应用切换 |
| `/chrome` | Chrome 扩展集成 |
| `/remote-env` | 远程环境 |
| `/remote-setup` | 远程设置 |
| `/teleport` | 传送(远程会话迁移) |
### 信息查看类
| 命令 | 功能 |
|------|------|
| `/help` | 帮助信息 |
| `/context` | 上下文可视化 |
| `/ctx_viz` | 上下文结构可视化 |
| `/stats` | 统计信息 |
| `/status` | 状态查看 |
| `/cost` | 成本查看 |
| `/memory` | 持久化内存管理 |
| `/files` | 文件列表 |
| `/session` | 会话管理 |
| `/thinkback` | 回溯思考过程 |
| `/thinkback-play` | 回放思考过程 |
| `/summary` | 会话摘要 |
| `/share` | 分享会话 |
| `/export` | 导出会话 |
| `/release-notes` | 更新日志 |
| `/feedback` | 反馈 |
### 开发调试类
| 命令 | 功能 |
|------|------|
| `/debug-tool-call` | 调试工具调用 |
| `/heapdump` | 堆转储 |
| `/perf-issue` | 性能问题 |
| `/break-cache` | 缓存清除 |
| `/mock-limits` | 模拟限制 |
| `/ant-trace` | 内部追踪 |
| `/backfill-sessions` | 回填会话 |
| `/debug` | 调试模式 |
### 特殊功能类
| 命令 | 功能 |
|------|------|
| `/voice` | 语音输入 |
| `/add-dir` | 添加工作目录 |
| `/branch` | 分支管理 |
| `/tag` | 标签管理 |
| `/copy` | 复制输出 |
| `/onboarding` | 新手引导 |
| `/upgrade` | 升级 |
| `/fast` | 快速模式切换 |
| `/btw` | 顺便提到 |
| `/good-claude` | 正面反馈 |
| `/stickers` | 贴纸 |
| `/issue` | Issue 管理 |
| `/install-github-app` | 安装 GitHub 应用 |
| `/install-slack-app` | 安装 Slack 应用 |
| `/advisor` | 顾问 |
| `/agents` | 代理管理 |
| `/passes` | 使用通行证 |
| `/privacy-settings` | 隐私设置 |
| `/terminalSetup` | 终端设置 |
## 命令执行流程
```mermaid
sequenceDiagram
participant User as 用户
participant Input as TextInput
participant Parser as 命令解析器
participant Registry as commands.ts
participant Impl as 命令实现
participant UI as Ink 渲染
User->>Input: 输入 /commit
Input->>Parser: 识别 / 前缀
Parser->>Registry: 查找命令
Registry->>Impl: 命令路由
Impl->>UI: 渲染交互界面
UI->>User: 显示结果
```
## 特殊命令集
| 集合 | 说明 |
|------|------|
| `REMOTE_SAFE_COMMANDS` | 远程模式下可安全执行的命令(不依赖本地文件系统) |
| `BRIDGE_SAFE_COMMANDS` | 可通过远程控制桥接安全执行的命令 |
| `immediate` 命令 | 不需要等待停止点,立即执行的命令(如 /status、/cost |