feat(ai-web): 尝试优化对话连接的稳定性

This commit is contained in:
v-zhangjc9
2025-06-18 10:33:44 +08:00
parent 60477f99f5
commit 368c30676e

View File

@@ -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.ChartTool;
import com.lanyuanxiaoyao.service.ai.web.tools.TableTool; import com.lanyuanxiaoyao.service.ai.web.tools.TableTool;
import com.lanyuanxiaoyao.service.ai.web.tools.YarnTool; 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.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.TimeoutException;
import org.eclipse.collections.api.list.ImmutableList; import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -98,24 +101,34 @@ public class ChatController {
@PostMapping("async") @PostMapping("async")
public SseEmitter chatAsync( public SseEmitter chatAsync(
@RequestBody ImmutableList<MessageVO> messages @RequestBody ImmutableList<MessageVO> messages,
HttpServletResponse httpResponse
) { ) {
SseEmitter emitter = new SseEmitter(); httpResponse.setHeader("X-Accel-Buffering", "no");
SseEmitter emitter = new SseEmitter(20 * 1000L);
ExecutorProvider.EXECUTORS.submit(() -> {
buildRequest(messages) buildRequest(messages)
.stream() .stream()
.chatResponse() .chatResponse()
.subscribe( .subscribe(
response -> { response -> {
try { try {
emitter.send(toMessage(response)); emitter.send(
SseEmitter.event()
.data(toMessage(response))
.reconnectTime(5000)
.build()
);
} catch (IOException e) { } catch (IOException e) {
emitter.completeWithError(e); emitter.completeWithError(e);
throw new RuntimeException(e);
} }
}, },
emitter::completeWithError, emitter::completeWithError,
emitter::complete emitter::complete
); );
});
emitter.onTimeout(() -> emitter.completeWithError(new TimeoutException("SseEmitter Timeout")));
return emitter; return emitter;
} }