Skip to content

fix: dispatch OnAfterMessageSentEvent after streaming delivery - #9733

Open
bingchengcc wants to merge 2 commits into
AstrBotDevs:masterfrom
bingchengcc:fix/streaming-after-message-sent-event
Open

fix: dispatch OnAfterMessageSentEvent after streaming delivery#9733
bingchengcc wants to merge 2 commits into
AstrBotDevs:masterfrom
bingchengcc:fix/streaming-after-message-sent-event

Conversation

@bingchengcc

@bingchengcc bingchengcc commented Aug 18, 2026

Copy link
Copy Markdown

The streaming output path in RespondStage early-returns after send_streaming(), skipping the OnAfterMessageSentEvent dispatch that the non-streaming path performs.

This means plugins hooking after_message_sent (e.g. simple_memory _capture) never fire for streaming platforms such as webchat.

Change

Add the same OnAfterMessageSentEvent dispatch after send_streaming() completes, matching the non-streaming path:

             await event.send_streaming(result.async_stream, realtime_segmenting)
+            if await call_event_hook(event, EventType.OnAfterMessageSentEvent):
+                return
+            event.clear_result()
             return

Test plan

  • Webchat: send a message, verify after_message_sent hook fires
  • Non-streaming (QQ): verify behavior unchanged

Summary by Sourcery

Bug Fixes:

  • Dispatch the after-message-sent event after streaming delivery so post-send hooks also run for streaming platforms.

Fixes #9740

@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 Aug 18, 2026

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've left some high level feedback:

  • To keep the streaming and non-streaming paths from diverging again, consider extracting the shared OnAfterMessageSentEvent + clear_result logic into a small helper so both branches call the same code.
  • Double-check that calling event.clear_result() after send_streaming matches the non-streaming path ordering (i.e., hooks can still access any required response data before it is cleared); if not, you may want to clear earlier or pass required data into the hook explicitly.
  • It may be worth wrapping send_streaming and the subsequent hook invocation in a try/finally to guarantee event.clear_result() is called even if streaming or hook execution raises, avoiding potential state leaks between requests.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- To keep the streaming and non-streaming paths from diverging again, consider extracting the shared `OnAfterMessageSentEvent` + `clear_result` logic into a small helper so both branches call the same code.
- Double-check that calling `event.clear_result()` after `send_streaming` matches the non-streaming path ordering (i.e., hooks can still access any required response data before it is cleared); if not, you may want to clear earlier or pass required data into the hook explicitly.
- It may be worth wrapping `send_streaming` and the subsequent hook invocation in a `try`/`finally` to guarantee `event.clear_result()` is called even if streaming or hook execution raises, avoiding potential state leaks between requests.

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 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 Aug 19, 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.

OnAfterMessageSentEvent not dispatched in streaming output path

1 participant