Skip to content

feat:将A_memorix画像功能接入maisaka检索工具以及自动注入#1666

Merged
A-Dawn merged 1 commit into
Mai-with-u:devfrom
A-Dawn:dev
May 9, 2026
Merged

feat:将A_memorix画像功能接入maisaka检索工具以及自动注入#1666
A-Dawn merged 1 commit into
Mai-with-u:devfrom
A-Dawn:dev

Conversation

@A-Dawn
Copy link
Copy Markdown
Collaborator

@A-Dawn A-Dawn commented May 9, 2026

  • ✅ 接受:与main直接相关的Bug修复:提交到dev分支
  • 新增功能类pr需要经过issue提前讨论,否则不会被合并
  • 🌐 i18n 提醒:除 bootstrap 或紧急修复外,请不要把非 zh-CN 目标翻译作为常规 GitHub 编辑面;常规翻译以 Crowdin -> l10n_* PR 回流为准,详见 docs/i18n.md

请填写以下内容

(删除掉中括号内的空格,并替换为小写的x

    • main 分支 禁止修改,请确认本次提交的分支 不是 main 分支
    • 我确认我阅读了贡献指南
    • 本次更新类型为:BUG修复
    • 本次更新类型为:功能新增
    • 本次更新是否经过测试
    • 如果本次修改涉及 src/A_memorix,我确认已阅读 src/A_memorix/MODIFICATION_POLICY.md,不涉及则无需勾选
  1. 请填写破坏性更新的具体内容(如有):
  2. 请简要说明本次更新的内容和目的:feat:将A_memorix画像功能接入maisaka检索工具以及自动注入

其他信息

  • 关联 Issue:Close #
  • 截图/GIF
  • 附加信息:

Summary by CodeRabbit

发布说明

  • 新功能

    • 新增人物画像查询工具,支持按ID或名称查询特定人物的详细信息
    • 新增人物画像自动注入功能,在对话进行中自动补充相关人物的背景信息
    • 新增人物画像相关配置选项,支持自定义启用状态和注入数量上限
  • 测试

    • 添加人物画像相关的完整测试覆盖

Review Change Stack

Describe:

- 新增 Maisaka 内置 query_person_profile 工具,支持按 person_id 或 person_name 查询 A_memorix 人物画像,并返回结构化摘要。

- 在 a_memorix.integration 配置中增加画像查询、自动注入和注入数量上限开关,同时提升 CONFIG_VERSION。

- Planner 调用前自动收集当前会话相关人物画像,并与 deferred tools 提示一起作为内部参考注入。

- 补充 query_memory 配置路径修正、画像工具、配置字段和自动注入流程测试。
Copilot AI review requested due to automatic review settings May 9, 2026 13:50
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 9, 2026

演练

此 PR 实现了人物画像查询和自动注入功能,包括新的 query_person_profile 内置工具、配置字段、注入构建器和推理引擎集成。

变更

人物画像查询和注入功能

层级 / 文件(s) 摘要
配置架构
src/config/config.py, src/config/official_configs.py
AMemorixIntegrationConfig 中新增三个字段:enable_person_profile_query_toolenable_person_profile_injectionperson_profile_injection_max_profiles(默认值 3,范围 1-5);配置版本从 8.10.15 升至 8.10.16。
查询工具实现
src/maisaka/builtin_tool/query_person_profile.py
实现 query_person_profile 工具:get_tool_spec 声明工具元信息与参数 schema,handle_tool 执行查询流程(验证 person_idperson_name 必须提供其一),调用 memory_service.profile_admin 获取档案,构建包含 summarytraitsperson_id 等的结构化内容,支持失败和成功两种结果返回。
人物画像注入器
src/maisaka/person_profile_injector.py
实现 PersonProfileCandidate 数据类和档案注入构建逻辑:collect_person_profile_candidates 从锚点消息和待处理消息中采集候选人,按优先级和去重排序;build_person_profile_injection_messages 通过 profile_admin 查询每个候选人的档案,跳过空档案,将档案文本截断并格式化为内部引用块,支持配置开关禁用。
工具注册和启用
src/maisaka/builtin_tool/__init__.py
导入 query_person_profile 工具的 spec 和 handler 函数;新增 _get_query_person_profile_tool_spec() 工厂方法根据 global_config.a_memorix.integration.enable_person_profile_query_tool 进行动态启用;在 BUILTIN_TOOL_ENTRIES 中注册工具条目。
推理引擎集成
src/maisaka/reasoning_engine.py
导入 build_person_profile_injection_messages;新增 _build_planner_injected_user_messages 辅助函数,将 deferred_tools_reminder 与从锚点和待处理消息生成的档案注入消息合并;在 Planner 阶段执行时调用此辅助函数向 _run_interruptible_planner 注入完整的消息集合;中断重试路径中设置 cached_messages = interrupted_messages
功能测试
pytests/test_maisaka_builtin_query_person_profile.py, pytests/test_maisaka_person_profile_config.py, pytests/test_maisaka_person_profile_injector.py
添加全面的测试套件:验证查询工具要求提供标识符、支持按 person_idperson_name 查询、返回结构化内容;验证配置字段通过 schema 生成器暴露;验证档案注入的候选人采集(私聊仅当前用户,群聊支持多源提取和去重)、档案查询和格式化、配置开关控制;验证推理引擎保留延迟工具提醒并正确转发源消息。
测试配置迁移
pytests/test_maisaka_builtin_query_memory.py
更新 autouse fixture _patch_maisaka_config,将 global_config 结构从扁平的 SimpleNamespace(memory=...) 改为嵌套的 SimpleNamespace(a_memorix=SimpleNamespace(integration=...)),对齐新的配置架构。

预计代码审查工作量

🎯 3 (中等) | ⏱️ ~25 分钟

可能相关的 PR

  • Mai-with-u/MaiBot#1624:修改 A_Memorix 相关配置形状和测试,迁移到嵌套 a_memorix 配置字段,并递增配置版本。
  • Mai-with-u/MaiBot#1580:添加了 MaiSaka 内存查询工具和配置字段,与此 PR 的新配置结构和内置工具注册相关联。
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.52% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: integrating A_memorix person profile functionality into maisaka retrieval tools and automatic injection.
Description check ✅ Passed The PR description follows the template structure and addresses key items: branch confirmation, contributor guidelines, feature type selection, testing confirmation, and a brief summary of changes. However, the brief description in item 7 is minimal and somewhat duplicates the title.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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: f5663ac6b6

ℹ️ 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".

Comment on lines +43 to +45
def _is_bot_user_id(user_id: str) -> bool:
bot_user_id = _clean_text(getattr(global_config.bot, "qq_account", ""))
return bool(user_id and bot_user_id and user_id == bot_user_id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Use platform-aware bot identity filtering

当前机器人过滤只比较 global_config.bot.qq_account,会把非 QQ 平台(如 Telegram)中的机器人账号当作普通用户候选,从而把机器人画像注入到 Planner,并挤占 max_profiles 名额,导致真实用户画像被跳过。仓库里已有跨平台判定逻辑(src/chat/utils/utils.pyis_bot_self/get_bot_account),这里应按 platform + user_id 过滤而不是仅按 QQ 账号字符串比较。

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/maisaka/person_profile_injector.py (1)

44-45: ⚡ Quick win

已确认类型的配置对象上,建议改为直接属性访问。

这里的 global_config / integration_config 字段是确定结构,继续使用 getattr(..., fallback) 会弱化类型检查并增加不必要回退分支。

♻️ 建议修改
 def _is_bot_user_id(user_id: str) -> bool:
-    bot_user_id = _clean_text(getattr(global_config.bot, "qq_account", ""))
+    bot_user_id = _clean_text(global_config.bot.qq_account)
     return bool(user_id and bot_user_id and user_id == bot_user_id)
@@
     integration_config = global_config.a_memorix.integration
-    if not bool(getattr(integration_config, "enable_person_profile_injection", True)):
+    if not integration_config.enable_person_profile_injection:
         return []
@@
     try:
-        max_profiles = int(getattr(integration_config, "person_profile_injection_max_profiles", 3) or 3)
+        max_profiles = int(integration_config.person_profile_injection_max_profiles)
     except (TypeError, ValueError):
         max_profiles = 3

As per coding guidelines "When a variable/instance type is confirmed (prioritize type annotations unless proven incorrect), you do not need to use or for fallback operations. Minimize use of getattr and setattr methods; only use them for dynamic class handling or pytest Monkeypatch. When refactoring code with these methods, check if the class instance has the attribute and replace with direct property access if it does."

Also applies to: 223-227

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/maisaka/person_profile_injector.py` around lines 44 - 45, Replace getattr
fallback usage with direct attribute access on the known config objects: instead
of _clean_text(getattr(global_config.bot, "qq_account", "")) use the confirmed
property (global_config.bot.qq_account) and pass that into _clean_text, and
similarly replace any getattr/inexact access in the later block (the code around
lines referencing 223-227) to use integration_config.<property> or
global_config.<property> directly; update any null/empty checks to operate on
the cleaned value returned by _clean_text so the equality check (user_id ==
bot_user_id) and truthiness logic remain correct.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/maisaka/person_profile_injector.py`:
- Around line 44-45: Replace getattr fallback usage with direct attribute access
on the known config objects: instead of _clean_text(getattr(global_config.bot,
"qq_account", "")) use the confirmed property (global_config.bot.qq_account) and
pass that into _clean_text, and similarly replace any getattr/inexact access in
the later block (the code around lines referencing 223-227) to use
integration_config.<property> or global_config.<property> directly; update any
null/empty checks to operate on the cleaned value returned by _clean_text so the
equality check (user_id == bot_user_id) and truthiness logic remain correct.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fedbd596-e29a-4552-9b8a-49ab129ff3b2

📥 Commits

Reviewing files that changed from the base of the PR and between 09eeefc and f5663ac.

📒 Files selected for processing (10)
  • pytests/test_maisaka_builtin_query_memory.py
  • pytests/test_maisaka_builtin_query_person_profile.py
  • pytests/test_maisaka_person_profile_config.py
  • pytests/test_maisaka_person_profile_injector.py
  • src/config/config.py
  • src/config/official_configs.py
  • src/maisaka/builtin_tool/__init__.py
  • src/maisaka/builtin_tool/query_person_profile.py
  • src/maisaka/person_profile_injector.py
  • src/maisaka/reasoning_engine.py

Copy link
Copy Markdown

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

该 PR 将 A_memorix 的“人物画像(profile)”能力接入 Maisaka:一方面新增内置检索工具供 Planner 主动查询,另一方面在每轮 Planner 请求前自动注入与当前对话对象相关的画像摘要,辅助模型稳定理解人物背景。

Changes:

  • 新增 query_person_profile 内置工具,并接入 Maisaka 内置工具注册表。
  • 新增人物画像自动注入服务,在 Planner 调用前构造一次性注入消息(含 deferred tools 提示与画像参考块)。
  • 扩展 A_memorix 集成配置项(开关与注入上限),并补充相应 pytest 覆盖。

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/maisaka/reasoning_engine.py 在 Planner 调用前增加一次性注入消息构造流程,并在打断重试时缓存最新消息批次。
src/maisaka/person_profile_injector.py 新增“人物画像自动注入”服务:候选人收集、画像查询与参考块格式化。
src/maisaka/builtin_tool/query_person_profile.py 新增 query_person_profile 内置工具实现与结构化返回。
src/maisaka/builtin_tool/init.py 注册 query_person_profile 工具,并按配置控制启用状态。
src/config/official_configs.py 为 A_memorix 集成新增画像查询开关、自动注入开关与注入上限配置。
src/config/config.py 配置版本号递增以反映新增配置字段。
pytests/test_maisaka_person_profile_injector.py 覆盖候选人收集逻辑、注入内容构造、与 reasoning_engine 注入消息拼接。
pytests/test_maisaka_person_profile_config.py 覆盖新配置字段在 WebUI schema 中的暴露与约束。
pytests/test_maisaka_builtin_query_person_profile.py 覆盖新工具的入参校验、按 person_id/person_name 查询与工具列表启用开关。
pytests/test_maisaka_builtin_query_memory.py 更新测试配置结构以匹配 a_memorix.integration.memory_query_default_limit
Comments suppressed due to low confidence (1)

src/maisaka/reasoning_engine.py:700

  • 这一行日志内容出现明显乱码(疑似文件编码/复制导致的 mojibake),会影响线上排障与可观测性。建议将该提示文本恢复为可读的中文/英文,并检查该文件保存编码是否为 UTF-8。
                            logger.info(
                                f"{self._runtime.log_prefix} 淇濇寔娲昏穬鐘舵€侊紝璺宠繃 Timing Gate 鐩存帴閲嶈瘯 Planner: "
                                f"鍥炲悎={round_index + 2}"
                            )

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +238 to +246
blocks: list[str] = []
for candidate in candidates:
try:
payload = await memory_service.profile_admin(
action="query",
person_id=candidate.person_id,
limit=PROFILE_QUERY_LIMIT,
)
except Exception as exc:
@A-Dawn A-Dawn merged commit 2b3c513 into Mai-with-u:dev May 9, 2026
7 of 8 checks passed
@github-project-automation github-project-automation Bot moved this to 已完成 in MaiM to the GATE May 9, 2026
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.

2 participants