看板平均响应按协同分发统计;查价失败可从主账恢复;报价模板匹配与填表更稳,后台改名运营后台。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
linmianxun
2026-10-05 18:00:08 +08:00
co-authored by Cursor
parent e95e8869a6
commit 58df358577
28 changed files with 1493 additions and 245 deletions
+98 -3
View File
@@ -310,8 +310,85 @@ def reply_paused_ticket(*, ticket: Any, reply: ReplyFn) -> None:
reply(copy.system_exception_fallback(kind, names=names))
def _quote_has_usable_total(quote: dict[str, Any] | None) -> bool:
"""报价包是否已有可用于出卡的总价。"""
src = dict(quote or {})
total = str(src.get("total") or src.get("salePrice") or src.get("total_amount") or "").strip()
return bool(total and total != "-")
def recover_tms_query_from_ledger(ledger: Any, *, work_order_no: str) -> dict[str, Any] | None:
"""
查价超时/失败后回读主账:若已 persistHit 落成已报价+销售价,按查价成功续办。
产品口径:主账已经查到价,只是智能体等回包超时,不能标 TMS异常、不能给销售发接口异常,
应直接推当前工单报价。无价才走真正的系统异常。
"""
no = (work_order_no or "").strip()
if not no or ledger is None:
return None
getter = getattr(ledger, "get_for_agent", None)
snap: Any = None
if callable(getter):
try:
snap = getter(work_order_no=no, sender_id="")
except TypeError:
try:
snap = getter(work_order_no=no)
except Exception: # noqa: BLE001
snap = None
except Exception: # noqa: BLE001
snap = None
if not isinstance(snap, dict) or not snap:
getter2 = getattr(ledger, "get_ticket", None)
if callable(getter2):
try:
snap = getter2(work_order_no=no)
except Exception: # noqa: BLE001
snap = None
if not isinstance(snap, dict) or not snap:
return None
status = str(snap.get("status") or "").strip()
tms_hit = snap.get("tms_hit") in (1, "1", True, "true")
sale = str(snap.get("sale_price") or snap.get("salePrice") or "").strip()
quote = dict(snap.get("quote") or {})
tms_quote = dict(snap.get("tms_quote") or snap.get("tmsQuote") or {})
if not _quote_has_usable_total(quote) and _quote_has_usable_total(tms_quote):
quote = dict(tms_quote)
if not _quote_has_usable_total(quote) and sale and sale != "-":
quote = dict(quote)
quote["total"] = sale
quote.setdefault("source", "TMS")
quote.setdefault("source_label", "TMS标准报价")
priced = status == "已报价" and (
tms_hit or (sale and sale != "-") or _quote_has_usable_total(quote)
)
if not priced:
return None
if not _quote_has_usable_total(quote):
return None
logger.warning(
"tms.query 超时/失败但主账已有价,按成功续办 wo=%s total=%s",
no,
quote.get("total"),
)
return {
"ok": True,
"has_price": True,
"classification": "HAS_PRICE",
"quote": quote,
"recovered": True,
"source": "LEDGER_AFTER_TIMEOUT",
}
def query_tms_resilient(ledger: Any, **kwargs: Any) -> dict[str, Any]:
"""TMS 查价:最多 3 次,ok=true(含无价)即成功。"""
"""
TMS 查价:最多 3 次,ok=true(含无价)即成功。
三次仍失败时,再读主账:若查价过程中已落库有价(常见于智能体超时、主账已 persistHit),
按成功回包返回,避免误标 TMS异常。
"""
getter = getattr(ledger, "query_tms", None)
if not callable(getter):
return {"ok": False, "classification": "TMS_TECH_FAILURE", "has_price": False, "quote": {}}
@@ -319,7 +396,14 @@ def query_tms_resilient(ledger: Any, **kwargs: Any) -> dict[str, Any]:
lambda: getter(**kwargs),
is_ok=lambda x: bool((x or {}).get("ok")),
)
return out if isinstance(out, dict) else {"ok": False, "classification": "TMS_TECH_FAILURE"}
result = out if isinstance(out, dict) else {"ok": False, "classification": "TMS_TECH_FAILURE"}
if result.get("ok"):
return result
no = str(kwargs.get("work_order_no") or "").strip()
recovered = recover_tms_query_from_ledger(ledger, work_order_no=no) if no else None
if recovered is not None:
return recovered
return result
def confirm_tms_quote_if_tech(
@@ -332,9 +416,20 @@ def confirm_tms_quote_if_tech(
ticket_status: str = "询价中",
facts: dict[str, Any] | None = None,
) -> bool:
"""技术失败则标 TMS异常并回话告警。返回是否已按系统异常处理。"""
"""
技术失败则标 TMS异常并回话告警。返回是否已按系统异常处理。
主账其实已有价时不再标异常(兜底:调用方应优先走 query_tms_resilient 恢复)。
"""
if not is_tms_tech_failure(tms):
return False
# 最后一道闸:主账其实已有价时禁止再打 TMS异常(正常应已在 query_tms_resilient 恢复)。
if recover_tms_query_from_ledger(ledger, work_order_no=work_order_no) is not None:
logger.warning(
"confirm_tms_quote_if_tech 跳过标异常:主账已有价 wo=%s",
work_order_no,
)
return False
err = str(tms.get("classification") or tms.get("error") or "TMS_TECH_FAILURE").strip()
confirm_system_exception(
event=SystemExceptionEvent(