-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.js
More file actions
388 lines (330 loc) · 13.8 KB
/
Copy pathserver.js
File metadata and controls
388 lines (330 loc) · 13.8 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env node
/**
* iblai-router — Local cost-optimizing proxy for Anthropic Claude models.
*
* Sits between OpenClaw and the Anthropic API, automatically routing each
* request to the cheapest capable Claude model using weighted scoring.
*
* All scoring config lives in config.json (or ROUTER_CONFIG env path).
* Zero dependencies — just Node.js standard library.
*
* Usage:
* ANTHROPIC_API_KEY=sk-ant-... node server.js
*/
const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
// ─── Load Config ───
const CONFIG_PATH = process.env.ROUTER_CONFIG || path.join(__dirname, "config.json");
function loadConfig() {
const raw = fs.readFileSync(CONFIG_PATH, "utf-8");
const cfg = JSON.parse(raw);
// Compile multiStepPatterns from strings to RegExp
if (cfg.scoring.multiStepPatterns) {
cfg.scoring.multiStepPatterns = cfg.scoring.multiStepPatterns.map(p =>
p instanceof RegExp ? p : new RegExp(p, "i")
);
}
// ─── Providers ───
// Each provider = { baseUrl, apiKeyEnv, auth: "x-api-key"|"bearer", format, referer }.
// Backward compat: a top-level `apiBaseUrl` (legacy) synthesizes an override for
// the default provider so old single-provider configs keep working unchanged.
cfg.defaultProvider = cfg.defaultProvider || "anthropic";
cfg.providers = cfg.providers || {};
if (!cfg.providers.anthropic) {
cfg.providers.anthropic = {
baseUrl: "https://api.anthropic.com",
apiKeyEnv: "ANTHROPIC_API_KEY",
auth: "x-api-key",
format: "anthropic",
};
}
if (cfg.apiBaseUrl) {
const isOR = /openrouter/i.test(cfg.apiBaseUrl);
cfg.providers[cfg.defaultProvider] = {
...cfg.providers[cfg.defaultProvider],
baseUrl: cfg.apiBaseUrl,
auth: isOR ? "bearer" : (cfg.providers[cfg.defaultProvider]?.auth || "x-api-key"),
referer: cfg.openRouterReferer || cfg.providers[cfg.defaultProvider]?.referer,
};
}
// ─── Tiers ───
// Normalize each tier value (string | {provider, model, stripThinking}) into a
// resolved spec. A bare string uses the default provider. LIGHT strips thinking
// by default (Haiku-class models reject extended-thinking params).
cfg.tiers = {};
for (const tier of ["LIGHT", "MEDIUM", "HEAVY"]) {
const v = cfg.models[tier];
const spec = typeof v === "string"
? { provider: cfg.defaultProvider, model: v }
: { provider: v.provider || cfg.defaultProvider, model: v.model, stripThinking: v.stripThinking };
if (spec.stripThinking === undefined) spec.stripThinking = tier === "LIGHT";
if (!cfg.providers[spec.provider]) {
throw new Error(`Tier ${tier} references unknown provider "${spec.provider}"`);
}
cfg.tiers[tier] = spec;
}
return cfg;
}
let config = loadConfig();
// Watch for config changes (hot reload)
fs.watchFile(CONFIG_PATH, { interval: 2000 }, () => {
try {
config = loadConfig();
console.log("[router] Config reloaded from", CONFIG_PATH);
} catch (e) {
console.error("[router] Config reload failed:", e.message);
}
});
// ─── Environment ───
const PORT = parseInt(process.env.ROUTER_PORT || "8402", 10);
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
const LOG_ROUTING = process.env.ROUTER_LOG !== "0";
// ─── Dimension Scorers ───
function scoreTokenCount(tokens, thresholds) {
if (tokens < thresholds.simple) return { score: -0.8, signal: `short(${tokens}t)` };
if (tokens > thresholds.complex) return { score: 0.8, signal: `long(${tokens}t)` };
return { score: 0, signal: null };
}
function scoreKeywords(text, keywords, threshLow, threshHigh, scoreLow, scoreHigh) {
const matches = keywords.filter(kw => text.includes(kw.toLowerCase()));
if (matches.length >= threshHigh) return { score: scoreHigh, signal: matches.slice(0, 3).join(",") };
if (matches.length >= threshLow) return { score: scoreLow, signal: matches.slice(0, 2).join(",") };
return { score: 0, signal: null };
}
function scorePatterns(text, patterns) {
const hits = patterns.filter(p => p.test(text));
if (hits.length > 0) return { score: 0.5, signal: "multi-step" };
return { score: 0, signal: null };
}
function scoreQuestions(text) {
const count = (text.match(/\?/g) || []).length;
if (count > 3) return { score: 0.5, signal: `${count}q` };
return { score: 0, signal: null };
}
// ─── Main Classifier ───
function classify(text, estimatedTokens) {
const s = config.scoring;
const lower = text.toLowerCase();
const dims = {
tokenCount: scoreTokenCount(estimatedTokens, s.tokenThresholds),
codePresence: scoreKeywords(lower, s.codeKeywords, 1, 3, 0.5, 1.0),
reasoningMarkers: scoreKeywords(lower, s.reasoningKeywords, 1, 2, 0.6, 1.0),
technicalTerms: scoreKeywords(lower, s.technicalKeywords, 2, 4, 0.5, 1.0),
creativeMarkers: scoreKeywords(lower, s.creativeKeywords, 1, 2, 0.4, 0.7),
simpleIndicators: scoreKeywords(lower, s.simpleKeywords, 1, 2, -0.8, -1.0),
multiStep: scorePatterns(lower, s.multiStepPatterns),
questionCount: scoreQuestions(text),
imperativeVerbs: scoreKeywords(lower, s.imperativeVerbs, 1, 2, 0.3, 0.5),
constraints: scoreKeywords(lower, s.constraintKeywords, 1, 3, 0.3, 0.7),
outputFormat: scoreKeywords(lower, s.formatKeywords, 1, 2, 0.4, 0.7),
domainSpecific: scoreKeywords(lower, s.domainKeywords, 1, 2, 0.5, 0.8),
agenticTask: scoreKeywords(lower, s.agenticKeywords, 2, 4, 0.4, 0.8),
relayIndicators: scoreKeywords(lower, s.relayKeywords, 1, 2, -0.9, -1.0),
};
// Weighted score
let score = 0;
const signals = [];
for (const [name, dim] of Object.entries(dims)) {
const w = s.weights[name] || 0;
score += dim.score * w;
if (dim.signal) signals.push(`${name}:${dim.signal}`);
}
const overrides = s.overrides || {};
// Build a decision from a tier name, attaching the resolved model + provider.
const decide = (tier, extra) => {
const spec = config.tiers[tier];
return { model: spec.model, provider: spec.provider, stripThinking: spec.stripThinking, tier, score, signals, ...extra };
};
// Override: 2+ reasoning keywords → HEAVY
const reasoningMin = overrides.reasoningKeywordMin || 2;
const reasoningHits = s.reasoningKeywords.filter(kw => lower.includes(kw.toLowerCase()));
if (reasoningHits.length >= reasoningMin) {
return decide("HEAVY", { confidence: 0.95, reasoning: "reasoning-override" });
}
// Override: large context → HEAVY
const largeCtx = overrides.largeContextTokens || 50000;
if (estimatedTokens > largeCtx) {
return decide("HEAVY", { confidence: 0.95, reasoning: "large-context" });
}
// Map score to tier
const { lightMedium, mediumHeavy } = s.boundaries;
let tier, distFromBoundary;
if (score < lightMedium) {
tier = "LIGHT";
distFromBoundary = lightMedium - score;
} else if (score < mediumHeavy) {
tier = "MEDIUM";
distFromBoundary = Math.min(score - lightMedium, mediumHeavy - score);
} else {
tier = "HEAVY";
distFromBoundary = score - mediumHeavy;
}
// Sigmoid confidence
const confidence = 1 / (1 + Math.exp(-s.confidenceSteepness * distFromBoundary));
// Ambiguous → default MEDIUM
if (confidence < s.confidenceThreshold) {
return decide("MEDIUM", { confidence, reasoning: "ambiguous→medium" });
}
return decide(tier, { confidence, reasoning: "scored" });
}
// ─── Extract scoring text from Anthropic messages format ───
function extractText(body) {
let text = "";
// Skip system prompt — it's the same for every request (OpenClaw's large
// system prompt) and inflates every score to HEAVY. Score only user messages.
if (Array.isArray(body.messages)) {
const recent = body.messages.slice(-3);
for (const msg of recent) {
if (typeof msg.content === "string") text += msg.content + " ";
else if (Array.isArray(msg.content)) {
for (const block of msg.content) {
if (block.type === "text") text += block.text + " ";
}
}
}
}
return text;
}
// ─── Proxy upstream ───
// Resolve the API key for a provider: its configured env var first, else the
// legacy ANTHROPIC_API_KEY so single-provider Anthropic setups keep working.
function providerKey(provider) {
return (provider.apiKeyEnv && process.env[provider.apiKeyEnv]) || ANTHROPIC_API_KEY;
}
function stripThinking(body, headers) {
delete body.thinking;
// Also strip thinking budget from anthropic-beta header
if (headers["anthropic-beta"]) {
const betas = headers["anthropic-beta"].split(",").map(s => s.trim())
.filter(b => !b.startsWith("thinking") && !b.startsWith("extended-thinking"));
if (betas.length > 0) {
headers["anthropic-beta"] = betas.join(",");
} else {
delete headers["anthropic-beta"];
}
}
}
function proxyUpstream(req, res, body, decision) {
body.model = decision.model;
const provider = config.providers[decision.provider] || config.providers[config.defaultProvider];
// Models that don't support extended thinking (Haiku-class / LIGHT tier) —
// strip the thinking params OpenClaw sends so the upstream doesn't 400.
if (decision.stripThinking) stripThinking(body, req.headers);
const payload = JSON.stringify(body);
const parsed = new URL(provider.baseUrl);
const options = {
hostname: parsed.hostname,
port: parsed.port || (parsed.protocol === "https:" ? 443 : 80),
path: (parsed.pathname.replace(/\/$/, "") || "") + "/v1/messages",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload),
},
};
const key = providerKey(provider);
if (provider.auth === "bearer") {
options.headers["Authorization"] = `Bearer ${key}`;
if (provider.referer) options.headers["HTTP-Referer"] = provider.referer;
} else {
options.headers["x-api-key"] = key;
}
// Anthropic-format providers still expect the version + optional beta headers.
if ((provider.format || "anthropic") === "anthropic") {
options.headers["anthropic-version"] = req.headers["anthropic-version"] || "2023-06-01";
if (req.headers["anthropic-beta"]) {
options.headers["anthropic-beta"] = req.headers["anthropic-beta"];
}
}
const transport = parsed.protocol === "https:" ? https : http;
const upstream = transport.request(options, (upstreamRes) => {
res.writeHead(upstreamRes.statusCode, upstreamRes.headers);
upstreamRes.pipe(res);
});
upstream.on("error", (err) => {
console.error("[router] upstream error:", err.message);
if (!res.headersSent) {
res.writeHead(502, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: { type: "proxy_error", message: err.message } }));
}
});
upstream.write(payload);
upstream.end();
}
// ─── Stats ───
const stats = {
total: 0,
byTier: {},
estimatedCost: 0,
baselineCost: 0,
startedAt: new Date().toISOString(),
};
// ─── HTTP Server ───
const server = http.createServer((req, res) => {
if (req.method === "GET" && (req.url === "/health" || req.url === "/")) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ status: "ok", models: config.models, tiers: config.tiers, port: PORT }));
return;
}
if (req.method === "GET" && req.url === "/stats") {
const savings = stats.baselineCost > 0
? ((1 - stats.estimatedCost / stats.baselineCost) * 100).toFixed(1) + "%"
: "n/a";
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ ...stats, savings }));
return;
}
if (req.method !== "POST" || !req.url.startsWith("/v1/messages")) {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Not found. Use POST /v1/messages" }));
return;
}
let chunks = [];
req.on("data", (chunk) => chunks.push(chunk));
req.on("end", () => {
let body;
try {
body = JSON.parse(Buffer.concat(chunks).toString());
} catch (e) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid JSON" }));
return;
}
const text = extractText(body);
const estimatedTokens = Math.ceil(text.length / 4);
const decision = classify(text, estimatedTokens);
// Track stats
stats.total++;
stats.byTier[decision.tier] = (stats.byTier[decision.tier] || 0) + 1;
const cost = config.costs[decision.model] || { input: 0 };
stats.estimatedCost += (estimatedTokens / 1_000_000) * cost.input;
const opusCost = (estimatedTokens / 1_000_000) * (config.costs[config.models.HEAVY]?.input || 15);
stats.baselineCost += opusCost;
if (LOG_ROUTING) {
const savings = opusCost > 0
? ((opusCost - (estimatedTokens / 1_000_000) * cost.input) / opusCost * 100).toFixed(0)
: 0;
console.log(
`[router] ${decision.tier.padEnd(6)} → ${decision.model} ` +
`| score=${decision.score.toFixed(3)} conf=${decision.confidence.toFixed(2)} ` +
`| ${decision.reasoning} | -${savings}% | ${text.slice(0, 80).replace(/\n/g, " ")}...`
);
}
proxyUpstream(req, res, body, decision);
});
});
// ─── Start ───
const defaultProviderCfg = config.providers[config.defaultProvider];
if (!providerKey(defaultProviderCfg)) {
const envName = defaultProviderCfg.apiKeyEnv || "ANTHROPIC_API_KEY";
console.error(`[router] No API key for default provider "${config.defaultProvider}" (set ${envName}). Exiting.`);
process.exit(1);
}
server.listen(PORT, "127.0.0.1", () => {
console.log(`[router] iblai-router listening on http://127.0.0.1:${PORT}`);
console.log(`[router] Config: ${CONFIG_PATH}`);
const fmt = t => `${config.tiers[t].provider}/${config.tiers[t].model}`;
console.log(`[router] Tiers: LIGHT=${fmt("LIGHT")} MEDIUM=${fmt("MEDIUM")} HEAVY=${fmt("HEAVY")}`);
});