Compare commits

...

2 Commits

Author SHA1 Message Date
e02315a0f6 Update. 2025-01-26 02:54:45 +01:00
c0f5be662a Stdin support 2025-01-25 14:19:01 +01:00
9 changed files with 69 additions and 49 deletions

3
.gitignore vendored
View File

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

View File

@ -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.

3
auth.h
View File

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

3
line.h
View File

@ -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;

42
main.c
View File

@ -38,17 +38,31 @@
#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 + 1);
char *prompt = malloc(1024 * 1024 * 10 + 1);
prompt[0] = 0;
for (int i = 1; i < c; i++) {
if (argv[i][0] == '-')
if (!strcmp(argv[i],"--stdin")){
prompt = get_prompt_from_stdin(prompt);
break;
strncat(prompt, argv[i], 1024 * 1024);
if (i < c - 1) {
strncat(prompt, " ", 1024 * 1024);
}else{
strncat(prompt, ".", 1024 * 1024);
strcat(prompt, argv[i]);
if (i < c - 1) {
strcat(prompt, " ");
} else {
strcat(prompt, ". ");
}
}
}
if (!*prompt) {
@ -62,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);
@ -86,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) {
@ -123,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);
@ -131,13 +150,14 @@ void repl() {
free(command);
continue;
}
if(*line){
line_add_history(line);
char *response = openai_chat("user", line);
render(response);
free(response);
}
}
}
void help() {
char help_text[1024 * 1024] = {0};
@ -172,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);

View File

@ -46,15 +46,15 @@ int is_keyword(const char *word) {
void highlight_code(const char *code) {
const char *ptr = code;
char buffer[256];
char buffer[4096];
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,13 +93,20 @@ void parse_markdown_to_ansi(const char *markdown) {
}
if (inside_code) {
char code_buffer[256];
char code_buffer[4096];
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';
}
code_buffer[index] = 0;
if(index)
highlight_code(code_buffer);
} else {
if (strncmp(ptr, "**", 2) == 0) {

View File

@ -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;
}

View File

@ -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";