Project update.

This commit is contained in:
retoor 2025-03-16 07:36:13 +01:00
parent ab254abb4b
commit 48a543b56e
9 changed files with 262 additions and 227 deletions

4
auth.h
View File

@ -1,8 +1,8 @@
// Written by retoor@molodetz.nl // Written by retoor@molodetz.nl
// This source code declares a constant character pointer variable that retrieves an API key from environment variables or falls back to a hardcoded key if not found. // This source code retrieves an API key from environment variables or defaults to a hardcoded key if none are found.
// Uses standard library functions from stdlib.h and stdio.h to manage environment variables and output error messages. // Uses the C standard library functions from stdlib.h for environment management and stdio.h for error handling.
// MIT License // MIT License

12
chat.h
View File

@ -35,14 +35,14 @@
#include "messages.h" #include "messages.h"
#ifdef FREE_VERSION #ifdef FREE_VERSION
char *prompt_model = "gpt-3.5-turbo"; static char *prompt_model = "gpt-3.5-turbo";
#else #else
char *prompt_model = "gpt-4o-mini"; static char *prompt_model = "gpt-4o-mini";
#endif #endif
int prompt_max_tokens = 2048; static int prompt_max_tokens = 2048;
double prompt_temperature = 0.1; static double prompt_temperature = 0.1;
json_object *_prompt = NULL; static json_object *_prompt = NULL;
void chat_free() { void chat_free() {
if (_prompt == NULL) return; if (_prompt == NULL) return;
@ -52,7 +52,7 @@ void chat_free() {
char *chat_json(const char *role, const char *message) { char *chat_json(const char *role, const char *message) {
chat_free(); chat_free();
struct json_object *root_object = json_object_new_object(); json_object *root_object = json_object_new_object();
json_object_object_add(root_object, "model", json_object_new_string(prompt_model)); json_object_object_add(root_object, "model", json_object_new_string(prompt_model));
if (role != NULL && message != NULL) { if (role != NULL && message != NULL) {

View File

@ -1,6 +1,6 @@
// Written by retoor@molodetz.nl // Written by retoor@molodetz.nl
// This code defines a simple HTTP client using libcurl in C, providing functions `curl_post` and `curl_get`. These functions enable POST and GET requests with JSON data and include authorization via a bearer token. // This code defines a simple HTTP client using libcurl in C. It provides functions for executing POST and GET HTTP requests with JSON data, including authorization via a bearer token. The functions `curl_post` and `curl_get` handle these operations and return the server's response as a string.
// Uses libcurl for HTTP requests and includes a custom "auth.h" for API key resolution. // Uses libcurl for HTTP requests and includes a custom "auth.h" for API key resolution.
@ -20,6 +20,7 @@
// from, out of or in connection with the software or the use or other dealings in // from, out of or in connection with the software or the use or other dealings in
// the Software. // the Software.
#ifndef HTTP_CURL #ifndef HTTP_CURL
#define HTTP_CURL #define HTTP_CURL
@ -52,18 +53,18 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *use
char *curl_post(const char *url, const char *data) { char *curl_post(const char *url, const char *data) {
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
struct ResponseBuffer response; struct ResponseBuffer response = {malloc(1), 0};
response.data = malloc(1);
response.size = 0; if (!response.data) return NULL;
curl = curl_easy_init(); curl = curl_easy_init();
if (curl) { if (curl) {
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
char *bearer_header = malloc(1337); char bearer_header[1337];
sprintf(bearer_header, "Authorization: Bearer %s", resolve_api_key()); sprintf(bearer_header, "Authorization: Bearer %s", resolve_api_key());
headers = curl_slist_append(headers, bearer_header); headers = curl_slist_append(headers, bearer_header);
free(bearer_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
@ -91,25 +92,19 @@ char *curl_get(const char *url) {
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
char bearer_header[1337];
char *bearer_header = malloc(1337);
sprintf(bearer_header, "Authorization: Bearer %s", resolve_api_key()); sprintf(bearer_header, "Authorization: Bearer %s", resolve_api_key());
headers = curl_slist_append(headers, bearer_header); headers = curl_slist_append(headers, bearer_header);
free(bearer_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
if (res != CURLE_OK) { if (res != CURLE_OK) {
fprintf(stderr, "An error occurred: %s\n", curl_easy_strerror(res)); fprintf(stderr, "An error occurred: %s\n", curl_easy_strerror(res));
} }
curl_slist_free_all(headers); curl_slist_free_all(headers);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
return response.data; return response.data;
} }

4
line.h
View File

@ -1,6 +1,6 @@
// Written by retoor@molodetz.nl // Written by retoor@molodetz.nl
// This source code provides command-line input functionalities with autocomplete and history features using readline library functionalities. It allows users to complete commands and manage input history. // This source code provides command-line input functionalities with autocomplete and history features using the readline library. It allows users to complete commands and manage input history.
// External includes: // External includes:
// - <readline/readline.h> // - <readline/readline.h>
@ -52,9 +52,7 @@ void line_init() {
char* line_read(char* prefix) { char* line_read(char* prefix) {
char* data = readline(prefix); char* data = readline(prefix);
if (!(data && *data)) { if (!(data && *data)) {
if (data) {
free(data); free(data);
}
return NULL; return NULL;
} }
return data; return data;

View File

@ -25,7 +25,6 @@ struct json_object *message_list() {
return message_array; return message_array;
} }
void messages_remove_last() { void messages_remove_last() {
struct json_object *messages = message_list(); struct json_object *messages = message_list();
int size = json_object_array_length(messages); int size = json_object_array_length(messages);
@ -40,7 +39,7 @@ struct json_object *message_add_tool_call(struct json_object *message) {
return message; return message;
} }
struct json_object *message_add_tool_result(char *tool_call_id, char *tool_result) { struct json_object *message_add_tool_result(const char *tool_call_id, const char *tool_result) {
struct json_object *messages = message_list(); struct json_object *messages = message_list();
struct json_object *message = json_object_new_object(); struct json_object *message = json_object_new_object();

View File

@ -4,7 +4,6 @@
// Uncommon imports include "http.h", "chat.h", and "http_curl.h". These may be internal or external libraries providing HTTP and JSON communication capabilities required to interact with APIs. // Uncommon imports include "http.h", "chat.h", and "http_curl.h". These may be internal or external libraries providing HTTP and JSON communication capabilities required to interact with APIs.
// MIT License // MIT License
// //
// Copyright (c) 2023 // Copyright (c) 2023

17
tools.h
View File

@ -58,7 +58,7 @@ char *tool_function_linux_terminal(char *command) {
while (fgets(buffer, sizeof(buffer), fp) != NULL) { while (fgets(buffer, sizeof(buffer), fp) != NULL) {
size_t chunk_size = strlen(buffer); size_t chunk_size = strlen(buffer);
char *new_output = (char *)realloc(output, total_size + chunk_size + 1); char* new_output = realloc(output, total_size + chunk_size + 1);
if (new_output == NULL) { if (new_output == NULL) {
perror("realloc failed"); perror("realloc failed");
free(output); free(output);
@ -79,7 +79,7 @@ char *tool_function_linux_terminal_interactive(char *command) {
int result_code = system(command); int result_code = system(command);
char * result = (char *)malloc(100); char* result = malloc(100);
result[0] = 0; result[0] = 0;
sprintf(result, "Command exited with status code %d.", result_code); sprintf(result, "Command exited with status code %d.", result_code);
@ -100,7 +100,7 @@ struct json_object *tool_description_linux_terminal_interactive() {
struct json_object* properties = json_object_new_object(); struct json_object* properties = json_object_new_object();
struct json_object* path = json_object_new_object(); struct json_object* path = json_object_new_object();
json_object_object_add(path, "type", json_object_new_string("string")); json_object_object_add(path, "type", json_object_new_string("string"));
json_object_object_add(path, "description", json_object_new_string("Executable with parameters to execute interactive."));; json_object_object_add(path, "description", json_object_new_string("Executable with parameters to execute interactively."));
json_object_object_add(properties, "command", path); json_object_object_add(properties, "command", path);
json_object_object_add(parameters, "properties", properties); json_object_object_add(parameters, "properties", properties);
@ -119,9 +119,6 @@ struct json_object *tool_description_linux_terminal_interactive() {
return root; return root;
} }
struct json_object* tool_description_directory_rglob() { struct json_object* tool_description_directory_rglob() {
struct json_object* root = json_object_new_object(); struct json_object* root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function")); json_object_object_add(root, "type", json_object_new_string("function"));
@ -129,7 +126,7 @@ struct json_object *tool_description_directory_rglob() {
struct json_object* function = json_object_new_object(); struct json_object* function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("directory_rglob")); json_object_object_add(function, "name", json_object_new_string("directory_rglob"));
json_object_object_add(function, "description", json_object_new_string("Recursively list the contents of a specified directory in glob format. " json_object_object_add(function, "description", json_object_new_string("Recursively list the contents of a specified directory in glob format. "
"Result is a json array containing objects with keys: name, modification_date(iso), creation_date(iso), type and size_bytes.")); "Result is a JSON array containing objects with keys: name, modification_date(iso), creation_date(iso), type, and size_bytes."));
struct json_object* parameters = json_object_new_object(); struct json_object* parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object")); json_object_object_add(parameters, "type", json_object_new_string("object"));
@ -241,7 +238,7 @@ char *tool_function_read_file(char *path) {
long size = ftell(fp); long size = ftell(fp);
rewind(fp); rewind(fp);
char *content = (char *)malloc(size + 1); char* content = malloc(size + 1);
if (content == NULL) { if (content == NULL) {
fclose(fp); fclose(fp);
return strdup("Memory allocation failed!"); return strdup("Memory allocation failed!");
@ -491,7 +488,7 @@ struct json_object *tool_description_directory_glob() {
struct json_object* function = json_object_new_object(); struct json_object* function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("directory_glob")); json_object_object_add(function, "name", json_object_new_string("directory_glob"));
json_object_object_add(function, "description", json_object_new_string("List the contents of a specified directory in glob format. " json_object_object_add(function, "description", json_object_new_string("List the contents of a specified directory in glob format. "
"Result is a json array containing objects with keys: name, modification_date(iso), creation_date(iso), type and size_bytes.")); "Result is a JSON array containing objects with keys: name, modification_date(iso), creation_date(iso), type, and size_bytes."));
struct json_object* parameters = json_object_new_object(); struct json_object* parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object")); json_object_object_add(parameters, "type", json_object_new_string("object"));
@ -670,7 +667,7 @@ struct json_object *tools_execute(struct json_object *tools_array) {
} }
} }
} else { } else {
fprintf(stderr, "Unkown function: %s\n", function_name); fprintf(stderr, "Unknown function: %s\n", function_name);
json_object_object_add(tool_result, "content", json_object_new_string("Error: function not found.")); json_object_object_add(tool_result, "content", json_object_new_string("Error: function not found."));
} }

47
utils.h Normal file
View File

@ -0,0 +1,47 @@
// Written by retoor@molodetz.nl
// This header file contains utility functions for manipulating file system paths, focusing primarily on expanding paths starting with '~' to the home directory.
// This code uses standard libraries: stdio.h, stdlib.h, and string.h, and conditionally includes the posix libraries pwd.h and unistd.h when expanding the home directory manually.
// MIT License
//
// Permission is granted to use, copy, modify, merge, distribute, sublicense, and/or sell copies of the Software.
// The license includes conditions about providing a copy of the license and the limitation of liability and warranty.
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* expand_home_directory(const char* path) {
if (path == NULL) return NULL;
if (path[0] == '~' && path[1] == '/') {
const char* home_dir = getenv("HOME");
if (home_dir == NULL) {
#include <pwd.h>
#include <unistd.h>
struct passwd* pw = getpwuid(getuid());
if (pw == NULL) return NULL;
home_dir = pw->pw_dir;
}
size_t home_len = strlen(home_dir);
size_t path_len = strlen(path) - 1;
char* expanded_path = malloc(home_len + path_len + 1);
if (expanded_path == NULL) return NULL;
strcpy(expanded_path, home_dir);
strcat(expanded_path, path + 1);
return expanded_path;
} else {
return strdup(path);
}
}
#endif