@bolt-cli/cli 0.0.0-stage → 0.13.0
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/LICENSE +75 -0
- package/README.md +155 -2
- package/bin/bolt.js +21 -0
- package/lib/agent.js +361 -0
- package/lib/api.js +246 -0
- package/lib/bootstrap.js +292 -0
- package/lib/browser.js +152 -0
- package/lib/cancel.js +153 -0
- package/lib/checkpoints.js +67 -0
- package/lib/cli.js +2637 -0
- package/lib/completion.js +77 -0
- package/lib/config.js +80 -0
- package/lib/doctor.js +110 -0
- package/lib/export.js +77 -0
- package/lib/history.js +78 -0
- package/lib/memory.js +80 -0
- package/lib/output.js +88 -0
- package/lib/permissions.js +167 -0
- package/lib/present.js +181 -0
- package/lib/redact.js +56 -0
- package/lib/session.js +153 -0
- package/lib/toolparse.js +752 -0
- package/lib/tools.js +606 -0
- package/lib/ui.js +783 -0
- package/lib/updatecheck.js +114 -0
- package/lib/watch.js +698 -0
- package/package.json +36 -6
- package/templates/README.md +44 -0
- package/templates/aslr-offset-probe.sh +96 -0
- package/templates/crash-offset-pattern.sh +81 -0
package/lib/cancel.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Run cancellation: Esc (or Ctrl-C) aborts the in-flight generation instead
|
|
3
|
+
// of forcing the operator to kill the terminal.
|
|
4
|
+
//
|
|
5
|
+
// Why raw mode: in cooked mode the terminal only delivers input on Enter, so
|
|
6
|
+
// a lone Esc would never arrive until the run is over. While attached we put
|
|
7
|
+
// stdin in raw mode and watch for Esc / Ctrl-C bytes, then abort the shared
|
|
8
|
+
// AbortController whose signal is passed to fetch. Other bytes are ignored —
|
|
9
|
+
// the readline (paused during a run) still receives them through its own
|
|
10
|
+
// listener, so typeahead is preserved.
|
|
11
|
+
//
|
|
12
|
+
// Usage:
|
|
13
|
+
// const cancel = createCanceller();
|
|
14
|
+
// cancel.attach();
|
|
15
|
+
// try {
|
|
16
|
+
// await api.streamGenerate({ ..., signal: cancel.signal });
|
|
17
|
+
// } finally {
|
|
18
|
+
// cancel.detach();
|
|
19
|
+
// }
|
|
20
|
+
// Then check `cancel.cancelled` / catch an error with code "CANCELLED".
|
|
21
|
+
|
|
22
|
+
// Whether an Esc watcher can work here at all. Terminals without raw mode
|
|
23
|
+
// (mintty/Git Bash with piped stdout, dumb terminals) only deliver input on
|
|
24
|
+
// Enter, so no watcher — and no UI text — should promise Esc. Side-effect
|
|
25
|
+
// free: callers probe this before choosing labels and hints.
|
|
26
|
+
function canWatchKeys() {
|
|
27
|
+
try {
|
|
28
|
+
return Boolean(process.stdin.isTTY) && typeof process.stdin.setRawMode === "function";
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function createCanceller() {
|
|
35
|
+
const controller = new AbortController();
|
|
36
|
+
let attached = false;
|
|
37
|
+
let wasRaw = false;
|
|
38
|
+
let handler = null;
|
|
39
|
+
|
|
40
|
+
function onData(chunk) {
|
|
41
|
+
const s = chunk.toString("utf8");
|
|
42
|
+
// Esc alone, Esc-prefixed sequences, or Ctrl-C.
|
|
43
|
+
if (s.includes("") || s === "") {
|
|
44
|
+
try {
|
|
45
|
+
controller.abort();
|
|
46
|
+
} catch {
|
|
47
|
+
// abort() never throws in practice; cancellation must not crash.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function attach() {
|
|
53
|
+
if (attached || !canWatchKeys()) return false;
|
|
54
|
+
handler = onData;
|
|
55
|
+
try {
|
|
56
|
+
wasRaw = Boolean(process.stdin.isRaw);
|
|
57
|
+
if (typeof process.stdin.setRawMode === "function" && !process.stdin.isRaw) {
|
|
58
|
+
process.stdin.setRawMode(true);
|
|
59
|
+
}
|
|
60
|
+
process.stdin.resume();
|
|
61
|
+
process.stdin.on("data", handler);
|
|
62
|
+
attached = true;
|
|
63
|
+
return true;
|
|
64
|
+
} catch {
|
|
65
|
+
attached = false;
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function detach() {
|
|
71
|
+
if (!attached) return;
|
|
72
|
+
try {
|
|
73
|
+
if (handler) process.stdin.removeListener("data", handler);
|
|
74
|
+
} catch {
|
|
75
|
+
// Removing a listener must never break the run teardown.
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
if (typeof process.stdin.setRawMode === "function" && Boolean(process.stdin.isRaw) !== wasRaw) {
|
|
79
|
+
process.stdin.setRawMode(wasRaw);
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
// Restoring raw mode is best-effort; the REPL resets its own prompt.
|
|
83
|
+
}
|
|
84
|
+
attached = false;
|
|
85
|
+
handler = null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
signal: controller.signal,
|
|
90
|
+
get cancelled() {
|
|
91
|
+
return controller.signal.aborted;
|
|
92
|
+
},
|
|
93
|
+
abort() {
|
|
94
|
+
try {
|
|
95
|
+
controller.abort();
|
|
96
|
+
} catch {
|
|
97
|
+
// ignore
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
attach,
|
|
101
|
+
detach,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isCancelError(error) {
|
|
106
|
+
if (!error) return false;
|
|
107
|
+
if (error.code === "CANCELLED") return true;
|
|
108
|
+
if (error.name === "AbortError") return true;
|
|
109
|
+
return /cancelled by operator|operation was aborted|aborted/i.test(error.message || "");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function cancelError() {
|
|
113
|
+
const error = new Error("Cancelled — session saved. Press Esc during a run to stop it; close the terminal only to quit entirely.");
|
|
114
|
+
error.code = "CANCELLED";
|
|
115
|
+
return error;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Abort-aware sleep for advised waits (rate-limit retries). Ticks onTick()
|
|
119
|
+
// with whole seconds remaining (for countdown lines); an abort rejects with
|
|
120
|
+
// CANCELLED instead of resolving. Zero/negative waits resolve at once.
|
|
121
|
+
function sleep(ms, { signal, onTick } = {}) {
|
|
122
|
+
const total = Math.max(0, Number(ms) || 0);
|
|
123
|
+
if (total <= 0) return Promise.resolve();
|
|
124
|
+
if (signal && signal.aborted) return Promise.reject(cancelError());
|
|
125
|
+
return new Promise((resolve, reject) => {
|
|
126
|
+
let elapsed = 0;
|
|
127
|
+
const notify = () => {
|
|
128
|
+
try {
|
|
129
|
+
if (typeof onTick === "function") onTick(Math.max(0, Math.ceil((total - elapsed) / 1000)));
|
|
130
|
+
} catch {
|
|
131
|
+
// Countdown rendering must never break the wait.
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const onAbort = () => {
|
|
135
|
+
clearInterval(timer);
|
|
136
|
+
reject(cancelError());
|
|
137
|
+
};
|
|
138
|
+
const timer = setInterval(() => {
|
|
139
|
+
elapsed += 1000;
|
|
140
|
+
if (elapsed >= total) {
|
|
141
|
+
clearInterval(timer);
|
|
142
|
+
if (signal) signal.removeEventListener("abort", onAbort);
|
|
143
|
+
resolve();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
notify();
|
|
147
|
+
}, 1000);
|
|
148
|
+
if (signal) signal.addEventListener("abort", onAbort, { once: true });
|
|
149
|
+
notify();
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = { createCanceller, isCancelError, cancelError, canWatchKeys, sleep };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// File checkpoints: every write/edit backs the original up first so the
|
|
3
|
+
// operator can undo. Backups live under ~/.bolt/backups as
|
|
4
|
+
// <relpath-with-__>/<timestamp>.bak (mode 600, mode-700 dirs). Never throws:
|
|
5
|
+
// checkpointing is safety net, not a crash path. `home` override exists for
|
|
6
|
+
// tests so the real homedir is untouched.
|
|
7
|
+
const crypto = require("crypto");
|
|
8
|
+
const fs = require("fs");
|
|
9
|
+
const os = require("os");
|
|
10
|
+
const path = require("path");
|
|
11
|
+
|
|
12
|
+
function baseDir(home) {
|
|
13
|
+
return path.join(home || os.homedir(), ".bolt", "backups");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function safeName(rel) {
|
|
17
|
+
return String(rel || "file").replace(/\\/g, "/").replace(/^\//, "").replace(/\.\./g, "__").replace(/[^A-Za-z0-9._/-]/g, "_").replace(/\//g, "__") || "file";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function saveBackup(root, absPath, home) {
|
|
21
|
+
try {
|
|
22
|
+
if (!fs.existsSync(absPath)) return null;
|
|
23
|
+
const rel = path.relative(root, absPath).replace(/\\/g, "/") || path.basename(absPath);
|
|
24
|
+
if (rel.startsWith("..")) return null;
|
|
25
|
+
const dir = path.join(baseDir(home), safeName(rel));
|
|
26
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
27
|
+
const dest = path.join(dir, `${Date.now()}-${crypto.randomBytes(3).toString("hex")}.bak`);
|
|
28
|
+
fs.writeFileSync(dest, fs.readFileSync(absPath), { mode: 0o600 });
|
|
29
|
+
const entries = fs.readdirSync(dir).sort();
|
|
30
|
+
while (entries.length > 10) {
|
|
31
|
+
const oldest = entries.shift();
|
|
32
|
+
try {
|
|
33
|
+
fs.unlinkSync(path.join(dir, oldest));
|
|
34
|
+
} catch {
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return dest;
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function latestBackup(root, relPath, home) {
|
|
45
|
+
try {
|
|
46
|
+
const dir = path.join(baseDir(home), safeName(relPath));
|
|
47
|
+
const entries = fs.readdirSync(dir).filter((f) => f.endsWith(".bak")).sort();
|
|
48
|
+
if (!entries.length) return null;
|
|
49
|
+
return path.join(dir, entries[entries.length - 1]);
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function restore(root, relPath, home) {
|
|
56
|
+
const src = latestBackup(root, relPath, home);
|
|
57
|
+
if (!src) return { ok: false, summary: "no backup", detail: `No backup found for ${relPath}.` };
|
|
58
|
+
try {
|
|
59
|
+
const dest = path.resolve(root, relPath);
|
|
60
|
+
fs.writeFileSync(dest, fs.readFileSync(src));
|
|
61
|
+
return { ok: true, summary: "restored", detail: `Restored ${relPath} from ${path.basename(src)}.` };
|
|
62
|
+
} catch {
|
|
63
|
+
return { ok: false, summary: "restore failed", detail: `Cannot restore ${relPath}.` };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = { saveBackup, restore, latestBackup };
|