From b03d158ab659db464a78d64fac23351b26326dbc Mon Sep 17 00:00:00 2001 From: JOJO <1498581755@qq.com> Date: Mon, 25 May 2026 22:57:29 +0800 Subject: [PATCH] fix(chat): avoid idle runtime notice injection --- server/chat.py | 48 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/server/chat.py b/server/chat.py index eeef886..0d0256c 100644 --- a/server/chat.py +++ b/server/chat.py @@ -54,7 +54,7 @@ def _dispatch_runtime_mode_notice(terminal: WebTerminal, username: str, text: st if not notice: return - # 若有运行任务,优先入队到该任务(下一轮工具后注入),确保会进入后续 API 请求上下文。 + # 仅在“有运行中的任务”时注入 [系统通知|notify],空闲期切换不写入 user 消息。 try: from .tasks import task_manager @@ -64,39 +64,21 @@ def _dispatch_runtime_mode_notice(terminal: WebTerminal, username: str, text: st ] if current_conv: running_tasks.sort(key=lambda r: 0 if r.conversation_id == current_conv else 1) - if running_tasks: - target = running_tasks[0] - result = task_manager.enqueue_runtime_guidance( - username=username, - task_id=target.task_id, - message=notice, - source="notify", - ) - if result.get("success"): - return - except Exception: - pass + if not running_tasks: + debug_log(f"[RuntimeMode] idle switch ignored notify injection: {notice}") + return - # 无运行任务时,直接写入当前会话并广播前端显示(来源固定为 notify)。 - from .chat_flow_task_support import inject_runtime_user_message - - conversation_id = getattr(getattr(terminal, "context_manager", None), "current_conversation_id", None) - - def _emit(event: str, payload: Dict[str, Any]) -> None: - try: - socketio.emit(event, payload, room=f"user_{username}") - except Exception: - pass - - inject_runtime_user_message( - web_terminal=terminal, - messages=None, - text=notice, - source="notify", - sender=_emit, - conversation_id=conversation_id, - inline=True, - ) + target = running_tasks[0] + result = task_manager.enqueue_runtime_guidance( + username=username, + task_id=target.task_id, + message=notice, + source="notify", + ) + if not result.get("success"): + debug_log(f"[RuntimeMode] enqueue runtime guidance failed: {result}") + except Exception as exc: + debug_log(f"[RuntimeMode] dispatch notice failed: {exc}") @chat_bp.route('/api/thinking-mode', methods=['POST']) @api_login_required