mirror of https://github.com/Mai-with-u/MaiBot.git
增加计数
parent
2bc9eece98
commit
299412f2a4
|
|
@ -6,7 +6,7 @@ from typing import Dict, Any, Optional
|
|||
from src.common.logger_manager import get_logger
|
||||
from maim_message import UserInfo
|
||||
from src.plugins.chat.chat_stream import chat_manager, ChatStream
|
||||
from ..chat.message import Message # 假设 Message 类型被 _convert_to_message 使用
|
||||
from ..chat.message import Message # 假设 Message 类型被 _convert_to_message 使用
|
||||
from src.config.config import global_config
|
||||
from ..person_info.person_info import person_info_manager
|
||||
from ..person_info.relationship_manager import relationship_manager
|
||||
|
|
@ -28,8 +28,7 @@ from .PFC_idle.idle_chat import IdleChat
|
|||
from .waiter import Waiter
|
||||
from .reply_checker import ReplyChecker
|
||||
|
||||
# >>> 新增导入 <<<
|
||||
from .conversation_loop import run_conversation_loop # 导入新的循环函数
|
||||
from .conversation_loop import run_conversation_loop
|
||||
|
||||
from rich.traceback import install
|
||||
|
||||
|
|
@ -37,13 +36,6 @@ install(extra_lines=3)
|
|||
|
||||
logger = get_logger("pfc_conversation")
|
||||
|
||||
# 时区配置移到 loop 文件或更全局的位置,这里不再需要
|
||||
# configured_tz = getattr(global_config, 'TIME_ZONE', 'Asia/Shanghai')
|
||||
# TIME_ZONE = tz.gettz(configured_tz)
|
||||
# if TIME_ZONE is None:
|
||||
# logger.error(f"配置的时区 '{configured_tz}' 无效,将使用默认时区 'Asia/Shanghai'")
|
||||
# TIME_ZONE = tz.gettz('Asia/Shanghai')
|
||||
|
||||
|
||||
class Conversation:
|
||||
"""
|
||||
|
|
@ -58,7 +50,7 @@ class Conversation:
|
|||
self.stream_id: str = stream_id
|
||||
self.private_name: str = private_name
|
||||
self.state: ConversationState = ConversationState.INIT
|
||||
self.should_continue: bool = False # Manager 会在初始化后设置
|
||||
self.should_continue: bool = False
|
||||
self.ignore_until_timestamp: Optional[float] = None
|
||||
self.generated_reply: str = ""
|
||||
self.chat_stream: Optional[ChatStream] = None
|
||||
|
|
@ -81,13 +73,15 @@ class Conversation:
|
|||
self.conversation_info: Optional[ConversationInfo] = None
|
||||
self.reply_checker: Optional[ReplyChecker] = None
|
||||
|
||||
self._initialized: bool = False # Manager 会在初始化成功后设为 True
|
||||
self._initialized: bool = False
|
||||
|
||||
self.bot_qq_str: Optional[str] = str(global_config.BOT_QQ) if global_config.BOT_QQ else None
|
||||
if not self.bot_qq_str:
|
||||
logger.error(f"[私聊][{self.private_name}] 严重错误:未能从配置中获取 BOT_QQ ID!")
|
||||
|
||||
#确保这个属性被正确初始化
|
||||
self.consecutive_llm_action_failures: int = 0 # LLM相关动作连续失败的计数器
|
||||
|
||||
# _initialize 和 _load_initial_history 方法已被移除
|
||||
|
||||
async def start(self):
|
||||
"""
|
||||
|
|
@ -103,27 +97,27 @@ class Conversation:
|
|||
|
||||
logger.info(f"[私聊][{self.private_name}] 对话系统启动,准备创建规划循环任务...")
|
||||
try:
|
||||
# >>> 修改后的调用 <<<
|
||||
# 创建PFC主循环任务
|
||||
_loop_task = asyncio.create_task(run_conversation_loop(self))
|
||||
logger.info(f"[私聊][{self.private_name}] 规划循环任务已创建。")
|
||||
except Exception as task_err:
|
||||
logger.error(f"[私聊][{self.private_name}] 创建规划循环任务时出错: {task_err}")
|
||||
await self.stop()
|
||||
await self.stop() # 发生错误时尝试停止
|
||||
|
||||
async def stop(self):
|
||||
"""
|
||||
停止对话实例并清理相关资源。
|
||||
"""
|
||||
logger.info(f"[私聊][{self.private_name}] 正在停止对话实例: {self.stream_id}")
|
||||
self.should_continue = False
|
||||
self.should_continue = False # 设置标志以退出循环
|
||||
|
||||
# 最终关系评估
|
||||
if (
|
||||
self._initialized
|
||||
self._initialized # 确保已初始化
|
||||
and self.relationship_updater
|
||||
and self.conversation_info
|
||||
and self.observation_info
|
||||
and self.chat_observer
|
||||
and self.chat_observer # 确保所有需要的组件都存在
|
||||
):
|
||||
try:
|
||||
logger.info(f"[私聊][{self.private_name}] 准备执行最终关系评估...")
|
||||
|
|
@ -141,24 +135,23 @@ class Conversation:
|
|||
|
||||
# 停止其他组件
|
||||
if self.idle_chat:
|
||||
# 减少活跃实例计数,而不是停止IdleChat
|
||||
await self.idle_chat.decrement_active_instances()
|
||||
await self.idle_chat.decrement_active_instances() # 减少活跃实例计数
|
||||
logger.debug(f"[私聊][{self.private_name}] 已减少IdleChat活跃实例计数")
|
||||
if self.observation_info and self.chat_observer:
|
||||
self.observation_info.unbind_from_chat_observer()
|
||||
if self.mood_mng and hasattr(self.mood_mng, "stop_mood_update") and self.mood_mng._running: # type: ignore
|
||||
self.mood_mng.stop_mood_update() # type: ignore
|
||||
if self.observation_info and self.chat_observer: # 确保二者都存在
|
||||
self.observation_info.unbind_from_chat_observer() # 解绑
|
||||
if self.mood_mng and hasattr(self.mood_mng, "stop_mood_update") and self.mood_mng._running: # type: ignore
|
||||
self.mood_mng.stop_mood_update() # type: ignore
|
||||
logger.debug(f"[私聊][{self.private_name}] MoodManager 后台更新已停止。")
|
||||
|
||||
self._initialized = False # 标记为未初始化
|
||||
self._initialized = False # 标记为未初始化
|
||||
logger.info(f"[私聊][{self.private_name}] 对话实例 {self.stream_id} 已停止。")
|
||||
|
||||
# _plan_and_action_loop 方法已被移除
|
||||
|
||||
def _convert_to_message(self, msg_dict: Dict[str, Any]) -> Optional[Message]:
|
||||
"""将从数据库或其他来源获取的消息字典转换为内部使用的 Message 对象"""
|
||||
# 这个方法似乎没有被其他内部方法调用,但为了完整性暂时保留
|
||||
try:
|
||||
# 尝试获取与此对话实例关联的 ChatStream
|
||||
chat_stream_to_use = self.chat_stream or chat_manager.get_stream(self.stream_id)
|
||||
if not chat_stream_to_use:
|
||||
logger.error(
|
||||
|
|
@ -166,6 +159,7 @@ class Conversation:
|
|||
)
|
||||
return None
|
||||
|
||||
# 解析 UserInfo
|
||||
user_info_dict = msg_dict.get("user_info", {})
|
||||
user_info: Optional[UserInfo] = None
|
||||
if isinstance(user_info_dict, dict):
|
||||
|
|
@ -175,21 +169,22 @@ class Conversation:
|
|||
logger.warning(
|
||||
f"[私聊][{self.private_name}] 从字典创建 UserInfo 时出错: {e}, dict: {user_info_dict}"
|
||||
)
|
||||
if not user_info:
|
||||
if not user_info: # 如果没有有效的 UserInfo,则无法创建 Message 对象
|
||||
logger.warning(
|
||||
f"[私聊][{self.private_name}] 消息缺少有效的 UserInfo,无法转换。 msg_id: {msg_dict.get('message_id')}"
|
||||
)
|
||||
return None
|
||||
|
||||
# 创建并返回 Message 对象
|
||||
return Message(
|
||||
message_id=msg_dict.get("message_id", f"gen_{time.time()}"),
|
||||
message_id=msg_dict.get("message_id", f"gen_{time.time()}"), # 提供默认 message_id
|
||||
chat_stream=chat_stream_to_use,
|
||||
time=msg_dict.get("time", time.time()),
|
||||
time=msg_dict.get("time", time.time()), # 提供默认时间
|
||||
user_info=user_info,
|
||||
processed_plain_text=msg_dict.get("processed_plain_text", ""),
|
||||
detailed_plain_text=msg_dict.get("detailed_plain_text", ""),
|
||||
processed_plain_text=msg_dict.get("processed_plain_text", ""), # 提供默认文本
|
||||
detailed_plain_text=msg_dict.get("detailed_plain_text", ""), # 提供默认详细文本
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"[私聊][{self.private_name}] 转换消息时出错: {e}")
|
||||
logger.error(f"[私聊][{self.private_name}] {traceback.format_exc()}")
|
||||
return None
|
||||
return None # 出错时返回 None
|
||||
|
|
|
|||
Loading…
Reference in New Issue