xonecas.io
64875
Mc

mini-coder / commit fe08402

refactor(llm-api): extract model-info runtime cache and resolvers

xonecas2026-03-14 fe08402d29d1771ab8d28b4dba2b2d02c5db6f3b parent b18f045

2 files changed

src/llm-api/model-info-cache.ts+219−0
@@ -0,0 +1,219 @@
1+import type {
2+ ModelCapabilityRow,
3+ ProviderModelRow,
4+} from "../session/db/model-info-repo.ts";
5+import {
6+ buildModelMatchIndex,
7+ type ModelMatchIndex,
8+ matchCanonicalModelId,
9+ normalizeModelId,
10+} from "./model-info-normalize.ts";
11+
12+export interface ModelInfo {
13+ canonicalModelId: string | null;
14+ contextWindow: number | null;
15+ reasoning: boolean;
16+}
17+
18+export interface LiveModel {
19+ id: string;
20+ displayName: string;
21+ provider: string;
22+ context?: number | undefined;
23+ free?: boolean | undefined;
24+}
25+
26+interface RuntimeCapability {
27+ canonicalModelId: string;
28+ contextWindow: number | null;
29+ reasoning: boolean;
30+ sourceProvider: string | null;
31+}
32+
33+interface RuntimeProviderModel {
34+ provider: string;
35+ providerModelId: string;
36+ displayName: string;
37+ canonicalModelId: string | null;
38+ contextWindow: number | null;
39+ free: boolean;
40+}
41+
42+export interface RuntimeCache {
43+ capabilitiesByCanonical: Map<string, RuntimeCapability>;
44+ providerModelsByKey: Map<string, RuntimeProviderModel>;
45+ providerModelUniqIndex: Map<string, string | null>;
46+ matchIndex: ModelMatchIndex;
47+ state: Map<string, string>;
48+}
49+
50+interface ParsedModelString {
51+ provider: string | null;
52+ modelId: string;
53+}
54+
55+function parseModelStringLoose(modelString: string): ParsedModelString {
56+ const slash = modelString.indexOf("/");
57+ if (slash === -1) {
58+ return { provider: null, modelId: modelString };
59+ }
60+ const provider = modelString.slice(0, slash).trim().toLowerCase();
61+ const modelId = modelString.slice(slash + 1);
62+ return { provider: provider || null, modelId };
63+}
64+
65+function providerModelKey(provider: string, modelId: string): string {
66+ return `${provider}/${modelId}`;
67+}
68+
69+export function emptyRuntimeCache(): RuntimeCache {
70+ return {
71+ capabilitiesByCanonical: new Map<string, RuntimeCapability>(),
72+ providerModelsByKey: new Map<string, RuntimeProviderModel>(),
73+ providerModelUniqIndex: new Map<string, string | null>(),
74+ matchIndex: {
75+ exact: new Map<string, string>(),
76+ alias: new Map<string, string | null>(),
77+ },
78+ state: new Map<string, string>(),
79+ };
80+}
81+
82+export function buildRuntimeCache(
83+ capabilityRows: ModelCapabilityRow[],
84+ providerRows: ProviderModelRow[],
85+ stateRows: Array<{ key: string; value: string }>,
86+): RuntimeCache {
87+ const capabilitiesByCanonical = new Map<string, RuntimeCapability>();
88+ for (const row of capabilityRows) {
89+ const canonical = normalizeModelId(row.canonical_model_id);
90+ if (!canonical) continue;
91+ capabilitiesByCanonical.set(canonical, {
92+ canonicalModelId: canonical,
93+ contextWindow: row.context_window,
94+ reasoning: row.reasoning === 1,
95+ sourceProvider: row.source_provider,
96+ });
97+ }
98+
99+ const providerModelsByKey = new Map<string, RuntimeProviderModel>();
100+ const providerModelUniqIndex = new Map<string, string | null>();
101+ for (const row of providerRows) {
102+ const provider = row.provider.trim().toLowerCase();
103+ const providerModelId = normalizeModelId(row.provider_model_id);
104+ if (!provider || !providerModelId) continue;
105+ const key = providerModelKey(provider, providerModelId);
106+ providerModelsByKey.set(key, {
107+ provider,
108+ providerModelId,
109+ displayName: row.display_name,
110+ canonicalModelId: row.canonical_model_id
111+ ? normalizeModelId(row.canonical_model_id)
112+ : null,
113+ contextWindow: row.context_window,
114+ free: row.free === 1,
115+ });
116+ const prev = providerModelUniqIndex.get(providerModelId);
117+ if (prev === undefined) {
118+ providerModelUniqIndex.set(providerModelId, key);
119+ } else if (prev !== key) {
120+ providerModelUniqIndex.set(providerModelId, null);
121+ }
122+ }
123+
124+ const matchIndex = buildModelMatchIndex(capabilitiesByCanonical.keys());
125+ const state = new Map<string, string>();
126+ for (const row of stateRows) {
127+ state.set(row.key, row.value);
128+ }
129+
130+ return {
131+ capabilitiesByCanonical,
132+ providerModelsByKey,
133+ providerModelUniqIndex,
134+ matchIndex,
135+ state,
136+ };
137+}
138+
139+function resolveFromProviderRow(
140+ row: RuntimeProviderModel,
141+ cache: RuntimeCache,
142+): ModelInfo {
143+ if (row.canonicalModelId) {
144+ const capability = cache.capabilitiesByCanonical.get(row.canonicalModelId);
145+ if (capability) {
146+ return {
147+ canonicalModelId: capability.canonicalModelId,
148+ contextWindow: capability.contextWindow ?? row.contextWindow,
149+ reasoning: capability.reasoning,
150+ };
151+ }
152+ }
153+ return {
154+ canonicalModelId: row.canonicalModelId,
155+ contextWindow: row.contextWindow,
156+ reasoning: false,
157+ };
158+}
159+
160+export function resolveModelInfoInCache(
161+ modelString: string,
162+ cache: RuntimeCache,
163+): ModelInfo | null {
164+ const parsed = parseModelStringLoose(modelString);
165+ const normalizedModelId = normalizeModelId(parsed.modelId);
166+ if (!normalizedModelId) return null;
167+
168+ if (parsed.provider) {
169+ const providerRow = cache.providerModelsByKey.get(
170+ providerModelKey(parsed.provider, normalizedModelId),
171+ );
172+ if (providerRow) return resolveFromProviderRow(providerRow, cache);
173+ }
174+
175+ const canonical = matchCanonicalModelId(normalizedModelId, cache.matchIndex);
176+ if (canonical) {
177+ const capability = cache.capabilitiesByCanonical.get(canonical);
178+ if (capability) {
179+ return {
180+ canonicalModelId: capability.canonicalModelId,
181+ contextWindow: capability.contextWindow,
182+ reasoning: capability.reasoning,
183+ };
184+ }
185+ }
186+
187+ if (!parsed.provider) {
188+ const uniqueProviderKey =
189+ cache.providerModelUniqIndex.get(normalizedModelId);
190+ if (uniqueProviderKey) {
191+ const providerRow = cache.providerModelsByKey.get(uniqueProviderKey);
192+ if (providerRow) return resolveFromProviderRow(providerRow, cache);
193+ }
194+ }
195+
196+ return null;
197+}
198+
199+export function readLiveModelsFromCache(
200+ cache: RuntimeCache,
201+ visibleProviders: ReadonlySet<string>,
202+): LiveModel[] {
203+ const models: LiveModel[] = [];
204+ for (const row of cache.providerModelsByKey.values()) {
205+ if (!visibleProviders.has(row.provider)) continue;
206+ const info = resolveFromProviderRow(row, cache);
207+ models.push({
208+ id: `${row.provider}/${row.providerModelId}`,
209+ displayName: row.displayName,
210+ provider: row.provider,
211+ context: info.contextWindow ?? undefined,
212+ free: row.free ? true : undefined,
213+ });
214+ }
215+ models.sort(
216+ (a, b) => a.provider.localeCompare(b.provider) || a.id.localeCompare(b.id),
217+ );
218+ return models;
219+}
src/llm-api/model-info.ts+14−209
@@ -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(),