feat(agent): 实现委派工具重构,支持子代理和代理团队模式

- 新增 spawn_subagent 和 spawn_agent_team 工具,替代原有的 spawn 工具
- 重构 DelegationManager 以支持单个子代理和代理团队两种委派模式
- 更新系统提示词中的委派策略说明,明确使用场景和区别
- 添加技能上下文传递功能,确保委派任务遵循指定技能
- 实现代理内部的受控下游委派机制,防止无限嵌套
- 更新工具注册和上下文设置逻辑以适配新架构
This commit is contained in:
2026-03-30 17:21:39 +08:00
parent 29dfd14aa6
commit fee9007da6
6 changed files with 490 additions and 90 deletions

View File

@ -86,15 +86,12 @@ Skills with available="false" need dependencies installed first - you can try in
{skills_summary}""")
if self.agent_registry:
# 把可委派 agent 目录加入 system prompt模型才知道 `spawn` 能调用谁。
agents_summary = self.agent_registry.build_agents_summary()
if agents_summary:
parts.append(f"""# Available Agents
parts.append("""# Delegation Tools
The following agents can be delegated to via the `spawn` tool.
Use `target` for a single agent and `targets` for a group.
{agents_summary}""")
Use `spawn_subagent` when the task should go to one delegated worker.
Use `spawn_agent_team` when the task should be explored in parallel by multiple workers.
At the top level, you do not need to choose concrete downstream agents.
Use the `skills` argument when the delegated worker or team must follow specific skills.""")
if execution_context:
# `execution_context` 用于 cron / system task 这类“不是普通用户消息”的额外运行说明。
@ -142,6 +139,14 @@ Reply directly with text for conversations. Only use the 'message' tool to send
- If a tool call fails, analyze the error before retrying with a different approach.
- Do not write directly into `{workspace_path}/skills`; new or updated skills must go through the review flow before activation.
## Delegation Policy
- Solve simple tasks yourself when the work is short, direct, and does not benefit from delegation.
- Delegate only when the task is complex, multi-step, time-consuming, or benefits from specialized/parallel work.
- Use `spawn_subagent` for one focused delegated worker when only the final result matters.
- Use `spawn_agent_team` when multiple agents should explore the task in parallel, compare findings, or work across separate areas.
- Do not delegate by default if you can complete the task reliably in the current turn.
- Do not create or modify persistent local sub-agents unless the user explicitly asks for a reusable long-lived worker.
## Memory
- Remember important facts: write to {workspace_path}/memory/MEMORY.md
- Recall past events: grep {workspace_path}/memory/HISTORY.md"""