feat:修复一些BUG

This commit is contained in:
2026-01-08 09:49:01 +08:00
parent 820861752b
commit 472b1a0d5e
33 changed files with 1643 additions and 742 deletions

View File

@@ -2831,10 +2831,64 @@ class AIChat(PluginBase):
refer_type = int(refer_type_elem.text) if refer_type_elem is not None and refer_type_elem.text else 0
logger.debug(f"被引用消息类型: {refer_type}")
# 纯文本消息不需要处理type=1
# 纯文本消息type=1:如果@了机器人,转发给 AI 处理
if refer_type == 1:
logger.debug("引用的是纯文本消息,跳过")
return True
if self._should_reply_quote(message, title_text):
# 获取被引用的文本内容
refer_content_elem = refermsg.find("content")
refer_text = refer_content_elem.text.strip() if refer_content_elem is not None and refer_content_elem.text else ""
# 获取被引用者昵称
refer_displayname = refermsg.find("displayname")
refer_nickname = refer_displayname.text if refer_displayname is not None and refer_displayname.text else "某人"
# 组合消息:引用内容 + 用户评论
# title_text 格式如 "@瑞依 评价下",需要去掉 @昵称 部分
import tomllib
with open("main_config.toml", "rb") as f:
main_config = tomllib.load(f)
bot_nickname = main_config.get("Bot", {}).get("nickname", "")
user_comment = title_text
if bot_nickname:
# 移除 @机器人昵称(可能有空格分隔)
user_comment = user_comment.replace(f"@{bot_nickname}", "").strip()
# 构造给 AI 的消息
combined_message = f"[引用 {refer_nickname} 的消息:{refer_text}]\n{user_comment}"
logger.info(f"引用纯文本消息,转发给 AI: {combined_message[:80]}...")
# 调用 AI 处理
nickname = await self._get_user_display_label(bot, from_wxid, user_wxid, is_group)
chat_id = from_wxid if is_group else user_wxid
# 保存用户消息到群组历史记录
history_enabled = bool(self.store) and self.config.get("history", {}).get("enabled", True)
sync_bot_messages = self.config.get("history", {}).get("sync_bot_messages", True)
if is_group and history_enabled:
history_chat_id = self._get_group_history_chat_id(from_wxid, user_wxid)
await self._add_to_history(history_chat_id, nickname, combined_message, sender_wxid=user_wxid)
ai_response = await self._call_ai_api(
combined_message,
bot=bot,
from_wxid=from_wxid,
chat_id=chat_id,
nickname=nickname
)
if ai_response:
final_response = self._sanitize_llm_output(ai_response)
await bot.send_text(from_wxid, final_response)
# 保存 AI 回复到群组历史记录
if is_group and history_enabled and sync_bot_messages:
bot_nickname_display = main_config.get("Bot", {}).get("nickname", "AI")
await self._add_to_history(history_chat_id, bot_nickname_display, final_response, role="assistant")
return False
else:
logger.debug("引用的是纯文本消息且未@机器人,跳过")
return True
# 只处理图片(3)、视频(43)、应用消息(49含聊天记录)
if refer_type not in [3, 43, 49]:
@@ -3553,40 +3607,50 @@ class AIChat(PluginBase):
def _should_reply_quote(self, message: dict, title_text: str) -> bool:
"""判断是否应该回复引用消息"""
is_group = message.get("IsGroup", False)
# 检查群聊/私聊开关
if is_group and not self.config["behavior"]["reply_group"]:
return False
if not is_group and not self.config["behavior"]["reply_private"]:
return False
trigger_mode = self.config["behavior"]["trigger_mode"]
# all模式回复所有消息
if trigger_mode == "all":
return True
# mention模式检查是否@了机器人
if trigger_mode == "mention":
if is_group:
# 方式1检查 Ats 字段(普通消息格式)
ats = message.get("Ats", [])
if not ats:
return False
import tomllib
with open("main_config.toml", "rb") as f:
main_config = tomllib.load(f)
bot_wxid = main_config.get("Bot", {}).get("wxid", "")
return bot_wxid and bot_wxid in ats
bot_nickname = main_config.get("Bot", {}).get("nickname", "")
# 检查 Ats 列表
if bot_wxid and bot_wxid in ats:
return True
# 方式2检查标题中是否包含 @机器人昵称(引用消息格式)
# 引用消息的 @ 信息在 title 中,如 "@瑞依 评价下"
if bot_nickname and f"@{bot_nickname}" in title_text:
logger.debug(f"引用消息标题中检测到 @{bot_nickname}")
return True
return False
else:
return True
# keyword模式检查关键词
if trigger_mode == "keyword":
keywords = self.config["behavior"]["keywords"]
return any(kw in title_text for kw in keywords)
return False
async def _call_ai_api_with_image(