@@ -0,0 +1,780 @@
1+import {
2+ type ModelCapabilityRow,
3+ type ProviderModelRow,
4+ listModelCapabilities,
5+ listModelInfoState,
6+ listProviderModels,
7+ replaceModelCapabilities,
8+ replaceProviderModels,
9+ setModelInfoState,
10+} from "../session/db/model-info-repo.ts";
11+
12+const ZEN_BASE = "https://opencode.ai/zen/v1";
13+const OPENAI_BASE = "https://api.openai.com";
14+const ANTHROPIC_BASE = "https://api.anthropic.com";
15+const GOOGLE_BASE = "https://generativelanguage.googleapis.com/v1beta";
16+const MODELS_DEV_URL = "https://models.dev/api.json";
17+
18+const MODELS_DEV_SYNC_KEY = "last_models_dev_sync_at";
19+const PROVIDER_SYNC_KEY_PREFIX = "last_provider_sync_at:";
20+
21+export const MODEL_INFO_TTL_MS = 7 * 24 * 60 * 60 * 1000;
22+
23+export interface ModelInfo {
24+ canonicalModelId: string | null;
25+ contextWindow: number | null;
26+ reasoning: boolean;
27+}
28+
29+export interface LiveModel {
30+ id: string;
31+ displayName: string;
32+ provider: string;
33+ context?: number | undefined;
34+ free?: boolean | undefined;
35+}
36+
37+export interface AvailableModelsSnapshot {
38+ models: LiveModel[];
39+ stale: boolean;
40+ refreshing: boolean;
41+ lastSyncAt: number | null;
42+}
43+
44+interface RuntimeCapability {
45+ canonicalModelId: string;
46+ contextWindow: number | null;
47+ reasoning: boolean;
48+ sourceProvider: string | null;
49+}
50+
51+interface RuntimeProviderModel {
52+ provider: string;
53+ providerModelId: string;
54+ displayName: string;
55+ canonicalModelId: string | null;
56+ contextWindow: number | null;
57+ free: boolean;
58+}
59+
60+interface RuntimeCache {
61+ capabilitiesByCanonical: Map<string, RuntimeCapability>;
62+ providerModelsByKey: Map<string, RuntimeProviderModel>;
63+ providerModelUniqIndex: Map<string, string | null>;
64+ matchIndex: ModelMatchIndex;
65+ state: Map<string, string>;
66+}
67+
68+export interface ModelMatchIndex {
69+ exact: Map<string, string>;
70+ alias: Map<string, string | null>;
71+}
72+
73+interface ModelsDevEntry {
74+ canonicalModelId: string;
75+ contextWindow: number | null;
76+ reasoning: boolean;
77+ sourceProvider: string;
78+ rawJson: string | null;
79+}
80+
81+interface ProviderModelCandidate {
82+ providerModelId: string;
83+ displayName: string;
84+ contextWindow: number | null;
85+ free: boolean;
86+}
87+
88+interface ParsedModelString {
89+ provider: string | null;
90+ modelId: string;
91+}
92+
93+let runtimeCache: RuntimeCache = emptyRuntimeCache();
94+let loaded = false;
95+let refreshInFlight: Promise<void> | null = null;
96+
97+function emptyRuntimeCache(): RuntimeCache {
98+ return {
99+ capabilitiesByCanonical: new Map<string, RuntimeCapability>(),
100+ providerModelsByKey: new Map<string, RuntimeProviderModel>(),
101+ providerModelUniqIndex: new Map<string, string | null>(),
102+ matchIndex: {
103+ exact: new Map<string, string>(),
104+ alias: new Map<string, string | null>(),
105+ },
106+ state: new Map<string, string>(),
107+ };
108+}
109+
110+function isRecord(value: unknown): value is Record<string, unknown> {
111+ return typeof value === "object" && value !== null;
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+
128+function basename(value: string): string {
129+ const idx = value.lastIndexOf("/");
130+ return idx === -1 ? value : value.slice(idx + 1);
131+}
132+
133+export function normalizeModelId(modelId: string): string {
134+ let out = modelId.trim().toLowerCase();
135+ while (out.startsWith("models/")) {
136+ out = out.slice("models/".length);
137+ }
138+ return out;
139+}
140+
141+function parseContextWindow(model: Record<string, unknown>): number | null {
142+ const limit = model.limit;
143+ if (!isRecord(limit)) return null;
144+ const context = limit.context;
145+ if (typeof context !== "number" || !Number.isFinite(context)) return null;
146+ return Math.max(0, Math.trunc(context));
147+}
148+
149+export function parseModelsDevCapabilities(
150+ payload: unknown,
151+ updatedAt: number,
152+): ModelCapabilityRow[] {
153+ if (!isRecord(payload)) return [];
154+
155+ const merged = new Map<string, ModelsDevEntry>();
156+ for (const [provider, providerValue] of Object.entries(payload)) {
157+ if (!isRecord(providerValue)) continue;
158+ const models = providerValue.models;
159+ if (!isRecord(models)) continue;
160+ for (const [modelKey, modelValue] of Object.entries(models)) {
161+ if (!isRecord(modelValue)) continue;
162+ const explicitId =
163+ typeof modelValue.id === "string" && modelValue.id.trim().length > 0
164+ ? modelValue.id
165+ : modelKey;
166+ const canonicalModelId = normalizeModelId(explicitId);
167+ if (!canonicalModelId) continue;
168+ const contextWindow = parseContextWindow(modelValue);
169+ const reasoning = modelValue.reasoning === true;
170+ const rawJson = JSON.stringify(modelValue);
171+ const prev = merged.get(canonicalModelId);
172+ if (!prev) {
173+ merged.set(canonicalModelId, {
174+ canonicalModelId,
175+ contextWindow,
176+ reasoning,
177+ sourceProvider: provider,
178+ rawJson,
179+ });
180+ continue;
181+ }
182+ merged.set(canonicalModelId, {
183+ canonicalModelId,
184+ contextWindow: prev.contextWindow ?? contextWindow,
185+ reasoning: prev.reasoning || reasoning,
186+ sourceProvider: prev.sourceProvider,
187+ rawJson: prev.rawJson ?? rawJson,
188+ });
189+ }
190+ }
191+
192+ return Array.from(merged.values()).map((entry) => ({
193+ canonical_model_id: entry.canonicalModelId,
194+ context_window: entry.contextWindow,
195+ reasoning: entry.reasoning ? 1 : 0,
196+ source_provider: entry.sourceProvider,
197+ raw_json: entry.rawJson,
198+ updated_at: updatedAt,
199+ }));
200+}
201+
202+export function buildModelMatchIndex(
203+ canonicalModelIds: Iterable<string>,
204+): ModelMatchIndex {
205+ const exact = new Map<string, string>();
206+ const aliasCandidates = new Map<string, Set<string>>();
207+
208+ for (const rawCanonical of canonicalModelIds) {
209+ const canonical = normalizeModelId(rawCanonical);
210+ if (!canonical) continue;
211+ exact.set(canonical, canonical);
212+ const short = basename(canonical);
213+ if (!short) continue;
214+ let set = aliasCandidates.get(short);
215+ if (!set) {
216+ set = new Set<string>();
217+ aliasCandidates.set(short, set);
218+ }
219+ set.add(canonical);
220+ }
221+
222+ const alias = new Map<string, string | null>();
223+ for (const [short, candidates] of aliasCandidates) {
224+ if (candidates.size === 1) {
225+ for (const value of candidates) {
226+ alias.set(short, value);
227+ }
228+ } else {
229+ alias.set(short, null);
230+ }
231+ }
232+
233+ return { exact, alias };
234+}
235+
236+export function matchCanonicalModelId(
237+ providerModelId: string,
238+ index: ModelMatchIndex,
239+): string | null {
240+ const normalized = normalizeModelId(providerModelId);
241+ if (!normalized) return null;
242+ const exactMatch = index.exact.get(normalized);
243+ if (exactMatch) return exactMatch;
244+ const short = basename(normalized);
245+ if (!short) return null;
246+ const alias = index.alias.get(short);
247+ return alias ?? null;
248+}
249+
250+export function isStaleTimestamp(
251+ timestamp: number | null,
252+ now = Date.now(),
253+ ttlMs = MODEL_INFO_TTL_MS,
254+): boolean {
255+ if (timestamp === null) return true;
256+ return now - timestamp > ttlMs;
257+}
258+
259+export function buildRuntimeCache(
260+ capabilityRows: ModelCapabilityRow[],
261+ providerRows: ProviderModelRow[],
262+ stateRows: Array<{ key: string; value: string }>,
263+): RuntimeCache {
264+ const capabilitiesByCanonical = new Map<string, RuntimeCapability>();
265+ for (const row of capabilityRows) {
266+ const canonical = normalizeModelId(row.canonical_model_id);
267+ if (!canonical) continue;
268+ capabilitiesByCanonical.set(canonical, {
269+ canonicalModelId: canonical,
270+ contextWindow: row.context_window,
271+ reasoning: row.reasoning === 1,
272+ sourceProvider: row.source_provider,
273+ });
274+ }
275+
276+ const providerModelsByKey = new Map<string, RuntimeProviderModel>();
277+ const providerModelUniqIndex = new Map<string, string | null>();
278+ for (const row of providerRows) {
279+ const provider = row.provider.trim().toLowerCase();
280+ const providerModelId = normalizeModelId(row.provider_model_id);
281+ if (!provider || !providerModelId) continue;
282+ const key = providerModelKey(provider, providerModelId);
283+ providerModelsByKey.set(key, {
284+ provider,
285+ providerModelId,
286+ displayName: row.display_name,
287+ canonicalModelId: row.canonical_model_id
288+ ? normalizeModelId(row.canonical_model_id)
289+ : null,
290+ contextWindow: row.context_window,
291+ free: row.free === 1,
292+ });
293+ const prev = providerModelUniqIndex.get(providerModelId);
294+ if (prev === undefined) {
295+ providerModelUniqIndex.set(providerModelId, key);
296+ } else if (prev !== key) {
297+ providerModelUniqIndex.set(providerModelId, null);
298+ }
299+ }
300+
301+ const matchIndex = buildModelMatchIndex(capabilitiesByCanonical.keys());
302+ const state = new Map<string, string>();
303+ for (const row of stateRows) {
304+ state.set(row.key, row.value);
305+ }
306+
307+ return {
308+ capabilitiesByCanonical,
309+ providerModelsByKey,
310+ providerModelUniqIndex,
311+ matchIndex,
312+ state,
313+ };
314+}
315+
316+function loadCacheFromDb(): void {
317+ runtimeCache = buildRuntimeCache(
318+ listModelCapabilities(),
319+ listProviderModels(),
320+ listModelInfoState(),
321+ );
322+ loaded = true;
323+}
324+
325+function ensureLoaded(): void {
326+ if (!loaded) loadCacheFromDb();
327+}
328+
329+export function initModelInfoCache(): void {
330+ loadCacheFromDb();
331+}
332+
333+function parseStateInt(key: string): number | null {
334+ const raw = runtimeCache.state.get(key);
335+ if (!raw) return null;
336+ const value = Number.parseInt(raw, 10);
337+ if (!Number.isFinite(value)) return null;
338+ return value;
339+}
340+
341+export function getRemoteProvidersFromEnv(
342+ env: Record<string, string | undefined>,
343+): string[] {
344+ const providers: string[] = [];
345+ if (env.OPENCODE_API_KEY) providers.push("zen");
346+ if (env.OPENAI_API_KEY) providers.push("openai");
347+ if (env.ANTHROPIC_API_KEY) providers.push("anthropic");
348+ if (env.GOOGLE_API_KEY ?? env.GEMINI_API_KEY) providers.push("google");
349+ return providers;
350+}
351+
352+export function getProvidersToRefreshFromEnv(
353+ env: Record<string, string | undefined>,
354+): string[] {
355+ return [...getRemoteProvidersFromEnv(env), "ollama"];
356+}
357+
358+export function getVisibleProvidersForSnapshotFromEnv(
359+ env: Record<string, string | undefined>,
360+): ReadonlySet<string> {
361+ return new Set(getProvidersToRefreshFromEnv(env));
362+}
363+
364+export function isProviderVisibleInSnapshot(
365+ provider: string,
366+ env: Record<string, string | undefined>,
367+): boolean {
368+ return getVisibleProvidersForSnapshotFromEnv(env).has(provider);
369+}
370+
371+function getConfiguredProvidersForSync(): string[] {
372+ return getProvidersToRefreshFromEnv(process.env);
373+}
374+
375+function getProvidersRequiredForFreshness(): string[] {
376+ return getRemoteProvidersFromEnv(process.env);
377+}
378+
379+function getProviderSyncKey(provider: string): string {
380+ return `${PROVIDER_SYNC_KEY_PREFIX}${provider}`;
381+}
382+
383+export function isModelInfoStale(now = Date.now()): boolean {
384+ ensureLoaded();
385+ if (isStaleTimestamp(parseStateInt(MODELS_DEV_SYNC_KEY), now)) return true;
386+ for (const provider of getProvidersRequiredForFreshness()) {
387+ const providerSync = parseStateInt(getProviderSyncKey(provider));
388+ if (isStaleTimestamp(providerSync, now)) return true;
389+ }
390+ return false;
391+}
392+
393+function getLastSyncAt(): number | null {
394+ let latest = parseStateInt(MODELS_DEV_SYNC_KEY);
395+ for (const provider of getProvidersRequiredForFreshness()) {
396+ const value = parseStateInt(getProviderSyncKey(provider));
397+ if (value !== null && (latest === null || value > latest)) latest = value;
398+ }
399+ return latest;
400+}
401+
402+async function fetchJson(
403+ url: string,
404+ init: RequestInit,
405+ timeoutMs: number,
406+): Promise<unknown | null> {
407+ try {
408+ const response = await fetch(url, {
409+ ...init,
410+ signal: AbortSignal.timeout(timeoutMs),
411+ });
412+ if (!response.ok) return null;
413+ return (await response.json()) as unknown;
414+ } catch {
415+ return null;
416+ }
417+}
418+
419+async function fetchModelsDevPayload(): Promise<unknown | null> {
420+ return fetchJson(MODELS_DEV_URL, {}, 10_000);
421+}
422+
423+async function fetchZenModels(): Promise<ProviderModelCandidate[] | null> {
424+ const key = process.env.OPENCODE_API_KEY;
425+ if (!key) return null;
426+ const payload = await fetchJson(
427+ `${ZEN_BASE}/models`,
428+ { headers: { Authorization: `Bearer ${key}` } },
429+ 8_000,
430+ );
431+ if (!isRecord(payload)) return null;
432+ const data = payload.data;
433+ if (!Array.isArray(data)) return null;
434+ const out: ProviderModelCandidate[] = [];
435+ for (const item of data) {
436+ if (!isRecord(item) || typeof item.id !== "string") continue;
437+ const modelId = normalizeModelId(item.id);
438+ if (!modelId) continue;
439+ const contextWindow =
440+ typeof item.context_window === "number" &&
441+ Number.isFinite(item.context_window)
442+ ? Math.max(0, Math.trunc(item.context_window))
443+ : null;
444+ out.push({
445+ providerModelId: modelId,
446+ displayName: item.id,
447+ contextWindow,
448+ free:
449+ item.id.endsWith("-free") ||
450+ item.id === "gpt-5-nano" ||
451+ item.id === "big-pickle",
452+ });
453+ }
454+ return out;
455+}
456+
457+async function fetchOpenAIModels(): Promise<ProviderModelCandidate[] | null> {
458+ const key = process.env.OPENAI_API_KEY;
459+ if (!key) return null;
460+ const payload = await fetchJson(
461+ `${OPENAI_BASE}/v1/models`,
462+ { headers: { Authorization: `Bearer ${key}` } },
463+ 6_000,
464+ );
465+ if (!isRecord(payload) || !Array.isArray(payload.data)) return null;
466+ const out: ProviderModelCandidate[] = [];
467+ for (const item of payload.data) {
468+ if (!isRecord(item) || typeof item.id !== "string") continue;
469+ const modelId = normalizeModelId(item.id);
470+ if (!modelId) continue;
471+ out.push({
472+ providerModelId: modelId,
473+ displayName: item.id,
474+ contextWindow: null,
475+ free: false,
476+ });
477+ }
478+ return out;
479+}
480+
481+async function fetchAnthropicModels(): Promise<
482+ ProviderModelCandidate[] | null
483+> {
484+ const key = process.env.ANTHROPIC_API_KEY;
485+ if (!key) return null;
486+ const payload = await fetchJson(
487+ `${ANTHROPIC_BASE}/v1/models`,
488+ {
489+ headers: {
490+ "x-api-key": key,
491+ "anthropic-version": "2023-06-01",
492+ },
493+ },
494+ 6_000,
495+ );
496+ if (!isRecord(payload) || !Array.isArray(payload.data)) return null;
497+ const out: ProviderModelCandidate[] = [];
498+ for (const item of payload.data) {
499+ if (!isRecord(item) || typeof item.id !== "string") continue;
500+ const modelId = normalizeModelId(item.id);
501+ if (!modelId) continue;
502+ const displayName =
503+ typeof item.display_name === "string" &&
504+ item.display_name.trim().length > 0
505+ ? item.display_name
506+ : item.id;
507+ out.push({
508+ providerModelId: modelId,
509+ displayName,
510+ contextWindow: null,
511+ free: false,
512+ });
513+ }
514+ return out;
515+}
516+
517+async function fetchGoogleModels(): Promise<ProviderModelCandidate[] | null> {
518+ const key = process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY;
519+ if (!key) return null;
520+ const payload = await fetchJson(
521+ `${GOOGLE_BASE}/models?key=${encodeURIComponent(key)}`,
522+ {},
523+ 6_000,
524+ );
525+ if (!isRecord(payload) || !Array.isArray(payload.models)) return null;
526+ const out: ProviderModelCandidate[] = [];
527+ for (const item of payload.models) {
528+ if (!isRecord(item) || typeof item.name !== "string") continue;
529+ const modelId = normalizeModelId(item.name);
530+ if (!modelId) continue;
531+ const displayName =
532+ typeof item.displayName === "string" && item.displayName.trim().length > 0
533+ ? item.displayName
534+ : modelId;
535+ const contextWindow =
536+ typeof item.inputTokenLimit === "number" &&
537+ Number.isFinite(item.inputTokenLimit)
538+ ? Math.max(0, Math.trunc(item.inputTokenLimit))
539+ : null;
540+ out.push({
541+ providerModelId: modelId,
542+ displayName,
543+ contextWindow,
544+ free: false,
545+ });
546+ }
547+ return out;
548+}
549+
550+async function fetchOllamaModels(): Promise<ProviderModelCandidate[] | null> {
551+ const base = process.env.OLLAMA_BASE_URL ?? "http://localhost:11434";
552+ const payload = await fetchJson(`${base}/api/tags`, {}, 3_000);
553+ if (!isRecord(payload) || !Array.isArray(payload.models)) return null;
554+ const out: ProviderModelCandidate[] = [];
555+ for (const item of payload.models) {
556+ if (!isRecord(item) || typeof item.name !== "string") continue;
Cut short: this commit's diff is over 1000 lines.