@stage-labs/metro 0.1.0-beta.13 → 0.1.0-beta.131
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/api.js +14 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +176 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +93 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +43 -0
- package/dist/plugin.js +120 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +38 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +150 -0
- package/dist/service.js +242 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -27
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +18 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +55 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +122 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/marketplace/plugin/orchestrator.md +16 -0
- package/runtime/marketplace/plugin/skills/refresh/SKILL.md +15 -0
- package/runtime/node_modules/@metro-labs/core/package.json +43 -0
- package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +87 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +108 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +365 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +300 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +371 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +95 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/seed.ts +33 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +209 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +309 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +169 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +140 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +253 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +239 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +154 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +310 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +175 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/json.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +171 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +296 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +286 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +332 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/watch.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +259 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +187 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +269 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +151 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +372 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/identity.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +261 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/http/package.json +33 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +219 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +130 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +132 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +123 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +29 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +10 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +190 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +347 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +18 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +39 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -252
- package/dist/broker.js +0 -202
- package/dist/cache.js +0 -69
- package/dist/cli/actions.js +0 -206
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -188
- package/dist/cli/skill.js +0 -62
- package/dist/cli/tail.js +0 -164
- package/dist/cli/util.js +0 -72
- package/dist/cli/webhook.js +0 -81
- package/dist/codex-rc.js +0 -245
- package/dist/dispatcher.js +0 -128
- package/dist/history.js +0 -121
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/monitor.js +0 -194
- package/dist/paths.js +0 -68
- package/dist/registry.js +0 -48
- package/dist/stations/claude.js +0 -45
- package/dist/stations/codex.js +0 -68
- package/dist/stations/discord.js +0 -216
- package/dist/stations/index.js +0 -129
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -234
- package/dist/stations/webhook.js +0 -103
- package/dist/tunnel.js +0 -64
- package/dist/webhooks.js +0 -41
- package/docs/broker.md +0 -214
- package/docs/monitor.md +0 -163
- package/docs/uri-scheme.md +0 -124
- package/docs/users.md +0 -226
- package/skills/metro/SKILL.md +0 -256
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const subjects = new Map<string, string>();
|
|
2
|
+
|
|
3
|
+
export function authorizeIdentity(address: string, subject: string): void {
|
|
4
|
+
subjects.set(address.toLowerCase(), subject.toLowerCase());
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function identitySubject(address: string): string | undefined {
|
|
8
|
+
return subjects.get(address.toLowerCase());
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function resetIdentities(): void {
|
|
12
|
+
subjects.clear();
|
|
13
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { cors, sendJson } from './api-http.js';
|
|
3
|
+
|
|
4
|
+
const PATH = '/api/mode';
|
|
5
|
+
|
|
6
|
+
export type DaemonMode = 'hosted' | 'linked' | 'local';
|
|
7
|
+
|
|
8
|
+
export interface ModeInfo {
|
|
9
|
+
mode: DaemonMode;
|
|
10
|
+
owner: string | null;
|
|
11
|
+
project: string | null;
|
|
12
|
+
version: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function handleModeRequest(
|
|
16
|
+
req: IncomingMessage,
|
|
17
|
+
res: ServerResponse,
|
|
18
|
+
info: () => ModeInfo,
|
|
19
|
+
): boolean {
|
|
20
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
21
|
+
if (path !== PATH) return false;
|
|
22
|
+
if (req.method === 'OPTIONS') {
|
|
23
|
+
res.writeHead(204, cors(req)).end();
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
if (req.method !== 'GET') {
|
|
27
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
sendJson(req, res, 200, info());
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const PREVIEW = /^([a-z0-9-]+--)?metro-ui\.netlify\.app$/;
|
|
2
|
+
|
|
3
|
+
export function allowedWebHost(host: string): boolean {
|
|
4
|
+
if (host === 'metro.box' || host === 'localhost' || host === '127.0.0.1')
|
|
5
|
+
return true;
|
|
6
|
+
return PREVIEW.test(host);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function validateReturnTo(returnTo: string): boolean {
|
|
10
|
+
let url: URL;
|
|
11
|
+
try {
|
|
12
|
+
url = new URL(returnTo);
|
|
13
|
+
} catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const host = url.hostname;
|
|
17
|
+
if (url.protocol === 'http:' && (host === 'localhost' || host === '127.0.0.1'))
|
|
18
|
+
return true;
|
|
19
|
+
if (url.protocol !== 'https:') return false;
|
|
20
|
+
return allowedWebHost(host);
|
|
21
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import { verifyMessage } from 'viem';
|
|
3
|
+
import { normalizeAddress } from '@metro-labs/core/address';
|
|
4
|
+
|
|
5
|
+
export const AUTH_SCHEME = 'Metro';
|
|
6
|
+
const SKEW_MS = 5 * 60_000;
|
|
7
|
+
const SIGNATURE_RE = /^0x[0-9a-fA-F]{130}$/;
|
|
8
|
+
|
|
9
|
+
export const identityChallenge = (method: string, path: string, at: number): string =>
|
|
10
|
+
`metro-auth\n${method} ${path}\n${String(at)}`;
|
|
11
|
+
|
|
12
|
+
export interface SignedHeader {
|
|
13
|
+
address: string;
|
|
14
|
+
at: number;
|
|
15
|
+
signature: `0x${string}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function parseIdentityHeader(header: string): SignedHeader | null {
|
|
19
|
+
const [scheme, rawAddress, rawAt, signature] = header.trim().split(/\s+/);
|
|
20
|
+
if (scheme !== AUTH_SCHEME || rawAddress === undefined || rawAt === undefined || signature === undefined) return null;
|
|
21
|
+
const address = normalizeAddress(rawAddress);
|
|
22
|
+
const at = Number(rawAt);
|
|
23
|
+
if (address === null || !Number.isFinite(at) || !SIGNATURE_RE.test(signature)) return null;
|
|
24
|
+
return { address, at, signature: signature as `0x${string}` };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function signedIdentity(req: IncomingMessage, now = Date.now()): Promise<string | null> {
|
|
28
|
+
const proof = parseIdentityHeader(req.headers.authorization ?? '');
|
|
29
|
+
if (proof === null || Math.abs(now - proof.at) > SKEW_MS) return null;
|
|
30
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
31
|
+
const message = identityChallenge(req.method ?? '', path, proof.at);
|
|
32
|
+
const ok = await verifyMessage({ address: proof.address as `0x${string}`, message, signature: proof.signature }).catch(
|
|
33
|
+
() => false,
|
|
34
|
+
);
|
|
35
|
+
return ok ? proof.address : null;
|
|
36
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metro-labs/telegram",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/station.ts",
|
|
8
|
+
"./train": "./src/index.ts",
|
|
9
|
+
"./login": "./src/login.ts"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"test": "tsc --noEmit && bun test test/"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@metro-labs/core": "workspace:*",
|
|
17
|
+
"@mtcute/bun": "0.30.1"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@stage-labs/config": "0.1.0-beta.2",
|
|
21
|
+
"eslint": "^10.3.0",
|
|
22
|
+
"knip": "^6.15.0",
|
|
23
|
+
"madge": "^8.0.0",
|
|
24
|
+
"typescript": "^5"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
makeAccountStore,
|
|
5
|
+
resolveAccountId,
|
|
6
|
+
type Die,
|
|
7
|
+
} from '@metro-labs/core/stations/account-store';
|
|
8
|
+
import type { UserAccount } from './types.js';
|
|
9
|
+
|
|
10
|
+
const ACCOUNTS_FILE =
|
|
11
|
+
process.env.TELEGRAM_ACCOUNTS_FILE ??
|
|
12
|
+
join(homedir(), '.metro', 'telegram-accounts.json');
|
|
13
|
+
|
|
14
|
+
const isSignedInt = (s: string): boolean => /^-?\d+$/.test(s);
|
|
15
|
+
const isTopic = (s: string): boolean => /^\d+$/.test(s);
|
|
16
|
+
|
|
17
|
+
function validateAccount(a: UserAccount, seen: Set<string>, die: Die): void {
|
|
18
|
+
if (!a.id) die('account missing id');
|
|
19
|
+
if (!a.session || typeof a.session !== 'string')
|
|
20
|
+
die(`account '${a.id}' missing session`);
|
|
21
|
+
if (!Number.isInteger(a.apiId) || (a.apiId ?? 0) <= 0)
|
|
22
|
+
die(`account '${a.id}' missing apiId`);
|
|
23
|
+
if (!a.apiHash || typeof a.apiHash !== 'string')
|
|
24
|
+
die(`account '${a.id}' missing apiHash`);
|
|
25
|
+
if (seen.has(a.id)) die(`duplicate account id '${a.id}'`);
|
|
26
|
+
seen.add(a.id);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const { loadAccounts } = makeAccountStore<UserAccount>({
|
|
30
|
+
prefix: 'telegram',
|
|
31
|
+
file: ACCOUNTS_FILE,
|
|
32
|
+
validate(raw, die) {
|
|
33
|
+
const seen = new Set<string>();
|
|
34
|
+
for (const a of raw) validateAccount(a, seen, die);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const accounts = new Map<string, UserAccount>();
|
|
39
|
+
|
|
40
|
+
export function accountFor(args: { account?: string; line?: string }): string {
|
|
41
|
+
return resolveAccountId(accounts, args, (line) => targetOf(line)?.accountId);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function lineOf(
|
|
45
|
+
accountId: string,
|
|
46
|
+
chatId: number | string,
|
|
47
|
+
topicId?: number,
|
|
48
|
+
): string {
|
|
49
|
+
const tail = topicId !== undefined ? `${chatId}/${topicId}` : `${chatId}`;
|
|
50
|
+
return `metro://telegram/${accountId}/${tail}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface Target {
|
|
54
|
+
accountId: string;
|
|
55
|
+
chatId: number;
|
|
56
|
+
topicId?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function splitScoped(path: string[]): { accountId: string; rest: string[] } {
|
|
60
|
+
const first = path[0];
|
|
61
|
+
if (path.length >= 2 && first !== undefined && !isSignedInt(first))
|
|
62
|
+
return { accountId: first, rest: path.slice(1) };
|
|
63
|
+
return { accountId: 'default', rest: path };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function targetOf(line: string): Target | undefined {
|
|
67
|
+
const prefix = 'metro://telegram/';
|
|
68
|
+
if (!line.startsWith(prefix)) return undefined;
|
|
69
|
+
const path = line.slice(prefix.length).split('/').filter(Boolean);
|
|
70
|
+
const { accountId, rest } = splitScoped(path);
|
|
71
|
+
const [chatId, topicId] = rest;
|
|
72
|
+
if (rest.length < 1 || rest.length > 2 || chatId === undefined) return undefined;
|
|
73
|
+
if (!isSignedInt(chatId)) return undefined;
|
|
74
|
+
if (topicId !== undefined && !isTopic(topicId)) return undefined;
|
|
75
|
+
return {
|
|
76
|
+
accountId,
|
|
77
|
+
chatId: Number(chatId),
|
|
78
|
+
...(topicId !== undefined ? { topicId: Number(topicId) } : {}),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
2
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
3
|
+
import {
|
|
4
|
+
makeStation,
|
|
5
|
+
respond,
|
|
6
|
+
type CallMsg,
|
|
7
|
+
type StationHandler,
|
|
8
|
+
} from '@metro-labs/core/stations/station-runtime';
|
|
9
|
+
import { accountFor, accounts, targetOf } from './accounts.js';
|
|
10
|
+
import type { UserClient } from './client.js';
|
|
11
|
+
import { normalizeTelegramUser } from './normalize.js';
|
|
12
|
+
import {
|
|
13
|
+
sendAttachments,
|
|
14
|
+
type CanonicalAttachment,
|
|
15
|
+
} from './media-actions.js';
|
|
16
|
+
import { clampLimit, shapeHistory } from './history.js';
|
|
17
|
+
import { fetchMembers, isRestricted, restrictedMemberList } from './members.js';
|
|
18
|
+
import {
|
|
19
|
+
groupAddMembers,
|
|
20
|
+
groupCreate,
|
|
21
|
+
groupInviteLink,
|
|
22
|
+
groupRemoveMembers,
|
|
23
|
+
} from './group.js';
|
|
24
|
+
|
|
25
|
+
type Args = Record<string, unknown>;
|
|
26
|
+
|
|
27
|
+
interface Resolved {
|
|
28
|
+
accountId: string;
|
|
29
|
+
client: UserClient;
|
|
30
|
+
chatId: number;
|
|
31
|
+
topicId?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type ClientFor = (accountId: string) => UserClient;
|
|
35
|
+
|
|
36
|
+
function str(v: unknown): string | undefined {
|
|
37
|
+
return typeof v === 'string' ? v : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function lineOfArgs(args: Args): string {
|
|
41
|
+
const line = str(args.line);
|
|
42
|
+
if (!line) throw new TrainError('bad_request', 'missing line');
|
|
43
|
+
return line;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolve(args: Args, clientFor: ClientFor): Resolved {
|
|
47
|
+
const line = lineOfArgs(args);
|
|
48
|
+
const target = targetOf(line);
|
|
49
|
+
if (!target) throw new TrainError('bad_request', `bad line '${line}'`);
|
|
50
|
+
const accountId = accountFor({ account: str(args.account), line });
|
|
51
|
+
return {
|
|
52
|
+
accountId,
|
|
53
|
+
client: clientFor(accountId),
|
|
54
|
+
chatId: target.chatId,
|
|
55
|
+
...(target.topicId !== undefined ? { topicId: target.topicId } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function guard<T>(run: () => Promise<T>): Promise<T> {
|
|
60
|
+
try {
|
|
61
|
+
return await run();
|
|
62
|
+
} catch (e) {
|
|
63
|
+
const msg = errMsg(e);
|
|
64
|
+
if (/FLOOD_WAIT|SLOWMODE_WAIT/i.test(msg))
|
|
65
|
+
throw new TrainError('rate_limited', msg);
|
|
66
|
+
throw new TrainError('telegram_call', msg);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function attachmentsOf(args: Args): CanonicalAttachment[] {
|
|
71
|
+
const raw = args.attachments;
|
|
72
|
+
return Array.isArray(raw) ? (raw as CanonicalAttachment[]) : [];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function makeSend(clientFor: ClientFor): StationHandler {
|
|
76
|
+
return async (id, args) => {
|
|
77
|
+
const { accountId, client, chatId, topicId } = resolve(args, clientFor);
|
|
78
|
+
const text = str(args.text) ?? '';
|
|
79
|
+
const replyTo = str(args.replyTo);
|
|
80
|
+
const explicitReplyTo = replyTo ? Number(replyTo) : undefined;
|
|
81
|
+
const replyParam = explicitReplyTo ?? topicId;
|
|
82
|
+
const attachments = attachmentsOf(args);
|
|
83
|
+
if (attachments.length) {
|
|
84
|
+
const sent = await guard(() =>
|
|
85
|
+
sendAttachments({ client, chatId, replyTo: replyParam }, attachments, text),
|
|
86
|
+
);
|
|
87
|
+
respond(id, {
|
|
88
|
+
result: {
|
|
89
|
+
messageId: String(sent.message.id),
|
|
90
|
+
account: accountId,
|
|
91
|
+
attachments: sent.delivered,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const peer = await guard(() => client.tg.resolvePeer(chatId));
|
|
97
|
+
const sent = await guard(() =>
|
|
98
|
+
client.tg.sendText(
|
|
99
|
+
peer,
|
|
100
|
+
text,
|
|
101
|
+
replyParam !== undefined ? { replyTo: replyParam } : undefined,
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
respond(id, {
|
|
105
|
+
result: { messageId: String(sent.id), account: accountId },
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function makeReact(clientFor: ClientFor): StationHandler {
|
|
111
|
+
return async (id, args) => {
|
|
112
|
+
const { accountId, client, chatId } = resolve(args, clientFor);
|
|
113
|
+
const message = Number(str(args.messageId));
|
|
114
|
+
const raw = str(args.emoji);
|
|
115
|
+
const emoji = raw === undefined || raw === '' ? null : raw;
|
|
116
|
+
await guard(() =>
|
|
117
|
+
client.tg.sendReaction({ chatId, message, emoji }),
|
|
118
|
+
);
|
|
119
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function makeEdit(clientFor: ClientFor): StationHandler {
|
|
124
|
+
return async (id, args) => {
|
|
125
|
+
const { accountId, client, chatId } = resolve(args, clientFor);
|
|
126
|
+
const message = Number(str(args.messageId));
|
|
127
|
+
const text = str(args.text) ?? '';
|
|
128
|
+
await guard(() => client.tg.editMessage({ chatId, message, text }));
|
|
129
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function makeDelete(clientFor: ClientFor): StationHandler {
|
|
134
|
+
return async (id, args) => {
|
|
135
|
+
const { accountId, client, chatId } = resolve(args, clientFor);
|
|
136
|
+
const message = Number(str(args.messageId));
|
|
137
|
+
await guard(() =>
|
|
138
|
+
client.tg.deleteMessagesById(chatId, [message], { revoke: true }),
|
|
139
|
+
);
|
|
140
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function makeRead(clientFor: ClientFor): StationHandler {
|
|
145
|
+
return async (id, args) => {
|
|
146
|
+
const { accountId, client, chatId } = resolve(args, clientFor);
|
|
147
|
+
const limit = clampLimit(args.limit);
|
|
148
|
+
const before = str(args.before);
|
|
149
|
+
const beforeId = before ? Number(before) : undefined;
|
|
150
|
+
const offset =
|
|
151
|
+
beforeId !== undefined && Number.isFinite(beforeId)
|
|
152
|
+
? { offset: { id: beforeId, date: 0 } }
|
|
153
|
+
: {};
|
|
154
|
+
const page = await guard(() =>
|
|
155
|
+
client.tg.getHistory(chatId, { limit, ...offset }),
|
|
156
|
+
);
|
|
157
|
+
respond(id, { result: shapeHistory(accountId, chatId, page) });
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function makeListMembers(clientFor: ClientFor): StationHandler {
|
|
162
|
+
return async (id, args) => {
|
|
163
|
+
const { client, chatId } = resolve(args, clientFor);
|
|
164
|
+
const limit = typeof args.limit === 'number' ? args.limit : undefined;
|
|
165
|
+
try {
|
|
166
|
+
respond(id, { result: await fetchMembers(client, chatId, limit) });
|
|
167
|
+
} catch (e) {
|
|
168
|
+
const msg = errMsg(e);
|
|
169
|
+
if (isRestricted(msg)) {
|
|
170
|
+
respond(id, { result: restrictedMemberList(msg) });
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
throw e;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function makeGroupCreate(clientFor: ClientFor): StationHandler {
|
|
179
|
+
return async (id, args) => {
|
|
180
|
+
const accountId = accountFor({ account: str(args.account) });
|
|
181
|
+
const client = clientFor(accountId);
|
|
182
|
+
respond(id, {
|
|
183
|
+
result: await guard(() => groupCreate(client, accountId, args)),
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function makeGroupAdd(clientFor: ClientFor): StationHandler {
|
|
189
|
+
return async (id, args) => {
|
|
190
|
+
const { client, chatId } = resolve(args, clientFor);
|
|
191
|
+
const line = lineOfArgs(args);
|
|
192
|
+
respond(id, {
|
|
193
|
+
result: await guard(() => groupAddMembers(client, chatId, line, args)),
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function makeGroupRemove(clientFor: ClientFor): StationHandler {
|
|
199
|
+
return async (id, args) => {
|
|
200
|
+
const { client, chatId } = resolve(args, clientFor);
|
|
201
|
+
const line = lineOfArgs(args);
|
|
202
|
+
respond(id, {
|
|
203
|
+
result: await guard(() => groupRemoveMembers(client, chatId, line, args)),
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function makeGroupInvite(clientFor: ClientFor): StationHandler {
|
|
209
|
+
return async (id, args) => {
|
|
210
|
+
const { client, chatId } = resolve(args, clientFor);
|
|
211
|
+
const line = lineOfArgs(args);
|
|
212
|
+
respond(id, {
|
|
213
|
+
result: await guard(() => groupInviteLink(client, chatId, line)),
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function makeAccounts(clientFor: ClientFor): StationHandler {
|
|
219
|
+
return (id) => {
|
|
220
|
+
const list = [...accounts.values()].map((a) => {
|
|
221
|
+
const me = clientFor(a.id).self();
|
|
222
|
+
const username = me?.username ?? null;
|
|
223
|
+
return {
|
|
224
|
+
id: a.id,
|
|
225
|
+
handle: username === null ? me?.phone ?? null : `@${username}`,
|
|
226
|
+
url: username === null ? null : `https://t.me/${username}`,
|
|
227
|
+
owner: a.owner ?? null,
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
respond(id, { result: { accounts: list } });
|
|
231
|
+
return Promise.resolve();
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function makeHandleCall(
|
|
236
|
+
clientFor: ClientFor,
|
|
237
|
+
): (msg: CallMsg) => Promise<void> {
|
|
238
|
+
return makeStation({
|
|
239
|
+
handlers: {
|
|
240
|
+
accounts: makeAccounts(clientFor),
|
|
241
|
+
send: makeSend(clientFor),
|
|
242
|
+
react: makeReact(clientFor),
|
|
243
|
+
edit: makeEdit(clientFor),
|
|
244
|
+
delete: makeDelete(clientFor),
|
|
245
|
+
read: makeRead(clientFor),
|
|
246
|
+
listMembers: makeListMembers(clientFor),
|
|
247
|
+
groupCreate: makeGroupCreate(clientFor),
|
|
248
|
+
groupAddMembers: makeGroupAdd(clientFor),
|
|
249
|
+
groupRemoveMembers: makeGroupRemove(clientFor),
|
|
250
|
+
groupInviteLink: makeGroupInvite(clientFor),
|
|
251
|
+
},
|
|
252
|
+
normalize: normalizeTelegramUser,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { Message } from '@mtcute/bun';
|
|
2
|
+
import { FileLocation } from '@mtcute/bun';
|
|
3
|
+
import {
|
|
4
|
+
saveBufferToCache,
|
|
5
|
+
type SavedAttachment,
|
|
6
|
+
} from '@metro-labs/core/stations/attachments';
|
|
7
|
+
import type { UserClient } from './client.js';
|
|
8
|
+
|
|
9
|
+
type Media = NonNullable<Message['media']>;
|
|
10
|
+
export type DownloadableMedia = Media & FileLocation;
|
|
11
|
+
|
|
12
|
+
export function isDownloadable(media: Media): media is DownloadableMedia {
|
|
13
|
+
return media instanceof FileLocation;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface MediaMeta {
|
|
17
|
+
mime?: string;
|
|
18
|
+
name?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function fallbackMime(media: DownloadableMedia): string | undefined {
|
|
22
|
+
const type = (media as { type?: unknown }).type;
|
|
23
|
+
if (type === 'photo') return 'image/jpeg';
|
|
24
|
+
if (type === 'sticker') return 'image/webp';
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function metaOf(media: DownloadableMedia): MediaMeta {
|
|
29
|
+
const withMime = media as { mimeType?: unknown };
|
|
30
|
+
const withName = media as { fileName?: unknown };
|
|
31
|
+
const mime =
|
|
32
|
+
(typeof withMime.mimeType === 'string' ? withMime.mimeType : undefined) ??
|
|
33
|
+
fallbackMime(media);
|
|
34
|
+
const name =
|
|
35
|
+
typeof withName.fileName === 'string' ? withName.fileName : undefined;
|
|
36
|
+
return {
|
|
37
|
+
...(mime ? { mime } : {}),
|
|
38
|
+
...(name ? { name } : {}),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface PendingDescriptor {
|
|
43
|
+
kind: string;
|
|
44
|
+
name?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const KIND_BY_TYPE: Record<string, string> = {
|
|
48
|
+
photo: 'image',
|
|
49
|
+
sticker: 'image',
|
|
50
|
+
voice: 'audio',
|
|
51
|
+
audio: 'audio',
|
|
52
|
+
video: 'video',
|
|
53
|
+
video_note: 'video',
|
|
54
|
+
animation: 'video',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function mediaType(media: DownloadableMedia): string {
|
|
58
|
+
const type = (media as { type?: unknown }).type;
|
|
59
|
+
return typeof type === 'string' ? type : '';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function kindOf(media: DownloadableMedia, mime: string | undefined): string {
|
|
63
|
+
const byType = KIND_BY_TYPE[mediaType(media)];
|
|
64
|
+
if (byType) return byType;
|
|
65
|
+
if (mime?.startsWith('image/')) return 'image';
|
|
66
|
+
if (mime?.startsWith('audio/')) return 'audio';
|
|
67
|
+
if (mime?.startsWith('video/')) return 'video';
|
|
68
|
+
return 'file';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function pendingDescriptorOf(media: DownloadableMedia): PendingDescriptor {
|
|
72
|
+
const meta = metaOf(media);
|
|
73
|
+
return {
|
|
74
|
+
kind: kindOf(media, meta.mime),
|
|
75
|
+
...(meta.name ? { name: meta.name } : {}),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function downloadMedia(
|
|
80
|
+
client: UserClient,
|
|
81
|
+
media: DownloadableMedia,
|
|
82
|
+
messageId: string,
|
|
83
|
+
index = 0,
|
|
84
|
+
): Promise<SavedAttachment> {
|
|
85
|
+
const buf = await client.tg.downloadAsBuffer(media);
|
|
86
|
+
return saveBufferToCache(buf, messageId, index, metaOf(media));
|
|
87
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { mkdirSync } from 'node:fs';
|
|
4
|
+
import { TelegramClient } from '@mtcute/bun';
|
|
5
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
6
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
7
|
+
import type { UserAccount } from './types.js';
|
|
8
|
+
|
|
9
|
+
const STATE_DIR =
|
|
10
|
+
process.env.METRO_STATE_DIR ?? join(homedir(), '.cache', 'metro');
|
|
11
|
+
|
|
12
|
+
export interface Self {
|
|
13
|
+
username: string | null;
|
|
14
|
+
phone: string | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface UserClient {
|
|
18
|
+
account: UserAccount;
|
|
19
|
+
tg: TelegramClient;
|
|
20
|
+
self: () => Self | null;
|
|
21
|
+
connect: () => Promise<void>;
|
|
22
|
+
startUpdates: () => Promise<void>;
|
|
23
|
+
disconnect: () => Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function storagePath(accountId: string): string {
|
|
27
|
+
const dir = join(STATE_DIR, 'telegram');
|
|
28
|
+
mkdirSync(dir, { recursive: true });
|
|
29
|
+
return join(dir, `${accountId}.session`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function createClient(account: UserAccount): UserClient {
|
|
33
|
+
const apiId = account.apiId ?? 0;
|
|
34
|
+
const { apiHash } = account;
|
|
35
|
+
if (!Number.isInteger(apiId) || apiId <= 0 || !apiHash)
|
|
36
|
+
throw new TrainError(
|
|
37
|
+
'telegram_auth',
|
|
38
|
+
`account '${account.id}' missing apiId/apiHash`,
|
|
39
|
+
);
|
|
40
|
+
const tg = new TelegramClient({
|
|
41
|
+
apiId,
|
|
42
|
+
apiHash,
|
|
43
|
+
storage: storagePath(account.id),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
let me: Self | null = null;
|
|
47
|
+
|
|
48
|
+
const connect = async (): Promise<void> => {
|
|
49
|
+
try {
|
|
50
|
+
await tg.importSession(account.session);
|
|
51
|
+
await tg.connect();
|
|
52
|
+
} catch (e) {
|
|
53
|
+
throw new TrainError(
|
|
54
|
+
'telegram_auth',
|
|
55
|
+
`account '${account.id}' connect failed: ${errMsg(e)}`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const user = await tg.getMe();
|
|
60
|
+
me = { username: user.username ?? null, phone: user.phoneNumber ?? null };
|
|
61
|
+
} catch (e) {
|
|
62
|
+
process.stderr.write(
|
|
63
|
+
`telegram[${account.id}] identity unavailable: ${errMsg(e)}\n`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const startUpdates = async (): Promise<void> => {
|
|
69
|
+
await tg.startUpdatesLoop();
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const disconnect = async (): Promise<void> => {
|
|
73
|
+
await tg.disconnect();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return { account, tg, self: () => me, connect, startUpdates, disconnect };
|
|
77
|
+
}
|