@stage-labs/metro 0.1.0-beta.13 → 0.1.0-beta.14

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.
Files changed (41) hide show
  1. package/README.md +76 -189
  2. package/dist/broker/claims.js +144 -0
  3. package/dist/{broker.js → broker/history-stream.js} +44 -99
  4. package/dist/cli/config.js +115 -121
  5. package/dist/cli/index.js +20 -58
  6. package/dist/cli/tail.js +161 -113
  7. package/dist/cli/webhook.js +103 -3
  8. package/dist/{codex-rc.js → codex-rc/client.js} +12 -32
  9. package/dist/codex-rc/protocol.js +38 -0
  10. package/dist/dispatcher/server.js +130 -0
  11. package/dist/dispatcher.js +51 -82
  12. package/dist/history.js +43 -18
  13. package/dist/ipc.js +28 -10
  14. package/dist/lines.js +54 -0
  15. package/dist/local-identity.js +80 -0
  16. package/dist/paths.js +58 -12
  17. package/dist/trains/protocol.js +99 -0
  18. package/dist/trains/supervisor.js +210 -0
  19. package/dist/tunnel.js +39 -1
  20. package/docs/broker.md +88 -136
  21. package/docs/monitor.md +19 -2
  22. package/docs/uri-scheme.md +10 -7
  23. package/examples/README.md +32 -0
  24. package/examples/telegram.ts +121 -0
  25. package/package.json +6 -5
  26. package/skills/metro/SKILL.md +63 -215
  27. package/dist/cache.js +0 -69
  28. package/dist/cli/actions.js +0 -206
  29. package/dist/cli/skill.js +0 -62
  30. package/dist/monitor.js +0 -194
  31. package/dist/registry.js +0 -48
  32. package/dist/stations/claude.js +0 -45
  33. package/dist/stations/codex.js +0 -68
  34. package/dist/stations/discord.js +0 -216
  35. package/dist/stations/index.js +0 -129
  36. package/dist/stations/telegram-md.js +0 -34
  37. package/dist/stations/telegram-upload.js +0 -113
  38. package/dist/stations/telegram.js +0 -234
  39. package/dist/stations/webhook.js +0 -103
  40. package/dist/webhooks.js +0 -41
  41. package/docs/users.md +0 -226
package/docs/users.md DELETED
@@ -1,226 +0,0 @@
1
- # Metro: a guide for Claude Code / Codex users
2
-
3
- You are running inside a session that has **launched `metro`** in the background. Metro emits a live stream of JSON events from Discord, Telegram, third-party webhooks (GitHub, Intercom, …), and other users on its stdout. Your job is to consume that stream and post replies back via subcommands.
4
-
5
- ## Starting metro
6
-
7
- The launch mechanics differ by runtime — pick the one that matches yours.
8
-
9
- ### Claude Code
10
-
11
- ```
12
- Bash(command: "metro", run_in_background: true)
13
- ```
14
-
15
- Then attach `Monitor` to its stdout. Each line is one JSON event you act on.
16
-
17
- ### Codex
18
-
19
- ```
20
- shell(command: "METRO_CODEX_RC=ws://127.0.0.1:8421 metro", run_in_background: true)
21
- ```
22
-
23
- Don't watch its stdout — Codex has no Monitor equivalent. Metro pushes each event into your thread via JSON-RPC `turn/start`, so events arrive as user input on your next turn. The user must have a daemon and TUI running for this to work:
24
-
25
- ```
26
- codex app-server --listen ws://127.0.0.1:8421 # daemon (terminal 1)
27
- codex --remote ws://127.0.0.1:8421 # TUI (this session — terminal 2)
28
- ```
29
-
30
- Run `metro doctor` if anything seems off.
31
-
32
- ## Event shape
33
-
34
- Every event is a **history entry** — the same record that's appended to `history.jsonl`. Fields: `kind` (`inbound`/`outbound`/`edit`/`react`), `id` (`msg_…`), `ts`, `station`, `line` (conversation), `lineName?`, `from` (participant URI), `fromName?`, `to`, `text`, `messageId?` (platform-side id; inbound/outbound only), `payload?` (raw platform message; inbound only).
35
-
36
- ```json
37
- {"kind":"inbound","id":"msg_aB3xY7zP","ts":"2026-05-14T12:00:00Z","station":"telegram","line":"metro://telegram/-100…/247","lineName":"infra","from":"metro://telegram/user/12345","fromName":"@alice","to":"metro://claude/user/9bfc7af0-…","messageId":"4567","text":"hello [image]","payload":{"message_id":4567,"chat":{"id":-100,"type":"supergroup","is_forum":true},"from":{"id":12345,"username":"alice"},"text":"hello","entities":[{"type":"mention","offset":0,"length":6}],"photo":[{"file_id":"…"}],"reply_to_message":{"message_id":4500,"text":"earlier","from":{"id":99,"username":"bob"}}}}
38
- ```
39
-
40
- ```json
41
- {"kind":"inbound","id":"msg_pQ4r5sT0","ts":"…","station":"claude","line":"metro://claude/9bfc7af0-…/50b00d11-…","from":"metro://codex/user/8119ecb1-…","to":"metro://claude/9bfc7af0-…/50b00d11-…","text":"deploy succeeded"}
42
- ```
43
-
44
- ### `payload` by station
45
-
46
- `payload` is the platform's native message shape. Narrow on `event.station`:
47
-
48
- - **`discord`** — discord.js `Message.toJSON()`: camelCase fields (`channelId`, `guildId`, `content`, `author`, `mentions: { users[], roles[], everyone }`, `attachments[]`, `reference`, …). Collections come back as **arrays of IDs**. `referencedMessage` (also `toJSON()`-shaped) is added inline on replies (auto-fetched).
49
- - **`telegram`** — raw Bot API `Message` (snake_case): `{ message_id, chat, from, text, caption, entities[], photo[], document, voice, audio, reply_to_message, … }`. `reply_to_message` is inline on replies.
50
- - **`webhook`** — `{ headers: Record<string,string>, body: <parsed JSON | raw string> }`. Narrow further on the provider — GitHub sets `headers['x-github-event']` (`push`, `pull_request`, `issues`, …) and includes a `repository`/`sender` in body; Intercom sets `x-intercom-topic` etc. `text` is a short summary; full event is always in `payload.body`.
51
-
52
- Use `payload` for anything the envelope doesn't surface — mentions, reply chains, embeds, stickers, entities.
53
-
54
- Both `from` and `to` are **participant URIs** (the conversation lives in `line`): `metro://<station>/user/<id>` for a person, `metro://claude/user/<orgId>` for a Claude Code user (orgId = stable Anthropic-account UUID), `metro://codex/user/<accountId>` for a Codex user (accountId = stable ChatGPT-account UUID), `metro://<station>/<channelId>` as a fallback `to` when sending to a group with no single recipient.
55
-
56
- When **you** call `metro send`/`reply`/`edit`/`react`, metro auto-stamps `from` to your runtime — `metro://claude/user/<orgId>` (when `$CLAUDECODE` is set; orgId comes from `claude auth status --json`) or `metro://codex/user/<accountId>` (when `$METRO_CODEX_RC`/`$CODEX_HOME` is set; accountId comes from `$CODEX_HOME/auth.json`, `tokens.account_id`). Both identities are account-scoped, not install-scoped: switch accounts with `claude auth login` / `codex login` and the next event uses the new id (within ~5 s for the daemon, immediately for one-shot CLI calls). Override with `--from=<uri>` or `$METRO_FROM`. When replying/reacting, `to` is auto-set to the original sender (history lookup).
57
-
58
- - `kind: "inbound"` — a message arrived. Source can be:
59
- - a human on a chat platform (Discord/Telegram),
60
- - a third-party service POSTing to a registered webhook endpoint (`station: "webhook"`, `payload: { headers, body }`),
61
- - another Claude / Codex user posting cross-process via `metro send` against your line.
62
-
63
- `text` may include `[image]` / `[voice]` / `[audio]` / `[file: <name>]` placeholders alongside the real text — non-image attachments are opaque markers, images can be materialized via `metro download`.
64
-
65
- ## Required flow on every event
66
-
67
- 1. **Echo `event.display` verbatim as your first chat output.** Every event ships a pre-rendered chat-bubble in `event.display` — bold header (icon + station + sender) and a markdown blockquote body. Paste it as-is before any commentary or tool calls. Monitor's notification chip is a CLI-only UI and won't surface visibly in VSCode/Cursor, so your own echo is the only cross-surface signal:
68
-
69
- ```
70
- **📩 telegram · @bonustrack**
71
- > Hey
72
- ```
73
-
74
- The format is centralized in metro's dispatcher (`formatDisplay()` in `src/history.ts`) — don't compose your own.
75
-
76
- 2. **Decide and act** using the subcommands below.
77
-
78
- ## Detecting "is this for me?"
79
-
80
- Derive from `payload`. Bot id per station is in `$METRO_STATE_DIR/bot-ids.json` (`{discord:"<userId>", telegram:"<userId>"}`).
81
-
82
- - **`discord`** — DM if `payload.guildId == null`; otherwise pinged if `payload.mentions.users.includes(<bot-id>)`.
83
- - **`telegram`** — DM if `payload.chat.type === 'private'`; otherwise pinged if any entity in `payload.entities` (or `caption_entities`) is `{type:"mention"}` matching `@<bot-username>`, or `{type:"text_mention", user:{id:<bot-id>}}`.
84
- - **`webhook`** — every POST is for you (you registered the endpoint). Route on `payload.headers['x-github-event']` / `x-intercom-topic` etc. to know which provider event it is.
85
-
86
- Default for chat: only reply on DM or ping; otherwise stay silent or `metro react` to ack. Webhooks just consume — no ack mechanism.
87
-
88
- ## Subcommands
89
-
90
- | Action | Command |
91
- |---|---|
92
- | Quote-reply (threads under original) | `metro reply <line> <messageId> <text>` |
93
- | Send a fresh message (no reply context) | `metro send <line> <text>` |
94
- | Edit a message you previously sent | `metro edit <line> <messageId> <text>` |
95
- | Reaction (empty emoji clears it) | `metro react <line> <messageId> <emoji>` |
96
- | Download `[image]` attachments | `metro download <line> <messageId> [--out=<dir>]` |
97
- | Recent-message lookback (Discord only) | `metro fetch <line> [--limit=20]` |
98
- | Cross-user ping | `metro send <user-line> <text> [--from=<line>]` |
99
- | Register webhook endpoint | `metro webhook add <label> [--secret=<hmac-secret>]` |
100
- | List / remove webhook endpoints | `metro webhook list` · `metro webhook remove <id>` |
101
- | Configure Cloudflare named tunnel | `metro tunnel setup <tunnel-name> <hostname>` |
102
-
103
- `reply` / `send` / `edit` accept multi-line text via stdin (heredoc). Webhooks are receive-only — there's no `reply` for them, just consume the event.
104
-
105
- ### Rich content flags
106
-
107
- `send` and `reply` accept these flags; `edit` accepts `--buttons` only.
108
-
109
- - `--image=<path>` — local image. **Repeatable** for albums: `--image=a.png --image=b.png` (or comma-separated). Up to 10 / message. Text becomes the caption (on the first image for albums).
110
- - `--document=<path>` — local file (PDF, log, csv, …). Same repeat/comma syntax.
111
- - `--voice=<path>` — single voice message (`.ogg` Opus or `.mp3`). On Telegram renders as a voice bubble; on Discord uploaded as audio attachment.
112
- - `--buttons='[[{"text":"…","url":"…"}]]'` — inline URL-button keyboard (2D rows × buttons). Pass `'[]'` to `edit` to clear.
113
-
114
- ```bash
115
- metro send <line> "screenshot" --image=/tmp/build.png
116
- metro send <line> "before/after" --image=/tmp/before.png --image=/tmp/after.png
117
- metro reply <line> <id> "voice note" --voice=/tmp/note.ogg
118
- metro send <line> "approve?" --buttons='[[{"text":"Open PR","url":"https://github.com/x/y/pull/1"}]]'
119
- ```
120
-
121
- Limits: 20 MB / file. Telegram albums are single-type (photos OR documents per album); mixing kinds still works — metro splits into two messages. Buttons are dropped on multi-attachment Telegram sends. URL buttons only for now.
122
-
123
- Append `--json` to any command for a single JSON line you can parse.
124
-
125
- ## When to use `reply` vs `send`
126
-
127
- - **`reply`** — responding to a specific inbound message. Threads under it. Default when handling an `inbound` event.
128
- - **`send`** — initiating without a triggering message: a long task you kicked off finished, a follow-up the user asked you to deliver later, or posting to a Claude / Codex line (`metro://claude/...`, `metro://codex/...`) to notify a peer.
129
-
130
- ## Universal message IDs
131
-
132
- The `id` field on every event and `metro history` row is metro's **universal ID** (`msg_<8 chars>`). It works anywhere a `<message_id>` is expected — `metro reply`, `edit`, `react`, `download` — and resolves to the platform's own id via the history file. Use it for chaining commands or referring back across stations.
133
-
134
- ## `metro history` — read the universal message log
135
-
136
- Every inbound, outbound, edit, and react is appended to `$METRO_STATE_DIR/history.jsonl` automatically.
137
-
138
- ```bash
139
- metro history --limit=20 # recent 20, newest first
140
- metro history --line=metro://discord/123 # only this conversation
141
- metro history --kind=inbound --since=2026-05-14 # inbounds since that day
142
- metro history --station=telegram --text=deploy # all Telegram entries containing "deploy"
143
- metro history --from='@alice' --json # everything from alice, JSON
144
- ```
145
-
146
- Filters: `--limit` (default 50), `--line`, `--station`, `--kind` (`inbound`/`outbound`/`edit`/`react`), `--from`, `--text`, `--since` (ISO), `--json`.
147
-
148
- ## Discovery
149
-
150
- ### `metro lines`
151
-
152
- ```
153
- $ metro lines
154
- 2m ago metro://discord/1234567890 infra
155
- 5m ago metro://telegram/-100123/42 design-review
156
- ```
157
-
158
- Lines sorted by recency. Use when the user says "the Telegram channel" or "that PR thread."
159
-
160
- ### `metro stations`
161
-
162
- ```
163
- $ metro stations
164
- ✓ discord in: text+image · out: text · features: reply, send, edit, react, download, fetch
165
- DISCORD_BOT_TOKEN
166
- ✓ telegram in: text+image · out: text · features: reply, send, edit, react, download, fetch
167
- TELEGRAM_BOT_TOKEN
168
- ✓ claude in: text · out: text · features: send
169
- account: 9bfc7af0-… · seen 1 user, 2 sessions
170
- seen: 9bfc7af0-… · sessions: 2
171
- ✗ codex in: text · out: text · features: send
172
- set METRO_CODEX_RC=ws://… to push
173
- ✓ webhook in: text · out: – · features: –
174
- 2 endpoints · base https://webhook.example.com
175
- ```
176
-
177
- `✓` = ready (env/runtime detected), `✗` = configured-but-broken or runtime not detected, `·` = informational. The detail line under each Claude / Codex row shows the resolved account id plus the per-user count of sessions metro has observed — pull addressable Claude / Codex lines from those.
178
-
179
- ## Webhooks (receiving HTTP events)
180
-
181
- When the user wants metro to receive events from a third-party service (GitHub PRs, Intercom conversations, Fireflies meetings, …):
182
-
183
- 1. **One-time tunnel setup** (only needed once per machine): `metro tunnel setup <tunnel-name> <hostname>`. Requires `cloudflared` on PATH (`brew install cloudflared`) and a Cloudflare account + domain on Cloudflare DNS. Run `cloudflared tunnel login` first if you haven't.
184
- 2. **Register an endpoint**: `metro webhook add <label> [--secret=<shared-secret>]`. Prints the public URL — paste it into the provider's webhook settings. For GitHub specifically, set **Content type: `application/json`** (form-encoded won't parse into `payload.body`).
185
- 3. **Run the daemon**: `metro`. With at least one endpoint registered, metro auto-binds the HTTP listener (port 8420, override `METRO_WEBHOOK_PORT`) and spawns `cloudflared tunnel run` if `tunnel.json` exists.
186
-
187
- Each POST becomes an inbound event:
188
-
189
- ```json
190
- {"kind":"inbound","station":"webhook","line":"metro://webhook/<id>","lineName":"github",
191
- "from":"metro://webhook/<id>","to":"metro://claude/user/<orgId>",
192
- "messageId":"<x-github-delivery>","text":"push POST /wh/<id>",
193
- "payload":{"headers":{"x-github-event":"push",…},"body":{"ref":"refs/heads/main",…}}}
194
- ```
195
-
196
- `text` is a short summary; the real event lives in `payload.body`. Use `payload.headers['x-github-event']` (or `x-intercom-topic` etc.) to narrow on provider event type. If you set `--secret`, metro verifies `X-Hub-Signature-256` and rejects bad signatures with 401 — you see only authenticated events.
197
-
198
- ## Image attachments
199
-
200
- When an inbound has an `[image]` tag in `text`:
201
-
202
- 1. `metro download <line> <messageId>` → prints absolute paths.
203
- 2. `Read` each path with your `Read` tool — the image enters your context as a vision input.
204
- 3. Reply normally via `metro reply`.
205
-
206
- ## Cross-user notification
207
-
208
- Both Claude Code and Codex can post to each other's **line** — `metro://claude/<user-id>/<session-id>` or `metro://codex/<user-id>/<session-id>`. `<user-id>` is the peer's stable account id (cross-device); `<session-id>` is one conversation. Discover both by running `metro stations` (which lists every user + session metro has seen), or by reading `$METRO_STATE_DIR/user-registry.json` directly. The daemon re-emits the post on its stdout stream (and pushes via codex-rc if configured), so the peer sees it as an inbound event:
209
-
210
- ```bash
211
- metro send metro://claude/9bfc7af0-…/50b00d11-… "build green, ready to ship"
212
- metro send metro://claude/9bfc7af0-…/50b00d11-… "build green" --from=metro://codex/user/8119ecb1-… # override sender
213
- ```
214
-
215
- This requires the metro daemon to be running on the machine. Without a daemon, Claude / Codex line sends error with a clear message.
216
-
217
- ## Don'ts
218
-
219
- - ❌ Spawning a second metro daemon — there's one per machine (lockfile-enforced).
220
- - ❌ `metro send` to a line that isn't in `metro lines` unless the user gave it to you explicitly.
221
- - ❌ Narrating the tool ("I'll now use metro reply to…"). The tool call is already visible.
222
-
223
- ## Further reading
224
-
225
- - URI scheme: [`uri-scheme.md`](uri-scheme.md)
226
- - Source: https://github.com/bonustrack/metro