blun-king-cli 9.1.653 → 9.1.655
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 +1938 -420
- 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/telegram-plugin/dist/bridge.mjs +33 -0
- package/telegram-plugin/dist/console-control.mjs +388 -0
- package/telegram-plugin/dist/mcp-server.mjs +1 -0
- package/worker-host.mjs +889 -109
|
@@ -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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as wave3Control from "./console-control.mjs";
|
|
1
2
|
import { createRequire } from "node:module";
|
|
2
3
|
import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
4
|
import { dirname, join, resolve } from "node:path";
|
|
@@ -59343,6 +59344,7 @@ function readAccess() {
|
|
|
59343
59344
|
return {
|
|
59344
59345
|
dmPolicy: parsed.dmPolicy ?? "pairing",
|
|
59345
59346
|
allowFrom: normalizeIdList(parsed.allowFrom),
|
|
59347
|
+
sessionControlAllowFrom: Array.isArray(parsed.sessionControlAllowFrom) ? parsed.sessionControlAllowFrom.filter(id => typeof id === "string") : undefined,
|
|
59346
59348
|
groups: normalizeGroups(parsed.groups),
|
|
59347
59349
|
pending: parsed.pending ?? {},
|
|
59348
59350
|
mentionPatterns: parsed.mentionPatterns,
|
|
@@ -68515,6 +68517,32 @@ async function handleInbound(event) {
|
|
|
68515
68517
|
else if (HEADLESS_ENABLED) session.send(deliveryTag, String(meta.chat_id), image);
|
|
68516
68518
|
else process.stderr.write(`telegram bridge: headless off — inbound ${chatId}:${String(msgId)} nicht zugestellt (kein TUI-Fenster offen)\n`);
|
|
68517
68519
|
}
|
|
68520
|
+
const wave3ControlLifetime = new AbortController();
|
|
68521
|
+
let wave3ControlRepliesStarted = false;
|
|
68522
|
+
wave3Control.setGrammyErrorType(import_out.GrammyError);
|
|
68523
|
+
const MAX_SESSION_CONTROL_AGE_SECONDS = 900;
|
|
68524
|
+
const signal = wave3ControlLifetime.signal;
|
|
68525
|
+
const sessionControlAvailable = () => wave3Control.readBinding(stateDir(), String(bot.botInfo.id)) !== undefined;
|
|
68526
|
+
const hasSessionControlGrant = wave3Control.hasSessionControlGrant;
|
|
68527
|
+
const handleSessionControl = async (request) => wave3Control.enqueueRequest(stateDir(), String(bot.botInfo.id), request);
|
|
68528
|
+
bot.on('message:text', async (ctx, next)=>{
|
|
68529
|
+
const { message } = ctx;
|
|
68530
|
+
const text = message.text;
|
|
68531
|
+
if (!/^\/(?:new|reload)(?=$|[\s@])/u.test(text) || !sessionControlAvailable()) return next();
|
|
68532
|
+
if (signal.aborted || text !== '/new' && text !== '/reload') return;
|
|
68533
|
+
const { chat, from, date, message_id: messageId } = message;
|
|
68534
|
+
const updateId = ctx.update.update_id;
|
|
68535
|
+
const age = Math.floor(Date.now() / 1000) - date;
|
|
68536
|
+
if (chat.type !== 'private' || from?.is_bot !== false || chat.id !== from.id || message.forward_origin !== undefined || message.is_automatic_forward === true || message.via_bot !== undefined || message.sender_chat !== undefined || message.is_from_offline !== undefined || message.is_topic_message !== undefined || message.message_thread_id !== undefined || message.edit_date !== undefined || message.business_connection_id !== undefined || !Number.isSafeInteger(chat.id) || chat.id <= 0 || !Number.isSafeInteger(messageId) || messageId <= 0 || !Number.isSafeInteger(updateId) || updateId <= 0 || !Number.isSafeInteger(date) || age < -30 || age > MAX_SESSION_CONTROL_AGE_SECONDS || !message.entities?.some((entity)=>entity.type === 'bot_command' && entity.offset === 0 && entity.length === text.length)) return;
|
|
68537
|
+
const chatId = String(chat.id);
|
|
68538
|
+
if (!hasSessionControlGrant(stateDir(), chatId)) return;
|
|
68539
|
+
await handleSessionControl({
|
|
68540
|
+
command: text === '/new' ? 'new' : 'reload',
|
|
68541
|
+
chatId,
|
|
68542
|
+
messageId: String(messageId),
|
|
68543
|
+
updateId
|
|
68544
|
+
});
|
|
68545
|
+
});
|
|
68518
68546
|
registerTypeHandlers(bot, TOKEN, handleInbound);
|
|
68519
68547
|
bot.command("start", async (ctx) => {
|
|
68520
68548
|
if (dmCommandGate(toInboundCtx(ctx, "")) === null) return;
|
|
@@ -68544,6 +68572,7 @@ bot.catch((err) => {
|
|
|
68544
68572
|
});
|
|
68545
68573
|
let shuttingDown = false;
|
|
68546
68574
|
function shutdown() {
|
|
68575
|
+
wave3ControlLifetime.abort();
|
|
68547
68576
|
if (shuttingDown) return;
|
|
68548
68577
|
shuttingDown = true;
|
|
68549
68578
|
process.stderr.write("telegram bridge: shutting down\n");
|
|
@@ -68582,6 +68611,10 @@ if (!HEADLESS_ENABLED) {
|
|
|
68582
68611
|
startApprovalWatcher(bot);
|
|
68583
68612
|
pollWithBackoff(bot, () => shuttingDown, (username) => {
|
|
68584
68613
|
botUsername = username;
|
|
68614
|
+
if (!wave3ControlRepliesStarted) {
|
|
68615
|
+
wave3ControlRepliesStarted = true;
|
|
68616
|
+
wave3Control.startConsoleControlReplies(bot, stateDir(), wave3ControlLifetime.signal);
|
|
68617
|
+
}
|
|
68585
68618
|
});
|
|
68586
68619
|
//#endregion
|
|
68587
68620
|
export {};
|