fix: 报错时也记录

This commit is contained in:
liu.zixi
2025-11-25 14:03:53 +08:00
committed by joshen
parent 2191db1c95
commit e513b08205

View File

@@ -132,6 +132,13 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
// 3. 构建请求参数
String userId = currentUser.getCode();
// 6. 用于收集对话数据的容器
AiChatConversationData conversationData = new AiChatConversationData();
conversationData.setQuery(caseAiChatDto.getQuery());
conversationData.setConversationId(conversationId);
conversationData.setUserId(userId);
String kId = caseAiProperties.getCaseKnowledgeId();
JSONObject chatParam = new JSONObject();
chatParam.put("userId", userId);
@@ -162,9 +169,18 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
String accessToken;
try {
accessToken = aiAccessTokenService.getAccessToken();
if (org.apache.commons.lang3.StringUtils.isBlank(accessToken)) {
errMessage(sseEmitter, SYS_ERR_MSG);
sseEmitter.complete();
conversationData.appendAnswer(SYS_ERR_MSG);
elasticSearchIndexService.createData(conversationData);
return sseEmitter;
}
} catch (Exception e) {
log.error("获取access_token失败", e);
errMessage(sseEmitter, SYS_ERR_MSG);
conversationData.appendAnswer(SYS_ERR_MSG);
elasticSearchIndexService.createData(conversationData);
sseEmitter.complete();
return sseEmitter;
}
@@ -176,13 +192,6 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
RequestBody bodyRequestBody = RequestBody.create(chatParamStr, MediaType.parse("application/json"));
builder.post(bodyRequestBody);
Request request = builder.build();
// 6. 用于收集对话数据的容器
AiChatConversationData conversationData = new AiChatConversationData();
conversationData.setQuery(caseAiChatDto.getQuery());
conversationData.setConversationId(conversationId);
conversationData.setUserId(userId);
// 7. 创建事件监听器
EventSourceListener listener = new EventSourceListener() {
@@ -271,6 +280,7 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
conversationEventSourceMap.remove(conversationId);
// 即使失败也要将已有的对话数据保存到ES
conversationData.appendAnswer(SYS_ERR_MSG);
elasticSearchIndexService.createData(conversationData);
return;
}
@@ -282,16 +292,17 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
// 从Map中移除失败的会话
conversationEventSourceMap.remove(conversationId);
// 即使失败也要将已有的对话数据保存到ES
conversationData.appendAnswer(SYS_ERR_MSG);
elasticSearchIndexService.createData(conversationData);
return;
}
sseEmitter.completeWithError(e);
// 从Map中移除失败的会话
conversationEventSourceMap.remove(conversationId);
// 即使失败也要将已有的对话数据保存到ES
elasticSearchIndexService.createData(conversationData);
sseEmitter.completeWithError(e);
}
};