为什么改:应用建的协同群只能代答,销售在群里发文件、@人、问报价都对不上人; 本次把群对话交给智价通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>
1018 lines
47 KiB
Python
1018 lines
47 KiB
Python
"""
|
||
入站分发壳:RouteDecision.intent → 唯一 handler。
|
||
|
||
本文件职责:维护意图→可调用对象表;dispatch_inbound 只查表调用。
|
||
禁止:在本文件写补问/建单/TMS 业务;禁止一个函数处理全部卡片。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from contextlib import contextmanager
|
||
from typing import Callable, Iterator, Optional
|
||
|
||
from agent.channel.wecom.models import InboundMessage
|
||
from agent.handlers import abnormal, card_action, close_deal, continue_thread, new_inquiry
|
||
from agent.handlers.echo_text import handle_echo_text
|
||
from agent.handlers.end_inquiry import handle_end_inquiry
|
||
from agent.handlers.group_collab import handle_group_collab
|
||
from agent.handlers.image_inquiry import handle_image_inquiry
|
||
from agent.handlers.attachment_inquiry import handle_attachment_inquiry
|
||
from agent.handlers.text_inquiry import (
|
||
handle_text_inquiry,
|
||
handle_text_other,
|
||
is_land_confirm_action,
|
||
lost_reason_wait_state,
|
||
should_resume_other_as_inquiry,
|
||
)
|
||
from agent.routing.decision import RouteDecision
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
def _fp(text: str) -> str:
|
||
"""排障用:算与去重逻辑一致的群文字指纹;失败返回空串,不影响业务。"""
|
||
try:
|
||
from agent.redis_coord.group_bot import _text_fingerprint
|
||
|
||
return _text_fingerprint(text)
|
||
except Exception: # noqa: BLE001
|
||
return ""
|
||
|
||
|
||
def _handled_marker(chat_id: str) -> str:
|
||
"""排障用:读当前「AIBOT 已办文字」标记;失败返回空串。"""
|
||
try:
|
||
from agent.redis_coord.group_bot import handled_marker
|
||
|
||
return handled_marker(chat_id)
|
||
except Exception: # noqa: BLE001
|
||
return ""
|
||
|
||
|
||
def _ledger_binding(chat_id: str) -> tuple[bool, str, str]:
|
||
"""
|
||
排障用:查主账「这个群绑的哪张单」,返回 (是否命中, 工单号, 状态)。
|
||
|
||
为什么要查主账而不只看 Redis:Redis 的 active_wo 是短命关联,群里一句普通
|
||
闲聊就会被清掉;主账绑群关系才是「这个群在服务哪张单」的权威来源。
|
||
失败一律按「没命中」返回,不能影响业务分支。
|
||
"""
|
||
try:
|
||
from agent.policy.air_text_flow import get_air_text_flow
|
||
from agent.policy.sea_group_ops import ticket_no, ticket_view
|
||
|
||
flow = get_air_text_flow()
|
||
raw = flow.ledger.find_by_collab_chat(chat_id=chat_id)
|
||
if raw is None:
|
||
return False, "", ""
|
||
view = ticket_view(raw)
|
||
return (
|
||
True,
|
||
ticket_no(raw),
|
||
str(view.get("status") or ""),
|
||
)
|
||
except Exception: # noqa: BLE001
|
||
return False, "", ""
|
||
|
||
# Handler 签名约定:只收 InboundMessage(出站/store 由 handler 自行取依赖)
|
||
HandlerFn = Callable[[InboundMessage], None]
|
||
|
||
|
||
def _noop(message: InboundMessage) -> None:
|
||
"""忽略类意图:不做事。"""
|
||
logger.info("dispatch.noop sender=%s msg=%s", message.sender_id, message.message_id)
|
||
|
||
|
||
def _wrap_echo(message: InboundMessage) -> None:
|
||
"""联调残留:仅 echo_text 意图使用。"""
|
||
from agent.channel.queue import get_message_store
|
||
from agent.config import get_settings
|
||
|
||
settings = get_settings()
|
||
handle_echo_text(
|
||
message,
|
||
get_message_store(),
|
||
ytd_env=settings.ytd_env,
|
||
reply_template=settings.wecom_callback_reply_text,
|
||
)
|
||
|
||
|
||
def _wrap_text_inquiry(message: InboundMessage) -> None:
|
||
"""普通文字询价:空运闭环。"""
|
||
handle_text_inquiry(message)
|
||
|
||
|
||
def _wrap_text_other(message: InboundMessage) -> None:
|
||
"""闲聊:回首次引导;若正等按钮则当按钮。"""
|
||
handle_text_other(message)
|
||
|
||
|
||
def _wrap_new(message: InboundMessage) -> None:
|
||
new_inquiry.handle_new_inquiry(message)
|
||
|
||
|
||
def _wrap_attachment(message: InboundMessage) -> None:
|
||
"""私聊附件询价:发卡等材料,认完当口播新开,复用文字流程。"""
|
||
handle_attachment_inquiry(message)
|
||
|
||
|
||
def _wrap_image(message: InboundMessage) -> None:
|
||
"""私聊图片询价:认图后当口播新开,复用文字流程。"""
|
||
handle_image_inquiry(message)
|
||
|
||
|
||
def _wrap_continue(message: InboundMessage, *, thread_id: str = "") -> None:
|
||
continue_thread.handle_continue_thread(message, thread_id=thread_id)
|
||
|
||
|
||
def _wrap_card(message: InboundMessage) -> None:
|
||
"""
|
||
卡片意图:把企微 TaskId / EventKey 原样交给流程,按当时工单号找回会话。
|
||
action 用正文(EventKey 或按钮文案),禁止写死 unknown。
|
||
"""
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
action = copy.canonical_button(message.content or "") or (message.content or "").strip()
|
||
payload = dict(message.raw or {})
|
||
payload.setdefault("event_key", message.content or "")
|
||
# 本次点击的入站消息号:出一张卡会被反复点,回执去重必须按「这一次点击」算,
|
||
# 不能只按工单+动作+正文,否则第二次点提交会被当成重复消息静默吞掉
|
||
# (销售看到按钮已置灰却收不到任何回复)。
|
||
if message.message_id:
|
||
payload.setdefault("inbound_msg_id", message.message_id)
|
||
if message.chat_id:
|
||
payload["chat_id"] = message.chat_id
|
||
payload["chat_type"] = message.chat_type
|
||
card_action.handle_card_action(
|
||
sender_id=message.sender_id,
|
||
thread_id="",
|
||
wait_version=0,
|
||
action=action or "unknown",
|
||
payload=payload,
|
||
)
|
||
|
||
|
||
def _wrap_close(message: InboundMessage) -> None:
|
||
close_deal.handle_close_deal(
|
||
sender_id=message.sender_id,
|
||
inquiry_no="",
|
||
outcome="unknown",
|
||
)
|
||
|
||
|
||
def _wrap_abnormal(message: InboundMessage) -> None:
|
||
abnormal.handle_abnormal(sender_id=message.sender_id, inquiry_no="", reason="dispatch")
|
||
|
||
|
||
def _wrap_end_inquiry(message: InboundMessage) -> None:
|
||
"""结束/终止询价:清上下文,允许新开。"""
|
||
handle_end_inquiry(message)
|
||
|
||
|
||
def _wrap_route_failed(message: InboundMessage) -> None:
|
||
"""模型没给出路由结论:请销售再说一次,不假装听懂。"""
|
||
from agent.channel.queue import get_message_store
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
get_message_store().enqueue_outbound(
|
||
touser=message.sender_id,
|
||
content=copy.ROUTE_MODEL_FAILED,
|
||
dedupe_key=f"route-fail:{message.message_id}",
|
||
)
|
||
|
||
|
||
def _wrap_route_model_failed(message: InboundMessage) -> None:
|
||
"""路由模型超时/报错/空结果:大模型异常(无工单不建单)。"""
|
||
from agent.policy.system_exception import confirm_llm_if_tech
|
||
from agent.ledger.http_ledger import HttpLedger
|
||
from agent.config import get_settings
|
||
from agent.channel.queue import get_message_store
|
||
|
||
def reply(text: str) -> None:
|
||
get_message_store().enqueue_outbound(
|
||
touser=message.sender_id,
|
||
content=text,
|
||
dedupe_key=f"route-llm:{message.message_id}",
|
||
)
|
||
|
||
ledger = None
|
||
try:
|
||
if (get_settings().ledger_backend or "").lower() != "memory":
|
||
ledger = HttpLedger()
|
||
except Exception:
|
||
ledger = None
|
||
confirm_llm_if_tech(
|
||
ledger=ledger,
|
||
reply=reply,
|
||
sender_id=message.sender_id,
|
||
reason="模型返回空结果",
|
||
conversation_kind="group" if getattr(message, "chat_id", "") else "private",
|
||
conversation_target=getattr(message, "chat_id", "") or message.sender_id,
|
||
)
|
||
|
||
|
||
# 意图 → handler(空壳或联调 echo);扩展时只改表,不改 ChannelRuntime 分支森林
|
||
HANDLER_REGISTRY: dict[str, HandlerFn] = {
|
||
"echo_text": _wrap_echo,
|
||
"ordinary_text_inquiry": _wrap_text_inquiry,
|
||
"ordinary_text_other": _wrap_text_other,
|
||
"quote_adjust": _wrap_text_inquiry,
|
||
"handoff_human": _wrap_text_inquiry,
|
||
"attachment_inquiry": _wrap_attachment,
|
||
"image_inquiry": _wrap_image,
|
||
# 多段联运与普通文字询价同一条闭环;禁止再走 new_inquiry 空壳(销售补字段会没回音)。
|
||
"multi_segment_transport": _wrap_text_inquiry,
|
||
"tms_read_only_quote": _wrap_continue,
|
||
"new_inquiry": _wrap_new,
|
||
"continue_thread": _wrap_continue,
|
||
"card_action": _wrap_card,
|
||
"close_deal": _wrap_close,
|
||
"end_inquiry": _wrap_end_inquiry,
|
||
"abnormal": _wrap_abnormal,
|
||
"reject_no_sender": _noop,
|
||
"ignore_empty": _noop,
|
||
"stub_needs_model": _wrap_echo,
|
||
"route_model_failed": _wrap_route_model_failed,
|
||
"route_parse_failed": _wrap_route_failed,
|
||
}
|
||
|
||
|
||
def _is_air_collab_room(message: InboundMessage) -> bool:
|
||
"""当前群已激活空运工单。查不到或海运群返回 False。"""
|
||
try:
|
||
from agent.handlers.air_group_collab import get_air_text_flow
|
||
from agent.policy.air_group_ops import bound_air_ticket
|
||
|
||
return bound_air_ticket(get_air_text_flow(), message.chat_id or "") is not None
|
||
except Exception:
|
||
logger.exception("判断空运协同群失败 chat=%s", getattr(message, "chat_id", ""))
|
||
return False
|
||
|
||
|
||
def _group_bot_text_is_dissolve(text: str) -> bool:
|
||
"""群内「解散群」不要求带工单号;只做宽松识别,精确判断仍交给 handler。"""
|
||
compact = "".join((text or "").split())
|
||
return "解散" in compact and "群" in compact
|
||
|
||
|
||
# handler 里「按人判不通过」的返回码:这些都是拿 sender 跟主账 sales_wecom_id / 岗位
|
||
# 名单比字符串得出的结论。只要入站身份是加密 open_userid 且换不成明文,这个结论就
|
||
# 不可信,必须允许会话存档(明文)那条补办,否则销售本人也会被一直拒。
|
||
_IDENTITY_REJECT_PHASES = frozenset(
|
||
{"not_sales", "not_airline", "not_product", "not_airline_or_product"}
|
||
)
|
||
|
||
|
||
def _sender_identity_untrusted(message: InboundMessage) -> bool:
|
||
"""
|
||
这条群入站的发送人身份是否不可信(加密 open_userid 且没换出明文)。
|
||
|
||
判定顺序:
|
||
1. 归一阶段已明确标 `sender_identity_trusted=0` → 不可信;
|
||
2. 否则看形态:`wo` 开头长 ID 说明这条还是加密值(归一关掉时也会走到这里)。
|
||
明文 userid 一律可信,保持改动前行为。
|
||
"""
|
||
raw = message.raw or {}
|
||
if str(raw.get("sender_identity_trusted") or "").strip() == "0":
|
||
return True
|
||
try:
|
||
from agent.channel.wecom.open_userid import looks_like_open_userid
|
||
|
||
return looks_like_open_userid(message.sender_id)
|
||
except Exception: # noqa: BLE001 - 判不出来就按可信处理,避免误放行存档
|
||
return False
|
||
|
||
|
||
def _allow_archive_retry_if_untrusted(
|
||
message: InboundMessage, *, chat_id: str, raw_text: str, phase: str
|
||
) -> None:
|
||
"""
|
||
AIBOT 那条因身份不可信而被拒时,撤销去重,让会话存档带着明文再办一遍。
|
||
|
||
为什么必须这样做(2026-11-19 真机 WO202611190016):
|
||
- AIBOT 先到,sender 是 `wo...` 加密值 → 判 not_sales;
|
||
- 存档晚 12~15 秒到,sender 是 `LuoXiaoHua` 明文,本来能办成;
|
||
- 但 AIBOT 那条已把文字指纹写进去重,存档被 `archive_dup_ignore` 丢掉,
|
||
销售于是只看到「只有负责该工单的销售可以确认成交状态」。
|
||
只对「按人判不通过」的返回码放行:办成了(发过模板/改过状态)不能放行,
|
||
否则同一句会被办两遍(2026-11-19 11:26 已经复现过双发模板)。
|
||
"""
|
||
if not chat_id or not raw_text:
|
||
return
|
||
if str(phase or "") not in _IDENTITY_REJECT_PHASES:
|
||
return
|
||
if not _sender_identity_untrusted(message):
|
||
return
|
||
from agent.redis_coord.group_bot import allow_archive_retry
|
||
|
||
allow_archive_retry(chat_id, raw_text)
|
||
logger.info(
|
||
"dispatch 身份不可信被拒,放行存档补办 chat=%s phase=%s", chat_id, phase
|
||
)
|
||
|
||
|
||
def _reply_group_need_work_order(message: InboundMessage) -> None:
|
||
"""
|
||
BOT 群 @ 了机器人却没带工单号、当前单也被打断:提示销售重新下达任务。
|
||
|
||
应用群不走这里(应用不回应群对话)。AIBOT 入站带 response_url/req_id,
|
||
由桥回话;会话存档兜底则用流程客户端,`send_group_text` 仍会按群改走 BOT。
|
||
"""
|
||
from agent.policy import inquiry_copy as copy
|
||
from agent.policy.sea_group_ops import send_group_text
|
||
|
||
chat_id = (message.chat_id or "").strip()
|
||
source = str((message.raw or {}).get("source") or "").lower()
|
||
if source in {"aibot", "aibot_bridge"}:
|
||
from agent.channel.aibot.reply import AibotReplyClient
|
||
|
||
client = AibotReplyClient(
|
||
str((message.raw or {}).get("response_url") or ""),
|
||
callback_req_id=str((message.raw or {}).get("aibot_req_id") or ""),
|
||
)
|
||
else:
|
||
from agent.policy.sea_text_flow import get_sea_text_flow
|
||
|
||
client = get_sea_text_flow().group_client()
|
||
prompt = copy.air_need_work_order()
|
||
send_group_text(client, chat_id, prompt)
|
||
|
||
|
||
def resolve_handler(intent: str) -> Optional[HandlerFn]:
|
||
"""查表;未知意图返回 None。"""
|
||
return HANDLER_REGISTRY.get(intent)
|
||
|
||
|
||
def _mark_explicit_attachment(message: InboundMessage) -> InboundMessage:
|
||
"""
|
||
给「明确 @ 过机器人」才进来的群附件打标记,供 handler 决定「必须回一句」。
|
||
|
||
为什么需要(2026-11-21 真机 WO 群):
|
||
海运/陆运群的附件默认口径是「别人的附件只存档、不搭话」(2026-11-19 定的),
|
||
但那是针对「群里随手丢文件」。若销售是先/后 @ 了机器人(本次指令窗口内),
|
||
这条附件就是他明确要求办的事,处理完必须回一句,
|
||
否则真机表现就是「发文件 + @机器人,BOT 一声不吭」。
|
||
|
||
实现:InboundMessage 是 frozen dataclass,用 replace 复制一份并在 raw 里加标记,
|
||
不改原对象,也不引入新字段(raw 本来就是通道透传字段,序列化不受影响)。
|
||
|
||
副作用:无;纯内存复制。
|
||
"""
|
||
from dataclasses import replace
|
||
|
||
raw = dict(message.raw or {})
|
||
raw["at_burst_explicit"] = True
|
||
return replace(message, raw=raw)
|
||
|
||
|
||
@contextmanager
|
||
def _group_attach_outbound(chat_id: str) -> Iterator[None]:
|
||
"""
|
||
群附件回执用哪个身份发:BOT 真在这个群就走机器人桥,否则走企微应用。
|
||
|
||
为什么必须按群判断(2026-11-21 真机):
|
||
存档群附件原先一律 `AibotReplyClient("")`(机器人桥主动发言)。但
|
||
「@会话存档机器人」这种群,AIBOT 根本没进群(bot_seen=False),桥主动发言
|
||
被企微拒:`aibot send msg frequency limit exceeded`(84660)/
|
||
`websocket request expired`,群里表现就是「发文件 + @机器人,BOT 一声不吭」。
|
||
同一群的文字兜底走的是应用发群、是通的,所以附件回执也必须跟着走应用。
|
||
|
||
参数 chat_id:群 chat_id,用来判断 BOT 是否见于该群。
|
||
副作用:只改当前线程的出站客户端(ContextVar)与「强制应用」标记,退出即还原。
|
||
线程:入站处理线程内同步使用,不影响别的工单。
|
||
"""
|
||
from agent.redis_coord.group_bot import bot_seen
|
||
|
||
in_room = bot_seen(chat_id)
|
||
from agent.policy.sea_group_ops import force_app_group_client, override_group_client
|
||
|
||
if in_room:
|
||
from agent.channel.aibot.reply import AibotReplyClient
|
||
|
||
with override_group_client(AibotReplyClient("")):
|
||
yield
|
||
return
|
||
# BOT 不在群:只能走应用发群;此时禁止被换成机器人桥(走桥必失败)。
|
||
from agent.policy.sea_text_flow import get_sea_text_flow
|
||
|
||
with override_group_client(get_sea_text_flow().group_client()), force_app_group_client():
|
||
yield
|
||
|
||
|
||
def _replay_pending_media(chat_id: str, line: str) -> int:
|
||
"""
|
||
@BOT 一到,就把之前「窗口外先到」的群图片/附件补办一次。
|
||
|
||
为什么必须有这一步(2026-11-21 真机):
|
||
企微群里 Excel/PDF/图片都不能被 @,销售实际习惯是「先发文件,再 @机器人」。
|
||
只看「先 @ 后发」会把这类真业务整条静默丢掉(真机表现为 BOT 一声不吭)。
|
||
缓冲内容存在 group_bot:pending_media,由 dispatch 在窗口外丢弃时写入。
|
||
|
||
返回:补办的条数(0 表示这次 @ 前面没有待归属材料)。
|
||
副作用:逐条重放 handler(可能发群消息、写主账附件);单条失败不影响其它条。
|
||
线程:只在入站处理线程里同步跑,和普通群消息同一条路径,不额外起线程。
|
||
"""
|
||
from agent.redis_coord.group_bot import bot_seen, drain_pending_media
|
||
|
||
entries = drain_pending_media(chat_id)
|
||
if not entries:
|
||
return 0
|
||
from agent.channel.aibot.reply import AibotReplyClient
|
||
from agent.policy.sea_group_ops import force_app_group_client, override_group_client
|
||
|
||
done = 0
|
||
# 出站身份跟着群状态走:BOT 在群用桥主动发言,否则用应用发群(同 seat@ 兜底)。
|
||
bot_in_room = bot_seen(chat_id)
|
||
for entry in entries:
|
||
try:
|
||
msg = InboundMessage.from_dict(entry)
|
||
except Exception: # noqa: BLE001 - 坏数据跳过,不能挡住后面那条指令
|
||
logger.warning("dispatch 待归属附件反序列化失败 chat=%s", chat_id, exc_info=True)
|
||
continue
|
||
try:
|
||
# 明确 @ 过才补办的附件:打标记让 handler 处理完必须回一句(不能静默存档)。
|
||
msg = _mark_explicit_attachment(msg)
|
||
if bot_in_room:
|
||
with override_group_client(AibotReplyClient("")):
|
||
phase = _seat_at_dispatch(msg, line)
|
||
else:
|
||
from agent.policy.sea_text_flow import get_sea_text_flow
|
||
|
||
with override_group_client(
|
||
get_sea_text_flow().group_client()
|
||
), force_app_group_client():
|
||
phase = _seat_at_dispatch(msg, line)
|
||
done += 1
|
||
logger.info(
|
||
"dispatch pending_media_replay chat=%s kind=%s phase=%s",
|
||
chat_id,
|
||
msg.msg_type,
|
||
phase,
|
||
)
|
||
except Exception: # noqa: BLE001
|
||
logger.exception("dispatch 待归属附件补办失败 chat=%s", chat_id)
|
||
return done
|
||
|
||
|
||
def _handle_seat_at_fallback(
|
||
message: InboundMessage,
|
||
*,
|
||
chat_id: str,
|
||
line: str,
|
||
raw_text: str,
|
||
wo_in_text: str,
|
||
) -> str:
|
||
"""
|
||
@会话存档账号兜底唤醒(2026-11-19 产品口径)。
|
||
|
||
背景:
|
||
- 企业微信智能机器人(智价通BOT)不能被接口拉进群,每个协同群都得人工拉一次;
|
||
- 会话存档席位账号(群里显示「会话存档-机器人」)是通讯录成员,建群时自动进群,
|
||
而且它本来就负责存档,所以 @ 它的文字一定会出现在存档里。
|
||
|
||
因此:存档听到「@会话存档账号」,就当作一次明确的任务指令交给 handler。
|
||
出站通道按群状态二选一(2026-11-19 调整):
|
||
- BOT 在群 ⇒ 用 BOT 身份回(桥主动发言),群里只有一套回话身份;
|
||
- BOT 不在群 ⇒ 走企微应用 appchat/send。
|
||
|
||
门禁:
|
||
- 已经 @ 了 BOT 的名字 ⇒ 交给 AIBOT 那条路,本函数不接(避免同一句办两遍);
|
||
- BOT 在群时也允许 @席位 唤醒:AIBOT 只在自己被 @ 时收到回调,
|
||
@席位 不会同时触发 BOT,所以不存在双回。
|
||
|
||
副作用:可能写当前单关联、调主账、发群消息;都在 handler 内,本函数不额外写状态。
|
||
"""
|
||
from dataclasses import replace
|
||
|
||
from agent.policy.bot_name import strip_bot_mention
|
||
from agent.redis_coord.group_bot import bot_seen, set_active_work_order
|
||
|
||
bot_in_room = bot_seen(chat_id)
|
||
if wo_in_text:
|
||
set_active_work_order(chat_id, wo_in_text)
|
||
|
||
# 把「@会话存档-机器人」从正文剥掉,避免账号名被当成品名/报价内容抽进字段。
|
||
clean = strip_bot_mention(raw_text)
|
||
if not clean:
|
||
# 只 @ 了个账号、没有任何指令:不当任务,也不回话。
|
||
logger.info("dispatch seat_at_empty_ignore chat=%s", chat_id)
|
||
return "seat_at_empty_ignore"
|
||
body = replace(message, content=clean) if clean != raw_text else message
|
||
|
||
from agent.policy.sea_group_ops import (
|
||
force_app_group_client,
|
||
override_group_client,
|
||
)
|
||
from agent.policy.sea_text_flow import get_sea_text_flow
|
||
|
||
app_client = get_sea_text_flow().group_client()
|
||
try:
|
||
if bot_in_room:
|
||
# BOT 已在群:用 BOT 身份回,否则同一个群会出现两套回话身份。
|
||
from agent.channel.aibot.reply import AibotReplyClient
|
||
|
||
with override_group_client(AibotReplyClient("")):
|
||
phase = _seat_at_dispatch(body, line)
|
||
else:
|
||
# BOT 不在群:只能走应用发群;此时禁止被换成机器人桥(走桥必失败)。
|
||
with override_group_client(app_client), force_app_group_client():
|
||
phase = _seat_at_dispatch(body, line)
|
||
except Exception:
|
||
logger.exception("dispatch seat@ fallback 失败 chat=%s", chat_id)
|
||
return "seat_at_fallback_error"
|
||
logger.info(
|
||
"dispatch seat@ fallback phase=%s chat=%s line=%s bot_in_room=%s",
|
||
phase,
|
||
chat_id,
|
||
line,
|
||
bot_in_room,
|
||
)
|
||
return "seat_at_fallback"
|
||
|
||
|
||
def _seat_at_dispatch(message: InboundMessage, line: str) -> str:
|
||
"""
|
||
@席位 兜底:按业务线交给唯一 handler。
|
||
|
||
海运/陆运/多式走 group_collab;空运走 air_group_collab。
|
||
单独抽函数是为了让「出站通道选择」与「业务分发」不搅在一起。
|
||
"""
|
||
if line in {"SEA", "LAND", "MULTI"}:
|
||
return handle_group_collab(message)
|
||
from agent.handlers.air_group_collab import handle_air_group
|
||
|
||
return handle_air_group(message)
|
||
|
||
|
||
def dispatch_inbound(message: InboundMessage, decision: RouteDecision) -> str:
|
||
"""
|
||
按 RouteDecision 调用唯一 handler。
|
||
|
||
返回:实际调用的 intent 名,或 unknown_intent / skipped。
|
||
副作用:仅 handler 内产生;本函数不做业务。
|
||
"""
|
||
if (message.chat_type or "") == "group":
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
event = str((message.raw or {}).get("event") or "").lower()
|
||
raw_text = (message.content or "").strip()
|
||
# 只有真点卡才走成交卡;群里打「成交」两个字要当文字成交,不能当私聊点卡丢掉。
|
||
is_card_event = event in {"template_card_event", "click", "templatecardevent"}
|
||
is_card_key = raw_text.startswith(
|
||
(copy.BTN_DEAL_KEY + ":", copy.BTN_LOST_KEY + ":", copy.BTN_NEGOTIATE_KEY + ":")
|
||
)
|
||
if is_card_event or is_card_key:
|
||
_wrap_card(message)
|
||
return "card_action"
|
||
source = str((message.raw or {}).get("source") or "").lower()
|
||
kind = (message.msg_type or "text").lower()
|
||
chat_id = (message.chat_id or "").strip()
|
||
from agent.policy.inquiry_copy import extract_work_order_no
|
||
|
||
wo_in_text = extract_work_order_no(raw_text) if raw_text else ""
|
||
from agent.redis_coord.group_bot import (
|
||
bot_text_handled,
|
||
clear_active_work_order,
|
||
clear_at_burst,
|
||
ensure_group_bot_line,
|
||
get_active_work_order,
|
||
group_bot_line,
|
||
in_at_burst,
|
||
is_group_bot,
|
||
mark_at_burst,
|
||
mark_bot_text_handled,
|
||
set_active_work_order,
|
||
)
|
||
|
||
# Bot 群标记:新群建群时写业务线;改造前的老群第一次收到 BOT 消息时按主账补齐。
|
||
line = group_bot_line(chat_id)
|
||
if source in {"aibot", "aibot_bridge"}:
|
||
line = ensure_group_bot_line(chat_id)
|
||
# 收到 AIBOT 回调 = 智价通BOT 确实在本群。
|
||
# @会话存档账号的兜底唤醒只在「BOT 不在群」时生效(2026-11-19 产品口径),
|
||
# 这个标记就是判断依据;Redis 只能短存,丢标记会让兜底多管一次,不致回错。
|
||
try:
|
||
from agent.redis_coord.group_bot import mark_bot_seen
|
||
|
||
mark_bot_seen(chat_id)
|
||
except Exception: # noqa: BLE001
|
||
logger.warning("dispatch 记 BOT 在群失败 chat=%s", chat_id, exc_info=True)
|
||
if wo_in_text:
|
||
# @BOT 且带工单号:恢复/切换本群当前单关联。
|
||
set_active_work_order(chat_id, wo_in_text)
|
||
if source in {"aibot", "aibot_bridge"} and (kind in {"", "text"} or raw_text):
|
||
# 产品口径(2026-11-21):AIBOT 只在被 @ 时才收到群消息,
|
||
# 所以收到即代表本群进入「@BOT 指令窗口」,后面的附件/图片算这次的材料。
|
||
mark_at_burst(chat_id)
|
||
# 先发文件、再 @BOT 的顺序:把缓冲区里的附件补办掉,再办这句指令。
|
||
_replay_pending_media(chat_id, line)
|
||
if raw_text:
|
||
# 记下这条已由 AIBOT 办过:会话存档会旁听到同一句(空运必现),
|
||
# 不记就会把同一份摘要/模板发两遍。
|
||
mark_bot_text_handled(chat_id, raw_text)
|
||
# 群里 @BOT 带图片:企微只推 mixed(文字+图片),图片是加密临时 URL,本链路不下载。
|
||
# 只有图没有字时,handler 会把空正文当文字去要工单号,等于对着图片回话;
|
||
# 产品口径(2026-11-19):这类整条静默,不入库也不提示。
|
||
if kind in {"mixed", "image", "video", "file"} and not raw_text:
|
||
logger.info(
|
||
"dispatch aibot_group_media_ignore chat=%s kind=%s", chat_id, kind
|
||
)
|
||
return "aibot_group_media_only_ignore"
|
||
if line in {"SEA", "LAND", "MULTI"}:
|
||
if (
|
||
kind in {"", "text"}
|
||
and raw_text
|
||
and not wo_in_text
|
||
and not get_active_work_order(chat_id)
|
||
and not _group_bot_text_is_dissolve(raw_text)
|
||
):
|
||
_reply_group_need_work_order(message)
|
||
logger.info("dispatch group_need_work_order chat=%s line=%s", chat_id, line)
|
||
return "group_need_work_order"
|
||
from agent.channel.aibot.reply import AibotReplyClient
|
||
from agent.policy.sea_group_ops import override_group_client
|
||
|
||
client = AibotReplyClient(
|
||
str((message.raw or {}).get("response_url") or ""),
|
||
callback_req_id=str((message.raw or {}).get("aibot_req_id") or ""),
|
||
)
|
||
with override_group_client(client):
|
||
phase = handle_group_collab(
|
||
_mark_explicit_attachment(message)
|
||
if kind in {"file", "image", "video", "mixed"}
|
||
else message
|
||
)
|
||
_allow_archive_retry_if_untrusted(
|
||
message, chat_id=chat_id, raw_text=raw_text, phase=phase
|
||
)
|
||
logger.info(
|
||
"dispatch group_collab bot phase=%s chat=%s line=%s",
|
||
phase,
|
||
chat_id,
|
||
line,
|
||
)
|
||
return "group_collab"
|
||
# 纯空运群仍走空运 handler;当前单被非 @ 文字打断后,没带号也先提示。
|
||
if (
|
||
kind in {"", "text"}
|
||
and raw_text
|
||
and not wo_in_text
|
||
and not get_active_work_order(chat_id)
|
||
and not _group_bot_text_is_dissolve(raw_text)
|
||
):
|
||
_reply_group_need_work_order(message)
|
||
logger.info("dispatch air_need_work_order chat=%s", chat_id)
|
||
return "group_need_work_order"
|
||
from agent.handlers.air_group_collab import handle_air_group
|
||
|
||
phase = handle_air_group(
|
||
_mark_explicit_attachment(message)
|
||
if kind in {"file", "image", "video", "mixed"}
|
||
else message
|
||
)
|
||
_allow_archive_retry_if_untrusted(
|
||
message, chat_id=chat_id, raw_text=raw_text, phase=phase
|
||
)
|
||
logger.info("dispatch air_group phase=%s chat=%s", phase, chat_id)
|
||
return "air_group_collab"
|
||
|
||
if source == "wshoto_archive":
|
||
# 老群升级(2026-11-19 真机):本轮之前建的协同群是应用建的,Redis 里没有
|
||
# BOT 标记,存档消息会落到下面「应用代答」的旧分支 —— 表现为「没 @ 也主动回话」。
|
||
# 先按主账绑群关系补标记;补不上(不是本环境的群,如正服群被存档旁听)就整条不回应。
|
||
if not is_group_bot(chat_id):
|
||
try:
|
||
promoted = ensure_group_bot_line(chat_id, fallback="")
|
||
except Exception: # noqa: BLE001
|
||
promoted = ""
|
||
logger.exception("存档群补 BOT 标记失败 chat=%s", chat_id)
|
||
if promoted:
|
||
line = promoted
|
||
if not is_group_bot(chat_id):
|
||
# 应用只管拉群/解散群,不代答群对话;未绑定工单的群一律静默。
|
||
logger.info("dispatch archive_unbound_ignore chat=%s", chat_id)
|
||
return "archive_unbound_ignore"
|
||
if is_group_bot(chat_id):
|
||
if kind in {"text", ""}:
|
||
from agent.policy.bot_name import (
|
||
text_mentions_archive_seat,
|
||
text_mentions_bot,
|
||
)
|
||
|
||
bot_at = text_mentions_bot(raw_text)
|
||
seat_at = text_mentions_archive_seat(raw_text)
|
||
if bot_at or seat_at:
|
||
# 产品口径(2026-11-21):存档听见明文的 @BOT(AIBOT 漏推时)也算
|
||
# 进入「@BOT 指令窗口」,后面的附件/图片才算这次指令的材料。
|
||
# @会话存档账号的兜底唤醒同样是明确指令,窗口一并开启,
|
||
# 否则「喊醒后发文件」这种老用法会被窗口挡掉。
|
||
mark_at_burst(chat_id)
|
||
# 「先发文件、再 @机器人」顺序同样成立:把缓冲区里的附件先补办。
|
||
_replay_pending_media(chat_id, line)
|
||
# @会话存档账号兜底唤醒:智能机器人不能自动进群,
|
||
# 这是唯一「不用人工拉 BOT 也能喊醒」的入口。
|
||
if seat_at and not bot_at:
|
||
return _handle_seat_at_fallback(
|
||
message,
|
||
chat_id=chat_id,
|
||
line=line,
|
||
raw_text=raw_text,
|
||
wo_in_text=wo_in_text,
|
||
)
|
||
if not bot_at:
|
||
# 产品口径(2026-11-20):「解散群」不要求 @BOT;存档听见就走解散。
|
||
# 其它非 @BOT 文字仍断开当前单关联,且不回应。
|
||
if _group_bot_text_is_dissolve(raw_text):
|
||
if line in {"SEA", "LAND", "MULTI"}:
|
||
from agent.channel.aibot.reply import AibotReplyClient
|
||
from agent.policy.sea_group_ops import override_group_client
|
||
|
||
with override_group_client(AibotReplyClient("")):
|
||
phase = handle_group_collab(message)
|
||
logger.info(
|
||
"dispatch group_dissolve_no_at archive phase=%s chat=%s",
|
||
phase,
|
||
chat_id,
|
||
)
|
||
return "group_collab"
|
||
from agent.handlers.air_group_collab import handle_air_group
|
||
|
||
phase = handle_air_group(message)
|
||
logger.info(
|
||
"dispatch air_dissolve_no_at archive phase=%s chat=%s",
|
||
phase,
|
||
chat_id,
|
||
)
|
||
return "air_group_collab"
|
||
if not bot_text_handled(chat_id, raw_text):
|
||
# 普通文字(非 @BOT)=这次指令结束:之后的附件必须重新 @BOT。
|
||
# 存档回声(AIBOT 刚办过的同一句)不算,否则 @ 完立刻被自己清掉。
|
||
clear_at_burst(chat_id)
|
||
# 指令已结束,还没被 @ 认领的旧附件也不能留到下一次 @ 再翻出来。
|
||
from agent.redis_coord.group_bot import drain_pending_media
|
||
|
||
drain_pending_media(chat_id)
|
||
clear_active_work_order(chat_id)
|
||
logger.info("dispatch group_unbound chat=%s", chat_id)
|
||
return "group_unbound_ignore"
|
||
if wo_in_text:
|
||
set_active_work_order(chat_id, wo_in_text)
|
||
if line in {"SEA", "LAND", "MULTI"}:
|
||
# 文字由 AIBOT 回调负责;存档只补附件,避免同一句回两遍。
|
||
logger.info(
|
||
"dispatch group_at_archive_ignore chat=%s line=%s", chat_id, line
|
||
)
|
||
return "group_at_archive_ignore"
|
||
# 空运保留「BOT 漏推时存档补听」;当前单断了也先提示。
|
||
if not wo_in_text and not get_active_work_order(chat_id):
|
||
_reply_group_need_work_order(message)
|
||
logger.info("dispatch air_need_work_order archive chat=%s", chat_id)
|
||
return "group_need_work_order"
|
||
elif line in {"SEA", "LAND", "MULTI"} and kind == "file":
|
||
# 附件不能 @:BOT 群附件仍靠会话存档听见,出站改走机器人桥。
|
||
# 产品口径(2026-11-21):只有「@BOT 指令窗口」内的附件才算这次询价材料;
|
||
# 窗口外(同事随手丢的报价表、别人的对账单)一律静默,不回话也不识别。
|
||
if not in_at_burst(chat_id):
|
||
logger.info(
|
||
"dispatch group_media_no_at_ignore kind=%s chat=%s line=%s",
|
||
kind,
|
||
chat_id,
|
||
line,
|
||
)
|
||
# 产品口径(2026-11-21)「@ 前后」:先发文件、再 @BOT 也算这次指令。
|
||
# 先静默不回,但把整条入站缓冲下来,等紧接着的 @BOT 认领补办。
|
||
from agent.redis_coord.group_bot import add_pending_media
|
||
|
||
add_pending_media(chat_id, message.to_dict())
|
||
return "group_media_no_at_ignore"
|
||
with _group_attach_outbound(chat_id):
|
||
phase = handle_group_collab(_mark_explicit_attachment(message))
|
||
logger.info(
|
||
"dispatch group_collab archive_file phase=%s chat=%s",
|
||
phase,
|
||
chat_id,
|
||
)
|
||
return "group_collab"
|
||
elif line in {"SEA", "LAND", "MULTI"}:
|
||
# 产品口径(2026-11-19 真机):图片/视频不是报价附件,没 @BOT 就不回应。
|
||
# 以前这里会把图片当附件丢给 handler,回一句「群里只收 Excel 和 PDF」,
|
||
# 群里随手发张图就被 BOT 搭话。
|
||
# 2026-11-21 补充:@BOT 指令窗口内的图片属于「这次要发的材料」,
|
||
# 交回 handler 按附件口径回应(明确告知只收 Excel/PDF),不能整条吞掉。
|
||
if in_at_burst(chat_id):
|
||
with _group_attach_outbound(chat_id):
|
||
phase = handle_group_collab(_mark_explicit_attachment(message))
|
||
logger.info(
|
||
"dispatch group_collab archive_media_at phase=%s chat=%s kind=%s",
|
||
phase,
|
||
chat_id,
|
||
kind,
|
||
)
|
||
return "group_collab"
|
||
logger.info(
|
||
"dispatch group_archive_media_ignore kind=%s chat=%s line=%s",
|
||
kind,
|
||
chat_id,
|
||
line,
|
||
)
|
||
# 同上:先静默不回,但缓冲下来等紧接着的 @BOT 认领(图片也可能是这次的材料)。
|
||
from agent.redis_coord.group_bot import add_pending_media
|
||
|
||
add_pending_media(chat_id, message.to_dict())
|
||
return "group_archive_media_ignore"
|
||
# 改造前的应用群:维持原逻辑(应用建群、应用发群、存档收报价)。
|
||
air_room = _is_air_collab_room(message)
|
||
# 会话存档是公司级数据:测服与正服共用同一企微公司,测服也会捞到正服群
|
||
# (2026-11-19 11:10 测服对正服空运群连回三条「查不到工单」的事故)。
|
||
archive_origin = str((message.raw or {}).get("wshoto_origin") or "").strip().lower()
|
||
if kind in {"text", ""}:
|
||
from agent.config import get_settings
|
||
from agent.routing.air_group_intent import is_missed_air_bot_at
|
||
|
||
# 测服兜底:存档旁听 + 群没有在测服建单 ⇒ 不是测服的群,不 @ 也不回「查不到」。
|
||
# 本机 AIBOT 回调(origin 非 archive)不受限,测服自己的协同群仍能补建工单。
|
||
test_cross_env = (
|
||
str(getattr(get_settings(), "ytd_env", "") or "").strip().lower() == "test"
|
||
and archive_origin == "archive"
|
||
and not air_room
|
||
and not is_group_bot(chat_id)
|
||
)
|
||
missed_at = (
|
||
is_missed_air_bot_at(raw_text, room_already_air=air_room) and not test_cross_env
|
||
)
|
||
if missed_at:
|
||
# AIBOT 已经办过同一句:存档这条是旁听重复,不能再办一遍。
|
||
if bot_text_handled(chat_id, raw_text):
|
||
logger.info(
|
||
"dispatch air_group archive_dup_ignore chat=%s wo=%s",
|
||
chat_id,
|
||
wo_in_text,
|
||
)
|
||
return "air_group_archive_dup_ignore"
|
||
from agent.handlers.air_group_collab import handle_air_group
|
||
|
||
phase = handle_air_group(message)
|
||
logger.info("dispatch air_group archive_at phase=%s chat=%s", phase, chat_id)
|
||
return "air_group_collab"
|
||
if air_room:
|
||
if kind == "file":
|
||
from agent.handlers.air_group_collab import handle_air_group
|
||
|
||
# 产品口径(2026-11-21):BOT 群里的附件必须处在「@BOT 指令窗口」内,
|
||
# 否则按静默处理;老式应用群(没有 BOT)仍保持存档补收报价的老逻辑。
|
||
if is_group_bot(chat_id) and not in_at_burst(chat_id):
|
||
logger.info(
|
||
"dispatch air_group media_no_at_ignore chat=%s kind=%s",
|
||
chat_id,
|
||
kind,
|
||
)
|
||
# 同上:空运 BOT 群里先发、后 @ 的附件也要能认领。
|
||
from agent.redis_coord.group_bot import add_pending_media
|
||
|
||
add_pending_media(chat_id, message.to_dict())
|
||
return "group_media_no_at_ignore"
|
||
phase = handle_air_group(message)
|
||
logger.info("dispatch air_group archive_file phase=%s chat=%s", phase, chat_id)
|
||
return "air_group_collab"
|
||
if kind in {"image", "video"}:
|
||
# 图片/视频不是报价附件;没 @BOT 一律不回应(2026-11-19 真机)。
|
||
logger.info(
|
||
"dispatch air_group archive_media_ignore kind=%s chat=%s", kind, chat_id
|
||
)
|
||
return "air_archive_media_ignore"
|
||
# 产品口径(2026-11-20):解散群不要求 @BOT。
|
||
if kind in {"text", ""} and _group_bot_text_is_dissolve(raw_text):
|
||
from agent.handlers.air_group_collab import handle_air_group
|
||
|
||
phase = handle_air_group(message)
|
||
logger.info(
|
||
"dispatch air_dissolve_no_at legacy_archive phase=%s chat=%s",
|
||
phase,
|
||
chat_id,
|
||
)
|
||
return "air_group_collab"
|
||
logger.info("dispatch 空运群存档文字忽略,须 @ BOT chat=%s", chat_id)
|
||
return "air_archive_text_ignore"
|
||
phase = handle_group_collab(message)
|
||
logger.info("dispatch group_collab phase=%s chat=%s", phase, chat_id)
|
||
return "group_collab"
|
||
|
||
# BOT 群的应用回调只接卡片事件(上面已处理);其它群文字一律不回应。
|
||
if is_group_bot(chat_id):
|
||
if kind in {"text", ""}:
|
||
clear_active_work_order(chat_id)
|
||
logger.info("dispatch group_non_bot_ignore chat=%s source=%s", chat_id, source or "-")
|
||
return "group_non_bot_ignore"
|
||
phase = handle_group_collab(message)
|
||
logger.info("dispatch group_collab phase=%s chat=%s", phase, chat_id)
|
||
return "group_collab"
|
||
|
||
if not message.has_sender() and decision.intent != "reject_no_sender":
|
||
logger.warning("dispatch 拒绝:无 sender_id")
|
||
HANDLER_REGISTRY["reject_no_sender"](message)
|
||
return "reject_no_sender"
|
||
|
||
kind = (message.msg_type or "text").lower()
|
||
if kind == "image":
|
||
from agent.handlers.attachment_inquiry import is_active_attachment_wait, join_attachment_image
|
||
|
||
if is_active_attachment_wait(message.sender_id):
|
||
join_attachment_image(message)
|
||
return "attachment_inquiry"
|
||
handle_image_inquiry(message)
|
||
return "image_inquiry"
|
||
if kind in {"text", ""}:
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
# 结束询价优先于附件/图片波次并字:否则「结束询价」会被当成补字段。
|
||
if copy.looks_like_end_inquiry(message.content or ""):
|
||
handle_end_inquiry(message)
|
||
return "end_inquiry"
|
||
from agent.handlers.attachment_inquiry import (
|
||
abandon_idle_attach_wait,
|
||
append_attach_text,
|
||
is_active_attachment_wait,
|
||
is_idle_attachment_wait,
|
||
)
|
||
from agent.handlers.image_inquiry import append_wave_text, is_active_image_wave
|
||
|
||
if is_active_attachment_wait(message.sender_id) and (message.content or "").strip():
|
||
if decision.intent == "attachment_inquiry":
|
||
handle_attachment_inquiry(message)
|
||
return "attachment_inquiry"
|
||
if is_idle_attachment_wait(message.sender_id):
|
||
abandon_idle_attach_wait(message.sender_id)
|
||
logger.info(
|
||
"dispatch 附件未传材料,改走文字 sender=%s intent=%s",
|
||
message.sender_id,
|
||
decision.intent,
|
||
)
|
||
else:
|
||
append_attach_text(message.sender_id, message.content or "")
|
||
logger.info("dispatch 附件波次收字 sender=%s", message.sender_id)
|
||
return "attach_wave_follow_text"
|
||
if is_active_image_wave(message.sender_id) and (message.content or "").strip():
|
||
append_wave_text(message.sender_id, message.content or "")
|
||
logger.info("dispatch 图片波次收字 sender=%s", message.sender_id)
|
||
return "image_wave_follow_text"
|
||
|
||
# 未成交原因优先于模型给的「结束询价」。
|
||
# 为什么(2026-11-17 正服 WO202611170021):销售点完「未成交」后,BOT 正在问
|
||
# 「请补充未成交原因」,销售回「客户不要了」;DeepSeek A 把这句话听成
|
||
# 「这单先不算了」,标了 end_inquiry。结束询价 handler 对「未成交」不会改六态,
|
||
# 但会直接把私聊书签清掉,于是这句原因永远进不了主账,后台一直空着。
|
||
# 产品口径是「未成交后下一句任意文字即原因」,所以这里把路由结论纠正回询价流程。
|
||
# 明确口令「结束询价 / 终止询价」已在上面优先返回,不受本保护影响。
|
||
if kind in {"text", ""} and decision.intent == "end_inquiry":
|
||
wait = lost_reason_wait_state(message.sender_id)
|
||
if wait.get("waiting"):
|
||
logger.info(
|
||
"dispatch 未成交原因优先:纠正结束询价路由 sender=%s wo=%s",
|
||
message.sender_id,
|
||
str(wait.get("work_order_no") or "-"),
|
||
)
|
||
handle_text_inquiry(message)
|
||
return "ordinary_text_inquiry"
|
||
|
||
fn = resolve_handler(decision.intent)
|
||
if fn is None:
|
||
logger.warning("dispatch 未知意图 intent=%s → noop", decision.intent)
|
||
_noop(message)
|
||
return "unknown_intent"
|
||
|
||
|
||
logger.info(
|
||
"dispatch intent=%s sender=%s msg=%s wo=%s land_confirm=%s",
|
||
decision.intent,
|
||
message.sender_id,
|
||
message.message_id,
|
||
decision.thread_id or "-",
|
||
is_land_confirm_action(message) if message.has_sender() else False,
|
||
)
|
||
if decision.intent in {
|
||
"ordinary_text_other",
|
||
"route_model_failed",
|
||
"route_parse_failed",
|
||
} and is_land_confirm_action(message):
|
||
logger.info("dispatch 陆运核对卡确定:纠正闲聊路由 sender=%s", message.sender_id)
|
||
handle_text_inquiry(message)
|
||
return "ordinary_text_inquiry"
|
||
if decision.intent == "ordinary_text_other" and should_resume_other_as_inquiry(message):
|
||
logger.info("dispatch 补问续填:纠正闲聊路由 sender=%s", message.sender_id)
|
||
handle_text_inquiry(message)
|
||
return "ordinary_text_inquiry"
|
||
if decision.intent == "quote_adjust":
|
||
logger.info("dispatch 调价意图 sender=%s", message.sender_id)
|
||
handle_text_inquiry(message, prefer_quoted=True)
|
||
return "quote_adjust"
|
||
if decision.intent == "handoff_human":
|
||
logger.info("dispatch 转人工意图 sender=%s", message.sender_id)
|
||
handle_text_inquiry(message, prefer_handoff=True)
|
||
return "handoff_human"
|
||
if decision.intent == "continue_thread":
|
||
continue_thread.handle_continue_thread(message, thread_id=decision.thread_id)
|
||
return decision.intent
|
||
fn(message)
|
||
return decision.intent
|