fix(chat): avoid idle runtime notice injection

This commit is contained in:
JOJO 2026-05-25 22:57:29 +08:00
parent 1e83bc9f68
commit b03d158ab6

View File

@ -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