适配记忆注入与全局记忆#3
Conversation
Reviewer's Guide添加全局(GLOBAL)记忆作用域和仅管理员可用的全局记忆存储工具,更新记忆召回/注入行为,更好地利用 AstrBot 的临时用户内容区域并明确记忆语义,同时引入 i18n 元数据以及针对新配置与行为的文档/更新日志。 使用临时用户内容的更新版记忆注入时序图sequenceDiagram
actor User
participant Event as AstrMessageEvent
participant Plugin as SimpleLongMemoryPlugin
participant Request as ProviderRequest
User ->> Event: send message
Event ->> Plugin: inject_memories(event, request)
Plugin ->> Plugin: format_memory_for_injection(memories)
Plugin ->> Plugin: _inject_memory_context(request, safe_memory_context)
Plugin ->> Plugin: _append_extra_user_content(request, content)
alt extra_user_content_parts available
Plugin ->> Request: extra_user_content_parts.append(_make_memory_text_part)
Plugin -->> Plugin: return "extra_user_content_parts"
else no extra_user_content_parts or unsupported
Plugin ->> Plugin: _normalize_contexts(request.contexts)
Plugin ->> Request: set contexts = [memory_msg] + contexts
Plugin -->> Plugin: return "contexts 底部"
end
Plugin -->> Event: continue LLM request with injected memory
仅管理员可用的全局 memory_store_global 工具时序图sequenceDiagram
actor Admin
participant Event as AstrMessageEvent
participant Plugin as SimpleLongMemoryPlugin
participant Manager as MemoryManager
Admin ->> Event: request LLM tool memory_store_global
Event ->> Plugin: tool_store_global(event, content, memory_type, disclosure)
Plugin ->> Plugin: self.config.get(enable_admin_global_memory_tool)
alt global tool disabled
Plugin -->> Event: "Global memory tool is disabled in plugin config"
else enabled
Plugin ->> Event: event.is_admin()
alt not admin
Plugin -->> Event: "Only administrators can store global memories"
else is admin
Plugin ->> Plugin: _sanitize_memory_content(content)
Plugin ->> Plugin: normalize_domain(memory_type)
Plugin ->> Plugin: MemoryURI.generate(domain)
Plugin ->> Manager: store_memory(event, content, domain, uri, memory_type=MemoryType.PERMANENT, disclosure, importance=5, memory_scope=MemoryScope.GLOBAL, visibility="group", subject="global")
Manager -->> Plugin: ok
Plugin -->> Event: "Global memory stored: {uri}"
end
end
文件级变更
Tips and commandsInteracting with Sourcery
Customizing Your Experience访问你的 dashboard 以:
Getting HelpOriginal review guide in EnglishReviewer's GuideAdds global memory scope and admin-only global memory storage tool, updates memory recall/injection behavior to better use AstrBot’s temporary user content area and clarify memory semantics, and introduces i18n metadata plus docs/changelog for the new configuration and behavior. Sequence diagram for updated memory injection with temporary user contentsequenceDiagram
actor User
participant Event as AstrMessageEvent
participant Plugin as SimpleLongMemoryPlugin
participant Request as ProviderRequest
User ->> Event: send message
Event ->> Plugin: inject_memories(event, request)
Plugin ->> Plugin: format_memory_for_injection(memories)
Plugin ->> Plugin: _inject_memory_context(request, safe_memory_context)
Plugin ->> Plugin: _append_extra_user_content(request, content)
alt extra_user_content_parts available
Plugin ->> Request: extra_user_content_parts.append(_make_memory_text_part)
Plugin -->> Plugin: return "extra_user_content_parts"
else no extra_user_content_parts or unsupported
Plugin ->> Plugin: _normalize_contexts(request.contexts)
Plugin ->> Request: set contexts = [memory_msg] + contexts
Plugin -->> Plugin: return "contexts 底部"
end
Plugin -->> Event: continue LLM request with injected memory
Sequence diagram for admin-only global memory_store_global toolsequenceDiagram
actor Admin
participant Event as AstrMessageEvent
participant Plugin as SimpleLongMemoryPlugin
participant Manager as MemoryManager
Admin ->> Event: request LLM tool memory_store_global
Event ->> Plugin: tool_store_global(event, content, memory_type, disclosure)
Plugin ->> Plugin: self.config.get(enable_admin_global_memory_tool)
alt global tool disabled
Plugin -->> Event: "Global memory tool is disabled in plugin config"
else enabled
Plugin ->> Event: event.is_admin()
alt not admin
Plugin -->> Event: "Only administrators can store global memories"
else is admin
Plugin ->> Plugin: _sanitize_memory_content(content)
Plugin ->> Plugin: normalize_domain(memory_type)
Plugin ->> Plugin: MemoryURI.generate(domain)
Plugin ->> Manager: store_memory(event, content, domain, uri, memory_type=MemoryType.PERMANENT, disclosure, importance=5, memory_scope=MemoryScope.GLOBAL, visibility="group", subject="global")
Manager -->> Plugin: ok
Plugin -->> Event: "Global memory stored: {uri}"
end
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - 我发现了 1 个问题,并提供了一些高层次的反馈:
- 在
_inject_memory_context中,返回的字符串 "contexts 底部" 容易产生误导,因为记忆消息实际上是被插入到contexts的最前面;建议返回/记录类似 "contexts 顶部" 的字符串,或者以其他方式明确实际的注入位置,以免在调试时造成困惑。 - 在
_normalize_extracted_scope中,GLOBAL会被立即规范化为PERSONAL,而代码的其他部分却将MemoryScope.GLOBAL视为一等作用域;如果这是有意设计(例如为了避免自动提取到全局记忆),建议要么在函数名/文档字符串中明确体现这一点,要么统一对GLOBAL的处理方式,以避免出现意外的作用域降级。
给 AI Agent 的提示词
Please address the comments from this code review:
## Overall Comments
- In `_inject_memory_context`, the returned string "contexts 底部" is misleading since the memory message is prepended to `contexts`; consider returning/ logging something like "contexts 顶部" or otherwise clarifying the actual injection position to avoid confusion when debugging.
- In `_normalize_extracted_scope`, `GLOBAL` is immediately normalized to `PERSONAL` while other parts of the code treat `MemoryScope.GLOBAL` as a first-class scope; if this is intentional (e.g., to prevent automatic extraction into global memory), it would be helpful to either encode that explicitly in the function name/docstring or handle `GLOBAL` consistently to avoid unexpected scope downgrades.
## Individual Comments
### Comment 1
<location path="main.py" line_range="174-183" />
<code_context>
+ return True
+
+
+def _inject_memory_context(request: ProviderRequest, content: str) -> str:
+ """注入记忆上下文,返回实际使用的注入位置。"""
+ if _append_extra_user_content(request, content):
+ return "extra_user_content_parts"
+
+ contexts = _normalize_contexts(getattr(request, "contexts", None))
+ memory_msg = {"role": "user", "content": content}
+ # 旧版回退:放在 contexts 最前面,使当前 prompt 仍保持最后、优先级更高。
+ request.contexts = [memory_msg] + contexts
+ return "contexts 底部"
+
+
</code_context>
<issue_to_address>
**issue:** 注入目标返回的标签具有误导性/不够准确。
在 `_inject_memory_context` 中,记忆消息被插入到 `contexts` 的前面(`[memory_msg] + contexts`),但函数却返回标签 `"contexts 底部"`,这与实际行为以及注释“放在 contexts 最前面”相矛盾。请重命名该标签(例如改为 `"contexts_top"` / `"contexts 顶部"`),以便日志和调试能够准确反映消息被注入的位置。
</issue_to_address>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据这些反馈改进后续的代码审查。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- In
_inject_memory_context, the returned string "contexts 底部" is misleading since the memory message is prepended tocontexts; consider returning/ logging something like "contexts 顶部" or otherwise clarifying the actual injection position to avoid confusion when debugging. - In
_normalize_extracted_scope,GLOBALis immediately normalized toPERSONALwhile other parts of the code treatMemoryScope.GLOBALas a first-class scope; if this is intentional (e.g., to prevent automatic extraction into global memory), it would be helpful to either encode that explicitly in the function name/docstring or handleGLOBALconsistently to avoid unexpected scope downgrades.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `_inject_memory_context`, the returned string "contexts 底部" is misleading since the memory message is prepended to `contexts`; consider returning/ logging something like "contexts 顶部" or otherwise clarifying the actual injection position to avoid confusion when debugging.
- In `_normalize_extracted_scope`, `GLOBAL` is immediately normalized to `PERSONAL` while other parts of the code treat `MemoryScope.GLOBAL` as a first-class scope; if this is intentional (e.g., to prevent automatic extraction into global memory), it would be helpful to either encode that explicitly in the function name/docstring or handle `GLOBAL` consistently to avoid unexpected scope downgrades.
## Individual Comments
### Comment 1
<location path="main.py" line_range="174-183" />
<code_context>
+ return True
+
+
+def _inject_memory_context(request: ProviderRequest, content: str) -> str:
+ """注入记忆上下文,返回实际使用的注入位置。"""
+ if _append_extra_user_content(request, content):
+ return "extra_user_content_parts"
+
+ contexts = _normalize_contexts(getattr(request, "contexts", None))
+ memory_msg = {"role": "user", "content": content}
+ # 旧版回退:放在 contexts 最前面,使当前 prompt 仍保持最后、优先级更高。
+ request.contexts = [memory_msg] + contexts
+ return "contexts 底部"
+
+
</code_context>
<issue_to_address>
**issue:** The returned label for the injection target is misleading/inaccurate.
In `_inject_memory_context`, the memory message is prepended to `contexts` (`[memory_msg] + contexts`), but the function returns the label `"contexts 底部"`, which contradicts both the behavior and the comment “放在 contexts 最前面”. Please rename this label (e.g. to `"contexts_top"` / `"contexts 顶部"`) so logs and debugging accurately reflect where the message is injected.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request introduces a global memory scope, allowing administrators to store facts and preferences accessible across all chat sessions via the new memory_store_global tool. It also adds internationalization support for English and Chinese and refines the memory injection logic to utilize AstrBot v4.24+ temporary content parts. The injection prompt was updated to better guide the AI on how to treat retrieved memories. Feedback was provided regarding a string mismatch in the fallback injection logic where the return value incorrectly described the injection position as the 'bottom' of the context instead of the 'top'.
Summary by Sourcery
添加全局记忆作用域和仅管理员可用的全局记忆存储,并更新记忆注入逻辑,以更好地集成新版 AstrBot API,并对新行为和配置进行文档说明。
New Features:
GLOBAL记忆作用域,并将其纳入记忆格式化和检索流程。memory_store_globalLLM 工具,该工具受enable_admin_global_memory_tool配置开关控制。Enhancements:
memory_recall(query)。Documentation:
Original summary in English
Summary by Sourcery
Add global memory scope and admin-only global memory storage, and update memory injection to better integrate with newer AstrBot APIs while documenting the new behavior and configuration.
New Features:
memory_store_globalLLM tool gated by theenable_admin_global_memory_toolconfiguration flag.Enhancements:
memory_recall(query)when more context is needed.Documentation: