Artifact 72568f73c5a86c17395be89854d4c83b334acc11f987dd3e7c003b98c67b3f35:
- File
src/provider_http.h
— part of check-in
[55cff08c2c]
at
2026-11-19 00:56:53
on branch stage-4
— Add the Chat Completions adapter for OpenAI, OpenRouter and compatibles
A chunk decoder following Pi: text, reasoning from reasoning_content, reasoning or reasoning_text kept under its field, OpenRouter reasoning_details verbatim, tool calls by index, usage, finish_reason, and in-stream errors. Requests follow Pi too: system or developer role, tools as functions, tool messages, stream_options, store and reasoning by dialect. The retry loop and header assembly move into a shared module both adapters use. Recorded streams, a fuzz target and fake server cases cover it, including a conversation moving between the two APIs in both directions. (user: dmestas, size: 2342) [annotate] [blame] [check-ins using]
/* 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); char *pic_strdup(const char *s); /* 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); #endif