labs-claude 0.12.0 → 0.12.1
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/labs.js +1 -1
- package/package.json +1 -1
- package/src/admin-cli.js +148 -38
package/bin/labs.js
CHANGED
package/package.json
CHANGED
package/src/admin-cli.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
// Cracked-engineer terminal-native Admin Control Room (`labs admin`).
|
|
2
2
|
// Zero browser needed: mint friend keys, pause/resume/revoke, and inspect live per-friend token bars in your terminal.
|
|
3
3
|
const axios = require('axios');
|
|
4
|
+
const https = require('https');
|
|
4
5
|
const fs = require('fs');
|
|
5
6
|
const path = require('path');
|
|
6
|
-
const { spawn,
|
|
7
|
+
const { spawn, execFile } = require('child_process');
|
|
8
|
+
const { promisify } = require('util');
|
|
9
|
+
const execFileAsync = promisify(execFile);
|
|
7
10
|
const { LABS_DIR } = require('./config');
|
|
8
11
|
const { storedConfig } = require('./gateway-client');
|
|
9
12
|
const { c, sparkline, randomQuip, playMascotIntro, withSpinner, printCard } = require('./ui');
|
|
@@ -58,10 +61,30 @@ function readAdminToken() {
|
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
function createAdminApi(url, token) {
|
|
61
|
-
const
|
|
64
|
+
const httpsAgent = new https.Agent({ keepAlive: true, maxSockets: 2, family: 4 });
|
|
65
|
+
const client = axios.create({
|
|
66
|
+
baseURL: `${url}/admin/api`,
|
|
67
|
+
headers: { authorization: `Bearer ${token}` },
|
|
68
|
+
timeout: 8000,
|
|
69
|
+
httpsAgent,
|
|
70
|
+
});
|
|
71
|
+
async function callWithRetry(fn, retries = 2) {
|
|
72
|
+
let lastErr;
|
|
73
|
+
for (let i = 0; i <= retries; i += 1) {
|
|
74
|
+
try {
|
|
75
|
+
return (await fn()).data;
|
|
76
|
+
} catch (err) {
|
|
77
|
+
lastErr = err;
|
|
78
|
+
const status = err?.response?.status;
|
|
79
|
+
if (status && status < 500) throw err;
|
|
80
|
+
if (i < retries) await new Promise(r => setTimeout(r, 400 * (i + 1)));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
throw lastErr;
|
|
84
|
+
}
|
|
62
85
|
return {
|
|
63
|
-
get: p =>
|
|
64
|
-
post: (p, body) =>
|
|
86
|
+
get: p => callWithRetry(() => client.get(p)),
|
|
87
|
+
post: (p, body) => callWithRetry(() => client.post(p, body)),
|
|
65
88
|
};
|
|
66
89
|
}
|
|
67
90
|
|
|
@@ -71,7 +94,20 @@ function resolveKeyTarget(keys, target) {
|
|
|
71
94
|
return keys.find(k => String(k.id) === raw || k.name.toLowerCase() === raw.toLowerCase()) || null;
|
|
72
95
|
}
|
|
73
96
|
|
|
74
|
-
function
|
|
97
|
+
function formatBoxStatus(box, ec2State) {
|
|
98
|
+
const st = ec2State ? String(ec2State).toLowerCase() : null;
|
|
99
|
+
if (st === 'stopped') return c.rose('SLEEPING ($0/hr)');
|
|
100
|
+
if (st === 'stopping' || st === 'shutting-down') return c.amber('STOPPING...');
|
|
101
|
+
if (st === 'pending') return c.amber('BOOTING (ec2 pending)');
|
|
102
|
+
if (st === 'running' && !box?.up) return c.amber('BOOTING (loading Qwen4...)');
|
|
103
|
+
if (box?.up) {
|
|
104
|
+
const models = (box.models || []).join(', ');
|
|
105
|
+
return `${c.mint('ONLINE')}${models ? ` ${c.dim(`(${models})`)}` : ''}`;
|
|
106
|
+
}
|
|
107
|
+
return c.rose('SLEEPING');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function renderDashboard(keys, usageRows, box, ec2State = null) {
|
|
75
111
|
const todayStr = new Date(Date.now() + 5 * 3600e3).toISOString().slice(0, 10);
|
|
76
112
|
const todayRows = usageRows.filter(r => r.day === todayStr);
|
|
77
113
|
const todayIn = todayRows.reduce((s, r) => s + r.input, 0);
|
|
@@ -93,9 +129,7 @@ function renderDashboard(keys, usageRows, box) {
|
|
|
93
129
|
last7.push(byDay[d] || 0);
|
|
94
130
|
}
|
|
95
131
|
|
|
96
|
-
const boxPill = box
|
|
97
|
-
? `${c.mint('ONLINE')} ${c.dim(`(${box.models.join(', ')})`)}`
|
|
98
|
-
: `${c.rose('SLEEPING')}`;
|
|
132
|
+
const boxPill = formatBoxStatus(box, ec2State);
|
|
99
133
|
|
|
100
134
|
printCard('LABS ADMIN CONTROL ROOM', [
|
|
101
135
|
`${c.dim('gpu box:')} ${boxPill} ${c.dim('7d trend:')} ${sparkline(last7)}`,
|
|
@@ -178,53 +212,119 @@ function awsBoxArgs(cfg) {
|
|
|
178
212
|
};
|
|
179
213
|
}
|
|
180
214
|
|
|
181
|
-
function runAws(args) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
215
|
+
async function runAws(args, timeout = 15000) {
|
|
216
|
+
try {
|
|
217
|
+
const { stdout } = await execFileAsync('aws', args, { encoding: 'utf8', timeout, windowsHide: true });
|
|
218
|
+
return (stdout || '').trim();
|
|
219
|
+
} catch (err) {
|
|
220
|
+
const msg = (err.stderr || err.stdout || err.message || 'aws command failed').trim();
|
|
221
|
+
throw new Error(msg);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function getEc2State(cfg, runAwsFn = runAws) {
|
|
226
|
+
const { instanceId, region } = awsBoxArgs(cfg);
|
|
227
|
+
try {
|
|
228
|
+
const out = await runAwsFn([
|
|
229
|
+
'ec2', 'describe-instances',
|
|
230
|
+
'--region', region,
|
|
231
|
+
'--instance-ids', instanceId,
|
|
232
|
+
'--query', 'Reservations[0].Instances[0].State.Name',
|
|
233
|
+
'--output', 'text',
|
|
234
|
+
], 6000);
|
|
235
|
+
return out && out !== 'None' ? out.trim().toLowerCase() : null;
|
|
236
|
+
} catch {
|
|
237
|
+
return null;
|
|
185
238
|
}
|
|
186
|
-
return (res.stdout || '').trim();
|
|
187
239
|
}
|
|
188
240
|
|
|
189
|
-
|
|
241
|
+
function fmtElapsed(ms) {
|
|
242
|
+
const elapsed = Math.max(0, Math.floor(ms / 1000));
|
|
243
|
+
const mm = String(Math.floor(elapsed / 60)).padStart(2, '0');
|
|
244
|
+
const ss = String(elapsed % 60).padStart(2, '0');
|
|
245
|
+
return `${mm}:${ss}`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async function wakeGpuBox(api, cfg, deps = {}) {
|
|
249
|
+
const runAwsFn = deps.runAws || runAws;
|
|
250
|
+
const sleepFn = deps.sleep || (ms => new Promise(r => setTimeout(r, ms)));
|
|
251
|
+
const pollMs = deps.pollMs ?? 5000;
|
|
252
|
+
const maxMs = deps.maxMs ?? (6 * 60 * 1000);
|
|
190
253
|
const { instanceId, region } = awsBoxArgs(cfg);
|
|
254
|
+
|
|
255
|
+
const preState = await getEc2State(cfg, runAwsFn);
|
|
256
|
+
if (preState === 'running') {
|
|
257
|
+
try {
|
|
258
|
+
const box = await api.get('/box');
|
|
259
|
+
if (box?.up) {
|
|
260
|
+
console.log(` ${c.mint('● ONLINE')} ${c.bold(instanceId)} is already up — ${c.cyan((box.models || []).join(', '))}\n`);
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
} catch { /* still booting */ }
|
|
264
|
+
}
|
|
265
|
+
|
|
191
266
|
await withSpinner(`Starting GPU box ${c.cyan(instanceId)} (${region})...`, async () => {
|
|
192
|
-
|
|
267
|
+
await runAwsFn(['ec2', 'start-instances', '--region', region, '--instance-ids', instanceId]);
|
|
193
268
|
});
|
|
194
269
|
console.log(` ${c.mint('✔')} EC2 start sent. Waiting for Qwen4-35B-A3B 1M + DFlash2 auto-boot (~3–4 min)...`);
|
|
195
270
|
const t0 = Date.now();
|
|
196
|
-
const maxMs = 6 * 60 * 1000;
|
|
197
271
|
while (Date.now() - t0 < maxMs) {
|
|
198
|
-
const
|
|
199
|
-
const
|
|
200
|
-
const ss = String(elapsed % 60).padStart(2, '0');
|
|
272
|
+
const elapsedStr = fmtElapsed(Date.now() - t0);
|
|
273
|
+
const ec2State = await getEc2State(cfg, runAwsFn);
|
|
201
274
|
try {
|
|
202
275
|
const box = await api.get('/box');
|
|
203
|
-
if (box?.up) {
|
|
204
|
-
process.stdout.write(`\r ${c.mint('● ONLINE')} in ${c.bold(
|
|
276
|
+
if (box?.up && (!ec2State || ec2State === 'running')) {
|
|
277
|
+
process.stdout.write(`\r ${c.mint('● ONLINE')} in ${c.bold(elapsedStr)} — ${c.cyan((box.models || []).join(', '))}${' '.repeat(24)}\n\n`);
|
|
205
278
|
return true;
|
|
206
279
|
}
|
|
207
280
|
} catch { /* gateway still waiting on tunnel */ }
|
|
208
281
|
if (process.stdout.isTTY) {
|
|
209
|
-
|
|
282
|
+
const phase = ec2State === 'pending'
|
|
283
|
+
? 'EC2 booting (pending)...'
|
|
284
|
+
: 'loading Qwen4 weights & warming CUDA graphs...';
|
|
285
|
+
process.stdout.write(`\r ${c.cyan('⏳')} ${elapsedStr} · ${phase}${' '.repeat(12)}`);
|
|
210
286
|
}
|
|
211
|
-
await
|
|
287
|
+
await sleepFn(pollMs);
|
|
212
288
|
}
|
|
213
289
|
console.log(`\n ${c.amber('!')} Still booting after 6m — check ${c.cyan('labs admin stats')} in a minute.\n`);
|
|
214
290
|
return false;
|
|
215
291
|
}
|
|
216
292
|
|
|
217
|
-
async function sleepGpuBox(cfg, ask) {
|
|
293
|
+
async function sleepGpuBox(api, cfg, ask, deps = {}) {
|
|
294
|
+
const runAwsFn = deps.runAws || runAws;
|
|
295
|
+
const sleepFn = deps.sleep || (ms => new Promise(r => setTimeout(r, ms)));
|
|
296
|
+
const pollMs = deps.pollMs ?? 3000;
|
|
297
|
+
const maxMs = deps.maxMs ?? (3 * 60 * 1000);
|
|
218
298
|
const { instanceId, region } = awsBoxArgs(cfg);
|
|
299
|
+
|
|
300
|
+
const preState = await getEc2State(cfg, runAwsFn);
|
|
301
|
+
if (preState === 'stopped') {
|
|
302
|
+
console.log(` ${c.mint('✔')} ${c.bold(instanceId)} is already ${c.rose('STOPPED')} ($0/hr compute).\n`);
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
|
|
219
306
|
const confirm = (await ask(` ${c.amber('!')} Stop GPU box ${c.bold(instanceId)} (${region}) to stop hourly billing? Type YES: `)).trim();
|
|
220
307
|
if (confirm !== 'YES') {
|
|
221
308
|
console.log(` ${c.dim('Cancelled.')}\n`);
|
|
222
309
|
return false;
|
|
223
310
|
}
|
|
224
|
-
await withSpinner(`
|
|
225
|
-
|
|
311
|
+
await withSpinner(`Sending stop signal to ${c.cyan(instanceId)}...`, async () => {
|
|
312
|
+
await runAwsFn(['ec2', 'stop-instances', '--region', region, '--instance-ids', instanceId]);
|
|
226
313
|
});
|
|
227
|
-
|
|
314
|
+
const t0 = Date.now();
|
|
315
|
+
while (Date.now() - t0 < maxMs) {
|
|
316
|
+
const elapsedStr = fmtElapsed(Date.now() - t0);
|
|
317
|
+
const ec2State = await getEc2State(cfg, runAwsFn);
|
|
318
|
+
if (ec2State === 'stopped') {
|
|
319
|
+
process.stdout.write(`\r ${c.rose('● STOPPED')} in ${c.bold(elapsedStr)} — ${c.bold(instanceId)} is at $0/hr compute. Wake anytime with ${c.cyan('labs admin wake')}.${' '.repeat(10)}\n\n`);
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
if (process.stdout.isTTY) {
|
|
323
|
+
process.stdout.write(`\r ${c.amber('⏳')} ${elapsedStr} · EC2 state: ${ec2State || 'stopping'} (waiting for full shutdown)...`);
|
|
324
|
+
}
|
|
325
|
+
await sleepFn(pollMs);
|
|
326
|
+
}
|
|
327
|
+
console.log(`\n ${c.amber('!')} Stop signal was accepted, still shutting down after 3m — press ${c.cyan('6')} to refresh.\n`);
|
|
228
328
|
return true;
|
|
229
329
|
}
|
|
230
330
|
|
|
@@ -258,20 +358,25 @@ async function runAdminCli(args, ask) {
|
|
|
258
358
|
}
|
|
259
359
|
if (sub === 'sleep' || sub === 'off' || sub === 'stop') {
|
|
260
360
|
await playMascotIntro(version, 'Admin · GPU Box Sleep');
|
|
261
|
-
await sleepGpuBox(cfg, ask);
|
|
361
|
+
await sleepGpuBox(api, cfg, ask);
|
|
262
362
|
return;
|
|
263
363
|
}
|
|
264
364
|
|
|
265
365
|
await playMascotIntro(version, `Admin Control Room · ${url}`);
|
|
266
366
|
|
|
267
367
|
const loadAll = async (days = 14) => withSpinner(randomQuip(), async () => {
|
|
268
|
-
const [k, u, b] = await Promise.all([
|
|
269
|
-
|
|
368
|
+
const [k, u, b, ec2State] = await Promise.all([
|
|
369
|
+
api.get('/keys'),
|
|
370
|
+
api.get(`/usage?days=${days}`),
|
|
371
|
+
api.get('/box').catch(() => ({ up: false, models: [] })),
|
|
372
|
+
getEc2State(cfg),
|
|
373
|
+
]);
|
|
374
|
+
return { keys: k.keys || [], rows: u.rows || [], box: b || { up: false, models: [] }, ec2State };
|
|
270
375
|
});
|
|
271
376
|
|
|
272
377
|
if (sub === 'stats' || sub === 'ls' || sub === 'list') {
|
|
273
|
-
const { keys, rows, box } = await loadAll(14);
|
|
274
|
-
renderDashboard(keys, rows, box);
|
|
378
|
+
const { keys, rows, box, ec2State } = await loadAll(14);
|
|
379
|
+
renderDashboard(keys, rows, box, ec2State);
|
|
275
380
|
return;
|
|
276
381
|
}
|
|
277
382
|
if (sub === 'usage') {
|
|
@@ -296,13 +401,18 @@ async function runAdminCli(args, ask) {
|
|
|
296
401
|
|
|
297
402
|
if (sub === 'watch' || sub === 'live') {
|
|
298
403
|
const fetchState = async () => {
|
|
299
|
-
const [k, u, b] = await Promise.all([
|
|
300
|
-
|
|
404
|
+
const [k, u, b, ec2State] = await Promise.all([
|
|
405
|
+
api.get('/keys'),
|
|
406
|
+
api.get('/usage?days=14'),
|
|
407
|
+
api.get('/box'),
|
|
408
|
+
getEc2State(cfg),
|
|
409
|
+
]);
|
|
410
|
+
return { keys: k.keys || [], rows: u.rows || [], box: b || { up: false, models: [] }, ec2State };
|
|
301
411
|
};
|
|
302
412
|
const renderLive = async () => {
|
|
303
413
|
const st = await fetchState();
|
|
304
414
|
process.stdout.write('\x1b[2J\x1b[H');
|
|
305
|
-
renderDashboard(st.keys, st.rows, st.box);
|
|
415
|
+
renderDashboard(st.keys, st.rows, st.box, st.ec2State);
|
|
306
416
|
console.log(` ${c.dim('live watch mode (refreshing every 3s) · press Ctrl+C to exit')}\n`);
|
|
307
417
|
};
|
|
308
418
|
await renderLive();
|
|
@@ -314,7 +424,7 @@ async function runAdminCli(args, ask) {
|
|
|
314
424
|
// Interactive menu loop
|
|
315
425
|
let state = await loadAll(14);
|
|
316
426
|
while (true) {
|
|
317
|
-
renderDashboard(state.keys, state.rows, state.box);
|
|
427
|
+
renderDashboard(state.keys, state.rows, state.box, state.ec2State);
|
|
318
428
|
console.log(` ${c.bold('actions:')}`);
|
|
319
429
|
console.log(` ${c.cyan('1.')} mint new friend api key ${c.cyan('4.')} revoke (delete) a key ${c.mint('7.')} wake gpu box (on)`);
|
|
320
430
|
console.log(` ${c.cyan('2.')} pause a key ${c.cyan('5.')} daily token table (14d) ${c.rose('8.')} stop gpu box (off)`);
|
|
@@ -330,7 +440,7 @@ async function runAdminCli(args, ask) {
|
|
|
330
440
|
continue;
|
|
331
441
|
}
|
|
332
442
|
if (choice === '8') {
|
|
333
|
-
await sleepGpuBox(cfg, ask);
|
|
443
|
+
await sleepGpuBox(api, cfg, ask);
|
|
334
444
|
state = await loadAll(14);
|
|
335
445
|
continue;
|
|
336
446
|
}
|
|
@@ -365,4 +475,4 @@ async function runAdminCli(args, ask) {
|
|
|
365
475
|
}
|
|
366
476
|
}
|
|
367
477
|
|
|
368
|
-
module.exports = { compactTokens, barChart, resolveKeyTarget, runAdminCli };
|
|
478
|
+
module.exports = { compactTokens, barChart, resolveKeyTarget, formatBoxStatus, getEc2State, sleepGpuBox, wakeGpuBox, runAdminCli };
|