fix: request timeout errors

This commit is contained in:
Yidadaa 2023-03-14 00:40:04 +08:00
parent 8e4fc83d3b
commit e2a4b3d45c

View File

@ -52,7 +52,7 @@ export async function requestChatStream(
});
const controller = new AbortController();
setTimeout(() => controller.abort(), 10000);
const reqTimeoutId = setTimeout(() => controller.abort(), 10000);
try {
const res = await fetch("/api/chat-stream", {
@ -63,10 +63,14 @@ export async function requestChatStream(
body: JSON.stringify(req),
signal: controller.signal,
});
clearTimeout(reqTimeoutId);
let responseText = "";
const finish = () => options?.onMessage(responseText, true);
const finish = () => {
options?.onMessage(responseText, true);
controller.abort();
};
if (res.ok) {
const reader = res.body?.getReader();
@ -74,7 +78,9 @@ export async function requestChatStream(
while (true) {
// handle time out, will stop if no response in 10 secs
const resTimeoutId = setTimeout(() => finish(), 10000);
const content = await reader?.read();
clearTimeout(resTimeoutId);
const text = decoder.decode(content?.value);
responseText += text;