feat: sse对话样式优化

This commit is contained in:
ageerle
2025-04-29 11:44:47 +08:00
parent dc6d00f0fc
commit b1ff44df4b
3 changed files with 6 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import org.jetbrains.annotations.NotNull;
import org.ruoyi.chat.service.chat.IChatCostService;
import org.ruoyi.chat.util.SSEUtil;
import org.ruoyi.common.chat.entity.chat.ChatCompletionResponse;
import org.ruoyi.common.chat.request.ChatRequest;
import org.ruoyi.common.core.utils.SpringUtils;
@@ -84,10 +85,10 @@ public class SSEEventSourceListener extends EventSourceListener {
modelName = completionResponse.getModel();
}
stringBuffer.append(content);
emitter.send(content);
emitter.send(data);
}
} catch (Exception e) {
emitter.completeWithError(e);
log.error(e.getMessage(), e);
}
}

View File

@@ -11,6 +11,7 @@ import org.ruoyi.chat.enums.ChatModeType;
import org.ruoyi.chat.service.chat.IChatCostService;
import org.ruoyi.chat.service.chat.ISseService;
import org.ruoyi.chat.util.IpUtil;
import org.ruoyi.chat.util.SSEUtil;
import org.ruoyi.common.chat.config.LocalCache;
import org.ruoyi.common.chat.entity.Tts.TextToSpeech;
import org.ruoyi.common.chat.entity.chat.ChatCompletion;
@@ -88,7 +89,7 @@ public class SseServiceImpl implements ISseService {
switchModelAndHandle(chatRequest,sseEmitter);
} catch (Exception e) {
log.error(e.getMessage(),e);
sseEmitter.completeWithError(e);
SSEUtil.sendErrorEvent(sseEmitter,e.getMessage());
}
return sseEmitter;
}

View File

@@ -21,11 +21,8 @@ public class SSEUtil {
* @param errorMessage 错误信息
*/
public static void sendErrorEvent(ResponseBodyEmitter sseEmitter, String errorMessage) {
SseEmitter.SseEventBuilder event = SseEmitter.event()
.name("error")
.data(errorMessage);
try {
sseEmitter.send(event);
sseEmitter.send(errorMessage);
} catch (IOException e) {
log.error("SSE发送失败: {}", e.getMessage());
}