Repository navigation
fix: inject group chat context for bare @ mentions #11347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OMSociety
wants to merge
2
commits into
AstrBotDevs:master
Choose a base branch
from
OMSociety:fix/empty-mention-group-context
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| from types import SimpleNamespace | ||
|
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 | ||
|
OMSociety marked this conversation as resolved.
Resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.