Nice changes.

This commit is contained in:
retoor 2025-03-21 01:15:40 +01:00
parent 40e85777b9
commit 64a2407f4d
3 changed files with 101 additions and 4 deletions

18
auth.h
View File

@ -13,14 +13,23 @@
#include <stdio.h>
enum AUTH_TYPE {
AUTH_TYPE_NONE,
AUTH_TYPE_API_KEY,
AUTH_TYPE_FREE
};
int auth_type = AUTH_TYPE_FREE;
int auth_type = AUTH_TYPE_NONE;
void auth_free(){
auth_type = AUTH_TYPE_FREE;
}
void auth_init() {
char *api_key = NULL;
if(auth_type != AUTH_TYPE_FREE) {
api_key = getenv("R_KEY");
if (api_key) {
auth_type = AUTH_TYPE_API_KEY;
@ -30,13 +39,16 @@ void auth_init() {
if (api_key) {
auth_type = AUTH_TYPE_API_KEY;
return;
}
}
}
auth_type = AUTH_TYPE_FREE;
return;
}
const char *resolve_api_key() {
static char *api_key = NULL;
if(auth_type != AUTH_TYPE_FREE) {
api_key = getenv("R_KEY");
if (api_key) {
auth_type = AUTH_TYPE_API_KEY;
@ -47,6 +59,8 @@ const char *resolve_api_key() {
auth_type = AUTH_TYPE_API_KEY;
return api_key;
}
}
auth_type = AUTH_TYPE_FREE;
api_key = "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";
return api_key;
}

32
main.c
View File

@ -32,6 +32,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "openai.h"
@ -44,7 +45,8 @@
#include <unistd.h>
#include "utils.h"
volatile sig_atomic_t sigint_count = 0;
time_t first_sigint_time = 0;
bool SYNTAX_HIGHLIGHT_ENABLED = true;
bool API_MODE = false;
void help();
@ -84,8 +86,11 @@ char *get_prompt_from_args(int c, char **argv) {
//plugin_run(file_content);
i++;
}
}else if (!strcmp(argv[i],"--free")){
auth_free();
continue;
}
else if(!strcmp(argv[i],"--context")){
if(i+1 <= c){
char * context_file_path = argv[i+1];
@ -202,6 +207,10 @@ void repl() {
printf("%s\n",message_json());
continue;
}
if (!strncmp(line, "model", 5)) {
printf("%s\n",get_prompt_model());
continue;
}
if (!strncmp(line, "exit", 4)) {
exit(0);
}
@ -304,7 +313,26 @@ void init() {
fprintf(stderr, "\r \r");
}
void handle_sigint(int sig) {
time_t current_time = time(NULL);
if (sigint_count == 0) {
first_sigint_time = current_time;
sigint_count++;
} else {
if (difftime(current_time, first_sigint_time) <= 1) {
exit(0);
} else {
sigint_count = 1;
first_sigint_time = current_time;
}
}
}
int main(int argc, char *argv[]) {
signal(SIGINT, handle_sigint);
init();
if (try_prompt(argc, argv))
return 0;

55
tools.h
View File

@ -25,6 +25,8 @@
#include <stdlib.h>
#include <string.h>
#include "indexer.h"
struct json_object* tool_description_http_get();
struct json_object* tool_description_linux_terminal();
struct json_object* tool_description_directory_glob();
@ -32,6 +34,7 @@ struct json_object* tool_description_read_file();
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* tools_descriptions() {
struct json_object* root = json_object_new_array();
@ -42,6 +45,7 @@ struct json_object* tools_descriptions() {
json_object_array_add(root, tool_description_write_file());
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());
return root;
}
@ -93,6 +97,40 @@ char* tool_function_linux_terminal_interactive(char* command) {
return result;
}
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"));
struct json_object* function = json_object_new_object();
json_object_object_add(function, "name", json_object_new_string("index_source_directory"));
json_object_object_add(function, "description", json_object_new_string("Returns a JSON array containing every source file with it's contents from given directory. Execute with '.' for current directory."));
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 index and retreive files from."));
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_linux_terminal_interactive() {
struct json_object* root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
@ -233,6 +271,11 @@ struct json_object* tool_description_write_file() {
return root;
}
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);
@ -665,6 +708,18 @@ struct json_object* tools_execute(struct json_object* tools_array) {
free(write_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));
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* listing_result = tool_function_index_source_directory(path);
json_object_object_add(tool_result, "content", json_object_new_string(listing_result));
free(listing_result);
}
}
} else if (!strcmp(function_name, "directory_rglob")) {
struct json_object* arguments_obj;
if (json_object_object_get_ex(function_obj, "arguments", &arguments_obj)) {