Compare commits

..

No commits in common. "e02315a0f6b92dd5f01805cecde529b0705ad692" and "d0697da15ef3608621ef19a24cdc7ec2ed519a20" have entirely different histories.

9 changed files with 49 additions and 69 deletions

3
.gitignore vendored
View File

@ -8,5 +8,4 @@ r
rf
.docs
auth.h
.rcontext*
tests
.rcontext

17
.rcontext.txt Normal file
View File

@ -0,0 +1,17 @@
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.

5
auth.h
View File

@ -7,10 +7,9 @@
// MIT License
#include <stdlib.h>
#include <stdio.h>
const char * resolve_api_key(){
static char * api_key = NULL;
char * api_key;
#ifndef FREE_VERSION
api_key = getenv("R_KEY");
if(api_key)
@ -27,4 +26,4 @@ const char * resolve_api_key(){
#endif
api_key = "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";
return api_key;
}
}

6
http.h
View File

@ -242,7 +242,7 @@ char *https_get(char *url)
SSL_set_tlsext_host_name(ssl, hostname);
SSL_set_fd(ssl, sock);
int buffer_size = 1024*1024;
int buffer_size = 4096;
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 = 1024*1024;
int buffer_size = 4096;
char *buffer = malloc(buffer_size);
size_t chunk_size_total = 0;
size_t len = strlen(data);
char *request = malloc(len + buffer_size);
char *request = malloc(len + 4096);
sprintf(request,
"POST %s HTTP/1.1\r\n"
"Content-Length: %ld\r\n"

5
line.h
View File

@ -50,9 +50,6 @@ void line_init() {
char* line_read(char* prefix) {
char* data = readline(prefix);
if (!(data && *data)) {
if(data){
free(data);
}
return NULL;
}
return data;
@ -62,4 +59,4 @@ void line_add_history(char* data) {
read_history(HISTORY_FILE);
add_history(data);
write_history(HISTORY_FILE);
}
}

54
main.c
View File

@ -38,31 +38,17 @@
#include <string.h>
#include <unistd.h>
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';
return prompt;
}
char *get_prompt_from_args(int c, char **argv) {
char *prompt = malloc(1024 * 1024 * 10 + 1);
char *prompt = malloc(1024 * 1024 + 1);
prompt[0] = 0;
for (int i = 1; i < c; i++) {
if (!strcmp(argv[i],"--stdin")){
prompt = get_prompt_from_stdin(prompt);
if (argv[i][0] == '-')
break;
}else{
strcat(prompt, argv[i]);
if (i < c - 1) {
strcat(prompt, " ");
} else {
strcat(prompt, ". ");
}
strncat(prompt, argv[i], 1024 * 1024);
if (i < c - 1) {
strncat(prompt, " ", 1024 * 1024);
} else {
strncat(prompt, ".", 1024 * 1024);
}
}
if (!*prompt) {
@ -76,11 +62,6 @@ 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);
@ -105,7 +86,7 @@ void render(char *content) {
void repl() {
line_init();
char *line = NULL;
char *line;
char *previous_line = NULL;
while ((line = line_read("> "))) {
if (!line || !*line) {
@ -142,7 +123,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);
@ -150,12 +131,11 @@ void repl() {
free(command);
continue;
}
if(*line){
line_add_history(line);
char *response = openai_chat("user", line);
render(response);
free(response);
}
line_add_history(line);
char *response = openai_chat("user", line);
render(response);
free(response);
}
}
@ -192,14 +172,14 @@ bool openai_include(char *path) {
long size = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = (char *)malloc(size + 1);
char *buffer = (char *)malloc(size);
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);
@ -231,4 +211,4 @@ int main(int argc, char *argv[]) {
repl();
return 0;
}
}

View File

@ -46,15 +46,15 @@ int is_keyword(const char *word) {
void highlight_code(const char *code) {
const char *ptr = code;
char buffer[4096];
char buffer[256];
size_t index = 0;
buffer[index] = 0;
while (*ptr) {
if ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z') || (*ptr == '_')) {
while ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z') || (*ptr >= '0' && *ptr <= '9') || (*ptr == '_')) {
buffer[index++] = *ptr++;
}
buffer[index] = 0;
buffer[index] = '\0';
if (is_keyword(buffer)) {
printf(FG_BLUE "%s" RESET, buffer);
@ -66,7 +66,7 @@ void highlight_code(const char *code) {
while (*ptr >= '0' && *ptr <= '9') {
buffer[index++] = *ptr++;
}
buffer[index] = 0;
buffer[index] = '\0';
printf(FG_CYAN "%s" RESET, buffer);
index = 0;
} else {
@ -93,21 +93,14 @@ void parse_markdown_to_ansi(const char *markdown) {
}
if (inside_code) {
char code_buffer[4096];
char code_buffer[256];
size_t index = 0;
while (*ptr && *ptr != '`') {
code_buffer[index++] = *ptr++;
if(*ptr == '\n' || *ptr == ' ' || *ptr == '\t' || *ptr == '.'){
code_buffer[index++] = 0;
highlight_code(code_buffer);
index = 0;
code_buffer[index] = 0;
}
}
code_buffer[index] = 0;
if(index)
highlight_code(code_buffer);
code_buffer[index] = '\0';
highlight_code(code_buffer);
} else {
if (strncmp(ptr, "**", 2) == 0) {
printf(BOLD);

View File

@ -39,7 +39,6 @@ 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;
}

View File

@ -35,13 +35,9 @@ 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";
@ -56,4 +52,4 @@ void plugin_run(char *src) {
void plugin_destruct() {
if (plugin_initialized)
Py_Finalize();
}
}