mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-13 16:14:31 +00:00
fix(models-dev): sanitize models.dev registry entries and harden pricing resolver (#1345)
* fix(models-dev): sanitize cached model entries before pricing lookup * style: apply prettier formatting
This commit is contained in:
1 parent
e2ef66d7d6
commit
9cd9b796ca
4 files changed
+65
-5
No files matched your search
@@ -36,6 +36,10 @@ function normalizeId(value: string): string {
|
||||
return value.trim().toLowerCase();
|
||||
}
|
||||
|
||||
function isModelEntry(value: unknown): value is ModelsDevModel {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function normalizeModelsDevProviderId(
|
||||
provider: string | null | undefined
|
||||
): string | undefined {
|
||||
@@ -91,7 +95,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;
|
||||
if (!isModelEntry(value)) continue;
|
||||
normalizedEntries.set(normalizeId(key), value);
|
||||
if (typeof value.id === 'string') normalizedEntries.set(normalizeId(value.id), value);
|
||||
}
|
||||
|
||||
@@ -38,10 +38,20 @@ function normalizeRegistryPayload(payload: unknown): ModelsDevRegistry | null {
|
||||
for (const [key, value] of Object.entries(payload)) {
|
||||
if (!isPlainObject(value)) continue;
|
||||
const id = typeof value.id === 'string' && value.id.trim() ? value.id.trim() : key;
|
||||
const models = isPlainObject(value.models)
|
||||
? (value.models as NonNullable<ModelsDevProvider['models']>)
|
||||
: undefined;
|
||||
if (!models || Object.keys(models).length === 0) continue;
|
||||
const modelsPayload = isPlainObject(value.models) ? value.models : undefined;
|
||||
if (!modelsPayload) continue;
|
||||
|
||||
const models: NonNullable<ModelsDevProvider['models']> = {};
|
||||
for (const [modelKey, modelValue] of Object.entries(modelsPayload)) {
|
||||
if (!isPlainObject(modelValue)) continue;
|
||||
const modelId =
|
||||
typeof modelValue.id === 'string' && modelValue.id.trim() ? modelValue.id.trim() : modelKey;
|
||||
models[modelKey] = {
|
||||
...(modelValue as NonNullable<ModelsDevProvider['models']>[string]),
|
||||
id: modelId,
|
||||
};
|
||||
}
|
||||
if (Object.keys(models).length === 0) continue;
|
||||
|
||||
providers[id] = {
|
||||
...(value as ModelsDevProvider),
|
||||
|
||||
Reference in new issue
Block a user