2025-03-03 08:32:41 +01:00
// Written by retoor@molodetz.nl
// This code defines functions for HTTP requests and structuring tool descriptions and executions in JSON format using the JSON-C library. It uses cURL for HTTP requests.
// Imports not part of the language itself: JSON-C (json-c/json.h, json-c/json_object.h) and a custom header "http_curl.h"
// MIT License
2025-03-03 08:07:17 +01:00
# ifndef R_TOOLS_H
# define R_TOOLS_H
# include <json-c/json.h>
# include <json-c/json_object.h>
# include <string.h>
# include "http_curl.h"
2025-03-03 08:32:41 +01:00
struct json_object * tool_description_http_get ( ) ;
2025-03-03 08:07:17 +01:00
2025-03-03 08:32:41 +01:00
struct json_object * tools_descriptions ( ) {
2025-03-03 08:07:17 +01:00
struct json_object * root = json_object_new_array ( ) ;
json_object_array_add ( root , tool_description_http_get ( ) ) ;
return root ;
}
2025-03-03 08:32:41 +01:00
char * tool_function_http_get ( char * url ) {
2025-03-03 08:07:17 +01:00
return curl_get ( url ) ;
}
2025-03-03 08:32:41 +01:00
struct json_object * tool_description_http_get ( ) {
2025-03-03 08:07:17 +01:00
struct json_object * root = json_object_new_object ( ) ;
json_object_object_add ( root , " type " , json_object_new_string ( " function " ) ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
struct json_object * function = json_object_new_object ( ) ;
json_object_object_add ( function , " name " , json_object_new_string ( " http_fetch " ) ) ;
2025-03-03 08:32:41 +01:00
json_object_object_add ( function , " description " , json_object_new_string ( " Get the contents of a URL. " ) ) ;
2025-03-03 08:07:17 +01:00
struct json_object * parameters = json_object_new_object ( ) ;
json_object_object_add ( parameters , " type " , json_object_new_string ( " object " ) ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
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 " ) ) ;
2025-03-03 08:32:41 +01:00
json_object_object_add ( url , " description " , json_object_new_string ( " Fetch URL contents. " ) ) ;
2025-03-03 08:07:17 +01:00
json_object_object_add ( properties , " url " , url ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
json_object_object_add ( parameters , " properties " , properties ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
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 ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
json_object_object_add ( parameters , " additionalProperties " , json_object_new_boolean ( 0 ) ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
json_object_object_add ( function , " parameters " , parameters ) ;
json_object_object_add ( function , " strict " , json_object_new_boolean ( 1 ) ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
json_object_object_add ( root , " function " , function ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
return root ;
}
2025-03-03 08:32:41 +01:00
struct json_object * tools_execute ( struct json_object * tools_array ) {
struct json_object * tools_result_messages = json_object_new_array ( ) ;
2025-03-03 08:07:17 +01:00
int array_len = json_object_array_length ( tools_array ) ;
for ( int i = 0 ; i < array_len ; i + + ) {
struct json_object * obj = json_object_array_get_idx ( tools_array , i ) ;
2025-03-03 08:32:41 +01:00
struct json_object * tool_result = json_object_new_object ( ) ;
2025-03-03 08:07:17 +01:00
2025-03-03 08:32:41 +01:00
json_object_object_add ( tool_result , " tool_call_id " ,
json_object_new_string ( json_object_get_string (
json_object_object_get ( obj , " id " ) ) ) ) ;
2025-03-03 08:07:17 +01:00
json_object_object_add ( tool_result , " role " , json_object_new_string ( " tool " ) ) ;
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
struct json_object * type_obj ;
if ( json_object_object_get_ex ( obj , " type " , & type_obj ) ) {
2025-03-03 08:32:41 +01:00
if ( strcmp ( json_object_get_string ( type_obj ) , " function " ) ) {
continue ;
}
2025-03-03 08:07:17 +01:00
}
2025-03-03 08:32:41 +01:00
2025-03-03 08:07:17 +01:00
struct json_object * function_obj ;
if ( json_object_object_get_ex ( obj , " function " , & function_obj ) ) {
struct json_object * name_obj ;
2025-03-03 08:32:41 +01:00
char * function_name = NULL ;
2025-03-03 08:07:17 +01:00
if ( json_object_object_get_ex ( function_obj , " name " , & name_obj ) ) {
function_name = ( char * ) json_object_get_string ( name_obj ) ;
}
2025-03-03 08:32:41 +01:00
if ( ! strcmp ( function_name , " http_fetch " ) ) {
struct json_object * arguments_obj ;
if ( json_object_object_get_ex ( function_obj , " arguments " , & arguments_obj ) ) {
struct json_object * arguments = json_tokener_parse ( json_object_get_string ( arguments_obj ) ) ;
struct json_object * url_obj ;
if ( json_object_object_get_ex ( arguments , " url " , & url_obj ) ) {
char * url = ( char * ) json_object_get_string ( url_obj ) ;
char * http_result = curl_get ( url ) ;
json_object_object_add ( tool_result , " content " , json_object_new_string ( http_result ) ) ;
}
2025-03-03 08:07:17 +01:00
}
}
json_object_array_add ( tools_result_messages , tool_result ) ;
}
}
return tools_result_messages ;
}
2025-03-03 08:32:41 +01:00
# endif