fix: intent阻塞主线程

This commit is contained in:
Sakura-RanChen
2026-07-20 14:48:26 +08:00
parent 7c58fa37b2
commit ed89c05245
2 changed files with 36 additions and 9 deletions
@@ -118,8 +118,17 @@ async def process_intent_result(
请根据以上信息回答用户的问题:{original_text}"""
response = conn.intent.replyResult(context_prompt, original_text)
speak_txt(conn, response)
# 使用异步调用避免阻塞事件循环,影响其他设备的音频播放
try:
response = asyncio.run_coroutine_threadsafe(
conn.intent.replyResult(context_prompt, original_text),
conn.loop,
).result()
except Exception as e:
conn.logger.bind(tag=TAG).error(f"LLM生成回复失败: {e}")
response = None
if response:
speak_txt(conn, response)
conn.executor.submit(process_context_result)
return True
@@ -184,7 +193,15 @@ async def process_intent_result(
elif result.action == Action.REQLLM: # 调用函数后再请求llm生成回复
text = result.result
conn.dialogue.put(Message(role="tool", content=text))
llm_result = conn.intent.replyResult(text, original_text)
# 使用异步调用避免阻塞事件循环,影响其他设备的音频播放
try:
llm_result = asyncio.run_coroutine_threadsafe(
conn.intent.replyResult(text, original_text),
conn.loop,
).result()
except Exception as e:
conn.logger.bind(tag=TAG).error(f"LLM生成回复失败: {e}")
llm_result = text
if llm_result is None:
llm_result = text
speak_txt(conn, llm_result)