2026-03-30 00:28:07 -04:00
|
|
|
// Fetch and cache suggested models for providers that expose a public models API
|
2026-04-14 23:45:46 -04:00
|
|
|
// Fetches via backend proxy to avoid CORS issues
|
2026-03-30 00:28:07 -04:00
|
|
|
|
|
|
|
|
const CACHE_TTL_MS = 10 * 60 * 1000; // 10 minutes
|
|
|
|
|
const cache = new Map(); // key: fetcher.url → { data, expiresAt }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch suggested models for a provider using its modelsFetcher config.
|
|
|
|
|
* Results are cached in-memory for CACHE_TTL_MS.
|
|
|
|
|
* @param {{ url: string, type: string }} fetcher
|
2026-04-14 23:45:46 -04:00
|
|
|
* @returns {Promise<Array<{ id: string, name: string, contextLength?: number }>>}
|
2026-03-30 00:28:07 -04:00
|
|
|
*/
|
|
|
|
|
export async function fetchSuggestedModels(fetcher) {
|
|
|
|
|
if (!fetcher?.url || !fetcher?.type) return [];
|
|
|
|
|
|
|
|
|
|
const cached = cache.get(fetcher.url);
|
|
|
|
|
if (cached && Date.now() < cached.expiresAt) return cached.data;
|
|
|
|
|
|
|
|
|
|
try {
|
2026-04-14 23:45:46 -04:00
|
|
|
const params = new URLSearchParams({ url: fetcher.url, type: fetcher.type });
|
|
|
|
|
const res = await fetch(`/api/providers/suggested-models?${params}`);
|
2026-03-30 00:28:07 -04:00
|
|
|
if (!res.ok) return [];
|
|
|
|
|
const json = await res.json();
|
2026-04-14 23:45:46 -04:00
|
|
|
const data = json.data ?? [];
|
2026-03-30 00:28:07 -04:00
|
|
|
cache.set(fetcher.url, { data, expiresAt: Date.now() + CACHE_TTL_MS });
|
|
|
|
|
return data;
|
|
|
|
|
} catch {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|