From 3b57bfc85cd47ae7e69c1c537afe478ff74a83ba Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Sun, 5 Oct 2025 18:25:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(proactive=5Fthinker):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=B8=BB=E5=8A=A8=E6=80=9D=E8=80=83=E7=9A=84=E5=86=B7?= =?UTF-8?q?=E5=8D=B4=E5=88=A4=E6=96=AD=E5=92=8C=E4=B8=8A=E4=B8=8B=E6=96=87?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主动思考插件的冷却时间判断逻辑已从依赖 `stream.last_active_time` 切换为获取最新的消息时间。这使得冷却判断更加准确,避免了因流未及时更新而导致的不准确。 同时,优化了上下文获取逻辑: - 获取最近聊天记录时,增加了12小时的时间限制。 - 获取历史主动决策记录时,改为获取过去24小时内的动作记录,以提供更相关的上下文。 --- .../proactive_thinker/proacive_thinker_event.py | 10 +++++++--- .../proactive_thinker_executor.py | 15 +++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/plugins/built_in/proactive_thinker/proacive_thinker_event.py b/src/plugins/built_in/proactive_thinker/proacive_thinker_event.py index 1899361c4..f7e1fb2d7 100644 --- a/src/plugins/built_in/proactive_thinker/proacive_thinker_event.py +++ b/src/plugins/built_in/proactive_thinker/proacive_thinker_event.py @@ -11,7 +11,7 @@ from src.common.logger import get_logger from src.config.config import global_config from src.manager.async_task_manager import AsyncTask, async_task_manager from src.plugin_system import BaseEventHandler, EventType -from src.plugin_system.apis import chat_api, person_api +from src.plugin_system.apis import chat_api, message_api, person_api from src.plugin_system.base.base_event import HandlerResult from .proactive_thinker_executor import ProactiveThinkerExecutor @@ -160,7 +160,9 @@ class ProactiveThinkingTask(AsyncTask): continue # 检查冷却时间 - time_since_last_active = time.time() - stream.last_active_time + recent_messages = await message_api.get_recent_messages(chat_id=stream.stream_id, limit=1,limit_mode="latest") + last_message_time = recent_messages[0]["time"] if recent_messages else stream.create_time + time_since_last_active = time.time() - last_message_time if time_since_last_active > next_interval: logger.info( f"【日常唤醒-私聊】聊天流 {stream.stream_id} 已冷却 {time_since_last_active:.2f} 秒,触发主动对话。" @@ -184,7 +186,9 @@ class ProactiveThinkingTask(AsyncTask): # 检查群聊是否在白名单内 if not enabled_groups or f"qq:{stream.group_info.group_id}" in enabled_groups: # 检查冷却时间 - time_since_last_active = time.time() - stream.last_active_time + recent_messages = await message_api.get_recent_messages(chat_id=stream.stream_id, limit=1) + last_message_time = recent_messages[0]["time"] if recent_messages else stream.create_time + time_since_last_active = time.time() - last_message_time if time_since_last_active > next_interval: logger.info( f"【日常唤醒-群聊】聊天流 {stream.stream_id} 已冷却 {time_since_last_active:.2f} 秒,触发主动对话。" diff --git a/src/plugins/built_in/proactive_thinker/proactive_thinker_executor.py b/src/plugins/built_in/proactive_thinker/proactive_thinker_executor.py index e651ecfd5..692b6e106 100644 --- a/src/plugins/built_in/proactive_thinker/proactive_thinker_executor.py +++ b/src/plugins/built_in/proactive_thinker/proactive_thinker_executor.py @@ -1,8 +1,10 @@ +import time from datetime import datetime from typing import Any import orjson +from src.chat.utils.chat_message_builder import get_actions_by_timestamp_with_chat from src.common.logger import get_logger from src.config.config import global_config, model_config from src.mood.mood_manager import mood_manager @@ -140,17 +142,18 @@ class ProactiveThinkerExecutor: else "今天没有日程安排。" ) - recent_messages = await message_api.get_recent_messages(stream.stream_id) + recent_messages = await message_api.get_recent_messages(stream.stream_id,limit=50,limit_mode="latest",hours=12) recent_chat_history = ( await message_api.build_readable_messages_to_str(recent_messages) if recent_messages else "无" ) - action_history_list = await database_api.db_query( - database_api.MODEL_MAPPING["ActionRecords"], - filters={"chat_id": stream_id, "action_name": "proactive_decision"}, - limit=3, - order_by=["-time"], + action_history_list = await get_actions_by_timestamp_with_chat( + chat_id=stream.stream_id, + timestamp_start=time.time() - 3600 * 24, #过去24小时 + timestamp_end=time.time(), + limit=7, ) + action_history_context = ( "\n".join([f"- {a['action_data']}" for a in action_history_list if isinstance(a, dict)]) if isinstance(action_history_list, list)