feat: add attachment saved event handling in chat and live chat routes#7869
feat: add attachment saved event handling in chat and live chat routes#7869
Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
attachment_savedhandling is implemented separately inchat.pyandlive_chat.pywith very similar logic; consider extracting a shared helper (e.g., to build the payload / SSE string) to keep the event format consistent and reduce duplication. - In
live_chat.py,send_attachment_saved_eventis awaited in the main message loop without any error isolation; consider wrapping the call in a try/except (or handling failures inside the helper) so that a transient send failure for this secondary event does not disrupt the main chat flow.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `attachment_saved` handling is implemented separately in `chat.py` and `live_chat.py` with very similar logic; consider extracting a shared helper (e.g., to build the payload / SSE string) to keep the event format consistent and reduce duplication.
- In `live_chat.py`, `send_attachment_saved_event` is awaited in the main message loop without any error isolation; consider wrapping the call in a try/except (or handling failures inside the helper) so that a transient send failure for this secondary event does not disrupt the main chat flow.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request introduces event notifications for saved attachments in both the chat and live chat routes. It adds helper functions to construct and dispatch attachment_saved events whenever an image, record, file, or video is processed. The reviewer suggests refactoring this logic into a shared helper function to eliminate code duplication between the two modules and recommends adding unit tests to verify the new functionality.
| def build_attachment_saved_event(part: dict | None) -> str | None: | ||
| if not part or not part.get("attachment_id") or not part.get("type"): | ||
| return None | ||
|
|
||
| payload = { | ||
| "type": "attachment_saved", | ||
| "data": { | ||
| "id": part["attachment_id"], | ||
| "type": part["type"], | ||
| }, | ||
| } | ||
| return f"data: {json.dumps(payload, ensure_ascii=False)}\n\n" |
There was a problem hiding this comment.
The logic for build_attachment_saved_event should be refactored into a shared helper function to avoid code duplication across different modules (e.g., chat and live_chat). Furthermore, new functionality for handling attachments must be accompanied by corresponding unit tests.
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.
- New functionality, such as handling attachments, should be accompanied by corresponding unit tests.
| async def send_attachment_saved_event(part: dict | None) -> None: | ||
| if not part or not part.get("attachment_id") or not part.get("type"): | ||
| return | ||
|
|
||
| await self._send_chat_payload( | ||
| session, | ||
| { | ||
| "ct": "chat", | ||
| "type": "attachment_saved", | ||
| "data": { | ||
| "id": part["attachment_id"], | ||
| "type": part["type"], | ||
| }, | ||
| }, | ||
| ) |
There was a problem hiding this comment.
The logic for send_attachment_saved_event should be refactored into a shared helper function to avoid code duplication across the repository. Additionally, ensure that this attachment handling functionality is covered by unit tests.
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.
- New functionality, such as handling attachments, should be accompanied by corresponding unit tests.
#7869) Co-authored-by: Zhilan615 <2864095951@qq.com>
Modifications / 改动点
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
Add real-time notification when attachments are saved in chat and live chat message streams.
New Features: