From 2ad5101a7736dc79f6f576e52b213fcbf1946983 Mon Sep 17 00:00:00 2001 From: Asklv Date: Wed, 18 Feb 2026 19:16:00 +0800 Subject: [PATCH] feat(search): add Tavily result formatter Signed-off-by: Asklv --- main/tools/tool_web_search.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/main/tools/tool_web_search.c b/main/tools/tool_web_search.c index 8df63be..3a293f8 100644 --- a/main/tools/tool_web_search.c +++ b/main/tools/tool_web_search.c @@ -156,6 +156,36 @@ static void format_results(cJSON *root, char *output, size_t output_size) } } +static void format_tavily_results(cJSON *root, char *output, size_t output_size) +{ + cJSON *results = cJSON_GetObjectItem(root, "results"); + if (!results || !cJSON_IsArray(results) || cJSON_GetArraySize(results) == 0) { + snprintf(output, output_size, "No web results found."); + return; + } + + size_t off = 0; + int idx = 0; + cJSON *item; + cJSON_ArrayForEach(item, results) { + if (idx >= SEARCH_RESULT_COUNT) break; + + cJSON *title = cJSON_GetObjectItem(item, "title"); + cJSON *url = cJSON_GetObjectItem(item, "url"); + cJSON *content = cJSON_GetObjectItem(item, "content"); + + off += snprintf(output + off, output_size - off, + "%d. %s\n %s\n %s\n\n", + idx + 1, + (title && cJSON_IsString(title)) ? title->valuestring : "(no title)", + (url && cJSON_IsString(url)) ? url->valuestring : "", + (content && cJSON_IsString(content)) ? content->valuestring : ""); + + if (off >= output_size - 1) break; + idx++; + } +} + /* ── Direct HTTPS request ─────────────────────────────────────── */ static esp_err_t brave_search_direct(const char *url, search_buf_t *sb)