diff --git a/src/chat/memory_system/memory_system.py b/src/chat/memory_system/memory_system.py index a0c08c56e..e2fd710e8 100644 --- a/src/chat/memory_system/memory_system.py +++ b/src/chat/memory_system/memory_system.py @@ -1111,7 +1111,6 @@ class MemorySystem: from src.chat.message_receive.chat_stream import get_chat_manager chat_manager = get_chat_manager() - # ChatManager.get_stream 是异步方法,需要 await,否则会产生 "coroutine was never awaited" 警告 chat_stream = await chat_manager.get_stream(stream_id) if chat_stream and hasattr(chat_stream, "context_manager"): history_limit = self._determine_history_limit(context) diff --git a/src/chat/planner_actions/action_manager.py b/src/chat/planner_actions/action_manager.py index 054acce63..13eebb548 100644 --- a/src/chat/planner_actions/action_manager.py +++ b/src/chat/planner_actions/action_manager.py @@ -615,6 +615,7 @@ class ChatterActionManager: """禁用批量存储模式""" self._batch_storage_enabled = False self._current_chat_id = None + self._pending_actions = [] # 清空队列 logger.debug("已禁用批量存储模式") def add_action_to_batch(self, action_name: str, action_data: dict, thinking_id: str = "", diff --git a/src/chat/replyer/replyer_manager.py b/src/chat/replyer/replyer_manager.py index 3e11c8a2f..4f3f4f428 100644 --- a/src/chat/replyer/replyer_manager.py +++ b/src/chat/replyer/replyer_manager.py @@ -37,7 +37,6 @@ class ReplyerManager: target_stream = chat_stream if not target_stream: if chat_manager := get_chat_manager(): - # get_stream 为异步,需要等待 target_stream = await chat_manager.get_stream(stream_id) if not target_stream: diff --git a/src/plugin_system/apis/generator_api.py b/src/plugin_system/apis/generator_api.py index c3eefe6ca..803a2d739 100644 --- a/src/plugin_system/apis/generator_api.py +++ b/src/plugin_system/apis/generator_api.py @@ -9,17 +9,18 @@ """ import traceback -from typing import Any +from typing import TYPE_CHECKING, Any from rich.traceback import install from src.chat.message_receive.chat_stream import ChatStream -from src.chat.replyer.default_generator import DefaultReplyer -from src.chat.replyer.replyer_manager import replyer_manager from src.chat.utils.utils import process_llm_response from src.common.logger import get_logger from src.plugin_system.base.component_types import ActionInfo +if TYPE_CHECKING: + from src.chat.replyer.default_generator import DefaultReplyer + install(extra_lines=3) # 日志记录器 @@ -35,7 +36,7 @@ async def get_replyer( chat_stream: ChatStream | None = None, chat_id: str | None = None, request_type: str = "replyer", -) -> DefaultReplyer | None: +) -> Any | None: """获取回复器对象 优先使用chat_stream,如果没有则使用chat_id直接查找。 @@ -56,6 +57,8 @@ async def get_replyer( raise ValueError("chat_stream 和 chat_id 不可均为空") try: logger.debug(f"[GeneratorAPI] 正在获取回复器,chat_id: {chat_id}, chat_stream: {'有' if chat_stream else '无'}") + # 动态导入避免循环依赖 + from src.chat.replyer.replyer_manager import replyer_manager return await replyer_manager.get_replyer( chat_stream=chat_stream, chat_id=chat_id, diff --git a/src/plugins/built_in/affinity_flow_chatter/plan_executor.py b/src/plugins/built_in/affinity_flow_chatter/plan_executor.py index 10d44b1a8..91ea6ccc7 100644 --- a/src/plugins/built_in/affinity_flow_chatter/plan_executor.py +++ b/src/plugins/built_in/affinity_flow_chatter/plan_executor.py @@ -441,7 +441,7 @@ class ChatterPlanExecutor: # 通过 chat_id 获取真实的 chat_stream 对象 from src.plugin_system.apis.chat_api import get_chat_manager chat_manager = get_chat_manager() - chat_stream = chat_manager.get_stream(plan.chat_id) + chat_stream = await chat_manager.get_stream(plan.chat_id) if chat_stream: # 调用 action_manager 的批量存储 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..d9fb6d2a2 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 build_readable_actions, 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,23 +142,20 @@ 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_context = ( - "\n".join([f"- {a['action_data']}" for a in action_history_list if isinstance(a, dict)]) - if isinstance(action_history_list, list) - else "无" + 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 = build_readable_actions(actions=action_history_list) + # 2. 构建基础上下文 mood_state = "暂时没有" if global_config.mood.enable_mood: