@stage-labs/metro 0.1.0-beta.22 → 0.1.0-beta.221
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/background.js +123 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +173 -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 +51 -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 +22 -26
- 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 +14 -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/mail-text.ts +72 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/mail-trust.ts +29 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/mail.ts +212 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +16 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/oauth.ts +126 -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 +49 -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/daemon/src/agents/map.ts +133 -0
- 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 +219 -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 +75 -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 +368 -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 +295 -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 +282 -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 +136 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +93 -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 +307 -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-lists.ts +57 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +202 -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 +106 -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 +118 -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/tool-search.ts +126 -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 +34 -80
- 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 +88 -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 +71 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/session.ts +47 -44
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +93 -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 +64 -113
- 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 +63 -51
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +206 -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-browser.ts +102 -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 +5 -31
- 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 +69 -3
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +267 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/registry.ts +20 -2
- 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-bot/package.json +2 -4
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +3 -23
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +19 -187
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +2 -2
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +26 -67
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +1 -1
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +8 -14
- package/runtime/node_modules/@metro-labs/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-bot/src/send-files.ts +12 -13
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +5 -1
- package/runtime/node_modules/@metro-labs/gmail/package.json +25 -0
- package/runtime/node_modules/@metro-labs/gmail/src/accounts.ts +81 -0
- package/runtime/node_modules/@metro-labs/gmail/src/actions.ts +14 -0
- package/runtime/node_modules/@metro-labs/gmail/src/api.ts +31 -0
- package/runtime/node_modules/@metro-labs/gmail/src/attachments.ts +35 -0
- package/runtime/node_modules/@metro-labs/gmail/src/auth.ts +105 -0
- package/runtime/node_modules/@metro-labs/gmail/src/config.ts +13 -0
- package/runtime/node_modules/@metro-labs/gmail/src/inbound.ts +96 -0
- package/runtime/node_modules/@metro-labs/gmail/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/gmail/src/login.ts +37 -0
- package/runtime/node_modules/@metro-labs/gmail/src/message.ts +125 -0
- package/runtime/node_modules/@metro-labs/gmail/src/mime.ts +70 -0
- package/runtime/node_modules/@metro-labs/gmail/src/outbound.ts +124 -0
- package/runtime/node_modules/@metro-labs/gmail/src/query.ts +99 -0
- package/runtime/node_modules/@metro-labs/gmail/src/state.ts +24 -0
- package/runtime/node_modules/@metro-labs/gmail/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/gmail/src/trust.ts +42 -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/outlook/package.json +25 -0
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +78 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +14 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +81 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +120 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +42 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +23 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +59 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +55 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +96 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +129 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +31 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +103 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +84 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +28 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +41 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +1 -3
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +5 -24
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +8 -6
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +2 -2
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +19 -34
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +3 -3
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +13 -34
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +4 -43
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +2 -3
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +2 -2
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +4 -1
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +0 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +1 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +7 -41
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +22 -77
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +2 -2
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +46 -48
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +15 -15
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +6 -82
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +85 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +5 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +20 -12
- 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 +43 -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/resolve.ts +44 -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 +6 -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 +38 -0
- package/dist/prompt.js +0 -10
- package/dist/store.js +0 -70
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +0 -195
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +0 -292
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +0 -197
- package/runtime/node_modules/@metro-labs/discord-bot/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 -341
- 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/agent-map.ts +0 -71
- 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 -371
- 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/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +0 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +0 -3
- 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/{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
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { tg,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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';
|
|
4
5
|
|
|
5
6
|
export function emitOutbound(
|
|
6
7
|
accountId: string,
|
|
@@ -10,18 +11,16 @@ export function emitOutbound(
|
|
|
10
11
|
replyTo?: string,
|
|
11
12
|
): void {
|
|
12
13
|
emit({
|
|
13
|
-
kind: 'outbound',
|
|
14
14
|
id: mintId(),
|
|
15
15
|
ts: new Date().toISOString(),
|
|
16
16
|
station: 'telegram-bot',
|
|
17
17
|
line,
|
|
18
|
-
from:
|
|
18
|
+
from: selfUri(),
|
|
19
19
|
to: line,
|
|
20
20
|
message_id: messageId,
|
|
21
21
|
text,
|
|
22
22
|
reply_to: replyTo,
|
|
23
23
|
...(replyTo ? { event: { type: 'reply', replyTo } } : {}),
|
|
24
|
-
account: accountId,
|
|
25
24
|
payload: { account: accountId },
|
|
26
25
|
});
|
|
27
26
|
}
|
|
@@ -46,7 +45,6 @@ interface MediaArgs {
|
|
|
46
45
|
path: string;
|
|
47
46
|
caption?: string;
|
|
48
47
|
replyTo?: string;
|
|
49
|
-
parseMode?: string;
|
|
50
48
|
account?: string;
|
|
51
49
|
name?: string;
|
|
52
50
|
}
|
|
@@ -61,7 +59,6 @@ export async function sendMedia(
|
|
|
61
59
|
path,
|
|
62
60
|
caption,
|
|
63
61
|
replyTo,
|
|
64
|
-
parseMode,
|
|
65
62
|
account,
|
|
66
63
|
name: fileName,
|
|
67
64
|
} = args as unknown as MediaArgs;
|
|
@@ -70,7 +67,6 @@ export async function sendMedia(
|
|
|
70
67
|
form.append('chat_id', String(chatId));
|
|
71
68
|
if (topicId !== undefined) form.append('message_thread_id', String(topicId));
|
|
72
69
|
if (caption) form.append('caption', caption);
|
|
73
|
-
if (parseMode) form.append('parse_mode', parseMode);
|
|
74
70
|
if (replyTo)
|
|
75
71
|
form.append(
|
|
76
72
|
'reply_parameters',
|
|
@@ -78,29 +74,10 @@ export async function sendMedia(
|
|
|
78
74
|
);
|
|
79
75
|
const name = fileName ?? path.split('/').pop() ?? fieldName;
|
|
80
76
|
await appendFile(form, fieldName, path, name);
|
|
81
|
-
const r = await
|
|
77
|
+
const r = await tg<{ message_id: number }>(accountId, method, form);
|
|
82
78
|
return { accountId, message_id: r.message_id };
|
|
83
79
|
}
|
|
84
80
|
|
|
85
|
-
export async function media(
|
|
86
|
-
id: string,
|
|
87
|
-
method: string,
|
|
88
|
-
field: string,
|
|
89
|
-
label: string,
|
|
90
|
-
args: Record<string, unknown>,
|
|
91
|
-
): Promise<void> {
|
|
92
|
-
const { accountId, message_id } = await sendMedia(method, field, args);
|
|
93
|
-
const line = (args as { line: string }).line;
|
|
94
|
-
finishSend(
|
|
95
|
-
id,
|
|
96
|
-
accountId,
|
|
97
|
-
line,
|
|
98
|
-
String(message_id),
|
|
99
|
-
label,
|
|
100
|
-
args.replyTo as string | undefined,
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
81
|
export const MEDIA_METHOD_FIELD: Record<
|
|
105
82
|
string,
|
|
106
83
|
{ method: string; field: string }
|
|
@@ -111,56 +88,3 @@ export const MEDIA_METHOD_FIELD: Record<
|
|
|
111
88
|
video: { method: 'sendVideo', field: 'video' },
|
|
112
89
|
document: { method: 'sendDocument', field: 'document' },
|
|
113
90
|
};
|
|
114
|
-
|
|
115
|
-
export async function sendDice(
|
|
116
|
-
id: string,
|
|
117
|
-
args: Record<string, unknown>,
|
|
118
|
-
): Promise<void> {
|
|
119
|
-
const {
|
|
120
|
-
line,
|
|
121
|
-
emoji = '\U0001F3B2',
|
|
122
|
-
account,
|
|
123
|
-
} = args as { line: string; emoji?: string; account?: string };
|
|
124
|
-
const { accountId, chatId, topicId } = targetOf(line, account);
|
|
125
|
-
const body: Record<string, unknown> = { chat_id: chatId, emoji };
|
|
126
|
-
if (topicId !== undefined) body.message_thread_id = topicId;
|
|
127
|
-
const r = await tg<{ message_id: number; dice?: { value: number } }>(
|
|
128
|
-
accountId,
|
|
129
|
-
'sendDice',
|
|
130
|
-
body,
|
|
131
|
-
);
|
|
132
|
-
finishSend(
|
|
133
|
-
id,
|
|
134
|
-
accountId,
|
|
135
|
-
line,
|
|
136
|
-
String(r.message_id),
|
|
137
|
-
`[dice ${emoji} = ${r.dice?.value ?? '?'}]`,
|
|
138
|
-
undefined,
|
|
139
|
-
{ value: r.dice?.value },
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export async function sendLocation(
|
|
144
|
-
id: string,
|
|
145
|
-
args: Record<string, unknown>,
|
|
146
|
-
): Promise<void> {
|
|
147
|
-
const { line, latitude, longitude, account } = args as {
|
|
148
|
-
line: string;
|
|
149
|
-
latitude: number;
|
|
150
|
-
longitude: number;
|
|
151
|
-
account?: string;
|
|
152
|
-
};
|
|
153
|
-
const { accountId, chatId } = targetOf(line, account);
|
|
154
|
-
const r = await tg<{ message_id: number }>(accountId, 'sendLocation', {
|
|
155
|
-
chat_id: chatId,
|
|
156
|
-
latitude,
|
|
157
|
-
longitude,
|
|
158
|
-
});
|
|
159
|
-
finishSend(
|
|
160
|
-
id,
|
|
161
|
-
accountId,
|
|
162
|
-
line,
|
|
163
|
-
String(r.message_id),
|
|
164
|
-
`[location: ${latitude}, ${longitude}]`,
|
|
165
|
-
);
|
|
166
|
-
}
|
|
@@ -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
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Station, Verb } from '@metro-labs/
|
|
1
|
+
import type { Station, Verb } from '@metro-labs/core/stations/types';
|
|
2
|
+
import { PROFILE_FIELDS, type ProfileField } from '@metro-labs/core/stations/profile';
|
|
2
3
|
|
|
3
4
|
export const telegramBotStation: Station = {
|
|
4
5
|
name: 'telegram-bot',
|
|
@@ -11,7 +12,10 @@ export const telegramBotStation: Station = {
|
|
|
11
12
|
'unreact',
|
|
12
13
|
'edit',
|
|
13
14
|
'delete',
|
|
15
|
+
'typing',
|
|
14
16
|
]),
|
|
15
17
|
attachmentMode: 'canonical',
|
|
18
|
+
profileFields: new Set<ProfileField>(PROFILE_FIELDS),
|
|
19
|
+
readsProfiles: true,
|
|
16
20
|
tools: [],
|
|
17
21
|
};
|
|
@@ -1,13 +1,26 @@
|
|
|
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
|
+
|
|
1
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 } };
|
|
2
20
|
message_id: number;
|
|
3
21
|
date: number;
|
|
4
22
|
chat: { id: number; type: string; title?: string; first_name?: string };
|
|
5
|
-
from?:
|
|
6
|
-
id: number;
|
|
7
|
-
username?: string;
|
|
8
|
-
first_name?: string;
|
|
9
|
-
is_bot?: boolean;
|
|
10
|
-
};
|
|
23
|
+
from?: TgUser;
|
|
11
24
|
text?: string;
|
|
12
25
|
caption?: string;
|
|
13
26
|
message_thread_id?: number;
|
|
@@ -26,12 +39,7 @@ export interface TgMsg {
|
|
|
26
39
|
export interface TgReaction {
|
|
27
40
|
chat: { id: number; type: string };
|
|
28
41
|
message_id: number;
|
|
29
|
-
user?:
|
|
30
|
-
id: number;
|
|
31
|
-
username?: string;
|
|
32
|
-
first_name?: string;
|
|
33
|
-
is_bot?: boolean;
|
|
34
|
-
};
|
|
42
|
+
user?: TgUser;
|
|
35
43
|
date: number;
|
|
36
44
|
old_reaction: { type: string; emoji?: string }[];
|
|
37
45
|
new_reaction: { type: string; emoji?: string }[];
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
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 { messagingAliases } 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 const handleCall = makeStation({
|
|
122
|
+
handlers: { accounts: listAccounts, send, react, callback, listMembers },
|
|
123
|
+
normalize: messagingAliases({ action: 'removed' }),
|
|
124
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
2
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
3
|
+
import { GATEWAY_API, MESSAGE_ID_RE } from './ids.js';
|
|
4
|
+
|
|
5
|
+
const TIMEOUT_MS = 15_000;
|
|
6
|
+
const PUBLIC_KEY_RE = /^[0-9a-f]{64}$/;
|
|
7
|
+
|
|
8
|
+
export interface GatewayCreds {
|
|
9
|
+
gatewayId: string;
|
|
10
|
+
secret: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function call(
|
|
14
|
+
path: string,
|
|
15
|
+
init: RequestInit,
|
|
16
|
+
label: string,
|
|
17
|
+
): Promise<Response> {
|
|
18
|
+
try {
|
|
19
|
+
return await fetch(`${GATEWAY_API}${path}`, {
|
|
20
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
21
|
+
...init,
|
|
22
|
+
});
|
|
23
|
+
} catch (err) {
|
|
24
|
+
throw new TrainError(
|
|
25
|
+
'threema_unreachable',
|
|
26
|
+
`${label}: could not reach the Threema Gateway (${errMsg(err)})`,
|
|
27
|
+
{ retryable: true },
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const authQuery = (c: GatewayCreds): string =>
|
|
33
|
+
new URLSearchParams({ from: c.gatewayId, secret: c.secret }).toString();
|
|
34
|
+
|
|
35
|
+
function refusal(status: number, what: string): TrainError {
|
|
36
|
+
if (status === 401)
|
|
37
|
+
return new TrainError(
|
|
38
|
+
'threema_unauthorized',
|
|
39
|
+
'the Threema Gateway rejected this account credentials; check the Gateway ID and API secret',
|
|
40
|
+
{ retryable: false },
|
|
41
|
+
);
|
|
42
|
+
if (status === 402)
|
|
43
|
+
return new TrainError(
|
|
44
|
+
'threema_no_credits',
|
|
45
|
+
'this Threema Gateway ID has no credits left; top it up at gateway.threema.ch',
|
|
46
|
+
{ retryable: false },
|
|
47
|
+
);
|
|
48
|
+
if (status === 404)
|
|
49
|
+
return new TrainError('threema_unknown_id', `Threema does not know the ID ${what}`, {
|
|
50
|
+
retryable: false,
|
|
51
|
+
});
|
|
52
|
+
if (status === 413)
|
|
53
|
+
return new TrainError(
|
|
54
|
+
'threema_message_too_long',
|
|
55
|
+
'Threema refused the message as too long',
|
|
56
|
+
{ retryable: false },
|
|
57
|
+
);
|
|
58
|
+
if (status === 400)
|
|
59
|
+
return new TrainError('threema_bad_recipient', `Threema refused the recipient ${what}`, {
|
|
60
|
+
retryable: false,
|
|
61
|
+
});
|
|
62
|
+
return new TrainError(
|
|
63
|
+
'threema_gateway_error',
|
|
64
|
+
`the Threema Gateway answered ${status}`,
|
|
65
|
+
{ retryable: status >= 500 },
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function fetchPublicKey(
|
|
70
|
+
c: GatewayCreds,
|
|
71
|
+
threemaId: string,
|
|
72
|
+
): Promise<string> {
|
|
73
|
+
const res = await call(
|
|
74
|
+
`/pubkeys/${encodeURIComponent(threemaId)}?${authQuery(c)}`,
|
|
75
|
+
{},
|
|
76
|
+
'pubkeys',
|
|
77
|
+
);
|
|
78
|
+
if (!res.ok) throw refusal(res.status, threemaId);
|
|
79
|
+
const hex = (await res.text()).trim().toLowerCase();
|
|
80
|
+
if (!PUBLIC_KEY_RE.test(hex))
|
|
81
|
+
throw new TrainError(
|
|
82
|
+
'threema_gateway_error',
|
|
83
|
+
`pubkeys: the Threema Gateway gave an unexpected answer for ${threemaId}`,
|
|
84
|
+
);
|
|
85
|
+
return hex;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function fetchCredits(c: GatewayCreds): Promise<number> {
|
|
89
|
+
const res = await call(`/credits?${authQuery(c)}`, {}, 'credits');
|
|
90
|
+
if (!res.ok) throw refusal(res.status, c.gatewayId);
|
|
91
|
+
const credits = Number((await res.text()).trim());
|
|
92
|
+
if (!Number.isInteger(credits))
|
|
93
|
+
throw new TrainError(
|
|
94
|
+
'threema_gateway_error',
|
|
95
|
+
'credits: the Threema Gateway gave an unexpected answer',
|
|
96
|
+
);
|
|
97
|
+
return credits;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function sendE2E(
|
|
101
|
+
c: GatewayCreds,
|
|
102
|
+
to: string,
|
|
103
|
+
nonceHex: string,
|
|
104
|
+
boxHex: string,
|
|
105
|
+
group = false,
|
|
106
|
+
): Promise<string> {
|
|
107
|
+
const body = new URLSearchParams({
|
|
108
|
+
from: c.gatewayId,
|
|
109
|
+
to,
|
|
110
|
+
nonce: nonceHex,
|
|
111
|
+
box: boxHex,
|
|
112
|
+
secret: c.secret,
|
|
113
|
+
...(group ? { group: '1' } : {}),
|
|
114
|
+
});
|
|
115
|
+
const res = await call(
|
|
116
|
+
'/send_e2e',
|
|
117
|
+
{
|
|
118
|
+
method: 'POST',
|
|
119
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
120
|
+
body: body.toString(),
|
|
121
|
+
},
|
|
122
|
+
'send_e2e',
|
|
123
|
+
);
|
|
124
|
+
if (!res.ok) throw refusal(res.status, to);
|
|
125
|
+
const messageId = (await res.text()).trim().toLowerCase();
|
|
126
|
+
if (!MESSAGE_ID_RE.test(messageId))
|
|
127
|
+
throw new TrainError(
|
|
128
|
+
'threema_gateway_error',
|
|
129
|
+
'send_e2e: the Threema Gateway answered without a message id',
|
|
130
|
+
);
|
|
131
|
+
return messageId;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const BLOB_ID_RE = /^[0-9a-f]{32}$/;
|
|
135
|
+
const BLOB_TIMEOUT_MS = 120_000;
|
|
136
|
+
|
|
137
|
+
export async function uploadBlob(c: GatewayCreds, data: Uint8Array): Promise<string> {
|
|
138
|
+
const form = new FormData();
|
|
139
|
+
form.append('blob', new Blob([Buffer.from(data)]), 'blob');
|
|
140
|
+
const res = await call(`/upload_blob?${authQuery(c)}`, { method: 'POST', body: form, signal: AbortSignal.timeout(BLOB_TIMEOUT_MS) }, 'upload_blob');
|
|
141
|
+
if (!res.ok) throw refusal(res.status, 'blob');
|
|
142
|
+
const blobId = (await res.text()).trim().toLowerCase();
|
|
143
|
+
if (!BLOB_ID_RE.test(blobId)) throw new TrainError('threema_gateway_error', 'upload_blob: the Threema Gateway answered without a blob id');
|
|
144
|
+
return blobId;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export async function downloadBlob(c: GatewayCreds, blobId: string): Promise<Uint8Array> {
|
|
148
|
+
if (!BLOB_ID_RE.test(blobId)) throw new TrainError('threema_bad_blob', `'${blobId}' is not a blob id`, { retryable: false });
|
|
149
|
+
const res = await call(`/blobs/${blobId}?${authQuery(c)}`, { signal: AbortSignal.timeout(BLOB_TIMEOUT_MS) }, 'blob download');
|
|
150
|
+
if (!res.ok) throw refusal(res.status, `blob ${blobId}`);
|
|
151
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
152
|
+
}
|