mirror of
				https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
				synced 2025-11-04 08:13:43 +08:00 
			
		
		
		
	fix: request timeout errors
This commit is contained in:
		@@ -52,7 +52,7 @@ export async function requestChatStream(
 | 
				
			|||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const controller = new AbortController();
 | 
					  const controller = new AbortController();
 | 
				
			||||||
  setTimeout(() => controller.abort(), 10000);
 | 
					  const reqTimeoutId = setTimeout(() => controller.abort(), 10000);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    const res = await fetch("/api/chat-stream", {
 | 
					    const res = await fetch("/api/chat-stream", {
 | 
				
			||||||
@@ -63,10 +63,14 @@ export async function requestChatStream(
 | 
				
			|||||||
      body: JSON.stringify(req),
 | 
					      body: JSON.stringify(req),
 | 
				
			||||||
      signal: controller.signal,
 | 
					      signal: controller.signal,
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					    clearTimeout(reqTimeoutId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let responseText = "";
 | 
					    let responseText = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const finish = () => options?.onMessage(responseText, true);
 | 
					    const finish = () => {
 | 
				
			||||||
 | 
					      options?.onMessage(responseText, true);
 | 
				
			||||||
 | 
					      controller.abort();
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (res.ok) {
 | 
					    if (res.ok) {
 | 
				
			||||||
      const reader = res.body?.getReader();
 | 
					      const reader = res.body?.getReader();
 | 
				
			||||||
@@ -74,7 +78,9 @@ export async function requestChatStream(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      while (true) {
 | 
					      while (true) {
 | 
				
			||||||
        // handle time out, will stop if no response in 10 secs
 | 
					        // handle time out, will stop if no response in 10 secs
 | 
				
			||||||
 | 
					        const resTimeoutId = setTimeout(() => finish(), 10000);
 | 
				
			||||||
        const content = await reader?.read();
 | 
					        const content = await reader?.read();
 | 
				
			||||||
 | 
					        clearTimeout(resTimeoutId);
 | 
				
			||||||
        const text = decoder.decode(content?.value);
 | 
					        const text = decoder.decode(content?.value);
 | 
				
			||||||
        responseText += text;
 | 
					        responseText += text;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user