fix(core): prevent duplicate citation prompt in agent hooks#8416
Open
ZhaiXB wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request modifies astrbot/core/astr_agent_hooks.py to ensure that the citation prompt is only appended to the system prompt if it is not already present. The reviewer suggests adding a newline separator (\n\n) before appending the citation prompt to ensure proper formatting and prevent it from merging directly with the existing prompt text.
Comment on lines
+103
to
+104
| if citation_prompt not in first_part.content: | ||
| first_part.content += citation_prompt |
Contributor
There was a problem hiding this comment.
直接将 citation_prompt 拼接在 first_part.content 后面可能会导致提示词与原系统提示词内容紧贴在一起(例如 ...assistant.Always cite...),缺乏适当的分隔(如换行符),这会影响 Prompt 的结构和 LLM 的理解效果。
建议在拼接前使用 .rstrip() 清理末尾空白,并统一添加 \n\n 作为分隔符,以确保 Prompt 格式整洁。
Suggested change
| if citation_prompt not in first_part.content: | |
| first_part.content += citation_prompt | |
| if citation_prompt not in first_part.content: | |
| first_part.content = first_part.content.rstrip() + "\n\n" + citation_prompt |
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider extracting the
citation_promptstring into a shared constant (e.g., module-level) so it isn’t reallocated and can be reused consistently across hooks or modules that might need the same text. - The idempotency check
if citation_prompt not in first_part.contentcould be made more precise (e.g.,endswithor checking for a sentinel marker) to avoid skipping injection in cases where similar text appears earlier in the system prompt for other reasons.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the `citation_prompt` string into a shared constant (e.g., module-level) so it isn’t reallocated and can be reused consistently across hooks or modules that might need the same text.
- The idempotency check `if citation_prompt not in first_part.content` could be made more precise (e.g., `endswith` or checking for a sentinel marker) to avoid skipping injection in cases where similar text appears earlier in the system prompt for other reasons.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.
修复了在工具(如网页搜索)执行结束时,搜索引用提示词被重复追加到系统提示词(System Prompt)结尾的问题。#8414
问题根源:在
MainAgentHooks.on_tool_end中,代码通过+=直接修改了常驻内存的run_context.messages[0].content对象,且缺乏去重(幂等性)判断。这导致在单次对话中,每当一个搜索工具调用结束,这段引用提示词就会被无条件追加一次。如果模型单轮触发了多次工具调用(例如复杂的天气或资讯查询),System Prompt 结尾就会出现严重的文本堆叠和重复。Modifications / 改动点
修改文件:
astrbot/core/agent/hooks.py(或相关 Agent Hooks 核心文件)在
MainAgentHooks.on_tool_end的提示词注入逻辑中,将原本直接+=的操作改为先进行字符串包含判断(if citation_prompt not in first_part.content:)。确保了无论单次交互中调用多少次搜索工具,该段引用提示词在系统上下文中永远只保留一份。
This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Bug Fixes: