feat: 优化
This commit is contained in:
@@ -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<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");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user