From e02315a0f6b92dd5f01805cecde529b0705ad692 Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 26 Jan 2025 02:54:45 +0100 Subject: [PATCH] Update. --- .gitignore | 3 ++- .rcontext.txt | 17 ----------------- auth.h | 5 +++-- http.h | 6 +++--- line.h | 5 ++++- main.c | 44 ++++++++++++++++++++++++-------------------- markdown.h | 11 +++-------- openai.h | 1 + plugin.h | 6 +++++- 9 files changed, 45 insertions(+), 53 deletions(-) delete mode 100644 .rcontext.txt diff --git a/.gitignore b/.gitignore index 1b88d9f..0a0f6a3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ r rf .docs auth.h -.rcontext +.rcontext* +tests diff --git a/.rcontext.txt b/.rcontext.txt deleted file mode 100644 index d61d83c..0000000 --- a/.rcontext.txt +++ /dev/null @@ -1,17 +0,0 @@ -Alle vragen die je krijgt en beantwoordt, worden behandeld in de context van de programmeertalen C of Python. -Alle voorbeelden die je geeft, moeten in een van deze programmeertalen zijn. - -Je maakt regelmatig gebruik van een emoticon en gebruikt veel markdown tenzij anders gevraagd wordt. - -Geef in elk antwoord aan of de gekozen taal de juiste keuze is voor het beschreven probleem. Noem één alternatief als dat niet het geval is. - -Als je wordt gevraagd om een review te doen: - - Geef de broncode een cijfer tussen 0 en 10. - - Geef een korte samenvatting van je review. - - Noem drie verbeterpunten voor de broncode. - - Noem drie sterke punten van de broncode. - -Als je wordt gevraagd om te controleren op AI-gebruik: - - Verdeel de broncode in categorieën. - - Wijs elke categorie een percentage van AI-gebruik toe. - - Geef een conclusie over de authenticiteit van de code. \ No newline at end of file diff --git a/auth.h b/auth.h index e38924e..aa90ea2 100644 --- a/auth.h +++ b/auth.h @@ -7,9 +7,10 @@ // MIT License #include +#include const char * resolve_api_key(){ - char * api_key; + static char * api_key = NULL; #ifndef FREE_VERSION api_key = getenv("R_KEY"); if(api_key) @@ -26,4 +27,4 @@ const char * resolve_api_key(){ #endif api_key = "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"; return api_key; -} \ No newline at end of file +} diff --git a/http.h b/http.h index a59f5ab..277116a 100644 --- a/http.h +++ b/http.h @@ -242,7 +242,7 @@ char *https_get(char *url) SSL_set_tlsext_host_name(ssl, hostname); SSL_set_fd(ssl, sock); - int buffer_size = 4096; + int buffer_size = 1024*1024; char *buffer = (char *)malloc(buffer_size); if (SSL_connect(ssl) <= 0) @@ -307,12 +307,12 @@ char *http_post(char *url, char *data) int port = atoi(parsed_url.port); int sock = create_socket(hostname, port); - int buffer_size = 4096; + int buffer_size = 1024*1024; char *buffer = malloc(buffer_size); size_t chunk_size_total = 0; size_t len = strlen(data); - char *request = malloc(len + 4096); + char *request = malloc(len + buffer_size); sprintf(request, "POST %s HTTP/1.1\r\n" "Content-Length: %ld\r\n" diff --git a/line.h b/line.h index 99b07de..c081ac7 100644 --- a/line.h +++ b/line.h @@ -50,6 +50,9 @@ void line_init() { char* line_read(char* prefix) { char* data = readline(prefix); if (!(data && *data)) { + if(data){ + free(data); + } return NULL; } return data; @@ -59,4 +62,4 @@ void line_add_history(char* data) { read_history(HISTORY_FILE); add_history(data); write_history(HISTORY_FILE); -} \ No newline at end of file +} diff --git a/main.c b/main.c index 7a70e9b..4a119b6 100644 --- a/main.c +++ b/main.c @@ -38,32 +38,30 @@ #include #include -char * get_prompt_from_stdin() { - char * prompt = malloc(1024 * 1024 * 10 + 1); - prompt[0] = 0; - int index = 0; +char * get_prompt_from_stdin(char * prompt) { + int index = 0; + prompt[index] = '\0'; char c = 0; while ((c = getchar()) != EOF) { prompt[index++] = c; } - prompt[index++] = 0; + prompt[index++] = '\0'; return prompt; } char *get_prompt_from_args(int c, char **argv) { - char *prompt = malloc(1024 * 1024 + 1); + char *prompt = malloc(1024 * 1024 * 10 + 1); prompt[0] = 0; for (int i = 1; i < c; i++) { if (!strcmp(argv[i],"--stdin")){ - free(prompt); - prompt = get_prompt_from_stdin(); + prompt = get_prompt_from_stdin(prompt); break; }else{ - strncat(prompt, argv[i], 1024 * 1024); + strcat(prompt, argv[i]); if (i < c - 1) { - strncat(prompt, " ", 1024 * 1024); + strcat(prompt, " "); } else { - strncat(prompt, ".", 1024 * 1024); + strcat(prompt, ". "); } } } @@ -78,6 +76,11 @@ bool try_prompt(int argc, char *argv[]) { char *prompt = get_prompt_from_args(argc, argv); if (prompt != NULL) { char *response = openai_chat("user", prompt); + if(!response){ + printf("Could not get response from server\n"); + free(prompt); + return false; + } parse_markdown_to_ansi(response); printf("\n"); free(response); @@ -102,7 +105,7 @@ void render(char *content) { void repl() { line_init(); - char *line; + char *line = NULL; char *previous_line = NULL; while ((line = line_read("> "))) { if (!line || !*line) { @@ -139,7 +142,7 @@ void repl() { offset = 4; } char *command = (char *)malloc(strlen(line) + 42); - command[0] = 0; + command[0] = '\0'; strcpy(command, "ls "); strcat(command, line + offset); int res = system(command); @@ -147,11 +150,12 @@ void repl() { free(command); continue; } - - line_add_history(line); - char *response = openai_chat("user", line); - render(response); - free(response); + if(*line){ + line_add_history(line); + char *response = openai_chat("user", line); + render(response); + free(response); + } } } @@ -188,14 +192,14 @@ bool openai_include(char *path) { long size = ftell(file); fseek(file, 0, SEEK_SET); - char *buffer = (char *)malloc(size); + char *buffer = (char *)malloc(size + 1); size_t read = fread(buffer, 1, size, file); if (read == 0) { return false; } fclose(file); - buffer[read] = 0; + buffer[read] = '\0'; openai_system(buffer); free(buffer); diff --git a/markdown.h b/markdown.h index 347f8ae..51eed36 100644 --- a/markdown.h +++ b/markdown.h @@ -105,14 +105,9 @@ void parse_markdown_to_ansi(const char *markdown) { code_buffer[index] = 0; } } - if(index > 0){ - highlight_code(code_buffer); - index = 0; - code_buffer[index] = 0; - - } - //code_buffer[index] = 0; - //highlight_code(code_buffer); + code_buffer[index] = 0; + if(index) + highlight_code(code_buffer); } else { if (strncmp(ptr, "**", 2) == 0) { printf(BOLD); diff --git a/openai.h b/openai.h index 9388580..ebd09c6 100644 --- a/openai.h +++ b/openai.h @@ -39,6 +39,7 @@ char *openai_chat(char *role, char *content) { struct json_object *parsed_json = json_tokener_parse(result); if (!parsed_json) { fprintf(stderr, "Failed to parse JSON.\n"); + fprintf(stderr, "%s\n", result); return NULL; } diff --git a/plugin.h b/plugin.h index f23225a..098750a 100644 --- a/plugin.h +++ b/plugin.h @@ -35,9 +35,13 @@ void plugin_run(char *src) { const char *basics = "import sys\n" "import os\n" + "from os import *\n" "import math\n" "import pathlib\n" + "from pathlib import Path\n" + "import re\n" "import subprocess\n" + "from subprocess import *\n" "import time\n" "from datetime import datetime\n" "%s"; @@ -52,4 +56,4 @@ void plugin_run(char *src) { void plugin_destruct() { if (plugin_initialized) Py_Finalize(); -} \ No newline at end of file +}