@@ -428,32 +428,44 @@ async function fetchZenModels(): Promise<ProviderModelCandidate[] | null> {
428428 { headers: { Authorization: `Bearer ${key}` } },
429429 8_000,
430430 );
431- if (!isRecord(payload)) return null;
432- const data = payload.data;
433- if (!Array.isArray(data)) return null;
434- const out: ProviderModelCandidate[] = [];
435- for (const item of data) {
436- if (!isRecord(item) || typeof item.id !== "string") continue;
437- const modelId = normalizeModelId(item.id);
438- if (!modelId) continue;
431+ return processModelsList(payload, "data", "id", (item, modelId) => {
439432 const contextWindow =
440433 typeof item.context_window === "number" &&
441434 Number.isFinite(item.context_window)
442435 ? Math.max(0, Math.trunc(item.context_window))
443436 : null;
444- out.push({
437+ return {
445438 providerModelId: modelId,
446- displayName: item.id,
439+ displayName: item.id as string,
447440 contextWindow,
448441 free:
449- item.id.endsWith("-free") ||
442+ (item.id as string).endsWith("-free") ||
450443 item.id === "gpt-5-nano" ||
451444 item.id === "big-pickle",
452- });
445+ };
446+ });
447+}
448+
449+function processModelsList(
450+ payload: unknown,
451+ arrayKey: string,
452+ idKey: string,
453+ mapper: (
454+ item: Record<string, unknown>,
455+ modelId: string,
456+ ) => ProviderModelCandidate | null,
457+): ProviderModelCandidate[] | null {
458+ if (!isRecord(payload) || !Array.isArray(payload[arrayKey])) return null;
459+ const out: ProviderModelCandidate[] = [];
460+ for (const item of payload[arrayKey] as unknown[]) {
461+ if (!isRecord(item) || typeof item[idKey] !== "string") continue;
462+ const modelId = normalizeModelId(item[idKey] as string);
463+ if (!modelId) continue;
464+ const mapped = mapper(item as Record<string, unknown>, modelId);
465+ if (mapped) out.push(mapped);
453466 }
454467 return out;
455468 }
456-
457469 async function fetchOpenAIModels(): Promise<ProviderModelCandidate[] | null> {
458470 const key = process.env.OPENAI_API_KEY;
459471 if (!key) return null;
@@ -462,20 +474,12 @@ async function fetchOpenAIModels(): Promise<ProviderModelCandidate[] | null> {
462474 { headers: { Authorization: `Bearer ${key}` } },
463475 6_000,
464476 );
465- if (!isRecord(payload) || !Array.isArray(payload.data)) return null;
466- const out: ProviderModelCandidate[] = [];
467- for (const item of payload.data) {
468- if (!isRecord(item) || typeof item.id !== "string") continue;
469- const modelId = normalizeModelId(item.id);
470- if (!modelId) continue;
471- out.push({
472- providerModelId: modelId,
473- displayName: item.id,
474- contextWindow: null,
475- free: false,
476- });
477- }
478- return out;
477+ return processModelsList(payload, "data", "id", (item, modelId) => ({
478+ providerModelId: modelId,
479+ displayName: item.id as string,
480+ contextWindow: null,
481+ free: false,
482+ }));
479483 }
480484
481485 async function fetchAnthropicModels(): Promise<
@@ -493,25 +497,19 @@ async function fetchAnthropicModels(): Promise<
493497 },
494498 6_000,
495499 );
496- if (!isRecord(payload) || !Array.isArray(payload.data)) return null;
497- const out: ProviderModelCandidate[] = [];
498- for (const item of payload.data) {
499- if (!isRecord(item) || typeof item.id !== "string") continue;
500- const modelId = normalizeModelId(item.id);
501- if (!modelId) continue;
500+ return processModelsList(payload, "data", "id", (item, modelId) => {
502501 const displayName =
503502 typeof item.display_name === "string" &&
504503 item.display_name.trim().length > 0
505504 ? item.display_name
506- : item.id;
507- out.push({
505+ : (item.id as string);
506+ return {
508507 providerModelId: modelId,
509508 displayName,
510509 contextWindow: null,
511510 free: false,
512- });
513- }
514- return out;
511+ };
512+ });
515513 }
516514
517515 async function fetchGoogleModels(): Promise<ProviderModelCandidate[] | null> {
@@ -522,12 +520,7 @@ async function fetchGoogleModels(): Promise<ProviderModelCandidate[] | null> {
522520 {},
523521 6_000,
524522 );
525- if (!isRecord(payload) || !Array.isArray(payload.models)) return null;
526- const out: ProviderModelCandidate[] = [];
527- for (const item of payload.models) {
528- if (!isRecord(item) || typeof item.name !== "string") continue;
529- const modelId = normalizeModelId(item.name);
530- if (!modelId) continue;
523+ return processModelsList(payload, "models", "name", (item, modelId) => {
531524 const displayName =
532525 typeof item.displayName === "string" && item.displayName.trim().length > 0
533526 ? item.displayName
@@ -537,38 +530,31 @@ async function fetchGoogleModels(): Promise<ProviderModelCandidate[] | null> {
537530 Number.isFinite(item.inputTokenLimit)
538531 ? Math.max(0, Math.trunc(item.inputTokenLimit))
539532 : null;
540- out.push({
533+ return {
541534 providerModelId: modelId,
542535 displayName,
543536 contextWindow,
544537 free: false,
545- });
546- }
547- return out;
538+ };
539+ });
548540 }
549541
550542 async function fetchOllamaModels(): Promise<ProviderModelCandidate[] | null> {
551543 const base = process.env.OLLAMA_BASE_URL ?? "http://localhost:11434";
552544 const payload = await fetchJson(`${base}/api/tags`, {}, 3_000);
553- if (!isRecord(payload) || !Array.isArray(payload.models)) return null;
554- const out: ProviderModelCandidate[] = [];
555- for (const item of payload.models) {
556- if (!isRecord(item) || typeof item.name !== "string") continue;
557- const modelId = normalizeModelId(item.name);
558- if (!modelId) continue;
545+ return processModelsList(payload, "models", "name", (item, modelId) => {
559546 const details = item.details;
560547 let sizeSuffix = "";
561548 if (isRecord(details) && typeof details.parameter_size === "string") {
562549 sizeSuffix = ` (${details.parameter_size})`;
563550 }
564- out.push({
551+ return {
565552 providerModelId: modelId,
566- displayName: `${item.name}${sizeSuffix}`,
553+ displayName: `${item.name as string}${sizeSuffix}`,
567554 contextWindow: null,
568555 free: false,
569- });
570- }
571- return out;
556+ };
557+ });
572558 }
573559
574560 async function fetchProviderCandidates(