Browse Source

fix: 修复循环查询的bug

main
tanxing 5 days ago
parent
commit
b3490f8bbd
  1. 21
      deepsearcher/backend/templates/index.html
  2. 12
      main.py

21
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;

12
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
# 尝试从队列获取消息,设置超时

Loading…
Cancel
Save