From 0c4cffe18e53a575ab00c8848d710afeb53525c0 Mon Sep 17 00:00:00 2001 From: shenaowei <450702724@qq.com> Date: Mon, 23 Feb 2026 17:46:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/tik/dify/client/DifyClient.java | 51 +++++-------------- 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/dify/client/DifyClient.java b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/dify/client/DifyClient.java index ba230e0bd2..542e5e59d1 100644 --- a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/dify/client/DifyClient.java +++ b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/dify/client/DifyClient.java @@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.tik.dify.client; import cn.iocoder.yudao.module.tik.dify.config.DifyProperties; import cn.iocoder.yudao.module.tik.dify.vo.DifyChatRespVO; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; @@ -26,6 +28,9 @@ public class DifyClient { @Resource private DifyProperties difyProperties; + @Resource + private ObjectMapper objectMapper; + /** * 调用 Dify 聊天流式 API * @@ -83,19 +88,21 @@ public class DifyClient { * 解析 SSE 事件 */ private DifyChatRespVO parseSSEEvent(String event) { - if (event == null || !event.startsWith("data:")) { + if (event == null || event.isEmpty()) { return null; } - String jsonStr = event.substring(5).trim(); + // 移除 data: 前缀(如果有) + String jsonStr = event.startsWith("data:") ? event.substring(5).trim() : event.trim(); if (jsonStr.isEmpty() || "[DONE]".equals(jsonStr)) { return null; } try { - String eventType = extractJsonValue(jsonStr, "event"); - String answer = extractJsonValue(jsonStr, "answer"); - String conversationId = extractJsonValue(jsonStr, "conversation_id"); + Map 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"); return switch (eventType) { case "message", "agent_message" -> DifyChatRespVO.message(answer, conversationId); @@ -109,38 +116,4 @@ public class DifyClient { } } - /** - * 简单提取 JSON 值(支持字符串和非字符串格式) - */ - private String extractJsonValue(String json, String key) { - // 查找 "key": 后的位置,兼容带空格和不带空格的格式 - int keyStart = json.indexOf("\"" + key + "\":"); - if (keyStart == -1) { - return null; - } - - int valueStart = keyStart + key.length() + 3; // 跳过 "key": - // 跳过空白字符 - while (valueStart < json.length() && Character.isWhitespace(json.charAt(valueStart))) { - valueStart++; - } - if (valueStart >= json.length()) { - return null; - } - - char firstChar = json.charAt(valueStart); - // 字符串值 "value" - if (firstChar == '"') { - int end = json.indexOf('"', valueStart + 1); - return end != -1 ? json.substring(valueStart + 1, end) : null; - } - - // 非字符串值(数字、布尔等) - int end = json.indexOf(',', valueStart); - if (end == -1) { - end = json.indexOf('}', valueStart); - } - return end != -1 ? json.substring(valueStart, end).trim() : null; - } - }