From 37004ecfb3b25d49eefac1da36bd2e2810703444 Mon Sep 17 00:00:00 2001 From: balibabu Date: Thu, 23 Oct 2025 16:17:28 +0800 Subject: [PATCH] Fix: Clicking "Stop receiving messages" in Firefox will cause the page to crash. #10752 (#10754) ### What problem does this PR solve? Fix: Clicking "Stop receiving messages" in Firefox will cause the page to crash. #10752 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- web/src/hooks/logic-hooks.ts | 43 +++++++++------- web/src/hooks/use-send-message.ts | 49 +++++++++++-------- .../agent/chat/use-send-agent-message.ts | 4 +- .../pages/next-chats/hooks/use-upload-file.ts | 2 +- 4 files changed, 56 insertions(+), 42 deletions(-) diff --git a/web/src/hooks/logic-hooks.ts b/web/src/hooks/logic-hooks.ts index f72354758..d7afe6347 100644 --- a/web/src/hooks/logic-hooks.ts +++ b/web/src/hooks/logic-hooks.ts @@ -257,25 +257,32 @@ export const useSendMessageWithSse = ( .getReader(); while (true) { - const x = await reader?.read(); - if (x) { - const { done, value } = x; - if (done) { - resetAnswer(); - break; - } - try { - const val = JSON.parse(value?.data || ''); - const d = val?.data; - if (typeof d !== 'boolean') { - setAnswer({ - ...d, - conversationId: body?.conversation_id, - chatBoxId: body.chatBoxId, - }); + try { + const x = await reader?.read(); + if (x) { + const { done, value } = x; + if (done) { + resetAnswer(); + break; } - } catch (e) { - // Swallow parse errors silently + try { + const val = JSON.parse(value?.data || ''); + const d = val?.data; + if (typeof d !== 'boolean') { + setAnswer({ + ...d, + conversationId: body?.conversation_id, + chatBoxId: body.chatBoxId, + }); + } + } catch (e) { + // Swallow parse errors silently + } + } + } catch (e) { + if (e instanceof DOMException && e.name === 'AbortError') { + console.log('Request was aborted by user or logic.'); + break; } } } diff --git a/web/src/hooks/use-send-message.ts b/web/src/hooks/use-send-message.ts index ecf961fdd..8d602f2e0 100644 --- a/web/src/hooks/use-send-message.ts +++ b/web/src/hooks/use-send-message.ts @@ -126,29 +126,36 @@ export const useSendMessageBySSE = (url: string = api.completeConversation) => { .getReader(); while (true) { - const x = await reader?.read(); - if (x) { - const { done, value } = x; - if (done) { - console.info('done'); - resetAnswerList(); - break; - } - try { - const val = JSON.parse(value?.data || ''); - - console.info('data:', val); - if (val.code === 500) { - message.error(val.message); + try { + const x = await reader?.read(); + if (x) { + const { done, value } = x; + if (done) { + console.info('done'); + resetAnswerList(); + break; } + try { + const val = JSON.parse(value?.data || ''); - setAnswerList((list) => { - const nextList = [...list]; - nextList.push(val); - return nextList; - }); - } catch (e) { - console.warn(e); + console.info('data:', val); + if (val.code === 500) { + message.error(val.message); + } + + setAnswerList((list) => { + const nextList = [...list]; + nextList.push(val); + return nextList; + }); + } catch (e) { + console.warn(e); + } + } + } catch (e) { + if (e instanceof DOMException && e.name === 'AbortError') { + console.log('Request was aborted by user or logic.'); + break; } } } diff --git a/web/src/pages/agent/chat/use-send-agent-message.ts b/web/src/pages/agent/chat/use-send-agent-message.ts index 7b20e7c6d..c822819f6 100644 --- a/web/src/pages/agent/chat/use-send-agent-message.ts +++ b/web/src/pages/agent/chat/use-send-agent-message.ts @@ -382,9 +382,9 @@ export const useSendAgentMessage = ({ const { content, id } = findMessageFromList(answerList); const inputAnswer = findInputFromList(answerList); const answer = content || getLatestError(answerList); - if (answerList.length > 0 && answer) { + if (answerList.length > 0) { addNewestOneAnswer({ - answer: answer, + answer: answer ?? '', id: id, ...inputAnswer, }); diff --git a/web/src/pages/next-chats/hooks/use-upload-file.ts b/web/src/pages/next-chats/hooks/use-upload-file.ts index 164366f0f..66eaef689 100644 --- a/web/src/pages/next-chats/hooks/use-upload-file.ts +++ b/web/src/pages/next-chats/hooks/use-upload-file.ts @@ -20,7 +20,7 @@ export function useUploadFile() { if (Array.isArray(files) && files.length) { const file = files[0]; const ret = await uploadAndParseFile({ file, options, conversationId }); - if (ret.code === 0 && Array.isArray(ret.data)) { + if (ret?.code === 0 && Array.isArray(ret?.data)) { setFileIds((list) => [...list, ...ret.data]); setFileMap((map) => { map.set(files[0], ret.data[0]);