feat(core): 增强 Plan Agent 支持细粒度 bash 权限和模式切换

- Plan Agent 细粒度 bash 权限:允许只读命令 (ls, grep, git log 等),
  禁止危险操作 (rm, mv, git commit 等)
- 新增 plan_mode_respond 工具:结构化输出计划、进度和探索状态
- Agent.switchMode() 方法:支持 Build ↔ Plan 模式切换保留对话历史
- WebSocket mode_switch 消息:支持运行时动态切换模式
- 更新 Plan Agent prompt 引导使用 plan_mode_respond 工具
This commit is contained in:
2025-12-15 20:51:57 +08:00
parent f238368f87
commit 35d87a04fb
8 changed files with 355 additions and 20 deletions
+126 -17
View File
@@ -2,25 +2,41 @@ import type { AgentInfo } from '../types.js';
/**
* 计划 Agent
* 主模式,设计实现方案(不执行修改)
* 主模式,设计实现方案(只读探索,不执行修改)
*
* 特性:
* - 细粒度 bash 权限:允许只读命令(ls, grep, git log 等)
* - plan_mode_respond 工具:结构化输出计划和进度
* - 完整探索能力:read + 只读 bash + search
*/
export const planAgent: Omit<AgentInfo, 'name'> = {
description: '计划模式,设计实现方案(不执行修改)',
description: '计划模式,设计实现方案(只读探索,不执行修改)',
mode: 'primary',
prompt: `你是一个软件架构师。你的任务是设计实现方案,而不是直接执行
prompt: `你是一个软件架构师和计划专家。你的任务是探索代码库并设计实现方案。
工作流程:
1. 理解需求:分析用户的需求和目标
2. 调研现状:阅读相关代码,了解现有架构
3. 设计方案:提出详细的实现计划
4. 评估风险:识别潜在问题和挑战
## 模式说明
你处于 **Plan 模式**(只读):
- ✅ 可以:读取文件、搜索代码、执行只读 bash 命令
- ❌ 禁止:创建/修改/删除文件、执行写操作
规则:
- 可以读取代码来了解现状
- 只输出计划,不要实际执行修改
- 提供具体、可操作的步骤
## 可用工具
- read_file, grep_content, search_files: 代码搜索
- bash: 只读命令 (ls, grep, find, git log/diff/status...)
- plan_mode_respond: 输出结构化计划
输出格式:
## 工作流程
1. **理解需求** - 分析用户目标
2. **深度探索** - 使用工具调研代码
3. **设计方案** - 使用 plan_mode_respond 输出计划
4. **迭代完善** - 根据反馈调整
## 输出格式
使用 plan_mode_respond 工具输出计划,包含:
- response: 计划内容
- needs_more_exploration: 是否需要更多探索
- task_progress: 当前进度 (0-100)
计划内容格式:
## 需求分析
[对需求的理解]
@@ -38,25 +54,32 @@ export const planAgent: Omit<AgentInfo, 'name'> = {
## 风险评估
- [风险1]: [应对方案]
- [风险2]: [应对方案]
## 测试计划
- [测试项1]
- [测试项2]`,
tools: {
disabled: [
// 文件写入操作
'write_file',
'edit_file',
'delete_file',
'move_file',
'copy_file',
'create_directory',
'bash',
'multi_edit',
// 注意:bash 不再禁用,改用细粒度权限控制
// Git 写操作
'git_add',
'git_commit',
'git_push',
'git_pull',
'git_checkout',
'git_stash',
// checkpoint 操作(会修改状态)
'checkpoint_create',
'checkpoint_restore',
'undo',
],
},
permission: {
@@ -67,7 +90,93 @@ export const planAgent: Omit<AgentInfo, 'name'> = {
delete: 'deny',
},
bash: {
enabled: false,
enabled: true,
rules: [
// ============ 文件查看 - 允许 ============
{ pattern: 'ls', action: 'allow' },
{ pattern: 'ls *', action: 'allow' },
{ pattern: 'pwd', action: 'allow' },
{ pattern: 'cat *', action: 'allow' },
{ pattern: 'head *', action: 'allow' },
{ pattern: 'tail *', action: 'allow' },
{ pattern: 'less *', action: 'allow' },
{ pattern: 'more *', action: 'allow' },
// ============ 搜索 - 允许 ============
{ pattern: 'find *', action: 'allow' },
{ pattern: 'grep *', action: 'allow' },
{ pattern: 'rg *', action: 'allow' },
{ pattern: 'tree', action: 'allow' },
{ pattern: 'tree *', action: 'allow' },
// ============ 文件信息 - 允许 ============
{ pattern: 'wc *', action: 'allow' },
{ pattern: 'stat *', action: 'allow' },
{ pattern: 'file *', action: 'allow' },
{ pattern: 'du *', action: 'allow' },
{ pattern: 'diff *', action: 'allow' },
{ pattern: 'which *', action: 'allow' },
{ pattern: 'whereis *', action: 'allow' },
// ============ 文本处理(只读)- 允许 ============
{ pattern: 'sort *', action: 'allow' },
{ pattern: 'uniq *', action: 'allow' },
{ pattern: 'cut *', action: 'allow' },
{ pattern: 'awk *', action: 'allow' },
{ pattern: 'sed -n *', action: 'allow' }, // 只允许 -n (不修改)
// ============ Git 只读 - 允许 ============
{ pattern: 'git status', action: 'allow' },
{ pattern: 'git status *', action: 'allow' },
{ pattern: 'git diff', action: 'allow' },
{ pattern: 'git diff *', action: 'allow' },
{ pattern: 'git log', action: 'allow' },
{ pattern: 'git log *', action: 'allow' },
{ pattern: 'git show *', action: 'allow' },
{ pattern: 'git branch', action: 'allow' },
{ pattern: 'git branch -v*', action: 'allow' },
{ pattern: 'git branch -a*', action: 'allow' },
{ pattern: 'git branch --list*', action: 'allow' },
{ pattern: 'git remote -v', action: 'allow' },
{ pattern: 'git tag', action: 'allow' },
{ pattern: 'git tag *', action: 'allow' },
{ pattern: 'git blame *', action: 'allow' },
{ pattern: 'git ls-files*', action: 'allow' },
{ pattern: 'git rev-parse *', action: 'allow' },
// ============ 危险命令 - 拒绝 ============
{ pattern: 'rm *', action: 'deny' },
{ pattern: 'rmdir *', action: 'deny' },
{ pattern: 'mv *', action: 'deny' },
{ pattern: 'cp *', action: 'deny' },
{ pattern: 'mkdir *', action: 'deny' },
{ pattern: 'touch *', action: 'deny' },
{ pattern: 'chmod *', action: 'deny' },
{ pattern: 'chown *', action: 'deny' },
{ pattern: 'sudo *', action: 'deny' },
{ pattern: 'su *', action: 'deny' },
// ============ Git 写操作 - 拒绝 ============
{ pattern: 'git add *', action: 'deny' },
{ pattern: 'git commit *', action: 'deny' },
{ pattern: 'git push *', action: 'deny' },
{ pattern: 'git pull *', action: 'deny' },
{ pattern: 'git checkout *', action: 'deny' },
{ pattern: 'git reset *', action: 'deny' },
{ pattern: 'git rebase *', action: 'deny' },
{ pattern: 'git merge *', action: 'deny' },
{ pattern: 'git stash *', action: 'deny' },
{ pattern: 'git clean *', action: 'deny' },
// ============ find 危险操作 - 拒绝 ============
{ pattern: 'find * -delete*', action: 'deny' },
{ pattern: 'find * -exec*', action: 'deny' },
// ============ 重定向操作 - 拒绝 ============
{ pattern: '* > *', action: 'deny' },
{ pattern: '* >> *', action: 'deny' },
],
default: 'ask', // 其他命令询问用户
},
git: {
read: 'allow',
@@ -78,5 +187,5 @@ export const planAgent: Omit<AgentInfo, 'name'> = {
model: {
temperature: 0.5,
},
maxSteps: 15,
maxSteps: 30, // 增加探索步数以支持更深入的调研
};