@oh-my-pi/pi-catalog 15.10.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/types/build.d.ts +3 -0
  3. package/dist/types/compat/anthropic.d.ts +11 -0
  4. package/dist/types/compat/apply.d.ts +7 -0
  5. package/dist/types/compat/openai.d.ts +21 -0
  6. package/dist/types/discovery/antigravity.d.ts +61 -0
  7. package/dist/types/discovery/codex.d.ts +38 -0
  8. package/dist/types/discovery/cursor-gen/agent_pb.d.ts +13022 -0
  9. package/dist/types/discovery/cursor.d.ts +23 -0
  10. package/dist/types/discovery/gemini.d.ts +25 -0
  11. package/dist/types/discovery/index.d.ts +4 -0
  12. package/dist/types/discovery/openai-compatible.d.ts +72 -0
  13. package/dist/types/effort.d.ts +9 -0
  14. package/dist/types/fireworks-model-id.d.ts +10 -0
  15. package/dist/types/hosts.d.ts +128 -0
  16. package/dist/types/identity/bundled.d.ts +6 -0
  17. package/dist/types/identity/classify.d.ts +45 -0
  18. package/dist/types/identity/equivalence.d.ts +46 -0
  19. package/dist/types/identity/family.d.ts +45 -0
  20. package/dist/types/identity/id.d.ts +12 -0
  21. package/dist/types/identity/index.d.ts +9 -0
  22. package/dist/types/identity/markers.d.ts +4 -0
  23. package/dist/types/identity/priority.d.ts +1 -0
  24. package/dist/types/identity/reference.d.ts +22 -0
  25. package/dist/types/identity/selection.d.ts +20 -0
  26. package/dist/types/index.d.ts +15 -0
  27. package/dist/types/model-cache.d.ts +17 -0
  28. package/dist/types/model-manager.d.ts +64 -0
  29. package/dist/types/model-thinking.d.ts +67 -0
  30. package/dist/types/models.d.ts +12 -0
  31. package/dist/types/provider-models/bundled-references.d.ts +11 -0
  32. package/dist/types/provider-models/descriptor-types.d.ts +74 -0
  33. package/dist/types/provider-models/descriptors.d.ts +384 -0
  34. package/dist/types/provider-models/discovery-constants.d.ts +11 -0
  35. package/dist/types/provider-models/google.d.ts +27 -0
  36. package/dist/types/provider-models/index.d.ts +6 -0
  37. package/dist/types/provider-models/ollama.d.ts +9 -0
  38. package/dist/types/provider-models/openai-compat.d.ts +385 -0
  39. package/dist/types/provider-models/special.d.ts +16 -0
  40. package/dist/types/types.d.ts +405 -0
  41. package/dist/types/utils.d.ts +5 -0
  42. package/dist/types/wire/codex.d.ts +26 -0
  43. package/dist/types/wire/gemini-headers.d.ts +18 -0
  44. package/dist/types/wire/github-copilot.d.ts +18 -0
  45. package/package.json +100 -0
  46. package/src/build.ts +40 -0
  47. package/src/compat/anthropic.ts +67 -0
  48. package/src/compat/apply.ts +15 -0
  49. package/src/compat/openai.ts +365 -0
  50. package/src/discovery/antigravity.ts +261 -0
  51. package/src/discovery/codex.ts +371 -0
  52. package/src/discovery/cursor-gen/agent_pb.ts +15274 -0
  53. package/src/discovery/cursor.ts +307 -0
  54. package/src/discovery/gemini.ts +249 -0
  55. package/src/discovery/index.ts +4 -0
  56. package/src/discovery/openai-compatible.ts +224 -0
  57. package/src/effort.ts +16 -0
  58. package/src/fireworks-model-id.ts +30 -0
  59. package/src/hosts.ts +114 -0
  60. package/src/identity/bundled.ts +38 -0
  61. package/src/identity/classify.ts +141 -0
  62. package/src/identity/equivalence.ts +870 -0
  63. package/src/identity/family.ts +88 -0
  64. package/src/identity/id.ts +81 -0
  65. package/src/identity/index.ts +9 -0
  66. package/src/identity/markers.ts +49 -0
  67. package/src/identity/priority.ts +56 -0
  68. package/src/identity/reference.ts +134 -0
  69. package/src/identity/selection.ts +65 -0
  70. package/src/index.ts +15 -0
  71. package/src/model-cache.ts +132 -0
  72. package/src/model-manager.ts +472 -0
  73. package/src/model-thinking.ts +407 -0
  74. package/src/models.json +75308 -0
  75. package/src/models.json.d.ts +9 -0
  76. package/src/models.ts +64 -0
  77. package/src/provider-models/bundled-references.ts +54 -0
  78. package/src/provider-models/descriptor-types.ts +79 -0
  79. package/src/provider-models/descriptors.ts +456 -0
  80. package/src/provider-models/discovery-constants.ts +11 -0
  81. package/src/provider-models/google.ts +105 -0
  82. package/src/provider-models/index.ts +6 -0
  83. package/src/provider-models/ollama.ts +154 -0
  84. package/src/provider-models/openai-compat.ts +3106 -0
  85. package/src/provider-models/special.ts +67 -0
  86. package/src/types.ts +470 -0
  87. package/src/utils.ts +27 -0
  88. package/src/wire/codex.ts +43 -0
  89. package/src/wire/gemini-headers.ts +41 -0
  90. package/src/wire/github-copilot.ts +72 -0
@@ -0,0 +1,472 @@
1
+ import { buildModel } from "./build";
2
+ import { readModelCache, writeModelCache } from "./model-cache";
3
+ import { type GeneratedProvider, getBundledModels } from "./models";
4
+ import type { Api, Model, ModelSpec, Provider } from "./types";
5
+ import { isRecord } from "./utils";
6
+
7
+ const DEFAULT_CACHE_TTL_MS = 2 * 60 * 60 * 1000;
8
+ const NON_AUTHORITATIVE_RETRY_MS = 5 * 60 * 1000;
9
+
10
+ /**
11
+ * Controls when dynamic endpoint models should be fetched.
12
+ */
13
+ export type ModelRefreshStrategy = "online" | "offline" | "online-if-uncached";
14
+
15
+ /**
16
+ * Hook for loading and mapping models.dev fallback data into canonical model objects.
17
+ */
18
+ export interface ModelsDevFallback<TApi extends Api = Api, TPayload = unknown> {
19
+ /** Fetches raw fallback payload (for example from models.dev). */
20
+ fetch(): Promise<TPayload>;
21
+ /** Maps payload into provider models. */
22
+ map(payload: TPayload, providerId: Provider): readonly ModelSpec<TApi>[];
23
+ }
24
+
25
+ /**
26
+ * Configuration for provider model resolution.
27
+ */
28
+ export interface ModelManagerOptions<TApi extends Api = Api, TModelsDevPayload = unknown> {
29
+ /** Provider id used for static lookup and cache namespacing. */
30
+ providerId: Provider;
31
+ /** Optional static list override. When omitted, bundled models.json is used. */
32
+ staticModels?: readonly ModelSpec<TApi>[];
33
+ /** Optional override for the cache database path. Default: <agent-dir>/models.db. */
34
+ cacheDbPath?: string;
35
+ /** Maximum cache age in milliseconds before considered stale. Default: 24h. */
36
+ cacheTtlMs?: number;
37
+ /** When true, a successful dynamic fetch is the complete provider catalog and prunes static-only models. */
38
+ dynamicModelsAuthoritative?: boolean;
39
+ /** Optional dynamic endpoint fetcher. */
40
+ fetchDynamicModels?: () => Promise<readonly ModelSpec<TApi>[] | null>;
41
+ /** Optional models.dev fallback hook. */
42
+ modelsDev?: ModelsDevFallback<TApi, TModelsDevPayload>;
43
+ /** Clock override for deterministic tests. */
44
+ now?: () => number;
45
+ }
46
+
47
+ /**
48
+ * Resolution result.
49
+ *
50
+ * `stale` is false when the resolved catalog is authoritative for the selected provider:
51
+ * - dynamic endpoint data was fetched in this call,
52
+ * - a still-fresh authoritative cache was reused in `online-if-uncached` mode, or
53
+ * - the provider has no dynamic fetcher configured.
54
+ */
55
+ export interface ModelResolutionResult<TApi extends Api = Api> {
56
+ models: Model<TApi>[];
57
+ stale: boolean;
58
+ }
59
+
60
+ /**
61
+ * Stateful facade over provider model resolution.
62
+ */
63
+ export interface ModelManager<TApi extends Api = Api> {
64
+ refresh(strategy?: ModelRefreshStrategy): Promise<ModelResolutionResult<TApi>>;
65
+ }
66
+
67
+ /**
68
+ * Creates a reusable provider model manager.
69
+ */
70
+ export function createModelManager<TApi extends Api = Api, TModelsDevPayload = unknown>(
71
+ options: ModelManagerOptions<TApi, TModelsDevPayload>,
72
+ ): ModelManager<TApi> {
73
+ return {
74
+ refresh(strategy: ModelRefreshStrategy = "online-if-uncached") {
75
+ return resolveProviderModels(options, strategy);
76
+ },
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Cheap fast path for trusted spec sources (caller-provided literals, our own
82
+ * cache rows). Skips per-field validation; only guards against
83
+ * catastrophically corrupt rows. Builds each spec into a runtime model.
84
+ */
85
+ function passModelList<TApi extends Api>(value: unknown): Model<TApi>[] {
86
+ if (!Array.isArray(value)) {
87
+ return [];
88
+ }
89
+ const out: Model<TApi>[] = [];
90
+ for (const item of value) {
91
+ if (item === null || typeof item !== "object" || typeof (item as { id: unknown }).id !== "string") {
92
+ continue;
93
+ }
94
+ out.push(buildModel(item as ModelSpec<TApi>));
95
+ }
96
+ return out;
97
+ }
98
+
99
+ /**
100
+ * Resolves provider models with source precedence:
101
+ * static -> models.dev -> cache -> dynamic.
102
+ *
103
+ * Later sources override earlier ones by model id.
104
+ */
105
+ export async function resolveProviderModels<TApi extends Api = Api, TModelsDevPayload = unknown>(
106
+ options: ModelManagerOptions<TApi, TModelsDevPayload>,
107
+ strategy: ModelRefreshStrategy = "online-if-uncached",
108
+ ): Promise<ModelResolutionResult<TApi>> {
109
+ const now = options.now ?? Date.now;
110
+ const ttlMs = options.cacheTtlMs ?? DEFAULT_CACHE_TTL_MS;
111
+ const dbPath = options.cacheDbPath;
112
+ const staticModels = options.staticModels
113
+ ? passModelList<TApi>(options.staticModels)
114
+ : (getBundledModels(options.providerId as GeneratedProvider) as Model<TApi>[]);
115
+ const cache = readModelCache<TApi>(options.providerId, ttlMs, now, dbPath);
116
+ const dynamicModelsAuthoritative = options.dynamicModelsAuthoritative ?? false;
117
+ const staticFingerprint = fingerprintStatic(staticModels, dynamicModelsAuthoritative);
118
+ const cacheFingerprintMatches = cache?.staticFingerprint === staticFingerprint && staticFingerprint.length > 0;
119
+ const hasUsableFreshCache = (cache?.fresh ?? false) && (!dynamicModelsAuthoritative || cacheFingerprintMatches);
120
+ const dynamicFetcher = options.fetchDynamicModels;
121
+ const hasDynamicFetcher = typeof dynamicFetcher === "function";
122
+ const hasAuthoritativeCache = ((cache?.authoritative ?? false) && hasUsableFreshCache) || !hasDynamicFetcher;
123
+ const cacheAgeMs = cache ? now() - cache.updatedAt : Number.POSITIVE_INFINITY;
124
+ const shouldFetchFromNetwork = shouldFetchRemoteSources(
125
+ strategy,
126
+ hasUsableFreshCache,
127
+ hasAuthoritativeCache,
128
+ cacheAgeMs,
129
+ );
130
+
131
+ // Cold-start fast path: when a fresh, authoritative cache exists, the network
132
+ // fetch is skipped, AND the static catalog slice is byte-identical to what
133
+ // was merged in last time, the cache row IS the authoritative merge result.
134
+ // Re-running `mergeDynamicModels(static, cache)` would just rebuild the same
135
+ // objects (~800ms in the steady-state cold-start profile for `omp -p hi`).
136
+ if (!shouldFetchFromNetwork && cache?.fresh && hasAuthoritativeCache && cacheFingerprintMatches) {
137
+ return { models: passModelList<TApi>(cache.models), stale: false };
138
+ }
139
+
140
+ const [fetchedModelsDevModels, fetchedDynamicModels] = shouldFetchFromNetwork
141
+ ? await Promise.all([fetchModelsDev(options), dynamicFetcher ? fetchDynamicModels(dynamicFetcher) : null])
142
+ : [null, null];
143
+ const modelsDevModels = normalizeModelList<TApi>(fetchedModelsDevModels ?? []);
144
+ const shouldUseFreshCacheAsAuthoritative =
145
+ strategy === "online-if-uncached" && hasUsableFreshCache && hasAuthoritativeCache;
146
+ const dynamicFetchSucceeded = fetchedDynamicModels !== null;
147
+ const cacheModels = dynamicFetchSucceeded ? [] : normalizeModelList<TApi>(cache?.models ?? []);
148
+ const dynamicModels = fetchedDynamicModels ?? [];
149
+ const mergedWithCache = mergeDynamicModels(mergeModelSources(staticModels, modelsDevModels), cacheModels);
150
+ const mergedModels = mergeDynamicModels(mergedWithCache, dynamicModels);
151
+ const models =
152
+ dynamicModelsAuthoritative && dynamicFetchSucceeded ? retainModelIds(mergedModels, dynamicModels) : mergedModels;
153
+ const dynamicAuthoritative = !hasDynamicFetcher || dynamicFetchSucceeded || shouldUseFreshCacheAsAuthoritative;
154
+ if (shouldFetchFromNetwork) {
155
+ if (dynamicFetchSucceeded) {
156
+ const mergedSnapshot = mergeDynamicModels(mergeModelSources(staticModels, modelsDevModels), dynamicModels);
157
+ const snapshotModels = dynamicModelsAuthoritative
158
+ ? retainModelIds(mergedSnapshot, dynamicModels)
159
+ : mergedSnapshot;
160
+ writeModelCache(options.providerId, now(), snapshotModels, true, staticFingerprint, dbPath);
161
+ } else {
162
+ // Dynamic fetch failed — update cache with a non-authoritative snapshot so
163
+ // stale state remains visible while retry backoff still applies.
164
+ const latestCache = readModelCache<TApi>(options.providerId, ttlMs, now, dbPath);
165
+ writeModelCache(
166
+ options.providerId,
167
+ now(),
168
+ mergeDynamicModels(
169
+ mergeModelSources(staticModels, modelsDevModels),
170
+ normalizeModelList<TApi>(latestCache?.models ?? cache?.models ?? []),
171
+ ),
172
+ false,
173
+ staticFingerprint,
174
+ dbPath,
175
+ );
176
+ }
177
+ }
178
+ return {
179
+ models,
180
+ stale: !dynamicAuthoritative,
181
+ };
182
+ }
183
+
184
+ async function fetchModelsDev<TApi extends Api, TModelsDevPayload>(
185
+ options: ModelManagerOptions<TApi, TModelsDevPayload>,
186
+ ): Promise<Model<TApi>[] | null> {
187
+ if (!options.modelsDev) {
188
+ return null;
189
+ }
190
+
191
+ try {
192
+ const payload = await options.modelsDev.fetch();
193
+ return normalizeModelList<TApi>(options.modelsDev.map(payload, options.providerId));
194
+ } catch {
195
+ return null;
196
+ }
197
+ }
198
+
199
+ async function fetchDynamicModels<TApi extends Api>(
200
+ fetcher: () => Promise<readonly ModelSpec<TApi>[] | null>,
201
+ ): Promise<Model<TApi>[] | null> {
202
+ try {
203
+ const models = await fetcher();
204
+ if (models === null) {
205
+ return null;
206
+ }
207
+ return normalizeModelList<TApi>(models);
208
+ } catch {
209
+ return null;
210
+ }
211
+ }
212
+
213
+ function shouldFetchRemoteSources(
214
+ strategy: ModelRefreshStrategy,
215
+ hasFreshCache: boolean,
216
+ hasAuthoritativeCache: boolean,
217
+ cacheAgeMs: number,
218
+ ): boolean {
219
+ if (strategy === "offline") {
220
+ return false;
221
+ }
222
+ if (strategy === "online") {
223
+ return true;
224
+ }
225
+ // online-if-uncached: skip fetch if cache is fresh.
226
+ // For non-authoritative caches (dynamic fetch previously failed),
227
+ // use a shorter retry interval instead of retrying every startup.
228
+ if (!hasFreshCache) {
229
+ return true;
230
+ }
231
+ if (!hasAuthoritativeCache) {
232
+ return cacheAgeMs >= NON_AUTHORITATIVE_RETRY_MS;
233
+ }
234
+ return false;
235
+ }
236
+
237
+ function mergeModelSources<TApi extends Api>(...sources: readonly (readonly Model<TApi>[])[]): Model<TApi>[] {
238
+ // Strip out empty/missing sources up front. The hot path is `(static, [])`
239
+ // (modelsDev disabled / failed) — a single non-empty source means we can
240
+ // skip the Map churn entirely and just hand back the array.
241
+ const nonEmpty = sources.filter(source => source.length > 0);
242
+ if (nonEmpty.length === 0) return [];
243
+ if (nonEmpty.length === 1) return [...nonEmpty[0]];
244
+ const merged = new Map<string, Model<TApi>>();
245
+ for (const source of nonEmpty) {
246
+ for (const model of source) {
247
+ if (!model?.id) continue;
248
+ merged.set(model.id, model);
249
+ }
250
+ }
251
+ return Array.from(merged.values());
252
+ }
253
+
254
+ function mergeDynamicModels<TApi extends Api>(
255
+ baseModels: readonly Model<TApi>[],
256
+ dynamicModels: readonly Model<TApi>[],
257
+ ): Model<TApi>[] {
258
+ // Empty-side fast paths: `mergeDynamicModels(base, [])` is the common shape
259
+ // after we've already merged the first pair, and `(...)` with no base
260
+ // happens for providers without static catalogs.
261
+ if (dynamicModels.length === 0) return baseModels.length === 0 ? [] : [...baseModels];
262
+ if (baseModels.length === 0) return [...dynamicModels];
263
+ const merged = new Map<string, Model<TApi>>(baseModels.map(model => [model.id, model]));
264
+ for (const dynamicModel of dynamicModels) {
265
+ if (!dynamicModel?.id) {
266
+ continue;
267
+ }
268
+ const existingModel = merged.get(dynamicModel.id);
269
+ if (!existingModel) {
270
+ merged.set(dynamicModel.id, dynamicModel);
271
+ continue;
272
+ }
273
+ merged.set(dynamicModel.id, mergeDynamicModel(existingModel, dynamicModel));
274
+ }
275
+ return Array.from(merged.values());
276
+ }
277
+
278
+ function retainModelIds<TApi extends Api>(
279
+ models: readonly Model<TApi>[],
280
+ retainedModels: readonly Model<TApi>[],
281
+ ): Model<TApi>[] {
282
+ if (retainedModels.length === 0 || models.length === 0) return [];
283
+ const retainedIds = new Set(retainedModels.map(model => model.id));
284
+ return models.filter(model => retainedIds.has(model.id));
285
+ }
286
+
287
+ /**
288
+ * Stable, low-collision fingerprint of a static catalog slice. Cached by
289
+ * reference so repeat calls in the same process (e.g. multiple cold-start
290
+ * arms calling `resolveProviderModels` with the same `staticModels` array)
291
+ * skip the JSON+hash work after the first call.
292
+ */
293
+ const MODEL_CACHE_FINGERPRINT_VERSION = "merge-v2";
294
+ const kStaticFingerprint = Symbol("model-manager.staticFingerprint");
295
+ type ModelArrayWithFingerprint = readonly Model<Api>[] & { [kStaticFingerprint]?: string };
296
+ function fingerprintStatic<TApi extends Api>(
297
+ models: readonly Model<TApi>[],
298
+ dynamicModelsAuthoritative = false,
299
+ ): string {
300
+ if (models.length === 0) return `${MODEL_CACHE_FINGERPRINT_VERSION}:empty`;
301
+ if (dynamicModelsAuthoritative)
302
+ return `${MODEL_CACHE_FINGERPRINT_VERSION}:authoritative:${fingerprintStatic(models)}`;
303
+ const tagged = models as ModelArrayWithFingerprint;
304
+ const cached = tagged[kStaticFingerprint];
305
+ if (cached !== undefined) return cached;
306
+ // `Bun.hash` returns a `bigint`; base36 keeps the string short for the
307
+ // SQLite column without sacrificing distinguishability.
308
+ const fingerprint = `${MODEL_CACHE_FINGERPRINT_VERSION}:${Bun.hash(JSON.stringify(models)).toString(36)}`;
309
+ tagged[kStaticFingerprint] = fingerprint;
310
+ return fingerprint;
311
+ }
312
+
313
+ function mergeDynamicModel<TApi extends Api>(existingModel: Model<TApi>, dynamicModel: Model<TApi>): Model<TApi> {
314
+ const supportsImage = existingModel.input.includes("image") || dynamicModel.input.includes("image");
315
+ // Re-build from spec stage: sparse compat comes from `compatConfig` (the
316
+ // verbatim override vocabulary), never the resolved `compat` record.
317
+ return buildModel({
318
+ ...existingModel,
319
+ ...dynamicModel,
320
+ name: preferDiscoveryName(dynamicModel.name, existingModel.name, dynamicModel.id),
321
+ reasoning: existingModel.reasoning || dynamicModel.reasoning,
322
+ input: supportsImage ? ["text", "image"] : ["text"],
323
+ cost: {
324
+ input: preferDiscoveryCost(dynamicModel.cost.input, existingModel.cost.input),
325
+ output: preferDiscoveryCost(dynamicModel.cost.output, existingModel.cost.output),
326
+ cacheRead: preferDiscoveryCost(dynamicModel.cost.cacheRead, existingModel.cost.cacheRead),
327
+ cacheWrite: preferDiscoveryCost(dynamicModel.cost.cacheWrite, existingModel.cost.cacheWrite),
328
+ },
329
+ contextWindow: preferDiscoveryLimit(dynamicModel.contextWindow, existingModel.contextWindow),
330
+ maxTokens: preferDiscoveryLimit(dynamicModel.maxTokens, existingModel.maxTokens),
331
+ headers: dynamicModel.headers ? { ...existingModel.headers, ...dynamicModel.headers } : existingModel.headers,
332
+ compat: dynamicModel.compatConfig ?? existingModel.compatConfig,
333
+ contextPromotionTarget: dynamicModel.contextPromotionTarget ?? existingModel.contextPromotionTarget,
334
+ } as ModelSpec<TApi>);
335
+ }
336
+
337
+ function preferDiscoveryCost(discoveryCost: number, fallbackCost: number): number {
338
+ if (Number.isFinite(discoveryCost) && discoveryCost > 0) {
339
+ return discoveryCost;
340
+ }
341
+ return fallbackCost;
342
+ }
343
+
344
+ function preferDiscoveryName(discoveryName: string, fallbackName: string, modelId: string): string {
345
+ const normalizedDiscoveryName = discoveryName.trim();
346
+ if (normalizedDiscoveryName.length === 0) {
347
+ return fallbackName;
348
+ }
349
+ if (normalizedDiscoveryName === modelId && fallbackName !== modelId) {
350
+ return fallbackName;
351
+ }
352
+ return normalizedDiscoveryName;
353
+ }
354
+
355
+ function preferDiscoveryLimit(discoveryLimit: number, fallbackLimit: number): number {
356
+ if (!Number.isFinite(discoveryLimit) || discoveryLimit <= 0) {
357
+ return fallbackLimit;
358
+ }
359
+ if (discoveryLimit === 4096 && fallbackLimit > discoveryLimit) {
360
+ return fallbackLimit;
361
+ }
362
+ return discoveryLimit;
363
+ }
364
+
365
+ function normalizeModelList<TApi extends Api>(value: unknown): Model<TApi>[] {
366
+ if (!Array.isArray(value)) {
367
+ return [];
368
+ }
369
+ const models: Model<TApi>[] = [];
370
+ for (const item of value) {
371
+ if (isModelLike(item)) {
372
+ models.push(buildModel(item as ModelSpec<TApi>));
373
+ }
374
+ }
375
+ return models;
376
+ }
377
+
378
+ function isModelLike(value: unknown): value is ModelSpec<Api> {
379
+ if (!isRecord(value)) {
380
+ return false;
381
+ }
382
+ const v = value as {
383
+ id?: unknown;
384
+ name?: unknown;
385
+ api?: unknown;
386
+ provider?: unknown;
387
+ baseUrl?: unknown;
388
+ reasoning?: unknown;
389
+ input?: unknown;
390
+ cost?: unknown;
391
+ contextWindow?: unknown;
392
+ maxTokens?: unknown;
393
+ };
394
+ if (typeof v.id !== "string" || v.id.length === 0) {
395
+ return false;
396
+ }
397
+ if (typeof v.name !== "string" || v.name.length === 0) {
398
+ return false;
399
+ }
400
+ if (typeof v.api !== "string" || v.api.length === 0) {
401
+ return false;
402
+ }
403
+ if (typeof v.provider !== "string" || v.provider.length === 0) {
404
+ return false;
405
+ }
406
+ if (typeof v.baseUrl !== "string" || v.baseUrl.length === 0) {
407
+ return false;
408
+ }
409
+ if (typeof v.reasoning !== "boolean") {
410
+ return false;
411
+ }
412
+ if (!isModelInputArray(v.input)) {
413
+ return false;
414
+ }
415
+ if (!isModelCost(v.cost)) {
416
+ return false;
417
+ }
418
+ // Finite positive: NaN > 0 is false, +Infinity < Infinity is false.
419
+ const cw = v.contextWindow;
420
+ if (typeof cw !== "number" || !(cw > 0 && cw < Infinity)) {
421
+ return false;
422
+ }
423
+ const mt = v.maxTokens;
424
+ if (typeof mt !== "number" || !(mt > 0 && mt < Infinity)) {
425
+ return false;
426
+ }
427
+ return true;
428
+ }
429
+
430
+ function isModelInputArray(value: unknown): value is ("text" | "image")[] {
431
+ if (!Array.isArray(value) || value.length === 0) {
432
+ return false;
433
+ }
434
+ for (let i = 0; i < value.length; i++) {
435
+ const item = value[i];
436
+ if (item !== "text" && item !== "image") {
437
+ return false;
438
+ }
439
+ }
440
+ return true;
441
+ }
442
+
443
+ function isModelCost(value: unknown): value is Model<Api>["cost"] {
444
+ if (!isRecord(value)) {
445
+ return false;
446
+ }
447
+ const c = value as {
448
+ input?: unknown;
449
+ output?: unknown;
450
+ cacheRead?: unknown;
451
+ cacheWrite?: unknown;
452
+ };
453
+ // Finite (NaN-safe): -Infinity < x < Infinity rejects NaN and both infinities.
454
+ // Preserves original behavior: 0 and negatives remain valid.
455
+ const ci = c.input;
456
+ if (typeof ci !== "number" || !(ci > -Infinity && ci < Infinity)) {
457
+ return false;
458
+ }
459
+ const co = c.output;
460
+ if (typeof co !== "number" || !(co > -Infinity && co < Infinity)) {
461
+ return false;
462
+ }
463
+ const cr = c.cacheRead;
464
+ if (typeof cr !== "number" || !(cr > -Infinity && cr < Infinity)) {
465
+ return false;
466
+ }
467
+ const cw = c.cacheWrite;
468
+ if (typeof cw !== "number" || !(cw > -Infinity && cw < Infinity)) {
469
+ return false;
470
+ }
471
+ return true;
472
+ }