diff --git a/main.c b/main.c index e43045b..cdc62cb 100644 --- a/main.c +++ b/main.c @@ -29,6 +29,46 @@ static void repl(void); static void init(void); static void handle_sigint(int sig); +char * get_env_string(){ + FILE *fp = popen("env", "r"); + if (fp == NULL) { + perror("popen failed"); + return NULL; + } + + size_t buffer_size = 1024; + size_t total_size = 0; + char *output = malloc(buffer_size); + if (output == NULL) { + perror("malloc failed"); + pclose(fp); + return NULL; + } + + size_t bytes_read; + while ((bytes_read = fread(output + total_size, 1, buffer_size - total_size, fp)) > 0) { + total_size += bytes_read; + if (total_size >= buffer_size) { + buffer_size *= 2; + char *temp = realloc(output, buffer_size); + if (temp == NULL) { + perror("realloc failed"); + free(output); + pclose(fp); + return NULL; + } + output = temp; + } + } + + // Null-terminate the output + output[total_size] = '\0'; + + + pclose(fp); + return output; +} + static char *get_prompt_from_stdin(char *prompt) { int index = 0; int c; @@ -194,14 +234,20 @@ static void init(void) { 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.", + "# LOCAL DATABASE" + "Your have a local database that you can mutate using the query tool and " + "the get and set tool." + "If you set a value using the tool, make sure that the key is stemmed and lowercased to prevent double entries." + "Dialect is sqlite. This is the schema in json format: %s. ", schema); free(schema); - fprintf(stderr, "Loading... 📨"); openai_system(payload); + char * env_system_message = get_env_system_message(); + if(env_system_message && *env_system_message){ + openai_system(env_system_message); + free(env_system_message); + } if (!openai_include(".rcontext.txt")) { openai_include("~/.rcontext.txt"); } @@ -228,6 +274,11 @@ int main(int argc, char *argv[]) { signal(SIGINT, handle_sigint); init(); + char * env_string = get_env_string(); + if(env_string && *env_string){ + openai_system(env_string); + free(env_string); + } if (try_prompt(argc, argv)) return 0; diff --git a/r.h b/r.h index 730ebd3..5d301e2 100644 --- a/r.h +++ b/r.h @@ -32,6 +32,13 @@ char *_model = NULL; #define DB_FILE "~/.r.db" #define PROMPT_TEMPERATURE 0.1 +char * get_env_system_message(){ + if (getenv("R_SYSTEM_MESSAGE") != NULL) { + return strdup(getenv("R_SYSTEM_MESSAGE")); + } + return NULL; +} + bool get_use_strict() { if (getenv("R_USE_STRICT") != NULL) { const char *value = getenv("R_USE_STRICT"); diff --git a/rpylib.so b/rpylib.so index eedf29d..d47f38a 100755 Binary files a/rpylib.so and b/rpylib.so differ diff --git a/tools.h b/tools.h index 62fbc21..366e8d2 100644 --- a/tools.h +++ b/tools.h @@ -66,7 +66,7 @@ struct json_object *tools_descriptions() { json_object_array_add(root, tool_description_directory_glob()); json_object_array_add(root, tool_description_read_file()); 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_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()); @@ -672,6 +672,7 @@ struct json_object *tool_description_linux_terminal_interactive() { } struct json_object *tool_description_directory_rglob() { + struct json_object *root = json_object_new_object(); json_object_object_add(root, "type", json_object_new_string("function"));