优化
This commit is contained in:
@@ -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_message(chatflow 模式)
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user