diff --git a/app/chain/message.py b/app/chain/message.py index 85d96e87..4a19e8e7 100644 --- a/app/chain/message.py +++ b/app/chain/message.py @@ -298,6 +298,20 @@ class MessageChain(ChainBase): ) return bool(processing_status) + if text.lower().startswith("/ai"): + return self._handle_ai_message( + text=text, + channel=channel, + source=source, + userid=userid, + username=username, + original_message_id=original_message_id, + original_chat_id=original_chat_id, + images=images, + files=files, + has_audio_input=has_audio_input, + ) + latest_slash_interaction = self._get_latest_slash_interaction(userid) if latest_slash_interaction == "sites": if SiteChain().handle_text_interaction( @@ -339,20 +353,6 @@ class MessageChain(ChainBase): ): return False - if text.lower().startswith("/ai"): - return self._handle_ai_message( - text=text, - channel=channel, - source=source, - userid=userid, - username=username, - original_message_id=original_message_id, - original_chat_id=original_chat_id, - images=images, - files=files, - has_audio_input=has_audio_input, - ) - if ( settings.AI_AGENT_ENABLE and (settings.AI_AGENT_GLOBAL or images or files or has_audio_input) diff --git a/tests/test_agent_message_routing.py b/tests/test_agent_message_routing.py new file mode 100644 index 00000000..b8f81335 --- /dev/null +++ b/tests/test_agent_message_routing.py @@ -0,0 +1,40 @@ +from unittest.mock import patch + +from app.chain.message import MessageChain +from app.helper.interaction import media_interaction_manager +from app.schemas.types import MessageChannel + + +def test_explicit_ai_message_bypasses_pending_media_interaction(): + """显式 /ai 消息应绕过误触发的媒体交互状态并回到 Agent 会话。""" + chain = MessageChain() + media_interaction_manager.clear() + media_interaction_manager.create_or_replace( + user_id="10001", + channel=MessageChannel.Wechat, + source="wechat-test", + username="tester", + action="Search", + keyword="确认", + title="确认", + ) + + try: + with patch.object(chain, "_record_user_message"), patch( + "app.chain.message.MediaInteractionChain.handle_text_interaction", + return_value=True, + ) as handle_media_interaction, patch.object( + chain, "_handle_ai_message", return_value=True + ) as handle_ai_message: + chain.handle_message( + channel=MessageChannel.Wechat, + source="wechat-test", + userid="10001", + username="tester", + text="/ai 确认", + ) + finally: + media_interaction_manager.clear() + + handle_ai_message.assert_called_once() + handle_media_interaction.assert_not_called()