/* Listings: get_available_models and get_commands, Pi's shapes, answered
* through the RPC command registry (listings.h). */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "catalog.h"
#include "listings.h"
#include "models.h"
#include "prompts.h"
#include "skills.h"
#include "userdb.h"
/* SQL's first column with up to four text values bound (NULL: null), malloc'd. */
static char *ls_sql(sqlite3 *db, const char *sql, const char *a, const char *b, const char *c, const char *d) {
sqlite3_stmt *q = NULL;
const char *v[4] = {a, b, c, d};
char *out = NULL;
if (sqlite3_prepare_v2(db, sql, -1, &q, NULL) != SQLITE_OK) {
return NULL;
}
for (int i = 0; i < 4; i++) {
if (v[i]) {
sqlite3_bind_text(q, i + 1, v[i], -1, SQLITE_STATIC);
}
}
if (sqlite3_step(q) == SQLITE_ROW && sqlite3_column_text(q, 0)) {
out = strdup((const char *)sqlite3_column_text(q, 0));
}
sqlite3_finalize(q);
return out;
}
static int ls_open(sqlite3 **db, pic_rpc_call *c) {
if (sqlite3_open(":memory:", db) != SQLITE_OK) {
sqlite3_close(*db);
(void)snprintf(c->err, sizeof c->err, "cannot open an in-memory database");
return -1;
}
return 0;
}
/* ---- get_available_models -------------------------------------------------- */
typedef struct {
sqlite3 *db;
char **names, **objects;
size_t n;
char provider[64];
int available;
} ls_models;
static int ls_collect(void *ctx, const char *provider, const char *json) {
ls_models *l = ctx;
char *id = ls_sql(l->db, "SELECT json_extract(?1, '$.id')", json, NULL, NULL, NULL);
char *display = ls_sql(l->db, "SELECT json_extract(?1, '$.name')", json, NULL, NULL, NULL), name[320], *obj;
if (strcmp(provider, l->provider) != 0) {
(void)snprintf(l->provider, sizeof l->provider, "%s", provider);
l->available = pic_model_available(provider);
}
if (!id || !l->available) {
free(id);
free(display);
return 0;
}
(void)snprintf(name, sizeof name, "%s/%s", provider, id);
for (size_t i = 0; i < l->n; i++) {
if (strcmp(l->names[i], name) == 0) { /* the models file's entry replaces the catalog's */
obj = pic_rpc_model_json(name, display);
if (obj) {
free(l->objects[i]);
l->objects[i] = obj;
}
free(id);
free(display);
return 0;
}
}
obj = pic_rpc_model_json(name, display);
if (obj) {
char **nv = realloc(l->names, (l->n + 1) * sizeof *nv), **ov;
if (nv) {
l->names = nv;
ov = realloc(l->objects, (l->n + 1) * sizeof *ov);
if (ov) {
l->objects = ov;
l->names[l->n] = strdup(name);
l->objects[l->n++] = obj;
obj = NULL;
}
}
free(obj);
}
free(id);
free(display);
return 0;
}
/* Pi's available models: the catalog's and the models file's whose provider has a key. */
static int ls_models_answer(pic_rpc_call *c) {
ls_models l;
sqlite3_str *t;
char *res;
memset(&l, 0, sizeof l);
if (ls_open(&l.db, c) != 0) {
return -1;
}
(void)pic_catalog_each(ls_collect, &l);
l.provider[0] = '\0';
(void)pic_models_each_user(ls_collect, &l);
t = sqlite3_str_new(NULL);
sqlite3_str_appendall(t, "{\"models\":[");
for (size_t i = 0; i < l.n; i++) {
sqlite3_str_appendf(t, "%s%s", i ? "," : "", l.objects[i]);
free(l.names[i]);
free(l.objects[i]);
}
sqlite3_str_appendall(t, "]}");
res = sqlite3_str_finish(t);
c->data = strdup(res ? res : "{\"models\":[]}");
sqlite3_free(res);
free(l.names);
free(l.objects);
sqlite3_close(l.db);
return 0;
}
const pic_rpc_command pic_models_rpc = {"get_available_models", "the models whose provider has a key",
PIC_RPC_JOURNAL_FREE | PIC_RPC_IMMEDIATE, ls_models_answer};
/* ---- get_commands ---------------------------------------------------------- */
/* Pi's get_commands: prompt templates and skills (pic has no extensions). */
static int ls_commands_answer(pic_rpc_call *c) {
pic_prompts p;
pic_skills k;
char real[PATH_MAX];
const char *ws = realpath(c->workspace, real) ? real : c->workspace;
int trusted = pic_trust_get(ws) == 1;
sqlite3 *db;
sqlite3_str *t;
int first = 1;
char *res;
if (ls_open(&db, c) != 0) {
return -1;
}
t = sqlite3_str_new(NULL);
sqlite3_str_appendall(t, "{\"commands\":[");
if (pic_prompts_load(&p, c->workspace, trusted) == 0) {
for (size_t i = 0; i < p.n; i++) {
char *o = ls_sql(db,
"SELECT json_object('name', ?1, 'description', ?2, 'source', 'prompt', 'sourceInfo', json_object('path', ?3,"
" 'source', 'local', 'scope', ?4, 'origin', 'top-level'))",
p.v[i].name, p.v[i].description ? p.v[i].description : "", p.v[i].path ? p.v[i].path : "",
p.v[i].project ? "project" : "user");
if (o) {
sqlite3_str_appendf(t, "%s%s", first ? "" : ",", o);
first = 0;
}
free(o);
}
pic_prompts_free(&p);
}
if (pic_skills_load(&k, c->workspace, trusted, NULL) == 0) {
for (size_t i = 0; i < k.n; i++) {
char name[320], *o;
(void)snprintf(name, sizeof name, "skill:%s", k.v[i].name);
o = ls_sql(db,
"SELECT json_object('name', ?1, 'description', ?2, 'source', 'skill', 'sourceInfo', json_object('path', ?3,"
" 'source', 'local', 'scope', ?4, 'origin', 'top-level'))",
name, k.v[i].description ? k.v[i].description : "", k.v[i].path ? k.v[i].path : "",
k.v[i].project ? "project" : "user");
if (o) {
sqlite3_str_appendf(t, "%s%s", first ? "" : ",", o);
first = 0;
}
free(o);
}
pic_skills_free(&k);
}
sqlite3_str_appendall(t, "]}");
res = sqlite3_str_finish(t);
c->data = strdup(res ? res : "{\"commands\":[]}");
sqlite3_free(res);
sqlite3_close(db);
return 0;
}
const pic_rpc_command pic_commands_rpc = {"get_commands", "the prompt templates and skills a prompt can name",
PIC_RPC_JOURNAL_FREE | PIC_RPC_IMMEDIATE, ls_commands_answer};