Artifact 25594a930e2604ecbdc03db2734b2331772aa8a8cc095df655c7276add694b29:
- File docs/dev/providers.md — part of check-in [80969c8097] at 2026-11-28 12:13:50 on branch ergo-keys — Check a key as it is saved; hold a key sent as a message (user: dmestas, size: 6062) [annotate] [blame] [check-ins using] [more...]
Provider adapters
Core speaks only the neutral form: the request loop, the journal and the transform layer know a
reply as pic_reply (src/pic.h), with its native form kept verbatim beside it. Everything about a
wire API is a bundled component, an adapter, that registers itself with core through
pic_provider_adapter (src/provider_http.h). The keel note says why.
What an adapter declares
| Field | What it is |
|---|---|
api, name |
the pic_api it serves (src/models.h) and the wire name replies record, as pic_api_name gives it |
open |
makes a pic_provider for a resolved model: reply, summarize, count (NULL when the API has no counter), free |
cache_long_seconds |
how long a long-retention prompt cache lives for a model; NULL means an hour |
norm_id |
a foreign tool-call id as this API takes it; NULL is the default, [A-Za-z0-9_-] and at most 64 |
shapes, nshapes |
the pic_content_shapes its request SQL names in pic_content |
probe |
a free check of a key (below); NULL when the API has none |
Core decides what is not the API's: short retention is 300 seconds and none is 0
(pic_cache_seconds), the shapes pi and pi-images are core's own, and refusals go through
the credential seam (pic_cred_health_ops, src/keyhealth.c), which keys its rows by provider, not
by API.
open streams through pic_stream_post with a pic_stream_decoder and fills the reply: native
(what the API sent, replayed only to the same provider, API and model), neutral (Pi's content
array), Pi's stop reason, the raw usage, and provider, api and model. Requests are built in
SQL over PIC_CONTEXT_CTE; a foreign turn is rebuilt from the neutral form with
pic_norm_id(id, NAME, provider), so a tool result's id is rewritten exactly as its call's.
A content shape names a form (PIC_SHAPE_BLOCKS, _BLOCKS_OR_TEXT, _TEXT, _AFTER,
_TEXT_IMAGES), whether it is a tool result (which placeholder an omitted image becomes), which
text to skip, the text block's type, and a function that writes one image block. Shape names are
unique across adapters; prefix them with the adapter's (chat-user, chat-tool).
Free checks of a key
probe points to a pic_provider_probe (src/provider_http.h): request says what to GET (a path
under the model's base URL, how the key goes, any extra header lines) and read, when not NULL,
turns a 200 body into a pic_key_health (remaining credit and its currency, or PIC_KEY_CREDIT
when nothing is left). Core never names a vendor: the chat adapter picks OpenRouter's /key and
DeepSeek's /user/balance by provider name or base URL and the rest's models list, Anthropic's
adds its version header. src/keyprobe.c does the rest, for every probe alike:
- It walks the providers and their keys (
pic_cred_entries), skips sign-ins, a key checked withinPIC_KEYPROBE_FRESH_MS(the later ofprobed_msand anokrow'schecked_ms) unless forced, and, withno_prompt, stored keys while the store is locked. - Each wanted key gets a worker thread that holds a copy of the key and one connection and touches
nothing shared; every read of the store and write of health stays on the calling thread, which
records each answer as it arrives. The key goes only to
base_urlplus the probe's path, with the model's own headers, and the reply is scrubbed of the key before it is read. - A 200 is
ok(thenread); anything else goes throughpic_cred_classify, as a request's refusal does. Onlyok, refused, out of credit and limited are recorded as a state (pic_cred_health_note); no answer, a 404 or a 5xx only stampsprobed_mswithpic_cred_health_probed(a key with no row gets stateother, showncheck failed). A 200 without a balance leaves a recorded out-of-credit or unexpired limit alone. - Callers:
pic auth status --check|--refresh(src/main.c), a key just saved (pic_keyprobe_saved:pic login,pic auth login|import, and/loginthroughpic auth status --check PROVIDER:LABEL), and the terminal interface, which runspic auth status --refreshin a child (src/tui/app.c:keychk_*) so a slow provider cannot stall it. Print, JSON and RPC never call it. Tests setPIC_PROBE_DEADLINE(seconds) andPIC_NO_KEY_CHECKS(no check starts on its own, after a login or in the interface; the Makefile andpty_driveset it unless the test says otherwise).
Adding one
- Write the decoder (
src/NAME.cand.h): SSE events in, one message out, pure over a SQLite connection, assrc/anthropic.cis. Add a directory of recorded streams with their.wantsummaries undertest/pic/, and a mode for it totest/pic/decode_stream.canddecode.sh. - Write the adapter (
src/provider_NAME.c):openand its provider functions, the shapes, andconst pic_provider_adapter pic_NAME_adapter, declared in the decoder's header with a forwardstruct pic_provider_adapter. Its statics need names no other file uses (make test-amal). - If the API is new to the registry, add it to
pic_api,pic_api_nameand the models file's names insrc/models.c. - Register it in
main()insrc/main.c, beside the others. - Add both files to
PIC_SRCin the Makefile and give each a row inci/tiers.tsv(bundled, seamprovider, clockvendor), then regenerate the table in tiers. - If the API has a free key check, add a
pic_provider_probeand pointprobeat it; tests script its path with the fake server'sat:PATH=SPEC(test/pic/probes.sh). - Test it against
build/pic/fake_serverastest/pic/chat.shdoes.
test/pic/test_adapter.c registers a toy adapter through the interface alone: it is the smallest
working example. Core with no adapter registered still runs; a model whose API has none fails to
open with "no adapter in this build speaks its API", and the scripted provider
(src/provider_fake.c) needs none, since its replies are neutral already.