@funkai/models 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["MODELS","GENERATED_MODELS","baseCreateOpenRouter"],"sources":["../src/catalog/providers/index.ts","../src/catalog/index.ts","../src/provider/openrouter.ts","../src/provider/resolver.ts","../src/cost/calculate.ts"],"sourcesContent":["// ──────────────────────────────────────────────────────────────\n// ███████╗██╗ ██╗███╗ ██╗██╗ ██╗ █████╗ ██╗\n// ██╔════╝██║ ██║████╗ ██║██║ ██╔╝██╔══██╗██║\n// █████╗ ██║ ██║██╔██╗ ██║█████╔╝ ███████║██║\n// ██╔══╝ ██║ ██║██║╚██╗██║██╔═██╗ ██╔══██║██║\n// ██║ ╚██████╔╝██║ ╚████║██║ ██╗██║ ██║██║\n// ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝\n//\n// AUTO-GENERATED — DO NOT EDIT\n// Source: https://models.dev\n// Update: pnpm --filter=@funkai/models generate:models\n// ──────────────────────────────────────────────────────────────\n\nimport type { ModelDefinition } from \"../types.js\";\nimport { ALIBABA_MODELS } from \"./alibaba.js\";\nimport { AMAZON_BEDROCK_MODELS } from \"./amazon-bedrock.js\";\nimport { ANTHROPIC_MODELS } from \"./anthropic.js\";\nimport { CEREBRAS_MODELS } from \"./cerebras.js\";\nimport { COHERE_MODELS } from \"./cohere.js\";\nimport { DEEPINFRA_MODELS } from \"./deepinfra.js\";\nimport { DEEPSEEK_MODELS } from \"./deepseek.js\";\nimport { FIREWORKS_AI_MODELS } from \"./fireworks-ai.js\";\nimport { GOOGLE_VERTEX_MODELS } from \"./google-vertex.js\";\nimport { GOOGLE_MODELS } from \"./google.js\";\nimport { GROQ_MODELS } from \"./groq.js\";\nimport { HUGGINGFACE_MODELS } from \"./huggingface.js\";\nimport { INCEPTION_MODELS } from \"./inception.js\";\nimport { LLAMA_MODELS } from \"./llama.js\";\nimport { MISTRAL_MODELS } from \"./mistral.js\";\nimport { NVIDIA_MODELS } from \"./nvidia.js\";\nimport { OPENAI_MODELS } from \"./openai.js\";\nimport { OPENROUTER_MODELS } from \"./openrouter.js\";\nimport { PERPLEXITY_MODELS } from \"./perplexity.js\";\nimport { TOGETHERAI_MODELS } from \"./togetherai.js\";\nimport { XAI_MODELS } from \"./xai.js\";\n\nexport const MODELS = [\n ...OPENAI_MODELS,\n ...ANTHROPIC_MODELS,\n ...GOOGLE_MODELS,\n ...GOOGLE_VERTEX_MODELS,\n ...MISTRAL_MODELS,\n ...AMAZON_BEDROCK_MODELS,\n ...GROQ_MODELS,\n ...DEEPSEEK_MODELS,\n ...XAI_MODELS,\n ...COHERE_MODELS,\n ...FIREWORKS_AI_MODELS,\n ...TOGETHERAI_MODELS,\n ...DEEPINFRA_MODELS,\n ...CEREBRAS_MODELS,\n ...PERPLEXITY_MODELS,\n ...OPENROUTER_MODELS,\n ...LLAMA_MODELS,\n ...ALIBABA_MODELS,\n ...NVIDIA_MODELS,\n ...HUGGINGFACE_MODELS,\n ...INCEPTION_MODELS,\n] as const satisfies readonly ModelDefinition[];\n","import type { LiteralUnion } from \"type-fest\";\n\nimport type { ModelCapabilities, ModelDefinition, ModelModalities, ModelPricing } from \"./types.js\";\nimport { MODELS as GENERATED_MODELS } from \"@/catalog/providers/index.js\";\n\nexport type { ModelCapabilities, ModelDefinition, ModelModalities, ModelPricing };\n\n/**\n * Known model identifiers from the generated catalog.\n */\nexport type KnownModelId = (typeof GENERATED_MODELS)[number][\"id\"];\n\n/**\n * A model identifier that suggests known models but accepts any string.\n *\n * Provides autocomplete for cataloged models while allowing arbitrary\n * model IDs for new or custom models not yet in the catalog.\n */\nexport type ModelId = LiteralUnion<KnownModelId, string>;\n\n/**\n * All supported models with pricing and capability data.\n */\nexport const MODELS = GENERATED_MODELS satisfies readonly ModelDefinition[];\n\nconst MODEL_INDEX = new Map<string, ModelDefinition>(MODELS.map((m) => [m.id, m]));\n\n/**\n * Look up a model definition by its identifier.\n *\n * Returns `null` when the ID is not in the catalog — callers should\n * handle missing models gracefully (e.g. custom or newly released models).\n *\n * @param id - The model identifier to look up.\n * @returns The matching model definition, or `null`.\n *\n * @example\n * ```typescript\n * const m = model('gpt-4.1')\n * if (m) {\n * console.log(m.pricing.input)\n * console.log(m.capabilities.reasoning)\n * }\n * ```\n */\nexport function model(id: ModelId): ModelDefinition | null {\n return MODEL_INDEX.get(id) ?? null;\n}\n\n/**\n * Return supported model definitions, optionally filtered.\n *\n * @param filter - Optional predicate to filter models.\n * @returns A readonly array of matching model definitions.\n *\n * @example\n * ```typescript\n * const all = models()\n * const reasoning = models((m) => m.capabilities.reasoning)\n * const vision = models((m) => m.modalities.input.includes('image'))\n * ```\n */\nexport function models(filter?: (m: ModelDefinition) => boolean): readonly ModelDefinition[] {\n return filter ? MODELS.filter(filter) : MODELS;\n}\n","import {\n createOpenRouter as baseCreateOpenRouter,\n type OpenRouterProvider,\n type OpenRouterProviderSettings,\n} from \"@openrouter/ai-sdk-provider\";\n\nimport type { ModelId } from \"@/catalog/index.js\";\nimport type { LanguageModel } from \"@/provider/types.js\";\n\n/**\n * Create an OpenRouter provider instance with automatic API key resolution.\n *\n * Falls back to the `OPENROUTER_API_KEY` environment variable when\n * no explicit `apiKey` is provided in the options.\n *\n * @param options - Provider settings forwarded to `@openrouter/ai-sdk-provider`.\n * @returns A configured {@link OpenRouterProvider} instance.\n *\n * @example\n * ```typescript\n * const openrouter = createOpenRouter({ apiKey: 'sk-...' })\n * const m = openrouter('openai/gpt-5.2-codex')\n * ```\n */\nexport function createOpenRouter(options?: OpenRouterProviderSettings): OpenRouterProvider {\n const apiKey = resolveApiKey(options);\n return baseCreateOpenRouter({\n ...options,\n apiKey,\n });\n}\n\n// ---------------------------------------------------------------------------\n// Private helpers\n// ---------------------------------------------------------------------------\n\n/**\n * @private\n */\nfunction resolveApiKey(options: OpenRouterProviderSettings | undefined): string {\n if (options != null && options.apiKey != null) {\n return options.apiKey;\n }\n return getOpenRouterApiKey();\n}\n\n/**\n * Create a cached OpenRouter model resolver.\n *\n * The returned function caches the underlying provider and invalidates\n * when the API key changes at runtime.\n *\n * @returns A function that resolves a model ID to a {@link LanguageModel}.\n *\n * @private\n */\n// Cache mutation is safe: JS is single-threaded and baseCreateOpenRouter is synchronous.\n// If this were refactored to async initialization, a lock or re-architecture would be needed.\nfunction createCachedOpenRouter(): (modelId: ModelId) => LanguageModel {\n const cache: { provider: OpenRouterProvider | undefined; apiKey: string | undefined } = {\n provider: undefined,\n apiKey: undefined,\n };\n return (modelId: ModelId): LanguageModel => {\n const apiKey = getOpenRouterApiKey();\n if (!cache.provider || cache.apiKey !== apiKey) {\n cache.provider = baseCreateOpenRouter({ apiKey });\n cache.apiKey = apiKey;\n }\n return cache.provider(modelId);\n };\n}\n\n/**\n * Shorthand for creating a single OpenRouter language model.\n *\n * Resolves the API key from the environment and returns a ready-to-use\n * {@link LanguageModel} that can be passed directly to AI SDK functions.\n *\n * The provider instance is cached at module scope and reused across\n * calls. If `OPENROUTER_API_KEY` changes at runtime, the cache is\n * invalidated and a new provider is created.\n *\n * @param modelId - A model identifier (e.g. `\"openai/gpt-5.2-codex\"`).\n * @returns A configured {@link LanguageModel} instance.\n *\n * @example\n * ```typescript\n * const m = openrouter('openai/gpt-5.2-codex')\n * ```\n */\nexport const openrouter: (modelId: ModelId) => LanguageModel = createCachedOpenRouter();\n\n/**\n * Read the OpenRouter API key from the environment.\n *\n * @throws {Error} If `OPENROUTER_API_KEY` is not set.\n *\n * @private\n */\nfunction getOpenRouterApiKey(): string {\n const apiKey = process.env.OPENROUTER_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"OPENROUTER_API_KEY environment variable is required. \" +\n \"Set it in your .env file or environment.\",\n );\n }\n return apiKey;\n}\n","import type { ModelId } from \"@/catalog/index.js\";\nimport type { LanguageModel } from \"@/provider/types.js\";\n\n/**\n * A function that resolves a provider-specific model name to a {@link LanguageModel}.\n *\n * @example\n * ```typescript\n * import { createOpenAI } from '@ai-sdk/openai'\n *\n * const factory: ProviderFactory = createOpenAI({ apiKey: '...' })\n * const model = factory('gpt-4.1')\n * ```\n */\nexport type ProviderFactory = (modelName: string) => LanguageModel;\n\n/**\n * Mapping from provider prefix (e.g. `\"openai\"`, `\"anthropic\"`) to a factory\n * that creates {@link LanguageModel} instances for that provider.\n */\nexport type ProviderMap = Readonly<Record<string, ProviderFactory>>;\n\n/**\n * Configuration for {@link createModelResolver}.\n */\nexport interface ModelResolverConfig {\n /**\n * Direct AI SDK provider mappings by prefix.\n *\n * When a model ID like `\"openai/gpt-4.1\"` is resolved, the prefix\n * `\"openai\"` is extracted and looked up in this map. If found, the\n * factory receives the model portion (`\"gpt-4.1\"`).\n */\n readonly providers?: ProviderMap;\n\n /**\n * A fallback factory used when no mapped provider matches.\n *\n * Receives the full model ID string. Useful for routing unmapped\n * providers through an aggregator like OpenRouter.\n *\n * @example\n * ```typescript\n * import { openrouter } from '@funkai/models'\n *\n * const resolve = createModelResolver({\n * providers: { openai: createOpenAI({ apiKey: '...' }) },\n * fallback: openrouter,\n * })\n * ```\n */\n readonly fallback?: (modelId: string) => LanguageModel;\n}\n\n/**\n * A function that resolves a model ID to a {@link LanguageModel} instance.\n */\nexport type ModelResolver = (modelId: ModelId) => LanguageModel;\n\n/**\n * Create a model resolver with configurable provider mappings.\n *\n * When a model ID like `\"openai/gpt-4.1\"` is resolved:\n * 1. The provider prefix (`\"openai\"`) is extracted\n * 2. If a direct provider is mapped for that prefix, it receives the model portion (`\"gpt-4.1\"`)\n * 3. Otherwise, the fallback is called with the full ID (if configured)\n * 4. If no fallback, an error is thrown\n *\n * @param config - Provider mappings and fallback configuration.\n * @returns A resolver function that maps model IDs to {@link LanguageModel} instances.\n *\n * @example\n * ```typescript\n * import { createOpenAI } from '@ai-sdk/openai'\n * import { createAnthropic } from '@ai-sdk/anthropic'\n *\n * const resolve = createModelResolver({\n * providers: {\n * openai: createOpenAI({ apiKey: '...' }),\n * anthropic: createAnthropic({ apiKey: '...' }),\n * },\n * })\n *\n * const m1 = resolve('openai/gpt-4.1') // uses @ai-sdk/openai directly\n * const m2 = resolve('anthropic/claude-sonnet-4') // uses @ai-sdk/anthropic directly\n * ```\n */\nexport function createModelResolver(config?: ModelResolverConfig): ModelResolver {\n const providers = config?.providers ?? {};\n const fallback = config?.fallback;\n\n return (modelId: ModelId): LanguageModel => {\n if (!modelId.trim()) {\n throw new Error(\"Cannot resolve model: model ID is empty\");\n }\n\n const slashIndex = modelId.indexOf(\"/\");\n\n if (slashIndex === -1) {\n if (fallback) {\n return fallback(modelId);\n }\n throw new Error(\n `Cannot resolve model \"${modelId}\": no provider prefix and no fallback configured`,\n );\n }\n\n const prefix = modelId.slice(0, slashIndex);\n // eslint-disable-next-line security/detect-object-injection -- Prefix extracted from user model ID, used as record key lookup\n const factory = providers[prefix];\n\n if (factory) {\n const modelName = modelId.slice(slashIndex + 1);\n return factory(modelName);\n }\n\n if (fallback) {\n return fallback(modelId);\n }\n\n throw new Error(\n `Cannot resolve model \"${modelId}\": no provider mapped for \"${prefix}\" and no fallback configured`,\n );\n };\n}\n","import type { UsageCost } from \"./types.js\";\nimport type { ModelPricing } from \"@/catalog/types.js\";\nimport type { TokenUsage } from \"@/provider/types.js\";\n\n/**\n * Calculate the dollar cost from token usage and model pricing.\n *\n * Multiplies each token count by the corresponding per-token pricing rate.\n * Optional pricing fields (cache) default to `0` when absent.\n *\n * @param usage - Token counts from a model invocation.\n * @param pricing - Per-token pricing rates for the model.\n * @returns A {@link UsageCost} with per-field and total costs in USD.\n *\n * @example\n * ```typescript\n * import { calculateCost, model } from '@funkai/models'\n *\n * const usage: TokenUsage = {\n * inputTokens: 1000,\n * outputTokens: 500,\n * totalTokens: 1500,\n * cacheReadTokens: 200,\n * cacheWriteTokens: 0,\n * reasoningTokens: 0,\n * }\n * const m = model('gpt-4.1')\n * if (m) {\n * const cost = calculateCost(usage, m.pricing)\n * console.log(cost.total) // 0.006\n * }\n * ```\n */\nexport function calculateCost(usage: TokenUsage, pricing: ModelPricing): UsageCost {\n const input = usage.inputTokens * pricing.input;\n const output = usage.outputTokens * pricing.output;\n const cacheRead = usage.cacheReadTokens * (pricing.cacheRead ?? 0);\n const cacheWrite = usage.cacheWriteTokens * (pricing.cacheWrite ?? 0);\n const reasoning = usage.reasoningTokens * (pricing.reasoning ?? 0);\n const total = input + output + cacheRead + cacheWrite + reasoning;\n\n return { input, output, cacheRead, cacheWrite, reasoning, total };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;ACuBA,MAAa,SDaS;CACpB,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACJ;ACjCD,MAAM,cAAc,IAAI,IAA6B,OAAO,KAAK,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;;;;;;;;;;;;;;;;;;;AAoBlF,SAAgB,MAAM,IAAqC;AACzD,QAAO,YAAY,IAAI,GAAG,IAAI;;;;;;;;;;;;;;;AAgBhC,SAAgB,OAAO,QAAsE;AAC3F,QAAO,SAAS,OAAO,OAAO,OAAO,GAAG;;;;;;;;;;;;;;;;;;;ACvC1C,SAAgB,iBAAiB,SAA0D;CACzF,MAAM,SAAS,cAAc,QAAQ;AACrC,QAAOE,mBAAqB;EAC1B,GAAG;EACH;EACD,CAAC;;;;;AAUJ,SAAS,cAAc,SAAyD;AAC9E,KAAI,WAAW,QAAQ,QAAQ,UAAU,KACvC,QAAO,QAAQ;AAEjB,QAAO,qBAAqB;;;;;;;;;;;;AAe9B,SAAS,yBAA8D;CACrE,MAAM,QAAkF;EACtF,UAAU,KAAA;EACV,QAAQ,KAAA;EACT;AACD,SAAQ,YAAoC;EAC1C,MAAM,SAAS,qBAAqB;AACpC,MAAI,CAAC,MAAM,YAAY,MAAM,WAAW,QAAQ;AAC9C,SAAM,WAAWA,mBAAqB,EAAE,QAAQ,CAAC;AACjD,SAAM,SAAS;;AAEjB,SAAO,MAAM,SAAS,QAAQ;;;;;;;;;;;;;;;;;;;;;AAsBlC,MAAa,aAAkD,wBAAwB;;;;;;;;AASvF,SAAS,sBAA8B;CACrC,MAAM,SAAS,QAAQ,IAAI;AAC3B,KAAI,CAAC,OACH,OAAM,IAAI,MACR,gGAED;AAEH,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACrBT,SAAgB,oBAAoB,QAA6C;CAC/E,MAAM,YAAY,QAAQ,aAAa,EAAE;CACzC,MAAM,WAAW,QAAQ;AAEzB,SAAQ,YAAoC;AAC1C,MAAI,CAAC,QAAQ,MAAM,CACjB,OAAM,IAAI,MAAM,0CAA0C;EAG5D,MAAM,aAAa,QAAQ,QAAQ,IAAI;AAEvC,MAAI,eAAe,IAAI;AACrB,OAAI,SACF,QAAO,SAAS,QAAQ;AAE1B,SAAM,IAAI,MACR,yBAAyB,QAAQ,kDAClC;;EAGH,MAAM,SAAS,QAAQ,MAAM,GAAG,WAAW;EAE3C,MAAM,UAAU,UAAU;AAE1B,MAAI,QAEF,QAAO,QADW,QAAQ,MAAM,aAAa,EAAE,CACtB;AAG3B,MAAI,SACF,QAAO,SAAS,QAAQ;AAG1B,QAAM,IAAI,MACR,yBAAyB,QAAQ,6BAA6B,OAAO,8BACtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACzFL,SAAgB,cAAc,OAAmB,SAAkC;CACjF,MAAM,QAAQ,MAAM,cAAc,QAAQ;CAC1C,MAAM,SAAS,MAAM,eAAe,QAAQ;CAC5C,MAAM,YAAY,MAAM,mBAAmB,QAAQ,aAAa;CAChE,MAAM,aAAa,MAAM,oBAAoB,QAAQ,cAAc;CACnE,MAAM,YAAY,MAAM,mBAAmB,QAAQ,aAAa;AAGhE,QAAO;EAAE;EAAO;EAAQ;EAAW;EAAY;EAAW,OAF5C,QAAQ,SAAS,YAAY,aAAa;EAES"}
1
+ {"version":3,"file":"index.mjs","names":["MODELS","GENERATED_MODELS","baseCreateProviderRegistry"],"sources":["../src/catalog/providers/index.ts","../src/catalog/index.ts","../src/provider/registry.ts","../src/cost/calculate.ts"],"sourcesContent":["// ──────────────────────────────────────────────────────────────\n// ███████╗██╗ ██╗███╗ ██╗██╗ ██╗ █████╗ ██╗\n// ██╔════╝██║ ██║████╗ ██║██║ ██╔╝██╔══██╗██║\n// █████╗ ██║ ██║██╔██╗ ██║█████╔╝ ███████║██║\n// ██╔══╝ ██║ ██║██║╚██╗██║██╔═██╗ ██╔══██║██║\n// ██║ ╚██████╔╝██║ ╚████║██║ ██╗██║ ██║██║\n// ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝\n//\n// AUTO-GENERATED — DO NOT EDIT\n// Source: https://models.dev\n// Update: pnpm --filter=@funkai/models generate:models\n// ──────────────────────────────────────────────────────────────\n\nimport type { ModelDefinition } from \"../types.js\";\nimport { ALIBABA_MODELS } from \"./alibaba.js\";\nimport { AMAZON_BEDROCK_MODELS } from \"./amazon-bedrock.js\";\nimport { ANTHROPIC_MODELS } from \"./anthropic.js\";\nimport { CEREBRAS_MODELS } from \"./cerebras.js\";\nimport { COHERE_MODELS } from \"./cohere.js\";\nimport { DEEPINFRA_MODELS } from \"./deepinfra.js\";\nimport { DEEPSEEK_MODELS } from \"./deepseek.js\";\nimport { FIREWORKS_AI_MODELS } from \"./fireworks-ai.js\";\nimport { GOOGLE_VERTEX_MODELS } from \"./google-vertex.js\";\nimport { GOOGLE_MODELS } from \"./google.js\";\nimport { GROQ_MODELS } from \"./groq.js\";\nimport { HUGGINGFACE_MODELS } from \"./huggingface.js\";\nimport { INCEPTION_MODELS } from \"./inception.js\";\nimport { LLAMA_MODELS } from \"./llama.js\";\nimport { MISTRAL_MODELS } from \"./mistral.js\";\nimport { NVIDIA_MODELS } from \"./nvidia.js\";\nimport { OPENAI_MODELS } from \"./openai.js\";\nimport { OPENROUTER_MODELS } from \"./openrouter.js\";\nimport { PERPLEXITY_MODELS } from \"./perplexity.js\";\nimport { TOGETHERAI_MODELS } from \"./togetherai.js\";\nimport { XAI_MODELS } from \"./xai.js\";\n\nexport const MODELS = [\n ...OPENAI_MODELS,\n ...ANTHROPIC_MODELS,\n ...GOOGLE_MODELS,\n ...GOOGLE_VERTEX_MODELS,\n ...MISTRAL_MODELS,\n ...AMAZON_BEDROCK_MODELS,\n ...GROQ_MODELS,\n ...DEEPSEEK_MODELS,\n ...XAI_MODELS,\n ...COHERE_MODELS,\n ...FIREWORKS_AI_MODELS,\n ...TOGETHERAI_MODELS,\n ...DEEPINFRA_MODELS,\n ...CEREBRAS_MODELS,\n ...PERPLEXITY_MODELS,\n ...OPENROUTER_MODELS,\n ...LLAMA_MODELS,\n ...ALIBABA_MODELS,\n ...NVIDIA_MODELS,\n ...HUGGINGFACE_MODELS,\n ...INCEPTION_MODELS,\n] as const satisfies readonly ModelDefinition[];\n","import type { LiteralUnion } from \"type-fest\";\n\nimport type { ModelCapabilities, ModelDefinition, ModelModalities, ModelPricing } from \"./types.js\";\nimport { MODELS as GENERATED_MODELS } from \"@/catalog/providers/index.js\";\n\nexport type { ModelCapabilities, ModelDefinition, ModelModalities, ModelPricing };\n\n/**\n * Known model identifiers from the generated catalog.\n */\nexport type KnownModelId = (typeof GENERATED_MODELS)[number][\"id\"];\n\n/**\n * A model identifier that suggests known models but accepts any string.\n *\n * Provides autocomplete for cataloged models while allowing arbitrary\n * model IDs for new or custom models not yet in the catalog.\n */\nexport type ModelId = LiteralUnion<KnownModelId, string>;\n\n/**\n * All supported models with pricing and capability data.\n */\nexport const MODELS = GENERATED_MODELS satisfies readonly ModelDefinition[];\n\nconst MODEL_INDEX = new Map<string, ModelDefinition>(MODELS.map((m) => [m.id, m]));\n\n/**\n * Look up a model definition by its identifier.\n *\n * Returns `null` when the ID is not in the catalog — callers should\n * handle missing models gracefully (e.g. custom or newly released models).\n *\n * @param id - The model identifier to look up.\n * @returns The matching model definition, or `null`.\n *\n * @example\n * ```typescript\n * const m = model('gpt-4.1')\n * if (m) {\n * console.log(m.pricing.input)\n * console.log(m.capabilities.reasoning)\n * }\n * ```\n */\nexport function model(id: ModelId): ModelDefinition | null {\n return MODEL_INDEX.get(id) ?? null;\n}\n\n/**\n * Return supported model definitions, optionally filtered.\n *\n * @param filter - Optional predicate to filter models.\n * @returns A readonly array of matching model definitions.\n *\n * @example\n * ```typescript\n * const all = models()\n * const reasoning = models((m) => m.capabilities.reasoning)\n * const vision = models((m) => m.modalities.input.includes('image'))\n * ```\n */\nexport function models(filter?: (m: ModelDefinition) => boolean): readonly ModelDefinition[] {\n return filter ? MODELS.filter(filter) : MODELS;\n}\n","import { createProviderRegistry as baseCreateProviderRegistry } from \"ai\";\n\nimport type { ModelId } from \"@/catalog/index.js\";\nimport type { LanguageModel } from \"@/provider/types.js\";\n\n/**\n * Extract the provider type accepted by the AI SDK's `createProviderRegistry`.\n *\n * @private\n */\ntype AIProviders = Parameters<typeof baseCreateProviderRegistry>[0];\n\n/**\n * Configuration for {@link createProviderRegistry}.\n */\nexport interface ProviderRegistryConfig {\n /**\n * AI SDK provider instances keyed by prefix.\n *\n * When a model ID like `\"openai/gpt-4.1\"` is resolved, the prefix\n * `\"openai\"` is extracted and looked up in this map.\n *\n * @example\n * ```typescript\n * import { createOpenAI } from '@ai-sdk/openai'\n * import { anthropic } from '@ai-sdk/anthropic'\n *\n * const registry = createProviderRegistry({\n * providers: {\n * openai: createOpenAI({ apiKey: '...' }),\n * anthropic,\n * },\n * })\n * ```\n */\n readonly providers: AIProviders;\n}\n\n/**\n * A function that resolves a model ID to a {@link LanguageModel} instance.\n *\n * @param modelId - Catalog model identifier in `provider/model` format.\n * @returns The resolved AI SDK {@link LanguageModel}.\n *\n * @example\n * ```typescript\n * const model = registry(\"openai/gpt-4.1\");\n * ```\n */\nexport type ProviderRegistry = (modelId: ModelId) => LanguageModel;\n\n/**\n * Create a provider registry that resolves `provider/model` string IDs\n * to AI SDK {@link LanguageModel} instances.\n *\n * Thin wrapper around the AI SDK's `createProviderRegistry` that uses\n * `/` as the separator instead of `:`, matching the model ID format used\n * throughout the funkai catalog (e.g. `\"openai/gpt-4.1\"`).\n *\n * @param config - Provider mappings.\n * @returns A resolver function that maps model IDs to {@link LanguageModel} instances.\n *\n * @example\n * ```typescript\n * import { createOpenAI } from '@ai-sdk/openai'\n * import { anthropic } from '@ai-sdk/anthropic'\n * import { createProviderRegistry } from '@funkai/models'\n *\n * const registry = createProviderRegistry({\n * providers: {\n * openai: createOpenAI({ apiKey: '...' }),\n * anthropic,\n * },\n * })\n *\n * const m1 = registry('openai/gpt-4.1')\n * const m2 = registry('anthropic/claude-sonnet-4')\n * ```\n */\nexport function createProviderRegistry(config: ProviderRegistryConfig): ProviderRegistry {\n const inner = baseCreateProviderRegistry(config.providers, {\n separator: \"/\",\n });\n\n return (modelId: ModelId): LanguageModel => {\n if (!modelId.trim()) {\n throw new Error(\"Cannot resolve model: model ID is empty\");\n }\n // Cast needed: AI SDK overloads expect `provider/model` template literal,\n // but our ModelId is a branded string union. The runtime validates the format.\n return inner.languageModel(modelId as `${string}/${string}`) as LanguageModel;\n };\n}\n","import type { UsageCost } from \"./types.js\";\nimport type { ModelPricing } from \"@/catalog/types.js\";\nimport type { TokenUsage } from \"@/provider/types.js\";\n\n/**\n * Calculate the dollar cost from token usage and model pricing.\n *\n * Multiplies each token count by the corresponding per-token pricing rate.\n * Optional pricing fields (cache) default to `0` when absent.\n *\n * @param usage - Token counts from a model invocation.\n * @param pricing - Per-token pricing rates for the model.\n * @returns A {@link UsageCost} with per-field and total costs in USD.\n *\n * @example\n * ```typescript\n * import { calculateCost, model } from '@funkai/models'\n *\n * const usage: TokenUsage = {\n * inputTokens: 1000,\n * outputTokens: 500,\n * totalTokens: 1500,\n * cacheReadTokens: 200,\n * cacheWriteTokens: 0,\n * reasoningTokens: 0,\n * }\n * const m = model('gpt-4.1')\n * if (m) {\n * const cost = calculateCost(usage, m.pricing)\n * console.log(cost.total) // 0.006\n * }\n * ```\n */\nexport function calculateCost(usage: TokenUsage, pricing: ModelPricing): UsageCost {\n const input = usage.inputTokens * pricing.input;\n const output = usage.outputTokens * pricing.output;\n const cacheRead = usage.cacheReadTokens * (pricing.cacheRead ?? 0);\n const cacheWrite = usage.cacheWriteTokens * (pricing.cacheWrite ?? 0);\n const reasoning = usage.reasoningTokens * (pricing.reasoning ?? 0);\n const total = input + output + cacheRead + cacheWrite + reasoning;\n\n return { input, output, cacheRead, cacheWrite, reasoning, total };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;ACuBA,MAAa,SDaS;CACpB,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACJ;ACjCD,MAAM,cAAc,IAAI,IAA6B,OAAO,KAAK,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;;;;;;;;;;;;;;;;;;;AAoBlF,SAAgB,MAAM,IAAqC;AACzD,QAAO,YAAY,IAAI,GAAG,IAAI;;;;;;;;;;;;;;;AAgBhC,SAAgB,OAAO,QAAsE;AAC3F,QAAO,SAAS,OAAO,OAAO,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACgB1C,SAAgB,uBAAuB,QAAkD;CACvF,MAAM,QAAQE,yBAA2B,OAAO,WAAW,EACzD,WAAW,KACZ,CAAC;AAEF,SAAQ,YAAoC;AAC1C,MAAI,CAAC,QAAQ,MAAM,CACjB,OAAM,IAAI,MAAM,0CAA0C;AAI5D,SAAO,MAAM,cAAc,QAAiC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACzDhE,SAAgB,cAAc,OAAmB,SAAkC;CACjF,MAAM,QAAQ,MAAM,cAAc,QAAQ;CAC1C,MAAM,SAAS,MAAM,eAAe,QAAQ;CAC5C,MAAM,YAAY,MAAM,mBAAmB,QAAQ,aAAa;CAChE,MAAM,aAAa,MAAM,oBAAoB,QAAQ,cAAc;CACnE,MAAM,YAAY,MAAM,mBAAmB,QAAQ,aAAa;AAGhE,QAAO;EAAE;EAAO;EAAQ;EAAW;EAAY;EAAW,OAF5C,QAAQ,SAAS,YAAY,aAAa;EAES"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funkai/models",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "description": "Model catalog, provider resolution, and cost calculations for the funkai AI SDK",
6
6
  "keywords": [
@@ -113,30 +113,25 @@
113
113
  "import": "./dist/providers/inception.mjs"
114
114
  }
115
115
  },
116
- "scripts": {
117
- "generate:models": "lauf run @funkai/models/generate-models",
118
- "build": "tsdown",
119
- "typecheck": "tsc --noEmit",
120
- "test": "vitest run --typecheck",
121
- "test:coverage": "vitest run --coverage"
122
- },
123
116
  "dependencies": {
124
- "@openrouter/ai-sdk-provider": "^2.3.1",
125
117
  "ai": "^6.0.116",
126
118
  "type-fest": "^5.4.4"
127
119
  },
128
120
  "devDependencies": {
129
- "@types/node": "catalog:",
130
- "@vitest/coverage-v8": "catalog:",
131
- "tsdown": "catalog:",
132
- "tsx": "catalog:",
133
- "typescript": "catalog:",
134
- "vitest": "catalog:"
135
- },
136
- "publishConfig": {
137
- "access": "public"
121
+ "@types/node": "^25.5.0",
122
+ "@vitest/coverage-v8": "^4.1.0",
123
+ "tsdown": "^0.21.3",
124
+ "tsx": "^4.21.0",
125
+ "typescript": "^5.9.3",
126
+ "vitest": "^4.1.0"
138
127
  },
139
128
  "engines": {
140
129
  "node": ">=24.0.0"
130
+ },
131
+ "scripts": {
132
+ "generate:models": "lauf run @funkai/models/generate-models",
133
+ "build": "tsdown",
134
+ "typecheck": "tsc --noEmit",
135
+ "test": "vitest run --typecheck"
141
136
  }
142
- }
137
+ }
@@ -6,10 +6,6 @@ import { lauf, z } from "laufen";
6
6
  const API_URL = "https://models.dev/api.json";
7
7
  const STALE_MS = 24 * 60 * 60 * 1000;
8
8
 
9
- // ---------------------------------------------------------------------------
10
- // Banner
11
- // ---------------------------------------------------------------------------
12
-
13
9
  const BANNER = `// ──────────────────────────────────────────────────────────────
14
10
  // ███████╗██╗ ██╗███╗ ██╗██╗ ██╗ █████╗ ██╗
15
11
  // ██╔════╝██║ ██║████╗ ██║██║ ██╔╝██╔══██╗██║
@@ -23,10 +19,6 @@ const BANNER = `// ────────────────────
23
19
  // Update: pnpm --filter=@funkai/models generate:models
24
20
  // ──────────────────────────────────────────────────────────────`;
25
21
 
26
- // ---------------------------------------------------------------------------
27
- // Types
28
- // ---------------------------------------------------------------------------
29
-
30
22
  interface ProviderEntry {
31
23
  name: string;
32
24
  prefix: string;
@@ -58,10 +50,6 @@ interface ApiProvider {
58
50
  models: Record<string, ApiModel>;
59
51
  }
60
52
 
61
- // ---------------------------------------------------------------------------
62
- // Helpers
63
- // ---------------------------------------------------------------------------
64
-
65
53
  /**
66
54
  * Convert a provider key to a TypeScript constant name.
67
55
  * e.g. "openai" → "OPENAI_MODELS", "meta-llama" → "META_LLAMA_MODELS"
@@ -152,10 +140,6 @@ function escapeStr(s: string): string {
152
140
  return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r");
153
141
  }
154
142
 
155
- // ---------------------------------------------------------------------------
156
- // Staleness check
157
- // ---------------------------------------------------------------------------
158
-
159
143
  function isFresh(reqPath: string): boolean {
160
144
  if (!existsSync(reqPath)) {
161
145
  return false;
@@ -169,10 +153,6 @@ function isFresh(reqPath: string): boolean {
169
153
  }
170
154
  }
171
155
 
172
- // ---------------------------------------------------------------------------
173
- // Script
174
- // ---------------------------------------------------------------------------
175
-
176
156
  export default lauf({
177
157
  description: "Fetch model data from models.dev and generate TypeScript catalog files",
178
158
  args: {
@@ -2,10 +2,6 @@ import { describe, expect, it } from "vitest";
2
2
 
3
3
  import { model, models, MODELS } from "@/catalog/index.js";
4
4
 
5
- // ---------------------------------------------------------------------------
6
- // MODELS constant
7
- // ---------------------------------------------------------------------------
8
-
9
5
  describe("MODELS", () => {
10
6
  it("is a non-empty array", () => {
11
7
  expect(MODELS.length).toBeGreaterThan(0);
@@ -41,10 +37,6 @@ describe("MODELS", () => {
41
37
  });
42
38
  });
43
39
 
44
- // ---------------------------------------------------------------------------
45
- // model()
46
- // ---------------------------------------------------------------------------
47
-
48
40
  describe("model()", () => {
49
41
  it("returns the model definition for a known ID", () => {
50
42
  const result = model("gpt-4o-mini");
@@ -78,10 +70,6 @@ describe("model()", () => {
78
70
  });
79
71
  });
80
72
 
81
- // ---------------------------------------------------------------------------
82
- // models()
83
- // ---------------------------------------------------------------------------
84
-
85
73
  describe("models()", () => {
86
74
  it("returns all models when called without filter", () => {
87
75
  const result = models();
@@ -4,10 +4,6 @@ import { calculateCost } from "./calculate.js";
4
4
  import type { ModelPricing } from "@/catalog/types.js";
5
5
  import type { TokenUsage } from "@/provider/types.js";
6
6
 
7
- // ---------------------------------------------------------------------------
8
- // Helpers
9
- // ---------------------------------------------------------------------------
10
-
11
7
  const ZERO_USAGE: TokenUsage = {
12
8
  inputTokens: 0,
13
9
  outputTokens: 0,
@@ -37,10 +33,6 @@ const REASONING_PRICING: ModelPricing = {
37
33
  reasoning: 0.000012,
38
34
  };
39
35
 
40
- // ---------------------------------------------------------------------------
41
- // calculateCost()
42
- // ---------------------------------------------------------------------------
43
-
44
36
  describe("calculateCost()", () => {
45
37
  it("returns all zeros for zero usage", () => {
46
38
  const result = calculateCost(ZERO_USAGE, FULL_PRICING);
package/src/index.ts CHANGED
@@ -10,14 +10,8 @@ export type {
10
10
  } from "@/catalog/index.js";
11
11
 
12
12
  // Provider
13
- export { createOpenRouter, openrouter } from "@/provider/openrouter.js";
14
- export { createModelResolver } from "@/provider/resolver.js";
15
- export type {
16
- ProviderFactory,
17
- ProviderMap,
18
- ModelResolverConfig,
19
- ModelResolver,
20
- } from "@/provider/resolver.js";
13
+ export { createProviderRegistry } from "@/provider/registry.js";
14
+ export type { ProviderRegistryConfig, ProviderRegistry } from "@/provider/registry.js";
21
15
  export type { LanguageModel, TokenUsage } from "@/provider/types.js";
22
16
 
23
17
  // Cost
@@ -1,9 +1,3 @@
1
- export { createOpenRouter, openrouter } from "./openrouter.js";
2
- export { createModelResolver } from "./resolver.js";
3
- export type {
4
- ProviderFactory,
5
- ProviderMap,
6
- ModelResolverConfig,
7
- ModelResolver,
8
- } from "./resolver.js";
1
+ export { createProviderRegistry } from "./registry.js";
2
+ export type { ProviderRegistryConfig, ProviderRegistry } from "./registry.js";
9
3
  export type { LanguageModel, TokenUsage } from "./types.js";
@@ -0,0 +1,87 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+
3
+ import { createProviderRegistry } from "@/provider/registry.js";
4
+ import type { LanguageModel } from "@/provider/types.js";
5
+
6
+ function fakeModel(id: string): LanguageModel {
7
+ return { modelId: id } as unknown as LanguageModel;
8
+ }
9
+
10
+ function fakeProvider(prefix: string) {
11
+ return {
12
+ specificationVersion: "v3" as const,
13
+ languageModel: (modelName: string) => fakeModel(`${prefix}/${modelName}`),
14
+ textEmbeddingModel: () => {
15
+ throw new Error("not implemented");
16
+ },
17
+ embeddingModel: () => {
18
+ throw new Error("not implemented");
19
+ },
20
+ imageModel: () => {
21
+ throw new Error("not implemented");
22
+ },
23
+ };
24
+ }
25
+
26
+ describe("createProviderRegistry()", () => {
27
+ it("resolves a model ID via the mapped provider", () => {
28
+ const registry = createProviderRegistry({
29
+ providers: { openai: fakeProvider("openai") },
30
+ });
31
+
32
+ const result = registry("openai/gpt-4.1");
33
+
34
+ expect(result).toEqual(fakeModel("openai/gpt-4.1"));
35
+ });
36
+
37
+ it("supports multiple mapped providers", () => {
38
+ const openai = fakeProvider("openai");
39
+ const anthropic = fakeProvider("anthropic");
40
+ const openaiSpy = vi.spyOn(openai, "languageModel");
41
+ const anthropicSpy = vi.spyOn(anthropic, "languageModel");
42
+
43
+ const registry = createProviderRegistry({
44
+ providers: { openai, anthropic },
45
+ });
46
+
47
+ registry("openai/gpt-4.1");
48
+ registry("anthropic/claude-sonnet-4-20250514");
49
+
50
+ expect(openaiSpy).toHaveBeenCalledWith("gpt-4.1");
51
+ expect(anthropicSpy).toHaveBeenCalledWith("claude-sonnet-4-20250514");
52
+ });
53
+
54
+ it("handles model IDs with multiple slashes correctly", () => {
55
+ const registry = createProviderRegistry({
56
+ providers: { "meta-llama": fakeProvider("meta-llama") },
57
+ });
58
+
59
+ const result = registry("meta-llama/llama-4-scout/extended");
60
+
61
+ expect(result).toEqual(fakeModel("meta-llama/llama-4-scout/extended"));
62
+ });
63
+
64
+ it("throws for empty model ID", () => {
65
+ const registry = createProviderRegistry({
66
+ providers: { openai: fakeProvider("openai") },
67
+ });
68
+
69
+ expect(() => registry("")).toThrow("Cannot resolve model: model ID is empty");
70
+ });
71
+
72
+ it("throws for whitespace-only model ID", () => {
73
+ const registry = createProviderRegistry({
74
+ providers: { openai: fakeProvider("openai") },
75
+ });
76
+
77
+ expect(() => registry(" ")).toThrow("Cannot resolve model: model ID is empty");
78
+ });
79
+
80
+ it("throws for unmapped provider prefix", () => {
81
+ const registry = createProviderRegistry({
82
+ providers: { openai: fakeProvider("openai") },
83
+ });
84
+
85
+ expect(() => registry("anthropic/claude-sonnet-4-20250514")).toThrow();
86
+ });
87
+ });
@@ -0,0 +1,93 @@
1
+ import { createProviderRegistry as baseCreateProviderRegistry } from "ai";
2
+
3
+ import type { ModelId } from "@/catalog/index.js";
4
+ import type { LanguageModel } from "@/provider/types.js";
5
+
6
+ /**
7
+ * Extract the provider type accepted by the AI SDK's `createProviderRegistry`.
8
+ *
9
+ * @private
10
+ */
11
+ type AIProviders = Parameters<typeof baseCreateProviderRegistry>[0];
12
+
13
+ /**
14
+ * Configuration for {@link createProviderRegistry}.
15
+ */
16
+ export interface ProviderRegistryConfig {
17
+ /**
18
+ * AI SDK provider instances keyed by prefix.
19
+ *
20
+ * When a model ID like `"openai/gpt-4.1"` is resolved, the prefix
21
+ * `"openai"` is extracted and looked up in this map.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * import { createOpenAI } from '@ai-sdk/openai'
26
+ * import { anthropic } from '@ai-sdk/anthropic'
27
+ *
28
+ * const registry = createProviderRegistry({
29
+ * providers: {
30
+ * openai: createOpenAI({ apiKey: '...' }),
31
+ * anthropic,
32
+ * },
33
+ * })
34
+ * ```
35
+ */
36
+ readonly providers: AIProviders;
37
+ }
38
+
39
+ /**
40
+ * A function that resolves a model ID to a {@link LanguageModel} instance.
41
+ *
42
+ * @param modelId - Catalog model identifier in `provider/model` format.
43
+ * @returns The resolved AI SDK {@link LanguageModel}.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const model = registry("openai/gpt-4.1");
48
+ * ```
49
+ */
50
+ export type ProviderRegistry = (modelId: ModelId) => LanguageModel;
51
+
52
+ /**
53
+ * Create a provider registry that resolves `provider/model` string IDs
54
+ * to AI SDK {@link LanguageModel} instances.
55
+ *
56
+ * Thin wrapper around the AI SDK's `createProviderRegistry` that uses
57
+ * `/` as the separator instead of `:`, matching the model ID format used
58
+ * throughout the funkai catalog (e.g. `"openai/gpt-4.1"`).
59
+ *
60
+ * @param config - Provider mappings.
61
+ * @returns A resolver function that maps model IDs to {@link LanguageModel} instances.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * import { createOpenAI } from '@ai-sdk/openai'
66
+ * import { anthropic } from '@ai-sdk/anthropic'
67
+ * import { createProviderRegistry } from '@funkai/models'
68
+ *
69
+ * const registry = createProviderRegistry({
70
+ * providers: {
71
+ * openai: createOpenAI({ apiKey: '...' }),
72
+ * anthropic,
73
+ * },
74
+ * })
75
+ *
76
+ * const m1 = registry('openai/gpt-4.1')
77
+ * const m2 = registry('anthropic/claude-sonnet-4')
78
+ * ```
79
+ */
80
+ export function createProviderRegistry(config: ProviderRegistryConfig): ProviderRegistry {
81
+ const inner = baseCreateProviderRegistry(config.providers, {
82
+ separator: "/",
83
+ });
84
+
85
+ return (modelId: ModelId): LanguageModel => {
86
+ if (!modelId.trim()) {
87
+ throw new Error("Cannot resolve model: model ID is empty");
88
+ }
89
+ // Cast needed: AI SDK overloads expect `provider/model` template literal,
90
+ // but our ModelId is a branded string union. The runtime validates the format.
91
+ return inner.languageModel(modelId as `${string}/${string}`) as LanguageModel;
92
+ };
93
+ }
package/tsdown.config.ts CHANGED
@@ -3,7 +3,6 @@ import { join } from "node:path";
3
3
 
4
4
  import { defineConfig } from "tsdown";
5
5
 
6
- // Read generated entry points (created by generate-models script)
7
6
  const entriesPath = join(import.meta.dirname, ".generated", "entries.json");
8
7
  const generatedEntries: string[] = existsSync(entriesPath)
9
8
  ? JSON.parse(readFileSync(entriesPath, "utf-8"))
@@ -1,4 +0,0 @@
1
-
2
- > @funkai/models@0.2.0 typecheck /Users/zacrosenbauer/Code/joggr/funkai/packages/models
3
- > tsc --noEmit
4
-
@@ -1,125 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- // Mock the external provider before importing the module under test
4
- const mockBaseCreateOpenRouter = vi.fn();
5
- const mockProviderInstance = vi.fn();
6
-
7
- vi.mock("@openrouter/ai-sdk-provider", () => ({
8
- createOpenRouter: mockBaseCreateOpenRouter,
9
- }));
10
-
11
- // Must import after mocking
12
- const { createOpenRouter, openrouter } = await import("@/provider/openrouter.js");
13
-
14
- // ---------------------------------------------------------------------------
15
- // createOpenRouter()
16
- // ---------------------------------------------------------------------------
17
-
18
- describe("createOpenRouter()", () => {
19
- beforeEach(() => {
20
- vi.clearAllMocks();
21
- mockBaseCreateOpenRouter.mockReturnValue(mockProviderInstance);
22
- vi.stubEnv("OPENROUTER_API_KEY", "env-key-123");
23
- });
24
-
25
- afterEach(() => {
26
- vi.unstubAllEnvs();
27
- });
28
-
29
- it("uses explicit apiKey when provided in options", () => {
30
- createOpenRouter({ apiKey: "explicit-key" });
31
-
32
- expect(mockBaseCreateOpenRouter).toHaveBeenCalledWith(
33
- expect.objectContaining({ apiKey: "explicit-key" }),
34
- );
35
- });
36
-
37
- it("falls back to OPENROUTER_API_KEY env var when no apiKey in options", () => {
38
- createOpenRouter();
39
-
40
- expect(mockBaseCreateOpenRouter).toHaveBeenCalledWith(
41
- expect.objectContaining({ apiKey: "env-key-123" }),
42
- );
43
- });
44
-
45
- it("falls back to env var when options are provided without apiKey", () => {
46
- createOpenRouter({});
47
-
48
- expect(mockBaseCreateOpenRouter).toHaveBeenCalledWith(
49
- expect.objectContaining({ apiKey: "env-key-123" }),
50
- );
51
- });
52
-
53
- it("throws when no apiKey provided and env var is not set", () => {
54
- vi.stubEnv("OPENROUTER_API_KEY", undefined as unknown as string);
55
-
56
- expect(() => createOpenRouter()).toThrow("OPENROUTER_API_KEY environment variable is required");
57
- });
58
-
59
- it("forwards additional options to the base provider", () => {
60
- createOpenRouter({ apiKey: "key", baseURL: "https://custom.api" } as Record<string, unknown>);
61
-
62
- expect(mockBaseCreateOpenRouter).toHaveBeenCalledWith(
63
- expect.objectContaining({
64
- apiKey: "key",
65
- baseURL: "https://custom.api",
66
- }),
67
- );
68
- });
69
- });
70
-
71
- // ---------------------------------------------------------------------------
72
- // openrouter() — cached model factory
73
- // ---------------------------------------------------------------------------
74
-
75
- describe("openrouter()", () => {
76
- beforeEach(() => {
77
- vi.clearAllMocks();
78
- mockProviderInstance.mockReturnValue({ modelId: "mock-model" });
79
- mockBaseCreateOpenRouter.mockReturnValue(mockProviderInstance);
80
- });
81
-
82
- afterEach(() => {
83
- vi.unstubAllEnvs();
84
- });
85
-
86
- it("creates a provider and returns a language model", () => {
87
- vi.stubEnv("OPENROUTER_API_KEY", "key-test-create");
88
-
89
- const result = openrouter("openai/gpt-5.2-codex");
90
-
91
- expect(mockBaseCreateOpenRouter).toHaveBeenCalledWith({ apiKey: "key-test-create" });
92
- expect(mockProviderInstance).toHaveBeenCalledWith("openai/gpt-5.2-codex");
93
- expect(result).toEqual({ modelId: "mock-model" });
94
- });
95
-
96
- it("reuses cached provider on subsequent calls with same api key", () => {
97
- vi.stubEnv("OPENROUTER_API_KEY", "key-test-reuse");
98
-
99
- openrouter("openai/gpt-5.2-codex");
100
- openrouter("openai/gpt-5.2");
101
-
102
- // Provider should only be created once since key is the same
103
- expect(mockBaseCreateOpenRouter).toHaveBeenCalledTimes(1);
104
- expect(mockProviderInstance).toHaveBeenCalledTimes(2);
105
- });
106
-
107
- it("creates new provider when api key changes", () => {
108
- vi.stubEnv("OPENROUTER_API_KEY", "key-test-change-1");
109
- openrouter("openai/gpt-5.2-codex");
110
-
111
- vi.stubEnv("OPENROUTER_API_KEY", "key-test-change-2");
112
- openrouter("openai/gpt-5.2");
113
-
114
- expect(mockBaseCreateOpenRouter).toHaveBeenCalledTimes(2);
115
- expect(mockBaseCreateOpenRouter).toHaveBeenLastCalledWith({ apiKey: "key-test-change-2" });
116
- });
117
-
118
- it("throws when OPENROUTER_API_KEY is not set", () => {
119
- vi.stubEnv("OPENROUTER_API_KEY", undefined as unknown as string);
120
-
121
- expect(() => openrouter("openai/gpt-5.2-codex")).toThrow(
122
- "OPENROUTER_API_KEY environment variable is required",
123
- );
124
- });
125
- });