# 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` | reserved for checking a key without spending a request; leave NULL |

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

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