/* 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 <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "catalog.h"
#include "endpoints.h"
#include "ignore.h"
#include "listings.h"
#include "mcp.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 ---------------------------------------------------------- */
/* The most bytes of a command's name, description or hint get_commands sends: a project's file
* writes them, and a phone or an RPC client shows them. */
#define LS_TEXT_MAX 200
/* IN for a listing, in OUT (LS_TEXT_MAX + 1 bytes): tab, newline and return become a space; other
* C0 controls, DEL, C1 controls (U+0080 to U+009F) and the bidi controls (U+061C, U+200E, U+200F,
* U+202A to U+202E, U+2066 to U+2069) are left out, and a byte that is not UTF-8 becomes '?'; then
* it is cut on a character to LS_TEXT_MAX bytes, "…" ending what was cut. */
static const char *ls_clean(const char *in, char *out) {
const unsigned char *p = (const unsigned char *)(in ? in : "");
size_t n = 0, keep = 0; /* keep: where the text ends if the next character must make room for "…" */
while (*p) {
unsigned cp = *p;
size_t len = 1;
if (*p >= 0xC2 && *p <= 0xDF && (p[1] & 0xC0) == 0x80) {
cp = (unsigned)(*p & 0x1F) << 6 | (p[1] & 0x3F), len = 2;
} else if (*p >= 0xE0 && *p <= 0xEF && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80) {
cp = (unsigned)(*p & 0x0F) << 12 | (unsigned)(p[1] & 0x3F) << 6 | (p[2] & 0x3F), len = 3;
if (cp < 0x800 || (cp >= 0xD800 && cp <= 0xDFFF)) {
cp = '?', len = 1;
}
} else if (*p >= 0xF0 && *p <= 0xF4 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80) {
cp = (unsigned)(*p & 0x07) << 18 | (unsigned)(p[1] & 0x3F) << 12 | (unsigned)(p[2] & 0x3F) << 6 | (p[3] & 0x3F), len = 4;
if (cp < 0x10000 || cp > 0x10FFFF) {
cp = '?', len = 1;
}
} else if (*p >= 0x80) {
cp = '?'; /* not UTF-8 */
}
{
const unsigned char *from = p;
unsigned char one = (unsigned char)cp;
size_t w = len;
p += len;
if (cp == '\t' || cp == '\n' || cp == '\r') {
one = ' ', from = &one, w = 1;
} else if (cp < 0x20 || cp == 0x7F || (cp >= 0x80 && cp <= 0x9F) || cp == 0x61C || cp == 0x200E || cp == 0x200F ||
(cp >= 0x202A && cp <= 0x202E) || (cp >= 0x2066 && cp <= 0x2069)) {
continue;
} else if (cp == '?' && len == 1) {
from = &one;
}
if (n + w > LS_TEXT_MAX) { /* cut: the ellipsis after the last character that leaves room */
(void)memcpy(out + keep, "\xe2\x80\xa6", 3);
out[keep + 3] = '\0';
return out;
}
(void)memcpy(out + n, from, w);
n += w;
if (n + 3 <= LS_TEXT_MAX) {
keep = n;
}
}
}
out[n] = '\0';
return out;
}
/* Pi's get_commands: prompt templates and skills (pic has no extensions), and the MCP servers'
* prompts as Claude Code offers them. */
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 nm[LS_TEXT_MAX + 1], de[LS_TEXT_MAX + 1], hi[LS_TEXT_MAX + 1];
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)",
ls_clean(p.v[i].name, nm), ls_clean(p.v[i].description, de), p.v[i].path ? p.v[i].path : "",
p.v[i].project ? "project" : "user", ls_clean(p.v[i].hint, hi));
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], nm[LS_TEXT_MAX + 1], de[LS_TEXT_MAX + 1], *o;
(void)snprintf(name, sizeof name, "skill:%s", ls_clean(k.v[i].name, nm));
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'))",
ls_clean(name, nm), ls_clean(k.v[i].description, de), 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);
}
{ /* the MCP servers' prompts, /mcp__SERVER__PROMPT (mcp.c) */
char *mcp = pic_mcp_prompt_commands();
if (mcp && strlen(mcp) > 2) {
sqlite3_str_appendf(t, "%s%.*s", first ? "" : ",", (int)strlen(mcp) - 2, mcp + 1);
}
free(mcp);
}
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[] = "<project_instructions path=\"";
sqlite3_str *t = sqlite3_str_new(NULL);
int first = 1;
sqlite3_str_appendall(t, "{\"files\":[");
for (const char *at = ctx; at && (at = strstr(at, tag));) {
const char *from = at + strlen(tag), *end = strchr(from, '"');
char path[PATH_MAX];
at = from;
if (!end || (size_t)(end - from) >= 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};