Skip to content

fix: pass image inputs through active replies#8119

Open
he-yufeng wants to merge 3 commits into
AstrBotDevs:masterfrom
he-yufeng:fix/active-reply-images
Open

fix: pass image inputs through active replies#8119
he-yufeng wants to merge 3 commits into
AstrBotDevs:masterfrom
he-yufeng:fix/active-reply-images

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

@he-yufeng he-yufeng commented May 9, 2026

Summary

  • collect image components in the active-reply path
  • pass the converted image paths to request_llm() as image_urls
  • keep the existing text prompt behavior unchanged for text messages

Fixes #8085.

To verify

  • python -m py_compile astrbot\builtin_stars\astrbot\main.py tests\unit\test_builtin_astrbot_main.py
  • python -m pytest tests\unit\test_builtin_astrbot_main.py::test_active_reply_passes_image_urls_to_llm -q
  • python -m ruff check astrbot\builtin_stars\astrbot\main.py tests\unit\test_builtin_astrbot_main.py
  • git diff --check

Summary by Sourcery

Handle active reply image components by forwarding their file paths to LLM requests while preserving existing text prompt behavior.

Bug Fixes:

  • Collect image components in active replies and pass their converted file paths to the LLM as image URLs.

Tests:

  • Add a unit test verifying that active replies pass image URLs through to the LLM request.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels May 9, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Catching BaseException around convert_to_file_path() is overly broad and will also swallow interrupts/system-exiting errors; consider catching Exception (or a narrower exception type) instead so truly fatal errors can still propagate.
  • The new image collection loop assumes event.message_obj.message is always an iterable of components; if this is not guaranteed by the event contract, it would be safer to guard that access or handle non-iterable values gracefully.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Catching `BaseException` around `convert_to_file_path()` is overly broad and will also swallow interrupts/system-exiting errors; consider catching `Exception` (or a narrower exception type) instead so truly fatal errors can still propagate.
- The new image collection loop assumes `event.message_obj.message` is always an iterable of components; if this is not guaranteed by the event contract, it would be safer to guard that access or handle non-iterable values gracefully.

## Individual Comments

### Comment 1
<location path="astrbot/builtin_stars/astrbot/main.py" line_range="185-186" />
<code_context>
+                        if isinstance(comp, Image):
+                            try:
+                                image_urls.append(await comp.convert_to_file_path())
+                            except BaseException as e:
+                                logger.error(f"主动回复处理图片失败: {e}")

                     if not conv:
</code_context>
<issue_to_address>
**issue (bug_risk):** Exception handling here is overly broad and loses traceback details.

Catching `BaseException` will also intercept `SystemExit`, `KeyboardInterrupt`, and `asyncio.CancelledError`, which can break normal shutdown and cancellation. Catch `Exception` instead, and use `logger.exception(...)` (without formatting in `e`) so the full traceback is logged:

```python
except Exception:
    logger.exception("主动回复处理图片失败")
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread astrbot/builtin_stars/astrbot/main.py Outdated
Copy link
Copy Markdown
Contributor

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

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 image support for active replies by extracting image URLs from message components and passing them to the LLM request. It also includes a unit test to verify this new functionality. The reviewer recommends catching 'Exception' instead of 'BaseException' to avoid intercepting system-level signals and task cancellations during image processing.

Comment thread astrbot/builtin_stars/astrbot/main.py Outdated
if isinstance(comp, Image):
try:
image_urls.append(await comp.convert_to_file_path())
except BaseException as e:
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.

medium

Catching BaseException is generally discouraged because it includes system-level exceptions such as KeyboardInterrupt and SystemExit. In an asynchronous context, it also catches asyncio.CancelledError, which can interfere with the proper cancellation of tasks (e.g., when the bot is shutting down or a timeout occurs). It is recommended to catch Exception instead to ensure that only standard runtime errors are handled while allowing system signals and task cancellations to propagate correctly.

Suggested change
except BaseException as e:
except Exception as e:
References
  1. When catching exceptions, use 'except Exception:' instead of a bare 'except:' or 'except BaseException:' to avoid catching system-exit exceptions like KeyboardInterrupt and asyncio.CancelledError. (link)

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 主动回复遗漏图片信息传递

1 participant