(modelId: string)
| 249 | * Get pricing information for a specific model (async - fetches if needed) |
| 250 | */ |
| 251 | export async function getModelPricingAsync(modelId: string): Promise<ModelPricing | undefined> { |
| 252 | const modelName = extractModelId(modelId) |
| 253 | const normalized = normalizeModelId(modelName) |
| 254 | const codexBase = codexBaseModelId(modelName) |
| 255 | const normalizedCodexBase = codexBase ? normalizeModelId(codexBase) : null |
| 256 | |
| 257 | // Check fallback first |
| 258 | if (FALLBACK_PRICING[modelName]) { |
| 259 | return FALLBACK_PRICING[modelName] |
| 260 | } |
| 261 | if (FALLBACK_PRICING[normalized]) { |
| 262 | return FALLBACK_PRICING[normalized] |
| 263 | } |
| 264 | if (codexBase && FALLBACK_PRICING[codexBase]) { |
| 265 | return FALLBACK_PRICING[codexBase] |
| 266 | } |
| 267 | if (normalizedCodexBase && FALLBACK_PRICING[normalizedCodexBase]) { |
| 268 | return FALLBACK_PRICING[normalizedCodexBase] |
| 269 | } |
| 270 | |
| 271 | const data = await fetchModelsDevData() |
| 272 | |
| 273 | // Search all providers for the model, prefer non-zero pricing |
| 274 | let zeroCostPricing: ModelPricing | undefined |
| 275 | |
| 276 | for (const provider of Object.values(data)) { |
| 277 | // Try exact match first |
| 278 | let model = provider.models[modelName] |
| 279 | // Try normalized match if exact fails |
| 280 | if (!model) { |
| 281 | for (const [key, value] of Object.entries(provider.models)) { |
| 282 | if (normalizeModelId(key) === normalized) { |
| 283 | model = value |
| 284 | break |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | if (!model && codexBase) { |
| 289 | model = provider.models[codexBase] |
| 290 | if (!model) { |
| 291 | for (const [key, value] of Object.entries(provider.models)) { |
| 292 | if (normalizeModelId(key) === normalizedCodexBase) { |
| 293 | model = value |
| 294 | break |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (model?.cost) { |
| 301 | const pricing = { |
| 302 | input: model.cost.input, |
| 303 | output: model.cost.output, |
| 304 | cacheWrite: model.cost.cache_write, |
| 305 | cacheRead: model.cost.cache_read, |
| 306 | } |
| 307 | |
| 308 | // If we find non-zero pricing, return it immediately |
no test coverage detected