瀏覽代碼

fix: request timeout errors

Yidadaa 2 年之前
父節點
當前提交
e2a4b3d45c
共有 1 個文件被更改,包括 8 次插入2 次删除
  1. 8 2
      app/requests.ts

+ 8 - 2
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;