为什么改:应用建的协同群只能代答,销售在群里发文件、@人、问报价都对不上人; 本次把群对话交给智价通BOT,应用只负责拉群和解散群,并把几处销售已反馈的口径补齐。 群与身份: - 新增 group_bot 短协调(Redis 带环境前缀):记录 BOT 已接管群、当前关联工单、 @BOT 指令窗口与窗口外先到的附件,只做协调不当六态,六态仍在主账。 - 群里没有 @BOT 的文字不回;非 @ 文字断开当前单关联,之后必须重新 @BOT 带工单号。 - 先发文件再 @BOT 也认:窗口外附件先缓冲,等 @BOT 认领后补办。 - 会话存档只补附件、不重复办理 BOT 已办的同一句文字,避免同一条消息回两遍。 - 入站 from.userid 是加密 open_userid 时按官方接口换明文(带 Redis 缓存), 否则与主账 sales_wecom_id 永远比不上,航线本人会被判成「不是本人」。 - 桥侧只认本群 req_id,不再借用别的群 req_id,避免测服把话发进别的群。 报价单: - 建群后只发字段摘要和成交卡,Excel/PDF 等群里 @BOT 说「生成报价单」再出; 多段逐段出,没价出空白表;出表后补推成交跟进不涨 deal_version、不重置等待计时。 - 陆运转人工先选业务类型和报价场景再建群,按选定模板名直取,不再靠关键词猜。 - 空运舱位只回一条时直接出该条报价,不再发选择卡。 字段与金额: - 客户名称改为海/空/陆询价非必填,有值写回工单列,后台列表、报价单与 factsJson 一致。 - 空运新增计费重投影:体积重=CBM×166.7,计费重=max(毛重,体积重); TMS 无价、航线手工按 KG 报价时按计费重×单价算整票金额,群摘要与报价单同公式。 - 陆运报价附加条件统一到 land_quote_condition:未含税/含0税/未含燃油/含燃油费, 燃油只乘主运费,不乘提货送货报关等杂费;跨境整车无备注列时追加到底部备注事项。 - 海运拼柜不适用箱型箱量:不进必填、不进补问、不出站,改回整柜可再用。 - 海运提货地址/派送地址改为非必填,写了才出站(placeOfReceipt/deliveryAddress), 禁止从港口或贸易条款反推地址;TMS 查不到价时把 TMS 原话透给销售。 - 中港集拼与跨境集拼同口径:按体积+毛重,不采车型数量;修正被国内拼车别名抢单。 - 报关费区只收报关类费目;口岸海关监管费+接驳费按车型列回填固定行。 主账与后台: - 看板处理量按协同群处理人统计;下线「平均报价销售额」;处理人搜索先把姓名换回企微 id。 - 「转人工」筛选同时兼容旧六态与新 collab_mode=handoff,新版单不再漏。 - 报价单客户名称、提货/派送地址、空运计费重、拼柜空箱型按新口径填表。 - 新增协同成员查询支持与 AIR_NEED_WO v2 迁移脚本。 文档:04 补海运/空运/陆运必填与地址口径,10 补地址出站规则,12 补测服独立应用 1000015 的部署与切换记录。 Co-authored-by: Cursor <cursoragent@cursor.com>
679 lines
27 KiB
Python
679 lines
27 KiB
Python
"""
|
||
意图:普通文字询价(空运 / 海运 / 陆运 / 多段联运)。
|
||
|
||
本文件职责:入站文字 → 流程编排;回复只写入 outbox。
|
||
禁止:在本文件抽字段语义(交给 extract);禁止直连 TMS;禁止回调线程外的额外阻塞。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from typing import Any, Optional
|
||
|
||
from agent.channel.queue import get_message_store
|
||
from agent.channel.wecom.models import InboundMessage
|
||
from agent.config import get_settings
|
||
from agent.llm.extract_text import detect_transport_mode
|
||
from agent.policy.air_text_flow import get_air_text_flow
|
||
from agent.policy.land_text_flow import get_land_text_flow
|
||
from agent.policy.sea_text_flow import get_sea_text_flow
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
def _mode_from_clarify_labels(text: str) -> str:
|
||
"""
|
||
从补问粘贴清单的字段标签认运输方式。
|
||
|
||
销售回填补问时常常不写「海运/陆运/空运」四个字,只贴字段行。
|
||
有「整柜或拼柜 / 箱型箱量」几乎只能是海运;「业务类型 + 报价场景」是陆运选项;
|
||
「件数 + 毛重/体积」偏空运。标签互相打架时宁可不认,交给后面书签或默认空运。
|
||
"""
|
||
raw = text or ""
|
||
sea_hits = sum(1 for k in ("整柜或拼柜", "箱型箱量", "货量", "贸易条款") if k in raw)
|
||
land_hits = sum(1 for k in ("业务类型", "报价场景", "车型/数量", "通关口岸") if k in raw)
|
||
air_hits = sum(1 for k in ("件数", "毛重", "体积", "包装方式", "包装类型") if k in raw)
|
||
# 海运清单也会写起运港/品名,不能只靠这两个;以柜型类字段为准。
|
||
if sea_hits >= 2 and sea_hits >= land_hits and sea_hits >= air_hits:
|
||
return "SEA"
|
||
if land_hits >= 2 and land_hits > sea_hits and land_hits >= air_hits:
|
||
return "LAND"
|
||
if air_hits >= 2 and air_hits > sea_hits and air_hits > land_hits:
|
||
return "AIR"
|
||
return ""
|
||
|
||
|
||
def _front_bookmark(sess) -> bool:
|
||
"""补问/核对手稿优先于已出票旧书签。还没定运输方式的草稿不算当前询价,避免抢走另一条已在补问的单。已转人工的票也不再当当前询价。"""
|
||
if sess is None:
|
||
return False
|
||
from agent.policy.handoff_human import is_handoff_frozen
|
||
|
||
if is_handoff_frozen(sess):
|
||
return False
|
||
phase = (sess.phase or "").strip()
|
||
line = (sess.business_line or "").upper()
|
||
if phase == "sensitive_block":
|
||
return True
|
||
if phase == "need_mode" and not line:
|
||
return False
|
||
return phase in {"clarify", "need_mode", "need_land_options", "wait_confirm"}
|
||
|
||
|
||
def _is_waiting_mode(sess) -> bool:
|
||
"""运输方式还没定、业务线空着。定方式前的几句询价字段都堆在这份草稿上。"""
|
||
if sess is None:
|
||
return False
|
||
from agent.policy.handoff_human import is_handoff_frozen
|
||
|
||
if is_handoff_frozen(sess):
|
||
return False
|
||
if (sess.phase or "").strip() != "need_mode":
|
||
return False
|
||
return not (sess.business_line or "").strip()
|
||
|
||
|
||
def _carry_waiting_inquiry_to_owner(*, owner: str, sender_id: str) -> None:
|
||
"""
|
||
销售点明空运/海运/陆运时,把定方式之前积累的询价字段整份交给该方式。
|
||
|
||
可能是连续几句,不只上一句。已在补问或核对的单不盖。不带旧工单号。
|
||
"""
|
||
kind = (owner or "").upper()
|
||
sid = (sender_id or "").strip()
|
||
if not sid or kind not in {"SEA", "AIR", "LAND"}:
|
||
return
|
||
flows = {
|
||
"AIR": get_air_text_flow(),
|
||
"SEA": get_sea_text_flow(),
|
||
"LAND": get_land_text_flow(),
|
||
}
|
||
owner_flow = flows[kind]
|
||
existing = owner_flow.session_of(sid)
|
||
if _front_bookmark(existing):
|
||
return
|
||
drafts = []
|
||
for flow in flows.values():
|
||
sess = flow.session_of(sid)
|
||
if _is_waiting_mode(sess):
|
||
drafts.append(sess)
|
||
if not drafts:
|
||
return
|
||
# 字段多的打底,其它草稿只补空缺,避免后写的空值盖掉已认出的港口。
|
||
drafts.sort(key=lambda item: len(item.facts or {}), reverse=True)
|
||
facts: dict[str, str] = {}
|
||
texts: list[str] = []
|
||
for sess in drafts:
|
||
for key, value in (sess.facts or {}).items():
|
||
text = str(value).strip()
|
||
if text and not str(facts.get(key) or "").strip():
|
||
facts[str(key)] = text
|
||
raw = (sess.immutable_text or "").strip()
|
||
if raw and raw not in texts:
|
||
texts.append(raw)
|
||
holds = [str(getattr(item, "sensitive_hold", "") or "").strip() for item in drafts]
|
||
holds = [item for item in holds if item]
|
||
if not facts and not texts and not holds:
|
||
return
|
||
from agent.policy.air_text_flow import FlowSession
|
||
|
||
owner_flow._save(
|
||
FlowSession(
|
||
sender_id=sid,
|
||
thread_id=sid,
|
||
phase="need_mode",
|
||
business_line="",
|
||
facts=facts,
|
||
immutable_text="\n".join(texts),
|
||
sensitive_hold="\n".join(holds),
|
||
)
|
||
)
|
||
logger.info(
|
||
"text_inquiry 未定方式的询价字段交给 %s keys=%s sender=%s",
|
||
kind,
|
||
",".join(sorted(facts)),
|
||
sid,
|
||
)
|
||
|
||
|
||
def _drop_other_mode_drafts(*, owner: str, sender_id: str) -> None:
|
||
"""
|
||
本句已点名空运/海运/陆运:先带走「还没定方式」时积下的询价字段,再丢掉其它方式的补问/选项草稿。
|
||
|
||
测服复现:旧陆运「选项未齐」书签还在,海运补「贸易条款 / 运输分类」会被陆运抢走,
|
||
回「参数提取不全」并发送填写样例图。已出号会话留在工单号索引,不当当前草稿。
|
||
定方式前的 need_mode 要整份移交,不能直接删。
|
||
"""
|
||
kind = (owner or "").upper()
|
||
sid = (sender_id or "").strip()
|
||
if not sid or kind not in {"SEA", "AIR", "LAND", "MULTI"}:
|
||
return
|
||
_carry_waiting_inquiry_to_owner(owner=kind, sender_id=sid)
|
||
land = get_land_text_flow()
|
||
air = get_air_text_flow()
|
||
sea = get_sea_text_flow()
|
||
others = []
|
||
if kind != "LAND":
|
||
others.append(land)
|
||
if kind != "AIR":
|
||
others.append(air)
|
||
if kind != "SEA":
|
||
others.append(sea)
|
||
for flow in others:
|
||
sess = flow.session_of(sid)
|
||
phase = (getattr(sess, "phase", None) or "").strip() if sess else ""
|
||
if _front_bookmark(sess) or phase == "need_mode":
|
||
flow.drop_current_bookmark(sid)
|
||
logger.info(
|
||
"text_inquiry 丢掉其它方式草稿 owner=%s dropped_phase=%s sender=%s",
|
||
kind,
|
||
phase or "-",
|
||
sid,
|
||
)
|
||
if kind != "LAND":
|
||
clearer = getattr(land, "_clear_parked_confirm", None)
|
||
if callable(clearer):
|
||
clearer(sid)
|
||
|
||
|
||
def lost_reason_wait_state(sender_id: str) -> dict[str, Any]:
|
||
"""
|
||
只读快照:当前是否有空运/海运/陆运书签停在「等未成交原因」。
|
||
|
||
为什么单独抽出来(2026-11-22 正服 WO202611170021):销售点完「未成交」后
|
||
BOT 回「请补充未成交原因」,下一句销售说「客户不要了」,DeepSeek A 把这句
|
||
误判成 end_inquiry;dispatch 直接把上下文清掉,未成交原因永远进不了主账。
|
||
分发层需要在调用 end_inquiry 之前知道「这条人正在等原因」,把这句话交回
|
||
询价流程当原因收下。本函数只读书签,不发消息、不改状态。
|
||
|
||
返回:waiting / phase / work_order_no / status / business_line。
|
||
不含多段:多段流程目前没有收未成交原因的实现,不能在这里把原因吞掉。
|
||
"""
|
||
out: dict[str, Any] = {
|
||
"waiting": False,
|
||
"phase": "",
|
||
"work_order_no": "",
|
||
"status": "",
|
||
"business_line": "",
|
||
}
|
||
sid = (sender_id or "").strip()
|
||
if not sid:
|
||
return out
|
||
try:
|
||
from agent.policy.air_text_flow import get_air_text_flow
|
||
from agent.policy.land_text_flow import get_land_text_flow
|
||
from agent.policy.sea_text_flow import get_sea_text_flow
|
||
|
||
candidates = (
|
||
("AIR", get_air_text_flow()),
|
||
("SEA", get_sea_text_flow()),
|
||
("LAND", get_land_text_flow()),
|
||
)
|
||
for line, flow in candidates:
|
||
sess = flow.session_of(sid)
|
||
if sess is None:
|
||
continue
|
||
phase = (sess.phase or "").strip()
|
||
if phase != "wait_lost_reason":
|
||
continue
|
||
out.update(
|
||
{
|
||
"waiting": True,
|
||
"phase": phase,
|
||
"work_order_no": str(getattr(sess, "work_order_no", "") or ""),
|
||
"status": str(getattr(sess, "status", "") or ""),
|
||
"business_line": line,
|
||
}
|
||
)
|
||
return out
|
||
except Exception: # noqa: BLE001 - 取证失败不能影响入站分发
|
||
logger.exception("读未成交原因等待状态失败 sender=%s", sid)
|
||
return out
|
||
|
||
|
||
def _flow_waiting_lost_reason(sender_id: str, sea, air, land):
|
||
"""谁在等未成交原因就交给谁。刚记下未成交的那条优先。"""
|
||
from agent.policy.deal_outcome import ticket_missing_in_ledger
|
||
from agent.policy.handoff_human import is_handoff_frozen
|
||
|
||
waiting = []
|
||
for flow in (sea, air, land):
|
||
sess = flow.session_of(sender_id)
|
||
if sess is None or is_handoff_frozen(sess):
|
||
continue
|
||
if (sess.phase or "").strip() != "wait_lost_reason":
|
||
continue
|
||
# 清数后主账无单:丢掉孤儿书签,不要继续抢走新询价。
|
||
if ticket_missing_in_ledger(
|
||
getattr(flow, "_ledger", None),
|
||
work_order_no=str(getattr(sess, "work_order_no", "") or ""),
|
||
sender_id=sender_id,
|
||
):
|
||
abandon = getattr(flow, "abandon_orphan_lost_wait", None)
|
||
if callable(abandon):
|
||
abandon(sess)
|
||
else:
|
||
drop = getattr(flow, "drop_current_bookmark", None)
|
||
if callable(drop):
|
||
drop(sender_id)
|
||
continue
|
||
waiting.append((int(getattr(sess, "wait_version", 0) or 0), flow))
|
||
if not waiting:
|
||
return None
|
||
waiting.sort(key=lambda item: item[0], reverse=True)
|
||
return waiting[0][1]
|
||
|
||
|
||
def should_resume_other_as_inquiry(message: InboundMessage) -> bool:
|
||
"""
|
||
A 把补问短答标成闲聊时,仍续填当前询价。
|
||
|
||
只拦还在问字段/选项/核对、未成交原因、转人工入口。
|
||
已报价协同中不拦,避免「谢谢」被当成新询价。
|
||
"""
|
||
if not message.has_sender():
|
||
return False
|
||
engine = _pick_text_flow(
|
||
sender_id=message.sender_id, text=message.content or ""
|
||
)
|
||
sess = engine.session_for_action(
|
||
message.sender_id, message.raw, text=message.content or ""
|
||
)
|
||
if sess is None:
|
||
return False
|
||
phase = (sess.phase or "").strip()
|
||
if phase in {"wait_lost_reason", "handoff_offer"}:
|
||
return True
|
||
return _front_bookmark(sess)
|
||
|
||
|
||
def _pick_text_flow(*, sender_id: str, text: str, injected_mode: str = "", prefer_quoted: bool = False):
|
||
"""
|
||
按当前书签或本句运输方式选空运/海运/陆运流程。
|
||
|
||
陆运核对卡上的「确定」优先于旧海运书签,避免「确定」被海运/空运吃掉。
|
||
本句点名海运/空运/陆运时盖掉其它方式的旧补问草稿。
|
||
核对卡优先于选项补问;海运/空运补问优先于旧陆运选项草稿,避免补贸易条款发陆运样例图。
|
||
正在补问时,补问书签优先于已报价调价,避免第 3 次反问出不了转人工卡。
|
||
A 已判成调价且当前不是补问:交给当前已报价工单,避免旧海运书签抢走。
|
||
图片识图在 Worker 写下的补问书签,HTTP 必须从 Redis 读到,否则「托盘」会变成再问运输方式。
|
||
"""
|
||
from agent.schema.land_options import looks_like_confirm
|
||
from agent.policy.quote_adjust_ops import quoted_adjust_flow
|
||
from agent.policy.handoff_human import is_handoff_frozen
|
||
|
||
from agent.policy.multi_segments import classify_multi_text
|
||
from agent.policy.multi_text_flow import get_multi_text_flow
|
||
|
||
multi = get_multi_text_flow()
|
||
# 多段核对上的「确定」优先于陆运旧核对,避免建出海运/陆运单段工单。
|
||
if looks_like_confirm(text):
|
||
multi_hit = multi.session_of(sender_id)
|
||
if (
|
||
multi_hit
|
||
and (multi_hit.business_line or "").upper() == "MULTI"
|
||
and (multi_hit.phase or "") == "wait_confirm"
|
||
):
|
||
return multi
|
||
land = get_land_text_flow()
|
||
if looks_like_confirm(text):
|
||
restored = land.restore_confirm_session(sender_id)
|
||
if restored:
|
||
return land
|
||
retry = land.restore_tms_retry_session(sender_id)
|
||
if retry:
|
||
return land
|
||
sea = get_sea_text_flow()
|
||
air = get_air_text_flow()
|
||
# 正在等未成交原因:下一句就是原因。调价、旧报价单、新的运输方式都不能把这句带走。
|
||
lost_flow = _flow_waiting_lost_reason(sender_id, sea, air, land)
|
||
if lost_flow is not None:
|
||
return lost_flow
|
||
spoken = (injected_mode or detect_transport_mode(text) or "").upper()
|
||
multi_kind = classify_multi_text(text) if not injected_mode else "single"
|
||
if multi_kind in {"multi", "same_mode"}:
|
||
_drop_other_mode_drafts(owner="MULTI", sender_id=sender_id)
|
||
return multi
|
||
# 本句点名运输方式:盖掉其它方式的旧补问,避免海运补字段走陆运样例图。
|
||
# 多段正在补问时例外:销售会写「段2:运输方式:空运」,不能把多段草稿丢掉改走单段。
|
||
# 但「例外」只留给真多段续问(段标 / 本句自身两段)。销售单独发一句「陆运询价」
|
||
# 是新开单段,必须按本句方式走,并把旧多段草稿让掉;否则一张多段比价草稿
|
||
# 会把后面每一句都接走(2026-11-19 正服事故,持续到进程重启)。
|
||
# 已出号等拉群不算补问:再发陆运附件要走陆运,不能粘在旧工单的手动拉群提示上。
|
||
if spoken in {"SEA", "AIR", "LAND"}:
|
||
from agent.policy.multi_segments import is_multi_continuation
|
||
|
||
multi_draft = multi.session_of(sender_id)
|
||
multi_waiting = bool(
|
||
multi_draft
|
||
and (multi_draft.business_line or "").upper() == "MULTI"
|
||
and (multi_draft.phase or "") in {"clarify", "sensitive_block"}
|
||
)
|
||
if multi_waiting and is_multi_continuation(text):
|
||
return multi
|
||
if multi_waiting:
|
||
# 本句是新开单段:让位前丢掉多段草稿,避免下一句补字段又被多段接走。
|
||
# 只丢当前书签,已出号的工单仍按工单号留在主账。
|
||
multi.drop_current_bookmark(sender_id)
|
||
logger.info(
|
||
"text_inquiry 旧多段草稿让位 owner=%s multi_phase=%s sender=%s",
|
||
spoken,
|
||
(multi_draft.phase or "-") if multi_draft else "-",
|
||
sender_id,
|
||
)
|
||
_drop_other_mode_drafts(owner=spoken, sender_id=sender_id)
|
||
stale_handoff = (multi_draft.phase or "") in {
|
||
"wait_collab",
|
||
"wait_manual_group",
|
||
"collab_group",
|
||
} if multi_draft else False
|
||
if _front_bookmark(multi_draft) or stale_handoff:
|
||
multi.drop_current_bookmark(sender_id)
|
||
if spoken == "SEA":
|
||
return sea
|
||
if spoken == "LAND":
|
||
return land
|
||
return air
|
||
# 本句没点名运输方式时:多段草稿也不能无条件接管。
|
||
# 正服 2026-11-23:销售粘贴海运补问清单(起运港/整柜或拼柜/贸易条款…,无「海运」二字),
|
||
# spoken 为空,旧逻辑仍因 multi=clarify 把整句交给多段,回「段1:陆运」。
|
||
# 只有真多段续问(段标/本句两段)或核对卡上的「确定」才继续多段;其余让位给单段。
|
||
from agent.policy.multi_segments import is_multi_continuation
|
||
|
||
multi_sess = multi.session_of(sender_id)
|
||
if (
|
||
multi_sess
|
||
and (multi_sess.business_line or "").upper() == "MULTI"
|
||
and (multi_sess.phase or "") in {"clarify", "wait_confirm", "sensitive_block"}
|
||
and not (looks_like_confirm(text) and (multi_sess.phase or "") != "wait_confirm")
|
||
):
|
||
phase_now = (multi_sess.phase or "").strip()
|
||
if phase_now == "wait_confirm" and looks_like_confirm(text):
|
||
return multi
|
||
if is_multi_continuation(text):
|
||
return multi
|
||
multi.drop_current_bookmark(sender_id)
|
||
logger.info(
|
||
"text_inquiry 旧多段草稿让位 owner=LABEL_OR_SINGLE multi_phase=%s sender=%s",
|
||
phase_now or "-",
|
||
sender_id,
|
||
)
|
||
# 补问粘贴常不写「海运/陆运」:用字段标签认方式,避免让位后落到默认空运。
|
||
label_mode = _mode_from_clarify_labels(text)
|
||
if label_mode == "SEA":
|
||
_drop_other_mode_drafts(owner="SEA", sender_id=sender_id)
|
||
return sea
|
||
if label_mode == "LAND":
|
||
_drop_other_mode_drafts(owner="LAND", sender_id=sender_id)
|
||
return land
|
||
if label_mode == "AIR":
|
||
_drop_other_mode_drafts(owner="AIR", sender_id=sender_id)
|
||
return air
|
||
sea_sess = sea.session_of(sender_id)
|
||
land_sess = land.session_of(sender_id)
|
||
air_sess = air.session_of(sender_id)
|
||
# 核对卡仍优先:销售正在改体积/点确定,不能被另一方式的补问草稿抢走。
|
||
for sess, flow in ((land_sess, land), (air_sess, air), (sea_sess, sea)):
|
||
if sess is not None and (sess.phase or "").strip() == "wait_confirm" and not is_handoff_frozen(sess):
|
||
return flow
|
||
# 补问书签:海运/空运优先于旧陆运选项草稿。陆运「确定」已在上面拦过。
|
||
for sess, flow in ((sea_sess, sea), (air_sess, air), (land_sess, land)):
|
||
if _front_bookmark(sess):
|
||
return flow
|
||
# 还在问运输方式:后面几句询价字段继续叠在同一份草稿上,不要另开一条只留最后一句。
|
||
parked = [
|
||
(sess, flow)
|
||
for sess, flow in ((sea_sess, sea), (air_sess, air), (land_sess, land))
|
||
if _is_waiting_mode(sess)
|
||
]
|
||
if parked:
|
||
parked.sort(key=lambda item: len(item[0].facts or {}), reverse=True)
|
||
return parked[0][1]
|
||
if prefer_quoted:
|
||
quoted = quoted_adjust_flow(sender_id)
|
||
if quoted is not None:
|
||
return quoted
|
||
if sea_sess and not is_handoff_frozen(sea_sess) and (sea_sess.business_line or "").upper() == "SEA":
|
||
return sea
|
||
if land_sess and not is_handoff_frozen(land_sess) and (land_sess.business_line or "").upper() == "LAND":
|
||
return land
|
||
return air
|
||
|
||
|
||
def is_pending_land_confirm(message: InboundMessage) -> bool:
|
||
"""
|
||
陆运核对卡已出、本句是确定查价。
|
||
|
||
测服实测「确定」两字会被 DeepSeek A 判成闲聊,必须在分发层拦住,
|
||
不能回首次问候、不能跳过建单。
|
||
"""
|
||
if not message.has_sender():
|
||
return False
|
||
from agent.schema.land_options import looks_like_confirm
|
||
|
||
if not looks_like_confirm(message.content or ""):
|
||
return False
|
||
sess = get_land_text_flow().restore_confirm_session(message.sender_id)
|
||
ok = bool(
|
||
sess
|
||
and (sess.business_line or "").upper() == "LAND"
|
||
and sess.phase == "wait_confirm"
|
||
)
|
||
if not ok:
|
||
logger.info(
|
||
"land.confirm miss sender=%s phase=%s line=%s",
|
||
message.sender_id,
|
||
getattr(sess, "phase", "-"),
|
||
getattr(sess, "business_line", "-"),
|
||
)
|
||
return ok
|
||
|
||
|
||
def is_pending_land_tms_retry(message: InboundMessage) -> bool:
|
||
"""
|
||
陆运工单已建、上次查价失败,本句是确定重查。
|
||
|
||
不能回首次问候、不能再开一张新工单。
|
||
"""
|
||
if not message.has_sender():
|
||
return False
|
||
from agent.schema.land_options import looks_like_confirm
|
||
|
||
if not looks_like_confirm(message.content or ""):
|
||
return False
|
||
sess = get_land_text_flow().restore_tms_retry_session(message.sender_id)
|
||
return bool(
|
||
sess
|
||
and (sess.business_line or "").upper() == "LAND"
|
||
and sess.phase == "tms_tech"
|
||
and (sess.work_order_no or "").strip()
|
||
)
|
||
|
||
|
||
def is_land_confirm_action(message: InboundMessage) -> bool:
|
||
"""核对卡确定建单,或查价失败后确定重查。"""
|
||
return is_pending_land_confirm(message) or is_pending_land_tms_retry(message)
|
||
|
||
|
||
def _log_flow_pick(
|
||
*,
|
||
sender_id: str,
|
||
text: str,
|
||
engine: Any,
|
||
injected_mode: str = "",
|
||
) -> None:
|
||
"""
|
||
记一行「这一单最后走了哪条线、为什么走这条线」。
|
||
|
||
为什么需要(2026-11-19 正服事故):当时销售发「陆运询价」,模型也判出 LAND,
|
||
但回的是海运补问;正服日志里只有 dispatch 意图和 schema.validate 的 line,
|
||
看不出选流那一刻本句点名了什么方式、三条线各自还挂着什么书签,只能靠猜。
|
||
这里把 spoken(本句点名的方式)和四条线的书签阶段一次打出来,
|
||
下次同样问题能直接对着日志定位,不必再翻 Redis。
|
||
|
||
只读:不建书签、不掉书签、不改任何会话;失败也不影响业务(只记异常)。
|
||
"""
|
||
try:
|
||
from agent.policy.multi_text_flow import get_multi_text_flow
|
||
|
||
def _state(sess: Any) -> str:
|
||
# 形如 clarify/SEA;没有书签记 "-",方便一眼区分「没书签」和「有书签但阶段空」
|
||
if sess is None:
|
||
return "-"
|
||
return f"{sess.phase or '-'}/{(sess.business_line or '-')}"
|
||
|
||
spoken = (injected_mode or detect_transport_mode(text or "") or "").upper() or "-"
|
||
logger.info(
|
||
"text_inquiry 选流 engine=%s spoken=%s sea=%s air=%s land=%s multi=%s chars=%d",
|
||
type(engine).__name__,
|
||
spoken,
|
||
_state(get_sea_text_flow().session_of(sender_id)),
|
||
_state(get_air_text_flow().session_of(sender_id)),
|
||
_state(get_land_text_flow().session_of(sender_id)),
|
||
_state(get_multi_text_flow().session_of(sender_id)),
|
||
len(text or ""),
|
||
)
|
||
except Exception: # noqa: BLE001 - 留痕失败绝不能影响询价主链路
|
||
logger.exception("text_inquiry 选流留痕失败 sender=%s", sender_id)
|
||
|
||
|
||
def handle_text_inquiry(
|
||
message: InboundMessage,
|
||
*,
|
||
injected_facts: Optional[dict[str, Any]] = None,
|
||
injected_mode: str = "",
|
||
b_result: Optional[dict[str, Any]] = None,
|
||
chat_fn=None,
|
||
flow=None,
|
||
store=None,
|
||
prefer_quoted: bool = False,
|
||
prefer_handoff: bool = False,
|
||
) -> str:
|
||
"""
|
||
处理一条私聊文字询价。
|
||
|
||
prefer_quoted:A 已判成调价时交给当前已报价工单。
|
||
须已有 sender_id。副作用:outbox 多条回复;主账经 flow.ledger。
|
||
返回:流程阶段名。
|
||
"""
|
||
if not message.has_sender():
|
||
logger.warning("text_inquiry 拒绝:无 sender_id")
|
||
return "reject_no_sender"
|
||
from agent.handlers.image_inquiry import note_private_text
|
||
|
||
note_private_text(message.sender_id, message.content or "")
|
||
settings = get_settings()
|
||
msg_store = store or get_message_store()
|
||
engine = flow or _pick_text_flow(
|
||
sender_id=message.sender_id,
|
||
text=message.content or "",
|
||
injected_mode=injected_mode,
|
||
prefer_quoted=prefer_quoted,
|
||
)
|
||
# 只有自己选流时才留痕:调用方直接指定 flow 时,选流不是这里决定的,记了会误导。
|
||
if flow is None:
|
||
_log_flow_pick(
|
||
sender_id=message.sender_id,
|
||
text=message.content or "",
|
||
engine=engine,
|
||
injected_mode=injected_mode,
|
||
)
|
||
replies: list[str] = []
|
||
|
||
def reply(text: str, extra: dict | None = None) -> str:
|
||
# 销售询价正文不加 [test];[test] 只给运维告警
|
||
body = text
|
||
replies.append(body)
|
||
ok, info = msg_store.enqueue_outbound(
|
||
touser=message.sender_id,
|
||
content=body,
|
||
dedupe_key=f"inq:{message.message_id}:{len(replies)}",
|
||
payload=dict(extra or {}),
|
||
)
|
||
logger.info("text_inquiry 出站 ok=%s info=%s phase_reply=%s", ok, info, body[:80])
|
||
return info if ok else ""
|
||
|
||
from agent.policy.system_exception import (
|
||
cabin_failure_pause,
|
||
extract_work_order_nos,
|
||
reply_paused_ticket,
|
||
ticket_is_paused,
|
||
)
|
||
|
||
ledger = getattr(engine, "_ledger", None)
|
||
for no in extract_work_order_nos(message.content or ""):
|
||
ticket = None
|
||
if ledger is not None:
|
||
getter = getattr(ledger, "get_ticket", None)
|
||
if callable(getter):
|
||
ticket = getter(work_order_no=no)
|
||
if ticket is None:
|
||
getter = getattr(ledger, "get_for_agent", None)
|
||
if callable(getter):
|
||
ticket = getter(work_order_no=no, sender_id=message.sender_id)
|
||
if ticket_is_paused(ticket) and not cabin_failure_pause(ticket):
|
||
reply_paused_ticket(ticket=ticket, reply=reply)
|
||
logger.info("text_inquiry 暂停工单只回兜底 wo=%s sender=%s", no, message.sender_id)
|
||
return "system_exception_paused"
|
||
|
||
if prefer_handoff:
|
||
phase = engine.offer_handoff(
|
||
sender_id=message.sender_id,
|
||
text=message.content or "",
|
||
reply=reply,
|
||
)
|
||
logger.info("text_inquiry handoff phase=%s sender=%s", phase, message.sender_id)
|
||
return phase
|
||
|
||
phase = engine.on_text(
|
||
sender_id=message.sender_id,
|
||
text=message.content or "",
|
||
reply=reply,
|
||
injected_facts=injected_facts,
|
||
injected_mode=injected_mode,
|
||
b_result=b_result,
|
||
allow_b=bool(settings.llm_allow_network and settings.llm_data_usage_confirmed),
|
||
chat_fn=chat_fn,
|
||
inbound_raw=message.raw,
|
||
)
|
||
logger.info("text_inquiry phase=%s sender=%s", phase, message.sender_id)
|
||
return phase
|
||
|
||
|
||
def handle_text_other(message: InboundMessage, *, store=None) -> str:
|
||
"""
|
||
闲聊/其它:补问短答续填;待点按钮当按钮;否则回首次单聊话术。
|
||
"""
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
if not message.has_sender():
|
||
return "reject_no_sender"
|
||
msg_store = store or get_message_store()
|
||
if should_resume_other_as_inquiry(message):
|
||
logger.info(
|
||
"text_other 续填当前询价 sender=%s chars=%s",
|
||
message.sender_id,
|
||
len((message.content or "").strip()),
|
||
)
|
||
return handle_text_inquiry(message, store=msg_store)
|
||
engine = _pick_text_flow(
|
||
sender_id=message.sender_id, text=message.content or ""
|
||
)
|
||
sess = engine.session_for_action(
|
||
message.sender_id, message.raw, text=message.content or ""
|
||
)
|
||
btn = engine.match_button(message.content or "", sess)
|
||
if btn:
|
||
return handle_text_inquiry(message, store=msg_store)
|
||
if is_land_confirm_action(message):
|
||
logger.info("text_other 陆运确定/重查,改走询价流程 sender=%s", message.sender_id)
|
||
return handle_text_inquiry(message, store=msg_store)
|
||
|
||
body = copy.first_chat()
|
||
msg_store.enqueue_outbound(
|
||
touser=message.sender_id,
|
||
content=body,
|
||
dedupe_key=f"other:{message.message_id}",
|
||
)
|
||
return "text_other"
|