Compare commits

...

3 Commits

Author SHA1 Message Date
d712b25bd0 Fix. 2025-03-22 03:16:14 +01:00
04785fe350 Update. 2025-03-22 03:15:49 +01:00
984e125cfd Added process output. 2025-03-21 09:19:41 +01:00
15 changed files with 853 additions and 17 deletions

View File

@ -2,7 +2,7 @@ all: build build_rpylib run
# Variables for compiler and flags
CC = gcc
CFLAGS = -Ofast -Werror -Wall -lreadline -lncurses -lcurl -lssl -lcrypto -ljson-c -lm
CFLAGS = -Ofast -Werror -Wall -lreadline -lncurses -lcurl -lssl -lcrypto -ljson-c -lm -lsqlite3
# Targets
build:
@ -14,4 +14,4 @@ build_rpylib:
publish rpylib.so
run:
./r
./r --verbose

15
browse.c Normal file
View File

@ -0,0 +1,15 @@
#include "browse.h"
int main() {
char *query = "example";
char *result = get_news(query);
if (result) {
printf("Result: %s\n", result);
free(result); // Free the allocated memory for the result
} else {
printf("Failed to fetch news.\n");
}
return 0;
}

41
browse.h Normal file
View File

@ -0,0 +1,41 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <json-c/json.h>
#include <json-c/json_util.h>
#include "http_curl.h"
char * url_encode(char *s){
return curl_easy_escape(NULL, s, 0);
}
char * web_search_news(char * q){
char * news = malloc(4096);
news[0] = 0;
char * q_encoded = url_encode(q);
sprintf(news, "https://search.molodetz.nl/search?q=%s&format=json&categories=news",q_encoded);
free(q_encoded);
char * ret = curl_get(news);
free(news);
json_object * json_ret = json_tokener_parse(ret);
json_object * json_results = json_object_object_get(json_ret, "results");
json_object * json_result = json_object_array_get_idx(json_results, 0);
if(!json_result){
json_object_put(json_ret);
free(ret);
return web_search_news(q);
}
json_object_put(json_ret);
return ret;
}
char * web_search_engine(char * q){
char * searx = malloc(4096);
searx[0] = 0;
sprintf(searx, "https://searx.molodetz.nl/search?q=%s&format=json", q);
char * ret = curl_get(searx);
free(searx);
return ret;
}

37
cmd.sh Normal file
View File

@ -0,0 +1,37 @@
#!/bin/bash
echo "🔍 Analyzing disk usage... Please wait."
# 1. Show overall disk usage
echo -e "\n📊 Disk Usage Overview:"
df -h
# 2. Show the largest directories in /
echo -e "\nđź“‚ Top 10 Largest Directories in Root (/):"
du -ahx / 2>/dev/null | sort -rh | head -10
# 3. Show the largest directories in /home
echo -e "\n🏠 Top 10 Largest Directories in /home:"
du -ahx /home 2>/dev/null | sort -rh | head -10
# 4. Show the largest files over 1GB
echo -e "\nđź“„ Top 10 Largest Files Over 1GB:"
find / -type f -size +1G -exec ls -lh {} + 2>/dev/null | sort -k5 -rh | head -10
# 5. Check system logs
echo -e "\n📝 Checking Large Log Files in /var/log:"
sudo du -sh /var/log/* 2>/dev/null | sort -rh | head -10
# 6. Find large installed packages (Debian-based)
if command -v dpkg-query &> /dev/null; then
echo -e "\n📦 Top 10 Largest Installed Packages (Debian-based):"
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -rh | head -10
fi
# 7. Find large installed packages (RPM-based)
if command -v dnf &> /dev/null; then
echo -e "\n📦 Top 10 Largest Installed Packages (Fedora-based):"
dnf list installed | awk '{print $2, $1}' | sort -rh | head -10
fi
echo -e "\nâś… Disk Usage Analysis Completed!"

BIN
database.db Normal file

Binary file not shown.

59
db_utils.c Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include "db_utils.h"
void db_initialize() {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("database.db", &db);
if (rc != SQLITE_OK) {
return;
}
const char *sql = "CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);";
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK) {
sqlite3_free(err_msg);
}
sqlite3_close(db);
}
void test_db_set() {
json_object *result = db_set("test_key", "test_value");
if (result) {
printf("db_set: %s\n", json_object_get_string(result));
json_object_put(result);
} else {
printf("db_set failed\n");
}
}
void test_db_get() {
json_object *result = db_get("test_key");
if (result) {
printf("db_get: %s\n", json_object_to_json_string(result));
json_object_put(result);
} else {
printf("db_get failed\n");
}
}
void test_db_query() {
json_object *result = db_query("SELECT * FROM kv_store");
if (result) {
printf("db_query: %s\n", json_object_to_json_string(result));
json_object_put(result);
} else {
printf("db_query failed\n");
}
}
int main() {
db_initialize();
test_db_set();
test_db_get();
test_db_query();
return 0;
}

141
db_utils.h Normal file
View File

@ -0,0 +1,141 @@
#ifndef DB_UTILS_H
#define DB_UTILS_H
#include <sqlite3.h>
#include <json-c/json.h>
#include "utils.h"
const char * db_file = "~/.r.db";
char * db_file_expanded(){
char * expanded = expand_home_directory(db_file);
static char result[4096];
result[0] = 0;
strcpy(result, expanded);
free(expanded);
return result;
}
void db_initialize();
json_object * db_set(const char *key, const char *value);
json_object* db_get(const char *key);
json_object* db_query(const char *query);
void db_initialize() {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open(db_file_expanded(), &db);
if (rc != SQLITE_OK) {
return;
}
const char *sql = "CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);";
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK) {
sqlite3_free(err_msg);
}
sqlite3_close(db);
}
json_object * db_set(const char *key, const char *value) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open(db_file_expanded(), &db);
if (rc != SQLITE_OK) {
return NULL;
}
char *sql = sqlite3_mprintf("INSERT INTO kv_store (key, value) VALUES (%Q, %Q)", key, value);
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
sqlite3_free(sql);
if (rc != SQLITE_OK) {
sqlite3_free(err_msg);
sqlite3_close(db);
return NULL;
}
sqlite3_close(db);
return json_object_new_string("Success");
}
json_object* db_get(const char *key) {
sqlite3 *db;
sqlite3_stmt *stmt;
json_object *result = json_object_new_object();
const char *value = NULL;
int rc = sqlite3_open(db_file_expanded(), &db);
if (rc != SQLITE_OK) {
return NULL;
}
const char *sql = "SELECT value FROM kv_store WHERE key = ?";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
value = (const char *)sqlite3_column_text(stmt, 0);
}
if (value) {
json_object_object_add(result, "value", json_object_new_string(value));
} else {
json_object_object_add(result, "error", json_object_new_string("Key not found"));
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return result;
}
json_object* db_query(const char *query) {
sqlite3 *db;
sqlite3_stmt *stmt;
json_object *result = json_object_new_array();
int rc = sqlite3_open(db_file_expanded(), &db);
if (rc != SQLITE_OK) {
return NULL;
}
sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
while (sqlite3_step(stmt) == SQLITE_ROW) {
json_object *row = json_object_new_object();
for (int i = 0; i < sqlite3_column_count(stmt); i++) {
const char *col_name = sqlite3_column_name(stmt, i);
switch (sqlite3_column_type(stmt, i)) {
case SQLITE_INTEGER:
json_object_object_add(row, col_name, json_object_new_int64(sqlite3_column_int64(stmt, i)));
break;
case SQLITE_FLOAT:
json_object_object_add(row, col_name, json_object_new_double(sqlite3_column_double(stmt, i)));
break;
case SQLITE_TEXT:
json_object_object_add(row, col_name, json_object_new_string((const char *)sqlite3_column_text(stmt, i)));
break;
case SQLITE_BLOB:
json_object_object_add(row, col_name, json_object_new_string_len((const char *)sqlite3_column_blob(stmt, i), sqlite3_column_bytes(stmt, i)));
break;
case SQLITE_NULL:
default:
json_object_object_add(row, col_name, json_object_new_string("NULL"));
break;
}
}
json_object_array_add(result, row);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return result;
}
#endif // DB_UTILS_H

133
indexer.h Normal file
View File

@ -0,0 +1,133 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <json-c/json.h>
#include <limits.h>
#include <unistd.h>
#define MAX_FILES 20000
#define MAX_PATH 4096
static const char *extensions[] = {".c", ".cpp", ".h", ".py", ".java", ".js", ".mk", ".html", "Makefile", ".css", ".json", ".cs", ".csproj", ".sln", ".toml",".rs"};
static size_t ext_count = sizeof(extensions) / sizeof(extensions[0]); // Updated count to reflect the new number of extensions
typedef struct {
char name[MAX_PATH];
char modification_date[20];
char creation_date[20];
char type[10];
size_t size_bytes;
} FileInfo;
FileInfo file_list[MAX_FILES];
size_t file_count = 0;
int is_valid_extension(const char *filename, const char *extensions[], size_t ext_count) {
const char *dot = strrchr(filename, '.');
if(!dot){
dot = filename;
}
for (size_t i = 0; i < ext_count; i++) {
if (strcmp(dot, extensions[i]) == 0) {
return 1;
}
}
return 0;
}
int is_ignored_directory(const char *dir_name) {
const char *ignored_dirs[] = {"env", ".venv", "node_modules", "venv", "virtualenv"};
for (size_t i = 0; i < sizeof(ignored_dirs) / sizeof(ignored_dirs[0]); i++) {
if (strcmp(dir_name, ignored_dirs[i]) == 0) {
return 1;
}
}
return 0;
}
void get_file_info(const char *path) {
struct stat file_stat;
if (stat(path, &file_stat) == 0) {
FileInfo info;
strncpy(info.name, path, MAX_PATH - 1); // Copy with one less to leave space for null terminator
info.name[MAX_PATH - 1] = '\0'; // Ensure null termination
strftime(info.modification_date, sizeof(info.modification_date), "%Y-%m-%d %H:%M:%S", localtime(&file_stat.st_mtime));
strftime(info.creation_date, sizeof(info.creation_date), "%Y-%m-%d %H:%M:%S", localtime(&file_stat.st_ctime));
strncpy(info.type, S_ISDIR(file_stat.st_mode) ? "directory" : "file", 10);
info.type[9] = '\0'; // Ensure null termination
info.size_bytes = file_stat.st_size;
file_list[file_count++] = info;
}
}
char* index_directory(const char *dir_path) {
DIR *dir = opendir(dir_path);
struct dirent *entry;
if (dir == NULL) {
perror("Failed to open directory");
return NULL;
}
json_object *jarray = json_object_new_array();
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
if (entry->d_name[0] == '.' || is_ignored_directory(entry->d_name)) {
continue;
}
char full_path[MAX_PATH];
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, entry->d_name);
if (entry->d_type == DT_DIR) {
char *subdir_json = index_directory(full_path);
if (subdir_json) {
json_object *jsubdir = json_object_new_string(subdir_json);
json_object_array_add(jarray, jsubdir);
free(subdir_json);
}
} else if (is_valid_extension(entry->d_name, extensions, ext_count)) {
get_file_info(full_path);
json_object *jfile = json_object_new_object();
json_object_object_add(jfile, "file_name", json_object_new_string(file_list[file_count - 1].name));
json_object_object_add(jfile, "modification_date", json_object_new_string(file_list[file_count - 1].modification_date));
json_object_object_add(jfile, "creation_date", json_object_new_string(file_list[file_count - 1].creation_date));
json_object_object_add(jfile, "type", json_object_new_string(file_list[file_count - 1].type));
json_object_object_add(jfile, "size_bytes", json_object_new_int64(file_list[file_count - 1].size_bytes));
// Read the file contents
FILE *fp = fopen(file_list[file_count - 1].name, "r");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
long length = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *content = malloc(length + 1);
if (content) {
size_t bytesRead = fread(content, 1, length, fp);
if (bytesRead != length) {
free(content);
content = NULL;
json_object_object_add(jfile, "file_current_content_data", json_object_new_string("Error reading file"));
} else {
content[length] = '\0'; // Null-terminate the string
json_object_object_add(jfile, "file_current_content_data", json_object_new_string(content));
}
free(content);
}
fclose(fp);
} else {
json_object_object_add(jfile, "content", json_object_new_string("Unable to read file"));
}
json_object_array_add(jarray, jfile);
}
}
}
closedir(dir);
char *result = strdup(json_object_to_json_string(jarray));
json_object_put(jarray);
return result;
}

10
main.c
View File

@ -44,6 +44,8 @@
#include <string.h>
#include <unistd.h>
#include "utils.h"
#include "r.h"
#include "db_utils.h"
volatile sig_atomic_t sigint_count = 0;
time_t first_sigint_time = 0;
@ -76,7 +78,11 @@ char *get_prompt_from_args(int c, char **argv) {
if (!strcmp(argv[i],"--stdin")){
fprintf(stderr, "%s\n", "Reading from stdin.");
get_from_std_in = true;
}else if(!strcmp(argv[i],"--py")){
}else if(!strcmp(argv[i],"--verbose")){
is_verbose = true;
}
else if(!strcmp(argv[i],"--py")){
if(i+1 <= c){
char * py_file_path = expand_home_directory(argv[i+1]);
fprintf(stderr, "Including \"%s\".\n", py_file_path);
@ -334,7 +340,7 @@ void handle_sigint(int sig) {
int main(int argc, char *argv[]) {
signal(SIGINT, handle_sigint);
db_initialize();
init();
if (try_prompt(argc, argv))
return 0;

View File

@ -54,14 +54,23 @@ struct json_object* openai_process_chat_message(const char* api_url, const char*
}
struct json_object *error_object;
if (json_object_object_get_ex(parsed_json, "error", &error_object)) {
json_object_put(parsed_json);
char *all_messages = (char *)json_object_to_json_string(message_array);
fprintf(stderr, "Messages: ");
fwrite(all_messages, strlen(all_messages), 1, stderr);
fprintf(stderr, "\n");
free(all_messages);
fprintf(stderr,"%s\n",json_object_to_json_string(parsed_json));
json_object_put(parsed_json);
messages_remove_last();
messages_remove_last();
return NULL;
}

8
r.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef R_H
#define R_H
#include <stdbool.h>
bool is_verbose = false;
#endif

BIN
rpylib.so Executable file

Binary file not shown.

9
script.sh Normal file
View File

@ -0,0 +1,9 @@
#!/bin/bash
echo "This is a sample shell script."
echo "Running some commands..."
# Add your commands below
# Example command
ls -la

10
setup.py Normal file
View File

@ -0,0 +1,10 @@
from setuptools import setup, Extension
module = Extension("rpylib", sources=["rpylib.c"])
setup(
name="rpylib",
version="1.0",
description="AI Module",
ext_modules=[module],
)

392
tools.h
View File

@ -26,6 +26,9 @@
#include <string.h>
#include "indexer.h"
#include "db_utils.h"
#include "r.h"
#include "browse.h"
struct json_object* tool_description_http_get();
struct json_object* tool_description_linux_terminal();
@ -35,6 +38,12 @@ struct json_object* tool_description_write_file();
struct json_object* tool_description_directory_rglob();
struct json_object* tool_description_linux_terminal_interactive();
struct json_object* tool_description_index_source_directory();
struct json_object* tool_description_chdir();
struct json_object* tool_description_getpwd();
struct json_object* tool_description_db_set();
struct json_object* tool_description_db_query();
struct json_object* tool_description_db_get();
struct json_object* tool_description_web_search_news();
struct json_object* tools_descriptions() {
struct json_object* root = json_object_new_array();
@ -46,16 +55,228 @@ struct json_object* tools_descriptions() {
json_object_array_add(root, tool_description_directory_rglob());
json_object_array_add(root, tool_description_linux_terminal_interactive());
json_object_array_add(root,tool_description_index_source_directory());
json_object_array_add(root,tool_description_chdir());
json_object_array_add(root,tool_description_getpwd());
json_object_array_add(root,tool_description_db_set());
json_object_array_add(root, tool_description_db_query());
json_object_array_add(root, tool_description_db_get());
json_object_array_add(root, tool_description_web_search_news());
return root;
}
char* tool_function_web_search_news(char* query) {
if (query == NULL) {
return strdup("Query cannot be NULL.");
}
char* result = web_search_news(query);
if (result == NULL) {
return strdup("Failed to fetch news.");
}
return result;
}
struct json_object* tool_description_web_search_news() {
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);
json_object_object_add(function, "strict", json_object_new_boolean(1));
json_object_object_add(root, "function", function);
return root;
}
char* tool_function_db_get(char* key) {
json_object* result = db_get(key);
if (result == NULL) {
return strdup("Failed to retrieve value from the database.");
}
char* response = strdup(json_object_to_json_string(result));
json_object_put(result); // Free the json_object
return response;
}
struct json_object* tool_description_db_get() {
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("db_get"));
json_object_object_add(function, "description", json_object_new_string("Retrieves a value from the database for a given key."));
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* key = json_object_new_object();
json_object_object_add(key, "type", json_object_new_string("string"));
json_object_object_add(key, "description", json_object_new_string("The key to retrieve from the database."));
json_object_object_add(properties, "key", key);
json_object_object_add(parameters, "properties", properties);
struct json_object* required = json_object_new_array();
json_object_array_add(required, json_object_new_string("key"));
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);
json_object_object_add(function, "strict", json_object_new_boolean(1));
json_object_object_add(root, "function", function);
return root;
}
char* tool_function_db_query(char* query) {
json_object* result = db_query(query);
if (result == NULL) {
return strdup("Failed to execute query on the database.");
}
char* response = strdup(json_object_to_json_string(result));
json_object_put(result); // Free the json_object
return response;
}
struct json_object* tool_description_db_query() {
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("db_query"));
json_object_object_add(function, "description", json_object_new_string("Executes a query on the database and returns the results."));
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 SQL query to execute."));
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);
json_object_object_add(function, "strict", json_object_new_boolean(1));
json_object_object_add(root, "function", function);
return root;
}
char* tool_function_db_set(char* key, char* value) {
json_object* result = db_set(key, value);
if (result == NULL) {
return strdup("Failed to set value in the database.");
}
const char* response = json_object_get_string(result);
json_object_put(result); // Free the json_object
return strdup(response);
}
struct json_object* tool_description_db_set() {
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("db_set"));
json_object_object_add(function, "description", json_object_new_string("Sets a value in the database for a given key."));
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* key = json_object_new_object();
json_object_object_add(key, "type", json_object_new_string("string"));
json_object_object_add(key, "description", json_object_new_string("The key to set in the database."));
json_object_object_add(properties, "key", key);
struct json_object* value = json_object_new_object();
json_object_object_add(value, "type", json_object_new_string("string"));
json_object_object_add(value, "description", json_object_new_string("The value to set for the given key."));
json_object_object_add(properties, "value", value);
json_object_object_add(parameters, "properties", properties);
struct json_object* required = json_object_new_array();
json_object_array_add(required, json_object_new_string("key"));
json_object_array_add(required, json_object_new_string("value"));
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);
json_object_object_add(function, "strict", json_object_new_boolean(1));
json_object_object_add(root, "function", function);
return root;
}
char* tool_function_http_get(char* url) {
fprintf(stderr, "Tool http_get: %s\n", url);
return curl_get(url);
}
char* tool_function_chdir(char* path) {
if (chdir(path) != 0) {
perror("chdir failed");
return strdup("Failed to change directory!");
}
return strdup("Directory successfully changed.");
}
char* tool_function_linux_terminal(char* command) {
fprintf(stderr, "Tool linux_terminal: %s\n", command);
FILE* fp;
char buffer[1024];
size_t total_size = 0;
@ -77,6 +298,8 @@ char* tool_function_linux_terminal(char* command) {
return strdup("Failed to allocate memory!");
}
output = new_output;
if(is_verbose)
fprintf(stderr, "%s", buffer);
strcpy(output + total_size, buffer);
total_size += chunk_size;
}
@ -86,8 +309,6 @@ char* tool_function_linux_terminal(char* command) {
}
char* tool_function_linux_terminal_interactive(char* command) {
fprintf(stderr, "Tool linux_terminal_interactive: %s\n", command);
int result_code = system(command);
char* result = malloc(100);
@ -97,6 +318,77 @@ char* tool_function_linux_terminal_interactive(char* command) {
return result;
}
char* tool_function_getpwd() {
char* cwd = (char*)malloc(PATH_MAX);
if (cwd == NULL) {
perror("Memory allocation failed");
return strdup("Failed to allocate memory for current working directory!");
}
if (getcwd(cwd, PATH_MAX) == NULL) {
free(cwd);
perror("getcwd failed");
return strdup("Failed to get current working directory!");
}
return cwd;
}
struct json_object* tool_description_getpwd() {
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("getpwd"));
json_object_object_add(function, "description", json_object_new_string("Returns the current working directory as a string."));
struct json_object* parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
json_object_object_add(function, "parameters", json_object_new_null());
json_object_object_add(root, "function", function);
return root;
}
struct json_object* tool_description_chdir() {
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("chdir"));
json_object_object_add(function, "description", json_object_new_string("Changes the current working directory to the specified path. Call this function when `cd` is prompted."));
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* path = json_object_new_object();
json_object_object_add(path, "type", json_object_new_string("string"));
json_object_object_add(path, "description", json_object_new_string("Path to change the current working directory to."));
json_object_object_add(properties, "path", path);
json_object_object_add(parameters, "properties", properties);
struct json_object* required = json_object_new_array();
json_object_array_add(required, json_object_new_string("path"));
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);
json_object_object_add(function, "strict", json_object_new_boolean(1));
json_object_object_add(root, "function", function);
return root;
}
struct json_object* tool_description_index_source_directory() {
struct json_object* root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
@ -272,14 +564,12 @@ struct json_object* tool_description_write_file() {
}
char* tool_function_index_source_directory(char * path){
fprintf(stderr, "Tool index_source_directory: %s\n", path);
return index_directory(path);
}
char* tool_function_read_file(char* path) {
char * expanded_path = expand_home_directory(path);
fprintf(stderr, "Tools read_file: %s\n", expanded_path);
FILE* fp = fopen(expanded_path, "r");
free(expanded_path);
@ -307,7 +597,6 @@ char* tool_function_read_file(char* path) {
}
char* tool_function_write_file(char* path, char* content) {
fprintf(stderr, "Tools write_file with %zu bytes: %s\n", strlen(content), path);
FILE* fp = fopen(path, "w");
if (fp == NULL) {
perror("fopen failed");
@ -417,7 +706,6 @@ void recursive_glob(const char* pattern, glob_t* results) {
}
char* tool_function_directory_rglob(char* target_dir) {
fprintf(stderr, "Tools directory_rglob: %s\n", target_dir);
glob_t results;
results.gl_pathc = 0;
struct stat file_stat;
@ -457,7 +745,6 @@ char* tool_function_directory_rglob(char* target_dir) {
}
char* tool_function_directory_glob(char* target_dir) {
fprintf(stderr, "Tools directory_glob: %s\n", target_dir);
glob_t results;
struct stat file_stat;
char mod_time[20], create_time[20];
@ -628,6 +915,14 @@ struct json_object* tools_execute(struct json_object* tools_array) {
char* function_name = NULL;
if (json_object_object_get_ex(function_obj, "name", &name_obj)) {
function_name = (char*)json_object_get_string(name_obj);
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));
if(is_verbose)
fprintf(stderr, "Executing function %s with arguments %s\n", function_name, json_object_to_json_string(arguments));
}
}
if (!strcmp(function_name, "linux_terminal_execute")) {
@ -654,7 +949,19 @@ struct json_object* tools_execute(struct json_object* tools_array) {
free(terminal_result);
}
}
} else if (!strcmp(function_name, "directory_glob")) {
}else if (!strcmp(function_name, "chdir")) {
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* path_obj;
if (json_object_object_get_ex(arguments, "path", &path_obj)) {
char* path = (char*)json_object_get_string(path_obj);
char* chdir_result = tool_function_chdir(path);
json_object_object_add(tool_result, "content", json_object_new_string(chdir_result));
free(chdir_result);
}
}
} else if (!strcmp(function_name, "directory_glob")) {
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));
@ -708,7 +1015,38 @@ struct json_object* tools_execute(struct json_object* tools_array) {
free(write_result);
}
}
} else if (!strcmp(function_name, "index_source_directory")) {
}else if (!strcmp(function_name, "db_query")) {
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* query_obj;
if (json_object_object_get_ex(arguments, "query", &query_obj)) {
char* query = (char*)json_object_get_string(query_obj);
char * db_query_result = tool_function_db_query(query);
json_object_object_add(tool_result, "content", json_object_new_string( db_query_result));;
if(db_query_result != NULL)
free(db_query_result);
}
}
} else if (!strcmp(function_name, "db_set")) {
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* key_obj;
struct json_object* value_obj;
if (json_object_object_get_ex(arguments, "key", &key_obj) &&
json_object_object_get_ex(arguments, "value", &value_obj)) {
char* key = (char*)json_object_get_string(key_obj);
char* value = (char*)json_object_get_string(value_obj);
char* db_set_result = tool_function_db_set(key, value);
json_object_object_add(tool_result, "content", json_object_new_string(db_set_result));
free(db_set_result);
}
}
}else if (!strcmp(function_name, "index_source_directory")) {
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));
@ -720,7 +1058,37 @@ struct json_object* tools_execute(struct json_object* tools_array) {
free(listing_result);
}
}
} else if (!strcmp(function_name, "directory_rglob")) {
}else if (!strcmp(function_name, "web_search_news")){
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* query_obj;
if (json_object_object_get_ex(arguments, "query", &query_obj)) {
char* query = (char*)json_object_get_string(query_obj);
char* news_result = tool_function_web_search_news(query);
json_object_object_add(tool_result, "content", json_object_new_string(news_result));
free(news_result);
}
}
}else if (!strcmp(function_name, "db_get")) {
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* key_obj;
if (json_object_object_get_ex(arguments, "key", &key_obj)) {
char* key = (char*)json_object_get_string(key_obj);
char* db_get_result = tool_function_db_get(key);
json_object_object_add(tool_result, "content", json_object_new_string(db_get_result));
free(db_get_result);
}
}
}else if (!strcmp(function_name, "getpwd")) {
char* pwd_result = tool_function_getpwd();
json_object_object_add(tool_result, "content", json_object_new_string(pwd_result));
free(pwd_result);
}
else if (!strcmp(function_name, "directory_rglob")) {
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));