
public class AgentWithTodo { private static final Path WORKDIR Paths.get(System.getProperty(user.dir)); // --- 1. 状态管理TodoManager --- // 任务状态枚举 public enum TaskStatus { PENDING(pending), IN_PROGRESS(in_progress), COMPLETED(completed); public final String label; TaskStatus(String label) { this.label label; } public static TaskStatus fromLabel(String s) { for (TaskStatus ts : values()) if (ts.label.equals(s)) return ts; return PENDING; } } // 任务实体 public static class TodoItem { public String id; public String text; public TaskStatus status; public TodoItem(String id, String text, String status) { this.id id; this.text text; this.status TaskStatus.fromLabel(status); } } // 管理器类 public static class TodoManager { private ListTodoItem items new ArrayList(); public String update(ListMapString, Object newItems) throws Exception { if (newItems.size() 20) throw new Exception(Max 20 todos allowed); ListTodoItem validated new ArrayList(); int inProgressCount 0; for (int i 0; i newItems.size(); i) { MapString, Object item newItems.get(i); String text (String) item.getOrDefault(text, ); String statusStr (String) item.getOrDefault(status, pending); String id String.valueOf(item.getOrDefault(id, String.valueOf(i 1))); if (text.trim().isEmpty()) throw new Exception(Item id : text required); TaskStatus status TaskStatus.fromLabel(statusStr.toLowerCase()); if (status TaskStatus.IN_PROGRESS) inProgressCount; validated.add(new TodoItem(id, text.trim(), status.label)); } if (inProgressCount 1) throw new Exception(Only one task can be in_progress at a time); this.items validated; return render(); } public String render() { if (items.isEmpty()) return No todos.; StringBuilder sb new StringBuilder(); for (TodoItem item : items) { String marker item.status TaskStatus.PENDING ? [ ] : item.status TaskStatus.IN_PROGRESS ? [] : [x]; sb.append(String.format(%s #%s: %s%n, marker, item.id, item.text)); } long done items.stream().filter(i - i.status TaskStatus.COMPLETED).count(); sb.append(String.format(%n(%d/%d completed), done, items.size())); return sb.toString(); } } private static final TodoManager TODO_MANAGER new TodoManager(); // --- 2. 工具定义与分发 --- public enum ToolType { BASH(bash), READ_FILE(read_file), WRITE_FILE(write_file), EDIT_FILE(edit_file), TODO(todo); // 新增 todo 工具 public final String name; ToolType(String name) { this.name name; } } private static final MapString, ToolExecutor TOOL_HANDLERS new HashMap(); static { // ... 省略已有的工具注册 // 注册 Todo 工具 TOOL_HANDLERS.put(ToolType.TODO.name, args - { SuppressWarnings(unchecked) ListMapString, Object items (ListMapString, Object) args.get(items); return TODO_MANAGER.update(items); }); } // --- 3. 核心循环 --- public static void agentLoop(ListMapString, Object messages) { int roundsSinceTodo 0; // 新增跟踪轮数 while (true) { // ... 省略相同的 LLM 调用、消息追加、停止检查逻辑 // 3. 执行工具 ListMapString, Object toolResults new ArrayList(); ListMapString, Object content (ListMapString, Object) response.get(content); boolean usedTodo false; // 新增标记是否使用了 todo 工具 for (MapString, Object block : content) { if (tool_use.equals(block.get(type))) { // ... 省略相同的工具调用逻辑 String toolName (String) block.get(name); // ... 执行工具 if (toolName.equals(todo)) usedTodo true; // 标记 todo 使用 } } // 4. 监工逻辑 (Nag Reminder) roundsSinceTodo usedTodo ? 0 : roundsSinceTodo 1; if (roundsSinceTodo 3) { // 关键超过3轮没更新就提醒 MapString, Object nag new HashMap(); nag.put(type, text); nag.put(text, reminderUpdate your todos./reminder); toolResults.add(0, nag); // 插入到结果列表最前面 System.out.println( 监工提醒更新待办列表); } // 5. 回传结果 // ... 省略相同的回传逻辑 } } // --- 4. 工具实现 (简化版) --- // ... 省略已有的工具实现 }状态管理TodoManager 类为Agent引入长期记忆和工作进度追踪能力让Agent能记住自己的任务列表和工作状态。java// 任务状态枚举 public enum TaskStatus { PENDING(pending), IN_PROGRESS(in_progress), COMPLETED(completed); // 状态枚举明确定义三种状态 // 状态驱动Agent根据状态决定下一步操作 }java// 任务实体 - 数据结构 public static class TodoItem { public String id; // 唯一标识 public String text; // 任务描述 public TaskStatus status; // 状态 // 结构化的任务表示 // 为LLM提供清晰的上下文 }java// TodoManager - 核心状态管理 public class TodoManager { private ListTodoItem items new ArrayList(); // 状态存储 public String update(ListMapString, Object newItems) throws Exception { if (newItems.size() 20) throw new Exception(Max 20 todos allowed); // 业务规则1限制任务数量防止滥用