|
18 | 18 | // cache on it meant nothing was ever cached and every load fell through to a |
19 | 19 | // network fetch, which hangs in sandboxed CI. So the check asserts only what |
20 | 20 | // consumers actually rely on: a non-empty map whose values are provider objects |
21 | | -// carrying a `models` map. |
| 21 | +// carrying a string `id` and a `models` map. |
22 | 22 | // |
23 | 23 | // Module shape follows packages/opencode/AGENTS.md: flat exports plus a |
24 | 24 | // self-reexport, not `export namespace`. |
25 | 25 |
|
| 26 | +// altimate_change start — upstream_fix: `isCatalog` only required one entry in |
| 27 | +// the map to be well-formed before caching (and trusting) the whole catalog. |
| 28 | +// `Provider.state()` then unconditionally maps every entry through |
| 29 | +// `fromModelsDevProvider`, which reads `provider.id` and `provider.models` |
| 30 | +// with no validation of its own — a single malformed entry (e.g. from a |
| 31 | +// misbehaving proxy mixing garbage with real data) crashed `Provider.state()` |
| 32 | +// for every provider, not just the bad one. `isCatalogEntry` is the per-entry |
| 33 | +// predicate, extracted so callers can filter individual entries with the same |
| 34 | +// rule `isCatalog` uses to gate the whole map. |
| 35 | +/** True when `entry` is shaped like a single models.dev provider entry. */ |
| 36 | +export function isCatalogEntry(entry: unknown): entry is { id: string; models: Record<string, unknown> } { |
| 37 | + if (!entry || typeof entry !== "object" || Array.isArray(entry)) return false |
| 38 | + const id = Reflect.get(entry, "id") |
| 39 | + if (typeof id !== "string") return false |
| 40 | + const models = Reflect.get(entry, "models") |
| 41 | + return !!models && typeof models === "object" && !Array.isArray(models) |
| 42 | +} |
| 43 | +// altimate_change end |
| 44 | + |
26 | 45 | /** True when `value` is shaped like a models.dev catalog rather than junk. */ |
27 | 46 | export function isCatalog(value: unknown): value is Record<string, unknown> { |
28 | 47 | if (!value || typeof value !== "object" || Array.isArray(value)) return false |
29 | 48 | const entries = Object.values(value) |
30 | 49 | if (entries.length === 0) return false |
31 | | - return entries.some((entry) => { |
32 | | - if (!entry || typeof entry !== "object" || Array.isArray(entry)) return false |
33 | | - const models = Reflect.get(entry, "models") |
34 | | - return !!models && typeof models === "object" && !Array.isArray(models) |
35 | | - }) |
| 50 | + return entries.some(isCatalogEntry) |
36 | 51 | } |
37 | 52 |
|
38 | 53 | /** Parse a models.dev response body, returning undefined unless it is a catalog. */ |
|
0 commit comments