@stage-labs/metro 0.1.0-beta.21 → 0.1.0-beta.210
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 +9 -101
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +152 -0
- package/dist/cli.js +52 -109
- package/dist/control.js +47 -75
- package/dist/hold.js +210 -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 +96 -0
- package/dist/runtime.js +29 -52
- package/dist/serve.js +157 -0
- package/dist/service.js +304 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +13 -1
- package/package.json +2 -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 +26 -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/{mcp/src/daemon → core/src}/events.ts +11 -7
- package/runtime/node_modules/@metro-labs/{mcp/src/db → core/src}/ids.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/stations → core/src}/lines.ts +24 -28
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/protocol.ts +7 -4
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/secure-fs.ts +17 -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 +83 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/attachments.ts +0 -1
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +16 -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/{mcp → core}/src/stations/station-runtime.ts +6 -11
- 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 +84 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/types.ts +18 -1
- package/runtime/node_modules/@metro-labs/core/src/tickets.ts +37 -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 +89 -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 +80 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +99 -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 +144 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +89 -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 +142 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/db/key-map.ts → daemon/src/agents/keys.ts} +1 -11
- package/runtime/node_modules/@metro-labs/{mcp/src/db/agent-map.ts → daemon/src/agents/map.ts} +58 -1
- package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/db/agent-scope.ts → daemon/src/agents/scope.ts} +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +94 -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/pending.ts +150 -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 +218 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +36 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/boot}/crash-guard.ts +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +21 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/boot}/paths.ts +6 -34
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +26 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/inbound.ts +70 -45
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/media-note.ts +11 -7
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/pending.ts +4 -1
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/relay.ts +2 -2
- 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 +32 -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 +284 -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 +273 -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/{mcp/src/daemon/connector-api.ts → daemon/src/connectors/api.ts} +79 -113
- package/runtime/node_modules/@metro-labs/{mcp/src/db/connector-config.ts → daemon/src/connectors/config.ts} +67 -41
- 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/{mcp/src/daemon → daemon/src/connectors}/oauth-client.ts +14 -17
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/connectors}/oauth-discovery.ts +33 -20
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth-routes.ts → daemon/src/connectors/oauth-routes.ts} +30 -32
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth.ts → daemon/src/connectors/oauth.ts} +61 -22
- 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.ts +347 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +385 -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/{mcp/src/daemon → daemon/src/files}/attach-grant.ts +4 -4
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-owner.ts +3 -3
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-reaper.ts +174 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-serve.ts +12 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/upload-api.ts +10 -27
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/upload-store.ts +4 -4
- 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 +195 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-version.ts +44 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +128 -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 +224 -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 +278 -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 +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +244 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/accounts.ts +28 -17
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/call-tools.ts +49 -20
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/ctx.ts +4 -4
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/event-store.ts +15 -16
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/group-tools.ts +2 -2
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/index.ts +24 -79
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +19 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/member-tools.ts +2 -2
- 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 +82 -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/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/{mcp → daemon}/src/mcp/session.ts +33 -42
- 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/{mcp → daemon}/src/mcp/tool-dispatch.ts +53 -40
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/tool-schemas.ts +68 -119
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/upload-tool.ts +6 -11
- 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/install.ts +37 -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/{mcp/src/daemon → daemon/src/routes}/http.ts +59 -51
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +205 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +66 -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/{mcp → daemon}/src/stations/attach-inline.ts +29 -1
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-interactive.ts +9 -36
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-outlook.ts +63 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-resolve.ts +38 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/attach-session.ts +32 -22
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-xmtp.ts +8 -9
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach.ts +75 -9
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +265 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/registry.ts +22 -6
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor-io.ts +3 -3
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor.ts +6 -9
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/train-call.ts +1 -1
- 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 +9 -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 +147 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +44 -0
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/package.json +3 -5
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/accounts.ts +8 -28
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/actions.ts +19 -187
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/format.ts +31 -72
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group-actions.ts +2 -2
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/index.ts +15 -21
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/members.ts +2 -2
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -0
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/send-files.ts +12 -13
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +23 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +135 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/session-api.ts → http/src/mode-api.ts} +13 -9
- 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/{telegram-user → outlook}/package.json +2 -5
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +93 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +14 -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 +140 -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 +3 -2
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +38 -96
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +214 -253
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +67 -108
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/client.ts +6 -6
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +101 -147
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/group.ts +4 -4
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/history.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +6 -102
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/login.ts +20 -20
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +64 -151
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +61 -33
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/reactions.ts +3 -3
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +11 -1
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +5 -47
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +86 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +240 -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 +170 -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 +90 -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 +85 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +21 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +95 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +124 -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 +47 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +164 -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 +1 -1
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +5 -8
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +2 -5
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +6 -28
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +54 -11
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +2 -2
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +86 -4
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +7 -58
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +11 -28
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +4 -37
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/names.ts +75 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +44 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +16 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +5 -9
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +0 -1
- package/runtime/node_modules/@metro-labs/xmtp/package.json +3 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +10 -20
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +2 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +15 -204
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +4 -43
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +41 -219
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +7 -24
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +8 -18
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +1 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +30 -16
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +12 -20
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +1 -19
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +1 -1
- 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 +5 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +4 -51
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +30 -44
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +4 -6
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +19 -5
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +14 -4
- package/runtime/runtime.json +1 -1
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +37 -0
- package/dist/prompt.js +0 -10
- package/dist/store.js +0 -70
- package/runtime/node_modules/@metro-labs/discord/src/station.ts +0 -19
- package/runtime/node_modules/@metro-labs/discord/src/voice-speak.ts +0 -195
- package/runtime/node_modules/@metro-labs/discord/src/voice-transcribe.ts +0 -292
- package/runtime/node_modules/@metro-labs/discord/src/voice.ts +0 -197
- package/runtime/node_modules/@metro-labs/discord/src/wire.ts +0 -5
- package/runtime/node_modules/@metro-labs/mcp/package.json +0 -66
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +0 -24
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +0 -311
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +0 -335
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +0 -129
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +0 -322
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +0 -111
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair.ts +0 -20
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/collection-api.ts +0 -202
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +0 -31
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +0 -100
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +0 -57
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +0 -333
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +0 -164
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +0 -208
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +0 -79
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +0 -65
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +0 -58
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +0 -214
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +0 -125
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-pair.ts +0 -18
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +0 -105
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +0 -41
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +0 -218
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +0 -169
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +0 -91
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +0 -215
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +0 -37
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-collections.ts +0 -218
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +0 -329
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +0 -364
- package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +0 -300
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +0 -31
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +0 -105
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +0 -131
- package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +0 -45
- package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +0 -57
- package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +0 -18
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +0 -29
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +0 -21
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +0 -66
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +0 -73
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +0 -172
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +0 -43
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +0 -240
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +0 -2
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +0 -80
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +0 -88
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +0 -3
- package/runtime/node_modules/@metro-labs/telegram-user/src/accounts.ts +0 -81
- package/runtime/node_modules/@metro-labs/telegram-user/src/actions.ts +0 -254
- package/runtime/node_modules/@metro-labs/telegram-user/src/attachments.ts +0 -87
- package/runtime/node_modules/@metro-labs/telegram-user/src/format.ts +0 -141
- package/runtime/node_modules/@metro-labs/telegram-user/src/inbound.ts +0 -78
- package/runtime/node_modules/@metro-labs/telegram-user/src/index.ts +0 -46
- package/runtime/node_modules/@metro-labs/telegram-user/src/media-actions.ts +0 -80
- package/runtime/node_modules/@metro-labs/telegram-user/src/members.ts +0 -75
- package/runtime/node_modules/@metro-labs/telegram-user/src/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/telegram-user/src/station.ts +0 -24
- package/runtime/node_modules/@metro-labs/telegram-user/src/types.ts +0 -7
- package/runtime/node_modules/@metro-labs/telegram-user/src/wire.ts +0 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +0 -5
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +0 -87
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +0 -55
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +0 -349
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/log.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/mcp → core/src}/str.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/train-error.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/webhook-note.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/raw-get-stream.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/api-base.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/verify.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → http/src}/api-error.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → http/src}/cors.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{telegram → telegram-bot}/src/verify.ts +0 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { accounts, lineOf } from './accounts.js';
|
|
2
|
+
import { reportAttachment } from '@metro-labs/core/stations/train-events';
|
|
3
|
+
import { mintId } from '@metro-labs/core/stations/station-runtime';
|
|
4
|
+
import { mediaRefOf, saveTelegramMedia } from './attachments.js';
|
|
5
|
+
import type { TgMsg, TgReaction, TgReactionCount, TgUser } from './types.js';
|
|
6
|
+
|
|
7
|
+
export type { TgMsg, TgReaction, TgReactionCount };
|
|
8
|
+
|
|
9
|
+
function lineForMsg(
|
|
10
|
+
accountId: string,
|
|
11
|
+
m: TgMsg,
|
|
12
|
+
): { line: string; topicId?: number } {
|
|
13
|
+
const topicId = m.is_topic_message ? m.message_thread_id : undefined;
|
|
14
|
+
return { line: lineOf(accountId, m.chat.id, topicId), topicId };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function stickerTag(s: NonNullable<TgMsg['sticker']>): string {
|
|
18
|
+
const set = s.set_name ? ` · ${s.set_name}` : '';
|
|
19
|
+
return `[sticker${s.emoji ? ` ${s.emoji}` : ''}${set}]`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const TAG_EXTRACTORS: ((m: TgMsg) => string | null)[] = [
|
|
23
|
+
(m) => (m.photo?.length ? '[image]' : null),
|
|
24
|
+
(m) => (m.voice ? '[voice]' : null),
|
|
25
|
+
(m) => (m.audio ? `[audio: ${m.audio.file_name ?? 'audio'}]` : null),
|
|
26
|
+
(m) => (m.video ? `[video: ${m.video.file_name ?? 'video'}]` : null),
|
|
27
|
+
(m) => (m.animation ? `[gif: ${m.animation.file_name ?? 'gif'}]` : null),
|
|
28
|
+
(m) => (m.sticker ? stickerTag(m.sticker) : null),
|
|
29
|
+
(m) =>
|
|
30
|
+
m.document && !m.animation
|
|
31
|
+
? `[file: ${m.document.file_name ?? 'doc'}]`
|
|
32
|
+
: null,
|
|
33
|
+
(m) =>
|
|
34
|
+
m.location
|
|
35
|
+
? `[location: ${m.location.latitude}, ${m.location.longitude}]`
|
|
36
|
+
: null,
|
|
37
|
+
(m) => (m.dice ? `[dice ${m.dice.emoji} = ${m.dice.value}]` : null),
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
function projectText(m: TgMsg): string {
|
|
41
|
+
const tags = TAG_EXTRACTORS.map((f) => f(m)).filter(
|
|
42
|
+
(t): t is string => t !== null,
|
|
43
|
+
);
|
|
44
|
+
return [m.text ?? m.caption, ...tags].filter(Boolean).join(' ');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const botIdOf = (accountId: string): number | null => {
|
|
48
|
+
const token = accounts.get(accountId)?.cfg.token ?? '';
|
|
49
|
+
const id = Number(token.split(':')[0]);
|
|
50
|
+
return Number.isInteger(id) && id > 0 ? id : null;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function mentionsBot(m: TgMsg, botId: number | null, username: string | undefined): boolean {
|
|
54
|
+
const text = m.text ?? m.caption ?? '';
|
|
55
|
+
const handle = username === undefined ? null : `@${username.toLowerCase()}`;
|
|
56
|
+
return [...(m.entities ?? []), ...(m.caption_entities ?? [])].some((e) => {
|
|
57
|
+
if (e.type === 'text_mention') return botId !== null && e.user?.id === botId;
|
|
58
|
+
if (e.type !== 'mention' || handle === null) return false;
|
|
59
|
+
return text.slice(e.offset, e.offset + e.length).toLowerCase() === handle;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function addressing(accountId: string, m: TgMsg): Record<string, unknown> {
|
|
64
|
+
const botId = botIdOf(accountId);
|
|
65
|
+
const topicRoot = m.is_topic_message === true && m.reply_to_message?.message_id === m.message_thread_id;
|
|
66
|
+
const replied = topicRoot ? undefined : m.reply_to_message;
|
|
67
|
+
const replyTo = replied === undefined ? undefined : String(replied.message_id);
|
|
68
|
+
return {
|
|
69
|
+
...(replyTo === undefined ? {} : { reply_to: replyTo, event: { type: 'reply', replyTo } }),
|
|
70
|
+
mentions_self: mentionsBot(m, botId, accounts.get(accountId)?.username),
|
|
71
|
+
reply_to_self: botId !== null && replied?.from?.id === botId,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const fullName = (u: TgUser | undefined): string | undefined => {
|
|
76
|
+
const name = [u?.first_name, u?.last_name].filter((part) => typeof part === 'string' && part !== '').join(' ');
|
|
77
|
+
return name === '' ? undefined : name;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export function envelope(accountId: string, m: TgMsg): Record<string, unknown> {
|
|
81
|
+
const { line } = lineForMsg(accountId, m);
|
|
82
|
+
return {
|
|
83
|
+
id: mintId(),
|
|
84
|
+
ts: new Date(m.date * 1000).toISOString(),
|
|
85
|
+
station: 'telegram-bot',
|
|
86
|
+
line,
|
|
87
|
+
line_name: m.chat.title ?? m.chat.first_name ?? undefined,
|
|
88
|
+
from: `metro://telegram-bot/${accountId}/user/${m.from?.id ?? 'unknown'}`,
|
|
89
|
+
from_name: m.from?.username ? `@${m.from.username}` : m.from?.first_name,
|
|
90
|
+
from_display_name: fullName(m.from),
|
|
91
|
+
message_id: String(m.message_id),
|
|
92
|
+
text: projectText(m),
|
|
93
|
+
payload: m,
|
|
94
|
+
is_private: m.chat.type === 'private',
|
|
95
|
+
...addressing(accountId, m),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function reactionEnvelope(
|
|
100
|
+
accountId: string,
|
|
101
|
+
r: TgReaction,
|
|
102
|
+
): Record<string, unknown> | null {
|
|
103
|
+
if (r.user?.is_bot) return null;
|
|
104
|
+
const newEmojis = r.new_reaction
|
|
105
|
+
.filter((x) => x.type === 'emoji')
|
|
106
|
+
.map((x) => x.emoji ?? '');
|
|
107
|
+
const oldEmojis = r.old_reaction
|
|
108
|
+
.filter((x) => x.type === 'emoji')
|
|
109
|
+
.map((x) => x.emoji ?? '');
|
|
110
|
+
const added = newEmojis.filter((e) => !oldEmojis.includes(e));
|
|
111
|
+
if (!added.length) return null;
|
|
112
|
+
return {
|
|
113
|
+
id: mintId(),
|
|
114
|
+
ts: new Date(r.date * 1000).toISOString(),
|
|
115
|
+
station: 'telegram-bot',
|
|
116
|
+
line: lineOf(accountId, r.chat.id),
|
|
117
|
+
from: `metro://telegram-bot/${accountId}/user/${r.user?.id ?? 'unknown'}`,
|
|
118
|
+
from_name: r.user?.username ? `@${r.user.username}` : r.user?.first_name,
|
|
119
|
+
from_display_name: fullName(r.user),
|
|
120
|
+
message_id: String(r.message_id),
|
|
121
|
+
emoji: added[0],
|
|
122
|
+
event: { type: 'react', emoji: added[0], targetId: String(r.message_id) },
|
|
123
|
+
is_private: r.chat.type === 'private',
|
|
124
|
+
payload: r,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function reactionCountEnvelope(
|
|
129
|
+
accountId: string,
|
|
130
|
+
rc: TgReactionCount,
|
|
131
|
+
): Record<string, unknown> | null {
|
|
132
|
+
const emojiReactions = rc.reactions.filter(
|
|
133
|
+
(r) => r.type.type === 'emoji' && r.type.emoji,
|
|
134
|
+
);
|
|
135
|
+
if (!emojiReactions.length) return null;
|
|
136
|
+
const top = emojiReactions.reduce((a, b) =>
|
|
137
|
+
b.total_count > a.total_count ? b : a,
|
|
138
|
+
);
|
|
139
|
+
const emoji = top.type.emoji ?? '';
|
|
140
|
+
if (!emoji) return null;
|
|
141
|
+
return {
|
|
142
|
+
id: mintId(),
|
|
143
|
+
ts: new Date(rc.date * 1000).toISOString(),
|
|
144
|
+
station: 'telegram-bot',
|
|
145
|
+
line: lineOf(accountId, rc.chat.id),
|
|
146
|
+
from: `metro://telegram-bot/${accountId}/user/unknown`,
|
|
147
|
+
message_id: String(rc.message_id),
|
|
148
|
+
emoji,
|
|
149
|
+
event: { type: 'react', emoji, targetId: String(rc.message_id) },
|
|
150
|
+
is_private: rc.chat.type === 'private',
|
|
151
|
+
payload: rc,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function saveMediaAndEmit(
|
|
156
|
+
accountId: string,
|
|
157
|
+
m: TgMsg,
|
|
158
|
+
sourceEnvId: string,
|
|
159
|
+
): void {
|
|
160
|
+
const ref = mediaRefOf(m);
|
|
161
|
+
if (!ref) return;
|
|
162
|
+
const line = lineForMsg(accountId, m).line;
|
|
163
|
+
reportAttachment(saveTelegramMedia(accountId, ref, String(m.message_id), 0), {
|
|
164
|
+
station: 'telegram-bot',
|
|
165
|
+
account: accountId,
|
|
166
|
+
line,
|
|
167
|
+
forId: sourceEnvId,
|
|
168
|
+
index: 0,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
2
|
+
import { accounts, loadAccounts, tg, type Account } from './accounts.js';
|
|
3
|
+
import { emitInbound } from '@metro-labs/core/stations/train-events';
|
|
4
|
+
import { announceAccounts } from '@metro-labs/core/stations/train-boot';
|
|
5
|
+
import {
|
|
6
|
+
envelope,
|
|
7
|
+
reactionCountEnvelope,
|
|
8
|
+
reactionEnvelope,
|
|
9
|
+
saveMediaAndEmit,
|
|
10
|
+
type TgMsg,
|
|
11
|
+
type TgReaction,
|
|
12
|
+
type TgReactionCount,
|
|
13
|
+
} from './format.js';
|
|
14
|
+
import { readCalls } from '@metro-labs/core/trains/protocol';
|
|
15
|
+
import { handleCall } from './actions.js';
|
|
16
|
+
|
|
17
|
+
readCalls('telegram-bot', handleCall);
|
|
18
|
+
|
|
19
|
+
interface Update {
|
|
20
|
+
update_id: number;
|
|
21
|
+
message?: TgMsg;
|
|
22
|
+
message_reaction?: TgReaction;
|
|
23
|
+
message_reaction_count?: TgReactionCount;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function handleUpdate(id: string, u: Update): void {
|
|
27
|
+
if (u.message && !u.message.from?.is_bot) {
|
|
28
|
+
const env = envelope(id, u.message);
|
|
29
|
+
emitInbound(id, env);
|
|
30
|
+
saveMediaAndEmit(id, u.message, env.id as string);
|
|
31
|
+
}
|
|
32
|
+
if (u.message_reaction) {
|
|
33
|
+
const env = reactionEnvelope(id, u.message_reaction);
|
|
34
|
+
if (env) emitInbound(id, env);
|
|
35
|
+
}
|
|
36
|
+
if (u.message_reaction_count) {
|
|
37
|
+
const env = reactionCountEnvelope(id, u.message_reaction_count);
|
|
38
|
+
if (env) emitInbound(id, env);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function runAccount(acct: Account): Promise<void> {
|
|
43
|
+
const { id } = acct.cfg;
|
|
44
|
+
try {
|
|
45
|
+
await tg(id, 'deleteWebhook', { drop_pending_updates: false });
|
|
46
|
+
} catch (err) {
|
|
47
|
+
process.stderr.write(
|
|
48
|
+
`telegram-bot[${id}] deleteWebhook: ${errMsg(err)}\n`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const me = await tg<{ username?: string }>(id, 'getMe', {});
|
|
53
|
+
if (typeof me.username === 'string') acct.username = me.username;
|
|
54
|
+
} catch (err) {
|
|
55
|
+
process.stderr.write(`telegram-bot[${id}] getMe: ${errMsg(err)}\n`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (;;) {
|
|
59
|
+
try {
|
|
60
|
+
const updates = await tg<Update[]>(
|
|
61
|
+
id,
|
|
62
|
+
'getUpdates',
|
|
63
|
+
{
|
|
64
|
+
offset: acct.offset,
|
|
65
|
+
timeout: 25,
|
|
66
|
+
allowed_updates: [
|
|
67
|
+
'message',
|
|
68
|
+
'message_reaction',
|
|
69
|
+
'message_reaction_count',
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
60_000,
|
|
73
|
+
);
|
|
74
|
+
for (const u of updates) {
|
|
75
|
+
acct.offset = u.update_id + 1;
|
|
76
|
+
handleUpdate(id, u);
|
|
77
|
+
}
|
|
78
|
+
} catch (err) {
|
|
79
|
+
process.stderr.write(
|
|
80
|
+
`telegram-bot[${id}] poll error: ${errMsg(err)}\n`,
|
|
81
|
+
);
|
|
82
|
+
await new Promise((r) => setTimeout(r, 2_000));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const cfgs = loadAccounts();
|
|
88
|
+
for (const cfg of cfgs) {
|
|
89
|
+
accounts.set(cfg.id, {
|
|
90
|
+
cfg,
|
|
91
|
+
api: `https://api.telegram.org/bot${cfg.token}`,
|
|
92
|
+
fileApi: `https://api.telegram.org/file/bot${cfg.token}`,
|
|
93
|
+
offset: 0,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
announceAccounts('telegram-bot', accounts.keys());
|
|
97
|
+
|
|
98
|
+
for (const acct of accounts.values())
|
|
99
|
+
runAccount(acct).catch((err: unknown) => {
|
|
100
|
+
process.stderr.write(
|
|
101
|
+
`telegram-bot[${acct.cfg.id}] account loop failed: ${errMsg(err)}\n`,
|
|
102
|
+
);
|
|
103
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { tg, targetOf } from './accounts.js';
|
|
2
|
+
import { selfUri } from '@metro-labs/core/stations/train-events';
|
|
3
|
+
import { emit, mintId, respond } from '@metro-labs/core/stations/station-runtime';
|
|
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
|
+
id: mintId(),
|
|
15
|
+
ts: new Date().toISOString(),
|
|
16
|
+
station: 'telegram-bot',
|
|
17
|
+
line,
|
|
18
|
+
from: selfUri(),
|
|
19
|
+
to: line,
|
|
20
|
+
message_id: messageId,
|
|
21
|
+
text,
|
|
22
|
+
reply_to: replyTo,
|
|
23
|
+
...(replyTo ? { event: { type: 'reply', replyTo } } : {}),
|
|
24
|
+
payload: { account: accountId },
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function finishSend(
|
|
29
|
+
id: string,
|
|
30
|
+
accountId: string,
|
|
31
|
+
line: string,
|
|
32
|
+
messageId: string,
|
|
33
|
+
label: string,
|
|
34
|
+
replyTo?: string,
|
|
35
|
+
extra?: Record<string, unknown>,
|
|
36
|
+
): void {
|
|
37
|
+
emitOutbound(accountId, line, messageId, label, replyTo);
|
|
38
|
+
respond(id, {
|
|
39
|
+
result: { messageId, account: accountId, ...extra },
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface MediaArgs {
|
|
44
|
+
line: string;
|
|
45
|
+
path: string;
|
|
46
|
+
caption?: string;
|
|
47
|
+
replyTo?: string;
|
|
48
|
+
account?: string;
|
|
49
|
+
name?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function sendMedia(
|
|
53
|
+
method: string,
|
|
54
|
+
fieldName: string,
|
|
55
|
+
args: Record<string, unknown>,
|
|
56
|
+
): Promise<{ accountId: string; message_id: number }> {
|
|
57
|
+
const {
|
|
58
|
+
line,
|
|
59
|
+
path,
|
|
60
|
+
caption,
|
|
61
|
+
replyTo,
|
|
62
|
+
account,
|
|
63
|
+
name: fileName,
|
|
64
|
+
} = args as unknown as MediaArgs;
|
|
65
|
+
const { accountId, chatId, topicId } = targetOf(line, account);
|
|
66
|
+
const form = new FormData();
|
|
67
|
+
form.append('chat_id', String(chatId));
|
|
68
|
+
if (topicId !== undefined) form.append('message_thread_id', String(topicId));
|
|
69
|
+
if (caption) form.append('caption', caption);
|
|
70
|
+
if (replyTo)
|
|
71
|
+
form.append(
|
|
72
|
+
'reply_parameters',
|
|
73
|
+
JSON.stringify({ message_id: Number(replyTo) }),
|
|
74
|
+
);
|
|
75
|
+
const name = fileName ?? path.split('/').pop() ?? fieldName;
|
|
76
|
+
await appendFile(form, fieldName, path, name);
|
|
77
|
+
const r = await tg<{ message_id: number }>(accountId, method, form);
|
|
78
|
+
return { accountId, message_id: r.message_id };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const MEDIA_METHOD_FIELD: Record<
|
|
82
|
+
string,
|
|
83
|
+
{ method: string; field: string }
|
|
84
|
+
> = {
|
|
85
|
+
image: { method: 'sendPhoto', field: 'photo' },
|
|
86
|
+
voice: { method: 'sendVoice', field: 'voice' },
|
|
87
|
+
audio: { method: 'sendAudio', field: 'audio' },
|
|
88
|
+
video: { method: 'sendVideo', field: 'video' },
|
|
89
|
+
document: { method: 'sendDocument', field: 'document' },
|
|
90
|
+
};
|
|
@@ -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,85 @@
|
|
|
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, accounts, tg } from './accounts.js';
|
|
13
|
+
import { respond } from '@metro-labs/core/stations/station-runtime';
|
|
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 tg(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
|
+
photo?: { small_file_id?: unknown };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const PHOTO_MAX = 64 * 1024;
|
|
46
|
+
|
|
47
|
+
async function photoData(accountId: string, fileId: unknown): Promise<string | undefined> {
|
|
48
|
+
if (typeof fileId !== 'string' || fileId === '') return undefined;
|
|
49
|
+
try {
|
|
50
|
+
const file = await tg<{ file_path?: string }>(accountId, 'getFile', { file_id: fileId }, 10_000);
|
|
51
|
+
const acct = accounts.get(accountId);
|
|
52
|
+
if (acct === undefined || typeof file.file_path !== 'string') return undefined;
|
|
53
|
+
const res = await fetch(`${acct.fileApi}/${file.file_path}`, { signal: AbortSignal.timeout(10_000) });
|
|
54
|
+
if (!res.ok) return undefined;
|
|
55
|
+
const bytes = Buffer.from(await res.arrayBuffer());
|
|
56
|
+
return bytes.length === 0 || bytes.length > PHOTO_MAX ? undefined : `data:image/jpeg;base64,${bytes.toString('base64')}`;
|
|
57
|
+
} catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const senders = makeProfileCache<SenderProfile>(
|
|
63
|
+
async (key) => {
|
|
64
|
+
const [accountId, user] = key.split(':', 2) as [string, string];
|
|
65
|
+
const chat = await tg<TgChat>(accountId, 'getChat', { chat_id: Number(user) }, 10_000);
|
|
66
|
+
const handle = nonEmpty(chat.username);
|
|
67
|
+
const display = nonEmpty([chat.first_name, chat.last_name].filter((part) => typeof part === 'string').join(' '));
|
|
68
|
+
const avatar = await photoData(accountId, chat.photo?.small_file_id);
|
|
69
|
+
return {
|
|
70
|
+
id: user,
|
|
71
|
+
...(handle === undefined ? {} : { name: `@${handle}` }),
|
|
72
|
+
...(display === undefined ? {} : { display_name: display }),
|
|
73
|
+
...(nonEmpty(chat.bio) === undefined ? {} : { about: nonEmpty(chat.bio) }),
|
|
74
|
+
...(avatar === undefined ? {} : { avatar }),
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
{ onError: (key, err) => process.stderr.write(`telegram-bot: could not read the profile of ${key}: ${err instanceof Error ? err.message : String(err)}\n`) },
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
export async function readProfile(id: string, args: Record<string, unknown>): Promise<void> {
|
|
81
|
+
const accountId = accountFor({ account: args.account as string | undefined });
|
|
82
|
+
const user = typeof args.user === 'string' ? args.user.trim() : '';
|
|
83
|
+
if (!/^\d+$/.test(user)) throw new TrainError('telegram_user_required', 'profile needs the numeric Telegram id of the person', { retryable: false });
|
|
84
|
+
respond(id, { result: (await senders.get(`${accountId}:${user}`)) ?? { id: user } });
|
|
85
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
'typing',
|
|
16
|
+
]),
|
|
17
|
+
attachmentMode: 'canonical',
|
|
18
|
+
profileFields: new Set<ProfileField>(PROFILE_FIELDS),
|
|
19
|
+
readsProfiles: true,
|
|
20
|
+
tools: [],
|
|
21
|
+
};
|
|
@@ -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,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,95 @@
|
|
|
1
|
+
import {
|
|
2
|
+
makeAccountStore,
|
|
3
|
+
resolveAccountId,
|
|
4
|
+
type Die,
|
|
5
|
+
} from '@metro-labs/core/stations/account-store';
|
|
6
|
+
import { Line } from '@metro-labs/core/lines';
|
|
7
|
+
import { fetchPublicKey } from './api.js';
|
|
8
|
+
import { hexToBytes, keyPairFrom, type KeyPair } from './crypto.js';
|
|
9
|
+
import { isGatewayId, normalizeThreemaId, parsePrivateKey } from './ids.js';
|
|
10
|
+
import { GroupStore, parseGroupKey } from './groups.js';
|
|
11
|
+
import type { GroupRef } from './messages.js';
|
|
12
|
+
|
|
13
|
+
export interface AccountConfig {
|
|
14
|
+
id: string;
|
|
15
|
+
gatewayId: string;
|
|
16
|
+
secret: string;
|
|
17
|
+
privateKey: string;
|
|
18
|
+
callbackId?: string;
|
|
19
|
+
callbackToken?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function checkAccount(a: AccountConfig, die: Die): void {
|
|
23
|
+
if (typeof a.gatewayId !== 'string' || !isGatewayId(a.gatewayId))
|
|
24
|
+
die(`account '${a.id}' has no Gateway ID`);
|
|
25
|
+
if (typeof a.secret !== 'string' || a.secret === '')
|
|
26
|
+
die(`account '${a.id}' missing secret`);
|
|
27
|
+
if (typeof a.privateKey !== 'string' || parsePrivateKey(a.privateKey) === null)
|
|
28
|
+
die(`account '${a.id}' has no usable private key`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const { loadAccounts } = makeAccountStore<AccountConfig>({
|
|
32
|
+
prefix: 'threema',
|
|
33
|
+
validate(raw, die) {
|
|
34
|
+
const seenGateway = new Set<string>();
|
|
35
|
+
for (const a of raw) {
|
|
36
|
+
checkAccount(a, die);
|
|
37
|
+
if (seenGateway.has(a.gatewayId))
|
|
38
|
+
die(`account '${a.id}' reuses the Gateway ID of another account`);
|
|
39
|
+
seenGateway.add(a.gatewayId);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export interface Account {
|
|
45
|
+
cfg: AccountConfig;
|
|
46
|
+
keys: KeyPair;
|
|
47
|
+
publicKeys: Map<string, Uint8Array>;
|
|
48
|
+
groups: GroupStore;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type Target = { kind: 'user'; id: string } | { kind: 'group'; group: GroupRef };
|
|
52
|
+
|
|
53
|
+
export const accounts = new Map<string, Account>();
|
|
54
|
+
|
|
55
|
+
export function bootAccount(cfg: AccountConfig): Account {
|
|
56
|
+
const key = parsePrivateKey(cfg.privateKey);
|
|
57
|
+
if (key === null) throw new Error(`account '${cfg.id}' has no usable private key`);
|
|
58
|
+
return { cfg, keys: keyPairFrom(key), publicKeys: new Map(), groups: new GroupStore(cfg.id, cfg.gatewayId) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function accountFor(id: string): Account {
|
|
62
|
+
const acct = accounts.get(id);
|
|
63
|
+
if (!acct)
|
|
64
|
+
throw new Error(
|
|
65
|
+
`unknown account '${id}' (have: ${[...accounts.keys()].join(', ')})`,
|
|
66
|
+
);
|
|
67
|
+
return acct;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function targetOf(
|
|
71
|
+
line: string,
|
|
72
|
+
account?: string,
|
|
73
|
+
): { acct: Account; target: Target } {
|
|
74
|
+
const parsed = Line.parseThreema(line);
|
|
75
|
+
if (parsed === null) throw new Error(`not a threema line: ${line}`);
|
|
76
|
+
const id = resolveAccountId(
|
|
77
|
+
accounts,
|
|
78
|
+
{ account, line },
|
|
79
|
+
(l) => Line.parseThreema(l)?.accountId,
|
|
80
|
+
);
|
|
81
|
+
const group = parseGroupKey(parsed.resource);
|
|
82
|
+
const target: Target = group === null ? { kind: 'user', id: normalizeThreemaId(parsed.resource) } : { kind: 'group', group };
|
|
83
|
+
return { acct: accountFor(id), target };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function publicKeyFor(
|
|
87
|
+
acct: Account,
|
|
88
|
+
threemaId: string,
|
|
89
|
+
): Promise<Uint8Array> {
|
|
90
|
+
const cached = acct.publicKeys.get(threemaId);
|
|
91
|
+
if (cached) return cached;
|
|
92
|
+
const key = hexToBytes(await fetchPublicKey(acct.cfg, threemaId), 'public key');
|
|
93
|
+
acct.publicKeys.set(threemaId, key);
|
|
94
|
+
return key;
|
|
95
|
+
}
|