mirror of
https://github.com/Nezumi-2711/astrbot_plugin_qq_group_daily_analysis.git
synced 2026-09-22 05:31:52 +00:00
feat(TraceID): 主要通过引入全链路追踪,增强了日志的可观测性,并加固了去重拦截逻辑
This commit is contained in:
@@ -8,10 +8,13 @@ QQ群日常分析插件
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from astrbot.api import AstrBotConfig, logger
|
||||
from astrbot.api import AstrBotConfig, logger as astrbot_logger
|
||||
from astrbot.api.event import AstrMessageEvent, filter
|
||||
from astrbot.api.event.filter import PermissionType
|
||||
from astrbot.api.star import Context, Star
|
||||
|
||||
from .src.utils.logger import logger
|
||||
from .src.utils.trace_context import TraceContext, TraceLogFilter
|
||||
from astrbot.core.message.components import File
|
||||
|
||||
from .src.application.commands.template_command_service import (
|
||||
@@ -159,6 +162,12 @@ class GroupDailyAnalysis(Star):
|
||||
return
|
||||
|
||||
try:
|
||||
# 注册 TraceID 过滤器
|
||||
trace_filter = TraceLogFilter()
|
||||
if not any(isinstance(f, TraceLogFilter) for f in astrbot_logger.filters):
|
||||
astrbot_logger.addFilter(trace_filter)
|
||||
astrbot_logger.info("[Trace] TraceID 日志追踪已启用")
|
||||
|
||||
logger.info(f"正在执行插件初始化 (来源: {source})...")
|
||||
# 检查插件是否被启用 (Fix for empty plugin_set issue)
|
||||
if self.context:
|
||||
@@ -417,7 +426,11 @@ class GroupDailyAnalysis(Star):
|
||||
yield event.plain_result("❌ 此群未启用日常分析功能")
|
||||
return
|
||||
|
||||
yield event.plain_result("🔍 正在启动跨平台分析引擎,正在拉取最近消息...")
|
||||
# 设置 TraceID
|
||||
trace_id = TraceContext.generate(prefix=f"manual_{group_id}")
|
||||
TraceContext.set(trace_id)
|
||||
|
||||
yield event.plain_result(f"🔍 正在启动跨平台分析引擎,正在拉取最近消息...\n[ID: {trace_id}]")
|
||||
|
||||
try:
|
||||
# 调用 DDD 应用级服务
|
||||
@@ -468,9 +481,10 @@ class GroupDailyAnalysis(Star):
|
||||
)
|
||||
|
||||
if image_url:
|
||||
caption = f"📊 每日群聊分析报告已生成:\n[ID: {trace_id}]"
|
||||
# 优先使用适配器的 send_image (由插件适配器统一处理 Base64 转换和路径问题)
|
||||
# 不再使用 yield event.image_result 回退,防止适配器超时回复导致重复发送图片
|
||||
await adapter.send_image(group_id, image_url)
|
||||
await adapter.send_image(group_id, image_url, caption=caption)
|
||||
|
||||
# 上传到群文件/群相册 (属于附加功能,不影响消息发送)
|
||||
await self._try_upload_image(group_id, image_url, platform_id)
|
||||
@@ -482,7 +496,7 @@ class GroupDailyAnalysis(Star):
|
||||
analysis_result,
|
||||
group_id,
|
||||
platform_id,
|
||||
caption="📊 每日群聊分析报告已生成:",
|
||||
caption=f"📊 每日群聊分析报告已生成:\n[ID: {trace_id}]",
|
||||
)
|
||||
else:
|
||||
text_report = self.report_generator.generate_text_report(
|
||||
|
||||
@@ -613,8 +613,10 @@ class OneBotAdapter(PlatformAdapter):
|
||||
user_id = str(
|
||||
msg.get("user_id", msg.get("sender", {}).get("user_id", ""))
|
||||
)
|
||||
if user_id != self_id:
|
||||
continue
|
||||
if user_id not in self.bot_self_ids:
|
||||
# 如果内存中没有,尝试最后一次实时提取作为兜底
|
||||
if not self_id or user_id != self_id:
|
||||
continue
|
||||
|
||||
# 检查消息内容是否包含图片
|
||||
raw_message = msg.get("message", [])
|
||||
@@ -627,14 +629,18 @@ class OneBotAdapter(PlatformAdapter):
|
||||
if search_token:
|
||||
# 精确匹配 TraceID
|
||||
if search_token in msg_str:
|
||||
logger.debug(
|
||||
f"自检发现群 {group_id} 已有匹配 ID ({search_token}) 的图片回显,拦截重复发送。"
|
||||
logger.info(
|
||||
f"[OneBot] [真相检查] 发现匹配 ID ({search_token}) 的历史图片。拦截重复发送。群: {group_id}"
|
||||
)
|
||||
return True
|
||||
else:
|
||||
logger.debug(
|
||||
f"[OneBot] [真相检查] 发现机器人发送的图片,但 ID 不匹配。跳过。群: {group_id}"
|
||||
)
|
||||
else:
|
||||
# 广义匹配(回退模式)
|
||||
logger.debug(
|
||||
f"自检发现群 {group_id} 已有成功发送的图片回显,无需重试。"
|
||||
logger.info(
|
||||
f"[OneBot] [真相检查] 发现近期发送过的图片回显 (广义匹配)。无需重试。群: {group_id}"
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
@@ -87,6 +87,11 @@ class BotManager:
|
||||
elif bot_self_ids:
|
||||
self._bot_self_ids = [str(bot_self_ids)]
|
||||
|
||||
# 同步更新所有现有适配器的 ID 列表
|
||||
for adapter in self._adapters.values():
|
||||
if hasattr(adapter, "bot_self_ids"):
|
||||
adapter.bot_self_ids = self._bot_self_ids.copy()
|
||||
|
||||
def get_bot_instance(self, platform_id=None):
|
||||
"""获取指定平台的bot实例,如果不指定则返回第一个可用的实例"""
|
||||
if platform_id:
|
||||
|
||||
Reference in New Issue
Block a user