Expand description
Provider + model resolution. Mirrors the provider/model selection and request-auth portions of native Pi’s model runtime.
The built-in lane uses Anthropic’s protocol, while models.json may add
named Anthropic-compatible, OpenAI Completions, and OpenAI Responses
providers. OAuth/Copilot remains deferred. This module resolves one provider
identity and its isolated model catalog for each run.
§Auth resolution and isolation
Credentials are resolved independently for every provider id:
--api-keyoverrides only the finally selected provider.auth.json[provider-id]wins over that provider’s configured key.models.json.providers[provider-id].apiKey(plusauthHeader) applies only to models owned by that provider.- Provider-specific environment variables are fallbacks. The global
ANTHROPIC_*andOPENAI_API_KEYvariables belong only to the built-inanthropicandopenaiidentities respectively.
A credential never makes another provider’s model available and is never passed to another provider’s runtime, even when endpoints or model ids are identical.
§Endpoint + catalog
--base-urloverrides the selectedmodel.base_urlat resolve time;ANTHROPIC_BASE_URLis the fallback for the built-inanthropicidentity only (the request URL is built from it per-request in rpi-ai).~/.rpi/models.json(if present) merges/overrides the built-in catalog: eachanthropic-messagesprovider contributes its models, with provider-levelbase_url/headers/authHeaderfolded in. The models.json provider id (e.g.gateway) remains the routing and credential identity, so providers may safely share a protocol, endpoint, or model id.
§Model pattern precedence (mirrors resolveCliModel)
--modelmay carryprovider/id[:thinking]. The prefix is interpreted as a provider only when it names a known built-in ormodels.jsonprovider. Otherwise the slash remains part of the raw model id (for examplemeta-llama/llama-*). If both interpretations exist, the known provider wins while authenticated; an unauthenticated inferred provider yields to one uniquely authenticated raw-id match, as in native Pi.- Otherwise treat
--modelasid[:thinking]: if a trailing:levelis a valid thinking level, strip it and apply it (overriding--thinking); else the whole string is the id. --providermust name a built-in provider or a configured models.json provider and restricts selection to that identity.- The model id is matched exactly, case-insensitively against the
catalog. The TS resolver additionally does fuzzy/partial matching; v1
keeps it exact to avoid surprising model picks (partial match is a common
source of “got the wrong model” bugs — documented as a divergence in
docs/m6-cli-open-questions.md). - No
--model⇒ [pick_default_model]: (a) scan native Pi’sdefaultModelPerProviderentries in their declared order and take the first authenticated match; otherwise (b) take the first authenticated model in the catalog — mirroring the TSfindInitialModelfallback overavailableModels. This lets amodels.json-only gateway config “just work”: the built-in Anthropic models carry no auth, so the gateway model (the only authenticated one) is picked. The all-builtin/no-custom-code default (ANTHROPIC_API_KEYpath) selectsclaude-opus-4-8. Last resort falls back toDEFAULT_MODEL_ID(or the catalog head) — unreachable in practice because the auth gate refuses an unauthed catalog earlier.
Structs§
- Resolved
Model - The resolved run configuration: the provider handle, the chosen model, and
the effective thinking level (after
--thinking/:level/ model-clamp).
Enums§
- Resolve
Error - A resolution error. The TS resolver returns
{ error, warning }; v1 folds both into a single enum since the CLI treats them the same (print + non-zero exit) exceptNoApiKey, which prints guidance then exits.
Constants§
- ANTHROPIC_
API_ KEY_ ENV - The env var consulted for the API key. Mirrors TS
ANTHROPIC_API_KEY. - ANTHROPIC_
AUTH_ TOKEN_ ENV - The env var consulted for a bearer token (routed as
Authorization: Bearer). Mirrors TSANTHROPIC_AUTH_TOKEN— used by third-party Anthropic-compatible gateways (one-api/new-api/claude-code-router and private reverse proxies) that authenticate viaAuthorizationrather thanx-api-key. - ANTHROPIC_
BASE_ URL_ ENV - The env var that overrides the Anthropic endpoint base URL. Mirrors TS
ANTHROPIC_BASE_URL— point this at a gateway/proxy that speaks the/v1/messagesprotocol. - DEFAULT_
MODEL_ ID - The default Anthropic model when
--modelis absent. Kept in sync with the current native PidefaultModelPerProvider.anthropicentry. - DEFAULT_
THINKING_ LEVEL - The default thinking level when neither
--thinkingnor a:levelsuffix is present. Mirrors the TSDEFAULT_THINKING_LEVEL("medium", clamped to model capabilities by the harness’s provider build_params). - NO_
API_ KEY_ HINT - Hint text surfaced when no credential source is available. Lists every accepted source so the user can pick the one that fits their setup.
- OPENAI_
API_ KEY_ ENV - Standard OpenAI API-key environment variable used by the
openai-completionsprovider.
Functions§
- available_
catalog - The catalog the TUI’s
/modelselector displays (read-only). Re-derives the auth-filtered snapshot the provider was built from so the selector shows exactly the models that can actually run (mirrors pigetAvailableSnapshot:available = all.filter(m => configuredProviders.has(m.provider))— v1’s single-provider equivalent of “configured” is [model_is_authed]). - catalog_
all - Return the merged built-in +
models.jsoncatalog without requiring a credential or selecting a runnable model. This is used by CLI commands such as--list-models, which must remain useful before authentication. - resolve
- Resolve the provider + model + thinking level from the CLI flags + env +
~/.rpi/config. - resolve_
for_ cwd - Resolve using native Pi’s global -> trusted-project settings precedence.