Skip to content

Commit ae05489

Browse files
authored
Refactor JSON handling and enhance error checking in AI components (#548)
1 parent b4712cb commit ae05489

10 files changed

Lines changed: 300 additions & 102 deletions

File tree

apps/tuya.ai/ai_components/ai_audio/src/ai_audio_input.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ static OPERATE_RET __audio_slice_check_and_send(bool *more_data)
7171
if (len >= sg_recorder->slice_size) {
7272

7373
uint8_t *cache_data = (uint8_t*)Malloc(sg_recorder->slice_size);
74+
if (NULL == cache_data) {
75+
PR_ERR("malloc cache_data fail, size: %d", sg_recorder->slice_size);
76+
tal_mutex_unlock(sg_recorder->mutex);
77+
return OPRT_MALLOC_FAILED;
78+
}
7479
uint32_t read_len = tuya_ring_buff_read(sg_recorder->ringbuf, cache_data, sg_recorder->slice_size);
7580

7681
sg_recorder->output_cb(cache_data, read_len);

apps/tuya.ai/ai_components/ai_mcp/src/ai_mcp_server.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ static MCP_SERVER_CTX_T s_server_ctx;
8181
/***********************************************************
8282
***********************function define**********************
8383
***********************************************************/
84+
static const char *__json_get_string(const cJSON *item)
85+
{
86+
if (item == NULL || !cJSON_IsString(item) || item->valuestring == NULL) {
87+
return NULL;
88+
}
89+
return item->valuestring;
90+
}
91+
8492
MCP_PROPERTY_T *ai_mcp_property_create(const char *name, MCP_PROPERTY_TYPE_E type, const char *description)
8593
{
8694
if (!name)
@@ -981,8 +989,10 @@ static OPERATE_RET __handle_tools_list(cJSON *params, const char *id)
981989
/* Parse parameters */
982990
if (params) {
983991
cJSON *cursor = cJSON_GetObjectItem(params, "cursor");
984-
if (cJSON_IsString(cursor))
985-
cursor_str = cursor->valuestring;
992+
const char *cursor_value = __json_get_string(cursor);
993+
if (cursor_value != NULL) {
994+
cursor_str = cursor_value;
995+
}
986996
}
987997

988998
result = cJSON_CreateObject();
@@ -1085,12 +1095,12 @@ static OPERATE_RET __parse_property_value(MCP_PROPERTY_LIST_T *prop_list,
10851095
break;
10861096

10871097
case MCP_PROPERTY_TYPE_STRING:
1088-
if (!cJSON_IsString(value))
1098+
if (!__json_get_string(value))
10891099
return OPRT_INVALID_PARM;
10901100
prop->default_val.type = MCP_PROPERTY_TYPE_STRING;
10911101
if (prop->has_default && prop->default_val.str_val)
10921102
AI_MCP_FREE(prop->default_val.str_val); // Free existing string
1093-
prop->default_val.str_val = mm_strdup(value->valuestring);
1103+
prop->default_val.str_val = mm_strdup(__json_get_string(value));
10941104
if (!prop->default_val.str_val)
10951105
return OPRT_MALLOC_FAILED;
10961106
prop->has_default = true;
@@ -1121,12 +1131,12 @@ static OPERATE_RET __handle_tools_call(cJSON *params, const char *id)
11211131
}
11221132

11231133
tool_name_json = cJSON_GetObjectItem(params, "name");
1124-
if (!cJSON_IsString(tool_name_json)) {
1134+
tool_name = __json_get_string(tool_name_json);
1135+
if (tool_name == NULL) {
11251136
error_code = MCP_ERROR_INVALID_PARAMS;
11261137
error_msg = "Missing tool name";
11271138
goto err;
11281139
}
1129-
tool_name = tool_name_json->valuestring;
11301140

11311141
tool_arguments = cJSON_GetObjectItem(params, "arguments");
11321142
if (tool_arguments && !cJSON_IsObject(tool_arguments)) {
@@ -1217,18 +1227,18 @@ OPERATE_RET ai_mcp_server_parse_message(const cJSON *json, VOID *user_data)
12171227

12181228
/* Check JSONRPC version */
12191229
node = cJSON_GetObjectItem(json, "jsonrpc");
1220-
if (!node || !cJSON_IsString(node) || strcmp(node->valuestring, "2.0") != 0) {
1230+
if (__json_get_string(node) == NULL || strcmp(__json_get_string(node), "2.0") != 0) {
12211231
PR_ERR("Invalid JSONRPC version");
12221232
return OPRT_INVALID_PARM;
12231233
}
12241234

12251235
/* Check method */
12261236
node = cJSON_GetObjectItem(json, "method");
1227-
if (!node || !cJSON_IsString(node)) {
1237+
method = __json_get_string(node);
1238+
if (method == NULL) {
12281239
PR_ERR("Missing method");
12291240
return OPRT_INVALID_PARM;
12301241
}
1231-
method = node->valuestring;
12321242

12331243
/* Skip notifications */
12341244
if (strncmp(method, "notifications", 13) == 0)
@@ -1241,11 +1251,11 @@ OPERATE_RET ai_mcp_server_parse_message(const cJSON *json, VOID *user_data)
12411251
return OPRT_INVALID_PARM;
12421252
}
12431253

1244-
if (!node || !cJSON_IsString(node) || node->valuestring == NULL) {
1254+
id = __json_get_string(node);
1255+
if (id == NULL) {
12451256
PR_ERR("Missing ID or Invalid ID type for method: %s", method);
12461257
return OPRT_INVALID_PARM;
12471258
}
1248-
id = node->valuestring;
12491259

12501260
/* Get params */
12511261
node = cJSON_GetObjectItem(json, "params");

apps/tuya.ai/ai_components/ai_skills/src/ai_skill.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@
4343
/***********************************************************
4444
***********************function define**********************
4545
***********************************************************/
46+
static const char *__json_get_string(const cJSON *item)
47+
{
48+
if (item == NULL || !cJSON_IsString(item) || item->valuestring == NULL) {
49+
return NULL;
50+
}
51+
52+
return item->valuestring;
53+
}
54+
4655
/**
4756
* @brief Process AI skill data from JSON.
4857
*
@@ -58,7 +67,7 @@ static OPERATE_RET __ai_skills_process(cJSON *root, bool eof)
5867

5968
/* Root is data:{}, parse code */
6069
node = cJSON_GetObjectItem(root, "code");
61-
code = cJSON_GetStringValue(node);
70+
code = __json_get_string(node);
6271
if (!code)
6372
return OPRT_OK;
6473

@@ -102,14 +111,14 @@ static OPERATE_RET __ai_skills_process(cJSON *root, bool eof)
102111
*/
103112
static OPERATE_RET __ai_asr_process(cJSON *root, bool eof)
104113
{
105-
char *content = cJSON_GetStringValue(root);
106-
PR_NOTICE("text -> ASR result: %s", content);
114+
const char *content = __json_get_string(root);
107115
if (!content) {
108116
content = "";
109117
}
118+
PR_NOTICE("text -> ASR result: %s", content);
110119

111120
AI_NOTIFY_TEXT_T text;
112-
text.data = content;
121+
text.data = (char *)content;
113122
text.datalen = strlen(content);
114123
text.timeindex = 0;
115124
ai_user_event_notify((0 == strlen(content))?AI_USER_EVT_ASR_EMPTY:AI_USER_EVT_ASR_OK, &text);
@@ -127,10 +136,7 @@ static OPERATE_RET __ai_asr_process(cJSON *root, bool eof)
127136
static OPERATE_RET __ai_images_process(cJSON *root)
128137
{
129138
cJSON *images = cJSON_GetObjectItem(root, "images");
130-
if(NULL == images) {
131-
PR_ERR("no images found");
132-
return OPRT_COM_ERROR;
133-
}
139+
TUYA_CHECK_NULL_RETURN(images, OPRT_COM_ERROR);
134140

135141
cJSON *url_array = cJSON_GetObjectItem(images, "url");
136142
if(NULL == url_array || !cJSON_IsArray(url_array)) {
@@ -146,7 +152,7 @@ static OPERATE_RET __ai_images_process(cJSON *root)
146152
continue;
147153
}
148154

149-
char *url_str = cJSON_GetStringValue(url_item);
155+
const char *url_str = __json_get_string(url_item);
150156
if(NULL == url_str) {
151157
PR_ERR("url string is null");
152158
continue;
@@ -192,13 +198,13 @@ static OPERATE_RET __ai_nlg_process(cJSON *root, bool eof)
192198
return OPRT_OK;
193199
}
194200

195-
char *content = cJSON_GetStringValue(cJSON_GetObjectItem(root, "content"));
201+
const char *content = __json_get_string(cJSON_GetObjectItem(root, "content"));
196202
if (!content) {
197203
content = "";
198204
}
199205

200206
AI_NOTIFY_TEXT_T text;
201-
text.data = content;
207+
text.data = (char *)content;
202208
text.datalen = strlen(content);
203209
PR_NOTICE("text -> NLG eof: %d, content: %s, time: %d", eof, content, text.timeindex);
204210

@@ -227,7 +233,7 @@ static OPERATE_RET __ai_nlg_process(cJSON *root, bool eof)
227233
AI_AGENT_EMO_T emo;
228234
cJSON *tags_array = cJSON_GetObjectItem(root, "tags");
229235
if (tags_array && cJSON_IsArray(tags_array) && cJSON_GetArraySize(tags_array) > 0) {
230-
char *emoji = cJSON_GetStringValue(cJSON_GetArrayItem(tags_array, 0));
236+
const char *emoji = __json_get_string(cJSON_GetArrayItem(tags_array, 0));
231237
if (emoji && strlen(emoji)) {
232238
emo.emoji = emoji;
233239
emo.name = ai_agent_emoji_get_name(emoji);
@@ -273,4 +279,4 @@ OPERATE_RET ai_text_process(AI_TEXT_TYPE_E type, cJSON *root, bool eof)
273279
}
274280

275281
return OPRT_OK;
276-
}
282+
}

0 commit comments

Comments
 (0)