fix: block leaked history summaries in replies
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import asyncio
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import List, Optional, Dict, Any
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -10,6 +11,41 @@ from db.chat_log_db import get_conversation, get_customer_orders
|
||||
|
||||
logger = logging.getLogger("cs_agent")
|
||||
|
||||
_TRANSFER_COMMAND_RE = re.compile(r"^\s*正在为您转接\|\[转移会话\],[^,\r\n]+,[^\r\n]*\s*$")
|
||||
_HISTORY_NOISE_PREFIXES = (
|
||||
"[系统订单信息]",
|
||||
"[进店卡片]",
|
||||
"【系统:已收到",
|
||||
"金额:",
|
||||
"定制:",
|
||||
)
|
||||
|
||||
|
||||
def _is_plain_transfer_command(text: str) -> bool:
|
||||
return bool(_TRANSFER_COMMAND_RE.fullmatch(str(text or "").strip()))
|
||||
|
||||
|
||||
def _normalize_history_message(message: str, role: str) -> str:
|
||||
text = str(message or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
if _is_plain_transfer_command(text):
|
||||
return "已转接设计师"
|
||||
if role == "客服" and "[转移会话]" in text:
|
||||
return "已尝试转接设计师"
|
||||
return text
|
||||
|
||||
|
||||
def _extract_need_snippet(message: str) -> str:
|
||||
text = str(message or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
if any(text.startswith(prefix) for prefix in _HISTORY_NOISE_PREFIXES):
|
||||
return ""
|
||||
if "http://" in text or "https://" in text:
|
||||
return ""
|
||||
return text[:60]
|
||||
|
||||
|
||||
class TransferSuccessException(Exception):
|
||||
"""转接成功后抛出此异常,用于提前终止 AI 处理流程"""
|
||||
@@ -117,16 +153,18 @@ async def lookup_chat_history_tool(
|
||||
for r in rows:
|
||||
role = "客户" if r["direction"] == "in" else "客服"
|
||||
ts = str(r.get("timestamp", ""))
|
||||
msg = r.get("message", "")
|
||||
msg = _normalize_history_message(r.get("message", ""), role)
|
||||
line = f"[{ts}] {role}:{msg}"
|
||||
lines.append(line)
|
||||
if r["direction"] == "in":
|
||||
msg_type = int(r.get("msg_type") or 0)
|
||||
raw_message = str(r.get("message", "") or "")
|
||||
image_urls = str(r.get("image_urls", "") or "").strip()
|
||||
if msg_type == 1 or image_urls or ("已收到" in msg and "图" in msg):
|
||||
has_images = True
|
||||
if any(k in msg for k in ["找原图", "修复", "高清", "去背景", "抠图", "做衣服", "打印"]):
|
||||
customer_needs.append(msg[:60])
|
||||
need_text = _extract_need_snippet(raw_message)
|
||||
if need_text and any(k in need_text for k in ["找原图", "修复", "高清", "去背景", "抠图", "做衣服", "打印", "大图", "素材"]):
|
||||
customer_needs.append(need_text)
|
||||
|
||||
summary_parts = [f"共{len(rows)}条历史消息。"]
|
||||
if has_images:
|
||||
|
||||
Reference in New Issue
Block a user