From bcf03a0378c26f2991f325d0d88166341e073dbb Mon Sep 17 00:00:00 2001 From: lgy1027 Date: Wed, 11 Mar 2026 18:05:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E9=85=8D=E7=BD=AE=EF=BC=8C=E5=93=8D=E5=BA=94=E7=A4=BE?= =?UTF-8?q?=E5=8C=BA=E5=8F=8D=E9=A6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 工具调用超时改为可配置项 tool_call_timeout,默认30秒 - OpenAI 超时恢复可配置机制,支持细粒度配置和单一值 - TTS 超时保护统一添加到所有流式TTS实现 - 将 ActionResponse 导入移到文件顶部 - 修复超时配置边界情况处理 --- main/xiaozhi-server/config.yaml | 2 ++ main/xiaozhi-server/core/connection.py | 8 +++---- .../core/handle/intentHandler.py | 6 ++--- .../core/providers/llm/openai/openai.py | 22 ++++++++++++++----- .../core/providers/tts/alibl_stream.py | 6 ++--- .../core/providers/tts/aliyun_stream.py | 6 ++--- .../xiaozhi-server/core/providers/tts/base.py | 3 ++- .../providers/tts/huoshan_double_stream.py | 7 +++--- .../core/providers/tts/xunfei_stream.py | 4 ++-- 9 files changed, 38 insertions(+), 26 deletions(-) diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index c4662afa..ce6e6780 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -61,6 +61,8 @@ delete_audio: true close_connection_no_voice_time: 120 # TTS请求超时时间(秒) tts_timeout: 10 +# 工具调用超时时间(秒) +tool_call_timeout: 30 # 开启唤醒词加速 enable_wakeup_words_response_cache: true # 开场是否回复唤醒词 diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 850f6f7a..5e069e79 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -32,7 +32,7 @@ from core.providers.asr.dto.dto import InterfaceType from core.handle.textHandle import handleTextMessage from core.providers.tools.unified_tool_handler import UnifiedToolHandler from plugins_func.loadplugins import auto_import_modules -from plugins_func.register import Action +from plugins_func.register import Action, ActionResponse from core.auth import AuthenticationError from config.config_loader import get_private_config_from_api from core.providers.tts.dto.dto import ContentType, TTSMessageDTO, SentenceType @@ -997,19 +997,19 @@ class ConnectionHandler: ) futures_with_data.append((future, tool_call_data)) - TOOL_CALL_TIMEOUT = 30 + # 工具调用超时时间,可配置,默认30秒 + tool_call_timeout = int(self.config.get("tool_call_timeout", 30)) # 等待协程结束(实际等待时长为最慢的那个) tool_results = [] for future, tool_call_data in futures_with_data: try: - result = future.result(timeout=TOOL_CALL_TIMEOUT) + result = future.result(timeout=tool_call_timeout) tool_results.append((result, tool_call_data)) except Exception as e: self.logger.bind(tag=TAG).error( f"工具调用超时或异常: {tool_call_data['name']}, 错误: {e}" ) # 超时时返回错误响应,避免整个流程卡死 - from plugins_func.register import Action, ActionResponse tool_results.append(( ActionResponse(action=Action.ERROR, result="哎呀,网络遇到点问题,请稍后再试下!"), tool_call_data diff --git a/main/xiaozhi-server/core/handle/intentHandler.py b/main/xiaozhi-server/core/handle/intentHandler.py index 35ade945..94c846df 100644 --- a/main/xiaozhi-server/core/handle/intentHandler.py +++ b/main/xiaozhi-server/core/handle/intentHandler.py @@ -150,8 +150,8 @@ async def process_intent_result( def process_function_call(): conn.dialogue.put(Message(role="user", content=original_text)) - # 使用统一工具处理器处理所有工具调用,添加超时保护 - TOOL_CALL_TIMEOUT = 30 + # 工具调用超时时间 + tool_call_timeout = int(conn.config.get("tool_call_timeout", 30)) # 使用统一工具处理器处理所有工具调用 try: result = asyncio.run_coroutine_threadsafe( @@ -159,7 +159,7 @@ async def process_intent_result( conn, function_call_data ), conn.loop, - ).result(timeout=TOOL_CALL_TIMEOUT) + ).result(timeout=tool_call_timeout) except Exception as e: conn.logger.bind(tag=TAG).error(f"工具调用失败: {e}") result = ActionResponse( diff --git a/main/xiaozhi-server/core/providers/llm/openai/openai.py b/main/xiaozhi-server/core/providers/llm/openai/openai.py index e6be8ddb..3d9882f3 100644 --- a/main/xiaozhi-server/core/providers/llm/openai/openai.py +++ b/main/xiaozhi-server/core/providers/llm/openai/openai.py @@ -17,12 +17,22 @@ class LLMProvider(LLMProviderBase): self.base_url = config.get("base_url") else: self.base_url = config.get("url") - custom_timeout = httpx.Timeout( - pool=2.0, - connect=3.0, - write=5.0, - read=10.0 - ) + + timeout_config = config.get("timeout") + if isinstance(timeout_config, dict): + # 细粒度超时配置 + custom_timeout = httpx.Timeout( + pool=timeout_config.get("pool", 2.0), + connect=timeout_config.get("connect", 3.0), + write=timeout_config.get("write", 5.0), + read=timeout_config.get("read", 60.0) + ) + elif isinstance(timeout_config, (int, float)) and timeout_config > 0: + # 兼容旧的单一超时配置(整数或浮点数) + custom_timeout = httpx.Timeout(timeout_config) + else: + # 未配置或配置无效,使用默认值 + custom_timeout = httpx.Timeout(300) param_defaults = { "max_tokens": int, diff --git a/main/xiaozhi-server/core/providers/tts/alibl_stream.py b/main/xiaozhi-server/core/providers/tts/alibl_stream.py index 8eb0f708..4ed9e559 100644 --- a/main/xiaozhi-server/core/providers/tts/alibl_stream.py +++ b/main/xiaozhi-server/core/providers/tts/alibl_stream.py @@ -127,7 +127,7 @@ class TTSProvider(TTSProviderBase): self.start_session(self.conn.sentence_id), loop=self.conn.loop, ) - future.result() + future.result(timeout=self.tts_timeout) self.before_stop_play_files.clear() logger.bind(tag=TAG).info("TTS会话启动成功") except Exception as e: @@ -144,7 +144,7 @@ class TTSProvider(TTSProviderBase): self.text_to_speak(message.content_detail, None), loop=self.conn.loop, ) - future.result() + future.result(timeout=self.tts_timeout) logger.bind(tag=TAG).debug("TTS文本发送成功") except Exception as e: logger.bind(tag=TAG).error(f"发送TTS文本失败: {str(e)}") @@ -165,7 +165,7 @@ class TTSProvider(TTSProviderBase): self.finish_session(self.conn.sentence_id), loop=self.conn.loop, ) - future.result() + future.result(timeout=self.tts_timeout) except Exception as e: logger.bind(tag=TAG).error(f"结束TTS会话失败: {str(e)}") continue diff --git a/main/xiaozhi-server/core/providers/tts/aliyun_stream.py b/main/xiaozhi-server/core/providers/tts/aliyun_stream.py index cf06e7ff..20a5a0c4 100644 --- a/main/xiaozhi-server/core/providers/tts/aliyun_stream.py +++ b/main/xiaozhi-server/core/providers/tts/aliyun_stream.py @@ -232,7 +232,7 @@ class TTSProvider(TTSProviderBase): self.start_session(self.task_id), loop=self.conn.loop, ) - future.result() + future.result(timeout=self.tts_timeout) self.before_stop_play_files.clear() logger.bind(tag=TAG).debug("TTS会话启动成功") @@ -250,7 +250,7 @@ class TTSProvider(TTSProviderBase): self.text_to_speak(message.content_detail, None), loop=self.conn.loop, ) - future.result() + future.result(timeout=self.tts_timeout) logger.bind(tag=TAG).debug("TTS文本发送成功") except Exception as e: logger.bind(tag=TAG).error(f"发送TTS文本失败: {str(e)}") @@ -270,7 +270,7 @@ class TTSProvider(TTSProviderBase): self.finish_session(self.task_id), loop=self.conn.loop, ) - future.result() + future.result(timeout=self.tts_timeout) except Exception as e: logger.bind(tag=TAG).error(f"结束TTS会话失败: {str(e)}") continue diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index bc99dcec..319494b1 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -36,6 +36,7 @@ class TTSProviderBase(ABC): self.delete_audio_file = delete_audio_file self.audio_file_type = "wav" self.output_file = config.get("output_dir", "tmp/") + self.tts_timeout = int(config.get("tts_timeout", 15)) self.tts_text_queue = queue.Queue() self.tts_audio_queue = queue.Queue() self.tts_audio_first_sentence = True @@ -357,7 +358,7 @@ class TTSProviderBase(ABC): sendAudioMessage(self.conn, sentence_type, audio_datas, text), self.conn.loop, ) - future.result(timeout=15) + future.result(timeout=self.tts_timeout) # 记录输出和报告 if self.conn.max_output_size > 0 and text: diff --git a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py index cdca9ed8..bcaf75dd 100644 --- a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py +++ b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py @@ -149,7 +149,6 @@ class TTSProvider(TTSProviderBase): self.cluster = config.get("cluster") self.resource_id = config.get("resource_id") self.activate_session = False - self.timeout = 30 if config.get("private_voice"): self.voice = config.get("private_voice") else: @@ -308,7 +307,7 @@ class TTSProvider(TTSProviderBase): self.start_session(self.conn.sentence_id), loop=self.conn.loop, ) - future.result(self.timeout) + future.result(timeout=self.tts_timeout) self.before_stop_play_files.clear() logger.bind(tag=TAG).debug("TTS会话启动成功") except Exception as e: @@ -325,7 +324,7 @@ class TTSProvider(TTSProviderBase): self.text_to_speak(message.content_detail, None), loop=self.conn.loop, ) - future.result(self.timeout) + future.result(timeout=self.tts_timeout) logger.bind(tag=TAG).debug("TTS文本发送成功") except Exception as e: logger.bind(tag=TAG).error(f"发送TTS文本失败: {str(e)}") @@ -345,7 +344,7 @@ class TTSProvider(TTSProviderBase): self.finish_session(self.conn.sentence_id), loop=self.conn.loop, ) - future.result(self.timeout) + future.result(timeout=self.tts_timeout) except Exception as e: logger.bind(tag=TAG).error(f"结束TTS会话失败: {str(e)}") continue diff --git a/main/xiaozhi-server/core/providers/tts/xunfei_stream.py b/main/xiaozhi-server/core/providers/tts/xunfei_stream.py index be53284e..0434edbf 100644 --- a/main/xiaozhi-server/core/providers/tts/xunfei_stream.py +++ b/main/xiaozhi-server/core/providers/tts/xunfei_stream.py @@ -178,7 +178,7 @@ class TTSProvider(TTSProviderBase): self.start_session(self.conn.sentence_id), loop=self.conn.loop, ) - future.result() + future.result(timeout=self.tts_timeout) self.before_stop_play_files.clear() logger.bind(tag=TAG).info("TTS会话启动成功") @@ -197,7 +197,7 @@ class TTSProvider(TTSProviderBase): self.text_to_speak(message.content_detail, None), loop=self.conn.loop, ) - future.result() + future.result(timeout=self.tts_timeout) logger.bind(tag=TAG).debug("TTS文本发送成功") except Exception as e: logger.bind(tag=TAG).error(f"发送TTS文本失败: {str(e)}")