/* What every provider adapter shares: one streaming POST with pic's retry
* rules, the model's key and headers, and the SSE framer. Retries happen only
* before any byte of a successful stream (connection failures, 429, 5xx, 529,
* honouring Retry-After); a stream that breaks after it began fails this
* generation as retryable, and the loop journals the attempt and asks again.
* Any other status fails for good, but for one 401 when the request sent a
* stored subscription's token: it is refreshed and the request sent again.
* The key lives in memory for the request only and is wiped after it. */
#ifndef PIC_PROVIDER_HTTP_H
#define PIC_PROVIDER_HTTP_H
#include <stddef.h>
#include "cred.h"
#include "net.h"
#include "pic.h"
#include "sse.h"
typedef enum { PIC_AUTH_X_API_KEY, PIC_AUTH_BEARER, PIC_AUTH_X_GOOG_API_KEY } pic_auth_style;
/* The header line that carries KEY in AUTH's style, into out. */
void pic_auth_line(pic_auth_style auth, const char *key, char *out, size_t cap);
/* The scalar at PATH ("$.data.limit_remaining") in JSON, as text, into out:
* 0 when there is one (a number as SQLite prints it), -1 when the text is
* not JSON or the path is missing or null. */
int pic_json_get(const char *json, const char *path, char *out, size_t cap);
/* A stream decoder for one API. begin() starts a fresh decode for one try;
* event() takes each SSE event; finish() is called after the stream ended
* cleanly and returns 0 only when a complete reply was decoded (else it puts
* the reason in why); end() frees the try. On success pic_stream_post returns
* with the decoder live: the adapter takes its results, then calls end(). */
typedef struct {
int (*begin)(void *ctx);
pic_sse_event_fn event;
int (*finish)(void *ctx, char *why, size_t cap);
void (*end)(void *ctx);
void *ctx;
} pic_stream_decoder;
/* POSTs body (JSON) to url for model m. extra are request headers beyond the
* key, the model's own and Content-Type. 0 when a complete reply was decoded
* (request_id set when the server sent one); -1 with err and *retryable. */
int pic_stream_post(pic_net *net, pic_store *s, const pic_model *m, const char *url, pic_auth_style auth,
const char *const *extra, size_t nextra, const char *body, const pic_stream_decoder *dec,
char *request_id, size_t request_id_cap, int *retryable, char *err, size_t errcap);
/* POSTs body (JSON) to url for model m, as pic_stream_post does (key,
* headers, retries), for a plain JSON reply: 0 with its body in *out
* (malloc'd); -1 with err. */
int pic_json_post(pic_net *net, pic_store *s, const pic_model *m, const char *url, pic_auth_style auth,
const char *const *extra, size_t nextra, const char *body, char **out, char *err, size_t errcap);
/* Pi's cacheRetention: the cacheRetention setting (short, long, none), else
* PI_CACHE_RETENTION=long, else short. What long means on the wire, and how
* long such a cache lives, is each adapter's (cache_long_seconds below). */
typedef enum { PIC_CACHE_SHORT, PIC_CACHE_LONG, PIC_CACHE_NONE } pic_cache;
pic_cache pic_cache_retention(void);
/* The session id of a submission (malloc'd): Pi's sessionId, the prompt cache key. */
char *pic_session_id(pic_store *s, int64_t submission);
char *pic_strdup(const char *s);
/* The text blocks of a neutral content array, joined (malloc'd). */
char *pic_neutral_text(sqlite3 *db, const char *neutral);
/* A JSON request body from one SQL statement with text parameters. */
char *pic_json_body(sqlite3 *db, const char *sql, int nargs, ...);
/* ---- the provider-adapter seam ----------------------------------------------
* Core speaks only the neutral form (pic.h's pic_reply). An adapter is a
* component that speaks one wire API: it builds requests from the journal,
* decodes the stream into a pic_reply, and says what the API's tool-call
* ids, content shapes and prompt-cache lifetime are. The composition root
* registers each bundled adapter (pic_provider_register); core finds one by
* the model's API and never names a vendor. */
/* How pic_content (transform.c) lays out a user or tool_result entry's text
* and images for one API, as that API's adapter builds them. Text blocks are
* {"type":TEXT_TYPE,"text"}; an image block is whatever IMAGE writes. */
typedef enum {
PIC_SHAPE_BLOCKS, /* every block in order (SKIP may leave text out) */
PIC_SHAPE_BLOCKS_OR_TEXT, /* the blocks while an image is left, else the text joined */
PIC_SHAPE_TEXT, /* the text joined, as a string */
PIC_SHAPE_AFTER, /* the blocks after the entry's text; NULL when none */
PIC_SHAPE_TEXT_IMAGES /* the text joined, then the images while one is left, else the text joined */
} pic_shape_form;
typedef struct pic_content_shape {
const char *name; /* what a request's SQL passes pic_content, unique across adapters */
pic_shape_form form;
int tool; /* a tool result: images become the tool placeholder */
int skip; /* BLOCKS: 1 leaves blank text blocks out, 2 empty ones */
int images_only; /* AFTER: the image blocks alone, placeholders left out */
int every_image; /* every image goes, whatever the model or the budget (sessions, export) */
const char *text_type; /* NULL: "text" */
void (*image)(sqlite3_str *out, const char *mime, const char *base64);
} pic_content_shape;
/* A free check of a key: one GET that spends no tokens and sends no
* conversation. The adapter says what to ask and reads the answer; core
* sends the key, only to the model's base URL, and reads any refusal
* (401, 402, 429) as it reads one of a request. */
typedef struct {
char path[96]; /* appended to the model's base URL */
pic_auth_style auth; /* how the key goes */
const char *extra[2]; /* more header lines (static) */
size_t nextra;
} pic_probe_request;
typedef struct pic_provider_probe {
/* What to ask for M's provider. */
void (*request)(const pic_model *m, pic_probe_request *req);
/* Reads a 200 reply: H arrives as "valid" (PIC_KEY_OK); the adapter may
* fill remaining and currency, or make it PIC_KEY_CREDIT when the reply
* says nothing is left. NULL: a 200 means valid. */
void (*read)(const pic_model *m, const char *body, pic_key_health *h);
} pic_provider_probe;
typedef struct pic_provider_adapter {
const char *name; /* the wire API it serves, as pic_model.api names it and replies record */
/* Makes *P the provider for the resolved model M: reply, summarize, count
* (NULL where the API has no counter), free, state and context_window.
* URL, when not NULL, replaces the model's endpoint (tests). */
int (*open)(pic_provider *p, const pic_model *m, const char *url, const char *workspace, char *err,
size_t errcap);
/* Seconds a prompt cache written under long retention lives for M; NULL:
* an hour. Short is always 300 and none 0, decided in core. */
long (*cache_long_seconds)(const pic_model *m);
/* A foreign tool-call id as this API accepts it: BUF (CAP bytes) holds a
* copy of ID, rewritten in place; PROVIDER is the target's. NULL: the
* default rule, [A-Za-z0-9_-] and at most 64 characters. */
void (*norm_id)(char *buf, size_t cap, const char *id, const char *provider);
const pic_content_shape *shapes; /* the shapes its request SQL names */
size_t nshapes;
/* How to check a key without spending tokens (keyprobe.h); NULL: the
* API has no free check, and its keys are never probed. */
const pic_provider_probe *probe;
} pic_provider_adapter;
/* Registers A (static storage) for its API (its name); a later one for the
* same API replaces it. 0, or -1 when the table is full. Before any store opens. */
int pic_provider_register(const pic_provider_adapter *a);
/* The adapter registered for wire name NAME; NULL when none. */
const pic_provider_adapter *pic_provider_adapter_named(const char *name);
/* The content shape called NAME: core's own (pi, pi-images) or a registered
* adapter's; NULL when none. */
const pic_content_shape *pic_content_shape_named(const char *name);
/* Helpers for id rules: characters outside [A-Za-z0-9_-] become '_'; 8 hex
* digits of FNV-1a of S (ids only need to be stable and distinct). */
void pic_id_sanitize(char *s);
void pic_id_hash(const char *s, char out[9]);
#endif