插件定时能力扩展:接入天气/群总结/百科问答/成员画像并补齐周月触发器编辑

- 将 weather、message_summary、game_task、member_context 从硬编码 async_job 注册迁移为插件调度能力(get_schedule_actions/run_scheduled_action)\n- 保持原有默认时间与默认启用行为,新增执行统计结果用于后台日志展示\n- 为群总结与天气推送增加目标群范围适配,支持按后台配置选择 all/白名单/单群执行\n- 成员交互摘要支持日/周/月三类动作接入调度中心,兼容指定群与全量群刷新\n- 后台插件调度页面新增 every_week_time 与 every_month_last_day_time 的编辑支持
This commit is contained in:
liuwei
2026-04-16 15:49:02 +08:00
parent 184999b175
commit 1166323ab5
5 changed files with 333 additions and 66 deletions

View File

@@ -3,7 +3,6 @@ from typing import Dict, Any, Tuple, Optional, List
from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.plugin_interface import PluginStatus
from plugins.member_context.service import MemberContextService
from utils.decorator.async_job import async_job
class MemberContextPlugin(MessagePluginInterface):
@@ -44,39 +43,10 @@ class MemberContextPlugin(MessagePluginInterface):
super().__init__()
self.feature = self.register_feature()
self.service: Optional[MemberContextService] = None
self._job_registered = False
def initialize(self, context: Dict[str, Any]) -> bool:
self.LOG.debug(f"正在初始化 {self.name} 插件...")
self.service = MemberContextService(context["db_manager"], self._config)
refresh_times = self._config.get("schedule", {}).get("refresh_times", [])
weekly_refresh_time = self._config.get("schedule", {}).get("weekly_refresh_time", "")
monthly_refresh_time = self._config.get("schedule", {}).get("monthly_refresh_time", "")
if not self._job_registered:
if refresh_times:
@async_job.at_times(refresh_times)
async def refresh_member_context_job():
if self.service:
self.LOG.info("开始刷新成员交互摘要(日任务)")
self.service.refresh_all_chatrooms(enable_weekly_digest=False, enable_monthly_digest=False)
self.LOG.info("成员交互摘要刷新完成(日任务)")
if weekly_refresh_time:
@async_job.every_week_time(weekday=6, time_str=weekly_refresh_time)
async def refresh_member_context_weekly_job():
if self.service:
self.LOG.info("开始刷新成员交互摘要(周任务)")
self.service.refresh_all_chatrooms(enable_weekly_digest=True, enable_monthly_digest=False)
self.LOG.info("成员交互摘要刷新完成(周任务)")
if monthly_refresh_time:
@async_job.every_month_last_day_time(monthly_refresh_time)
async def refresh_member_context_monthly_job():
if self.service:
self.LOG.info("开始刷新成员交互摘要(月任务)")
self.service.refresh_all_chatrooms(enable_weekly_digest=False, enable_monthly_digest=True)
self.LOG.info("成员交互摘要刷新完成(月任务)")
self._job_registered = True
self.LOG.debug(f"{self.name} 插件初始化完成")
return True
@@ -95,3 +65,98 @@ class MemberContextPlugin(MessagePluginInterface):
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
return False, None
def get_schedule_actions(self) -> List[Dict[str, Any]]:
"""把成员画像日/周/月刷新动作声明给统一调度中心。"""
schedule_cfg = self._config.get("schedule", {}) or {}
refresh_times = schedule_cfg.get("refresh_times", []) or []
weekly_refresh_time = str(schedule_cfg.get("weekly_refresh_time", "") or "").strip()
monthly_refresh_time = str(schedule_cfg.get("monthly_refresh_time", "") or "").strip()
actions: List[Dict[str, Any]] = []
if refresh_times:
actions.append(
{
"action_key": "daily_refresh",
"name": "成员画像日刷新",
"description": "刷新启用群的成员交互摘要(日任务)",
"trigger_type": "at_times",
"trigger_config": {"time_list": refresh_times},
"target_scope": "all_enabled_groups",
"target_config": {},
"payload": {},
"default_enabled": True,
}
)
if weekly_refresh_time:
actions.append(
{
"action_key": "weekly_refresh",
"name": "成员画像周刷新",
"description": "刷新启用群的成员交互摘要(周任务,含周摘要)",
"trigger_type": "every_week_time",
"trigger_config": {"weekday": 6, "time_str": weekly_refresh_time},
"target_scope": "all_enabled_groups",
"target_config": {},
"payload": {},
"default_enabled": True,
}
)
if monthly_refresh_time:
actions.append(
{
"action_key": "monthly_refresh",
"name": "成员画像月刷新",
"description": "刷新启用群的成员交互摘要(月任务,含月摘要)",
"trigger_type": "every_month_last_day_time",
"trigger_config": {"time_str": monthly_refresh_time},
"target_scope": "all_enabled_groups",
"target_config": {},
"payload": {},
"default_enabled": True,
}
)
return actions
async def run_scheduled_action(self, action_key: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""执行成员画像后台调度动作。"""
if not self.service:
return {"success": False, "summary": "服务未初始化", "detail": {}}
if action_key not in {"daily_refresh", "weekly_refresh", "monthly_refresh"}:
return {"success": False, "summary": f"不支持的动作: {action_key}", "detail": {"action_key": action_key}}
# 兼容“指定群执行”的场景;若未指定则沿用全量刷新逻辑。
target_groups = [str(g).strip() for g in (context.get("target_groups") or []) if str(g).strip()]
enable_weekly = action_key == "weekly_refresh"
enable_monthly = action_key == "monthly_refresh"
try:
if target_groups:
groups = 0
members = 0
skipped = 0
for group_id in target_groups:
result = self.service.refresh_group_contexts(
group_id,
enable_weekly_digest=enable_weekly,
enable_monthly_digest=enable_monthly,
)
if result.get("disabled"):
continue
groups += 1
members += int(result.get("refreshed", 0))
skipped += int(result.get("skipped", 0))
detail = {"groups": groups, "members": members, "skipped": skipped, "targeted": True}
else:
detail = self.service.refresh_all_chatrooms(
enable_weekly_digest=enable_weekly,
enable_monthly_digest=enable_monthly,
)
detail["targeted"] = False
return {
"success": True,
"summary": f"成员画像刷新完成: 群{detail.get('groups', 0)},成员{detail.get('members', 0)}",
"detail": detail,
}
except Exception as e:
return {"success": False, "summary": f"执行异常: {e}", "detail": {"error": str(e)}}