Refactor error handling to config-driven approach with centralized error rules

Made-with: Cursor
This commit is contained in:
decolua
2026-04-15 11:46:47 +07:00
parent b1288c5064
commit b669b6ffc1
20 changed files with 1056 additions and 991 deletions
+5 -20
View File
@@ -1,28 +1,14 @@
// Fetch and cache suggested models for providers that expose a public models API
// Designed to be extensible: add new types in FILTERS below
// Fetches via backend proxy to avoid CORS issues
const CACHE_TTL_MS = 10 * 60 * 1000; // 10 minutes
const cache = new Map(); // key: fetcher.url → { data, expiresAt }
const FILTERS = {
// Free models with context >= 200k tokens
"openrouter-free": (models) =>
models
.filter(
(m) =>
m.pricing?.prompt === "0" &&
m.pricing?.completion === "0" &&
m.context_length >= 200000
)
.map((m) => ({ id: m.id, name: m.name, contextLength: m.context_length }))
.sort((a, b) => b.contextLength - a.contextLength),
};
/**
* 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
* @returns {Promise<Array<{ id: string, name: string, contextLength: number }>>}
* @returns {Promise<Array<{ id: string, name: string, contextLength?: number }>>}
*/
export async function fetchSuggestedModels(fetcher) {
if (!fetcher?.url || !fetcher?.type) return [];
@@ -31,12 +17,11 @@ export async function fetchSuggestedModels(fetcher) {
if (cached && Date.now() < cached.expiresAt) return cached.data;
try {
const res = await fetch(fetcher.url);
const params = new URLSearchParams({ url: fetcher.url, type: fetcher.type });
const res = await fetch(`/api/providers/suggested-models?${params}`);
if (!res.ok) return [];
const json = await res.json();
const raw = json.data ?? json.models ?? json;
const filter = FILTERS[fetcher.type];
const data = filter ? filter(Array.isArray(raw) ? raw : []) : [];
const data = json.data ?? [];
cache.set(fetcher.url, { data, expiresAt: Date.now() + CACHE_TTL_MS });
return data;
} catch {