解决:1509 手表-蓝牙-AI正在回复中,开启新的对话,显示响应内容为空,不能连续对话 2648/1 2648/head 2649/target/1
authorluozheng <1732036298@qq.com>
Fri, 31 Oct 2025 06:52:46 +0000 (14:52 +0800)
committerluozheng <1732036298@qq.com>
Fri, 31 Oct 2025 06:52:46 +0000 (14:52 +0800)
code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/include/AiVoiceModel.h
code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp

index 17b87f8..10b37c4 100644 (file)
@@ -206,6 +206,9 @@ namespace OHOS
 
         bool isFirstReceive{true};        //静态变量标记是否是首次接收
         bool hasTtsStartSucceeded{false}; // uapi_start_tts 成功后置位,仅允许发送一次
+
+        // UI 合并更新控制:上一次文本更新完成前不再叠加刷新
+        bool isAiAnswerUpdating{false};
     };
 
 }
index 2846941..a9848df 100644 (file)
@@ -443,54 +443,82 @@ namespace OHOS
     bool AiVoiceModel::UpdateAiAnswerStr()
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr");
-        int index = GetChatContentListCount() - 1;
-        if (index < 0 || index >= MaxChatContentListCount)
-        {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid index: %d", index);
-            return false;
-        }
 
-        if (chatTtems[index].isAiAnswer) // 处理ai1的多段回答
+        // 合并节流:上一次未结束则仅记录一次待处理
+        if (isAiAnswerUpdating)
         {
-            ChatContentListCount--;
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "isAiAnswerUpdating");
+            // hasPendingAiAnswerUpdate = true;
+            return true;
         }
+        isAiAnswerUpdating = true;
 
-        if (!AddAiAnswerToChatItems())
+        bool success = true;
+        do
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddAiAnswerToChatItems fail!!");
-            return false;
-        }
+            int index = GetChatContentListCount() - 1;
+            if (index < 0 || index >= MaxChatContentListCount)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid index: %d", index);
+                success = false;
+                break;
+            }
 
-        // 更新显示列表
-        AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance();
-        if (!p_AivoiceReturnResultTextView)
-        {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!");
-            return false;
-        }
+            if (chatTtems[index].isAiAnswer) // 处理ai1的多段回答
+            {
+                ChatContentListCount--;
+            }
 
-        AivoiceChatContentListAdapter *adapter = static_cast<AivoiceChatContentListAdapter *>(p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter());
-        if (adapter != nullptr && !adapter->GetChatContentItem().empty())
-        {
-            // 如果最后一条是 AI 回答,则移除
-            if (adapter->GetChatContentItem().back().isAiAnswer)
+            if (!AddAiAnswerToChatItems())
             {
-                adapter->GetChatContentItem().pop_back();
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddAiAnswerToChatItems fail!!");
+                success = false;
+                break;
             }
-            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,line = %d", GetChatContentListCount() - 1, __LINE__);
-            // 将更新后的 AI 回答内容添加到列表
-            adapter->GetChatContentItem().push_back(chatTtems[GetChatContentListCount() - 1]);
-            if (AiVoicePresenter::GetInstance())
-                AiVoicePresenter::GetInstance()->calculateChatContentItemViewHeight(chatTtems[GetChatContentListCount() - 1].text, true);
-            p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList();
-        }
-        else
-        {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Chat content adapter is nullptr!!");
-            return false;
-        }
 
-        return true;
+            // 更新显示列表
+            AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance();
+            if (!p_AivoiceReturnResultTextView)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!");
+                success = false;
+                break;
+            }
+
+            AivoiceChatContentListAdapter *adapter = static_cast<AivoiceChatContentListAdapter *>(p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter());
+            if (adapter != nullptr && !adapter->GetChatContentItem().empty())
+            {
+                // 如果最后一条是 AI 回答,则移除
+                if (adapter->GetChatContentItem().back().isAiAnswer)
+                {
+                    adapter->GetChatContentItem().pop_back();
+                }
+                WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,line = %d", GetChatContentListCount() - 1, __LINE__);
+                // 将更新后的 AI 回答内容添加到列表
+                adapter->GetChatContentItem().push_back(chatTtems[GetChatContentListCount() - 1]);
+                if (AiVoicePresenter::GetInstance())
+                    AiVoicePresenter::GetInstance()->calculateChatContentItemViewHeight(chatTtems[GetChatContentListCount() - 1].text, true);
+                p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList();
+            }
+            else
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Chat content adapter is nullptr!!");
+                success = false;
+                break;
+            }
+        } while (0);
+
+        // 标记本次结束
+        isAiAnswerUpdating = false;
+
+        // 若期间丢弃过调用,合并为一次刷新
+        // if (hasPendingAiAnswerUpdate)
+        // {
+        //     hasPendingAiAnswerUpdate = false;
+        //     SendMsgToUIService(UI_UPDATE_AI_ANSWER_STR, 0, nullptr, 0);
+        // }
+
+        return success;
     }
 
     void AiVoiceModel::SetIsClickable(bool value)
@@ -844,7 +872,7 @@ namespace OHOS
                     if (!OHOS::AiVoiceModel::GetInstance().GetIsLimitDisplayed()) // 如果ai答案没有显示到极限了,就更新ai答案字符串
                     {
                         aiAnswerStr += str;
-                        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "接受字的符串:%s", aiAnswerStr.c_str());
+                        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "接受字的符串:%s", aiAnswerStr.c_str());
                         // SendMsgToUIService(UI_UPDATE_AI_ANSWER_STR, 0, nullptr, 0);
                     }
                 }
@@ -866,9 +894,9 @@ namespace OHOS
                 if (errnote.empty()) // 如果没有回应数据
                 {
                     WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::GetErrnote is empty!!");
-                    AiVoiceModel::GetInstance().SetErrnote("响应内容为空");
-                    std::string str = AiVoiceModel::GetInstance().GetErrnote();
-                    SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)str.c_str(), str.length());
+                    // AiVoiceModel::GetInstance().SetErrnote("响应内容为空");
+                    // std::string str = AiVoiceModel::GetInstance().GetErrnote();
+                    // SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)str.c_str(), str.length());
                     return;
                 }
             }