237 lines
9.2 KiB
C
Raw Normal View History

// retoor <retoor@molodetz.nl>
#include "tool.h"
#include "http_client.h"
#include "r_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
static struct json_object *http_fetch_get_description(void);
static char *http_fetch_execute(tool_t *self, struct json_object *args);
static void http_fetch_print_action(const char *name, struct json_object *args);
static struct json_object *web_search_get_description(void);
static char *web_search_execute(tool_t *self, struct json_object *args);
static void web_search_print_action(const char *name, struct json_object *args);
static struct json_object *web_search_news_get_description(void);
static char *web_search_news_execute(tool_t *self, struct json_object *args);
static const tool_vtable_t http_fetch_vtable = {
.get_description = http_fetch_get_description,
.execute = http_fetch_execute,
.print_action = http_fetch_print_action
};
static const tool_vtable_t web_search_vtable = {
.get_description = web_search_get_description,
.execute = web_search_execute,
.print_action = web_search_print_action
};
static const tool_vtable_t web_search_news_vtable = {
.get_description = web_search_news_get_description,
.execute = web_search_news_execute,
.print_action = web_search_print_action
};
static tool_t http_fetch_tool = { .vtable = &http_fetch_vtable, .name = "http_fetch" };
static tool_t web_search_tool = { .vtable = &web_search_vtable, .name = "web_search" };
static tool_t web_search_news_tool = { .vtable = &web_search_news_vtable, .name = "web_search_news" };
tool_t *tool_http_fetch_create(void) { return &http_fetch_tool; }
tool_t *tool_web_search_create(void) { return &web_search_tool; }
tool_t *tool_web_search_news_create(void) { return &web_search_news_tool; }
static void http_fetch_print_action(const char *name, struct json_object *args) {
(void)name;
if (!args) return;
struct json_object *url;
if (json_object_object_get_ex(args, "url", &url)) {
fprintf(stderr, " -> Fetching URL: %s\n", json_object_get_string(url));
}
}
static char *http_fetch_execute(tool_t *self, struct json_object *args) {
(void)self;
struct json_object *url_obj;
if (!json_object_object_get_ex(args, "url", &url_obj)) {
return strdup("Error: missing 'url' argument");
}
http_client_handle client = http_client_create(NULL);
if (!client) return strdup("Failed to create HTTP client.");
char *response = NULL;
r_status_t status = http_get(client, json_object_get_string(url_obj), &response);
http_client_destroy(client);
if (status != R_SUCCESS || !response) {
return strdup("Failed to fetch URL.");
}
return response;
}
static struct json_object *http_fetch_get_description(void) {
struct json_object *root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
struct json_object *function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("http_fetch"));
json_object_object_add(function, "description",
json_object_new_string("Get the contents of a URL."));
struct json_object *parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
struct json_object *properties = json_object_new_object();
struct json_object *url = json_object_new_object();
json_object_object_add(url, "type", json_object_new_string("string"));
json_object_object_add(url, "description",
json_object_new_string("Fetch URL contents."));
json_object_object_add(properties, "url", url);
json_object_object_add(parameters, "properties", properties);
struct json_object *required = json_object_new_array();
json_object_array_add(required, json_object_new_string("url"));
json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters);
r_config_handle cfg = r_config_get_instance();
if (r_config_use_strict(cfg)) {
json_object_object_add(function, "strict", json_object_new_boolean(1));
}
json_object_object_add(root, "function", function);
return root;
}
static void web_search_print_action(const char *name, struct json_object *args) {
(void)name;
if (!args) return;
struct json_object *query;
if (json_object_object_get_ex(args, "query", &query)) {
fprintf(stderr, " -> Searching web: %s\n", json_object_get_string(query));
}
}
static char *do_web_search(const char *query) {
if (!query) return strdup("Query cannot be NULL.");
char *q_encoded = curl_easy_escape(NULL, query, 0);
if (!q_encoded) return strdup("Failed to encode query.");
char url[4096];
snprintf(url, sizeof(url), "https://static.molodetz.nl/search.cgi?query=%s", q_encoded);
curl_free(q_encoded);
http_client_handle client = http_client_create(NULL);
if (!client) return strdup("Failed to create HTTP client.");
char *response = NULL;
r_status_t status = http_get(client, url, &response);
http_client_destroy(client);
if (status != R_SUCCESS || !response) {
return strdup("Failed to search.");
}
return response;
}
static char *web_search_execute(tool_t *self, struct json_object *args) {
(void)self;
struct json_object *query_obj;
if (!json_object_object_get_ex(args, "query", &query_obj)) {
return strdup("Error: missing 'query' argument");
}
return do_web_search(json_object_get_string(query_obj));
}
static struct json_object *web_search_get_description(void) {
struct json_object *root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
struct json_object *function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("web_search"));
json_object_object_add(function, "description",
json_object_new_string("Searches for information based on a query using search engines like google."));
struct json_object *parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
struct json_object *properties = json_object_new_object();
struct json_object *query = json_object_new_object();
json_object_object_add(query, "type", json_object_new_string("string"));
json_object_object_add(query, "description",
json_object_new_string("The url encoded query string to search for information."));
json_object_object_add(properties, "query", query);
json_object_object_add(parameters, "properties", properties);
struct json_object *required = json_object_new_array();
json_object_array_add(required, json_object_new_string("query"));
json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters);
r_config_handle cfg = r_config_get_instance();
if (r_config_use_strict(cfg)) {
json_object_object_add(function, "strict", json_object_new_boolean(1));
}
json_object_object_add(root, "function", function);
return root;
}
static char *web_search_news_execute(tool_t *self, struct json_object *args) {
(void)self;
struct json_object *query_obj;
if (!json_object_object_get_ex(args, "query", &query_obj)) {
return strdup("Error: missing 'query' argument");
}
return do_web_search(json_object_get_string(query_obj));
}
static struct json_object *web_search_news_get_description(void) {
struct json_object *root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
struct json_object *function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("web_search_news"));
json_object_object_add(function, "description",
json_object_new_string("Searches for news articles based on a query."));
struct json_object *parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
struct json_object *properties = json_object_new_object();
struct json_object *query = json_object_new_object();
json_object_object_add(query, "type", json_object_new_string("string"));
json_object_object_add(query, "description",
json_object_new_string("The url encoded query string to search for news articles."));
json_object_object_add(properties, "query", query);
json_object_object_add(parameters, "properties", properties);
struct json_object *required = json_object_new_array();
json_object_array_add(required, json_object_new_string("query"));
json_object_object_add(parameters, "required", required);
json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters);
r_config_handle cfg = r_config_get_instance();
if (r_config_use_strict(cfg)) {
json_object_object_add(function, "strict", json_object_new_boolean(1));
}
json_object_object_add(root, "function", function);
return root;
}