feat(ui): 实现 Agent 模式切换和 Auto Edit 功能

- 添加 AgentModeSelector 组件,支持 Build/Plan 模式切换
- Build 模式下显示 Auto Edit 开关,自动授权文件写入/编辑
- 扩展 useChat hook 添加会话级别的 agentMode/autoApprove 状态
- 服务端支持解析和应用 Agent 模式配置
- 权限处理器实现 auto-approve 检查(仅 write/edit,不含 delete)
This commit is contained in:
2025-12-15 19:42:51 +08:00
parent f09f8f2b03
commit ec3c7bccf9
13 changed files with 409 additions and 7 deletions
+53 -1
View File
@@ -728,8 +728,31 @@ export class Agent {
/**
* 切换 Agent 模式
* @param agent AgentInfo 对象或模式字符串 ('build'/'plan')
*/
setAgentMode(agent: AgentInfo | null): void {
setAgentMode(agent: AgentInfo | 'build' | 'plan' | null): void {
// 如果是字符串模式,从 registry 获取预设
if (typeof agent === 'string') {
const presetAgent = agentRegistry.get(agent);
if (presetAgent) {
this.currentAgentMode = presetAgent;
if (presetAgent.prompt) {
this.config = {
...this.config,
systemPrompt: presetAgent.prompt,
};
}
} else {
// 如果找不到预设,回退到默认模式
this.currentAgentMode = null;
this.config = {
...this.config,
systemPrompt: this.originalSystemPrompt,
};
}
return;
}
this.currentAgentMode = agent;
if (agent?.prompt) {
@@ -747,6 +770,35 @@ export class Agent {
}
}
// ============================================================================
// Auto-approve 功能(用于前端 Build 模式的自动授权)
// ============================================================================
/** 临时自动授权配置 */
private autoApproveConfig: { file?: { write?: 'allow'; edit?: 'allow' } } | null = null;
/**
* 设置自动授权配置
* 仅影响 file write 和 file edit 操作(不包含 delete
*/
setAutoApprove(config: { file?: { write?: 'allow'; edit?: 'allow' } }): void {
this.autoApproveConfig = config;
}
/**
* 清除自动授权配置
*/
clearAutoApprove(): void {
this.autoApproveConfig = null;
}
/**
* 获取当前自动授权配置
*/
getAutoApproveConfig(): { file?: { write?: 'allow'; edit?: 'allow' } } | null {
return this.autoApproveConfig;
}
/**
* 获取当前 Agent 模式
*/