From c5487e3e42bb9366bdc3098b77a416609b0f2a33 Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Fri, 31 Oct 2025 14:52:46 +0800 Subject: [PATCH] =?utf8?q?=E8=A7=A3=E5=86=B3=EF=BC=9A1509=20=E6=89=8B?= =?utf8?q?=E8=A1=A8-=E8=93=9D=E7=89=99-AI=E6=AD=A3=E5=9C=A8=E5=9B=9E?= =?utf8?q?=E5=A4=8D=E4=B8=AD=EF=BC=8C=E5=BC=80=E5=90=AF=E6=96=B0=E7=9A=84?= =?utf8?q?=E5=AF=B9=E8=AF=9D=EF=BC=8C=E6=98=BE=E7=A4=BA=E5=93=8D=E5=BA=94?= =?utf8?q?=E5=86=85=E5=AE=B9=E4=B8=BA=E7=A9=BA=EF=BC=8C=E4=B8=8D=E8=83=BD?= =?utf8?q?=E8=BF=9E=E7=BB=AD=E5=AF=B9=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/aivoice/include/AiVoiceModel.h | 3 + .../nativeui/aivoice/src/AiVoiceModel.cpp | 112 +++++++++++------- 2 files changed, 73 insertions(+), 42 deletions(-) diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/include/AiVoiceModel.h b/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/include/AiVoiceModel.h index 17b87f873..10b37c4c1 100644 --- a/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/include/AiVoiceModel.h +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/include/AiVoiceModel.h @@ -206,6 +206,9 @@ namespace OHOS bool isFirstReceive{true}; //静态变量标记是否是首次接收 bool hasTtsStartSucceeded{false}; // uapi_start_tts 成功后置位,仅允许发送一次 + + // UI 合并更新控制:上一次文本更新完成前不再叠加刷新 + bool isAiAnswerUpdating{false}; }; } diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp index 284694110..a9848dfa6 100644 --- a/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceModel.cpp @@ -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(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(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; } } -- 2.17.1