From da4580b4022e1a90e30e842581808f7fc637920e Mon Sep 17 00:00:00 2001 From: Ike-li Date: Thu, 27 Mar 2025 19:37:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=AC=E5=9C=B0=E8=AE=B0?= =?UTF-8?q?=E5=BF=86=E5=AD=98=E5=82=A8=E8=AE=B0=E5=BF=86=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/connection.py | 37 ++++++++++++-------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 98cf5c5c8..882870797 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -303,9 +303,16 @@ class ConnectionHandler: future = self.executor.submit(self.speak_and_play, segment_text, text_index) self.tts_queue.put(future) + # 存储对话内容 + if len(response_message) > 0: + self.dialogue.put(Message(role="assistant", content="".join(response_message))) + + # 异步保存记忆 + asyncio.create_task(self.memory.save_memory(self.dialogue.dialogue)) + self.llm_finish_task = True - self.dialogue.put(Message(role="assistant", content="".join(response_message))) self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False)) + return True def chat_with_function_calling(self, query, tool_call=False): @@ -580,26 +587,16 @@ class ConnectionHandler: self.tts_first_text_index = text_index self.tts_last_text_index = text_index - async def close(self, ws=None): - """资源清理方法""" - - # 触发停止事件并清理资源 - if self.stop_event: - self.stop_event.set() - - # 立即关闭线程池 - if self.executor: - self.executor.shutdown(wait=False, cancel_futures=True) - self.executor = None - - # 清空任务队列 - self._clear_queues() - - if ws: + async def close(self, ws): + """关闭连接""" + try: await ws.close() - elif self.websocket: - await self.websocket.close() - self.logger.bind(tag=TAG).info("连接资源已释放") + except Exception as e: + self.logger.bind(tag=TAG).error(f"关闭连接失败: {str(e)}") + finally: + if ws in self.active_connections: + self.active_connections.remove(ws) + self.logger.bind(tag=TAG).info(f"连接已关闭 - Session: {self.session_id}") def _clear_queues(self): # 清空所有任务队列