xonecas.io
64875
Mc

mini-coder / commit eb45017

refactor(llm-api): extract model id normalization and matching

xonecas2026-03-14 eb45017bc46690cf46147eda2c4c9d470804d184 parent 1c378e7

2 files changed

src/llm-api/model-info-normalize.ts+140−0
@@ -0,0 +1,140 @@
1+import type { ModelCapabilityRow } from "../session/db/model-info-repo.ts";
2+
3+interface ModelsDevEntry {
4+ canonicalModelId: string;
5+ contextWindow: number | null;
6+ reasoning: boolean;
7+ sourceProvider: string;
8+ rawJson: string | null;
9+}
10+
11+export interface ModelMatchIndex {
12+ exact: Map<string, string>;
13+ alias: Map<string, string | null>;
14+}
15+
16+function isRecord(value: unknown): value is Record<string, unknown> {
17+ return typeof value === "object" && value !== null;
18+}
19+
20+function basename(value: string): string {
21+ const idx = value.lastIndexOf("/");
22+ return idx === -1 ? value : value.slice(idx + 1);
23+}
24+
25+export function normalizeModelId(modelId: string): string {
26+ let out = modelId.trim().toLowerCase();
27+ while (out.startsWith("models/")) {
28+ out = out.slice("models/".length);
29+ }
30+ return out;
31+}
32+
33+function parseContextWindow(model: Record<string, unknown>): number | null {
34+ const limit = model.limit;
35+ if (!isRecord(limit)) return null;
36+ const context = limit.context;
37+ if (typeof context !== "number" || !Number.isFinite(context)) return null;
38+ return Math.max(0, Math.trunc(context));
39+}
40+
41+export function parseModelsDevCapabilities(
42+ payload: unknown,
43+ updatedAt: number,
44+): ModelCapabilityRow[] {
45+ if (!isRecord(payload)) return [];
46+
47+ const merged = new Map<string, ModelsDevEntry>();
48+ for (const [provider, providerValue] of Object.entries(payload)) {
49+ if (!isRecord(providerValue)) continue;
50+ const models = providerValue.models;
51+ if (!isRecord(models)) continue;
52+ for (const [modelKey, modelValue] of Object.entries(models)) {
53+ if (!isRecord(modelValue)) continue;
54+ const explicitId =
55+ typeof modelValue.id === "string" && modelValue.id.trim().length > 0
56+ ? modelValue.id
57+ : modelKey;
58+ const canonicalModelId = normalizeModelId(explicitId);
59+ if (!canonicalModelId) continue;
60+ const contextWindow = parseContextWindow(modelValue);
61+ const reasoning = modelValue.reasoning === true;
62+ const rawJson = JSON.stringify(modelValue);
63+ const prev = merged.get(canonicalModelId);
64+ if (!prev) {
65+ merged.set(canonicalModelId, {
66+ canonicalModelId,
67+ contextWindow,
68+ reasoning,
69+ sourceProvider: provider,
70+ rawJson,
71+ });
72+ continue;
73+ }
74+ merged.set(canonicalModelId, {
75+ canonicalModelId,
76+ contextWindow: prev.contextWindow ?? contextWindow,
77+ reasoning: prev.reasoning || reasoning,
78+ sourceProvider: prev.sourceProvider,
79+ rawJson: prev.rawJson ?? rawJson,
80+ });
81+ }
82+ }
83+
84+ return Array.from(merged.values()).map((entry) => ({
85+ canonical_model_id: entry.canonicalModelId,
86+ context_window: entry.contextWindow,
87+ reasoning: entry.reasoning ? 1 : 0,
88+ source_provider: entry.sourceProvider,
89+ raw_json: entry.rawJson,
90+ updated_at: updatedAt,
91+ }));
92+}
93+
94+export function buildModelMatchIndex(
95+ canonicalModelIds: Iterable<string>,
96+): ModelMatchIndex {
97+ const exact = new Map<string, string>();
98+ const aliasCandidates = new Map<string, Set<string>>();
99+
100+ for (const rawCanonical of canonicalModelIds) {
101+ const canonical = normalizeModelId(rawCanonical);
102+ if (!canonical) continue;
103+ exact.set(canonical, canonical);
104+ const short = basename(canonical);
105+ if (!short) continue;
106+ let set = aliasCandidates.get(short);
107+ if (!set) {
108+ set = new Set<string>();
109+ aliasCandidates.set(short, set);
110+ }
111+ set.add(canonical);
112+ }
113+
114+ const alias = new Map<string, string | null>();
115+ for (const [short, candidates] of aliasCandidates) {
116+ if (candidates.size === 1) {
117+ for (const value of candidates) {
118+ alias.set(short, value);
119+ }
120+ } else {
121+ alias.set(short, null);
122+ }
123+ }
124+
125+ return { exact, alias };
126+}
127+
128+export function matchCanonicalModelId(
129+ providerModelId: string,
130+ index: ModelMatchIndex,
131+): string | null {
132+ const normalized = normalizeModelId(providerModelId);
133+ if (!normalized) return null;
134+ const exactMatch = index.exact.get(normalized);
135+ if (exactMatch) return exactMatch;
136+ const short = basename(normalized);
137+ if (!short) return null;
138+ const alias = index.alias.get(short);
139+ return alias ?? null;
140+}
src/llm-api/model-info.ts+14−139
@@ -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(),