Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions astrbot/builtin_stars/astrbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
):
Comment thread
OMSociety marked this conversation as resolved.
Resolved
try:
await self.group_chat_context.handle_message(event)
Comment thread
OMSociety marked this conversation as resolved.
Resolved
Comment thread
OMSociety marked this conversation as resolved.
Resolved
Comment thread
OMSociety marked this conversation as resolved.
Resolved
Comment thread
OMSociety marked this conversation as resolved.
Resolved
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,
)
Expand Down
97 changes: 97 additions & 0 deletions tests/unit/test_empty_mention_group_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from types import SimpleNamespace
Comment thread
OMSociety marked this conversation as resolved.
Resolved
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
Comment thread
OMSociety marked this conversation as resolved.
Resolved
Loading