- 新增员工岗位(InquiryStaffRole)实体、服务与后台页面,数据范围默认按岗位,看板由「仅本人」切换为岗位范围,补充业务负责人与角色说明迁移 - 新增企微群 webhook 通道、access_gate/staff_roles 鉴权与智价通/双 BOT 回复配置 - 报价回执增加去重防抖(quote_receipt_debounce),询价原文落库并补单测 - 同义词补陆运类型修正与地名通配 v2,更新种子生成脚本 - 空运/陆运/海运询价流程、报价单按需生成、转人工与多段处理调整 - 后台工单需求字段、BOT 显示名、站点标题与看板配套更新
811 lines
29 KiB
Python
811 lines
29 KiB
Python
"""
|
||
调价执行:私聊出新卡,群里出全量文本 + 成交跟进;Excel/PDF 按指令另出。
|
||
|
||
本文件职责:资格、抽项、落主账、出站。算价只调用 schema.quote_adjust。
|
||
禁止:回调线程同步跑;模型改价;调价后自动往群里甩报价单文件。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from typing import Any, Optional
|
||
|
||
from agent.policy import inquiry_copy as copy
|
||
from agent.schema.quote_adjust import (
|
||
adjustable_rows,
|
||
apply_adjust,
|
||
extract_adjust,
|
||
inspect_adjust_recognition,
|
||
preview_adjust,
|
||
private_session_can_adjust,
|
||
quote_fee_rows,
|
||
)
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
_ADJUST_PHASES = frozenset({"wait_collab", "wait_adopt", "wait_adjust", "wait_deal"})
|
||
|
||
|
||
def looks_like_adjust(text: str) -> bool:
|
||
"""本地兜底:正式认调价走 DeepSeek A,这里只给无网单测。"""
|
||
return extract_adjust(text, allow_model=False).is_adjust
|
||
|
||
|
||
def session_quoted(sess: Any) -> bool:
|
||
"""当前书签是否处在可尝试调价的阶段。"""
|
||
if sess is None:
|
||
return False
|
||
phase = str(getattr(sess, "phase", "") or "")
|
||
status = str(getattr(sess, "status", "") or "")
|
||
if phase in _ADJUST_PHASES:
|
||
return True
|
||
if status == "协商中" and (getattr(sess, "quote", None) or getattr(sess, "work_order_no", "")):
|
||
return True
|
||
return False
|
||
|
||
|
||
def quoted_adjust_flow(sender_id: str):
|
||
"""
|
||
三条线里谁有可调报价就交给谁;多条时用最新工单号。
|
||
|
||
私聊调价常被判成闲聊,选线不能再按「有海运书签就走海运」,
|
||
否则陆运刚出的卡会被旧海运书签抢走、回开场白。
|
||
"""
|
||
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
|
||
|
||
hits: list[tuple[str, Any]] = []
|
||
for flow in (get_land_text_flow(), get_air_text_flow(), get_sea_text_flow()):
|
||
sess = flow.session_of(sender_id)
|
||
if session_quoted(sess):
|
||
hits.append((str(getattr(sess, "work_order_no", "") or ""), flow))
|
||
if not hits:
|
||
return None
|
||
hits.sort(key=lambda item: item[0], reverse=True)
|
||
return hits[0][1]
|
||
|
||
|
||
def _baselines(quote: dict[str, Any]) -> tuple[str, str]:
|
||
src = dict(quote or {})
|
||
cost = str(src.get("tms_baseline_cost") or src.get("cost") or src.get("costTotal") or "")
|
||
sale = str(src.get("tms_baseline_sale") or src.get("total") or "")
|
||
return cost, sale
|
||
|
||
|
||
def _reply_apply_messages(result, reply) -> None:
|
||
names = [str(x.get("name") or "") for x in quote_fee_rows(result.quote)]
|
||
if result.applied:
|
||
reply(copy.adjust_confirm(result.applied))
|
||
if result.unmatched:
|
||
reply(copy.adjust_unmatched(result.unmatched, names))
|
||
if result.ambiguous:
|
||
for name in result.ambiguous:
|
||
reply(copy.adjust_ambiguous(name, names))
|
||
if result.lines_hint:
|
||
reply(copy.adjust_need_lines())
|
||
if result.currency_hint:
|
||
reply(copy.adjust_currency_hint())
|
||
|
||
|
||
def _emit_private_card(flow: Any, sess: Any, reply) -> None:
|
||
"""新价格明细卡:海运/陆运带拉群,空运只带跳过协同。
|
||
|
||
空运货物信息用工单 facts(起运地/品名等);不传会整段变成「-」。
|
||
"""
|
||
line = (sess.business_line or "").upper()
|
||
no = sess.work_order_no
|
||
quote = dict(sess.quote or {})
|
||
if line:
|
||
quote["business_line"] = line
|
||
first = getattr(sess, "first_or_same", "first") or "first"
|
||
history = getattr(sess, "history_work_order_no", "") or ""
|
||
facts = {str(k): str(v) for k, v in dict(getattr(sess, "facts", None) or {}).items()}
|
||
if line in {"SEA", "LAND"} and hasattr(flow, "_emit_sea_hit"):
|
||
sess.allowed = (copy.BTN_PULL_COLLAB, copy.BTN_SKIP_COLLAB)
|
||
flow._emit_sea_hit(
|
||
reply,
|
||
work_order_no=no,
|
||
quote=quote,
|
||
first_or_same=first,
|
||
history_work_order_no=history,
|
||
status="已报价",
|
||
business_line=line or "SEA",
|
||
)
|
||
return
|
||
sess.allowed = (copy.BTN_SKIP_COLLAB,)
|
||
emit_air = getattr(flow, "_emit_air_hit", None)
|
||
if callable(emit_air):
|
||
emit_air(
|
||
reply,
|
||
work_order_no=no,
|
||
quote=quote,
|
||
facts=facts,
|
||
first_or_same=first,
|
||
history_work_order_no=history,
|
||
status="已报价",
|
||
)
|
||
return
|
||
reply(
|
||
copy.tms_hit_card(
|
||
work_order_no=no,
|
||
quote=quote,
|
||
first_or_same=first,
|
||
history_work_order_no=history,
|
||
status="已报价",
|
||
),
|
||
copy.tms_hit_wecom_payload(
|
||
work_order_no=no,
|
||
quote=quote,
|
||
first_or_same=first,
|
||
history_work_order_no=history,
|
||
status="已报价",
|
||
),
|
||
)
|
||
|
||
|
||
# 「1」「第一项」「第2条」:反问后回序号。
|
||
_CN_NUMBERS = {
|
||
"一": 1, "二": 2, "三": 3, "四": 4, "五": 5,
|
||
"六": 6, "七": 7, "八": 8, "九": 9, "十": 10,
|
||
}
|
||
|
||
|
||
def _choice_index(text: str, choices: list[dict[str, Any]]) -> Optional[int]:
|
||
"""
|
||
从销售回话里认出他选了第几项。
|
||
|
||
先认序号(1/第一项),再按段名/费用名片段匹配;
|
||
两个候选同时命中且分不出高下时返回 None,宁可再问一次也不猜。
|
||
"""
|
||
import re
|
||
|
||
from agent.policy.bot_name import strip_mention_keep_layout
|
||
|
||
blob = strip_mention_keep_layout(text or "").strip()
|
||
blob = blob.strip("。.!!,,;;:: \t\r\n")
|
||
if not blob or not choices:
|
||
return None
|
||
hit = re.fullmatch(r"第?\s*([0-9]+|[一二三四五六七八九十])\s*(?:项|个|条|号)?", blob)
|
||
if hit:
|
||
raw = hit.group(1)
|
||
num = int(raw) if raw.isdigit() else _CN_NUMBERS.get(raw, 0)
|
||
if 1 <= num <= len(choices):
|
||
return num - 1
|
||
return None
|
||
scored: list[tuple[int, int]] = []
|
||
for index, choice in enumerate(choices):
|
||
label = str(choice.get("label") or "")
|
||
tokens = [t for t in re.split(r"[→\s\-()(),,/]+", label) if len(t) >= 2]
|
||
score = sum(len(t) for t in tokens if t in blob)
|
||
if score:
|
||
scored.append((score, index))
|
||
if not scored:
|
||
return None
|
||
scored.sort(reverse=True)
|
||
if len(scored) == 1 or scored[0][0] > scored[1][0]:
|
||
return scored[0][1]
|
||
return None
|
||
|
||
|
||
def _save_adjust_pick(
|
||
work_order_no: str,
|
||
extract: Any,
|
||
preview: Any,
|
||
*,
|
||
line: str,
|
||
sender_id: str,
|
||
text: str,
|
||
chat_id: str = "",
|
||
) -> None:
|
||
"""记下「正在等销售选哪一项」,下一句回 1/段名时直接接着改。"""
|
||
from agent.redis_coord.pending_quote_confirm import save_pending_confirm
|
||
|
||
save_pending_confirm(
|
||
work_order_no,
|
||
{
|
||
"kind": "adjust_pick",
|
||
"adjust": _adjust_payload(extract),
|
||
"item_index": int(getattr(preview, "item_index", 0) or 0),
|
||
"choices": list(getattr(preview, "choices", None) or []),
|
||
"line": line,
|
||
"sender_id": sender_id,
|
||
"text": text,
|
||
"chat_id": chat_id,
|
||
},
|
||
)
|
||
|
||
|
||
def _resume_adjust_pick(pending: dict[str, Any], text: str) -> tuple[Any, Optional[dict[str, Any]]]:
|
||
"""把销售选中的候选写回 AdjustExtract;认不出返回 (None, None)。"""
|
||
choices = [c for c in (pending.get("choices") or []) if isinstance(c, dict)]
|
||
index = _choice_index(text, choices)
|
||
if index is None:
|
||
return None, None
|
||
payload = dict(pending.get("adjust") or {})
|
||
items = [dict(x) for x in (payload.get("items") or []) if isinstance(x, dict)]
|
||
item_index = int(pending.get("item_index") or 0)
|
||
if item_index < 0:
|
||
item_index = 0
|
||
# 没有原始调价项(没抽出金额):不拿 0 去改价,让销售重发。
|
||
if item_index >= len(items):
|
||
return None, None
|
||
chosen = choices[index]
|
||
items[item_index]["fee_name"] = str(chosen.get("name") or items[item_index].get("fee_name") or "")
|
||
items[item_index]["target_key"] = str(chosen.get("key") or "")
|
||
payload["items"] = items
|
||
# 选定了具体费用项后就不再是「只改合计」,合计由该项派生。
|
||
payload["wants_total"] = False
|
||
return extract_adjust("", injected=payload, allow_model=False), chosen
|
||
|
||
|
||
def _begin_private_adjust(flow: Any, sess: Any, extract: Any, text: str, reply, names: list[str]) -> Optional[str]:
|
||
"""调价正式入口:先定向(歧义/冲突先问),再回执或直接改价。"""
|
||
quote = dict(getattr(sess, "quote", None) or {})
|
||
preview = preview_adjust(
|
||
quote,
|
||
extract,
|
||
business_line=str(getattr(sess, "business_line", "") or ""),
|
||
)
|
||
if preview.code == "choose_item":
|
||
_save_adjust_pick(
|
||
str(getattr(sess, "work_order_no", "") or ""),
|
||
extract,
|
||
preview,
|
||
line=str(getattr(sess, "business_line", "") or ""),
|
||
sender_id=str(getattr(sess, "sender_id", "") or ""),
|
||
text=text,
|
||
)
|
||
reply(copy.adjust_choose_item(preview.choices))
|
||
return "adjust_choose_item"
|
||
if preview.code == "conflict":
|
||
reply(copy.adjust_conflict())
|
||
return "adjust_conflict"
|
||
if preview.code == "none_matched":
|
||
reply(copy.adjust_unmatched(preview.unmatched, names))
|
||
return "adjust_none"
|
||
if preview.code == "need_fee":
|
||
reply(copy.adjust_need_fee())
|
||
return "adjust_need_fee"
|
||
# 产品 2026-11-24:调价先回识别回执,成员@机器人回「确认/确定」后才改价。
|
||
from agent.policy.quote_confirm import quote_confirm_enabled
|
||
from agent.redis_coord.pending_quote_confirm import save_pending_confirm
|
||
|
||
if quote_confirm_enabled():
|
||
payload = _adjust_payload(extract)
|
||
save_pending_confirm(
|
||
str(getattr(sess, "work_order_no", "") or ""),
|
||
{
|
||
"kind": "adjust",
|
||
"adjust": payload,
|
||
"text": text,
|
||
"line": str(getattr(sess, "business_line", "") or ""),
|
||
"sender_id": str(getattr(sess, "sender_id", "") or ""),
|
||
},
|
||
)
|
||
reply(
|
||
copy.adjust_confirm_receipt(
|
||
payload.get("items") or [],
|
||
work_order_no=str(getattr(sess, "work_order_no", "") or ""),
|
||
)
|
||
)
|
||
return "adjust_confirm_pending"
|
||
return _apply_private_adjust(flow, sess, extract, reply, names)
|
||
|
||
|
||
def try_private_adjust(
|
||
flow: Any,
|
||
sess: Any,
|
||
text: str,
|
||
reply,
|
||
*,
|
||
injected_adjust=None,
|
||
) -> Optional[str]:
|
||
"""
|
||
私聊调价。不是调价或不够资格尝试则返回 None,让原流程继续。
|
||
|
||
副作用:确认话术、主账 upsert、新卡、书签 waitVersion+1。
|
||
"""
|
||
if not session_quoted(sess):
|
||
return None
|
||
names = [str(r.get("name") or "") for r in adjustable_rows(getattr(sess, "quote", None))]
|
||
# 产品 2026-11-24:调价先回识别回执;这里接「确认/确定」并真正改价。
|
||
from agent.policy.quote_confirm import is_confirm_text, quote_confirm_enabled
|
||
from agent.redis_coord.pending_quote_confirm import (
|
||
clear_pending_confirm,
|
||
load_pending_confirm,
|
||
)
|
||
|
||
no = str(getattr(sess, "work_order_no", "") or "")
|
||
pending = load_pending_confirm(no)
|
||
# 上一句已经反问「改哪一项」,这一句回 1/段名就接着改。
|
||
if pending.get("kind") == "adjust_pick":
|
||
resumed, _chosen = _resume_adjust_pick(pending, text)
|
||
if resumed is not None:
|
||
if str(getattr(sess, "sender_id", "") or "") != str(pending.get("sender_id") or ""):
|
||
reply(copy.adjust_confirm_not_allowed())
|
||
return "adjust_confirm_not_allowed"
|
||
clear_pending_confirm(no)
|
||
return _begin_private_adjust(
|
||
flow,
|
||
sess,
|
||
resumed,
|
||
str(pending.get("text") or text),
|
||
reply,
|
||
names,
|
||
)
|
||
if quote_confirm_enabled():
|
||
if pending.get("kind") == "adjust" and is_confirm_text(text):
|
||
if str(getattr(sess, "sender_id", "") or "") != str(
|
||
pending.get("sender_id") or ""
|
||
):
|
||
reply(copy.adjust_confirm_not_allowed())
|
||
return "adjust_confirm_not_allowed"
|
||
clear_pending_confirm(no)
|
||
confirmed = extract_adjust(
|
||
text, injected=pending.get("adjust") or {}, allow_model=False
|
||
)
|
||
return _apply_private_adjust(flow, sess, confirmed, reply, names)
|
||
extract = extract_adjust(text, fee_names=names, injected=injected_adjust, allow_bare=True)
|
||
# 调价也走分级提示:只说了费用名没说幅度、只说了幅度没点名费用,
|
||
# 或「改成」后面没写值,都先回「我识别内容为…」;完全不像调价才让原流程继续。
|
||
attempt = inspect_adjust_recognition(text, extract, fee_names=names)
|
||
if attempt is not None and attempt["status"] in {"partial", "none"}:
|
||
reason = private_session_can_adjust(
|
||
str(getattr(sess, "status", "") or ""),
|
||
bool(quote_fee_rows(getattr(sess, "quote", None))),
|
||
str(getattr(sess, "collab_chat_id", "") or ""),
|
||
)
|
||
if reason:
|
||
reply(copy.adjust_ineligible(reason))
|
||
return "adjust_blocked"
|
||
if attempt["status"] == "partial":
|
||
reply(copy.adjust_partial_recognized(attempt["recognized"], attempt["missing"]))
|
||
return "adjust_partial_recognized"
|
||
reply(copy.adjust_not_recognized())
|
||
return "adjust_not_recognized"
|
||
if not extract.is_adjust:
|
||
return None
|
||
return _begin_private_adjust(flow, sess, extract, text, reply, names)
|
||
|
||
|
||
def _adjust_payload(extract: Any) -> dict[str, Any]:
|
||
"""把 AdjustExtract 收成可落 Redis 的 JSON。"""
|
||
return {
|
||
"is_adjust": bool(getattr(extract, "is_adjust", False)),
|
||
"wants_total": bool(getattr(extract, "wants_total", False)),
|
||
"items": [
|
||
{
|
||
"fee_name": str(getattr(it, "fee_name", "") or ""),
|
||
"kind": str(getattr(it, "kind", "") or "percent"),
|
||
"direction": str(getattr(it, "direction", "") or "up"),
|
||
"value": float(getattr(it, "value", 0) or 0),
|
||
"target_key": str(getattr(it, "target_key", "") or ""),
|
||
}
|
||
for it in (getattr(extract, "items", None) or [])
|
||
],
|
||
}
|
||
|
||
|
||
def _apply_private_adjust(flow: Any, sess: Any, extract: Any, reply, names: list[str]) -> Optional[str]:
|
||
"""私聊调价真正改价:资格、算价、写主账、出新卡。确认前后共用。"""
|
||
reason = private_session_can_adjust(
|
||
str(getattr(sess, "status", "") or ""),
|
||
bool(quote_fee_rows(getattr(sess, "quote", None))),
|
||
str(getattr(sess, "collab_chat_id", "") or ""),
|
||
)
|
||
if reason:
|
||
reply(copy.adjust_ineligible(reason))
|
||
return "adjust_blocked"
|
||
result = apply_adjust(
|
||
dict(sess.quote or {}),
|
||
extract,
|
||
business_line=str(getattr(sess, "business_line", "") or ""),
|
||
tms_cost=_baselines(sess.quote)[0],
|
||
tms_sale=_baselines(sess.quote)[1],
|
||
)
|
||
if result.code == "choose_item":
|
||
reply(copy.adjust_choose_item(result.choices))
|
||
return "adjust_choose_item"
|
||
if result.code == "conflict":
|
||
reply(copy.adjust_conflict())
|
||
return "adjust_conflict"
|
||
if result.code == "need_fee":
|
||
reply(copy.adjust_need_fee())
|
||
return "adjust_need_fee"
|
||
if result.code == "need_lines":
|
||
reply(copy.adjust_need_lines())
|
||
return "adjust_need_lines"
|
||
if result.code == "none_matched":
|
||
reply(copy.adjust_unmatched(result.unmatched, names))
|
||
return "adjust_none"
|
||
if result.code == "ambiguous":
|
||
reply(copy.adjust_ambiguous((extract.items[0].fee_name if extract.items else ""), names))
|
||
return "adjust_ambiguous"
|
||
if not result.ok:
|
||
return None
|
||
copy.remember_tms_quote(sess, sess.quote)
|
||
persist = dict(result.quote)
|
||
persist["source"] = "SALES_ADJUST"
|
||
persist["source_label"] = "销售调价"
|
||
result.quote["source"] = persist["source"]
|
||
result.quote["source_label"] = persist["source_label"]
|
||
writer = getattr(flow.ledger, "upsert_product_quote", None) or flow.ledger.upsert_quote
|
||
remark = copy.adjust_ledger_remark(result.applied)
|
||
try:
|
||
up = writer(
|
||
work_order_no=sess.work_order_no,
|
||
quote=persist,
|
||
to_status="已报价",
|
||
remark=remark,
|
||
kind="adjust",
|
||
) or {}
|
||
except TypeError:
|
||
up = writer(work_order_no=sess.work_order_no, quote=persist, to_status="已报价") or {}
|
||
if not up.get("ok"):
|
||
reply("主账写入报价失败,调价未入账。")
|
||
return "ledger_fail"
|
||
_reply_apply_messages(result, reply)
|
||
sess.quote = persist
|
||
sess.status = "已报价"
|
||
sess.phase = "wait_collab"
|
||
# 必须先写回允许点的按钮再存书签。若等 emit 后再改 allowed,
|
||
# Worker 重启后会读到空 allowed,销售点「生成报价单」会被静默丢掉。
|
||
line = (str(getattr(sess, "business_line", "") or "")).upper()
|
||
if line in {"SEA", "LAND"}:
|
||
sess.allowed = (copy.BTN_PULL_COLLAB, copy.BTN_SKIP_COLLAB)
|
||
else:
|
||
sess.allowed = (copy.BTN_SKIP_COLLAB,)
|
||
sess.wait_version += 1
|
||
flow._save(sess)
|
||
_emit_private_card(flow, sess, reply)
|
||
return "wait_collab"
|
||
|
||
|
||
def _apply_group_adjust_math(
|
||
*,
|
||
flow: Any,
|
||
ticket: object,
|
||
no: str,
|
||
view: dict[str, Any],
|
||
current: dict[str, Any],
|
||
names: list[str],
|
||
extract: Any,
|
||
sess: Any,
|
||
sender_id: str,
|
||
room: str,
|
||
client: Any,
|
||
) -> str:
|
||
"""群内调价真正改价:算价、写主账、发全量文本 + 成交跟进。确认前后共用。"""
|
||
from agent.policy.sea_group_ops import (
|
||
_save_product_quote,
|
||
emit_group_deal,
|
||
send_group_text,
|
||
ticket_view,
|
||
)
|
||
|
||
result = apply_adjust(
|
||
current,
|
||
extract,
|
||
business_line=str(view.get("business_line") or getattr(sess, "business_line", "") or ""),
|
||
tms_cost=_baselines(current)[0],
|
||
tms_sale=_baselines(current)[1],
|
||
)
|
||
if result.code == "choose_item":
|
||
send_group_text(client, room, copy.adjust_choose_item(result.choices))
|
||
return "adjust_choose_item"
|
||
if result.code == "conflict":
|
||
send_group_text(client, room, copy.adjust_conflict())
|
||
return "adjust_conflict"
|
||
if result.code == "need_fee":
|
||
send_group_text(client, room, copy.adjust_need_fee())
|
||
return "adjust_need_fee"
|
||
if result.code == "need_lines":
|
||
send_group_text(client, room, copy.adjust_need_lines())
|
||
return "adjust_need_lines"
|
||
if result.code == "none_matched":
|
||
send_group_text(client, room, copy.adjust_unmatched(result.unmatched, names))
|
||
return "adjust_none"
|
||
if not result.ok:
|
||
send_group_text(client, room, copy.adjust_unmatched(result.unmatched or ["该项"], names))
|
||
return "adjust_none"
|
||
copy.remember_tms_quote(sess, current)
|
||
_save_product_quote(
|
||
flow,
|
||
no,
|
||
result.quote,
|
||
sess,
|
||
remark=copy.adjust_ledger_remark(result.applied),
|
||
kind="adjust",
|
||
sender_id=sender_id,
|
||
)
|
||
ticket2 = ticket_view(ticket)
|
||
ticket2["quote"] = result.quote
|
||
ticket2["status"] = "已报价"
|
||
ticket2["work_order_no"] = no
|
||
parts = [copy.adjust_confirm(result.applied)]
|
||
if result.unmatched:
|
||
parts.append(copy.adjust_unmatched(result.unmatched, [str(r.get("name") or "") for r in quote_fee_rows(result.quote)]))
|
||
if result.lines_hint:
|
||
parts.append(copy.adjust_need_lines())
|
||
if result.currency_hint:
|
||
parts.append(copy.adjust_currency_hint())
|
||
line = str(view.get("business_line") or getattr(sess, "business_line", "") or "SEA")
|
||
parts.append(copy.adjust_group_body(result.quote, business_line=line))
|
||
send_group_text(client, room, "\n".join(parts))
|
||
# 调价只更新文字与成交跟进;Excel/PDF 等「生成报价单」指令。
|
||
emit_group_deal(flow=flow, ticket=ticket2, sess=sess, quote=result.quote)
|
||
logger.info("group_adjust wo=%s applied=%s", no, [x.get("name") for x in result.applied])
|
||
return "group_adjusted"
|
||
|
||
|
||
def _begin_group_adjust(
|
||
*,
|
||
flow: Any,
|
||
ticket: object,
|
||
no: str,
|
||
view: dict[str, Any],
|
||
current: dict[str, Any],
|
||
names: list[str],
|
||
extract: Any,
|
||
sess: Any,
|
||
sender_id: str,
|
||
room: str,
|
||
client: Any,
|
||
text: str,
|
||
) -> str:
|
||
"""群内调价正式入口:先定向(歧义/冲突先问),再回执或直接改价。"""
|
||
from agent.policy.sea_group_ops import send_group_text
|
||
|
||
line = str(view.get("business_line") or getattr(sess, "business_line", "") or "")
|
||
preview = preview_adjust(current, extract, business_line=line)
|
||
if preview.code == "choose_item":
|
||
_save_adjust_pick(
|
||
no,
|
||
extract,
|
||
preview,
|
||
line=line,
|
||
sender_id=sender_id,
|
||
text=text,
|
||
chat_id=room,
|
||
)
|
||
send_group_text(client, room, copy.adjust_choose_item(preview.choices))
|
||
return "adjust_choose_item"
|
||
if preview.code == "conflict":
|
||
send_group_text(client, room, copy.adjust_conflict())
|
||
return "adjust_conflict"
|
||
if preview.code == "none_matched":
|
||
send_group_text(client, room, copy.adjust_unmatched(preview.unmatched, names))
|
||
return "adjust_none"
|
||
if preview.code == "need_fee":
|
||
send_group_text(client, room, copy.adjust_need_fee())
|
||
return "adjust_need_fee"
|
||
# 产品 2026-11-24:调价先回识别回执,成员@机器人回「确认/确定」后才改价。
|
||
from agent.policy.quote_confirm import quote_confirm_enabled
|
||
from agent.redis_coord.pending_quote_confirm import save_pending_confirm
|
||
|
||
if quote_confirm_enabled():
|
||
payload = _adjust_payload(extract)
|
||
save_pending_confirm(
|
||
no,
|
||
{
|
||
"kind": "adjust",
|
||
"adjust": payload,
|
||
"text": text,
|
||
"line": line,
|
||
"sender_id": sender_id,
|
||
"chat_id": room,
|
||
},
|
||
)
|
||
send_group_text(
|
||
client,
|
||
room,
|
||
copy.adjust_confirm_receipt(payload.get("items") or [], work_order_no=no),
|
||
)
|
||
return "adjust_confirm_pending"
|
||
return _apply_group_adjust_math(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
no=no,
|
||
view=view,
|
||
current=current,
|
||
names=names,
|
||
extract=extract,
|
||
sess=sess,
|
||
sender_id=sender_id,
|
||
room=room,
|
||
client=client,
|
||
)
|
||
|
||
|
||
def confirm_pending_group_adjust(
|
||
flow: Any,
|
||
ticket: object,
|
||
message: Any,
|
||
sess: Any = None,
|
||
) -> str:
|
||
"""
|
||
群里成员 @机器人 回「确认/确定」:把待确认的调价真正改价。
|
||
|
||
发起调价的销售本人,或产品/航线岗可以确认。
|
||
"""
|
||
from agent.policy.quote_confirm import (
|
||
can_confirm_adjust,
|
||
is_confirm_text,
|
||
quote_confirm_enabled,
|
||
)
|
||
from agent.redis_coord.pending_quote_confirm import (
|
||
clear_pending_confirm,
|
||
load_pending_confirm,
|
||
)
|
||
|
||
from agent.policy.sea_group_ops import (
|
||
_current_quote,
|
||
group_client_of,
|
||
hydrate_ticket,
|
||
send_group_text,
|
||
ticket_no,
|
||
ticket_status,
|
||
ticket_view,
|
||
)
|
||
|
||
ticket = hydrate_ticket(flow, ticket)
|
||
no = ticket_no(ticket)
|
||
pending = load_pending_confirm(no)
|
||
if not pending:
|
||
return ""
|
||
text = getattr(message, "content", "") or ""
|
||
sender_id = getattr(message, "sender_id", "") or ""
|
||
room = getattr(message, "chat_id", "") or ""
|
||
client = group_client_of(flow)
|
||
# 上一句已经反问「改哪一项」:认得出选项就接着改,认不出静默交给正常流程。
|
||
if pending.get("kind") == "adjust_pick":
|
||
resumed, _chosen = _resume_adjust_pick(pending, text)
|
||
if resumed is None:
|
||
return ""
|
||
if not can_confirm_adjust(
|
||
flow, ticket, sender_id, pending_sender=str(pending.get("sender_id") or "")
|
||
):
|
||
send_group_text(client, room, copy.adjust_confirm_not_allowed())
|
||
return "adjust_confirm_not_allowed"
|
||
if ticket_status(ticket) not in {"已报价", "协商中"}:
|
||
send_group_text(client, room, copy.adjust_ineligible("no_quote"))
|
||
return "adjust_blocked"
|
||
clear_pending_confirm(no)
|
||
view = ticket_view(ticket)
|
||
current = _current_quote(view, sess)
|
||
names = [str(r.get("name") or "") for r in adjustable_rows(current)]
|
||
return _begin_group_adjust(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
no=no,
|
||
view=view,
|
||
current=current,
|
||
names=names,
|
||
extract=resumed,
|
||
sess=sess,
|
||
sender_id=sender_id,
|
||
room=room,
|
||
client=client,
|
||
text=str(pending.get("text") or text),
|
||
)
|
||
if not quote_confirm_enabled():
|
||
return ""
|
||
if pending.get("kind") != "adjust":
|
||
return ""
|
||
if not is_confirm_text(text):
|
||
return ""
|
||
if not can_confirm_adjust(
|
||
flow, ticket, sender_id, pending_sender=str(pending.get("sender_id") or "")
|
||
):
|
||
send_group_text(client, room, copy.adjust_confirm_not_allowed())
|
||
return "adjust_confirm_not_allowed"
|
||
if ticket_status(ticket) not in {"已报价", "协商中"}:
|
||
send_group_text(client, room, copy.adjust_ineligible("no_quote"))
|
||
return "adjust_blocked"
|
||
clear_pending_confirm(no)
|
||
view = ticket_view(ticket)
|
||
current = _current_quote(view, sess)
|
||
names = [str(r.get("name") or "") for r in adjustable_rows(current)]
|
||
extract = extract_adjust(text, injected=pending.get("adjust") or {}, allow_model=False)
|
||
return _apply_group_adjust_math(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
no=no,
|
||
view=view,
|
||
current=current,
|
||
names=names,
|
||
extract=extract,
|
||
sess=sess,
|
||
sender_id=sender_id,
|
||
room=room,
|
||
client=client,
|
||
)
|
||
|
||
|
||
def apply_group_adjust(
|
||
*,
|
||
flow: Any,
|
||
ticket: object,
|
||
sender_id: str,
|
||
text: str,
|
||
sess: Any = None,
|
||
chat_id: str = "",
|
||
injected_adjust=None,
|
||
) -> str:
|
||
"""
|
||
群内销售调价。非销售只说明、不当报价。
|
||
|
||
至少改成一项:全量文本 + 成交跟进;不自动发 Excel/PDF。
|
||
一项都没有:只说明。要报价单再说「生成报价单」。
|
||
"""
|
||
from agent.policy.sea_group_ops import (
|
||
_current_quote,
|
||
_save_product_quote,
|
||
emit_group_deal,
|
||
group_client_of,
|
||
halt_closed_group,
|
||
hydrate_ticket,
|
||
is_sales,
|
||
send_group_text,
|
||
ticket_no,
|
||
ticket_status,
|
||
ticket_view,
|
||
)
|
||
|
||
ticket = hydrate_ticket(flow, ticket)
|
||
no = ticket_no(ticket)
|
||
view = ticket_view(ticket)
|
||
room = (chat_id or str((sess.collab_chat_id if sess else "") or view.get("collab_chat_id") or "")).strip()
|
||
client = group_client_of(flow)
|
||
stopped = halt_closed_group(flow=flow, ticket=ticket, sess=sess, chat_id=room)
|
||
if stopped:
|
||
return stopped
|
||
status = ticket_status(ticket)
|
||
if status not in {"已报价", "协商中"}:
|
||
send_group_text(client, room, copy.adjust_ineligible("no_quote"))
|
||
return "adjust_blocked"
|
||
if not is_sales(ticket, sender_id):
|
||
line = str(view.get("business_line") or view.get("businessLine") or "").upper()
|
||
air_room = line == "AIR"
|
||
if line == "MULTI":
|
||
from agent.policy.multi_group_ops import load_segments
|
||
from agent.policy.multi_segments import includes_air
|
||
|
||
air_room = includes_air(load_segments(ticket))
|
||
send_group_text(client, room, copy.adjust_only_sales(air_room=air_room))
|
||
return "adjust_not_sales"
|
||
current = _current_quote(view, sess)
|
||
names = [str(r.get("name") or "") for r in adjustable_rows(current)]
|
||
extract = extract_adjust(text, fee_names=names, injected=injected_adjust, allow_bare=True)
|
||
# 调价分级提示:部分识别先回「我识别内容为…」,完全不像调价才静默。
|
||
attempt = inspect_adjust_recognition(text, extract, fee_names=names)
|
||
if attempt is not None and attempt["status"] in {"partial", "none"}:
|
||
if attempt["status"] == "partial":
|
||
send_group_text(
|
||
client,
|
||
room,
|
||
copy.adjust_partial_recognized(
|
||
attempt["recognized"], attempt["missing"]
|
||
),
|
||
)
|
||
return "adjust_partial_recognized"
|
||
send_group_text(client, room, copy.adjust_not_recognized())
|
||
return "adjust_not_recognized"
|
||
if not extract.is_adjust:
|
||
return "not_adjust"
|
||
return _begin_group_adjust(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
no=no,
|
||
view=view,
|
||
current=current,
|
||
names=names,
|
||
extract=extract,
|
||
sess=sess,
|
||
sender_id=sender_id,
|
||
room=room,
|
||
client=client,
|
||
text=text,
|
||
)
|