feat: enhance long-term memory with structured chat records #4683
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: