feat: 新增初始化会话接口,给前端返回初始化会话id

This commit is contained in:
郭诚奇
2025-12-16 13:54:20 +08:00
parent 4fba985ddd
commit 395dd3244f
3 changed files with 39 additions and 1 deletions

View File

@@ -12,6 +12,7 @@ import com.xboe.module.boecase.service.ICaseAiPermissionService;
import com.xboe.module.boecase.service.IElasticSearchIndexService; import com.xboe.module.boecase.service.IElasticSearchIndexService;
import com.xboe.module.boecase.vo.CaseAiMessageVo; import com.xboe.module.boecase.vo.CaseAiMessageVo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@@ -46,6 +47,25 @@ public class CaseAiChatApi extends ApiBaseController {
@Autowired @Autowired
private IElasticSearchIndexService elasticSearchIndexService; private IElasticSearchIndexService elasticSearchIndexService;
/**
* 初始化会话
* @return 会话id
*/
@GetMapping("/initChat")
public JsonResponse<String> initChat() {
try {
String conversationId = caseAiChatService.initChat(getCurrent());
if (StringUtils.isNotBlank(conversationId)) {
return success(conversationId, "初始化会话成功");
} else {
return success(conversationId, "初始化会话失败");
}
} catch (Exception e) {
log.error("初始化会话异常", e);
return error("初始化会话失败", e.getMessage());
}
}
/** /**
* 聊天 * 聊天
* @param caseAiChatDto * @param caseAiChatDto

View File

@@ -17,6 +17,13 @@ import java.util.List;
*/ */
public interface ICaseAiChatService { public interface ICaseAiChatService {
/**
* 初始化会话
*
* @return {@link String } 会话id
*/
String initChat(CurrentUser currentUser);
/** /**
* 聊天 * 聊天
* *

View File

@@ -1,5 +1,6 @@
package com.xboe.module.boecase.service.impl; package com.xboe.module.boecase.service.impl;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
@@ -112,6 +113,16 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
// 用于存储会话ID与EventSource的映射关系以便能够中断特定会话 // 用于存储会话ID与EventSource的映射关系以便能够中断特定会话
private final Map<String, EventSource> conversationEventSourceMap = new ConcurrentHashMap<>(); private final Map<String, EventSource> conversationEventSourceMap = new ConcurrentHashMap<>();
/**
* 初始化会话
*
* @return {@link String } 会话id
*/
@Override
public String initChat(CurrentUser currentUser) {
return getOrCreateConversationId(new CaseAiChatDto(), currentUser);
}
@Override @Override
@Transactional @Transactional
public SseEmitter chat(CaseAiChatDto caseAiChatDto, CurrentUser currentUser) { public SseEmitter chat(CaseAiChatDto caseAiChatDto, CurrentUser currentUser) {
@@ -385,7 +396,7 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
if (StringUtils.isEmpty(conversationId)) { if (StringUtils.isEmpty(conversationId)) {
// 新会话,调用创建会话接口 // 新会话,调用创建会话接口
String conversationName = "AI案例咨询-" + LocalDateTime.now().toString(); String conversationName = "AI案例咨询-" + LocalDateTime.now().toString();
CaseAiConversations newConversation = createNewConversation(currentUser.getCode(), conversationName); CaseAiConversations newConversation = SpringUtil.getBean(ICaseAiChatService.class).createNewConversation(currentUser.getCode(), conversationName);
return newConversation.getAiConversationId(); return newConversation.getAiConversationId();
} else { } else {
// 已存在会话,从数据库查询 // 已存在会话,从数据库查询