多个任务之间有依赖关系怎么搞Java实现代码public class TaskSystem { // --- 配置 --- private static final Path WORKDIR Paths.get(System.getProperty(user.dir)); private static final Path TASKS_DIR WORKDIR.resolve(.tasks); // 任务存储目录 private static final Gson gson new GsonBuilder().setPrettyPrinting().create(); // --- 工具枚举--- public enum ToolType { BASH(bash, Run a shell command.), READ_FILE(read_file, Read file contents.), WRITE_FILE(write_file, Write content to file.), EDIT_FILE(edit_file, Replace exact text in file.), TASK_CREATE(task_create, Create a new task.), // 新增创建任务 TASK_GET(task_get, Get full details of a task by ID.), // 新增获取任务详情 TASK_UPDATE(task_update, Update a tasks status or dependencies.), // 新增更新任务 TASK_LIST(task_list, List all tasks with status summary.); // 新增列出任务 public final String name; public final String description; ToolType(String name, String description) { this.name name; this.description description; } } // ... 省略相同的 ToolExecutor 接口 // --- 任务管理器 --- static class TaskManager { private final Path tasksDir; private int nextId 1; public TaskManager(Path tasksDir) throws IOException { this.tasksDir tasksDir; Files.createDirectories(tasksDir); this.nextId getMaxId() 1; // 自动计算下一个ID } private int getMaxId() { // 扫描已有任务文件找到最大ID try { return Files.list(tasksDir) .filter(p - p.getFileName().toString().startsWith(task_)) .map(p - { try { String name p.getFileName().toString(); return Integer.parseInt(name.substring(5, name.length() - 5)); // task_xxx.json } catch (NumberFormatException e) { return 0; } }) .max(Integer::compare) .orElse(0); } catch (IOException e) { return 0; } } private MapString, Object loadTask(int taskId) throws IOException { Path path tasksDir.resolve(task_ taskId .json); if (!Files.exists(path)) { throw new IllegalArgumentException(Task taskId not found); } String content Files.readString(path); Type type new TypeTokenMapString, Object(){}.getType(); return gson.fromJson(content, type); } private void saveTask(MapString, Object task) throws IOException { int id ((Double) task.get(id)).intValue(); Path path tasksDir.resolve(task_ id .json); Files.writeString(path, gson.toJson(task)); // JSON格式存储 } public String createTask(String subject, String description) throws IOException { MapString, Object task new LinkedHashMap(); task.put(id, nextId); task.put(subject, subject); task.put(description, description ! null ? description : ); task.put(status, pending); // 默认状态 task.put(blockedBy, new ArrayListInteger()); // 被哪些任务阻塞 task.put(blocks, new ArrayListInteger()); // 阻塞哪些任务 task.put(owner, ); // 任务负责人 saveTask(task); nextId; return gson.toJson(task); } public String getTask(int taskId) throws IOException { return gson.toJson(loadTask(taskId)); } public String updateTask(int taskId, String status, ListInteger addBlockedBy, ListInteger addBlocks) throws IOException { MapString, Object task loadTask(taskId); if (status ! null) { if (!Arrays.asList(pending, in_progress, completed).contains(status)) { throw new IllegalArgumentException(Invalid status: status); } task.put(status, status); // 任务完成时从其他任务的 blockedBy 中移除 if (completed.equals(status)) { clearDependency(taskId); } } if (addBlockedBy ! null !addBlockedBy.isEmpty()) { SuppressWarnings(unchecked) ListInteger currentBlockedBy (ListInteger) task.get(blockedBy); ListInteger newList new ArrayList(currentBlockedBy); newList.addAll(addBlockedBy); task.put(blockedBy, newList.stream().distinct().collect(Collectors.toList())); } if (addBlocks ! null !addBlocks.isEmpty()) { SuppressWarnings(unchecked) ListInteger currentBlocks (ListInteger) task.get(blocks); ListInteger newList new ArrayList(currentBlocks); newList.addAll(addBlocks); ListInteger distinctBlocks newList.stream().distinct().collect(Collectors.toList()); task.put(blocks, distinctBlocks); // 双向更新更新被阻塞任务的 blockedBy 列表 for (int blockedId : distinctBlocks) { try { MapString, Object blockedTask loadTask(blockedId); SuppressWarnings(unchecked) ListInteger blockedByList (ListInteger) blockedTask.get(blockedBy); if (!blockedByList.contains(taskId)) { blockedByList.add(taskId); saveTask(blockedTask); } } catch (Exception e) { // 忽略不存在的任务 } } } saveTask(task); return gson.toJson(task); } private void clearDependency(int completedId) throws IOException { Files.list(tasksDir) .filter(p - p.getFileName().toString().endsWith(.json)) .forEach(p - { try { String content Files.readString(p); Type type new TypeTokenMapString, Object(){}.getType(); MapString, Object task gson.fromJson(content, type); SuppressWarnings(unchecked) ListInteger blockedBy (ListInteger) task.get(blockedBy); if (blockedBy ! null blockedBy.contains(completedId)) { blockedBy.remove(Integer.valueOf(completedId)); Files.writeString(p, gson.toJson(task)); } } catch (IOException e) { // 忽略读取错误 } }); } public String listAllTasks() throws IOException { ListMapString, Object tasks new ArrayList(); Files.list(tasksDir) .filter(p - p.getFileName().toString().endsWith(.json)) .sorted() .forEach(p - { try { String content Files.readString(p); Type type new TypeTokenMapString, Object(){}.getType(); tasks.add(gson.fromJson(content, type)); } catch (IOException e) { // 忽略错误 } }); if (tasks.isEmpty()) { return No tasks.; } StringBuilder sb new StringBuilder(); for (MapString, Object task : tasks) { String status (String) task.get(status); String marker switch(status) { case pending - [ ]; case in_progress - []; case completed - [x]; default - [?]; }; int id ((Double) task.get(id)).intValue(); String subject (String) task.get(subject); SuppressWarnings(unchecked) ListInteger blockedBy (ListInteger) task.get(blockedBy); String blockedStr (blockedBy ! null !blockedBy.isEmpty()) ? (blocked by: blockedBy ) : ; sb.append(String.format(%s #%d: %s%s\n, marker, id, subject, blockedStr)); } return sb.toString().trim(); } } // --- 工具处理器映射 --- private static final MapString, ToolExecutor TOOL_HANDLERS new HashMap(); static { // 初始化任务管理器 TaskManager taskManager; try { taskManager new TaskManager(TASKS_DIR); } catch (IOException e) { throw new RuntimeException(Failed to initialize task manager, e); } // ... 省略基础工具注册 // 注册 Task Create 工具 TOOL_HANDLERS.put(ToolType.TASK_CREATE.name, args - { String subject (String) args.get(subject); String description (String) args.get(description); return taskManager.createTask(subject, description); }); // 注册 Task Get 工具 TOOL_HANDLERS.put(ToolType.TASK_GET.name, args - { int taskId ((Number) args.get(task_id)).intValue(); return taskManager.getTask(taskId); }); // 注册 Task Update 工具 TOOL_HANDLERS.put(ToolType.TASK_UPDATE.name, args - { int taskId ((Number) args.get(task_id)).intValue(); String status (String) args.get(status); SuppressWarnings(unchecked) ListInteger addBlockedBy (ListInteger) args.get(addBlockedBy); SuppressWarnings(unchecked) ListInteger addBlocks (ListInteger) args.get(addBlocks); return taskManager.updateTask(taskId, status, addBlockedBy, addBlocks); }); // 注册 Task List 工具 TOOL_HANDLERS.put(ToolType.TASK_LIST.name, args - { return taskManager.listAllTasks(); }); } // ... 省略相同的工具实现和主循环 }核心思想利用基于文件的任务图Agent 开始理解任务间的先后顺序与并行逻辑成为真正的项目协调者。企业级任务管理系统架构核心思想从简单的内存中Todo管理器升级为持久化、结构化的企业级任务管理系统支持复杂依赖关系、多任务协同、状态持久化适用于真实的项目管理和协作场景。// 任务管理器 - 持久化存储架构 static class TaskManager { private final Path tasksDir; // 任务存储目录 private int nextId 1; // 自增ID public TaskManager(Path tasksDir) throws IOException { this.tasksDir tasksDir; Files.createDirectories(tasksDir); this.nextId getMaxId() 1; // 启动时自动计算下一个ID } // 文件系统存储每个任务存储为独立的JSON文件 // 自动ID管理启动时扫描现有文件避免ID冲突 // 持久化重启后任务状态不丢失 }企业级存储从内存中Todo升级为文件系统持久化存储原子操作每个任务独立文件避免并发问题增量ID自动管理任务ID支持大规模任务灾备恢复文件存储支持手动备份和恢复任务数据结构与依赖管理// 创建任务时初始化完整数据结构 public String createTask(String subject, String description) throws IOException { MapString, Object task new LinkedHashMap(); task.put(id, nextId); task.put(subject, subject); // 任务主题 task.put(description, description ! null ? description : ); // 详细描述 task.put(status, pending); // 状态pending/in_progress/completed task.put(blockedBy, new ArrayListInteger()); // 被哪些任务阻塞 task.put(blocks, new ArrayListInteger()); // 阻塞哪些任务 task.put(owner, ); // 任务负责人支持分派 // 结构化任务包含完整元数据和关系 // 依赖管理blockedBy和blocks双向记录依赖关系 // 权限控制owner字段支持任务分派 saveTask(task); nextId; return gson.toJson(task); }结构化元数据任务包含丰富的信息字段依赖管理支持任务间的阻塞/被阻塞关系扩展性预留owner字段支持团队协作JSON格式人类可读便于调试和手动修改双向依赖同步机制// 更新任务时自动同步依赖关系 public String updateTask(int taskId, String status, ListInteger addBlockedBy, ListInteger addBlocks) throws IOException { MapString, Object task loadTask(taskId); if (status ! null) { task.put(status, status); // 任务完成时从其他任务的 blockedBy 中移除 if (completed.equals(status)) { clearDependency(taskId); } } if (addBlocks ! null !addBlocks.isEmpty()) { // 双向更新更新被阻塞任务的 blockedBy 列表 for (int blockedId : distinctBlocks) { try { MapString, Object blockedTask loadTask(blockedId); SuppressWarnings(unchecked) ListInteger blockedByList (ListInteger) blockedTask.get(blockedBy); if (!blockedByList.contains(taskId)) { blockedByList.add(taskId); saveTask(blockedTask); } } catch (Exception e) { // 忽略不存在的任务 } } } // 依赖自动化更新一个任务时自动更新相关任务的依赖关系 // 完成清理任务完成后自动清理对它的阻塞依赖 // 容错处理忽略不存在的任务ID }关系自动维护更新一个任务的依赖时自动更新相关任务完成时清理任务完成后自动清理阻塞关系容错设计忽略不存在任务的引用数据一致性确保依赖关系的双向一致性复杂查询与可视化展示// 列出所有任务的摘要信息 public String listAllTasks() throws IOException { ListMapString, Object tasks new ArrayList(); Files.list(tasksDir) .filter(p - p.getFileName().toString().endsWith(.json)) .sorted() // 按文件名排序通常是ID顺序 .forEach(p - { // 逐个加载任务文件 }); StringBuilder sb new StringBuilder(); for (MapString, Object task : tasks) { String status (String) task.get(status); String marker switch(status) { case pending - [ ]; case in_progress - []; case completed - [x]; default - [?]; }; int id ((Double) task.get(id)).intValue(); String subject (String) task.get(subject); SuppressWarnings(unchecked) ListInteger blockedBy (ListInteger) task.get(blockedBy); String blockedStr (blockedBy ! null !blockedBy.isEmpty()) ? (blocked by: blockedBy ) : ; // 状态可视化[ ]待办 []进行中 [x]已完成 // 依赖提示显示哪些任务阻塞了当前任务 // 简洁摘要只显示关键信息 sb.append(String.format(%s #%d: %s%s\n, marker, id, subject, blockedStr)); } return sb.toString().trim(); }状态可视化用图标清晰展示任务状态依赖提示明确显示阻塞关系批量加载高效加载所有任务人性化格式便于人类阅读和理解任务工具生态系统// 完整的任务工具集定义 public enum ToolType { TASK_CREATE(task_create, Create a new task.), // CRUD: Create TASK_GET(task_get, Get full details of a task by ID.), // CRUD: Read TASK_UPDATE(task_update, Update a tasks status or dependencies.), // CRUD: Update TASK_LIST(task_list, List all tasks with status summary.); // CRUD: List // 完整CRUD创建、读取、更新、删除(通过更新状态为完成) // 语义清晰每个工具单一职责 // 与基础工具分离任务管理工具独立于文件操作工具 }完整CRUD提供完整的任务管理操作单一职责每个工具功能明确语义接口名称明确便于LLM理解分离关注任务工具与基础文件工具分离JSON存储格式// 任务存储格式示例 private static final Gson gson new GsonBuilder().setPrettyPrinting().create(); private void saveTask(MapString, Object task) throws IOException { int id ((Double) task.get(id)).intValue(); Path path tasksDir.resolve(task_ id .json); Files.writeString(path, gson.toJson(task)); // 美化的JSON格式 } // 标准化格式每个任务存储为格式化的JSON文件 // 命名规范task_id.json // 人类可读美化的JSON便于手动查看和编辑 // 可互操作标准JSON格式支持外部工具处理标准化存储JSON是通用的数据交换格式可读性美化格式便于调试可扩展随时可以添加新字段互操作性其他工具可以读取任务文件架构演进与价值从 AgentWithTodo 到 TaskSystem 的升级文章转载自程序员Seven原文链接https://www.cnblogs.com/sevencoding/p/19821558体验地址http://www.jnpfsoft.com/?from416