ref: 6ff6f63950fa65e7d3ca0981f572bb31ac328796
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] 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;
req.prompts = nil;
req.model = nil;
ARGBEGIN{
case 'h':
usage();
case 'k':
key = EARGF(usage());
break;
case 'm':
req.model = EARGF(usage());
break;
}ARGEND;
if (argc != 1)
usage();
winid = getenv("winid");
if (!winid || winid[0] == 0)
sysfatal("not in acme");
if (!initoai(argv[0], 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);
}