-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.ts
More file actions
233 lines (185 loc) · 6.08 KB
/
models.ts
File metadata and controls
233 lines (185 loc) · 6.08 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
export type ProviderName = "openrouter" | "ollama" | "llamacpp" | "mlx" | "lmstudio";
export type ModelConfig = {
id: string;
label: string;
provider: ProviderName;
model: string;
baseUrl: string;
apiKey?: string;
};
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"]);
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";
}
}
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");
}
}
}
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, or lmstudio.`
);
}
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)
};
}
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;
}