mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-12 20:15:40 +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:
1 parent
af7aa0c2dd
commit
66966f3527
2 files changed
+22
No files matched your search
@@ -91,6 +91,7 @@ function findModel(provider: ModelsDevProvider, model: string): ModelsDevModel |
|
|||||||
|
|
||||||
const normalizedEntries = new Map<string, ModelsDevModel>();
|
const normalizedEntries = new Map<string, ModelsDevModel>();
|
||||||
for (const [key, value] of Object.entries(models)) {
|
for (const [key, value] of Object.entries(models)) {
|
||||||
|
if (!value || typeof value !== 'object') continue;
|
||||||
normalizedEntries.set(normalizeId(key), value);
|
normalizedEntries.set(normalizeId(key), value);
|
||||||
if (typeof value.id === 'string') normalizedEntries.set(normalizeId(value.id), 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>();
|
const ids = new Set<string>();
|
||||||
for (const provider of Object.values(registry)) {
|
for (const provider of Object.values(registry)) {
|
||||||
for (const model of Object.values(provider.models ?? {})) {
|
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}`);
|
if (typeof model.id === 'string') ids.add(`${provider.id}/${model.id}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -392,6 +392,26 @@ describe('model-pricing', () => {
|
|||||||
expect(pricing.outputPerMillion).toBe(0);
|
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', () => {
|
it('prefers provider-aware models.dev pricing over exact static table matches', () => {
|
||||||
const pricing = getModelPricing('gpt-4o', { provider: 'github-copilot' });
|
const pricing = getModelPricing('gpt-4o', { provider: 'github-copilot' });
|
||||||
expect(pricing.inputPerMillion).toBe(0);
|
expect(pricing.inputPerMillion).toBe(0);
|
||||||
|
|||||||
Reference in new issue
Block a user