fix(computer): send sandbox image downloads as images#7785
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/tools/computer_tools/fs.py" line_range="743-745" />
<code_context>
+ MessageChain(chain=[message_component])
)
except Exception as e:
logger.error(f"Error sending file message: {e}")
+ return (
+ f"File downloaded successfully to {local_path} "
+ f"but sending to user failed: {e}"
+ )
</code_context>
<issue_to_address>
**🚨 issue (security):** Returning the full local path and raw exception message may expose internal details to the user.
Consider returning a user-friendly error and logging the full path/exception instead, or redacting sensitive parts of the path and exception before including them in the user-visible message.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| logger.error(f"Error sending file message: {e}") | ||
| return ( | ||
| f"File downloaded successfully to {local_path} " |
There was a problem hiding this comment.
🚨 issue (security): Returning the full local path and raw exception message may expose internal details to the user.
Consider returning a user-friendly error and logging the full path/exception instead, or redacting sensitive parts of the path and exception before including them in the user-visible message.
There was a problem hiding this comment.
Code Review
This pull request updates the file system tool to distinguish between images and general files by checking file extensions, ensuring images are sent as image components. It also adds more descriptive error handling when message delivery fails. Feedback was provided to use os.path.splitext for consistency with existing code and to refactor the attachment logic into a shared helper function to avoid potential duplication.
| if Path(local_path).suffix.lower() in _IMAGE_FILE_SUFFIXES: | ||
| message_component = Image.fromFileSystem(local_path) | ||
| sent_as = "image" | ||
| else: | ||
| message_component = File(name=name, file=local_path) | ||
| sent_as = "file" |
There was a problem hiding this comment.
The logic for determining the message component based on the file extension is correct. However, since os.path is already heavily used in this function (e.g., os.path.basename on line 732), you might consider using os.path.splitext(local_path)[1].lower() to avoid creating a Path object just for the suffix check, which would be slightly more efficient and consistent with the surrounding code. Additionally, if this logic for handling attachments is implemented similarly for different cases (e.g., direct vs. quoted attachments), please refactor it into a shared helper function to avoid code duplication.
suffix = os.path.splitext(local_path)[1].lower()
if suffix in _IMAGE_FILE_SUFFIXES:
message_component = Image.fromFileSystem(local_path)
sent_as = "image"
else:
message_component = File(name=name, file=local_path)
sent_as = "file"References
- When implementing similar functionality for different cases (e.g., direct vs. quoted attachments), refactor the logic into a shared helper function to avoid code duplication.
|
补充验证:当前 head |
背景
修复 #7783 中提到的问题:sandbox 内生成图片后,通过
astrbot_download_file下载并发送到 OneBot/NapCat 时,会被File组件发送为文件消息,导致客户端显示为文件而非图片。修改内容
本次修改在
astrbot_download_file的also_send_to_user分支中按本地下载文件后缀选择消息组件:png、jpg、jpeg、gif、bmp、webp使用Image.fromFileSystem()发送。File组件发送。验证
astrbot/core/tools/computer_tools/fs.py。Fixes #7783
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
Adjust sandbox file download behavior so that image files are sent as image messages instead of generic files when also sending to the user, while preserving existing behavior for non-image files and improving error reporting on send failures.
Bug Fixes:
Enhancements: