shithub: oai

Download patch

ref: 41fac5b8618b693401ef07d24e4031060526b84c
parent: ec0edc79ed72540533a156f825262def93198a4d
author: sirjofri <sirjofri@sirjofri.de>
date: Tue Feb 24 15:41:43 EST 2026

adds meta console with read command

--- a/oai.c
+++ b/oai.c
@@ -164,6 +164,69 @@
 	maketool(tools, Function, "read_file", readfiledesc, readfileargs, read_file, nil);
 }
 
+static char*
+callfunc(char *s)
+{
+	int pin[2];
+	Biobuf *bin;
+	
+	pipe(pin);
+	
+	switch (fork()) {
+	default:
+		/* parent */
+		break;
+	case 0:
+		/* child */
+		dup(pin[1], 1);
+		close(pin[1]);
+		close(pin[0]);
+		execl("/bin/rc", "rc", "-c", s, nil);
+		break;
+	case -1:
+		sysfatal("fork: %r");
+	}
+	
+	close(pin[1]);
+	bin = Bfdopen(pin[0], OREAD);
+	s = Brdstr(bin, 0, 0);
+	Bterm(bin);
+	close(pin[0]);
+	wait();
+	return s;
+}
+
+static char*
+readconsole(Biobuf *bin)
+{
+	char *s, *result;
+	
+	print(">>> ");
+	while (s = Brdstr(bin, '\n', 1)) {
+		if (!s[0]) {
+			free(s);
+			print(">>> ");
+			continue;
+		}
+		switch (s[0]) {
+		case '':
+			free(s);
+			return nil;
+		case '!':
+			result = callfunc(s+1);
+			free(s);
+			return result;
+		default:
+			result = callfunc(s);
+			free(s);
+			print("%s", result);
+			free(result);
+			print(">>> ");
+		}
+	}
+	return 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."
 
 char *plan9prompt = "You are a helpful AI assistant on a Plan 9 system. Your name is Glenda. Your tone is serious. Be friendly and concise. " COMMONPROMPT;
@@ -241,6 +304,14 @@
 	
 	if (!quiet) print("user: ");
 	while (s = Brdstr(bin, '\n', 1)) {
+		if (s[0] == '') {
+			free(s);
+			s = readconsole(bin);
+		}
+		if (!(s && s[0])) {
+			free(s);
+			goto Next;
+		}
 		addstrprompt(&req, "user", s);
 		res = makerequest(&req);
 		if (!res.success) {
@@ -249,6 +320,7 @@
 		}
 		print("%s%s%s\n\n", res.role, (quiet ? "" : ": "), res.message);
 		addprompt(&req, res.asprompt);
+Next:
 		if (!quiet) print("user: ");
 	}
 	exits(nil);
--