From 9c7321014384b2f00a788dc2def60e6e885eae9e Mon Sep 17 00:00:00 2001 From: qixinbo Date: Thu, 19 Mar 2026 15:33:12 +0800 Subject: [PATCH] fix: chart viz status updated --- backend/main.py | 32 ++++++++++++++++++----- frontend/src/components/ChatInterface.tsx | 6 ++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/backend/main.py b/backend/main.py index f9efc89..b7ec825 100644 --- a/backend/main.py +++ b/backend/main.py @@ -197,14 +197,24 @@ async def nanobot_chat_stream(request: ChatRequest): ) text = "" - viz_sent = False + last_viz_hash = None while True: # Check for viz payload during processing viz_payload = current_viz_data.get() - if viz_payload and not viz_sent: - yield f"data: {json.dumps({'type': 'viz', **viz_payload}, ensure_ascii=False)}\n\n" - viz_sent = True + if viz_payload: + try: + # Only hash sql and chart to avoid dumping large result arrays every 0.2s + current_hash = hash(( + viz_payload.get("sql"), + viz_payload.get("error"), + json.dumps(viz_payload.get("chart"), sort_keys=True) + )) + if current_hash != last_viz_hash: + yield f"data: {json.dumps({'type': 'viz', **viz_payload}, ensure_ascii=False)}\n\n" + last_viz_hash = current_hash + except Exception as e: + print(f"Error checking viz_payload: {e}") if current_task.done() and progress_queue.empty(): break @@ -219,8 +229,18 @@ async def nanobot_chat_stream(request: ChatRequest): # Check again for viz payload after task completes if not sent yet viz_payload = current_viz_data.get() - if viz_payload and not viz_sent: - yield f"data: {json.dumps({'type': 'viz', **viz_payload}, ensure_ascii=False)}\n\n" + if viz_payload: + try: + current_hash = hash(( + viz_payload.get("sql"), + viz_payload.get("error"), + json.dumps(viz_payload.get("chart"), sort_keys=True) + )) + if current_hash != last_viz_hash: + yield f"data: {json.dumps({'type': 'viz', **viz_payload}, ensure_ascii=False)}\n\n" + last_viz_hash = current_hash + except Exception as e: + pass # Persist viz payload to session if viz_payload and nanobot_service.agent: diff --git a/frontend/src/components/ChatInterface.tsx b/frontend/src/components/ChatInterface.tsx index 17ae049..9821b46 100644 --- a/frontend/src/components/ChatInterface.tsx +++ b/frontend/src/components/ChatInterface.tsx @@ -648,7 +648,11 @@ export function ChatInterface() { } if (payload.type === "viz") { - pushProgressLog("可视化结果已生成"); + if (payload.chart?.chart_spec) { + pushProgressLog("图表生成完成"); + } else if (payload.sql) { + pushProgressLog("数据查询完成"); + } streamedViz = buildMessageViz(payload); flushAssistant(true); // 立即把 viz 状态刷入 messages }