From d494a1025ff0caf9a369cd6f19b7efcd2787a7ab Mon Sep 17 00:00:00 2001 From: Rdzleo Date: Mon, 2 Mar 2026 14:48:42 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=96=B0=E5=A2=9EFunction=20Calling?= =?UTF-8?q?=E7=9A=84=E8=AE=B2=E6=95=85=E4=BA=8B=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E8=AF=AD=E9=9F=B3=E8=8E=B7=E5=8F=96=E5=B0=8F?= =?UTF-8?q?=E6=95=85=E4=BA=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + main/application.cc | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/.gitignore b/.gitignore index 3223c73..d62fd21 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ components/ managed_components/ build/ .vscode/ +.cache/ .devcontainer/ sdkconfig.old sdkconfig diff --git a/main/application.cc b/main/application.cc index c69523c..6041068 100644 --- a/main/application.cc +++ b/main/application.cc @@ -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); + } + } } } // 新增代码(小程序控制 暂停/继续播放 音频)