恢复群总结结果自动入库能力
- 重新新增群总结数据库操作类并自动建表 t_message_summary - 在群总结插件初始化时接入总结入库数据库对象 - 定时总结成功发送后自动写入数据库,保留文本结果、图片路径和消息数量 - 失败提醒不入库,避免脏数据进入总结表
This commit is contained in:
@@ -12,6 +12,7 @@ from loguru import logger
|
||||
|
||||
from base.plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from base.plugin_common.plugin_interface import PluginStatus
|
||||
from db.message_summary_db import MessageSummaryDBOperator
|
||||
from utils.compress_chat_data import compress_chat_data
|
||||
from utils.decorator.async_job import async_job
|
||||
from utils.decorator.plugin_decorators import plugin_stats_decorator
|
||||
@@ -68,6 +69,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.message_storage = None
|
||||
self.message_summary_db = None
|
||||
self.revoke = None
|
||||
self._auto_revoke = None
|
||||
# 注册功能权限
|
||||
@@ -83,6 +85,9 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
self._api_key = api_config.get("api_key", "app-McGLzBhBjeBCSEi7n83MtuTo")
|
||||
self._api_url = api_config.get("api_url", "http://192.168.2.240/v1/chat-messages")
|
||||
self.message_storage = MessageStorage()
|
||||
db_manager = context.get("db_manager")
|
||||
if db_manager:
|
||||
self.message_summary_db = MessageSummaryDBOperator(db_manager)
|
||||
|
||||
self.LOG.debug(f"初始化 {self.name} 插件成功")
|
||||
return True
|
||||
@@ -223,6 +228,44 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
revoke_manager.add_message_to_revoke(target, client_msg_id, create_time, new_msg_id, delay)
|
||||
return client_msg_id, create_time, new_msg_id
|
||||
|
||||
def _save_daily_summary_record(
|
||||
self,
|
||||
group_id: str,
|
||||
group_name: str,
|
||||
period_start: datetime,
|
||||
period_end: datetime,
|
||||
message_count: int,
|
||||
summary_text: str,
|
||||
image_path: Optional[str],
|
||||
) -> bool:
|
||||
"""保存每日总结到数据库"""
|
||||
if not self.message_summary_db:
|
||||
self.LOG.warning("群总结数据库未初始化,跳过总结入库")
|
||||
return False
|
||||
|
||||
summary_record = {
|
||||
"chatroom_id": group_id,
|
||||
"group_name": group_name,
|
||||
"summary_type": "daily",
|
||||
"period_key": period_start.strftime("%Y-%m-%d"),
|
||||
"period_start": period_start.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"period_end": period_end.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"source_message_count": message_count,
|
||||
"summary_text": summary_text or "",
|
||||
"image_path": image_path,
|
||||
"meta": {
|
||||
"source": "message_summary_plugin",
|
||||
"summary_date": period_start.strftime("%Y-%m-%d"),
|
||||
"has_image": bool(image_path),
|
||||
},
|
||||
}
|
||||
saved = self.message_summary_db.save_summary(summary_record)
|
||||
if saved:
|
||||
self.LOG.info(f"群总结入库成功: group_id={group_id}, period_key={summary_record['period_key']}")
|
||||
else:
|
||||
self.LOG.error(f"群总结入库失败: group_id={group_id}, period_key={summary_record['period_key']}")
|
||||
return saved
|
||||
|
||||
async def _generate_summary(self, chat_content: str, group_name: str) -> Tuple[str, Optional[str]]:
|
||||
"""生成总结"""
|
||||
# Dify API配置
|
||||
@@ -404,6 +447,15 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
if image_path:
|
||||
# 图片生成成功,发送图片
|
||||
await self.bot.send_image_message(group_id, Path(image_path))
|
||||
self._save_daily_summary_record(
|
||||
group_id,
|
||||
group_name,
|
||||
yesterday_start,
|
||||
yesterday_end,
|
||||
message_count,
|
||||
summary,
|
||||
str(image_path),
|
||||
)
|
||||
self.LOG.info(f"成功发送群 {group_name} 的昨日总结图片")
|
||||
else:
|
||||
# 图片生成失败,发送文本消息
|
||||
@@ -417,6 +469,15 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
self.LOG.warning(f"群 {group_name} 的昨日总结生成失败,已发送可撤回失败提醒")
|
||||
else:
|
||||
await self.bot.send_text_message(group_id, summary)
|
||||
self._save_daily_summary_record(
|
||||
group_id,
|
||||
group_name,
|
||||
yesterday_start,
|
||||
yesterday_end,
|
||||
message_count,
|
||||
summary,
|
||||
None,
|
||||
)
|
||||
self.LOG.info(f"成功发送群 {group_name} 的昨日总结文本")
|
||||
else:
|
||||
await self._send_text_with_revoke(group_id, f"❌ [{yesterday.strftime('%Y-%m-%d')}] 聊天总结生成失败,请稍后再试", 5)
|
||||
|
||||
Reference in New Issue
Block a user