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;
|
|
|
|
|
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;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
2026-01-29 20:58:16 +08:00
|
|
|
import org.springframework.context.annotation.Configuration;
|
2026-01-29 11:44:29 +08:00
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
|
|
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:15:46 +08:00
|
|
|
SyncMcpToolCallbackProvider mcpToolCallbackProvider) {
|
2026-01-29 20:58:16 +08:00
|
|
|
|
|
|
|
|
// @formatter:off
|
|
|
|
|
return ChatClient.builder(chatModel)
|
|
|
|
|
|
|
|
|
|
// Skills tool
|
|
|
|
|
.defaultToolCallbacks(SkillsTool.builder().addSkillsResources(agentSkillsDirs).build())
|
|
|
|
|
|
|
|
|
|
// 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-29 11:44:29 +08:00
|
|
|
}
|