@stage-labs/metro 0.1.0-beta.0 → 0.1.0-beta.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/README.md +42 -57
- package/dist/channels/discord.js +70 -39
- package/dist/channels/telegram.js +7 -3
- package/dist/cli.js +569 -9
- package/dist/lib/address.js +21 -0
- package/dist/lib/codex-rc.js +274 -0
- package/dist/lib/dotenv.js +31 -0
- package/dist/log.js +10 -3
- package/dist/paths.js +45 -0
- package/dist/tail.js +45 -15
- package/package.json +5 -4
- package/skills/metro/SKILL.md +122 -0
- package/dist/config.js +0 -33
- package/dist/server.js +0 -158
package/dist/cli.js
CHANGED
|
@@ -1,13 +1,573 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
7
|
+
import * as discord from './channels/discord.js';
|
|
8
|
+
import * as telegram from './channels/telegram.js';
|
|
9
|
+
import { buildSendBody, tg } from './channels/telegram.js';
|
|
10
|
+
import { parseAddress } from './lib/address.js';
|
|
11
|
+
import { readDotenv, writeDotenv } from './lib/dotenv.js';
|
|
12
|
+
import { errMsg, log } from './log.js';
|
|
13
|
+
import { CONFIG_ENV_FILE, configuredPlatforms, loadMetroEnv, STATE_DIR, skillDir, } from './paths.js';
|
|
14
|
+
// ---------------- USAGE ----------------------------------------------------
|
|
15
|
+
const USAGE = `metro — Telegram + Discord bridge for your agent
|
|
16
|
+
|
|
17
|
+
Usage:
|
|
18
|
+
metro Inbound stream (long-running; run in the background).
|
|
19
|
+
metro setup […] Manage tokens and the agent skill (see below).
|
|
20
|
+
metro doctor Health check across tokens, gateways, and skills.
|
|
21
|
+
metro reply --to=<addr> [--text=<t>] Quote-reply, threading under the original. Clears 👀.
|
|
22
|
+
metro react --to=<addr> --emoji=<e> Set or clear ('') a reaction.
|
|
23
|
+
metro edit --to=<addr> [--text=<t>] Edit a message the bot previously sent.
|
|
24
|
+
metro send --to=<addr> [--text=<t>] Send a proactive message (no reply context).
|
|
25
|
+
<addr> is channel-only: <platform>:<chat_id> (no /message_id).
|
|
26
|
+
metro download --to=<addr> [--out=<dir>] Download image attachments to disk.
|
|
27
|
+
metro fetch --to=<addr> [--limit=N] Recent-message lookback (Discord only).
|
|
28
|
+
metro update Upgrade in place (npm/bun/pnpm auto-detected).
|
|
29
|
+
|
|
30
|
+
setup verbs:
|
|
31
|
+
metro setup Status: tokens, skills, what's next.
|
|
32
|
+
metro setup telegram <token> Save TELEGRAM_BOT_TOKEN (validated via getMe; --no-validate skips).
|
|
33
|
+
metro setup discord <token> Save DISCORD_BOT_TOKEN (validated via getMe; --no-validate skips).
|
|
34
|
+
metro setup clear [telegram|discord|all] Remove tokens.
|
|
35
|
+
metro setup skill [--project] [--clear] Install (or remove) the agent skill.
|
|
36
|
+
|
|
37
|
+
Address format:
|
|
38
|
+
telegram:<chat_id>/<message_id> e.g. telegram:-100123456789/4567
|
|
39
|
+
discord:<channel_id>/<message_id> e.g. discord:1234567890/9876543210
|
|
40
|
+
discord:<channel_id> fetch only (no message id)
|
|
41
|
+
|
|
42
|
+
Common flags:
|
|
43
|
+
--json machine-parseable output (single JSON line/array)
|
|
44
|
+
--version, -v print the metro version
|
|
45
|
+
--help, -h print this help
|
|
46
|
+
|
|
47
|
+
reply / edit Telegram extras:
|
|
48
|
+
--parse-mode=HTML|MarkdownV2 --no-link-preview --buttons-json='[[{"text":"…","url":"…"}]]'
|
|
49
|
+
|
|
50
|
+
Exit codes:
|
|
51
|
+
0 success
|
|
52
|
+
1 usage error (bad flags, unknown subcommand)
|
|
53
|
+
2 configuration error (no tokens — run \`metro setup\`)
|
|
54
|
+
3 upstream error (rate limit, auth, network — retry once, then surface)
|
|
55
|
+
|
|
56
|
+
Codex push (opt-in):
|
|
57
|
+
Set METRO_CODEX_RC to the codex app-server URL — metro will push each
|
|
58
|
+
inbound into the agent's history via JSON-RPC \`turn/start\`, the Codex
|
|
59
|
+
equivalent of Claude Code's Monitor.
|
|
60
|
+
|
|
61
|
+
Three terminals share the same URL (codex 0.130's TUI --remote only
|
|
62
|
+
accepts ws://):
|
|
63
|
+
|
|
64
|
+
codex app-server --listen ws://127.0.0.1:8421 # daemon
|
|
65
|
+
METRO_CODEX_RC=ws://127.0.0.1:8421 metro # bridge
|
|
66
|
+
codex --remote ws://127.0.0.1:8421 # TUI
|
|
67
|
+
`;
|
|
68
|
+
function exitErr(message, code) {
|
|
69
|
+
return Object.assign(new Error(message), { code });
|
|
5
70
|
}
|
|
6
|
-
|
|
7
|
-
|
|
71
|
+
function parseArgs(argv) {
|
|
72
|
+
const positional = [];
|
|
73
|
+
const flags = {};
|
|
74
|
+
for (let i = 0; i < argv.length; i++) {
|
|
75
|
+
const a = argv[i];
|
|
76
|
+
if (!a.startsWith('--')) {
|
|
77
|
+
positional.push(a);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const eq = a.indexOf('=');
|
|
81
|
+
if (eq !== -1) {
|
|
82
|
+
flags[a.slice(2, eq)] = a.slice(eq + 1);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const key = a.slice(2);
|
|
86
|
+
const next = argv[i + 1];
|
|
87
|
+
if (next !== undefined && !next.startsWith('--')) {
|
|
88
|
+
flags[key] = next;
|
|
89
|
+
i++;
|
|
90
|
+
}
|
|
91
|
+
else
|
|
92
|
+
flags[key] = true;
|
|
93
|
+
}
|
|
94
|
+
return { positional, flags };
|
|
8
95
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
process.
|
|
96
|
+
const isJson = (flags) => flags.json === true;
|
|
97
|
+
function emitResult(flags, human, structured) {
|
|
98
|
+
process.stdout.write(isJson(flags) ? JSON.stringify(structured) + '\n' : human + '\n');
|
|
12
99
|
}
|
|
13
|
-
|
|
100
|
+
function resolveAddr(flags, requireMessage) {
|
|
101
|
+
const addr = parseAddress(String(flags.to), requireMessage);
|
|
102
|
+
if (!configuredPlatforms()[addr.platform]) {
|
|
103
|
+
throw exitErr(`platform '${addr.platform}' is not configured (missing token)`, 2);
|
|
104
|
+
}
|
|
105
|
+
return addr;
|
|
106
|
+
}
|
|
107
|
+
function tgMessageId(addr) {
|
|
108
|
+
const n = Number(addr.messageId);
|
|
109
|
+
if (!Number.isInteger(n))
|
|
110
|
+
throw new Error(`telegram message_id must be an integer: ${addr.messageId}`);
|
|
111
|
+
return n;
|
|
112
|
+
}
|
|
113
|
+
// Tell tail.ts to stop refreshing the typing indicator (the agent has replied).
|
|
114
|
+
function signalReplyComplete(platform, chat) {
|
|
115
|
+
const dir = join(STATE_DIR, '.typing-stop');
|
|
116
|
+
try {
|
|
117
|
+
mkdirSync(dir, { recursive: true });
|
|
118
|
+
writeFileSync(join(dir, `${platform}_${chat}`), '');
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
log.warn({ err: errMsg(err) }, 'typing stop-signal write failed');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function clearReaction(addr) {
|
|
125
|
+
if (addr.platform === 'telegram') {
|
|
126
|
+
await tg('setMessageReaction', { chat_id: addr.chat, message_id: tgMessageId(addr), reaction: [] });
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
await discord.setReaction(addr.chat, addr.messageId, '');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async function readStdinText() {
|
|
133
|
+
if (process.stdin.isTTY)
|
|
134
|
+
return '';
|
|
135
|
+
const chunks = [];
|
|
136
|
+
for await (const c of process.stdin)
|
|
137
|
+
chunks.push(c);
|
|
138
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
139
|
+
}
|
|
140
|
+
async function resolveText(flags) {
|
|
141
|
+
if (typeof flags.text === 'string')
|
|
142
|
+
return flags.text;
|
|
143
|
+
const stdin = (await readStdinText()).replace(/\n$/, '');
|
|
144
|
+
if (!stdin)
|
|
145
|
+
throw new Error('--text is required (or pipe text on stdin)');
|
|
146
|
+
return stdin;
|
|
147
|
+
}
|
|
148
|
+
function readTelegramOpts(flags) {
|
|
149
|
+
const opts = {};
|
|
150
|
+
if (flags['parse-mode']) {
|
|
151
|
+
const v = String(flags['parse-mode']);
|
|
152
|
+
if (v !== 'HTML' && v !== 'MarkdownV2')
|
|
153
|
+
throw new Error("--parse-mode must be 'HTML' or 'MarkdownV2'");
|
|
154
|
+
opts.parseMode = v;
|
|
155
|
+
}
|
|
156
|
+
if (flags['no-link-preview'])
|
|
157
|
+
opts.disableLinkPreview = true;
|
|
158
|
+
if (flags['buttons-json'])
|
|
159
|
+
opts.buttons = JSON.parse(String(flags['buttons-json']));
|
|
160
|
+
return opts;
|
|
161
|
+
}
|
|
162
|
+
function maskToken(token) {
|
|
163
|
+
if (!token)
|
|
164
|
+
return '';
|
|
165
|
+
if (token.length <= 8)
|
|
166
|
+
return '••••';
|
|
167
|
+
return `${token.slice(0, 6)}…${token.slice(-2)}`;
|
|
168
|
+
}
|
|
169
|
+
const EXT_FROM_MIME = {
|
|
170
|
+
'image/png': 'png',
|
|
171
|
+
'image/jpeg': 'jpg',
|
|
172
|
+
'image/gif': 'gif',
|
|
173
|
+
'image/webp': 'webp',
|
|
174
|
+
};
|
|
175
|
+
// Path to the SKILL.md bundled with the npm package. dist/cli.js → ../skills/metro/SKILL.md.
|
|
176
|
+
const BUNDLED_SKILL = join(dirname(fileURLToPath(import.meta.url)), '..', 'skills', 'metro', 'SKILL.md');
|
|
177
|
+
const SKILL_RUNTIMES = ['claude-code', 'codex'];
|
|
178
|
+
const SKILL_SCOPES = ['user', 'project'];
|
|
179
|
+
const skillFile = (runtime, scope) => join(skillDir(runtime, scope), 'SKILL.md');
|
|
180
|
+
// ---------------- setup ----------------------------------------------------
|
|
181
|
+
const TOKEN_KEYS = { telegram: 'TELEGRAM_BOT_TOKEN', discord: 'DISCORD_BOT_TOKEN' };
|
|
182
|
+
async function cmdSetup(positional, flags) {
|
|
183
|
+
const [sub, value] = positional;
|
|
184
|
+
if (!sub)
|
|
185
|
+
return cmdSetupStatus(flags);
|
|
186
|
+
if (sub === 'telegram' || sub === 'discord') {
|
|
187
|
+
if (!value)
|
|
188
|
+
throw new Error(`metro setup ${sub} <token> — token is required`);
|
|
189
|
+
const trimmed = value.trim();
|
|
190
|
+
let identity;
|
|
191
|
+
if (!flags['no-validate']) {
|
|
192
|
+
// Validate against the platform before persisting — catches typos /
|
|
193
|
+
// wrong-token-from-portal / rotated tokens at the earliest moment.
|
|
194
|
+
process.env[TOKEN_KEYS[sub]] = trimmed;
|
|
195
|
+
try {
|
|
196
|
+
identity = sub === 'telegram'
|
|
197
|
+
? `@${(await telegram.getMe()).username}`
|
|
198
|
+
: (await discord.getMe()).username;
|
|
199
|
+
}
|
|
200
|
+
catch (err) {
|
|
201
|
+
delete process.env[TOKEN_KEYS[sub]];
|
|
202
|
+
throw exitErr(`token rejected by ${sub}: ${errMsg(err)} (re-run with --no-validate to save anyway)`, 3);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const env = readDotenv(CONFIG_ENV_FILE);
|
|
206
|
+
env[TOKEN_KEYS[sub]] = trimmed;
|
|
207
|
+
writeDotenv(CONFIG_ENV_FILE, env);
|
|
208
|
+
const human = identity
|
|
209
|
+
? `saved ${TOKEN_KEYS[sub]} (verified as ${identity}) to ${CONFIG_ENV_FILE} (chmod 0600)`
|
|
210
|
+
: `saved ${TOKEN_KEYS[sub]} to ${CONFIG_ENV_FILE} (chmod 0600)`;
|
|
211
|
+
emitResult(flags, human, { ok: true, saved: TOKEN_KEYS[sub], path: CONFIG_ENV_FILE, verified_as: identity ?? null });
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (sub === 'clear') {
|
|
215
|
+
const target = value ?? 'all';
|
|
216
|
+
const env = readDotenv(CONFIG_ENV_FILE);
|
|
217
|
+
if (target === 'all') {
|
|
218
|
+
delete env.TELEGRAM_BOT_TOKEN;
|
|
219
|
+
delete env.DISCORD_BOT_TOKEN;
|
|
220
|
+
}
|
|
221
|
+
else if (target === 'telegram' || target === 'discord') {
|
|
222
|
+
delete env[TOKEN_KEYS[target]];
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
throw new Error(`metro setup clear <telegram|discord|all> — got '${target}'`);
|
|
226
|
+
}
|
|
227
|
+
writeDotenv(CONFIG_ENV_FILE, env);
|
|
228
|
+
const human = `cleared ${target === 'all' ? 'all metro tokens' : TOKEN_KEYS[target]} from ${CONFIG_ENV_FILE}`;
|
|
229
|
+
emitResult(flags, human, { ok: true, cleared: target, path: CONFIG_ENV_FILE });
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (sub === 'skill')
|
|
233
|
+
return cmdSetupSkill(flags);
|
|
234
|
+
throw new Error(`unknown setup subcommand '${sub}' (try: telegram, discord, clear, skill)`);
|
|
235
|
+
}
|
|
236
|
+
function readSkillState() {
|
|
237
|
+
return Object.fromEntries(SKILL_RUNTIMES.map(r => [r, Object.fromEntries(SKILL_SCOPES.map(s => [s, existsSync(skillFile(r, s))]))]));
|
|
238
|
+
}
|
|
239
|
+
async function cmdSetupStatus(flags) {
|
|
240
|
+
loadMetroEnv();
|
|
241
|
+
const tg = process.env.TELEGRAM_BOT_TOKEN ?? '';
|
|
242
|
+
const dc = process.env.DISCORD_BOT_TOKEN ?? '';
|
|
243
|
+
const skills = readSkillState();
|
|
244
|
+
const anySkill = SKILL_RUNTIMES.some(r => SKILL_SCOPES.some(s => skills[r][s]));
|
|
245
|
+
if (isJson(flags)) {
|
|
246
|
+
process.stdout.write(JSON.stringify({
|
|
247
|
+
version: pkg.version,
|
|
248
|
+
config_env_file: CONFIG_ENV_FILE,
|
|
249
|
+
tokens: {
|
|
250
|
+
telegram: { set: !!tg, masked: maskToken(tg) },
|
|
251
|
+
discord: { set: !!dc, masked: maskToken(dc) },
|
|
252
|
+
},
|
|
253
|
+
skills,
|
|
254
|
+
}) + '\n');
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const fmtSkill = (s) => {
|
|
258
|
+
const set = SKILL_SCOPES.filter(k => s[k]);
|
|
259
|
+
return set.length ? `installed (${set.join(' + ')})` : 'not installed';
|
|
260
|
+
};
|
|
261
|
+
process.stdout.write(`metro ${pkg.version}\n\n` +
|
|
262
|
+
`config: ${CONFIG_ENV_FILE}${existsSync(CONFIG_ENV_FILE) ? '' : ' (not yet written)'}\n\n` +
|
|
263
|
+
` TELEGRAM_BOT_TOKEN ${tg ? `set (${maskToken(tg)})` : 'not set'}\n` +
|
|
264
|
+
` DISCORD_BOT_TOKEN ${dc ? `set (${maskToken(dc)})` : 'not set'}\n\n` +
|
|
265
|
+
'Skills:\n' +
|
|
266
|
+
` claude-code ${fmtSkill(skills['claude-code'])}\n` +
|
|
267
|
+
` codex ${fmtSkill(skills.codex)}\n\n`);
|
|
268
|
+
if (!tg && !dc) {
|
|
269
|
+
process.stdout.write('Get started:\n' +
|
|
270
|
+
' 1. metro setup telegram <token> # https://t.me/BotFather\n' +
|
|
271
|
+
' metro setup discord <token> # https://discord.com/developers/applications\n' +
|
|
272
|
+
' 2. metro setup skill # auto-onboard your agent (writes to both runtimes)\n' +
|
|
273
|
+
' 3. metro doctor # verify everything works\n' +
|
|
274
|
+
' 4. metro # start the inbound stream\n');
|
|
275
|
+
}
|
|
276
|
+
else if (!anySkill) {
|
|
277
|
+
process.stdout.write('Next: `metro setup skill` to auto-onboard your agent. Then `metro doctor`, then `metro`.\n');
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
process.stdout.write('Run `metro` to start the inbound stream, or `metro doctor` to verify.\n');
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
async function cmdSetupSkill(flags) {
|
|
284
|
+
const scope = flags.project ? 'project' : 'user';
|
|
285
|
+
if (flags.clear) {
|
|
286
|
+
const removed = [];
|
|
287
|
+
for (const runtime of SKILL_RUNTIMES) {
|
|
288
|
+
const dir = skillDir(runtime, scope);
|
|
289
|
+
if (existsSync(dir)) {
|
|
290
|
+
rmSync(dir, { recursive: true, force: true });
|
|
291
|
+
removed.push(dir);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
emitResult(flags, removed.length ? `removed:\n ${removed.join('\n ')}` : '(no skills installed at this scope)', { ok: true, cleared: removed });
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (!existsSync(BUNDLED_SKILL))
|
|
298
|
+
throw new Error(`bundled SKILL.md missing at ${BUNDLED_SKILL} (broken install?)`);
|
|
299
|
+
const written = SKILL_RUNTIMES.map(r => {
|
|
300
|
+
const dest = skillFile(r, scope);
|
|
301
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
302
|
+
copyFileSync(BUNDLED_SKILL, dest);
|
|
303
|
+
return dest;
|
|
304
|
+
});
|
|
305
|
+
emitResult(flags, `wrote skill (${scope}) to:\n ${written.join('\n ')}\n\nThe agent will pick it up on its next session start.`, { ok: true, scope, paths: written });
|
|
306
|
+
}
|
|
307
|
+
async function cmdDoctor(flags) {
|
|
308
|
+
loadMetroEnv();
|
|
309
|
+
const checks = [];
|
|
310
|
+
// tokens
|
|
311
|
+
const cfg = configuredPlatforms();
|
|
312
|
+
checks.push({
|
|
313
|
+
name: 'tokens',
|
|
314
|
+
ok: cfg.telegram || cfg.discord,
|
|
315
|
+
detail: cfg.telegram || cfg.discord
|
|
316
|
+
? `loaded from ${existsSync(CONFIG_ENV_FILE) ? CONFIG_ENV_FILE : 'process env'}`
|
|
317
|
+
: 'no platform configured — run `metro setup telegram|discord <token>`',
|
|
318
|
+
});
|
|
319
|
+
// gateway / API healthcheck per configured platform
|
|
320
|
+
for (const [platform, getMe] of [['telegram', telegram.getMe], ['discord', discord.getMe]]) {
|
|
321
|
+
if (!cfg[platform]) {
|
|
322
|
+
checks.push({ name: platform, ok: null, detail: 'not configured' });
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
try {
|
|
326
|
+
const me = await getMe();
|
|
327
|
+
checks.push({ name: platform, ok: true, detail: `getMe → ${platform === 'telegram' ? '@' : ''}${me.username}` });
|
|
328
|
+
}
|
|
329
|
+
catch (err) {
|
|
330
|
+
checks.push({ name: platform, ok: false, detail: errMsg(err) });
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// tail process state. A stale lockfile (process gone) is informational —
|
|
334
|
+
// the next `metro` start auto-reclaims it, so we don't fail the doctor on it.
|
|
335
|
+
const lockFile = join(STATE_DIR, '.tail-lock');
|
|
336
|
+
if (!existsSync(lockFile)) {
|
|
337
|
+
checks.push({ name: 'stream', ok: null, detail: 'not running' });
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
try {
|
|
341
|
+
const pid = Number(readFileSync(lockFile, 'utf8').trim());
|
|
342
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
343
|
+
throw new Error('invalid pid');
|
|
344
|
+
process.kill(pid, 0);
|
|
345
|
+
checks.push({ name: 'stream', ok: true, detail: `running (pid ${pid})` });
|
|
346
|
+
}
|
|
347
|
+
catch {
|
|
348
|
+
checks.push({ name: 'stream', ok: null, detail: 'stale lockfile (will auto-reclaim on next start)' });
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// skill install + freshness vs bundled
|
|
352
|
+
for (const runtime of SKILL_RUNTIMES) {
|
|
353
|
+
const present = SKILL_SCOPES.filter(s => existsSync(skillFile(runtime, s)));
|
|
354
|
+
if (present.length === 0) {
|
|
355
|
+
checks.push({ name: `skill: ${runtime}`, ok: null, detail: 'not installed — run `metro setup skill` (writes both runtimes)' });
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
const bundled = existsSync(BUNDLED_SKILL) ? readFileSync(BUNDLED_SKILL) : null;
|
|
359
|
+
const stale = bundled
|
|
360
|
+
? present.filter(s => !readFileSync(skillFile(runtime, s)).equals(bundled))
|
|
361
|
+
: [];
|
|
362
|
+
checks.push(stale.length === 0
|
|
363
|
+
? { name: `skill: ${runtime}`, ok: true, detail: present.join(' + ') }
|
|
364
|
+
: { name: `skill: ${runtime}`, ok: false, detail: `stale at ${stale.join(' + ')} — re-run \`metro setup skill${stale.includes('project') ? ' --project' : ''}\`` });
|
|
365
|
+
}
|
|
366
|
+
if (isJson(flags)) {
|
|
367
|
+
process.stdout.write(JSON.stringify({ checks }) + '\n');
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
process.stdout.write('metro doctor\n\n');
|
|
371
|
+
for (const c of checks) {
|
|
372
|
+
const icon = c.ok === true ? '✓' : c.ok === false ? '✗' : '–';
|
|
373
|
+
process.stdout.write(` ${icon} ${c.name.padEnd(20)} ${c.detail}\n`);
|
|
374
|
+
}
|
|
375
|
+
process.stdout.write('\n');
|
|
376
|
+
}
|
|
377
|
+
if (checks.some(c => c.ok === false))
|
|
378
|
+
throw exitErr('one or more checks failed', 3);
|
|
379
|
+
}
|
|
380
|
+
// ---------------- update ---------------------------------------------------
|
|
381
|
+
async function cmdUpdate(flags) {
|
|
382
|
+
// While metro is in prerelease, the @beta dist-tag is what we publish.
|
|
383
|
+
// After GA, swap to 'latest' (or auto-pick from current version's prerelease tag).
|
|
384
|
+
const tag = pkg.version.includes('-') ? 'beta' : 'latest';
|
|
385
|
+
const res = await fetch('https://registry.npmjs.org/@stage-labs/metro', { signal: AbortSignal.timeout(15_000) });
|
|
386
|
+
if (!res.ok)
|
|
387
|
+
throw new Error(`npm registry: ${res.status}`);
|
|
388
|
+
const data = (await res.json());
|
|
389
|
+
const latest = data['dist-tags']?.[tag];
|
|
390
|
+
if (!latest)
|
|
391
|
+
throw new Error(`no '${tag}' dist-tag for @stage-labs/metro`);
|
|
392
|
+
if (latest === pkg.version) {
|
|
393
|
+
emitResult(flags, `already on ${pkg.version} (latest ${tag})`, { ok: true, current: pkg.version, latest, upgraded: false });
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const argv1 = process.argv[1] ?? '';
|
|
397
|
+
const spec = `@stage-labs/metro@${tag}`;
|
|
398
|
+
const argv = argv1.includes('/.bun/') || argv1.includes('\\bun\\') ? ['bun', 'add', '-g', spec]
|
|
399
|
+
: argv1.includes('/pnpm/') || argv1.includes('\\pnpm\\') ? ['pnpm', 'add', '-g', spec]
|
|
400
|
+
: ['npm', 'install', '-g', spec];
|
|
401
|
+
if (isJson(flags)) {
|
|
402
|
+
process.stdout.write(JSON.stringify({ ok: true, current: pkg.version, latest, command: argv.join(' '), upgraded: 'pending' }) + '\n');
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
process.stdout.write(`metro ${pkg.version} → ${latest}\n$ ${argv.join(' ')}\n`);
|
|
406
|
+
}
|
|
407
|
+
// Snapshot which skill destinations are installed BEFORE the package
|
|
408
|
+
// manager replaces our binary — after the spawn, BUNDLED_SKILL on disk
|
|
409
|
+
// points at the new content (npm install -g overwrites in place), so a
|
|
410
|
+
// fresh copyFileSync picks it up automatically.
|
|
411
|
+
const refreshTargets = SKILL_RUNTIMES.flatMap(r => SKILL_SCOPES.filter(s => existsSync(skillFile(r, s))).map(s => skillFile(r, s)));
|
|
412
|
+
await new Promise((resolve, reject) => {
|
|
413
|
+
const child = spawn(argv[0], argv.slice(1), { stdio: isJson(flags) ? 'ignore' : 'inherit' });
|
|
414
|
+
child.on('exit', code => (code === 0 ? resolve() : reject(new Error(`${argv[0]} exited with code ${code}`))));
|
|
415
|
+
child.on('error', reject);
|
|
416
|
+
});
|
|
417
|
+
// Refresh installed skills from the (now-updated) bundled SKILL.md.
|
|
418
|
+
if (existsSync(BUNDLED_SKILL)) {
|
|
419
|
+
for (const dest of refreshTargets) {
|
|
420
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
421
|
+
copyFileSync(BUNDLED_SKILL, dest);
|
|
422
|
+
}
|
|
423
|
+
if (refreshTargets.length > 0 && !isJson(flags)) {
|
|
424
|
+
process.stdout.write(`\nrefreshed installed skills:\n ${refreshTargets.join('\n ')}\n`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
// ---------------- inbound action commands ----------------------------------
|
|
429
|
+
async function cmdSend(flags) {
|
|
430
|
+
const addr = resolveAddr(flags, false);
|
|
431
|
+
const text = await resolveText(flags);
|
|
432
|
+
const sentMessageId = addr.platform === 'telegram'
|
|
433
|
+
? String((await tg('sendMessage', buildSendBody(addr.chat, text, readTelegramOpts(flags)))).message_id)
|
|
434
|
+
: await discord.sendMessage(addr.chat, text);
|
|
435
|
+
emitResult(flags, 'sent', { ok: true, platform: addr.platform, to: String(flags.to), sent_message_id: sentMessageId });
|
|
436
|
+
}
|
|
437
|
+
async function cmdReply(flags) {
|
|
438
|
+
const addr = resolveAddr(flags, true);
|
|
439
|
+
const text = await resolveText(flags);
|
|
440
|
+
let sentMessageId;
|
|
441
|
+
if (addr.platform === 'telegram') {
|
|
442
|
+
const body = buildSendBody(addr.chat, text, readTelegramOpts(flags));
|
|
443
|
+
body.reply_parameters = { message_id: tgMessageId(addr), allow_sending_without_reply: true };
|
|
444
|
+
sentMessageId = String((await tg('sendMessage', body)).message_id);
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
sentMessageId = await discord.replyToMessage(addr.chat, addr.messageId, text);
|
|
448
|
+
}
|
|
449
|
+
signalReplyComplete(addr.platform, addr.chat);
|
|
450
|
+
await clearReaction(addr).catch(err => log.warn({ err: errMsg(err) }, 'clear-reaction failed'));
|
|
451
|
+
emitResult(flags, 'sent', { ok: true, platform: addr.platform, to: String(flags.to), sent_message_id: sentMessageId });
|
|
452
|
+
}
|
|
453
|
+
async function cmdReact(flags) {
|
|
454
|
+
const addr = resolveAddr(flags, true);
|
|
455
|
+
const emoji = typeof flags.emoji === 'string' ? flags.emoji : '';
|
|
456
|
+
if (addr.platform === 'telegram') {
|
|
457
|
+
await tg('setMessageReaction', { chat_id: addr.chat, message_id: tgMessageId(addr), reaction: emoji ? [{ type: 'emoji', emoji }] : [] });
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
await discord.setReaction(addr.chat, addr.messageId, emoji);
|
|
461
|
+
}
|
|
462
|
+
emitResult(flags, emoji ? 'reacted' : 'cleared', { ok: true, to: String(flags.to), emoji, action: emoji ? 'reacted' : 'cleared' });
|
|
463
|
+
}
|
|
464
|
+
async function cmdEdit(flags) {
|
|
465
|
+
const addr = resolveAddr(flags, true);
|
|
466
|
+
const text = await resolveText(flags);
|
|
467
|
+
if (addr.platform === 'telegram') {
|
|
468
|
+
const body = buildSendBody(addr.chat, text, readTelegramOpts(flags));
|
|
469
|
+
body.message_id = tgMessageId(addr);
|
|
470
|
+
await tg('editMessageText', body);
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
await discord.editMessage(addr.chat, addr.messageId, text);
|
|
474
|
+
}
|
|
475
|
+
emitResult(flags, 'edited', { ok: true, to: String(flags.to) });
|
|
476
|
+
}
|
|
477
|
+
async function cmdDownload(flags) {
|
|
478
|
+
const addr = resolveAddr(flags, true);
|
|
479
|
+
const outDir = typeof flags.out === 'string' ? flags.out : join(STATE_DIR, 'attachments');
|
|
480
|
+
mkdirSync(outDir, { recursive: true });
|
|
481
|
+
const images = addr.platform === 'telegram'
|
|
482
|
+
? await Promise.all(telegram.getCachedAttachments(addr.chat, tgMessageId(addr)).map(a => telegram.downloadAttachment(a.file_id, a.mime)))
|
|
483
|
+
: await discord.fetchAttachments(addr.chat, addr.messageId);
|
|
484
|
+
if (images.length === 0) {
|
|
485
|
+
if (isJson(flags))
|
|
486
|
+
process.stdout.write(JSON.stringify({ ok: true, images: [] }) + '\n');
|
|
487
|
+
else
|
|
488
|
+
process.stderr.write('no image attachments found\n');
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const safeChat = addr.chat.replace(/[^\w-]/g, '_');
|
|
492
|
+
const safeMsg = (addr.messageId ?? '').replace(/[^\w-]/g, '_');
|
|
493
|
+
const out = images.map((img, i) => {
|
|
494
|
+
const ext = EXT_FROM_MIME[img.mime] ?? 'bin';
|
|
495
|
+
const path = join(outDir, `${addr.platform}_${safeChat}_${safeMsg}_${i}.${ext}`);
|
|
496
|
+
writeFileSync(path, Buffer.from(img.data, 'base64'));
|
|
497
|
+
return { path, mime: img.mime };
|
|
498
|
+
});
|
|
499
|
+
if (isJson(flags))
|
|
500
|
+
process.stdout.write(JSON.stringify({ ok: true, images: out }) + '\n');
|
|
501
|
+
else
|
|
502
|
+
process.stdout.write(out.map(o => o.path).join('\n') + '\n');
|
|
503
|
+
}
|
|
504
|
+
async function cmdFetch(flags) {
|
|
505
|
+
const addr = resolveAddr(flags, false);
|
|
506
|
+
if (addr.platform !== 'discord') {
|
|
507
|
+
throw new Error('metro fetch is Discord-only — Telegram has no recent-messages API for bots');
|
|
508
|
+
}
|
|
509
|
+
const limit = Number(flags.limit ?? 10);
|
|
510
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 100)
|
|
511
|
+
throw new Error('--limit must be an integer 1–100');
|
|
512
|
+
const msgs = await discord.fetchRecentMessages(addr.chat, limit);
|
|
513
|
+
if (isJson(flags)) {
|
|
514
|
+
process.stdout.write(JSON.stringify(msgs) + '\n');
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
const text = msgs.map(m => `[message_id=${m.message_id} ${m.timestamp}] ${m.author}: ${m.text}`).join('\n');
|
|
518
|
+
process.stdout.write((text || '(channel is empty)') + '\n');
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
// ---------------- main dispatcher ------------------------------------------
|
|
522
|
+
const COMMANDS = {
|
|
523
|
+
setup: (positional, flags) => cmdSetup(positional, flags),
|
|
524
|
+
doctor: (_, flags) => cmdDoctor(flags),
|
|
525
|
+
update: (_, flags) => cmdUpdate(flags),
|
|
526
|
+
reply: (_, flags) => cmdReply(flags),
|
|
527
|
+
react: (_, flags) => cmdReact(flags),
|
|
528
|
+
edit: (_, flags) => cmdEdit(flags),
|
|
529
|
+
send: (_, flags) => cmdSend(flags),
|
|
530
|
+
download: (_, flags) => cmdDownload(flags),
|
|
531
|
+
fetch: (_, flags) => cmdFetch(flags),
|
|
532
|
+
};
|
|
533
|
+
const NEEDS_ENV = new Set(['doctor', 'reply', 'react', 'edit', 'send', 'download', 'fetch']);
|
|
534
|
+
async function main() {
|
|
535
|
+
const cmd = process.argv[2];
|
|
536
|
+
if (cmd === '--version' || cmd === '-v') {
|
|
537
|
+
process.stdout.write(`${pkg.version}\n`);
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
if (cmd === '--help' || cmd === '-h') {
|
|
541
|
+
process.stdout.write(USAGE);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
// Bare `metro` runs the inbound stream — the primary action; one-shot
|
|
545
|
+
// subcommands are the secondary surface (matches claude / redis-server /
|
|
546
|
+
// nginx convention where the daemon's the default).
|
|
547
|
+
if (!cmd) {
|
|
548
|
+
await import('./tail.js');
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
const handler = COMMANDS[cmd];
|
|
552
|
+
if (!handler) {
|
|
553
|
+
process.stderr.write(`unknown command '${cmd}'\n\n${USAGE}`);
|
|
554
|
+
process.exit(1);
|
|
555
|
+
}
|
|
556
|
+
const { positional, flags } = parseArgs(process.argv.slice(3));
|
|
557
|
+
if (NEEDS_ENV.has(cmd))
|
|
558
|
+
loadMetroEnv();
|
|
559
|
+
try {
|
|
560
|
+
await handler(positional, flags);
|
|
561
|
+
}
|
|
562
|
+
catch (err) {
|
|
563
|
+
const code = err.code;
|
|
564
|
+
if (isJson(flags)) {
|
|
565
|
+
process.stdout.write(JSON.stringify({ ok: false, error: errMsg(err), code: code ?? 1 }) + '\n');
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
process.stderr.write(`error: ${errMsg(err)}\n`);
|
|
569
|
+
}
|
|
570
|
+
process.exit(typeof code === 'number' ? code : 1);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
await main();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// `<platform>:<chat>[/<message_id>]` — the wire format of every metro
|
|
2
|
+
// inbound `to` field, and the only address shape any subcommand accepts.
|
|
3
|
+
export function parseAddress(to, requireMessage) {
|
|
4
|
+
const colon = to.indexOf(':');
|
|
5
|
+
if (colon === -1) {
|
|
6
|
+
throw new Error(`invalid --to (expected '<platform>:<chat>[/<message_id>]'): ${to}`);
|
|
7
|
+
}
|
|
8
|
+
const platform = to.slice(0, colon);
|
|
9
|
+
if (platform !== 'telegram' && platform !== 'discord') {
|
|
10
|
+
throw new Error(`unknown platform '${platform}' in --to (expected 'telegram' or 'discord')`);
|
|
11
|
+
}
|
|
12
|
+
const rest = to.slice(colon + 1);
|
|
13
|
+
const slash = rest.indexOf('/');
|
|
14
|
+
const chat = slash === -1 ? rest : rest.slice(0, slash);
|
|
15
|
+
const messageId = slash === -1 ? undefined : rest.slice(slash + 1);
|
|
16
|
+
if (!chat)
|
|
17
|
+
throw new Error(`empty chat/channel id in --to: ${to}`);
|
|
18
|
+
if (requireMessage && !messageId)
|
|
19
|
+
throw new Error(`--to must include /<message_id>: ${to}`);
|
|
20
|
+
return { platform, chat, messageId };
|
|
21
|
+
}
|