|
| 1 | +// Live model catalog for the Meta Model API, with static fallback. |
| 2 | +// |
| 3 | +// buildProvider resolves the subscription key on every call (explicit env |
| 4 | +// wins, otherwise a fresh cache read, so no gateway restart is needed after |
| 5 | +// the first login), fetches GET {baseUrl}/models with a short TTL, filters |
| 6 | +// non-chat families, and falls back to the static baseline on any failure: |
| 7 | +// missing key, network error, or unexpected shape. |
| 8 | + |
| 9 | +import { defaultCachePath, explicitToken } from "./config.js"; |
| 10 | +import { readCacheSync } from "./auth.js"; |
| 11 | + |
| 12 | +export const MODELS_PATH = "/models"; |
| 13 | +export const LIVE_CATALOG_TTL_MS = 60_000; |
| 14 | +export const FETCH_TIMEOUT_MS = 8_000; |
| 15 | + |
| 16 | +// Allowlist: this provider serves the Muse Spark chat family. The live |
| 17 | +// endpoint also returns other families (sam-*, muse-image-*, muse-voice-*) |
| 18 | +// that are not usable as chat/completions models — a denylist would leak |
| 19 | +// the next such family, so only muse-spark-* is admitted. Anything else |
| 20 | +// still resolves through the dynamic resolver if requested explicitly. |
| 21 | +const CHAT_FAMILY_PREFIX = "muse-spark-"; |
| 22 | + |
| 23 | +type ModelsResponse = { |
| 24 | + data?: unknown; |
| 25 | +}; |
| 26 | + |
| 27 | +type CachedEntry = { |
| 28 | + ids: string[]; |
| 29 | + expiresAt: number; |
| 30 | +}; |
| 31 | + |
| 32 | +let cache: CachedEntry | null = null; |
| 33 | + |
| 34 | +/** Test-only cache reset. */ |
| 35 | +export function __clearLiveCatalogCache(): void { |
| 36 | + cache = null; |
| 37 | +} |
| 38 | + |
| 39 | +/** Resolve the subscription key: explicit env wins, else a fresh cache read. */ |
| 40 | +export function resolveKey(): string { |
| 41 | + return explicitToken() || readCacheSync(defaultCachePath()); |
| 42 | +} |
| 43 | + |
| 44 | +function parseModelIds(body: unknown): string[] | null { |
| 45 | + if (typeof body !== "object" || body === null) return null; |
| 46 | + const data = (body as ModelsResponse).data; |
| 47 | + if (!Array.isArray(data)) return null; |
| 48 | + const ids = data |
| 49 | + .filter((row): row is { id: string } => { |
| 50 | + return ( |
| 51 | + typeof row === "object" && |
| 52 | + row !== null && |
| 53 | + typeof (row as { id?: unknown }).id === "string" && |
| 54 | + ((row as { id: string }).id.length > 0) |
| 55 | + ); |
| 56 | + }) |
| 57 | + .map((row) => row.id) |
| 58 | + .filter((id) => id.startsWith(CHAT_FAMILY_PREFIX)); |
| 59 | + return ids.length > 0 ? ids : null; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Fetch the live chat-model id list. Returns null on any failure |
| 64 | + * (network, auth, unexpected shape, empty list) — callers fall back |
| 65 | + * to the static baseline. |
| 66 | + */ |
| 67 | +export async function fetchLiveModelIds( |
| 68 | + baseUrl: string, |
| 69 | + apiKey: string, |
| 70 | + timeoutMs: number = FETCH_TIMEOUT_MS, |
| 71 | +): Promise<string[] | null> { |
| 72 | + if (!apiKey) return null; |
| 73 | + try { |
| 74 | + const res = await fetch(`${baseUrl}${MODELS_PATH}`, { |
| 75 | + headers: { |
| 76 | + Accept: "application/json", |
| 77 | + Authorization: `Bearer ${apiKey}`, |
| 78 | + }, |
| 79 | + signal: AbortSignal.timeout(timeoutMs), |
| 80 | + }); |
| 81 | + if (!res.ok) return null; |
| 82 | + return parseModelIds((await res.json()) as unknown); |
| 83 | + } catch { |
| 84 | + return null; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** TTL-cached live fetch. Returns null on miss/expiry+failure (use static). */ |
| 89 | +export async function getLiveModelIdsCached( |
| 90 | + baseUrl: string, |
| 91 | + apiKey: string, |
| 92 | + ttlMs: number = LIVE_CATALOG_TTL_MS, |
| 93 | +): Promise<string[] | null> { |
| 94 | + if (cache && Date.now() < cache.expiresAt) return cache.ids; |
| 95 | + const ids = await fetchLiveModelIds(baseUrl, apiKey); |
| 96 | + if (!ids) { |
| 97 | + cache = null; |
| 98 | + return null; |
| 99 | + } |
| 100 | + cache = { ids, expiresAt: Date.now() + Math.max(0, ttlMs) }; |
| 101 | + return ids; |
| 102 | +} |
0 commit comments