# 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](../design/keel.md) 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_shape`s its request SQL names in `pic_content` |
| `probe` | a free check of a key ([below](#free-checks-of-a-key)); 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 within
  `PIC_KEYPROBE_FRESH_MS` (the later of `probed_ms` and an `ok` row's `checked_ms`) unless forced,
  and, with `no_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_url` plus 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` (then `read`); anything else goes through `pic_cred_classify`, as a request's
  refusal does. Only `ok`, refused, out of credit and limited are recorded as a state
  (`pic_cred_health_note`); no answer, a 404 or a 5xx only stamps `probed_ms` with
  `pic_cred_health_probed` (a key with no row gets state `other`, shown `check 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`) and the terminal interface, which
  runs `pic auth status --refresh` in a child (`src/tui/app.c`: `keychk_*`) so a slow provider
  cannot stall it. Print, JSON and RPC never call it. Tests set `PIC_PROBE_DEADLINE` (seconds) and
  `PIC_NO_KEY_CHECKS` (the interface starts no check; `pty_drive` sets it unless the test says
  otherwise).

## Adding one

1. Write the decoder (`src/NAME.c` and `.h`): SSE events in, one message out, pure over a SQLite
   connection, as `src/anthropic.c` is. Add a directory of recorded streams with their `.want`
   summaries under `test/pic/`, and a mode for it to `test/pic/decode_stream.c` and `decode.sh`.
2. Write the adapter (`src/provider_NAME.c`): `open` and its provider functions, the shapes, and
   `const pic_provider_adapter pic_NAME_adapter`, declared in the decoder's header with a forward
   `struct pic_provider_adapter`. Its statics need names no other file uses (`make test-amal`).
3. If the API is new to the registry, add it to `pic_api`, `pic_api_name` and the models file's
   names in `src/models.c`.
4. Register it in `main()` in `src/main.c`, beside the others.
5. Add both files to `PIC_SRC` in the Makefile and give each a row in `ci/tiers.tsv` (`bundled`,
   seam `provider`, clock `vendor`), then regenerate the table in [tiers](tiers.md).
6. If the API has a free key check, add a `pic_provider_probe` and point `probe` at it; tests script
   its path with the fake server's `at:PATH=SPEC` (`test/pic/probes.sh`).
7. Test it against `build/pic/fake_server` as `test/pic/chat.sh` does.

`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.
