@drsamonline/gateway 0.0.0-stage → 0.12.5
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.
- package/package.json +39 -4
- package/src/alerts.js +62 -0
- package/src/index.js +156 -0
- package/src/limits.js +94 -0
- package/src/planes.js +181 -0
- package/src/server.js +886 -0
- package/src/workers.js +298 -0
- package/README.md +0 -3
package/src/server.js
ADDED
|
@@ -0,0 +1,886 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP surface.
|
|
3
|
+
*
|
|
4
|
+
* POST /v1/chat/completions OpenAI-compatible. Point any tool at it.
|
|
5
|
+
* GET /v1/models the combos, as model objects
|
|
6
|
+
* GET /api/status everything the dashboard renders, in one call
|
|
7
|
+
* GET /api/metrics aggregates: cost, latency, per provider/model/combo/arm
|
|
8
|
+
* GET /api/metrics.csv the same, as a spreadsheet export
|
|
9
|
+
* GET /api/traces prompt-free usage (hashes, not prompts)
|
|
10
|
+
* GET /api/config sanitised config (no secrets)
|
|
11
|
+
* POST /api/config/validate the dashboard's commit gate for a toggle
|
|
12
|
+
* POST /api/breaker/reset bring a resting model back early
|
|
13
|
+
* POST /api/cache/clear
|
|
14
|
+
* GET /healthz
|
|
15
|
+
* GET / the dashboard
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { createServer } from 'node:http';
|
|
19
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
20
|
+
import { join, dirname } from 'node:path';
|
|
21
|
+
import { fileURLToPath } from 'node:url';
|
|
22
|
+
import { RunRegistry } from '@drsamonline/guardrails';
|
|
23
|
+
import { createModelPlaneClient, createPlaneGuard, describePlane } from './planes.js';
|
|
24
|
+
import { checkToggle, sanitizeConfig, providerHasCredentials, providerMeetsPrivacyTier, verifyApiKey, hashApiKey, INVARIANTS, auditConfig, BUDGETS, PRIVACY_TIERS, REGIONS, tenantSnapshot, tenantSummary } from '@drsamonline/core';
|
|
25
|
+
import { SuiError } from '@drsamonline/core';
|
|
26
|
+
import { explainPlan } from '@drsamonline/router';
|
|
27
|
+
import { PRESETS, compress as compressText } from '@drsamonline/compression';
|
|
28
|
+
import { RateLimiter, checkKeyBudget } from './limits.js';
|
|
29
|
+
|
|
30
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
31
|
+
const DASHBOARD = join(HERE, '..', '..', 'dashboard', 'web', 'dashboard.html');
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {{router: any, config: any, ledger: any, breaker: any, cache: any, logger?: any}} deps
|
|
35
|
+
*/
|
|
36
|
+
export function createSuiServer(deps) {
|
|
37
|
+
const { router, config, ledger, breaker, cache, workers, canary } = deps;
|
|
38
|
+
// B5.5: one run registry per process. A run is a caller-supplied id with
|
|
39
|
+
// bounds; a request that names one is counted, costed and gated.
|
|
40
|
+
const runs = new RunRegistry({});
|
|
41
|
+
// B5.7: which half of the split this process is
|
|
42
|
+
const planes = config.planes ?? { role: 'both', modelPlaneUrl: null, serviceTokenEnv: 'SUI_PLANE_TOKEN' };
|
|
43
|
+
const planeToken = config.env?.[planes.serviceTokenEnv] ?? process.env[planes.serviceTokenEnv] ?? '';
|
|
44
|
+
const guard = createPlaneGuard({ serviceToken: planeToken, role: planes.role });
|
|
45
|
+
let planesInfo = describePlane({
|
|
46
|
+
role: planes.role,
|
|
47
|
+
modelPlaneUrl: planes.modelPlaneUrl,
|
|
48
|
+
hasServiceToken: Boolean(planeToken),
|
|
49
|
+
hasProviderCredentials: planes.role !== 'agent',
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// An agent plane that cannot reach its model plane should still start, so that
|
|
53
|
+
// /api/status can say what is wrong. A process that dies on boot with a stack
|
|
54
|
+
// trace tells the operator nothing they can act on.
|
|
55
|
+
let modelPlane = null;
|
|
56
|
+
let modelPlaneError = null;
|
|
57
|
+
if (planes.role === 'agent') {
|
|
58
|
+
if (!planes.modelPlaneUrl) {
|
|
59
|
+
modelPlaneError = 'This is the agent plane and no modelPlaneUrl is configured. Set planes.modelPlaneUrl or pass --model-plane <url>.';
|
|
60
|
+
} else if (!planeToken) {
|
|
61
|
+
modelPlaneError = `The agent plane needs a service token to reach the model plane. Set ${planes.serviceTokenEnv} to the same value on both planes (generate one with: openssl rand -hex 32).`;
|
|
62
|
+
} else {
|
|
63
|
+
try {
|
|
64
|
+
modelPlane = createModelPlaneClient({ modelPlaneUrl: planes.modelPlaneUrl, serviceToken: planeToken });
|
|
65
|
+
} catch (err) {
|
|
66
|
+
modelPlaneError = /** @type {any} */ (err)?.message ?? String(err);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (modelPlaneError) (deps.logger ?? console).warn?.(`agent plane: ${modelPlaneError}`);
|
|
70
|
+
// the reason travels with the plane description, so /api/status can show it
|
|
71
|
+
planesInfo = { ...planesInfo, error: modelPlaneError };
|
|
72
|
+
}
|
|
73
|
+
// declared here so the plane-error block above can annotate it
|
|
74
|
+
const logger = deps.logger ?? { info() {}, warn() {}, debug() {} };
|
|
75
|
+
const limiter = deps.limiter ?? new RateLimiter();
|
|
76
|
+
let configRef = config;
|
|
77
|
+
|
|
78
|
+
const server = createServer(async (req, res) => {
|
|
79
|
+
const url = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`);
|
|
80
|
+
const path = url.pathname;
|
|
81
|
+
|
|
82
|
+
// Local tools (Cline, Continue, a browser tab) call this from other origins.
|
|
83
|
+
res.setHeader('access-control-allow-origin', '*');
|
|
84
|
+
res.setHeader('access-control-allow-headers', 'authorization, content-type, x-sui-key');
|
|
85
|
+
res.setHeader('access-control-allow-methods', 'GET, POST, OPTIONS');
|
|
86
|
+
if (req.method === 'OPTIONS') {
|
|
87
|
+
res.writeHead(204).end();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// I15: Host-header allowlist. Without this, a DNS-rebinding page can reach a
|
|
92
|
+
// localhost gateway from a browser tab and read the dashboard API.
|
|
93
|
+
if (!hostAllowed(configRef, req.headers.host)) {
|
|
94
|
+
return sendJson(res, 403, {
|
|
95
|
+
error: {
|
|
96
|
+
message: `This gateway does not answer to the host "${req.headers.host}". Allowed: ${allowedHostNames(configRef).join(', ')}. Add yours to server.allowedHosts if this is intended.`,
|
|
97
|
+
type: 'host_not_allowed',
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
// B5.7: the plane boundary comes first, or a route that returns early
|
|
104
|
+
// would serve a caller the model plane should have refused.
|
|
105
|
+
const planeCheck = guard.check(req, path);
|
|
106
|
+
if (!planeCheck.ok) {
|
|
107
|
+
throw new SuiError(planeCheck.message, { code: planeCheck.code, status: planeCheck.status });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (req.method === 'GET' && (path === '/' || path === '/dashboard')) {
|
|
111
|
+
return sendHtml(res, readDashboard());
|
|
112
|
+
}
|
|
113
|
+
if (req.method === 'GET' && path === '/healthz') {
|
|
114
|
+
return sendJson(res, 200, { ok: true, service: 'sui-router', uptimeSeconds: Math.round(process.uptime()) });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ---- OpenAI-compatible surface -------------------------------------
|
|
118
|
+
if (req.method === 'POST' && (path === '/v1/chat/completions' || path === '/chat/completions')) {
|
|
119
|
+
return await handleChat(req, res);
|
|
120
|
+
}
|
|
121
|
+
if (req.method === 'GET' && (path === '/v1/models' || path === '/models')) {
|
|
122
|
+
return sendJson(res, 200, { object: 'list', data: modelList(configRef, url.searchParams.get('all') === '1') });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ---- dashboard API --------------------------------------------------
|
|
126
|
+
if (req.method === 'GET' && path === '/api/status') {
|
|
127
|
+
return sendJson(res, 200, statusPayload(configRef, ledger, breaker, cache, router, workers, planesInfo));
|
|
128
|
+
}
|
|
129
|
+
// B5.5: bounded runs. A caller that names a run gets its calls counted,
|
|
130
|
+
// costed and gated; a caller that does not is unbounded, exactly as before.
|
|
131
|
+
if (path === '/api/runs' || path.startsWith('/api/runs/')) {
|
|
132
|
+
if (req.method === 'POST' && path === '/api/runs') {
|
|
133
|
+
const body = await readJson(req);
|
|
134
|
+
const id = String(body.id ?? `run-${Date.now().toString(36)}`);
|
|
135
|
+
if (runs.get(id)) throw new SuiError(`Run "${id}" already exists.`, { code: 'run_exists', status: 409 });
|
|
136
|
+
const tracker = runs.start(id, body.limits ?? {});
|
|
137
|
+
return sendJson(res, 201, { id, run: tracker.snapshot() });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (req.method === 'GET' && path === '/api/runs') {
|
|
141
|
+
return sendJson(res, 200, { runs: runs.list() });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const rest = path.slice('/api/runs/'.length).split('/');
|
|
145
|
+
const id = decodeURIComponent(rest[0]);
|
|
146
|
+
const tracker = runs.get(id);
|
|
147
|
+
if (!tracker) throw new SuiError(`No run called "${id}".`, { code: 'unknown_run', status: 404 });
|
|
148
|
+
|
|
149
|
+
if (req.method === 'GET' && rest.length === 1) return sendJson(res, 200, { id, run: tracker.snapshot() });
|
|
150
|
+
|
|
151
|
+
if (req.method === 'POST' && rest[1] === 'approve') {
|
|
152
|
+
const body = await readJson(req);
|
|
153
|
+
if (!body.tool) throw new SuiError('Say which tool to approve.', { code: 'bad_approval', status: 400 });
|
|
154
|
+
const signature = tracker.approve({ tool: String(body.tool), input: body.input ?? {} });
|
|
155
|
+
return sendJson(res, 200, { id, approved: signature, run: tracker.snapshot() });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (req.method === 'POST' && rest[1] === 'finish') {
|
|
159
|
+
runs.finish(id);
|
|
160
|
+
return sendJson(res, 200, { id, run: tracker.snapshot() });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// B2.7: health over time. Bucketed, prompt-free, and bounded.
|
|
165
|
+
if (req.method === 'GET' && path === '/api/history') {
|
|
166
|
+
const history = workers?.history;
|
|
167
|
+
if (!history) return sendJson(res, 200, { enabled: false, series: [] });
|
|
168
|
+
const model = url.searchParams.get('model');
|
|
169
|
+
const hours = Number(url.searchParams.get('hours') ?? 24);
|
|
170
|
+
const buckets = Number(url.searchParams.get('buckets') ?? 24);
|
|
171
|
+
const series = model ? [history.series(model, { hours, buckets })] : history.all({ hours, buckets });
|
|
172
|
+
return sendJson(res, 200, { enabled: true, hours, buckets, series, summary: history.summary() });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (req.method === 'GET' && path === '/api/metrics') {
|
|
176
|
+
const since = sinceParam(url);
|
|
177
|
+
return sendJson(res, 200, ledger.aggregate({ since }));
|
|
178
|
+
}
|
|
179
|
+
if (req.method === 'GET' && path === '/api/metrics.csv') {
|
|
180
|
+
res.writeHead(200, {
|
|
181
|
+
'content-type': 'text/csv; charset=utf-8',
|
|
182
|
+
'content-disposition': `attachment; filename="sui-usage-${new Date().toISOString().slice(0, 10)}.csv"`,
|
|
183
|
+
});
|
|
184
|
+
return res.end(ledger.csv());
|
|
185
|
+
}
|
|
186
|
+
if (req.method === 'GET' && path === '/api/savings') {
|
|
187
|
+
const since = sinceParam(url);
|
|
188
|
+
const agg = ledger.aggregate({ since });
|
|
189
|
+
return sendJson(res, 200, {
|
|
190
|
+
since: agg.since,
|
|
191
|
+
savings: agg.totals.savings,
|
|
192
|
+
spentUsd: agg.totals.costUsd,
|
|
193
|
+
turns: agg.totals.turns,
|
|
194
|
+
// what the same turns would have cost without cache hits and compression
|
|
195
|
+
avoidedShare: agg.totals.costUsd + agg.totals.savings.totalUsd
|
|
196
|
+
? Math.round((agg.totals.savings.totalUsd / (agg.totals.costUsd + agg.totals.savings.totalUsd)) * 1000) / 1000
|
|
197
|
+
: 0,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (req.method === 'GET' && path === '/api/traces') {
|
|
201
|
+
return sendJson(res, 200, { prompts: ledger.promptUsage(50), note: 'Hashes only. Prompt text is never stored unless guardrails.logPrompts is on.' });
|
|
202
|
+
}
|
|
203
|
+
// B7.4: explain a route without sending a request. The plan is a pure
|
|
204
|
+
// function of config plus ledger, so this costs nothing and stores nothing.
|
|
205
|
+
if (req.method === 'GET' && path === '/api/explain') {
|
|
206
|
+
const combo = url.searchParams.get('combo') ?? Object.keys(configRef.combos)[0];
|
|
207
|
+
try {
|
|
208
|
+
const plan = router.plan(combo, {});
|
|
209
|
+
return sendJson(res, 200, { combo, lines: explainPlan(plan), plan: { ordered: plan.ordered.map((t) => t.modelId), resting: plan.resting.map((t) => t.modelId), skipped: plan.skipped } });
|
|
210
|
+
} catch (err) {
|
|
211
|
+
throw new SuiError(/** @type {any} */ (err).message, { code: 'unknown_combo', status: 404 });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// B10.5: the last eval run, if one has been done on this machine.
|
|
215
|
+
// B10.4: what a preset would do to a prompt. The text is used and dropped:
|
|
216
|
+
// it is never logged, never stored, and never sent to a provider.
|
|
217
|
+
if (req.method === 'POST' && path === '/api/compress') {
|
|
218
|
+
const body = await readJson(req);
|
|
219
|
+
const text = String(body.text ?? '');
|
|
220
|
+
const preset = String(body.preset ?? 'standard');
|
|
221
|
+
if (!PRESETS[preset]) {
|
|
222
|
+
throw new SuiError(`Unknown preset "${preset}". Use: ${Object.keys(PRESETS).join(', ')}.`, {
|
|
223
|
+
code: 'bad_preset',
|
|
224
|
+
status: 400,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
if (!text.trim()) throw new SuiError('Send some text to compress.', { code: 'empty_text', status: 400 });
|
|
228
|
+
// The body limit is 20 MB, but compression is CPU-bound: a 20 MB prompt
|
|
229
|
+
// would hold the event loop for seconds. This endpoint is a preview, so
|
|
230
|
+
// it takes a preview-sized input.
|
|
231
|
+
if (text.length > 200_000) {
|
|
232
|
+
throw new SuiError('That is too long to preview — send up to 200,000 characters.', {
|
|
233
|
+
code: 'text_too_long',
|
|
234
|
+
status: 413,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const result = compressText(text, { preset });
|
|
239
|
+
return sendJson(res, 200, {
|
|
240
|
+
preset: result.preset,
|
|
241
|
+
originalChars: result.originalChars,
|
|
242
|
+
compressedChars: result.compressedChars,
|
|
243
|
+
ratio: result.ratio,
|
|
244
|
+
savedChars: result.savedChars,
|
|
245
|
+
skipped: result.skipped,
|
|
246
|
+
steps: result.steps,
|
|
247
|
+
// the compressed text is returned so a person can read it, and that is
|
|
248
|
+
// the only place it exists
|
|
249
|
+
text: result.text,
|
|
250
|
+
note: 'Computed locally. Nothing was stored, logged or sent to a provider.',
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// B7.3: what the budget monitor has announced today.
|
|
255
|
+
if (req.method === 'GET' && path === '/api/alerts') {
|
|
256
|
+
const snap = workers?.snapshot?.() ?? { alerts: [] };
|
|
257
|
+
const spent = ledger.todaySpendUsd();
|
|
258
|
+
const cap = configRef.budgets?.dailyUsd ?? null;
|
|
259
|
+
return sendJson(res, 200, {
|
|
260
|
+
spentUsd: Math.round(spent * 1e6) / 1e6,
|
|
261
|
+
dailyUsd: cap,
|
|
262
|
+
pct: cap ? Math.round((spent / cap) * 1000) / 10 : null,
|
|
263
|
+
thresholds: [75, 90, 100],
|
|
264
|
+
alerts: snap.alerts ?? [],
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
if (req.method === 'GET' && path === '/api/evals') {
|
|
268
|
+
const reportPath =
|
|
269
|
+
process.env.SUI_EVAL_REPORT ??
|
|
270
|
+
join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'evals', 'last-report.json');
|
|
271
|
+
if (!existsSync(reportPath)) {
|
|
272
|
+
return sendJson(res, 200, {
|
|
273
|
+
available: false,
|
|
274
|
+
message: 'No eval has been run here yet. Run `npm run eval` or `npm run eval:self`.',
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
return sendJson(res, 200, { available: true, report: JSON.parse(readFileSync(reportPath, 'utf8')) });
|
|
279
|
+
} catch (err) {
|
|
280
|
+
return sendJson(res, 200, { available: false, message: `The eval report could not be read: ${/** @type {any} */ (err).message}` });
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (req.method === 'GET' && path === '/api/canary') {
|
|
284
|
+
return sendJson(res, 200, canary ? canary.snapshot() : { enabled: false, message: 'No canary is configured. See canary in sui.config.json.' });
|
|
285
|
+
}
|
|
286
|
+
if (req.method === 'POST' && path === '/api/canary/rollback') {
|
|
287
|
+
if (!canary) throw new SuiError('No canary is configured.', { code: 'no_canary', status: 404 });
|
|
288
|
+
const result = canary.rollback('rolled back by an operator');
|
|
289
|
+
return sendJson(res, 200, { ...result, snapshot: canary.snapshot() });
|
|
290
|
+
}
|
|
291
|
+
// B7.7: the tenant console. Read-only by default; writes need an admin
|
|
292
|
+
// token when one is configured, because a console that can raise a cap is
|
|
293
|
+
// a console that can spend your money.
|
|
294
|
+
if (path === '/api/tenants' || path.startsWith('/api/tenants/')) {
|
|
295
|
+
requireAdmin(req, configRef);
|
|
296
|
+
const tenants = tenantSnapshot({ config: configRef, ledger, limiter });
|
|
297
|
+
|
|
298
|
+
if (req.method === 'GET' && path === '/api/tenants') {
|
|
299
|
+
return sendJson(res, 200, { summary: tenantSummary(tenants), tenants });
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const name = decodeURIComponent(path.slice('/api/tenants/'.length));
|
|
303
|
+
const tenant = tenants.find((t) => t.name === name);
|
|
304
|
+
if (!tenant) throw new SuiError(`No tenant called "${name}".`, { code: 'unknown_tenant', status: 404 });
|
|
305
|
+
|
|
306
|
+
if (req.method === 'GET') return sendJson(res, 200, tenant);
|
|
307
|
+
|
|
308
|
+
if (req.method === 'PATCH') {
|
|
309
|
+
const body = await readJson(req);
|
|
310
|
+
const key = (configRef.apiKeys ?? []).find((k) => (k.name ?? 'unnamed') === name);
|
|
311
|
+
if (!key) throw new SuiError(`No tenant called "${name}".`, { code: 'unknown_tenant', status: 404 });
|
|
312
|
+
|
|
313
|
+
const changed = {};
|
|
314
|
+
if (body.dailyUsd !== undefined) {
|
|
315
|
+
const value = body.dailyUsd === null ? null : Number(body.dailyUsd);
|
|
316
|
+
if (value !== null && (!Number.isFinite(value) || value < 0)) {
|
|
317
|
+
throw new SuiError('dailyUsd must be a number of zero or more, or null for uncapped.', { code: 'bad_budget', status: 400 });
|
|
318
|
+
}
|
|
319
|
+
changed.dailyUsd = { from: key.dailyUsd ?? null, to: value };
|
|
320
|
+
key.dailyUsd = value ?? undefined;
|
|
321
|
+
if (value === null) delete key.dailyUsd;
|
|
322
|
+
}
|
|
323
|
+
if (body.rpm !== undefined) {
|
|
324
|
+
const value = Number(body.rpm);
|
|
325
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
326
|
+
throw new SuiError('rpm must be a number of zero or more.', { code: 'bad_rate', status: 400 });
|
|
327
|
+
}
|
|
328
|
+
key.rateLimit = { ...(key.rateLimit ?? {}), rpm: value };
|
|
329
|
+
changed.rpm = { to: value };
|
|
330
|
+
}
|
|
331
|
+
if (!Object.keys(changed).length) {
|
|
332
|
+
throw new SuiError('Nothing to change. Send dailyUsd or rpm.', { code: 'nothing_to_change', status: 400 });
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const updated = tenantSnapshot({ config: configRef, ledger, limiter }).find((t) => t.name === name);
|
|
336
|
+
return sendJson(res, 200, {
|
|
337
|
+
tenant: updated,
|
|
338
|
+
changed,
|
|
339
|
+
note: 'Applied in memory for this process. Edit sui.config.json to make it permanent.',
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (req.method === 'GET' && path === '/api/limits') {
|
|
345
|
+
return sendJson(res, 200, { buckets: limiter.snapshot(), keys: (configRef.apiKeys ?? []).map((k) => ({ name: k.name ?? 'unnamed', rateLimit: k.rateLimit ?? null, dailyUsd: k.dailyUsd ?? null })) });
|
|
346
|
+
}
|
|
347
|
+
if (req.method === 'GET' && path === '/api/workers') {
|
|
348
|
+
return sendJson(res, 200, workers ? workers.snapshot() : { enabled: false, jobs: [], probes: [] });
|
|
349
|
+
}
|
|
350
|
+
if (req.method === 'GET' && path === '/api/invariants') {
|
|
351
|
+
return sendJson(res, 200, {
|
|
352
|
+
invariants: INVARIANTS,
|
|
353
|
+
budgets: BUDGETS,
|
|
354
|
+
audit: auditConfig(configRef),
|
|
355
|
+
paidTiers: { enabled: configRef.paidTiers?.enabled === true },
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (req.method === 'GET' && path === '/api/config') {
|
|
359
|
+
return sendJson(res, 200, sanitizeConfig(configRef));
|
|
360
|
+
}
|
|
361
|
+
if (req.method === 'POST' && path === '/api/config/validate') {
|
|
362
|
+
const patch = await readJson(req);
|
|
363
|
+
const result = checkToggle(configRef, patch);
|
|
364
|
+
return sendJson(res, result.ok ? 200 : 409, result);
|
|
365
|
+
}
|
|
366
|
+
if (req.method === 'POST' && path === '/api/breaker/reset') {
|
|
367
|
+
const body = await readJson(req).catch(() => ({}));
|
|
368
|
+
if (body?.modelId) breaker.reset(body.modelId);
|
|
369
|
+
else breaker.resetAll();
|
|
370
|
+
return sendJson(res, 200, { ok: true, breakers: breaker.snapshot() });
|
|
371
|
+
}
|
|
372
|
+
if (req.method === 'POST' && path === '/api/cache/clear') {
|
|
373
|
+
cache.clear();
|
|
374
|
+
return sendJson(res, 200, { ok: true, cache: cache.stats() });
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return sendJson(res, 404, { error: { message: `No route for ${req.method} ${path}.`, type: 'not_found' } });
|
|
378
|
+
} catch (err) {
|
|
379
|
+
const e = /** @type {any} */ (err);
|
|
380
|
+
if (res.headersSent) {
|
|
381
|
+
try {
|
|
382
|
+
res.end();
|
|
383
|
+
} catch {}
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const status = e instanceof SuiError ? e.status : 500;
|
|
387
|
+
const payload = e instanceof SuiError ? e.toJSON() : {
|
|
388
|
+
error: { message: 'Something went wrong inside the gateway. The details are in the server log.', type: 'internal_error' },
|
|
389
|
+
};
|
|
390
|
+
logger.warn(`request failed: ${e?.message}`);
|
|
391
|
+
return sendJson(res, status, payload);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
/** @param {any} req @param {any} res */
|
|
396
|
+
async function handleChat(req, res) {
|
|
397
|
+
const body = await readJson(req);
|
|
398
|
+
const keyMeta = authorise(req, configRef);
|
|
399
|
+
|
|
400
|
+
// B6.6: a key is a tenant — its rate and its spend cap are checked before
|
|
401
|
+
// any provider is chosen, so a cheap combo cannot be used to bypass them.
|
|
402
|
+
if (keyMeta) {
|
|
403
|
+
const keyId = keyMeta.name ?? 'unnamed';
|
|
404
|
+
const rate = limiter.check(keyId, keyMeta.rateLimit ?? {});
|
|
405
|
+
if (!rate.ok) {
|
|
406
|
+
res.setHeader('retry-after', String(Math.ceil(rate.retryAfterMs / 1000)));
|
|
407
|
+
throw new SuiError(
|
|
408
|
+
`This API key is over its rate limit (${rate.limit} requests/minute). Try again in ${Math.max(1, Math.ceil(rate.retryAfterMs / 1000))}s.`,
|
|
409
|
+
{ code: 'rate_limited', status: 429, retryable: true, detail: `key: ${keyId}` },
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
const budget = checkKeyBudget({ keyMeta, ledger });
|
|
413
|
+
if (!budget.ok) {
|
|
414
|
+
throw new SuiError(budget.message, { code: 'key_budget_reached', status: 429, retryable: false, detail: `key: ${keyId}` });
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const requested = body.model ?? Object.keys(configRef.combos)[0];
|
|
419
|
+
if (!configRef.combos[requested] && !configRef.models[requested]) {
|
|
420
|
+
throw new SuiError(
|
|
421
|
+
`"${requested}" is not a combo or model this gateway knows. Call GET /v1/models for the list.`,
|
|
422
|
+
{ code: 'unknown_model', status: 404 },
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
if (keyMeta?.allowedCombos && !keyMeta.allowedCombos.includes(requested)) {
|
|
426
|
+
throw new SuiError(`This API key may not use "${requested}".`, { code: 'forbidden_model', status: 403 });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const stream = Boolean(body.stream);
|
|
430
|
+
const startedAt = Date.now();
|
|
431
|
+
// I2 measures OUR overhead, so it is measured here, on the server side.
|
|
432
|
+
// Client sockets, DNS and JSON serialisation belong to the caller, not to us.
|
|
433
|
+
const handlerStart = performance.now();
|
|
434
|
+
/** @type {{provider?: string, modelId?: string, model?: string, name?: string}|null} */
|
|
435
|
+
let target = null;
|
|
436
|
+
/** @type {boolean} */
|
|
437
|
+
let committed = false;
|
|
438
|
+
|
|
439
|
+
/** @param {string} delta */
|
|
440
|
+
const onToken = (delta) => {
|
|
441
|
+
if (!committed) {
|
|
442
|
+
committed = true;
|
|
443
|
+
res.writeHead(200, {
|
|
444
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
445
|
+
'cache-control': 'no-cache, no-transform',
|
|
446
|
+
connection: 'keep-alive',
|
|
447
|
+
'x-sui-provider': String(target?.providerId ?? ''),
|
|
448
|
+
'x-sui-model': String(target?.model ?? ''),
|
|
449
|
+
'x-sui-combo': String(requested),
|
|
450
|
+
'x-sui-ttft-ms': String(Date.now() - startedAt),
|
|
451
|
+
});
|
|
452
|
+
res.write(
|
|
453
|
+
sse({
|
|
454
|
+
id: `chatcmpl-sui-${startedAt}`,
|
|
455
|
+
object: 'chat.completion.chunk',
|
|
456
|
+
created: Math.floor(startedAt / 1000),
|
|
457
|
+
model: requested,
|
|
458
|
+
choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }],
|
|
459
|
+
}),
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
res.write(
|
|
463
|
+
sse({
|
|
464
|
+
id: `chatcmpl-sui-${startedAt}`,
|
|
465
|
+
object: 'chat.completion.chunk',
|
|
466
|
+
created: Math.floor(startedAt / 1000),
|
|
467
|
+
model: requested,
|
|
468
|
+
choices: [{ index: 0, delta: { content: delta }, finish_reason: null }],
|
|
469
|
+
}),
|
|
470
|
+
);
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
// B6.5: a caller may ask for a stricter privacy tier than the default.
|
|
474
|
+
const requestedTier = String(
|
|
475
|
+
req.headers['x-sui-privacy'] ?? body.sui_privacy ?? configRef.privacy?.defaultTier ?? 'any',
|
|
476
|
+
);
|
|
477
|
+
if (!PRIVACY_TIERS.includes(requestedTier)) {
|
|
478
|
+
throw new SuiError(`Unknown privacy tier "${requestedTier}". Use ${PRIVACY_TIERS.join(', ')}.`, {
|
|
479
|
+
code: 'bad_privacy_tier',
|
|
480
|
+
status: 400,
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// B7.1: attribution the caller supplies. Nothing is inferred beyond the key
|
|
485
|
+
// name and the user agent, and neither is treated as identity.
|
|
486
|
+
const attribution = {
|
|
487
|
+
user: String(req.headers['x-sui-user'] ?? keyMeta?.name ?? 'unattributed').slice(0, 64),
|
|
488
|
+
tool: String(req.headers['x-sui-tool'] ?? shortAgent(req.headers['user-agent']) ?? 'unattributed').slice(0, 64),
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// B6.6: a caller may require a region, exactly like a privacy tier
|
|
492
|
+
const requestedRegion = String(req.headers['x-sui-region'] ?? body.sui_region ?? configRef.privacy?.region ?? 'any');
|
|
493
|
+
if (!REGIONS.includes(requestedRegion)) {
|
|
494
|
+
throw new SuiError(`Unknown region "${requestedRegion}". Use ${REGIONS.join(', ')}.`, { code: 'bad_region', status: 400 });
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// B5.5: a named run is bounded. The check happens before the call, and the
|
|
498
|
+
// record happens after, so a loop cannot slip past on a technicality.
|
|
499
|
+
const runId = req.headers['x-sui-run'] ? String(req.headers['x-sui-run']) : null;
|
|
500
|
+
const tracker = runId ? runs.get(runId) : null;
|
|
501
|
+
if (runId && !tracker) {
|
|
502
|
+
throw new SuiError(`No run called "${runId}". Create one with POST /api/runs first.`, { code: 'unknown_run', status: 404 });
|
|
503
|
+
}
|
|
504
|
+
if (tracker) {
|
|
505
|
+
const decision = tracker.check({ tool: 'chat', input: { combo: requested, prompt: body?.messages?.at(-1)?.content ?? '' } });
|
|
506
|
+
if (decision.decision === 'deny') {
|
|
507
|
+
throw new SuiError(decision.reason, { code: 'run_bounded', status: 409, detail: decision.signature });
|
|
508
|
+
}
|
|
509
|
+
if (decision.decision === 'needsApproval') {
|
|
510
|
+
throw new SuiError(decision.reason, { code: 'run_needs_approval', status: 409, detail: decision.signature });
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// B5.7: an agent plane has no providers. It bounds the run, then asks the
|
|
515
|
+
// model plane to do the work.
|
|
516
|
+
if (planes.role === 'agent') {
|
|
517
|
+
if (tracker) {
|
|
518
|
+
const decision = tracker.check({ tool: 'chat', input: { combo: requested, prompt: body?.messages?.at(-1)?.content ?? '' } });
|
|
519
|
+
if (decision.decision !== 'allow') {
|
|
520
|
+
throw new SuiError(decision.reason, {
|
|
521
|
+
code: decision.decision === 'deny' ? 'run_bounded' : 'run_needs_approval',
|
|
522
|
+
status: 409,
|
|
523
|
+
detail: decision.signature,
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
if (!modelPlane) {
|
|
528
|
+
throw new SuiError(modelPlaneError ?? 'This agent plane cannot reach a model plane.', {
|
|
529
|
+
code: 'model_plane_not_configured',
|
|
530
|
+
status: 503,
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
const forwarded = await modelPlane.forward({
|
|
534
|
+
path: '/v1/chat/completions',
|
|
535
|
+
method: 'POST',
|
|
536
|
+
body,
|
|
537
|
+
headers: { 'x-sui-user': attribution.user, 'x-sui-tool': attribution.tool, 'x-sui-region': requestedRegion },
|
|
538
|
+
});
|
|
539
|
+
if (tracker && forwarded.status === 200) {
|
|
540
|
+
tracker.record({
|
|
541
|
+
tool: 'chat',
|
|
542
|
+
input: { combo: requested, prompt: body?.messages?.at(-1)?.content ?? '' },
|
|
543
|
+
costUsd: forwarded.json?.sui?.costUsd ?? 0,
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
res.writeHead(forwarded.status, { 'content-type': 'application/json' });
|
|
547
|
+
res.end(forwarded.text);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const result = await router.chat({
|
|
552
|
+
combo: requested,
|
|
553
|
+
body,
|
|
554
|
+
keyMeta,
|
|
555
|
+
attribution,
|
|
556
|
+
region: requestedRegion,
|
|
557
|
+
privacyTier: requestedTier,
|
|
558
|
+
// I12: naming a model directly is a deliberate act, not auto-routing, so
|
|
559
|
+
// an explicitly requested tos:avoid model is allowed through.
|
|
560
|
+
allowAvoid: !configRef.combos[requested] && Boolean(configRef.models[requested]),
|
|
561
|
+
onToken: stream ? onToken : undefined,
|
|
562
|
+
onAttempt: (t) => {
|
|
563
|
+
target = t;
|
|
564
|
+
},
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
// B5.5: the call happened, so it counts against the run — before the
|
|
568
|
+
// response is written, so a client cannot outrun the bound.
|
|
569
|
+
if (tracker) {
|
|
570
|
+
tracker.record({ tool: 'chat', input: { combo: requested, prompt: body?.messages?.at(-1)?.content ?? '' }, costUsd: result.meta?.costUsd ?? 0 });
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (stream) {
|
|
574
|
+
const meta = result.meta;
|
|
575
|
+
meta.handlerMs = Math.round((performance.now() - handlerStart) * 100) / 100;
|
|
576
|
+
meta.overheadMs = Math.max(0, Math.round((meta.handlerMs - meta.latencyMs) * 100) / 100);
|
|
577
|
+
res.write(
|
|
578
|
+
sse({
|
|
579
|
+
id: `chatcmpl-sui-${startedAt}`,
|
|
580
|
+
object: 'chat.completion.chunk',
|
|
581
|
+
created: Math.floor(startedAt / 1000),
|
|
582
|
+
model: requested,
|
|
583
|
+
choices: [{ index: 0, delta: {}, finish_reason: result.finishReason ?? 'stop' }],
|
|
584
|
+
usage: {
|
|
585
|
+
prompt_tokens: result.usage.prompt_tokens,
|
|
586
|
+
completion_tokens: result.usage.completion_tokens,
|
|
587
|
+
total_tokens: result.usage.prompt_tokens + result.usage.completion_tokens,
|
|
588
|
+
},
|
|
589
|
+
sui: provenance(meta),
|
|
590
|
+
}),
|
|
591
|
+
);
|
|
592
|
+
res.write('data: [DONE]\n\n');
|
|
593
|
+
return res.end();
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const meta = result.meta;
|
|
597
|
+
meta.handlerMs = Math.round((performance.now() - handlerStart) * 100) / 100;
|
|
598
|
+
meta.overheadMs = Math.max(0, Math.round((meta.handlerMs - meta.latencyMs) * 100) / 100);
|
|
599
|
+
res.setHeader('x-sui-handler-ms', String(meta.handlerMs));
|
|
600
|
+
res.setHeader('x-sui-overhead-ms', String(meta.overheadMs));
|
|
601
|
+
res.setHeader('x-sui-provider', meta.provider);
|
|
602
|
+
res.setHeader('x-sui-model', meta.model);
|
|
603
|
+
res.setHeader('x-sui-combo', meta.combo);
|
|
604
|
+
res.setHeader('x-sui-verified', String(meta.verified));
|
|
605
|
+
res.setHeader('x-sui-cached', String(meta.cached));
|
|
606
|
+
if (meta.cacheKind) res.setHeader('x-sui-cache-kind', meta.cacheKind);
|
|
607
|
+
res.setHeader('x-sui-cost-usd', String(meta.costUsd));
|
|
608
|
+
res.setHeader('x-sui-latency-ms', String(meta.latencyMs));
|
|
609
|
+
|
|
610
|
+
return sendJson(res, 200, {
|
|
611
|
+
id: `chatcmpl-sui-${startedAt}`,
|
|
612
|
+
object: 'chat.completion',
|
|
613
|
+
created: Math.floor(startedAt / 1000),
|
|
614
|
+
model: requested,
|
|
615
|
+
choices: [
|
|
616
|
+
{
|
|
617
|
+
index: 0,
|
|
618
|
+
message: { role: 'assistant', content: result.text },
|
|
619
|
+
finish_reason: result.finishReason ?? 'stop',
|
|
620
|
+
},
|
|
621
|
+
],
|
|
622
|
+
usage: {
|
|
623
|
+
prompt_tokens: result.usage.prompt_tokens,
|
|
624
|
+
completion_tokens: result.usage.completion_tokens,
|
|
625
|
+
total_tokens: result.usage.prompt_tokens + result.usage.completion_tokens,
|
|
626
|
+
},
|
|
627
|
+
sui: provenance(meta),
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
return server;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** The chip under every answer: who answered, was it real, was it cached, how fast. */
|
|
635
|
+
export function provenance(meta) {
|
|
636
|
+
return {
|
|
637
|
+
provider: meta.provider,
|
|
638
|
+
providerName: meta.providerName,
|
|
639
|
+
model: meta.model,
|
|
640
|
+
modelId: meta.modelId,
|
|
641
|
+
combo: meta.combo,
|
|
642
|
+
arm: meta.arm,
|
|
643
|
+
strategy: meta.strategy,
|
|
644
|
+
verified: meta.verified,
|
|
645
|
+
cached: meta.cached,
|
|
646
|
+
cacheKind: meta.cacheKind ?? null,
|
|
647
|
+
compression: meta.compression ?? null,
|
|
648
|
+
verification: meta.verification ?? null,
|
|
649
|
+
route: meta.route ?? null,
|
|
650
|
+
latencyMs: meta.latencyMs,
|
|
651
|
+
ttftMs: meta.ttftMs,
|
|
652
|
+
costUsd: meta.costUsd,
|
|
653
|
+
handlerMs: meta.handlerMs ?? null,
|
|
654
|
+
overheadMs: meta.overheadMs ?? null,
|
|
655
|
+
escalation: meta.escalation ?? null,
|
|
656
|
+
attempts: meta.attempts ?? [],
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** @param {any} config @param {boolean} all */
|
|
661
|
+
function modelList(config, all) {
|
|
662
|
+
const combos = Object.entries(config.combos).map(([id, c]) => ({
|
|
663
|
+
id,
|
|
664
|
+
object: 'model',
|
|
665
|
+
owned_by: 'sui-router',
|
|
666
|
+
label: /** @type {any} */ (c).label ?? id,
|
|
667
|
+
strategy: /** @type {any} */ (c).strategy,
|
|
668
|
+
targets: /** @type {any} */ (c).targets.map((t) => (typeof t === 'string' ? t : t.model)),
|
|
669
|
+
}));
|
|
670
|
+
if (!all) return combos;
|
|
671
|
+
const models = Object.entries(config.models).map(([id, m]) => ({
|
|
672
|
+
id,
|
|
673
|
+
object: 'model',
|
|
674
|
+
owned_by: /** @type {any} */ (m).provider,
|
|
675
|
+
label: /** @type {any} */ (m).label ?? id,
|
|
676
|
+
available: providerHasCredentials(config.providers[/** @type {any} */ (m).provider], config.env, config),
|
|
677
|
+
}));
|
|
678
|
+
return [...combos, ...models];
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function statusPayload(config, ledger, breaker, cache, router, workers = null, planesInfo = null) {
|
|
682
|
+
const agg = ledger.aggregate({ since: startOfToday() });
|
|
683
|
+
const spent = ledger.todaySpendUsd();
|
|
684
|
+
const daily = config.budgets.dailyUsd;
|
|
685
|
+
|
|
686
|
+
const pool = Object.entries(config.models)
|
|
687
|
+
.filter(([, m]) => providerHasCredentials(config.providers[/** @type {any} */ (m).provider], config.env, config))
|
|
688
|
+
.map(([id, m]) => {
|
|
689
|
+
const state = breaker.stateOf(id);
|
|
690
|
+
return {
|
|
691
|
+
modelId: id,
|
|
692
|
+
label: /** @type {any} */ (m).label ?? id,
|
|
693
|
+
provider: /** @type {any} */ (m).provider,
|
|
694
|
+
quality: /** @type {any} */ (m).quality ?? null,
|
|
695
|
+
tier: /** @type {any} */ (m).tier ?? 'paid',
|
|
696
|
+
costPer1kIn: /** @type {any} */ (m).costPer1kIn ?? 0,
|
|
697
|
+
costPer1kOut: /** @type {any} */ (m).costPer1kOut ?? 0,
|
|
698
|
+
health: state.state,
|
|
699
|
+
retryInMs: state.retryInMs,
|
|
700
|
+
lastError: state.lastError,
|
|
701
|
+
turnsToday: agg.byModel[id]?.turns ?? 0,
|
|
702
|
+
costToday: agg.byModel[id]?.costUsd ?? 0,
|
|
703
|
+
avgLatencyMs: agg.byModel[id]?.avgLatencyMs ?? 0,
|
|
704
|
+
};
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
const unconfigured = Object.entries(config.models)
|
|
708
|
+
.filter(([, m]) => !providerHasCredentials(config.providers[/** @type {any} */ (m).provider], config.env, config))
|
|
709
|
+
.map(([id, m]) => ({ modelId: id, provider: /** @type {any} */ (m).provider, needsEnv: config.providers[/** @type {any} */ (m).provider]?.apiKeyEnv ?? null }));
|
|
710
|
+
|
|
711
|
+
return {
|
|
712
|
+
server: { host: config.server.host, port: config.server.port, uptimeSeconds: Math.round(process.uptime()) },
|
|
713
|
+
// B5.7: which half of the split this is, and what it can reach
|
|
714
|
+
planes: planesInfo,
|
|
715
|
+
// B9.4: quota windows the router is currently honouring
|
|
716
|
+
quotaWindows: router.quotaSnapshot?.() ?? [],
|
|
717
|
+
// B6.4: what each provider does with what you send it, and which tiers it
|
|
718
|
+
// clears — the answer a person needs before choosing a default tier.
|
|
719
|
+
privacy: {
|
|
720
|
+
defaultTier: config.privacy?.defaultTier ?? 'any',
|
|
721
|
+
providers: Object.entries(config.providers ?? {}).map(([id, provider]) => ({
|
|
722
|
+
id,
|
|
723
|
+
retention: provider.privacy?.retention ?? 'unknown',
|
|
724
|
+
trainsOnData: provider.privacy?.trainsOnData ?? null,
|
|
725
|
+
tiers: {
|
|
726
|
+
standard: providerMeetsPrivacyTier(provider, 'standard'),
|
|
727
|
+
strict: providerMeetsPrivacyTier(provider, 'strict'),
|
|
728
|
+
},
|
|
729
|
+
})),
|
|
730
|
+
},
|
|
731
|
+
configPath: config.path,
|
|
732
|
+
budget: {
|
|
733
|
+
dailyUsd: daily,
|
|
734
|
+
spentUsd: spent,
|
|
735
|
+
pct: daily ? Math.min(100, Math.round((spent / Number(daily)) * 100)) : null,
|
|
736
|
+
warnAtPct: config.budgets.warnAtPct,
|
|
737
|
+
},
|
|
738
|
+
pool,
|
|
739
|
+
unconfigured,
|
|
740
|
+
combos: Object.entries(config.combos).map(([id, c]) => ({
|
|
741
|
+
id,
|
|
742
|
+
label: /** @type {any} */ (c).label ?? id,
|
|
743
|
+
strategy: /** @type {any} */ (c).strategy,
|
|
744
|
+
targets: /** @type {any} */ (c).targets.map((t) => (typeof t === 'string' ? t : t.model)),
|
|
745
|
+
turnsToday: agg.byCombo[id]?.turns ?? 0,
|
|
746
|
+
costToday: agg.byCombo[id]?.costUsd ?? 0,
|
|
747
|
+
})),
|
|
748
|
+
metrics: agg,
|
|
749
|
+
cache: cache.stats(),
|
|
750
|
+
breakers: breaker.snapshot(),
|
|
751
|
+
guardrails: config.guardrails,
|
|
752
|
+
paidTiers: { enabled: config.paidTiers?.enabled === true },
|
|
753
|
+
invariantWarnings: auditConfig(config).filter((a) => !a.ok),
|
|
754
|
+
probes: workers?.snapshot?.().probes ?? [],
|
|
755
|
+
promptUsage: ledger.promptUsage(20),
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
function startOfToday() {
|
|
760
|
+
const d = new Date();
|
|
761
|
+
d.setHours(0, 0, 0, 0);
|
|
762
|
+
return d.getTime();
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
function sinceParam(url) {
|
|
766
|
+
const raw = url.searchParams.get('since');
|
|
767
|
+
if (!raw) return startOfToday();
|
|
768
|
+
if (raw === 'all') return 0;
|
|
769
|
+
const n = Number(raw);
|
|
770
|
+
if (Number.isFinite(n) && n > 0) return n;
|
|
771
|
+
const parsed = Date.parse(raw);
|
|
772
|
+
return Number.isFinite(parsed) ? parsed : startOfToday();
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/** @param {any} req @param {any} config */
|
|
776
|
+
/**
|
|
777
|
+
* The console is admin surface: when SUI_ADMIN_TOKEN is set, every write and the
|
|
778
|
+
* tenant list need it. Without one, it is loopback-only like the rest of /api.
|
|
779
|
+
*/
|
|
780
|
+
function requireAdmin(req, config) {
|
|
781
|
+
const expected = config.env?.SUI_ADMIN_TOKEN ?? process.env.SUI_ADMIN_TOKEN;
|
|
782
|
+
if (!expected) return;
|
|
783
|
+
const presented = String(req.headers['x-sui-admin'] ?? req.headers.authorization ?? '').replace(/^Bearer\s+/i, '');
|
|
784
|
+
if (presented !== expected) {
|
|
785
|
+
throw new SuiError('This endpoint needs the admin token (SUI_ADMIN_TOKEN).', { code: 'admin_required', status: 403 });
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/** A user agent is a label, not a fingerprint: keep the product, drop the rest. */
|
|
790
|
+
function shortAgent(agent) {
|
|
791
|
+
if (!agent) return null;
|
|
792
|
+
const first = String(agent).split(/[\s/]/)[0];
|
|
793
|
+
return first ? first.slice(0, 40) : null;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
function authorise(req, config) {
|
|
797
|
+
const keys = config.apiKeys ?? [];
|
|
798
|
+
const envKey = config.env?.SUI_API_KEY;
|
|
799
|
+
if (!keys.length && !envKey) return null;
|
|
800
|
+
|
|
801
|
+
const header = String(req.headers.authorization ?? '');
|
|
802
|
+
const presented = header.startsWith('Bearer ') ? header.slice(7).trim() : String(req.headers['x-sui-key'] ?? '');
|
|
803
|
+
if (!presented) {
|
|
804
|
+
throw new SuiError('This gateway needs an API key. Send it as "Authorization: Bearer <key>".', {
|
|
805
|
+
code: 'unauthorized',
|
|
806
|
+
status: 401,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
const match = keys.find((k) => verifyApiKey(presented, k));
|
|
810
|
+
if (match) return match;
|
|
811
|
+
if (envKey && verifyApiKey(presented, { key: envKey })) {
|
|
812
|
+
return { key_hash: hashApiKey(envKey), name: 'SUI_API_KEY', allowedCombos: null };
|
|
813
|
+
}
|
|
814
|
+
throw new SuiError('That API key is not recognised by this gateway.', { code: 'unauthorized', status: 401 });
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
function readDashboard() {
|
|
818
|
+
try {
|
|
819
|
+
return readFileSync(DASHBOARD, 'utf8');
|
|
820
|
+
} catch {
|
|
821
|
+
return '<!doctype html><meta charset="utf-8"><title>Sui</title><body style="font:16px system-ui;padding:2rem"><h1>Dashboard file missing</h1><p>Expected web/dashboard.html next to the server. Reinstall the package.</p></body>';
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/** @param {any} req */
|
|
826
|
+
function readJson(req) {
|
|
827
|
+
return new Promise((resolve, reject) => {
|
|
828
|
+
let raw = '';
|
|
829
|
+
req.on('data', (c) => {
|
|
830
|
+
raw += c;
|
|
831
|
+
if (raw.length > 20_000_000) {
|
|
832
|
+
reject(new SuiError('Request body is larger than 20 MB.', { code: 'payload_too_large', status: 413 }));
|
|
833
|
+
req.destroy();
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
req.on('end', () => {
|
|
837
|
+
if (!raw.trim()) return resolve({});
|
|
838
|
+
try {
|
|
839
|
+
resolve(JSON.parse(raw));
|
|
840
|
+
} catch {
|
|
841
|
+
reject(new SuiError('Request body is not valid JSON.', { code: 'bad_request', status: 400 }));
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
req.on('error', reject);
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/** @param {any} res @param {number} status @param {any} payload */
|
|
849
|
+
function sendJson(res, status, payload) {
|
|
850
|
+
const body = JSON.stringify(payload);
|
|
851
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'content-length': Buffer.byteLength(body) });
|
|
852
|
+
res.end(body);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/** @param {any} res @param {string} html */
|
|
856
|
+
function sendHtml(res, html) {
|
|
857
|
+
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' });
|
|
858
|
+
res.end(html);
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
function sse(obj) {
|
|
862
|
+
return `data: ${JSON.stringify(obj)}\n\n`;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/* ------------------------------------------------------- host allowlisting */
|
|
866
|
+
|
|
867
|
+
const DEFAULT_ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '[::1]', '::1'];
|
|
868
|
+
|
|
869
|
+
/** @param {any} config */
|
|
870
|
+
function allowedHostNames(config) {
|
|
871
|
+
return config.server?.allowedHosts?.length ? config.server.allowedHosts : DEFAULT_ALLOWED_HOSTS;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/**
|
|
875
|
+
* @param {any} config
|
|
876
|
+
* @param {string|undefined} hostHeader
|
|
877
|
+
*/
|
|
878
|
+
export function hostAllowed(config, hostHeader) {
|
|
879
|
+
if (!hostHeader) return true; // HTTP/1.0 and some local clients omit it
|
|
880
|
+
const host = String(hostHeader).toLowerCase();
|
|
881
|
+
const withoutPort = host.replace(/:\d+$/, '');
|
|
882
|
+
return allowedHostNames(config).some((allowed) => {
|
|
883
|
+
const a = String(allowed).toLowerCase();
|
|
884
|
+
return a === host || a === withoutPort;
|
|
885
|
+
});
|
|
886
|
+
}
|