This commit is contained in:
SengokuCola
2025-09-14 17:10:10 +08:00
13 changed files with 903 additions and 342 deletions

View File

@@ -492,7 +492,7 @@ class HeartFChatting:
return False, "", ""
# 处理动作并获取结果
result = await action_handler.handle_action()
result = await action_handler.execute()
success, action_text = result
command = ""

View File

@@ -180,6 +180,23 @@ class ChatBot:
return
async def echo_message_process(self, raw_data: Dict[str, Any]) -> None:
"""
用于专门处理回送消息ID的函数
"""
message_data: Dict[str, Any] = raw_data.get("content", {})
if not message_data:
return
message_type = message_data.get("type")
if message_type != "echo":
return
mmc_message_id = message_data.get("echo")
actual_message_id = message_data.get("actual_id")
if MessageStorage.update_message(mmc_message_id, actual_message_id):
logger.debug(f"更新消息ID成功: {mmc_message_id} -> {actual_message_id}")
else:
logger.warning(f"更新消息ID失败: {mmc_message_id} -> {actual_message_id}")
async def message_process(self, message_data: Dict[str, Any]) -> None:
"""处理转化后的统一格式消息
这个函数本质是预处理一些数据,根据配置信息和消息内容,预处理消息,并分发到合适的消息处理器中
@@ -215,18 +232,8 @@ class ChatBot:
# print(message_data)
# logger.debug(str(message_data))
message = MessageRecv(message_data)
if await self.handle_notice_message(message):
# return
pass
group_info = message.message_info.group_info
user_info = message.message_info.user_info
if message.message_info.additional_config:
sent_message = message.message_info.additional_config.get("echo", False)
if sent_message: # 处理上报的自身消息更新message_id需要ada支持上报事件
await MessageStorage.update_message(message)
return
continue_flag, modified_message = await events_manager.handle_mai_events(
EventType.ON_MESSAGE_PRE_PROCESS, message
@@ -236,6 +243,10 @@ class ChatBot:
if modified_message and modified_message._modify_flags.modify_message_segments:
message.message_segment = Seg(type="seglist", data=modified_message.message_segments)
if await self.handle_notice_message(message):
# return
pass
get_chat_manager().register_message(message)
chat = await get_chat_manager().get_or_create_stream(

View File

@@ -143,31 +143,26 @@ class MessageStorage:
# 如果需要其他存储相关的函数,可以在这里添加
@staticmethod
async def update_message(
message: MessageRecv,
) -> None: # 用于实时更新数据库的自身发送消息ID目前能处理text,reply,image和emoji
"""更新最新一条匹配消息的message_id"""
def update_message(mmc_message_id: str | None, qq_message_id: str | None) -> bool:
"""实时更新数据库的自身发送消息ID"""
try:
if message.message_segment.type == "notify":
mmc_message_id = message.message_segment.data.get("echo") # type: ignore
qq_message_id = message.message_segment.data.get("actual_id") # type: ignore
else:
logger.info(f"更新消息ID错误seg类型为{message.message_segment.type}")
return
if not qq_message_id:
logger.info("消息不存在message_id无法更新")
return
return False
if matched_message := (
Messages.select().where((Messages.message_id == mmc_message_id)).order_by(Messages.time.desc()).first()
):
# 更新找到的消息记录
Messages.update(message_id=qq_message_id).where(Messages.id == matched_message.id).execute() # type: ignore
logger.debug(f"更新消息ID成功: {matched_message.message_id} -> {qq_message_id}")
return True
else:
logger.debug("未找到匹配的消息")
return False
except Exception as e:
logger.error(f"更新消息ID失败: {e}")
return False
@staticmethod
def replace_image_descriptions(text: str) -> str:

View File

@@ -25,6 +25,7 @@ from src.chat.utils.chat_message_builder import (
replace_user_references,
)
from src.chat.express.expression_selector import expression_selector
# from src.chat.memory_system.memory_activator import MemoryActivator
from src.mood.mood_manager import mood_manager
from src.person_info.person_info import Person, is_person_known
@@ -213,7 +214,7 @@ class DefaultReplyer:
traceback.print_exc()
return False, llm_response
async def build_relation_info(self, chat_content: str, sender: str, person_list: List[Person] = None):
async def build_relation_info(self, chat_content: str, sender: str, person_list: List[Person]):
if not global_config.relationship.enable_relationship:
return ""
@@ -234,7 +235,7 @@ class DefaultReplyer:
for person in person_list:
person_relation = await person.build_relationship()
others_relation += person_relation
return f"{sender_relation}\n{others_relation}"
async def build_expression_habits(self, chat_history: str, target: str) -> Tuple[str, List[int]]:
@@ -665,12 +666,19 @@ class DefaultReplyer:
timestamp=time.time(),
limit=int(global_config.chat.max_context_size * 0.33),
)
person_list_short:List[Person] = []
person_list_short: List[Person] = []
for msg in message_list_before_short:
if global_config.bot.qq_account == msg.user_info.user_id and global_config.bot.platform == msg.user_info.platform:
if (
global_config.bot.qq_account == msg.user_info.user_id
and global_config.bot.platform == msg.user_info.platform
):
continue
if reply_message and reply_message.user_info.user_id == msg.user_info.user_id and reply_message.user_info.platform == msg.user_info.platform:
if (
reply_message
and reply_message.user_info.user_id == msg.user_info.user_id
and reply_message.user_info.platform == msg.user_info.platform
):
continue
person = Person(platform=msg.user_info.platform, user_id=msg.user_info.user_id)
if person.is_known:
@@ -692,7 +700,9 @@ class DefaultReplyer:
self._time_and_run_task(
self.build_expression_habits(chat_talking_prompt_short, target), "expression_habits"
),
self._time_and_run_task(self.build_relation_info(chat_talking_prompt_short,sender, person_list_short), "relation_info"),
self._time_and_run_task(
self.build_relation_info(chat_talking_prompt_short, sender, person_list_short), "relation_info"
),
# self._time_and_run_task(self.build_memory_block(message_list_before_short, target), "memory_block"),
self._time_and_run_task(
self.build_tool_info(chat_talking_prompt_short, sender, target, enable_tool=enable_tool), "tool_info"
@@ -846,7 +856,7 @@ class DefaultReplyer:
# 并行执行2个构建任务
(expression_habits_block, _), relation_info, personality_prompt = await asyncio.gather(
self.build_expression_habits(chat_talking_prompt_half, target),
self.build_relation_info(chat_talking_prompt_half, sender),
self.build_relation_info(chat_talking_prompt_half, sender, []),
self.build_personality_prompt(),
)
@@ -950,7 +960,7 @@ class DefaultReplyer:
with Timer("LLM生成", {}): # 内部计时器,可选保留
# 直接使用已初始化的模型实例
logger.info(f"\n{prompt}\n")
if global_config.debug.show_prompt:
logger.info(f"\n{prompt}\n")
else: