@stage-labs/metro 0.1.0-beta.2 → 0.1.0-beta.200
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 +11 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +163 -0
- package/dist/cli.js +77 -567
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +43 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +44 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +58 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +158 -0
- package/dist/service.js +243 -0
- package/dist/tail.js +102 -144
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -23
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +17 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +71 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +30 -0
- package/runtime/marketplace/plugin/bin/policy.mjs +97 -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 +24 -0
- package/runtime/node_modules/@metro-labs/core/package.json +47 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +119 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +24 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +91 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +227 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +74 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -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/profile.ts +58 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +61 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-boot.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-events.ts +89 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +123 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/tickets.ts +37 -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/agent-user/agent-fs.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files-api.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files.ts +119 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/home-fs.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/job-env.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules.ts +252 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +87 -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-lookups.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +76 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +357 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +30 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +59 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +305 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +168 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +86 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/api.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/needs.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/preview.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +221 -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 +21 -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 +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +370 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +78 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/react-note.ts +13 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/reply-id.ts +1 -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 +355 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +359 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +283 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +271 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +306 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +176 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/health.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +159 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +135 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +158 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +348 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +81 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +106 -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-reaper.ts +174 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +278 -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 +233 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +208 -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 +173 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +194 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +137 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +77 -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 +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/frames.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +274 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +160 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-client.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-setup.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-stream.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +199 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +299 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +193 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/pkce.ts +36 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/subscription.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/text.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +244 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +156 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +226 -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 +90 -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 +137 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +19 -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-prompt.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +83 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-lookup.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +96 -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/read-tool.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-slot.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +88 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-def.ts +9 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +225 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +333 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/api.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/helper-script.ts +126 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/move.ts +165 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/privilege.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +316 -0
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +35 -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 +365 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +71 -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 +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +58 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/owner.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-driver.ts +42 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-outlook.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +225 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +288 -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 +265 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +51 -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/stations/webhook-endpoints.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +124 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/api.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/config.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/firewall.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/index.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/install.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/paths.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/proxy.ts +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +130 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +61 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +26 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +117 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +240 -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 +174 -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 +122 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -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 +22 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -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 +138 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -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/workos-token.ts +149 -0
- package/runtime/node_modules/@metro-labs/outlook/package.json +25 -0
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +107 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +20 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +97 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +53 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +36 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +141 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +60 -0
- package/runtime/node_modules/@metro-labs/outlook/src/html.ts +72 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +104 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +41 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +116 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +119 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +43 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +70 -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 +256 -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 +128 -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 +57 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +7 -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/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +27 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +6 -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 +119 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +246 -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 +173 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +95 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +65 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +20 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -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 +1 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +106 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +134 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +152 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/files.ts +58 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +169 -0
- package/runtime/node_modules/@metro-labs/threema/src/groups.ts +96 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +89 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +24 -0
- package/runtime/node_modules/@metro-labs/threema/src/messages.ts +264 -0
- package/runtime/node_modules/@metro-labs/threema/src/outbound.ts +160 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +13 -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 +213 -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 +388 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +83 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +7 -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/names.ts +28 -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 +24 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +94 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +5 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +114 -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 +101 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +118 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +244 -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-payloads.ts +316 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +69 -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 +47 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +122 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +139 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/names.ts +61 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/profile.ts +153 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/sender.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/smart.ts +72 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +79 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +198 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +88 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +98 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +51 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +37 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -78
- package/dist/channels/discord.js +0 -148
- package/dist/channels/telegram.js +0 -137
- package/dist/lib/address.js +0 -21
- package/dist/lib/codex-rc.js +0 -274
- package/dist/lib/dotenv.js +0 -31
- package/dist/log.js +0 -12
- package/dist/paths.js +0 -45
- package/skills/metro/SKILL.md +0 -122
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { tgForm, targetOf } from './accounts.js';
|
|
2
|
+
import { selfUri } from '@metro-labs/core/stations/train-events';
|
|
3
|
+
import { emit, mintId, respond } from './wire.js';
|
|
4
|
+
import { appendFile } from '@metro-labs/core/stations/attachments';
|
|
5
|
+
|
|
6
|
+
export function emitOutbound(
|
|
7
|
+
accountId: string,
|
|
8
|
+
line: string,
|
|
9
|
+
messageId: string,
|
|
10
|
+
text: string,
|
|
11
|
+
replyTo?: string,
|
|
12
|
+
): void {
|
|
13
|
+
emit({
|
|
14
|
+
kind: 'outbound',
|
|
15
|
+
id: mintId(),
|
|
16
|
+
ts: new Date().toISOString(),
|
|
17
|
+
station: 'telegram-bot',
|
|
18
|
+
line,
|
|
19
|
+
from: selfUri('telegram-bot', accountId),
|
|
20
|
+
to: line,
|
|
21
|
+
message_id: messageId,
|
|
22
|
+
text,
|
|
23
|
+
reply_to: replyTo,
|
|
24
|
+
...(replyTo ? { event: { type: 'reply', replyTo } } : {}),
|
|
25
|
+
account: accountId,
|
|
26
|
+
payload: { account: accountId },
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function finishSend(
|
|
31
|
+
id: string,
|
|
32
|
+
accountId: string,
|
|
33
|
+
line: string,
|
|
34
|
+
messageId: string,
|
|
35
|
+
label: string,
|
|
36
|
+
replyTo?: string,
|
|
37
|
+
extra?: Record<string, unknown>,
|
|
38
|
+
): void {
|
|
39
|
+
emitOutbound(accountId, line, messageId, label, replyTo);
|
|
40
|
+
respond(id, {
|
|
41
|
+
result: { messageId, account: accountId, ...extra },
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface MediaArgs {
|
|
46
|
+
line: string;
|
|
47
|
+
path: string;
|
|
48
|
+
caption?: string;
|
|
49
|
+
replyTo?: string;
|
|
50
|
+
parseMode?: string;
|
|
51
|
+
account?: string;
|
|
52
|
+
name?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function sendMedia(
|
|
56
|
+
method: string,
|
|
57
|
+
fieldName: string,
|
|
58
|
+
args: Record<string, unknown>,
|
|
59
|
+
): Promise<{ accountId: string; message_id: number }> {
|
|
60
|
+
const {
|
|
61
|
+
line,
|
|
62
|
+
path,
|
|
63
|
+
caption,
|
|
64
|
+
replyTo,
|
|
65
|
+
parseMode,
|
|
66
|
+
account,
|
|
67
|
+
name: fileName,
|
|
68
|
+
} = args as unknown as MediaArgs;
|
|
69
|
+
const { accountId, chatId, topicId } = targetOf(line, account);
|
|
70
|
+
const form = new FormData();
|
|
71
|
+
form.append('chat_id', String(chatId));
|
|
72
|
+
if (topicId !== undefined) form.append('message_thread_id', String(topicId));
|
|
73
|
+
if (caption) form.append('caption', caption);
|
|
74
|
+
if (parseMode) form.append('parse_mode', parseMode);
|
|
75
|
+
if (replyTo)
|
|
76
|
+
form.append(
|
|
77
|
+
'reply_parameters',
|
|
78
|
+
JSON.stringify({ message_id: Number(replyTo) }),
|
|
79
|
+
);
|
|
80
|
+
const name = fileName ?? path.split('/').pop() ?? fieldName;
|
|
81
|
+
await appendFile(form, fieldName, path, name);
|
|
82
|
+
const r = await tgForm<{ message_id: number }>(accountId, method, form);
|
|
83
|
+
return { accountId, message_id: r.message_id };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const MEDIA_METHOD_FIELD: Record<
|
|
87
|
+
string,
|
|
88
|
+
{ method: string; field: string }
|
|
89
|
+
> = {
|
|
90
|
+
image: { method: 'sendPhoto', field: 'photo' },
|
|
91
|
+
voice: { method: 'sendVoice', field: 'voice' },
|
|
92
|
+
audio: { method: 'sendAudio', field: 'audio' },
|
|
93
|
+
video: { method: 'sendVideo', field: 'video' },
|
|
94
|
+
document: { method: 'sendDocument', field: 'document' },
|
|
95
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { MemberList, MetroMember } from '@metro-labs/core/stations/types';
|
|
2
|
+
|
|
3
|
+
export interface TgUser {
|
|
4
|
+
id: number;
|
|
5
|
+
username?: string;
|
|
6
|
+
first_name?: string;
|
|
7
|
+
last_name?: string;
|
|
8
|
+
is_bot?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface TgChatMember {
|
|
12
|
+
user: TgUser;
|
|
13
|
+
status: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function mapTgMember(m: TgChatMember): MetroMember {
|
|
17
|
+
const u = m.user;
|
|
18
|
+
const member: MetroMember = { id: String(u.id) };
|
|
19
|
+
if (u.username) member.name = u.username;
|
|
20
|
+
const display = [u.first_name, u.last_name].filter(Boolean).join(' ');
|
|
21
|
+
if (display) member.display_name = display;
|
|
22
|
+
member.is_admin = m.status === 'creator' || m.status === 'administrator';
|
|
23
|
+
member.is_bot = Boolean(u.is_bot);
|
|
24
|
+
return member;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const BOT_LIMIT_REASON =
|
|
28
|
+
'Telegram Bot API cannot enumerate full membership; returning administrators only.';
|
|
29
|
+
|
|
30
|
+
export function adminMemberList(
|
|
31
|
+
admins: TgChatMember[],
|
|
32
|
+
total: number | null,
|
|
33
|
+
): MemberList {
|
|
34
|
+
return {
|
|
35
|
+
members: admins.map(mapTgMember),
|
|
36
|
+
capability: {
|
|
37
|
+
supported: true,
|
|
38
|
+
complete: false,
|
|
39
|
+
reason: BOT_LIMIT_REASON,
|
|
40
|
+
...(total != null ? { total } : {}),
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function inaccessibleMemberList(reason: string): MemberList {
|
|
46
|
+
return { members: [], capability: { supported: false, complete: false, reason } };
|
|
47
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { appendFile } from '@metro-labs/core/stations/attachments';
|
|
2
|
+
import {
|
|
3
|
+
assertImage,
|
|
4
|
+
fieldsOf,
|
|
5
|
+
parseProfileChange,
|
|
6
|
+
type ProfileApplied,
|
|
7
|
+
type ProfileAvatar,
|
|
8
|
+
type ProfileChange,
|
|
9
|
+
} from '@metro-labs/core/stations/profile';
|
|
10
|
+
import { makeProfileCache, nonEmpty, type SenderProfile } from '@metro-labs/core/stations/sender-profile';
|
|
11
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
12
|
+
import { accountFor, tg, tgForm } from './accounts.js';
|
|
13
|
+
import { respond } from './wire.js';
|
|
14
|
+
|
|
15
|
+
async function setPhoto(accountId: string, avatar: ProfileAvatar): Promise<void> {
|
|
16
|
+
assertImage(avatar);
|
|
17
|
+
const form = new FormData();
|
|
18
|
+
form.append('photo', JSON.stringify({ type: 'static', photo: 'attach://avatar' }));
|
|
19
|
+
await appendFile(form, 'avatar', avatar.path, avatar.name);
|
|
20
|
+
await tgForm(accountId, 'setMyProfilePhoto', form);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function applyProfile(accountId: string, change: ProfileChange): Promise<void> {
|
|
24
|
+
if (change.name !== undefined) await tg(accountId, 'setMyName', { name: change.name });
|
|
25
|
+
if (change.bio !== undefined) await tg(accountId, 'setMyDescription', { description: change.bio });
|
|
26
|
+
if (change.avatar !== undefined) await setPhoto(accountId, change.avatar);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function setProfile(id: string, args: Record<string, unknown>): Promise<void> {
|
|
30
|
+
const accountId = accountFor({ account: args.account as string | undefined });
|
|
31
|
+
const change = parseProfileChange(args);
|
|
32
|
+
await applyProfile(accountId, change);
|
|
33
|
+
const result: ProfileApplied = { account: accountId, applied: fieldsOf(change) };
|
|
34
|
+
respond(id, { result });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface TgChat {
|
|
38
|
+
username?: unknown;
|
|
39
|
+
first_name?: unknown;
|
|
40
|
+
last_name?: unknown;
|
|
41
|
+
bio?: unknown;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const senders = makeProfileCache<SenderProfile>(
|
|
45
|
+
async (key) => {
|
|
46
|
+
const [accountId, user] = key.split(':', 2) as [string, string];
|
|
47
|
+
const chat = await tg<TgChat>(accountId, 'getChat', { chat_id: Number(user) }, 10_000);
|
|
48
|
+
const handle = nonEmpty(chat.username);
|
|
49
|
+
const display = nonEmpty([chat.first_name, chat.last_name].filter((part) => typeof part === 'string').join(' '));
|
|
50
|
+
return {
|
|
51
|
+
id: user,
|
|
52
|
+
...(handle === undefined ? {} : { name: `@${handle}` }),
|
|
53
|
+
...(display === undefined ? {} : { display_name: display }),
|
|
54
|
+
...(nonEmpty(chat.bio) === undefined ? {} : { about: nonEmpty(chat.bio) }),
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
{ onError: (key, err) => process.stderr.write(`telegram-bot: could not read the profile of ${key}: ${err instanceof Error ? err.message : String(err)}\n`) },
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
export async function readProfile(id: string, args: Record<string, unknown>): Promise<void> {
|
|
61
|
+
const accountId = accountFor({ account: args.account as string | undefined });
|
|
62
|
+
const user = typeof args.user === 'string' ? args.user.trim() : '';
|
|
63
|
+
if (!/^\d+$/.test(user)) throw new TrainError('telegram_user_required', 'profile needs the numeric Telegram id of the person', { retryable: false });
|
|
64
|
+
respond(id, { result: (await senders.get(`${accountId}:${user}`)) ?? { id: user } });
|
|
65
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Station, Verb } from '@metro-labs/core/stations/types';
|
|
2
|
+
import { PROFILE_FIELDS, type ProfileField } from '@metro-labs/core/stations/profile';
|
|
3
|
+
|
|
4
|
+
export const telegramBotStation: Station = {
|
|
5
|
+
name: 'telegram-bot',
|
|
6
|
+
hasAccounts: true,
|
|
7
|
+
hasTrain: true,
|
|
8
|
+
messageVerbs: new Set<Verb>([
|
|
9
|
+
'send',
|
|
10
|
+
'reply',
|
|
11
|
+
'react',
|
|
12
|
+
'unreact',
|
|
13
|
+
'edit',
|
|
14
|
+
'delete',
|
|
15
|
+
]),
|
|
16
|
+
attachmentMode: 'canonical',
|
|
17
|
+
profileFields: new Set<ProfileField>(PROFILE_FIELDS),
|
|
18
|
+
readsProfiles: true,
|
|
19
|
+
tools: [],
|
|
20
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export interface TgEntity {
|
|
2
|
+
type: string;
|
|
3
|
+
offset: number;
|
|
4
|
+
length: number;
|
|
5
|
+
user?: { id: number };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface TgUser {
|
|
9
|
+
id: number;
|
|
10
|
+
username?: string;
|
|
11
|
+
first_name?: string;
|
|
12
|
+
last_name?: string;
|
|
13
|
+
is_bot?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface TgMsg {
|
|
17
|
+
entities?: TgEntity[];
|
|
18
|
+
caption_entities?: TgEntity[];
|
|
19
|
+
reply_to_message?: { message_id: number; from?: { id: number; username?: string; is_bot?: boolean } };
|
|
20
|
+
message_id: number;
|
|
21
|
+
date: number;
|
|
22
|
+
chat: { id: number; type: string; title?: string; first_name?: string };
|
|
23
|
+
from?: TgUser;
|
|
24
|
+
text?: string;
|
|
25
|
+
caption?: string;
|
|
26
|
+
message_thread_id?: number;
|
|
27
|
+
is_topic_message?: boolean;
|
|
28
|
+
photo?: { file_id: string; file_size?: number }[];
|
|
29
|
+
document?: { file_name?: string; file_id?: string; mime_type?: string };
|
|
30
|
+
voice?: { file_id?: string; duration?: number; mime_type?: string };
|
|
31
|
+
audio?: { file_id?: string; file_name?: string; mime_type?: string };
|
|
32
|
+
video?: { file_id?: string; file_name?: string; mime_type?: string };
|
|
33
|
+
animation?: { file_id?: string; file_name?: string; mime_type?: string };
|
|
34
|
+
sticker?: { file_id?: string; emoji?: string; set_name?: string };
|
|
35
|
+
location?: { latitude: number; longitude: number };
|
|
36
|
+
dice?: { emoji: string; value: number };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface TgReaction {
|
|
40
|
+
chat: { id: number; type: string };
|
|
41
|
+
message_id: number;
|
|
42
|
+
user?: TgUser;
|
|
43
|
+
date: number;
|
|
44
|
+
old_reaction: { type: string; emoji?: string }[];
|
|
45
|
+
new_reaction: { type: string; emoji?: string }[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface TgReactionCount {
|
|
49
|
+
chat: { id: number; type: string };
|
|
50
|
+
message_id: number;
|
|
51
|
+
date: number;
|
|
52
|
+
reactions: {
|
|
53
|
+
type: { type: string; emoji?: string };
|
|
54
|
+
total_count: number;
|
|
55
|
+
}[];
|
|
56
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export class TelegramTokenError extends Error {}
|
|
2
|
+
|
|
3
|
+
export interface TelegramBotIdentity {
|
|
4
|
+
botId: number;
|
|
5
|
+
username: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const API = 'https://api.telegram.org';
|
|
9
|
+
const TIMEOUT_MS = 10_000;
|
|
10
|
+
|
|
11
|
+
interface GetMe {
|
|
12
|
+
ok?: unknown;
|
|
13
|
+
result?: { id?: unknown; username?: unknown; is_bot?: unknown };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function verifyTelegramBotToken(
|
|
17
|
+
token: string,
|
|
18
|
+
): Promise<TelegramBotIdentity> {
|
|
19
|
+
let res: Response;
|
|
20
|
+
try {
|
|
21
|
+
res = await fetch(`${API}/bot${token}/getMe`, {
|
|
22
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
23
|
+
});
|
|
24
|
+
} catch {
|
|
25
|
+
throw new TelegramTokenError(
|
|
26
|
+
'could not reach the Telegram API to check that bot token',
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
const body = (await res.json().catch(() => null)) as GetMe | null;
|
|
30
|
+
const me = body?.ok === true ? body.result : undefined;
|
|
31
|
+
if (!me) throw new TelegramTokenError('Telegram rejected that bot token');
|
|
32
|
+
if (typeof me.id !== 'number' || me.is_bot !== true)
|
|
33
|
+
throw new TelegramTokenError('that token does not belong to a Telegram bot');
|
|
34
|
+
return {
|
|
35
|
+
botId: me.id,
|
|
36
|
+
username: typeof me.username === 'string' ? me.username : '',
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { emit, respond, mintId } from '@metro-labs/core/stations/station-runtime';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metro-labs/threema",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/station.ts",
|
|
8
|
+
"./train": "./src/index.ts",
|
|
9
|
+
"./verify": "./src/verify.ts"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"test": "tsc --noEmit && bun test test/"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@metro-labs/core": "workspace:*",
|
|
17
|
+
"tweetnacl": "^1.0.3"
|
|
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,106 @@
|
|
|
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 { Line } from '@metro-labs/core/lines';
|
|
9
|
+
import { fetchPublicKey } from './api.js';
|
|
10
|
+
import { hexToBytes, keyPairFrom, type KeyPair } from './crypto.js';
|
|
11
|
+
import { isGatewayId, normalizeThreemaId, parsePrivateKey } from './ids.js';
|
|
12
|
+
import { GroupStore, parseGroupKey } from './groups.js';
|
|
13
|
+
import type { GroupRef } from './messages.js';
|
|
14
|
+
|
|
15
|
+
const ACCOUNTS_FILE =
|
|
16
|
+
process.env.THREEMA_ACCOUNTS_FILE ??
|
|
17
|
+
join(homedir(), '.metro', 'threema-accounts.json');
|
|
18
|
+
|
|
19
|
+
export interface AccountConfig {
|
|
20
|
+
id: string;
|
|
21
|
+
gatewayId: string;
|
|
22
|
+
secret: string;
|
|
23
|
+
privateKey: string;
|
|
24
|
+
callbackId?: string;
|
|
25
|
+
callbackToken?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function checkAccount(a: AccountConfig, die: Die): void {
|
|
29
|
+
if (!a.id) die('account missing id');
|
|
30
|
+
if (typeof a.gatewayId !== 'string' || !isGatewayId(a.gatewayId))
|
|
31
|
+
die(`account '${a.id}' has no Gateway ID`);
|
|
32
|
+
if (typeof a.secret !== 'string' || a.secret === '')
|
|
33
|
+
die(`account '${a.id}' missing secret`);
|
|
34
|
+
if (typeof a.privateKey !== 'string' || parsePrivateKey(a.privateKey) === null)
|
|
35
|
+
die(`account '${a.id}' has no usable private key`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const { loadAccounts } = makeAccountStore<AccountConfig>({
|
|
39
|
+
prefix: 'threema',
|
|
40
|
+
file: ACCOUNTS_FILE,
|
|
41
|
+
validate(raw, die) {
|
|
42
|
+
const seenId = new Set<string>();
|
|
43
|
+
const seenGateway = new Set<string>();
|
|
44
|
+
for (const a of raw) {
|
|
45
|
+
checkAccount(a, die);
|
|
46
|
+
if (seenId.has(a.id)) die(`duplicate account id '${a.id}'`);
|
|
47
|
+
if (seenGateway.has(a.gatewayId))
|
|
48
|
+
die(`account '${a.id}' reuses the Gateway ID of another account`);
|
|
49
|
+
seenId.add(a.id);
|
|
50
|
+
seenGateway.add(a.gatewayId);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export interface Account {
|
|
56
|
+
cfg: AccountConfig;
|
|
57
|
+
keys: KeyPair;
|
|
58
|
+
publicKeys: Map<string, Uint8Array>;
|
|
59
|
+
groups: GroupStore;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type Target = { kind: 'user'; id: string } | { kind: 'group'; group: GroupRef };
|
|
63
|
+
|
|
64
|
+
export const accounts = new Map<string, Account>();
|
|
65
|
+
|
|
66
|
+
export function bootAccount(cfg: AccountConfig): Account {
|
|
67
|
+
const key = parsePrivateKey(cfg.privateKey);
|
|
68
|
+
if (key === null) throw new Error(`account '${cfg.id}' has no usable private key`);
|
|
69
|
+
return { cfg, keys: keyPairFrom(key), publicKeys: new Map(), groups: new GroupStore(cfg.id, cfg.gatewayId) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function accountFor(id: string): Account {
|
|
73
|
+
const acct = accounts.get(id);
|
|
74
|
+
if (!acct)
|
|
75
|
+
throw new Error(
|
|
76
|
+
`unknown account '${id}' (have: ${[...accounts.keys()].join(', ')})`,
|
|
77
|
+
);
|
|
78
|
+
return acct;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function targetOf(
|
|
82
|
+
line: string,
|
|
83
|
+
account?: string,
|
|
84
|
+
): { acct: Account; target: Target } {
|
|
85
|
+
const parsed = Line.parseThreema(line);
|
|
86
|
+
if (parsed === null) throw new Error(`not a threema line: ${line}`);
|
|
87
|
+
const id = resolveAccountId(
|
|
88
|
+
accounts,
|
|
89
|
+
{ account, line },
|
|
90
|
+
(l) => Line.parseThreema(l)?.accountId,
|
|
91
|
+
);
|
|
92
|
+
const group = parseGroupKey(parsed.resource);
|
|
93
|
+
const target: Target = group === null ? { kind: 'user', id: normalizeThreemaId(parsed.resource) } : { kind: 'group', group };
|
|
94
|
+
return { acct: accountFor(id), target };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function publicKeyFor(
|
|
98
|
+
acct: Account,
|
|
99
|
+
threemaId: string,
|
|
100
|
+
): Promise<Uint8Array> {
|
|
101
|
+
const cached = acct.publicKeys.get(threemaId);
|
|
102
|
+
if (cached) return cached;
|
|
103
|
+
const key = hexToBytes(await fetchPublicKey(acct.cfg, threemaId), 'public key');
|
|
104
|
+
acct.publicKeys.set(threemaId, key);
|
|
105
|
+
return key;
|
|
106
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
2
|
+
import {
|
|
3
|
+
makeStation,
|
|
4
|
+
respond,
|
|
5
|
+
type CallMsg,
|
|
6
|
+
} from '@metro-labs/core/stations/station-runtime';
|
|
7
|
+
import type { Normalized } from '@metro-labs/core/stations/messaging-normalize';
|
|
8
|
+
import { accountFor, accounts, publicKeyFor, targetOf, type Account } from './accounts.js';
|
|
9
|
+
import { hexToBytes, macMatches, open } from './crypto.js';
|
|
10
|
+
import { groupLineOf, type InboundMeta } from './format.js';
|
|
11
|
+
import { groupKey } from './groups.js';
|
|
12
|
+
import { deliver } from './inbound.js';
|
|
13
|
+
import { isThreemaId, normalizeThreemaId } from './ids.js';
|
|
14
|
+
import { decode } from './messages.js';
|
|
15
|
+
import { react, send } from './outbound.js';
|
|
16
|
+
|
|
17
|
+
export type { CallMsg };
|
|
18
|
+
export { MAX_TEXT_BYTES } from './outbound.js';
|
|
19
|
+
|
|
20
|
+
type Args = Record<string, unknown>;
|
|
21
|
+
|
|
22
|
+
const WEB_CHAT = 'https://web.threema.com/#!/messenger/conversation/contact/';
|
|
23
|
+
|
|
24
|
+
export const chatUrl = (gatewayId: string): string => `${WEB_CHAT}${encodeURIComponent(gatewayId)}`;
|
|
25
|
+
|
|
26
|
+
function listAccounts(id: string): void {
|
|
27
|
+
const list = [...accounts.values()].map((a) => ({
|
|
28
|
+
id: a.cfg.id,
|
|
29
|
+
handle: a.cfg.gatewayId,
|
|
30
|
+
url: chatUrl(a.cfg.gatewayId),
|
|
31
|
+
gatewayId: a.cfg.gatewayId,
|
|
32
|
+
groups: a.groups.list().map((g) => ({ line: groupLineOf(a.cfg.id, g), name: g.name, members: g.members.length })),
|
|
33
|
+
...(a.cfg.callbackId && a.cfg.callbackToken
|
|
34
|
+
? { callbackId: a.cfg.callbackId, callbackToken: a.cfg.callbackToken }
|
|
35
|
+
: {}),
|
|
36
|
+
}));
|
|
37
|
+
respond(id, { result: { accounts: list } });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function listMembers(id: string, args: Args): void {
|
|
41
|
+
const line = typeof args.line === 'string' ? args.line : '';
|
|
42
|
+
const { acct, target } = targetOf(line, typeof args.account === 'string' ? args.account : undefined);
|
|
43
|
+
if (target.kind !== 'group') {
|
|
44
|
+
respond(id, { result: { members: [], capability: { supported: false, complete: false, reason: 'a Threema 1:1 chat has no roster' } } });
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const roster = acct.groups.get(target.group);
|
|
48
|
+
if (roster === undefined) {
|
|
49
|
+
respond(id, { result: { members: [], capability: { supported: true, complete: false, reason: `no member list yet for ${groupKey(target.group)}` } } });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const members = roster.members.map((m) => ({ id: m, name: m, is_admin: m === roster.creator, is_bot: m === acct.cfg.gatewayId }));
|
|
53
|
+
respond(id, { result: { members, capability: { supported: true, complete: true, total: members.length } } });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface CallbackArgs {
|
|
57
|
+
account: string;
|
|
58
|
+
from: string;
|
|
59
|
+
to: string;
|
|
60
|
+
messageId: string;
|
|
61
|
+
date: string;
|
|
62
|
+
nonce: string;
|
|
63
|
+
box: string;
|
|
64
|
+
mac: string;
|
|
65
|
+
nickname?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const str = (v: unknown): string => (typeof v === 'string' ? v : '');
|
|
69
|
+
|
|
70
|
+
function parseCallback(args: Args): CallbackArgs {
|
|
71
|
+
const fields = {
|
|
72
|
+
account: str(args.account),
|
|
73
|
+
from: str(args.from),
|
|
74
|
+
to: str(args.to),
|
|
75
|
+
messageId: str(args.messageId),
|
|
76
|
+
date: str(args.date),
|
|
77
|
+
nonce: str(args.nonce),
|
|
78
|
+
box: str(args.box),
|
|
79
|
+
mac: str(args.mac),
|
|
80
|
+
};
|
|
81
|
+
for (const [key, value] of Object.entries(fields))
|
|
82
|
+
if (value === '')
|
|
83
|
+
throw new TrainError('threema_bad_callback', `callback is missing ${key}`, {
|
|
84
|
+
retryable: false,
|
|
85
|
+
});
|
|
86
|
+
const nickname = str(args.nickname);
|
|
87
|
+
return nickname === '' ? fields : { ...fields, nickname };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function checkedSender(acct: Account, cb: CallbackArgs): string {
|
|
91
|
+
if (!macMatches(acct.cfg.secret, cb, cb.mac))
|
|
92
|
+
throw new TrainError('threema_bad_mac', 'the callback MAC does not match this account API secret', { retryable: false });
|
|
93
|
+
const from = normalizeThreemaId(cb.from);
|
|
94
|
+
if (!isThreemaId(from))
|
|
95
|
+
throw new TrainError('threema_bad_callback', `'${cb.from}' is not a Threema ID`, { retryable: false });
|
|
96
|
+
if (normalizeThreemaId(cb.to) !== acct.cfg.gatewayId)
|
|
97
|
+
throw new TrainError('threema_wrong_recipient', `callback addressed to ${cb.to}, not to ${acct.cfg.gatewayId}`, { retryable: false });
|
|
98
|
+
return from;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function callback(id: string, args: Args): Promise<void> {
|
|
102
|
+
const cb = parseCallback(args);
|
|
103
|
+
const acct = accountFor(cb.account);
|
|
104
|
+
const from = checkedSender(acct, cb);
|
|
105
|
+
const plain = open(
|
|
106
|
+
hexToBytes(cb.box, 'box'),
|
|
107
|
+
hexToBytes(cb.nonce, 'nonce'),
|
|
108
|
+
await publicKeyFor(acct, from),
|
|
109
|
+
acct.keys,
|
|
110
|
+
);
|
|
111
|
+
if (plain === null)
|
|
112
|
+
throw new TrainError(
|
|
113
|
+
'threema_undecryptable',
|
|
114
|
+
`could not decrypt a message from ${from}; the private key may not match the Gateway ID`,
|
|
115
|
+
{ retryable: false },
|
|
116
|
+
);
|
|
117
|
+
const meta: InboundMeta = { ...cb, from, messageId: cb.messageId.toLowerCase() };
|
|
118
|
+
respond(id, { result: { ok: true, kind: deliver(acct, meta, decode(plain)) } });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function normalizeThreema(action: string, env: Args): Normalized {
|
|
122
|
+
if (action === 'reply')
|
|
123
|
+
return {
|
|
124
|
+
action: 'send',
|
|
125
|
+
args: { line: env.line, text: env.text, replyTo: env.replyTo, account: env.account },
|
|
126
|
+
};
|
|
127
|
+
if (action === 'unreact') return { action: 'react', args: { ...env, action: 'removed' } };
|
|
128
|
+
return { action, args: env };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export const handleCall = makeStation({
|
|
132
|
+
handlers: { accounts: listAccounts, send, react, callback, listMembers },
|
|
133
|
+
normalize: normalizeThreema,
|
|
134
|
+
});
|