🤖 自动格式化代码 [skip ci]

This commit is contained in:
github-actions[bot]
2025-04-28 10:55:43 +00:00
parent 44549a43f8
commit 79a70e0202

View File

@@ -46,15 +46,18 @@ class ObservationInfoHandler(NotificationHandler):
try: try:
user_info = UserInfo.from_dict(user_info_dict) user_info = UserInfo.from_dict(user_info_dict)
except Exception as e: except Exception as e:
logger.error(f"[私聊][{self.private_name}]从字典创建 UserInfo 时出错: {e}, 字典内容: {user_info_dict}") logger.error(
f"[私聊][{self.private_name}]从字典创建 UserInfo 时出错: {e}, 字典内容: {user_info_dict}"
)
# 可以选择在这里返回或记录错误,避免后续代码出错 # 可以选择在这里返回或记录错误,避免后续代码出错
return return
elif user_info_dict is not None: elif user_info_dict is not None:
logger.warning(f"[私聊][{self.private_name}]收到的 user_info 不是预期的字典类型: {type(user_info_dict)}") logger.warning(
f"[私聊][{self.private_name}]收到的 user_info 不是预期的字典类型: {type(user_info_dict)}"
)
# 根据需要处理非字典情况,这里暂时返回 # 根据需要处理非字典情况,这里暂时返回
return return
message = { message = {
"message_id": message_id, "message_id": message_id,
"processed_plain_text": processed_plain_text, "processed_plain_text": processed_plain_text,
@@ -65,7 +68,6 @@ class ObservationInfoHandler(NotificationHandler):
# 传递 UserInfo 对象(如果成功创建)或原始字典 # 传递 UserInfo 对象(如果成功创建)或原始字典
await self.observation_info.update_from_message(message, user_info) # 修改:传递 user_info 对象 await self.observation_info.update_from_message(message, user_info) # 修改:传递 user_info 对象
elif notification_type == NotificationType.COLD_CHAT: elif notification_type == NotificationType.COLD_CHAT:
# 处理冷场通知 # 处理冷场通知
is_cold = data.get("is_cold", False) is_cold = data.get("is_cold", False)
@@ -97,7 +99,6 @@ class ObservationInfoHandler(NotificationHandler):
if len(self.observation_info.unprocessed_messages) < original_count: if len(self.observation_info.unprocessed_messages) < original_count:
logger.info(f"[私聊][{self.private_name}]移除了未处理的消息 (ID: {message_id})") logger.info(f"[私聊][{self.private_name}]移除了未处理的消息 (ID: {message_id})")
elif notification_type == NotificationType.USER_JOINED: elif notification_type == NotificationType.USER_JOINED:
# 处理用户加入通知 (如果适用私聊场景) # 处理用户加入通知 (如果适用私聊场景)
user_id = data.get("user_id") user_id = data.get("user_id")
@@ -192,7 +193,7 @@ class ObservationInfo:
def unbind_from_chat_observer(self): def unbind_from_chat_observer(self):
"""解除与chat_observer的绑定""" """解除与chat_observer的绑定"""
if self.chat_observer and hasattr(self.chat_observer, 'notification_manager'): # 增加检查 if self.chat_observer and hasattr(self.chat_observer, "notification_manager"): # 增加检查
try: try:
self.chat_observer.notification_manager.unregister_handler( self.chat_observer.notification_manager.unregister_handler(
target="observation_info", notification_type=NotificationType.NEW_MESSAGE, handler=self.handler target="observation_info", notification_type=NotificationType.NEW_MESSAGE, handler=self.handler
@@ -212,7 +213,6 @@ class ObservationInfo:
else: else:
logger.warning(f"[私聊][{self.private_name}]尝试解绑时 ChatObserver 不存在或无效") logger.warning(f"[私聊][{self.private_name}]尝试解绑时 ChatObserver 不存在或无效")
# 修改update_from_message 接收 UserInfo 对象 # 修改update_from_message 接收 UserInfo 对象
async def update_from_message(self, message: Dict[str, Any], user_info: Optional[UserInfo]): async def update_from_message(self, message: Dict[str, Any], user_info: Optional[UserInfo]):
"""从消息更新信息 """从消息更新信息
@@ -235,7 +235,6 @@ class ObservationInfo:
self.cold_chat_start_time = None self.cold_chat_start_time = None
self.cold_chat_duration = 0.0 self.cold_chat_duration = 0.0
if user_info: if user_info:
sender_id = str(user_info.user_id) # 确保是字符串 sender_id = str(user_info.user_id) # 确保是字符串
self.last_message_sender = sender_id self.last_message_sender = sender_id
@@ -246,10 +245,11 @@ class ObservationInfo:
self.last_user_speak_time = message_time self.last_user_speak_time = message_time
self.active_users.add(sender_id) # 用户发言则认为其活跃 self.active_users.add(sender_id) # 用户发言则认为其活跃
else: else:
logger.warning(f"[私聊][{self.private_name}]处理消息更新时缺少有效的 UserInfo 对象, message_id: {message_id}") logger.warning(
f"[私聊][{self.private_name}]处理消息更新时缺少有效的 UserInfo 对象, message_id: {message_id}"
)
self.last_message_sender = None # 发送者未知 self.last_message_sender = None # 发送者未知
# 将原始消息字典添加到未处理列表 # 将原始消息字典添加到未处理列表
self.unprocessed_messages.append(message) self.unprocessed_messages.append(message)
self.new_messages_count = len(self.unprocessed_messages) # 直接用列表长度 self.new_messages_count = len(self.unprocessed_messages) # 直接用列表长度
@@ -261,13 +261,11 @@ class ObservationInfo:
pass pass
# logger.warning(f"[私聊][{self.private_name}]收到过时或无效时间戳的消息: ID={message_id}, time={message_time}") # logger.warning(f"[私聊][{self.private_name}]收到过时或无效时间戳的消息: ID={message_id}, time={message_time}")
def update_changed(self): def update_changed(self):
"""标记状态已改变,并重置标记""" """标记状态已改变,并重置标记"""
# logger.debug(f"[私聊][{self.private_name}]状态标记为已改变 (changed=True)") # logger.debug(f"[私聊][{self.private_name}]状态标记为已改变 (changed=True)")
self.changed = True self.changed = True
async def update_cold_chat_status(self, is_cold: bool, current_time: float): async def update_cold_chat_status(self, is_cold: bool, current_time: float):
"""更新冷场状态 """更新冷场状态
@@ -279,7 +277,9 @@ class ObservationInfo:
self.is_cold_chat = is_cold self.is_cold_chat = is_cold
if is_cold: if is_cold:
# 进入冷场状态 # 进入冷场状态
self.cold_chat_start_time = self.last_message_time or current_time # 从最后消息时间开始算,或从当前时间开始 self.cold_chat_start_time = (
self.last_message_time or current_time
) # 从最后消息时间开始算,或从当前时间开始
logger.info(f"[私聊][{self.private_name}]进入冷场状态,开始时间: {self.cold_chat_start_time}") logger.info(f"[私聊][{self.private_name}]进入冷场状态,开始时间: {self.cold_chat_start_time}")
else: else:
# 结束冷场状态 # 结束冷场状态
@@ -293,7 +293,6 @@ class ObservationInfo:
if self.is_cold_chat and self.cold_chat_start_time: if self.is_cold_chat and self.cold_chat_start_time:
self.cold_chat_duration = current_time - self.cold_chat_start_time self.cold_chat_duration = current_time - self.cold_chat_start_time
def get_active_duration(self) -> float: def get_active_duration(self) -> float:
"""获取当前活跃时长 (距离最后一条消息的时间) """获取当前活跃时长 (距离最后一条消息的时间)
@@ -350,7 +349,6 @@ class ObservationInfo:
logger.error(f"[私聊][{self.private_name}]构建聊天记录字符串时出错: {e}") logger.error(f"[私聊][{self.private_name}]构建聊天记录字符串时出错: {e}")
self.chat_history_str = "[构建聊天记录出错]" # 提供错误提示 self.chat_history_str = "[构建聊天记录出错]" # 提供错误提示
# 清空未处理消息列表和计数 # 清空未处理消息列表和计数
# cleared_count = len(self.unprocessed_messages) # cleared_count = len(self.unprocessed_messages)
self.unprocessed_messages.clear() self.unprocessed_messages.clear()