feat: 将控制台应用改造为 Web 聊天界面

- 添加 ChatController 提供 REST API(流式/非流式聊天)
- 添加 ChatService 封装 ChatClient 调用逻辑
- 添加前端聊天界面(index.html、style.css、chat.js)
- 支持 SSE 流式响应和多轮对话记忆
- 集成 Markdown 渲染和代码高亮

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-29 20:57:28 +08:00
1 parent 6b4a347688
commit 13181ef406
6 files changed
+1306

No files matched your search

@@ -0,0 +1,59 @@
package org.springaicommunity.skills.controller;
import java.util.UUID;
import org.springaicommunity.skills.model.ChatRequest;
import org.springaicommunity.skills.service.ChatService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/api/chat")
public class ChatController {
private final ChatService chatService;
public ChatController(ChatService chatService) {
this.chatService = chatService;
}
/**
* 流式聊天接口(SSE)
*/
@PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> chatStream(
@RequestParam(required = false) String conversationId,
@RequestBody ChatRequest request) {
String finalConversationId = conversationId != null ? conversationId : UUID.randomUUID().toString();
return chatService.chatStream(finalConversationId, request.getMessage());
}
/**
* 非流式聊天接口(用于测试)
*/
@PostMapping("/chat")
public ResponseEntity<String> chat(
@RequestParam(required = false) String conversationId,
@RequestBody ChatRequest request) {
String finalConversationId = conversationId != null ? conversationId : UUID.randomUUID().toString();
String response = chatService.chat(finalConversationId, request.getMessage());
return ResponseEntity.ok(response);
}
/**
* 清空对话历史
*/
@DeleteMapping("/clear")
public ResponseEntity<String> clear(@RequestParam String conversationId) {
chatService.clear(conversationId);
return ResponseEntity.ok("Conversation cleared: " + conversationId);
}
}
@@ -0,0 +1,20 @@
package org.springaicommunity.skills.model;
public class ChatRequest {
private String message;
public ChatRequest() {
}
public ChatRequest(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
@@ -0,0 +1,110 @@
package org.springaicommunity.skills.service;
import java.util.Map;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;
@Service
public class ChatService {
private final ChatClient streamChatClient; // 流式用(不支持工具)
private final ChatClient chatClient; // 非流式用(支持工具)
private final ChatMemory chatMemory;
private final ObjectMapper objectMapper;
public ChatService(
@Qualifier("streamChatClient") ChatClient streamChatClient,
@Qualifier("chatClient") ChatClient chatClient,
ChatMemory chatMemory) {
this.streamChatClient = streamChatClient;
this.chatClient = chatClient;
this.chatMemory = chatMemory;
this.objectMapper = new ObjectMapper();
}
/**
* 流式聊天,返回 SSE 格式的响应(不支持工具调用)
*/
public Flux<String> chatStream(String conversationId, String userMessage) {
return streamChatClient.prompt()
.user(userMessage)
.advisors(a -> a.param("conversation_id", conversationId))
.stream()
.content()
.map(this::mapToSSE);
}
/**
* 非流式聊天(支持工具调用)
*/
public String chat(String conversationId, String userMessage) {
// 清空之前的工具调用记录
org.springaicommunity.skills.ToolCallCaptureAdvisor.clearToolCalls();
System.out.println("DEBUG: 开始聊天,conversationId=" + conversationId);
String content = chatClient.prompt()
.user(userMessage)
.advisors(a -> a.param("conversation_id", conversationId))
.call()
.content();
System.out.println("DEBUG: AI 回复内容: " + content);
// 获取工具调用信息
var toolCalls = org.springaicommunity.skills.ToolCallCaptureAdvisor.getToolCalls();
System.out.println("DEBUG: 捕获到的工具调用数量: " + (toolCalls != null ? toolCalls.size() : "null"));
// 如果有工具调用,将其附加到响应中
if (toolCalls != null && !toolCalls.isEmpty()) {
// 将工具调用信息作为 JSON 字符串附加到内容中
try {
String toolCallsJson = objectMapper.writeValueAsString(toolCalls);
System.out.println("DEBUG: 工具调用 JSON: " + toolCallsJson);
return content + "\n\n__TOOL_CALLS__:" + toolCallsJson;
} catch (Exception e) {
System.err.println("ERROR: 序列化工具调用失败: " + e.getMessage());
e.printStackTrace();
}
}
return content;
}
/**
* 清空对话历史
*/
public void clear(String conversationId) {
chatMemory.clear(conversationId);
}
/**
* 将文本内容映射为 SSE 事件
*/
private String mapToSSE(String content) {
try {
if (content != null && !content.isEmpty()) {
return formatSSE("text", objectMapper.writeValueAsString(
Map.of("content", content)
));
}
} catch (Exception e) {
return formatSSE("error", "{\"message\": \"" + e.getMessage() + "\"}");
}
return "";
}
/**
* 格式化 SSE 事件
*/
private String formatSSE(String eventType, String data) {
return "event: " + eventType + "\ndata: " + data + "\n\n";
}
}