mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
This builds the model catalog from per-provider definitions and models.dev v2 data. It eplacer `generate-models.ts` with one `defineProvider()` file per built-in provider, shared model-family rules (Claude, GPT, DeepSeek, Kimi, Google), and source adapters. models.dev is read through its v2 API. Outputs and CLI flags are unchanged.
4 KiB
4 KiB
| name | description |
|---|---|
| add-llm-provider | Checklist for adding a new LLM provider to packages/ai. Covers core types, provider implementation, lazy registration, model generation, the full test matrix, coding-agent wiring, and docs. |
Adding a New LLM Provider (packages/ai)
A new provider touches multiple files. Work through these steps in order.
1. Core Types (packages/ai/src/types.ts)
- Add the provider name to the
KnownProviderunion. - Only for a new API: add the API identifier to
KnownApi(e.g."bedrock-converse-stream") and its options type toApiOptionsMap.
2. API Implementation (packages/ai/src/api/, only for a new API)
Most providers reuse an existing API such as openai-completions; skip this step then.
src/api/<api-id>.tsexports exactlystreamandstreamSimple, plus an options interface extendingStreamOptions.- Convert messages and tools to the provider format. Read the prompt and tools from the transcript with
getInitialSystemMessage(),getCurrentTools(), andresolveTranscript(). - Parse responses into standardized events (
text,tool_call,thinking,usage,stop). - Add a lazy wrapper
src/api/<api-id>.lazy.ts(<name>Api()vialazyApi()) so providers can reference the implementation without importing its SDK. - Register the lazy wrapper in the builtin API list in
src/compat.ts. - Add
export typere-exports insrc/index.tsfor option types that should be available from the root entry. The./api/*subpath export inpackage.jsonalready covers the new module.
3. Model Catalog (packages/ai/catalog/providers/<id>.ts)
- Add a provider definition with
defineProvider()and register it incatalog/providers/index.ts. Readpackages/ai/catalog/README.mdfirst. - For a models.dev provider, use
modelsDevModels(); set compat defaults per API incompatand provider-specific behavior infinalize. - Regenerate with
npm run generate-modelsinpackages/ai. This writes the stable catalog wrappersrc/providers/<id>.models.ts(exporting<ID>_MODELS) that the provider factory imports, and updatessrc/models.generated.ts.
4. Provider Factory and Auth (packages/ai/src/providers/)
- Create
src/providers/<id>.tsexporting<id>Provider(), which callscreateProvider({ id, name, baseUrl, auth, models, api }).nameis the display name shown by/loginand related UI. Seedeepseek.tsfor a minimal example. - Auth:
envApiKeyAuth()for standard API keys, a customApiKeyAuthfor ambient credentials (AWS profiles, ADC),lazyOAuth()where an OAuth flow exists. - Pass the lazy API wrapper (e.g.
openAICompletionsApi()) asapi; do not statically import API implementation modules. - Register the factory in
src/providers/all.ts. - Add the env var to
src/env-api-keys.tsso the compatgetEnvApiKey()finds it.
5. Tests (packages/ai/test/)
- Always add the provider to
stream.test.tswith at least one representative model, even if it reuses an existing API impl such asopenai-completions. - Add the provider to the broader matrix where applicable:
tokens.test.ts,abort.test.ts,empty.test.ts,context-overflow.test.ts,unicode-surrogate.test.ts,tool-call-without-result.test.ts,image-tool-result.test.ts,total-tokens.test.ts,cross-provider-handoff.test.ts. - For
cross-provider-handoff.test.ts, add at least one provider/model pair. If the provider exposes multiple model families (e.g. GPT and Claude), add at least one pair per family. - For non-standard auth, create a utility (e.g.
bedrock-utils.ts) with credential detection.
6. Coding Agent (packages/coding-agent/)
src/core/model-resolver.ts: add default model ID todefaultModelPerProvider.src/cli/args.ts: add env var documentation.docs/providers.md: add setup instructions, env var, andauth.jsonkey.
7. Documentation
packages/ai/README.md: add to providers table, document options/auth, add env vars.packages/ai/CHANGELOG.md: add entry under## [Unreleased].