Normalize Telegram HTML send_message responses

This commit is contained in:
jxxghp
2026-06-21 11:39:53 +08:00
parent 5c1b303908
commit d483b805d8
3 changed files with 196 additions and 1 deletions

View File

@@ -578,6 +578,28 @@ class AgentImageSupportTest(unittest.TestCase):
self.assertEqual(payload.parse_mode, "HTML")
def test_send_message_input_normalizes_common_html_tags(self):
payload = SendMessageInput(
explanation="send html notice",
message="<h1>标题</h1><p>第一行<br>第二行</p><ul><li>A</li></ul>",
parse_mode="HTML",
)
self.assertEqual(
payload.message,
"<b>标题</b>\n第一行\n第二行\n• A\n",
)
def test_send_message_input_rejects_unsupported_html_tags(self):
with self.assertRaises(ValueError) as error:
SendMessageInput(
explanation="send html notice",
message="<table><tr><td>A</td></tr></table>",
parse_mode="HTML",
)
self.assertIn("HTML 标签 <table> 不受 Telegram 支持", str(error.exception))
def test_send_message_tool_uses_regular_notification_type(self):
"""发送消息工具应按普通通知消息登记。"""
@@ -613,6 +635,58 @@ class AgentImageSupportTest(unittest.TestCase):
self.assertEqual(notification.image, "https://example.com/poster.png")
self.assertEqual(notification.parse_mode, "HTML")
def test_send_message_tool_marks_reply_sent_after_dispatch(self):
"""发送消息工具成功发送后应终止本轮回复。"""
async def _run():
tool = SendMessageTool(session_id="session-1", user_id="10001")
agent_context = {}
tool.set_agent_context(agent_context)
tool.set_message_attr(
channel=MessageChannel.Telegram.value,
source="telegram-test",
username="tester",
)
with patch(
"app.agent.tools.base.ToolChain.async_post_message",
new_callable=AsyncMock,
):
result = await tool.run(message="<b>处理完成</b>", parse_mode="HTML")
return result, agent_context
result, agent_context = asyncio.run(_run())
self.assertEqual(result, "消息已发送")
self.assertTrue(agent_context["user_reply_sent"])
self.assertEqual(agent_context["reply_mode"], "send_message")
def test_send_message_tool_rejects_unsupported_html_before_dispatch(self):
"""发送消息工具应在进入消息链路前拒绝不支持的 HTML。"""
async def _run():
tool = SendMessageTool(session_id="session-1", user_id="10001")
tool.set_message_attr(
channel=MessageChannel.Telegram.value,
source="telegram-test",
username="tester",
)
with patch(
"app.agent.tools.base.ToolChain.async_post_message",
new_callable=AsyncMock,
) as async_post_message:
result = await tool.run(
message="<table><tr><td>A</td></tr></table>",
parse_mode="HTML",
)
return result, async_post_message
result, async_post_message = asyncio.run(_run())
self.assertIn("HTML 标签 <table> 不受 Telegram 支持", result)
async_post_message.assert_not_awaited()
def test_send_message_tool_rejects_invalid_parse_mode(self):
"""发送消息工具应拒绝不支持的格式类型。"""

View File

@@ -8,6 +8,7 @@ from app.agent.tools.impl.ask_user_choice import (
AskUserChoiceTool,
UserChoiceOptionInput,
)
from app.agent.tools.impl.send_message import SendMessageTool
from app.helper.interaction import (
AgentInteractionOption,
agent_interaction_manager,
@@ -89,6 +90,13 @@ class TestAgentInteraction(unittest.TestCase):
self.assertTrue(tool.return_direct)
self.assertIn("terminal interaction tool", tool.description)
def test_send_message_tool_returns_direct_after_sending_message(self):
"""发送消息工具发出用户可见消息后应结束当前 Agent 轮次。"""
tool = SendMessageTool(session_id="session-1", user_id="10001")
self.assertTrue(tool.return_direct)
self.assertIn("terminal response tool", tool.description)
def test_choice_tool_sends_buttons_and_registers_pending_request(self):
tool = AskUserChoiceTool(session_id="session-1", user_id="10001")
tool.set_message_attr(