|
| 1 | +# CodeAgent v0.6 - 自定义命令、Subagents 支持 |
| 2 | + |
| 3 | +## 背景认知 |
| 4 | + |
| 5 | +通过调研,我们知道要想让 AI 写代码效果更好,应该: |
| 6 | + |
| 7 | +- 优化指令,让 AI 做事更有针对性,比如一个阶段只敢一件事 |
| 8 | +- 使用 Extended Thinking 模式,主动让 AI 更充分的思考(claude) |
| 9 | +- 使用 Subagents ,构建专业 Agent,让其做特定的事 |
| 10 | +- 基于实际场景,使用一些被证明行之有效的工作流 |
| 11 | + - explore, plan,code , commit |
| 12 | + - Write tests, commit; code, iterate, commit |
| 13 | + - Write code, screenshot result, iterate |
| 14 | +- 使用高级的模型,如 Opus 4.1 |
| 15 | + |
| 16 | +结合上述行为在 claude code 侧的实际设计(PS: [gemini 相关能力能力还在开发中](https://github.com/google-gemini/gemini-cli/issues/3132) ),v0.6 计划引入了**自定义命令系统**和**Subagents 支持**,以充分利用 claude code 的能力。 |
| 17 | + |
| 18 | +设计要点: |
| 19 | + |
| 20 | +- 在 github 交互中用到的 slash 命令,就类比 claude code 的命令。其最终对应的 prompt 细节都会映射到目录文件,类似/code 命令,会映射到 .codeagent/commands/code.md |
| 21 | + |
| 22 | +- 命令格式,采用 YAML frontmatter + markdown 格式, 整体语法跟 claude code 一致 |
| 23 | + |
| 24 | +- 支持在命令里引入 GITHUB 上下文,该信息会统一由 CodeAgents 自动注入 |
| 25 | + |
| 26 | +- 支持 Claude Code 的 subagents 设计,最终命令文件也会挂载到合适的位置,如 .claude/commands、.claude/agents/ |
| 27 | + |
| 28 | +- 支持各仓库自定义命令,如果自定义命令跟系统默认重名,优先选择自定义命令 |
| 29 | + |
| 30 | +有了上述能力,我们就可以并行的,更加充分的调试 Prompt,探索最佳 AI 工作流实践。 |
| 31 | + |
| 32 | +## 命令示例 |
| 33 | + |
| 34 | +/analyze |
| 35 | + |
| 36 | +```markdown |
| 37 | +--- |
| 38 | +allowed-tools: all |
| 39 | +description: 需求深度分析 |
| 40 | +model: claude-3-5-haiku-20241022 |
| 41 | +--- |
| 42 | + |
| 43 | +## 当前 Issue 信息 |
| 44 | + |
| 45 | +- 仓库: $REPOSITORY |
| 46 | +- Issue: #$ISSUE_NUMBER - $ISSUE_TITLE |
| 47 | +- 提交者: $TRIGGER_USERNAME |
| 48 | + |
| 49 | +## Issue 描述 |
| 50 | + |
| 51 | +$ISSUE_BODY |
| 52 | + |
| 53 | +## 历史讨论 |
| 54 | + |
| 55 | +$ISSUE_COMMENTS |
| 56 | + |
| 57 | +## 分析任务 |
| 58 | + |
| 59 | +请使用 Extended Thinking 模式对 Issue 进行深度分析 |
| 60 | +``` |
| 61 | + |
| 62 | +#### Agent 定义格式示例 |
| 63 | + |
| 64 | +requirements-analyst agent: |
| 65 | + |
| 66 | +```markdown |
| 67 | +--- |
| 68 | +name: requirements-analyst |
| 69 | +description: "专业的需求分析专家" |
| 70 | +model: claude-3-5-sonnet |
| 71 | +temperature: 0.1 |
| 72 | +tools: ["read", "grep"] |
| 73 | +--- |
| 74 | + |
| 75 | +# 需求分析专家 |
| 76 | + |
| 77 | +你是一个经验丰富的需求分析专家,专注于理解和分析软件需求... |
| 78 | + |
| 79 | +## 专业技能 |
| 80 | + |
| 81 | +- 深度需求理解和澄清 |
| 82 | +- 技术可行性评估 |
| 83 | +- 实现风险识别 |
| 84 | + |
| 85 | +## 工作方法 |
| 86 | + |
| 87 | +使用结构化分析方法,提供清晰准确的需求总结和实现建议 |
| 88 | +``` |
| 89 | + |
| 90 | +对于 agent 的定义, claude code 要求: |
| 91 | + |
| 92 | +| Field | Required | Description | |
| 93 | +| ----------- | -------- | ------------------------------------------------------------------------------------------- | |
| 94 | +| name | Yes | Unique identifier using lowercase letters and hyphens | |
| 95 | +| description | Yes | Natural language description of the subagent's purpose | |
| 96 | +| tools | No | Comma-separated list of specific tools. If omitted, inherits all tools from the main thread | |
| 97 | + |
| 98 | +### 2. 目录结构 |
| 99 | + |
| 100 | +#### 2.1 配置文件 |
| 101 | + |
| 102 | +```yaml |
| 103 | +# config.yaml |
| 104 | +commands: |
| 105 | + global_path: "/opt/codeagent/.codeagent" # 保存agent默认的全局命令和Agent定义目录 |
| 106 | + |
| 107 | +# 其他配置... |
| 108 | +``` |
| 109 | + |
| 110 | +#### 2.2 目录合并机制 |
| 111 | + |
| 112 | +CodeAgent 在运行时将两个 `.codeagent` 目录**物理合并**成一个临时目录,然后挂载到容器: |
| 113 | + |
| 114 | +**合并流程**: |
| 115 | + |
| 116 | +```bash |
| 117 | +# 1. 创建临时合并目录 |
| 118 | +mkdir /tmp/codeagent-merged-${repo}-${timestamp} |
| 119 | + |
| 120 | +# 2. 先拷贝全局配置 |
| 121 | +cp -r /opt/codeagent/.codeagent/* /tmp/codeagent-merged-${repo}/ |
| 122 | + |
| 123 | +# 3. 仓库配置覆盖全局配置(cp -rf 语义) |
| 124 | +if [ -d "${workspace}/.codeagent" ]; then |
| 125 | + cp -rf ${workspace}/.codeagent/* /tmp/codeagent-merged-${repo}/ |
| 126 | +fi |
| 127 | + |
| 128 | +# 4. 挂载到容器 |
| 129 | +docker run -v /tmp/codeagent-merged-${repo}/commands:/root/.claude/commands \ |
| 130 | + -v /tmp/codeagent-merged-${repo}/agents:/root/.claude/agents \ |
| 131 | + ... |
| 132 | +``` |
| 133 | + |
| 134 | +**实际效果**: |
| 135 | + |
| 136 | +- 全局有 `analyze.md`, `plan.md`, `code.md` |
| 137 | +- 仓库有 `analyze.md`, `custom.md` |
| 138 | +- 合并后:`analyze.md`(仓库版本), `plan.md`(全局版本), `code.md`(全局版本), `custom.md`(仓库版本) |
| 139 | + |
| 140 | +#### 2.3 目录层级 |
| 141 | + |
| 142 | +``` |
| 143 | +# 全局配置目录(通过config.yaml指定) |
| 144 | +/opt/codeagent/.codeagent/ |
| 145 | +├── commands/ # 命令定义文件 (.md) |
| 146 | +│ ├── analyze.md |
| 147 | +│ ├── plan.md |
| 148 | +│ ├── code.md |
| 149 | +│ ├── plan-and-code.md |
| 150 | +│ └── continue.md |
| 151 | +└── agents/ # Agent 定义文件 (.md) |
| 152 | + ├── requirements-analyst.md |
| 153 | + ├── solution-architect.md |
| 154 | + ├── implementation-expert.md |
| 155 | + └── code-reviewer.md |
| 156 | +
|
| 157 | +# 仓库级配置目录(仓库根目录下,可选) |
| 158 | +{workspace}/.codeagent/ |
| 159 | +├── commands/ # 项目特定命令(与全局合并,仓库级覆盖全局级) |
| 160 | +└── agents/ # 项目特定 agents(与全局合并,仓库级覆盖全局级) |
| 161 | +
|
| 162 | +# 运行时挂载 |
| 163 | +Docker 模式: |
| 164 | + .codeagent/commands → ~/.claude/commands |
| 165 | + .codeagent/agents → ~/.claude/agents |
| 166 | +
|
| 167 | +``` |
| 168 | + |
| 169 | +### 3. 核心实现:GitHub 上下文注入器 |
| 170 | + |
| 171 | +**设计理念**:提供智能的模板引擎,支持简单变量替换和 Go Template 语法,适配不同事件类型的精确上下文。 |
| 172 | + |
| 173 | +#### 3.1 变量命名规范 |
| 174 | + |
| 175 | +采用 `GITHUB_` 前缀,类似如下: |
| 176 | + |
| 177 | +```go |
| 178 | +// 核心变量 |
| 179 | +$GITHUB_REPOSITORY // owner/repo |
| 180 | +$GITHUB_EVENT_TYPE // issues|pull_request|pull_request_review_comment |
| 181 | +$GITHUB_TRIGGER_USER // 触发用户 |
| 182 | + |
| 183 | +// Issue变量 |
| 184 | +$GITHUB_ISSUE_NUMBER // 123 |
| 185 | +$GITHUB_ISSUE_TITLE // Issue标题 |
| 186 | +$GITHUB_ISSUE_BODY // Issue内容 |
| 187 | + |
| 188 | +// PR变量 |
| 189 | +$GITHUB_PR_NUMBER // 456 |
| 190 | +$GITHUB_PR_TITLE // PR标题 |
| 191 | +$GITHUB_BRANCH_NAME // feature-branch |
| 192 | +$GITHUB_BASE_BRANCH // main |
| 193 | + |
| 194 | +// Review Comment变量(高精度行级上下文) |
| 195 | +$GITHUB_REVIEW_FILE_PATH // src/main.go |
| 196 | +$GITHUB_REVIEW_LINE_RANGE // "行号:123" 或 "行号范围:45-67" |
| 197 | +$GITHUB_REVIEW_COMMENT_BODY // 评论内容 |
| 198 | +$GITHUB_REVIEW_DIFF_HUNK // 差异片段 |
| 199 | +$GITHUB_REVIEW_FILE_CONTENT // 目标行周围代码上下文 |
| 200 | +``` |
| 201 | + |
| 202 | +#### 3.4 模板语法示例 |
| 203 | + |
| 204 | +```markdown |
| 205 | +# 智能事件分析 |
| 206 | + |
| 207 | +## 基本信息 |
| 208 | + |
| 209 | +- 仓库: {{.GITHUB_REPOSITORY}} |
| 210 | +- 事件: {{.GITHUB_EVENT_TYPE}} |
| 211 | + |
| 212 | +## 上下文 |
| 213 | + |
| 214 | +{{if .GITHUB_IS_PR}} |
| 215 | + |
| 216 | +### PR 信息 |
| 217 | + |
| 218 | +- PR #{{.GITHUB_PR_NUMBER}}: {{.GITHUB_PR_TITLE}} |
| 219 | + {{if gt (len .GITHUB_CHANGED_FILES) 10}} |
| 220 | +- 变更文件: {{len .GITHUB_CHANGED_FILES}}个(显示前 10 个) |
| 221 | + {{else}} |
| 222 | +- 变更文件: {{len .GITHUB_CHANGED_FILES}}个 |
| 223 | + {{end}} |
| 224 | + {{else if .GITHUB_IS_ISSUE}} |
| 225 | + |
| 226 | +### Issue 信息 |
| 227 | + |
| 228 | +- Issue #{{.GITHUB_ISSUE_NUMBER}}: {{.GITHUB_ISSUE_TITLE}} |
| 229 | +- 标签: {{join .GITHUB_ISSUE_LABELS ", "}} |
| 230 | + {{end}} |
| 231 | +``` |
| 232 | + |
| 233 | +GITHUb 上下文会基于后续需求,不断补充和调整。 |
| 234 | + |
| 235 | +## 内置命令设计 |
| 236 | + |
| 237 | +CodeAgent v0.6 预计会提供以下核心命令,支持从需求分析到代码实现的完整工作流: |
| 238 | + |
| 239 | +### Issue 阶段命令 |
| 240 | + |
| 241 | +#### `/analyze` - 深度需求分析 |
| 242 | + |
| 243 | +- **用途**: 深入理解 Issue 需求,识别技术挑战和实现风险 |
| 244 | +- **Subagent**: requirements-analyst |
| 245 | +- **GitHub 变量**: Issue 相关上下文(标题、内容、评论、标签等) |
| 246 | +- **输出**: 结构化的需求分析报告,包含技术难点和实施建议 |
| 247 | + |
| 248 | +#### `/plan` - 技术方案设计 |
| 249 | + |
| 250 | +- **用途**: 制定详细的技术实现方案和分步执行计划 |
| 251 | +- **Subagent**: solution-architect |
| 252 | +- **GitHub 变量**: Issue 上下文 + 代码库结构信息 |
| 253 | +- **输出**: 完整的技术方案文档,包含架构设计和实施清单 |
| 254 | + |
| 255 | +#### `/code` - 直接代码实现 |
| 256 | + |
| 257 | +- **用途**: 完整实现 Issue 需求,包含编码、测试、文档 |
| 258 | +- **Subagent**: implementation-expert |
| 259 | +- **GitHub 变量**: Issue 上下文 |
| 260 | +- **输出**: 可运行的代码实现 + 测试 + 文档 |
| 261 | + |
| 262 | +### PR 阶段命令 |
| 263 | + |
| 264 | +#### `/continue` - PR 协作开发 |
| 265 | + |
| 266 | +- **用途**: 在 PR 中处理反馈、迭代改进、解决问题 |
| 267 | +- **Subagent**: pr-collaborator |
| 268 | +- **GitHub 变量**: PR 完整上下文(变更文件、评论历史、Review 反馈等) |
| 269 | +- **输出**: 改进后的代码 + 反馈回应 + 变更说明 |
| 270 | + |
| 271 | +## 参考资料 |
| 272 | + |
| 273 | +- https://www.anthropic.com/engineering/claude-code-best-practices |
| 274 | +- https://docs.anthropic.com/en/docs/claude-code/sub-agents |
| 275 | +- https://docs.anthropic.com/en/docs/claude-code/slash-commands |
| 276 | +- https://github.com/cexll/myclaude |
| 277 | +- https://github.com/anthropics/claude-code-security-review |
| 278 | +- https://github.com/anthropics/claude-code-action |
| 279 | +- https://github.com/Pimzino/claude-code-spec-workflow |
0 commit comments