blun-king-cli 9.1.653 → 9.1.654
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/LIESMICH.txt +1 -1
- package/README.md +1 -1
- package/blun.mjs +1227 -392
- package/original-history-runtime.mjs +1420 -0
- package/original-history-tui.mjs +879 -0
- package/original-markdown-runtime.mjs +291 -0
- package/package.json +1 -1
- package/worker-host.mjs +310 -101
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// packages/agent-core/src/session/export/original-markdown.ts
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import { link, lstat, mkdir, open, realpath, unlink } from "node:fs/promises";
|
|
4
|
+
import { basename as basename2, dirname as dirname2, join as join2, parse as parse2, resolve as resolve2 } from "node:path";
|
|
5
|
+
|
|
6
|
+
// node_modules/.pnpm/pathe@2.0.3/node_modules/pathe/dist/shared/pathe.M-eThtNZ.mjs
|
|
7
|
+
var _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
8
|
+
function normalizeWindowsPath(input = "") {
|
|
9
|
+
if (!input) {
|
|
10
|
+
return input;
|
|
11
|
+
}
|
|
12
|
+
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
13
|
+
}
|
|
14
|
+
var _UNC_REGEX = /^[/\\]{2}/;
|
|
15
|
+
var _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
16
|
+
var _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
|
|
17
|
+
var normalize = function(path) {
|
|
18
|
+
if (path.length === 0) {
|
|
19
|
+
return ".";
|
|
20
|
+
}
|
|
21
|
+
path = normalizeWindowsPath(path);
|
|
22
|
+
const isUNCPath = path.match(_UNC_REGEX);
|
|
23
|
+
const isPathAbsolute = isAbsolute(path);
|
|
24
|
+
const trailingSeparator = path[path.length - 1] === "/";
|
|
25
|
+
path = normalizeString(path, !isPathAbsolute);
|
|
26
|
+
if (path.length === 0) {
|
|
27
|
+
if (isPathAbsolute) {
|
|
28
|
+
return "/";
|
|
29
|
+
}
|
|
30
|
+
return trailingSeparator ? "./" : ".";
|
|
31
|
+
}
|
|
32
|
+
if (trailingSeparator) {
|
|
33
|
+
path += "/";
|
|
34
|
+
}
|
|
35
|
+
if (_DRIVE_LETTER_RE.test(path)) {
|
|
36
|
+
path += "/";
|
|
37
|
+
}
|
|
38
|
+
if (isUNCPath) {
|
|
39
|
+
if (!isPathAbsolute) {
|
|
40
|
+
return `//./${path}`;
|
|
41
|
+
}
|
|
42
|
+
return `//${path}`;
|
|
43
|
+
}
|
|
44
|
+
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
|
|
45
|
+
};
|
|
46
|
+
function normalizeString(path, allowAboveRoot) {
|
|
47
|
+
let res = "";
|
|
48
|
+
let lastSegmentLength = 0;
|
|
49
|
+
let lastSlash = -1;
|
|
50
|
+
let dots = 0;
|
|
51
|
+
let char = null;
|
|
52
|
+
for (let index = 0; index <= path.length; ++index) {
|
|
53
|
+
if (index < path.length) {
|
|
54
|
+
char = path[index];
|
|
55
|
+
} else if (char === "/") {
|
|
56
|
+
break;
|
|
57
|
+
} else {
|
|
58
|
+
char = "/";
|
|
59
|
+
}
|
|
60
|
+
if (char === "/") {
|
|
61
|
+
if (lastSlash === index - 1 || dots === 1) ;
|
|
62
|
+
else if (dots === 2) {
|
|
63
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
64
|
+
if (res.length > 2) {
|
|
65
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
66
|
+
if (lastSlashIndex === -1) {
|
|
67
|
+
res = "";
|
|
68
|
+
lastSegmentLength = 0;
|
|
69
|
+
} else {
|
|
70
|
+
res = res.slice(0, lastSlashIndex);
|
|
71
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
72
|
+
}
|
|
73
|
+
lastSlash = index;
|
|
74
|
+
dots = 0;
|
|
75
|
+
continue;
|
|
76
|
+
} else if (res.length > 0) {
|
|
77
|
+
res = "";
|
|
78
|
+
lastSegmentLength = 0;
|
|
79
|
+
lastSlash = index;
|
|
80
|
+
dots = 0;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (allowAboveRoot) {
|
|
85
|
+
res += res.length > 0 ? "/.." : "..";
|
|
86
|
+
lastSegmentLength = 2;
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
if (res.length > 0) {
|
|
90
|
+
res += `/${path.slice(lastSlash + 1, index)}`;
|
|
91
|
+
} else {
|
|
92
|
+
res = path.slice(lastSlash + 1, index);
|
|
93
|
+
}
|
|
94
|
+
lastSegmentLength = index - lastSlash - 1;
|
|
95
|
+
}
|
|
96
|
+
lastSlash = index;
|
|
97
|
+
dots = 0;
|
|
98
|
+
} else if (char === "." && dots !== -1) {
|
|
99
|
+
++dots;
|
|
100
|
+
} else {
|
|
101
|
+
dots = -1;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return res;
|
|
105
|
+
}
|
|
106
|
+
var isAbsolute = function(p) {
|
|
107
|
+
return _IS_ABSOLUTE_RE.test(p);
|
|
108
|
+
};
|
|
109
|
+
var basename = function(p, extension) {
|
|
110
|
+
const segments = normalizeWindowsPath(p).split("/");
|
|
111
|
+
let lastSegment = "";
|
|
112
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
113
|
+
const val = segments[i];
|
|
114
|
+
if (val) {
|
|
115
|
+
lastSegment = val;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return extension && lastSegment.endsWith(extension) ? lastSegment.slice(0, -extension.length) : lastSegment;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// packages/agent-core/src/tools/policies/sensitive.ts
|
|
123
|
+
var SENSITIVE_BASENAMES = /* @__PURE__ */ new Set([
|
|
124
|
+
".env",
|
|
125
|
+
"id_rsa",
|
|
126
|
+
"id_ed25519",
|
|
127
|
+
"id_ecdsa",
|
|
128
|
+
"credentials"
|
|
129
|
+
]);
|
|
130
|
+
var SENSITIVE_PATH_SUFFIXES = [
|
|
131
|
+
[".aws", "credentials"],
|
|
132
|
+
[".gcp", "credentials"]
|
|
133
|
+
];
|
|
134
|
+
var ENV_PREFIX = ".env.";
|
|
135
|
+
var ENV_EXEMPTIONS = /* @__PURE__ */ new Set([".env.example", ".env.sample", ".env.template"]);
|
|
136
|
+
var SENSITIVE_BASENAME_PREFIXES = ["id_rsa", "id_ed25519", "id_ecdsa", "credentials"];
|
|
137
|
+
var PUBLIC_KEY_BASENAMES = /* @__PURE__ */ new Set(["id_rsa.pub", "id_ed25519.pub", "id_ecdsa.pub"]);
|
|
138
|
+
var SENSITIVE_DOT_VARIANT_SUFFIXES = [
|
|
139
|
+
".bak",
|
|
140
|
+
".backup",
|
|
141
|
+
".copy",
|
|
142
|
+
".disabled",
|
|
143
|
+
".key",
|
|
144
|
+
".old",
|
|
145
|
+
".orig",
|
|
146
|
+
".pem",
|
|
147
|
+
".save",
|
|
148
|
+
".tmp"
|
|
149
|
+
];
|
|
150
|
+
var SENSITIVE_DOT_VARIANT_SUFFIX_SET = new Set(SENSITIVE_DOT_VARIANT_SUFFIXES);
|
|
151
|
+
function comparable(path) {
|
|
152
|
+
return path.toLowerCase();
|
|
153
|
+
}
|
|
154
|
+
function isSensitiveFile(path) {
|
|
155
|
+
const name = basename(path);
|
|
156
|
+
const comparableName = comparable(name);
|
|
157
|
+
const comparablePath = comparable(path);
|
|
158
|
+
if (ENV_EXEMPTIONS.has(comparableName)) return false;
|
|
159
|
+
if (PUBLIC_KEY_BASENAMES.has(comparableName)) return false;
|
|
160
|
+
if (SENSITIVE_BASENAMES.has(comparableName)) return true;
|
|
161
|
+
if (comparableName.startsWith(ENV_PREFIX)) return true;
|
|
162
|
+
for (const prefix of SENSITIVE_BASENAME_PREFIXES) {
|
|
163
|
+
if (comparableName === prefix) return true;
|
|
164
|
+
if (comparableName.length > prefix.length && comparableName.startsWith(prefix)) {
|
|
165
|
+
const suffix = comparableName.slice(prefix.length);
|
|
166
|
+
const next = suffix[0];
|
|
167
|
+
if (next === "-" || next === "_") return true;
|
|
168
|
+
if (next === "." && SENSITIVE_DOT_VARIANT_SUFFIX_SET.has(suffix)) return true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
for (const suffixParts of SENSITIVE_PATH_SUFFIXES) {
|
|
172
|
+
const suffix = suffixParts.join("/");
|
|
173
|
+
const comparableSuffix = comparable(suffix);
|
|
174
|
+
if (comparablePath.endsWith(`/${comparableSuffix}`) || comparablePath.includes(`/${comparableSuffix}/`)) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// packages/agent-core/src/session/export/original-markdown.ts
|
|
182
|
+
var HEADINGS = {
|
|
183
|
+
user: "User",
|
|
184
|
+
assistant: "Assistant",
|
|
185
|
+
tool: "Tool",
|
|
186
|
+
output: "Tool output",
|
|
187
|
+
compaction: "Compaction",
|
|
188
|
+
clear: "Context cleared",
|
|
189
|
+
undo: "Context undo"
|
|
190
|
+
};
|
|
191
|
+
var UNAVAILABLE = {
|
|
192
|
+
record: "Original record is unreadable.",
|
|
193
|
+
"tool-output": "Original tool output is unavailable.",
|
|
194
|
+
size: "Original record or output exceeds the 16 MiB export limit."
|
|
195
|
+
};
|
|
196
|
+
var BAD_PATH = /[\u0000-\u001F\u007F-\u009F\u202A-\u202E\u2066-\u2069]/u;
|
|
197
|
+
var WIN_DEVICE = /^(?:con|prn|aux|nul|conin\$|conout\$|(?:com|lpt)[1-9\u00B9\u00B2\u00B3])(?:\.|$)/iu;
|
|
198
|
+
async function writeOriginalHistoryMarkdown(input) {
|
|
199
|
+
const { signal, view, assertOwner } = input;
|
|
200
|
+
const checkOperation = () => {
|
|
201
|
+
signal?.throwIfAborted();
|
|
202
|
+
assertOwner?.();
|
|
203
|
+
};
|
|
204
|
+
checkOperation();
|
|
205
|
+
const requested = resolve2(input.outputPath);
|
|
206
|
+
const leaf = basename2(requested);
|
|
207
|
+
if (invalidDestination(requested)) throw new Error("MARKDOWN_EXPORT_PATH_INVALID");
|
|
208
|
+
const entries = view.exportEntries(signal);
|
|
209
|
+
try {
|
|
210
|
+
const first = await entries.next();
|
|
211
|
+
if (first.done) return void 0;
|
|
212
|
+
checkOperation();
|
|
213
|
+
await mkdir(dirname2(requested), { recursive: true });
|
|
214
|
+
const directory = await realpath(dirname2(requested));
|
|
215
|
+
const outputPath = join2(directory, leaf);
|
|
216
|
+
if (invalidDestination(outputPath)) throw new Error("MARKDOWN_EXPORT_PATH_INVALID");
|
|
217
|
+
const parent = await lstat(directory, { bigint: true });
|
|
218
|
+
const sameParent = async () => {
|
|
219
|
+
const current = await lstat(directory, { bigint: true });
|
|
220
|
+
return current.isDirectory() && current.dev === parent.dev && current.ino === parent.ino && current.birthtimeNs === parent.birthtimeNs && await realpath(directory) === directory;
|
|
221
|
+
};
|
|
222
|
+
const temporary = join2(directory, ".blun-export-" + randomUUID() + ".tmp");
|
|
223
|
+
const hash = createHash("sha256");
|
|
224
|
+
let bytes = 0;
|
|
225
|
+
let exported = 0;
|
|
226
|
+
let unavailable = 0;
|
|
227
|
+
checkOperation();
|
|
228
|
+
const handle = await open(temporary, "wx", 384);
|
|
229
|
+
let fileId;
|
|
230
|
+
let synced;
|
|
231
|
+
const owns = (current) => fileId !== void 0 && current.isFile() && current.dev === fileId.dev && current.ino === fileId.ino && current.birthtimeNs === fileId.birthtimeNs;
|
|
232
|
+
try {
|
|
233
|
+
const write = async (text) => {
|
|
234
|
+
checkOperation();
|
|
235
|
+
const buffer = Buffer.from(text);
|
|
236
|
+
await handle.writeFile(buffer);
|
|
237
|
+
bytes += buffer.length;
|
|
238
|
+
hash.update(buffer);
|
|
239
|
+
};
|
|
240
|
+
try {
|
|
241
|
+
fileId = await handle.stat({ bigint: true });
|
|
242
|
+
await write("# Session transcript\n\n" + literal(input.sessionId) + "\n\nOriginal visible history, including entries before context changes. Media files and hidden runtime records are not included.\n\n");
|
|
243
|
+
for (let next = first; !next.done; next = await entries.next()) {
|
|
244
|
+
const entry = next.value;
|
|
245
|
+
if (entry.kind === "unavailable") unavailable++;
|
|
246
|
+
else exported++;
|
|
247
|
+
await write(render(entry));
|
|
248
|
+
}
|
|
249
|
+
await write(`---
|
|
250
|
+
|
|
251
|
+
Visible records: ${exported}. Unavailable: ${unavailable}.
|
|
252
|
+
`);
|
|
253
|
+
await handle.sync();
|
|
254
|
+
synced = await handle.stat({ bigint: true });
|
|
255
|
+
} finally {
|
|
256
|
+
await handle.close();
|
|
257
|
+
}
|
|
258
|
+
const staged = await lstat(temporary, { bigint: true });
|
|
259
|
+
if (!await sameParent() || !owns(staged) || staged.nlink !== 1n || staged.size !== BigInt(bytes) || staged.mtimeNs !== synced.mtimeNs || staged.ctimeNs !== synced.ctimeNs) {
|
|
260
|
+
throw new Error("MARKDOWN_EXPORT_PATH_CHANGED");
|
|
261
|
+
}
|
|
262
|
+
checkOperation();
|
|
263
|
+
await link(temporary, outputPath);
|
|
264
|
+
return { outputPath, sha256: hash.digest("hex"), bytes, exported, unavailable };
|
|
265
|
+
} finally {
|
|
266
|
+
if (await sameParent().catch(() => false) && await lstat(temporary, { bigint: true }).then(owns, () => false)) {
|
|
267
|
+
await unlink(temporary).catch(() => {
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
} finally {
|
|
272
|
+
await entries.return(void 0);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function invalidDestination(path) {
|
|
276
|
+
if (BAD_PATH.test(path) || isSensitiveFile(normalize(path)) || /^[A-Za-z]:[/\\]*$/u.test(path)) return true;
|
|
277
|
+
const windows = process.platform === "win32";
|
|
278
|
+
if (windows && path.startsWith("\\\\")) return true;
|
|
279
|
+
const segments = windows ? path.slice(parse2(path).root.length).split(/[\\/]/u) : [basename2(path)];
|
|
280
|
+
return segments.some((segment) => segment.includes(":") || /[. ]$/u.test(segment) || windows && WIN_DEVICE.test(segment));
|
|
281
|
+
}
|
|
282
|
+
function render(entry) {
|
|
283
|
+
if (entry.kind === "unavailable") return "## Unavailable\n\n" + UNAVAILABLE[entry.reason] + "\n\n";
|
|
284
|
+
return "## " + HEADINGS[entry.kind] + "\n\n" + (entry.text.length === 0 ? "" : literal(entry.text) + "\n\n");
|
|
285
|
+
}
|
|
286
|
+
function literal(text) {
|
|
287
|
+
return " " + text.replaceAll(/\r\n|\r|\n/gu, "\n ");
|
|
288
|
+
}
|
|
289
|
+
export {
|
|
290
|
+
writeOriginalHistoryMarkdown
|
|
291
|
+
};
|