聚合版 Agent 循环。 s01 展示 LLM -> 工具 -> LLM 的闭环,s02 开始把工具查找交给 ToolRegistry; s03 在工具执行前加入 PermissionManager,s04 开始在固定位置触发 hook; s06 新增 maxTurns,让父 Agent 和子 Agent 可以使用不同的循环上限。
| 23 | * s06 新增 maxTurns,让父 Agent 和子 Agent 可以使用不同的循环上限。 |
| 24 | */ |
| 25 | public class AgentLoop { |
| 26 | |
| 27 | private static final int DEFAULT_MAX_TURNS = 20; |
| 28 | |
| 29 | private final LlmClient llmClient; |
| 30 | |
| 31 | private final ToolRegistry toolRegistry; |
| 32 | |
| 33 | private final AgentLoopListener listener; |
| 34 | |
| 35 | private final PermissionManager permissionManager; |
| 36 | |
| 37 | private final HookManager hookManager; |
| 38 | |
| 39 | private final int maxTurns; |
| 40 | |
| 41 | public AgentLoop(LlmClient llmClient, List<Tool> tools) { |
| 42 | this(llmClient, tools, new AgentLoopListener() { |
| 43 | }, null); |
| 44 | } |
| 45 | |
| 46 | public AgentLoop(LlmClient llmClient, List<Tool> tools, AgentLoopListener listener) { |
| 47 | this(llmClient, tools, listener, null); |
| 48 | } |
| 49 | |
| 50 | public AgentLoop(LlmClient llmClient, List<Tool> tools, AgentLoopListener listener, |
| 51 | PermissionManager permissionManager) { |
| 52 | this(llmClient, toRegistry(tools), listener, permissionManager, null, DEFAULT_MAX_TURNS); |
| 53 | } |
| 54 | |
| 55 | public AgentLoop(LlmClient llmClient, ToolRegistry toolRegistry) { |
| 56 | this(llmClient, toolRegistry, new AgentLoopListener() { |
| 57 | }, (PermissionManager) null); |
| 58 | } |
| 59 | |
| 60 | public AgentLoop(LlmClient llmClient, ToolRegistry toolRegistry, AgentLoopListener listener) { |
| 61 | this(llmClient, toolRegistry, listener, (PermissionManager) null); |
| 62 | } |
| 63 | |
| 64 | public AgentLoop(LlmClient llmClient, ToolRegistry toolRegistry, AgentLoopListener listener, |
| 65 | PermissionManager permissionManager) { |
| 66 | this(llmClient, toolRegistry, listener, permissionManager, null, DEFAULT_MAX_TURNS); |
| 67 | } |
| 68 | |
| 69 | public AgentLoop(LlmClient llmClient, ToolRegistry toolRegistry, AgentLoopListener listener, |
| 70 | HookManager hookManager) { |
| 71 | this(llmClient, toolRegistry, listener, null, hookManager, DEFAULT_MAX_TURNS); |
| 72 | } |
| 73 | |
| 74 | public AgentLoop(LlmClient llmClient, ToolRegistry toolRegistry, AgentLoopListener listener, |
| 75 | int maxTurns) { |
| 76 | // 子 Agent 可以使用不同的轮数上限,但默认仍保持 20。 |
| 77 | this(llmClient, toolRegistry, listener, null, null, maxTurns); |
| 78 | } |
| 79 | |
| 80 | private AgentLoop(LlmClient llmClient, ToolRegistry toolRegistry, AgentLoopListener listener, |
| 81 | PermissionManager permissionManager, HookManager hookManager, int maxTurns) { |
| 82 | this.llmClient = llmClient; |
nothing calls this directly
no outgoing calls
no test coverage detected