feat(core): 使用模板系统重构 task 工具描述

- 创建 task.txt 模板文件,包含完整的工具描述
- 添加 AGENT_TYPE_REGISTRY_STRING 变量支持动态 Agent 列表
- 添加 agentOutput 工具名称映射
- 更新 task.ts 使用模板渲染,保留回退逻辑
This commit is contained in:
2025-12-17 10:50:33 +08:00
parent cfb2175916
commit c5b92e740c
5 changed files with 109 additions and 13 deletions
+1
View File
@@ -332,6 +332,7 @@ export const DEFAULT_TOOL_NAMES: ToolNameMapping = {
exitPlanMode: 'exit_plan_mode',
enterPlanMode: 'enter_plan_mode',
task: 'task',
agentOutput: 'agent_output',
todoRead: 'todoread',
todoWrite: 'todowrite',
webSearch: 'web_search',
+1
View File
@@ -78,6 +78,7 @@ export interface ToolNameMapping {
exitPlanMode: string;
enterPlanMode: string;
task: string;
agentOutput: string;
todoRead: string;
todoWrite: string;
webSearch: string;
@@ -0,0 +1,64 @@
Launch a new agent to handle complex, multi-step tasks autonomously.
The ${TASK_TOOL_NAME} tool launches specialized agents (subprocesses) that autonomously handle complex tasks. Each agent type has specific capabilities and tools available to it.
Available agent types and the tools they have access to:
${AGENT_TYPE_REGISTRY_STRING}
When using the ${TASK_TOOL_NAME} tool, you must specify a subagent_type parameter to select which agent type to use.
When NOT to use the ${TASK_TOOL_NAME} tool:
- If you want to read a specific file path, use the ${READ_TOOL_NAME} or ${GLOB_TOOL_NAME} tool instead of the ${TASK_TOOL_NAME} tool, to find the match more quickly
- If you are searching for a specific class definition like "class Foo", use the ${GLOB_TOOL_NAME} tool instead, to find the match more quickly
- If you are searching for code within a specific file or set of 2-3 files, use the ${READ_TOOL_NAME} tool instead of the ${TASK_TOOL_NAME} tool, to find the match more quickly
- Other tasks that are not related to the agent descriptions above
Usage notes:
- Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses
- When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.
- You can optionally run agents in the background using the run_in_background parameter. When an agent runs in the background, you will need to use ${AGENT_OUTPUT_TOOL_NAME} to retrieve its results once it's done. You can continue to work while background agents run - When you need their results to continue you can use ${AGENT_OUTPUT_TOOL_NAME} in blocking mode to pause and wait for their results.
- Agents can be resumed using the `resume` parameter by passing the agent ID from a previous invocation. When resumed, the agent continues with its full previous context preserved. When NOT resuming, each invocation starts fresh and you should provide a detailed task description with all necessary context.
- When the agent is done, it will return a single message back to you along with its agent ID. You can use this ID to resume the agent later if needed for follow-up work.
- Provide clear, detailed prompts so the agent can work autonomously and return exactly the information you need.
- Agents with "access to current context" can see the full conversation history before the tool call. When using these agents, you can write concise prompts that reference earlier context (e.g., "investigate the error discussed above") instead of repeating information. The agent will receive all prior messages and understand the context.
- The agent's outputs should generally be trusted
- Clearly tell the agent whether you expect it to write code or just to do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent
- If the agent description mentions that it should be used proactively, then you should try your best to use it without the user having to ask for it first. Use your judgement.
- If the user specifies that they want you to run agents "in parallel", you MUST send a single message with multiple ${TASK_TOOL_NAME} tool use content blocks. For example, if you need to launch both a code-reviewer agent and a test-runner agent in parallel, send a single message with both tool calls.
Example usage:
<example_agent_descriptions>
"code-reviewer": use this agent after you are done writing a signficant piece of code
"greeting-responder": use this agent when to respond to user greetings with a friendly joke
</example_agent_description>
<example>
user: "Please write a function that checks if a number is prime"
assistant: Sure let me write a function that checks if a number is prime
assistant: First let me use the ${WRITE_TOOL_NAME} tool to write a function that checks if a number is prime
assistant: I'm going to use the ${WRITE_TOOL_NAME} tool to write the following code:
<code>
function isPrime(n) {
if (n <= 1) return false
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) return false
}
return true
}
</code>
<commentary>
Since a signficant piece of code was written and the task was completed, now use the code-reviewer agent to review the code
</commentary>
assistant: Now let me use the code-reviewer agent to review the code
assistant: Uses the ${TASK_TOOL_NAME} tool to launch the code-reviewer agent
</example>
<example>
user: "Hello"
<commentary>
Since the user is greeting, use the greeting-responder agent to respond with a friendly joke
</commentary>
assistant: "I'm going to use the ${TASK_TOOL_NAME} tool to launch the greeting-responder agent"
</example>
@@ -42,6 +42,9 @@ const TOOL_CATEGORY_MAP: Record<string, string> = {
todo_write: 'todo',
// plan
ask_user_question: 'plan',
// task
task: 'task',
agent_output: 'task',
};
export function loadDescription(toolName: string): string {
+40 -13
View File
@@ -6,6 +6,8 @@ import { toolRegistry } from '../registry.js';
import { SessionManager } from '../../session/index.js';
import { getAgentManager } from '../../agent/manager.js';
import { loadVisionConfig } from '../../utils/config.js';
import { loadDescription } from '../load_description.js';
import { renderTemplate, createToolDescriptionContext } from '../../template/renderer.js';
/**
* 生成短 ID8 字符)
@@ -47,27 +49,52 @@ export function getTaskContext(): typeof taskContext {
}
/**
* 获取 Task 工具动态描述
* 生成 Agent 类型注册表字符串(用于模板变量)
*/
function getTaskDescription(): string {
function generateAgentTypeRegistryString(): string {
const subagents = agentRegistry.listSubagents();
if (subagents.length === 0) {
return '执行子任务(当前没有可用的子 Agent)';
return '(当前没有可用的子 Agent';
}
const agentList = subagents
.map((a) => `- ${a.name}: ${a.description}`)
.join('\n');
return subagents.map((a) => `- ${a.name}: ${a.description}`).join('\n');
}
return `启动子 Agent 执行复杂任务,支持后台运行和模型选择。
/**
* 获取 Task 工具动态描述
* 使用模板系统渲染描述,支持变量替换
*/
function getTaskDescription(): string {
try {
// 加载模板
const template = loadDescription('task');
// 创建上下文,添加 AGENT_TYPE_REGISTRY_STRING 变量
const context = createToolDescriptionContext();
const extendedContext = {
...context,
custom: {
...context.custom,
AGENT_TYPE_REGISTRY_STRING: generateAgentTypeRegistryString(),
},
};
// 渲染模板
return renderTemplate(template, extendedContext);
} catch {
// 如果加载模板失败,回退到简单描述
const subagents = agentRegistry.listSubagents();
if (subagents.length === 0) {
return '执行子任务(当前没有可用的子 Agent)';
}
const agentList = subagents.map((a) => `- ${a.name}: ${a.description}`).join('\n');
return `启动子 Agent 执行复杂任务,支持后台运行和模型选择。
可用的 Agent:
${agentList}
使用示例:
- 同步执行: task({ subagent_type: "explore", prompt: "找到所有 API 路由" })
- 后台运行: task({ subagent_type: "code-reviewer", prompt: "审查代码", run_in_background: true })
- 指定模型: task({ subagent_type: "general", prompt: "复杂分析", model: "opus" })`;
${agentList}`;
}
}
/**