checkpoint: 提交当前工作区代码(同义词配置初版 + 既有流程改动)

- 新增 inq_synonym 表、62组同义词种子与智能体只读接口
- Python 同义词加载/归一化与 1板/2箱 机读
- 同步当前分支既有协作、报价识别、陆运等改动
This commit is contained in:
linmianxun
2026-11-24 16:26:49 +08:00
parent d9b2029c47
commit 04e00f0cfc
69 changed files with 7126 additions and 395 deletions
+389 -7
View File
@@ -373,11 +373,14 @@ class FlowTests(unittest.TestCase):
def setUp(self) -> None:
self.ledger = MemoryLedger()
self.replies: list[str] = []
self.payloads: list[dict] = []
self.flow = MultiTextInquiryFlow(self.ledger, group_client=_Group())
def reply(self, text: str, extra: dict | None = None) -> str:
_ = extra
# 出站带 payload 时正文只是卡发不出去时的兜底;仍收进 replies 保持旧断言可用。
self.replies.append(text)
if extra:
self.payloads.append(dict(extra))
return "ok"
def last_prompt(self) -> str:
@@ -455,7 +458,8 @@ class FlowTests(unittest.TestCase):
reply=self.reply,
chat_fn=chat_fn,
)
card = self.last_prompt()
# 陆运段改出卡:文字摘要(海运缺口)与卡的兜底正文是两条出站。
card = "\n".join(self.replies)
self.assertEqual(phase, "clarify")
self.assertIn("业务类型(必填):", card)
self.assertIn("纽约港", card)
@@ -609,11 +613,12 @@ class FlowTests(unittest.TestCase):
text="段1:陆运\n运输类型:国内运输拼车\n段2:海运\n起运港:上海港\n目的港:汉堡",
reply=self.reply,
)
joined = "\n".join(self.replies)
self.assertNotEqual(phase, "wait_manual_group")
self.assertNotIn(old_wo, self.last_prompt())
self.assertNotIn("请手动拉相关人员和智能体", self.last_prompt())
self.assertIn("陆运", self.last_prompt())
self.assertIn("海运", self.last_prompt())
self.assertNotIn(old_wo, joined)
self.assertNotIn("请手动拉相关人员和智能体", joined)
self.assertIn("陆运", joined)
self.assertIn("海运", joined)
def test_plain_text_after_manual_group_still_hints(self) -> None:
"""不是新询价时,含空运工单仍提示去原有群发工单号。"""
@@ -635,6 +640,277 @@ class FlowTests(unittest.TestCase):
self.assertEqual(phase, "reject_same_mode")
self.assertEqual(self.ledger._tickets, {})
def test_land_options_are_asked_by_card(self) -> None:
"""多段陆运缺业务类型/报价场景:出卡,复制清单只作卡发不出去时的兜底。"""
phase = self.flow.on_text(
sender_id="sales", text=SEA_LAND_OPTIONS_MISSING, reply=self.reply
)
self.assertEqual(phase, "clarify")
self.assertTrue(self.payloads)
card = self.payloads[-1]["template_card"]
self.assertEqual(card["card_type"], "multiple_interaction")
self.assertIn("multi_land_options", card["task_id"])
self.assertEqual(card["main_title"]["title"], "段2:陆运")
self.assertEqual(
[q["question_key"] for q in card["select_list"]],
["biz_type", "quote_scenario"],
)
# 只有陆运段一个缺口:不再多发一条文字摘要,卡片本身就是这条补问。
self.assertEqual(len(self.replies), 1)
# 卡发不出去时正文仍是完整复制清单。
self.assertIn("Step1: 请复制以下内容", self.replies[-1])
def test_land_options_card_keeps_other_segment_summary(self) -> None:
"""海运也缺字段时先发文字摘要(陆运段只留点卡提示),再发卡。"""
text = (
"段1:海运\n起运港:上海\n目的港:巴生西港\n品名:机器设备\n"
"段2:陆运\n始发站:广州\n目的地:深圳\n货物品名:衣服\n"
"体积:100CBM\n毛重:50kg"
)
phase = self.flow.on_text(sender_id="sales", text=text, reply=self.reply)
self.assertEqual(phase, "clarify")
self.assertTrue(self.payloads)
self.assertGreaterEqual(len(self.replies), 2)
summary = self.replies[0]
self.assertIn("段1:海运", summary)
self.assertIn(copy.MULTI_LAND_OPTIONS_ON_CARD, summary)
self.assertNotIn("Step1: 请复制以下内容", summary)
def test_land_options_card_submit_writes_land_segment(self) -> None:
"""点提交后两项写回陆运段,并把流程往前推,不是另开一单。"""
from agent.schema import land_option_card as loc
self.flow.on_text(
sender_id="sales", text=SEA_LAND_OPTIONS_MISSING, reply=self.reply
)
card = self.payloads[-1]["template_card"]
phase = self.flow.on_button(
sender_id="sales",
action=copy.BTN_LAND_OPTIONS,
reply=self.reply,
card_meta={
"task_id": card["task_id"],
"event_key": card["submit_button"]["key"],
"selected_items": {
loc.QUESTION_BIZ: ["dom_ltl"],
loc.QUESTION_SCENARIO: ["common"],
},
},
)
self.assertIn(phase, {"clarify", "wait_confirm"})
land = next(
seg
for seg in self.flow._segments(self.flow.session_of("sales"))
if seg.mode == "LAND"
)
self.assertEqual(land.facts.get("运输类型"), "国内运输拼车")
self.assertEqual(land.facts.get("线路类别"), "国内长途/零担")
self.assertEqual(land.facts.get("运输分类"), "普件(常用线路)")
# 始发站在工单内部键是起运港,提交卡片不能把这一项冲掉。
self.assertEqual(land.facts.get("起运港"), "广州")
def test_sea_fields_reply_continues_multi(self) -> None:
"""海运复制清单回填后仍走多段,不能丢草稿去反问运输方式。"""
phase = self.flow.on_text(
sender_id="sales", text=SEA_LAND_BOTH_MISSING, reply=self.reply
)
self.assertEqual(phase, "clarify")
self.replies.clear()
self.payloads.clear()
phase = self.flow.on_text(
sender_id="sales",
text="3.贸易条款(必填):门到门\n4.运输分类(必填):其他",
reply=self.reply,
)
self.assertNotEqual(phase, "need_mode")
self.assertNotIn("告知我运输方式", "\n".join(self.replies))
sea = next(
seg
for seg in self.flow._segments(self.flow.session_of("sales"))
if seg.mode == "SEA"
)
self.assertEqual(sea.facts.get("贸易条款"), "门到门")
self.assertEqual(sea.facts.get("运输分类"), "其他")
def test_screenshot_sea_then_land_backfill_stays_multi(self) -> None:
"""正服截图顺序:先填海运清单,再填陆运清单,两句都不能被单段接走。"""
phase = self.flow.on_text(
sender_id="sales", text=SEA_LAND_BOTH_MISSING, reply=self.reply
)
self.assertEqual(phase, "clarify")
self.replies.clear()
self.payloads.clear()
phase = self.flow.on_text(
sender_id="sales",
text=(
"2.箱型箱量(必填):1x40HQ\n"
"3.贸易条款(必填):门到门\n"
"4.运输分类(必填):其他"
),
reply=self.reply,
)
self.assertNotEqual(phase, "need_mode")
self.assertNotIn("告知我运输方式", "\n".join(self.replies))
self.replies.clear()
self.payloads.clear()
phase = self.flow.on_text(
sender_id="sales",
text=(
"1.车型/数量(必填):8T\n"
"(如:9米6/1)\n"
"2.客户名称(非必填):\n"
"(如:某某贸易有限公司)\n"
"货物品名(必填):茶叶"
),
reply=self.reply,
)
self.assertNotEqual(phase, "need_mode")
self.assertNotIn("告知我运输方式", "\n".join(self.replies))
segs = self.flow._segments(self.flow.session_of("sales"))
sea = next(seg for seg in segs if seg.mode == "SEA")
land = next(seg for seg in segs if seg.mode == "LAND")
self.assertEqual(sea.facts.get("箱型箱量"), "1x40HQ")
self.assertEqual(sea.facts.get("贸易条款"), "门到门")
self.assertEqual(sea.facts.get("运输分类"), "其他")
self.assertEqual(land.facts.get("车型/数量"), "8T")
self.assertEqual(land.facts.get("品名"), "茶叶")
def test_mixed_land_air_reply_splits_fields(self) -> None:
"""正服截图:一句里同时补陆运车型和空运体积/包装类型,要各归各段。"""
phase = self.flow.on_text(
sender_id="sales",
text=(
"段1:陆运\n始发站:广州\n目的地:深圳\n货物品名:茶叶\n"
"段2:空运\n起运地:上海\n目的地:北宁\n品名:茶叶\n"
"件数:21件\n重量:11kg\n尺寸:1.1*2.1*2.2"
),
reply=self.reply,
)
self.assertEqual(phase, "clarify")
self.replies.clear()
self.payloads.clear()
phase = self.flow.on_text(
sender_id="sales",
text="车型/数量:3T\n体积:2cbm\n包装类型:托盘",
reply=self.reply,
)
self.assertNotEqual(phase, "need_mode")
self.assertNotIn("告知我运输方式", "\n".join(self.replies))
segs = self.flow._segments(self.flow.session_of("sales"))
land = next(seg for seg in segs if seg.mode == "LAND")
air = next(seg for seg in segs if seg.mode == "AIR")
self.assertEqual(land.facts.get("车型/数量"), "3T")
self.assertNotIn("包装方式", land.facts)
self.assertEqual(air.facts.get("体积"), "2cbm")
self.assertEqual(air.facts.get("包装方式"), "托盘")
def test_mixed_reply_updates_ready_land_and_air(self) -> None:
"""陆运段已齐时,混合回填里的车型/数量仍按改正写回,不能丢到空运。"""
segs = split_segments(
"段1:陆运\n运输类型:国内运输整车\n线路类别:国内长途/零担\n"
"运输分类:急件\n始发站:广州\n目的地:深圳\n货物品名:茶叶\n"
"车型/数量:2T\n体积:1CBM\n"
"段2:空运\n起运地:上海\n目的地:北宁\n品名:茶叶\n件数:21件\n"
"重量:11kg\n尺寸:1.1*2.1*2.2"
).segments
merged = merge_supplement(
segs, "车型/数量:3T\n体积:2cbm\n包装类型:托盘"
)
land = next(seg for seg in merged if seg.mode == "LAND")
air = next(seg for seg in merged if seg.mode == "AIR")
self.assertEqual(land.facts.get("车型/数量"), "3T")
self.assertEqual(air.facts.get("体积"), "2cbm")
self.assertEqual(air.facts.get("包装方式"), "托盘")
# 体积只给真正问过它的空运段,不冲掉陆运已有体积。
self.assertEqual(land.facts.get("体积"), "1CBM")
def test_land_display_labels_write_to_land_segment(self) -> None:
"""始发站/目的地/货物品名归一后是海运也用的键,必须写回陆运段。"""
segs = split_segments(
"段1:海运\n起运港:上海\n目的港:洛杉矶\n品名:普货\n货物数量:20吨\n"
"段2:陆运\n运输类型:国内运输拼车\n体积:100CBM"
).segments
merged = merge_supplement(
segs, "始发站:广州\n目的地:深圳\n货物品名:茶叶"
)
_, updated, _ = refresh_segments(merged)
land = next(seg for seg in updated if seg.mode == "LAND")
sea = next(seg for seg in updated if seg.mode == "SEA")
self.assertEqual(land.facts.get("起运港"), "广州")
self.assertEqual(land.facts.get("目的港"), "深圳")
self.assertEqual(land.facts.get("品名"), "茶叶")
# 海运段原有起运港不能被陆运的始发站盖掉。
self.assertEqual(sea.facts.get("起运港"), "上海")
def test_land_cargo_reply_continues_multi(self) -> None:
"""补陆运货物字段后仍走多段,不能丢草稿去反问运输方式。"""
phase = self.flow.on_text(
sender_id="sales",
text=(
"段1:陆运\n段2:空运\n目的地:CRK\n品名:治具\n件数:1件\n"
"毛重:1.5\n体积:100CBM\n包装类型:托盘"
),
reply=self.reply,
)
self.assertEqual(phase, "clarify")
self.replies.clear()
self.payloads.clear()
phase = self.flow.on_text(
sender_id="sales",
text="车型/数量:3T,2cbm,托盘",
reply=self.reply,
)
self.assertNotEqual(phase, "need_mode")
self.assertNotIn("告知我运输方式", "\n".join(self.replies))
land = next(
seg
for seg in self.flow._segments(self.flow.session_of("sales"))
if seg.mode == "LAND"
)
self.assertIn("3T", land.facts.get("车型/数量") or "")
def test_land_options_card_expired_without_draft(self) -> None:
self.replies.clear()
phase = self.flow.on_button(
sender_id="nobody",
action=copy.BTN_LAND_OPTIONS,
reply=self.reply,
card_meta={"task_id": "multi_land_options-nobody-x", "selected_items": {}},
)
self.assertEqual(phase, "land_options_expired")
self.assertIn("已过期", self.replies[-1])
SEA_LAND_OPTIONS_MISSING = """段1:海运
起运港:上海
目的港:巴生西港
品名:机器设备
货物数量:15件
整柜或拼柜:FCL
箱型箱量:40HQ*1
贸易条款:CIF
运输分类:Port to Port
段2:陆运
始发站:广州
目的地:深圳
货物品名:衣服
体积:100CBM
毛重:50kg
"""
SEA_LAND_BOTH_MISSING = """段1:海运
起运港:上海
目的港:洛杉矶
品名:普货
货物数量:20吨
段2:陆运
始发站:广州
目的地:深圳
货物品名:茶叶
"""
SEA_LAND_READY = """段1:海运
起运港:广州港
@@ -724,7 +1000,8 @@ class PullTests(unittest.TestCase):
brief = group.messages[-1]
self.assertIn("段1:海运", brief)
self.assertIn("段2:陆运", brief)
self.assertIn("运输类型(必填)", brief)
self.assertIn("业务类型(必填):国内拼车", brief)
self.assertIn("报价场景(必填):急件", brief)
self.assertNotIn("TMS报价", brief)
brief_text = group_brief(
work_order_no="WO1",
@@ -830,6 +1107,111 @@ class DispatchClarifyTests(unittest.TestCase):
self.assertIs(picked, flow)
self.assertEqual((flow.session_of("wu-clarify").phase or ""), "clarify")
def test_labeled_land_option_reply_stays_on_multi(self) -> None:
"""陆运卡文字兜底:带业务类型/报价场景标签的回复不能被单段陆运拖走。"""
from agent.handlers.text_inquiry import _pick_text_flow
from agent.policy.multi_text_flow import reset_multi_text_flow_for_test
flow = reset_multi_text_flow_for_test()
flow.on_text(
sender_id="wu-multi-land-card",
text="段1:陆运\n段2:海运\n起运港:上海\n目的港:汉堡",
reply=lambda t, extra=None: "ok",
)
picked = _pick_text_flow(
sender_id="wu-multi-land-card",
text="业务类型:国内拼车\n报价场景:急件",
)
self.assertIs(picked, flow)
def test_sea_clarify_reply_stays_on_multi(self) -> None:
"""海运复制清单回填没有段标,也要回多段,不能被单段接走反问运输方式。"""
from agent.handlers.text_inquiry import _pick_text_flow
from agent.policy.multi_text_flow import reset_multi_text_flow_for_test
flow = reset_multi_text_flow_for_test()
flow.on_text(
sender_id="wu-multi-sea",
text="段1:海运\n起运港:上海\n段2:陆运\n始发站:广州",
reply=lambda t, extra=None: "ok",
)
picked = _pick_text_flow(
sender_id="wu-multi-sea",
text="3.贸易条款(必填):门到门\n4.运输分类(必填):其他",
)
self.assertIs(picked, flow)
def test_sea_clarify_reply_leaves_multi_without_sea_segment(self) -> None:
"""多段里没有海运段时,粘贴海运清单仍按新单段走(2026-11-23 事故)。"""
from agent.handlers.text_inquiry import _pick_text_flow
from agent.policy.multi_text_flow import reset_multi_text_flow_for_test
flow = reset_multi_text_flow_for_test()
flow.on_text(
sender_id="wu-multi-no-sea",
text="段1:陆运\n段2:空运",
reply=lambda t, extra=None: "ok",
)
picked = _pick_text_flow(
sender_id="wu-multi-no-sea",
text="起运港:上海\n目的港:洛杉矶\n整柜或拼柜:整柜\n箱型箱量:1x40HQ",
)
self.assertIsNot(picked, flow)
def test_air_optional_reply_stays_on_multi(self) -> None:
"""空运非必填(增值服务/货源地)也是补问清单里问过的,不能被单段接走。"""
from agent.handlers.text_inquiry import _pick_text_flow
from agent.policy.multi_text_flow import reset_multi_text_flow_for_test
flow = reset_multi_text_flow_for_test()
flow.on_text(
sender_id="wu-multi-air",
text="段1:空运\n目的地:CRK\n件数:1件\n段2:陆运\n始发站:广州",
reply=lambda t, extra=None: "ok",
)
picked = _pick_text_flow(
sender_id="wu-multi-air",
text="增值服务:托盘\n货源地:东莞",
)
self.assertIs(picked, flow)
def test_land_cargo_reply_stays_on_multi(self) -> None:
"""车型/数量只在陆运段,补它不能被单段流程接走反问运输方式。"""
from agent.handlers.text_inquiry import _pick_text_flow
from agent.policy.multi_text_flow import reset_multi_text_flow_for_test
flow = reset_multi_text_flow_for_test()
flow.on_text(
sender_id="wu-multi-cargo",
text="段1:陆运\n段2:空运",
reply=lambda t, extra=None: "ok",
)
picked = _pick_text_flow(
sender_id="wu-multi-cargo",
text=(
"1.车型/数量(必填):8T\n"
"(如:9米6/1)\n"
"2.客户名称(非必填):\n"
"(如:某某贸易有限公司)\n"
"货物品名(必填):茶叶"
),
)
self.assertIs(picked, flow)
def test_pick_card_flow_routes_multi_land_options(self) -> None:
"""多段陆运卡按 task_id 回多段,不能因 event_key 里的 land_options 被单聊抢走。"""
from agent.handlers.card_action import _pick_card_flow
from agent.policy.multi_text_flow import get_multi_text_flow
picked = _pick_card_flow(
"sales-card",
{
"task_id": "multi_land_options-sales-card-abc",
"event_key": "land_options_submit:sales-card",
},
)
self.assertIs(picked, get_multi_text_flow())
def test_manual_group_bookmark_does_not_steal_new_land_sea(self) -> None:
from agent.handlers.text_inquiry import _pick_text_flow, handle_text_inquiry
from agent.channel.wecom.models import InboundMessage