From 368c30676e5859f0269a105728c3e1a87c8ae3a9 Mon Sep 17 00:00:00 2001 From: v-zhangjc9 Date: Wed, 18 Jun 2025 10:33:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai-web):=20=E5=B0=9D=E8=AF=95=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=AF=B9=E8=AF=9D=E8=BF=9E=E6=8E=A5=E7=9A=84=E7=A8=B3?= =?UTF-8?q?=E5=AE=9A=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/chat/ChatController.java | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/controller/chat/ChatController.java b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/controller/chat/ChatController.java index a45a28e..7c5abb1 100644 --- a/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/controller/chat/ChatController.java +++ b/service-ai/service-ai-web/src/main/java/com/lanyuanxiaoyao/service/ai/web/controller/chat/ChatController.java @@ -6,10 +6,13 @@ import com.lanyuanxiaoyao.service.ai.web.entity.vo.MessageVO; import com.lanyuanxiaoyao.service.ai.web.tools.ChartTool; import com.lanyuanxiaoyao.service.ai.web.tools.TableTool; import com.lanyuanxiaoyao.service.ai.web.tools.YarnTool; +import com.lanyuanxiaoyao.service.configuration.ExecutorProvider; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Optional; +import java.util.concurrent.TimeoutException; import org.eclipse.collections.api.list.ImmutableList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,24 +101,34 @@ public class ChatController { @PostMapping("async") public SseEmitter chatAsync( - @RequestBody ImmutableList messages + @RequestBody ImmutableList messages, + HttpServletResponse httpResponse ) { - SseEmitter emitter = new SseEmitter(); - buildRequest(messages) - .stream() - .chatResponse() - .subscribe( - response -> { - try { - emitter.send(toMessage(response)); - } catch (IOException e) { - emitter.completeWithError(e); - throw new RuntimeException(e); - } - }, - emitter::completeWithError, - emitter::complete - ); + httpResponse.setHeader("X-Accel-Buffering", "no"); + + SseEmitter emitter = new SseEmitter(20 * 1000L); + ExecutorProvider.EXECUTORS.submit(() -> { + buildRequest(messages) + .stream() + .chatResponse() + .subscribe( + response -> { + try { + emitter.send( + SseEmitter.event() + .data(toMessage(response)) + .reconnectTime(5000) + .build() + ); + } catch (IOException e) { + emitter.completeWithError(e); + } + }, + emitter::completeWithError, + emitter::complete + ); + }); + emitter.onTimeout(() -> emitter.completeWithError(new TimeoutException("SseEmitter Timeout"))); return emitter; }