@powerhousedao/ph-clint-openrouter
v0.1.0-dev.1
Published
ph-clint plugin: resolve a runtime model config into a Mastra model config for OpenRouter / OpenAI-compatible providers alongside the Anthropic default.
Readme
@powerhousedao/ph-clint-openrouter
A ph-clint plugin that
adds OpenRouter and any OpenAI-compatible provider as an opt-in
alongside the Anthropic default. It exposes resolveModelConfig, a pure
function that maps a ph-clint runtime config (model, modelBaseURL,
modelApiKey, per-provider keys) into a Mastra
MastraModelConfig.
It is an external counterpart to default ph-clint codegen: core and the codegen
builders stay Anthropic-only, so multi-provider support stays out of them. A
consumer that wants it pulls in this package and calls resolveModelConfig from
its own createAgent factory — no provider logic leaks into core.
Install
pnpm add @powerhousedao/ph-clint-openrouterPeer dependencies: @powerhousedao/ph-clint and @mastra/core.
Usage
import { Agent } from '@mastra/core/agent';
import { resolveModelConfig } from '@powerhousedao/ph-clint-openrouter';
const agent = new Agent({
id: 'my-agent',
name: 'My Agent',
instructions: '…',
model: resolveModelConfig({
model: ctx.config.model, // e.g. 'openrouter/openai/gpt-4o'
modelBaseURL: ctx.config.modelBaseURL,
modelApiKey: ctx.config.modelApiKey,
anthropicApiKey: ctx.config.anthropicApiKey,
openrouterApiKey: ctx.config.openrouterApiKey,
}),
// …
});Resolution
resolveModelConfig(cfg) returns either an OpenAI-compatible
{ id, url?, apiKey? } object or a bare model string. It is structurally
assignable to Mastra's MastraModelConfig.
| Input | Output |
| ------------------------------------------------------ | --------------------------------------------------- |
| anthropic/* + key | { id, apiKey } |
| anthropic/*, no key | bare model string |
| openrouter/* + key | { id, url: openrouter/api/v1, apiKey } |
| openrouter/*, no key | bare gateway string (Mastra reads OPENROUTER_API_KEY) |
| any modelBaseURL set | { id, url, apiKey? } |
| otherwise | { id, apiKey } with a key, else bare model string |
Key precedence: a generic modelApiKey overrides the legacy <provider>ApiKey
field (e.g. anthropicApiKey, openrouterApiKey).
OpenRouter model ids
Mastra strips the leading openrouter/ provider segment before calling the
gateway, so the remainder must itself be a valid OpenRouter slug
(<vendor>/<model>) — e.g. openrouter/openai/gpt-4o is sent as
openai/gpt-4o. OpenRouter's own namespaced models (the routers
openrouter/free / openrouter/auto, and stealth models) are themselves
slugs, so write the prefix twice: openrouter/openrouter/free. A bare
openrouter/free would be sent as free and rejected with an opaque 502, so
resolveModelConfig throws a descriptive error for that shape instead.
License
MIT
