Skip to content

feat: 😽对reset在不同情况下的权限特殊处理, 使其兼容alter_cmd 🤠为new指令增加清理上下文选项, 默认为清理, 更符合直觉 #1362

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

Merged
merged 8 commits into from
Apr 23, 2025

Conversation

anka-afk
Copy link
Member

@anka-afk anka-afk commented Apr 21, 2025

修复了 #1360

Motivation

  1. alter_cmd指令无法修改reset指令权限, 这是反直觉的
  2. new指令通常需要搭配reset使用才能清理上下文, 默认情况下应当自动清理

Modifications

  1. 对alter_cmd和reset进行特殊处理, 来针对不同配置情况下设置权限
  2. 对new指令进行修改, 提供一个参数选择是否清理上下文(默认清理)

Check

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

好的,这是翻译成中文的 pull request 总结:

Sourcery 总结

增强 reset 和 new 命令的权限和上下文管理,以改善用户体验并提供更灵活的配置选项。

新功能:

  • 为不同场景(具有/不具有唯一会话的群组、私聊)的 reset 命令添加细粒度的权限配置
  • 为 new 命令引入一个可选的上下文清理参数

增强功能:

  • 改进 reset 命令权限逻辑,以支持更细致的访问控制
  • 通过默认为清理,使 new 命令上下文清理行为更加直观
Original summary in English

Summary by Sourcery

Enhance the permission and context management for reset and new commands to improve user experience and provide more flexible configuration options

New Features:

  • Add granular permission configuration for reset command across different scenarios (group with/without unique session, private chat)
  • Introduce an optional context cleaning parameter for new command

Enhancements:

  • Improve reset command permission logic to support more nuanced access control
  • Make new command context cleaning behavior more intuitive by defaulting to clean

Copy link

sourcery-ai bot commented Apr 21, 2025

## Sourcery 评审指南

此拉取请求引入了对 `reset` 和 `new` 命令的更改。它增强了 `reset` 命令的权限控制,允许基于上下文(具有唯一会话的群聊、没有唯一会话的群聊或私聊)进行更精细的配置。它还修改了 `new` 命令,使其包含自动清除当前对话上下文的选项。

#### reset 命令权限检查的序列图

```mermaid
sequenceDiagram
    participant User
    participant AstrBot
    participant Context

    User->>AstrBot: /reset
    AstrBot->>Context: get_config()
    Context-->>AstrBot: config
    AstrBot->>AstrBot: Determine scene (group_unique_on/off, private)
    AstrBot->>Context: sp.get("alter_cmd", {})
    Context-->>AstrBot: alter_cmd_cfg
    AstrBot->>AstrBot: Get required permission from config
    alt User is not admin and required permission is admin
        AstrBot->>User: Error message (insufficient permissions)
    else User is admin or required permission is member
        AstrBot->>AstrBot: Reset LLM session
        AstrBot->>User: Success message
    end

带有上下文清理的 new 命令的序列图

sequenceDiagram
    participant User
    participant AstrBot
    participant ConversationManager
    participant LTM (Long Term Memory)

    User->>AstrBot: /new [clean: bool]
    AstrBot->>ConversationManager: new_conversation(message.unified_msg_origin)
    ConversationManager-->>AstrBot: cid
    alt clean == True
        AstrBot->>ConversationManager: update_conversation(message.unified_msg_origin, cid, [])
        AstrBot->>LTM: remove_session(event=message)
        LTM-->>AstrBot: Ack
    end
    AstrBot->>User: Switch to new conversation message
Loading

alter_cmd reset scene 的序列图

sequenceDiagram
    participant User
    participant AstrBot
    participant sp (SettingsProvider)

    User->>AstrBot: /alter_cmd reset scene <scene_num> <perm_type>
    AstrBot->>AstrBot: Validate scene_num and perm_type
    alt scene_num or perm_type invalid
        AstrBot->>User: Error message
    else scene_num and perm_type valid
        AstrBot->>sp: get("alter_cmd", {})
        sp-->>AstrBot: alter_cmd_cfg
        AstrBot->>AstrBot: Update reset_cfg with new permission
        AstrBot->>sp: put("alter_cmd", alter_cmd_cfg)
        sp-->>AstrBot: Ack
        AstrBot->>User: Confirmation message
    end
Loading

文件级别更改

变更 详情 文件
增强了 reset 命令的权限控制,允许基于聊天上下文进行配置。
  • 实现了确定聊天上下文的逻辑(具有唯一会话的群组、没有唯一会话的群组、私聊)。
  • 引入了配置选项,用于为每个聊天上下文设置所需的权限级别(管理员或成员)。
  • 修改了 reset 命令,以根据当前聊天上下文的配置权限级别检查用户的角色。
  • 添加了一个 alter_cmd 子命令,用于根据上下文配置重置权限。
packages/astrbot/main.py
修改了 new 命令,使其包含自动清除对话上下文的选项。
  • new 命令添加了一个 clean 参数,默认为 True
  • 实现了在 clean 参数设置为 True 时清除对话上下文的逻辑。
  • 添加了在 clean 参数设置为 True 时从长期记忆中删除会话的逻辑。
packages/astrbot/main.py

提示和命令

与 Sourcery 交互

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

自定义您的体验

访问您的 仪表板 以:

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

获取帮助

```
Original review guide in English

Reviewer's Guide by Sourcery

This pull request introduces changes to the reset and new commands. It enhances the permission control for the reset command, allowing for more granular configuration based on the context (group chat with unique sessions, group chat without unique sessions, or private chat). It also modifies the new command to include an option to automatically clear the context of the current conversation.

Sequence diagram for reset command permission check

sequenceDiagram
    participant User
    participant AstrBot
    participant Context

    User->>AstrBot: /reset
    AstrBot->>Context: get_config()
    Context-->>AstrBot: config
    AstrBot->>AstrBot: Determine scene (group_unique_on/off, private)
    AstrBot->>Context: sp.get("alter_cmd", {})
    Context-->>AstrBot: alter_cmd_cfg
    AstrBot->>AstrBot: Get required permission from config
    alt User is not admin and required permission is admin
        AstrBot->>User: Error message (insufficient permissions)
    else User is admin or required permission is member
        AstrBot->>AstrBot: Reset LLM session
        AstrBot->>User: Success message
    end
Loading

Sequence diagram for new command with context cleaning

sequenceDiagram
    participant User
    participant AstrBot
    participant ConversationManager
    participant LTM (Long Term Memory)

    User->>AstrBot: /new [clean: bool]
    AstrBot->>ConversationManager: new_conversation(message.unified_msg_origin)
    ConversationManager-->>AstrBot: cid
    alt clean == True
        AstrBot->>ConversationManager: update_conversation(message.unified_msg_origin, cid, [])
        AstrBot->>LTM: remove_session(event=message)
        LTM-->>AstrBot: Ack
    end
    AstrBot->>User: Switch to new conversation message
Loading

Sequence diagram for alter_cmd reset scene

sequenceDiagram
    participant User
    participant AstrBot
    participant sp (SettingsProvider)

    User->>AstrBot: /alter_cmd reset scene <scene_num> <perm_type>
    AstrBot->>AstrBot: Validate scene_num and perm_type
    alt scene_num or perm_type invalid
        AstrBot->>User: Error message
    else scene_num and perm_type valid
        AstrBot->>sp: get("alter_cmd", {})
        sp-->>AstrBot: alter_cmd_cfg
        AstrBot->>AstrBot: Update reset_cfg with new permission
        AstrBot->>sp: put("alter_cmd", alter_cmd_cfg)
        sp-->>AstrBot: Ack
        AstrBot->>User: Confirmation message
    end
Loading

File-Level Changes

Change Details Files
Enhanced permission control for the reset command, allowing configuration based on chat context.
  • Implemented logic to determine the chat context (group with unique sessions, group without unique sessions, private chat).
  • Introduced configuration options to set the required permission level (admin or member) for each chat context.
  • Modified the reset command to check the user's role against the configured permission level for the current chat context.
  • Added a subcommand to alter_cmd to configure reset permissions based on the context.
packages/astrbot/main.py
Modified the new command to include an option to automatically clear the conversation context.
  • Added a clean parameter to the new command, defaulting to True.
  • Implemented logic to clear the conversation context when the clean parameter is set to True.
  • Added logic to remove the session from long-term memory when the clean parameter is set to True.
packages/astrbot/main.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.

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

总体评论

  • 考虑在帮助文本中添加有关 reset 命令的配置选项的更详细描述。
  • 用于处理 reset 命令权限的逻辑有点复杂;确保对其进行全面测试以涵盖所有场景。
以下是我在审查期间查看的内容
  • 🟡 一般问题:发现 1 个问题
  • 🟢 安全性:一切看起来都不错
  • 🟢 测试:一切看起来都不错
  • 🟡 复杂性:发现 1 个问题
  • 🟢 文档:一切看起来都不错

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

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

Overall Comments:

  • Consider adding a more detailed description of the configuration options for the reset command in the help text.
  • The logic for handling the reset command's permissions is a bit complex; ensure it's thoroughly tested to cover all scenarios.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 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.

@anka-afk anka-afk linked an issue Apr 21, 2025 that may be closed by this pull request
2 tasks
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request enhances the permission and context management for the reset and new commands. The changes include:

  • Adding granular scene-based configuration for the reset command.
  • Introducing an optional context cleaning parameter for the new conversation command.
  • Extending the alter_cmd command to support detailed reset permissions configuration.
Comments suppressed due to low confidence (1)

packages/astrbot/main.py:1418

  • The docstring for 'scene_key' mentions '场景编号,1-3', but the actual expected values are 'group_unique_on', 'group_unique_off', or 'private'. Update the docstring for clarity.
def update_reset_permission(self, scene_key: str, perm_type: str):

@@ -732,8 +775,13 @@ async def convs(self, message: AstrMessageEvent, page: int = 1):
return

@filter.command("new")
async def new_conv(self, message: AstrMessageEvent):
"""创建新对话"""
async def new_conv(self, message: AstrMessageEvent, clean: bool = True):
Copy link
Member

Choose a reason for hiding this comment

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

我认为不需要这个参数(而且目前来说指令系统无法处理 bool 类型参数),new 指令一定会清理 “记录群员聊天” 产生的上下文,即 await self.ltm.remove_session(event=message)。

Copy link
Member Author

Choose a reason for hiding this comment

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

这么看的话应该不太适合在new清理长期记忆了, 现在的长期记忆没有对应每个对话一个, 原本设计来就是为了跨越多个对话的吗?

Copy link
Member

Choose a reason for hiding this comment

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

我觉得如果用户希望在群聊里 new 对话,那么应该就是打算清理整个对话,重新开始了

Copy link
Member Author

Choose a reason for hiding this comment

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

我觉得如果用户希望在群聊里 new 对话,那么应该就是打算清理整个对话,重新开始了

我直觉上也是这样的, 之前new之后仍然保留前面的对话的bug这么看应该是聊天增强没清除的问题

@Soulter
Copy link
Member

Soulter commented Apr 23, 2025

感觉没问题了

@Soulter Soulter merged commit 7e5461a into AstrBotDevs:master Apr 23, 2025
2 checks passed
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]/reset等权限问题
3 participants