This commit is contained in:
2026-02-23 19:49:12 +08:00
parent 0c4cffe18e
commit c02f5d36c8
2 changed files with 27 additions and 4 deletions

View File

@@ -101,14 +101,36 @@ public class DifyClient {
try {
Map<String, Object> data = objectMapper.readValue(jsonStr, new TypeReference<>() {});
String eventType = (String) data.get("event");
String answer = (String) data.get("answer");
String conversationId = (String) data.get("conversation_id");
// 获取内容:优先 answer其次 text_messagechatflow 模式)
String answer = (String) data.get("answer");
if (answer == null) {
answer = (String) data.get("text_message");
}
log.debug("[parseSSEEvent] event={}, answer={}", eventType, answer);
return switch (eventType) {
case "message", "agent_message" -> DifyChatRespVO.message(answer, conversationId);
case "workflow_finished", "message_end" -> DifyChatRespVO.done(conversationId, null);
case "error" -> DifyChatRespVO.error(answer);
default -> null;
// chatflow 节点事件,尝试提取内容
case "node_finished" -> {
@SuppressWarnings("unchecked")
Map<String, Object> outputs = (Map<String, Object>) data.get("outputs");
if (outputs != null) {
String text = (String) outputs.get("text");
if (text != null) {
yield DifyChatRespVO.message(text, conversationId);
}
}
yield null;
}
default -> {
log.debug("[parseSSEEvent] 未处理的事件类型: {}", eventType);
yield null;
}
};
} catch (Exception e) {
log.warn("[parseSSEEvent] 解析 SSE 事件失败: {}", event, e);