@stage-labs/metro 0.1.0-beta.5 → 0.1.0-beta.7
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 +93 -177
- package/dist/cache.js +75 -0
- package/dist/cli/actions.js +139 -0
- package/dist/cli/config.js +185 -0
- package/dist/cli/index.js +106 -183
- package/dist/cli/skill.js +62 -0
- package/dist/cli/util.js +72 -0
- package/dist/codex-rc.js +236 -0
- package/dist/dispatcher.js +55 -163
- package/dist/history.js +84 -0
- package/dist/ipc.js +72 -0
- package/dist/stations/discord.js +200 -0
- package/dist/stations/index.js +73 -0
- package/dist/stations/{telegram/format.js → telegram-md.js} +9 -14
- package/dist/stations/telegram-upload.js +113 -0
- package/dist/stations/telegram.js +213 -0
- package/docs/agents.md +141 -48
- package/docs/uri-scheme.md +42 -23
- package/package.json +3 -2
- package/skills/metro/SKILL.md +241 -0
- package/dist/cli/lines.js +0 -40
- package/dist/cli/update.js +0 -27
- package/dist/helpers/async-queue.js +0 -41
- package/dist/helpers/scope-cache.js +0 -67
- package/dist/helpers/streaming.js +0 -219
- package/dist/helpers/turn.js +0 -62
- package/dist/stations/claude/index.js +0 -224
- package/dist/stations/codex/index.js +0 -226
- package/dist/stations/discord/index.js +0 -160
- package/dist/stations/github/index.js +0 -135
- package/dist/stations/line.js +0 -54
- package/dist/stations/listing.js +0 -16
- package/dist/stations/send.js +0 -19
- package/dist/stations/telegram/files.js +0 -31
- package/dist/stations/telegram/index.js +0 -209
- package/dist/stations/types.js +0 -2
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/** Setup / doctor / update — config-side commands consumed by cli.ts. */
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import pkg from '../../package.json' with { type: 'json' };
|
|
6
|
+
import { DiscordStation } from '../stations/discord.js';
|
|
7
|
+
import { TelegramStation } from '../stations/telegram.js';
|
|
8
|
+
import { errMsg } from '../log.js';
|
|
9
|
+
import { CONFIG_ENV_FILE, configuredPlatforms, loadMetroEnv, readDotenv, STATE_DIR, writeDotenv, } from '../paths.js';
|
|
10
|
+
import { emit, exitErr, isJson, writeJson } from './util.js';
|
|
11
|
+
import { cmdSetupSkill, skillStatus } from './skill.js';
|
|
12
|
+
const TOKEN_KEYS = { telegram: 'TELEGRAM_BOT_TOKEN', discord: 'DISCORD_BOT_TOKEN' };
|
|
13
|
+
const stationFor = (p) => p === 'telegram' ? new TelegramStation() : new DiscordStation();
|
|
14
|
+
const maskToken = (t) => !t ? '' : t.length <= 8 ? '••••' : `${t.slice(0, 6)}…${t.slice(-2)}`;
|
|
15
|
+
/** Apply token across CONFIG_ENV_FILE (always set/cleared) AND cwd/.env (only if it exists). */
|
|
16
|
+
function applyToken(key, value) {
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const path of [CONFIG_ENV_FILE, join(process.cwd(), '.env')]) {
|
|
19
|
+
if (path !== CONFIG_ENV_FILE && !existsSync(path))
|
|
20
|
+
continue;
|
|
21
|
+
const env = readDotenv(path);
|
|
22
|
+
if (value === null) {
|
|
23
|
+
if (!(key in env))
|
|
24
|
+
continue;
|
|
25
|
+
delete env[key];
|
|
26
|
+
}
|
|
27
|
+
else
|
|
28
|
+
env[key] = value;
|
|
29
|
+
writeDotenv(path, env);
|
|
30
|
+
out.push(path);
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
async function cmdSetupStatus(f) {
|
|
35
|
+
loadMetroEnv();
|
|
36
|
+
const tg = process.env.TELEGRAM_BOT_TOKEN ?? '', dc = process.env.DISCORD_BOT_TOKEN ?? '';
|
|
37
|
+
if (isJson(f))
|
|
38
|
+
return writeJson({
|
|
39
|
+
version: pkg.version, config_env_file: CONFIG_ENV_FILE,
|
|
40
|
+
tokens: { telegram: { set: !!tg, masked: maskToken(tg) }, discord: { set: !!dc, masked: maskToken(dc) } },
|
|
41
|
+
});
|
|
42
|
+
const cfgState = existsSync(CONFIG_ENV_FILE) ? '' : ' (not yet written)';
|
|
43
|
+
const getStarted = !tg && !dc
|
|
44
|
+
? 'Get started:\n 1. metro setup telegram <token> # https://t.me/BotFather'
|
|
45
|
+
+ '\n metro setup discord <token> # https://discord.com/developers/applications'
|
|
46
|
+
+ '\n 2. metro doctor\n 3. metro\n'
|
|
47
|
+
: 'Run `metro` to start the dispatcher, or `metro doctor` to verify.\n';
|
|
48
|
+
process.stdout.write(`metro ${pkg.version}\n\nconfig: ${CONFIG_ENV_FILE}${cfgState}\n\n`
|
|
49
|
+
+ ` TELEGRAM_BOT_TOKEN ${tg ? `set (${maskToken(tg)})` : 'not set'}\n`
|
|
50
|
+
+ ` DISCORD_BOT_TOKEN ${dc ? `set (${maskToken(dc)})` : 'not set'}\n\n${getStarted}`);
|
|
51
|
+
}
|
|
52
|
+
export async function cmdSetup(p, f) {
|
|
53
|
+
const [sub, value] = p;
|
|
54
|
+
if (!sub)
|
|
55
|
+
return cmdSetupStatus(f);
|
|
56
|
+
if (sub === 'skill')
|
|
57
|
+
return cmdSetupSkill(p.slice(1), f);
|
|
58
|
+
if (sub === 'telegram' || sub === 'discord') {
|
|
59
|
+
if (!value)
|
|
60
|
+
throw new Error(`metro setup ${sub} <token> — token is required`);
|
|
61
|
+
const trimmed = value.trim();
|
|
62
|
+
let identity;
|
|
63
|
+
if (!f['no-validate']) {
|
|
64
|
+
process.env[TOKEN_KEYS[sub]] = trimmed;
|
|
65
|
+
try {
|
|
66
|
+
const me = await stationFor(sub).getMe();
|
|
67
|
+
identity = sub === 'telegram' ? `@${me.username}` : me.username;
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
delete process.env[TOKEN_KEYS[sub]];
|
|
71
|
+
throw exitErr(`token rejected by ${sub}: ${errMsg(err)} (use --no-validate to save anyway)`, 3);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const paths = applyToken(TOKEN_KEYS[sub], trimmed);
|
|
75
|
+
const verified = identity ? ` (verified as ${identity})` : '';
|
|
76
|
+
emit(f, `saved ${TOKEN_KEYS[sub]}${verified} to ${paths.join(', ')}\nrestart metro for the new token to take effect.`, { ok: true, saved: TOKEN_KEYS[sub], paths, verified_as: identity ?? null });
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (sub === 'clear') {
|
|
80
|
+
const target = value ?? 'all';
|
|
81
|
+
if (target !== 'all' && target !== 'telegram' && target !== 'discord')
|
|
82
|
+
throw new Error(`metro setup clear <telegram|discord|all> — got '${target}'`);
|
|
83
|
+
const keys = target === 'all' ? Object.values(TOKEN_KEYS) : [TOKEN_KEYS[target]];
|
|
84
|
+
const paths = new Set();
|
|
85
|
+
for (const k of keys)
|
|
86
|
+
for (const path of applyToken(k, null))
|
|
87
|
+
paths.add(path);
|
|
88
|
+
const label = target === 'all' ? 'all metro tokens' : TOKEN_KEYS[target];
|
|
89
|
+
emit(f, `cleared ${label} from ${[...paths].join(', ') || '(no files had it)'}\nrestart metro for changes to take effect.`, { ok: true, cleared: target, paths: [...paths] });
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
throw new Error(`unknown setup subcommand '${sub}' (try: telegram, discord, clear, skill)`);
|
|
93
|
+
}
|
|
94
|
+
function tokenSource(key) {
|
|
95
|
+
const val = process.env[key];
|
|
96
|
+
if (!val)
|
|
97
|
+
return '';
|
|
98
|
+
for (const path of [join(process.cwd(), '.env'), CONFIG_ENV_FILE]) {
|
|
99
|
+
if (existsSync(path) && readDotenv(path)[key] === val)
|
|
100
|
+
return path;
|
|
101
|
+
}
|
|
102
|
+
return 'process env';
|
|
103
|
+
}
|
|
104
|
+
export async function cmdDoctor(_, f) {
|
|
105
|
+
loadMetroEnv();
|
|
106
|
+
const cfg = configuredPlatforms();
|
|
107
|
+
const checks = [];
|
|
108
|
+
const sources = [];
|
|
109
|
+
for (const p of ['telegram', 'discord']) {
|
|
110
|
+
if (!cfg[p]) {
|
|
111
|
+
checks.push({ name: p, ok: null, detail: 'not configured' });
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
sources.push(`${p}←${tokenSource(TOKEN_KEYS[p])}`);
|
|
115
|
+
try {
|
|
116
|
+
const me = await stationFor(p).getMe();
|
|
117
|
+
checks.push({ name: p, ok: true, detail: `getMe → ${p === 'telegram' ? '@' : ''}${me.username}` });
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
checks.push({ name: p, ok: false, detail: errMsg(err) });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
checks.unshift({
|
|
124
|
+
name: 'tokens', ok: cfg.telegram || cfg.discord,
|
|
125
|
+
detail: sources.length ? sources.join(', ') : 'no platform configured — run `metro setup telegram|discord <token>`',
|
|
126
|
+
});
|
|
127
|
+
const lockFile = join(STATE_DIR, '.tail-lock');
|
|
128
|
+
if (!existsSync(lockFile))
|
|
129
|
+
checks.push({ name: 'dispatcher', ok: null, detail: 'not running' });
|
|
130
|
+
else
|
|
131
|
+
try {
|
|
132
|
+
const pid = Number(readFileSync(lockFile, 'utf8').trim());
|
|
133
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
134
|
+
throw new Error('invalid pid');
|
|
135
|
+
process.kill(pid, 0);
|
|
136
|
+
checks.push({ name: 'dispatcher', ok: true, detail: `running (pid ${pid})` });
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
checks.push({ name: 'dispatcher', ok: null, detail: 'stale lockfile (auto-reclaims)' });
|
|
140
|
+
}
|
|
141
|
+
checks.push({
|
|
142
|
+
name: 'codex-rc', ok: null,
|
|
143
|
+
detail: process.env.METRO_CODEX_RC
|
|
144
|
+
? `push enabled → ${process.env.METRO_CODEX_RC}`
|
|
145
|
+
: 'not configured (set METRO_CODEX_RC=ws://… to enable Codex push)',
|
|
146
|
+
});
|
|
147
|
+
const sk = skillStatus();
|
|
148
|
+
const installed = Object.entries(sk).filter(([, ok]) => ok).map(([r]) => r);
|
|
149
|
+
checks.push({
|
|
150
|
+
name: 'skill', ok: installed.length ? true : null,
|
|
151
|
+
detail: installed.length ? `installed for ${installed.join(', ')}` : 'not installed (run `metro setup skill`)',
|
|
152
|
+
});
|
|
153
|
+
if (isJson(f))
|
|
154
|
+
return writeJson({ checks });
|
|
155
|
+
process.stdout.write('metro doctor\n\n');
|
|
156
|
+
for (const c of checks) {
|
|
157
|
+
const mark = c.ok === true ? '✓' : c.ok === false ? '✗' : '–';
|
|
158
|
+
process.stdout.write(` ${mark} ${c.name.padEnd(15)} ${c.detail}\n`);
|
|
159
|
+
}
|
|
160
|
+
process.stdout.write('\n');
|
|
161
|
+
if (checks.some(c => c.ok === false))
|
|
162
|
+
throw exitErr('one or more checks failed', 3);
|
|
163
|
+
}
|
|
164
|
+
export async function cmdUpdate(_, f) {
|
|
165
|
+
const tag = pkg.version.includes('-') ? 'beta' : 'latest';
|
|
166
|
+
const res = await fetch('https://registry.npmjs.org/@stage-labs/metro', { signal: AbortSignal.timeout(15_000) });
|
|
167
|
+
if (!res.ok)
|
|
168
|
+
throw new Error(`npm registry: ${res.status}`);
|
|
169
|
+
const latest = (await res.json())['dist-tags']?.[tag];
|
|
170
|
+
if (!latest)
|
|
171
|
+
throw new Error(`no '${tag}' dist-tag for @stage-labs/metro`);
|
|
172
|
+
if (latest === pkg.version) {
|
|
173
|
+
return emit(f, `already on ${pkg.version} (latest ${tag})`, { ok: true, current: pkg.version, latest, upgraded: false });
|
|
174
|
+
}
|
|
175
|
+
const argv1 = process.argv[1] ?? '', spec = `@stage-labs/metro@${tag}`;
|
|
176
|
+
const argv = argv1.includes('/.bun/') || argv1.includes('\\bun\\') ? ['bun', 'add', '-g', spec]
|
|
177
|
+
: argv1.includes('/pnpm/') || argv1.includes('\\pnpm\\') ? ['pnpm', 'add', '-g', spec]
|
|
178
|
+
: ['npm', 'install', '-g', spec];
|
|
179
|
+
emit(f, `metro ${pkg.version} → ${latest}\n$ ${argv.join(' ')}`, { ok: true, current: pkg.version, latest, command: argv.join(' ') });
|
|
180
|
+
await new Promise((resolve, reject) => {
|
|
181
|
+
const child = spawn(argv[0], argv.slice(1), { stdio: isJson(f) ? 'ignore' : 'inherit' });
|
|
182
|
+
child.on('exit', code => code === 0 ? resolve() : reject(new Error(`${argv[0]} exited ${code}`)));
|
|
183
|
+
child.on('error', reject);
|
|
184
|
+
});
|
|
185
|
+
}
|
package/dist/cli/index.js
CHANGED
|
@@ -1,209 +1,132 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { join } from 'node:path';
|
|
2
|
+
/** Metro CLI entry: parses argv, dispatches to subcommands, owns action + info commands. */
|
|
4
3
|
import pkg from '../../package.json' with { type: 'json' };
|
|
5
|
-
import { cmdLines } from './lines.js';
|
|
6
|
-
import { cmdUpdate } from './update.js';
|
|
7
|
-
import { DiscordStation } from '../stations/discord/index.js';
|
|
8
|
-
import { TelegramStation } from '../stations/telegram/index.js';
|
|
9
|
-
import { fmtCapabilities, listStations } from '../stations/listing.js';
|
|
10
|
-
import { sendToLine } from '../stations/send.js';
|
|
11
4
|
import { errMsg } from '../log.js';
|
|
12
|
-
import {
|
|
13
|
-
|
|
5
|
+
import { listLines } from '../cache.js';
|
|
6
|
+
import { fmtCapabilities, listStations } from '../stations/index.js';
|
|
7
|
+
import { loadMetroEnv } from '../paths.js';
|
|
8
|
+
import { readHistory } from '../history.js';
|
|
9
|
+
import { cmdDoctor, cmdSetup, cmdUpdate } from './config.js';
|
|
10
|
+
import { cmdDownload, cmdEdit, cmdFetch, cmdReact, cmdReply, cmdSend, } from './actions.js';
|
|
11
|
+
import { flagOne, isJson, parseArgs, writeJson, } from './util.js';
|
|
12
|
+
const USAGE = `metro — Telegram + Discord stream for your Claude Code / Codex agent
|
|
14
13
|
|
|
15
14
|
Usage:
|
|
16
|
-
metro Run the dispatcher
|
|
15
|
+
metro Run the dispatcher (emits JSON events on stdout).
|
|
17
16
|
metro setup [telegram|discord <token>] Save token, or show status with no args.
|
|
18
17
|
metro setup clear [telegram|discord|all] Remove tokens.
|
|
18
|
+
metro setup skill [clear] Install the metro skill into ~/.claude / ~/.codex.
|
|
19
19
|
metro doctor Health check.
|
|
20
20
|
metro stations List stations + capabilities.
|
|
21
|
-
metro lines List
|
|
22
|
-
metro send <line> <text>
|
|
21
|
+
metro lines List recently-seen conversations.
|
|
22
|
+
metro send <line> <text> [--image=<path>]… [--document=<path>]… [--voice=<path>] [--buttons=<json>]
|
|
23
|
+
Post a fresh message; repeat --image/--document for multi-file albums.
|
|
24
|
+
metro reply <line> <message_id> <text> [--image=… --document=… --voice=… --buttons=…]
|
|
25
|
+
Threaded reply (same flags as send).
|
|
26
|
+
metro edit <line> <message_id> <text> [--buttons=<json>]
|
|
27
|
+
Edit a previously-sent message (text + buttons).
|
|
28
|
+
metro react <line> <message_id> <emoji> Set or clear ('') a reaction.
|
|
29
|
+
metro download <line> <message_id> [--out=<dir>]
|
|
30
|
+
Download image attachments to disk.
|
|
31
|
+
metro fetch <line> [--limit=N] Recent-message lookback (Discord only).
|
|
32
|
+
metro history [--limit=N] [--line=…] [--station=…] [--kind=…] [--from=…] [--text=…] [--since=…]
|
|
33
|
+
Read the universal message log (newest first).
|
|
23
34
|
metro update Upgrade in place.
|
|
24
35
|
metro --version | --help
|
|
36
|
+
|
|
37
|
+
Lines: metro://<station>/<path>. See docs/uri-scheme.md.
|
|
38
|
+
Multi-line --text: pipe on stdin in place of the positional arg.
|
|
25
39
|
Exit codes: 0 success · 1 usage · 2 config · 3 upstream
|
|
26
40
|
`;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (!a.startsWith('--')) {
|
|
37
|
-
positional.push(a);
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
const eq = a.indexOf('=');
|
|
41
|
-
if (eq !== -1) {
|
|
42
|
-
flags[a.slice(2, eq)] = a.slice(eq + 1);
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
const key = a.slice(2), next = argv[i + 1];
|
|
46
|
-
if (next !== undefined && !next.startsWith('--')) {
|
|
47
|
-
flags[key] = next;
|
|
48
|
-
i++;
|
|
49
|
-
}
|
|
50
|
-
else
|
|
51
|
-
flags[key] = true;
|
|
52
|
-
}
|
|
53
|
-
return { positional, flags };
|
|
54
|
-
}
|
|
55
|
-
/** Apply a token across CONFIG_ENV_FILE (always set/cleared) AND cwd/.env (only if it exists). Returns paths touched. */
|
|
56
|
-
function applyTokenToAllEnvs(key, value) {
|
|
57
|
-
const out = [];
|
|
58
|
-
for (const path of [CONFIG_ENV_FILE, join(process.cwd(), '.env')]) {
|
|
59
|
-
if (path !== CONFIG_ENV_FILE && !existsSync(path))
|
|
60
|
-
continue;
|
|
61
|
-
const env = readDotenv(path);
|
|
62
|
-
if (value === null) {
|
|
63
|
-
if (!(key in env))
|
|
64
|
-
continue;
|
|
65
|
-
delete env[key];
|
|
66
|
-
}
|
|
67
|
-
else
|
|
68
|
-
env[key] = value;
|
|
69
|
-
writeDotenv(path, env);
|
|
70
|
-
out.push(path);
|
|
71
|
-
}
|
|
72
|
-
return out;
|
|
73
|
-
}
|
|
74
|
-
async function cmdSetup(positional, flags) {
|
|
75
|
-
const [sub, value] = positional;
|
|
76
|
-
if (!sub)
|
|
77
|
-
return cmdSetupStatus(flags);
|
|
78
|
-
if (sub === 'telegram' || sub === 'discord') {
|
|
79
|
-
if (!value)
|
|
80
|
-
throw new Error(`metro setup ${sub} <token> — token is required`);
|
|
81
|
-
const trimmed = value.trim();
|
|
82
|
-
let identity;
|
|
83
|
-
if (!flags['no-validate']) {
|
|
84
|
-
process.env[TOKEN_KEYS[sub]] = trimmed;
|
|
85
|
-
try {
|
|
86
|
-
identity = sub === 'telegram' ? `@${(await new TelegramStation().getMe()).username}` : (await new DiscordStation().getMe()).username;
|
|
87
|
-
}
|
|
88
|
-
catch (err) {
|
|
89
|
-
delete process.env[TOKEN_KEYS[sub]];
|
|
90
|
-
throw exitErr(`token rejected by ${sub}: ${errMsg(err)} (use --no-validate to save anyway)`, 3);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
const paths = applyTokenToAllEnvs(TOKEN_KEYS[sub], trimmed);
|
|
94
|
-
emit(flags, `saved ${TOKEN_KEYS[sub]}${identity ? ` (verified as ${identity})` : ''} to ${paths.join(', ')}\nrestart metro for the new token to take effect.`, { ok: true, saved: TOKEN_KEYS[sub], paths, verified_as: identity ?? null });
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
if (sub === 'clear') {
|
|
98
|
-
const target = value ?? 'all';
|
|
99
|
-
if (target !== 'all' && target !== 'telegram' && target !== 'discord')
|
|
100
|
-
throw new Error(`metro setup clear <telegram|discord|all> — got '${target}'`);
|
|
101
|
-
const keys = target === 'all' ? ['TELEGRAM_BOT_TOKEN', 'DISCORD_BOT_TOKEN'] : [TOKEN_KEYS[target]];
|
|
102
|
-
const paths = new Set();
|
|
103
|
-
for (const k of keys)
|
|
104
|
-
for (const p of applyTokenToAllEnvs(k, null))
|
|
105
|
-
paths.add(p);
|
|
106
|
-
const label = target === 'all' ? 'all metro tokens' : TOKEN_KEYS[target];
|
|
107
|
-
return void emit(flags, `cleared ${label} from ${[...paths].join(', ') || '(no files had it)'}\nrestart metro for changes to take effect.`, { ok: true, cleared: target, paths: [...paths] });
|
|
41
|
+
async function cmdStations(_, f) {
|
|
42
|
+
loadMetroEnv();
|
|
43
|
+
const rows = listStations();
|
|
44
|
+
if (isJson(f))
|
|
45
|
+
return writeJson({ stations: rows });
|
|
46
|
+
process.stdout.write('metro stations\n\n');
|
|
47
|
+
for (const s of rows) {
|
|
48
|
+
const mark = s.configured === true ? '✓' : s.configured === false ? '✗' : '·';
|
|
49
|
+
process.stdout.write(` ${mark} ${s.name.padEnd(10)} ${s.kind.padEnd(6)} ${fmtCapabilities(s.capabilities)}\n ${s.detail}\n`);
|
|
108
50
|
}
|
|
109
|
-
|
|
51
|
+
process.stdout.write('\n');
|
|
110
52
|
}
|
|
111
|
-
async function
|
|
53
|
+
async function cmdLines(_, f) {
|
|
112
54
|
loadMetroEnv();
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
55
|
+
const rows = listLines()
|
|
56
|
+
.map(({ line, entry }) => ({ line, name: entry.name ?? null, lastSeenAt: entry.lastSeenAt ?? null }))
|
|
57
|
+
.sort((a, b) => (b.lastSeenAt ?? '').localeCompare(a.lastSeenAt ?? ''));
|
|
58
|
+
if (isJson(f))
|
|
59
|
+
return writeJson({ lines: rows });
|
|
60
|
+
if (!rows.length)
|
|
61
|
+
return void process.stdout.write('metro lines\n\n (none yet — start the dispatcher and send a message)\n\n');
|
|
62
|
+
const widest = Math.max(...rows.map(r => r.line.length));
|
|
63
|
+
process.stdout.write('metro lines\n\n');
|
|
64
|
+
for (const r of rows) {
|
|
65
|
+
const when = r.lastSeenAt ? humanAgo(r.lastSeenAt) : '—';
|
|
66
|
+
const tag = r.name ? ` ${r.name.slice(0, 40)}${r.name.length > 40 ? '…' : ''}` : '';
|
|
67
|
+
process.stdout.write(` ${when.padEnd(10)} ${r.line.padEnd(widest)}${tag}\n`);
|
|
68
|
+
}
|
|
69
|
+
process.stdout.write('\n');
|
|
121
70
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return
|
|
71
|
+
function humanAgo(iso) {
|
|
72
|
+
const sec = Math.max(0, Math.floor((Date.now() - new Date(iso).getTime()) / 1000));
|
|
73
|
+
if (sec < 60)
|
|
74
|
+
return `${sec}s ago`;
|
|
75
|
+
if (sec < 3600)
|
|
76
|
+
return `${Math.floor(sec / 60)}m ago`;
|
|
77
|
+
if (sec < 86400)
|
|
78
|
+
return `${Math.floor(sec / 3600)}h ago`;
|
|
79
|
+
return `${Math.floor(sec / 86400)}d ago`;
|
|
131
80
|
}
|
|
132
|
-
async function
|
|
81
|
+
async function cmdHistory(_, f) {
|
|
133
82
|
loadMetroEnv();
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
catch (err) {
|
|
150
|
-
checks.push({ name: p, ok: false, detail: errMsg(err) });
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
checks.push(dispatcherCheck());
|
|
154
|
-
if (isJson(flags))
|
|
155
|
-
process.stdout.write(JSON.stringify({ checks }) + '\n');
|
|
156
|
-
else {
|
|
157
|
-
process.stdout.write('metro doctor\n\n');
|
|
158
|
-
for (const c of checks)
|
|
159
|
-
process.stdout.write(` ${c.ok === true ? '✓' : c.ok === false ? '✗' : '–'} ${c.name.padEnd(15)} ${c.detail}\n`);
|
|
160
|
-
process.stdout.write('\n');
|
|
161
|
-
}
|
|
162
|
-
if (checks.some(c => c.ok === false))
|
|
163
|
-
throw exitErr('one or more checks failed', 3);
|
|
164
|
-
}
|
|
165
|
-
function dispatcherCheck() {
|
|
166
|
-
const lockFile = join(STATE_DIR, '.tail-lock');
|
|
167
|
-
if (!existsSync(lockFile))
|
|
168
|
-
return { name: 'dispatcher', ok: null, detail: 'not running' };
|
|
169
|
-
try {
|
|
170
|
-
const pid = Number(readFileSync(lockFile, 'utf8').trim());
|
|
171
|
-
if (!Number.isInteger(pid) || pid <= 0)
|
|
172
|
-
throw new Error('invalid pid');
|
|
173
|
-
process.kill(pid, 0);
|
|
174
|
-
return { name: 'dispatcher', ok: true, detail: `running (pid ${pid})` };
|
|
83
|
+
const since = flagOne(f, 'since');
|
|
84
|
+
const entries = readHistory({
|
|
85
|
+
line: flagOne(f, 'line'),
|
|
86
|
+
station: flagOne(f, 'station'),
|
|
87
|
+
kind: flagOne(f, 'kind'),
|
|
88
|
+
from: flagOne(f, 'from'),
|
|
89
|
+
textContains: flagOne(f, 'text'),
|
|
90
|
+
since: since ? new Date(since) : undefined,
|
|
91
|
+
limit: Number(flagOne(f, 'limit')) || 50,
|
|
92
|
+
});
|
|
93
|
+
if (isJson(f))
|
|
94
|
+
return writeJson({ entries });
|
|
95
|
+
if (!entries.length) {
|
|
96
|
+
process.stdout.write('(no matching history entries)\n');
|
|
97
|
+
return;
|
|
175
98
|
}
|
|
176
|
-
|
|
177
|
-
|
|
99
|
+
process.stdout.write('time id kind from → to body\n');
|
|
100
|
+
for (const e of entries.reverse()) {
|
|
101
|
+
const ts = e.ts.slice(11, 19);
|
|
102
|
+
const from = pad(fmtActor(e.from, e.fromName), 24);
|
|
103
|
+
const to = pad(fmtActor(e.to), 24);
|
|
104
|
+
const body = e.text ?? (e.emoji ? `[react ${e.emoji}]` : '');
|
|
105
|
+
const text = body.length > 60 ? body.slice(0, 59) + '…' : body;
|
|
106
|
+
process.stdout.write(`${ts} ${e.id.padEnd(12)} ${e.kind.padEnd(12)} ${from} → ${to} ${text}\n`);
|
|
178
107
|
}
|
|
179
108
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
process.stdout.write('metro stations\n\n');
|
|
194
|
-
for (const s of rows) {
|
|
195
|
-
const mark = s.configured === true ? '✓' : s.configured === false ? '✗' : '·';
|
|
196
|
-
process.stdout.write(` ${mark} ${s.name.padEnd(10)} ${s.kind.padEnd(6)} ${fmtCapabilities(s.capabilities)}\n ${s.detail}\n`);
|
|
197
|
-
}
|
|
198
|
-
process.stdout.write('\n');
|
|
109
|
+
/** Compact display: fromName if known; else `station:@<id>` (user), `station:bot`, or `station:<id>`. */
|
|
110
|
+
function fmtActor(uri, name) {
|
|
111
|
+
if (name)
|
|
112
|
+
return name;
|
|
113
|
+
const m = uri.match(/^metro:\/\/([^/]+)(?:\/(?:(user|bot)\/)?(.*))?$/);
|
|
114
|
+
if (!m)
|
|
115
|
+
return uri;
|
|
116
|
+
const [, station, kind, rest] = m;
|
|
117
|
+
if (kind === 'bot')
|
|
118
|
+
return `${station}:bot`;
|
|
119
|
+
if (kind === 'user')
|
|
120
|
+
return `${station}:@${shortId(rest ?? '')}`;
|
|
121
|
+
return rest ? `${station}:${shortId(rest)}` : station;
|
|
199
122
|
}
|
|
123
|
+
const shortId = (s) => s.length <= 12 ? s : `${s.slice(0, 5)}…${s.slice(-4)}`;
|
|
124
|
+
const pad = (s, n) => (s.length > n ? `${s.slice(0, n - 1)}…` : s.padEnd(n));
|
|
200
125
|
const COMMANDS = {
|
|
201
|
-
setup: cmdSetup,
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
send: cmdSend,
|
|
206
|
-
update: (_, f) => cmdUpdate(isJson(f)),
|
|
126
|
+
setup: cmdSetup, doctor: cmdDoctor, stations: cmdStations, lines: cmdLines,
|
|
127
|
+
send: cmdSend, reply: cmdReply, edit: cmdEdit, react: cmdReact,
|
|
128
|
+
download: cmdDownload, fetch: cmdFetch,
|
|
129
|
+
history: cmdHistory, update: cmdUpdate,
|
|
207
130
|
};
|
|
208
131
|
async function main() {
|
|
209
132
|
const cmd = process.argv[2];
|
|
@@ -227,7 +150,7 @@ async function main() {
|
|
|
227
150
|
catch (err) {
|
|
228
151
|
const code = err.code;
|
|
229
152
|
if (isJson(flags))
|
|
230
|
-
|
|
153
|
+
writeJson({ ok: false, error: errMsg(err), code: code ?? 1 });
|
|
231
154
|
else
|
|
232
155
|
process.stderr.write(`error: ${errMsg(err)}\n`);
|
|
233
156
|
process.exit(typeof code === 'number' ? code : 1);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** `metro setup skill` — install the bundled SKILL.md into each detected agent runtime. */
|
|
2
|
+
import { copyFileSync, existsSync, mkdirSync, unlinkSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { errMsg } from '../log.js';
|
|
7
|
+
import { emit, exitErr } from './util.js';
|
|
8
|
+
const RUNTIME_DIRS = {
|
|
9
|
+
'claude-code': join(homedir(), '.claude', 'skills', 'metro'),
|
|
10
|
+
codex: join(homedir(), '.codex', 'skills', 'metro'),
|
|
11
|
+
};
|
|
12
|
+
/** dist/cli/skill.js → <package-root>/skills/metro/SKILL.md */
|
|
13
|
+
const bundledPath = () => join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'skills', 'metro', 'SKILL.md');
|
|
14
|
+
const dest = (r) => join(RUNTIME_DIRS[r], 'SKILL.md');
|
|
15
|
+
export const skillStatus = () => ({
|
|
16
|
+
'claude-code': existsSync(dest('claude-code')),
|
|
17
|
+
codex: existsSync(dest('codex')),
|
|
18
|
+
});
|
|
19
|
+
export async function cmdSetupSkill(p, f) {
|
|
20
|
+
const [sub] = p;
|
|
21
|
+
if (sub === 'clear')
|
|
22
|
+
return clear(f);
|
|
23
|
+
if (sub && sub !== 'install')
|
|
24
|
+
throw exitErr(`unknown skill subcommand '${sub}' (try: install, clear)`, 1);
|
|
25
|
+
return install(f);
|
|
26
|
+
}
|
|
27
|
+
function install(f) {
|
|
28
|
+
const src = bundledPath();
|
|
29
|
+
if (!existsSync(src))
|
|
30
|
+
throw exitErr(`bundled SKILL.md missing at ${src} (broken install?)`, 2);
|
|
31
|
+
const installed = [];
|
|
32
|
+
for (const r of Object.keys(RUNTIME_DIRS)) {
|
|
33
|
+
if (!existsSync(join(homedir(), r === 'claude-code' ? '.claude' : '.codex')))
|
|
34
|
+
continue;
|
|
35
|
+
try {
|
|
36
|
+
mkdirSync(RUNTIME_DIRS[r], { recursive: true });
|
|
37
|
+
copyFileSync(src, dest(r));
|
|
38
|
+
installed.push(dest(r));
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
throw exitErr(`failed to install skill for ${r}: ${errMsg(err)}`, 2);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!installed.length) {
|
|
45
|
+
throw exitErr('no agent runtime detected (~/.claude or ~/.codex). Install one and rerun.', 2);
|
|
46
|
+
}
|
|
47
|
+
emit(f, `installed metro skill → ${installed.join(', ')}`, { ok: true, installed });
|
|
48
|
+
}
|
|
49
|
+
function clear(f) {
|
|
50
|
+
const removed = [];
|
|
51
|
+
for (const r of Object.keys(RUNTIME_DIRS)) {
|
|
52
|
+
const path = dest(r);
|
|
53
|
+
if (existsSync(path)) {
|
|
54
|
+
try {
|
|
55
|
+
unlinkSync(path);
|
|
56
|
+
removed.push(path);
|
|
57
|
+
}
|
|
58
|
+
catch { /* ignore */ }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
emit(f, removed.length ? `removed metro skill from ${removed.join(', ')}` : 'no installed skill found', { ok: true, removed });
|
|
62
|
+
}
|
package/dist/cli/util.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Shared CLI primitives consumed by index.ts + config.ts. */
|
|
2
|
+
export const exitErr = (msg, code) => Object.assign(new Error(msg), { code });
|
|
3
|
+
export const isJson = (f) => f.json === true;
|
|
4
|
+
export const writeJson = (obj) => void process.stdout.write(JSON.stringify(obj) + '\n');
|
|
5
|
+
export const emit = (f, human, structured) => isJson(f) ? writeJson(structured) : void process.stdout.write(human + '\n');
|
|
6
|
+
export const need = (positional, min, usage) => {
|
|
7
|
+
if (positional.length < min)
|
|
8
|
+
throw exitErr(`usage: ${usage}`, 1);
|
|
9
|
+
};
|
|
10
|
+
/** Return the last string value for `key` (or undefined). */
|
|
11
|
+
export function flagOne(f, key) {
|
|
12
|
+
const v = f[key];
|
|
13
|
+
if (typeof v === 'string')
|
|
14
|
+
return v;
|
|
15
|
+
if (Array.isArray(v) && v.length)
|
|
16
|
+
return v[v.length - 1];
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
/** Return all string values for `key`, also splitting comma-separated entries. */
|
|
20
|
+
export function flagList(f, key) {
|
|
21
|
+
const v = f[key];
|
|
22
|
+
const raw = typeof v === 'string' ? [v] : Array.isArray(v) ? v : [];
|
|
23
|
+
return raw.flatMap(s => s.split(',').map(p => p.trim()).filter(Boolean));
|
|
24
|
+
}
|
|
25
|
+
export function parseArgs(argv) {
|
|
26
|
+
const positional = [], flags = {};
|
|
27
|
+
const add = (k, val) => {
|
|
28
|
+
const cur = flags[k];
|
|
29
|
+
if (cur === undefined) {
|
|
30
|
+
flags[k] = val;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (typeof val === 'boolean') {
|
|
34
|
+
flags[k] = val;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
flags[k] = Array.isArray(cur) ? [...cur, val] : [cur, val];
|
|
38
|
+
};
|
|
39
|
+
for (let i = 0; i < argv.length; i++) {
|
|
40
|
+
const a = argv[i];
|
|
41
|
+
if (!a.startsWith('--')) {
|
|
42
|
+
positional.push(a);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const eq = a.indexOf('=');
|
|
46
|
+
if (eq !== -1) {
|
|
47
|
+
add(a.slice(2, eq), a.slice(eq + 1));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const next = argv[i + 1];
|
|
51
|
+
if (next !== undefined && !next.startsWith('--')) {
|
|
52
|
+
add(a.slice(2), next);
|
|
53
|
+
i++;
|
|
54
|
+
}
|
|
55
|
+
else
|
|
56
|
+
add(a.slice(2), true);
|
|
57
|
+
}
|
|
58
|
+
return { positional, flags };
|
|
59
|
+
}
|
|
60
|
+
export async function resolveText(positional, from) {
|
|
61
|
+
if (positional.length > from)
|
|
62
|
+
return positional.slice(from).join(' ');
|
|
63
|
+
if (process.stdin.isTTY)
|
|
64
|
+
throw exitErr('text is required (or pipe text on stdin)', 1);
|
|
65
|
+
const chunks = [];
|
|
66
|
+
for await (const c of process.stdin)
|
|
67
|
+
chunks.push(c);
|
|
68
|
+
const stdin = Buffer.concat(chunks).toString('utf8').replace(/\n$/, '');
|
|
69
|
+
if (!stdin)
|
|
70
|
+
throw exitErr('text is required (or pipe text on stdin)', 1);
|
|
71
|
+
return stdin;
|
|
72
|
+
}
|