Skip to content

修复: persona老旧povider逻辑#6492

Open
0d00-Ciallo-0721 wants to merge 3 commits intoAstrBotDevs:masterfrom
0d00-Ciallo-0721:fix-persona
Open

修复: persona老旧povider逻辑#6492
0d00-Ciallo-0721 wants to merge 3 commits intoAstrBotDevs:masterfrom
0d00-Ciallo-0721:fix-persona

Conversation

@0d00-Ciallo-0721
Copy link
Copy Markdown

@0d00-Ciallo-0721 0d00-Ciallo-0721 commented Mar 17, 2026

修复了 `/persona` 指令(如查看详情、切换人格等)失效的问题。之前的代码仍在使用过时的数据源和 Dict 字典获取逻辑,导致无法正确匹配新版本下的人格对象模型。

### Modifications / 改动点

主要修改了核心命令文件 `astrbot/builtin_stars/builtin_commands/commands/persona.py`1. **数据源修正**:将获取人格的逻辑由 `self.context.provider_manager.personas` 更正为使用最新的 `self.context.persona_manager.personas`2. **对象结构适配**:适配了新版 Persona 数据库对象结构,将原先的字典取值逻辑(获取 `name``prompt`)重构为规范的对象属性访问(使用 `p.persona_id``p.system_prompt`)。

**具体代码修改逻辑对比如下:**

```python
# ❌ 修改前 (旧逻辑)
# if persona := next(
#    builtins.filter(
#         lambda persona: persona["name"] == ps,
#         self.context.provider_manager.personas, 
#     ),
#     None,
# ):
#     # ...
#     msg += f"{persona['prompt']}\n"

# ✅ 修改后 (新逻辑)
if persona := next(
    builtins.filter(
        lambda p: p.persona_id == ps, # 替换为对象属性访问
        self.context.persona_manager.personas, # 替换为正确的数据源
    ),
    None,
):
    # ...
    msg += f"{persona.system_prompt}\n" # 替换为对象属性 system_prompt
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

已在本地编写测试脚本进行验证,证明新版逻辑可以直接安全地访问对象属性并读取正确的全量数据。以下是验证成功的日志截图:

[15:19:00.373] [Core] [INFO] [core.event_bus:59]: [default] [webchat(webchat)] feifei/feifei: /test_persona a
[15:19:00.376] [Core] [INFO] [respond.stage:184]: Prepare to send - feifei/feifei: 🔍 **全字段透视测试** (目标: a)
━━━━━━━━━━━━━━━━━━
❌ **老版本格式 (Dict 字典)**
{'prompt': '23tgrgfwefwsdncbhfg', 'name': 'a', 'begin_dialogs': [], 'mood_imitation_dialogs': [], 'tools': None, 'skills': None, '_begin_dialogs_processed': [], '_mood_imitation_dialogs_processed': ''}

✅ **新版本格式 (Persona 数据库对象全字段)**
{
  "updated_at": "2026-03-16 12:11:26.232145",
  "system_prompt": "23tgrgfwefwsdncbhfg",
  "id": "1",
  "tools": "None",
  "folder_id": "None",
  "persona_id": "a",
  "created_at": "2026-03-16 12:11:26.232145",
  "begin_dialogs": "[]",
  "skills": "None",
  "sort_order": "0"
}

[15:19:14.136] [Core] [INFO] [core.event_bus:59]: [default] [webchat(webchat)] feifei/feifei: /verify_persona a
[15:19:14.141] [Core] [INFO] [respond.stage:184]: Prepare to send - feifei/feifei: ✅ **最新逻辑验证成功!** 找到了人格: a
━━━━━━━━━━━━━━━━━━
🎯 **对象属性直接访问测试**:
• persona_id: a
• system_prompt: 23tgrgfwefwsdncbhfg
• tools: None

📦 **完整底层数据读取**:
{
  "updated_at": "2026-03-16 12:11:26.232145",
  "system_prompt": "23tgrgfwefwsdncbhfg",
  "id": "1",
  "tools": "None",
  "folder_id": "None",
  "persona_id": "a",
  "created_at": "2026-03-16 12:11:26.232145",
  "begin_dialogs": "[]",
  "skills": "None",
  "sort_order": "0"
}

---

### Checklist / 检查清单

- [x] 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc. 
  / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

- [x] 👀 My changes have been well-tested, **and "Verification Steps" and "Screenshots" have been provided above**.
  / 我的更改经过了良好的测试,**并已在上方提供了“验证步骤”和“运行截图”**。

- [x] 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in `requirements.txt` and `pyproject.toml`.
  / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 `requirements.txt` 和 `pyproject.toml` 文件相应位置。

- [x] 😮 My changes do not introduce malicious code.
  / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Restore /persona subcommands such as viewing details and switching personas by using the correct persona manager and matching on the current persona object fields instead of outdated dict-based data.

@auto-assign auto-assign bot requested review from Fridemn and Raven95676 March 17, 2026 07:55
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 17, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在修复 /persona 相关指令因使用过时的人格数据源和不兼容的字典访问逻辑而导致的功能失效问题。通过更新数据源和适配新的人格对象结构,确保了指令能够正确地获取和处理人格信息,从而恢复了其正常功能。

Highlights

  • 数据源更新: 更新了 /persona 指令中获取人格的数据源,从 self.context.provider_manager.personas 切换到 self.context.persona_manager.personas,以使用最新的人格管理逻辑。
  • 人格对象结构适配: 将人格对象的访问方式从旧的字典键值对(如 persona["name"]persona["prompt"])更改为新的对象属性访问(如 p.persona_idp.system_prompt),以适应新版本的人格数据库对象模型。
Changelog
  • astrbot/builtin_stars/builtin_commands/commands/persona.py
    • 修复了人格数据源的引用,从 provider_manager 更新为 persona_manager
    • 将人格对象的属性访问从字典键值对 ("name", "prompt") 转换为直接的对象属性 (persona_id, system_prompt)。
Activity
  • 没有检测到此拉取请求的任何评论、审查或活动。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@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.

Hey - I've left some high level feedback:

  • The persona lookup code is duplicated in two branches; consider extracting the next(filter(...), None) logic into a small helper to keep the behavior consistent and easier to maintain if the persona model changes again.
  • Right now the matching uses p.persona_id == ps directly; if user input is not strictly controlled you might want to normalize or validate ps (e.g., trimming/normalizing case or checking allowed IDs) before lookup to avoid subtle mismatches.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The persona lookup code is duplicated in two branches; consider extracting the `next(filter(...), None)` logic into a small helper to keep the behavior consistent and easier to maintain if the persona model changes again.
- Right now the matching uses `p.persona_id == ps` directly; if user input is not strictly controlled you might want to normalize or validate `ps` (e.g., trimming/normalizing case or checking allowed IDs) before lookup to avoid subtle mismatches.

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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 修复了 /persona 指令因数据源和对象模型过时而失效的问题,修改是正确且必要的。代码中存在两处重复的 persona 查找逻辑,我已在代码中提出了具体的重构建议,通过提取辅助方法和使用生成器表达式来优化代码,以提高可读性和可维护性。

Comment on lines 157 to 163
if persona := next(
builtins.filter(
lambda persona: persona["name"] == ps,
self.context.provider_manager.personas,
lambda p: p.persona_id == ps, # 替换字典取值
self.context.persona_manager.personas, # 替换错误的数据源
),
None,
):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

为了提高代码的可读性和简洁性,可以考虑使用生成器表达式来替代 builtins.filterlambda 的组合。另外,这段查找逻辑在当前文件中存在重复(另一处在 189-195 行),建议将其提取到一个私有辅助方法中,以遵循 DRY (Don't Repeat Yourself) 原则,提高代码的可维护性。

            if persona := next((p for p in self.context.persona_manager.personas if p.persona_id == ps), None):

Comment on lines 189 to 195
if persona := next(
builtins.filter(
lambda persona: persona["name"] == ps,
self.context.provider_manager.personas,
lambda p: p.persona_id == ps, # 替换字典取值
self.context.persona_manager.personas, # 替换错误的数据源
),
None,
):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

与 157-163 行的建议类似,这里也可以使用更简洁的生成器表达式来简化代码。将这部分重复的查找逻辑提取到一个共享的辅助方法中会是更好的实践。

            if persona := next((p for p in self.context.persona_manager.personas if p.persona_id == ps), None):

@dosubot dosubot bot added the feature:persona The bug / feature is about astrbot AI persona system (system prompt) label Mar 17, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 699d37a4d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

builtins.filter(
lambda persona: persona["name"] == ps,
self.context.provider_manager.personas,
lambda p: p.persona_id == ps, # 替换字典取值
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Rewrite new inline comments in English

This commit introduces Chinese inline comments, which violates the repository rule in AGENTS.md (“Use English for all new comments”). Keeping non-English comments in command logic reduces maintainability for contributors who rely on English-only code annotations, so these new comments should be translated (or removed) to comply with project standards.

Useful? React with 👍 / 👎.

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 17, 2026
@Soulter Soulter force-pushed the master branch 2 times, most recently from faf411f to 0068960 Compare April 19, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature:persona The bug / feature is about astrbot AI persona system (system prompt) size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant