feat: enhance long-term memory with structured chat records#4683
Open
ocetars wants to merge 2 commits intoAstrBotDevs:masterfrom
Open
feat: enhance long-term memory with structured chat records#4683ocetars wants to merge 2 commits intoAstrBotDevs:masterfrom
ocetars wants to merge 2 commits intoAstrBotDevs:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Hey - 我在这里给出一些整体性的反馈:
ChatRecord中的created_at字段使用了没有时区信息的datetime.now().isoformat();建议改为使用显式的 UTC 时间戳(例如datetime.datetime.now(datetime.timezone.utc)),以避免在不同环境中检查或比较记录时产生歧义。_ltm_msg_id这个用于event.get_extra/set_extra的魔法字符串目前是直接内联写死的;建议提升为模块级常量,以避免拼写错误,并让未来查找和修改更加方便。
给 AI 代理的提示
Please address the comments from this code review:
## Overall Comments
- `ChatRecord` 中的 `created_at` 字段使用了没有时区信息的 `datetime.now().isoformat()`;建议改为使用显式的 UTC 时间戳(例如 `datetime.datetime.now(datetime.timezone.utc)`),以避免在不同环境中检查或比较记录时产生歧义。
- `_ltm_msg_id` 这个用于 `event.get_extra`/`set_extra` 的魔法字符串目前是直接内联写死的;建议提升为模块级常量,以避免拼写错误,并让未来查找和修改更加方便。帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据这些反馈改进后续的评审。
Original comment in English
Hey - I've left some high level feedback:
- The
created_atfield inChatRecordusesdatetime.now().isoformat()without timezone info; consider switching to an explicit UTC timestamp (e.g.,datetime.datetime.now(datetime.timezone.utc)) to avoid ambiguity when inspecting or comparing records across environments. - The
_ltm_msg_idmagic string used inevent.get_extra/set_extrais currently inlined; consider lifting it to a module-level constant to avoid typos and make it easier to find and change in the future.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `created_at` field in `ChatRecord` uses `datetime.now().isoformat()` without timezone info; consider switching to an explicit UTC timestamp (e.g., `datetime.datetime.now(datetime.timezone.utc)`) to avoid ambiguity when inspecting or comparing records across environments.
- The `_ltm_msg_id` magic string used in `event.get_extra`/`set_extra` is currently inlined; consider lifting it to a module-level constant to avoid typos and make it easier to find and change in the future.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4427:启用长期记忆后,同一条“当前用户消息”会同时出现在 system(历史)与 user(当前输入)中,导致一次请求内重复注入,影响模型理解且占用 token 。
Modifications / 改动点
This is NOT a breaking change. / 这不是一个破坏性变更。
astrbot/builtin_stars/astrbot/long_term_memory.pyChatRecord(msg_id/role/text/created_at),session_chats存储为ChatRecord列表。_get_event_msg_id(),优先使用event.message_obj.message_id,缺失时使用event.extra缓存生成的 ID,保证同一事件链路一致。on_req_llm()构造聊天历史时,过滤msg_id == current_msg_id的记录,确保当前轮消息只作为 user prompt 发送,不再被重复写入 system 历史。max_cnt截断、图片描述(可选)与 active reply 分支逻辑。Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.Summary by Sourcery
为长期聊天记忆记录建立结构化格式,以避免在单次 LLM 请求中将当前消息同时重复出现在历史记录和用户提示中。
Bug Fixes:
Enhancements:
Original summary in English
Summary by Sourcery
Structure long-term chat memory records to avoid duplicating the current message in both history and user prompt within a single LLM request.
Bug Fixes:
Enhancements: