166 lines
5.5 KiB
Python
166 lines
5.5 KiB
Python
"""
|
|
意图:结束/终止当前询价(断开私聊上下文)。
|
|
|
|
本文件职责:丢掉当前书签与图/附件波次;有正式未闭环工单时关主账到「已关闭」;回执后允许新开。
|
|
禁止:当成成交/未成交;禁止同步等 LLM;禁止清别人的工单。
|
|
"""
|
|
|
|
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.policy import inquiry_copy as copy
|
|
from agent.policy.handoff_human import is_virtual_no
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _peek_active_session(sender_id: str) -> Optional[Any]:
|
|
"""
|
|
在清书签前看一眼当前私聊会话。
|
|
|
|
优先有正式工单号的;否则任取一份非空书签(用于判断「有没有进行中」)。
|
|
"""
|
|
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.multi_text_flow import get_multi_text_flow
|
|
from agent.policy.sea_text_flow import get_sea_text_flow
|
|
|
|
sid = (sender_id or "").strip()
|
|
if not sid:
|
|
return None
|
|
candidates: list[Any] = []
|
|
for flow in (
|
|
get_air_text_flow(),
|
|
get_sea_text_flow(),
|
|
get_land_text_flow(),
|
|
get_multi_text_flow(),
|
|
):
|
|
sess = flow.session_of(sid)
|
|
if sess is None:
|
|
continue
|
|
candidates.append(sess)
|
|
if not candidates:
|
|
return None
|
|
with_wo = [
|
|
s
|
|
for s in candidates
|
|
if (getattr(s, "work_order_no", "") or "").strip()
|
|
and not is_virtual_no(str(getattr(s, "work_order_no", "") or ""))
|
|
]
|
|
return with_wo[0] if with_wo else candidates[0]
|
|
|
|
|
|
def _drop_context(sender_id: str) -> None:
|
|
"""丢掉三线+多段书签,并作废进行中的图/附件波次。"""
|
|
from agent.handlers.attachment_inquiry import drop_all_private_bookmarks
|
|
from agent.policy.attachment_wave import get_attach_wave_book
|
|
from agent.policy.image_wave import get_image_wave_book
|
|
|
|
drop_all_private_bookmarks(sender_id)
|
|
get_image_wave_book().mark_business_done(sender_id)
|
|
get_attach_wave_book().mark_business_done(sender_id)
|
|
# 闲置附件等待票也作废,避免下一句还被并进旧上传卡。
|
|
get_attach_wave_book().abandon_if_idle(sender_id)
|
|
|
|
|
|
def _close_open_ticket(*, ledger: Any, work_order_no: str, status: str) -> bool:
|
|
"""
|
|
正式未闭环工单关到「已关闭」。
|
|
|
|
已成交/未成交/已关闭:不重复关。
|
|
返回是否成功迁到已关闭。
|
|
"""
|
|
no = (work_order_no or "").strip()
|
|
st = (status or "").strip()
|
|
if not no or is_virtual_no(no):
|
|
return False
|
|
if st in {"已成交", "未成交", "已关闭"}:
|
|
return False
|
|
writer = getattr(ledger, "transition", None)
|
|
if not callable(writer):
|
|
return False
|
|
try:
|
|
out = writer(
|
|
work_order_no=no,
|
|
to_status="已关闭",
|
|
remark="销售结束询价",
|
|
)
|
|
except Exception:
|
|
logger.exception("end_inquiry 关单失败 wo=%s", no)
|
|
return False
|
|
ok = bool(out and out.get("ok"))
|
|
if ok:
|
|
try:
|
|
from agent.policy.event_exception import clear_wait
|
|
|
|
clear_wait(ledger, work_order_no=no)
|
|
except Exception:
|
|
logger.exception("end_inquiry 清等待失败 wo=%s", no)
|
|
return ok
|
|
|
|
|
|
def handle_end_inquiry(message: InboundMessage, *, store=None, ledger: Any = None) -> str:
|
|
"""
|
|
结束当前询价上下文。
|
|
|
|
须已有 sender_id。副作用:清书签/波次;可能主账 transition→已关闭;outbox 一条回执。
|
|
返回:ended_closed / ended_context / idle_no_session / reject_no_sender。
|
|
"""
|
|
if not message.has_sender():
|
|
logger.warning("end_inquiry 拒绝:无 sender_id")
|
|
return "reject_no_sender"
|
|
|
|
sid = message.sender_id
|
|
sess = _peek_active_session(sid)
|
|
had_session = sess is not None
|
|
wo = str(getattr(sess, "work_order_no", "") or "").strip() if sess else ""
|
|
status = str(getattr(sess, "status", "") or "").strip() if sess else ""
|
|
|
|
closed = False
|
|
if had_session and wo and not is_virtual_no(wo):
|
|
from agent.config import get_settings
|
|
from agent.ledger.http_ledger import HttpLedger
|
|
|
|
book = ledger
|
|
if book is None:
|
|
try:
|
|
if (get_settings().ledger_backend or "").lower() != "memory":
|
|
book = HttpLedger()
|
|
else:
|
|
# 单测/内存态:尽量复用当前流程上的 ledger
|
|
from agent.policy.air_text_flow import get_air_text_flow
|
|
|
|
book = getattr(get_air_text_flow(), "_ledger", None)
|
|
except Exception:
|
|
book = None
|
|
if book is not None:
|
|
closed = _close_open_ticket(ledger=book, work_order_no=wo, status=status)
|
|
|
|
_drop_context(sid)
|
|
|
|
body = copy.end_inquiry_ack(
|
|
work_order_no=wo if closed else "",
|
|
closed=closed,
|
|
had_session=had_session,
|
|
)
|
|
msg_store = store or get_message_store()
|
|
msg_store.enqueue_outbound(
|
|
touser=sid,
|
|
content=body,
|
|
dedupe_key=f"end-inq:{message.message_id}",
|
|
)
|
|
|
|
phase_out = "ended_closed" if closed else ("ended_context" if had_session else "idle_no_session")
|
|
logger.info(
|
|
"end_inquiry phase=%s sender=%s wo=%s closed=%s",
|
|
phase_out,
|
|
sid,
|
|
wo or "-",
|
|
closed,
|
|
)
|
|
return phase_out
|