feat: restore NVS runtime config with CLI commands (NVS > build-time priority)

Bring back two-layer configuration: build-time defaults from mimi_secrets.h
with runtime NVS overrides via serial CLI. NVS values take highest priority
so a pre-flashed board can be reconfigured anywhere with just a USB cable.

Restored CLI commands: wifi_set, set_tg_token, set_api_key, set_model,
set_proxy, clear_proxy, set_search_key. Added new commands: config_show
(displays all config with sensitive fields masked), config_reset (clears
all NVS overrides), and help (lists all commands).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
crispyberry
2026-02-08 20:42:10 +08:00
parent 9eb26587ed
commit da43c27003
15 changed files with 568 additions and 20 deletions

View File

@@ -8,6 +8,7 @@
#include "esp_http_client.h"
#include "esp_crt_bundle.h"
#include "esp_heap_caps.h"
#include "nvs.h"
#include "cJSON.h"
static const char *TAG = "web_search";
@@ -43,14 +44,26 @@ static esp_err_t http_event_handler(esp_http_client_event_t *evt)
esp_err_t tool_web_search_init(void)
{
/* Start with build-time default */
if (MIMI_SECRET_SEARCH_KEY[0] != '\0') {
strncpy(s_search_key, MIMI_SECRET_SEARCH_KEY, sizeof(s_search_key) - 1);
}
/* NVS overrides take highest priority (set via CLI) */
nvs_handle_t nvs;
if (nvs_open(MIMI_NVS_SEARCH, NVS_READONLY, &nvs) == ESP_OK) {
char tmp[128] = {0};
size_t len = sizeof(tmp);
if (nvs_get_str(nvs, MIMI_NVS_KEY_API_KEY, tmp, &len) == ESP_OK && tmp[0]) {
strncpy(s_search_key, tmp, sizeof(s_search_key) - 1);
}
nvs_close(nvs);
}
if (s_search_key[0]) {
ESP_LOGI(TAG, "Web search initialized (key configured)");
} else {
ESP_LOGW(TAG, "No search API key. Set MIMI_SECRET_SEARCH_KEY in mimi_secrets.h");
ESP_LOGW(TAG, "No search API key. Use CLI: set_search_key <KEY>");
}
return ESP_OK;
}
@@ -283,3 +296,16 @@ esp_err_t tool_web_search_execute(const char *input_json, char *output, size_t o
ESP_LOGI(TAG, "Search complete, %d bytes result", (int)strlen(output));
return ESP_OK;
}
esp_err_t tool_web_search_set_key(const char *api_key)
{
nvs_handle_t nvs;
ESP_ERROR_CHECK(nvs_open(MIMI_NVS_SEARCH, NVS_READWRITE, &nvs));
ESP_ERROR_CHECK(nvs_set_str(nvs, MIMI_NVS_KEY_API_KEY, api_key));
ESP_ERROR_CHECK(nvs_commit(nvs));
nvs_close(nvs);
strncpy(s_search_key, api_key, sizeof(s_search_key) - 1);
ESP_LOGI(TAG, "Search API key saved");
return ESP_OK;
}

View File

@@ -18,3 +18,7 @@ esp_err_t tool_web_search_init(void);
*/
esp_err_t tool_web_search_execute(const char *input_json, char *output, size_t output_size);
/**
* Save Brave Search API key to NVS.
*/
esp_err_t tool_web_search_set_key(const char *api_key);