2026-01-29 11:44:29 +08:00
|
|
|
package org.springaicommunity.skills;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import org.springaicommunity.agent.tools.FileSystemTools;
|
|
|
|
|
import org.springaicommunity.agent.tools.ShellTools;
|
|
|
|
|
import org.springaicommunity.agent.tools.SkillsTool;
|
2026-01-30 15:35:34 +08:00
|
|
|
import org.springaicommunity.agent.tools.TodoWriteTool;
|
|
|
|
|
import org.springaicommunity.agent.tools.TodoWriteTool.Todos;
|
|
|
|
|
import org.springaicommunity.agent.tools.TodoWriteTool.Todos.TodoItem;
|
2026-01-29 11:44:29 +08:00
|
|
|
import org.springframework.ai.chat.client.ChatClient;
|
|
|
|
|
import org.springframework.ai.chat.client.advisor.ToolCallAdvisor;
|
2026-01-29 20:58:16 +08:00
|
|
|
import org.springframework.ai.chat.memory.ChatMemory;
|
|
|
|
|
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
|
|
|
|
import org.springframework.ai.chat.model.ChatModel;
|
2026-01-30 15:15:46 +08:00
|
|
|
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
|
2026-01-29 20:58:16 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
2026-01-29 11:44:29 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.boot.SpringApplication;
|
|
|
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
2026-01-30 15:35:34 +08:00
|
|
|
import org.springframework.context.ApplicationEvent;
|
|
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
2026-01-29 11:44:29 +08:00
|
|
|
import org.springframework.context.annotation.Bean;
|
2026-01-29 20:58:16 +08:00
|
|
|
import org.springframework.context.annotation.Configuration;
|
2026-01-30 15:35:34 +08:00
|
|
|
import org.springframework.context.event.EventListener;
|
2026-01-29 11:44:29 +08:00
|
|
|
import org.springframework.core.io.Resource;
|
2026-01-30 15:35:34 +08:00
|
|
|
import org.springframework.stereotype.Component;
|
2026-01-29 11:44:29 +08:00
|
|
|
|
2026-01-29 20:58:16 +08:00
|
|
|
|
2026-01-29 11:44:29 +08:00
|
|
|
@SpringBootApplication
|
2026-01-29 20:58:16 +08:00
|
|
|
@Configuration
|
2026-01-29 11:44:29 +08:00
|
|
|
public class Application {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SpringApplication.run(Application.class, args);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 20:58:16 +08:00
|
|
|
/**
|
|
|
|
|
* 用于流式响应的 ChatClient.Builder (不支持工具调用)
|
|
|
|
|
*/
|
|
|
|
|
@Bean("streamChatClientBuilder")
|
|
|
|
|
ChatClient.Builder streamChatClientBuilder(ChatModel chatModel, @Value("${agent.skills.dirs:Unknown}") List<Resource> agentSkillsDirs) {
|
|
|
|
|
|
|
|
|
|
// @formatter:off
|
|
|
|
|
return ChatClient.builder(chatModel)
|
|
|
|
|
.defaultSystem("Always use the available skills to assist the user in their requests.")
|
|
|
|
|
|
|
|
|
|
// 流式响应不支持工具调用,所以不配置工具
|
|
|
|
|
|
|
|
|
|
.defaultAdvisors(
|
|
|
|
|
// Custom logging advisor
|
|
|
|
|
MyLoggingAdvisor.builder()
|
|
|
|
|
.showAvailableTools(false)
|
|
|
|
|
.showSystemMessage(false)
|
|
|
|
|
.build())
|
|
|
|
|
.defaultToolContext(Map.of("foo", "bar"));
|
|
|
|
|
// @formatter:on
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 流式 ChatClient 实例(预先构建好)
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
ChatClient streamChatClient(@Qualifier("streamChatClientBuilder") ChatClient.Builder streamChatClientBuilder, ChatMemory chatMemory) {
|
|
|
|
|
return streamChatClientBuilder
|
|
|
|
|
.defaultAdvisors(
|
|
|
|
|
org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor.builder(chatMemory).build()
|
|
|
|
|
)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用于非流式响应的 ChatClient.Builder (支持工具调用)
|
|
|
|
|
*/
|
|
|
|
|
@Bean("chatClientBuilder")
|
|
|
|
|
ChatClient.Builder chatClientBuilder(ChatModel chatModel,
|
|
|
|
|
@Value("${agent.skills.dirs:Unknown}") List<Resource> agentSkillsDirs,
|
2026-01-30 15:35:34 +08:00
|
|
|
SyncMcpToolCallbackProvider mcpToolCallbackProvider,
|
|
|
|
|
ApplicationEventPublisher applicationEventPublisher) {
|
2026-01-29 20:58:16 +08:00
|
|
|
|
|
|
|
|
// @formatter:off
|
|
|
|
|
return ChatClient.builder(chatModel)
|
|
|
|
|
// Skills tool
|
|
|
|
|
.defaultToolCallbacks(SkillsTool.builder().addSkillsResources(agentSkillsDirs).build())
|
|
|
|
|
|
2026-01-30 15:35:34 +08:00
|
|
|
// Todo management tool
|
|
|
|
|
.defaultTools(TodoWriteTool.builder()
|
|
|
|
|
// Publish todo update events
|
|
|
|
|
.todoEventHandler(event ->
|
|
|
|
|
applicationEventPublisher.publishEvent(new TodoUpdateEvent(this, event.todos())))
|
|
|
|
|
.build())
|
|
|
|
|
|
2026-01-30 18:05:52 +08:00
|
|
|
// 添加 System Prompt 指导 TodoWrite 工具的正确使用
|
|
|
|
|
.defaultSystem("""
|
|
|
|
|
When using the TodoWrite tool, you MUST provide the todos parameter as an object with a "todos" array, not a direct array.
|
|
|
|
|
|
|
|
|
|
Correct format:
|
|
|
|
|
{
|
|
|
|
|
"todos": [
|
|
|
|
|
{"content": "task 1", "status": "pending"},
|
|
|
|
|
{"content": "task 2", "status": "pending"}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Incorrect format (DO NOT USE):
|
|
|
|
|
[
|
|
|
|
|
{"content": "task 1", "status": "pending"},
|
|
|
|
|
{"content": "task 2", "status": "pending"}
|
|
|
|
|
]
|
|
|
|
|
""")
|
|
|
|
|
|
2026-01-29 20:58:16 +08:00
|
|
|
// Built-in tools
|
|
|
|
|
.defaultTools(
|
|
|
|
|
//Bash execution tool
|
|
|
|
|
ShellTools.builder().build(),// built-in shell tools
|
|
|
|
|
// Read, Write and Edit files tool
|
|
|
|
|
FileSystemTools.builder().build())// built-in file system tools
|
|
|
|
|
|
2026-01-30 15:15:46 +08:00
|
|
|
// MCP tools (通过 Spring AI 自动配置注入)
|
|
|
|
|
.defaultToolCallbacks(mcpToolCallbackProvider.getToolCallbacks())
|
2026-01-29 20:58:16 +08:00
|
|
|
|
|
|
|
|
.defaultAdvisors(
|
|
|
|
|
// Tool Calling advisor
|
|
|
|
|
ToolCallAdvisor.builder().build(),
|
|
|
|
|
// Custom logging advisor - order=-1 确保最先执行,可以捕获工具调用
|
|
|
|
|
MyLoggingAdvisor.builder()
|
|
|
|
|
.order(-1)
|
|
|
|
|
.showAvailableTools(true)
|
|
|
|
|
.showSystemMessage(true)
|
|
|
|
|
.build(),
|
|
|
|
|
// Tool call capture advisor (接收来自 MyLoggingAdvisor 的工具调用信息)
|
|
|
|
|
ToolCallCaptureAdvisor.builder().order(0).build(),
|
|
|
|
|
// LLM 请求响应日志记录 - order=100 确保最后执行
|
|
|
|
|
LLMLoggingAdvisor.builder()
|
|
|
|
|
.logDirectory("logs/llm")
|
|
|
|
|
.order(100)
|
|
|
|
|
.build())
|
|
|
|
|
.defaultToolContext(Map.of("foo", "bar"));
|
|
|
|
|
// @formatter:on
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 非流式 ChatClient 实例(预先构建好,支持工具调用)
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
ChatClient chatClient(@Qualifier("chatClientBuilder") ChatClient.Builder chatClientBuilder, ChatMemory chatMemory) {
|
|
|
|
|
return chatClientBuilder
|
|
|
|
|
.defaultAdvisors(
|
|
|
|
|
org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor.builder(chatMemory).build()
|
|
|
|
|
)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
ChatMemory chatMemory() {
|
|
|
|
|
return MessageWindowChatMemory.builder()
|
|
|
|
|
.maxMessages(100)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 15:35:34 +08:00
|
|
|
/**
|
|
|
|
|
* 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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 11:44:29 +08:00
|
|
|
}
|