From b597cb068657362e7cf76cbbbcc90fd2d44a4aac Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sat, 20 Dec 2025 00:30:20 +0800 Subject: [PATCH] =?UTF-8?q?update:=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/AgentChatHistoryBizServiceImpl.java | 26 ------------------- .../impl/AgentChatSummaryServiceImpl.java | 24 ++++++++--------- .../modules/security/config/ShiroConfig.java | 2 +- .../config/manage_api_client.py | 14 ---------- .../memory/mem_local_short/mem_local_short.py | 13 ++-------- 5 files changed, 15 insertions(+), 64 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/biz/impl/AgentChatHistoryBizServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/biz/impl/AgentChatHistoryBizServiceImpl.java index 16bcce50..8831bb1a 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/biz/impl/AgentChatHistoryBizServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/biz/impl/AgentChatHistoryBizServiceImpl.java @@ -83,9 +83,6 @@ public class AgentChatHistoryBizServiceImpl implements AgentChatHistoryBizServic log.warn("聊天记录上报时,未找到mac地址为 {} 的设备", macAddress); } - // 异步触发聊天记录总结(仅在对话结束时触发) - triggerChatSummaryAsync(report.getSessionId(), chatType); - return Boolean.TRUE; } @@ -130,27 +127,4 @@ public class AgentChatHistoryBizServiceImpl implements AgentChatHistoryBizServic log.info("设备 {} 对应智能体 {} 上报成功", macAddress, agentId); } - - /** - * 异步触发聊天记录总结 - * 仅在对话结束时(chatType=2)触发总结,避免频繁总结 - */ - private void triggerChatSummaryAsync(String sessionId, Byte chatType) { - // 仅在对话结束时触发总结(chatType=2表示对话结束) - if (chatType != null && chatType == 2) { - new Thread(() -> { - try { - log.info("开始为会话 {} 生成聊天记录总结", sessionId); - boolean success = agentChatSummaryService.generateAndSaveChatSummary(sessionId); - if (success) { - log.info("会话 {} 的聊天记录总结生成并保存成功", sessionId); - } else { - log.warn("会话 {} 的聊天记录总结生成失败", sessionId); - } - } catch (Exception e) { - log.error("触发会话 {} 的聊天记录总结时发生异常", sessionId, e); - } - }).start(); - } - } } diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentChatSummaryServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentChatSummaryServiceImpl.java index 5e855be6..f367891f 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentChatSummaryServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentChatSummaryServiceImpl.java @@ -1,11 +1,19 @@ package xiaozhi.modules.agent.service.impl; -import lombok.RequiredArgsConstructor; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.apache.commons.lang3.StringUtils; -import org.springframework.stereotype.Service; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; + +import lombok.RequiredArgsConstructor; import xiaozhi.modules.agent.dto.AgentChatHistoryDTO; import xiaozhi.modules.agent.dto.AgentChatSummaryDTO; import xiaozhi.modules.agent.dto.AgentMemoryDTO; @@ -14,19 +22,12 @@ import xiaozhi.modules.agent.entity.AgentChatHistoryEntity; import xiaozhi.modules.agent.service.AgentChatHistoryService; import xiaozhi.modules.agent.service.AgentChatSummaryService; import xiaozhi.modules.agent.service.AgentService; -import xiaozhi.modules.config.service.ConfigService; +import xiaozhi.modules.agent.vo.AgentInfoVO; import xiaozhi.modules.device.entity.DeviceEntity; import xiaozhi.modules.device.service.DeviceService; import xiaozhi.modules.llm.service.LLMService; -import xiaozhi.modules.agent.vo.AgentInfoVO; import xiaozhi.modules.model.entity.ModelConfigEntity; import xiaozhi.modules.model.service.ModelConfigService; -import java.util.Map; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * 智能体聊天记录总结服务实现类 @@ -42,7 +43,6 @@ public class AgentChatSummaryServiceImpl implements AgentChatSummaryService { private final AgentService agentService; private final DeviceService deviceService; private final LLMService llmService; - private final ConfigService configService; private final ModelConfigService modelConfigService; // 总结规则常量 diff --git a/main/manager-api/src/main/java/xiaozhi/modules/security/config/ShiroConfig.java b/main/manager-api/src/main/java/xiaozhi/modules/security/config/ShiroConfig.java index 051cbc29..29b9ad2f 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/security/config/ShiroConfig.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/security/config/ShiroConfig.java @@ -89,7 +89,7 @@ public class ShiroConfig { filterMap.put("/config/**", "server"); filterMap.put("/agent/chat-history/report", "server"); filterMap.put("/agent/chat-history/download/**", "anon"); - filterMap.put("/agent/saveMemory/**", "server"); + filterMap.put("/agent/chat-summary/**", "server"); filterMap.put("/agent/play/**", "anon"); filterMap.put("/voiceClone/play/**", "anon"); filterMap.put("/**", "oauth2"); diff --git a/main/xiaozhi-server/config/manage_api_client.py b/main/xiaozhi-server/config/manage_api_client.py index 70d9dbf0..f899d61c 100644 --- a/main/xiaozhi-server/config/manage_api_client.py +++ b/main/xiaozhi-server/config/manage_api_client.py @@ -172,20 +172,6 @@ async def get_agent_models( ) -async def save_mem_local_short(mac_address: str, short_momery: str) -> Optional[Dict]: - try: - return await ManageApiClient._instance._execute_async_request( - "PUT", - f"/agent/saveMemory/" + mac_address, - json={ - "summaryMemory": short_momery, - }, - ) - except Exception as e: - print(f"存储短期记忆到服务器失败: {e}") - return None - - async def generate_and_save_chat_summary(session_id: str) -> Optional[Dict]: """生成并保存聊天记录总结""" try: diff --git a/main/xiaozhi-server/core/providers/memory/mem_local_short/mem_local_short.py b/main/xiaozhi-server/core/providers/memory/mem_local_short/mem_local_short.py index e7648bf4..5904d440 100644 --- a/main/xiaozhi-server/core/providers/memory/mem_local_short/mem_local_short.py +++ b/main/xiaozhi-server/core/providers/memory/mem_local_short/mem_local_short.py @@ -4,10 +4,7 @@ import json import os import yaml from config.config_loader import get_project_dir -from config.manage_api_client import ( - save_mem_local_short, - generate_and_save_chat_summary, -) +from config.manage_api_client import generate_and_save_chat_summary import asyncio from core.utils.util import check_model_key @@ -181,13 +178,7 @@ class MemoryProvider(MemoryProviderBase): else: # 当save_to_file为False时,调用Java端的聊天记录总结接口 summary_id = session_id if session_id else self.role_id - # 使用异步版本,需要在事件循环中运行 - try: - loop = asyncio.get_running_loop() - loop.create_task(generate_and_save_chat_summary(summary_id)) - except RuntimeError: - # 如果没有运行中的事件循环,创建一个新的 - asyncio.run(generate_and_save_chat_summary(summary_id)) + await generate_and_save_chat_summary(summary_id) logger.bind(tag=TAG).info( f"Save memory successful - Role: {self.role_id}, Session: {session_id}" )