为什么改:应用建的协同群只能代答,销售在群里发文件、@人、问报价都对不上人; 本次把群对话交给智价通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>
176 lines
5.8 KiB
Python
176 lines
5.8 KiB
Python
"""
|
||
意图路由:DeepSeek A → 唯一 RouteDecision。
|
||
|
||
禁止:在本包产生业务副作用(建单、发消息、改六态)。
|
||
正式路径:DeepSeekARouter(strict tool);route_stub 仅兼容旧自检名。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from dataclasses import dataclass
|
||
from typing import Optional
|
||
|
||
from agent.llm import LlmGateway, LlmMode
|
||
from agent.routing.decision import RouteDecision
|
||
from agent.routing.deepseek_a import DeepSeekARouter
|
||
from agent.routing.dispatch import HANDLER_REGISTRY, dispatch_inbound, resolve_handler
|
||
from agent.routing.ordered_actions import (
|
||
ORDERED_ACTIONS_TOOL_NAME,
|
||
OrderedActionsError,
|
||
OrderedActionsResult,
|
||
ordered_actions_tool_definition,
|
||
parse_ordered_actions_arguments,
|
||
)
|
||
|
||
# IntentRouter 定义见下
|
||
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
@dataclass
|
||
class IntentRouter:
|
||
"""
|
||
路由门面。
|
||
|
||
- route():优先 DeepSeek A(默认 stub,不开费)
|
||
- enqueue_ordered_actions():快回路径入 LLM Stream
|
||
"""
|
||
|
||
llm: Optional[LlmGateway] = None
|
||
deepseek_a: Optional[DeepSeekARouter] = None
|
||
allow_network: bool = False
|
||
|
||
def _a(self) -> DeepSeekARouter:
|
||
if self.deepseek_a is not None:
|
||
return self.deepseek_a
|
||
return DeepSeekARouter.from_settings(allow_network=self.allow_network)
|
||
|
||
def route_stub(self, *, text: str, sender_id: str) -> RouteDecision:
|
||
"""
|
||
兼容旧名:等价于无网络的 A stub fixture。
|
||
|
||
正式生产请用 route() 并在 Worker 开 allow_network + 数据确认。
|
||
"""
|
||
return self._a().route_stub_fixture(text=text, sender_id=sender_id)
|
||
|
||
def route(
|
||
self,
|
||
*,
|
||
text: str,
|
||
sender_id: str,
|
||
session_quoted: bool = False,
|
||
fee_names: Optional[list[str]] = None,
|
||
work_order_no: str = "",
|
||
) -> RouteDecision:
|
||
"""一条入站一个 RouteDecision。已报价时把书签事实交给 A 认调价。"""
|
||
return self._a().route(
|
||
text=text,
|
||
sender_id=sender_id,
|
||
session_quoted=session_quoted,
|
||
fee_names=fee_names,
|
||
work_order_no=work_order_no,
|
||
)
|
||
|
||
def route_inbound(
|
||
self,
|
||
*,
|
||
text: str,
|
||
sender_id: str,
|
||
event: str = "",
|
||
) -> RouteDecision:
|
||
"""
|
||
入站后再路由。
|
||
|
||
点卡片是结构化事件,不进大模型。
|
||
自由文字交给 DeepSeek A 认意图(续办 / 新询价 / 调价 / 闲聊),不要求固定口令。
|
||
"""
|
||
if not sender_id:
|
||
return RouteDecision(intent="reject_no_sender", confidence=1.0)
|
||
kind = (event or "").strip().lower()
|
||
if kind in {"template_card_event", "click", "templatecardevent"}:
|
||
return RouteDecision(intent="card_action", confidence=1.0)
|
||
from agent.handlers.text_inquiry import is_land_confirm_action
|
||
from agent.channel.wecom.models import InboundMessage
|
||
from agent.policy import inquiry_copy as copy
|
||
from agent.schema.land_options import looks_like_confirm
|
||
|
||
# 结束/终止询价是产品短指令:不进模型,避免被当成补字段或闲聊。
|
||
if copy.looks_like_end_inquiry(text or ""):
|
||
logger.info("route 结束询价口令:跳过模型 sender=%s", sender_id)
|
||
return RouteDecision(intent="end_inquiry", confidence=1.0)
|
||
|
||
if looks_like_confirm(text or ""):
|
||
if is_land_confirm_action(
|
||
InboundMessage(
|
||
sender_id=sender_id,
|
||
content=text or "",
|
||
message_id="route-land-confirm",
|
||
)
|
||
):
|
||
logger.info("route 陆运核对卡确定:跳过闲聊模型 sender=%s", sender_id)
|
||
return RouteDecision(intent="ordinary_text_inquiry", confidence=1.0)
|
||
logger.info("route 确定类短句但无陆运核对卡,仍走模型 sender=%s", sender_id)
|
||
quoted = False
|
||
names: list[str] = []
|
||
wo = ""
|
||
try:
|
||
from agent.policy.quote_adjust_ops import quoted_adjust_flow
|
||
from agent.schema.quote_adjust import quote_fee_rows
|
||
|
||
flow = quoted_adjust_flow(sender_id)
|
||
sess = flow.session_of(sender_id) if flow is not None else None
|
||
if sess is not None:
|
||
quoted = True
|
||
wo = str(getattr(sess, "work_order_no", "") or "")
|
||
names = [str(r.get("name") or "") for r in quote_fee_rows(getattr(sess, "quote", None))]
|
||
except Exception:
|
||
logger.exception("route 收集调价书签失败 sender=%s", sender_id)
|
||
decision = self.route(
|
||
text=text,
|
||
sender_id=sender_id,
|
||
session_quoted=quoted,
|
||
fee_names=names,
|
||
work_order_no=wo,
|
||
)
|
||
return decision
|
||
|
||
def enqueue_ordered_actions(
|
||
self,
|
||
*,
|
||
text: str,
|
||
sender_id: str,
|
||
idempotency_key: str,
|
||
) -> str:
|
||
"""
|
||
将 A 路由任务入 LLM 队列(快回路径用)。
|
||
|
||
返回 stream entry id;无 LlmGateway 时抛错。
|
||
"""
|
||
if self.llm is None:
|
||
raise RuntimeError("IntentRouter 未注入 LlmGateway")
|
||
result = self.llm.enqueue(
|
||
mode=LlmMode.ORDERED_ACTIONS,
|
||
payload={"text": text, "sender_id": sender_id},
|
||
idempotency_key=idempotency_key,
|
||
)
|
||
if not result.accepted:
|
||
raise RuntimeError(result.reason or "enqueue_rejected")
|
||
return result.entry_id
|
||
|
||
|
||
__all__ = [
|
||
"RouteDecision",
|
||
"IntentRouter",
|
||
"DeepSeekARouter",
|
||
"ORDERED_ACTIONS_TOOL_NAME",
|
||
"OrderedActionsError",
|
||
"OrderedActionsResult",
|
||
"ordered_actions_tool_definition",
|
||
"parse_ordered_actions_arguments",
|
||
"HANDLER_REGISTRY",
|
||
"dispatch_inbound",
|
||
"resolve_handler",
|
||
]
|