| 1 | /** Pure website projection of the Engine's committed descriptor identities. */ |
| 2 | export function parseProviderDescriptors(input) { |
| 3 | let data; |
| 4 | try { data = typeof input === "string" ? JSON.parse(input) : input; } catch { return null; } |
| 5 | if (!data || data.schema_version !== 3 || !Array.isArray(data.providers) || !data.providers.length) return null; |
| 6 | const labels = Object.create(null); |
| 7 | const ids = new Set(), kinds = new Set(), tags = new Set(), orders = new Set(); |
| 8 | const publicRows = []; |
| 9 | for (const row of data.providers) { |
| 10 | if (!row || typeof row.id !== "string" || !row.id || typeof row.kind !== "string" || !row.kind || |
| 11 | typeof row.label !== "string" || !row.label || typeof row.default_model !== "string" || |
| 12 | typeof row.retired !== "boolean" || typeof row.selectable !== "boolean" || |
| 13 | !Array.isArray(row.env_vars) || !row.env_vars.every(v => typeof v === "string") || |
| 14 | typeof row.tui_wire_tag !== "string" || !row.tui_wire_tag || tags.has(row.tui_wire_tag) || |
| 15 | typeof row.catalog_id !== "string" || !row.catalog_id || |
| 16 | typeof row.catalog_source_id !== "string" || !row.catalog_source_id || |
| 17 | ids.has(row.id) || kinds.has(row.kind)) return null; |
| 18 | ids.add(row.id); kinds.add(row.kind); tags.add(row.tui_wire_tag); |
| 19 | if (row.retired || row.kind === "Antigravity" || row.kind === "Custom") continue; |
| 20 | if (!row.web || !Number.isSafeInteger(row.web.order) || row.web.order < 0 || orders.has(row.web.order) || |
| 21 | Object.hasOwn(row.web, "variant") || |
| 22 | (row.web.label !== undefined && typeof row.web.label !== "string") || |
| 23 | (row.web.env !== undefined && typeof row.web.env !== "string")) return null; |
| 24 | orders.add(row.web.order); |
| 25 | const fact = { id: row.id, label: row.web.label ?? row.label, env: row.web.env ?? row.env_vars.join(" / ") }; |
| 26 | labels[row.id] = fact; |
| 27 | publicRows.push({ order: row.web.order, fact }); |
| 28 | } |
| 29 | if (data.providers.some(row => !ids.has(row.catalog_id) || !ids.has(row.catalog_source_id))) return null; |
| 30 | publicRows.sort((a,b) => a.order - b.order); |
| 31 | if (!publicRows.length || publicRows.some((row,index) => row.order !== index)) return null; |
| 32 | const ref = data.constant_refs?.DEFAULT_TEXT_MODEL; |
| 33 | if (!ref || ref.field !== "default_model" || typeof ref.provider !== "string") return null; |
| 34 | const defaultRow = data.providers.find(row => row.id === ref.provider); |
| 35 | if (!defaultRow || defaultRow.retired || !defaultRow.default_model) return null; |
| 36 | return { labels, providers: publicRows.map(row => row.fact), defaultModel: defaultRow.default_model }; |
| 37 | } |
| 38 |