From b3490f8bbd5f4f3cb6c1fc20c69568186d2f7934 Mon Sep 17 00:00:00 2001 From: tanxing Date: Wed, 13 Aug 2025 14:04:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deepsearcher/backend/templates/index.html | 21 +++++++++++++++++++-- main.py | 12 ++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/deepsearcher/backend/templates/index.html b/deepsearcher/backend/templates/index.html index 36bfdd7..607d467 100644 --- a/deepsearcher/backend/templates/index.html +++ b/deepsearcher/backend/templates/index.html @@ -449,9 +449,15 @@ // 工具函数:关闭EventSource连接 function closeEventSource() { if (eventSource) { + console.log('Closing eventSource in closeEventSource function'); eventSource.close(); eventSource = null; } + if (window.currentEventSource) { + console.log('Closing currentEventSource in closeEventSource function'); + window.currentEventSource.close(); + window.currentEventSource = null; + } isStreaming = false; } @@ -472,15 +478,17 @@ showStatus('queryStatus', '查询已开始,正在处理...', 'loading'); break; case 'complete': - console.log('Query completed'); + console.log('Query completed - closing connection'); showStatus('queryStatus', '查询完成', 'success'); // 关闭EventSource连接 if (window.currentEventSource) { + console.log('Closing currentEventSource'); window.currentEventSource.close(); window.currentEventSource = null; } isStreaming = false; setButtonLoading(document.getElementById('queryBtn'), false); + console.log('Query completed - connection closed, isStreaming set to false'); break; case 'query_error': console.error('Query error:', message.error); @@ -680,6 +688,7 @@ } if (isStreaming) { + console.log('Query already in progress, isStreaming:', isStreaming); showStatus('queryStatus', '查询正在进行中,请等待完成', 'error'); return; } @@ -693,8 +702,16 @@ container.innerHTML = ''; try { + console.log('Starting new query, setting isStreaming to true'); isStreaming = true; + // 确保没有其他连接存在 + if (window.currentEventSource) { + console.log('Closing existing EventSource connection'); + window.currentEventSource.close(); + window.currentEventSource = null; + } + // 使用EventSource直接连接到查询流 const eventSource = new EventSource(`/query-stream/?original_query=${encodeURIComponent(queryText)}&max_iter=${maxIter}`); @@ -714,7 +731,7 @@ eventSource.onerror = function(event) { console.error('EventSource error:', event); if (eventSource.readyState === EventSource.CLOSED) { - console.log('EventSource connection closed'); + console.log('EventSource connection closed due to error'); isStreaming = false; setButtonLoading(button, false); window.currentEventSource = null; diff --git a/main.py b/main.py index 7b648a4..e276550 100644 --- a/main.py +++ b/main.py @@ -291,12 +291,12 @@ async def perform_query_stream( # 在后台线程中执行查询 def run_query(): try: + print(f"Starting query: {original_query} with max_iter: {max_iter}") result_text, _ = query(original_query, max_iter) - # 查询完成后发送complete消息 - from deepsearcher.utils.message_stream import send_complete - send_complete() + print(f"Query completed with result length: {len(result_text) if result_text else 0}") return result_text, None except Exception as e: + print(f"Query failed with error: {e}") return None, str(e) # 使用线程池执行查询 @@ -311,9 +311,13 @@ async def perform_query_stream( if query_future.done(): result_text, error = query_future.result() if error: + print(f"Query error: {error}") yield f"data: {json.dumps({'type': 'query_error', 'error': error}, ensure_ascii=False)}\n\n" return - # 成功完成时,complete消息会通过消息流自动发送 + # 成功完成时,发送complete消息并结束 + print("Query completed successfully, sending complete message") + yield f"data: {json.dumps({'type': 'complete'}, ensure_ascii=False)}\n\n" + print("Complete message sent, ending stream") return # 尝试从队列获取消息,设置超时