ref: c30d74014c8bc587f00ef0d178d1a4f16b448035
dir: /ocomplete.c/
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "oai.h"
static void
usage(void)
{
fprint(2, "usage: %s [-k apikey] [-m model] [-u baseurl]\n", argv0);
exits("usage");
}
void
main(int argc, char **argv)
{
char buf[128];
ORequest req;
OResult res;
char *key = nil;
char *body, *rdsel;
Biobuf *bodyin, *selio;
char *winid;
char *url = nil;
req.prompts = nil;
req.model = nil;
ARGBEGIN{
case 'h':
usage();
case 'k':
key = EARGF(usage());
break;
case 'm':
req.model = EARGF(usage());
break;
case 'u':
url = EARGF(usage());
break;
}ARGEND;
winid = getenv("winid");
if (!winid || winid[0] == 0)
sysfatal("not in acme");
if (!initoai(url, key))
sysfatal("initoai: %r");
snprint(buf, sizeof buf, "/mnt/acme/%s/body", winid);
bodyin = Bopen(buf, OREAD);
snprint(buf, sizeof buf, "/mnt/acme/%s/rdsel", winid);
selio = Bopen(buf, OREAD);
if (!(bodyin && selio))
sysfatal("error opening files: %r");
body = Brdstr(bodyin, 0, 1);
rdsel = Brdstr(selio, 0, 1);
Bterm(bodyin);
Bterm(selio);
if (!(body && rdsel))
sysfatal("read: %r");
addprompt(&req, "system", "You are the Acme completion assistant. Your task is to append matching content to the user prompt, based on the context of the given file. The user prompt is part of the file contents, and your response will be appended there. No further discussion, just this completion. Do not repeat the user prompt. The file contents are:\n\n%s", body);
addprompt(&req, "user", rdsel);
res = makerequest(req);
if (!res.message)
sysfatal("invalid result");
snprint(buf, sizeof buf, "/mnt/acme/%s/wrsel", winid);
selio = Bopen(buf, OWRITE);
Bprint(selio, "%s%s", rdsel, res.message);
Bterm(selio);
exits(nil);
}