feat: 集成 TodoWrite 工具实现任务管理和进度追踪

- 添加 TodoWriteTool 用于 Agent 任务分解和进度管理
- 实现 TodoUpdateEvent 事件发布机制
- 添加 TodoProgressListener 监听器实时显示任务进度
- 支持任务状态转换:pending → in_progress → completed
- 在控制台显示任务进度百分比和状态图标

参考示例: spring-ai-agent-util-examples/todo-demo

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-30 15:35:34 +08:00
1 parent 7141a3f72c
commit 1a9f8d047a
1 file changed
+64 -2
@@ -6,6 +6,9 @@ import java.util.Map;
import org.springaicommunity.agent.tools.FileSystemTools; import org.springaicommunity.agent.tools.FileSystemTools;
import org.springaicommunity.agent.tools.ShellTools; import org.springaicommunity.agent.tools.ShellTools;
import org.springaicommunity.agent.tools.SkillsTool; import org.springaicommunity.agent.tools.SkillsTool;
import org.springaicommunity.agent.tools.TodoWriteTool;
import org.springaicommunity.agent.tools.TodoWriteTool.Todos;
import org.springaicommunity.agent.tools.TodoWriteTool.Todos.TodoItem;
import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.ToolCallAdvisor; import org.springframework.ai.chat.client.advisor.ToolCallAdvisor;
import org.springframework.ai.chat.memory.ChatMemory; import org.springframework.ai.chat.memory.ChatMemory;
@@ -16,9 +19,13 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
@SpringBootApplication @SpringBootApplication
@@ -70,14 +77,21 @@ public class Application {
@Bean("chatClientBuilder") @Bean("chatClientBuilder")
ChatClient.Builder chatClientBuilder(ChatModel chatModel, ChatClient.Builder chatClientBuilder(ChatModel chatModel,
@Value("${agent.skills.dirs:Unknown}") List<Resource> agentSkillsDirs, @Value("${agent.skills.dirs:Unknown}") List<Resource> agentSkillsDirs,
SyncMcpToolCallbackProvider mcpToolCallbackProvider) { SyncMcpToolCallbackProvider mcpToolCallbackProvider,
ApplicationEventPublisher applicationEventPublisher) {
// @formatter:off // @formatter:off
return ChatClient.builder(chatModel) return ChatClient.builder(chatModel)
// Skills tool // Skills tool
.defaultToolCallbacks(SkillsTool.builder().addSkillsResources(agentSkillsDirs).build()) .defaultToolCallbacks(SkillsTool.builder().addSkillsResources(agentSkillsDirs).build())
// Todo management tool
.defaultTools(TodoWriteTool.builder()
// Publish todo update events
.todoEventHandler(event ->
applicationEventPublisher.publishEvent(new TodoUpdateEvent(this, event.todos())))
.build())
// Built-in tools // Built-in tools
.defaultTools( .defaultTools(
//Bash execution tool //Bash execution tool
@@ -127,4 +141,52 @@ public class Application {
.build(); .build();
} }
/**
* Todo 更新事件
*/
public static class TodoUpdateEvent extends ApplicationEvent {
private final List<TodoItem> todos;
public TodoUpdateEvent(Object source, List<TodoItem> todos) {
super(source);
this.todos = List.copyOf(todos);
}
public List<TodoItem> getTodos() {
return todos;
}
}
/**
* Todo 进度监听器
*/
@Component
public static class TodoProgressListener {
@EventListener
public void onTodoUpdate(TodoUpdateEvent event) {
int completed = (int) event.getTodos().stream().filter(t -> t.status() == Todos.Status.completed).count();
int total = event.getTodos().size();
if (total == 0) {
return;
}
System.out.printf("\n📋 Progress: %d/%d tasks completed (%.0f%%)\n", completed, total,
(completed * 100.0 / total));
for (TodoItem item : event.getTodos()) {
String statusIcon = switch (item.status()) {
case completed -> "[✓]";
case in_progress -> "[→]";
case pending -> "[ ]";
};
System.out.printf(" %s %s\n", statusIcon, item.content());
}
}
}
} }