Artifact 7bb81b8d1c23e058df97ad6df57dd16db65b9dd8bb0db5936d2974d2e61db523:
- File
src/provider_http.h
— part of check-in
[376fd483ed]
at
2026-11-20 00:29:28
on branch cache
— Prompt caching on every provider, as Pi's
Anthropic: cache_control on the system block, the last tool and the last block of the final user message; 5 minutes by default, ttl 1h with cacheRetention=long, none with cacheRetention=none (setting in pic.sqlite, PI_CACHE_RETENTION as fallback). Anthropic models through OpenRouter get the same markers. OpenAI Responses and Chat Completions send prompt_cache_key (the conversation id) and 24h retention when long, as Pi does. The system prompt and tools stay byte-identical across turns, and 1-hour cache writes are kept in the stored usage. (user: dmestas, size: 3351) [annotate] [blame] [check-ins using] [more...]
/* 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. 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 "net.h" #include "pic.h" #include "sse.h" typedef enum { PIC_AUTH_X_API_KEY, PIC_AUTH_BEARER } pic_auth_style; /* 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); /* Pi's cacheRetention: the cacheRetention setting (short, long, none), else * PI_CACHE_RETENTION=long, else short. */ typedef enum { PIC_CACHE_SHORT, PIC_CACHE_LONG, PIC_CACHE_NONE } pic_cache; pic_cache pic_cache_retention(void); /* The cache_control object for RETENTION as JSON, or NULL for none: * {"type":"ephemeral"}, with "ttl":"1h" for long when LONG_OK. */ const char *pic_cache_control(pic_cache retention, int long_ok); /* 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 adapter for a resolved model; url, when not NULL, replaces the * model's endpoint (tests). Defined per adapter, chosen in provider_http.c. */ int pic_provider_anthropic_new(pic_provider *p, const pic_model *m, const char *url, const char *workspace, char *err, size_t errcap); int pic_provider_chat_new(pic_provider *p, const pic_model *m, const char *url, const char *workspace, char *err, size_t errcap); int pic_provider_responses_new(pic_provider *p, const pic_model *m, const char *url, const char *workspace, char *err, size_t errcap); #endif