AI对话bug修复

This commit is contained in:
2025-12-23 23:04:32 +08:00
parent 1eb1a61b0b
commit 700438ca42
2 changed files with 155 additions and 24 deletions
@@ -80,12 +80,17 @@ public class AiChatServiceImpl implements AiChatService {
private AiConfigService aiConfigService;
private static final String DEFAULT_USER_ID = "emotion-museum-user";
// 使用场景常量
private static final String USAGE_SCENARIO_CHAT = "chat";
private static final String USAGE_SCENARIO_SUMMARY = "summary";
private static final String DEFAULT_ENVIRONMENT = "production";
/**
* Coze工作流配置键 - 聊天场景
*/
private static final String COZE_CHAT_CONFIG_KEY = "coze.chat.default";
// API 相关常量
private static final String CONTENT_KEY = "content";
private static final String ROLE_KEY = "role";
@@ -744,23 +749,98 @@ public class AiChatServiceImpl implements AiChatService {
/**
* 发送消息到Coze AI(带messageId
* 使用通用工作流调用方式,通过配置键获取AI配置
*
* @param conversationId 会话ID
* @param messageId 消息ID
* @param userMessage 用户消息
* @param userId 用户ID
* @return AI回复内容
*/
private String sendMessageWithMessageId(String conversationId, String messageId, String userMessage,
String userId) {
log.info("发送消息到Coze AI: conversationId={}, messageId={}, userId={}", conversationId, messageId, userId);
// 创建API调用记录(包含messageId
CozeApiCall apiCall = createApiCallRecord(conversationId, messageId, userMessage, userId, "chat");
// 1. 获取AI配置
AiConfig config = aiConfigService.getByConfigKey(COZE_CHAT_CONFIG_KEY);
if (config == null) {
log.error("未找到聊天场景的AI配置或配置已禁用: configKey={}", COZE_CHAT_CONFIG_KEY);
return "抱歉,AI服务暂时不可用,请稍后再试。";
}
// 2. 创建API调用记录(包含conversationId和messageId
CozeApiCall apiCall = createChatWorkflowApiCallRecord(config, conversationId, messageId, userMessage, userId);
try {
return executeCozeApiCall(apiCall, conversationId, userMessage, userId);
// 3. 构建工作流请求参数
Map<String, Object> parameters = new HashMap<>();
parameters.put("input", userMessage);
parameters.put("user_id", userId);
// 4. 构建工作流请求体
Map<String, Object> requestBody = buildWorkflowRequest(config, parameters, userId);
// 5. 执行工作流调用(带API调用记录)
return executeWorkflowCallWithRecord(config, requestBody, COZE_CHAT_CONFIG_KEY, userId, apiCall);
} catch (Exception e) {
log.error("发送消息失败", e);
log.error("发送消息失败: conversationId={}, messageId={}, error={}",
conversationId, messageId, e.getMessage(), e);
updateApiCallFailure(apiCall, e.getMessage());
return "抱歉,AI服务暂时不可用,请稍后再试。";
}
}
/**
* 创建聊天工作流API调用记录(包含conversationId和messageId
*
* @param config AI配置
* @param conversationId 会话ID
* @param messageId 消息ID
* @param userMessage 用户消息
* @param userId 用户ID
* @return API调用记录
*/
private CozeApiCall createChatWorkflowApiCallRecord(AiConfig config, String conversationId,
String messageId, String userMessage, String userId) {
CozeApiCall apiCall = CozeApiCall.builder()
.id(snowflakeIdGenerator.nextIdAsString())
.conversationId(conversationId)
.messageId(messageId)
.workflowId(config.getWorkflowId())
.botId(config.getBotId())
.userId(userId)
.requestType("workflow")
.userMessage(userMessage)
.userMessageType("text")
.status("pending")
.startTime(LocalDateTime.now())
.traceId(UUID.randomUUID().toString().replace("-", ""))
.metadata(JSON.toJSONString(Map.of("configKey", COZE_CHAT_CONFIG_KEY)))
.createBy(userId)
.updateBy(userId)
.build();
// 获取客户端信息
try {
HttpServletRequest request = getCurrentRequest();
if (request != null) {
apiCall.setClientIp(getClientIp(request));
apiCall.setUserAgent(request.getHeader("User-Agent"));
apiCall.setSessionId(request.getSession().getId());
}
} catch (Exception e) {
log.warn("获取客户端信息失败: {}", e.getMessage());
}
// 保存API调用记录
cozeApiCallService.save(apiCall);
log.info("创建聊天工作流API调用记录: id={}, conversationId={}, messageId={}, workflowId={}, traceId={}",
apiCall.getId(), conversationId, messageId, config.getWorkflowId(), apiCall.getTraceId());
return apiCall;
}
/**
* 执行Coze API调用的公共逻辑
*/