agentmast 0.0.0-stage → 0.1.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/dist/bin.js +153 -0
- package/dist/bot-HTAE3OOZ.js +215 -0
- package/dist/bus-6EDCL6C2.js +168 -0
- package/dist/chunk-FRKDGPVA.js +216 -0
- package/dist/chunk-LOCISTVR.js +125 -0
- package/dist/chunk-QCSXSHRA.js +16 -0
- package/dist/chunk-SNKD4OK2.js +67 -0
- package/dist/chunk-TV3CPBRC.js +62 -0
- package/dist/chunk-UZGN4SDK.js +153 -0
- package/dist/chunk-XFF5UE5F.js +40 -0
- package/dist/config-MJRV3XTY.js +12 -0
- package/dist/hook-transport-RECZISPK.js +11 -0
- package/dist/hook.js +11 -0
- package/dist/hooks-GYQZJCWH.js +231 -0
- package/dist/local-state-K2T7BJTT.js +17 -0
- package/dist/pairing-RQBLIMDP.js +119 -0
- package/dist/replay-4LHZWBSJ.js +81 -0
- package/dist/server-NJK23UFH.js +487 -0
- package/dist/video-RJZII4N6.js +153 -0
- package/dist/web/assets/index-CMw6GB5R.css +1 -0
- package/dist/web/assets/index-CiSHj7dg.js +48 -0
- package/dist/web/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/dist/web/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/dist/web/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/dist/web/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/dist/web/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/dist/web/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/dist/web/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/web/index.html +14 -0
- package/dist/web/licenses/Inter.txt +93 -0
- package/dist/web/licenses/JetBrains-Mono.txt +93 -0
- package/package.json +34 -4
- package/README.md +0 -3
package/dist/hook.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
runHook
|
|
4
|
+
} from "./chunk-UZGN4SDK.js";
|
|
5
|
+
import "./chunk-FRKDGPVA.js";
|
|
6
|
+
import "./chunk-QCSXSHRA.js";
|
|
7
|
+
import "./chunk-SNKD4OK2.js";
|
|
8
|
+
import "./chunk-LOCISTVR.js";
|
|
9
|
+
|
|
10
|
+
// src/hook-entry.ts
|
|
11
|
+
await runHook(process.argv.slice(2));
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
ANTIGRAVITY_HOOKS,
|
|
4
|
+
CLAUDE_HOOKS,
|
|
5
|
+
CODEX_HOOKS,
|
|
6
|
+
HOOK_MARKER,
|
|
7
|
+
ZCODE_HOOKS
|
|
8
|
+
} from "./chunk-FRKDGPVA.js";
|
|
9
|
+
import {
|
|
10
|
+
CODEX_PERMISSION_HOOK_TIMEOUT_SEC
|
|
11
|
+
} from "./chunk-QCSXSHRA.js";
|
|
12
|
+
import {
|
|
13
|
+
writeAtomically
|
|
14
|
+
} from "./chunk-SNKD4OK2.js";
|
|
15
|
+
|
|
16
|
+
// src/install/hooks.ts
|
|
17
|
+
import { execFileSync } from "child_process";
|
|
18
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, statSync, unlinkSync } from "fs";
|
|
19
|
+
import { homedir } from "os";
|
|
20
|
+
import { dirname, join, resolve } from "path";
|
|
21
|
+
import { isDeepStrictEqual } from "util";
|
|
22
|
+
var adapters = { "claude-code": CLAUDE_HOOKS, codex: CODEX_HOOKS, antigravity: ANTIGRAVITY_HOOKS, zcode: ZCODE_HOOKS };
|
|
23
|
+
var POWERSHELL_HOOK = String.raw`"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -EncodedCommand `;
|
|
24
|
+
var POWERSHELL_EXIT = "; exit $LASTEXITCODE";
|
|
25
|
+
function isObject(value) {
|
|
26
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
27
|
+
}
|
|
28
|
+
function readOptional(path) {
|
|
29
|
+
try {
|
|
30
|
+
return readFileSync(path);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error.code === "ENOENT") return;
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function parseSettings(contents, path) {
|
|
37
|
+
let data;
|
|
38
|
+
try {
|
|
39
|
+
data = JSON.parse(contents.toString("utf8").replace(/^\uFEFF/, ""));
|
|
40
|
+
} catch {
|
|
41
|
+
throw new Error(`${path} contains invalid JSON`);
|
|
42
|
+
}
|
|
43
|
+
if (!isObject(data) || Object.hasOwn(data, "hooks") && !isObject(data.hooks)) {
|
|
44
|
+
throw new Error(`${path} must contain a settings object and an object-valued hooks field`);
|
|
45
|
+
}
|
|
46
|
+
return data;
|
|
47
|
+
}
|
|
48
|
+
function isManaged(handler, argument, source) {
|
|
49
|
+
if (!isObject(handler)) return false;
|
|
50
|
+
if (source === "codex" || source === "antigravity") {
|
|
51
|
+
if (handler.type !== "command" || typeof handler.command !== "string") return false;
|
|
52
|
+
const command = handler.command;
|
|
53
|
+
if (source === "antigravity" && command.startsWith(POWERSHELL_HOOK)) {
|
|
54
|
+
const script2 = Buffer.from(command.slice(POWERSHELL_HOOK.length), "base64").toString("utf16le");
|
|
55
|
+
return script2.endsWith(` antigravity ${argument} ${HOOK_MARKER}${POWERSHELL_EXIT}`);
|
|
56
|
+
}
|
|
57
|
+
return command.endsWith(` ${source} ${argument} ${HOOK_MARKER}`) || command.endsWith(` ${source} ${argument} web-approval ${HOOK_MARKER}`);
|
|
58
|
+
}
|
|
59
|
+
if (handler.type !== (source === "zcode" ? "process" : "command")) return false;
|
|
60
|
+
if (!Array.isArray(handler.args)) return false;
|
|
61
|
+
if (source === "zcode") {
|
|
62
|
+
const [script2, runner, event2, ...rest] = handler.args;
|
|
63
|
+
return typeof script2 === "string" && /[/\\]hook\.js$/.test(script2) && runner === source && event2 === argument && rest.at(-1) === HOOK_MARKER;
|
|
64
|
+
}
|
|
65
|
+
const [script, event, marker] = handler.args;
|
|
66
|
+
return typeof script === "string" && /[/\\]hook\.js$/.test(script) && event === argument && marker === HOOK_MARKER;
|
|
67
|
+
}
|
|
68
|
+
function flatHandler(source, event) {
|
|
69
|
+
return source === "antigravity" && event !== "PreToolUse" && event !== "PostToolUse";
|
|
70
|
+
}
|
|
71
|
+
function buildGroup(source, event, argument, hookPath) {
|
|
72
|
+
if (source === "claude-code") {
|
|
73
|
+
return { hooks: [{ type: "command", command: process.execPath, args: [hookPath, argument, HOOK_MARKER], timeout: 2 }] };
|
|
74
|
+
}
|
|
75
|
+
const webApproval = source !== "antigravity" && event === "PermissionRequest";
|
|
76
|
+
if (source === "zcode") {
|
|
77
|
+
return { hooks: [{
|
|
78
|
+
type: "process",
|
|
79
|
+
command: process.execPath,
|
|
80
|
+
args: [hookPath, source, argument, ...webApproval ? ["web-approval"] : [], HOOK_MARKER],
|
|
81
|
+
timeoutMs: webApproval ? CODEX_PERMISSION_HOOK_TIMEOUT_SEC * 1e3 : 2e3
|
|
82
|
+
}] };
|
|
83
|
+
}
|
|
84
|
+
const quote = (path) => "'" + path.replaceAll("'", "'\\''") + "'";
|
|
85
|
+
const suffix = `${source} ${argument}${webApproval ? " web-approval" : ""} ${HOOK_MARKER}`;
|
|
86
|
+
const handler = {
|
|
87
|
+
type: "command",
|
|
88
|
+
command: `${quote(process.execPath)} ${quote(hookPath)} ${suffix}`,
|
|
89
|
+
timeout: webApproval ? CODEX_PERMISSION_HOOK_TIMEOUT_SEC : 2
|
|
90
|
+
};
|
|
91
|
+
if (process.platform === "win32") {
|
|
92
|
+
const quoteWindows = (path) => "'" + path.replaceAll("'", "''") + "'";
|
|
93
|
+
const command = `& ${quoteWindows(process.execPath)} ${quoteWindows(hookPath)} ${suffix}`;
|
|
94
|
+
if (source === "codex") handler.commandWindows = command;
|
|
95
|
+
else {
|
|
96
|
+
const encoding = "$utf8 = [System.Text.UTF8Encoding]::new($false); [Console]::InputEncoding = $utf8; [Console]::OutputEncoding = $utf8; $OutputEncoding = $utf8; ";
|
|
97
|
+
const pipeline = `${encoding}[Console]::In.ReadToEnd() | ${command}${POWERSHELL_EXIT}`;
|
|
98
|
+
handler.command = POWERSHELL_HOOK + Buffer.from(pipeline, "utf16le").toString("base64");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (flatHandler(source, event)) return handler;
|
|
102
|
+
return {
|
|
103
|
+
...source === "codex" && event === "SessionStart" ? { matcher: "startup|resume|clear|fork" } : {},
|
|
104
|
+
hooks: [handler]
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function editHooks(source, settings, hookPath, original) {
|
|
108
|
+
const key = source === "antigravity" ? "agentdeck" : "hooks";
|
|
109
|
+
const currentScope = settings[key];
|
|
110
|
+
if (currentScope !== void 0 && !isObject(currentScope)) throw new Error(`${key} must be an object`);
|
|
111
|
+
const originalScope = isObject(original?.[key]) ? original[key] : void 0;
|
|
112
|
+
const currentEvents = source === "zcode" ? currentScope?.events : currentScope;
|
|
113
|
+
if (currentEvents !== void 0 && !isObject(currentEvents)) throw new Error("hooks.events must be an object");
|
|
114
|
+
const originalEvents = source === "zcode" ? originalScope?.events : originalScope;
|
|
115
|
+
const hooks = { ...currentEvents };
|
|
116
|
+
let hadManaged = false;
|
|
117
|
+
for (const [event, { argument }] of Object.entries(adapters[source])) {
|
|
118
|
+
const current = hooks[event];
|
|
119
|
+
if (current !== void 0 && !Array.isArray(current)) throw new Error(`hooks.${event} must be an array`);
|
|
120
|
+
let removed = false;
|
|
121
|
+
const groups = (current ?? []).flatMap((group) => {
|
|
122
|
+
if (flatHandler(source, event)) {
|
|
123
|
+
if (!isManaged(group, argument, source)) return [group];
|
|
124
|
+
removed = true;
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
if (!isObject(group) || !Array.isArray(group.hooks)) return [group];
|
|
128
|
+
const handlers = group.hooks.filter((handler) => !isManaged(handler, argument, source));
|
|
129
|
+
if (handlers.length === group.hooks.length) return [group];
|
|
130
|
+
removed = true;
|
|
131
|
+
return handlers.length ? [{ ...group, hooks: handlers }] : [];
|
|
132
|
+
});
|
|
133
|
+
hadManaged ||= removed;
|
|
134
|
+
if (hookPath) {
|
|
135
|
+
groups.push(buildGroup(source, event, argument, hookPath));
|
|
136
|
+
hooks[event] = groups;
|
|
137
|
+
} else if (removed) {
|
|
138
|
+
if (groups.length || isObject(originalEvents) && Array.isArray(originalEvents[event])) hooks[event] = groups;
|
|
139
|
+
else delete hooks[event];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const next = { ...settings };
|
|
143
|
+
if (source === "zcode") {
|
|
144
|
+
const scope = { ...currentScope };
|
|
145
|
+
const hasEvents = Object.keys(hooks).length > 0;
|
|
146
|
+
if (hasEvents || isObject(originalEvents)) scope.events = hooks;
|
|
147
|
+
else delete scope.events;
|
|
148
|
+
if (hookPath && scope.enabled === void 0 && !Object.keys(currentEvents ?? {}).length) scope.enabled = true;
|
|
149
|
+
if (!hookPath && !hasEvents && originalScope?.enabled === void 0 && scope.enabled === true) delete scope.enabled;
|
|
150
|
+
if (Object.keys(scope).length || originalScope) next[key] = scope;
|
|
151
|
+
else delete next[key];
|
|
152
|
+
} else if (Object.keys(hooks).length || originalScope || !hadManaged && Object.hasOwn(settings, key)) next[key] = hooks;
|
|
153
|
+
else delete next[key];
|
|
154
|
+
return { next, hadManaged };
|
|
155
|
+
}
|
|
156
|
+
function updateClaudeHooks(project, hookPath) {
|
|
157
|
+
const directory = project ? join(process.cwd(), ".claude") : resolve(process.env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"));
|
|
158
|
+
return updateHooks("claude-code", join(directory, "settings.json"), hookPath);
|
|
159
|
+
}
|
|
160
|
+
function projectRoot() {
|
|
161
|
+
const cwd = process.cwd();
|
|
162
|
+
try {
|
|
163
|
+
return execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
164
|
+
cwd,
|
|
165
|
+
encoding: "utf8",
|
|
166
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
167
|
+
}).replace(/\r?\n$/, "") || cwd;
|
|
168
|
+
} catch {
|
|
169
|
+
return cwd;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function updateCodexHooks(project, hookPath) {
|
|
173
|
+
const directory = project ? join(projectRoot(), ".codex") : resolve(process.env.CODEX_HOME || join(homedir(), ".codex"));
|
|
174
|
+
return updateHooks("codex", join(directory, "hooks.json"), hookPath);
|
|
175
|
+
}
|
|
176
|
+
function updateZCodeHooks(project, hookPath) {
|
|
177
|
+
if (!project) return updateHooks("zcode", join(homedir(), ".zcode", "cli", "config.json"), hookPath);
|
|
178
|
+
const root = projectRoot();
|
|
179
|
+
const config = join(root, ".zcode", "config.json");
|
|
180
|
+
const alternative = join(root, "zcode.json");
|
|
181
|
+
if (hookPath) return updateHooks("zcode", existsSync(config) || !existsSync(alternative) ? config : alternative, hookPath);
|
|
182
|
+
const messages = [];
|
|
183
|
+
const errors = [];
|
|
184
|
+
for (const path of [config, alternative]) {
|
|
185
|
+
try {
|
|
186
|
+
messages.push(updateHooks("zcode", path));
|
|
187
|
+
} catch (error) {
|
|
188
|
+
errors.push(error);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (errors.length) throw new Error([...messages, ...errors.map((error) => error instanceof Error ? error.message : String(error))].join("\n"));
|
|
192
|
+
return messages.join("\n");
|
|
193
|
+
}
|
|
194
|
+
function updateAntigravityHooks(project, hookPath) {
|
|
195
|
+
const directory = project ? join(projectRoot(), ".agents") : join(homedir(), ".gemini", "config");
|
|
196
|
+
return updateHooks("antigravity", join(directory, "hooks.json"), hookPath);
|
|
197
|
+
}
|
|
198
|
+
function updateHooks(source, settingsPath, hookPath) {
|
|
199
|
+
const backupPath = `${settingsPath}.agentdeck-bak`;
|
|
200
|
+
const before = readOptional(settingsPath);
|
|
201
|
+
const settings = before ? parseSettings(before, settingsPath) : {};
|
|
202
|
+
const target = before ? realpathSync(settingsPath) : settingsPath;
|
|
203
|
+
const backup = hookPath ? void 0 : readOptional(backupPath);
|
|
204
|
+
const original = backup && backup.toString("utf8") !== "null\n" ? parseSettings(backup, backupPath) : void 0;
|
|
205
|
+
const { next, hadManaged } = editHooks(source, settings, hookPath, original);
|
|
206
|
+
if (!hookPath && !hadManaged || isDeepStrictEqual(settings, next)) {
|
|
207
|
+
return hookPath ? `AgentMast hooks are already installed in ${settingsPath}` : `No AgentMast hooks found in ${settingsPath}`;
|
|
208
|
+
}
|
|
209
|
+
if (hookPath && !hadManaged) {
|
|
210
|
+
mkdirSync(dirname(settingsPath), { recursive: true, mode: 448 });
|
|
211
|
+
writeAtomically(backupPath, before ?? "null\n");
|
|
212
|
+
}
|
|
213
|
+
const latest = readOptional(settingsPath);
|
|
214
|
+
if (!isDeepStrictEqual(before, latest)) {
|
|
215
|
+
throw new Error(`${settingsPath} changed during the update. Run the command again.`);
|
|
216
|
+
}
|
|
217
|
+
if (!hookPath && backup && !original && !Object.keys(next).length) {
|
|
218
|
+
unlinkSync(target);
|
|
219
|
+
} else {
|
|
220
|
+
const contents = original && isDeepStrictEqual(original, next) ? backup : `${JSON.stringify(next, null, 2)}
|
|
221
|
+
`;
|
|
222
|
+
writeAtomically(target, contents, before ? statSync(target).mode & 511 : 384);
|
|
223
|
+
}
|
|
224
|
+
return hookPath ? `Installed AgentMast hooks in ${settingsPath}` : `Removed AgentMast hooks from ${settingsPath}`;
|
|
225
|
+
}
|
|
226
|
+
export {
|
|
227
|
+
updateAntigravityHooks,
|
|
228
|
+
updateClaudeHooks,
|
|
229
|
+
updateCodexHooks,
|
|
230
|
+
updateZCodeHooks
|
|
231
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_PORT,
|
|
4
|
+
loadOrCreateToken,
|
|
5
|
+
readDaemonPort,
|
|
6
|
+
readToken,
|
|
7
|
+
stateDir,
|
|
8
|
+
writeAtomically
|
|
9
|
+
} from "./chunk-SNKD4OK2.js";
|
|
10
|
+
export {
|
|
11
|
+
DEFAULT_PORT,
|
|
12
|
+
loadOrCreateToken,
|
|
13
|
+
readDaemonPort,
|
|
14
|
+
readToken,
|
|
15
|
+
stateDir,
|
|
16
|
+
writeAtomically
|
|
17
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
loadTelegramConfig,
|
|
4
|
+
saveTelegramConfig,
|
|
5
|
+
validBotToken
|
|
6
|
+
} from "./chunk-XFF5UE5F.js";
|
|
7
|
+
import "./chunk-SNKD4OK2.js";
|
|
8
|
+
import {
|
|
9
|
+
botApiSignal,
|
|
10
|
+
createTelegramBot,
|
|
11
|
+
pollTelegram,
|
|
12
|
+
telegramFailure
|
|
13
|
+
} from "./chunk-TV3CPBRC.js";
|
|
14
|
+
|
|
15
|
+
// src/telegram/pairing.ts
|
|
16
|
+
import { randomInt } from "crypto";
|
|
17
|
+
import { createInterface } from "readline/promises";
|
|
18
|
+
import { Writable } from "stream";
|
|
19
|
+
var PAIRING_DURATION_MS = 3e5;
|
|
20
|
+
var MAX_ATTEMPTS = 5;
|
|
21
|
+
function installPairing(bot, code, startsAt, finish) {
|
|
22
|
+
let attempts = 0;
|
|
23
|
+
let completed = false;
|
|
24
|
+
bot.command("start", async (ctx) => {
|
|
25
|
+
if (completed || ctx.chat.type !== "private" || !ctx.from || ctx.from.is_bot || !ctx.message || ctx.message.date < Math.floor(startsAt / 1e3)) return;
|
|
26
|
+
if (Date.now() >= startsAt + PAIRING_DURATION_MS) {
|
|
27
|
+
completed = true;
|
|
28
|
+
finish(void 0);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const supplied = ctx.match.trim();
|
|
32
|
+
if (!supplied) {
|
|
33
|
+
await ctx.reply("Send /start followed by the pairing code shown in your AgentMast terminal.");
|
|
34
|
+
} else if (supplied === code) {
|
|
35
|
+
completed = true;
|
|
36
|
+
const saved = finish({ chatId: ctx.chat.id, userId: ctx.from.id });
|
|
37
|
+
await ctx.reply(saved ? "AgentMast paired. Approval requests will appear in this chat when AgentMast is running." : "AgentMast could not save this pairing.");
|
|
38
|
+
} else {
|
|
39
|
+
attempts++;
|
|
40
|
+
completed = attempts >= MAX_ATTEMPTS;
|
|
41
|
+
if (completed) finish(void 0);
|
|
42
|
+
await ctx.reply(completed ? "Pairing code expired. Start setup again." : "Invalid pairing code.");
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async function readBotToken() {
|
|
47
|
+
if (process.env.AGENTDECK_TELEGRAM_TOKEN) return process.env.AGENTDECK_TELEGRAM_TOKEN.trim();
|
|
48
|
+
let savedToken;
|
|
49
|
+
try {
|
|
50
|
+
savedToken = loadTelegramConfig()?.token;
|
|
51
|
+
} catch {
|
|
52
|
+
console.warn("Enter a new token to replace the unreadable Telegram configuration.");
|
|
53
|
+
}
|
|
54
|
+
const output = new Writable({ write(_chunk, _encoding, callback) {
|
|
55
|
+
callback();
|
|
56
|
+
} });
|
|
57
|
+
const rl = createInterface({ input: process.stdin, output, terminal: process.stdin.isTTY });
|
|
58
|
+
const closed = new AbortController();
|
|
59
|
+
rl.once("close", () => closed.abort());
|
|
60
|
+
rl.once("SIGINT", () => rl.close());
|
|
61
|
+
process.stderr.write(savedToken ? "BotFather token (Enter to reuse the saved token): " : "BotFather token: ");
|
|
62
|
+
try {
|
|
63
|
+
return (await rl.question("", { signal: closed.signal })).trim() || savedToken;
|
|
64
|
+
} finally {
|
|
65
|
+
rl.close();
|
|
66
|
+
process.stderr.write("\n");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async function setupTelegram() {
|
|
70
|
+
const token = await readBotToken();
|
|
71
|
+
if (!validBotToken(token)) throw new Error("Enter a valid bot token from BotFather.");
|
|
72
|
+
const bot = createTelegramBot(token);
|
|
73
|
+
try {
|
|
74
|
+
bot.botInfo = await bot.api.getMe(botApiSignal(AbortSignal.timeout(15e3)));
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw new Error(telegramFailure(error));
|
|
77
|
+
}
|
|
78
|
+
const username = bot.botInfo.username ?? bot.botInfo.active_usernames?.[0];
|
|
79
|
+
if (!username) throw new Error("This bot has no public username. Configure one in BotFather before pairing.");
|
|
80
|
+
const code = randomInt(1e6).toString().padStart(6, "0");
|
|
81
|
+
const startsAt = Date.now();
|
|
82
|
+
const controller = new AbortController();
|
|
83
|
+
let paired;
|
|
84
|
+
let storageError = false;
|
|
85
|
+
const expire = setTimeout(() => controller.abort(), PAIRING_DURATION_MS);
|
|
86
|
+
const interrupt = () => controller.abort();
|
|
87
|
+
process.once("SIGINT", interrupt);
|
|
88
|
+
process.once("SIGTERM", interrupt);
|
|
89
|
+
installPairing(bot, code, startsAt, (chat) => {
|
|
90
|
+
if (controller.signal.aborted) return false;
|
|
91
|
+
if (chat) {
|
|
92
|
+
try {
|
|
93
|
+
const config = { token, ...chat };
|
|
94
|
+
saveTelegramConfig(config);
|
|
95
|
+
paired = config;
|
|
96
|
+
} catch {
|
|
97
|
+
storageError = true;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
controller.abort();
|
|
101
|
+
return !!paired;
|
|
102
|
+
});
|
|
103
|
+
try {
|
|
104
|
+
console.log(`Open https://t.me/${username} and send /start ${code}`);
|
|
105
|
+
console.log("The pairing code expires in 5 minutes.");
|
|
106
|
+
await pollTelegram(bot, ["message"], controller.signal, console.warn);
|
|
107
|
+
if (storageError) throw new Error("Could not save the Telegram pairing.");
|
|
108
|
+
if (!paired) throw new Error("Pairing ended. Run agentmast telegram setup for a new code.");
|
|
109
|
+
console.log("Telegram paired. Start AgentMast with agentmast up.");
|
|
110
|
+
} finally {
|
|
111
|
+
clearTimeout(expire);
|
|
112
|
+
process.removeListener("SIGINT", interrupt);
|
|
113
|
+
process.removeListener("SIGTERM", interrupt);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export {
|
|
117
|
+
installPairing,
|
|
118
|
+
setupTelegram
|
|
119
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/adapters/replay.ts
|
|
4
|
+
var ScenarioPlayer = class {
|
|
5
|
+
constructor(bus, scenario, opts = {}) {
|
|
6
|
+
this.bus = bus;
|
|
7
|
+
this.scenario = scenario;
|
|
8
|
+
this.opts = opts;
|
|
9
|
+
for (const ds of scenario.datasets) bus.addDataset(ds);
|
|
10
|
+
}
|
|
11
|
+
bus;
|
|
12
|
+
scenario;
|
|
13
|
+
opts;
|
|
14
|
+
timers = [];
|
|
15
|
+
raceTimer;
|
|
16
|
+
running = false;
|
|
17
|
+
start() {
|
|
18
|
+
this.stop();
|
|
19
|
+
this.running = true;
|
|
20
|
+
this.bus.reset();
|
|
21
|
+
for (const ds of this.scenario.datasets) this.bus.broadcast({ type: "token_tick", datasetId: ds.id, t: 0 });
|
|
22
|
+
const speed = this.opts.speed ?? 1;
|
|
23
|
+
const kinds = new Map(this.scenario.sessions.map((s) => [s.id, s.kind]));
|
|
24
|
+
for (const step of [...this.scenario.timeline].sort((a, b) => a.at - b.at)) {
|
|
25
|
+
this.timers.push(setTimeout(() => this.run(step, kinds), step.at * 1e3 / speed));
|
|
26
|
+
}
|
|
27
|
+
if (this.opts.loop) {
|
|
28
|
+
const total = this.scenario.duration + (this.opts.loopGap ?? 3);
|
|
29
|
+
this.timers.push(setTimeout(() => this.running && this.start(), total * 1e3 / speed));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
stop() {
|
|
33
|
+
this.running = false;
|
|
34
|
+
for (const t of this.timers) clearTimeout(t);
|
|
35
|
+
this.timers = [];
|
|
36
|
+
if (this.raceTimer) clearInterval(this.raceTimer);
|
|
37
|
+
}
|
|
38
|
+
run(step, kinds) {
|
|
39
|
+
if ("event" in step) {
|
|
40
|
+
const meta = this.scenario.sessions.find((s) => s.id === step.event.sessionId);
|
|
41
|
+
this.bus.ingest({
|
|
42
|
+
title: step.event.type === "session_start" ? meta?.title : void 0,
|
|
43
|
+
cwd: step.event.type === "session_start" ? meta?.cwd : void 0,
|
|
44
|
+
...step.event,
|
|
45
|
+
kind: step.event.kind ?? kinds.get(step.event.sessionId) ?? "generic",
|
|
46
|
+
ts: Date.now()
|
|
47
|
+
});
|
|
48
|
+
} else if ("approval" in step) {
|
|
49
|
+
const { tool, ...req } = step.approval;
|
|
50
|
+
void this.bus.requestApproval({ ...req, timeoutSec: 600 }, tool);
|
|
51
|
+
} else if ("decide" in step) {
|
|
52
|
+
this.bus.decide({ by: "telegram", ...step.decide });
|
|
53
|
+
} else if ("phoneReply" in step) {
|
|
54
|
+
const { approvalId, text, duration } = step.phoneReply;
|
|
55
|
+
if (this.bus.getApproval(approvalId)) {
|
|
56
|
+
this.bus.broadcast({ type: "demo_phone_reply", approvalId, text, durationMs: duration * 1e3 / (this.opts.speed ?? 1) });
|
|
57
|
+
}
|
|
58
|
+
} else if ("tokenRace" in step) {
|
|
59
|
+
this.race(step.tokenRace.datasetId, step.tokenRace.duration);
|
|
60
|
+
} else if ("toast" in step) {
|
|
61
|
+
this.bus.broadcast({ type: "toast", text: step.toast });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Streams dataset time to clients so the bars "race" in real time. */
|
|
65
|
+
race(datasetId, duration) {
|
|
66
|
+
const ds = this.scenario.datasets.find((d) => d.id === datasetId);
|
|
67
|
+
if (!ds) return;
|
|
68
|
+
const maxT = Math.max(...ds.variants.flatMap((v) => v.samples.map((s) => s.t)));
|
|
69
|
+
const speed = this.opts.speed ?? 1;
|
|
70
|
+
const started = Date.now();
|
|
71
|
+
if (this.raceTimer) clearInterval(this.raceTimer);
|
|
72
|
+
this.raceTimer = setInterval(() => {
|
|
73
|
+
const p = Math.min(1, (Date.now() - started) / 1e3 * speed / duration);
|
|
74
|
+
this.bus.broadcast({ type: "token_tick", datasetId, t: p * maxT });
|
|
75
|
+
if (p >= 1) clearInterval(this.raceTimer);
|
|
76
|
+
}, 1e3 / 20);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
export {
|
|
80
|
+
ScenarioPlayer
|
|
81
|
+
};
|