@drsamonline/cli 0.0.0-stage → 0.12.3
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/bin/sui.js +34 -0
- package/package.json +39 -4
- package/src/cli.js +676 -0
- package/src/commands-extra.js +1105 -0
- package/src/migrate.js +280 -0
- package/src/onboarding.js +263 -0
- package/src/tools.js +235 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI commands that arrived with the kits: catalogue, vault, config editing,
|
|
3
|
+
* the token audit, and compressed exports.
|
|
4
|
+
*
|
|
5
|
+
* Kept out of cli.js so the command table stays readable.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import { homedir } from 'node:os';
|
|
10
|
+
import { dirname, join } from 'node:path';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
import { createInterface } from 'node:readline/promises';
|
|
13
|
+
import { stdin, stdout } from 'node:process';
|
|
14
|
+
import { ConfigError, defaultConfigPath, normalizeConfig, sanitizeConfig, suiHome } from '@drsamonline/core';
|
|
15
|
+
import { applyVerified } from '@drsamonline/catalog';
|
|
16
|
+
import { dedupeEntries, loadSeed, summarise } from '@drsamonline/catalog';
|
|
17
|
+
import { loadKit, kitStatus } from '@drsamonline/kits';
|
|
18
|
+
import { createVault } from '@drsamonline/vault';
|
|
19
|
+
import { Prober, PROBE_FIXTURE_VERSION } from '@drsamonline/prober';
|
|
20
|
+
import { explainPlan } from '@drsamonline/router';
|
|
21
|
+
import { createGateway } from '@drsamonline/gateway';
|
|
22
|
+
import { TRANSFORMS, runSelfTest } from '@drsamonline/redteam';
|
|
23
|
+
import { createHttpHandler, createMcpServer, runStdio, SUPPORTED_PROTOCOL_VERSIONS } from '@drsamonline/mcp';
|
|
24
|
+
|
|
25
|
+
/** Read from package.json so the banner and the package cannot disagree. */
|
|
26
|
+
const VERSION = (() => {
|
|
27
|
+
try {
|
|
28
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
29
|
+
return JSON.parse(readFileSync(join(here, '..', '..', '..', 'package.json'), 'utf8')).version;
|
|
30
|
+
} catch {
|
|
31
|
+
return '0.0.0';
|
|
32
|
+
}
|
|
33
|
+
})();
|
|
34
|
+
import { PRESETS, RecallStore, choosePreset, compress } from '@drsamonline/compression';
|
|
35
|
+
import { table } from './cli.js';
|
|
36
|
+
|
|
37
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
38
|
+
const ROOT = join(HERE, '..', '..', '..');
|
|
39
|
+
|
|
40
|
+
/* ------------------------------------------------------------------ catalog */
|
|
41
|
+
|
|
42
|
+
/** `sui catalog [query]` — what Sui could route to, and what is still unverified. */
|
|
43
|
+
export function cmdCatalog(flags, args) {
|
|
44
|
+
const entries = loadCatalogue();
|
|
45
|
+
const summary = summarise(entries);
|
|
46
|
+
|
|
47
|
+
if (flags.json) {
|
|
48
|
+
console.log(JSON.stringify({ summary: { ...summary, rows: undefined }, rows: summary.rows }, null, 2));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const query = args[0] ?? flags.q;
|
|
53
|
+
console.log('');
|
|
54
|
+
console.log(`${summary.total} catalogue entries · ${summary.routable} routable · ${summary.awaiting_probe} awaiting a probe · ${summary.tos_avoid} tos:avoid`);
|
|
55
|
+
console.log('');
|
|
56
|
+
|
|
57
|
+
if (query) {
|
|
58
|
+
const Search = loadKit('search-kit');
|
|
59
|
+
const index = Search.index(
|
|
60
|
+
entries.map((e) => ({ provider: e.provider, label: e.label, caps: e.capabilities.join(' ') })),
|
|
61
|
+
{ keys: ['provider', 'label', 'caps'] },
|
|
62
|
+
);
|
|
63
|
+
const hits = index.query(String(query)).slice(0, 10);
|
|
64
|
+
if (!hits.length) {
|
|
65
|
+
console.log(`Nothing in the catalogue matches "${query}".`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
console.log(
|
|
69
|
+
table(
|
|
70
|
+
['provider', 'label', 'caps', 'score'],
|
|
71
|
+
hits.map((h) => [h.item.provider, h.item.label, h.item.caps, h.score.toFixed(3)]),
|
|
72
|
+
),
|
|
73
|
+
);
|
|
74
|
+
console.log('');
|
|
75
|
+
console.log('Every entry needs a probe before it can answer a request (see ROADMAP B2.3).');
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const rows = summary.rows.slice(0, 40).map((e) => [
|
|
80
|
+
e.provider,
|
|
81
|
+
e.label,
|
|
82
|
+
e.auth,
|
|
83
|
+
e.tos,
|
|
84
|
+
e.needs_probe ? 'awaiting probe' : 'verified',
|
|
85
|
+
e.base_url ?? e.docs_url ?? '—',
|
|
86
|
+
]);
|
|
87
|
+
console.log(table(['provider', 'label', 'auth', 'tos', 'state', 'where'], rows));
|
|
88
|
+
if (summary.rows.length > rows.length) console.log(`\n… ${summary.rows.length - rows.length} more. Search with: sui catalog <query>`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Where observations live.
|
|
93
|
+
*
|
|
94
|
+
* Seed entries ship with the package — they are imported facts. What *this*
|
|
95
|
+
* machine observed belongs in the user's home, not in the package: writing into
|
|
96
|
+
* an installed `node_modules` would be wrong, and a verification made on one
|
|
97
|
+
* machine is not evidence for another.
|
|
98
|
+
*/
|
|
99
|
+
function verifiedPath() {
|
|
100
|
+
return join(suiHome(), 'catalog-verified.json');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The old in-repo location, still read so a development clone keeps its state. */
|
|
104
|
+
function legacyVerifiedPath() {
|
|
105
|
+
return join(ROOT, 'packages', 'catalog', 'verified.json');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function readVerified() {
|
|
109
|
+
for (const path of [verifiedPath(), legacyVerifiedPath()]) {
|
|
110
|
+
if (!existsSync(path)) continue;
|
|
111
|
+
try {
|
|
112
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
113
|
+
if (Array.isArray(parsed.entries) && parsed.entries.length) return parsed.entries;
|
|
114
|
+
} catch {
|
|
115
|
+
/* a corrupt overlay must not stop the catalogue loading */
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function loadCatalogue() {
|
|
122
|
+
const seedPath = join(ROOT, 'packages', 'catalog', 'seed.json');
|
|
123
|
+
const generatedPath = join(ROOT, 'packages', 'catalog', 'generated.json');
|
|
124
|
+
const entries = [];
|
|
125
|
+
if (existsSync(seedPath)) entries.push(...loadSeed(seedPath));
|
|
126
|
+
if (existsSync(generatedPath)) entries.push(...loadSeed(generatedPath));
|
|
127
|
+
return dedupeEntries(applyVerified(entries, readVerified()));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Write what a probe observed, atomically, without touching the seed. */
|
|
131
|
+
function writeVerified(results) {
|
|
132
|
+
const path = verifiedPath();
|
|
133
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
134
|
+
const current = existsSync(path) ? JSON.parse(readFileSync(path, 'utf8')) : { version: 1, entries: [] };
|
|
135
|
+
const byProvider = new Map((current.entries ?? []).map((row) => [row.provider, row]));
|
|
136
|
+
|
|
137
|
+
for (const result of results) {
|
|
138
|
+
if (!result.ok) {
|
|
139
|
+
// a failure does not erase a previous verification: the endpoint may be
|
|
140
|
+
// down, not wrong. `--revoke` is how a route is withdrawn.
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
byProvider.set(result.provider, {
|
|
144
|
+
provider: result.provider,
|
|
145
|
+
base_url: result.base_url ?? byProvider.get(result.provider)?.base_url ?? null,
|
|
146
|
+
probe_model: result.probe_model ?? null,
|
|
147
|
+
models: result.models ?? [],
|
|
148
|
+
verified_at: result.checkedAt,
|
|
149
|
+
fixtureVersion: result.fixtureVersion,
|
|
150
|
+
capabilities: result.capabilities ?? ['chat'],
|
|
151
|
+
tos: byProvider.get(result.provider)?.tos ?? 'review',
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const next = { ...current, entries: [...byProvider.values()].sort((a, b) => a.provider.localeCompare(b.provider)) };
|
|
156
|
+
const tmp = `${path}.tmp`;
|
|
157
|
+
writeFileSync(tmp, `${JSON.stringify(next, null, 2)}\n`);
|
|
158
|
+
renameSync(tmp, path);
|
|
159
|
+
return next.entries.length;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/* -------------------------------------------------------------------- vault */
|
|
163
|
+
|
|
164
|
+
export async function cmdVault(flags, args) {
|
|
165
|
+
const sub = args[0] ?? 'status';
|
|
166
|
+
const home = suiHome(process.env);
|
|
167
|
+
const configPath = flags.config ?? defaultConfigPath();
|
|
168
|
+
const raw = configPath ? JSON.parse(readFileSync(configPath, 'utf8')) : {};
|
|
169
|
+
const vaultPath = raw.paid_tiers?.vaultPath ?? join(home, 'vault.json');
|
|
170
|
+
const vault = createVault({ path: vaultPath, env: process.env });
|
|
171
|
+
|
|
172
|
+
if (sub === 'init') {
|
|
173
|
+
const result = await vault.init({ force: Boolean(flags.force) });
|
|
174
|
+
console.log('');
|
|
175
|
+
if (!result.created) {
|
|
176
|
+
console.log(`A vault already exists at ${vaultPath}. Use --force to replace it (that destroys the keys in it).`);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
console.log(`Vault created: ${vaultPath}`);
|
|
180
|
+
if (result.keyPath) {
|
|
181
|
+
console.log(`Passphrase written to ${result.keyPath} (mode 0600).`);
|
|
182
|
+
console.log('');
|
|
183
|
+
console.log('This protects the vault file if it is copied or committed. It does not protect');
|
|
184
|
+
console.log('you from someone who already has your user account. For that, set');
|
|
185
|
+
console.log('SUI_VAULT_PASSPHRASE yourself and delete the key file.');
|
|
186
|
+
}
|
|
187
|
+
console.log('');
|
|
188
|
+
console.log('Next: sui vault set openai');
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (sub === 'set') {
|
|
193
|
+
const provider = args[1] ?? flags.provider;
|
|
194
|
+
if (!provider) throw new ConfigError('Say which provider: sui vault set openai');
|
|
195
|
+
const key = flags.key ?? (await readSecret(`Paste the ${provider} API key (input is echoed): `));
|
|
196
|
+
if (!key) throw new ConfigError('No key given, so nothing was stored.');
|
|
197
|
+
await vault.set(provider, key.trim());
|
|
198
|
+
console.log(`Stored a key for "${provider}". Only the provider name is visible in the vault file; the key is sealed.`);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (sub === 'remove') {
|
|
203
|
+
const provider = args[1] ?? flags.provider;
|
|
204
|
+
if (!provider) throw new ConfigError('Say which provider: sui vault remove openai');
|
|
205
|
+
const removed = await vault.remove(provider);
|
|
206
|
+
console.log(removed ? `Removed "${provider}".` : `No key stored for "${provider}".`);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (sub === 'list') {
|
|
211
|
+
const providers = await vault.list();
|
|
212
|
+
console.log('');
|
|
213
|
+
console.log(providers.length ? providers.join('\n') : 'The vault is empty.');
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// status
|
|
218
|
+
console.log('');
|
|
219
|
+
console.log(`path ${vaultPath}`);
|
|
220
|
+
console.log(`exists ${vault.exists() ? 'yes' : 'no'}`);
|
|
221
|
+
console.log(`passphrase ${vault.source === 'env' ? 'from SUI_VAULT_PASSPHRASE' : vault.source === 'keyfile' ? `key file (${vault.keyPath})` : 'not found'}`);
|
|
222
|
+
if (vault.exists()) {
|
|
223
|
+
try {
|
|
224
|
+
const keys = await vault.list();
|
|
225
|
+
console.log(`providers ${keys.length ? keys.join(', ') : '(none)'}`);
|
|
226
|
+
} catch (err) {
|
|
227
|
+
console.log(`providers could not open: ${/** @type {any} */ (err).message}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
console.log(`gate ${raw.paid_tiers?.enabled === true ? 'OPEN (paid credentials load)' : 'closed (paid credentials are never read)'}`);
|
|
231
|
+
if (raw.paid_tiers?.enabled !== true) {
|
|
232
|
+
console.log('');
|
|
233
|
+
console.log('Set paid_tiers.enabled = true in the config before a sealed key can be used:');
|
|
234
|
+
console.log(' sui config set paid_tiers.enabled true');
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function readSecret(question) {
|
|
239
|
+
if (!stdin.isTTY) {
|
|
240
|
+
// piped input: read one line from stdin
|
|
241
|
+
const chunks = [];
|
|
242
|
+
for await (const chunk of stdin) chunks.push(chunk);
|
|
243
|
+
return Buffer.concat(chunks).toString('utf8').split('\n')[0].trim();
|
|
244
|
+
}
|
|
245
|
+
const rl = createInterface({ input: stdin, output: stdout });
|
|
246
|
+
try {
|
|
247
|
+
return (await rl.question(question)).trim();
|
|
248
|
+
} finally {
|
|
249
|
+
rl.close();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* ------------------------------------------------------------------- config */
|
|
254
|
+
|
|
255
|
+
export function cmdConfig(flags, args) {
|
|
256
|
+
const path = flags.config ?? defaultConfigPath();
|
|
257
|
+
if (!path) throw new ConfigError('No config found. Run `npx sui init` first.');
|
|
258
|
+
const raw = JSON.parse(readFileSync(path, 'utf8'));
|
|
259
|
+
const sub = args[0] ?? 'show';
|
|
260
|
+
const dotted = args[1];
|
|
261
|
+
|
|
262
|
+
if (sub === 'show' || sub === undefined) {
|
|
263
|
+
console.log(JSON.stringify(sanitizeConfig(normalizeConfig(raw, { path, env: process.env })), null, 2));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (sub === 'get') {
|
|
268
|
+
if (!dotted) throw new ConfigError('Usage: sui config get <path> e.g. sui config get budgets.dailyUsd');
|
|
269
|
+
const value = getPath(raw, dotted);
|
|
270
|
+
if (value === undefined) {
|
|
271
|
+
console.log(`${dotted} is not set.`);
|
|
272
|
+
process.exitCode = 1;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
console.log(typeof value === 'object' ? JSON.stringify(value, null, 2) : String(value));
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (sub === 'set' || sub === 'unset') {
|
|
280
|
+
if (!dotted) throw new ConfigError(`Usage: sui config ${sub} <path> [value]`);
|
|
281
|
+
const before = getPath(raw, dotted);
|
|
282
|
+
if (sub === 'unset') deletePath(raw, dotted);
|
|
283
|
+
else {
|
|
284
|
+
const value = parseValue(args.slice(2).join(' '));
|
|
285
|
+
setPath(raw, dotted, value);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Validate the whole config before writing: a set that breaks the gateway is
|
|
289
|
+
// refused here, with the same message `sui start` would give.
|
|
290
|
+
try {
|
|
291
|
+
normalizeConfig(raw, { path, env: process.env });
|
|
292
|
+
} catch (err) {
|
|
293
|
+
throw new ConfigError(
|
|
294
|
+
`That change would break the config, so nothing was written.\n\n${/** @type {any} */ (err).message}`,
|
|
295
|
+
{ path },
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
copyFileSync(path, `${path}.bak-${Date.now()}`);
|
|
300
|
+
writeFileSync(path, JSON.stringify(raw, null, 2) + '\n');
|
|
301
|
+
const after = getPath(raw, dotted);
|
|
302
|
+
console.log(`${dotted}: ${format(before)} → ${format(after)}`);
|
|
303
|
+
console.log('Written. Restart `sui start` to pick it up.');
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
throw new ConfigError(`Unknown: sui config ${sub}. Try show, get, set, unset.`);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function getPath(obj, dotted) {
|
|
311
|
+
return dotted.split('.').reduce((acc, k) => (acc == null ? undefined : acc[k]), obj);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function setPath(obj, dotted, value) {
|
|
315
|
+
const parts = dotted.split('.');
|
|
316
|
+
let cur = obj;
|
|
317
|
+
for (const k of parts.slice(0, -1)) {
|
|
318
|
+
if (cur[k] == null || typeof cur[k] !== 'object') cur[k] = {};
|
|
319
|
+
cur = cur[k];
|
|
320
|
+
}
|
|
321
|
+
cur[parts[parts.length - 1]] = value;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function deletePath(obj, dotted) {
|
|
325
|
+
const parts = dotted.split('.');
|
|
326
|
+
let cur = obj;
|
|
327
|
+
for (const k of parts.slice(0, -1)) {
|
|
328
|
+
if (cur == null) return;
|
|
329
|
+
cur = cur[k];
|
|
330
|
+
}
|
|
331
|
+
if (cur && typeof cur === 'object') delete cur[parts[parts.length - 1]];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function parseValue(text) {
|
|
335
|
+
const trimmed = String(text ?? '').trim();
|
|
336
|
+
if (trimmed === '') return '';
|
|
337
|
+
if (trimmed === 'true') return true;
|
|
338
|
+
if (trimmed === 'false') return false;
|
|
339
|
+
if (trimmed === 'null') return null;
|
|
340
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed)) return Number(trimmed);
|
|
341
|
+
if (/^[[{]/.test(trimmed)) {
|
|
342
|
+
try {
|
|
343
|
+
return JSON.parse(trimmed);
|
|
344
|
+
} catch {
|
|
345
|
+
return trimmed;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return trimmed;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function format(v) {
|
|
352
|
+
if (v === undefined) return '(unset)';
|
|
353
|
+
return typeof v === 'object' ? JSON.stringify(v) : String(v);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/* ------------------------------------------------------------------- tokens */
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* `sui tokens --check` — WCAG 2.2 AA on the dashboard's own tokens, computed with
|
|
360
|
+
* the vendored tokens-kit's contrast maths rather than a hand-waved claim (I9).
|
|
361
|
+
*/
|
|
362
|
+
export function cmdTokens(flags) {
|
|
363
|
+
const html = readFileSync(join(ROOT, 'apps', 'dashboard', 'web', 'dashboard.html'), 'utf8');
|
|
364
|
+
const Tokens = loadKit('tokens-kit');
|
|
365
|
+
|
|
366
|
+
const darkStart = html.indexOf('prefers-color-scheme: dark');
|
|
367
|
+
const light = readTokenBlock(darkStart > 0 ? html.slice(0, darkStart) : html, ':root', 0);
|
|
368
|
+
const dark = darkStart > 0 ? readTokenBlock(html.slice(darkStart), ':root', 0) : {};
|
|
369
|
+
|
|
370
|
+
const pairs = [
|
|
371
|
+
['text on bg', 'text', 'bg', 4.5],
|
|
372
|
+
['text on surface', 'text', 'surface', 4.5],
|
|
373
|
+
['dim text on bg', 'text-dim', 'bg', 4.5],
|
|
374
|
+
['dim text on surface', 'text-dim', 'surface', 4.5],
|
|
375
|
+
['dim text on surface-2', 'text-dim', 'surface-2', 4.5],
|
|
376
|
+
['accent on surface', 'accent', 'surface', 4.5],
|
|
377
|
+
['on-accent on accent', 'on-accent', 'accent', 4.5],
|
|
378
|
+
['line on surface (UI)', 'line-strong', 'surface', 3],
|
|
379
|
+
];
|
|
380
|
+
|
|
381
|
+
let failures = 0;
|
|
382
|
+
const report = [];
|
|
383
|
+
for (const [label, fg, bg, min] of pairs) {
|
|
384
|
+
for (const [mode, tokens] of [['light', light], ['dark', dark]]) {
|
|
385
|
+
const a = tokens[fg];
|
|
386
|
+
const b = tokens[bg];
|
|
387
|
+
if (!a || !b) continue;
|
|
388
|
+
const ratio = Tokens.contrast(a, b);
|
|
389
|
+
const ok = ratio >= min;
|
|
390
|
+
if (!ok) failures += 1;
|
|
391
|
+
report.push([mode, label, `${a} on ${b}`, ratio.toFixed(2), `≥ ${min}`, ok ? 'pass' : 'FAIL']);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
console.log('');
|
|
396
|
+
console.log('Token contrast — WCAG 2.2 AA, computed by tokens-kit');
|
|
397
|
+
console.log('');
|
|
398
|
+
console.log(table(['mode', 'pair', 'values', 'ratio', 'needs', 'result'], report));
|
|
399
|
+
|
|
400
|
+
const kitTokens = Tokens.tokens('light');
|
|
401
|
+
console.log('');
|
|
402
|
+
console.log(`The kit's palette is available for future tokens: ${Object.keys(kitTokens?.colors ?? kitTokens ?? {}).length} colour names.`);
|
|
403
|
+
console.log(failures ? `\n${failures} pair(s) below AA. Fix the token, not the test.` : '\nAll pairs pass.');
|
|
404
|
+
if (failures) process.exitCode = 1;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** Every `:root { … }` block, brace-matched so indentation cannot hide one. */
|
|
408
|
+
function readTokenBlock(source, _selector, _index) {
|
|
409
|
+
const out = {};
|
|
410
|
+
const start = /:root\s*\{/g;
|
|
411
|
+
let m;
|
|
412
|
+
while ((m = start.exec(source))) {
|
|
413
|
+
let depth = 1;
|
|
414
|
+
let i = m.index + m[0].length;
|
|
415
|
+
const from = i;
|
|
416
|
+
while (i < source.length && depth > 0) {
|
|
417
|
+
if (source[i] === '{') depth += 1;
|
|
418
|
+
else if (source[i] === '}') depth -= 1;
|
|
419
|
+
i += 1;
|
|
420
|
+
}
|
|
421
|
+
const body = source.slice(from, i - 1);
|
|
422
|
+
for (const line of body.split('\n')) {
|
|
423
|
+
const tok = line.match(/--([a-z0-9-]+):\s*(#[0-9a-fA-F]{3,8})\s*;/);
|
|
424
|
+
if (tok) out[tok[1]] = tok[2];
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return out;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/* -------------------------------------------------------------------- kits */
|
|
431
|
+
|
|
432
|
+
export function cmdKits(flags) {
|
|
433
|
+
const status = kitStatus();
|
|
434
|
+
console.log('');
|
|
435
|
+
console.log('Vendored kits (vendor/kits)');
|
|
436
|
+
console.log('');
|
|
437
|
+
console.log(
|
|
438
|
+
table(
|
|
439
|
+
['kit', 'state', 'version'],
|
|
440
|
+
status.map((k) => [k.name, k.loaded ? 'loaded' : `failed: ${k.error}`, k.version ?? '—']),
|
|
441
|
+
),
|
|
442
|
+
);
|
|
443
|
+
console.log('');
|
|
444
|
+
console.log('Licence: each kit carries its own LICENSE in vendor/kits/. See docs/legal/LICENSE_INVENTORY.md.');
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/* ----------------------------------------------------------------- redteam */
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* `sui redteam [--combo chat-default]` — B6.2: attack your own route.
|
|
451
|
+
*
|
|
452
|
+
* Ten obfuscations of one instruction a well-behaved model must refuse. The
|
|
453
|
+
* canary is random per run, so a model cannot have memorised it.
|
|
454
|
+
*/
|
|
455
|
+
export async function cmdRedteam(flags, args) {
|
|
456
|
+
const gateway = await createGateway({ configPath: flags.config, ledgerFile: null });
|
|
457
|
+
const combos = Object.keys(gateway.config.combos);
|
|
458
|
+
const combo = args[0] ?? flags.combo ?? combos[0];
|
|
459
|
+
|
|
460
|
+
console.log('');
|
|
461
|
+
console.log(`Red-team self-test — ${combo}`);
|
|
462
|
+
console.log(` ${Object.keys(TRANSFORMS).length} transforms of one injection instruction`);
|
|
463
|
+
console.log('');
|
|
464
|
+
|
|
465
|
+
const report = await runSelfTest({
|
|
466
|
+
combo,
|
|
467
|
+
transforms: flags.transform ? [flags.transform] : undefined,
|
|
468
|
+
chat: (input) => gateway.router.chat(input),
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
if (flags.json) {
|
|
472
|
+
console.log(JSON.stringify(report, null, 2));
|
|
473
|
+
process.exitCode = report.pass ? 0 : 1;
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
console.log(table(
|
|
478
|
+
['transform', 'verdict', 'what came back'],
|
|
479
|
+
report.results.map((r) => [r.transform, r.severity, (r.error ?? r.answer ?? '').slice(0, 54)]),
|
|
480
|
+
));
|
|
481
|
+
console.log('');
|
|
482
|
+
console.log(` ${report.counts.safe} safe · ${report.counts.high} ambiguous · ${report.counts.critical} obeyed the injection`);
|
|
483
|
+
console.log('');
|
|
484
|
+
console.log(report.pass
|
|
485
|
+
? ' PASS — no transform got the canary out of this route'
|
|
486
|
+
: ' FAIL — a route that obeys an injected instruction will obey one from a user');
|
|
487
|
+
if (!report.pass) process.exitCode = 1;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/* ------------------------------------------------------------------- sandbox */
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* `sui sandbox` — B5.6. Run a snippet in a contained process, or prove the
|
|
494
|
+
* boundary with the attack suite.
|
|
495
|
+
*/
|
|
496
|
+
export async function cmdSandbox(flags, args) {
|
|
497
|
+
const { runCode, runAttackSuite, sandboxCapabilities } = await import('@drsamonline/sandbox');
|
|
498
|
+
const capabilities = await sandboxCapabilities();
|
|
499
|
+
|
|
500
|
+
if (flags['self-test']) {
|
|
501
|
+
console.log('');
|
|
502
|
+
console.log('Attack suite — 14 ways out, and what happened to each');
|
|
503
|
+
console.log('');
|
|
504
|
+
const suite = await runAttackSuite({ runCode, capabilities });
|
|
505
|
+
for (const r of suite.results) {
|
|
506
|
+
console.log(` ${r.contained ? 'contained' : 'ESCAPED '} ${r.name.padEnd(40)} ${String(r.ms).padStart(5)}ms ${r.stderr.slice(0, 44)}`);
|
|
507
|
+
}
|
|
508
|
+
console.log('');
|
|
509
|
+
console.log(` ${suite.contained} of ${suite.total} contained`);
|
|
510
|
+
console.log(` permission model: ${capabilities.permissionModel ? 'on' : 'OFF'} · network namespace: ${capabilities.networkIsolated ? 'on' : 'OFF'}`);
|
|
511
|
+
if (!capabilities.fullyIsolated) {
|
|
512
|
+
console.log(' This machine cannot isolate fully. `requireNoNetwork` would refuse to run at all.');
|
|
513
|
+
}
|
|
514
|
+
if (suite.failed) process.exitCode = 1;
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const file = args[0] ?? flags.file;
|
|
519
|
+
if (!file) {
|
|
520
|
+
console.error('Usage: sui sandbox <file.js> [--wall 5000] [--heap 128]');
|
|
521
|
+
console.error(' sui sandbox --self-test run the attack suite');
|
|
522
|
+
process.exitCode = 1;
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const code = readFileSync(file, 'utf8');
|
|
527
|
+
const result = await runCode({
|
|
528
|
+
code,
|
|
529
|
+
capabilities,
|
|
530
|
+
requireNoNetwork: Boolean(flags['require-no-network']),
|
|
531
|
+
limits: {
|
|
532
|
+
...(flags.wall ? { wallMs: Number(flags.wall) } : {}),
|
|
533
|
+
...(flags.heap ? { heapMb: Number(flags.heap) } : {}),
|
|
534
|
+
},
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
if (result.refused) {
|
|
538
|
+
console.error(result.reason);
|
|
539
|
+
process.exitCode = 1;
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
544
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
545
|
+
console.log('');
|
|
546
|
+
console.log(
|
|
547
|
+
` exit ${result.exitCode ?? '—'} · ${result.ms}ms · ${result.killed ? 'killed at the wall clock' : 'finished'} · permission model ${result.capabilities.permissionModel ? 'on' : 'off'} · network ${result.capabilities.networkIsolated ? 'isolated' : 'NOT isolated'}`,
|
|
548
|
+
);
|
|
549
|
+
if (!result.ok) process.exitCode = 1;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/* --------------------------------------------------------------------- local */
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* `sui local` — B2.6, find a model server on this machine and wire it up.
|
|
556
|
+
*
|
|
557
|
+
* Loopback only, unauthenticated GETs, nothing written unless asked.
|
|
558
|
+
*/
|
|
559
|
+
export async function cmdLocal(flags) {
|
|
560
|
+
const { discoverLocalRuntimes, toConfigPatch } = await import('@drsamonline/prober');
|
|
561
|
+
|
|
562
|
+
console.log('');
|
|
563
|
+
console.log('Looking for local model servers on loopback (127.0.0.1)…');
|
|
564
|
+
|
|
565
|
+
const found = await discoverLocalRuntimes({ timeoutMs: Number(flags.timeout ?? 300) });
|
|
566
|
+
|
|
567
|
+
if (!found.length) {
|
|
568
|
+
console.log('');
|
|
569
|
+
console.log(' nothing answered. Servers this looks for, and their default ports:');
|
|
570
|
+
console.log(' Ollama 11434');
|
|
571
|
+
console.log(' LM Studio 1234');
|
|
572
|
+
console.log(' llama.cpp 8080');
|
|
573
|
+
console.log(' vLLM / LocalAI 8000 / 5000');
|
|
574
|
+
console.log('');
|
|
575
|
+
console.log(' Start one and run this again. Nothing was probed outside 127.0.0.1.');
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
console.log('');
|
|
580
|
+
for (const runtime of found) {
|
|
581
|
+
console.log(` ${runtime.kind} on port ${runtime.port} — ${runtime.models.length} model(s), answered in ${runtime.latencyMs}ms`);
|
|
582
|
+
for (const model of runtime.models.slice(0, 8)) console.log(` ${model}`);
|
|
583
|
+
if (runtime.models.length > 8) console.log(` … and ${runtime.models.length - 8} more`);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if (!flags.write) {
|
|
587
|
+
console.log('');
|
|
588
|
+
console.log('Nothing was written. Run with --write to add these to sui.config.json.');
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const { applyRecipe } = await import('@drsamonline/core');
|
|
593
|
+
const gateway = await createGateway({ configPath: flags.config });
|
|
594
|
+
const path = flags.config ?? gateway.config.path ?? 'sui.config.json';
|
|
595
|
+
const onDisk = JSON.parse(readFileSync(path, 'utf8'));
|
|
596
|
+
|
|
597
|
+
let merged = onDisk;
|
|
598
|
+
let added = 0;
|
|
599
|
+
for (const runtime of found) {
|
|
600
|
+
const recipe = {
|
|
601
|
+
name: 'local-discovery',
|
|
602
|
+
title: `Local ${runtime.kind}`,
|
|
603
|
+
description: 'Discovered on loopback',
|
|
604
|
+
patch: toConfigPatch(runtime),
|
|
605
|
+
};
|
|
606
|
+
const result = applyRecipe(merged, recipe, { force: Boolean(flags.force) });
|
|
607
|
+
merged = result.config;
|
|
608
|
+
added += result.changes.length;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
if (!added) {
|
|
612
|
+
console.log('');
|
|
613
|
+
console.log(' already in the config — nothing to add.');
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
writeFileSync(path, `${JSON.stringify(merged, null, 2)}\n`);
|
|
618
|
+
console.log('');
|
|
619
|
+
console.log(`Wrote ${added} change(s) to ${path}. Restart the gateway to pick them up.`);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/* ------------------------------------------------------------------- recipes */
|
|
623
|
+
|
|
624
|
+
/** Where recipes live: the repo's own folder, then the user's config dir. */
|
|
625
|
+
/** The same rule the CLI uses: npx only works once the package is published. */
|
|
626
|
+
function runAs() {
|
|
627
|
+
const invoked = String(process.argv[1] ?? '');
|
|
628
|
+
if (/node_modules/.test(invoked) || !/apps[\\/]cli[\\/]bin/.test(invoked)) return 'sui';
|
|
629
|
+
return process.platform === 'win32' ? '.\\sui.cmd' : './sui';
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function recipeDirs() {
|
|
633
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
634
|
+
const root = join(here, '..', '..', '..');
|
|
635
|
+
return [join(root, 'recipes'), join(homedir(), '.sui', 'recipes')];
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
function loadRecipes() {
|
|
639
|
+
const found = new Map();
|
|
640
|
+
for (const dir of recipeDirs()) {
|
|
641
|
+
if (!existsSync(dir)) continue;
|
|
642
|
+
for (const file of readdirSync(dir).filter((f) => f.endsWith('.json'))) {
|
|
643
|
+
try {
|
|
644
|
+
const recipe = JSON.parse(readFileSync(join(dir, file), 'utf8'));
|
|
645
|
+
if (recipe.name && !found.has(recipe.name)) found.set(recipe.name, recipe);
|
|
646
|
+
} catch {
|
|
647
|
+
/* a broken recipe must not stop the others from loading */
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
return found;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
export async function cmdRecipe(flags, args) {
|
|
655
|
+
const [action = 'list', name] = args;
|
|
656
|
+
const recipes = loadRecipes();
|
|
657
|
+
|
|
658
|
+
if (action === 'list') {
|
|
659
|
+
if (!recipes.size) {
|
|
660
|
+
console.log('No recipes found. Looked in:', recipeDirs().join(', '));
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
console.log('');
|
|
664
|
+
console.log('Recipes');
|
|
665
|
+
console.log('');
|
|
666
|
+
for (const recipe of recipes.values()) {
|
|
667
|
+
console.log(` ${recipe.name.padEnd(18)} ${recipe.title}`);
|
|
668
|
+
console.log(` ${''.padEnd(18)} ${recipe.description}`);
|
|
669
|
+
console.log('');
|
|
670
|
+
}
|
|
671
|
+
console.log(`Apply one: ${runAs()} recipe apply <name> [--dry-run] [--force]`);
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
if (!name) {
|
|
676
|
+
console.error(`Which recipe? Try: ${[...recipes.keys()].join(', ')}`);
|
|
677
|
+
process.exitCode = 1;
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
const recipe = recipes.get(name);
|
|
682
|
+
if (!recipe) {
|
|
683
|
+
console.error(`No recipe called "${name}". Available: ${[...recipes.keys()].join(', ')}.`);
|
|
684
|
+
process.exitCode = 1;
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (action === 'show') {
|
|
689
|
+
console.log('');
|
|
690
|
+
console.log(`${recipe.title} (${recipe.name})`);
|
|
691
|
+
console.log('');
|
|
692
|
+
console.log(` ${recipe.description}`);
|
|
693
|
+
console.log('');
|
|
694
|
+
console.log(JSON.stringify(recipe.patch, null, 2).replace(/^/gm, ' '));
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if (action !== 'apply') {
|
|
699
|
+
console.error(`Unknown action "${action}". Use list, show or apply.`);
|
|
700
|
+
process.exitCode = 1;
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const { applyRecipe, describeRecipe } = await import('@drsamonline/core');
|
|
705
|
+
const gateway = await createGateway({ configPath: flags.config });
|
|
706
|
+
const result = applyRecipe(gateway.config, recipe, { force: Boolean(flags.force) });
|
|
707
|
+
|
|
708
|
+
console.log('');
|
|
709
|
+
console.log(describeRecipe(recipe));
|
|
710
|
+
console.log('');
|
|
711
|
+
|
|
712
|
+
if (result.missingEnv.length) {
|
|
713
|
+
console.log(` needs env: ${result.missingEnv.join(', ')} — set these before the recipe does anything useful`);
|
|
714
|
+
console.log('');
|
|
715
|
+
}
|
|
716
|
+
for (const change of result.changes) console.log(` + ${change.path}${change.note ? ` (${change.note})` : ''}`);
|
|
717
|
+
for (const conflict of result.conflicts) console.log(` = ${conflict.path}: ${conflict.reason}`);
|
|
718
|
+
|
|
719
|
+
if (!result.changes.length) {
|
|
720
|
+
console.log(' nothing to change — this recipe is already applied.');
|
|
721
|
+
console.log('');
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
if (flags['dry-run']) {
|
|
726
|
+
console.log('');
|
|
727
|
+
console.log('Dry run: nothing was written. Run without --dry-run to apply.');
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const path = flags.config ?? gateway.config.path ?? 'sui.config.json';
|
|
732
|
+
const onDisk = JSON.parse(readFileSync(path, 'utf8'));
|
|
733
|
+
// Merge the recipe's additions into the file as it is, so anything the user
|
|
734
|
+
// edited by hand survives.
|
|
735
|
+
const merged = applyRecipe(onDisk, recipe, { force: Boolean(flags.force) }).config;
|
|
736
|
+
writeFileSync(path, `${JSON.stringify(merged, null, 2)}\n`);
|
|
737
|
+
console.log('');
|
|
738
|
+
console.log(`Written to ${path}. Restart the gateway to pick it up.`);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/* ---------------------------------------------------------------------- mcp */
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* `sui mcp` — the gateway as a Model Context Protocol server.
|
|
745
|
+
*
|
|
746
|
+
* stdio is what an editor launches; http is what a remote agent can reach. Both
|
|
747
|
+
* serve the same tools, so an agent does not need to know which one you chose.
|
|
748
|
+
*/
|
|
749
|
+
export async function cmdMcp(flags) {
|
|
750
|
+
const gateway = await createGateway({ configPath: flags.config, ledgerFile: undefined });
|
|
751
|
+
const transport = String(flags.transport ?? 'stdio');
|
|
752
|
+
const server = createMcpServer({ gateway, version: VERSION });
|
|
753
|
+
|
|
754
|
+
if (transport === 'http') {
|
|
755
|
+
const port = Number(flags.port ?? 8788);
|
|
756
|
+
const handleMcp = createHttpHandler({ server });
|
|
757
|
+
|
|
758
|
+
// Its own listener, not the gateway's: a request handler that has already
|
|
759
|
+
// answered cannot be second-guessed, and MCP deserves its own port anyway.
|
|
760
|
+
const http = await import('node:http');
|
|
761
|
+
const mcpHttp = http.createServer(async (req, res) => {
|
|
762
|
+
const url = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`);
|
|
763
|
+
const handled = await handleMcp(req, res, url);
|
|
764
|
+
if (!handled) {
|
|
765
|
+
res.writeHead(404, { 'content-type': 'application/json' });
|
|
766
|
+
res.end(JSON.stringify({ error: `No MCP route for ${req.method} ${url.pathname}. The endpoint is /mcp.` }));
|
|
767
|
+
}
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
await new Promise((resolve, reject) => {
|
|
771
|
+
mcpHttp.once('error', reject);
|
|
772
|
+
mcpHttp.listen(port, gateway.config.server.host, resolve);
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
console.log('');
|
|
776
|
+
console.log(`sui ${VERSION} — MCP over HTTP`);
|
|
777
|
+
console.log(` endpoint http://${gateway.config.server.host}:${port}/mcp`);
|
|
778
|
+
console.log(` protocol ${SUPPORTED_PROTOCOL_VERSIONS.join(', ')}`);
|
|
779
|
+
console.log(` tools ${['sui_chat', 'sui_models', 'sui_explain', 'sui_cost', 'sui_compress', 'sui_status'].join(', ')}`);
|
|
780
|
+
console.log('');
|
|
781
|
+
console.log('POST a JSON-RPC message to the endpoint. Ctrl-C to stop.');
|
|
782
|
+
|
|
783
|
+
await new Promise((resolve) => {
|
|
784
|
+
const stop = () => {
|
|
785
|
+
mcpHttp.closeAllConnections?.();
|
|
786
|
+
mcpHttp.close(() => resolve(undefined));
|
|
787
|
+
};
|
|
788
|
+
process.once('SIGINT', stop);
|
|
789
|
+
process.once('SIGTERM', stop);
|
|
790
|
+
});
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
// stdout is the protocol. The banner goes to stderr, or the client sees a
|
|
795
|
+
// non-JSON line and drops the connection.
|
|
796
|
+
console.error(`sui ${VERSION} — MCP over stdio (protocol ${SUPPORTED_PROTOCOL_VERSIONS[0]}). Logs go to stderr.`);
|
|
797
|
+
await runStdio({ server });
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/* ------------------------------------------------------------------ tenants */
|
|
801
|
+
|
|
802
|
+
/** `sui tenants` — B7.7: what each key has spent, and what it may reach. */
|
|
803
|
+
export async function cmdTenants(flags) {
|
|
804
|
+
const gateway = await createGateway({ configPath: flags.config, ledgerFile: undefined });
|
|
805
|
+
const { config, ledger } = gateway;
|
|
806
|
+
|
|
807
|
+
const { tenantSnapshot, tenantSummary } = await import('@drsamonline/core');
|
|
808
|
+
const snapshot = tenantSnapshot({ config, ledger });
|
|
809
|
+
const summary = tenantSummary(snapshot);
|
|
810
|
+
const configured = (config.apiKeys ?? []).length > 0;
|
|
811
|
+
|
|
812
|
+
// --json means stdout is the document. A helpful sentence there breaks a pipe,
|
|
813
|
+
// and an empty result is still a result: it says zero tenants are configured.
|
|
814
|
+
if (flags.json) {
|
|
815
|
+
console.log(
|
|
816
|
+
JSON.stringify(
|
|
817
|
+
{
|
|
818
|
+
summary,
|
|
819
|
+
tenants: snapshot,
|
|
820
|
+
...(configured ? {} : { note: 'No API keys are configured, so every caller is the same anonymous tenant.' }),
|
|
821
|
+
},
|
|
822
|
+
null,
|
|
823
|
+
2,
|
|
824
|
+
),
|
|
825
|
+
);
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
if (!configured) {
|
|
830
|
+
console.log('');
|
|
831
|
+
console.log('No API keys are configured, so every caller is the same anonymous tenant.');
|
|
832
|
+
console.log('Add an apiKeys entry to sui.config.json to separate them.');
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
console.log('');
|
|
837
|
+
console.log(`Tenants — ${summary.tenants} key(s), ${summary.capped} capped, ${summary.uncapped} uncapped`);
|
|
838
|
+
console.log('');
|
|
839
|
+
console.log(
|
|
840
|
+
table(
|
|
841
|
+
['tenant', 'turns', 'failed', 'spent', 'cap', 'used', 'rpm', 'reaches'],
|
|
842
|
+
snapshot.map((t) => [
|
|
843
|
+
t.name,
|
|
844
|
+
String(t.turns),
|
|
845
|
+
String(t.failed),
|
|
846
|
+
`$${t.spentUsd.toFixed(4)}`,
|
|
847
|
+
t.dailyUsd == null ? 'uncapped' : `$${t.dailyUsd.toFixed(2)}`,
|
|
848
|
+
t.pct == null ? '—' : `${t.pct}%`,
|
|
849
|
+
t.rateLimit?.rpm ? String(t.rateLimit.rpm) : '—',
|
|
850
|
+
t.allowedCombos ? t.allowedCombos.join(',') : 'everything',
|
|
851
|
+
]),
|
|
852
|
+
),
|
|
853
|
+
);
|
|
854
|
+
if (summary.nearestCap) {
|
|
855
|
+
console.log('');
|
|
856
|
+
console.log(` closest to its cap: ${summary.nearestCap}`);
|
|
857
|
+
}
|
|
858
|
+
console.log('');
|
|
859
|
+
console.log('Change a cap live: curl -X PATCH /api/tenants/<name> -d \'{"dailyUsd":5}\'');
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/* ------------------------------------------------------------------ explain */
|
|
863
|
+
|
|
864
|
+
/** `sui explain [combo]` — B7.4, why a route would go where it goes. */
|
|
865
|
+
export async function cmdExplain(flags, args) {
|
|
866
|
+
const gateway = await createGateway({ configPath: flags.config, ledgerFile: null });
|
|
867
|
+
const combos = Object.keys(gateway.config.combos);
|
|
868
|
+
const combo = args[0] ?? flags.combo ?? combos[0];
|
|
869
|
+
if (!gateway.config.combos[combo]) {
|
|
870
|
+
throw new ConfigError(`No combo called "${combo}". Available: ${combos.join(', ')}.`);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
const plan = gateway.router.plan(combo, {});
|
|
874
|
+
const lines = explainPlan(plan);
|
|
875
|
+
|
|
876
|
+
if (flags.json) {
|
|
877
|
+
console.log(JSON.stringify({ combo, lines, plan: { ordered: plan.ordered.map((t) => t.modelId), resting: plan.resting.map((t) => t.modelId), skipped: plan.skipped } }, null, 2));
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
console.log('');
|
|
882
|
+
console.log(`Explain-my-route — ${combo}`);
|
|
883
|
+
console.log('');
|
|
884
|
+
for (const line of lines) console.log(` ${line}`);
|
|
885
|
+
console.log('');
|
|
886
|
+
console.log('Nothing was sent. This is the plan, computed from the config and the ledger.');
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
/* ------------------------------------------------------------------ savings */
|
|
890
|
+
|
|
891
|
+
/** `sui savings` — B7.2: what cache hits and compression did not spend. */
|
|
892
|
+
export async function cmdSavings(flags) {
|
|
893
|
+
const gateway = await createGateway({ configPath: flags.config, ledgerFile: undefined });
|
|
894
|
+
const { ledger } = gateway;
|
|
895
|
+
const since = flags.all ? 0 : startOfToday();
|
|
896
|
+
const agg = ledger.aggregate({ since });
|
|
897
|
+
const s = agg.totals.savings;
|
|
898
|
+
|
|
899
|
+
if (flags.json) {
|
|
900
|
+
console.log(JSON.stringify({ since: agg.since, savings: s, spentUsd: agg.totals.costUsd, turns: agg.totals.turns }, null, 2));
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
const wouldHaveCost = agg.totals.costUsd + s.totalUsd;
|
|
905
|
+
console.log('');
|
|
906
|
+
console.log('Savings ledger');
|
|
907
|
+
console.log('');
|
|
908
|
+
console.log(table(
|
|
909
|
+
['what', 'value'],
|
|
910
|
+
[
|
|
911
|
+
['spent', `$${agg.totals.costUsd.toFixed(4)}`],
|
|
912
|
+
['saved by cache', `$${s.cacheUsd.toFixed(4)} (${s.cacheHits} hit${s.cacheHits === 1 ? '' : 's'})`],
|
|
913
|
+
['saved by compression', `$${s.compressionUsd.toFixed(4)} (${s.compressedTurns} turn${s.compressedTurns === 1 ? '' : 's'})`],
|
|
914
|
+
['total saved', `$${s.totalUsd.toFixed(4)}`],
|
|
915
|
+
['tokens not sent', s.tokensSaved.toLocaleString('en-IN')],
|
|
916
|
+
['would have cost', `$${wouldHaveCost.toFixed(4)}`],
|
|
917
|
+
['share avoided', wouldHaveCost ? `${Math.round((s.totalUsd / wouldHaveCost) * 100)}%` : '—'],
|
|
918
|
+
],
|
|
919
|
+
));
|
|
920
|
+
console.log('');
|
|
921
|
+
console.log(flags.all ? 'All time.' : 'Today. Use --all for everything in the ledger.');
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
function startOfToday() {
|
|
925
|
+
const d = new Date();
|
|
926
|
+
d.setHours(0, 0, 0, 0);
|
|
927
|
+
return d.getTime();
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/* ----------------------------------------------------------------- compress */
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* `sui compress <file> [--preset ultra]` — see exactly what a preset would do to
|
|
934
|
+
* a prompt before you turn compression on for real traffic.
|
|
935
|
+
*/
|
|
936
|
+
export function cmdCompress(flags, args) {
|
|
937
|
+
const preset = String(flags.preset ?? 'standard');
|
|
938
|
+
if (!PRESETS[preset]) throw new ConfigError(`Unknown preset "${preset}". Use: ${Object.keys(PRESETS).join(', ')}.`);
|
|
939
|
+
|
|
940
|
+
let text;
|
|
941
|
+
if (typeof flags.text === 'string') text = flags.text;
|
|
942
|
+
else if (args[0] && args[0] !== '-') {
|
|
943
|
+
const path = args[0];
|
|
944
|
+
if (!existsSync(path)) throw new ConfigError(`No file at ${path}.`);
|
|
945
|
+
text = readFileSync(path, 'utf8');
|
|
946
|
+
} else {
|
|
947
|
+
text = readFileSync(0, 'utf8'); // stdin
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
if (!text.trim()) throw new ConfigError('Nothing to compress. Pass a file, --text "…", or pipe something in.');
|
|
951
|
+
|
|
952
|
+
const store = new RecallStore({ secret: process.env.SUI_RECALL_SECRET ?? 'sui-cli' });
|
|
953
|
+
const result = compress(text, { preset, store });
|
|
954
|
+
|
|
955
|
+
if (flags.json) {
|
|
956
|
+
console.log(JSON.stringify({ ...result, text: undefined, textChars: result.text.length }, null, 2));
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
console.log('');
|
|
961
|
+
console.log(`preset ${preset} — ${result.originalChars} → ${result.compressedChars} chars (${Math.round(result.ratio * 100)}% smaller)`);
|
|
962
|
+
console.log('');
|
|
963
|
+
if (result.skipped) {
|
|
964
|
+
console.log(` nothing to do: ${result.skipped}`);
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
console.log(table(['engine', 'what it did', 'chars'], result.steps.map((s) => [s.engine, s.note, String(s.droppedChars)])));
|
|
968
|
+
|
|
969
|
+
if (result.tokens.length) {
|
|
970
|
+
console.log('');
|
|
971
|
+
console.log(`recall tokens: ${result.tokens.join(', ')} (the dropped text is in memory for this process only)`);
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
if (flags.out) {
|
|
975
|
+
writeFileSync(String(flags.out), result.text);
|
|
976
|
+
console.log('');
|
|
977
|
+
console.log(`written: ${flags.out}`);
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
console.log('');
|
|
981
|
+
console.log('--- compressed prompt ---');
|
|
982
|
+
console.log(result.text);
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/** `sui budget` — what preset the quota would pick right now. */
|
|
986
|
+
export function cmdBudget(flags) {
|
|
987
|
+
const fraction = Number(flags.fraction ?? 1);
|
|
988
|
+
const chosen = choosePreset({ fraction });
|
|
989
|
+
console.log('');
|
|
990
|
+
console.log(`quota fraction ${fraction} → preset ${chosen.preset}`);
|
|
991
|
+
console.log(` ${chosen.reason}`);
|
|
992
|
+
console.log('');
|
|
993
|
+
console.log(table(['preset', 'target', 'floor', 'engines'], Object.entries(PRESETS).map(([name, p]) => [name, `${Math.round(p.target * 100)}%`, `${Math.round(p.floor * 100)}%`, p.engines.join(' → ')])));
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/* ------------------------------------------------------------------- probe */
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* `sui probe [provider] [--interval <ms>]` — the canary prober (B2.3).
|
|
1000
|
+
*
|
|
1001
|
+
* Only entries with a base_url can be probed; the rest are identities, and the
|
|
1002
|
+
* report says so rather than inventing a result.
|
|
1003
|
+
*/
|
|
1004
|
+
export async function cmdProbe(flags, args) {
|
|
1005
|
+
const entries = loadCatalogue();
|
|
1006
|
+
const wanted = args[0] ?? flags.provider;
|
|
1007
|
+
const selected = wanted ? entries.filter((e) => e.provider === wanted) : entries;
|
|
1008
|
+
|
|
1009
|
+
if (!selected.length) {
|
|
1010
|
+
throw new ConfigError(`No catalogue entry called "${wanted}". Run \`sui catalog\` for the list.`);
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
const probeable = selected.filter((e) => e.base_url);
|
|
1014
|
+
const identities = selected.filter((e) => !e.base_url);
|
|
1015
|
+
|
|
1016
|
+
// In --json mode stdout is the document: a preamble on stdout makes the output
|
|
1017
|
+
// unparseable, which is how a CI step discovers it before a user does.
|
|
1018
|
+
const say = flags.json ? (...a) => console.error(...a) : (...a) => console.log(...a);
|
|
1019
|
+
say('');
|
|
1020
|
+
say(`Probing ${probeable.length} of ${selected.length} entries (fixtures v${PROBE_FIXTURE_VERSION})`);
|
|
1021
|
+
if (identities.length) {
|
|
1022
|
+
say(`${identities.length} have no base_url yet — an identity is not an endpoint, so they are not probed.`);
|
|
1023
|
+
}
|
|
1024
|
+
say('');
|
|
1025
|
+
|
|
1026
|
+
const prober = new Prober({ entries: probeable, timeoutMs: Number(flags.timeout ?? 8000) });
|
|
1027
|
+
const results = await prober.runOnce();
|
|
1028
|
+
|
|
1029
|
+
if (flags.json) {
|
|
1030
|
+
console.log(JSON.stringify({ fixtureVersion: PROBE_FIXTURE_VERSION, results, state: prober.snapshot() }, null, 2));
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
if (!results.length) {
|
|
1035
|
+
console.log('Nothing to probe. Add a base_url to a catalogue entry, or run `sui catalog` to see what is waiting.');
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
const rows = results.map((r) => [
|
|
1040
|
+
r.provider,
|
|
1041
|
+
r.ok ? 'ok' : 'fail',
|
|
1042
|
+
r.levels.liveness?.ok ? 'yes' : 'no',
|
|
1043
|
+
r.levels.readiness?.ok ? 'yes' : 'no',
|
|
1044
|
+
Object.entries(r.levels.suitability).map(([k, v]) => `${k}:${v.ok ? 'ok' : 'no'}`).join(' ') || '—',
|
|
1045
|
+
`${r.latencyMs} ms`,
|
|
1046
|
+
r.reasons.length ? r.reasons.join('; ').slice(0, 60) : '',
|
|
1047
|
+
]);
|
|
1048
|
+
console.log(table(['provider', 'result', 'live', 'ready', 'suitable', 'latency', 'why not'], rows));
|
|
1049
|
+
|
|
1050
|
+
const ok = results.filter((r) => r.ok).length;
|
|
1051
|
+
console.log('');
|
|
1052
|
+
console.log(`${ok} of ${results.length} probed entries answered.`);
|
|
1053
|
+
|
|
1054
|
+
if (flags.revoke) {
|
|
1055
|
+
const provider = args[0] ?? flags.provider;
|
|
1056
|
+
const path = verifiedPath();
|
|
1057
|
+
const current = existsSync(path) ? JSON.parse(readFileSync(path, 'utf8')) : { version: 1, entries: [] };
|
|
1058
|
+
const before = (current.entries ?? []).length;
|
|
1059
|
+
current.entries = (current.entries ?? []).filter((row) => row.provider !== provider);
|
|
1060
|
+
writeFileSync(path, `${JSON.stringify(current, null, 2)}\n`);
|
|
1061
|
+
console.log(`Revoked ${before - current.entries.length} verification(s) for ${provider}.`);
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
if (flags.write) {
|
|
1066
|
+
if (!ok) {
|
|
1067
|
+
console.log('Nothing to write: no entry answered, so no route was promoted.');
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
const total = writeVerified(results.map((r) => ({ ...r, base_url: probeable.find((e) => e.provider === r.provider)?.base_url })));
|
|
1071
|
+
console.log(`Wrote ${ok} verification(s) to ${verifiedPath()} (${total} on record).`);
|
|
1072
|
+
console.log('Those entries are now routable. `sui catalog` shows them as verified.');
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
if (!ok) {
|
|
1077
|
+
console.log('Nothing is routable yet. That is the honest state, not a bug: a route becomes verified by answering.');
|
|
1078
|
+
} else {
|
|
1079
|
+
console.log('Run `sui probe --write` to promote what answered into the catalogue.');
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
/* ------------------------------------------------------------------ exports */
|
|
1084
|
+
|
|
1085
|
+
/** `sui cost --export usage.csv [--gzip]` — compressed with compress-kit. */
|
|
1086
|
+
export async function writeExport({ ledger, file, format, gzip }) {
|
|
1087
|
+
const body = format === 'json' ? JSON.stringify(ledger.aggregate({ since: 0 }), null, 2) : ledger.csv();
|
|
1088
|
+
const target = file ?? `sui-usage.${format === 'json' ? 'json' : 'csv'}`;
|
|
1089
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
1090
|
+
|
|
1091
|
+
if (!gzip) {
|
|
1092
|
+
writeFileSync(target, body);
|
|
1093
|
+
console.log(`Wrote ${target} (${body.length} bytes).`);
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
const Compress = loadKit('compress-kit');
|
|
1098
|
+
// gzip, not compressString: that one hands back base64, and a .gz file has to
|
|
1099
|
+
// be real gzip bytes so gunzip(1) can read it.
|
|
1100
|
+
const bytes = await Compress.gzip(Compress.bytes.fromText(body));
|
|
1101
|
+
const gzPath = `${target}.gz`;
|
|
1102
|
+
writeFileSync(gzPath, Buffer.from(bytes));
|
|
1103
|
+
console.log(`Wrote ${gzPath} (${body.length} bytes → ${bytes.length} bytes, ${Math.round(Compress.ratio(body.length, bytes.length) * 100)}%).`);
|
|
1104
|
+
console.log('Read it back with: gunzip -c ' + gzPath);
|
|
1105
|
+
}
|