打断修改
authorhcj <1224667075@qq.com>
Mon, 3 Nov 2025 09:42:25 +0000 (17:42 +0800)
committerhcj <1224667075@qq.com>
Mon, 3 Nov 2025 09:42:25 +0000 (17:42 +0800)
code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c

index 32aef11..bffcdc5 100644 (file)
@@ -140,12 +140,18 @@ static bool check_playback_completion_by_timeout(void);
 uint16_t send_audio=0;
 
 // 音频聊天相关结构体
+typedef enum {
+    LN_CHAT_NOT_ENDED = 0,            // 未结束对话
+    LN_CHAT_SENT_END_NO_REPLY = 1,    // 已发送结束但未收到回复结束
+    LN_CHAT_RECEIVED_END = 2          // 已收到对端结束
+} audio_chat_finish_state_t;
+
 typedef struct {
     char chat_id[64];           // 聊天ID
     uint32_t message_counter;   // 消息计数器
     bool is_active;             // 是否活跃
     bool is_reply;              // 是否回复
-    bool finished;              // 是否完成
+    audio_chat_finish_state_t finish_state; // 对话结束状态
 } audio_chat_context_t;
 
 // 全局音频聊天上下文
@@ -535,7 +541,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;
+        g_audio_chat_ctx.finish_state = LN_CHAT_NOT_ENDED;
         // 重置对话ID匹配标志
         reset_chat_id_match();
         LN_WS_DEBUG("New chat started: %s\n", g_audio_chat_ctx.chat_id);
@@ -687,7 +693,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;
+    g_audio_chat_ctx.finish_state = LN_CHAT_NOT_ENDED;
     
     // 清空音频时长数组,避免不同对话间的数据混淆
     clear_audio_duration_buffer();
@@ -727,6 +733,7 @@ int ln_ws_end_audio_chat(void)
     if (!g_recording_upload_started) {
         LN_WS_WARN("Recording upload was not started, but ending audio chat\n");
     }
+    g_audio_chat_ctx.finish_state = LN_CHAT_SENT_END_NO_REPLY;
     
     // 设置结束标志,等待下次AIVoiceDataCallback_ln调用时发送结束消息
     g_audio_chat_ending = true;
@@ -1431,7 +1438,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.finished ==false) {
+        if (need_reconnect ||  g_audio_chat_ctx.finish_state == LN_CHAT_SENT_END_NO_REPLY) {
             // 如果检测到频繁数据接收,直接重连
             LN_WS_INFO("Triggering reconnection due to frequent binary data reception\n");
             if (ln_ws_reconnect() != 0) {
@@ -1713,9 +1720,9 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms
                 // 对话ID匹配成功,更新全局上下文
                 {
                     g_audio_chat_ctx.is_reply = true;
-                    // 只有在对话ID匹配的情况下才更新finished状态
-                    if (finished && cJSON_IsBool(finished)) {
-                        g_audio_chat_ctx.finished = data_msg->finished;
+                    // 只有在对话ID匹配的情况下才更新结束状态
+                    if (finished && cJSON_IsBool(finished) && data_msg->finished) {
+                        g_audio_chat_ctx.finish_state = LN_CHAT_RECEIVED_END;
                     }
                     LN_WS_DEBUG("VoiceChatResponse matched current chat, set is_reply=true, finished=%s\n",
                            finished && cJSON_IsBool(finished) ? (data_msg->finished ? "true" : "false") : "unknown");
@@ -1776,9 +1783,9 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms
                 // 对话ID匹配成功,更新全局上下文
                 {
                     g_audio_chat_ctx.is_reply = true;
-                    // 只有在对话ID匹配的情况下才更新finished状态
-                    if (finished && cJSON_IsBool(finished)) {
-                        g_audio_chat_ctx.finished = data_msg->finished;
+                    // 只有在对话ID匹配的情况下才更新结束状态
+                    if (finished && cJSON_IsBool(finished) && data_msg->finished) {
+                        g_audio_chat_ctx.finish_state = LN_CHAT_RECEIVED_END;
                     }
                     LN_WS_DEBUG("ChooseStoryResponse matched current chat, set is_reply=true, finished=%s\n",
                            finished && cJSON_IsBool(finished) ? (data_msg->finished ? "true" : "false") : "unknown");
@@ -1838,9 +1845,9 @@ static int ln_ws_parse_data_message(const char *json_str, ws_data_msg_t *data_ms
                 // 对话ID匹配成功,更新全局上下文
                 {
                     g_audio_chat_ctx.is_reply = true;
-                    // 只有在对话ID匹配的情况下才更新finished状态
-                    if (finished && cJSON_IsBool(finished)) {
-                        g_audio_chat_ctx.finished = data_msg->finished;
+                    // 只有在对话ID匹配的情况下才更新结束状态
+                    if (finished && cJSON_IsBool(finished) && data_msg->finished) {
+                        g_audio_chat_ctx.finish_state = LN_CHAT_RECEIVED_END;
                     }
                     LN_WS_DEBUG("RandomStoryResponse matched current chat, set is_reply=true, finished=%s\n",
                            finished && cJSON_IsBool(finished) ? (data_msg->finished ? "true" : "false") : "unknown");
@@ -2735,7 +2742,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;
+        g_audio_chat_ctx.finish_state = LN_CHAT_NOT_ENDED;
         // 重置对话ID匹配标志
         reset_chat_id_match();
         LN_WS_DEBUG("New chat started: %s\n", g_audio_chat_ctx.chat_id);