This commit is contained in:
retoor 2025-03-22 03:15:49 +01:00
parent 984e125cfd
commit 04785fe350
16 changed files with 1323 additions and 18 deletions

470
.r_history Normal file
View File

@ -0,0 +1,470 @@
htop
How much bigger is my disk than my memory?
fetch my ten bytes from /dev/urandom
ddos https://molodetz.nl
do ab benchmark on https://molodetz.nl
do ab benchmark on https://molodetz.nl with -c 10 -n 1000
validate computer security
r check system security
does https://blog.nldr.net/ contain a typo?
any more?
does https://molodetz.nl contain a typo?
r how much times is my disk bigger than my memory? How much would it kinda cost to upgrade my memory so much that it will match my disk side?
generate a python script that prints "Lekker voor je!". Save it to lekker.py. execute it with python3.
execute
Hi
What model are you?
What eversion of the model?
what is iotop-c package?
What is the difference between iotop-c and iotop?
execute iotop
sudo iptop
json-c to string in c
does fprintf have a limit?
what is better to use than fprintf when writing much data.
How to do fwrite
how to pretty print json-c string
json-c remove last element from json array
asasfqgwq
execute interactive iotop
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) attempt to write a readonly database
What points are not very valid from https://devrant.com/rants/13665478/at12bitfloat-serious-debate-on-rust-vs-c-here-are-my-arguments-c-has-1-better-sy
What points are just plain wrong from https://devrant.com/rants/13665478/at12bitfloat-serious-debate-on-rust-vs-c-here-are-my-arguments-c-has-1-better-sy
Grade every statement from 1-10 regarding https://devrant.com/rants/13665478/at12bitfloat-serious-debate-on-rust-vs-c-here-are-my-arguments-c-has-1-better-sy
find out where my disk space goes to
I want a c websocket client that can send text
Does windows have a shortcut to open a terminal?
Hi
Why
hi
How much mb is 134217728 bytes
hi
hi
list all files
check if my pc has rust
create a ne wrust project and add cli libs
do it
run it
execute the binary result
do it
run the binary
do a cargo run porjecct
Bash traps how do they work and how can i let it repeat last command?
HOw to make a backup command iwth tiemstamp file name in a make file command
list files in current directory.
read x.txt write xy.txt say haha
read x.txt write xy.txt say haha and finally send DONE!
read x.txt write xy.txt say haha and finally send DONE!
read x.txt write xy.txt say haha and finally send DONE!
continue
continue
How to uncomment vim
There must be a shortcut
What would you say what hte level is of this developer?? http://localhost:8082/
What could be better about the site??
what book
what is the exiting year?
what about 1984?
what are you fan of?
Who are you?
list files
ls
cat all text files
cat all text files
cat all text files
cat all text files
Who are you?
how to run ftpd
How to run the normal ftpd on linux
How does ollama tools api work using http?
How to do class based views in aiohttp
How to access app from clas based view
How to do have url tail in aiohttp server route
Show how to use sqlite in python
make sqlite3 cursor select query return dicts
Generate rust source code that shows a message in the right upper corner of the screen.
Not of terminal, but on desktop
do all this for me
continue
It says | ^^^^^^^^^^^^^^^^^^^^^^^^^ no `native_windows_gui` in the root
do it
do it
remove this project
create a rust websocket client project that sends this json to https://snek.molodetz.nl/rpc.ws: {"method":"login","args":["username","password"]}
do it
error[E0599]: no method named `send` found for struct `WebSocketStream` in the current scope
--> src/main.rs:19:15
|
19 | ws_stream.send(Message::Text(json_message.to_string())).await.expec...
| ^^^^
|
::: /home/retoor/.cargo/registry/src/index.crates.io-6f17d22bba15001f/futures-util-0.3.31/src/sink/mod.rs
do it
error[E0433]: failed to resolve: use of undeclared crate or module `futures_util`
--> src/main.rs:8:5
|
8 | use futures_util::sink::SinkExt;
| ^^^^^^^^^^^^ use of undeclared crate or module `futures_util`
error[E0599]: no method named `send` found for struct `WebSocketStream` in the current scope
--> src/main.rs:20:15
|
do it
replace url with wss://snek.molodetz.nl/rpc.ws
do it
Failed to connect: Url(TlsFeatureNotEnabled)
do it
error: failed to select a version for `tokio-tungstenite`.
... required by package `websocket_client v0.1.0 (/home/retoor/websocket_client)`
versions that meet the requirements `^0.17` (locked to 0.17.2) are: 0.17.2
do it
error: failed to select a version for `tokio-tungstenite`.
... required by package `websocket_client v0.1.0 (/home/retoor/websocket_client)`
versions that meet the requirements `^0.17` (locked to 0.17.2) are: 0.17.2
the package `websocket_client` depends on `tokio-tungstenite`, with features: `tls` but `tokio-tungstenite` does not have these features.
do it
error: failed to select a version for `tokio-tungstenite`.
... required by package `websocket_client v0.1.0 (/home/retoor/websocket_client)`
versions that meet the requirements `^0.15` are: 0.15.0
the package `websocket_client` depends on `tokio-tungstenite`, with features: `tls` but `tokio-tungstenite` does not have these features.
failed to select a version for `tokio-tungstenite` which could resolve this conflict
do it
thread 'main' panicked at src/main.rs:13:55:
Failed to connect: Url(TlsFeatureNotEnabled)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
do it
do it
do it
do it
do it
thread 'main' panicked at src/main.rs:13:55:
Failed to connect: Url(TlsFeatureNotEnabled)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
do it
do it
do it
create a C# project that does the same things as the rust project you've just generated
do it
do it
do it
do it
do it
do it
do it
upgrade the project to dotnet 9.0
do it
do it
Unhandled exception. System.PlatformNotSupportedException: Operation is not supported on this platform.
at System.Func`3.BeginInvoke(T1 arg1, T2 arg2, AsyncCallback callback, Object object)
at WebSocketSharp.WebSocket.send(Opcode opcode, Byte[] data, Action`1 completed)
at WebSocketSharp.WebSocket.Send(String data, Action`1 completed)
at WebSocketSharp.WebSocket.Send(String data)
at Program.Main(String[] args) in /home/retoor/WebSocketClient/Program.cs:line 23
do it
do it
Make it listen for response message afterwards and display it.
do it
replace username with retoor and password with retoorded
do it
make it receive messages within a loon instead of once.
make run
If c application says 'core dumped' where is it dumped?
Why
do it
htop
Helping someone with his phone always comes with danger.
I just touched someones phone and his screen went black.
The guy was directly like 'What did you do?' omg.
asfafs
Haaah
index
index
r
r
index
index
investigate
investigate
investigate
investigate
ls *.cs
read file
Do you see any optimizations?
list *.c
list *.cs
read
read that file
what can be optimized?
Apply optimizations
make run
index
make it auto reconnecting after error / connection lost.
yes
make run
index
format Program.cs
yes
make linux
I think there were warnings.
yes
How doe a javascript sum
jinja number format 1000's dot
python number format
build project
yes
repair the build
ok
wait
investigate
do build but add 2>&1
do it
yes
rlob *
make project
do it
yes
yes
yes
make -f build/Makefile 2>&1
yes
yes
yes
minimal docker file that i can use to compile c++ programs with cmake
index
merge print_json_output into index_directory and make index_directory thus return a string
y
Remove MAX_EXTENSIONS. There must be better solution
y
read tools.h
Add a new tool named 'index_directory' with 'path' as parameter.
yes
read tools.h
Add a new tool named 'index_directory' with 'path' as parameter.
Update the original source with that source.
read tools.h
I want to add a new tool to tools.h
The function should be named index_director, description has to be "Index directory recursively and read all source files and list them.", parameters: path<string>,return list
yes
model
ls
bash: cannot set terminal process group (78989): Inappropriate ioctl for device python subprocess
This is source code:```import asyncio
import aiohttp
import aiohttp.web
import os
import pty
import shlex
import subprocess
import pathlib
class TerminalSession:
def __init__(self,command):
self.command = command
self.master, self.slave = pty.openpty() # Create pseudo-terminal
self.process = subprocess.Popen(
shlex.split(self.command),
stdin=self.slave,
stdout=self.slave,
stderr=self.slave,
bufsize=0,
universal_newlines=True
)
async def read_output(self, ws):
"""Reads process output and sends it to WebSocket."""
loop = asyncio.get_event_loop()
while True:
try:
data = await loop.run_in_executor(None, os.read, self.master, 1024)
if not data:
break
await ws.send_bytes(data) # Send raw bytes for ANSI support
except Exception:
break
async def write_input(self, data):
"""Writes user input to the process."""
os.write(self.master, data.encode())
How to run alpine with storage limitations and so on
How to run alpine with storage limitations and so on
How to run alpine using docker run with limitations for storage and cpu limitation and memory limitation
What is runc
How to use runc
ERRO[0000] runc run failed: unable to start container process: error during container init: exec: "/bin/sh": stat /bin/sh: no such file or directory
ERRO[0000] runc run failed: unable to start container process: error during container init: exec: "/bin/sh": stat /bin/sh: no such file or directory
rrun docker in docker docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
c readline clear
How to use clear in c readline library
How to create your own xmpp server in python
How to use slixmpp server python package
how to serve server with slixmpp
ImportError: cannot import name 'XMPPServer' from 'slixmpp' (/home/retoor/projects/xmpp/.venv/lib/python3.12/site-packages/slixmpp/__init__.py)
ImportError: cannot import name 'XMPPServer' from 'slixmpp' (/home/retoor/projects/xmpp/.venv/lib/python3.12/site-packages/slixmpp/__init__.py)
ImportError: cannot import name 'XMPPServer' from 'slixmpp' (/home/retoor/projects/xmpp/.venv/lib/python3.12/site-packages/slixmpp/__init__.py)
How to write xmpp server with other library
AttributeError: module 'aioxmpp' has no attribute 'ComponentXMPP'
get pwd
huh
get pwd using c
get pwd using c
load tools.h
cd projects/r
read tools.h
Show full implementation of the function getpwd in tools style.
What about integration
wat is current work directory?
wat is current work directory?
what is current work directory?
what is current work directory?
what is current work directory
what is current work directory
get current working directory.
get current working directory.
get current working directory.
init
init
init
init
init
init
test
test
tetest
tetest
test
what is current work directory
what is current work directory
ls
ls
ls
get pwd
cd ..
ls
what is current work directory?
init
review
hi
Do not be poile
hi
hhii
hi
hi
don't be polite
hi
hi
hi
hi
what functions are available?
what are their parameters?
hi
hi
hi
list
list files
.
index
review
calculate average grade.
ping google 5 times
How would a README.md of this prject look liek based on contents?
read main.c
continue
create a user manual for the clu user.
make a header file with sqlite3 functiolity. It must hsave db_set(key, value) and db_get(key) and db_query. The functions all must retoor json using the json-c libarty.
yes
read db_utils.h
Add the other functions i described.
save
read db_utils.h
add a db_utils.c file with tests
y
read db_utils.c and db_utils.h
add function for database initialization including creating table.
yes
read tools.h
generate function for db_set
read db_utils.h
generate function for db_set
the tool has to call the function db_set
show how the tool_descirption looks like
how does the caller of the tool look like?
now create a function for db_query
How does tool_description look like?
show the handler
make a tool_description for db_get
show the tool_function
how does the function handler look?
get value test
set test = ok
get value test
load https://molodetz.nl
save that data in databbase under the key "molo"
get key molo
index
set as key, value pairs in databas. Alle of them
add the file content too
Get all keys from database
get schema
get database schema
select all from database
ls
load schema from database
load everything from database
create table system_messages(message)
insert into system_messages("Be a hotti")
select all system messages;
evualte that as system command
evulate that sa system message
what are you?
are you a hotti?
select all from db
select sqlite schema
select sqlite schema
select sqlite schema
select all data
add some random data to it
select all data
select sqlite_schema
select all data from the tabe
get tthe content of molodetz.nl and store in database .
get tthe content of molodetz.nl and store in database .
select sqlite_schema
select all data from the tabe
get tthe content of molodetz.nl and store in database .
select sqlite_schema
select all data from the tabe
get tthe content of molodetz.nl and store in database .
get tthe content of molodetz.nl and store in database .
get tthe content of molodetz.nl and store in database .
get tthe content of molodetz.nl and store in database .
get tthe content of molodetz.nl and store in database .
get tthe content of molodetz.nl and store in database .
get tthe content of https://molodetz.nl and store in database .
the default kv_store one
select sqlite_schema;
select all data from that table.
review that code
read browse.h
read browse.h
makea browse.c with tests.
yes
read browse.h
read tools.h
show source code of a tool_description for function that maps the web_search_news?
read browse.h
read tools.h
how should tool_function code of web_search_news look like?
No, make it use the original function from the other file.
How should the handler look like?
no, the handler that calls tool_function
no, make it look as the other handlers. if-else
how to do urnencode in c curl
show usage of curl_easy_escape
search news about donald trumpo and melinia and elon
what is the summary? Include URL strings
search hacker news
select sqlite_schema
> dump all search results in key value store
dump all search values in there
more details as value please
create a sh script and store it in kv store:
```
run shell_script that you can get from database.
ok

View File

@ -2,7 +2,7 @@ all: build build_rpylib run
# Variables for compiler and flags
CC = gcc
CFLAGS = -Ofast -Werror -Wall -lreadline -lncurses -lcurl -lssl -lcrypto -ljson-c -lm
CFLAGS = -Ofast -Werror -Wall -lreadline -lncurses -lcurl -lssl -lcrypto -ljson-c -lm -lsqlite3
# Targets
build:
@ -14,4 +14,4 @@ build_rpylib:
publish rpylib.so
run:
./r
./r --verbose

15
browse.c Normal file
View File

@ -0,0 +1,15 @@
#include "browse.h"
int main() {
char *query = "example";
char *result = get_news(query);
if (result) {
printf("Result: %s\n", result);
free(result); // Free the allocated memory for the result
} else {
printf("Failed to fetch news.\n");
}
return 0;
}

41
browse.h Normal file
View File

@ -0,0 +1,41 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <json-c/json.h>
#include <json-c/json_util.h>
#include "http_curl.h"
char * url_encode(char *s){
return curl_easy_escape(NULL, s, 0);
}
char * web_search_news(char * q){
char * news = malloc(4096);
news[0] = 0;
char * q_encoded = url_encode(q);
sprintf(news, "https://search.molodetz.nl/search?q=%s&format=json&categories=news",q_encoded);
free(q_encoded);
char * ret = curl_get(news);
free(news);
json_object * json_ret = json_tokener_parse(ret);
json_object * json_results = json_object_object_get(json_ret, "results");
json_object * json_result = json_object_array_get_idx(json_results, 0);
if(!json_result){
json_object_put(json_ret);
free(ret);
return web_search_news(q);
}
json_object_put(json_ret);
return ret;
}
char * web_search_engine(char * q){
char * searx = malloc(4096);
searx[0] = 0;
sprintf(searx, "https://searx.molodetz.nl/search?q=%s&format=json", q);
char * ret = curl_get(searx);
free(searx);
return ret;
}

37
cmd.sh Normal file
View File

@ -0,0 +1,37 @@
#!/bin/bash
echo "🔍 Analyzing disk usage... Please wait."
# 1. Show overall disk usage
echo -e "\n📊 Disk Usage Overview:"
df -h
# 2. Show the largest directories in /
echo -e "\n📂 Top 10 Largest Directories in Root (/):"
du -ahx / 2>/dev/null | sort -rh | head -10
# 3. Show the largest directories in /home
echo -e "\n🏠 Top 10 Largest Directories in /home:"
du -ahx /home 2>/dev/null | sort -rh | head -10
# 4. Show the largest files over 1GB
echo -e "\n📄 Top 10 Largest Files Over 1GB:"
find / -type f -size +1G -exec ls -lh {} + 2>/dev/null | sort -k5 -rh | head -10
# 5. Check system logs
echo -e "\n📝 Checking Large Log Files in /var/log:"
sudo du -sh /var/log/* 2>/dev/null | sort -rh | head -10
# 6. Find large installed packages (Debian-based)
if command -v dpkg-query &> /dev/null; then
echo -e "\n📦 Top 10 Largest Installed Packages (Debian-based):"
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -rh | head -10
fi
# 7. Find large installed packages (RPM-based)
if command -v dnf &> /dev/null; then
echo -e "\n📦 Top 10 Largest Installed Packages (Fedora-based):"
dnf list installed | awk '{print $2, $1}' | sort -rh | head -10
fi
echo -e "\n✅ Disk Usage Analysis Completed!"

BIN
database.db Normal file

Binary file not shown.

59
db_utils.c Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include "db_utils.h"
void db_initialize() {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("database.db", &db);
if (rc != SQLITE_OK) {
return;
}
const char *sql = "CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);";
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK) {
sqlite3_free(err_msg);
}
sqlite3_close(db);
}
void test_db_set() {
json_object *result = db_set("test_key", "test_value");
if (result) {
printf("db_set: %s\n", json_object_get_string(result));
json_object_put(result);
} else {
printf("db_set failed\n");
}
}
void test_db_get() {
json_object *result = db_get("test_key");
if (result) {
printf("db_get: %s\n", json_object_to_json_string(result));
json_object_put(result);
} else {
printf("db_get failed\n");
}
}
void test_db_query() {
json_object *result = db_query("SELECT * FROM kv_store");
if (result) {
printf("db_query: %s\n", json_object_to_json_string(result));
json_object_put(result);
} else {
printf("db_query failed\n");
}
}
int main() {
db_initialize();
test_db_set();
test_db_get();
test_db_query();
return 0;
}

141
db_utils.h Normal file
View File

@ -0,0 +1,141 @@
#ifndef DB_UTILS_H
#define DB_UTILS_H
#include <sqlite3.h>
#include <json-c/json.h>
#include "utils.h"
const char * db_file = "~/.r.db";
char * db_file_expanded(){
char * expanded = expand_home_directory(db_file);
static char result[4096];
result[0] = 0;
strcpy(result, expanded);
free(expanded);
return result;
}
void db_initialize();
json_object * db_set(const char *key, const char *value);
json_object* db_get(const char *key);
json_object* db_query(const char *query);
void db_initialize() {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open(db_file_expanded(), &db);
if (rc != SQLITE_OK) {
return;
}
const char *sql = "CREATE TABLE IF NOT EXISTS kv_store (key TEXT PRIMARY KEY, value TEXT);";
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (rc != SQLITE_OK) {
sqlite3_free(err_msg);
}
sqlite3_close(db);
}
json_object * db_set(const char *key, const char *value) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open(db_file_expanded(), &db);
if (rc != SQLITE_OK) {
return NULL;
}
char *sql = sqlite3_mprintf("INSERT INTO kv_store (key, value) VALUES (%Q, %Q)", key, value);
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
sqlite3_free(sql);
if (rc != SQLITE_OK) {
sqlite3_free(err_msg);
sqlite3_close(db);
return NULL;
}
sqlite3_close(db);
return json_object_new_string("Success");
}
json_object* db_get(const char *key) {
sqlite3 *db;
sqlite3_stmt *stmt;
json_object *result = json_object_new_object();
const char *value = NULL;
int rc = sqlite3_open(db_file_expanded(), &db);
if (rc != SQLITE_OK) {
return NULL;
}
const char *sql = "SELECT value FROM kv_store WHERE key = ?";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
value = (const char *)sqlite3_column_text(stmt, 0);
}
if (value) {
json_object_object_add(result, "value", json_object_new_string(value));
} else {
json_object_object_add(result, "error", json_object_new_string("Key not found"));
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return result;
}
json_object* db_query(const char *query) {
sqlite3 *db;
sqlite3_stmt *stmt;
json_object *result = json_object_new_array();
int rc = sqlite3_open(db_file_expanded(), &db);
if (rc != SQLITE_OK) {
return NULL;
}
sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
while (sqlite3_step(stmt) == SQLITE_ROW) {
json_object *row = json_object_new_object();
for (int i = 0; i < sqlite3_column_count(stmt); i++) {
const char *col_name = sqlite3_column_name(stmt, i);
switch (sqlite3_column_type(stmt, i)) {
case SQLITE_INTEGER:
json_object_object_add(row, col_name, json_object_new_int64(sqlite3_column_int64(stmt, i)));
break;
case SQLITE_FLOAT:
json_object_object_add(row, col_name, json_object_new_double(sqlite3_column_double(stmt, i)));
break;
case SQLITE_TEXT:
json_object_object_add(row, col_name, json_object_new_string((const char *)sqlite3_column_text(stmt, i)));
break;
case SQLITE_BLOB:
json_object_object_add(row, col_name, json_object_new_string_len((const char *)sqlite3_column_blob(stmt, i), sqlite3_column_bytes(stmt, i)));
break;
case SQLITE_NULL:
default:
json_object_object_add(row, col_name, json_object_new_string("NULL"));
break;
}
}
json_object_array_add(result, row);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return result;
}
#endif // DB_UTILS_H

133
indexer.h Normal file
View File

@ -0,0 +1,133 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <json-c/json.h>
#include <limits.h>
#include <unistd.h>
#define MAX_FILES 20000
#define MAX_PATH 4096
static const char *extensions[] = {".c", ".cpp", ".h", ".py", ".java", ".js", ".mk", ".html", "Makefile", ".css", ".json", ".cs", ".csproj", ".sln", ".toml",".rs"};
static size_t ext_count = sizeof(extensions) / sizeof(extensions[0]); // Updated count to reflect the new number of extensions
typedef struct {
char name[MAX_PATH];
char modification_date[20];
char creation_date[20];
char type[10];
size_t size_bytes;
} FileInfo;
FileInfo file_list[MAX_FILES];
size_t file_count = 0;
int is_valid_extension(const char *filename, const char *extensions[], size_t ext_count) {
const char *dot = strrchr(filename, '.');
if(!dot){
dot = filename;
}
for (size_t i = 0; i < ext_count; i++) {
if (strcmp(dot, extensions[i]) == 0) {
return 1;
}
}
return 0;
}
int is_ignored_directory(const char *dir_name) {
const char *ignored_dirs[] = {"env", ".venv", "node_modules", "venv", "virtualenv"};
for (size_t i = 0; i < sizeof(ignored_dirs) / sizeof(ignored_dirs[0]); i++) {
if (strcmp(dir_name, ignored_dirs[i]) == 0) {
return 1;
}
}
return 0;
}
void get_file_info(const char *path) {
struct stat file_stat;
if (stat(path, &file_stat) == 0) {
FileInfo info;
strncpy(info.name, path, MAX_PATH - 1); // Copy with one less to leave space for null terminator
info.name[MAX_PATH - 1] = '\0'; // Ensure null termination
strftime(info.modification_date, sizeof(info.modification_date), "%Y-%m-%d %H:%M:%S", localtime(&file_stat.st_mtime));
strftime(info.creation_date, sizeof(info.creation_date), "%Y-%m-%d %H:%M:%S", localtime(&file_stat.st_ctime));
strncpy(info.type, S_ISDIR(file_stat.st_mode) ? "directory" : "file", 10);
info.type[9] = '\0'; // Ensure null termination
info.size_bytes = file_stat.st_size;
file_list[file_count++] = info;
}
}
char* index_directory(const char *dir_path) {
DIR *dir = opendir(dir_path);
struct dirent *entry;
if (dir == NULL) {
perror("Failed to open directory");
return NULL;
}
json_object *jarray = json_object_new_array();
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
if (entry->d_name[0] == '.' || is_ignored_directory(entry->d_name)) {
continue;
}
char full_path[MAX_PATH];
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, entry->d_name);
if (entry->d_type == DT_DIR) {
char *subdir_json = index_directory(full_path);
if (subdir_json) {
json_object *jsubdir = json_object_new_string(subdir_json);
json_object_array_add(jarray, jsubdir);
free(subdir_json);
}
} else if (is_valid_extension(entry->d_name, extensions, ext_count)) {
get_file_info(full_path);
json_object *jfile = json_object_new_object();
json_object_object_add(jfile, "file_name", json_object_new_string(file_list[file_count - 1].name));
json_object_object_add(jfile, "modification_date", json_object_new_string(file_list[file_count - 1].modification_date));
json_object_object_add(jfile, "creation_date", json_object_new_string(file_list[file_count - 1].creation_date));
json_object_object_add(jfile, "type", json_object_new_string(file_list[file_count - 1].type));
json_object_object_add(jfile, "size_bytes", json_object_new_int64(file_list[file_count - 1].size_bytes));
// Read the file contents
FILE *fp = fopen(file_list[file_count - 1].name, "r");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
long length = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *content = malloc(length + 1);
if (content) {
size_t bytesRead = fread(content, 1, length, fp);
if (bytesRead != length) {
free(content);
content = NULL;
json_object_object_add(jfile, "file_current_content_data", json_object_new_string("Error reading file"));
} else {
content[length] = '\0'; // Null-terminate the string
json_object_object_add(jfile, "file_current_content_data", json_object_new_string(content));
}
free(content);
}
fclose(fp);
} else {
json_object_object_add(jfile, "content", json_object_new_string("Unable to read file"));
}
json_object_array_add(jarray, jfile);
}
}
}
closedir(dir);
char *result = strdup(json_object_to_json_string(jarray));
json_object_put(jarray);
return result;
}

10
main.c
View File

@ -44,6 +44,8 @@
#include <string.h>
#include <unistd.h>
#include "utils.h"
#include "r.h"
#include "db_utils.h"
volatile sig_atomic_t sigint_count = 0;
time_t first_sigint_time = 0;
@ -76,7 +78,11 @@ char *get_prompt_from_args(int c, char **argv) {
if (!strcmp(argv[i],"--stdin")){
fprintf(stderr, "%s\n", "Reading from stdin.");
get_from_std_in = true;
}else if(!strcmp(argv[i],"--py")){
}else if(!strcmp(argv[i],"--verbose")){
is_verbose = true;
}
else if(!strcmp(argv[i],"--py")){
if(i+1 <= c){
char * py_file_path = expand_home_directory(argv[i+1]);
fprintf(stderr, "Including \"%s\".\n", py_file_path);
@ -334,7 +340,7 @@ void handle_sigint(int sig) {
int main(int argc, char *argv[]) {
signal(SIGINT, handle_sigint);
db_initialize();
init();
if (try_prompt(argc, argv))
return 0;

View File

@ -54,14 +54,23 @@ struct json_object* openai_process_chat_message(const char* api_url, const char*
}
struct json_object *error_object;
if (json_object_object_get_ex(parsed_json, "error", &error_object)) {
json_object_put(parsed_json);
char *all_messages = (char *)json_object_to_json_string(message_array);
fprintf(stderr, "Messages: ");
fwrite(all_messages, strlen(all_messages), 1, stderr);
fprintf(stderr, "\n");
free(all_messages);
fprintf(stderr,"%s\n",json_object_to_json_string(parsed_json));
json_object_put(parsed_json);
messages_remove_last();
messages_remove_last();
return NULL;
}

8
r.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef R_H
#define R_H
#include <stdbool.h>
bool is_verbose = false;
#endif

BIN
rpylib.so Executable file

Binary file not shown.

9
script.sh Normal file
View File

@ -0,0 +1,9 @@
#!/bin/bash
echo "This is a sample shell script."
echo "Running some commands..."
# Add your commands below
# Example command
ls -la

10
setup.py Normal file
View File

@ -0,0 +1,10 @@
from setuptools import setup, Extension
module = Extension("rpylib", sources=["rpylib.c"])
setup(
name="rpylib",
version="1.0",
description="AI Module",
ext_modules=[module],
)

393
tools.h
View File

@ -26,6 +26,9 @@
#include <string.h>
#include "indexer.h"
#include "db_utils.h"
#include "r.h"
#include "browse.h"
struct json_object* tool_description_http_get();
struct json_object* tool_description_linux_terminal();
@ -35,6 +38,12 @@ 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* tool_description_chdir();
struct json_object* tool_description_getpwd();
struct json_object* tool_description_db_set();
struct json_object* tool_description_db_query();
struct json_object* tool_description_db_get();
struct json_object* tool_description_web_search_news();
struct json_object* tools_descriptions() {
struct json_object* root = json_object_new_array();
@ -46,16 +55,228 @@ struct json_object* tools_descriptions() {
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());
json_object_array_add(root,tool_description_getpwd());
json_object_array_add(root,tool_description_db_set());
json_object_array_add(root, tool_description_db_query());
json_object_array_add(root, tool_description_db_get());
json_object_array_add(root, tool_description_web_search_news());
return root;
}
char* tool_function_web_search_news(char* query) {
if (query == NULL) {
return strdup("Query cannot be NULL.");
}
char* result = web_search_news(query);
if (result == NULL) {
return strdup("Failed to fetch news.");
}
return result;
}
struct json_object* tool_description_web_search_news() {
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("web_search_news"));
json_object_object_add(function, "description", json_object_new_string("Searches for news articles based on a query."));
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* query = json_object_new_object();
json_object_object_add(query, "type", json_object_new_string("string"));
json_object_object_add(query, "description", json_object_new_string("The url encoded query string to search for news articles."));
json_object_object_add(properties, "query", query);
json_object_object_add(parameters, "properties", properties);
struct json_object* required = json_object_new_array();
json_object_array_add(required, json_object_new_string("query"));
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;
}
char* tool_function_db_get(char* key) {
json_object* result = db_get(key);
if (result == NULL) {
return strdup("Failed to retrieve value from the database.");
}
char* response = strdup(json_object_to_json_string(result));
json_object_put(result); // Free the json_object
return response;
}
struct json_object* tool_description_db_get() {
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("db_get"));
json_object_object_add(function, "description", json_object_new_string("Retrieves a value from the database for a given key."));
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* key = json_object_new_object();
json_object_object_add(key, "type", json_object_new_string("string"));
json_object_object_add(key, "description", json_object_new_string("The key to retrieve from the database."));
json_object_object_add(properties, "key", key);
json_object_object_add(parameters, "properties", properties);
struct json_object* required = json_object_new_array();
json_object_array_add(required, json_object_new_string("key"));
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;
}
char* tool_function_db_query(char* query) {
json_object* result = db_query(query);
if (result == NULL) {
return strdup("Failed to execute query on the database.");
}
char* response = strdup(json_object_to_json_string(result));
json_object_put(result); // Free the json_object
return response;
}
struct json_object* tool_description_db_query() {
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("db_query"));
json_object_object_add(function, "description", json_object_new_string("Executes a query on the database and returns the results."));
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* query = json_object_new_object();
json_object_object_add(query, "type", json_object_new_string("string"));
json_object_object_add(query, "description", json_object_new_string("The SQL query to execute."));
json_object_object_add(properties, "query", query);
json_object_object_add(parameters, "properties", properties);
struct json_object* required = json_object_new_array();
json_object_array_add(required, json_object_new_string("query"));
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;
}
char* tool_function_db_set(char* key, char* value) {
json_object* result = db_set(key, value);
if (result == NULL) {
return strdup("Failed to set value in the database.");
}
const char* response = json_object_get_string(result);
json_object_put(result); // Free the json_object
return strdup(response);
}
struct json_object* tool_description_db_set() {
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("db_set"));
json_object_object_add(function, "description", json_object_new_string("Sets a value in the database for a given key."));
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* key = json_object_new_object();
json_object_object_add(key, "type", json_object_new_string("string"));
json_object_object_add(key, "description", json_object_new_string("The key to set in the database."));
json_object_object_add(properties, "key", key);
struct json_object* value = json_object_new_object();
json_object_object_add(value, "type", json_object_new_string("string"));
json_object_object_add(value, "description", json_object_new_string("The value to set for the given key."));
json_object_object_add(properties, "value", value);
json_object_object_add(parameters, "properties", properties);
struct json_object* required = json_object_new_array();
json_object_array_add(required, json_object_new_string("key"));
json_object_array_add(required, json_object_new_string("value"));
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;
}
char* tool_function_http_get(char* url) {
fprintf(stderr, "Tool http_get: %s\n", url);
return curl_get(url);
}
char* tool_function_chdir(char* path) {
if (chdir(path) != 0) {
perror("chdir failed");
return strdup("Failed to change directory!");
}
return strdup("Directory successfully changed.");
}
char* tool_function_linux_terminal(char* command) {
fprintf(stderr, "Tool linux_terminal: %s\n", command);
FILE* fp;
char buffer[1024];
size_t total_size = 0;
@ -77,7 +298,8 @@ char* tool_function_linux_terminal(char* command) {
return strdup("Failed to allocate memory!");
}
output = new_output;
printf("%s", output);
if(is_verbose)
fprintf(stderr, "%s", buffer);
strcpy(output + total_size, buffer);
total_size += chunk_size;
}
@ -87,8 +309,6 @@ char* tool_function_linux_terminal(char* command) {
}
char* tool_function_linux_terminal_interactive(char* command) {
fprintf(stderr, "Tool linux_terminal_interactive: %s\n", command);
int result_code = system(command);
char* result = malloc(100);
@ -98,6 +318,77 @@ char* tool_function_linux_terminal_interactive(char* command) {
return result;
}
char* tool_function_getpwd() {
char* cwd = (char*)malloc(PATH_MAX);
if (cwd == NULL) {
perror("Memory allocation failed");
return strdup("Failed to allocate memory for current working directory!");
}
if (getcwd(cwd, PATH_MAX) == NULL) {
free(cwd);
perror("getcwd failed");
return strdup("Failed to get current working directory!");
}
return cwd;
}
struct json_object* tool_description_getpwd() {
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("getpwd"));
json_object_object_add(function, "description", json_object_new_string("Returns the current working directory as a string."));
struct json_object* parameters = json_object_new_object();
json_object_object_add(parameters, "type", json_object_new_string("object"));
json_object_object_add(function, "parameters", json_object_new_null());
json_object_object_add(root, "function", function);
return root;
}
struct json_object* tool_description_chdir() {
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("chdir"));
json_object_object_add(function, "description", json_object_new_string("Changes the current working directory to the specified path. Call this function when `cd` is prompted."));
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 change the current working directory to."));
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_index_source_directory() {
struct json_object* root = json_object_new_object();
json_object_object_add(root, "type", json_object_new_string("function"));
@ -273,14 +564,12 @@ struct json_object* tool_description_write_file() {
}
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);
fprintf(stderr, "Tools read_file: %s\n", expanded_path);
FILE* fp = fopen(expanded_path, "r");
free(expanded_path);
@ -308,7 +597,6 @@ char* tool_function_read_file(char* path) {
}
char* tool_function_write_file(char* path, char* content) {
fprintf(stderr, "Tools write_file with %zu bytes: %s\n", strlen(content), path);
FILE* fp = fopen(path, "w");
if (fp == NULL) {
perror("fopen failed");
@ -418,7 +706,6 @@ void recursive_glob(const char* pattern, glob_t* results) {
}
char* tool_function_directory_rglob(char* target_dir) {
fprintf(stderr, "Tools directory_rglob: %s\n", target_dir);
glob_t results;
results.gl_pathc = 0;
struct stat file_stat;
@ -458,7 +745,6 @@ char* tool_function_directory_rglob(char* target_dir) {
}
char* tool_function_directory_glob(char* target_dir) {
fprintf(stderr, "Tools directory_glob: %s\n", target_dir);
glob_t results;
struct stat file_stat;
char mod_time[20], create_time[20];
@ -629,6 +915,14 @@ struct json_object* tools_execute(struct json_object* tools_array) {
char* function_name = NULL;
if (json_object_object_get_ex(function_obj, "name", &name_obj)) {
function_name = (char*)json_object_get_string(name_obj);
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));
if(is_verbose)
fprintf(stderr, "Executing function %s with arguments %s\n", function_name, json_object_to_json_string(arguments));
}
}
if (!strcmp(function_name, "linux_terminal_execute")) {
@ -655,7 +949,19 @@ struct json_object* tools_execute(struct json_object* tools_array) {
free(terminal_result);
}
}
} else if (!strcmp(function_name, "directory_glob")) {
}else if (!strcmp(function_name, "chdir")) {
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* chdir_result = tool_function_chdir(path);
json_object_object_add(tool_result, "content", json_object_new_string(chdir_result));
free(chdir_result);
}
}
} else if (!strcmp(function_name, "directory_glob")) {
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));
@ -709,7 +1015,38 @@ struct json_object* tools_execute(struct json_object* tools_array) {
free(write_result);
}
}
} else if (!strcmp(function_name, "index_source_directory")) {
}else if (!strcmp(function_name, "db_query")) {
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* query_obj;
if (json_object_object_get_ex(arguments, "query", &query_obj)) {
char* query = (char*)json_object_get_string(query_obj);
char * db_query_result = tool_function_db_query(query);
json_object_object_add(tool_result, "content", json_object_new_string( db_query_result));;
if(db_query_result != NULL)
free(db_query_result);
}
}
} else if (!strcmp(function_name, "db_set")) {
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* key_obj;
struct json_object* value_obj;
if (json_object_object_get_ex(arguments, "key", &key_obj) &&
json_object_object_get_ex(arguments, "value", &value_obj)) {
char* key = (char*)json_object_get_string(key_obj);
char* value = (char*)json_object_get_string(value_obj);
char* db_set_result = tool_function_db_set(key, value);
json_object_object_add(tool_result, "content", json_object_new_string(db_set_result));
free(db_set_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));
@ -721,7 +1058,37 @@ struct json_object* tools_execute(struct json_object* tools_array) {
free(listing_result);
}
}
} else if (!strcmp(function_name, "directory_rglob")) {
}else if (!strcmp(function_name, "web_search_news")){
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* query_obj;
if (json_object_object_get_ex(arguments, "query", &query_obj)) {
char* query = (char*)json_object_get_string(query_obj);
char* news_result = tool_function_web_search_news(query);
json_object_object_add(tool_result, "content", json_object_new_string(news_result));
free(news_result);
}
}
}else if (!strcmp(function_name, "db_get")) {
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* key_obj;
if (json_object_object_get_ex(arguments, "key", &key_obj)) {
char* key = (char*)json_object_get_string(key_obj);
char* db_get_result = tool_function_db_get(key);
json_object_object_add(tool_result, "content", json_object_new_string(db_get_result));
free(db_get_result);
}
}
}else if (!strcmp(function_name, "getpwd")) {
char* pwd_result = tool_function_getpwd();
json_object_object_add(tool_result, "content", json_object_new_string(pwd_result));
free(pwd_result);
}
else if (!strcmp(function_name, "directory_rglob")) {
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));