chore: harden runtime checks and split websocket inbound/outbound flows

This commit is contained in:
2026-03-02 18:17:09 +08:00
parent 89eb94486d
commit e1ce17f2aa
7 changed files with 481 additions and 23 deletions

View File

@@ -4,12 +4,14 @@
"""
import asyncio
import os
import logging
from datetime import datetime
import httpx
from dotenv import load_dotenv
load_dotenv()
logger = logging.getLogger("cs_agent")
_last_push: dict[tuple[str, str], tuple[str, str, float]] = {}
@@ -38,6 +40,7 @@ def _get_recent_conversation(customer_id: str, acc_id: str, last_n: int = 8) ->
from db.chat_log_db import get_recent_conversation
return get_recent_conversation(customer_id, acc_id, limit=last_n)
except Exception:
logger.debug("[WechatChatLog] 获取近期对话失败,返回空列表", exc_info=True)
return []
@@ -68,7 +71,7 @@ async def push_chat_to_wechat(
return
_last_push[key] = ((customer_msg or ""), (reply_msg or ""), now)
except Exception:
pass
logger.debug("[WechatChatLog] 去重检查异常,忽略本次去重", exc_info=True)
reply_msg = _truncate(reply_msg, 300)
ts = datetime.now().strftime("%H:%M")
shop = acc_id or "未知店铺"
@@ -109,11 +112,11 @@ async def push_chat_to_wechat(
)
data = resp.json()
if data.get("errcode") == 0:
pass # 成功静默
return
else:
print(f"[WechatChatLog] 推送失败: {data}")
logger.warning("[WechatChatLog] 推送失败: %s", data)
except Exception as e:
print(f"[WechatChatLog] 推送异常: {e}")
logger.exception("[WechatChatLog] 推送异常: %s", e)
async def send_morning_startup():
@@ -129,14 +132,14 @@ async def send_morning_startup():
webhook,
json={"msgtype": "markdown", "markdown": {"content": content}},
)
print(f"[WechatChatLog] 早8点启动消息已发送")
logger.info("[WechatChatLog] 早8点启动消息已发送")
except Exception as e:
print(f"[WechatChatLog] 启动消息发送失败: {e}")
logger.exception("[WechatChatLog] 启动消息发送失败: %s", e)
async def morning_startup_scheduler():
"""每天 8:00 发送启动消息"""
print("[WechatChatLog] 早8点启动消息定时任务已启动")
logger.info("[WechatChatLog] 早8点启动消息定时任务已启动")
sent_today = None
while True:
now = datetime.now()