HFC对应修改(部分)

This commit is contained in:
UnCLAS-Prommer
2026-02-24 15:59:35 +08:00
parent 12d4f236be
commit a8e8f6b7b3
5 changed files with 62 additions and 422 deletions

View File

@@ -1,11 +1,11 @@
import traceback
from typing import Any, Optional, Dict
from typing import Optional, Dict
import traceback
from src.chat.message_receive.chat_stream import get_chat_manager
from src.common.logger import get_logger
from src.chat.message_receive.chat_manager import chat_manager
from src.chat.heart_flow.heartFC_chat import HeartFChatting
from src.chat.brain_chat.brain_chat import BrainChatting
from src.chat.message_receive.chat_stream import ChatStream
logger = get_logger("heartflow")
@@ -14,29 +14,26 @@ class Heartflow:
"""主心流协调器,负责初始化并协调聊天"""
def __init__(self):
self.heartflow_chat_list: Dict[Any, HeartFChatting | BrainChatting] = {}
self.heartflow_chat_list: Dict[str, HeartFChatting | BrainChatting] = {}
async def get_or_create_heartflow_chat(self, chat_id: Any) -> Optional[HeartFChatting | BrainChatting]:
async def get_or_create_heartflow_chat(self, session_id: str) -> Optional[HeartFChatting | BrainChatting]:
"""获取或创建一个新的HeartFChatting实例"""
try:
if chat_id in self.heartflow_chat_list:
if chat := self.heartflow_chat_list.get(chat_id):
return chat
else:
chat_stream: ChatStream | None = get_chat_manager().get_stream(chat_id)
if not chat_stream:
raise ValueError(f"未找到 chat_id={chat_id} 的聊天流")
if chat_stream.group_info:
new_chat = HeartFChatting(chat_id=chat_id)
else:
new_chat = BrainChatting(chat_id=chat_id)
await new_chat.start()
self.heartflow_chat_list[chat_id] = new_chat
return new_chat
if chat := self.heartflow_chat_list.get(session_id):
return chat
chat_session = chat_manager.get_session_by_session_id(session_id)
if not chat_session:
raise ValueError(f"未找到 session_id={session_id} 的聊天流")
new_chat = (
HeartFChatting(session_id=session_id) if chat_session.group_id else BrainChatting(session_id=session_id)
)
await new_chat.start()
self.heartflow_chat_list[session_id] = new_chat
return new_chat
except Exception as e:
logger.error(f"创建心流聊天 {chat_id} 失败: {e}", exc_info=True)
logger.error(f"创建心流聊天 {session_id} 失败: {e}", exc_info=True)
traceback.print_exc()
return None
return None
heartflow = Heartflow()

View File

@@ -1,21 +1,16 @@
import re
import traceback
from typing import TYPE_CHECKING
from src.chat.message_receive.message import MessageRecv
from src.chat.message_receive.storage import MessageStorage
import traceback
from src.chat.heart_flow.heartflow import heartflow
from src.chat.utils.utils import is_mentioned_bot_in_message
from src.chat.utils.chat_message_builder import replace_user_references
# from src.chat.utils.chat_message_builder import replace_user_references
from src.common.utils.utils_message import MessageUtils
from src.common.logger import get_logger
from src.person_info.person_info import Person
from sqlmodel import select, col
from src.common.database.database import get_db_session
from src.common.database.database_model import Images, ImageType
if TYPE_CHECKING:
pass
from src.chat.message_receive.message import SessionMessage
logger = get_logger("chat")
@@ -24,10 +19,9 @@ class HeartFCMessageReceiver:
"""心流处理器,负责处理接收到的消息并计算兴趣度"""
def __init__(self):
"""初始化心流处理器,创建消息存储实例"""
self.storage = MessageStorage()
pass
async def process_message(self, message: MessageRecv) -> None:
async def process_message(self, message: "SessionMessage"):
"""处理接收到的原始消息数据
主要流程:
@@ -38,7 +32,7 @@ class HeartFCMessageReceiver:
5. 关系处理
Args:
message_data: 原始消息字符串
message: SessionMessage对象包含原始消息数据和相关信息
"""
try:
# 通知消息不处理
@@ -48,70 +42,46 @@ class HeartFCMessageReceiver:
# 1. 消息解析与初始化
userinfo = message.message_info.user_info
chat = message.chat_stream
if userinfo is None or message.message_info.platform is None:
group_info = message.message_info.group_info
if userinfo is None or message.platform is None:
raise ValueError("message userinfo or platform is missing")
if userinfo.user_id is None or userinfo.user_nickname is None:
raise ValueError("message userinfo id or nickname is missing")
user_id = userinfo.user_id
nickname = userinfo.user_nickname
# 2. 计算at信息
is_mentioned, is_at, reply_probability_boost = is_mentioned_bot_in_message(message)
# print(f"is_mentioned: {is_mentioned}, is_at: {is_at}, reply_probability_boost: {reply_probability_boost}")
message.is_mentioned = is_mentioned
message.is_at = is_at
message.reply_probability_boost = reply_probability_boost
# 2. 计算at信息 现在转移给Adapter完成
# is_mentioned, is_at, reply_probability_boost = is_mentioned_bot_in_message(message)
# # print(f"is_mentioned: {is_mentioned}, is_at: {is_at}, reply_probability_boost: {reply_probability_boost}")
# message.is_mentioned = is_mentioned
# message.is_at = is_at
await self.storage.store_message(message, chat)
MessageUtils.store_message_to_db(message) # 存储消息到数据库
await heartflow.get_or_create_heartflow_chat(chat.stream_id) # type: ignore
await heartflow.get_or_create_heartflow_chat(message.session_id)
# 3. 日志记录
mes_name = chat.group_info.group_name if chat.group_info else "私聊"
mes_name = group_info.group_name if group_info else "私聊"
# 用这个pattern截取出id部分picid是一个list并替换成对应的图片描述
picid_pattern = r"\[picid:([^\]]+)\]"
picid_list = re.findall(picid_pattern, message.processed_plain_text)
# TODO: 修复引用格式替换
# # 应用用户引用格式替换,将回复<aaa:bbb>和@<aaa:bbb>格式转换为可读格式
# processed_plain_text = replace_user_references(
# processed_text, message.message_info.platform, replace_bot_name=True
# )
# # if not processed_plain_text:
# # print(message)
# 创建替换后的文本
processed_text = message.processed_plain_text
if picid_list:
for picid in picid_list:
with get_db_session() as session:
statement = (
select(Images).where(
(col(Images.id) == int(picid)) & (col(Images.image_type) == ImageType.IMAGE)
)
if picid.isdigit()
else None
)
image = session.exec(statement).first() if statement is not None else None
if image and image.description:
# 将[picid:xxxx]替换成图片描述
processed_text = processed_text.replace(f"[picid:{picid}]", f"[图片:{image.description}]")
else:
# 如果没有找到图片描述,则移除[picid:xxxx]标记
processed_text = processed_text.replace(f"[picid:{picid}]", "[图片:网络不好,图片无法加载]")
# 应用用户引用格式替换,将回复<aaa:bbb>和@<aaa:bbb>格式转换为可读格式
processed_plain_text = replace_user_references(
processed_text, message.message_info.platform, replace_bot_name=True
)
# if not processed_plain_text:
# print(message)
logger.info(f"[{mes_name}]{userinfo.user_nickname}:{processed_plain_text}")
logger.info(f"[{mes_name}]{userinfo.user_nickname}:{message.processed_plain_text}")
# 如果是群聊,获取群号和群昵称
group_id = None
group_nick_name = None
if chat.group_info:
group_id = chat.group_info.group_id
if group_info:
group_id = group_info.group_id
group_nick_name = userinfo.user_cardname
_ = Person.register_person(
platform=message.message_info.platform,
platform=message.platform,
user_id=user_id,
nickname=nickname,
group_id=group_id,