Skip to content

feat: 插件权限控制精细到群聊 #1301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zhx8702
Copy link

@zhx8702 zhx8702 commented Apr 17, 2025

修复了 #1281

Motivation

插件开关控制到群聊

Modifications

基于原先PlatformCompatibilityStage 扩展插件对于群聊的控制

前端配置入口设置在对话数据库的列表中,群聊类型有权限按钮。

  1. platform_compatibility 重命名为plugin_compatibility . 处理activated_handlers 调用 is_enabled_for_platform 方法添加 platform_compatible Tag

  2. StarMetadata 新增 group_permissions 字典 缓存插件 黑名单机制

    • 格式为 {group_id: bool},key为群聊ID,value为插件是否启用
  3. 在处理链路中的过滤逻辑:
    activated_handlers 过滤带了platform_compatible 的 tag
    get_handlers_by_event_type 方法:集中过滤特定事件类型的处理器

    • 当指定 platform_idgroup_id 时,会调用 is_enabled_for_platform 方法过滤不兼容的处理器
    • 若只需要激活的插件(only_activated=True),会检查插件的 activated 状态
    • 特殊情况下,OnAstrBotLoadedEvent 事件类型会跳过平台兼容性检查
  4. is_enabled_for_platform 方法 新增group_id参数

    • 传入group_id时 检查插件元数据是否存在、优先检查群聊黑名单,其次检查平台是否在支持列表中,并返回相应的兼容性状态
  5. 当平台配置或群聊插件权限配置更新时:

    • update_plugin_compatibility 方法会遍历所有插件,更新其平台兼容性设置 以及清除group_permissions重新生成插件黑名单
    • 同时会刷新所有处理器的缓存,确保处理器使用最新的兼容性配置

Check

  • 我的 Commit Message 符合良好的规范
  • 我新增/修复/优化的功能经过良好的测试

Sourcery 总结

为群聊实现细粒度的插件权限控制,允许管理员为单个群聊启用或禁用特定插件。

新功能:

  • 在仪表板中添加群组级别的插件权限配置
  • 实现对群组特定插件权限的后端支持
  • 创建一个新的 UI 对话框,用于管理群聊插件权限

增强功能:

  • 重构平台兼容性逻辑以支持群组特定的插件设置
  • 改进插件兼容性缓存机制
  • 为插件权限调试添加更详细的日志记录

杂项:

  • platform_compatibility 重命名为 plugin_compatibility
  • 更新方法名称以反映新的插件权限处理
Original summary in English

Summary by Sourcery

Implement fine-grained plugin permission control for group chats, allowing administrators to enable or disable specific plugins for individual group chats

New Features:

  • Add group-level plugin permission configuration in the dashboard
  • Implement backend support for group-specific plugin permissions
  • Create a new UI dialog for managing group chat plugin permissions

Enhancements:

  • Refactor platform compatibility logic to support group-specific plugin settings
  • Improve plugin compatibility caching mechanism
  • Add more detailed logging for plugin permission debugging

Chores:

  • Rename platform_compatibility to plugin_compatibility
  • Update method names to reflect new plugin permission handling

Copy link

sourcery-ai bot commented Apr 17, 2025

## Sourcery 评审员指南

此 Pull Request 引入了组聊天级别的细粒度插件权限控制。它扩展了现有的平台兼容性设置,以包含特定于组的配置,允许管理员为单个组聊天启用或禁用插件。该实现涉及前端、API 端点、插件元数据和兼容性逻辑的更改。实施了缓存机制来优化权限检查过程。

#### 更新插件兼容性的顺序图

```mermaid
sequenceDiagram
    participant PluginManager
    participant StarMetadata
    participant Config

    PluginManager->>Config: Get plugin_enable_config
    Config-->>PluginManager: Returns plugin_enable_config
    loop For each plugin
        PluginManager->>StarMetadata: update_plugin_compatibility(plugin_enable_config)
        StarMetadata->>StarMetadata: Clear supported_platforms and group_permissions
        StarMetadata->>StarMetadata: Update supported_platforms from plugin_enable_config
        StarMetadata->>StarMetadata: Update group_permissions from config
    end

事件处理期间检查插件权限的顺序图

sequenceDiagram
    participant Event
    participant StarHandlerMetadata
    participant StarMetadata

    Event->>StarHandlerMetadata: is_enabled_for_platform(platform_id, group_id)
    StarHandlerMetadata->>StarMetadata: Get plugin metadata
    alt group_id is present
        StarMetadata->>StarMetadata: Check group_permissions for group_id
        alt group_id in group_permissions
            StarMetadata-->>StarHandlerMetadata: Return group_permissions[group_id]
        else group_id not in group_permissions
            StarMetadata->>StarMetadata: Check supported_platforms for platform_id
            StarMetadata-->>StarHandlerMetadata: Return supported_platforms[platform_id]
        end
    else group_id is not present
        StarMetadata->>StarMetadata: Check supported_platforms for platform_id
        StarMetadata-->>StarHandlerMetadata: Return supported_platforms[platform_id]
    end
Loading

StarMetadata 的更新类图

classDiagram
    class StarMetadata {
        name: str
        desc: str
        version: str
        repo: str
        supported_platforms: Dict[str, bool]
        group_permissions: Dict[str, bool]
        +update_plugin_compatibility(plugin_enable_config: dict) : None
    }
    note for StarMetadata "存储插件元数据,包括特定于组的权限。"
Loading

文件级别更改

变更 详情 文件
引入了一项新功能,可以在群聊级别控制插件权限,从而增强现有的平台兼容性设置。
  • 在前端的对话列表中添加了一个“权限”按钮,供群聊使用,允许管理员为特定群组配置插件权限。
  • 在前端实现了一个用于配置群聊插件权限的对话框,包括选择/取消选择所有插件、切换选择和保存配置的选项。
  • 添加 API 端点以获取和设置群聊的插件权限。
  • 修改插件元数据以包含特定于组的权限,允许为单个群聊启用或禁用插件。
  • 更新插件兼容性逻辑,以在确定是否应为特定事件启用插件时考虑特定于组的权限。
  • 将平台兼容性阶段重构为插件兼容性阶段。
  • 更新插件管理器以更新插件兼容性设置。
dashboard/src/views/ConversationPage.vue
astrbot/dashboard/routes/conversation.py
astrbot/core/star/star.py
astrbot/core/pipeline/process_stage/method/llm_request.py
astrbot/core/star/star_handler.py
astrbot/core/pipeline/plugin_compatibility/stage.py
astrbot/core/pipeline/respond/stage.py
astrbot/core/star/star_manager.py
astrbot/core/pipeline/waking_check/stage.py
astrbot/dashboard/routes/plugin.py
astrbot/core/pipeline/__init__.py
platform_compatibility 重命名为 plugin_compatibility,以反映兼容性设置的更广泛范围,现在包括群聊。
  • 在代码中将 platform_compatibility 字段重命名为 plugin_compatibility
  • 在整个代码库中更新对 platform_compatibility 的引用为 plugin_compatibility
dashboard/src/views/ConversationPage.vue
astrbot/dashboard/routes/conversation.py
astrbot/core/star/star.py
astrbot/core/pipeline/process_stage/method/llm_request.py
astrbot/core/star/star_handler.py
astrbot/core/pipeline/plugin_compatibility/stage.py
astrbot/core/pipeline/respond/stage.py
astrbot/core/star/star_manager.py
astrbot/core/pipeline/waking_check/stage.py
astrbot/dashboard/routes/plugin.py
astrbot/core/pipeline/__init__.py
实施特定于组的插件权限缓存,以优化权限检查过程。
  • group_permissions 字典添加到 StarMetadata 类,以缓存群聊的插件权限。
  • 更新 is_enabled_for_platform 方法以在检查平台级别兼容性之前检查 group_permissions 缓存。
  • 更新 update_plugin_compatibility 方法以在更新插件兼容性设置时清除并重新生成 group_permissions 缓存。
dashboard/src/views/ConversationPage.vue
astrbot/dashboard/routes/conversation.py
astrbot/core/star/star.py
astrbot/core/pipeline/process_stage/method/llm_request.py
astrbot/core/star/star_handler.py
astrbot/core/pipeline/plugin_compatibility/stage.py
astrbot/core/pipeline/respond/stage.py
astrbot/core/star/star_manager.py
astrbot/core/pipeline/waking_check/stage.py
astrbot/dashboard/routes/plugin.py
astrbot/core/pipeline/__init__.py

提示和命令

与 Sourcery 互动

  • 触发新的审查: 在 Pull Request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 通过回复审查评论,要求 Sourcery 从审查评论创建一个 issue。您也可以回复带有 @sourcery-ai issue 的审查评论以从中创建一个 issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在 Pull Request 上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在 Pull Request 上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成评审员指南: 在 Pull Request 上评论 @sourcery-ai guide 以随时(重新)生成评审员指南。
  • 解决所有 Sourcery 评论: 在 Pull Request 上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在 Pull Request 上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想重新开始新的审查,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!
  • 为 issue 生成行动计划: 在 issue 上评论 @sourcery-ai plan 以为其生成行动计划。

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的 Pull Request 摘要、评审员指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获得帮助

```
Original review guide in English

Reviewer's Guide by Sourcery

This pull request introduces fine-grained plugin permission control at the group chat level. It extends the existing platform compatibility settings to include group-specific configurations, allowing administrators to enable or disable plugins for individual group chats. The implementation involves changes to the frontend, API endpoints, plugin metadata, and compatibility logic. Caching mechanisms are implemented to optimize the permission checking process.

Sequence Diagram for Updating Plugin Compatibility

sequenceDiagram
    participant PluginManager
    participant StarMetadata
    participant Config

    PluginManager->>Config: Get plugin_enable_config
    Config-->>PluginManager: Returns plugin_enable_config
    loop For each plugin
        PluginManager->>StarMetadata: update_plugin_compatibility(plugin_enable_config)
        StarMetadata->>StarMetadata: Clear supported_platforms and group_permissions
        StarMetadata->>StarMetadata: Update supported_platforms from plugin_enable_config
        StarMetadata->>StarMetadata: Update group_permissions from config
    end
Loading

Sequence Diagram for Checking Plugin Permissions during Event Processing

sequenceDiagram
    participant Event
    participant StarHandlerMetadata
    participant StarMetadata

    Event->>StarHandlerMetadata: is_enabled_for_platform(platform_id, group_id)
    StarHandlerMetadata->>StarMetadata: Get plugin metadata
    alt group_id is present
        StarMetadata->>StarMetadata: Check group_permissions for group_id
        alt group_id in group_permissions
            StarMetadata-->>StarHandlerMetadata: Return group_permissions[group_id]
        else group_id not in group_permissions
            StarMetadata->>StarMetadata: Check supported_platforms for platform_id
            StarMetadata-->>StarHandlerMetadata: Return supported_platforms[platform_id]
        end
    else group_id is not present
        StarMetadata->>StarMetadata: Check supported_platforms for platform_id
        StarMetadata-->>StarHandlerMetadata: Return supported_platforms[platform_id]
    end
Loading

Updated Class Diagram for StarMetadata

classDiagram
    class StarMetadata {
        name: str
        desc: str
        version: str
        repo: str
        supported_platforms: Dict[str, bool]
        group_permissions: Dict[str, bool]
        +update_plugin_compatibility(plugin_enable_config: dict) : None
    }
    note for StarMetadata "Stores plugin metadata, including group-specific permissions."
Loading

File-Level Changes

Change Details Files
Introduces a new feature to control plugin permissions at the group chat level, enhancing the existing platform compatibility settings.
  • Adds a '权限' (Permissions) button to the conversation list in the frontend for group chats, allowing administrators to configure plugin permissions for specific groups.
  • Implements a dialog in the frontend for configuring plugin permissions for group chats, including options to select/deselect all plugins, toggle selection, and save the configuration.
  • Adds API endpoints to fetch and set plugin permissions for group chats.
  • Modifies the plugin metadata to include group-specific permissions, allowing plugins to be enabled or disabled for individual group chats.
  • Updates the plugin compatibility logic to consider group-specific permissions when determining whether a plugin should be enabled for a particular event.
  • Refactors the platform compatibility stage to plugin compatibility stage.
  • Updates the plugin manager to update plugin compatibility settings.
dashboard/src/views/ConversationPage.vue
astrbot/dashboard/routes/conversation.py
astrbot/core/star/star.py
astrbot/core/pipeline/process_stage/method/llm_request.py
astrbot/core/star/star_handler.py
astrbot/core/pipeline/plugin_compatibility/stage.py
astrbot/core/pipeline/respond/stage.py
astrbot/core/star/star_manager.py
astrbot/core/pipeline/waking_check/stage.py
astrbot/dashboard/routes/plugin.py
astrbot/core/pipeline/__init__.py
Renames platform_compatibility to plugin_compatibility to reflect the broader scope of compatibility settings, which now includes group chats.
  • Renames the platform_compatibility field to plugin_compatibility in the code.
  • Updates references to platform_compatibility to plugin_compatibility throughout the codebase.
dashboard/src/views/ConversationPage.vue
astrbot/dashboard/routes/conversation.py
astrbot/core/star/star.py
astrbot/core/pipeline/process_stage/method/llm_request.py
astrbot/core/star/star_handler.py
astrbot/core/pipeline/plugin_compatibility/stage.py
astrbot/core/pipeline/respond/stage.py
astrbot/core/star/star_manager.py
astrbot/core/pipeline/waking_check/stage.py
astrbot/dashboard/routes/plugin.py
astrbot/core/pipeline/__init__.py
Implements caching of group-specific plugin permissions to optimize the permission checking process.
  • Adds a group_permissions dictionary to the StarMetadata class to cache plugin permissions for group chats.
  • Updates the is_enabled_for_platform method to check the group_permissions cache before checking platform-level compatibility.
  • Updates the update_plugin_compatibility method to clear and regenerate the group_permissions cache when plugin compatibility settings are updated.
dashboard/src/views/ConversationPage.vue
astrbot/dashboard/routes/conversation.py
astrbot/core/star/star.py
astrbot/core/pipeline/process_stage/method/llm_request.py
astrbot/core/star/star_handler.py
astrbot/core/pipeline/plugin_compatibility/stage.py
astrbot/core/pipeline/respond/stage.py
astrbot/core/star/star_manager.py
astrbot/core/pipeline/waking_check/stage.py
astrbot/dashboard/routes/plugin.py
astrbot/core/pipeline/__init__.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhx8702 - 我已经审查了你的更改 - 这里有一些反馈:

总体评论

  • 考虑添加更具体的错误处理和日志记录,以帮助调试权限问题。
  • 前端代码引入了大量新代码 - 考虑将其分解为更小的组件。
以下是我在审查期间查看的内容
  • 🟢 一般问题:一切看起来都很好
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟡 复杂性:发现 1 个问题
  • 🟢 文档:一切看起来都很好

Sourcery 对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @zhx8702 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding more specific error handling and logging to help debug permission issues.
  • The frontend code introduces a lot of new code - consider breaking it down into smaller components.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@zhx8702 zhx8702 force-pushed the feat-add-plugin-permission branch from d3a122f to a34df84 Compare April 18, 2025 01:33
@anka-afk anka-afk linked an issue Apr 19, 2025 that may be closed by this pull request
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]可以增加限制某些群组使用某些功能或插件的吗?
1 participant