@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/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drsamonline/gateway",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.12.5",
|
|
4
|
+
"description": "HTTP gateway: OpenAI-compatible surface, dashboard API.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./src/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js",
|
|
10
|
+
"./src/*": "./src/*"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"web",
|
|
15
|
+
"bin",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git://github.com/drsamonline/sui.git",
|
|
21
|
+
"directory": "apps/gateway"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/drsamonline/sui#readme",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@drsamonline/canary": "^0.12.5",
|
|
30
|
+
"@drsamonline/catalog": "^0.12.5",
|
|
31
|
+
"@drsamonline/compression": "^0.12.5",
|
|
32
|
+
"@drsamonline/core": "^0.12.5",
|
|
33
|
+
"@drsamonline/guardrails": "^0.12.5",
|
|
34
|
+
"@drsamonline/prober": "^0.12.5",
|
|
35
|
+
"@drsamonline/providers": "^0.12.5",
|
|
36
|
+
"@drsamonline/redteam": "^0.12.5",
|
|
37
|
+
"@drsamonline/router": "^0.12.5",
|
|
38
|
+
"@drsamonline/telemetry": "^0.12.5",
|
|
39
|
+
"@drsamonline/vault": "^0.12.5"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/alerts.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Budget alerts (B7.3).
|
|
3
|
+
*
|
|
4
|
+
* A budget that is never announced is a surprise, not a control. These are the
|
|
5
|
+
* thresholds the spec asks for — 75, 90 and 100 percent — applied to the daily
|
|
6
|
+
* cap and to each per-provider cap.
|
|
7
|
+
*
|
|
8
|
+
* Two rules:
|
|
9
|
+
* 1. **One alert per threshold per scope per day.** An alert that fires on every
|
|
10
|
+
* request is noise, and noise gets muted.
|
|
11
|
+
* 2. **The message carries the numbers.** "Budget alert" tells nobody anything;
|
|
12
|
+
* "$4.51 of $5.00 spent today (90%)" tells them what to do.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const THRESHOLDS = [75, 90, 100];
|
|
16
|
+
|
|
17
|
+
const LEVELS = { 75: 'warning', 90: 'critical', 100: 'exhausted' };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {{config: any, ledger: any, now?: () => number, dayKey?: string}} input
|
|
21
|
+
* @returns {Array<{scope: string, scopeId: string, pct: number, threshold: number, level: string, message: string, key: string}>}
|
|
22
|
+
*/
|
|
23
|
+
export function checkBudgets(input) {
|
|
24
|
+
const now = input.now ?? Date.now;
|
|
25
|
+
const day = input.dayKey ?? new Date(now()).toISOString().slice(0, 10);
|
|
26
|
+
const alerts = [];
|
|
27
|
+
|
|
28
|
+
const consider = (scope, scopeId, spent, cap) => {
|
|
29
|
+
if (cap == null || !Number.isFinite(Number(cap)) || Number(cap) <= 0) return;
|
|
30
|
+
const pct = Math.round((spent / Number(cap)) * 1000) / 10;
|
|
31
|
+
const threshold = [...THRESHOLDS].reverse().find((t) => pct >= t);
|
|
32
|
+
if (!threshold) return;
|
|
33
|
+
|
|
34
|
+
const level = LEVELS[threshold];
|
|
35
|
+
const money = (v) => `$${Number(v).toFixed(2)}`;
|
|
36
|
+
const message =
|
|
37
|
+
scope === 'daily'
|
|
38
|
+
? `Daily budget ${level}: ${money(spent)} of ${money(cap)} spent today (${pct}%).`
|
|
39
|
+
: `Provider budget ${level}: ${scopeId} has spent ${money(spent)} of ${money(cap)} today (${pct}%).`;
|
|
40
|
+
|
|
41
|
+
alerts.push({ scope, scopeId, pct, threshold, level, message, key: `${day}:${scope}:${scopeId}:${threshold}` });
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
consider('daily', 'gateway', input.ledger.todaySpendUsd(), input.config?.budgets?.dailyUsd);
|
|
45
|
+
|
|
46
|
+
for (const [providerId, cap] of Object.entries(input.config?.budgets?.perProvider ?? {})) {
|
|
47
|
+
consider('provider', providerId, input.ledger.providerSpendToday(providerId), cap);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return alerts;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Keep only alerts that have not been sent yet, and remember them.
|
|
55
|
+
* @param {ReturnType<typeof checkBudgets>} alerts
|
|
56
|
+
* @param {Set<string>} sent
|
|
57
|
+
*/
|
|
58
|
+
export function dedupe(alerts, sent) {
|
|
59
|
+
const fresh = alerts.filter((a) => !sent.has(a.key));
|
|
60
|
+
for (const a of fresh) sent.add(a.key);
|
|
61
|
+
return fresh;
|
|
62
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @drsamonline/gateway — public API.
|
|
3
|
+
*
|
|
4
|
+
* import { createGateway } from '@drsamonline/gateway';
|
|
5
|
+
* const gw = await createGateway({ configPath: './sui.config.json' });
|
|
6
|
+
* gw.server.listen(gw.config.server.port);
|
|
7
|
+
*
|
|
8
|
+
* Everything the CLI does is a thin wrapper over this.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
12
|
+
import { join, resolve } from 'node:path';
|
|
13
|
+
import { CircuitBreaker } from '@drsamonline/router';
|
|
14
|
+
import { ResponseCache } from '@drsamonline/telemetry';
|
|
15
|
+
import { loadConfig, normalizeConfig, suiHome } from '@drsamonline/core';
|
|
16
|
+
import { Ledger } from '@drsamonline/telemetry';
|
|
17
|
+
import { Router } from '@drsamonline/router';
|
|
18
|
+
import { openVaultForStartup } from '@drsamonline/vault';
|
|
19
|
+
import { createSuiServer } from './server.js';
|
|
20
|
+
import { startWorkers } from './workers.js';
|
|
21
|
+
import { createCanary } from '@drsamonline/canary';
|
|
22
|
+
|
|
23
|
+
export { CircuitBreaker } from '@drsamonline/router';
|
|
24
|
+
export { ResponseCache } from '@drsamonline/telemetry';
|
|
25
|
+
export { Ledger } from '@drsamonline/telemetry';
|
|
26
|
+
export { Router } from '@drsamonline/router';
|
|
27
|
+
export { createSuiServer } from './server.js';
|
|
28
|
+
export { loadConfig, normalizeConfig, resolveCombo, checkToggle, sanitizeConfig, providerHasCredentials, estimatedCost, suiHome, defaultConfigPath } from '@drsamonline/core';
|
|
29
|
+
export { SuiError, ProviderError, ConfigError, ExhaustedError } from '@drsamonline/core';
|
|
30
|
+
export { createAdapter, displayName } from '@drsamonline/providers';
|
|
31
|
+
export { INVARIANTS, auditConfig, BUDGETS } from '@drsamonline/core';
|
|
32
|
+
|
|
33
|
+
/** Minimal .env reader — no dependency, no surprises, never overrides a real env var. */
|
|
34
|
+
export function loadEnvFile(path = '.env', env = process.env) {
|
|
35
|
+
if (!existsSync(path)) return env;
|
|
36
|
+
const text = readFileSync(path, 'utf8');
|
|
37
|
+
for (const line of text.split('\n')) {
|
|
38
|
+
const trimmed = line.trim();
|
|
39
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
40
|
+
const eq = trimmed.indexOf('=');
|
|
41
|
+
if (eq === -1) continue;
|
|
42
|
+
const key = trimmed.slice(0, eq).trim();
|
|
43
|
+
let value = trimmed.slice(eq + 1).trim();
|
|
44
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
45
|
+
value = value.slice(1, -1);
|
|
46
|
+
}
|
|
47
|
+
if (!(key in env) || !env[key]) env[key] = value;
|
|
48
|
+
}
|
|
49
|
+
return env;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Build a gateway: config, ledger, breaker, cache, router and an HTTP server,
|
|
54
|
+
* all wired together but not yet listening.
|
|
55
|
+
*
|
|
56
|
+
* @param {{configPath?: string, env?: NodeJS.ProcessEnv, configObject?: any, configOverride?: any, ledgerFile?: string|null, logger?: any, startWorkers?: boolean, catalogDir?: string, historyFile?: string|null, now?: () => number}} [opts]
|
|
57
|
+
*/
|
|
58
|
+
export async function createGateway(opts = {}) {
|
|
59
|
+
const env = opts.env ?? loadEnvFile(process.env.SUI_ENV_FILE ?? '.env', { ...process.env });
|
|
60
|
+
// A run-time override — a plane role, a port — changes this run only. It is
|
|
61
|
+
// applied after loading and never written back to the file.
|
|
62
|
+
const loaded = opts.configObject ? normalizeConfig(opts.configObject, { env }) : loadConfig(opts.configPath, env);
|
|
63
|
+
const config = opts.configOverride
|
|
64
|
+
? normalizeConfig(
|
|
65
|
+
{
|
|
66
|
+
...(opts.configObject ?? JSON.parse(JSON.stringify({ ...loaded, path: undefined, env: undefined }))),
|
|
67
|
+
...opts.configOverride,
|
|
68
|
+
planes: { ...(loaded.planes ?? {}), ...(opts.configOverride.planes ?? {}) },
|
|
69
|
+
server: { ...(loaded.server ?? {}), ...(opts.configOverride.server ?? {}) },
|
|
70
|
+
},
|
|
71
|
+
{ env },
|
|
72
|
+
)
|
|
73
|
+
: loaded;
|
|
74
|
+
|
|
75
|
+
const home = suiHome(env);
|
|
76
|
+
|
|
77
|
+
// B6.7, the second half of the air-gap: sealed storage for paid credentials.
|
|
78
|
+
// Opened only when the gate (I4) is on. A locked or missing vault is a warning,
|
|
79
|
+
// never a reason to refuse free-tier traffic.
|
|
80
|
+
if (config.paidTiers?.enabled) {
|
|
81
|
+
const vaultPath = config.paidTiers.vaultPath ?? join(home, 'vault.json');
|
|
82
|
+
const opened = await openVaultForStartup({ path: vaultPath, env });
|
|
83
|
+
config.vault = { path: vaultPath, reason: opened.reason, providers: Object.keys(opened.keys).sort() };
|
|
84
|
+
for (const [providerId, key] of Object.entries(opened.keys)) {
|
|
85
|
+
const provider = config.providers[providerId];
|
|
86
|
+
if (provider?.apiKeyEnv && !config.env[provider.apiKeyEnv]) config.env[provider.apiKeyEnv] = key;
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
config.vault = { path: null, reason: 'paid tier is closed (I4) — the vault was not opened', providers: [] };
|
|
90
|
+
}
|
|
91
|
+
const ledger = new Ledger({
|
|
92
|
+
file: opts.ledgerFile === null ? null : opts.ledgerFile ?? join(home, 'ledger.jsonl'),
|
|
93
|
+
salt: env.SUI_LEDGER_SALT ?? 'sui-local-salt',
|
|
94
|
+
});
|
|
95
|
+
const breaker = new CircuitBreaker(config.breaker);
|
|
96
|
+
const cache = new ResponseCache({ ...config.server.cache });
|
|
97
|
+
const logger = opts.logger ?? defaultLogger();
|
|
98
|
+
const canary = config.canary?.enabled
|
|
99
|
+
? createCanary({
|
|
100
|
+
id: config.canary.id ?? undefined,
|
|
101
|
+
baseline: config.canary.baseline,
|
|
102
|
+
candidate: config.canary.candidate,
|
|
103
|
+
percent: config.canary.percent,
|
|
104
|
+
slo: config.canary.slo,
|
|
105
|
+
})
|
|
106
|
+
: null;
|
|
107
|
+
|
|
108
|
+
const router = new Router({ config, ledger, breaker, cache, logger, canary });
|
|
109
|
+
// Background jobs are opt-in: creating a gateway programmatically must not
|
|
110
|
+
// start probing the internet. `sui start` turns them on.
|
|
111
|
+
const workers =
|
|
112
|
+
opts.startWorkers === true
|
|
113
|
+
? startWorkers({
|
|
114
|
+
config,
|
|
115
|
+
cache,
|
|
116
|
+
ledger,
|
|
117
|
+
logger,
|
|
118
|
+
router,
|
|
119
|
+
catalogDir: opts.catalogDir,
|
|
120
|
+
now: opts.now,
|
|
121
|
+
// B2.7: health history lives beside the ledger, and nowhere else
|
|
122
|
+
historyFile: opts.historyFile === null ? null : opts.historyFile ?? join(home, 'history.jsonl'),
|
|
123
|
+
})
|
|
124
|
+
: { jobs: [], prober: null, history: null, snapshot: () => ({ enabled: false, jobs: [], probes: [] }), stop() {} };
|
|
125
|
+
const server = createSuiServer({ router, config, ledger, breaker, cache, logger, workers, canary });
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
config,
|
|
129
|
+
ledger,
|
|
130
|
+
breaker,
|
|
131
|
+
cache,
|
|
132
|
+
router,
|
|
133
|
+
server,
|
|
134
|
+
workers,
|
|
135
|
+
canary,
|
|
136
|
+
env,
|
|
137
|
+
home: resolve(home),
|
|
138
|
+
/** Stop timers, then close the server. Used by the CLI and by tests. */
|
|
139
|
+
async close() {
|
|
140
|
+
workers.stop();
|
|
141
|
+
await new Promise((resolve) => {
|
|
142
|
+
server.closeAllConnections?.();
|
|
143
|
+
server.close(() => resolve());
|
|
144
|
+
});
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function defaultLogger() {
|
|
150
|
+
const quiet = process.env.SUI_QUIET === '1';
|
|
151
|
+
return {
|
|
152
|
+
debug: (...a) => !quiet && process.env.SUI_DEBUG === '1' && console.log('[sui]', ...a),
|
|
153
|
+
info: (...a) => !quiet && console.log('[sui]', ...a),
|
|
154
|
+
warn: (...a) => !quiet && console.warn('[sui]', ...a),
|
|
155
|
+
};
|
|
156
|
+
}
|
package/src/limits.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-key limits (B6.6).
|
|
3
|
+
*
|
|
4
|
+
* A key is a tenant. A tenant gets a rate, a spend cap and its own guardrails,
|
|
5
|
+
* and none of those may be bypassed by calling a different combo.
|
|
6
|
+
*
|
|
7
|
+
* The limiter is a token bucket in memory: cheap, honest about being per-process,
|
|
8
|
+
* and enough for a gateway that runs as one process. A shared limiter needs a
|
|
9
|
+
* shared store, which is a Phase 9 problem.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {{now?: () => number}} [opts]
|
|
14
|
+
*/
|
|
15
|
+
export class RateLimiter {
|
|
16
|
+
constructor(opts = {}) {
|
|
17
|
+
this.now = opts.now ?? Date.now;
|
|
18
|
+
/** @type {Map<string, {tokens: number, last: number, limited: number}>} */
|
|
19
|
+
this.buckets = new Map();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} keyId
|
|
24
|
+
* @param {{rpm?: number, burst?: number}} limit
|
|
25
|
+
* @returns {{ok: boolean, remaining: number, retryAfterMs: number, limit: number}}
|
|
26
|
+
*/
|
|
27
|
+
check(keyId, limit = {}) {
|
|
28
|
+
const rpm = Number(limit.rpm ?? 0);
|
|
29
|
+
if (!rpm || rpm <= 0) return { ok: true, remaining: Infinity, retryAfterMs: 0, limit: 0 };
|
|
30
|
+
|
|
31
|
+
const capacity = Math.max(1, Number(limit.burst ?? Math.ceil(rpm / 10)));
|
|
32
|
+
const refillPerMs = rpm / 60000;
|
|
33
|
+
const now = this.now();
|
|
34
|
+
|
|
35
|
+
let bucket = this.buckets.get(keyId);
|
|
36
|
+
if (!bucket) {
|
|
37
|
+
bucket = { tokens: capacity, last: now, limited: 0 };
|
|
38
|
+
this.buckets.set(keyId, bucket);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// refill, capped at the bucket size
|
|
42
|
+
const elapsed = Math.max(0, now - bucket.last);
|
|
43
|
+
bucket.tokens = Math.min(capacity, bucket.tokens + elapsed * refillPerMs);
|
|
44
|
+
bucket.last = now;
|
|
45
|
+
|
|
46
|
+
if (bucket.tokens < 1) {
|
|
47
|
+
bucket.limited += 1;
|
|
48
|
+
const retryAfterMs = Math.ceil((1 - bucket.tokens) / refillPerMs);
|
|
49
|
+
return { ok: false, remaining: 0, retryAfterMs, limit: rpm };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
bucket.tokens -= 1;
|
|
53
|
+
return { ok: true, remaining: Math.floor(bucket.tokens), retryAfterMs: 0, limit: rpm };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
snapshot() {
|
|
57
|
+
return [...this.buckets.entries()].map(([id, b]) => ({ key: id, tokens: Math.floor(b.tokens), limited: b.limited }));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* What has this key spent today?
|
|
63
|
+
* @param {{entries: (opts?: any) => any[]}} ledger
|
|
64
|
+
* @param {string} keyName
|
|
65
|
+
*/
|
|
66
|
+
export function keySpendToday(ledger, keyName) {
|
|
67
|
+
const start = new Date();
|
|
68
|
+
start.setHours(0, 0, 0, 0);
|
|
69
|
+
const rows = ledger.entries({ since: start.getTime() }).filter((r) => r.apiKey === keyName);
|
|
70
|
+
return {
|
|
71
|
+
spentUsd: Math.round(rows.reduce((a, r) => a + (r.costUsd ?? 0), 0) * 1e6) / 1e6,
|
|
72
|
+
turns: rows.filter((r) => r.ok).length,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {{keyMeta: any, ledger: any}} input
|
|
78
|
+
* @returns {{ok: boolean, spentUsd: number, capUsd: number|null, message: string|null}}
|
|
79
|
+
*/
|
|
80
|
+
export function checkKeyBudget(input) {
|
|
81
|
+
const cap = input.keyMeta?.dailyUsd;
|
|
82
|
+
if (cap == null) return { ok: true, spentUsd: 0, capUsd: null, message: null };
|
|
83
|
+
|
|
84
|
+
const { spentUsd } = keySpendToday(input.ledger, input.keyMeta.name ?? 'unnamed');
|
|
85
|
+
if (spentUsd >= Number(cap)) {
|
|
86
|
+
return {
|
|
87
|
+
ok: false,
|
|
88
|
+
spentUsd,
|
|
89
|
+
capUsd: Number(cap),
|
|
90
|
+
message: `This API key has spent $${spentUsd.toFixed(4)} of its $${Number(cap).toFixed(2)} daily cap. It resets at local midnight, or raise dailyUsd on the key.`,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return { ok: true, spentUsd, capUsd: Number(cap), message: null };
|
|
94
|
+
}
|
package/src/planes.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @drsamonline/gateway/planes — B5.7, splitting the gateway in two.
|
|
3
|
+
*
|
|
4
|
+
* One process doing everything is the wrong shape for the two jobs it does:
|
|
5
|
+
*
|
|
6
|
+
* **Model plane** holds provider credentials, talks to providers, and is the
|
|
7
|
+
* only thing that needs egress. Loopback, no public surface.
|
|
8
|
+
* **Agent plane** the surface agents talk to: MCP, run bounds, approvals,
|
|
9
|
+
* attribution. Holds no provider credentials at all.
|
|
10
|
+
*
|
|
11
|
+
* The point of the split is the credential boundary. If an agent-facing surface
|
|
12
|
+
* is ever exposed, or an MCP client is compromised, it cannot reach a provider
|
|
13
|
+
* key — because it does not have one, and cannot get one. It can only ask the
|
|
14
|
+
* model plane, which knows who it is talking to.
|
|
15
|
+
*
|
|
16
|
+
* The link between them is a service token, checked in constant time, and the
|
|
17
|
+
* model plane refuses anything else — including its own admin surface.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
21
|
+
|
|
22
|
+
// not exported: the config schema is the authority on valid roles, and a second
|
|
23
|
+
// list would be a second thing to keep in step
|
|
24
|
+
const PLANE_ROLES = ['both', 'model', 'agent'];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Compare two secrets without leaking their length or content through timing.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} presented
|
|
30
|
+
* @param {string} expected
|
|
31
|
+
*/
|
|
32
|
+
export function tokenMatches(presented, expected) {
|
|
33
|
+
if (typeof presented !== 'string' || typeof expected !== 'string' || !expected) return false;
|
|
34
|
+
const a = Buffer.from(presented);
|
|
35
|
+
const b = Buffer.from(expected);
|
|
36
|
+
if (a.length !== b.length) {
|
|
37
|
+
// still do the comparison, so the timing does not depend on the length
|
|
38
|
+
timingSafeEqual(b, b);
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return timingSafeEqual(a, b);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* What a plane of this role is allowed to serve.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} role
|
|
48
|
+
*/
|
|
49
|
+
export function planePolicy(role) {
|
|
50
|
+
// An unknown role is a configuration mistake, and defaulting it to `both`
|
|
51
|
+
// would silently give a misconfigured agent plane the whole surface.
|
|
52
|
+
if (!PLANE_ROLES.includes(role)) {
|
|
53
|
+
throw new Error(`Unknown plane role "${role}". Use one of: ${PLANE_ROLES.join(', ')}.`);
|
|
54
|
+
}
|
|
55
|
+
const isModel = role === 'model';
|
|
56
|
+
const isAgent = role === 'agent';
|
|
57
|
+
return {
|
|
58
|
+
role,
|
|
59
|
+
// the model plane serves the data plane; the agent plane serves the control
|
|
60
|
+
// surface; `both` serves everything, as it did before the split existed
|
|
61
|
+
serves: {
|
|
62
|
+
// the agent plane serves chat too — it forwards it. What it does not have
|
|
63
|
+
// is a provider, which is the point of the split.
|
|
64
|
+
chat: true,
|
|
65
|
+
models: !isAgent,
|
|
66
|
+
status: true,
|
|
67
|
+
metrics: !isAgent,
|
|
68
|
+
runs: !isModel,
|
|
69
|
+
mcp: !isModel,
|
|
70
|
+
tenants: !isModel,
|
|
71
|
+
},
|
|
72
|
+
// only the model plane may hold provider credentials
|
|
73
|
+
holdsProviderCredentials: !isAgent,
|
|
74
|
+
// only the agent plane is meant to be reachable by agents
|
|
75
|
+
agentFacing: !isModel,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The agent plane's forwarder: it takes a chat request, applies the run bounds
|
|
81
|
+
* it already knows about, and hands the request to the model plane with the
|
|
82
|
+
* service token attached.
|
|
83
|
+
*
|
|
84
|
+
* @param {{modelPlaneUrl: string, serviceToken: string, fetchImpl?: typeof fetch, timeoutMs?: number, logger?: any}} opts
|
|
85
|
+
*/
|
|
86
|
+
export function createModelPlaneClient(opts) {
|
|
87
|
+
if (!opts?.modelPlaneUrl) throw new Error('The agent plane needs modelPlaneUrl — where is the model plane?');
|
|
88
|
+
if (!opts?.serviceToken) throw new Error('The agent plane needs a service token to reach the model plane.');
|
|
89
|
+
|
|
90
|
+
const base = String(opts.modelPlaneUrl).replace(/\/+$/, '');
|
|
91
|
+
const doFetch = opts.fetchImpl ?? fetch;
|
|
92
|
+
const timeoutMs = Number(opts.timeoutMs ?? 120_000);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {{path: string, method?: string, body?: any, headers?: Record<string, string>}} req
|
|
96
|
+
*/
|
|
97
|
+
async function forward(req) {
|
|
98
|
+
const controller = new AbortController();
|
|
99
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
100
|
+
try {
|
|
101
|
+
const res = await doFetch(`${base}${req.path}`, {
|
|
102
|
+
method: req.method ?? 'GET',
|
|
103
|
+
headers: {
|
|
104
|
+
'content-type': 'application/json',
|
|
105
|
+
// the token is the whole point: the model plane serves nobody else
|
|
106
|
+
'x-sui-plane-token': opts.serviceToken,
|
|
107
|
+
...(req.headers ?? {}),
|
|
108
|
+
},
|
|
109
|
+
body: req.body === undefined ? undefined : JSON.stringify(req.body),
|
|
110
|
+
signal: controller.signal,
|
|
111
|
+
});
|
|
112
|
+
const text = await res.text();
|
|
113
|
+
let json = null;
|
|
114
|
+
try {
|
|
115
|
+
json = text ? JSON.parse(text) : null;
|
|
116
|
+
} catch {
|
|
117
|
+
json = null;
|
|
118
|
+
}
|
|
119
|
+
return { status: res.status, json, text };
|
|
120
|
+
} catch (err) {
|
|
121
|
+
const message = /** @type {any} */ (err)?.name === 'AbortError'
|
|
122
|
+
? `The model plane did not answer within ${Math.round(timeoutMs / 1000)}s.`
|
|
123
|
+
: `Could not reach the model plane at ${base}: ${/** @type {any} */ (err)?.message ?? err}`;
|
|
124
|
+
opts.logger?.warn?.(message);
|
|
125
|
+
return { status: 502, json: { error: { message, type: 'model_plane_unreachable' } }, text: '' };
|
|
126
|
+
} finally {
|
|
127
|
+
clearTimeout(timer);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return { forward, base };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The model plane's guard: everything except health must carry the token, and
|
|
136
|
+
* the admin surface is not served at all.
|
|
137
|
+
*
|
|
138
|
+
* @param {{serviceToken: string, role: string}} opts
|
|
139
|
+
*/
|
|
140
|
+
export function createPlaneGuard(opts) {
|
|
141
|
+
const policy = planePolicy(opts.role);
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {{headers: any}} req
|
|
145
|
+
* @param {string} path
|
|
146
|
+
* @returns {{ok: boolean, status?: number, code?: string, message?: string}}
|
|
147
|
+
*/
|
|
148
|
+
function check(req, path) {
|
|
149
|
+
if (path === '/health' || path === '/api/status') return { ok: true };
|
|
150
|
+
|
|
151
|
+
if (opts.role === 'model') {
|
|
152
|
+
if (path.startsWith('/api/runs') || path === '/mcp') {
|
|
153
|
+
return { ok: false, status: 404, code: 'not_this_plane', message: 'This is the model plane: runs and MCP belong to the agent plane.' };
|
|
154
|
+
}
|
|
155
|
+
const presented = req.headers?.['x-sui-plane-token'] ?? String(req.headers?.authorization ?? '').replace(/^Bearer\s+/i, '');
|
|
156
|
+
if (!tokenMatches(String(presented ?? ''), opts.serviceToken)) {
|
|
157
|
+
return { ok: false, status: 403, code: 'plane_token_required', message: 'The model plane serves only the agent plane. Send x-sui-plane-token.' };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return { ok: true };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return { check, policy };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A description of the topology, for `sui planes` and `/api/status`.
|
|
168
|
+
*
|
|
169
|
+
* @param {{role: string, modelPlaneUrl?: string|null, hasServiceToken?: boolean, hasProviderCredentials?: boolean}} input
|
|
170
|
+
*/
|
|
171
|
+
export function describePlane(input) {
|
|
172
|
+
const policy = planePolicy(input.role);
|
|
173
|
+
return {
|
|
174
|
+
role: input.role,
|
|
175
|
+
agentFacing: policy.agentFacing,
|
|
176
|
+
holdsProviderCredentials: input.hasProviderCredentials ?? policy.holdsProviderCredentials,
|
|
177
|
+
modelPlaneUrl: input.modelPlaneUrl ?? null,
|
|
178
|
+
serviceTokenConfigured: Boolean(input.hasServiceToken),
|
|
179
|
+
serves: policy.serves,
|
|
180
|
+
};
|
|
181
|
+
}
|