diff --git a/app/agent/__init__.py b/app/agent/__init__.py index 45378647..1f0e1a91 100644 --- a/app/agent/__init__.py +++ b/app/agent/__init__.py @@ -1604,6 +1604,7 @@ class MoviePilotAgent: original_chat_id=self.original_chat_id, title=title, text=message, + save_history=False, ) ) diff --git a/app/agent/callback/__init__.py b/app/agent/callback/__init__.py index d2621249..a091e7fd 100644 --- a/app/agent/callback/__init__.py +++ b/app/agent/callback/__init__.py @@ -536,6 +536,7 @@ class StreamingHandler: original_chat_id=self._original_chat_id, title=self._title, text=current_text, + save_history=False, ), ) if response and response.success and response.message_id: @@ -581,6 +582,7 @@ class StreamingHandler: original_chat_id=self._original_chat_id, title=self._title, text=current_text, + save_history=False, ), ) if response and response.success and response.message_id: diff --git a/app/agent/tools/base.py b/app/agent/tools/base.py index d73a0067..67b1ff4e 100644 --- a/app/agent/tools/base.py +++ b/app/agent/tools/base.py @@ -649,5 +649,6 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta): title=title, text=message, image=image, + save_history=False, ) ) diff --git a/app/agent/tools/impl/send_local_file.py b/app/agent/tools/impl/send_local_file.py index 00c8f4d9..5d6b85f8 100644 --- a/app/agent/tools/impl/send_local_file.py +++ b/app/agent/tools/impl/send_local_file.py @@ -105,6 +105,7 @@ class SendLocalFileTool(MoviePilotTool): text=message, file_path=str(resolved_path), file_name=file_name or resolved_path.name, + save_history=False, ) ) return "本地附件已发送" diff --git a/app/agent/tools/impl/send_message.py b/app/agent/tools/impl/send_message.py index 07ff5612..58822f4d 100644 --- a/app/agent/tools/impl/send_message.py +++ b/app/agent/tools/impl/send_message.py @@ -100,6 +100,7 @@ class SendMessageTool(MoviePilotTool): title=title, text=text, image=image_url, + save_history=False, ) ) self._agent_context["user_reply_sent"] = True diff --git a/app/agent/tools/impl/send_voice_message.py b/app/agent/tools/impl/send_voice_message.py index 482336b0..2ffdaa26 100644 --- a/app/agent/tools/impl/send_voice_message.py +++ b/app/agent/tools/impl/send_voice_message.py @@ -96,6 +96,7 @@ class SendVoiceMessageTool(MoviePilotTool): if voice_path and settings.AUDIO_OUTPUT_INCLUDE_TEXT else None ), + save_history=False, ) ) self._agent_context["user_reply_sent"] = True diff --git a/tests/test_agent_message_routing.py b/tests/test_agent_message_routing.py index 047dce53..c0657501 100644 --- a/tests/test_agent_message_routing.py +++ b/tests/test_agent_message_routing.py @@ -1,17 +1,19 @@ import asyncio from unittest.mock import AsyncMock, Mock, patch +from app.agent import MoviePilotAgent from app.agent.tools.impl.ask_user_choice import ( AskUserChoiceTool, UserChoiceOptionInput, ) +from app.agent.tools.impl.send_message import SendMessageTool from app.chain.message import MessageChain from app.core.config import settings from app.db import SessionFactory from app.db.message_oper import MessageOper from app.db.models.message import Message from app.helper.interaction import AgentInteractionOption, agent_interaction_manager, media_interaction_manager -from app.schemas.types import MessageChannel +from app.schemas.types import MessageChannel, NotificationType def _clear_messages() -> None: @@ -120,6 +122,49 @@ def test_ask_user_choice_message_is_not_recorded_to_message_history(): async_send_message.assert_awaited_once() +def test_agent_final_reply_disables_notification_history(): + """Agent 最终回复发往渠道时不保存通知历史。""" + agent = MoviePilotAgent( + session_id="session-agent-reply", + user_id="10001", + channel=MessageChannel.Telegram.value, + source="telegram-test", + username="tester", + ) + + with patch( + "app.agent.AgentChain.async_post_message", + new_callable=AsyncMock, + ) as async_post_message: + asyncio.run(agent.send_agent_message("已完成处理")) + + notification = async_post_message.await_args.args[0] + assert notification.mtype == NotificationType.Agent + assert notification.save_history is False + + +def test_send_message_tool_disables_notification_history(): + """Agent 主动发消息工具发送的通知不保存通知历史。""" + tool = SendMessageTool(session_id="session-send-message", user_id="10001") + tool.set_message_attr( + channel=MessageChannel.Telegram.value, + source="telegram-test", + username="tester", + ) + tool.set_agent_context(agent_context={}) + + with patch( + "app.agent.tools.base.ToolChain.async_post_message", + new_callable=AsyncMock, + ) as async_post_message: + result = asyncio.run(tool.run(message="处理结果", title="MoviePilot助手")) + + notification = async_post_message.await_args.args[0] + assert result == "消息已发送" + assert notification.text == "处理结果" + assert notification.save_history is False + + def test_agent_choice_callback_is_not_recorded_to_message_history(): """Agent 按钮选择回传不登记到数据库或实时消息队列。""" chain = MessageChain()