From ad4d79d2c885982e3deba168fa7566dd0c0a1487 Mon Sep 17 00:00:00 2001 From: OMSociety <85862362+OMSociety@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:47:50 +0800 Subject: [PATCH 1/2] fix: inject group chat context for bare @ mentions --- astrbot/builtin_stars/astrbot/main.py | 13 +++ .../unit/test_empty_mention_group_context.py | 97 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 tests/unit/test_empty_mention_group_context.py diff --git a/astrbot/builtin_stars/astrbot/main.py b/astrbot/builtin_stars/astrbot/main.py index d662142b5b..66f03511d7 100644 --- a/astrbot/builtin_stars/astrbot/main.py +++ b/astrbot/builtin_stars/astrbot/main.py @@ -88,6 +88,19 @@ async def handle_empty_mention(self, event: AstrMessageEvent): ) ) + # This handler stops the event before on_message runs, so a + # bare @ is never recorded. Without a record id on_req_llm + # skips injecting the group messages sent since the bot's + # last reply, so record it here. + if ( + self.group_chat_context + and cfg["provider_ltm_settings"]["group_icl_enable"] + ): + try: + await self.group_chat_context.handle_message(event) + except Exception as e: + logger.error(f"group chat context: {e}") + yield event.request_llm( prompt=( "注意,你正在社交媒体上中与用户进行聊天,用户只是通过@来唤醒你,但并未在这条消息中输入内容,他可能会在接下来一条发送他想发送的内容。" diff --git a/tests/unit/test_empty_mention_group_context.py b/tests/unit/test_empty_mention_group_context.py new file mode 100644 index 0000000000..8d6db9aaa7 --- /dev/null +++ b/tests/unit/test_empty_mention_group_context.py @@ -0,0 +1,97 @@ +from types import SimpleNamespace +from unittest.mock import AsyncMock, MagicMock + +import pytest + +import astrbot.api.message_components as Comp +from astrbot.api.provider import ProviderRequest +from astrbot.builtin_stars.astrbot.group_chat_context import GroupChatContext +from astrbot.builtin_stars.astrbot.main import Main +from astrbot.core.platform.message_type import MessageType + +UMO = "aiocqhttp:GroupMessage:group_456" + + +def _config(*, icl_enable: bool) -> dict: + return { + "wake_prefix": ["/"], + "platform_settings": { + "empty_mention_waiting": True, + "empty_mention_waiting_need_reply": True, + }, + "provider_settings": {"image_caption_prompt": ""}, + "provider_ltm_settings": { + "group_icl_enable": icl_enable, + "image_caption": False, + "active_reply": { + "enable": False, + "method": "possibility_reply", + "possibility_reply": 0.1, + }, + }, + } + + +def _make_event(components: list, nickname: str) -> MagicMock: + extras: dict[str, object] = {} + event = MagicMock() + event.unified_msg_origin = UMO + event.get_self_id.return_value = "bot" + event.get_platform_id.return_value = "aiocqhttp" + event.get_message_type.return_value = MessageType.GROUP_MESSAGE + event.get_messages.return_value = components + event.message_obj = SimpleNamespace( + message=components, + sender=SimpleNamespace(nickname=nickname), + ) + event.get_extra.side_effect = extras.get + event.set_extra.side_effect = extras.__setitem__ + return event + + +def _make_main(icl_enable: bool) -> Main: + main = Main.__new__(Main) + main.context = MagicMock() + main.context.get_config.return_value = _config(icl_enable=icl_enable) + main.context.conversation_manager = SimpleNamespace( + get_curr_conversation_id=AsyncMock(return_value="cid-1"), + get_conversation=AsyncMock(return_value=SimpleNamespace(cid="cid-1")), + ) + main.group_chat_context = GroupChatContext(MagicMock(), main.context) + return main + + +async def _run_empty_mention(main: Main, event: MagicMock) -> None: + """Drive handle_empty_mention up to the LLM request it yields.""" + handler = main.handle_empty_mention(event) + await anext(handler) + await handler.aclose() + + +@pytest.mark.asyncio +async def test_empty_mention_injects_unreplied_group_messages(): + main = _make_main(icl_enable=True) + earlier = _make_event([Comp.Plain("do you like cats?")], "alice") + await main.group_chat_context.handle_message(earlier) + + mention = _make_event([Comp.At(qq="bot", name="bot")], "bob") + await _run_empty_mention(main, mention) + + req = ProviderRequest(prompt="ignored") + await main.group_chat_context.on_req_llm(mention, req) + + injected = "".join(part.text for part in req.extra_user_content_parts) + assert "do you like cats?" in injected + assert "alice" in injected + + +@pytest.mark.asyncio +async def test_empty_mention_does_not_record_when_group_context_disabled(): + main = _make_main(icl_enable=False) + earlier = _make_event([Comp.Plain("do you like cats?")], "alice") + await main.group_chat_context.handle_message(earlier) + + mention = _make_event([Comp.At(qq="bot", name="bot")], "bob") + await _run_empty_mention(main, mention) + + assert mention.get_extra("_group_context_record_id") is None From ecdc72775331dd0df6efd0d8270ca0e374cc0d59 Mon Sep 17 00:00:00 2001 From: OMSociety <85862362+OMSociety@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:00:16 +0800 Subject: [PATCH 2/2] fix: record a bare @ before awaiting conversation state A later group message could reach on_message while this handler waited on the conversation manager; the mention was then recorded after it, so the reply injected that later message and consumed both records. Record first, then await. --- astrbot/builtin_stars/astrbot/main.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/astrbot/builtin_stars/astrbot/main.py b/astrbot/builtin_stars/astrbot/main.py index 66f03511d7..4cbbce1ea4 100644 --- a/astrbot/builtin_stars/astrbot/main.py +++ b/astrbot/builtin_stars/astrbot/main.py @@ -68,6 +68,20 @@ async def handle_empty_mention(self, event: AstrMessageEvent): ) if p_settings.get("empty_mention_waiting_need_reply", True): try: + # This handler stops the event before on_message runs, so a + # bare @ is never recorded. Record it before the awaits below: + # a group message arriving meanwhile would otherwise be + # recorded ahead of the mention and be wrongly included in + # this reply. + if ( + self.group_chat_context + and cfg["provider_ltm_settings"]["group_icl_enable"] + ): + try: + await self.group_chat_context.handle_message(event) + except Exception as e: + logger.error(f"group chat context: {e}") + curr_cid = await self.context.conversation_manager.get_curr_conversation_id( event.unified_msg_origin, ) @@ -88,19 +102,6 @@ async def handle_empty_mention(self, event: AstrMessageEvent): ) ) - # This handler stops the event before on_message runs, so a - # bare @ is never recorded. Without a record id on_req_llm - # skips injecting the group messages sent since the bot's - # last reply, so record it here. - if ( - self.group_chat_context - and cfg["provider_ltm_settings"]["group_icl_enable"] - ): - try: - await self.group_chat_context.handle_message(event) - except Exception as e: - logger.error(f"group chat context: {e}") - yield event.request_llm( prompt=( "注意,你正在社交媒体上中与用户进行聊天,用户只是通过@来唤醒你,但并未在这条消息中输入内容,他可能会在接下来一条发送他想发送的内容。"