Q84: live model lists follow every page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 14:23:01 -04:00
1 parent 665bf3cae1
commit 9599d0aecd
2 files changed
+73 -11

No files matched your search

+34 -9
View File
@@ -9,30 +9,55 @@ import { getJson, type JsonRecord } from "./provider-http";
* rather than falling back to a stale static list.
*/
const MAX_MODEL_PAGES = 50;
export function mapOpenAiModels(data: unknown): ProviderModelEntry[] {
return collectModels(data, (item, id) => ({ id, name: id }));
return collectModels(modelItems(data), (item, id) => ({ id, name: id }));
}
export function mapAnthropicModels(data: unknown): ProviderModelEntry[] {
return collectModels(data, (item, id) => ({
return collectModels(modelItems(data), (item, id) => ({
id,
name: typeof item.display_name === "string" && item.display_name.trim() ? item.display_name.trim() : id,
}));
}
/** OpenAI-style lists come whole; one that says has_more is followed through OpenAI's `after` cursor. */
export async function fetchOpenAiModels(modelsUrl: string, headers: Record<string, string>): Promise<ProviderModelEntry[]> {
const response = await getJson(modelsUrl, headers);
return mapOpenAiModels(response.data);
return mapOpenAiModels({ data: await fetchAllPages(modelsUrl, headers, (cursor) => withQuery(modelsUrl, { after: cursor })) });
}
/** Anthropic pages 20 models by default; ask for its largest page and follow `after_id` to the end. */
export async function fetchAnthropicModels(modelsUrl: string, headers: Record<string, string>): Promise<ProviderModelEntry[]> {
const response = await getJson(modelsUrl, headers);
return mapAnthropicModels(response.data);
const first = withQuery(modelsUrl, { limit: "1000" });
return mapAnthropicModels({ data: await fetchAllPages(first, headers, (cursor) => withQuery(modelsUrl, { limit: "1000", after_id: cursor })) });
}
function collectModels(data: unknown, toEntry: (item: JsonRecord, id: string) => ProviderModelEntry): ProviderModelEntry[] {
const list = isRecord(data) && Array.isArray(data.data) ? data.data : null;
if (!list) throw new Error("Provider model list response was not in the expected shape.");
async function fetchAllPages(firstUrl: string, headers: Record<string, string>, nextUrl: (cursor: string) => string): Promise<unknown[]> {
const items: unknown[] = [];
let url = firstUrl;
for (let page = 1; page <= MAX_MODEL_PAGES; page += 1) {
const { data } = await getJson(url, headers);
items.push(...modelItems(data));
if (!isRecord(data) || data.has_more !== true) return items;
if (typeof data.last_id !== "string" || !data.last_id) throw new Error("The provider said it has more models but no cursor to fetch them.");
url = nextUrl(data.last_id);
}
throw new Error(`The provider's model list ran past ${MAX_MODEL_PAGES} pages.`);
}
function withQuery(url: string, params: Record<string, string>): string {
const target = new URL(url);
for (const [name, value] of Object.entries(params)) target.searchParams.set(name, value);
return target.toString();
}
function modelItems(data: unknown): unknown[] {
if (!isRecord(data) || !Array.isArray(data.data)) throw new Error("Provider model list response was not in the expected shape.");
return data.data;
}
function collectModels(list: unknown[], toEntry: (item: JsonRecord, id: string) => ProviderModelEntry): ProviderModelEntry[] {
const entries: ProviderModelEntry[] = [];
const seen = new Set<string>();
for (const item of list) {