mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-16 08:17:11 +00:00
fix(pricing): guard malformed models.dev model entries (#1344)
* fix(pricing): guard malformed models.dev model entries * style: apply prettier formatting
This commit is contained in:
@@ -91,6 +91,7 @@ function findModel(provider: ModelsDevProvider, model: string): ModelsDevModel |
|
||||
|
||||
const normalizedEntries = new Map<string, ModelsDevModel>();
|
||||
for (const [key, value] of Object.entries(models)) {
|
||||
if (!value || typeof value !== 'object') continue;
|
||||
normalizedEntries.set(normalizeId(key), value);
|
||||
if (typeof value.id === 'string') normalizedEntries.set(normalizeId(value.id), value);
|
||||
}
|
||||
@@ -188,6 +189,7 @@ export function getKnownModelsDevModels(): string[] {
|
||||
const ids = new Set<string>();
|
||||
for (const provider of Object.values(registry)) {
|
||||
for (const model of Object.values(provider.models ?? {})) {
|
||||
if (!model || typeof model !== 'object') continue;
|
||||
if (typeof model.id === 'string') ids.add(`${provider.id}/${model.id}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,6 +392,26 @@ describe('model-pricing', () => {
|
||||
expect(pricing.outputPerMillion).toBe(0);
|
||||
});
|
||||
|
||||
it('ignores malformed models.dev entries during provider-aware lookups', () => {
|
||||
setCachedModelsDevRegistry({
|
||||
openai: {
|
||||
id: 'openai',
|
||||
name: 'OpenAI',
|
||||
models: {
|
||||
bad: null as unknown as never,
|
||||
'gpt-4o': {
|
||||
id: 'gpt-4o',
|
||||
name: 'GPT-4o',
|
||||
cost: { input: 2.5, output: 10 },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(() => getModelPricing('gpt-4o', { provider: 'openai' })).not.toThrow();
|
||||
expect(getModelPricing('gpt-4o', { provider: 'openai' }).inputPerMillion).toBe(2.5);
|
||||
});
|
||||
|
||||
it('prefers provider-aware models.dev pricing over exact static table matches', () => {
|
||||
const pricing = getModelPricing('gpt-4o', { provider: 'github-copilot' });
|
||||
expect(pricing.inputPerMillion).toBe(0);
|
||||
|
||||
Reference in New Issue
Block a user