Subagents
在华为云码道(CodeArts)代码智能体中,Subagent是主Agent的AI助手,用于处理特定类型的工作,并将结果返回给主Agent。
Subagent主要用于以下场景:
- 长链路复杂任务,需要Subagent自主规划多个步骤,分步执行。例如:全链路代码开发。
- 上下文隔离,解决主Agent Token膨胀、上下文溢出。例如:批量数据解析。
- 专项领域深度专家能力,每个Subagent拥有专属提示词、私有知识库,提供专业推理和更高准确率。例如:安全审计专家Subagent。
- 多任务并行、异步任务处理,主Agent同时派发多个相互独立的子任务给多个Subagent并行异步执行。例如:多模块代码并行分析与评审。
- 权限隔离,每个SubAgent独立配置专属访问权限,实现敏感数据、私有知识库、高低危接口、文件资源、部门项目数据之间权限隔离,保障系统数据安全。例如:数据删除、系统指令等高风险操作权限隔离。
核心能力
Subagent主要具备以下核心能力:
- 上下文隔离:每个Subagent都拥有自己独立的上下文,任务执行完成后只返回结果给主Agent,不会把上下文返回给主Agent。减少Subagent上下文对主Agent的污染。
- 并行执行:多个Subagent同时工作,节省复杂任务的运行时间。
- 精细化权限管控:通过自定义提示词和工具权限配置Subagent,满足特定领域的任务处理需求。
Subagent与Skill的对比
| 对比维度 | Subagent | Skill |
|---|---|---|
| 定义 | 具备独立决策与自主执行能力的子Agent,用于描述如何完成复杂长链路的任务。 | 依附主Agent的被动执行能力模块,自身无自主推理与规划能力,用于描述如何完成简单、固定的任务。 |
| 上下文 | 完全独立隔离的上下文,仅回传最终结果给主Agent | 共享主Agent上下文 |
| 自主能力 | 具备独立决策与自主执行能力 | 无自主推理、流程规划能力 |
| 权限控制 | 拥有独立权限体系,可单独配置权限 | 继承主Agent全部工具权限 |
内置SubAgent
华为云码道已内置多款Subagent,无需您配置,由智能体决定何时使用。
| 名称 | 说明 |
|---|---|
| general | 通用代理,用于研究复杂问题和执行多步骤任务。 |
| explore | 快速代码库探索代理,专用于按模式查找文件、搜索代码关键字、回答代码库问题。 |
| spec-task-agent | 根据需求和设计生成实现任务。 |
| spec-requirement-agent | 基于项目描述和上下文生成EARS格式需求。 |
| spec-design-agent | 生成综合技术设计,将需求(做什么)转化为架构(如何做)。 |
| developer-test-agent | 专门用于单元测试生成、修复、覆盖率优化和审查的子代理。 使用示例请参见单元测试。 |
Subagent分类
华为云码道支持项目级和个人级两种Subagent。用户可根据具体开发场景与协作需求,选择最适合的Subagent层级进行设置,实现灵活高效的代码管理。存在同名的项目级和个人级Subagent时,项目级Subagent优先级高于个人级Subagent。
| 类型 | 作用域 | 说明 |
|---|---|---|
| 项目级 | 当前项目 | 仅针对当前项目的Subagent,随代码库分发,存于本地。 存储位置:本地项目根目录下./.codeartsdoer/agents |
| 个人级 | 当前用户下的所有项目 | 个人习惯或特定偏好的Subagent,仅对本人生效。 存储位置:本地~/.codeartsdoer/agents |
Subagent格式
每个Subagent都是一个带有YAML frontmatter的Markdown文件:
--- name: SubAgent名称 description: SubAgent描述信息,主Agent通过该描述决定什么场景使用此SubAgent。 tools: 工具名称 --- SubAgent执行任务的具体提示词
示例如下:
--- name: code-reviewer description: Expert code reviewer specializing in code quality, security vulnerabilities, and best practices across multiple languages. Masters static analysis, design patterns, and performance optimization with focus on maintainability and technical debt reduction. tools: Read, Write, Edit, Bash, Glob, Grep --- You are a senior code reviewer with expertise in identifying code quality issues, security vulnerabilities, and optimization opportunities across multiple programming languages. Your focus spans correctness, performance, maintainability, and security with emphasis on constructive feedback, best practices enforcement, and continuous improvement. When invoked: 1. Query context manager for code review requirements and standards 2. Review code changes, patterns, and architectural decisions 3. Analyze code quality, security, performance, and maintainability 4. Provide actionable feedback with specific improvement suggestions Code review checklist: - Zero critical security issues verified - Code coverage > 80% confirmed - Cyclomatic complexity < 10 maintained - No high-priority vulnerabilities found - Documentation complete and clear - No significant code smells detected - Performance impact validated thoroughly - Best practices followed consistently
创建Subagent
华为云码道支持添加项目级与个人级两种类型的Subagent。除了直接创建Subagent外,您还可以通过导入的方式,快速添加自定义Subagent,灵活高效,轻松扩展能力。
为当前项目定制的Subagent,随代码库一并分发。项目级Subagent添加后即生效,何时使用由智能体决定。
- 参考快速启动操作,登录华为云码道。
- 打开华为云码道聊天窗口。
- 华为云码道IDE:单击IDE顶部菜单栏中的展开AI侧栏图标
,打开华为云码道聊天窗口。 - Visual Studio Code:单击Visual Studio Code侧边栏图标
,打开华为云码道聊天窗口。 - JetBrains:以IntelliJ IDEA为例,单击IntelliJ IDEA侧边栏图标
,打开华为云码道聊天窗口。
不同工具中华为云码道的界面风格有所差异,本章以其在IDE工具中的界面为例进行介绍,实际操作请以所用工具的显示为准。
- 华为云码道IDE:单击IDE顶部菜单栏中的展开AI侧栏图标
- 在“资源管理器”的“./.codeartsdoer/agents”文件夹上,单击鼠标右键选择“新建文件”,新建一个Markdown格式的文件。
Subagent名称由小写字母、数字和连字符(-)组成,开头和结尾不能是连字符,且连字符不可连续使用,长度1~64字符。例如code-reviewer。
- 在code-reviewer.md文件中编写SubAgent,完成后按快捷键(Windows/Linux:“Ctrl+S”;macOS:“Command(⌘)+S”)保存。
个人级Subagent添加后即生效,何时使用由智能体决定。
- 在“本地~/.codeartsdoer/agents/”路径中,新建一个Markdown格式的Subagent文件。
Subagent名称由小写字母、数字和连字符(-)组成,开头和结尾不能是连字符,且连字符不可连续使用,长度1~64字符。例如code-reviewer。
- 在code-reviewer.md文件中编写SubAgent,完成后按快捷键(Windows/Linux:“Ctrl+S”;macOS:“Command(⌘)+S”)保存。
如果您本地已创建好对应的Subagent,也可以通过导入的方式,快速将本地Subagent导入,供华为云码道调用。
- 导入项目级Subagent
将已创建的Subagent直接复制到工程目录下,放置到工程根目录下的“./.codeartsdoer/agents”。导入后,Subagent由智能体决定何时使用。
- 导入个人级Subagent
将已创建的Subagent直接复制到用户目录下,放置到“本地~/.codeartsdoer/agents/”。导入后,Subagent由智能体决定何时使用。