ref: ec0edc79ed72540533a156f825262def93198a4d
parent: ac96e27a0b379e3b01508ca2573b37760558a365
author: sirjofri <sirjofri@sirjofri.de>
date: Tue Feb 24 15:11:21 EST 2026
allows file listing of other folders
--- a/oai.c
+++ b/oai.c
@@ -60,14 +60,26 @@
int n, i, fd;
Dir *dirbuf;
char *s;
+ JSON *j, *jf;
if (!allowed(tc))
return abortcall(tc);
- fd = open(".", OREAD);+ j = jsonparse(tc.arguments);
+ jf = jsonbyname(j, "folder");
+ s = jsonstr(jf);
+ if (!(s && s[0])) {+ fprint(2, "list_files: invalid folder!");
+ jsonfree(j);
+ return strdup("list_files: invalid folder");+ }
+
+ fd = open(s, OREAD);
if (fd < 0)
return strdup("");+ jsonfree(j);
+
n = dirreadall(fd, &dirbuf);
close(fd);
if (n < 0)
@@ -83,6 +95,15 @@
return s;
}
+static char* listfilesdesc = "list all files in the specified directory, similar to `ls` command. Paths are relative to the current working directory. Use `.` for the current directory.";
+static char* listfilesargs = "{"+" \"type\": \"object\","
+" \"properties\": {"+" \"folder\": { \"type\": \"string\" }"+" },"
+" \"required\": [ \"folder\" ]"
+"}";
+
static char*
read_file(OToolcall toolcall, void*)
{@@ -98,6 +119,7 @@
fj = jsonbyname(j, "file");
if (!fj) {fprint(2, "no file in read_file request!\n");
+ jsonfree(j);
return strdup("bad request in read_file!\n");}
@@ -104,6 +126,7 @@
file = jsonstr(fj);
if (!(file && file[0])) {fprint(2, "invalid file in read_file request!\n");
+ jsonfree(j);
return strdup("bad request in read_file: no file!\n");}
@@ -121,6 +144,7 @@
return s;
}
+static char *readfiledesc = "read in a specific file, similar to the `cat` unix command.";
static char *readfileargs = "{"" \"type\": \"object\","
" \"properties\": {"@@ -127,7 +151,8 @@
" \"file\": {"" \"type\": \"string\""
" }"
-" }"
+" },"
+" \"required\": [ \"file\" ]"
"}";
OTool *tools = nil;
@@ -135,8 +160,8 @@
static void
inittools(void)
{- tools = maketool(tools, Function, "list_files", "list all files in the current directory, similar to `ls` on unix systems.", nil, list_files, nil);
- maketool(tools, Function, "read_file", "read in a specific file, similar to the `cat` unix command.", readfileargs, read_file, nil);
+ tools = maketool(tools, Function, "list_files", listfilesdesc, listfilesargs, list_files, nil);
+ maketool(tools, Function, "read_file", readfiledesc, readfileargs, read_file, nil);
}
#define COMMONPROMPT "When writing code or text, you are serious and helpful. Your replies are NOT formatted as markdown. You DO NOT make a lot of words."
--- a/oailib.c
+++ b/oailib.c
@@ -15,6 +15,9 @@
JSON *out;
char *s;
+ if (!in)
+ return nil;
+
s = smprint("%J", in);out = jsonparse(s);
free(s);
@@ -468,7 +471,6 @@
OResult ret;
JSON *jreq;
JSON *jres;
- OPrompt *pr;
jreq = req2json(req);
--
⑨