落地空运单聊询价字段与线路选择。

必填核对后再建单;多条线路走 H5 点选;费用只展示该线路 TMS 回包,不套用整单空运费。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-22 15:02:32 +08:00
co-authored by Cursor
parent 8afe909230
commit cfbce2fd69
53 changed files with 3470 additions and 372 deletions
+237 -64
View File
@@ -32,6 +32,14 @@ from agent.policy.air_text_flow import AirTextInquiryFlow
from agent.schema.field_validate import validate_required_fields
def _drive_until_quoted(flow, *, sender_id: str, reply, **kwargs) -> str:
"""必填齐时先出核对卡,再自动回确定。"""
phase = flow.on_text(sender_id=sender_id, reply=reply, **kwargs)
if phase == "wait_confirm":
phase = flow.on_text(sender_id=sender_id, text="确定", reply=reply)
return phase
COMPLETE_AIR = {
"起运港": "上海",
"目的港": "洛杉矶",
@@ -56,6 +64,19 @@ class AirTextInquiryTests(unittest.TestCase):
self.replies.append(text)
self.extras.append(dict(extra) if extra else {})
def _on_text(self, **kwargs):
"""单测默认:核对卡出现后自动回确定,对齐销售点确定后的旧断言。"""
reply = kwargs.get("reply") or self._reply
kwargs["reply"] = reply
phase = self.flow.on_text(**kwargs)
if phase == "wait_confirm":
phase = self.flow.on_text(
sender_id=kwargs.get("sender_id"),
text="确定",
reply=reply,
)
return phase
def test_clarify_shows_known_and_examples(self) -> None:
text = copy.ask_clarify(
facts={
@@ -69,24 +90,17 @@ class AirTextInquiryTests(unittest.TestCase):
missing_keys=["毛重", "体积", "包装方式"],
transport_mode="空运",
)
self.assertEqual(
text,
"\n".join(
[
"请确认并补充以下信息:",
"运输方式:空运",
"起运地:珠海",
"目的地:克拉克",
"品名:茶叶",
"件数:10件",
"",
"待补充:",
"重量:(如:100KGS)",
"体积:(如:2CBM)",
"包装类型:(如:托盘)",
]
),
)
self.assertIn("请确认并补充以下信息:", text)
self.assertIn("运输方式:空运", text)
self.assertIn("起运地:珠海", text)
self.assertIn("目的地:克拉克", text)
self.assertIn("品名:茶叶", text)
self.assertIn("件数:10件", text)
self.assertIn("待补充:", text)
self.assertIn("重量:(如:100KGS)", text)
self.assertIn("体积:(如:2CBM)", text)
self.assertIn("包装类型:(如:托盘)", text)
self.assertIn("货源地(非必填)", text)
def test_inquiry_card_matches_sales_format(self) -> None:
text = copy.inquiry_card(
@@ -130,7 +144,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertNotIn("报价日期", result["missing"])
def test_fly_is_air_and_followup_keeps_mode(self) -> None:
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u_fly",
text="从南京飞吉隆坡,9月14日茶叶,托盘,4.2cbm,1.5KG",
reply=self._reply,
@@ -139,7 +153,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertIn("运输方式:空运", self.replies[-1])
self.assertNotIn("请补充运输方式", self.replies[-1])
self.replies.clear()
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u_fly",
text="10件",
reply=self._reply,
@@ -177,7 +191,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertEqual(sess.facts.get("包装方式"), "托盘")
def test_missing_mode_asks_transport(self) -> None:
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u1",
text="帮我看看价格",
reply=self._reply,
@@ -190,7 +204,7 @@ class AirTextInquiryTests(unittest.TestCase):
def test_pack_bulk_cargo_accepted_as_user_said(self) -> None:
facts = dict(COMPLETE_AIR)
facts["包装方式"] = "散货"
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u_bulk",
text="空运",
reply=self._reply,
@@ -204,7 +218,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertEqual(self.flow.session_of("u_bulk").facts["包装方式"], "散货")
def test_oral_kgs_cbm_not_asked_again(self) -> None:
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u_kgs",
text="空运 茶叶 10件 100KGS 2CBM",
reply=self._reply,
@@ -222,10 +236,10 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertEqual(sess.facts["体积"], "2CBM")
self.assertEqual(sess.facts["originCode"], "ZUH")
self.assertEqual(sess.facts["destinationCode"], "CRK")
self.assertEqual(sess.facts["packageType"], "2")
self.assertEqual(sess.facts["packageType"], "托盘")
def test_missing_fields_prompt(self) -> None:
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u1",
text="空运询价",
reply=self._reply,
@@ -245,7 +259,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertNotIn("报价日期:(如", last)
def test_complete_air_hit_then_skip_adopt_excel_deal(self) -> None:
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u1",
text="空运完整",
reply=self._reply,
@@ -299,7 +313,10 @@ class AirTextInquiryTests(unittest.TestCase):
]
self.assertEqual(len(scheme_cards), 1)
card = scheme_cards[0]["template_card"]
self.assertIn("空运费:USD 1,200", card["quote_area"]["quote_text"])
self.assertEqual(
card["quote_area"]["quote_text"],
"总销售价:USD 1,280\n共2项费用,详见上一条明细",
)
self.assertEqual(len(card["button_list"]), 3)
self.assertEqual(
[x["text"] for x in card["button_list"]],
@@ -371,7 +388,7 @@ class AirTextInquiryTests(unittest.TestCase):
"包装方式": "散货",
"报价日期": "2026-09-16",
}
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u_nkg",
text="南京飞吉隆坡,9月16日茶叶,散货,4.2cbm,1.5KG,10件",
reply=self._reply,
@@ -414,7 +431,8 @@ class AirTextInquiryTests(unittest.TestCase):
facts["起运港"] = "阿拉伯"
facts["目的港"] = "广州"
flow = AirTextInquiryFlow(_Spy())
phase = flow.on_text(
phase = _drive_until_quoted(
flow,
sender_id="u_arab",
text="空运",
reply=self._reply,
@@ -439,7 +457,7 @@ class AirTextInquiryTests(unittest.TestCase):
def test_unknown_airport_no_card(self) -> None:
facts = dict(COMPLETE_AIR)
facts["目的港"] = "歧义港"
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u2c",
text="空运",
reply=self._reply,
@@ -454,7 +472,7 @@ class AirTextInquiryTests(unittest.TestCase):
def test_tms_miss_copy(self) -> None:
facts = dict(COMPLETE_AIR)
facts["tms_force"] = "1002"
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u2",
text="空运",
reply=self._reply,
@@ -535,7 +553,8 @@ class AirTextInquiryTests(unittest.TestCase):
try:
with patch("agent.channel.queue.get_message_store", return_value=store):
flow = AirTextInquiryFlow(_Led())
phase = flow.on_text(
phase = _drive_until_quoted(
flow,
sender_id="u_ord",
text="空运",
reply=reply,
@@ -566,7 +585,8 @@ class AirTextInquiryTests(unittest.TestCase):
}
flow = AirTextInquiryFlow(_Skip())
phase = flow.on_text(
phase = _drive_until_quoted(
flow,
sender_id="u_skip",
text="空运",
reply=self._reply,
@@ -580,7 +600,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertNotIn("查价参数尚未齐全", joined)
def test_same_content_second_ticket(self) -> None:
self.flow.on_text(
self._on_text(
sender_id="u3",
text="第一次",
reply=self._reply,
@@ -589,7 +609,7 @@ class AirTextInquiryTests(unittest.TestCase):
)
no1 = self.flow.session_of("u3").work_order_no
self.replies.clear()
self.flow.on_text(
self._on_text(
sender_id="u4",
text="第二次相同",
reply=self._reply,
@@ -606,7 +626,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertNotEqual(no1, no2)
def test_reject_then_adjust_prompt(self) -> None:
self.flow.on_text(
self._on_text(
sender_id="u5",
text="空运",
reply=self._reply,
@@ -619,7 +639,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertIn("提货费加 10%", self.replies[-1])
def test_private_adjust_percent_refreshes_card(self) -> None:
self.flow.on_text(
self._on_text(
sender_id="u-adj",
text="空运",
reply=self._reply,
@@ -627,7 +647,7 @@ class AirTextInquiryTests(unittest.TestCase):
injected_mode="AIR",
)
self.replies.clear()
phase = self.flow.on_text(
phase = self._on_text(
sender_id="u-adj",
text="空运费加 10%",
reply=self._reply,
@@ -647,7 +667,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertIn("1320", scheme)
def test_adopt_pdf(self) -> None:
self.flow.on_text(
self._on_text(
sender_id="u6",
text="空运",
reply=self._reply,
@@ -676,6 +696,17 @@ class AirTextInquiryTests(unittest.TestCase):
flow=self.flow,
store=store,
)
if phase == "wait_confirm":
phase = handle_text_inquiry(
InboundMessage(
sender_id="u7",
message_id="m7b",
content="确定",
msg_type="text",
),
flow=self.flow,
store=store,
)
self.assertEqual(phase, "wait_collab")
pending = [i for i in store._outbox.values()]
self.assertGreaterEqual(len(pending), 2)
@@ -684,7 +715,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertFalse(any("【询价卡片】" in i.content for i in pending))
def test_wrong_button_discarded(self) -> None:
self.flow.on_text(
self._on_text(
sender_id="u8",
text="空运",
reply=self._reply,
@@ -705,7 +736,8 @@ class AirTextInquiryTests(unittest.TestCase):
flow = AirTextInquiryFlow(_FailCreate())
replies: list[str] = []
phase = flow.on_text(
phase = _drive_until_quoted(
flow,
sender_id="u9",
text="空运",
reply=replies.append,
@@ -775,8 +807,10 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertNotIn("horizontal_content_list", card)
quote_text = card["quote_area"]["quote_text"]
self.assertNotIn("title", card["quote_area"])
self.assertIn("空运费:USD 1,200", quote_text)
self.assertIn("总销售价:USD 1,280", quote_text)
self.assertEqual(
quote_text,
"总销售价:USD 1,280\n共2项费用,详见上一条明细",
)
self.assertNotIn("时效:", quote_text)
numeric = copy.tms_hit_wecom_payload(
work_order_no="WO202609140048",
@@ -788,8 +822,10 @@ class AirTextInquiryTests(unittest.TestCase):
"fee_lines": [{"name": "空运费", "amount": "14669.60", "currency": "CNY"}],
},
)["template_card"]["quote_area"]["quote_text"]
self.assertIn("空运费:CNY 14669.60", numeric)
self.assertIn("总销售价:CNY 14669.60", numeric)
self.assertEqual(
numeric,
"总销售价:CNY 14669.60\n共1项费用,详见上一条明细",
)
self.assertEqual(copy.money_text("14669.60", ""), "14669.60")
self.assertNotIn(copy.collab_hint("WO202609140001"), quote_text)
sub = card["sub_title_text"]
@@ -801,6 +837,132 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertTrue(sub.endswith(copy.collab_hint("WO202609140001")))
self.assertNotIn("内部协同", json.dumps(card, ensure_ascii=False))
def test_long_fee_lines_go_to_text_not_clipped_card(self) -> None:
"""空运多项费用跟海运一样:完整明细走文本,卡片引用条不得半截。"""
long_quote = {
"total": "142998.53",
"currency": "CNY",
"eta": "3天",
"valid_until": "2026-09-30",
"source_label": "TMS标准报价",
"fee_lines": [
{"name": "操作费", "amount": "1220.00", "currency": "CNY"},
{"name": "箱单费", "amount": "50.00", "currency": "CNY"},
{"name": "文件费", "amount": "450.00", "currency": "CNY"},
{"name": "电放费", "amount": "100.00", "currency": "CNY"},
{"name": "更改费", "amount": "450.00", "currency": "CNY"},
{"name": "空运费", "amount": "1250.00", "currency": "USD"},
{"name": "目的港操作费", "amount": "240.00", "currency": "USD"},
],
}
text = copy.sea_quote_detail_text(
work_order_no="WO202609220005",
quote=long_quote,
business_line="AIR",
)
self.assertIn("TMS标准报价", text)
self.assertIn("WO202609220005价格明细", text)
self.assertIn("操作费:CNY 1220.00", text)
self.assertIn("空运费:USD 1250.00", text)
self.assertIn("总销售价:CNY 142998.53", text)
self.assertNotIn("时效", text)
card_text = copy.quote_area_safe_text(long_quote, business_line="AIR")
self.assertIn("总销售价:CNY 142998.53", card_text)
self.assertIn("详见上一条明细", card_text)
self.assertNotIn("操作费:CNY 1220.00", card_text)
payload = copy.tms_hit_wecom_payload(
work_order_no="WO202609220005", quote=long_quote
)
self.assertEqual(
payload["template_card"]["quote_area"]["quote_text"], card_text
)
def test_air_quote_area_always_summary_and_transfer_on_detail_and_scheme(self) -> None:
"""
空运引用条即使只有 3 项也不能塞费用,企微仍会裁成半截。
明细和方案卡航线必须带中转三字码。
"""
quote = {
"total": "183370",
"currency": "CNY",
"source_label": "TMS标准报价",
"selected_air_option": {
"originAirportCode": "ZUH",
"transferAirportCode": "CAN",
"destinationAirportCode": "CRK",
},
"fee_lines": [
{"name": "运输费", "amount": "11.0", "currency": "CNY"},
{"name": "提送货费", "amount": "6.0", "currency": "CNY"},
{"name": "人工费", "amount": "123.0", "currency": "CNY"},
],
}
card_text = copy.quote_area_safe_text(quote, business_line="AIR")
self.assertEqual(
card_text,
"总销售价:CNY 183370\n共3项费用,详见上一条明细",
)
payload = copy.tms_hit_wecom_payload(
work_order_no="WO202609220006", quote=quote
)
self.assertEqual(payload["template_card"]["quote_area"]["quote_text"], card_text)
self.assertNotIn("运输费", card_text)
detail = copy.sea_quote_detail_text(
work_order_no="WO202609220006",
quote=quote,
business_line="AIR",
)
self.assertIn("ZUH-CAN-CRK", detail)
self.assertIn("运输费:CNY 11.0", detail)
self.assertIn("总销售价:CNY 183370", detail)
rows = {
x["keyname"]: x["value"]
for x in copy.scheme_horizontal_rows(
work_order_no="WO202609220006",
facts={"起运港": "珠海", "目的港": "克拉克", "品名": "茶叶"},
quote=quote,
transport_mode="空运",
)
}
self.assertEqual(rows["航线"], "空运ZUH→CAN→CRK")
scheme = copy.scheme_wecom_payload(
work_order_no="WO202609220006",
facts={"起运港": "珠海", "目的港": "克拉克", "品名": "茶叶"},
quote=quote,
transport_mode="空运",
)
self.assertEqual(
scheme["template_card"]["quote_area"]["quote_text"],
"总销售价:CNY 183370\n共3项费用,详见上一条明细",
)
def test_air_hit_sends_detail_text_before_card(self) -> None:
"""有价必须先发出价格明细文字,再发卡,避免卡上半截费用。"""
steps: list[str] = []
def reply(text: str, extra=None) -> str:
kind = "card" if extra else "text"
steps.append(kind)
return f"out-{len(steps)}"
def wait(out_id: str, *, work_order_no: str, what: str) -> None:
steps.append(f"wait:{out_id}")
flow = AirTextInquiryFlow(MemoryLedger())
flow._wait_outbound_sent = wait # type: ignore[method-assign]
flow._detail_before_card_gap_sec = 0.0
flow._emit_air_hit(
reply,
work_order_no="WO202609220005",
quote={
"total": "142998.53",
"currency": "CNY",
"source_label": "TMS标准报价",
"fee_lines": [{"name": "操作费", "amount": "1220", "currency": "CNY"}],
},
)
self.assertEqual(steps[:3], ["text", "wait:out-1", "card"])
def test_scheme_card_payload_has_quote_and_buttons(self) -> None:
quote = {
"total": "USD 1,280",
@@ -843,13 +1005,14 @@ class AirTextInquiryTests(unittest.TestCase):
)
quote_text = card["quote_area"]["quote_text"]
self.assertNotIn("title", card["quote_area"])
self.assertIn("空运费:USD 1,200", quote_text)
self.assertIn("总销售价:USD 1,280", quote_text)
self.assertEqual(
quote_text,
"总销售价:USD 1,280\n共2项费用,详见上一条明细",
)
self.assertNotIn("sub_title_text", card)
rows = {x["keyname"]: x["value"] for x in card["horizontal_content_list"]}
self.assertEqual(rows["工单号"], "WO202609140001")
self.assertIn("上海", rows["航线"])
self.assertIn("洛杉矶", rows["航线"])
self.assertEqual(rows["航线"], "空运SHA→LAX")
self.assertIn("普货", rows["货物"])
self.assertIn("2.5", rows["货物"])
self.assertEqual(rows["时效"], "3-5 天")
@@ -953,7 +1116,8 @@ class AirTextInquiryTests(unittest.TestCase):
replies_a.append(text)
_ = extra
flow.on_text(
_drive_until_quoted(
flow,
sender_id="hist-a",
text="空运",
reply=_keep_a,
@@ -969,7 +1133,8 @@ class AirTextInquiryTests(unittest.TestCase):
replies_b.append(text)
_ = extra
flow.on_text(
_drive_until_quoted(
flow,
sender_id="hist-b",
text="空运",
reply=_keep_b,
@@ -988,7 +1153,8 @@ class AirTextInquiryTests(unittest.TestCase):
replies_c.append(text)
_ = extra
flow.on_text(
_drive_until_quoted(
flow,
sender_id="hist-c",
text="空运",
reply=_keep_c,
@@ -1023,7 +1189,7 @@ class AirTextInquiryTests(unittest.TestCase):
copy.work_order_from_card_meta(msg.raw, text=msg.content),
"WO202609140099",
)
self.flow.on_text(
self._on_text(
sender_id="WuJiLin",
text="空运",
reply=self._reply,
@@ -1033,7 +1199,7 @@ class AirTextInquiryTests(unittest.TestCase):
wo = self.flow.session_of("WuJiLin").work_order_no
xml_live = xml.replace("WO202609140099", wo)
live = parse_plain_xml(xml_live, default_agent_id="1000010")
phase = self.flow.on_text(
phase = self._on_text(
sender_id="WuJiLin",
text=live.content,
reply=self._reply,
@@ -1047,7 +1213,7 @@ class AirTextInquiryTests(unittest.TestCase):
facts_a["品名"] = "茶叶"
facts_b = dict(COMPLETE_AIR)
facts_b["品名"] = "服装"
self.flow.on_text(
self._on_text(
sender_id="card-owner",
text="空运",
reply=self._reply,
@@ -1056,7 +1222,7 @@ class AirTextInquiryTests(unittest.TestCase):
)
wo1 = self.flow.session_of("card-owner").work_order_no
self.assertTrue(wo1)
self.flow.on_text(
self._on_text(
sender_id="card-owner",
text="空运",
reply=self._reply,
@@ -1068,7 +1234,7 @@ class AirTextInquiryTests(unittest.TestCase):
self.assertNotEqual(wo1, wo2)
self.replies.clear()
phase = self.flow.on_text(
phase = self._on_text(
sender_id="card-owner",
text=f"skip_collab:{wo1}",
reply=self._reply,
@@ -1096,7 +1262,8 @@ class AirTextInquiryTests(unittest.TestCase):
flow = AirTextInquiryFlow(ledger)
def _open(uid: str) -> str:
flow.on_text(
_drive_until_quoted(
flow,
sender_id=uid,
text="空运",
reply=lambda *_a, **_k: None,
@@ -1136,7 +1303,8 @@ class AirTextInquiryTests(unittest.TestCase):
ledger = MemoryLedger()
flow = AirTextInquiryFlow(ledger)
replies: list[str] = []
flow.on_text(
_drive_until_quoted(
flow,
sender_id="u-tpl-0",
text="空运",
reply=lambda t, extra=None: replies.append(t),
@@ -1181,7 +1349,8 @@ class AirTextInquiryTests(unittest.TestCase):
)
flow = AirTextInquiryFlow(ledger)
replies: list[str] = []
flow.on_text(
_drive_until_quoted(
flow,
sender_id="u-tpl-k",
text="空运",
reply=lambda t, extra=None: replies.append(t),
@@ -1207,7 +1376,8 @@ class AirTextInquiryTests(unittest.TestCase):
ny["目的港"] = "纽约"
flow2 = AirTextInquiryFlow(ledger)
replies2: list[str] = []
flow2.on_text(
_drive_until_quoted(
flow2,
sender_id="u-tpl-d",
text="空运",
reply=lambda t, extra=None: replies2.append(t),
@@ -1244,7 +1414,8 @@ class AirTextInquiryTests(unittest.TestCase):
ledger.render_quote = _render # type: ignore[method-assign]
flow = AirTextInquiryFlow(ledger)
replies: list[str] = []
flow.on_text(
_drive_until_quoted(
flow,
sender_id="u-pdf-q",
text="空运",
reply=lambda t, extra=None: replies.append(t),
@@ -1287,7 +1458,8 @@ class AirTextInquiryTests(unittest.TestCase):
ledger.render_quote = _render # type: ignore[method-assign]
http = AirTextInquiryFlow(ledger)
replies: list[str] = []
http.on_text(
_drive_until_quoted(
http,
sender_id="u-pdf-x",
text="空运",
reply=lambda t, extra=None: replies.append(t),
@@ -1334,7 +1506,8 @@ class AirTextInquiryTests(unittest.TestCase):
ledger.render_quote = _render # type: ignore[method-assign]
flow = AirTextInquiryFlow(ledger)
flow.on_text(
_drive_until_quoted(
flow,
sender_id="u-pdf-d",
text="空运",
reply=lambda t, extra=None: None,