Skip to content

Comments

fix: chatui cannot persist file segment#5386

Merged
Soulter merged 1 commit intomasterfrom
fix/chatui-file-1
Feb 23, 2026
Merged

fix: chatui cannot persist file segment#5386
Soulter merged 1 commit intomasterfrom
fix/chatui-file-1

Conversation

@Soulter
Copy link
Member

@Soulter Soulter commented Feb 23, 2026

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ 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.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

由 Sourcery 提供的摘要

在统一的附件目录下持久化存储网页聊天附件,并新增对从旧版图片目录加载附件的向后兼容支持。

Bug 修复:

  • 确保网页聊天中的图片、音频和文件片段被保存到共享附件目录中,以实现正确的持久化存储。
  • 当文件仍存放在旧位置时,允许附件检索回退到旧版图片目录。
Original summary in English

Summary by Sourcery

Persist web chat attachments under a unified attachments directory and add backward-compatible loading of attachments from the legacy image directory.

Bug Fixes:

  • Ensure web chat image, audio, and file segments are saved to the shared attachments directory for proper persistence.
  • Allow attachment retrieval to fall back to the legacy image directory when files are stored in the old location.

@auto-assign auto-assign bot requested review from Raven95676 and anka-afk February 23, 2026 14:02
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 23, 2026
@Soulter Soulter merged commit a56e43d into master Feb 23, 2026
5 checks passed
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Soulter, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where file segments were not persisting correctly within the chat UI. It achieves this by refactoring the internal handling of attachments, centralizing their storage location, and introducing backward compatibility to ensure that previously stored files remain accessible regardless of their original storage path. This change enhances the reliability of attachment persistence across the application.

Highlights

  • Centralized Attachment Storage: The directory for storing various attachment types, previously named imgs_dir, has been renamed to a more generic attachments_dir to better reflect its purpose and accommodate different file segments like images, records, and general files.
  • Backward Compatibility for Attachments: Implemented a mechanism to ensure that existing attachments stored under the old directory structure (imgs_dir) can still be accessed. The system now checks both the new attachments_dir and the legacy_img_dir when retrieving files.
Changelog
  • astrbot/core/platform/sources/webchat/webchat_event.py
    • Renamed the imgs_dir variable to attachments_dir to generalize its use for all attachment types.
    • Updated the directory creation call to use the new attachments_dir.
    • Modified all instances where images, records, and general files were saved to local storage to use the attachments_dir.
  • astrbot/dashboard/routes/chat.py
    • Updated the _create_attachment_from_file method to search for attachment files in a list of candidate paths, including both the new attachments_dir and the legacy_img_dir, to maintain backward compatibility.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 - 我在这里给出了一些整体性的反馈:

  • WebChatMessageEvent 中,建议暂时保留一个兼容性别名或注释,指明旧的 webchat/imgs 路径,这样运维人员可以理解为什么文件现在存储在 attachments 下,以及这对现有部署有何影响。
  • _create_attachment_from_file 中,当找不到某个文件名的候选路径时,记录一条警告日志可能会有所帮助,这样更容易诊断缺失或遗留附件。
给 AI 代理的提示
Please address the comments from this code review:

## Overall Comments
- In `WebChatMessageEvent`, consider keeping a temporary compatibility alias or comment for the old `webchat/imgs` path so operators understand why files are now stored under `attachments` and how this impacts existing deployments.
- In `_create_attachment_from_file`, it may be helpful to log a warning when no candidate path is found for a filename so that missing/legacy attachments can be diagnosed more easily.

Sourcery 对开源项目是免费的——如果你觉得我们的代码审查有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English

Hey - I've left some high level feedback:

  • In WebChatMessageEvent, consider keeping a temporary compatibility alias or comment for the old webchat/imgs path so operators understand why files are now stored under attachments and how this impacts existing deployments.
  • In _create_attachment_from_file, it may be helpful to log a warning when no candidate path is found for a filename so that missing/legacy attachments can be diagnosed more easily.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `WebChatMessageEvent`, consider keeping a temporary compatibility alias or comment for the old `webchat/imgs` path so operators understand why files are now stored under `attachments` and how this impacts existing deployments.
- In `_create_attachment_from_file`, it may be helpful to log a warning when no candidate path is found for a filename so that missing/legacy attachments can be diagnosed more easily.

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.

@dosubot
Copy link

dosubot bot commented Feb 23, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@dosubot dosubot bot added the feature:chatui The bug / feature is about astrbot's chatui, webchat label Feb 23, 2026
astrbot-doc-agent bot pushed a commit to AstrBotDevs/AstrBot-docs that referenced this pull request Feb 23, 2026
@astrbot-doc-agent
Copy link

Generated docs update PR (pending manual review):
AstrBotDevs/AstrBot-docs#136
Trigger: PR merged


AI change summary:

  • 更新 zh/faq.mden/faq.md,新增 WebChat 附件存储路径说明,明确附件统一存储于 data/attachments/ 目录。
  • 更新 zh/dev/star/guides/storage.mden/dev/star/guides/storage.md,新增数据目录结构表格,说明 attachments/ 目录用途及旧版 webchat/imgs/ 目录废弃情况。
  • i18n 状态:中英文文档均已同步更新。

Experimental bot notice:

  • This output is generated by AstrBot-Doc-Agent for review only.
  • It does not represent the final documentation form.

Copy link
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

The pull request addresses a critical bug where chat UI attachments were not persisting correctly. The changes involve migrating attachment storage from a webchat/imgs directory to a unified attachments directory. Additionally, backward compatibility is introduced to allow retrieval of attachments from the old location if they still exist there. This is a good fix that improves the robustness of attachment handling.

from .webchat_queue_mgr import webchat_queue_mgr

imgs_dir = os.path.join(get_astrbot_data_path(), "webchat", "imgs")
attachments_dir = os.path.join(get_astrbot_data_path(), "attachments")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The variable attachments_dir is defined at the module level. While this is acceptable, consider encapsulating it within the WebChatMessageEvent class or a dedicated configuration module if it's meant to be a configurable path. This improves modularity and makes the path's purpose clearer within the class context.

def __init__(self, message_str, message_obj, platform_meta, session_id) -> None:
super().__init__(message_str, message_obj, platform_meta, session_id)
os.makedirs(imgs_dir, exist_ok=True)
os.makedirs(attachments_dir, exist_ok=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The os.makedirs call here ensures the attachments_dir exists. This is good for ensuring the directory is ready before use. However, consider if this operation needs to be performed every time a WebChatMessageEvent is initialized. If the directory is guaranteed to exist after application startup, this could be moved to a single initialization point to avoid redundant calls.

Comment on lines +218 to +223
basename = os.path.basename(filename)
candidate_paths = [
os.path.join(self.attachments_dir, basename),
os.path.join(self.legacy_img_dir, basename),
]
file_path = next((p for p in candidate_paths if os.path.exists(p)), None)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The introduction of candidate_paths and the use of next((p for p in candidate_paths if os.path.exists(p)), None) is a good approach for backward compatibility. This ensures that attachments can still be found even if they are in the legacy directory. This is a robust solution for handling the migration.

@Soulter Soulter deleted the fix/chatui-file-1 branch February 23, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant