Files
zhenxun_bot/zhenxun/services/help_service.py
T
ce94f63d9a ✨ feat(hook): 增强认证钩子和运行时缓存管理 (#2106)
* ✨ feat(hook): 增强认证钩子和运行时缓存管理
```
♻ refactor(hook): 移除未使用的配置项并优化缓存设置

移除 AUTH_HOOKS_CONCURRENCY_LIMIT 配置项,该配置项不再使用

✨feat(auth_ban): 简化缓存配置并添加实体参数支持

将 BAN_CACHE_TTL 相关配置从动态配置改为常量定义,
移除复杂的 TTL 值转换逻辑,并为 auth_ban 函数添加可选的
entity 参数以支持外部传入实体信息

♻ refactor(auth_limit): 移除未使用的配置依赖

移除 AUTH_LIMIT_NOTICE_CD 配置项,直接使用常量值 2
作为限制通知冷却时间

📦 依赖更新: update playwright dependency to version 1.57.0 in pyproject.toml and requirements.txt
```

* ✨ feat(auth_checker): 增强插件模块处理和预过滤逻辑,支持用户插件兼容性

* ✨ feat(http_utils): 添加内容缓存机制以优化HTTP响应处理

* ✨ feat(cache): 添加群组插件设置视图缓存类型并更新相关逻辑

* ✨ feat(renderer): 优化渲染引擎,增加内存缓存管理和HTML文档处理逻辑

* ✨ feat(sign_in): 添加好感度排行和好感度总排行命令

* ✨ feat(renderer): 增强浏览器实例管理和模板预处理,支持历史 include 语法兼容

* ✨ feat(renderer): 优化 Playwright 环境检查逻辑,增加结果缓存以提高性能

* ✨ feat(renderer): 增强模板文件渲染策略,优化资产加载路径处理

* 🚨 auto fix by pre-commit hooks

* ✨ feat(log): 增加日志内容安全序列化,避免超长 base64 等污染日志
✨ feat(log_sanitizer): 添加对嵌入超长 base64/data URI 的清理功能
✨ feat(auth_checker): 添加 Alconna 快捷方式缓存检查,优化路由匹配逻辑

* feat(renderer): 添加渲染结果内存缓存功能以优化性能
feat(help): 实现帮助菜单图像缓存机制
feat(sign_in): 更新HTML卡片生成以支持动画禁用和剪裁
feat(superuser): 在启动时预热超级用户帮助缓存
feat(theme): 优化主题管理器的资源解析缓存

* 🚨 auto fix by pre-commit hooks

* feat(renderer): 增加全页面视口最大宽度限制并优化内容尺寸计算

---------

Co-authored-by: ATTomatoo <1126160939@qq.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: HibiKier <45528451+HibiKier@users.noreply.github.com>
2026-03-03 15:54:47 +08:00

141 lines
4.5 KiB
Python

from collections import defaultdict
import nonebot
from nonebot.plugin import PluginMetadata
from pydantic import BaseModel
from zhenxun import ui
from zhenxun.configs.config import BotConfig, Config
from zhenxun.models.plugin_info import PluginInfo
from zhenxun.models.task_info import TaskInfo
from zhenxun.services.renderer.result_cache import RenderResultMemoryCache
from zhenxun.ui.models import HelpCategory, HelpItem, PluginHelpPageData
from zhenxun.utils.common_utils import format_usage_for_markdown
from zhenxun.utils.enum import PluginType
_PLUGIN_HELP_IMAGE_CACHE = RenderResultMemoryCache(
ttl_seconds=300,
max_items=48,
max_total_bytes=48 * 1024 * 1024,
)
class PluginData(BaseModel):
plugin: PluginInfo
metadata: PluginMetadata
class Config:
arbitrary_types_allowed = True
async def _get_plugins_by_types(plugin_types: list[PluginType]) -> list[PluginData]:
"""根据指定的插件类型列表获取插件数据"""
plugin_list = await PluginInfo.filter(plugin_type__in=plugin_types).all()
data_list = []
for plugin in plugin_list:
if _plugin := nonebot.get_plugin_by_module_name(plugin.module_path):
if _plugin.metadata:
data_list.append(PluginData(plugin=plugin, metadata=_plugin.metadata))
return data_list
async def _get_task_category() -> dict:
"""获取被动技能帮助类别"""
task_items = []
if task_list := await TaskInfo.all():
task_names = "\n".join([task.name for task in task_list])
task_items.append(
{
"name": "被动技能",
"description": "控制群组中的被动技能状态",
"usage": "通过 开启/关闭群被动 来控制群被动\n"
+ " 示例:开启/关闭群被动早晚安\n 示例:开启/关闭全部群被动"
+ " \n ---------- \n "
+ task_names,
}
)
return {
"title": "被动技能管理",
"icon_svg_path": "M10,20V14H14V20H19V12H22L12,3L2,12H5V20H10Z",
"items": task_items,
}
async def create_plugin_help_image(
plugin_types: list[PluginType], page_title: str
) -> bytes:
"""
一个通用的函数,用于创建插件帮助图片。
参数:
plugin_types: 要包含在帮助中的插件类型列表。
page_title: 生成图片的标题。
返回:
bytes: 生成的图片字节流。
"""
plugins_data = await _get_plugins_by_types(plugin_types)
grouped_plugins = defaultdict(list)
for data in plugins_data:
menu_type = data.plugin.menu_type or "功能"
grouped_plugins[menu_type].append(
HelpItem(
name=data.plugin.name,
description=format_usage_for_markdown(data.metadata.description),
usage=format_usage_for_markdown(data.metadata.usage),
)
)
# 直接构建 HelpCategory 列表
categories = []
for menu_type, items in grouped_plugins.items():
categories.append(
HelpCategory(
title=menu_type,
icon_svg_path="M12,2L15.09,8.26L22,9.27L17,14.14L18.18,21.02L12,17.77L5.82,21.02L7,14.14L2,9.27L8.91,8.26L12,2Z",
items=sorted(items, key=lambda x: x.name),
)
)
task_category_data = await _get_task_category()
if task_category_data["items"]:
task_items = [HelpItem(**item) for item in task_category_data["items"]]
categories.append(
HelpCategory(
title=task_category_data["title"],
icon_svg_path=task_category_data["icon_svg_path"],
items=task_items,
)
)
# 直接实例化 Data Model
page_data = PluginHelpPageData(
bot_nickname=BotConfig.self_nickname,
page_title=page_title,
categories=categories,
)
cache_payload = {
"plugin_types": sorted([plugin_type.value for plugin_type in plugin_types]),
"page_title": page_title,
"theme": Config.get_config("UI", "THEME", "default"),
"page_data": page_data,
}
cache_key = RenderResultMemoryCache.build_key(cache_payload)
if cached_image := await _PLUGIN_HELP_IMAGE_CACHE.get(cache_key):
return cached_image
image_bytes = await ui.render(
page_data,
use_cache=True,
clip_selector=".container",
clip_padding=20,
disable_animations=True,
)
await _PLUGIN_HELP_IMAGE_CACHE.set(cache_key, image_bytes)
return image_bytes