diff --git a/app/requests.ts b/app/requests.ts index 54c09e8d..6429cf31 100644 --- a/app/requests.ts +++ b/app/requests.ts @@ -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;