update:右智控台下发聊天记录上报策略

This commit is contained in:
hrz
2025-05-12 18:06:58 +08:00
parent ac7b02d28a
commit dd38fc74db
3 changed files with 49 additions and 18 deletions
@@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import xiaozhi.common.constant.Constant;
import xiaozhi.common.exception.ErrorCode; import xiaozhi.common.exception.ErrorCode;
import xiaozhi.common.exception.RenException; import xiaozhi.common.exception.RenException;
import xiaozhi.common.redis.RedisKeys; import xiaozhi.common.redis.RedisKeys;
@@ -108,6 +109,17 @@ public class ConfigServiceImpl implements ConfigService {
// 获取单台设备每天最多输出字数 // 获取单台设备每天最多输出字数
String deviceMaxOutputSize = sysParamsService.getValue("device_max_output_size", true); String deviceMaxOutputSize = sysParamsService.getValue("device_max_output_size", true);
result.put("device_max_output_size", deviceMaxOutputSize); result.put("device_max_output_size", deviceMaxOutputSize);
// 获取聊天记录配置
Integer chatHistoryConf = agent.getChatHistoryConf();
if (agent.getMemModelId() != null && agent.getMemModelId().equals(Constant.MEMORY_NO_MEM)) {
chatHistoryConf = Constant.ChatHistoryConfEnum.IGNORE.getCode();
} else if (agent.getMemModelId() != null
&& !agent.getMemModelId().equals(Constant.MEMORY_NO_MEM)
&& agent.getChatHistoryConf() == null) {
chatHistoryConf = Constant.ChatHistoryConfEnum.RECORD_TEXT_AUDIO.getCode();
}
result.put("chat_history_conf", chatHistoryConf);
// 如果客户端已实例化模型,则不返回 // 如果客户端已实例化模型,则不返回
String alreadySelectedVadModelId = (String) selectedModule.get("VAD"); String alreadySelectedVadModelId = (String) selectedModule.get("VAD");
if (alreadySelectedVadModelId != null && alreadySelectedVadModelId.equals(agent.getVadModelId())) { if (alreadySelectedVadModelId != null && alreadySelectedVadModelId.equals(agent.getVadModelId())) {
+25 -13
View File
@@ -75,6 +75,7 @@ class ConnectionHandler:
self.prompt = None self.prompt = None
self.welcome_msg = None self.welcome_msg = None
self.max_output_size = 0 self.max_output_size = 0
self.chat_history_conf = 0
# 客户端状态相关 # 客户端状态相关
self.client_abort = False self.client_abort = False
@@ -250,11 +251,15 @@ class ConnectionHandler:
self.logger.bind(tag=TAG).info("收到服务器重启指令,准备执行...") self.logger.bind(tag=TAG).info("收到服务器重启指令,准备执行...")
# 发送确认响应 # 发送确认响应
await self.websocket.send(json.dumps({ await self.websocket.send(
"type": "server_response", json.dumps(
"status": "success", {
"message": "服务器重启中..." "type": "server_response",
})) "status": "success",
"message": "服务器重启中...",
}
)
)
# 异步执行重启操作 # 异步执行重启操作
def restart_server(): def restart_server():
@@ -266,7 +271,7 @@ class ConnectionHandler:
stdin=sys.stdin, stdin=sys.stdin,
stdout=sys.stdout, stdout=sys.stdout,
stderr=sys.stderr, stderr=sys.stderr,
start_new_session=True start_new_session=True,
) )
os._exit(0) os._exit(0)
@@ -275,11 +280,15 @@ class ConnectionHandler:
except Exception as e: except Exception as e:
self.logger.bind(tag=TAG).error(f"重启失败: {str(e)}") self.logger.bind(tag=TAG).error(f"重启失败: {str(e)}")
await self.websocket.send(json.dumps({ await self.websocket.send(
"type": "server_response", json.dumps(
"status": "error", {
"message": f"Restart failed: {str(e)}" "type": "server_response",
})) "status": "error",
"message": f"Restart failed: {str(e)}",
}
)
)
def _initialize_components(self): def _initialize_components(self):
"""初始化组件""" """初始化组件"""
@@ -306,6 +315,8 @@ class ConnectionHandler:
"""初始化ASR和TTS上报线程""" """初始化ASR和TTS上报线程"""
if not self.read_config_from_api or self.need_bind: if not self.read_config_from_api or self.need_bind:
return return
if self.chat_history_conf == 0:
return
if self.tts_report_thread is None or not self.tts_report_thread.is_alive(): if self.tts_report_thread is None or not self.tts_report_thread.is_alive():
self.tts_report_thread = threading.Thread( self.tts_report_thread = threading.Thread(
target=self._tts_report_worker, daemon=True target=self._tts_report_worker, daemon=True
@@ -379,7 +390,8 @@ class ConnectionHandler:
self.config["prompt"] = private_config["prompt"] self.config["prompt"] = private_config["prompt"]
if private_config.get("device_max_output_size", None) is not None: if private_config.get("device_max_output_size", None) is not None:
self.max_output_size = int(private_config["device_max_output_size"]) self.max_output_size = int(private_config["device_max_output_size"])
if private_config.get("chat_history_conf", None) is not None:
self.chat_history_conf = int(private_config["chat_history_conf"])
try: try:
modules = initialize_modules( modules = initialize_modules(
self.logger, self.logger,
@@ -838,7 +850,7 @@ class ConnectionHandler:
if future is None: if future is None:
continue continue
text = None text = None
opus_datas, tts_file = [], None audio_datas, tts_file = [], None
try: try:
self.logger.bind(tag=TAG).debug("正在处理TTS任务...") self.logger.bind(tag=TAG).debug("正在处理TTS任务...")
tts_timeout = int(self.config.get("tts_timeout", 10)) tts_timeout = int(self.config.get("tts_timeout", 10))
@@ -92,6 +92,8 @@ def opus_to_wav(conn, opus_data):
def enqueue_tts_report(conn, type, text, opus_data): def enqueue_tts_report(conn, type, text, opus_data):
if not conn.read_config_from_api or conn.need_bind: if not conn.read_config_from_api or conn.need_bind:
return return
if conn.chat_history_conf == 0:
return
"""将TTS数据加入上报队列 """将TTS数据加入上报队列
Args: Args:
@@ -101,10 +103,15 @@ def enqueue_tts_report(conn, type, text, opus_data):
""" """
try: try:
# 使用连接对象的队列,传入文本和二进制数据而非文件路径 # 使用连接对象的队列,传入文本和二进制数据而非文件路径
conn.tts_report_queue.put((type, text, opus_data)) if conn.chat_history_conf == 2:
conn.tts_report_queue.put((type, text, opus_data))
conn.logger.bind(tag=TAG).debug( conn.logger.bind(tag=TAG).debug(
f"TTS数据已加入上报队列: {conn.device_id}, 音频大小: {len(opus_data)} " f"TTS数据已加入上报队列: {conn.device_id}, 音频大小: {len(opus_data)} "
) )
else:
conn.tts_report_queue.put((type, text, None))
conn.logger.bind(tag=TAG).debug(
f"TTS数据已加入上报队列: {conn.device_id}, 不上报音频"
)
except Exception as e: except Exception as e:
conn.logger.bind(tag=TAG).error(f"加入TTS上报队列失败: {text}, {e}") conn.logger.bind(tag=TAG).error(f"加入TTS上报队列失败: {text}, {e}")