From 3463ac525081a54925dc54ae51e440d807241c99 Mon Sep 17 00:00:00 2001 From: hcj <1224667075@qq.com> Date: Sun, 2 Nov 2025 21:14:59 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E6=94=B9=E6=89=93=E6=96=AD=E9=80=BB?= =?utf8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../zhihui/leiniao/imsl_ln/ln_api_websocket.c | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c index b53a320fa..32aef112c 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c @@ -145,6 +145,7 @@ typedef struct { uint32_t message_counter; // 消息计数器 bool is_active; // 是否活跃 bool is_reply; // 是否回复 + bool finished; // 是否完成 } audio_chat_context_t; // 全局音频聊天上下文 @@ -534,6 +535,7 @@ int ln_ws_send_audio_data(const uint8_t *audio_data, uint32_t audio_size, bool f g_audio_chat_ctx.message_counter = 0; g_audio_chat_ctx.is_active = true; g_audio_chat_ctx.is_reply = false; + g_audio_chat_ctx.finished = false; // 重置对话ID匹配标志 reset_chat_id_match(); LN_WS_DEBUG("New chat started: %s\n", g_audio_chat_ctx.chat_id); @@ -685,6 +687,7 @@ int ln_ws_start_audio_chat(ln_audio_chat_command_t command_type) g_audio_chat_ctx.message_counter = 0; g_audio_chat_ctx.is_active = true; g_audio_chat_ctx.is_reply = false; + g_audio_chat_ctx.finished = false; // 清空音频时长数组,避免不同对话间的数据混淆 clear_audio_duration_buffer(); @@ -1428,7 +1431,7 @@ int ln_ws_send_voice_chat_interrupt(const char *reason) if (ret == 0) { LN_WS_DEBUG("VoiceChatInterrupt sent successfully\n"); - if (need_reconnect || g_audio_chat_ctx.is_reply==false) { + if (need_reconnect || g_audio_chat_ctx.finished ==false) { // 如果检测到频繁数据接收,直接重连 LN_WS_INFO("Triggering reconnection due to frequent binary data reception\n"); if (ln_ws_reconnect() != 0) { @@ -1691,7 +1694,7 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms if (data_msg->command == WS_CMD_VOICE_CHAT_RESPONSE) { cJSON *data_obj = cJSON_GetObjectItem(root, "data"); if (data_obj && cJSON_IsObject(data_obj)) { - // 解析finished字段 + // 解析finished字段(先解析到data_msg,但不更新全局上下文) cJSON *finished = cJSON_GetObjectItem(data_obj, "finished"); if (finished && cJSON_IsBool(finished)) { data_msg->finished = cJSON_IsTrue(finished); @@ -1707,10 +1710,15 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms if (ln_ws_check_chat_id_match(data_msg->chat_id, root) != 0) { return -1; // 不匹配,已清理JSON } - // 对话ID匹配成功,标记为已回复(仅当对话活跃时) - { + // 对话ID匹配成功,更新全局上下文 + { g_audio_chat_ctx.is_reply = true; - LN_WS_DEBUG("VoiceChatResponse matched current chat, set is_reply=true\n"); + // 只有在对话ID匹配的情况下才更新finished状态 + if (finished && cJSON_IsBool(finished)) { + g_audio_chat_ctx.finished = data_msg->finished; + } + LN_WS_DEBUG("VoiceChatResponse matched current chat, set is_reply=true, finished=%s\n", + finished && cJSON_IsBool(finished) ? (data_msg->finished ? "true" : "false") : "unknown"); } } @@ -1749,7 +1757,7 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms if (data_msg->command == WS_CMD_CHOOSE_STORY_RESPONSE) { cJSON *data_obj = cJSON_GetObjectItem(root, "data"); if (data_obj && cJSON_IsObject(data_obj)) { - // 解析finished字段 + // 解析finished字段(先解析到data_msg,但不更新全局上下文) cJSON *finished = cJSON_GetObjectItem(data_obj, "finished"); if (finished && cJSON_IsBool(finished)) { data_msg->finished = cJSON_IsTrue(finished); @@ -1765,10 +1773,15 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms if (ln_ws_check_chat_id_match(data_msg->chat_id, root) != 0) { return -1; // 不匹配,已清理JSON } - // 对话ID匹配成功,标记为已回复(仅当对话活跃时) - { + // 对话ID匹配成功,更新全局上下文 + { g_audio_chat_ctx.is_reply = true; - LN_WS_DEBUG("ChooseStoryResponse matched current chat, set is_reply=true\n"); + // 只有在对话ID匹配的情况下才更新finished状态 + if (finished && cJSON_IsBool(finished)) { + g_audio_chat_ctx.finished = data_msg->finished; + } + LN_WS_DEBUG("ChooseStoryResponse matched current chat, set is_reply=true, finished=%s\n", + finished && cJSON_IsBool(finished) ? (data_msg->finished ? "true" : "false") : "unknown"); } } @@ -1806,7 +1819,7 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms if (data_msg->command == WS_CMD_RANDOM_STORY_RESPONSE) { cJSON *data_obj = cJSON_GetObjectItem(root, "data"); if (data_obj && cJSON_IsObject(data_obj)) { - // 解析finished字段 + // 解析finished字段(先解析到data_msg,但不更新全局上下文) cJSON *finished = cJSON_GetObjectItem(data_obj, "finished"); if (finished && cJSON_IsBool(finished)) { data_msg->finished = cJSON_IsTrue(finished); @@ -1822,10 +1835,15 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms if (ln_ws_check_chat_id_match(data_msg->chat_id, root) != 0) { return -1; // 不匹配,已清理JSON } - // 对话ID匹配成功,标记为已回复(仅当对话活跃时) + // 对话ID匹配成功,更新全局上下文 { g_audio_chat_ctx.is_reply = true; - LN_WS_DEBUG("RandomStoryResponse matched current chat, set is_reply=true\n"); + // 只有在对话ID匹配的情况下才更新finished状态 + if (finished && cJSON_IsBool(finished)) { + g_audio_chat_ctx.finished = data_msg->finished; + } + LN_WS_DEBUG("RandomStoryResponse matched current chat, set is_reply=true, finished=%s\n", + finished && cJSON_IsBool(finished) ? (data_msg->finished ? "true" : "false") : "unknown"); } } @@ -2717,6 +2735,7 @@ static int ln_ws_send_audio_data_direct(const uint8_t *audio_data, uint32_t audi g_audio_chat_ctx.message_counter = 0; g_audio_chat_ctx.is_active = true; g_audio_chat_ctx.is_reply = false; + g_audio_chat_ctx.finished = false; // 重置对话ID匹配标志 reset_chat_id_match(); LN_WS_DEBUG("New chat started: %s\n", g_audio_chat_ctx.chat_id); -- 2.17.1