""" 调价执行:私聊出新卡,群里出全量文本 + 成交跟进;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, 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="已报价", ), ) 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, ) if quote_confirm_enabled(): no = str(getattr(sess, "work_order_no", "") or "") pending = load_pending_confirm(no) 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) # 调价也走分级提示:只说了费用名没说幅度、只说了幅度没点名费用, # 或「改成」后面没写值,都先回「我识别内容为…」;完全不像调价才让原流程继续。 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 # 产品 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 _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), } 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 == "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 == "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 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, ) if not quote_confirm_enabled(): return "" 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 or pending.get("kind") != "adjust": return "" text = getattr(message, "content", "") or "" if not is_confirm_text(text): return "" sender_id = getattr(message, "sender_id", "") or "" room = getattr(message, "chat_id", "") or "" client = group_client_of(flow) 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) # 调价分级提示:部分识别先回「我识别内容为…」,完全不像调价才静默。 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" # 产品 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": str(view.get("business_line") or ""), "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, )