fix: PFC不读取聊天记录
This commit is contained in:
@@ -6,7 +6,7 @@ import time
|
||||
from dataclasses import dataclass, field
|
||||
from src.common.logger import get_module_logger
|
||||
from .chat_observer import ChatObserver
|
||||
from .chat_states import NotificationHandler
|
||||
from .chat_states import NotificationHandler, NotificationType
|
||||
|
||||
logger = get_module_logger("observation_info")
|
||||
|
||||
@@ -22,63 +22,70 @@ class ObservationInfoHandler(NotificationHandler):
|
||||
"""
|
||||
self.observation_info = observation_info
|
||||
|
||||
async def handle_notification(self, notification: Dict[str, Any]):
|
||||
"""处理通知
|
||||
|
||||
Args:
|
||||
notification: 通知数据
|
||||
"""
|
||||
notification_type = notification.get("type")
|
||||
data = notification.get("data", {})
|
||||
|
||||
if notification_type == "NEW_MESSAGE":
|
||||
async def handle_notification(self, notification):
|
||||
# 获取通知类型和数据
|
||||
notification_type = notification.type
|
||||
data = notification.data
|
||||
|
||||
if notification_type == NotificationType.NEW_MESSAGE:
|
||||
# 处理新消息通知
|
||||
logger.debug(f"收到新消息通知data: {data}")
|
||||
message = data.get("message", {})
|
||||
message_id = data.get("message_id")
|
||||
processed_plain_text = data.get("processed_plain_text")
|
||||
detailed_plain_text = data.get("detailed_plain_text")
|
||||
user_info = data.get("user_info")
|
||||
time_value = data.get("time")
|
||||
|
||||
message = {
|
||||
"message_id": message_id,
|
||||
"processed_plain_text": processed_plain_text,
|
||||
"detailed_plain_text": detailed_plain_text,
|
||||
"user_info": user_info,
|
||||
"time": time_value
|
||||
}
|
||||
|
||||
self.observation_info.update_from_message(message)
|
||||
# self.observation_info.has_unread_messages = True
|
||||
# self.observation_info.new_unread_message.append(message.get("processed_plain_text", ""))
|
||||
|
||||
elif notification_type == "COLD_CHAT":
|
||||
elif notification_type == NotificationType.COLD_CHAT:
|
||||
# 处理冷场通知
|
||||
is_cold = data.get("is_cold", False)
|
||||
self.observation_info.update_cold_chat_status(is_cold, time.time())
|
||||
|
||||
elif notification_type == "ACTIVE_CHAT":
|
||||
elif notification_type == NotificationType.ACTIVE_CHAT:
|
||||
# 处理活跃通知
|
||||
is_active = data.get("is_active", False)
|
||||
self.observation_info.is_cold = not is_active
|
||||
|
||||
elif notification_type == "BOT_SPEAKING":
|
||||
elif notification_type == NotificationType.BOT_SPEAKING:
|
||||
# 处理机器人说话通知
|
||||
self.observation_info.is_typing = False
|
||||
self.observation_info.last_bot_speak_time = time.time()
|
||||
|
||||
elif notification_type == "USER_SPEAKING":
|
||||
elif notification_type == NotificationType.USER_SPEAKING:
|
||||
# 处理用户说话通知
|
||||
self.observation_info.is_typing = False
|
||||
self.observation_info.last_user_speak_time = time.time()
|
||||
|
||||
elif notification_type == "MESSAGE_DELETED":
|
||||
elif notification_type == NotificationType.MESSAGE_DELETED:
|
||||
# 处理消息删除通知
|
||||
message_id = data.get("message_id")
|
||||
self.observation_info.unprocessed_messages = [
|
||||
msg for msg in self.observation_info.unprocessed_messages if msg.get("message_id") != message_id
|
||||
]
|
||||
|
||||
elif notification_type == "USER_JOINED":
|
||||
elif notification_type == NotificationType.USER_JOINED:
|
||||
# 处理用户加入通知
|
||||
user_id = data.get("user_id")
|
||||
if user_id:
|
||||
self.observation_info.active_users.add(user_id)
|
||||
|
||||
elif notification_type == "USER_LEFT":
|
||||
elif notification_type == NotificationType.USER_LEFT:
|
||||
# 处理用户离开通知
|
||||
user_id = data.get("user_id")
|
||||
if user_id:
|
||||
self.observation_info.active_users.discard(user_id)
|
||||
|
||||
elif notification_type == "ERROR":
|
||||
elif notification_type == NotificationType.ERROR:
|
||||
# 处理错误通知
|
||||
error_msg = data.get("error", "")
|
||||
logger.error(f"收到错误通知: {error_msg}")
|
||||
@@ -100,6 +107,7 @@ class ObservationInfo:
|
||||
last_message_content: str = ""
|
||||
last_message_sender: Optional[str] = None
|
||||
bot_id: Optional[str] = None
|
||||
chat_history_count: int = 0
|
||||
new_messages_count: int = 0
|
||||
cold_chat_duration: float = 0.0
|
||||
|
||||
@@ -117,28 +125,37 @@ class ObservationInfo:
|
||||
self.chat_observer = None
|
||||
self.handler = ObservationInfoHandler(self)
|
||||
|
||||
def bind_to_chat_observer(self, stream_id: str):
|
||||
def bind_to_chat_observer(self, chat_observer: ChatObserver):
|
||||
"""绑定到指定的chat_observer
|
||||
|
||||
Args:
|
||||
stream_id: 聊天流ID
|
||||
"""
|
||||
self.chat_observer = ChatObserver.get_instance(stream_id)
|
||||
self.chat_observer = chat_observer
|
||||
print(f"1919810----------------------绑定-----------------------------")
|
||||
print(self.chat_observer)
|
||||
print(f"1919810--------------------绑定-----------------------------")
|
||||
print(self.chat_observer.notification_manager)
|
||||
print(f"1919810-------------------绑定-----------------------------")
|
||||
self.chat_observer.notification_manager.register_handler(
|
||||
target="observation_info", notification_type="NEW_MESSAGE", handler=self.handler
|
||||
target="observation_info", notification_type=NotificationType.NEW_MESSAGE, handler=self.handler
|
||||
)
|
||||
self.chat_observer.notification_manager.register_handler(
|
||||
target="observation_info", notification_type="COLD_CHAT", handler=self.handler
|
||||
target="observation_info", notification_type=NotificationType.COLD_CHAT, handler=self.handler
|
||||
)
|
||||
print("1919810------------------------绑定-----------------------------")
|
||||
print(f"1919810--------------------绑定-----------------------------")
|
||||
print(self.chat_observer.notification_manager)
|
||||
print(f"1919810-------------------绑定-----------------------------")
|
||||
|
||||
def unbind_from_chat_observer(self):
|
||||
"""解除与chat_observer的绑定"""
|
||||
if self.chat_observer:
|
||||
self.chat_observer.notification_manager.unregister_handler(
|
||||
target="observation_info", notification_type="NEW_MESSAGE", handler=self.handler
|
||||
target="observation_info", notification_type=NotificationType.NEW_MESSAGE, handler=self.handler
|
||||
)
|
||||
self.chat_observer.notification_manager.unregister_handler(
|
||||
target="observation_info", notification_type="COLD_CHAT", handler=self.handler
|
||||
target="observation_info", notification_type=NotificationType.COLD_CHAT, handler=self.handler
|
||||
)
|
||||
self.chat_observer = None
|
||||
|
||||
@@ -148,8 +165,11 @@ class ObservationInfo:
|
||||
Args:
|
||||
message: 消息数据
|
||||
"""
|
||||
print("1919810-----------------------------------------------------")
|
||||
logger.debug(f"更新信息from_message: {message}")
|
||||
self.last_message_time = message["time"]
|
||||
self.last_message_id = message["message_id"]
|
||||
|
||||
self.last_message_content = message.get("processed_plain_text", "")
|
||||
|
||||
user_info = UserInfo.from_dict(message.get("user_info", {}))
|
||||
@@ -169,7 +189,6 @@ class ObservationInfo:
|
||||
def update_changed(self):
|
||||
"""更新changed状态"""
|
||||
self.changed = True
|
||||
# self.meta_plan_trigger = True
|
||||
|
||||
def update_cold_chat_status(self, is_cold: bool, current_time: float):
|
||||
"""更新冷场状态
|
||||
@@ -223,17 +242,3 @@ class ObservationInfo:
|
||||
self.unprocessed_messages.clear()
|
||||
self.new_messages_count = 0
|
||||
|
||||
def add_unprocessed_message(self, message: Dict[str, Any]):
|
||||
"""添加未处理的消息
|
||||
|
||||
Args:
|
||||
message: 消息数据
|
||||
"""
|
||||
# 防止重复添加同一消息
|
||||
message_id = message.get("message_id")
|
||||
if message_id and not any(m.get("message_id") == message_id for m in self.unprocessed_messages):
|
||||
self.unprocessed_messages.append(message)
|
||||
self.new_messages_count += 1
|
||||
|
||||
# 同时更新其他消息相关信息
|
||||
self.update_from_message(message)
|
||||
|
||||
Reference in New Issue
Block a user