| 25 | |
| 26 | /** Fetches a models.dev-style catalog. Public endpoint, no credentials needed. */ |
| 27 | export async function fetchCatalog( |
| 28 | url: string, |
| 29 | signal?: AbortSignal, |
| 30 | fetchImpl: typeof fetch = fetch, |
| 31 | ): Promise<Catalog> { |
| 32 | const res = await fetchImpl(url, { headers: { Accept: 'application/json' }, signal }); |
| 33 | if (!res.ok) { |
| 34 | throw new CatalogFetchError(`Failed to fetch catalog (HTTP ${res.status}).`, res.status); |
| 35 | } |
| 36 | const payload: unknown = await res.json(); |
| 37 | if (typeof payload !== 'object' || payload === null || Array.isArray(payload)) { |
| 38 | throw new Error(`Unexpected catalog response from ${url}.`); |
| 39 | } |
| 40 | return payload as Catalog; |
| 41 | } |
| 42 | |
| 43 | function capabilityToStrings(capability: ModelCapability): string[] | undefined { |
| 44 | const caps: string[] = []; |