Skip to content

fix: avoid duplicate quoted image captions#8718

Open
he-yufeng wants to merge 1 commit into
AstrBotDevs:masterfrom
he-yufeng:fix/quote-image-caption-dedupe
Open

fix: avoid duplicate quoted image captions#8718
he-yufeng wants to merge 1 commit into
AstrBotDevs:masterfrom
he-yufeng:fix/quote-image-caption-dedupe

Conversation

@he-yufeng

@he-yufeng he-yufeng commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

修复 #8708

当引用消息里包含图片、且主对话模型不支持图片输入时,构建请求阶段会先把引用图片加入 req.image_urls,随后 _ensure_img_caption() 会统一生成 <image_caption>。之后 _process_quote_message() 又会重新处理同一张引用图片,并生成 [Image Caption in quoted message],导致同一图片被转述两次、也多调用一次图转述模型。

这个改动在已经生成 <image_caption> 的请求里跳过引用图片的第二次转述,只保留 <Quoted Message> 里的引用文本和附件上下文。主模型支持图片输入时的现有跳过逻辑保持不变;没有生成全局图片转述时,引用图片转述 fallback 仍然可用。

验证:

  • python -m py_compile astrbot\core\astr_main_agent.py tests\unit\test_astr_main_agent.py
  • python -m ruff check astrbot\core\astr_main_agent.py tests\unit\test_astr_main_agent.py
  • isolated pytest runner: tests/unit/test_astr_main_agent.py -q88 passed

说明:当前 Python 环境里有另一个 editable checkout 把 AnyCoder/tests 注册成了 tests 包,直接 pytest 会把 AstrBot 的 tests.fixtures 解析到错误位置;因此本地验证使用了隔离 runner,手动把本仓 tests 绑定到当前 worktree,并显式加载 pytest_asyncio.plugin

Summary by Sourcery

Prevent duplicate caption generation for quoted images when a global image caption has already been created in main agent requests.

Bug Fixes:

  • Ensure quoted images are not captioned twice in a single request when global image captioning has already handled them.

Tests:

  • Add a regression test verifying that quoted images are only captioned once and that the quoted-message placeholder caption is omitted when a global caption exists.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a mechanism to prevent duplicate captioning of quoted images. It adds a skip_quote_image_caption flag to _process_quote_message and determines if quote images have already been captioned by checking for the presence of <image_caption> tags in the request's extra user content parts. A corresponding unit test has been added to verify this behavior. There are no review comments, and I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread astrbot/core/astr_main_agent.py Outdated
await _ensure_persona_and_skills(req, cfg, plugin_context, event)

img_cap_prov_id: str = cfg.get("default_image_caption_provider_id") or ""
quote_images_already_captioned = False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这里的quote_images_already_captioned和下面的else处的应当可以提到if前。

Comment thread astrbot/core/astr_main_agent.py Outdated
break

if image_seg and main_provider_supports_image:
if image_seg and skip_quote_image_caption:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

是否可以考虑下image_seg统一判断一下。
目前4个if挨个判断有点...

@he-yufeng he-yufeng force-pushed the fix/quote-image-caption-dedupe branch from 47321e9 to 536c23e Compare June 11, 2026 21:38
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jun 11, 2026
@he-yufeng

Copy link
Copy Markdown
Contributor Author

已在 536c23e7 处理两处 review:

  • quote_images_already_captioned / img_cap_prov_id 初始化提到 req.conversation 分支前,去掉重复的 else 初始化。
  • 将 quoted image 的处理改成先统一判断 image_seg,再在内部区分已 caption、主 provider 支持图片、未配置 caption provider、实际调用 caption provider 四种情况。

本地验证:

python -m py_compile astrbot\core\astr_main_agent.py tests\unit\test_astr_main_agent.py
python -m ruff format --check astrbot\core\astr_main_agent.py tests\unit\test_astr_main_agent.py
python -m ruff check astrbot\core\astr_main_agent.py tests\unit\test_astr_main_agent.py
python -m pytest tests\unit\test_astr_main_agent.py -q
# 88 passed

git diff --check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants