Formatting.

This commit is contained in:
retoor 2025-05-05 14:33:08 +02:00
parent 44c7063033
commit 255e5458be
12 changed files with 618 additions and 471 deletions

117
browse.h
View File

@ -12,15 +12,63 @@ char *web_search_news(char *q) {
const int MAX_RETRIES = 3;
for (int retry = 0; retry < MAX_RETRIES; retry++) {
char *news = malloc(4096);
if (!news) return NULL;
if (!news)
return NULL;
news[0] = 0;
char *q_encoded = url_encode(q);
snprintf(news, 4096, "https://search.molodetz.nl/search?q=%s&format=json&categories=news&preferences=eJx1WMuy4zYO_Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQcPQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3Asf0LZ3tOtJ09g4DIFCibGfG_6UU0K52CuOvFGbSo8vis0TR9yADi3Kv8LaDlOX9QhpX5IM9v2hcvHDVWt9NVBs0xN78Q4BOI7bTg5QjAyyCzUKztgMU7M6rXCegJxQDMknAwbgi9Pk7SYDMamEvIA4IQMjEOiJ5vhu2GT519NZfSFbzS3MLmS1BQf-BCWHHGet3nZq4uX8KdY4-gcnccyx6tdXYcLdBBJzdydyDps0qOS4usplAThR6J6eKkBjMHAGSy6v_rwH9CMqXW6Y58IJPeDXwPVQIFw5qtvjQQY6CP32lWXAgSS3fycyAZQptxP1fNXJqlmyKx4XKCWOHgilU7muIEjNCO2UkRhUF5R8bEcOn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IoX1Zm6OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4FqVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV", q_encoded);
snprintf(
news, 4096,
"https://search.molodetz.nl/"
"search?q=%s&format=json&categories=news&preferences=eJx1WMuy4zYO_"
"Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQc"
"PQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-"
"faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-"
"eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-"
"jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_"
"mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__"
"MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3As"
"f0LZ3tOtJ09g4DIFCibGfG_"
"6UU0K52CuOvFGbSo8vis0TR9y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mHj2"
"qTKA7a2dYrmhlfx3LBTRDf8O_Q0bOcth49stoplzwDn5mYnwZWKaq5dV-"
"QI4Ucy7gDpRzLRzK44i79-freYty1bY6M7b_Nc4LH0hkghBeWj5SZc-o9D0z-"
"q0DrFI6ch_wOz3-JBxYF5-jsrzN49HL6WxD9C6-hrqArAPZkP6-"
"N5JIJ9npDqUjw1Sf5FSBXZ7zLuepxRasvwWh4TD2CJZ7p9sJuM8PexaOKXhHS7TYrymoFY"
"1tAkhFJky_L9ulzouNeEMt10qojIRMxNkUvCyAi0J_-7c5d3Ptr5npuFbLldY7hfo1_z_"
"HhCHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6"
"OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_"
"y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-"
"_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-"
"gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_"
"3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_"
"cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_"
"hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-"
"k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4F"
"qVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV",
q_encoded);
free(q_encoded);
char *ret = curl_get(news);
free(news);
if (!ret) continue;
if (!ret)
continue;
json_object *json_ret = json_tokener_parse(ret);
if (!json_ret) {
@ -44,15 +92,63 @@ char *web_search(char *q) {
const int MAX_RETRIES = 3;
for (int retry = 0; retry < MAX_RETRIES; retry++) {
char *news = malloc(4096);
if (!news) return NULL;
if (!news)
return NULL;
news[0] = 0;
char *q_encoded = url_encode(q);
snprintf(news, 4096, "https://search.molodetz.nl/search?q=%s&format=json&preferences=eJx1WMuy4zYO_Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQcPQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3Asf0LZ3tOtJ09g4DIFCibGfG_6UU0K52CuOvFGbSo8vis0TR9yADi3Kv8LaDlOX9QhpX5IM9v2hcvHDVWt9NVBs0xN78Q4BOI7bTg5QjAyyCzUKztgMU7M6rXCegJxQDMknAwbgi9Pk7SYDMamEvIA4IQMjEOiJ5vhu2GT519NZfSFbzS3MLmS1BQf-BCWHHGet3nZq4uX8KdY4-gcnccyx6tdXYcLdBBJzdydyDps0qOS4usplAThR6J6eKkBjMHAGSy6v_rwH9CMqXW6Y58IJPeDXwPVQIFw5qtvjQQY6CP32lWXAgSS3fycyAZQptxP1fNXJqlmyKx4XKCWOHgilU7muIEjNCO2UkRhUF5R8bEcOnPEQVsevALzeycNK4lWKZfmOznSHYej6HArF6FVxPnr-EynH72N7VnHEAlPJXOj5B-NAxzFcHEGFuRG-jVhc31IXE1zDbjfTp9KT8nudTbtT_azUsY3MHKGsGprAb4ccpwrICoFqKMBEhQkrW2Dc65TLb2XBcwE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tXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4FqVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV", q_encoded);
snprintf(
news, 4096,
"https://search.molodetz.nl/"
"search?q=%s&format=json&preferences=eJx1WMuy4zYO_"
"Zp4o4ormaRqahZepZL5gexVFAlLiEhCzYdt3a8PoIdFWt2Ldl8fkCCIxwForRL0FBDirQc"
"PQdmLVb7PqoebyokulrSycPP2Il81uclCgtvlrh6oybcBItkHhNsFHW9qp0Cv-"
"faXshEuDtJA5vb_P_--RHWHCCro4fbLJQ3g4BZRdF1YQbYptqzLw7NNqtt2G8JDOyn-"
"eqXQX9ZtbUyz3UzU4BOEVlnsveO_t_3KPJTXYNrt3BX9liHMLfo2YWIFK4j-"
"jh4Ta9WBrN3QdZ8YplcvzazKgk63v0OGy0BphDneDNwV3-CSg23vFJxKCX1_"
"mwKkNF8MRtVZtgJ8j57d_L9e9W0bSaOyjQOD6qf__"
"MG366CJ7OCxbRdHxhptHmiA2nb571OmskGWuRxRi8hOfFJj0edXMyk9ijpWm0TmvYqN3As"
"f0LZ3tOtJ09g4DIFCibGfG_"
"6UU0K52CuOvFGbSo8vis0TR9yADi3Kv8LaDlOX9QhpX5IM9v2hcvHDVWt9NVBs0xN78Q4B"
"OI7bTg5QjAyyCzUKztgMU7M6rXCegJxQDMknAwbgi9Pk7SYDMamEvIA4IQMjEOiJ5vhu2G"
"T519NZfSFbzS3MLmS1BQf-BCWHHGet3nZq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N49HL6WxD9C6-hrqArAPZkP6-"
"N5JIJ9npDqUjw1Sf5FSBXZ7zLuepxRasvwWh4TD2CJZ7p9sJuM8PexaOKXhHS7TYrymoFY"
"1tAkhFJky_L9ulzouNeEMt10qojIRMxNkUvCyAi0J_-7c5d3Ptr5npuFbLldY7hfo1_z_"
"HhCHEbmzomVxcXmaafVb08mhlLnAtS1sUKnMlrhU8UExZNX0zF5xXKqD9gPqTGqIoX1Zm6"
"OWGde5G6rdl8czYkyc3qdNPxU0CMxyd4tPfdWEcfcZZ_"
"y3pUyz345vhMnChNO8igujySLhufvsFbV96aNY3H2kYfhOJTPGjVQdbcFqB05U_6YEd7I-"
"_Gj0DItSsIWyx7oqkfmE7v5k4Y7ojF-"
"gtXxAnzLVLt6mZaWtn1C9xmqgsWyaoCVpdLkllGv1vCg-cPLggp1cTleTwlVCk9pVQo_"
"3tWlaInbEb4n2XtQjl_eQ92jUKcv8pVtjkcpxw-w5k2GphT_-ttv_30dmiN8eeWq_"
"cvkWCJePSTpypyoG_OTgvE4FgMcn62reuRul-QBKYo-yPSprB34CP_"
"hQ8c9vTolpXDFYvbldvCorr4A54Cs8HnKWeDP9lnpS7_vPyIcP7RMNvPYF2-"
"k2vUnpGfgsl86aUzsy7XeKLQ8IunxPSzu28QzPJW08nNO4EA91uFJWZ05VhSWsuQ3Dbc4F"
"qVtHrb3Fv2d5BSp11XZhZ8WzP-3fwFOiVFV",
q_encoded);
free(q_encoded);
char *ret = curl_get(news);
free(news);
if (!ret) continue;
if (!ret)
continue;
json_object *json_ret = json_tokener_parse(ret);
if (!json_ret) {
@ -76,12 +172,15 @@ char *web_search_engine(char *q) {
const int MAX_RETRIES = 3;
for (int retry = 0; retry < MAX_RETRIES; retry++) {
char *searx = malloc(4096);
if (!searx) return NULL;
if (!searx)
return NULL;
searx[0] = 0;
snprintf(searx, 4096, "https://searx.molodetz.nl/search?q=%s&format=json", q);
snprintf(searx, 4096, "https://searx.molodetz.nl/search?q=%s&format=json",
q);
char *ret = curl_get(searx);
free(searx);
if (!ret) continue;
if (!ret)
continue;
json_object *json_ret = json_tokener_parse(ret);
if (!json_ret) {

View File

@ -1,7 +1,7 @@
#include "db_utils.h"
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
void db_initialize() {
sqlite3 *db;

View File

@ -22,8 +22,6 @@ 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;
int rc = sqlite3_open(db_file_expanded(), &db);
@ -32,8 +30,10 @@ void db_initialize() {
return;
}
db_execute("CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);");
db_execute("CREATE TABLE IF NOT EXISTS file_version_history ( id INTEGER PRIMARY KEY AUTOINCREMENT,"
db_execute("CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value "
"TEXT);");
db_execute("CREATE TABLE IF NOT EXISTS file_version_history ( id INTEGER "
"PRIMARY KEY AUTOINCREMENT,"
"path TEXT NOT NULL,"
"content TEXT,"
"date DATETIME DEFAULT CURRENT_TIMESTAMP"
@ -180,15 +180,17 @@ json_object *db_execute(const char *query) {
sqlite3_close(db);
return result;
}
void db_store_file_version(const char * path) {
char * expanded = expand_home_directory(path);
void db_store_file_version(const char *path) {
char *expanded = expand_home_directory(path);
char * content = read_file(expanded);
if(!content) {
char *content = read_file(expanded);
if (!content) {
return;
}
fprintf(stderr, "Creating backup:: %s\n", expanded);
char * formatted = sqlite3_mprintf("INSERT INTO file_version_history (path, content) VALUES (%Q, %Q)", expanded, content);
char *formatted = sqlite3_mprintf(
"INSERT INTO file_version_history (path, content) VALUES (%Q, %Q)",
expanded, content);
db_execute(formatted);
sqlite3_free(formatted);
free(content);

View File

@ -76,8 +76,8 @@ char *curl_post(const char *url, const char *data) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "Url: %s\n",data);
fprintf(stderr, "Data: %s\n",data);
fprintf(stderr, "Url: %s\n", data);
fprintf(stderr, "Data: %s\n", data);
fprintf(stderr, "An error occurred: %s\n", curl_easy_strerror(res));
}
curl_slist_free_all(headers);

View File

@ -12,17 +12,20 @@
#define MAX_PATH 4096
static const char *extensions[] = {
".c", ".cpp", ".h", ".py", ".java", ".js", ".mk", ".html",
"Makefile", ".css", ".json", ".cs", ".csproj", ".sln", ".toml", ".rs",
".go", ".rb", ".swift", ".php", ".pl", ".sh", ".bash", ".sql",
".xml", ".yaml", ".yml", ".kt", ".dart", ".scala", ".clj", ".asm",
".m", ".r", ".lua", ".groovy", ".v", ".pas", ".d", ".f90", ".f95",
".for", ".s", ".tcl", ".vhdl", ".verilog", ".coffee", ".less", ".scss",
".ps1", ".psm1", ".cmd", ".bat", ".json5", ".cxx", ".cc", ".hpp",
".hxx", ".inc", ".nsi", ".ninja", ".cmake", ".cmake.in", ".mk.in",
".make", ".makefile", ".gyp", ".gypi", ".pro", ".qml", ".ui", ".wxs",
".wxl", ".wxi", ".wxl", ".wxs", ".wxi", ".wxl", ".wxs", ".wxi"
};
".c", ".cpp", ".h", ".py", ".java", ".js",
".mk", ".html", "Makefile", ".css", ".json", ".cs",
".csproj", ".sln", ".toml", ".rs", ".go", ".rb",
".swift", ".php", ".pl", ".sh", ".bash", ".sql",
".xml", ".yaml", ".yml", ".kt", ".dart", ".scala",
".clj", ".asm", ".m", ".r", ".lua", ".groovy",
".v", ".pas", ".d", ".f90", ".f95", ".for",
".s", ".tcl", ".vhdl", ".verilog", ".coffee", ".less",
".scss", ".ps1", ".psm1", ".cmd", ".bat", ".json5",
".cxx", ".cc", ".hpp", ".hxx", ".inc", ".nsi",
".ninja", ".cmake", ".cmake.in", ".mk.in", ".make", ".makefile",
".gyp", ".gypi", ".pro", ".qml", ".ui", ".wxs",
".wxl", ".wxi", ".wxl", ".wxs", ".wxi", ".wxl",
".wxs", ".wxi"};
static const size_t ext_count = sizeof(extensions) / sizeof(extensions[0]);
typedef struct {
@ -38,17 +41,21 @@ static size_t file_count = 0;
static int is_valid_extension(const char *filename) {
const char *dot = strrchr(filename, '.');
if (!dot) dot = filename;
if (!dot)
dot = filename;
for (size_t i = 0; i < ext_count; i++) {
if (strcmp(dot, extensions[i]) == 0) return 1;
if (strcmp(dot, extensions[i]) == 0)
return 1;
}
return 0;
}
static int is_ignored_directory(const char *dir_name) {
const char *ignored_dirs[] = {"env", ".venv", "node_modules", "venv", "virtualenv"};
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;
if (strcmp(dir_name, ignored_dirs[i]) == 0)
return 1;
}
return 0;
}
@ -59,9 +66,12 @@ static void get_file_info(const char *path) {
FileInfo info;
strncpy(info.name, path, MAX_PATH - 1);
info.name[MAX_PATH - 1] = '\0';
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", sizeof(info.type) - 1);
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",
sizeof(info.type) - 1);
info.type[sizeof(info.type) - 1] = '\0';
info.size_bytes = file_stat.st_size;
file_list[file_count++] = info;
@ -79,8 +89,10 @@ char *index_directory(const char *dir_path) {
json_object *jarray = json_object_new_array();
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
if (entry->d_name[0] == '.' || is_ignored_directory(entry->d_name)) continue;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
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);
@ -95,11 +107,21 @@ char *index_directory(const char *dir_path) {
} else if (is_valid_extension(entry->d_name)) {
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));
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));
json_object_array_add(jarray, jfile);
}

42
main.c
View File

@ -1,18 +1,18 @@
#include "r.h"
#include <locale.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>
#include "db_utils.h"
#include "line.h"
#include "markdown.h"
#include "openai.h"
#include "utils.h"
#include "db_utils.h"
#include "tools.h"
#include "utils.h"
volatile sig_atomic_t sigint_count = 0;
time_t first_sigint_time = 0;
@ -75,7 +75,8 @@ char *get_prompt_from_args(int argc, char **argv) {
}
if (get_from_std_in) {
if (*system) openai_system(system);
if (*system)
openai_system(system);
prompt = get_prompt_from_stdin(prompt);
} else {
free(prompt);
@ -143,7 +144,8 @@ void repl() {
while (true) {
line = line_read("> ");
if (!line || !*line) continue;
if (!line || !*line)
continue;
if (!strncmp(line, "!dump", 5)) {
printf("%s\n", message_json());
@ -151,12 +153,15 @@ void repl() {
}
if (!strncmp(line, "!verbose", 8)) {
is_verbose = !is_verbose;
fprintf(stderr, "%s\n", is_verbose ? "Verbose mode enabled" : "Verbose mode disabled");
fprintf(stderr, "%s\n",
is_verbose ? "Verbose mode enabled" : "Verbose mode disabled");
continue;
}
if (line && *line != '\n') line_add_history(line);
if(!strncmp(line, "!tools", 6)) {
printf("Available tools: %s\n", json_object_to_json_string(tools_descriptions()));
if (line && *line != '\n')
line_add_history(line);
if (!strncmp(line, "!tools", 6)) {
printf("Available tools: %s\n",
json_object_to_json_string(tools_descriptions()));
continue;
}
if (!strncmp(line, "!models", 7)) {
@ -170,7 +175,8 @@ void repl() {
printf("Current model: %s\n", get_prompt_model());
continue;
}
if (!strncmp(line, "exit", 4)) exit(0);
if (!strncmp(line, "exit", 4))
exit(0);
while (line && *line != '\n') {
char *response = openai_chat("user", line);
@ -192,17 +198,20 @@ void repl() {
char *strreplace(const char *content, const char *what, const char *with) {
char *pos = strstr(content, what);
if (!pos) return strdup(content);
if (!pos)
return strdup(content);
size_t result_size = strlen(content) + strlen(with) - strlen(what) + 1;
char *result = malloc(result_size);
snprintf(result, result_size, "%.*s%s%s", (int)(pos - content), content, with, pos + strlen(what));
snprintf(result, result_size, "%.*s%s%s", (int)(pos - content), content, with,
pos + strlen(what));
return result;
}
bool openai_include(const char *path) {
char *file_content = read_file(path);
if (!file_content) return false;
if (!file_content)
return false;
openai_system(file_content);
free(file_content);
@ -217,7 +226,9 @@ void init() {
char *schema = db_get_schema();
char payload[1024 * 1024] = {0};
snprintf(payload, sizeof(payload),
"Your have a database that you can mutate using the query tool and the get and set tool. This is the schema in json format: %s. Dialect is sqlite.",
"Your have a database that you can mutate using the query tool and "
"the get and set tool. This is the schema in json format: %s. "
"Dialect is sqlite.",
schema);
free(schema);
@ -249,7 +260,8 @@ int main(int argc, char *argv[]) {
signal(SIGINT, handle_sigint);
init();
if (try_prompt(argc, argv)) return 0;
if (try_prompt(argc, argv))
return 0;
repl();
return 0;

View File

@ -25,10 +25,10 @@
#ifndef R_MESSAGES_H
#define R_MESSAGES_H
#include "db_utils.h"
#include "json-c/json.h"
#include "tools.h"
#include <string.h>
#include "db_utils.h"
struct json_object *message_array = NULL;
struct json_object *message_list() {
@ -66,7 +66,7 @@ struct json_object *message_add_tool_result(const char *tool_call_id,
json_object_object_add(message, "tool_call_id",
json_object_new_string(tool_call_id));
if(strlen(tool_result) > 104000){
if (strlen(tool_result) > 104000) {
tool_result[104000] = '\0';
}
@ -82,7 +82,6 @@ void message_add_object(json_object *message) {
json_object_array_add(messages, message);
}
struct json_object *message_add(const char *role, const char *content);
struct json_object *message_add(const char *role, const char *content) {
@ -90,17 +89,19 @@ struct json_object *message_add(const char *role, const char *content) {
struct json_object *message = json_object_new_object();
json_object_object_add(message, "role", json_object_new_string(role));
if(content){
char * formatted_content = strdup(content);
if(strlen(formatted_content) > 1048570){
if (content) {
char *formatted_content = strdup(content);
if (strlen(formatted_content) > 1048570) {
formatted_content[1048570] = '\0';
}
json_object_object_add(message, "content", json_object_new_string(formatted_content));
json_object_object_add(message, "content",
json_object_new_string(formatted_content));
free(formatted_content);
}
if (!strcmp(role, "user")) {
json_object_object_add(message, "tools", tools_descriptions());
json_object_object_add(message, "parallel_tool_calls", json_object_new_boolean(true));
json_object_object_add(message, "parallel_tool_calls",
json_object_new_boolean(true));
}
json_object_array_add(messages, message);

View File

@ -128,8 +128,8 @@ char *openai_chat(const char *user_role, const char *message_content) {
message_add_tool_call(tool_call_result);
}
char *tool_calls_result_str = chat_json(NULL, NULL);
message_object =
openai_process_chat_message(get_completions_api_url(), tool_calls_result_str);
message_object = openai_process_chat_message(get_completions_api_url(),
tool_calls_result_str);
if (message_object == NULL) {
return NULL;
}

55
r.h
View File

@ -11,55 +11,54 @@ char *completions_api_url = "https://api.openai.com/v1/chat/completions";
char *advanced_model = "gpt-4o-mini";
char *fast_model = "gpt-3.5-turbo";
//char *models_api_url = "https://api.openai.com/v1/models";
//char *completions_api_url = "https://api.anthropic.com/v1/chat/completions";
//char *advanced_model = "claude-3-5-haiku-20241022";
//char *advanced_model = "meta-llama/Meta-Llama-3.1-8B-Instruct";
//char *advanced_model = "google/gemini-1.5-flash";
//char *fast_model = "claude-3-5-haiku-20241022";
// char *models_api_url = "https://api.openai.com/v1/models";
// char *completions_api_url = "https://api.anthropic.com/v1/chat/completions";
// char *advanced_model = "claude-3-5-haiku-20241022";
// char *advanced_model = "meta-llama/Meta-Llama-3.1-8B-Instruct";
// char *advanced_model = "google/gemini-1.5-flash";
// char *fast_model = "claude-3-5-haiku-20241022";
//#endif
//#ifdef OLLAMA
//char *models_api_url = "https://ollama.molodetz.nl/v1/models";
//char *completions_api_url = "https://ollama.molodetz.nl/v1/chat/completions";
//char *advanced_model = "qwen2.5:3b";
//char *advanced_model = "qwen2.5-coder:0.5b";
//char *fast_model = "qwen2.5:0.5b";
//#endif
// #endif
// #ifdef OLLAMA
// char *models_api_url = "https://ollama.molodetz.nl/v1/models";
// char *completions_api_url = "https://ollama.molodetz.nl/v1/chat/completions";
// char *advanced_model = "qwen2.5:3b";
// char *advanced_model = "qwen2.5-coder:0.5b";
// char *fast_model = "qwen2.5:0.5b";
// #endif
char *_model = NULL;
#define DB_FILE "~/.r.db"
#define PROMPT_TEMPERATURE 0.1
bool get_use_strict(){
if(getenv("R_USE_STRICT") != NULL) {
const char * value = getenv("R_USE_STRICT");
if(!strcmp(value, "true")) {
bool get_use_strict() {
if (getenv("R_USE_STRICT") != NULL) {
const char *value = getenv("R_USE_STRICT");
if (!strcmp(value, "true")) {
return true;
}
if(!strcmp(value, "false")) {
if (!strcmp(value, "false")) {
return false;
}
if(!strcmp(value, "1")) {
if (!strcmp(value, "1")) {
return true;
}
if(!strcmp(value, "0")) {
if (!strcmp(value, "0")) {
return false;
}
}
return true;
}
char * get_completions_api_url() {
if(getenv("R_BASE_URL") != NULL) {
char *get_completions_api_url() {
if (getenv("R_BASE_URL") != NULL) {
return joinpath(getenv("R_BASE_URL"), "v1/chat/completions");
}
return completions_api_url;
}
char * get_models_api_url() {
if(getenv("R_BASE_URL") != NULL) {
char *get_models_api_url() {
if (getenv("R_BASE_URL") != NULL) {
return joinpath(getenv("R_BASE_URL"), "v1/models");
}
return models_api_url;
@ -73,10 +72,10 @@ void set_prompt_model(const char *model) {
}
const char *get_prompt_model() {
if(_model == NULL && getenv("R_MODEL") != NULL) {
if (_model == NULL && getenv("R_MODEL") != NULL) {
_model = getenv("R_MODEL");
}
if(_model){
if (_model) {
return _model;
}
if (auth_type != AUTH_TYPE_API_KEY) {

BIN
rpylib.so

Binary file not shown.

95
tools.h
View File

@ -12,23 +12,21 @@
#ifndef R_TOOLS_H
#define R_TOOLS_H
#include "http_curl.h"
#include "r.h"
#include "utils.h"
#include <dirent.h>
#include <errno.h>
#include <glob.h>
#include <json-c/json.h>
#include <json-c/json_object.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "browse.h"
#include "db_utils.h"
@ -120,8 +118,9 @@ struct json_object *tool_description_web_search_news() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -176,8 +175,9 @@ struct json_object *tool_description_web_search() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -229,8 +229,9 @@ struct json_object *tool_description_db_get() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -281,8 +282,9 @@ struct json_object *tool_description_db_query() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -340,8 +342,9 @@ struct json_object *tool_description_db_set() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -367,19 +370,17 @@ char *tool_function_linux_terminal(char *command) {
char stderr_command[1024] = {0};
if(!strstr(command, "2>")) {
if (!strstr(command, "2>")) {
snprintf(stderr_command, sizeof(stderr_command), "%s 2>&1", command);
command = stderr_command;
}
fp = popen(command, "r");
if (fp == NULL) {
perror("popen failed");
return strdup("Popen failed!");
}
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
size_t chunk_size = strlen(buffer);
char *new_output = realloc(output, total_size + chunk_size + 1);
@ -482,8 +483,9 @@ struct json_object *tool_description_chdir() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -524,8 +526,9 @@ struct json_object *tool_description_index_source_directory() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -569,8 +572,9 @@ struct json_object *tool_description_linux_terminal_interactive() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -614,8 +618,9 @@ struct json_object *tool_description_directory_rglob() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -655,8 +660,9 @@ struct json_object *tool_description_read_file() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -709,8 +715,9 @@ struct json_object *tool_description_write_file() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -721,10 +728,11 @@ char *tool_function_index_source_directory(char *path) {
return index_directory(path);
}
char * tool_function_mkdir(char *path);
char *tool_function_mkdir(char *path);
void ensure_parent_directory_exists(char *path_including_file_name) {
if(!path_including_file_name) return;
if (!path_including_file_name)
return;
char *path = strdup(path_including_file_name);
char *last_slash = strrchr(path, '/');
@ -884,7 +892,7 @@ char *tool_function_directory_rglob(char *target_dir) {
struct stat file_stat;
char mod_time[20], create_time[20];
if(!strcmp(target_dir, ".")) {
if (!strcmp(target_dir, ".")) {
target_dir[0] = '*';
}
@ -1008,8 +1016,9 @@ struct json_object *tool_description_http_get() {
json_object_new_boolean(0));
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -1052,8 +1061,9 @@ struct json_object *tool_description_directory_glob() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -1093,8 +1103,9 @@ struct json_object *tool_description_linux_terminal() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);
@ -1133,7 +1144,8 @@ struct json_object *tool_description_mkdir() {
json_object_object_add(function, "name", json_object_new_string("mkdir"));
json_object_object_add(
function, "description",
json_object_new_string("Creates a new directory with the specified path."));
json_object_new_string(
"Creates a new directory with the specified path."));
struct json_object *parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
@ -1157,8 +1169,9 @@ struct json_object *tool_description_mkdir() {
json_object_object_add(function, "parameters", parameters);
if(get_use_strict())
json_object_object_add(function, "strict", json_object_new_boolean(get_use_strict()));
if (get_use_strict())
json_object_object_add(function, "strict",
json_object_new_boolean(get_use_strict()));
json_object_object_add(root, "function", function);

17
utils.h
View File

@ -21,13 +21,12 @@
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <limits.h>
#include <pwd.h>
#include <unistd.h>
#endif
void get_current_directory() {
@ -49,17 +48,17 @@ void get_current_directory() {
#endif
}
char* expand_home_directory(const char* path) {
char *expand_home_directory(const char *path) {
if (path[0] != '~') {
return strdup(path); // Return the original path if it doesn't start with ~
}
char* home_dir;
char *home_dir;
#ifdef _WIN32
home_dir = getenv("USERPROFILE"); // Get home directory on Windows
#else
struct passwd* pw = getpwuid(getuid());
struct passwd *pw = getpwuid(getuid());
home_dir = pw->pw_dir; // Get home directory on Linux
#endif
@ -69,7 +68,7 @@ char* expand_home_directory(const char* path) {
// Allocate memory for the expanded path
size_t expanded_size = strlen(home_dir) + strlen(path);
char* expanded_path = (char *)malloc(expanded_size);
char *expanded_path = (char *)malloc(expanded_size);
if (expanded_path == NULL) {
return NULL; // Memory allocation error
}
@ -90,14 +89,14 @@ unsigned long hash(const char *str) {
return hash;
}
char * joinpath(const char *base_url, const char *path) {
char *joinpath(const char *base_url, const char *path) {
static char result[1024];
result[0] = '\0';
strcat(result, base_url);
if(base_url[strlen(base_url) - 1] != '/') {
if (base_url[strlen(base_url) - 1] != '/') {
strcat(result, "/");
}
if(path[0] == '/') {
if (path[0] == '/') {
path++;
}
strcat(result, path);