@@ -8,6 +8,15 @@ import {
88 replaceProviderModels,
99 setModelInfoState,
1010 } from "../session/db/model-info-repo.ts";
11+import {
12+ buildRuntimeCache,
13+ emptyRuntimeCache,
14+ type LiveModel,
15+ type ModelInfo,
16+ type RuntimeCache,
17+ readLiveModelsFromCache,
18+ resolveModelInfoInCache,
19+} from "./model-info-cache.ts";
1120 import {
1221 fetchModelsDevPayload,
1322 fetchProviderCandidates,
@@ -17,7 +26,6 @@ import {
1726 buildModelMatchIndex,
1827 type ModelMatchIndex,
1928 matchCanonicalModelId,
20- normalizeModelId,
2129 parseModelsDevCapabilities,
2230 } from "./model-info-normalize.ts";
2331
@@ -33,6 +41,7 @@ export {
3341 normalizeModelId,
3442 parseModelsDevCapabilities,
3543 } from "./model-info-normalize.ts";
44+export type { LiveModel };
3645
3746 const REMOTE_PROVIDER_ENV_KEYS: ReadonlyArray<{
3847 provider: string;
@@ -44,20 +53,6 @@ const REMOTE_PROVIDER_ENV_KEYS: ReadonlyArray<{
4453 { provider: "google", envKeys: ["GOOGLE_API_KEY", "GEMINI_API_KEY"] },
4554 ];
4655
47-interface ModelInfo {
48- canonicalModelId: string | null;
49- contextWindow: number | null;
50- reasoning: boolean;
51-}
52-
53-export interface LiveModel {
54- id: string;
55- displayName: string;
56- provider: string;
57- context?: number | undefined;
58- free?: boolean | undefined;
59-}
60-
6156 export interface AvailableModelsSnapshot {
6257 models: LiveModel[];
6358 stale: boolean;
@@ -65,66 +60,10 @@ export interface AvailableModelsSnapshot {
6560 lastSyncAt: number | null;
6661 }
6762
68-interface RuntimeCapability {
69- canonicalModelId: string;
70- contextWindow: number | null;
71- reasoning: boolean;
72- sourceProvider: string | null;
73-}
74-
75-interface RuntimeProviderModel {
76- provider: string;
77- providerModelId: string;
78- displayName: string;
79- canonicalModelId: string | null;
80- contextWindow: number | null;
81- free: boolean;
82-}
83-
84-interface RuntimeCache {
85- capabilitiesByCanonical: Map<string, RuntimeCapability>;
86- providerModelsByKey: Map<string, RuntimeProviderModel>;
87- providerModelUniqIndex: Map<string, string | null>;
88- matchIndex: ModelMatchIndex;
89- state: Map<string, string>;
90-}
91-
92-interface ParsedModelString {
93- provider: string | null;
94- modelId: string;
95-}
96-
9763 let runtimeCache: RuntimeCache = emptyRuntimeCache();
9864 let loaded = false;
9965 let refreshInFlight: Promise<void> | null = null;
10066
101-function emptyRuntimeCache(): RuntimeCache {
102- return {
103- capabilitiesByCanonical: new Map<string, RuntimeCapability>(),
104- providerModelsByKey: new Map<string, RuntimeProviderModel>(),
105- providerModelUniqIndex: new Map<string, string | null>(),
106- matchIndex: {
107- exact: new Map<string, string>(),
108- alias: new Map<string, string | null>(),
109- },
110- state: new Map<string, string>(),
111- };
112-}
113-
114-function parseModelStringLoose(modelString: string): ParsedModelString {
115- const slash = modelString.indexOf("/");
116- if (slash === -1) {
117- return { provider: null, modelId: modelString };
118- }
119- const provider = modelString.slice(0, slash).trim().toLowerCase();
120- const modelId = modelString.slice(slash + 1);
121- return { provider: provider || null, modelId };
122-}
123-
124-function providerModelKey(provider: string, modelId: string): string {
125- return `${provider}/${modelId}`;
126-}
127-
12867 export function isStaleTimestamp(
12968 timestamp: number | null,
13069 now = Date.now(),
@@ -134,63 +73,6 @@ export function isStaleTimestamp(
13473 return now - timestamp > ttlMs;
13574 }
13675
137-function buildRuntimeCache(
138- capabilityRows: ModelCapabilityRow[],
139- providerRows: ProviderModelRow[],
140- stateRows: Array<{ key: string; value: string }>,
141-): RuntimeCache {
142- const capabilitiesByCanonical = new Map<string, RuntimeCapability>();
143- for (const row of capabilityRows) {
144- const canonical = normalizeModelId(row.canonical_model_id);
145- if (!canonical) continue;
146- capabilitiesByCanonical.set(canonical, {
147- canonicalModelId: canonical,
148- contextWindow: row.context_window,
149- reasoning: row.reasoning === 1,
150- sourceProvider: row.source_provider,
151- });
152- }
153-
154- const providerModelsByKey = new Map<string, RuntimeProviderModel>();
155- const providerModelUniqIndex = new Map<string, string | null>();
156- for (const row of providerRows) {
157- const provider = row.provider.trim().toLowerCase();
158- const providerModelId = normalizeModelId(row.provider_model_id);
159- if (!provider || !providerModelId) continue;
160- const key = providerModelKey(provider, providerModelId);
161- providerModelsByKey.set(key, {
162- provider,
163- providerModelId,
164- displayName: row.display_name,
165- canonicalModelId: row.canonical_model_id
166- ? normalizeModelId(row.canonical_model_id)
167- : null,
168- contextWindow: row.context_window,
169- free: row.free === 1,
170- });
171- const prev = providerModelUniqIndex.get(providerModelId);
172- if (prev === undefined) {
173- providerModelUniqIndex.set(providerModelId, key);
174- } else if (prev !== key) {
175- providerModelUniqIndex.set(providerModelId, null);
176- }
177- }
178-
179- const matchIndex = buildModelMatchIndex(capabilitiesByCanonical.keys());
180- const state = new Map<string, string>();
181- for (const row of stateRows) {
182- state.set(row.key, row.value);
183- }
184-
185- return {
186- capabilitiesByCanonical,
187- providerModelsByKey,
188- providerModelUniqIndex,
189- matchIndex,
190- state,
191- };
192-}
193-
19476 function loadCacheFromDb(): void {
19577 runtimeCache = buildRuntimeCache(
19678 listModelCapabilities(),
@@ -355,66 +237,6 @@ function isModelInfoRefreshing(): boolean {
355237 return refreshInFlight !== null;
356238 }
357239
358-function resolveFromProviderRow(
359- row: RuntimeProviderModel,
360- cache: RuntimeCache,
361-): ModelInfo {
362- if (row.canonicalModelId) {
363- const capability = cache.capabilitiesByCanonical.get(row.canonicalModelId);
364- if (capability) {
365- return {
366- canonicalModelId: capability.canonicalModelId,
367- contextWindow: capability.contextWindow ?? row.contextWindow,
368- reasoning: capability.reasoning,
369- };
370- }
371- }
372- return {
373- canonicalModelId: row.canonicalModelId,
374- contextWindow: row.contextWindow,
375- reasoning: false,
376- };
377-}
378-
379-function resolveModelInfoInCache(
380- modelString: string,
381- cache: RuntimeCache,
382-): ModelInfo | null {
383- const parsed = parseModelStringLoose(modelString);
384- const normalizedModelId = normalizeModelId(parsed.modelId);
385- if (!normalizedModelId) return null;
386-
387- if (parsed.provider) {
388- const providerRow = cache.providerModelsByKey.get(
389- providerModelKey(parsed.provider, normalizedModelId),
390- );
391- if (providerRow) return resolveFromProviderRow(providerRow, cache);
392- }
393-
394- const canonical = matchCanonicalModelId(normalizedModelId, cache.matchIndex);
395- if (canonical) {
396- const capability = cache.capabilitiesByCanonical.get(canonical);
397- if (capability) {
398- return {
399- canonicalModelId: capability.canonicalModelId,
400- contextWindow: capability.contextWindow,
401- reasoning: capability.reasoning,
402- };
403- }
404- }
405-
406- if (!parsed.provider) {
407- const uniqueProviderKey =
408- cache.providerModelUniqIndex.get(normalizedModelId);
409- if (uniqueProviderKey) {
410- const providerRow = cache.providerModelsByKey.get(uniqueProviderKey);
411- if (providerRow) return resolveFromProviderRow(providerRow, cache);
412- }
413- }
414-
415- return null;
416-}
417-
418240 function resolveModelInfo(modelString: string): ModelInfo | null {
419241 ensureLoaded();
420242 return resolveModelInfoInCache(modelString, runtimeCache);
@@ -438,26 +260,6 @@ export function supportsThinking(modelString: string): boolean {
438260 return resolveModelInfo(modelString)?.reasoning ?? false;
439261 }
440262
441-function readLiveModelsFromCache(): LiveModel[] {
442- const models: LiveModel[] = [];
443- const visibleProviders = getVisibleProvidersForSnapshotFromEnv(process.env);
444- for (const row of runtimeCache.providerModelsByKey.values()) {
445- if (!visibleProviders.has(row.provider)) continue;
446- const info = resolveFromProviderRow(row, runtimeCache);
447- models.push({
448- id: `${row.provider}/${row.providerModelId}`,
449- displayName: row.displayName,
450- provider: row.provider,
451- context: info.contextWindow ?? undefined,
452- free: row.free ? true : undefined,
453- });
454- }
455- models.sort(
456- (a, b) => a.provider.localeCompare(b.provider) || a.id.localeCompare(b.id),
457- );
458- return models;
459-}
460-
461263 export async function fetchAvailableModelsSnapshot(): Promise<AvailableModelsSnapshot> {
462264 ensureLoaded();
463265 if (isModelInfoStale() && !isModelInfoRefreshing()) {
@@ -468,7 +270,10 @@ export async function fetchAvailableModelsSnapshot(): Promise<AvailableModelsSna
468270 }
469271 }
470272 return {
471- models: readLiveModelsFromCache(),
273+ models: readLiveModelsFromCache(
274+ runtimeCache,
275+ getVisibleProvidersForSnapshotFromEnv(process.env),
276+ ),
472277 stale: isModelInfoStale(),
473278 refreshing: isModelInfoRefreshing(),
474279 lastSyncAt: getLastSyncAt(),