完善消息链路trace并贯通AI与发送动作
This commit is contained in:
8
robot.py
8
robot.py
@@ -32,6 +32,7 @@ from utils.wechat.contact_manager import ContactManager
|
||||
from utils.wechat.member_monitor import ChatroomMemberMonitor
|
||||
from utils.wechat.message_to_db import MessageStorage
|
||||
from utils.ai.llm_registry import LLMRegistry
|
||||
from utils.trace_context import set_current_trace_id, reset_current_trace_id
|
||||
from wechat_ipad import WechatAPIClient
|
||||
from wechat_ipad.models.message import WxMessage, MessageType
|
||||
|
||||
@@ -387,10 +388,17 @@ class Robot:
|
||||
|
||||
async def _process_with_semaphore(self, wxmsg):
|
||||
async with sem:
|
||||
# 进入单条消息处理前,把 trace_id 放入当前异步上下文:
|
||||
# 1. 后续插件中的 AI 调用、消息发送、子协程都可以自动继承这个 trace_id;
|
||||
# 2. 这样不需要给大量现有方法额外加 trace_id 参数,侵入性更小;
|
||||
# 3. finally 中会回滚 token,避免把当前消息的 trace_id 泄漏到下一条消息。
|
||||
trace_token = set_current_trace_id(self._get_trace_id(wxmsg))
|
||||
try:
|
||||
await self._process_ipad_message(wxmsg)
|
||||
except Exception as e:
|
||||
self.LOG.error(self._trace_message(wxmsg, f"处理消息失败 msg_id={wxmsg.msg_id}, 错误: {e}"))
|
||||
finally:
|
||||
reset_current_trace_id(trace_token)
|
||||
|
||||
async def _handle_ipad_login(self, wxid, device_name, device_id):
|
||||
"""处理wechat_ipad登录"""
|
||||
|
||||
Reference in New Issue
Block a user