1.解决上次对话的问题会拼接到下次对话的问题中bug 1690/3 1690/head
authorluozheng <1732036298@qq.com>
Fri, 14 Mar 2025 01:51:08 +0000 (09:51 +0800)
committerluozheng <1732036298@qq.com>
Fri, 14 Mar 2025 01:51:08 +0000 (09:51 +0800)
2.解决AI大模型对话只会显示问题而不显示答案的bug

code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h
code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp
code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp
code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c
code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.h
code/sdk/application/wearable/service/ble_service/bleservice/bleService.c
code/sdk/application/wearable/service/ble_service/bleservice/bleService.h
code/sdk/middleware/services/media/foundation/sample/wrapper/audio_capture_wrapper.cpp
code/sdk/middleware/services/media/foundation/sample/wrapper/audio_capture_wrapper.h

index ed34b51..6a393a3 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-06-20 15:14:43
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2025-03-12 11:10:53
+ * @LastEditTime: 2025-03-13 16:02:18
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h
  */
 #ifndef AI_VOICE_MODEL_H
@@ -66,7 +66,7 @@ namespace OHOS
         uint8_t GetChatContentListCount(void);
 
         // 更新和清空问题及回答
-        void RemoveLastTwoItemsOrClear();
+        bool RemoveLastTwoItemsOrClear();
         bool UpdateUserQuestionStr(void);
         bool UpdateAiAnswerStr(void);
         void ClearQuestionAndAiAnswerStr(void);
@@ -77,6 +77,7 @@ namespace OHOS
         std::string GetAiAnswerStr(void);
         void SetAiAnswerStrStr(std::string str);
         void SetAiAnswerStrStr(std::string str,uint8_t type);
+        uint8_t GetCurUsingAiModelType(void);
 
         // 文件检查和AI状态管理
         void CheckFileAndSendAIIsReady();
index cbd7562..110c808 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-06-20 15:14:50
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2025-03-12 11:24:13
+ * @LastEditTime: 2025-03-14 09:43:46
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp
  */
 #include "aivoice/include/AiVoiceModel.h"
@@ -164,11 +164,22 @@ namespace OHOS
         auto it = selectAiModelToTextId.find(GetSelectAiModel());
         if (it != selectAiModelToTextId.end())
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[QuerySelectedAiModelIsConnect] SelectAiModel = %d", it->second.second);
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[QuerySelectedAiModelIsConnect] SelectAiModel = %d", it->second.second);
             inquire_AI_is_ready(it->second.second);
         }
     }
 
+    // 获取当前正在使用的AI模型
+    uint8_t AiVoiceModel::GetCurUsingAiModelType(void)
+    {
+        auto it = selectAiModelToTextId.find(GetSelectAiModel());
+        if (it != selectAiModelToTextId.end())
+        {
+            return it->second.second;
+        }
+        return 0;
+    }
+
     /*取消当前AI对话*/
     void AiVoiceModel::CancelCurAIChat(void)
     {
@@ -298,14 +309,14 @@ namespace OHOS
     }
 
     // 函数定义:移除列表的最后两项或清空列表
-    void AiVoiceModel::RemoveLastTwoItemsOrClear()
+    bool AiVoiceModel::RemoveLastTwoItemsOrClear()
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::RemoveLastTwoItemsOrClear");
         AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance();
         if (p_AivoiceReturnResultTextView == nullptr)
         {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!");
-            return;
+            return false;
         }
         // 获取适配器
         AivoiceChatContentListAdapter *adapter = p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter();
@@ -329,6 +340,7 @@ namespace OHOS
             WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "listSize = %d", listSize);
             ChatContentListCount -= (listSize < 2 ? listSize : 2);
         }
+        return true;
     }
 
     /* 更新用户问题字符串 */
@@ -336,7 +348,11 @@ namespace OHOS
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionStr");
 
-        RemoveLastTwoItemsOrClear();
+        if(!RemoveLastTwoItemsOrClear())
+        {
+            return false;
+        }
+        
         if (!AddUserQuestionToChatItems())
         {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddUserQuestionToChatItems fail!!");
@@ -440,7 +456,9 @@ namespace OHOS
             usleep(50000);             // 延时等待创建完成,0.05秒
         }
         /* -------------------------------------------------------------------------- */
-        userQuestionStr += str;
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "传入的问题:%s",str.c_str());
+        userQuestionStr = str;
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "拼接的问题:%s",userQuestionStr.c_str());
         /* -------------------------------------------------------------------------- */
         SendMsgToUIService(UI_UPDATE_USER_QUESTION_STR, 0, nullptr, 0);
     }
@@ -484,6 +502,7 @@ namespace OHOS
             }
         }
         aiAnswerStr += str;
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "接受字的符串:%s", aiAnswerStr.c_str());
         SendMsgToUIService(UI_UPDATE_AI_ANSWER_STR, 0, nullptr, 0);
     }
 
@@ -506,7 +525,7 @@ namespace OHOS
     {
 #endif
         int32_t AudioCaptureSample(int32_t argc, const char *argv[]);
-        int32_t streamingRecordAndSend_AI(void);
+        int32_t streamingRecordAndSend_AI(int type);
         #define IS_SUPPORT_REALTIME_RECORD_SEND_AI 1 //是否支持实时发送录音数据
 #ifdef __cplusplus
     };
@@ -526,7 +545,9 @@ namespace OHOS
             (view_->GetAiVoiceScrollView(AIVOICE_LISTENING_VIEWID)->IsVisible()))
         {
 #if IS_SUPPORT_REALTIME_RECORD_SEND_AI
-            streamingRecordAndSend_AI();//实时发送录音数据
+            int type = GetCurUsingAiModelType();
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "type = %d", type);
+            streamingRecordAndSend_AI(type);//实时发送录音数据
 #else
             std::string audioPath = AiVoiceModel::GetInstance().GetAIAudioPath();
             if (uapi_delete_files(audioPath.c_str())) // 先删除文件
index a9961c1..d22a5c1 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-06-20 15:11:17
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2025-03-12 10:45:44
+ * @LastEditTime: 2025-03-13 11:52:43
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp
  */
 #include "AppViewIDs.h"
@@ -320,7 +320,7 @@ namespace OHOS
             {
                 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CancelCurAIChat");
                 AiVoiceModel::GetInstance().CancelCurAIChat();
-                AiVoiceModel::GetInstance().sendAIIsReady();
+                // AiVoiceModel::GetInstance().sendAIIsReady();
                 PlayersModel::GetInstance()->HandPausePhoneMusic(); // 暂停手机音乐
                 PlayersModel::GetInstance()->PlayerPause();         // 暂停手机音乐
             }
@@ -558,7 +558,7 @@ namespace OHOS
             if (isScroll)
             {
                 // 监听,让List实现滚动
-                AivoiceReturnResultTextView::GetInstance()->GetChatContentList()->ScrollBy(-150);
+                AivoiceReturnResultTextView::GetInstance()->GetChatContentList()->ScrollBy(-120);
                 isScroll = false;
             }
         }
index 05974aa..fc9fdb4 100644 (file)
@@ -360,6 +360,11 @@ static uint8_t Calculate_CRC(uint8_t *data,uint16_t len)//计算20byte
     return crcbuf;\r
 }\r
 \r
+uint8_t Calculate_CRC_AI(uint8_t *data,uint16_t len)\r
+{\r
+       return Calculate_CRC(data,len);\r
+}\r
+\r
 static uint8_t Calculate_CRC_Big(uint8_t *data,uint16_t len)//计算20byte\r
 {\r
     uint8_t crcbuf  = 0;\r
index e985db4..90bccfa 100644 (file)
@@ -825,6 +825,8 @@ void inquire_AI_is_ready(uint8_t type);
 void cancel_AI_cur_chat(void);
 void cancel_AI_cur_chat_(void);
 
+uint8_t Calculate_CRC_AI(uint8_t *data,uint16_t len);
+
 void GetCurDataCmd(uint16_t cmd,uint8_t *data,uint16_t len,uint8_t *totaldata,uint8_t crcvalue);
 
 bool get_ctkd_flag(void);
index 38882d8..1f0930e 100644 (file)
@@ -2440,7 +2440,7 @@ errcode_t uapi_Ai_send_service_init(void)
 uint32_t send_msg_data_into_Ai_msg_queue_count=0;\r
 \r
 \r
-void AI_data_assembly(uint8_t type,uint8_t *buf)\r
+void AI_data_assembly(uint8_t type,uint8_t *buf,uint8_t aitype)\r
 {\r
    \r
     memset_s(buf,ble_nus_max_data_len,0,19);\r
@@ -2457,7 +2457,8 @@ void AI_data_assembly(uint8_t type,uint8_t *buf)
     memcpy(buf + 11, &temp_data, 4); // 数据包ID\r
     memcpy(buf + 15, &temp_data, 2); // 包数据长度\r
     \r
-    buf[18] = 0; // 计算CRC\r
+    buf[17] = aitype;\r
+    buf[18] = Calculate_CRC_AI(buf,18);; // 计算CRC\r
 }\r
 \r
 static uint8_t g_blesrv_mail_flag=0;\r
index 3b0c4c0..5b7eaf7 100644 (file)
@@ -100,8 +100,7 @@ void Check_ble_adv_status(void);
 
 int send_msg_data_into_Ai_msg_queue(void *buff,uint16_t len,uint8_t type);
 
-void AI_data_assembly(uint8_t type,uint8_t *buf);
-
+void AI_data_assembly(uint8_t type,uint8_t *buf,uint8_t aitype);
 
 void set_AI_interval(uint8_t data);
 
index 9f41453..791048a 100755 (executable)
@@ -417,9 +417,10 @@ EXIT:
 }
 
 /* ------------------------------- AI录音数据上传接口 ------------------------------- */
-#define AI_RECORD_TEST 1 //若需要调试可以打开该宏
+#define AI_RECORD_TEST 0 //若需要调试可以打开该宏
 #include "bleService.h"
 static uint32_t AI_audio_size=0;
+static uint8_t Cur_AI_Model= 0;
 static void* AudioCaptureProcess_AI(void* arg)
 {
     #if AI_RECORD_TEST
@@ -430,7 +431,7 @@ static void* AudioCaptureProcess_AI(void* arg)
     g_audioSourceProcessInput.bThreadRun = true;
     uint8_t start_data[19]={0};
     
-    AI_data_assembly(1,start_data);
+    AI_data_assembly(1,start_data, Cur_AI_Model);
     send_msg_data_into_Ai_msg_queue((void *)start_data,19,1);
     AI_audio_size=0;
 
@@ -460,6 +461,7 @@ static void* AudioCaptureProcess_AI(void* arg)
         if (readCnt++ > READ_COUNT_MAX) {
             break;
         }
+        #if AI_RECORD_TEST
         if (readCnt % GET_AUDIO_TIME_INTERVAL_TIMES == 0) {
             if (g_audioCap->GetAudioTime(timeStamp, Timestamp::Timebase::MONOTONIC)) {
                 MEDIA_INFO_LOG("read %d frames timestamp seconds:%ld, nanoseconds:%ld",
@@ -467,13 +469,14 @@ static void* AudioCaptureProcess_AI(void* arg)
             }
         }
         MEDIA_INFO_LOG("audioCap Read readCnt = %d, size = %d", readCnt, ret);
+        #endif
     }
     #if AI_RECORD_TEST
     fclose(pfd);
     #endif
     MEDIA_ERR_LOG("AI_audio_size %d",AI_audio_size);
     
-    AI_data_assembly(0,start_data);
+    AI_data_assembly(0,start_data,Cur_AI_Model);
     send_msg_data_into_Ai_msg_queue((void *)start_data,19,2);
 
 
@@ -482,10 +485,11 @@ static void* AudioCaptureProcess_AI(void* arg)
     return NULL;
 }
 
-int32_t streamingRecordAndSend_AI(void)
+int32_t streamingRecordAndSend_AI(int type)
 {
     const char *argv[] = {"01", "sbc", "16000", "1", "16", "/user/aiRecord.sbc"};
     int32_t argc = 6;
+    Cur_AI_Model = type;
     if (g_audioSourceProcessInput.bThreadRun) {
         return HandleAudioCaptureCmd(argc, argv);
     }
@@ -534,7 +538,9 @@ int32_t streamingRecordAndSend_AI(void)
     audioCapturerInfo.streamType = AUDIO_STREAM_VOICE_RECORD;
     ret = AudioCaptureSetInfo(audioCapturerInfo);
     if (ret != 0) {
+        #if AI_RECORD_TEST
         fclose(pfd);
+        #endif
         g_audioCap.reset();
         (void)g_amInstance.DeactivateAudioInterrupt(g_sample.interrupt);
         goto EXIT;
index 9c06a9f..fbc42bd 100755 (executable)
@@ -16,7 +16,7 @@ extern "C" {
 
 int32_t AudioCaptureSample(int32_t argc, const char *argv[]);
 
-int32_t streamingRecordAndSend_AI(void);
+int32_t streamingRecordAndSend_AI(int type);
 
 #ifdef __cplusplus
 };