重构项目结构:迁移到单体架构并优化代码组织

- 删除分布式架构相关文件和配置
- 将backend-distributed重命名为backend保留分布式代码作为参考
- 优化backend-single单体架构实现
- 添加Coze API集成相关文档和测试
- 清理项目根目录的部署脚本和配置文件
- 更新WebSocket和消息服务实现
- 完善认证服务和密码加密功能
This commit is contained in:
2025-07-24 22:16:27 +08:00
parent 847f5126cf
commit ca42a7d9a4
308 changed files with 1263 additions and 13496 deletions
@@ -18,10 +18,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* AI聊天服务实现类
@@ -51,6 +49,9 @@ public class AiChatServiceImpl implements AIChatService {
@Value("${emotion.coze.api.base-url:https://api.coze.cn}")
private String cozeBaseUrl;
@Value("${emotion.coze.api.chat.path:/v3/chat}")
private String chatPath;
@Value("${emotion.coze.api.chat.talk.bot-id:}")
private String chatBotId;
@@ -74,6 +75,15 @@ public class AiChatServiceImpl implements AIChatService {
private static final String DEFAULT_USER_ID = "emotion-museum-user";
// API 相关常量
private static final String CONTENT_KEY = "content";
private static final String ROLE_KEY = "role";
private static final String USER_ROLE = "user";
private static final String ASSISTANT_ROLE = "assistant";
private static final String CONTENT_TYPE_KEY = "content_type";
private static final String TEXT_TYPE = "text";
private static final String ANSWER_TYPE = "answer";
@Override
public String sendChatMessage(String conversationId, String message, String userId) {
log.info("发送聊天消息: conversationId={}, userId={}, message={}", conversationId, userId, message);
@@ -83,22 +93,22 @@ public class AiChatServiceImpl implements AIChatService {
String aiReply = sendMessage(conversationId, message, userId);
// 保存用户消息
Message userMessage = messageService.createMessage(
conversationId,
userId,
message,
"text",
"user",
userId);
Message userMessage = new Message();
userMessage.setConversationId(conversationId);
userMessage.setCreateBy(userId);
userMessage.setContent(message);
userMessage.setType("text");
userMessage.setSender("user");
userMessage = messageService.createMessage(userMessage);
// 保存AI回复
Message aiMessage = messageService.createMessage(
conversationId,
"ai",
aiReply,
"text",
"ai",
"ai");
Message aiMessage = new Message();
aiMessage.setConversationId(conversationId);
aiMessage.setCreateBy("ai");
aiMessage.setContent(aiReply);
aiMessage.setType("text");
aiMessage.setSender("ai");
aiMessage = messageService.createMessage(aiMessage);
log.info("聊天消息处理完成: userMessageId={}, aiMessageId={}",
userMessage.getId(), aiMessage.getId());
@@ -166,13 +176,14 @@ public class AiChatServiceImpl implements AIChatService {
headers.set("Authorization", "Bearer " + cozeApiToken);
headers.set("Content-Type", "application/json");
// 构建请求体 - 参考backend-distributed的实现
// 构建请求体 - 使用正确的Coze API格式
Map<String, Object> requestBody = buildCozeRequest(conversationId, userMessage, userId);
HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers);
// 构建完整的API URL
String cozeApiUrl = cozeBaseUrl + "/api/message";
String cozeApiUrl = cozeBaseUrl + chatPath;
log.info("发送Coze请求到: {}, 请求体: {}", cozeApiUrl, requestBody);
// 发送请求
ResponseEntity<String> response = restTemplate.exchange(
@@ -181,13 +192,22 @@ public class AiChatServiceImpl implements AIChatService {
request,
String.class);
// 解析响应
log.info("收到Coze初始响应: {}", response.getBody());
// 解析响应获取chat_id和conversation_id
JSONObject responseJson = JSON.parseObject(response.getBody());
String aiReply = extractContentFromCozeResponse(responseJson);
String chatId = extractChatIdFromResponse(responseJson);
String cozeConversationId = extractConversationIdFromResponse(responseJson);
log.info("Coze AI响应成功: reply={}", aiReply);
return aiReply;
if (chatId != null && cozeConversationId != null) {
// 轮询聊天状态直到完成并获取回复内容
String aiReply = waitForChatCompletion(chatId, cozeConversationId);
log.info("Coze AI响应成功: reply={}", aiReply);
return aiReply;
} else {
log.error("无法从Coze响应中获取chat_id或conversation_id");
return "抱歉,AI服务响应异常,请稍后再试。";
}
} catch (Exception e) {
log.error("发送消息到Coze AI失败", e);
@@ -209,22 +229,22 @@ public class AiChatServiceImpl implements AIChatService {
String aiReply = sendMessage(guestConversationId, message, "guest");
// 保存访客消息
Message guestMessage = messageService.createMessage(
guestConversationId,
"guest",
message,
"text",
"guest",
clientIp);
Message guestMessage = new Message();
guestMessage.setConversationId(guestConversationId);
guestMessage.setCreateBy("guest");
guestMessage.setContent(message);
guestMessage.setType("text");
guestMessage.setSender("guest");
guestMessage = messageService.createMessage(guestMessage);
// 保存AI回复
Message aiMessage = messageService.createMessage(
guestConversationId,
"ai",
aiReply,
"text",
"ai",
"ai");
Message aiMessage = new Message();
aiMessage.setConversationId(guestConversationId);
aiMessage.setCreateBy("ai");
aiMessage.setContent(aiReply);
aiMessage.setType("text");
aiMessage.setSender("ai");
aiMessage = messageService.createMessage(aiMessage);
result.put("message", aiReply);
result.put("messageId", aiMessage.getId());
@@ -252,9 +272,6 @@ public class AiChatServiceImpl implements AIChatService {
Map<String, Object> result = new HashMap<>();
try {
// 创建数据库对话记录
String conversationId = UUID.randomUUID().toString();
// 调用数据库服务创建对话
Conversation conversation = conversationService.createConversation(userId, title, "user");
@@ -324,20 +341,19 @@ public class AiChatServiceImpl implements AIChatService {
@Override
public boolean healthCheck() {
try {
// 调用Coze bot信息接口检查健康状态
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + cozeApiToken);
// 简化健康检查 - 检查必要配置是否存在
boolean configValid = cozeApiToken != null && !cozeApiToken.trim().isEmpty() &&
chatBotId != null && !chatBotId.trim().isEmpty() &&
cozeBaseUrl != null && !cozeBaseUrl.trim().isEmpty();
HttpEntity<String> request = new HttpEntity<>(headers);
if (!configValid) {
log.warn("Coze API 配置不完整");
return false;
}
ResponseEntity<String> response = restTemplate.exchange(
cozeBaseUrl + "/v1/bot/get_online_info?bot_id=" + chatBotId,
HttpMethod.GET,
request,
String.class);
JSONObject responseJson = JSON.parseObject(response.getBody());
return responseJson != null && responseJson.get("code") != null;
// 可选:发送一个简单的测试请求
// 这里可以调用一个轻量级的API来验证连接
return true;
} catch (Exception e) {
log.error("健康检查失败: {}", e.getMessage());
@@ -346,7 +362,7 @@ public class AiChatServiceImpl implements AIChatService {
}
/**
* 构建Coze API请求 - 参考backend-distributed的实现
* 构建Coze API请求 - 根据官方文档修正格式
*/
private Map<String, Object> buildCozeRequest(String conversationId, String userMessage, String userId) {
Map<String, Object> cozeRequest = new HashMap<>();
@@ -360,21 +376,14 @@ public class AiChatServiceImpl implements AIChatService {
cozeRequest.put("user_id", userId != null ? userId : DEFAULT_USER_ID);
cozeRequest.put("stream", false);
// 构建消息内容
String message = userMessage;
if (conversationId != null && !conversationId.trim().isEmpty()) {
// 可以在这里添加上下文信息
message = "会话ID: " + conversationId + "\n\n用户消息: " + message;
}
// 添加聊天历史(简化版本)
// 构建消息列表 - 按照 Coze API 标准格式
java.util.List<Map<String, Object>> messages = new java.util.ArrayList<>();
// 添加当前消息
// 添加当前用户消息
Map<String, Object> currentMsg = new HashMap<>();
currentMsg.put("role", "user");
currentMsg.put("content", message);
currentMsg.put("content_type", "text");
currentMsg.put(ROLE_KEY, USER_ROLE);
currentMsg.put(CONTENT_KEY, userMessage);
currentMsg.put(CONTENT_TYPE_KEY, TEXT_TYPE);
currentMsg.put("type", "question");
messages.add(currentMsg);
@@ -385,36 +394,156 @@ public class AiChatServiceImpl implements AIChatService {
}
/**
* 从Coze响应中提取内容
* 从Coze响应中提取chat_id
*/
private String extractContentFromCozeResponse(JSONObject responseJson) {
private String extractChatIdFromResponse(JSONObject responseJson) {
try {
if (responseJson != null && responseJson.get("data") != null) {
JSONObject data = responseJson.getJSONObject("data");
// 根据Coze API响应格式解析内容
if (data.get("messages") != null) {
java.util.List<JSONObject> messages = data.getJSONArray("messages").toJavaList(JSONObject.class);
for (JSONObject message : messages) {
if ("assistant".equals(message.getString("role")) &&
"answer".equals(message.getString("type"))) {
return message.getString("content");
}
}
}
// 兼容旧格式
if (data.getString("reply") != null) {
return data.getString("reply");
}
if (responseJson != null && responseJson.getJSONObject("data") != null) {
return responseJson.getJSONObject("data").getString("id");
}
return "抱歉,我现在无法理解您的消息。";
} catch (Exception e) {
log.error("解析Coze响应失败: {}", e.getMessage());
return "抱歉,响应解析出现问题。";
log.error("提取chat_id失败: {}", e.getMessage());
}
return null;
}
/**
* 从Coze响应中提取conversation_id
*/
private String extractConversationIdFromResponse(JSONObject responseJson) {
try {
if (responseJson != null && responseJson.getJSONObject("data") != null) {
return responseJson.getJSONObject("data").getString("conversation_id");
}
} catch (Exception e) {
log.error("提取conversation_id失败: {}", e.getMessage());
}
return null;
}
/**
* 等待聊天完成并获取回复内容
*/
private String waitForChatCompletion(String chatId, String conversationId) {
try {
// 最多等待30秒,每2秒轮询一次
int maxAttempts = 15;
int attempt = 0;
while (attempt < maxAttempts) {
log.info("轮询聊天状态,第{}次尝试: chatId={}, conversationId={}", attempt + 1, chatId, conversationId);
// 构建状态查询URL
String statusUrl = cozeBaseUrl + "/v3/chat/retrieve?chat_id=" + chatId + "&conversation_id=" + conversationId;
// 构建请求头
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + cozeApiToken);
headers.set("Content-Type", "application/json");
HttpEntity<String> request = new HttpEntity<>(headers);
// 发送状态查询请求
ResponseEntity<String> response = restTemplate.exchange(
statusUrl,
HttpMethod.GET,
request,
String.class);
JSONObject statusResponse = JSON.parseObject(response.getBody());
log.info("轮询响应: {}", statusResponse);
if (statusResponse != null && statusResponse.getJSONObject("data") != null) {
JSONObject data = statusResponse.getJSONObject("data");
String status = data.getString("status");
log.info("聊天状态: {}", status);
if ("completed".equals(status)) {
// 聊天完成,获取消息
log.info("聊天完成,开始获取消息: chatId={}, conversationId={}", chatId, conversationId);
return getChatMessages(chatId, conversationId);
} else if ("failed".equals(status)) {
log.error("Coze聊天失败: chatId={}, conversationId={}", chatId, conversationId);
return "抱歉,AI服务暂时不可用,请稍后再试。";
}
} else {
log.warn("轮询响应为空或无data字段: {}", statusResponse);
}
// 等待2秒后重试
Thread.sleep(2000);
attempt++;
}
log.warn("Coze聊天超时: chatId={}, conversationId={}", chatId, conversationId);
return "抱歉,AI响应超时,请稍后再试。";
} catch (Exception e) {
log.error("等待Coze聊天完成失败: chatId={}, conversationId={}, error={}",
chatId, conversationId, e.getMessage(), e);
return "抱歉,AI服务出现错误,请稍后再试。";
}
}
/**
* 获取聊天消息
*/
private String getChatMessages(String chatId, String conversationId) {
try {
log.info("获取聊天消息: chatId={}, conversationId={}", chatId, conversationId);
// 构建消息查询URL
String messagesUrl = cozeBaseUrl + "/v3/chat/message/list?chat_id=" + chatId + "&conversation_id=" + conversationId;
// 构建请求头
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + cozeApiToken);
headers.set("Content-Type", "application/json");
HttpEntity<String> request = new HttpEntity<>(headers);
// 发送消息查询请求
ResponseEntity<String> response = restTemplate.exchange(
messagesUrl,
HttpMethod.GET,
request,
String.class);
JSONObject messagesResponse = JSON.parseObject(response.getBody());
log.info("消息响应: {}", messagesResponse);
if (messagesResponse != null && messagesResponse.getJSONArray("data") != null) {
java.util.List<JSONObject> messages = messagesResponse.getJSONArray("data").toJavaList(JSONObject.class);
log.info("收到{}条消息", messages.size());
// 查找AI的回复消息(role=assistant, type=answer
for (JSONObject message : messages) {
String role = message.getString("role");
String type = message.getString("type");
log.info("消息详情: role={}, type={}, content={}", role, type, message.getString("content"));
if (ASSISTANT_ROLE.equals(role) && ANSWER_TYPE.equals(type)) {
String content = message.getString("content");
log.info("找到AI回复: {}", content);
return content;
}
}
log.warn("未找到AI回复消息");
} else {
log.warn("消息响应为空或无data字段");
}
return "抱歉,未能获取到AI回复。";
} catch (Exception e) {
log.error("获取Coze聊天消息失败: chatId={}, conversationId={}, error={}",
chatId, conversationId, e.getMessage(), e);
return "抱歉,获取AI回复失败。";
}
}
/**
* 发送总结消息到Coze AI
*/
@@ -433,8 +562,9 @@ public class AiChatServiceImpl implements AIChatService {
HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers);
// 构建完整的API URL
String cozeApiUrl = cozeBaseUrl + "/api/message";
String cozeApiUrl = cozeBaseUrl + chatPath;
log.info("发送Coze总结请求到: {}, 请求体: {}", cozeApiUrl, requestBody);
// 发送请求
ResponseEntity<String> response = restTemplate.exchange(
cozeApiUrl,
@@ -442,13 +572,22 @@ public class AiChatServiceImpl implements AIChatService {
request,
String.class);
// 解析响应
log.info("收到Coze总结初始响应: {}", response.getBody());
// 解析响应获取chat_id和conversation_id
JSONObject responseJson = JSON.parseObject(response.getBody());
String aiReply = extractContentFromCozeResponse(responseJson);
String chatId = extractChatIdFromResponse(responseJson);
String cozeConversationId = extractConversationIdFromResponse(responseJson);
log.info("Coze AI总结响应成功: reply={}", aiReply);
return aiReply;
if (chatId != null && cozeConversationId != null) {
// 轮询聊天状态直到完成并获取回复内容
String aiReply = waitForChatCompletion(chatId, cozeConversationId);
log.info("Coze AI总结响应成功: reply={}", aiReply);
return aiReply;
} else {
log.error("无法从Coze总结响应中获取chat_id或conversation_id");
return "抱歉,AI总结服务响应异常,请稍后再试。";
}
} catch (Exception e) {
log.error("发送总结消息到Coze AI失败", e);
@@ -461,7 +600,7 @@ public class AiChatServiceImpl implements AIChatService {
*/
private Map<String, Object> buildSummaryRequest(String conversationId, String userMessage, String userId) {
Map<String, Object> cozeRequest = new HashMap<>();
cozeRequest.put("bot_id", summaryBotId);
cozeRequest.put("bot_id", summaryBotId != null && !summaryBotId.trim().isEmpty() ? summaryBotId : chatBotId);
// 如果有总结workflow_id,则添加
if (summaryWorkflowId != null && !summaryWorkflowId.trim().isEmpty()) {
@@ -470,27 +609,24 @@ public class AiChatServiceImpl implements AIChatService {
cozeRequest.put("user_id", userId != null ? userId : DEFAULT_USER_ID);
cozeRequest.put("stream", false);
cozeRequest.put("auto_save_history", true);
// 构建消息内容
String message = userMessage;
// 如果有会话ID,则添加
if (conversationId != null && !conversationId.trim().isEmpty()) {
// 可以在这里添加上下文信息
message = "会话ID: " + conversationId + "\n\n总结内容: " + message;
cozeRequest.put("conversation_id", conversationId);
}
// 添加聊天历史(简化版本)
// 构建消息列表 - 按照 Coze API 标准格式
java.util.List<Map<String, Object>> messages = new java.util.ArrayList<>();
// 添加当前消息
// 添加当前用户消息
Map<String, Object> currentMsg = new HashMap<>();
currentMsg.put("role", "user");
currentMsg.put("content", message);
currentMsg.put("content_type", "text");
currentMsg.put("type", "question");
currentMsg.put(ROLE_KEY, USER_ROLE);
currentMsg.put(CONTENT_KEY, userMessage);
currentMsg.put(CONTENT_TYPE_KEY, TEXT_TYPE);
messages.add(currentMsg);
cozeRequest.put("additional_messages", messages);
cozeRequest.put("parameters", new HashMap<>());
return cozeRequest;
}