feat(ai-web): 尝试优化对话连接的稳定性
This commit is contained in:
@@ -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");
|
||||||
buildRequest(messages)
|
|
||||||
.stream()
|
SseEmitter emitter = new SseEmitter(20 * 1000L);
|
||||||
.chatResponse()
|
ExecutorProvider.EXECUTORS.submit(() -> {
|
||||||
.subscribe(
|
buildRequest(messages)
|
||||||
response -> {
|
.stream()
|
||||||
try {
|
.chatResponse()
|
||||||
emitter.send(toMessage(response));
|
.subscribe(
|
||||||
} catch (IOException e) {
|
response -> {
|
||||||
emitter.completeWithError(e);
|
try {
|
||||||
throw new RuntimeException(e);
|
emitter.send(
|
||||||
}
|
SseEmitter.event()
|
||||||
},
|
.data(toMessage(response))
|
||||||
emitter::completeWithError,
|
.reconnectTime(5000)
|
||||||
emitter::complete
|
.build()
|
||||||
);
|
);
|
||||||
|
} catch (IOException e) {
|
||||||
|
emitter.completeWithError(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
emitter::completeWithError,
|
||||||
|
emitter::complete
|
||||||
|
);
|
||||||
|
});
|
||||||
|
emitter.onTimeout(() -> emitter.completeWithError(new TimeoutException("SseEmitter Timeout")));
|
||||||
return emitter;
|
return emitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user