Skip to content
pi-c Documentation
Login

Documentation

/* Saved model endpoints (endpoints.c, endpoints_fetch.c): an OpenAI-compatible server the person
 * named, kept beside the providers rather than over them. A row of picu_endpoint in
 * ~/.pic/pic.sqlite holds its name, its URL and the model list its GET {URL}/models last gave,
 * with when that was and why the last fetch failed. Its key, when it has one, is the credential
 * store's row under the endpoint's name. Its models are NAME/MODEL_ID, sent to the URL's
 * chat completions. Nothing is fetched except when an endpoint is added or refreshed. */
#ifndef PIC_ENDPOINTS_H
#define PIC_ENDPOINTS_H

#include <stddef.h>

#include "rpc.h"

/* The largest URL an endpoint keeps (a model's base_url). */
#define PIC_ENDPOINT_URL_CAP 256

/* A name is 1-32 of a-z, 0-9, '-', '_' and '.', starting with a letter or digit, and not a
 * provider pic already knows (built in or in the models file). 0, or -1 with err. */
int pic_endpoint_name_check(const char *name, char *err, size_t errcap);
/* URL normalised into out (no trailing '/'): https://HOST..., or http:// to loopback only
 * (127.0.0.1, localhost, [::1]); no spaces, no user:password@. 0, or -1 with err. */
int pic_endpoint_url_check(const char *url, char *out, size_t cap, char *err, size_t errcap);

/* One saved endpoint, as pic_endpoints_each hands it out. models is a JSON array of
 * {"id","owned_by"} (owned_by null when the server gave none); error is the last failed fetch's
 * reason, or NULL when the last fetch worked or none ran. */
typedef struct {
    const char *name, *url, *models, *error;
    int count;
    long long added_ms, fetched_ms; /* fetched_ms 0: never fetched */
} pic_endpoint;
/* Calls fn for each endpoint, by name, until it returns nonzero. 0 or -1. */
int pic_endpoints_each(int (*fn)(void *ctx, const pic_endpoint *e), void *ctx);
/* NAME's URL into out: 1, or 0 when there is no such endpoint. */
int pic_endpoint_url(const char *name, char *out, size_t cap);
/* Calls fn for each listed model of every endpoint (endpoint by name), as a Pi model object
 * with "endpoint" added: {"id", "endpoint", "ownedBy"}. 0 or -1. */
int pic_endpoint_models_each(int (*fn)(void *ctx, const char *provider, const char *json), void *ctx);

/* Saves NAME at URL (checked by both checks; an endpoint already saved under NAME gets the
 * new URL and loses its old list). 0, or -1 with err. */
int pic_endpoint_save(const char *name, const char *url, char *err, size_t errcap);
/* Records a fetch: MODELS (a JSON array as above) replaces the list, or, NULL, ERROR is kept
 * and the list stays. 0 or -1. */
int pic_endpoint_note(const char *name, const char *models, const char *error);
/* Deletes NAME's row and its stored key: 1 removed, 0 none, -1. */
int pic_endpoint_remove(const char *name);

/* GET {URL}/models for NAME now, with its stored key as a bearer token when it has one (the
 * store is unlocked first, which may ask for the passphrase on a terminal); the list replaces
 * the stored one. On failure the old list stays and the reason is kept. 0 with *count, or -1
 * with err. */
int pic_endpoint_refresh(const char *name, int *count, char *err, size_t errcap);
/* Saves NAME at URL, seals KEY (NULL: none; a key already stored stays) under NAME, then
 * fetches its models. 0 saved and fetched; 1 saved but the fetch failed (err says why); -1
 * nothing saved, with err. */
int pic_endpoint_add(const char *name, const char *url, const char *key, int *count, char *err, size_t errcap);

/* The RPC commands: get_endpoints, add_endpoint, remove_endpoint, refresh_endpoints. */
extern const pic_rpc_command pic_get_endpoints_rpc, pic_add_endpoint_rpc, pic_remove_endpoint_rpc,
    pic_refresh_endpoints_rpc;

#endif