fix: alert wecom on brain fallback
This commit is contained in:
@@ -10,6 +10,7 @@ from pydantic_ai.models.openai import OpenAIChatModel
|
|||||||
from pydantic_ai.providers.openai import OpenAIProvider
|
from pydantic_ai.providers.openai import OpenAIProvider
|
||||||
from core.schema import StandardMessage, StandardResponse
|
from core.schema import StandardMessage, StandardResponse
|
||||||
from core.agent_tools import register_agent_tools, TransferSuccessException
|
from core.agent_tools import register_agent_tools, TransferSuccessException
|
||||||
|
from services.service_wecom_bot import wecom_bot_service
|
||||||
|
|
||||||
logger = logging.getLogger("cs_agent")
|
logger = logging.getLogger("cs_agent")
|
||||||
|
|
||||||
@@ -126,6 +127,46 @@ def _clip(text: str, limit: int = 1200) -> str:
|
|||||||
return f"{text[:limit]}...(截断, 共{len(text)}字)"
|
return f"{text[:limit]}...(截断, 共{len(text)}字)"
|
||||||
|
|
||||||
|
|
||||||
|
async def _notify_brain_fallback(
|
||||||
|
msg: StandardMessage,
|
||||||
|
error: Exception,
|
||||||
|
history_messages: Optional[List[Dict[str, Any]]] = None,
|
||||||
|
) -> None:
|
||||||
|
history_messages = history_messages or []
|
||||||
|
recent_lines: List[str] = []
|
||||||
|
for item in history_messages[-3:]:
|
||||||
|
role = str(item.get("role") or "").strip() or "unknown"
|
||||||
|
content = _clip(str(item.get("content") or "").replace("\r", " ").replace("\n", " "), 80)
|
||||||
|
if content:
|
||||||
|
recent_lines.append(f"{role}: {content}")
|
||||||
|
|
||||||
|
current_input = _clip(str(msg.content or "").replace("\r", " ").replace("\n", " "), 200)
|
||||||
|
image_count = len(getattr(msg, "image_urls", None) or [])
|
||||||
|
lines = [
|
||||||
|
"【AI兜底告警】",
|
||||||
|
f"店铺:{msg.acc_id or '-'}",
|
||||||
|
f"客户:{msg.user_id or '-'}",
|
||||||
|
f"消息类型:{getattr(msg, 'msg_type', '-')}",
|
||||||
|
f"图片数:{image_count}",
|
||||||
|
f"当前消息:{current_input or '-'}",
|
||||||
|
f"错误:{_clip(str(error), 300)}",
|
||||||
|
]
|
||||||
|
if recent_lines:
|
||||||
|
lines.append("最近上下文:")
|
||||||
|
lines.extend(recent_lines)
|
||||||
|
|
||||||
|
try:
|
||||||
|
ok = await wecom_bot_service.send_text("\n".join(lines))
|
||||||
|
if ok:
|
||||||
|
logger.info(f"[Brain Fallback Alert] 已发送企业微信告警 user={msg.user_id} acc={msg.acc_id}")
|
||||||
|
else:
|
||||||
|
logger.warning(f"[Brain Fallback Alert] 企业微信告警发送失败 user={msg.user_id} acc={msg.acc_id}")
|
||||||
|
except Exception as notify_err:
|
||||||
|
logger.warning(
|
||||||
|
f"[Brain Fallback Alert] 企业微信告警异常 user={msg.user_id} acc={msg.acc_id}: {notify_err}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _fmt_time(ts: Any) -> str:
|
def _fmt_time(ts: Any) -> str:
|
||||||
s = str(ts or "").strip()
|
s = str(ts or "").strip()
|
||||||
if not s:
|
if not s:
|
||||||
@@ -531,4 +572,5 @@ class CustomerServiceBrain:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[Brain Error]: {e}")
|
logger.error(f"[Brain Error]: {e}")
|
||||||
|
await _notify_brain_fallback(msg, e, history)
|
||||||
return StandardResponse(reply_content="好哒,我在看图,稍等回你哈。", metadata={"acc_id": msg.acc_id})
|
return StandardResponse(reply_content="好哒,我在看图,稍等回你哈。", metadata={"acc_id": msg.acc_id})
|
||||||
|
|||||||
Reference in New Issue
Block a user