1、新增Function Calling的讲故事功能,可以语音获取小故事

This commit is contained in:
Rdzleo 2026-03-02 14:48:42 +08:00
parent 7663018d4c
commit d494a1025f
2 changed files with 55 additions and 0 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ components/
managed_components/
build/
.vscode/
.cache/
.devcontainer/
sdkconfig.old
sdkconfig

View File

@ -899,6 +899,24 @@ void Application::Start() {
}
}
}
// 讲故事功能通过WebSocket发送故事请求
else if (strcmp(name->valuestring, "obtain_story") == 0) {
ESP_LOGI(TAG, "收到obtain_story工具调用通过WebSocket请求故事");
cJSON* sn = cJSON_GetObjectItem(args_obj, "story_name");
const char* story = (sn && cJSON_IsString(sn) && sn->valuestring) ? sn->valuestring : "random";
ESP_LOGI(TAG, "故事名称: %s", story);
// 打断当前播放 + 通过WebSocket发送故事请求和按story按钮一样
AbortSpeaking(kAbortReasonNone);
SendStoryRequest();
std::string msg = "正在为你获取故事";
cJSON* call_id_item = cJSON_GetObjectItem(call, "id");
const char* call_id = (call_id_item && cJSON_IsString(call_id_item) && call_id_item->valuestring) ? call_id_item->valuestring : "";
if (protocol_ && call_id && call_id[0] != '\0') {
protocol_->SendFunctionResult(call_id, msg);
}
}
// // 添加天气查询功能处理 get_weather_aihub
// else if (strcmp(name->valuestring, "get_weather_aihub") == 0) {
// ESP_LOGI(TAG, "[WeatherAPI] ===== 收到get_weather_aihub工具调用 =====");
@ -1094,6 +1112,18 @@ void Application::Start() {
}
}
}
// 讲故事功能通过WebSocket发送故事请求
else if (strcmp(name->valuestring, "obtain_story") == 0) {
ESP_LOGI(TAG, "收到obtain_story工具调用通过WebSocket请求故事");
AbortSpeaking(kAbortReasonNone);
SendStoryRequest();
std::string msg = "正在为你获取故事";
cJSON* call_id_item = cJSON_GetObjectItem(call, "id");
const char* call_id = (call_id_item && cJSON_IsString(call_id_item) && call_id_item->valuestring) ? call_id_item->valuestring : "";
if (protocol_ && call_id && call_id[0] != '\0') {
protocol_->SendFunctionResult(call_id, msg);
}
}
}
}
}
@ -1215,6 +1245,18 @@ void Application::Start() {
}
}
}
// 讲故事功能通过WebSocket发送故事请求
else if (strcmp(name->valuestring, "obtain_story") == 0) {
ESP_LOGI(TAG, "收到obtain_story工具调用通过WebSocket请求故事");
AbortSpeaking(kAbortReasonNone);
SendStoryRequest();
std::string msg = "正在为你获取故事";
cJSON* call_id_item = cJSON_GetObjectItem(root, "call_id");
const char* call_id = (call_id_item && cJSON_IsString(call_id_item) && call_id_item->valuestring) ? call_id_item->valuestring : "";
if (protocol_ && call_id && call_id[0] != '\0') {
protocol_->SendFunctionResult(call_id, msg);
}
}
cJSON_Delete(args_obj);
} else {
ESP_LOGI(TAG, "工具调用: name=%s arguments=%s", name->valuestring, args_str);
@ -1267,6 +1309,18 @@ void Application::Start() {
}
}
}
// 讲故事功能通过WebSocket发送故事请求
else if (strcmp(name->valuestring, "obtain_story") == 0) {
ESP_LOGI(TAG, "收到obtain_story工具调用通过WebSocket请求故事");
AbortSpeaking(kAbortReasonNone);
SendStoryRequest();
std::string msg = "正在为你获取故事";
cJSON* call_id_item = cJSON_GetObjectItem(root, "call_id");
const char* call_id = (call_id_item && cJSON_IsString(call_id_item) && call_id_item->valuestring) ? call_id_item->valuestring : "";
if (protocol_ && call_id && call_id[0] != '\0') {
protocol_->SendFunctionResult(call_id, msg);
}
}
}
}
// 新增代码(小程序控制 暂停/继续播放 音频)