forked from stevibe/ToolCall-15
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.ts
More file actions
327 lines (269 loc) · 9.36 KB
/
Copy pathmodels.ts
File metadata and controls
327 lines (269 loc) · 9.36 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
export type ProviderName = "openrouter" | "ollama" | "llamacpp" | "mlx" | "lmstudio" | "vllm";
export type ModelMetadata = {
contextWindow?: number;
costPerMillionInput?: number;
costPerMillionOutput?: number;
};
export type ModelConfig = {
id: string;
label: string;
provider: ProviderName;
model: string;
baseUrl: string;
apiKey?: string;
metadata: ModelMetadata;
};
export type PublicModelConfig = Omit<ModelConfig, "apiKey">;
export type ModelConfigGroups = {
primary: ModelConfig[];
secondary: ModelConfig[];
all: ModelConfig[];
};
export type PublicModelConfigGroups = {
primary: PublicModelConfig[];
secondary: PublicModelConfig[];
all: PublicModelConfig[];
};
const PROVIDERS = new Set<ProviderName>(["openrouter", "ollama", "llamacpp", "mlx", "lmstudio", "vllm"]);
// Known context windows for common Hermes and tool-calling models.
// Used as fallback when MODEL_CONTEXT_WINDOWS env is not set.
const KNOWN_CONTEXT_WINDOWS: Record<string, number> = {
"hermes3:8b": 8192,
"hermes3:70b": 8192,
"hermes3:405b": 8192,
"NousResearch/Hermes-3-Llama-3.1-8B": 131072,
"NousResearch/Hermes-3-Llama-3.1-70B": 131072,
"NousResearch/Hermes-3-Llama-3.1-405B": 131072,
"NousResearch/Hermes-2-Pro-Llama-3-8B": 8192,
"nousresearch/hermes-3-llama-3.1-405b": 131072,
"nousresearch/hermes-3-llama-3.1-70b": 131072,
"nousresearch/hermes-3-llama-3.1-8b": 131072,
"openai/gpt-4.1": 1047576,
"openai/gpt-4.1-mini": 1047576,
"openai/gpt-4o": 128000,
"anthropic/claude-sonnet-4": 200000,
"anthropic/claude-opus-4": 200000,
"google/gemini-2.5-pro": 1048576,
"mistralai/mistral-small-3.2": 128000,
"qwen/qwen-3-32b": 131072,
"qwen/qwen-3-8b": 131072,
};
// Known costs per million tokens (input/output) for OpenRouter models.
const KNOWN_COSTS: Record<string, { input: number; output: number }> = {
"nousresearch/hermes-3-llama-3.1-405b": { input: 5.0, output: 15.0 },
"nousresearch/hermes-3-llama-3.1-70b": { input: 0.8, output: 0.8 },
"nousresearch/hermes-3-llama-3.1-8b": { input: 0.06, output: 0.06 },
"openai/gpt-4.1": { input: 2.0, output: 8.0 },
"openai/gpt-4.1-mini": { input: 0.4, output: 1.6 },
"openai/gpt-4o": { input: 2.5, output: 10.0 },
"anthropic/claude-sonnet-4": { input: 3.0, output: 15.0 },
"anthropic/claude-opus-4": { input: 15.0, output: 75.0 },
"google/gemini-2.5-pro": { input: 1.25, output: 10.0 },
};
function resolveModelMetadata(provider: ProviderName, model: string): ModelMetadata {
const metadata: ModelMetadata = {};
// Check known context windows
const ctxKey = provider === "openrouter" ? model : `${model}`;
for (const [pattern, ctx] of Object.entries(KNOWN_CONTEXT_WINDOWS)) {
if (pattern.toLowerCase() === ctxKey.toLowerCase()) {
metadata.contextWindow = ctx;
break;
}
}
// Check known costs (mostly relevant for cloud providers)
if (provider === "openrouter") {
const costKey = model.toLowerCase();
for (const [pattern, cost] of Object.entries(KNOWN_COSTS)) {
if (pattern.toLowerCase() === costKey) {
metadata.costPerMillionInput = cost.input;
metadata.costPerMillionOutput = cost.output;
break;
}
}
}
// Parse MODEL_CONTEXT_WINDOWS env overrides: "model1:8192,model2:131072"
const ctxOverrides = process.env.MODEL_CONTEXT_WINDOWS?.trim();
if (ctxOverrides) {
for (const entry of ctxOverrides.split(",")) {
const [key, val] = entry.split(":").map(s => s.trim());
if (key && val && model.toLowerCase().includes(key.toLowerCase())) {
metadata.contextWindow = parseInt(val, 10);
}
}
}
return metadata;
}
function normalizeHostBaseUrl(host: string, envName: string): string {
const trimmed = host.trim().replace(/\/+$/, "");
if (!trimmed) {
throw new Error(`${envName} is empty.`);
}
if (!/^https?:\/\//i.test(trimmed)) {
throw new Error(`${envName} must start with http:// or https://.`);
}
const url = new URL(trimmed);
const path = url.pathname.replace(/\/+$/, "");
if (!path || path === "/") {
url.pathname = "/v1";
return url.toString().replace(/\/$/, "");
}
if (path.endsWith("/v1")) {
url.pathname = path;
return url.toString().replace(/\/$/, "");
}
if (path.endsWith("/api")) {
url.pathname = `${path.slice(0, -4) || ""}/v1`;
return url.toString().replace(/\/$/, "");
}
url.pathname = `${path}/v1`;
return url.toString().replace(/\/$/, "");
}
function providerLabel(provider: ProviderName): string {
switch (provider) {
case "openrouter":
return "OpenRouter";
case "ollama":
return "Ollama";
case "llamacpp":
return "llama.cpp";
case "mlx":
return "mlx_lm";
case "lmstudio":
return "LM Studio";
case "vllm":
return "vLLM";
}
}
function buildProviderBaseUrl(provider: ProviderName, envName: string): string {
switch (provider) {
case "openrouter":
return "https://openrouter.ai/api/v1";
case "ollama": {
const host = process.env.OLLAMA_HOST?.trim();
if (!host) {
throw new Error(`OLLAMA_HOST is required when ${envName} includes an ollama model.`);
}
return normalizeHostBaseUrl(host, "OLLAMA_HOST");
}
case "llamacpp": {
const host = process.env.LLAMACPP_HOST?.trim();
if (!host) {
throw new Error(`LLAMACPP_HOST is required when ${envName} includes a llamacpp model.`);
}
return normalizeHostBaseUrl(host, "LLAMACPP_HOST");
}
case "mlx": {
const host = process.env.MLX_HOST?.trim();
if (!host) {
throw new Error(`MLX_HOST is required when ${envName} includes an mlx model.`);
}
return normalizeHostBaseUrl(host, "MLX_HOST");
}
case "lmstudio": {
const host = process.env.LMSTUDIO_HOST?.trim();
if (!host) {
throw new Error(`LMSTUDIO_HOST is required when ${envName} includes an lmstudio model.`);
}
return normalizeHostBaseUrl(host, "LMSTUDIO_HOST");
}
case "vllm": {
const host = process.env.VLLM_HOST?.trim();
if (!host) {
throw new Error(`VLLM_HOST is required when ${envName} includes a vllm model.`);
}
return normalizeHostBaseUrl(host, "VLLM_HOST");
}
}
}
function buildProviderApiKey(provider: ProviderName, envName: string): string | undefined {
if (provider !== "openrouter") {
return undefined;
}
const apiKey = process.env.OPENROUTER_API_KEY?.trim();
if (!apiKey) {
throw new Error(`OPENROUTER_API_KEY is required when ${envName} includes an openrouter model.`);
}
return apiKey;
}
function parseProvider(rawProvider: string, index: number, envName: string): ProviderName {
const normalized = rawProvider.trim().toLowerCase();
if (!PROVIDERS.has(normalized as ProviderName)) {
throw new Error(
`${envName} entry ${index + 1} has unsupported provider "${rawProvider}". Use openrouter, ollama, llamacpp, mlx, lmstudio, or vllm.`
);
}
return normalized as ProviderName;
}
function parseModelEntry(entry: string, index: number, envName: string): ModelConfig {
const trimmed = entry.trim();
if (!trimmed) {
throw new Error(`${envName} entry ${index + 1} is empty.`);
}
const separatorIndex = trimmed.indexOf(":");
if (separatorIndex <= 0 || separatorIndex === trimmed.length - 1) {
throw new Error(
`${envName} entry ${index + 1} must use the format provider:model, for example openrouter:openai/gpt-4.1.`
);
}
const provider = parseProvider(trimmed.slice(0, separatorIndex), index, envName);
const model = trimmed.slice(separatorIndex + 1).trim();
if (!model) {
throw new Error(`${envName} entry ${index + 1} is missing the model name.`);
}
return {
id: `${provider}:${model}`,
label: `${model} via ${providerLabel(provider)}`,
provider,
model,
baseUrl: buildProviderBaseUrl(provider, envName),
apiKey: buildProviderApiKey(provider, envName),
metadata: resolveModelMetadata(provider, model)
};
}
function parseModelConfigList(envName: "LLM_MODELS" | "LLM_MODELS_2"): ModelConfig[] {
const raw = process.env[envName]?.trim() ?? "";
if (!raw) {
return [];
}
return raw
.split(",")
.map((entry) => entry.trim())
.filter(Boolean)
.map((entry, index) => parseModelEntry(entry, index, envName));
}
function assertUniqueModelIds(models: ModelConfig[]): void {
const seen = new Set<string>();
for (const model of models) {
if (seen.has(model.id)) {
throw new Error(
`Duplicate model "${model.id}" found across LLM_MODELS and LLM_MODELS_2. Each configured provider:model must be unique.`
);
}
seen.add(model.id);
}
}
export function getModelConfigGroups(): ModelConfigGroups {
const primary = parseModelConfigList("LLM_MODELS");
const secondary = parseModelConfigList("LLM_MODELS_2");
const all = [...primary, ...secondary];
assertUniqueModelIds(all);
return {
primary,
secondary,
all
};
}
export function getModelConfigs(): ModelConfig[] {
return getModelConfigGroups().all;
}
export function getPublicModelConfigGroups(): PublicModelConfigGroups {
const { primary, secondary, all } = getModelConfigGroups();
return {
primary: primary.map(({ apiKey: _apiKey, ...model }) => model),
secondary: secondary.map(({ apiKey: _apiKey, ...model }) => model),
all: all.map(({ apiKey: _apiKey, ...model }) => model)
};
}
export function getPublicModelConfigs(): PublicModelConfig[] {
return getPublicModelConfigGroups().all;
}