package com.claudecode.tool.impl; import com.claudecode.tool.Tool; import com.claudecode.tool.ToolContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Map; /** * 子 Agent 工具 —— 对应 claude-code/src/tools/agent/AgentTool.ts。 *
* 创建一个独立的子 Agent 来处理复杂的子任务。子 Agent 拥有独立的消息历史, * 但共享工具集和上下文环境。适用于: *
* 注意:子 Agent 使用主 Agent 的 ChatModel 和工具集,
* 通过 ToolContext 中的 "agentLoop.factory" 获取 AgentLoop 工厂方法。
*/
public class AgentTool implements Tool {
private static final Logger log = LoggerFactory.getLogger(AgentTool.class);
/** ToolContext 中存储 AgentLoop 工厂的键名 */
public static final String AGENT_FACTORY_KEY = "__agent_factory__";
@Override
public String name() {
return "Agent";
}
@Override
public String description() {
return """
Launch a sub-agent to handle a complex task independently. \
The sub-agent has its own conversation context but shares tools \
and environment. Use this for tasks that require focused attention \
or when you want to isolate a subtask. \
The sub-agent will execute the given prompt and return its final response.""";
}
@Override
public String inputSchema() {
return """
{
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "The task description / prompt for the sub-agent"
},
"context": {
"type": "string",
"description": "Additional context or instructions (optional)"
}
},
"required": ["prompt"]
}""";
}
@Override
public String execute(Map