/* Saved model endpoints: their rows in ~/.pic/pic.sqlite and the checks on a name and a URL * (endpoints.h). Fetching their lists is endpoints_fetch.c's. */ #include #include #include #include #include "cred.h" #include "endpoints.h" #include "models.h" #include "platform.h" #include "userdb.h" static long long ep_now_ms(void) { return (long long)(pic_platform_get()->wall_ns() / 1000000); } /* Opens the user database and ensures picu_endpoint has its api column (an older pic.sqlite * lacked it; kept here rather than userdb.c so core stays free of vendor names). */ static sqlite3 *ep_db(char *err, size_t errcap) { sqlite3 *db = pic_userdb(err, errcap); sqlite3_stmt *q = NULL; int have = 0; if (!db) { return NULL; } if (sqlite3_prepare_v2(db, "SELECT 1 FROM pragma_table_info('picu_endpoint') WHERE name = 'api'", -1, &q, NULL) != SQLITE_OK) { if (err && errcap) { (void)snprintf(err, errcap, "cannot inspect picu_endpoint in ~/.pic/pic.sqlite: %s", sqlite3_errmsg(db)); } return NULL; } have = sqlite3_step(q) == SQLITE_ROW; sqlite3_finalize(q); /* two openers may both get here: the loser's "duplicate column" is no failure */ if (!have && sqlite3_exec(db, "ALTER TABLE picu_endpoint ADD COLUMN api TEXT NOT NULL DEFAULT 'openai-completions'", NULL, NULL, NULL) != SQLITE_OK && sqlite3_errcode(db) != SQLITE_ERROR) { if (err && errcap) { (void)snprintf(err, errcap, "cannot upgrade picu_endpoint in ~/.pic/pic.sqlite: %s", sqlite3_errmsg(db)); } return NULL; } return db; } typedef struct { const char *name; int taken; } ep_taken; static int ep_provider_is(void *ctx, const char *provider) { ep_taken *t = ctx; t->taken |= strcmp(provider, t->name) == 0; return t->taken; } int pic_endpoint_name_check(const char *name, char *err, size_t errcap) { size_t n = strlen(name); ep_taken t = {name, 0}; if (n == 0 || n > 32 || !(islower((unsigned char)name[0]) || isdigit((unsigned char)name[0]))) { (void)snprintf(err, errcap, "an endpoint's name is 1-32 of a-z, 0-9, '-', '_' and '.', starting with a letter or digit"); return -1; } for (size_t i = 0; i < n; i++) { unsigned char c = (unsigned char)name[i]; if (!(islower(c) || isdigit(c) || c == '-' || c == '_' || c == '.')) { (void)snprintf(err, errcap, "an endpoint's name is 1-32 of a-z, 0-9, '-', '_' and '.', starting with a letter or digit"); return -1; } } (void)pic_providers_each(ep_provider_is, &t); if (t.taken) { (void)snprintf(err, errcap, "%s is a provider pic already has; give the endpoint another name", name); return -1; } return 0; } int pic_endpoint_url_check(const char *url, char *out, size_t cap, char *err, size_t errcap) { const char *host, *end; size_t n = strlen(url), hn; int tls; while (n > 0 && url[n - 1] == '/') { n--; } tls = strncmp(url, "https://", 8) == 0; if (!tls && strncmp(url, "http://", 7) != 0) { (void)snprintf(err, errcap, "the URL starts with https:// (or http:// to this machine)"); return -1; } for (size_t i = 0; i < n; i++) { if ((unsigned char)url[i] <= ' ' || url[i] == 0x7f) { (void)snprintf(err, errcap, "the URL has a space or a control character in it"); return -1; } if (url[i] == '?' || url[i] == '#') { (void)snprintf(err, errcap, "the URL is the API's base, without a query: http://127.0.0.1:8317/v1, say"); return -1; } } host = url + (tls ? 8 : 7); end = host + strcspn(host, "/?#"); if (end > url + n) { end = url + n; } if (end < host) { /* "https://" with its slashes trimmed: no host, not a negative length */ end = host; } if (memchr(host, '@', (size_t)(end - host))) { (void)snprintf(err, errcap, "the URL names a user: give the key with --key instead"); return -1; } hn = (size_t)(end - host); if (hn == 0 || host[0] == ':') { (void)snprintf(err, errcap, "the URL has no host"); return -1; } if (!tls) { size_t h = host[0] == '[' ? strcspn(host, "]") + 1 : strcspn(host, ":/?#"); if (!((h == 9 && strncmp(host, "127.0.0.1", 9) == 0) || (h == 9 && strncmp(host, "localhost", 9) == 0) || (h == 5 && strncmp(host, "[::1]", 5) == 0))) { (void)snprintf(err, errcap, "plain http:// only to this machine (127.0.0.1, localhost); use https://"); return -1; } } if (n >= cap || n >= PIC_ENDPOINT_URL_CAP) { (void)snprintf(err, errcap, "the URL is longer than %d bytes", PIC_ENDPOINT_URL_CAP - 1); return -1; } (void)snprintf(out, cap, "%.*s", (int)n, url); return 0; } const char *pic_endpoint_api_check(const char *api, char *err, size_t errcap) { if (!api) { return PIC_ENDPOINT_API_DEFAULT; } if (strcmp(api, "openai-completions") == 0) { return "openai-completions"; } if (strcmp(api, "anthropic-messages") == 0) { return "anthropic-messages"; } if (strcmp(api, "openai-responses") == 0) { return "openai-responses"; } if (err && errcap) { (void)snprintf(err, errcap, "unknown endpoint API '%s' (use openai-completions, anthropic-messages or openai-responses)", api); } return NULL; } int pic_endpoint_save(const char *name, const char *url, const char *api, char *err, size_t errcap) { char norm[PIC_ENDPOINT_URL_CAP]; const char *capi; sqlite3 *db; sqlite3_stmt *q = NULL; int rc = -1; if (pic_endpoint_name_check(name, err, errcap) != 0 || pic_endpoint_url_check(url, norm, sizeof norm, err, errcap) != 0 || !(capi = pic_endpoint_api_check(api, err, errcap))) { return -1; } if (!(db = ep_db(err, errcap))) { return -1; } if (sqlite3_prepare_v2(db, "INSERT INTO picu_endpoint(name, url, api, models, added_ms) VALUES(?1, ?2, ?3, '[]', ?4)" " ON CONFLICT(name) DO UPDATE SET url = excluded.url, api = excluded.api," " models = iif(url = excluded.url AND api = excluded.api, models, '[]')," " fetched_ms = iif(url = excluded.url AND api = excluded.api, fetched_ms, NULL)," " error = iif(url = excluded.url AND api = excluded.api, error, NULL)", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC); sqlite3_bind_text(q, 2, norm, -1, SQLITE_STATIC); sqlite3_bind_text(q, 3, capi, -1, SQLITE_STATIC); sqlite3_bind_int64(q, 4, ep_now_ms()); rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; } if (rc != 0) { (void)snprintf(err, errcap, "cannot save the endpoint in ~/.pic/pic.sqlite: %s", sqlite3_errmsg(db)); } sqlite3_finalize(q); return rc; } int pic_endpoint_note(const char *name, const char *models, const char *error) { sqlite3 *db = ep_db(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, models ? "UPDATE picu_endpoint SET models = json(?2), fetched_ms = ?3, error = NULL WHERE name = ?1" : "UPDATE picu_endpoint SET error = ?4 WHERE name = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC); if (models) { sqlite3_bind_text(q, 2, models, -1, SQLITE_STATIC); sqlite3_bind_int64(q, 3, ep_now_ms()); } else { sqlite3_bind_text(q, 4, error ? error : "the fetch failed", -1, SQLITE_STATIC); } rc = sqlite3_step(q) == SQLITE_DONE && sqlite3_changes(db) == 1 ? 0 : -1; } sqlite3_finalize(q); return rc; } int pic_endpoint_basic_user(const char *name, char *user, size_t cap) { sqlite3 *db = ep_db(NULL, 0); sqlite3_stmt *q = NULL; int found = 0; if (db && sqlite3_prepare_v2(db, "SELECT basic_user FROM picu_endpoint WHERE name = ?1 AND basic_user IS NOT NULL", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC); if (sqlite3_step(q) == SQLITE_ROW) { if (user && cap) { (void)snprintf(user, cap, "%s", (const char *)sqlite3_column_text(q, 0)); } found = 1; } } sqlite3_finalize(q); return found; } int pic_endpoint_basic_check(const char *user, const char *password, char *err, size_t errcap) { if (!*user || strchr(user, ':')) { (void)snprintf(err, errcap, "the user is not empty and has no ':' (HTTP Basic joins user and password with one)"); return -1; } if (!*password) { (void)snprintf(err, errcap, "the password is empty"); return -1; } for (const char *p = user; *p; p++) { if ((unsigned char)*p < ' ' || *p == 0x7f) { (void)snprintf(err, errcap, "the user has a control character in it"); return -1; } } for (const char *p = password; *p; p++) { if ((unsigned char)*p < ' ' || *p == 0x7f) { (void)snprintf(err, errcap, "the password has a control character in it"); return -1; } } if (strlen(user) + 1 + strlen(password) >= PIC_CRED_KEY_CAP) { (void)snprintf(err, errcap, "the user and password are longer than %d bytes together", PIC_CRED_KEY_CAP - 1); return -1; } return 0; } int pic_endpoint_basic_set(const char *name, const char *user) { sqlite3 *db = ep_db(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, "UPDATE picu_endpoint SET basic_user = ?2 WHERE name = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC); if (user) { sqlite3_bind_text(q, 2, user, -1, SQLITE_STATIC); } else { sqlite3_bind_null(q, 2); } rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1; } sqlite3_finalize(q); return rc; } int pic_endpoint_remove(const char *name) { sqlite3 *db = ep_db(NULL, 0); sqlite3_stmt *q = NULL; int rc = -1; if (db && sqlite3_prepare_v2(db, "DELETE FROM picu_endpoint WHERE name = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC); rc = sqlite3_step(q) == SQLITE_DONE ? sqlite3_changes(db) > 0 : -1; } sqlite3_finalize(q); if (rc == 1 && pic_cred_logout(name) < 0) { rc = -1; } return rc; } int pic_endpoints_each(int (*fn)(void *ctx, const pic_endpoint *e), void *ctx) { sqlite3 *db = ep_db(NULL, 0); sqlite3_stmt *q = NULL; if (!db || sqlite3_prepare_v2(db, "SELECT name, url, coalesce(api, 'openai-completions'), models, error," " json_array_length(models), added_ms, coalesce(fetched_ms, 0)" " FROM picu_endpoint ORDER BY name", -1, &q, NULL) != SQLITE_OK) { sqlite3_finalize(q); return -1; } while (sqlite3_step(q) == SQLITE_ROW) { pic_endpoint e; e.name = (const char *)sqlite3_column_text(q, 0); e.url = (const char *)sqlite3_column_text(q, 1); e.api = (const char *)sqlite3_column_text(q, 2); e.models = (const char *)sqlite3_column_text(q, 3); e.error = (const char *)sqlite3_column_text(q, 4); e.count = sqlite3_column_int(q, 5); e.added_ms = sqlite3_column_int64(q, 6); e.fetched_ms = sqlite3_column_int64(q, 7); if (fn(ctx, &e)) { break; } } sqlite3_finalize(q); return 0; } int pic_endpoint_url(const char *name, char *out, size_t cap) { sqlite3 *db = ep_db(NULL, 0); sqlite3_stmt *q = NULL; int found = 0; if (db && sqlite3_prepare_v2(db, "SELECT url FROM picu_endpoint WHERE name = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC); if (sqlite3_step(q) == SQLITE_ROW) { (void)snprintf(out, cap, "%s", (const char *)sqlite3_column_text(q, 0)); found = 1; } } sqlite3_finalize(q); return found; } int pic_endpoint_api(const char *name, char *out, size_t cap) { sqlite3 *db = ep_db(NULL, 0); sqlite3_stmt *q = NULL; int found = 0; if (db && sqlite3_prepare_v2(db, "SELECT coalesce(api, 'openai-completions') FROM picu_endpoint WHERE name = ?1", -1, &q, NULL) == SQLITE_OK) { sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC); if (sqlite3_step(q) == SQLITE_ROW) { if (out && cap) { (void)snprintf(out, cap, "%s", (const char *)sqlite3_column_text(q, 0)); } found = 1; } } sqlite3_finalize(q); return found; } int pic_endpoint_models_each(int (*fn)(void *ctx, const char *provider, const char *json), void *ctx) { sqlite3 *db = ep_db(NULL, 0); sqlite3_stmt *q = NULL; if (!db || sqlite3_prepare_v2(db, "SELECT e.name, json_object('id', json_extract(m.value, '$.id'), 'endpoint', e.name," " 'ownedBy', json_extract(m.value, '$.owned_by'))" " FROM picu_endpoint e, json_each(e.models) m" " WHERE json_valid(e.models) AND json_type(m.value, '$.id') = 'text'" " ORDER BY e.name, m.key", -1, &q, NULL) != SQLITE_OK) { sqlite3_finalize(q); return -1; } while (sqlite3_step(q) == SQLITE_ROW) { if (fn(ctx, (const char *)sqlite3_column_text(q, 0), (const char *)sqlite3_column_text(q, 1))) { break; } } sqlite3_finalize(q); return 0; }