From a7ae72d0b21ad0c0ba6ea7742abc60c34880b5c1 Mon Sep 17 00:00:00 2001 From: Jeason <1710884619@qq.com> Date: Wed, 8 Apr 2026 09:07:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Pipeline=20=E5=A4=84=E7=90=86=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=80=97=E6=97=B6=E6=97=A5=E5=BF=97=E5=92=8C=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - message_pipeline.handle_message 添加每步耗时日志(租户解析/会话管理/消息处理) - feishu_longconn_service Pipeline 调用包裹 try/catch,异常时记录完整堆栈 - feishu_bot.py 同样添加 Pipeline 异常捕获 - 防止 Pipeline 内部异常被静默吞掉导致消息无回复 --- data/tsp_assistant.db | Bin 159744 -> 159744 bytes src/core/database.py | 3 --- src/core/session_store.py | 1 - src/dialogue/message_pipeline.py | 11 ++++++++++ src/integrations/feishu_longconn_service.py | 21 +++++++++++------- src/web/blueprints/feishu_bot.py | 23 ++++++++++++-------- start_dashboard.py | 9 -------- 7 files changed, 38 insertions(+), 30 deletions(-) diff --git a/data/tsp_assistant.db b/data/tsp_assistant.db index 22f3ac3ca07065b20f454aafe21920a01894cac3..03e4e5723392cf9e55b3c202c28c14babf095cf1 100644 GIT binary patch delta 200 zcmZp8z}fJCbAq&>F#`jGIuOG^+(aE?M&peM3(pI(zhhuyxy8V(%YTr+i<6n<*2J=c z%}o~~Sh$+3S=q%^RT*0>C;wpoKKT->`sBnnBAdn7ZZNX&|6ml|{OP(dha_Vi179;Q zJI@`Sja;pabsRb@jciTK<}5**1qBiqxth&**u^a^8Qa)5|9@e@IQjKckSPJ1%^!C# oGco3GKKLw~k*g()mtEY^k+H*k^Z92t;EI}CUu|!F#aP`60OEB*{r~^~ delta 160 zcmZp8z}fJCbAq&>0RsbrIuOG^^h6zFMuUwB3(xcN|6pKalVIT13Da SessionStore: socket_timeout=2 ) client.ping() - logger.info("会话存储使用 Redis") return RedisSessionStore(client) except Exception as e: logger.info(f"Redis 不可用({e}),会话存储使用内存") diff --git a/src/dialogue/message_pipeline.py b/src/dialogue/message_pipeline.py index 994f92f..56e50c6 100644 --- a/src/dialogue/message_pipeline.py +++ b/src/dialogue/message_pipeline.py @@ -73,9 +73,20 @@ class MessagePipeline: 完整的消息处理流程(一步到位)。 各入口可以直接调用此方法,不需要自己管理会话。 """ + import time as _time + t0 = _time.time() + resolved_tenant = self.resolve_tenant(chat_id=chat_id, tenant_id=tenant_id) + t1 = _time.time() + session_id = self.get_or_create_session(user_id, resolved_tenant, work_order_id) + t2 = _time.time() + result = self.process(session_id, message, ip_address, invocation_method) + t3 = _time.time() + + logger.info(f"Pipeline 耗时: 租户解析={t1-t0:.2f}s, 会话管理={t2-t1:.2f}s, 消息处理={t3-t2:.2f}s, 总计={t3-t0:.2f}s") + result['tenant_id'] = resolved_tenant result['session_id'] = session_id return result diff --git a/src/integrations/feishu_longconn_service.py b/src/integrations/feishu_longconn_service.py index efb6456..29fe981 100644 --- a/src/integrations/feishu_longconn_service.py +++ b/src/integrations/feishu_longconn_service.py @@ -165,14 +165,19 @@ class FeishuLongConnService: # 使用 Pipeline 统一处理消息 session_user_id = f"feishu_{chat_id}_{sender_id}" - pipeline = service_manager.get_pipeline() - response_data = pipeline.handle_message( - user_id=session_user_id, - message=text_content, - chat_id=chat_id, - ip_address=f"feishu:{sender_id}:{sender_name}", - invocation_method=f"feishu_longconn({chat_type})" - ) + try: + pipeline = service_manager.get_pipeline() + logger.info(f"Pipeline 开始处理: user={session_user_id}, chat_id={chat_id}") + response_data = pipeline.handle_message( + user_id=session_user_id, + message=text_content, + chat_id=chat_id, + ip_address=f"feishu:{sender_id}:{sender_name}", + invocation_method=f"feishu_longconn({chat_type})" + ) + except Exception as pipe_err: + logger.error(f"Pipeline 处理异常: {pipe_err}", exc_info=True) + response_data = {"success": False, "error": str(pipe_err)} tenant_id = response_data.get('tenant_id', 'default') logger.info(f"处理结果: success={response_data.get('success')}, 租户={tenant_id}") diff --git a/src/web/blueprints/feishu_bot.py b/src/web/blueprints/feishu_bot.py index 9767251..51a5855 100644 --- a/src/web/blueprints/feishu_bot.py +++ b/src/web/blueprints/feishu_bot.py @@ -119,15 +119,20 @@ def _process_message_in_background(app, event_data: dict): logger.info(f"[Feishu Bot] 发送者={sender_name}({sender_id}), 群={chat_id}, 租户={tenant_id}") # 4. 使用 Pipeline 统一处理消息 - pipeline = service_manager.get_pipeline() - response_data = pipeline.handle_message( - user_id=user_id, - message=text_content, - tenant_id=tenant_id, - chat_id=chat_id, - ip_address=f"feishu:{sender_id}:{sender_name}", - invocation_method=f"feishu_bot({chat_type})" - ) + try: + pipeline = service_manager.get_pipeline() + logger.info(f"[Feishu Bot] Pipeline 开始处理: user={user_id}") + response_data = pipeline.handle_message( + user_id=user_id, + message=text_content, + tenant_id=tenant_id, + chat_id=chat_id, + ip_address=f"feishu:{sender_id}:{sender_name}", + invocation_method=f"feishu_bot({chat_type})" + ) + except Exception as pipe_err: + logger.error(f"[Feishu Bot] Pipeline 处理异常: {pipe_err}", exc_info=True) + response_data = {"success": False, "error": str(pipe_err)} logger.info(f"[Feishu Bot] 处理结果: success={response_data.get('success')}") # 5. 提取回复并发送 diff --git a/start_dashboard.py b/start_dashboard.py index f373430..d9ab30c 100644 --- a/start_dashboard.py +++ b/start_dashboard.py @@ -48,9 +48,6 @@ def start_feishu_longconn_service(): try: import logging logger = logging.getLogger("feishu_longconn") - logger.info("=" * 80) - logger.info("🚀 启动飞书长连接服务(后台线程)") - logger.info("=" * 80) from src.integrations.feishu_longconn_service import get_feishu_longconn_service service = get_feishu_longconn_service() @@ -76,9 +73,6 @@ def check_database_connection(): def main(): """主函数""" - print("=" * 60) - print("TSP智能助手 - 综合管理平台") - print("=" * 60) start_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") print(f"启动时间: {start_time}") print() @@ -91,9 +85,6 @@ def main(): # 检查必要目录 os.makedirs("logs", exist_ok=True) os.makedirs("data", exist_ok=True) - - logger.info("正在启动TSP智能助手综合管理平台...") - # 检查数据库连接 if not check_database_connection(): logger.error("数据库连接失败,退出启动")