- 新增 inq_synonym 表、62组同义词种子与智能体只读接口 - Python 同义词加载/归一化与 1板/2箱 机读 - 同步当前分支既有协作、报价识别、陆运等改动
353 lines
13 KiB
Python
353 lines
13 KiB
Python
"""
|
||
陆运补问卡片:业务类型 / 报价场景 两项一次点完。
|
||
|
||
本文件职责:
|
||
1. 把 `handoff_options` 里产品确认的 7 个业务类型、5 个报价场景拼成企微
|
||
`multiple_interaction` 卡片载荷;
|
||
2. 决定这次该问哪几项(已知的不再问;只剩品名时不出卡);
|
||
3. 把回调里的 `<SelectedItems>` 收回标准项,并推出工单继续用的
|
||
运输类型 / 线路类别 / 运输分类(与 TMS、模板、超时编码同口径)。
|
||
|
||
为什么由三项合并成两项:
|
||
产品在 `资料/陆运业务类型整理-终版.xlsx` 里把「运输类型 + 线路类别」
|
||
合成一张「陆运业务类型」按钮表(国内整车、国内拼车、国内城配/打包、
|
||
东南亚整车、东南亚拼车、中港/亚/欧整车、中港/亚/欧拼车)。
|
||
销售只点一次就能同时定类型和线路,也不会再点出「中港整车 + 国内长途」
|
||
这种搭不上的组合;第二项沿用转人工卡的「报价场景」,与后台超时编码一一对应。
|
||
|
||
为什么单独一个模块:标准枚举的真相在 `handoff_options` / `land_options`,
|
||
企微卡片载荷是通道形态。塞进业务表会让枚举同时管 UI,违反分模块规则。
|
||
|
||
与转人工卡片的区别:
|
||
- 这张卡只收「补问字段」,不建单、不建群、不改六态;
|
||
- 转人工卡收同样的两项,但提交后一次到底建单建群;
|
||
- 货物品名是自由文字,卡片收不了,仍由文字回复补(提交后在正文里提示)。
|
||
|
||
禁止:在这里改六态、发明选项、直连主账。
|
||
线程:纯函数 + 只读常量,无共享可变状态,可在 Worker 槽调用。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from dataclasses import dataclass
|
||
from typing import Any, Iterable, Mapping, Optional
|
||
|
||
from agent.schema.handoff_options import (
|
||
BUSINESS_TYPES,
|
||
SCENARIOS,
|
||
biz_by_key,
|
||
biz_for_pair,
|
||
scenario_by_key,
|
||
)
|
||
from agent.schema.land_options import (
|
||
ROUTE_CODES,
|
||
canonicalize_land_class,
|
||
canonicalize_route,
|
||
canonicalize_transport_type,
|
||
land_option_gaps,
|
||
sample_pair_allowed,
|
||
)
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
# 企微 multiple_interaction 单个问题最多 10 个选项。业务类型 7 项、报价场景 5 项,
|
||
# 都在上限内;以后再加按钮时仍要检查,超了宁可回落到文字清单,也别发一张被企微整卡拒掉的卡。
|
||
MAX_OPTIONS_PER_QUESTION = 10
|
||
|
||
# 卡片卡种:task_id 写成 land_options-<令牌>-<时间戳>。
|
||
# 点卡回调靠它认出「这是陆运补问卡」,不按书签里残留的其它运输线去猜。
|
||
CARD_KIND = "land_options"
|
||
|
||
# 多段联运里的陆运补问卡。卡种必须独立:
|
||
# 回调路由按 task_id 认卡种,普通 land_options 归单聊陆运;
|
||
# 多段这张要回多段草稿,否则会被单聊陆运当成过期卡或另开一单。
|
||
# 提交按钮仍复用 land_options_submit,动作枚举不用再加一条。
|
||
MULTI_CARD_KIND = "multi_land_options"
|
||
|
||
# 合并后的两个问题键。回调按 QuestionKey 原样回传,必须与出卡一致。
|
||
QUESTION_BIZ = "biz_type"
|
||
QUESTION_SCENARIO = "quote_scenario"
|
||
|
||
# 旧三项卡的键(运输类型/线路类别/运输分类)。部署切换那一小段时间里,
|
||
# 销售手里可能还握着合并前发出去、尚未点击的旧卡;回调仍要认得,
|
||
# 否则销售点完只收到「卡已过期」,同一单要重新讲一遍。
|
||
QUESTION_TYPE = "transport_type"
|
||
QUESTION_ROUTE = "route_code"
|
||
QUESTION_CLASS = "land_class"
|
||
|
||
# 提交按钮。正文=EventKey(land_options_submit:令牌),选中项在 SelectedItems 里。
|
||
BTN_LAND_OPTIONS_KEY = "land_options_submit"
|
||
BTN_LAND_OPTIONS_TEXT = "提交选项"
|
||
|
||
|
||
def _first_ids(raw: Any) -> list[str]:
|
||
"""回调里的多选值可能是列表,也可能是单个字符串;统一成去空列表。"""
|
||
if raw is None:
|
||
return []
|
||
if isinstance(raw, str):
|
||
text = raw.strip()
|
||
return [text] if text else []
|
||
if isinstance(raw, Iterable):
|
||
out: list[str] = []
|
||
for item in raw:
|
||
text = str(item or "").strip()
|
||
if text:
|
||
out.append(text)
|
||
return out
|
||
text = str(raw).strip()
|
||
return [text] if text else []
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class LandOptionPick:
|
||
"""
|
||
一次卡片提交的结论。
|
||
|
||
字段含义:
|
||
biz_label 业务类型按钮文字(国内拼车 / 中港/亚/欧整车 …),写进工单;
|
||
transport_type 由业务类型推出的标准运输类型(TMS、模板、货物字段都用它);
|
||
route 由业务类型推出的标准线路类别(拉产品按它整词匹配);
|
||
land_class 报价场景按钮文字(与后台超时编码一一对应);
|
||
error 仅旧三项卡兼容用:类型与线路搭不上时返回 mismatch。
|
||
|
||
为什么保留 error:合并卡单选一个按钮,天然不存在不搭的组合;
|
||
但切换期旧卡可能仍回传两个独立值,调用方需要知道「这组不能写」。
|
||
"""
|
||
|
||
biz_label: str = ""
|
||
transport_type: str = ""
|
||
route: str = ""
|
||
land_class: str = ""
|
||
error: str = ""
|
||
|
||
@property
|
||
def ok(self) -> bool:
|
||
return not self.error
|
||
|
||
|
||
def _pick_biz(selected: Mapping[str, Any] | None) -> str:
|
||
"""从回调里取业务类型按钮文字;认不到返回空串。"""
|
||
for opt in _first_ids((selected or {}).get(QUESTION_BIZ)):
|
||
hit = biz_by_key(opt)
|
||
if hit is not None:
|
||
return hit.label
|
||
return ""
|
||
|
||
|
||
def _pick_scenario(selected: Mapping[str, Any] | None) -> str:
|
||
"""从回调里取报价场景按钮文字;认不到返回空串。"""
|
||
for opt in _first_ids((selected or {}).get(QUESTION_SCENARIO)):
|
||
hit = scenario_by_key(opt)
|
||
if hit is not None:
|
||
return hit.label
|
||
return ""
|
||
|
||
|
||
def _pick_legacy_class(selected: Mapping[str, Any] | None) -> str:
|
||
"""
|
||
旧三项卡的运输分类:先按分类全称认,再认旧选项 id(cls_express 这类)。
|
||
|
||
切换期旧卡还可能在销售手机里,点完不能因为认不出分类而白点一次。
|
||
认到的是合并后的报价场景文字,与后台超时编码同口径。
|
||
"""
|
||
for opt in _first_ids((selected or {}).get(QUESTION_CLASS)):
|
||
hit = canonicalize_land_class(opt)
|
||
if hit:
|
||
return hit
|
||
if opt.startswith("cls_"):
|
||
scene = scenario_by_key(opt[len("cls_") :])
|
||
if scene is not None:
|
||
return scene.label
|
||
return ""
|
||
|
||
|
||
def resolve_option_pick(selected: Mapping[str, Any] | None) -> LandOptionPick:
|
||
"""
|
||
把 SelectedItems 收回标准项。
|
||
|
||
规则:
|
||
1. 首选合并后的业务类型:一个按钮同时定运输类型和线路类别;
|
||
2. 报价场景按按钮 id 或文字认,认不到留空由调用方提醒补选;
|
||
3. 旧三项卡(transport_type / route_code / land_class)继续兼容:
|
||
只选了线路编号时按编号补类型;类型与线路搭不上返回 error=mismatch。
|
||
副作用:无。
|
||
"""
|
||
sel = selected or {}
|
||
land_class = _pick_scenario(sel)
|
||
|
||
biz_label = _pick_biz(sel)
|
||
if biz_label:
|
||
biz = biz_by_key(biz_label)
|
||
assert biz is not None
|
||
return LandOptionPick(
|
||
biz_label=biz.label,
|
||
transport_type=biz.transport_type,
|
||
route=biz.route_category,
|
||
land_class=land_class,
|
||
)
|
||
|
||
# ---- 旧三项卡兼容(部署切换期的在途卡) ----
|
||
t = ""
|
||
for opt in _first_ids(sel.get(QUESTION_TYPE)):
|
||
hit = canonicalize_transport_type(opt)
|
||
if not hit:
|
||
biz = biz_by_key(opt)
|
||
hit = biz.transport_type if biz is not None else ""
|
||
if hit:
|
||
t = hit
|
||
break
|
||
r = ""
|
||
implied = ""
|
||
for opt in _first_ids(sel.get(QUESTION_ROUTE)):
|
||
pair = None
|
||
for code, typ, route in ROUTE_CODES:
|
||
if opt == "r" + code.replace(".", "_"):
|
||
pair = (typ, route)
|
||
break
|
||
if pair:
|
||
implied, r = pair
|
||
break
|
||
hit = canonicalize_route(opt)
|
||
if hit:
|
||
r = hit
|
||
break
|
||
if not land_class:
|
||
land_class = _pick_legacy_class(sel)
|
||
if r and not t:
|
||
t = implied
|
||
if t and r:
|
||
if not sample_pair_allowed(t, r):
|
||
return LandOptionPick(
|
||
transport_type=t,
|
||
route=r,
|
||
land_class=land_class,
|
||
error="mismatch",
|
||
)
|
||
biz = biz_for_pair(t, r)
|
||
return LandOptionPick(
|
||
biz_label=biz.label if biz else "",
|
||
transport_type=t,
|
||
route=r,
|
||
land_class=land_class,
|
||
)
|
||
return LandOptionPick(
|
||
transport_type=t,
|
||
route=r,
|
||
land_class=land_class,
|
||
)
|
||
|
||
|
||
def missing_option_fields(facts: Mapping[str, Any] | None) -> list[str]:
|
||
"""
|
||
两项里仍然空着、需要点卡的字段(业务类型→报价场景)。
|
||
|
||
判断与文字清单共用 `land_options.land_option_gaps`,避免卡和清单两套口径。
|
||
货物品名不在此列:卡片收不了自由文字。
|
||
"""
|
||
return land_option_gaps(facts)
|
||
|
||
|
||
def _biz_options() -> list[dict[str, Any]]:
|
||
"""业务类型 7 项。卡片只问缺的项,不存在「已知还要再点」的情况。"""
|
||
return [{"id": item.key, "text": item.label} for item in BUSINESS_TYPES]
|
||
|
||
|
||
def _scenario_options() -> list[dict[str, Any]]:
|
||
"""报价场景 5 项。"""
|
||
return [{"id": item.key, "text": item.label} for item in SCENARIOS]
|
||
|
||
|
||
def land_option_card_payload(
|
||
*,
|
||
task_token: str,
|
||
facts: Mapping[str, Any] | None = None,
|
||
note: str = "",
|
||
cargo_missing: Optional[bool] = None,
|
||
card_kind: str = CARD_KIND,
|
||
) -> Optional[dict[str, Any]]:
|
||
"""
|
||
陆运补问卡载荷(业务类型 + 报价场景 两项)。
|
||
|
||
参数:
|
||
task_token 出卡令牌(一般是销售 userid),进 task_id 与按钮 key;
|
||
每次出卡都加时间后缀,避免企微 42014「同一张卡不能重发」;
|
||
facts 当前会话已填字段,用来跳过已知项(已填项不再问);
|
||
note 顶部说明(补问计数、校验提示);
|
||
cargo_missing 货物品名是否还缺。缺就把它排进「待补充」清单里一起说,
|
||
不再单独写一句「提交后回复货物品名」。
|
||
传 None(默认)时按 facts 里的品名自动判断,调用方忘了传也不会漏。
|
||
card_kind 卡种前缀。单聊陆运用默认值;多段联运传 MULTI_CARD_KIND,
|
||
回调按 task_id 认回多段草稿。
|
||
|
||
返回:企微 template_card 载荷;两种情况返回 None,由调用方回落到文字:
|
||
1. 两项都齐、只缺品名——卡片收不了自由文字,空卡只会让销售白点一次提交;
|
||
2. 一个问题都组不出来。
|
||
副作用:无。
|
||
"""
|
||
src = dict(facts or {})
|
||
missing = missing_option_fields(src)
|
||
if not missing:
|
||
return None
|
||
if cargo_missing is None:
|
||
# 默认按当前字段推断:卡头说明与流程里的待补充清单同口径。
|
||
from agent.schema.land_options import land_cargo_name
|
||
|
||
cargo_missing = not land_cargo_name(src)
|
||
|
||
token = (task_token or "land").strip() or "land"
|
||
# 说明文字按「待补充清单」统一口径写:这次缺哪几项就列哪几项,货物品名也排在
|
||
# 同一行里(括注怎么给),再统一一句「选好后点提交」。
|
||
# 不再单独写「提交后回复货物品名」,否则看起来像又多问了一项。
|
||
pending = list(missing)
|
||
if cargo_missing:
|
||
pending.append("货物品名(回文字如:衣服)")
|
||
desc = "待补充:" + "、".join(pending) + "。选好后点提交。"
|
||
head = (note or "").strip() or "请补充以下信息"
|
||
|
||
select_list: list[dict[str, Any]] = []
|
||
if "业务类型" in missing:
|
||
select_list.append(
|
||
{
|
||
"question_key": QUESTION_BIZ,
|
||
"title": "业务类型(单选)",
|
||
"option_list": _biz_options(),
|
||
"selected_type": 1,
|
||
}
|
||
)
|
||
if "报价场景" in missing:
|
||
select_list.append(
|
||
{
|
||
"question_key": QUESTION_SCENARIO,
|
||
"title": "报价场景(单选)",
|
||
"option_list": _scenario_options(),
|
||
"selected_type": 1,
|
||
}
|
||
)
|
||
if not select_list:
|
||
return None
|
||
if any(len(q["option_list"]) > MAX_OPTIONS_PER_QUESTION for q in select_list):
|
||
# 超企微上限:整卡会被拒(销售一条提示都收不到),交回文字清单更稳。
|
||
logger.warning(
|
||
"land.options_card.too_many_options counts=%s",
|
||
[len(q["option_list"]) for q in select_list],
|
||
)
|
||
return None
|
||
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "multiple_interaction",
|
||
"source": {"desc": "陆运补问", "desc_color": 0},
|
||
"task_id": copy.card_task_id(card_kind or CARD_KIND, token),
|
||
"main_title": {"title": head, "desc": desc},
|
||
"select_list": select_list,
|
||
"submit_button": {
|
||
"text": BTN_LAND_OPTIONS_TEXT,
|
||
"key": f"{BTN_LAND_OPTIONS_KEY}:{token}",
|
||
},
|
||
},
|
||
}
|