shithub: oai

Download patch

ref: 28e74ed0ea7654d07acb195574fdff3a44334310
parent: 838fda0aba94e2a6235c3c7cfe35430ed5104c06
author: sirjofri <sirjofri@sirjofri.de>
date: Fri Feb 27 17:57:49 EST 2026

fetch model from env, adds stats

--- a/oai.c
+++ b/oai.c
@@ -269,8 +269,8 @@
 {
 	tools = maketool(tools, Function, "list_files", listfilesdesc, listfilesargs, list_files, nil);
 	maketool(tools, Function, "read_file", readfiledesc, readfileargs, read_file, nil);
-	maketool(tools, Function, "lookman", lookmandesc, lookmanargs, lookman, nil);
-	maketool(tools, Function, "man", mandesc, manargs, man, nil);
+	maketool(tools, Function, "search_man", lookmandesc, lookmanargs, lookman, nil);
+	maketool(tools, Function, "read_man", mandesc, manargs, man, nil);
 }
 
 static char*
@@ -336,6 +336,12 @@
 	return nil;
 }
 
+static void
+printusage(OResult *r)
+{
+	print("usage: c=%d p=%d t=%d\n", r->tokscompletion, r->toksprompt, r->tokstotal);
+}
+
 #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. Do NOT assume this is a unix or linux system. You first plan your steps before executing them. DO NOT simulate running commands. DO NOT invent files, folders or commands. Use the provided tools to proactively increase the context. Use the tools to solve the problem. Learn about Plan 9 by reading the 0intro man page in section 1. If unsure, search the man pages."
 
 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.\n\n" COMMONPROMPT;
@@ -422,6 +428,7 @@
 		}
 		addstrprompt(&req, "user", s);
 		res = makerequest(&req);
+		printusage(&res);
 		if (!res.success) {
 			fprint(2, "exiting!\n");
 			exits("fail");
--- a/oai.h
+++ b/oai.h
@@ -31,6 +31,9 @@
 	int success;
 	char *role;
 	char *message;
+	int tokscompletion;
+	int toksprompt;
+	int tokstotal;
 	OPrompt *asprompt;
 };
 
--- a/oailib.c
+++ b/oailib.c
@@ -28,7 +28,7 @@
 }
 
 int
-initoai(char *url, char *key, char *imodel)
+initoai(char *url, char *key, char *mod)
 {
 	if (!url)
 		url = getenv("oaiurl");
@@ -42,9 +42,9 @@
 		key = getenv("oaikey");
 	apikey = key;
 	
-	if (!imodel)
-		imodel = getenv("oaimodel");
-	model = imodel;
+	if (!mod)
+		mod = getenv("oaimodel");
+	model = mod;
 	
 	JSONfmtinstall();
 	
@@ -241,6 +241,36 @@
 	return message;
 }
 
+static void
+fillstats(JSON *j, OResult *r)
+{
+	JSON *usage;
+	JSON *tcompletion;
+	JSON *tprompt;
+	JSON *ttotal;
+	
+	r->tokscompletion = -1;
+	r->toksprompt = -1;
+	r->tokstotal = -1;
+	
+	usage = jsonbyname(j, "usage");
+	if (!usage)
+		return;
+	if (usage->t != JSONObject)
+		return;
+	
+	tcompletion = jsonbyname(usage, "completion_tokens");
+	tprompt = jsonbyname(usage, "prompt_tokens");
+	ttotal = jsonbyname(usage, "total_tokens");
+	
+	if (tcompletion && tcompletion->t == JSONNumber)
+		r->tokscompletion = tcompletion->n;
+	if (tprompt && tprompt->t == JSONNumber)
+		r->toksprompt = tprompt->n;
+	if (ttotal && ttotal->t == JSONNumber)
+		r->toksprompt = tprompt->n;
+}
+
 static OResult
 j2res(JSON *j)
 {
@@ -249,6 +279,8 @@
 	JSON *message;
 	JSON *role;
 	JSON *content;
+	
+	fillstats(j, &r);
 	
 	choices = jsonbyname(j, "choices");
 	
--