@@ -13,6 +13,13 @@ import {
1313 fetchProviderCandidates,
1414 type ProviderModelCandidate,
1515 } from "./model-info-fetch.ts";
16+import {
17+ buildModelMatchIndex,
18+ type ModelMatchIndex,
19+ matchCanonicalModelId,
20+ normalizeModelId,
21+ parseModelsDevCapabilities,
22+} from "./model-info-normalize.ts";
1623
1724 const MODELS_DEV_SYNC_KEY = "last_models_dev_sync_at";
1825 const PROVIDER_SYNC_KEY_PREFIX = "last_provider_sync_at:";
@@ -20,6 +27,13 @@ const CACHE_VERSION_KEY = "model_info_cache_version";
2027 const CACHE_VERSION = 2;
2128 export const MODEL_INFO_TTL_MS = 7 * 24 * 60 * 60 * 1000;
2229
30+export {
31+ buildModelMatchIndex,
32+ matchCanonicalModelId,
33+ normalizeModelId,
34+ parseModelsDevCapabilities,
35+} from "./model-info-normalize.ts";
36+
2337 const REMOTE_PROVIDER_ENV_KEYS: ReadonlyArray<{
2438 provider: string;
2539 envKeys: readonly string[];
@@ -75,19 +89,6 @@ interface RuntimeCache {
7589 state: Map<string, string>;
7690 }
7791
78-interface ModelMatchIndex {
79- exact: Map<string, string>;
80- alias: Map<string, string | null>;
81-}
82-
83-interface ModelsDevEntry {
84- canonicalModelId: string;
85- contextWindow: number | null;
86- reasoning: boolean;
87- sourceProvider: string;
88- rawJson: string | null;
89-}
90-
9192 interface ParsedModelString {
9293 provider: string | null;
9394 modelId: string;
@@ -110,10 +111,6 @@ function emptyRuntimeCache(): RuntimeCache {
110111 };
111112 }
112113
113-function isRecord(value: unknown): value is Record<string, unknown> {
114- return typeof value === "object" && value !== null;
115-}
116-
117114 function parseModelStringLoose(modelString: string): ParsedModelString {
118115 const slash = modelString.indexOf("/");
119116 if (slash === -1) {
@@ -128,128 +125,6 @@ function providerModelKey(provider: string, modelId: string): string {
128125 return `${provider}/${modelId}`;
129126 }
130127
131-function basename(value: string): string {
132- const idx = value.lastIndexOf("/");
133- return idx === -1 ? value : value.slice(idx + 1);
134-}
135-
136-export function normalizeModelId(modelId: string): string {
137- let out = modelId.trim().toLowerCase();
138- while (out.startsWith("models/")) {
139- out = out.slice("models/".length);
140- }
141- return out;
142-}
143-
144-function parseContextWindow(model: Record<string, unknown>): number | null {
145- const limit = model.limit;
146- if (!isRecord(limit)) return null;
147- const context = limit.context;
148- if (typeof context !== "number" || !Number.isFinite(context)) return null;
149- return Math.max(0, Math.trunc(context));
150-}
151-
152-export function parseModelsDevCapabilities(
153- payload: unknown,
154- updatedAt: number,
155-): ModelCapabilityRow[] {
156- if (!isRecord(payload)) return [];
157-
158- const merged = new Map<string, ModelsDevEntry>();
159- for (const [provider, providerValue] of Object.entries(payload)) {
160- if (!isRecord(providerValue)) continue;
161- const models = providerValue.models;
162- if (!isRecord(models)) continue;
163- for (const [modelKey, modelValue] of Object.entries(models)) {
164- if (!isRecord(modelValue)) continue;
165- const explicitId =
166- typeof modelValue.id === "string" && modelValue.id.trim().length > 0
167- ? modelValue.id
168- : modelKey;
169- const canonicalModelId = normalizeModelId(explicitId);
170- if (!canonicalModelId) continue;
171- const contextWindow = parseContextWindow(modelValue);
172- const reasoning = modelValue.reasoning === true;
173- const rawJson = JSON.stringify(modelValue);
174- const prev = merged.get(canonicalModelId);
175- if (!prev) {
176- merged.set(canonicalModelId, {
177- canonicalModelId,
178- contextWindow,
179- reasoning,
180- sourceProvider: provider,
181- rawJson,
182- });
183- continue;
184- }
185- merged.set(canonicalModelId, {
186- canonicalModelId,
187- contextWindow: prev.contextWindow ?? contextWindow,
188- reasoning: prev.reasoning || reasoning,
189- sourceProvider: prev.sourceProvider,
190- rawJson: prev.rawJson ?? rawJson,
191- });
192- }
193- }
194-
195- return Array.from(merged.values()).map((entry) => ({
196- canonical_model_id: entry.canonicalModelId,
197- context_window: entry.contextWindow,
198- reasoning: entry.reasoning ? 1 : 0,
199- source_provider: entry.sourceProvider,
200- raw_json: entry.rawJson,
201- updated_at: updatedAt,
202- }));
203-}
204-
205-export function buildModelMatchIndex(
206- canonicalModelIds: Iterable<string>,
207-): ModelMatchIndex {
208- const exact = new Map<string, string>();
209- const aliasCandidates = new Map<string, Set<string>>();
210-
211- for (const rawCanonical of canonicalModelIds) {
212- const canonical = normalizeModelId(rawCanonical);
213- if (!canonical) continue;
214- exact.set(canonical, canonical);
215- const short = basename(canonical);
216- if (!short) continue;
217- let set = aliasCandidates.get(short);
218- if (!set) {
219- set = new Set<string>();
220- aliasCandidates.set(short, set);
221- }
222- set.add(canonical);
223- }
224-
225- const alias = new Map<string, string | null>();
226- for (const [short, candidates] of aliasCandidates) {
227- if (candidates.size === 1) {
228- for (const value of candidates) {
229- alias.set(short, value);
230- }
231- } else {
232- alias.set(short, null);
233- }
234- }
235-
236- return { exact, alias };
237-}
238-
239-export function matchCanonicalModelId(
240- providerModelId: string,
241- index: ModelMatchIndex,
242-): string | null {
243- const normalized = normalizeModelId(providerModelId);
244- if (!normalized) return null;
245- const exactMatch = index.exact.get(normalized);
246- if (exactMatch) return exactMatch;
247- const short = basename(normalized);
248- if (!short) return null;
249- const alias = index.alias.get(short);
250- return alias ?? null;
251-}
252-
253128 export function isStaleTimestamp(
254129 timestamp: number | null,
255130 now = Date.now(),