-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: ignore face-attached text when matching wake prefix #9395
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
tangtaizong666
wants to merge
2
commits into
AstrBotDevs:master
Choose a base branch
from
tangtaizong666:fix/9341
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.
+179
−1
Open
Changes from all commits
Commits
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,169 @@ | ||
| """Tests for the wake-prefix decision in WakingCheckStage. | ||
|
|
||
| Regression tests for #9341: mobile QQ yellow-face emojis are serialized by | ||
| some OneBot implementations as a ``face`` segment followed by a text segment | ||
| holding the face's display name (e.g. ``/干饭``). Because ``message_str`` only | ||
| concatenates text segments, that face-attached text used to be mistaken for a | ||
| wake prefix and woke the LLM. | ||
| """ | ||
|
|
||
| import pytest | ||
| import pytest_asyncio | ||
|
|
||
| from astrbot.core.message.components import At, Face, Image, Plain, Reply | ||
| from astrbot.core.pipeline.waking_check import stage as waking_stage | ||
| from astrbot.core.platform.astr_message_event import AstrMessageEvent | ||
| from astrbot.core.platform.astrbot_message import AstrBotMessage, MessageMember | ||
| from astrbot.core.platform.message_type import MessageType | ||
| from astrbot.core.platform.platform_metadata import PlatformMetadata | ||
|
|
||
| SELF_ID = "123456" | ||
|
|
||
|
|
||
| class FakeContext: | ||
| """Minimal PipelineContext substitute for WakingCheckStage.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self.astrbot_config = { | ||
| "platform_settings": {}, | ||
| "admins_id": [], | ||
| "wake_prefix": ["/"], | ||
| "plugin_set": ["*"], | ||
| } | ||
| self.plugin_manager = None | ||
|
|
||
|
|
||
| def make_event( | ||
| components: list, | ||
| message_str: str, | ||
| message_type: MessageType = MessageType.GROUP_MESSAGE, | ||
| ) -> AstrMessageEvent: | ||
| abm = AstrBotMessage() | ||
| abm.type = message_type | ||
| abm.self_id = SELF_ID | ||
| abm.session_id = "test-session" | ||
| abm.message_id = "1" | ||
| abm.sender = MessageMember(user_id="10000", nickname="tester") | ||
| abm.message = components | ||
| abm.message_str = message_str | ||
| abm.raw_message = None | ||
| if message_type == MessageType.GROUP_MESSAGE: | ||
| abm.group_id = "654321" | ||
| meta = PlatformMetadata(name="aiocqhttp", description="test", id="aiocqhttp") | ||
| return AstrMessageEvent( | ||
| message_str=message_str, | ||
| message_obj=abm, | ||
| platform_meta=meta, | ||
| session_id="test-session", | ||
| ) | ||
|
|
||
|
|
||
| @pytest_asyncio.fixture | ||
| async def stage(monkeypatch) -> waking_stage.WakingCheckStage: | ||
| inst = waking_stage.WakingCheckStage() | ||
| await inst.initialize(FakeContext()) | ||
|
|
||
| async def passthrough(event, handlers): | ||
| return handlers | ||
|
|
||
| monkeypatch.setattr( | ||
| waking_stage.star_handlers_registry, | ||
| "get_handlers_by_event_type", | ||
| lambda *args, **kwargs: [], | ||
| ) | ||
| monkeypatch.setattr( | ||
| waking_stage.SessionPluginManager, | ||
| "filter_handlers_by_session", | ||
| passthrough, | ||
| ) | ||
| return inst | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_face_attached_text_does_not_wake(stage): | ||
| """A face segment's auto-attached name text must not count as a prefix.""" | ||
| event = make_event([Face(id=475), Plain("/干饭")], "/干饭") | ||
| await stage.process(event) | ||
| assert not event.is_wake | ||
| assert not event.is_at_or_wake_command | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_reply_then_face_attached_text_does_not_wake(stage): | ||
| """Replying to a non-bot message with a face emoji must not wake either.""" | ||
| event = make_event( | ||
| [Reply(id="42", sender_id="20000"), Face(id=475), Plain("/干饭")], | ||
| "/干饭", | ||
| ) | ||
| await stage.process(event) | ||
| assert not event.is_wake | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_plain_command_still_wakes(stage): | ||
| event = make_event([Plain("/help")], "/help") | ||
| await stage.process(event) | ||
| assert event.is_wake | ||
| assert event.message_str == "help" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_media_with_command_caption_still_wakes(stage): | ||
| """Pasting an image and then typing a command must keep waking the bot.""" | ||
| event = make_event([Image(file="a.png"), Plain("/ocr")], "/ocr") | ||
| await stage.process(event) | ||
| assert event.is_wake | ||
| assert event.message_str == "ocr" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_at_bot_then_command_still_wakes(stage): | ||
| event = make_event([At(qq=SELF_ID), Plain("/help")], "/help") | ||
| await stage.process(event) | ||
| assert event.is_wake | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_face_text_then_at_bot_still_wakes(stage): | ||
| """`[表情] 你好 @bot`: the prefix path skips the face text, yet the At | ||
| branch must still wake the bot.""" | ||
| event = make_event( | ||
| [Face(id=475), Plain("/干饭 你好"), At(qq=SELF_ID)], | ||
| "/干饭 你好", | ||
| ) | ||
| await stage.process(event) | ||
| assert event.is_wake | ||
| assert event.is_at_or_wake_command | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_face_text_then_at_other_does_not_wake(stage): | ||
| """`[表情] 你好 @someone-else`: face-attached text is not a command and | ||
| the At targets another member, so the bot must stay asleep.""" | ||
| event = make_event( | ||
| [Face(id=475), Plain("/干饭 你好"), At(qq="99999")], | ||
| "/干饭 你好", | ||
| ) | ||
| await stage.process(event) | ||
| assert not event.is_wake | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_face_after_command_text_still_wakes(stage): | ||
| """A trailing face emoji after a real command must not block waking.""" | ||
| event = make_event([Plain("/help"), Face(id=475)], "/help") | ||
| await stage.process(event) | ||
| assert event.is_wake | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_private_face_attached_text_needs_no_false_wake(stage): | ||
| """With friend_message_needs_wake_prefix, face text must not satisfy it.""" | ||
| stage.friend_message_needs_wake_prefix = True | ||
| event = make_event( | ||
| [Face(id=475), Plain("/干饭")], | ||
| "/干饭", | ||
| message_type=MessageType.FRIEND_MESSAGE, | ||
| ) | ||
| await stage.process(event) | ||
| assert not event.is_wake |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi,下面的情况是否也会被 break
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不会的。这里的
break只是跳出唤醒词匹配循环,不影响后面的 At 检测分支——该分支会扫描消息链中所有段,只要有 @机器人 / @全体 / 引用机器人消息就照常唤醒。分两种情况:
[QQ表情] 你好 @机器人:唤醒词路径确实会 break(第一个内容段是表情),但随后 At 分支检测到 @机器人,正常唤醒,与改动前行为一致;[QQ表情] 你好 @其他人:不唤醒——这正是 [Bug]qq黄脸表情会触发 / 唤醒词 #9341 要修的场景。前导的/表情名是 OneBot 实现附带的表情文本而非用户输入的指令,改动前这种消息会被/前缀误唤醒。已补两个回归测试覆盖这两种情况(fae7dfe1):
test_face_text_then_at_bot_still_wakes、test_face_text_then_at_other_does_not_wake。后者在未修复代码上会失败(误唤醒),前者验证 At 唤醒不受影响。