/* Listings: get_available_models and get_commands, Pi's shapes, answered * through the RPC command registry (listings.h). */ #ifndef _DEFAULT_SOURCE #define _DEFAULT_SOURCE 1 #endif #include #include #include #include #include "catalog.h" #include "endpoints.h" #include "ignore.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_sql5(sqlite3 *db, const char *sql, const char *a, const char *b, const char *c, const char *d, const char *e) { sqlite3_stmt *q = NULL; const char *v[5] = {a, b, c, d, e}; char *out = NULL; if (sqlite3_prepare_v2(db, sql, -1, &q, NULL) != SQLITE_OK) { return NULL; } for (int i = 0; i < 5; 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 char *ls_sql(sqlite3 *db, const char *sql, const char *a, const char *b, const char *c, const char *d) { return ls_sql5(db, sql, a, b, c, d, NULL); } 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, *endpoint; 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 && (endpoint = ls_sql(l->db, "SELECT json_extract(?1, '$.endpoint')", json, NULL, NULL, NULL))) { char *marked = ls_sql(l->db, "SELECT json_set(?1, '$.endpoint', ?2)", obj, endpoint, NULL, NULL); /* from NAME */ free(endpoint); if (marked) { free(obj); obj = marked; } } 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, then the * saved endpoints' (marked with "endpoint"). */ 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); l.provider[0] = '\0'; (void)pic_endpoint_models_each(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; char *warning = NULL; 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_sql5(db, "SELECT json_object('name', ?1, 'description', ?2, 'source', 'prompt', 'sourceInfo', json_object('path', ?3," " 'source', 'local', 'scope', ?4, 'origin', 'top-level'), 'argumentHint', ?5)", 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", p.v[i].hint ? p.v[i].hint : ""); 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) { char *window = pic_rpc_text(c, "$.contextWindow"); if (window) { /* pic's: what the skill listing's budget cuts at this window */ char *listing = pic_skills_prompt(&k, strtol(window, NULL, 10), &warning); free(listing); free(window); } 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, "]"); if (warning) { sqlite3_str_appendall(t, ",\"skillsWarning\":"); pic_rpc_quote(t, warning); free(warning); } 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}; /* ---- get_providers --------------------------------------------------------- */ static int ls_provider_row(void *ctx, const char *provider) { sqlite3_str *t = ctx; if (sqlite3_str_length(t) > 14) { sqlite3_str_appendall(t, ","); } pic_rpc_quote(t, provider); return 0; } /* pic's: every provider pic knows, built-in ones first, then the models file's. */ static int ls_providers_answer(pic_rpc_call *c) { sqlite3_str *t = sqlite3_str_new(NULL); char *res; sqlite3_str_appendall(t, "{\"providers\":["); (void)pic_providers_each(ls_provider_row, t); sqlite3_str_appendall(t, "]}"); res = sqlite3_str_finish(t); c->data = res ? strdup(res) : NULL; sqlite3_free(res); return c->data ? 0 : -1; } const pic_rpc_command pic_providers_rpc = {"get_providers", "every provider pic knows", PIC_RPC_JOURNAL_FREE | PIC_RPC_IMMEDIATE, ls_providers_answer}; /* ---- get_context_files ------------------------------------------------------ */ /* pic's: the instruction files every request reads (AGENTS.md, CLAUDE.md), by path. */ static int ls_context_answer(pic_rpc_call *c) { char *ctx = pic_project_instructions(c->workspace), *res; static const char tag[] = "= sizeof path) { break; } (void)snprintf(path, sizeof path, "%.*s", (int)(end - from), from); sqlite3_str_appendall(t, first ? "" : ","); pic_rpc_quote(t, path); first = 0; } sqlite3_str_appendall(t, "]}"); free(ctx); res = sqlite3_str_finish(t); c->data = res ? strdup(res) : NULL; sqlite3_free(res); return c->data ? 0 : -1; } const pic_rpc_command pic_context_rpc = {"get_context_files", "the instruction files every request reads", PIC_RPC_JOURNAL_FREE | PIC_RPC_IMMEDIATE, ls_context_answer}; /* ---- list_files ------------------------------------------------------------- */ /* pic's: a folder's entries, breadth-first, ignored files * included (ignore.c's walk, which skips only version-control metadata): "dir" (the workspace when absent), at most * "maxEntries" (20000) entries and "maxDepth" (8) folders deep. Each entry is * [path from dir, is a folder, depth]. */ static int ls_files_answer(pic_rpc_call *c) { char *dir = pic_rpc_text(c, "$.dir"), *me = pic_rpc_text(c, "$.maxEntries"), *md = pic_rpc_text(c, "$.maxDepth"), *res; long max_entries = me ? strtol(me, NULL, 10) : 20000, max_depth = md ? strtol(md, NULL, 10) : 8; pic_ignore_entry *v = NULL; size_t n = pic_ignore_walk(dir ? dir : c->workspace, max_entries > 0 ? (size_t)max_entries : 0, max_depth > 0 && max_depth < 64 ? (int)max_depth : 8, &v); sqlite3_str *t = sqlite3_str_new(NULL); sqlite3_str_appendall(t, "{\"entries\":["); for (size_t i = 0; i < n; i++) { sqlite3_str_appendall(t, i ? ",[" : "["); pic_rpc_quote(t, v[i].path); sqlite3_str_appendf(t, ",%s,%d]", v[i].is_dir ? "true" : "false", v[i].depth); } sqlite3_str_appendall(t, "]}"); pic_ignore_entries_free(v, n); free(dir); free(me); free(md); res = sqlite3_str_finish(t); c->data = res ? strdup(res) : NULL; sqlite3_free(res); return c->data ? 0 : -1; } const pic_rpc_command pic_files_rpc = {"list_files", "a folder's files and folders, ignored files too", PIC_RPC_JOURNAL_FREE | PIC_RPC_IMMEDIATE, ls_files_answer};