@@ -11,7 +11,9 @@ use axum::{
1111use serde:: { Deserialize , Serialize } ;
1212use std:: collections:: HashMap ;
1313use std:: sync:: Arc ;
14- use tracing:: { error, info} ;
14+ use std:: time:: Duration ;
15+ use tokio:: time:: timeout;
16+ use tracing:: { error, info, warn} ;
1517use wechat_oa_sdk:: {
1618 WeChatClient ,
1719 api:: message:: IncomingMessage ,
@@ -201,14 +203,19 @@ async fn handle_message(message: IncomingMessage, llm_client: Option<&Arc<LlmCli
201203 "对话历史已清除!" . to_string ( )
202204 }
203205 _ => {
204- // 使用 LLM 回复
206+ // 使用 LLM 回复(4秒超时,微信要求5秒内响应)
205207 if let Some ( client) = llm_client {
206- match client. chat ( & msg. from_user_name , & msg. content ) . await {
207- Ok ( response) => response,
208- Err ( e) => {
208+ let llm_timeout = Duration :: from_secs ( 4 ) ;
209+ match timeout ( llm_timeout, client. chat ( & msg. from_user_name , & msg. content ) ) . await {
210+ Ok ( Ok ( response) ) => response,
211+ Ok ( Err ( e) ) => {
209212 error ! ( "LLM 调用失败: {}" , e) ;
210213 format ! ( "抱歉,AI 服务暂时不可用:{}" , e)
211214 }
215+ Err ( _) => {
216+ warn ! ( "LLM 调用超时 for user {}" , msg. from_user_name) ;
217+ "AI 正在思考中,请稍后再试..." . to_string ( )
218+ }
212219 }
213220 } else {
214221 // 没有配置 LLM,使用简单回复
0 commit comments