/* Refreshing the model catalog from pi.dev (see catalog.h). */
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "catalog.h"
#include "http.h"
#include "net.h"
#include "sqlite3.h"
#include "userdb.h"
#define REFRESH_MS (4LL * 3600 * 1000) /* Pi's REMOTE_CATALOG_REFRESH_INTERVAL_MS */
#define BODY_CAP (8u << 20)
static long long now_ms(void) { return (long long)time(NULL) * 1000; }
/* ---- refreshing from pi.dev -------------------------------------------- */
typedef struct {
char *buf;
size_t len;
int too_big;
} body;
static int on_body(void *ctx, const char *bytes, size_t n) {
body *b = ctx;
char *grown;
if (b->len + n > BODY_CAP) {
b->too_big = 1;
return 1;
}
if (!(grown = realloc(b->buf, b->len + n + 1))) {
return 1;
}
b->buf = grown;
memcpy(b->buf + b->len, bytes, n);
b->len += n;
b->buf[b->len] = '\0';
return 0;
}
/* An HTTP date (Last-Modified) in ms, or 0. */
static long long http_date_ms(const char *s) {
struct tm tm;
memset(&tm, 0, sizeof tm);
if (!s || !*s || !strptime(s, "%a, %d %b %Y %H:%M:%S GMT", &tm)) {
return 0;
}
return (long long)timegm(&tm) * 1000;
}
/* Stores PROVIDER's list (sqlar rule) and when it was checked; MODELS NULL
* keeps the stored list and only records the check (a 304). */
static int put_entry(sqlite3 *db, const char *provider, const char *models, long long checked, long long modified,
const char *etag) {
sqlite3_stmt *q = NULL;
int rc = -1;
if (sqlite3_prepare_v2(db,
models ? "INSERT INTO picu_catalog(provider, sz, models, checked_ms, last_modified_ms, etag)"
" VALUES(?1, length(CAST(?2 AS BLOB)), sqlar_compress(CAST(?2 AS BLOB)), ?3, ?4, ?5)"
" ON CONFLICT(provider) DO UPDATE SET sz = excluded.sz, models = excluded.models,"
" checked_ms = excluded.checked_ms, last_modified_ms = excluded.last_modified_ms, etag = excluded.etag"
: "UPDATE picu_catalog SET checked_ms = ?3, last_modified_ms = ?4, etag = coalesce(?5, etag)"
" WHERE provider = ?1 AND ?2 IS NULL",
-1, &q, NULL) != SQLITE_OK) {
return -1;
}
sqlite3_bind_text(q, 1, provider, -1, SQLITE_STATIC);
if (models) {
sqlite3_bind_text(q, 2, models, -1, SQLITE_STATIC);
}
sqlite3_bind_int64(q, 3, checked);
sqlite3_bind_int64(q, 4, modified);
if (etag && *etag) {
sqlite3_bind_text(q, 5, etag, -1, SQLITE_STATIC);
}
rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1;
sqlite3_finalize(q);
return rc;
}
/* What is stored for PROVIDER: when checked, its Last-Modified, its ETag. */
static void entry(sqlite3 *db, const char *provider, long long *checked, long long *modified, char *etag, size_t cap) {
sqlite3_stmt *q = NULL;
*checked = *modified = 0;
etag[0] = '\0';
if (sqlite3_prepare_v2(db, "SELECT checked_ms, last_modified_ms, etag FROM picu_catalog WHERE provider = ?1", -1, &q,
NULL) == SQLITE_OK) {
sqlite3_bind_text(q, 1, provider, -1, SQLITE_STATIC);
if (sqlite3_step(q) == SQLITE_ROW) {
*checked = sqlite3_column_int64(q, 0);
*modified = sqlite3_column_int64(q, 1);
if (sqlite3_column_text(q, 2)) {
(void)snprintf(etag, cap, "%s", (const char *)sqlite3_column_text(q, 2));
}
}
sqlite3_finalize(q);
}
}
int pic_catalog_refresh(int force, char *err, size_t errcap) {
const char *mode = getenv("PIC_MODEL_CATALOG");
sqlite3 *db;
pic_net *net;
char neterr[256];
int rc = 0;
err[0] = '\0';
if (mode && strcmp(mode, "offline") == 0) {
return 0;
}
if (!(db = pic_userdb(err, errcap))) {
return -1;
}
if (!(net = pic_net_new(neterr, sizeof neterr))) {
(void)snprintf(err, errcap, "%s", neterr);
return -1;
}
for (size_t p = 0; p < PIC_CATALOG_NPROVIDERS; p++) {
const char *prov = pic_catalog_providers[p];
long long checked, modified;
char url[256], etag[160], inm[200], herr[256];
const char *headers[3];
size_t nh = 0;
body b = {NULL, 0, 0};
pic_http_resp r;
entry(db, prov, &checked, &modified, etag, sizeof etag);
if (!force && checked && modified && now_ms() - checked < REFRESH_MS) {
continue;
}
(void)snprintf(url, sizeof url, "https://pi.dev/api/models/providers/%s?types=chat", prov);
headers[nh++] = "Accept: application/json";
headers[nh++] = "User-Agent: pic";
if (etag[0]) {
(void)snprintf(inm, sizeof inm, "If-None-Match: %s", etag);
headers[nh++] = inm;
}
pic_http_resp_init(&r, on_body, &b);
if (pic_http_get(net, url, headers, nh, &r, 4, 15, herr, sizeof herr) != 0) {
if (!err[0]) {
(void)snprintf(err, errcap, "%s: %s", prov, herr);
}
rc = -1;
} else if (r.status == 200 && !b.too_big && b.buf) {
sqlite3_stmt *v = NULL;
int valid = 0;
if (sqlite3_prepare_v2(db, "SELECT json_valid(?1) AND json_type(?1) = 'array'", -1, &v, NULL) == SQLITE_OK) {
sqlite3_bind_text(v, 1, b.buf, -1, SQLITE_STATIC);
valid = sqlite3_step(v) == SQLITE_ROW && sqlite3_column_int(v, 0);
sqlite3_finalize(v);
}
if (!valid || put_entry(db, prov, b.buf, now_ms(), http_date_ms(r.last_modified), r.etag) != 0) {
if (!err[0]) {
(void)snprintf(err, errcap, "%s: %s", prov, valid ? sqlite3_errmsg(db) : "the catalog was not a list of models");
}
rc = -1;
}
} else if (r.status == 304) {
(void)put_entry(db, prov, NULL, now_ms(), modified, etag);
} else if (r.status == 404 || r.status == 501) {
(void)put_entry(db, prov, "[]", now_ms(), 0, NULL);
} else {
if (!err[0]) {
(void)snprintf(err, errcap, "%s: HTTP %d", prov, r.status);
}
rc = -1;
}
free(b.buf);
}
pic_net_free(net);
return rc;
}