/* Saved model endpoints (endpoints.c, endpoints_fetch.c): a model server or proxy 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, its wire API (openai-completions, anthropic-messages
* or openai-responses) and the model list its GET 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 through that wire API. 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
/* The default wire API and buffer size for a saved endpoint's API name. */
#define PIC_ENDPOINT_API_DEFAULT "openai-completions"
#define PIC_ENDPOINT_API_CAP 32
/* 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);
/* Wire API check: NULL defaults to "openai-completions"; otherwise must be one of
* "openai-completions", "anthropic-messages", "openai-responses". Returns the canonical string,
* or NULL with err. */
const char *pic_endpoint_api_check(const char *api, 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, *api, *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);
/* NAME's wire API into out: 1, or 0 when there is no such endpoint. */
int pic_endpoint_api(const char *name, char *out, size_t cap);
/* Whether NAME's stored credential is a user and password for HTTP Basic (not a bearer key):
* 1, with the user name (never the password) into user when it is not NULL, else 0. */
int pic_endpoint_basic_user(const char *name, char *user, size_t cap);
/* Records NAME's credential as a user and password for USER, or, USER NULL, as a bearer key. 0 or -1. */
int pic_endpoint_basic_set(const char *name, const char *user);
/* A user and password fit to seal for HTTP Basic: the user is not empty and has no ':' or control
* character, the password is not empty and has no control character, and together they fit a
* credential. 0, or -1 with err. */
int pic_endpoint_basic_check(const char *user, const char *password, char *err, size_t errcap);
/* 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 with API (NULL: openai-completions; checked by all three checks; an endpoint
* already saved under NAME whose URL or API changes loses its old list). 0, or -1 with err. */
int pic_endpoint_save(const char *name, const char *url, const char *api, 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 models for NAME now, with its stored key (bearer, x-api-key for anthropic-messages, or HTTP
* Basic) 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 with API (NULL: openai-completions), seals KEY (NULL: none; a key already
* stored stays) under NAME, then fetches its models. With USER (not NULL) KEY is the password:
* user:password is sealed and sent as HTTP Basic, else KEY is the API key. 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 *api, const char *user, 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