Skip to content

feat: add attachment saved event handling in chat and live chat routes#7869

Merged
Soulter merged 1 commit intomasterfrom
feat/chat-api-attachment
Apr 28, 2026
Merged

feat: add attachment saved event handling in chat and live chat routes#7869
Soulter merged 1 commit intomasterfrom
feat/chat-api-attachment

Conversation

@Soulter
Copy link
Copy Markdown
Member

@Soulter Soulter commented Apr 28, 2026

Modifications / 改动点

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

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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.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:

  • Emit an attachment_saved event in chat streaming responses whenever an image, record, file, or video attachment is created.
  • Send an attachment_saved chat payload over live chat sessions when new attachments are stored.

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 28, 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 left some high level feedback:

  • 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.
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.

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 dosubot Bot added the area:webui The bug / feature is about webui(dashboard) of astrbot. label Apr 28, 2026
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 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.

Comment on lines +822 to +833
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"
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

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
  1. 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.
  2. New functionality, such as handling attachments, should be accompanied by corresponding unit tests.

Comment on lines +540 to +554
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"],
},
},
)
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

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
  1. 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.
  2. New functionality, such as handling attachments, should be accompanied by corresponding unit tests.

@Soulter Soulter merged commit d8de003 into master Apr 28, 2026
20 of 21 checks passed
@Soulter Soulter deleted the feat/chat-api-attachment branch April 28, 2026 09:14
LIghtJUNction pushed a commit that referenced this pull request Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:webui The bug / feature is about webui(dashboard) of astrbot. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant