修改移动ai入库接口
authorhcj <1224667075@qq.com>
Mon, 24 Nov 2025 06:41:12 +0000 (14:41 +0800)
committerhcj <1224667075@qq.com>
Mon, 24 Nov 2025 06:41:12 +0000 (14:41 +0800)
code/sdk/middleware/services/media/foundation/audiomanager/frameworks/audiostream/include/cpp/audio_stream_out.h
code/sdk/zhihui/4g/cat1Service/Service/api_shell.c
code/sdk/zhihui/ai_old/chat_websocket_server.cpp
code/sdk/zhihui/ai_old/chat_websocket_server.h
code/sdk/zhihui/ai_old/tts_websocket_server.cpp
code/sdk/zhihui/ai_old/websocket_server_example.cpp
code/sdk/zhihui/ai_old/zhihui_ws.cpp

index 9cf4c4b..1b2fa72 100644 (file)
@@ -21,7 +21,7 @@ public:
     ~AudioStreamOut();
     int32_t Init(AudioRendererConfig &config, RendererCallback cbk = nullptr, const void *caller = nullptr);
     int32_t Play() const;
-    int32_t Pause() const;
+    int32_t Pause() const;//不建议使用
     int32_t Stop() const;
     int32_t Release() const;
     int32_t SetPlayBackSpeed(float speed) const;
index f230f44..b09432d 100644 (file)
@@ -105,7 +105,7 @@ struct ifconfig_option {
 };
 
 static char result_data[256] = {0};
-static char hostname[20] = {0};
+static char hostname[64] = {0};
 
 void get_ping_hostname(char *databuf,uint8_t len)
 {
index 05bc427..986d0a0 100644 (file)
@@ -275,9 +275,10 @@ bool chat_ws_url_init(char * put_buf, uint8_t type)
  * @param msg 要发送的消息
  * @param chat_client WebSocket客户端
  * @param cache 是否缓存,false: 缓存文本,true: 发送缓存+当前文本
+ * @param system_message 系统消息(可选,为NULL时不添加此字段)
  * @return true 成功,false 失败
  */
-bool ai_chat_send_txt(uint8_t type, char *msg, WebSocketClient *chat_client, bool cache)
+bool ai_chat_send_txt(uint8_t type, char *msg, WebSocketClient *chat_client, bool cache, const char *system_message)
 {   
     if(msg)
         printf("ai_chat_send_txt message: %s, cache: %s\n", msg, cache ? "true" : "false");
@@ -349,6 +350,11 @@ bool ai_chat_send_txt(uint8_t type, char *msg, WebSocketClient *chat_client, boo
     
     cJSON_AddStringToObject(dataJSON, "type", "jiutian-lan");
     cJSON_AddStringToObject(dataJSON, "content", final_msg);
+    
+    // 如果system_message不为NULL,添加到JSON中
+    if (system_message != NULL) {
+        cJSON_AddStringToObject(dataJSON, "system_message", system_message);
+    }
 
     char *jsonString = cJSON_PrintUnformatted(dataJSON);
     
index a79514a..e304bc1 100644 (file)
@@ -84,9 +84,11 @@ bool chat_ws_url_init(char * put_buf, uint8_t type);
  * @param type 消息类型
  * @param msg 消息内容
  * @param chat_client WebSocket客户端(可选,为NULL时使用全局chat_client)
+ * @param cache 是否缓存,false: 缓存文本,true: 发送缓存+当前文本
+ * @param system_message 系统消息(可选,为NULL时不添加此字段)
  * @return 成功返回true,失败返回false
  */
-bool ai_chat_send_txt(uint8_t type, char *msg, WebSocketClient *chat_client, bool cache);
+bool ai_chat_send_txt(uint8_t type, char *msg, WebSocketClient *chat_client, bool cache, const char *system_message);
 
 /**
  * @brief 初始化Chat缓存内存
index 7cadc4c..784df91 100644 (file)
@@ -511,12 +511,7 @@ bool uapi_tts_process_text(WebSocketClient *tts_client, char *buf, bool ready)
         }
     }
     
-    // 检查是否有缓存的stop命令需要发送
-    if (tts_has_stop_cached) {
-        printf("TTS ready, sending cached stop command\n");
-        uapi_stop_tts_type_stop(tts_client);
-        tts_has_stop_cached = false;  // 重置标志
-    }
+
     
     // 发送本次携带的内容
     if (buf && strlen(buf) > 0) {
@@ -531,6 +526,13 @@ bool uapi_tts_process_text(WebSocketClient *tts_client, char *buf, bool ready)
     } else {
         printf("TTS ready but no current text to send\n");
     }
+
+        // 检查是否有缓存的stop命令需要发送
+    if (tts_has_stop_cached) {
+        printf("TTS ready, sending cached stop command\n");
+        uapi_stop_tts_type_stop(tts_client);
+        tts_has_stop_cached = false;  // 重置标志
+    }
     
     return true;
 }
index 97f7e5f..4e6ebf2 100644 (file)
@@ -104,7 +104,6 @@ void websocket_event_callback(const ws_event_data_t *event_data)
                 case WS_SERVER_TYPE_CHAT:
                     if(g_chat_ai_type==1)
                     {
-                        send_ai_system_message(g_chat_server->client);
                         chat_is_fist=1;
                     }
                     break;
@@ -228,7 +227,7 @@ void handle_asr_data(const ws_event_data_t *event_data)
                                      NULL, NULL, 0, 0, 0, NULL);
                     }
                 #endif
-                ai_chat_send_txt(0, data.result_text, g_chat_server->client,false);
+                ai_chat_send_txt(0, data.result_text, g_chat_server->client,false, NULL);
                 if (g_ai_question_cb) g_ai_question_cb(data.result_text); else printf("[WS][ASR] ai_question 回调未注册,丢弃内容: %s\n", data.result_text);
                 }
 
@@ -236,7 +235,16 @@ void handle_asr_data(const ws_event_data_t *event_data)
 
             case ASR_EVENT_TRANSCRIPTION_COMPLETED:
                 g_chat_server->interrupt_flag=0;
-                ai_chat_send_txt(0, "", g_chat_server->client,true);
+                if(chat_is_fist && g_chat_ai_type==1)
+                {
+                    ai_chat_send_txt(0, "", g_chat_server->client,true, "您好,我是中国移动的智能助理灵犀。如果您询问我的身份,我会回答:“您好,我是中国移动智能助理灵犀”。我能够回答各种类型的问题。如果遇到我无法解答的问题,我会告知您:“您的问题超出了我的能力范围,请尝试问其他问题。");
+                    chat_is_fist=0;
+                }
+                else
+                {
+                    ai_chat_send_txt(0, "", g_chat_server->client,true, NULL);
+
+                }
                 asr_is_ready=0;
                 break;
             default:
@@ -328,10 +336,9 @@ void handle_chat_data(const ws_event_data_t *event_data)
 
                     tts_is_ready = TTS_READY_SENT_SUCCESS;
                     g_tts_server->interrupt_flag=0;
-                    if(chat_is_fist!=1)
-                    {
-                        uapi_start_tts_type_start(g_tts_server->client);
-                    }
+          
+                    uapi_start_tts_type_start(g_tts_server->client);
+                    
                     
                 }
                 break;
@@ -339,24 +346,19 @@ void handle_chat_data(const ws_event_data_t *event_data)
             case CHAT_EVENT_MIDDLE:
                 // printf("Chat event: MIDDLE - Content: %s\n", buf);
                 if (g_ai_answer_cb) g_ai_answer_cb(buf, 2); else printf("[WS][CHAT] ai_answer 回调未注册,丢弃内容: %s\n", buf);
-                if(chat_is_fist!=1)
+
                 uapi_tts_process_text(g_tts_server->client,buf,(tts_is_ready == TTS_READY_SUCCESS_READY));
                 break;
                 
             case CHAT_EVENT_END:
                 printf("Chat event: END - Final content: %s\n", buf);
                 if (g_ai_answer_cb) g_ai_answer_cb(buf, 2); else printf("[WS][CHAT] ai_answer 回调未注册,丢弃内容: %s\n", buf);
-                 if(chat_is_fist!=1)
-                 {
+     
+                 
                     uapi_tts_process_text(g_tts_server->client,buf,(tts_is_ready == TTS_READY_SUCCESS_READY));
                     uapi_cache_stop_command(g_tts_server->client,(tts_is_ready == TTS_READY_SUCCESS_READY));
 
-                 }
-                 else
-                 {
-                    chat_is_fist=0;
-                 }
-
+               
                 
                 break;
                 
@@ -581,7 +583,7 @@ errcode_t start_all_websocket_servers(uint8_t chat_ai_type)
 {
     errcode_t ret;
     
-    tts_is_ready == TTS_READY_NOT_SENT;
+    tts_is_ready = TTS_READY_NOT_SENT;
     // 设置全局AI类型
     g_chat_ai_type = chat_ai_type;
     
index be10a4c..1840bf6 100644 (file)
@@ -475,6 +475,7 @@ BK_URL parse_url(const char *url) {
     char *slash = strchr(temp_url, '/');
     if (!slash) {
         set_url_error(&result, "Invalid URL: No path specified");
+        free(result.protocol);
         free(temp_url_t);
         return result;
     }
@@ -495,6 +496,8 @@ BK_URL parse_url(const char *url) {
         result.port = atoi(colon_port + 1);
         if (!result.port) {
             set_url_error(&result, "Invalid port number");
+free(result.protocol);
+            free(result.host);
             free(temp_url_t);
             return result;
         }
@@ -502,6 +505,7 @@ BK_URL parse_url(const char *url) {
         result.host = bk_strdup(temp_url);
         if (!result.host) {
             set_url_error(&result, "Failed to allocate memory for host");
+free(result.protocol);
             free(temp_url_t);
             return result;
         }
@@ -520,12 +524,17 @@ BK_URL parse_url(const char *url) {
         result.path = bk_strdup(temp_url);
         if (!result.path) {
             set_url_error(&result, "Failed to allocate memory for path");
+free(result.protocol);
+            free(result.host);
             free(temp_url_t);
             return result;
         }
         result.query = bk_strdup(question + 1);
         if (!result.query) {
             set_url_error(&result, "Failed to allocate memory for query");
+free(result.protocol);
+            free(result.host);
+            free(result.path);
             free(temp_url_t);
             return result;
         }
@@ -533,6 +542,8 @@ BK_URL parse_url(const char *url) {
         result.path = bk_strdup(temp_url);
         if (!result.path) {
             set_url_error(&result, "Failed to allocate memory for path");
+free(result.protocol);
+            free(result.host);
             free(temp_url_t);
             return result;
         }
@@ -943,6 +954,7 @@ WebSocketClient *init_websocket_client(const char *url, const char *headers,uint
     int sockfd = -1;
 
     int ret=0;
+    int flags;
 
     parsed_url = parse_url(url);//解析url
     bool use_ssl =false;
@@ -1123,6 +1135,9 @@ WebSocketClient *init_websocket_client(const char *url, const char *headers,uint
 
     client->handshake=1;
 
+    // flags = fcntl(client->sockfd, F_GETFL, 0);
+    // fcntl(client->sockfd, F_SETFL, flags | O_NONBLOCK);
+
 
     return client;
 client_ERROR: