@yanzc/claude-pro 0.7.0 → 0.7.2
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/.env.example +4 -6
- package/README.md +55 -99
- package/package.json +2 -2
- package/src/agent.js +19 -14
- package/src/api.js +1 -1
- package/src/attach.js +14 -0
- package/src/background.js +102 -12
- package/src/checkpoints.js +4 -2
- package/src/claude-code.js +4 -4
- package/src/commands.js +10 -29
- package/src/config.js +16 -22
- package/src/history.js +69 -0
- package/src/host.js +64 -10
- package/src/main.js +4 -5
- package/src/oauth.js +3 -3
- package/src/print.js +0 -1
- package/src/prompts.js +3 -11
- package/src/ratelimit.js +2 -2
- package/src/runtime.js +2 -6
- package/src/tools/index.js +2 -3
- package/src/tools/interaction.js +2 -30
- package/src/tools/task.js +1 -1
- package/src/ui/ansi.js +0 -1
- package/src/ui/render.js +0 -4
- package/src/ui/repl.js +32 -53
- package/src/modes.js +0 -154
- package/src/qr.js +0 -206
package/src/ui/repl.js
CHANGED
|
@@ -269,10 +269,8 @@ export class App {
|
|
|
269
269
|
const stdin = this.stdin;
|
|
270
270
|
stdin.setRawMode(true);
|
|
271
271
|
// Where the cursor is, before anything is drawn: the input box is kept on the last rows of the window from
|
|
272
|
-
// there (see screen.js).
|
|
273
|
-
|
|
274
|
-
if (pinOff(process.env.CLAUDE_PRO_PIN_INPUT)) this.screen.pin = false;
|
|
275
|
-
else if (stdin.isTTY && this.stdout.isTTY && !this.headless) this.screen.pinFrom(await queryCursorRow(stdin, this.stdout));
|
|
272
|
+
// there (see screen.js).
|
|
273
|
+
if (stdin.isTTY && this.stdout.isTTY && !this.headless) this.screen.pinFrom(await queryCursorRow(stdin, this.stdout));
|
|
276
274
|
stdin.setEncoding('utf8');
|
|
277
275
|
this.stdout.write(ESC.bracketedPasteOn);
|
|
278
276
|
this.watchCopies();
|
|
@@ -292,7 +290,6 @@ export class App {
|
|
|
292
290
|
this.screen.schedule();
|
|
293
291
|
};
|
|
294
292
|
this.commands = buildCommands(this);
|
|
295
|
-
if (pinOff(this.config.env.CLAUDE_PRO_PIN_INPUT)) { this.screen.pin = false; this.screen.pinned = null; }
|
|
296
293
|
// What the session showed before a restart: scrolled back to in fullscreen, repainted after a resize otherwise.
|
|
297
294
|
if (Array.isArray(this.flags.restart?.screenHistory)) this.screen.history = this.flags.restart.screenHistory.filter((l) => typeof l === 'string');
|
|
298
295
|
if (this.fullscreenWanted()) this.screen.setFull(true);
|
|
@@ -306,7 +303,6 @@ export class App {
|
|
|
306
303
|
try { this.onKey(str, key || {}); } catch (e) { this.logError(e); }
|
|
307
304
|
});
|
|
308
305
|
stdin.resume();
|
|
309
|
-
this.runtime.modes.listeners.add(() => this.screen.schedule());
|
|
310
306
|
// Started by a restart of this session: the output of the process before it is still on screen, so no
|
|
311
307
|
// welcome box and no transcript, just a line saying so.
|
|
312
308
|
const restart = this.flags.restart || null;
|
|
@@ -469,7 +465,7 @@ export class App {
|
|
|
469
465
|
id: this.flags.restart?.peerId,
|
|
470
466
|
onMessage: (msg) => this.onPeerMessage(msg),
|
|
471
467
|
getStatus: () => ({ busy: this.working() }),
|
|
472
|
-
info: () => ({ kind: this.headless ? 'background' : 'terminal', remoteId: this.remote?.id || this.flags.remoteId || this.flags.restart?.remote?.id || null, conversationId: this.store.id }),
|
|
468
|
+
info: () => ({ kind: this.headless ? 'background' : 'terminal', remoteId: this.remote?.id || this.flags.remoteId || this.flags.restart?.remote?.id || null, conversationId: this.store.id, attached: this.vterm ? this.vterm.clients.size : 0 }),
|
|
473
469
|
onControl: (body) => this.onControl(body),
|
|
474
470
|
onAttach: this.headless ? (socket, size) => this.attachClient(socket, size) : null,
|
|
475
471
|
}).start();
|
|
@@ -501,13 +497,24 @@ export class App {
|
|
|
501
497
|
}
|
|
502
498
|
|
|
503
499
|
// `claude-pro attach` (see attach.js) on this background session: the screen, drawn whole for it, and its keys.
|
|
504
|
-
// From the client: one JSON object a line, {keys: base64} or {resize: [columns, rows]}.
|
|
500
|
+
// From the client: one JSON object a line, {keys: base64} or {resize: [columns, rows]}. A turn that runs goes on:
|
|
501
|
+
// the client gets the conversation as it is (scrolled back with the wheel, PgUp), the live part and what follows.
|
|
502
|
+
// The registry says how many are attached (the web page shows it, see background.js); one that goes away
|
|
503
|
+
// (detached, its window closed, its process killed) changes nothing else.
|
|
505
504
|
attachClient(socket, { cols, rows } = {}) {
|
|
506
505
|
const client = { write: (d) => { if (!socket.destroyed) socket.write(d); }, close: () => socket.end() };
|
|
507
506
|
socket.on('error', () => { /* its close follows */ });
|
|
508
|
-
|
|
507
|
+
// An HTTP server's sockets stay half open when the other side closes (allowHalfOpen): without this, a client that
|
|
508
|
+
// went away (its window closed, its process killed) would count as attached until a write failed, a minute later.
|
|
509
|
+
socket.on('end', () => socket.destroy());
|
|
510
|
+
socket.on('close', () => {
|
|
511
|
+
this.vterm.detach(client);
|
|
512
|
+
this.peers?.update();
|
|
513
|
+
});
|
|
509
514
|
this.vterm.attach(client, { cols, rows });
|
|
515
|
+
this.peers?.update();
|
|
510
516
|
client.write(`${ATTACH_MODES}${ESC.bracketedPasteOn}`);
|
|
517
|
+
this.screen.title = ''; // its window gets the title too (sent again with the next draw)
|
|
511
518
|
if (this.screen.full) this.screen.repaint();
|
|
512
519
|
let buf = '';
|
|
513
520
|
// (no setEncoding: an HTTP server's socket refuses it; the lines are ASCII anyway, JSON with base64)
|
|
@@ -950,21 +957,21 @@ export class App {
|
|
|
950
957
|
this.screen.schedule();
|
|
951
958
|
}
|
|
952
959
|
|
|
953
|
-
// This session at the gateway, for /status: {text
|
|
960
|
+
// This session at the gateway, for /status: {text}. (Its page is in the web page's list, 远程控制: no link here.)
|
|
954
961
|
remoteState() {
|
|
955
962
|
const r = this.remote;
|
|
956
963
|
if (r) {
|
|
957
964
|
const who = r.account ? ` as ${r.account}` : '';
|
|
958
965
|
const pages = r.viewers ? c.dim(` · ${r.viewers} page${r.viewers === 1 ? '' : 's'} open now`) : '';
|
|
959
966
|
const text = r.online ? `${c.green('online')}${who}${pages}` : r.registered ? c.yellow(`not connected now, retrying${who}`) : `connecting to ${r.base}${who}…`;
|
|
960
|
-
return { text
|
|
967
|
+
return { text };
|
|
961
968
|
}
|
|
962
|
-
if (this.remoteStarting) return { text: 'connecting…'
|
|
969
|
+
if (this.remoteStarting) return { text: 'connecting…' };
|
|
963
970
|
const login = readGatewayLogin(this.config.gatewayUrl);
|
|
964
|
-
if (!login) return { text: c.dim(`offline: this computer is not logged in to the gateway ${this.config.gatewayUrl} (/login)`)
|
|
965
|
-
if (login.apiKey === this.remoteRefused) return { text: c.red('offline: the gateway refused this computer\'s key (/logout, then /login)')
|
|
966
|
-
if (!onlineEnabled(this.config.env)) return { text: c.dim('offline (CLAUDE_PRO_ONLINE=0)')
|
|
967
|
-
return { text: c.dim('offline')
|
|
971
|
+
if (!login) return { text: c.dim(`offline: this computer is not logged in to the gateway ${this.config.gatewayUrl} (/login)`) };
|
|
972
|
+
if (login.apiKey === this.remoteRefused) return { text: c.red('offline: the gateway refused this computer\'s key (/logout, then /login)') };
|
|
973
|
+
if (!onlineEnabled(this.config.env)) return { text: c.dim('offline (CLAUDE_PRO_ONLINE=0)') };
|
|
974
|
+
return { text: c.dim('offline') };
|
|
968
975
|
}
|
|
969
976
|
|
|
970
977
|
// The state the page shows below the transcript; sent whenever it changes.
|
|
@@ -976,7 +983,7 @@ export class App {
|
|
|
976
983
|
if (w) {
|
|
977
984
|
const label = w.label.charAt(0).toUpperCase() + w.label.slice(1);
|
|
978
985
|
rateWait = {
|
|
979
|
-
text: w.waitingForLogin ? 'Waiting for the Claude
|
|
986
|
+
text: w.waitingForLogin ? 'Waiting for the Claude subscription login to be renewed' : `${w.overloaded ? label : `${label} reached`}${w.polling ? ' · checking again' : ` · resumes at ${formatClock(w.resumeAt)}`}`,
|
|
980
987
|
resumeIn: w.resumeAt - Date.now(),
|
|
981
988
|
};
|
|
982
989
|
}
|
|
@@ -990,7 +997,6 @@ export class App {
|
|
|
990
997
|
todos: (this.agent.todos || []).map((t) => ({ content: t.content, status: t.status })),
|
|
991
998
|
queue: this.queue.map((q) => q.display ?? q.text),
|
|
992
999
|
dialog: this.remoteDialog(),
|
|
993
|
-
mode: this.runtime.modes.mode,
|
|
994
1000
|
model,
|
|
995
1001
|
effort: effort || defaultEffort(cfg.mainModel.id, cfg),
|
|
996
1002
|
title: this.sessionLabel() || path.basename(this.cwd),
|
|
@@ -1091,7 +1097,7 @@ export class App {
|
|
|
1091
1097
|
const cfg = this.config;
|
|
1092
1098
|
const [models, efforts] = await Promise.all([modelChoices(cfg), effortChoices(cfg)]);
|
|
1093
1099
|
const { effort } = modelInfo(cfg);
|
|
1094
|
-
return { model: cfg.mainModel.id, models, effort: effort || defaultEffort(cfg.mainModel.id, cfg), efforts: efforts.options
|
|
1100
|
+
return { model: cfg.mainModel.id, models, effort: effort || defaultEffort(cfg.mainModel.id, cfg), efforts: efforts.options };
|
|
1095
1101
|
}
|
|
1096
1102
|
|
|
1097
1103
|
// For the page's input box: what the input box here would suggest (/commands, @files, &sessions), in plain text,
|
|
@@ -1218,7 +1224,6 @@ export class App {
|
|
|
1218
1224
|
modelOverride: cfg.modelOverride,
|
|
1219
1225
|
effortOverride: cfg.effortOverride,
|
|
1220
1226
|
effortBoost: cfg.effortBoost,
|
|
1221
|
-
mode: this.runtime.modes.mode,
|
|
1222
1227
|
todos: this.agent.todos,
|
|
1223
1228
|
draft: this.editor.text,
|
|
1224
1229
|
screenHistory: this.screen.history.slice(-3000),
|
|
@@ -1263,7 +1268,6 @@ export class App {
|
|
|
1263
1268
|
if ('effortOverride' in state) cfg.effortOverride = state.effortOverride || null;
|
|
1264
1269
|
// The Effort tool's level holds for the request the restart continues, not for the user's next one.
|
|
1265
1270
|
if (state.effortBoost) { cfg.effortBoost = state.effortBoost; cfg.effortBoostKeepNext = Boolean(state.continueWith); }
|
|
1266
|
-
if (state.mode === 'plan') this.runtime.modes.setMode('plan');
|
|
1267
1271
|
this.restarts = (Array.isArray(state.restarts) ? state.restarts : []).filter((t) => Number.isFinite(t));
|
|
1268
1272
|
if (resumable) {
|
|
1269
1273
|
this.loadSession(state.sessionFile, state.sessionId, { quiet: true });
|
|
@@ -1421,7 +1425,7 @@ export class App {
|
|
|
1421
1425
|
const model = cfg.mainModel;
|
|
1422
1426
|
const lines = [
|
|
1423
1427
|
c.claude('╭' + '─'.repeat(w - 2) + '╮'),
|
|
1424
|
-
row(`${c.claude('✻')} Welcome to ${c.bold(APP_NAME)}! ${c.dim(`v${VERSION} · a
|
|
1428
|
+
row(`${c.claude('✻')} Welcome to ${c.bold(APP_NAME)}! ${c.dim(`v${VERSION} · a command line coding agent`)}`),
|
|
1425
1429
|
row(''),
|
|
1426
1430
|
row(c.dim(' /help for help, /status for your current setup')),
|
|
1427
1431
|
row(''),
|
|
@@ -1469,13 +1473,13 @@ export class App {
|
|
|
1469
1473
|
return mode === 'fullscreen' && this.stdout.isTTY !== false;
|
|
1470
1474
|
}
|
|
1471
1475
|
|
|
1472
|
-
// Fullscreen: the wheel scrolls the conversation (
|
|
1476
|
+
// Fullscreen: the wheel scrolls the conversation (3 rows a notch); a drag,
|
|
1473
1477
|
// a double or a triple click selects, and what is selected goes to the clipboard (see Screen.mouse).
|
|
1474
1478
|
onMouse(m) {
|
|
1475
1479
|
if (!this.screen.full || this.exiting) return;
|
|
1476
1480
|
if (m.button & 64) {
|
|
1477
1481
|
if (m.button & 128) return;
|
|
1478
|
-
const speed =
|
|
1482
|
+
const speed = 3;
|
|
1479
1483
|
this.screen.scrollBy(m.button & 1 ? -speed : speed);
|
|
1480
1484
|
return;
|
|
1481
1485
|
}
|
|
@@ -1541,7 +1545,6 @@ export class App {
|
|
|
1541
1545
|
}
|
|
1542
1546
|
if (key.ctrl && key.name === 'f') { this.openFind(); return; }
|
|
1543
1547
|
if (this.finder) { this.findKey(str, key); this.screen.schedule(); return; }
|
|
1544
|
-
if (key.name === 'tab' && key.shift) { this.runtime.modes.toggle(); this.screen.schedule(); return; }
|
|
1545
1548
|
// alt+v like Claude Code on Windows (ctrl+v is the terminal's own text paste there).
|
|
1546
1549
|
if ((key.meta || key.ctrl) && key.name === 'v') { this.pasteImage(); return; }
|
|
1547
1550
|
if (key.ctrl && key.name === 'o') {
|
|
@@ -1700,12 +1703,12 @@ export class App {
|
|
|
1700
1703
|
shortcutsHelp() {
|
|
1701
1704
|
const rows = [
|
|
1702
1705
|
['! for bash mode', 'double tap esc to clear input'],
|
|
1703
|
-
['/ for commands', '
|
|
1704
|
-
['@ for file paths, & for other sessions', 'ctrl +
|
|
1706
|
+
['/ for commands', 'ctrl + o to toggle verbose output'],
|
|
1707
|
+
['@ for file paths, & for other sessions', 'ctrl + f to find in the conversation'],
|
|
1705
1708
|
['PgUp/PgDn, wheel to scroll', 'ctrl + home/end to the top/bottom'],
|
|
1706
1709
|
['# to memorize', 'ctrl + t to toggle todos'],
|
|
1707
1710
|
['\\⏎ or alt+⏎ for newline', 'ctrl + l to redraw, ctrl + d to exit'],
|
|
1708
|
-
['alt + v to paste an image', '
|
|
1711
|
+
['alt + v to paste an image', ''],
|
|
1709
1712
|
];
|
|
1710
1713
|
return rows.map(([a, b]) => c.dim(' ' + padEnd(a, 30) + b));
|
|
1711
1714
|
}
|
|
@@ -2157,27 +2160,6 @@ export class App {
|
|
|
2157
2160
|
});
|
|
2158
2161
|
}
|
|
2159
2162
|
|
|
2160
|
-
async approvePlan(plan) {
|
|
2161
|
-
this.flushStreaming();
|
|
2162
|
-
const md = renderMarkdown(plan, { width: this.width - 8 });
|
|
2163
|
-
const max = Math.max(8, this.screen.height - 14);
|
|
2164
|
-
const body = md.length > max ? [...md.slice(0, max), c.dim(`… +${md.length - max} lines (the full plan is printed above)`)] : md;
|
|
2165
|
-
if (md.length > max) this.logLocal(c.plan(`${DOT} Plan:`), ...md.map((l) => ' ' + l), '');
|
|
2166
|
-
const dialog = new SelectDialog({
|
|
2167
|
-
title: 'Ready to code?',
|
|
2168
|
-
body: ["Here is the plan:", '', ...body],
|
|
2169
|
-
question: 'Would you like to proceed?',
|
|
2170
|
-
color: c.plan,
|
|
2171
|
-
cancelValue: 'reject',
|
|
2172
|
-
options: [
|
|
2173
|
-
{ label: 'Yes, start coding', value: 'full' },
|
|
2174
|
-
{ label: 'No, keep planning', value: 'reject' },
|
|
2175
|
-
],
|
|
2176
|
-
});
|
|
2177
|
-
dialog.markdown = plan; // the remote page shows the whole plan
|
|
2178
|
-
return await this.showDialog(dialog);
|
|
2179
|
-
}
|
|
2180
|
-
|
|
2181
2163
|
async askQuestion(q) {
|
|
2182
2164
|
const answer = await this.showDialog(new SelectDialog({
|
|
2183
2165
|
title: q.header || 'Question',
|
|
@@ -2384,7 +2366,6 @@ export class App {
|
|
|
2384
2366
|
}
|
|
2385
2367
|
};
|
|
2386
2368
|
return {
|
|
2387
|
-
approvePlan: async () => 'reject',
|
|
2388
2369
|
askQuestion: async () => null,
|
|
2389
2370
|
notice: (text, kind) => app.notice(`[${def.name}] ${text}`, kind),
|
|
2390
2371
|
toolStart(tu, tool, input, agent) {
|
|
@@ -2472,7 +2453,7 @@ export class App {
|
|
|
2472
2453
|
const w = this.rateWait;
|
|
2473
2454
|
const left = humanDuration(w.resumeAt - Date.now()); // the same wording as the notice above it
|
|
2474
2455
|
const esc = c.dim(`(${this.controller?.signal.aborted ? 'cancelling…' : 'esc to cancel'})`);
|
|
2475
|
-
if (w.waitingForLogin) return c.yellow(`${frame} Waiting for the Claude
|
|
2456
|
+
if (w.waitingForLogin) return c.yellow(`${frame} Waiting for the Claude subscription login to be renewed · checking again in ${left}`) + ' ' + esc;
|
|
2476
2457
|
const label = w.label.charAt(0).toUpperCase() + w.label.slice(1);
|
|
2477
2458
|
const when = w.polling ? `checking again in ${left}` : `resumes at ${formatClock(w.resumeAt)} (in ${left})`;
|
|
2478
2459
|
return c.yellow(`${frame} ${w.overloaded ? label : `${label} reached`} · ${when}`) + ' ' + esc;
|
|
@@ -2582,13 +2563,11 @@ export class App {
|
|
|
2582
2563
|
}
|
|
2583
2564
|
|
|
2584
2565
|
footer(w) {
|
|
2585
|
-
const mode = this.runtime.modes.mode;
|
|
2586
2566
|
let left;
|
|
2587
2567
|
const flash = this.flashMessage && Date.now() < this.flashMessage.until ? this.flashMessage : null;
|
|
2588
2568
|
if (this.exitArmed && Date.now() - this.exitArmed < 1500) left = c.dim(' Press Ctrl-C again to exit');
|
|
2589
2569
|
else if (flash) left = (flash.kind === 'warn' ? c.yellow : c.dim)(' ' + truncate(flash.text, w - 4));
|
|
2590
2570
|
else if (this.restartRequest) left = c.dim(' ↻ restarting when this turn ends');
|
|
2591
|
-
else if (mode === 'plan') left = ' ' + c.plan('⏸ plan mode on (read-only)') + c.dim(' (shift+tab to leave)');
|
|
2592
2571
|
else if (this.editor.mode === 'bash') left = c.magenta(' ! for bash mode');
|
|
2593
2572
|
else if (this.editor.mode === 'memory') left = c.blue(' # to memorize');
|
|
2594
2573
|
else left = c.dim(' ? for shortcuts');
|
package/src/modes.js
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
// Session mode. There are no permission prompts or rules: every tool call runs right away.
|
|
2
|
-
// The only switch is plan mode, an opt-in read-only phase for investigating and proposing a
|
|
3
|
-
// plan before changing anything (shift+tab or --plan turns it on; ExitPlanMode turns it off).
|
|
4
|
-
|
|
5
|
-
export const MODES = ['full', 'plan'];
|
|
6
|
-
|
|
7
|
-
// Split a shell command into simple commands on && || ; | and newlines (quote aware).
|
|
8
|
-
export function splitCommand(cmd) {
|
|
9
|
-
const parts = [];
|
|
10
|
-
let cur = '', quote = null;
|
|
11
|
-
for (let i = 0; i < cmd.length; i++) {
|
|
12
|
-
const ch = cmd[i];
|
|
13
|
-
if (quote) {
|
|
14
|
-
cur += ch;
|
|
15
|
-
if (ch === '\\' && quote === '"' && i + 1 < cmd.length) { cur += cmd[++i]; continue; }
|
|
16
|
-
if (ch === quote) quote = null;
|
|
17
|
-
continue;
|
|
18
|
-
}
|
|
19
|
-
if (ch === '"' || ch === "'") { quote = ch; cur += ch; continue; }
|
|
20
|
-
if (ch === '\\' && i + 1 < cmd.length) { cur += ch + cmd[++i]; continue; }
|
|
21
|
-
const two = cmd.slice(i, i + 2);
|
|
22
|
-
if (two === '&&' || two === '||') { parts.push(cur); cur = ''; i++; continue; }
|
|
23
|
-
if (ch === ';' || ch === '|' || ch === '\n') { parts.push(cur); cur = ''; continue; }
|
|
24
|
-
if (ch === '&' && cmd[i + 1] !== '>' && cmd[i - 1] !== '>') { parts.push(cur); cur = ''; continue; }
|
|
25
|
-
cur += ch;
|
|
26
|
-
}
|
|
27
|
-
parts.push(cur);
|
|
28
|
-
return parts.map((p) => p.trim()).filter(Boolean);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function stripQuotedStrings(s) {
|
|
32
|
-
return s.replace(/'[^']*'/g, "''").replace(/"(?:\\.|[^"\\])*"/g, '""');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Syntax we can't classify: command substitution, redirection to files, etc.
|
|
36
|
-
function hasUnclassifiableSyntax(cmd) {
|
|
37
|
-
const s = stripQuotedStrings(cmd);
|
|
38
|
-
if (/\$\(|`|<\(|>\(/.test(s)) return true;
|
|
39
|
-
// Redirections other than to /dev/null or fd duplication.
|
|
40
|
-
const redir = s.replace(/\d?>&\d/g, '').replace(/&?>>?\s*\/dev\/null/g, '').replace(/\d?>>?\s*\/dev\/null/g, '');
|
|
41
|
-
return /[<>]/.test(redir);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Commands that read only, whatever their arguments (the ones whose arguments can make them write or run
|
|
45
|
-
// something have their own check in COMMAND_CHECKS below). No pagers: `less +!cmd` runs a command.
|
|
46
|
-
const READ_ONLY_SIMPLE = new Set([
|
|
47
|
-
'ls', 'pwd', 'echo', 'cat', 'head', 'tail', 'wc', 'which', 'whoami', 'stat', 'du', 'df',
|
|
48
|
-
'uname', 'printenv', 'type', 'realpath', 'basename', 'dirname', 'cut', 'tr', 'diff',
|
|
49
|
-
'grep', 'egrep', 'fgrep', 'true', 'false', 'test', 'nproc', 'id', 'cd', 'column', 'jq', 'md5sum', 'sha256sum',
|
|
50
|
-
]);
|
|
51
|
-
const READ_ONLY_PREFIXES = [
|
|
52
|
-
'git status', 'git rev-parse', 'git ls-files', 'git blame', 'git describe', 'git stash list',
|
|
53
|
-
'git config --get', 'git config -l', 'git config --list',
|
|
54
|
-
'node --version', 'node -v', 'npm --version', 'npm -v', 'npm ls', 'npm list', 'npm view', 'npm outdated', 'pnpm --version',
|
|
55
|
-
'yarn --version', 'python --version', 'python3 --version', 'pip list', 'pip show', 'pip --version', 'go version',
|
|
56
|
-
'cargo --version', 'rustc --version', 'java -version', 'dotnet --version', 'docker ps', 'docker images', 'where',
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
// `git branch` only lists with these flags; a name, -d/-D, -m/-M, -c/-C, -u ... would create, delete or rename.
|
|
60
|
-
const GIT_BRANCH_FLAGS = new Set(['-a', '--all', '-r', '--remotes', '-v', '-vv', '--verbose', '-l', '--list', '--show-current', '--no-color', '--color', '--no-column', '--column']);
|
|
61
|
-
const GIT_BRANCH_VALUE_FLAGS = /^--(?:contains|no-contains|merged|no-merged|points-at|sort|format|abbrev)(?:=.*)?$/;
|
|
62
|
-
function gitBranchReads(args) {
|
|
63
|
-
let listing = false;
|
|
64
|
-
for (let i = 0; i < args.length; i++) {
|
|
65
|
-
const a = args[i];
|
|
66
|
-
if (GIT_BRANCH_FLAGS.has(a)) { if (a === '-l' || a === '--list') listing = true; continue; }
|
|
67
|
-
if (GIT_BRANCH_VALUE_FLAGS.test(a)) { if (!a.includes('=') && !/^--abbrev$/.test(a)) i++; continue; }
|
|
68
|
-
if (listing && !a.startsWith('-')) continue; // a pattern to list
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// `git tag` likewise: listing is fine, anything that makes or removes a tag is not.
|
|
75
|
-
function gitTagReads(args) {
|
|
76
|
-
if (!args.some((a) => a === '-l' || a === '--list')) return false;
|
|
77
|
-
return args.every((a) => !a.startsWith('-') || /^(?:-l|--list|-n\d*|--sort=.*|--column|--no-column|--contains|--no-contains|--merged|--no-merged|--points-at|--format=.*|--color|--no-color)$/.test(a));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const positional = (args) => args.filter((a) => !a.startsWith('-') && !a.startsWith('+'));
|
|
81
|
-
|
|
82
|
-
// Commands that are read-only only with certain arguments: args => true when this call reads only.
|
|
83
|
-
const COMMAND_CHECKS = {
|
|
84
|
-
// -exec and -delete run or remove things, -fprint* and -fls write files
|
|
85
|
-
find: (args) => !args.some((a) => /^-(?:exec|execdir|ok|okdir|delete|fprint0?|fprintf|fls)$/.test(a)),
|
|
86
|
-
// -o FILE and --output FILE (GNU getopt accepts any unambiguous prefix of it)
|
|
87
|
-
sort: (args) => !args.some((a) => /^-[a-zA-Z]*o/.test(a) || /^--o/.test(a)),
|
|
88
|
-
// `uniq INPUT OUTPUT` writes OUTPUT
|
|
89
|
-
uniq: (args) => positional(args).length <= 1,
|
|
90
|
-
tree: (args) => !args.some((a) => /^-[a-zA-Z]*o/.test(a) || /^--output/.test(a)),
|
|
91
|
-
// setting the clock or the host name changes the machine
|
|
92
|
-
date: (args) => !args.some((a) => /^-[a-zA-Z]*s/.test(a) || /^--set/.test(a)) && positional(args).length === 0,
|
|
93
|
-
hostname: (args) => args.length === 0 || args.every((a) => /^(?:-[fsiIdAy]|--(?:fqdn|short|ip-address|domain|all-ip-addresses))$/.test(a)),
|
|
94
|
-
// --pre runs a command on every file
|
|
95
|
-
rg: (args) => !args.some((a) => /^--(?:pre|hostname-bin)(?:=|$)/.test(a)),
|
|
96
|
-
file: (args) => !args.some((a) => /^-[a-zA-Z]*C/.test(a) || a === '--compile'),
|
|
97
|
-
// `git log --output=FILE` and friends write the result to a file
|
|
98
|
-
git: (args, part) => {
|
|
99
|
-
const sub = args.find((a) => !a.startsWith('-'));
|
|
100
|
-
const rest = args.slice(args.indexOf(sub) + 1);
|
|
101
|
-
if (args[0] !== sub) return null; // options before the subcommand (-C dir, -c key=value): not classified
|
|
102
|
-
if (sub === 'diff' || sub === 'log' || sub === 'show') return !rest.some((a) => /^--output(?:=|$)/.test(a));
|
|
103
|
-
if (sub === 'branch') return gitBranchReads(rest);
|
|
104
|
-
if (sub === 'tag') return gitTagReads(rest);
|
|
105
|
-
if (sub === 'remote') return part === 'git remote' || part === 'git remote -v';
|
|
106
|
-
return null;
|
|
107
|
-
},
|
|
108
|
-
// `go env -w` and `-u` change the Go settings
|
|
109
|
-
go: (args) => (args[0] === 'env' ? !args.some((a) => a === '-w' || a === '-u') : args[0] === 'version'),
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
// True when every part of the command only reads (used to let research commands run in plan mode).
|
|
113
|
-
export function isReadOnlyCommand(cmd) {
|
|
114
|
-
if (hasUnclassifiableSyntax(cmd)) return false;
|
|
115
|
-
return splitCommand(cmd).every((part) => {
|
|
116
|
-
const words = part.split(/\s+/);
|
|
117
|
-
const first = words[0];
|
|
118
|
-
if (/^\w+=/.test(first)) return false; // env assignment prefix
|
|
119
|
-
// hasOwn: a command called `constructor` or `toString` must not find a function on the prototype.
|
|
120
|
-
if (Object.hasOwn(COMMAND_CHECKS, first)) {
|
|
121
|
-
const verdict = COMMAND_CHECKS[first](words.slice(1), part);
|
|
122
|
-
if (verdict !== null) return verdict;
|
|
123
|
-
}
|
|
124
|
-
if (READ_ONLY_SIMPLE.has(first)) return true;
|
|
125
|
-
// A command that has an argument check is only ever read-only through that check, except where it
|
|
126
|
-
// is also in the simple list (above); the rest is matched by prefix.
|
|
127
|
-
return READ_ONLY_PREFIXES.some((p) => part === p || part.startsWith(`${p} `));
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export class ModeManager {
|
|
132
|
-
constructor(mode = 'full') {
|
|
133
|
-
this.mode = MODES.includes(mode) ? mode : 'full';
|
|
134
|
-
this.listeners = new Set();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
setMode(mode) {
|
|
138
|
-
if (!MODES.includes(mode)) return;
|
|
139
|
-
this.mode = mode;
|
|
140
|
-
for (const l of this.listeners) l(mode);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
toggle() {
|
|
144
|
-
this.setMode(this.mode === 'plan' ? 'full' : 'plan');
|
|
145
|
-
return this.mode;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// {behavior: 'allow'} or, in plan mode for anything that changes state, {behavior: 'deny', message}.
|
|
149
|
-
check(tool, input = {}) {
|
|
150
|
-
if (this.mode !== 'plan' || tool.readOnly) return { behavior: 'allow' };
|
|
151
|
-
if (tool.name === 'Bash' && isReadOnlyCommand(String(input.command || ''))) return { behavior: 'allow' };
|
|
152
|
-
return { behavior: 'deny', message: 'Plan mode is active: do not make changes or run commands that modify anything yet. Keep researching with read-only tools, then present your plan with ExitPlanMode.' };
|
|
153
|
-
}
|
|
154
|
-
}
|
package/src/qr.js
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
// QR codes in the terminal: /status prints one of the session's page, so that a phone opens it by pointing its camera
|
|
2
|
-
// at the screen. Byte mode, error correction level L, versions 1-6 (up to 134 bytes, which a URL with a token
|
|
3
|
-
// fits in easily); the mask is chosen with the standard penalty rules.
|
|
4
|
-
|
|
5
|
-
// [EC codewords per block, blocks, data codewords per block] at level L, by version.
|
|
6
|
-
const EC_L = [null, [7, 1, 19], [10, 1, 34], [15, 1, 55], [20, 1, 80], [26, 1, 108], [18, 2, 68]];
|
|
7
|
-
const MAX_VERSION = 6;
|
|
8
|
-
|
|
9
|
-
// GF(256) with the polynomial 0x11d.
|
|
10
|
-
const EXP = new Uint8Array(512);
|
|
11
|
-
const LOG = new Uint8Array(256);
|
|
12
|
-
for (let i = 0, x = 1; i < 255; i++) {
|
|
13
|
-
EXP[i] = x;
|
|
14
|
-
LOG[x] = i;
|
|
15
|
-
x <<= 1;
|
|
16
|
-
if (x & 0x100) x ^= 0x11d;
|
|
17
|
-
}
|
|
18
|
-
for (let i = 255; i < 512; i++) EXP[i] = EXP[i - 255];
|
|
19
|
-
const mul = (a, b) => (a && b ? EXP[LOG[a] + LOG[b]] : 0);
|
|
20
|
-
|
|
21
|
-
function rsRemainder(data, n) {
|
|
22
|
-
let gen = [1]; // (x - a^0)(x - a^1)...(x - a^(n-1)), highest degree first
|
|
23
|
-
for (let i = 0; i < n; i++) {
|
|
24
|
-
const next = new Array(gen.length + 1).fill(0);
|
|
25
|
-
for (let j = 0; j < gen.length; j++) { next[j] ^= gen[j]; next[j + 1] ^= mul(gen[j], EXP[i]); }
|
|
26
|
-
gen = next;
|
|
27
|
-
}
|
|
28
|
-
const rem = new Array(n).fill(0);
|
|
29
|
-
for (const byte of data) {
|
|
30
|
-
const factor = byte ^ rem.shift();
|
|
31
|
-
rem.push(0);
|
|
32
|
-
for (let i = 0; i < n; i++) rem[i] ^= mul(gen[i + 1], factor);
|
|
33
|
-
}
|
|
34
|
-
return rem;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function codewords(bytes, version) {
|
|
38
|
-
const [ecLen, blocks, dataLen] = EC_L[version];
|
|
39
|
-
const capacity = blocks * dataLen;
|
|
40
|
-
const bits = [];
|
|
41
|
-
const put = (value, len) => { for (let i = len - 1; i >= 0; i--) bits.push((value >>> i) & 1); };
|
|
42
|
-
put(0b0100, 4);
|
|
43
|
-
put(bytes.length, 8);
|
|
44
|
-
for (const b of bytes) put(b, 8);
|
|
45
|
-
put(0, Math.min(4, capacity * 8 - bits.length));
|
|
46
|
-
while (bits.length % 8) bits.push(0);
|
|
47
|
-
const data = [];
|
|
48
|
-
for (let i = 0; i < bits.length; i += 8) data.push(bits.slice(i, i + 8).reduce((v, b) => (v << 1) | b, 0));
|
|
49
|
-
for (let pad = 0xec; data.length < capacity; pad ^= 0xec ^ 0x11) data.push(pad);
|
|
50
|
-
const dataBlocks = [];
|
|
51
|
-
const ecBlocks = [];
|
|
52
|
-
for (let b = 0; b < blocks; b++) {
|
|
53
|
-
const d = data.slice(b * dataLen, (b + 1) * dataLen);
|
|
54
|
-
dataBlocks.push(d);
|
|
55
|
-
ecBlocks.push(rsRemainder(d, ecLen));
|
|
56
|
-
}
|
|
57
|
-
const out = [];
|
|
58
|
-
for (let i = 0; i < dataLen; i++) for (const d of dataBlocks) out.push(d[i]);
|
|
59
|
-
for (let i = 0; i < ecLen; i++) for (const e of ecBlocks) out.push(e[i]);
|
|
60
|
-
return out;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const MASKS = [
|
|
64
|
-
(x, y) => (x + y) % 2 === 0,
|
|
65
|
-
(x, y) => y % 2 === 0,
|
|
66
|
-
(x) => x % 3 === 0,
|
|
67
|
-
(x, y) => (x + y) % 3 === 0,
|
|
68
|
-
(x, y) => (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0,
|
|
69
|
-
(x, y) => ((x * y) % 2) + ((x * y) % 3) === 0,
|
|
70
|
-
(x, y) => (((x * y) % 2) + ((x * y) % 3)) % 2 === 0,
|
|
71
|
-
(x, y) => (((x + y) % 2) + ((x * y) % 3)) % 2 === 0,
|
|
72
|
-
];
|
|
73
|
-
|
|
74
|
-
function penalty(m) {
|
|
75
|
-
const n = m.length;
|
|
76
|
-
let score = 0;
|
|
77
|
-
let dark = 0;
|
|
78
|
-
const lines = [];
|
|
79
|
-
for (let i = 0; i < n; i++) {
|
|
80
|
-
lines.push(m[i]);
|
|
81
|
-
lines.push(m.map((row) => row[i]));
|
|
82
|
-
}
|
|
83
|
-
for (const line of lines) {
|
|
84
|
-
for (let i = 0; i < n;) {
|
|
85
|
-
let j = i;
|
|
86
|
-
while (j < n && line[j] === line[i]) j++;
|
|
87
|
-
if (j - i >= 5) score += 3 + (j - i - 5);
|
|
88
|
-
i = j;
|
|
89
|
-
}
|
|
90
|
-
const s = line.map((v) => (v ? 1 : 0)).join('');
|
|
91
|
-
for (const p of ['10111010000', '00001011101']) for (let k = s.indexOf(p); k >= 0; k = s.indexOf(p, k + 1)) score += 40;
|
|
92
|
-
}
|
|
93
|
-
for (let y = 0; y < n; y++) {
|
|
94
|
-
for (let x = 0; x < n; x++) {
|
|
95
|
-
if (m[y][x]) dark++;
|
|
96
|
-
if (x < n - 1 && y < n - 1 && m[y][x] === m[y][x + 1] && m[y][x] === m[y + 1][x] && m[y][x] === m[y + 1][x + 1]) score += 3;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return score + 10 * Math.floor(Math.abs((dark * 100) / (n * n) - 50) / 5);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// The modules of the QR code for `text` (rows of booleans, true = dark), or null when it is too long.
|
|
103
|
-
// `mask`: use this mask (0-7) instead of the best one.
|
|
104
|
-
export function qrMatrix(text, { mask: forcedMask } = {}) {
|
|
105
|
-
const bytes = [...Buffer.from(String(text), 'utf8')];
|
|
106
|
-
let version = 1;
|
|
107
|
-
while (version <= MAX_VERSION && EC_L[version][1] * EC_L[version][2] * 8 < 12 + 8 * bytes.length) version++;
|
|
108
|
-
if (version > MAX_VERSION) return null;
|
|
109
|
-
const size = 17 + 4 * version;
|
|
110
|
-
const base = Array.from({ length: size }, () => new Array(size).fill(false));
|
|
111
|
-
const fn = Array.from({ length: size }, () => new Array(size).fill(false));
|
|
112
|
-
const set = (m, x, y, dark) => { m[y][x] = dark; fn[y][x] = true; };
|
|
113
|
-
|
|
114
|
-
for (const [cx, cy] of [[3, 3], [size - 4, 3], [3, size - 4]]) {
|
|
115
|
-
for (let dy = -4; dy <= 4; dy++) {
|
|
116
|
-
for (let dx = -4; dx <= 4; dx++) {
|
|
117
|
-
const x = cx + dx;
|
|
118
|
-
const y = cy + dy;
|
|
119
|
-
if (x < 0 || y < 0 || x >= size || y >= size) continue;
|
|
120
|
-
const d = Math.max(Math.abs(dx), Math.abs(dy));
|
|
121
|
-
set(base, x, y, d !== 2 && d !== 4);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
for (let i = 0; i < size; i++) {
|
|
126
|
-
if (!fn[6][i]) set(base, i, 6, i % 2 === 0);
|
|
127
|
-
if (!fn[i][6]) set(base, 6, i, i % 2 === 0);
|
|
128
|
-
}
|
|
129
|
-
if (version >= 2) {
|
|
130
|
-
const c = size - 7;
|
|
131
|
-
for (let dy = -2; dy <= 2; dy++) for (let dx = -2; dx <= 2; dx++) set(base, c + dx, c + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1);
|
|
132
|
-
}
|
|
133
|
-
const drawFormat = (m, mask) => {
|
|
134
|
-
const data = (1 << 3) | mask; // level L = 01
|
|
135
|
-
let rem = data;
|
|
136
|
-
for (let i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
|
|
137
|
-
const bits = ((data << 10) | rem) ^ 0x5412;
|
|
138
|
-
const bit = (i) => ((bits >>> i) & 1) === 1;
|
|
139
|
-
for (let i = 0; i <= 5; i++) set(m, 8, i, bit(i));
|
|
140
|
-
set(m, 8, 7, bit(6));
|
|
141
|
-
set(m, 8, 8, bit(7));
|
|
142
|
-
set(m, 7, 8, bit(8));
|
|
143
|
-
for (let i = 9; i < 15; i++) set(m, 14 - i, 8, bit(i));
|
|
144
|
-
for (let i = 0; i < 8; i++) set(m, size - 1 - i, 8, bit(i));
|
|
145
|
-
for (let i = 8; i < 15; i++) set(m, 8, size - 15 + i, bit(i));
|
|
146
|
-
set(m, 8, size - 8, true);
|
|
147
|
-
};
|
|
148
|
-
drawFormat(base, 0); // reserves the format areas before the data goes in
|
|
149
|
-
|
|
150
|
-
const data = codewords(bytes, version);
|
|
151
|
-
let bitIndex = 0;
|
|
152
|
-
for (let right = size - 1; right >= 1; right -= 2) {
|
|
153
|
-
if (right === 6) right = 5;
|
|
154
|
-
for (let vert = 0; vert < size; vert++) {
|
|
155
|
-
for (let j = 0; j < 2; j++) {
|
|
156
|
-
const x = right - j;
|
|
157
|
-
const upward = ((right + 1) & 2) === 0;
|
|
158
|
-
const y = upward ? size - 1 - vert : vert;
|
|
159
|
-
if (fn[y][x]) continue;
|
|
160
|
-
if (bitIndex < data.length * 8) base[y][x] = ((data[bitIndex >>> 3] >>> (7 - (bitIndex & 7))) & 1) === 1;
|
|
161
|
-
bitIndex++;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
let best = null;
|
|
167
|
-
for (let mask = 0; mask < 8; mask++) {
|
|
168
|
-
if (forcedMask !== undefined && mask !== forcedMask) continue;
|
|
169
|
-
const m = base.map((row, y) => row.map((v, x) => (fn[y][x] ? v : v !== MASKS[mask](x, y))));
|
|
170
|
-
drawFormat(m, mask);
|
|
171
|
-
const score = forcedMask !== undefined ? 0 : penalty(m);
|
|
172
|
-
if (!best || score < best.score) best = { m, score };
|
|
173
|
-
}
|
|
174
|
-
return best.m;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// The QR code as terminal lines, two rows of modules per line. With colors it is drawn black on white whatever the
|
|
178
|
-
// terminal's theme; without, it assumes a dark background (light modules are the drawn ones). Null when too long.
|
|
179
|
-
export function qrLines(text, { color = true, quiet = 2 } = {}) {
|
|
180
|
-
const m = qrMatrix(text);
|
|
181
|
-
if (!m) return null;
|
|
182
|
-
const n = m.length + quiet * 2;
|
|
183
|
-
const dark = (x, y) => {
|
|
184
|
-
const mx = x - quiet;
|
|
185
|
-
const my = y - quiet;
|
|
186
|
-
return mx >= 0 && my >= 0 && mx < m.length && my < m.length && m[my][mx];
|
|
187
|
-
};
|
|
188
|
-
const lines = [];
|
|
189
|
-
for (let y = 0; y < n; y += 2) {
|
|
190
|
-
let line = '';
|
|
191
|
-
let last = null;
|
|
192
|
-
for (let x = 0; x < n; x++) {
|
|
193
|
-
const top = dark(x, y);
|
|
194
|
-
const bottom = y + 1 < n && dark(x, y + 1);
|
|
195
|
-
if (color) {
|
|
196
|
-
const sgr = `\x1b[${top ? 30 : 97};${bottom ? 40 : 107}m`;
|
|
197
|
-
if (sgr !== last) { line += sgr; last = sgr; }
|
|
198
|
-
line += '▀';
|
|
199
|
-
} else {
|
|
200
|
-
line += !top && !bottom ? '█' : !top ? '▀' : !bottom ? '▄' : ' ';
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
lines.push(color ? `${line}\x1b[0m` : line);
|
|
204
|
-
}
|
|
205
|
-
return lines;
|
|
206
|
-
}
|