This commit is contained in:
parent
42eb1c21e2
commit
2753063e70
|
|
@ -82,6 +82,28 @@ public class SseController {
|
||||||
clientUserIdMap.remove(clientId);
|
clientUserIdMap.remove(clientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 为新用户添加默认欢迎消息
|
||||||
|
Long userId = clientUserIdMap.get(clientId);
|
||||||
|
if (userId != null) {
|
||||||
|
String sessionId = sessionManager.getOrCreateSessionId(userId);
|
||||||
|
// 检查是否有对话历史
|
||||||
|
int messageCount = conversationHistoryManager.getMessageCount(sessionId);
|
||||||
|
if (messageCount == 0) {
|
||||||
|
// 添加欢迎消息到对话历史
|
||||||
|
String welcomeMessage = "您好,我是您的智能助手,我能帮你打开页面、查询订单、查询商品、查询库存等等。欢迎提问!";
|
||||||
|
conversationHistoryManager.addMessage(userId, sessionId, "assistant", welcomeMessage);
|
||||||
|
|
||||||
|
// 发送欢迎消息给客户端
|
||||||
|
try {
|
||||||
|
emitter.send(SseEmitter.event()
|
||||||
|
.name("message")
|
||||||
|
.data(welcomeMessage));
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("发送欢迎消息失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 定期发送心跳
|
// 定期发送心跳
|
||||||
executorService.scheduleAtFixedRate(() -> {
|
executorService.scheduleAtFixedRate(() -> {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
CREATE TABLE `ai_conversation_history` (
|
||||||
|
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id,自增',
|
||||||
|
`user_id` bigint(20) NOT NULL COMMENT '用户id',
|
||||||
|
`session_id` varchar(36) NOT NULL COMMENT '会话id',
|
||||||
|
`role` varchar(20) NOT NULL COMMENT '角色:user或assistant',
|
||||||
|
`content` text NOT NULL COMMENT '消息内容',
|
||||||
|
`timestamp` bigint(20) NOT NULL COMMENT '消息时间戳',
|
||||||
|
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||||
|
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
KEY `idx_session_id` (`session_id`),
|
||||||
|
KEY `idx_user_id` (`user_id`),
|
||||||
|
KEY `idx_timestamp` (`timestamp`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI聊天历史表';
|
||||||
Loading…
Reference in New Issue