- 新增 inq_synonym 表、62组同义词种子与智能体只读接口 - Python 同义词加载/归一化与 1板/2箱 机读 - 同步当前分支既有协作、报价识别、陆运等改动
330 lines
14 KiB
Python
330 lines
14 KiB
Python
"""
|
|
协同群 BOT 口径:@ 才唤醒(解散群除外)、非 @ 文字断开工单关联、附件仍走存档。
|
|
|
|
不连企微 / Redis / 主账;用进程内兜底和 patch。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
|
if ROOT not in sys.path:
|
|
sys.path.insert(0, ROOT)
|
|
|
|
os.environ.setdefault("YTD_ENV", "test")
|
|
os.environ.setdefault("REDIS_BACKEND", "memory")
|
|
os.environ.setdefault("CHECKPOINT_BACKEND", "memory")
|
|
os.environ.setdefault("MESSAGE_STORE_BACKEND", "memory")
|
|
os.environ.setdefault("LLM_DATA_USAGE_CONFIRMED", "false")
|
|
os.environ.setdefault("LLM_ALLOW_NETWORK", "false")
|
|
os.environ.setdefault("LEDGER_BACKEND", "memory")
|
|
|
|
from agent.channel.wecom.models import InboundMessage
|
|
from agent.policy import inquiry_copy as copy
|
|
from agent.redis_coord.group_bot import (
|
|
allow_archive_retry,
|
|
bot_text_handled,
|
|
clear_at_burst,
|
|
clear_active_work_order,
|
|
get_active_work_order,
|
|
in_at_burst,
|
|
is_group_bot,
|
|
mark_at_burst,
|
|
mark_bot_text_handled,
|
|
mark_group_bot,
|
|
)
|
|
from agent.routing.decision import RouteDecision
|
|
from agent.routing.dispatch import dispatch_inbound
|
|
|
|
|
|
class BotTextDedupTests(unittest.TestCase):
|
|
"""同群不同人发同一句不是重复消息(2026-11-23 真机 WO202611230017)。"""
|
|
|
|
def test_same_text_different_sender_not_deduped(self) -> None:
|
|
chat = "wr_dedup_sender_diff"
|
|
mark_bot_text_handled(chat, "WO1 USD11/kg, TH、11月23日", "u_lin")
|
|
self.assertTrue(bot_text_handled(chat, "WO1 USD11/kg, TH、11月23日", "u_lin"))
|
|
# 吴季林再发同一句:指纹不同,必须照常处理,不能被静默吞掉。
|
|
self.assertFalse(bot_text_handled(chat, "WO1 USD11/kg, TH、11月23日", "u_wu"))
|
|
|
|
def test_archive_retry_still_releases_same_sender(self) -> None:
|
|
chat = "wr_dedup_retry_same"
|
|
mark_bot_text_handled(chat, "WO1 成交", "u_lin")
|
|
self.assertTrue(bot_text_handled(chat, "WO1 成交", "u_lin"))
|
|
allow_archive_retry(chat, "WO1 成交", "u_lin")
|
|
self.assertFalse(bot_text_handled(chat, "WO1 成交", "u_lin"))
|
|
|
|
|
|
def _msg(
|
|
*,
|
|
chat_id: str,
|
|
content: str = "",
|
|
msg_type: str = "text",
|
|
source: str = "aibot",
|
|
sender: str = "u1",
|
|
) -> InboundMessage:
|
|
"""构造一条群入站;raw.source 决定走 AIBOT 还是会话存档。"""
|
|
return InboundMessage(
|
|
sender_id=sender,
|
|
message_id=f"mid-{chat_id}-{msg_type}-{len(content)}",
|
|
content=content,
|
|
msg_type=msg_type,
|
|
chat_id=chat_id,
|
|
chat_type="group",
|
|
raw={"source": source},
|
|
)
|
|
|
|
|
|
class GroupBotStateTests(unittest.TestCase):
|
|
def test_mark_seeds_active_then_clear_only_breaks_active(self) -> None:
|
|
chat = "wr_bot_state_1"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
self.assertTrue(is_group_bot(chat))
|
|
self.assertEqual(get_active_work_order(chat), "WO202611190001")
|
|
clear_active_work_order(chat)
|
|
# BOT 接管标记保留;只断开当前单关联。
|
|
self.assertTrue(is_group_bot(chat))
|
|
self.assertEqual(get_active_work_order(chat), "")
|
|
|
|
|
|
class GroupBotDispatchTests(unittest.TestCase):
|
|
def test_non_at_archive_text_breaks_active_and_does_not_reply(self) -> None:
|
|
chat = "wr_bot_dispatch_1"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
with patch("agent.routing.dispatch.handle_group_collab") as handler:
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, content="大家看一下这个", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_unbound_ignore")
|
|
self.assertFalse(handler.called)
|
|
self.assertEqual(get_active_work_order(chat), "")
|
|
|
|
def test_dissolve_without_at_still_goes_to_group_handler(self) -> None:
|
|
"""产品口径:解散群不要求 @BOT;存档听见「解散群」就走解散。"""
|
|
chat = "wr_bot_dispatch_dissolve"
|
|
mark_group_bot(chat, "SEA", "WO202611190099")
|
|
with patch(
|
|
"agent.routing.dispatch.handle_group_collab", return_value="group_dissolved"
|
|
) as handler:
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, content="解散群", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_collab")
|
|
self.assertTrue(handler.called)
|
|
|
|
def test_at_without_active_work_order_sends_one_sentence_prompt(self) -> None:
|
|
chat = "wr_bot_dispatch_2"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
clear_active_work_order(chat)
|
|
sent: list[tuple[str, str]] = []
|
|
|
|
def fake_send(self, *, chat_id, content, mention_userids=None):
|
|
sent.append((chat_id, content))
|
|
return {"ok": True}
|
|
|
|
with patch(
|
|
"agent.channel.aibot.reply.AibotReplyClient.send_group", fake_send
|
|
), patch("agent.routing.dispatch.handle_group_collab") as handler:
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, content="帮我报个价", source="aibot"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_need_work_order")
|
|
self.assertFalse(handler.called)
|
|
self.assertEqual(sent, [(chat, copy.air_need_work_order())])
|
|
# 产品口径:只教一种写法。
|
|
self.assertIn("已成交WO****@", sent[0][1])
|
|
self.assertNotIn("WO****已成交@", sent[0][1])
|
|
|
|
def test_at_with_active_work_order_goes_to_group_handler(self) -> None:
|
|
chat = "wr_bot_dispatch_3"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
with patch(
|
|
"agent.routing.dispatch.handle_group_collab", return_value="group_done"
|
|
) as handler:
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, content="成交", source="aibot"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_collab")
|
|
self.assertTrue(handler.called)
|
|
|
|
def test_at_with_work_order_restores_active_and_goes_to_handler(self) -> None:
|
|
chat = "wr_bot_dispatch_4"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
clear_active_work_order(chat)
|
|
with patch(
|
|
"agent.routing.dispatch.handle_group_collab", return_value="group_done"
|
|
) as handler:
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, content="WO202611190001 成交", source="aibot"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_collab")
|
|
self.assertTrue(handler.called)
|
|
self.assertEqual(get_active_work_order(chat), "WO202611190001")
|
|
|
|
def test_archive_at_text_is_left_to_bot_to_avoid_double_reply(self) -> None:
|
|
chat = "wr_bot_dispatch_5"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
with patch("agent.routing.dispatch.handle_group_collab") as handler:
|
|
result = dispatch_inbound(
|
|
_msg(
|
|
chat_id=chat,
|
|
content="@智报通AIQ 成交",
|
|
source="wshoto_archive",
|
|
),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_at_archive_ignore")
|
|
self.assertFalse(handler.called)
|
|
|
|
def test_archive_file_without_at_window_is_silent(self) -> None:
|
|
"""产品口径(2026-11-21):没有 @BOT 窗口的群附件一律静默,不识别不建单。"""
|
|
chat = "wr_bot_dispatch_6"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
clear_at_burst(chat)
|
|
with patch("agent.routing.dispatch.handle_group_collab") as handler:
|
|
result = dispatch_inbound(
|
|
_msg(
|
|
chat_id=chat,
|
|
content="产品报价.pdf",
|
|
msg_type="file",
|
|
source="wshoto_archive",
|
|
),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_media_no_at_ignore")
|
|
self.assertFalse(handler.called)
|
|
|
|
def test_archive_file_in_at_window_handled_with_bot_client(self) -> None:
|
|
"""@BOT 之后到下一句普通文字之前:附件算这次指令的材料,照旧交给 handler。"""
|
|
chat = "wr_bot_dispatch_6b"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
mark_at_burst(chat)
|
|
captured: dict[str, object] = {}
|
|
|
|
def fake_handler(message):
|
|
from agent.policy.sea_group_ops import _outbound_client
|
|
|
|
captured["client"] = _outbound_client.get()
|
|
return "file_done"
|
|
|
|
with patch("agent.routing.dispatch.handle_group_collab", fake_handler):
|
|
result = dispatch_inbound(
|
|
_msg(
|
|
chat_id=chat,
|
|
content="产品报价.pdf",
|
|
msg_type="file",
|
|
source="wshoto_archive",
|
|
),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_collab")
|
|
self.assertIsNotNone(captured.get("client"))
|
|
|
|
def test_plain_archive_text_closes_at_window(self) -> None:
|
|
"""窗口边界:@ 之后遇到普通文字就结束,后面的附件必须重新 @BOT。"""
|
|
chat = "wr_bot_dispatch_8"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
mark_at_burst(chat)
|
|
self.assertTrue(in_at_burst(chat))
|
|
dispatch_inbound(
|
|
_msg(chat_id=chat, content="这个价格我晚点确认", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertFalse(in_at_burst(chat))
|
|
|
|
def test_archive_image_in_at_window_goes_to_handler(self) -> None:
|
|
"""窗口内图片:交给 handler 按附件口径回应,不再整条吞掉。"""
|
|
chat = "wr_bot_dispatch_9"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
mark_at_burst(chat)
|
|
with patch(
|
|
"agent.routing.dispatch.handle_group_collab", return_value="image_done"
|
|
) as handler:
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, msg_type="image", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_collab")
|
|
self.assertTrue(handler.called)
|
|
|
|
def test_archive_image_without_at_window_is_silent(self) -> None:
|
|
chat = "wr_bot_dispatch_10"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
clear_at_burst(chat)
|
|
with patch("agent.routing.dispatch.handle_group_collab") as handler:
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, msg_type="image", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_archive_media_ignore")
|
|
self.assertFalse(handler.called)
|
|
|
|
def test_file_then_bare_seat_at_replays_attachment(self) -> None:
|
|
"""真机口径(2026-11-21):「先发 Excel、再 @会话存档机器人」也算这次指令。"""
|
|
chat = "wr_bot_dispatch_11"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
clear_at_burst(chat)
|
|
with patch(
|
|
"agent.routing.dispatch.handle_group_collab", return_value="file_done"
|
|
) as handler:
|
|
first = dispatch_inbound(
|
|
_msg(chat_id=chat, msg_type="file", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(first, "group_media_no_at_ignore")
|
|
self.assertFalse(handler.called)
|
|
second = dispatch_inbound(
|
|
_msg(chat_id=chat, content="@会话存档机器人", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
# 附件在 @ 到来时被补办一次;@ 本身只有账号名,不额外回话。
|
|
self.assertEqual(second, "seat_at_empty_ignore")
|
|
self.assertEqual(handler.call_count, 1)
|
|
replayed = handler.call_args[0][0]
|
|
self.assertEqual(replayed.msg_type, "file")
|
|
|
|
def test_pending_attachment_dropped_when_plain_text_ends_burst(self) -> None:
|
|
"""普通文字结束这次指令后,之前没被 @ 认领的附件不能再翻出来重放。"""
|
|
chat = "wr_bot_dispatch_12"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
clear_at_burst(chat)
|
|
with patch("agent.routing.dispatch.handle_group_collab") as handler:
|
|
dispatch_inbound(
|
|
_msg(chat_id=chat, msg_type="file", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
dispatch_inbound(
|
|
_msg(chat_id=chat, content="这个价格我晚点确认", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, content="@会话存档机器人", source="wshoto_archive"),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "seat_at_empty_ignore")
|
|
self.assertFalse(handler.called)
|
|
|
|
def test_app_source_in_bot_group_is_silent(self) -> None:
|
|
chat = "wr_bot_dispatch_7"
|
|
mark_group_bot(chat, "SEA", "WO202611190001")
|
|
with patch("agent.routing.dispatch.handle_group_collab") as handler:
|
|
result = dispatch_inbound(
|
|
_msg(chat_id=chat, content="成交", source=""),
|
|
RouteDecision(intent="ordinary_text_other"),
|
|
)
|
|
self.assertEqual(result, "group_non_bot_ignore")
|
|
self.assertFalse(handler.called)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|