返回 CodeWhale
model-catalog.mjs
根目录 / web / lib / model-catalog.mjs
1 /** Public model presentation from the same reviewed Engine catalog owner.
2 * Live/configured/account inventories never enter this projection. */
3 export function parseModelCatalog(input) {
4 let data;
5 try { data = typeof input === "string" ? JSON.parse(input) : input; } catch { return null; }
6 const reviewed = data?._reviewed;
7 if (!reviewed || typeof reviewed.revision !== "string" || !reviewed.revision ||
8 !reviewed.intrinsic || typeof reviewed.intrinsic !== "object" || Array.isArray(reviewed.intrinsic) ||
9 !Array.isArray(reviewed.public_models) || !reviewed.public_models.length) return null;
10 const safeText = value => typeof value === "string" && value.length > 0 && value.length <= 512 &&
11 value.trim() === value && !/[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/u.test(value);
12 const nullableNumber = value => value === undefined || value === null ||
13 (Number.isSafeInteger(value) && value > 0 && value <= 0xffffffff);
14 const validDate = value => typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value) &&
15 Number.isFinite(Date.parse(`${value}T00:00:00Z`)) && new Date(`${value}T00:00:00Z`).toISOString().slice(0, 10) === value;
16 const rows = [];
17 const ids = new Set();
18 for (const row of reviewed.public_models) {
19 if (!row || !safeText(row.id) || ids.has(row.id) ||
20 !(row.label === null || safeText(row.label)) ||
21 !(row.added_at === null || validDate(row.added_at)) ||
22 !Array.isArray(row.aliases) || !row.aliases.every(safeText)) return null;
23 const fact = reviewed.intrinsic[row.id.toLowerCase()];
24 if (!fact || !safeText(fact.source) || !nullableNumber(fact.context_window) || !nullableNumber(fact.max_output) ||
25 !(fact.reasoning === undefined || fact.reasoning === null || typeof fact.reasoning === "boolean")) return null;
26 ids.add(row.id);
27 rows.push({ id: row.id, provider: row.label, contextWindow: fact.context_window ?? null,
28 maxOutput: fact.max_output ?? null, reasoning: fact.reasoning === true, addedAt: row.added_at });
29 }
30 rows.sort((a, b) => a.addedAt && b.addedAt && a.addedAt !== b.addedAt ? b.addedAt.localeCompare(a.addedAt) :
31 a.addedAt !== b.addedAt ? a.addedAt ? -1 : 1 : a.id.localeCompare(b.id));
32 return rows;
33 }
34
34 lines Plain Text