This repository was archived by the owner on Aug 21, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmodel-discover.ts
More file actions
102 lines (93 loc) · 3.4 KB
/
Copy pathmodel-discover.ts
File metadata and controls
102 lines (93 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/** Live Command Code model discovery without a local CLI dependency. */
import type { CommandEffort } from "./constants.js";
import { LAGUNA_MODEL_ID } from "./constants.js";
export type ModelCostRates = {
input: number;
output: number;
cache: { read: number; write: number };
};
export type DiscoveredModelMeta = {
id: string;
name: string;
description?: string;
contextWindow: number;
maxTokens: number;
vision: boolean;
free: boolean;
efforts: CommandEffort[];
reasoning: boolean;
cost?: ModelCostRates;
};
type ProviderModel = {
id: string;
name?: string;
contextLength?: number;
};
const MODEL_CATALOG_URL = "https://api.commandcode.ai/provider/v1/models";
const DEFAULT_CONTEXT = 256_000;
const DEFAULT_OUTPUT = 32_768;
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
/** Parse the public OpenAI-compatible model catalog at the network boundary. */
export function parseProviderModelCatalog(payload: unknown): ProviderModel[] {
if (!isRecord(payload) || !Array.isArray(payload.data)) return [];
const models: ProviderModel[] = [];
for (const entry of payload.data) {
if (!isRecord(entry) || typeof entry.id !== "string" || !entry.id.trim()) continue;
const contextLength = typeof entry.context_length === "number"
&& Number.isFinite(entry.context_length)
&& entry.context_length > 0
? entry.context_length
: undefined;
models.push({
id: entry.id.trim(),
...(typeof entry.name === "string" && entry.name.trim() ? { name: entry.name.trim() } : {}),
...(contextLength ? { contextLength } : {}),
});
}
return models;
}
function displayNameFromId(id: string): string {
const short = id.includes("/") ? id.slice(id.lastIndexOf("/") + 1) : id;
return short.replace(/[-_]/g, " ").replace(/\b\w/g, (character) => character.toUpperCase());
}
export function fallbackCommandModels(): DiscoveredModelMeta[] {
return [{
id: LAGUNA_MODEL_ID,
name: "Laguna S 2.1",
contextWindow: DEFAULT_CONTEXT,
maxTokens: DEFAULT_OUTPUT,
vision: false,
free: true,
efforts: [],
reasoning: true,
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
}];
}
export async function discoverCommandModels(options?: {
fetchFn?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
url?: string;
}): Promise<DiscoveredModelMeta[]> {
const response = await (options?.fetchFn ?? fetch)(options?.url ?? MODEL_CATALOG_URL, {
headers: { Accept: "application/json" },
});
if (!response.ok) throw new Error(`Model catalog request failed (${response.status})`);
const listed = parseProviderModelCatalog(await response.json());
if (listed.length === 0) throw new Error("Model catalog response contained no models");
return listed.map((model) => ({
id: model.id,
name: model.name ?? displayNameFromId(model.id),
contextWindow: model.contextLength ?? DEFAULT_CONTEXT,
maxTokens: DEFAULT_OUTPUT,
// The public endpoint does not expose modalities. Passing images through
// lets the authoritative upstream accept or reject them per model.
vision: model.id !== LAGUNA_MODEL_ID,
free: /(?:^|[-/])free(?:$|[-/])/i.test(model.id),
efforts: [],
reasoning: true,
...(model.id === LAGUNA_MODEL_ID
? { cost: { input: 0, output: 0, cache: { read: 0, write: 0 } } }
: {}),
}));
}