@stage-labs/metro 0.1.0-beta.13 → 0.1.0-beta.130
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +14 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +176 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +93 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +43 -0
- package/dist/plugin.js +120 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +38 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +150 -0
- package/dist/service.js +242 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -27
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +18 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +55 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +122 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/marketplace/plugin/orchestrator.md +16 -0
- package/runtime/marketplace/plugin/skills/refresh/SKILL.md +15 -0
- package/runtime/node_modules/@metro-labs/core/package.json +43 -0
- package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +87 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +108 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +365 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +300 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +371 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +95 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +309 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +169 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +140 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +253 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +239 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +154 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +310 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +175 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/json.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +171 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +296 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +286 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +332 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/watch.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +259 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +187 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +269 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +151 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +372 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/identity.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +261 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/http/package.json +33 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +219 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +130 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +132 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +123 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +29 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +10 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +190 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +347 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +18 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +39 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -252
- package/dist/broker.js +0 -202
- package/dist/cache.js +0 -69
- package/dist/cli/actions.js +0 -206
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -188
- package/dist/cli/skill.js +0 -62
- package/dist/cli/tail.js +0 -164
- package/dist/cli/util.js +0 -72
- package/dist/cli/webhook.js +0 -81
- package/dist/codex-rc.js +0 -245
- package/dist/dispatcher.js +0 -128
- package/dist/history.js +0 -121
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/monitor.js +0 -194
- package/dist/paths.js +0 -68
- package/dist/registry.js +0 -48
- package/dist/stations/claude.js +0 -45
- package/dist/stations/codex.js +0 -68
- package/dist/stations/discord.js +0 -216
- package/dist/stations/index.js +0 -129
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -234
- package/dist/stations/webhook.js +0 -103
- package/dist/tunnel.js +0 -64
- package/dist/webhooks.js +0 -41
- package/docs/broker.md +0 -214
- package/docs/monitor.md +0 -163
- package/docs/uri-scheme.md +0 -124
- package/docs/users.md +0 -226
- package/skills/metro/SKILL.md +0 -256
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BufferJSON,
|
|
3
|
+
initAuthCreds,
|
|
4
|
+
proto,
|
|
5
|
+
type AuthenticationCreds,
|
|
6
|
+
type AuthenticationState,
|
|
7
|
+
type SignalDataSet,
|
|
8
|
+
type SignalDataTypeMap,
|
|
9
|
+
type SignalKeyStore,
|
|
10
|
+
} from 'baileys';
|
|
11
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
12
|
+
import {
|
|
13
|
+
isPersistedKeyType,
|
|
14
|
+
tokenStoreFor,
|
|
15
|
+
type KeyTable,
|
|
16
|
+
} from './token-store.js';
|
|
17
|
+
|
|
18
|
+
interface AuthBlob {
|
|
19
|
+
creds: AuthenticationCreds;
|
|
20
|
+
keys: KeyTable;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function encode(value: unknown): unknown {
|
|
24
|
+
return JSON.parse(JSON.stringify(value, BufferJSON.replacer));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function decode(value: unknown): unknown {
|
|
28
|
+
return JSON.parse(JSON.stringify(value), BufferJSON.reviver);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function loadBlob(raw: unknown): AuthBlob {
|
|
32
|
+
const obj = raw as { creds?: unknown; keys?: KeyTable } | null;
|
|
33
|
+
const creds = obj?.creds;
|
|
34
|
+
if (creds === undefined || creds === null)
|
|
35
|
+
throw new TrainError(
|
|
36
|
+
'whatsapp_auth',
|
|
37
|
+
'stored credentials blob has no creds',
|
|
38
|
+
);
|
|
39
|
+
return {
|
|
40
|
+
creds: decode(creds) as AuthenticationCreds,
|
|
41
|
+
keys: obj?.keys ?? {},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function makeKeyStore(
|
|
46
|
+
table: KeyTable,
|
|
47
|
+
onPersistedWrite?: (table: KeyTable) => void,
|
|
48
|
+
): SignalKeyStore {
|
|
49
|
+
return {
|
|
50
|
+
get: <T extends keyof SignalDataTypeMap>(type: T, ids: string[]) => {
|
|
51
|
+
const bucket = table[type] ?? {};
|
|
52
|
+
const data: Record<string, SignalDataTypeMap[T]> = {};
|
|
53
|
+
for (const id of ids) {
|
|
54
|
+
const raw = bucket[id];
|
|
55
|
+
if (raw === undefined || raw === null) continue;
|
|
56
|
+
const revived = decode(raw);
|
|
57
|
+
data[id] = (
|
|
58
|
+
type === 'app-state-sync-key'
|
|
59
|
+
? proto.Message.AppStateSyncKeyData.fromObject(
|
|
60
|
+
revived as Record<string, unknown>,
|
|
61
|
+
)
|
|
62
|
+
: revived
|
|
63
|
+
) as SignalDataTypeMap[T];
|
|
64
|
+
}
|
|
65
|
+
return data;
|
|
66
|
+
},
|
|
67
|
+
set: (data: SignalDataSet) => {
|
|
68
|
+
let persisted = false;
|
|
69
|
+
for (const category of Object.keys(data) as (keyof SignalDataSet)[]) {
|
|
70
|
+
const items = data[category];
|
|
71
|
+
if (!items) continue;
|
|
72
|
+
if (isPersistedKeyType(category)) persisted = true;
|
|
73
|
+
const bucket = (table[category] ??= {});
|
|
74
|
+
for (const id of Object.keys(items)) {
|
|
75
|
+
const value = items[id];
|
|
76
|
+
bucket[id] = value ? encode(value) : undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (persisted) onPersistedWrite?.(table);
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function inMemoryAuthState(
|
|
85
|
+
raw?: unknown,
|
|
86
|
+
opts?: { seed?: KeyTable; onPersistedWrite?: (table: KeyTable) => void },
|
|
87
|
+
): {
|
|
88
|
+
state: AuthenticationState;
|
|
89
|
+
serialize: () => unknown;
|
|
90
|
+
} {
|
|
91
|
+
const blob: AuthBlob =
|
|
92
|
+
raw === undefined || raw === null
|
|
93
|
+
? { creds: initAuthCreds(), keys: {} }
|
|
94
|
+
: loadBlob(raw);
|
|
95
|
+
for (const [type, bucket] of Object.entries(opts?.seed ?? {}))
|
|
96
|
+
blob.keys[type] = { ...blob.keys[type], ...bucket };
|
|
97
|
+
const state: AuthenticationState = {
|
|
98
|
+
creds: blob.creds,
|
|
99
|
+
keys: makeKeyStore(blob.keys, opts?.onPersistedWrite),
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
state,
|
|
103
|
+
serialize: () => ({ creds: encode(blob.creds), keys: blob.keys }),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function useAccountAuthState(
|
|
108
|
+
credentials: unknown,
|
|
109
|
+
accountId: string,
|
|
110
|
+
): {
|
|
111
|
+
state: AuthenticationState;
|
|
112
|
+
saveCreds: () => Promise<void>;
|
|
113
|
+
} {
|
|
114
|
+
if (credentials === undefined || credentials === null)
|
|
115
|
+
throw new TrainError(
|
|
116
|
+
'whatsapp_auth',
|
|
117
|
+
`no WhatsApp credentials in accounts for '${accountId}' — run scripts/login.ts to pair`,
|
|
118
|
+
);
|
|
119
|
+
const store = tokenStoreFor(accountId, (message) => {
|
|
120
|
+
process.stderr.write(`whatsapp[${accountId}] token store: ${message}\n`);
|
|
121
|
+
});
|
|
122
|
+
const { state } = inMemoryAuthState(credentials, {
|
|
123
|
+
seed: store.load(),
|
|
124
|
+
onPersistedWrite: (table) => {
|
|
125
|
+
store.scheduleSave(table);
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
return { state, saveCreds: () => Promise.resolve() };
|
|
129
|
+
}
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import makeWASocket, {
|
|
2
|
+
jidNormalizedUser,
|
|
3
|
+
Browsers,
|
|
4
|
+
DisconnectReason,
|
|
5
|
+
fetchLatestWaWebVersion,
|
|
6
|
+
type proto,
|
|
7
|
+
type WAMessage,
|
|
8
|
+
type WAMessageKey,
|
|
9
|
+
type WASocket,
|
|
10
|
+
} from 'baileys';
|
|
11
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
12
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
13
|
+
import type { WhatsAppAccount } from './types.js';
|
|
14
|
+
import type { InboundMessage, ReactionInput } from './format.js';
|
|
15
|
+
import type { SenderFound } from './resolve.js';
|
|
16
|
+
import { toInbound, toReaction, type ReactionEvent, type SelfRef } from './parse.js';
|
|
17
|
+
import { baileysLogger } from './logger.js';
|
|
18
|
+
import { useAccountAuthState } from './auth-state.js';
|
|
19
|
+
import { knownKey, makeKeyCache, targetKey, type KeyCache } from './keys.js';
|
|
20
|
+
import { makeOutbox, type Outbox } from './outbox.js';
|
|
21
|
+
import { deliveryNotes, describeNote } from './delivery.js';
|
|
22
|
+
import {
|
|
23
|
+
ACK_WAIT_MS,
|
|
24
|
+
bindAcks,
|
|
25
|
+
makeAckWatch,
|
|
26
|
+
rejection,
|
|
27
|
+
type AckWatch,
|
|
28
|
+
} from './ack.js';
|
|
29
|
+
|
|
30
|
+
export interface InboundHandlers {
|
|
31
|
+
onMessage(m: InboundMessage, raw: WAMessage): void;
|
|
32
|
+
onReaction(r: ReactionInput): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface WAMedia {
|
|
36
|
+
kind: string;
|
|
37
|
+
path: string;
|
|
38
|
+
mime: string;
|
|
39
|
+
name: string;
|
|
40
|
+
caption?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface WAClient {
|
|
44
|
+
account: WhatsAppAccount;
|
|
45
|
+
self(): string | null;
|
|
46
|
+
start(handlers: InboundHandlers): Promise<void>;
|
|
47
|
+
sendText(jid: string, text: string, quotedId?: string): Promise<string>;
|
|
48
|
+
sendMedia(jid: string, media: WAMedia, quotedId?: string): Promise<string>;
|
|
49
|
+
sendReaction(jid: string, messageId: string, emoji: string): Promise<void>;
|
|
50
|
+
editMessage(jid: string, messageId: string, text: string): Promise<void>;
|
|
51
|
+
deleteMessage(jid: string, messageId: string): Promise<void>;
|
|
52
|
+
reuploadMedia(m: WAMessage): Promise<WAMessage>;
|
|
53
|
+
lookupSender(number: string): Promise<SenderFound>;
|
|
54
|
+
disconnect(): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface State {
|
|
58
|
+
account: WhatsAppAccount;
|
|
59
|
+
handlers?: InboundHandlers;
|
|
60
|
+
sock?: WASocket;
|
|
61
|
+
closed: boolean;
|
|
62
|
+
openResolve?: () => void;
|
|
63
|
+
openPromise: Promise<void>;
|
|
64
|
+
keys: KeyCache;
|
|
65
|
+
outbox: Outbox;
|
|
66
|
+
acks: AckWatch;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type SendOpts = Parameters<WASocket['sendMessage']>[2];
|
|
70
|
+
type SendContent = Parameters<WASocket['sendMessage']>[1];
|
|
71
|
+
|
|
72
|
+
function resetGate(st: State): void {
|
|
73
|
+
st.openPromise = new Promise<void>((resolve) => {
|
|
74
|
+
st.openResolve = resolve;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function selfRef(st: State, sock: WASocket): SelfRef {
|
|
79
|
+
const jids = new Set<string>();
|
|
80
|
+
const me = sock.user;
|
|
81
|
+
if (me?.id) jids.add(jidNormalizedUser(me.id));
|
|
82
|
+
if (me?.lid) jids.add(jidNormalizedUser(me.lid));
|
|
83
|
+
return { jids, sentByMe: (jid, id) => st.keys.lookup(jid, id)?.fromMe === true };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function bindInbound(st: State, sock: WASocket): void {
|
|
87
|
+
sock.ev.on('messages.upsert', ({ messages, type }) => {
|
|
88
|
+
for (const m of messages) st.keys.remember(m.key);
|
|
89
|
+
if (type !== 'notify' || !st.handlers) return;
|
|
90
|
+
for (const m of messages) {
|
|
91
|
+
if (m.key.fromMe) continue;
|
|
92
|
+
const inbound = toInbound(st.account.id, m, selfRef(st, sock));
|
|
93
|
+
if (inbound) st.handlers.onMessage(inbound, m);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
sock.ev.on('messages.reaction', (events: ReactionEvent[]) => {
|
|
97
|
+
if (!st.handlers) return;
|
|
98
|
+
for (const event of events) {
|
|
99
|
+
const reaction = toReaction(st.account.id, event);
|
|
100
|
+
if (reaction) st.handlers.onReaction(reaction);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function bindDelivery(st: State, sock: WASocket): void {
|
|
106
|
+
sock.ev.on('messages.update', (updates) => {
|
|
107
|
+
for (const note of deliveryNotes(updates))
|
|
108
|
+
process.stderr.write(
|
|
109
|
+
`whatsapp[${st.account.id}] send ${note.messageId} to ${note.jid}: ${describeNote(note)}\n`,
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
bindAcks(sock, st.acks, (ack) => {
|
|
113
|
+
process.stderr.write(
|
|
114
|
+
`whatsapp[${st.account.id}] send ${ack.messageId} to ${ack.jid} REFUSED by WhatsApp: ack error ${ack.error ?? '?'}\n`,
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function servedFromOutbox(
|
|
120
|
+
st: State,
|
|
121
|
+
key: WAMessageKey,
|
|
122
|
+
): proto.IMessage | undefined {
|
|
123
|
+
const message = st.outbox.lookup(key);
|
|
124
|
+
if (key.fromMe === true)
|
|
125
|
+
process.stderr.write(
|
|
126
|
+
message
|
|
127
|
+
? `whatsapp[${st.account.id}] asked to send ${key.id} to ${key.remoteJid} again — served from the outbox\n`
|
|
128
|
+
: `whatsapp[${st.account.id}] asked to send ${key.id} to ${key.remoteJid} again — NOT in the outbox, so that message can never arrive\n`,
|
|
129
|
+
);
|
|
130
|
+
return message;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const TERMINAL: Record<number, string> = {
|
|
134
|
+
[DisconnectReason.loggedOut]: ' — re-pair required',
|
|
135
|
+
[DisconnectReason.connectionReplaced]:
|
|
136
|
+
' — another metro took this device; not reconnecting (two sockets on one ' +
|
|
137
|
+
'identity evict each other and risk a 463 timelock)',
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export function terminalReason(code: number | undefined): string | undefined {
|
|
141
|
+
return code === undefined ? undefined : TERMINAL[code];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function onClose(st: State, code: number | undefined): void {
|
|
145
|
+
const terminal = terminalReason(code);
|
|
146
|
+
if (st.closed || terminal !== undefined) {
|
|
147
|
+
process.stderr.write(
|
|
148
|
+
`whatsapp[${st.account.id}] disconnected (code=${code ?? '?'})${terminal ?? ''}\n`,
|
|
149
|
+
);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
resetGate(st);
|
|
153
|
+
process.stderr.write(`whatsapp[${st.account.id}] reconnecting\n`);
|
|
154
|
+
void connect(st).catch((e: unknown) => {
|
|
155
|
+
process.stderr.write(
|
|
156
|
+
`whatsapp[${st.account.id}] reconnect failed: ${errMsg(e)}\n`,
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function bindConnection(st: State, sock: WASocket): void {
|
|
162
|
+
sock.ev.on('connection.update', (update) => {
|
|
163
|
+
const { connection, lastDisconnect } = update;
|
|
164
|
+
if (connection === 'open') {
|
|
165
|
+
process.stderr.write(`whatsapp[${st.account.id}] connected\n`);
|
|
166
|
+
st.openResolve?.();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (connection !== 'close') return;
|
|
170
|
+
const code = (
|
|
171
|
+
lastDisconnect?.error as { output?: { statusCode?: number } } | undefined
|
|
172
|
+
)?.output?.statusCode;
|
|
173
|
+
onClose(st, code);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function connect(st: State): Promise<void> {
|
|
178
|
+
const { state, saveCreds } = useAccountAuthState(
|
|
179
|
+
st.account.credentials,
|
|
180
|
+
st.account.id,
|
|
181
|
+
);
|
|
182
|
+
const { version, error } = await fetchLatestWaWebVersion({});
|
|
183
|
+
if (error) {
|
|
184
|
+
throw new TrainError(
|
|
185
|
+
'whatsapp_connect',
|
|
186
|
+
`failed to fetch WhatsApp web version: ${errMsg(error)}`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
const sock = makeWASocket({
|
|
190
|
+
version,
|
|
191
|
+
auth: state,
|
|
192
|
+
browser: Browsers.macOS('Safari'),
|
|
193
|
+
markOnlineOnConnect: false,
|
|
194
|
+
syncFullHistory: false,
|
|
195
|
+
logger: baileysLogger(st.account.id),
|
|
196
|
+
getMessage: (key) => Promise.resolve(servedFromOutbox(st, key)),
|
|
197
|
+
});
|
|
198
|
+
st.sock = sock;
|
|
199
|
+
sock.ev.on('creds.update', () => void saveCreds());
|
|
200
|
+
bindConnection(st, sock);
|
|
201
|
+
bindInbound(st, sock);
|
|
202
|
+
bindDelivery(st, sock);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function lidFor(st: State, sock: WASocket, jid: string): Promise<string | null> {
|
|
206
|
+
try {
|
|
207
|
+
return await sock.signalRepository.lidMapping.getLIDForPN(jid);
|
|
208
|
+
} catch (e) {
|
|
209
|
+
process.stderr.write(
|
|
210
|
+
`whatsapp[${st.account.id}] no lid on file for ${jid}: ${errMsg(e)}\n`,
|
|
211
|
+
);
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function ready(st: State): Promise<WASocket> {
|
|
217
|
+
await st.openPromise;
|
|
218
|
+
if (!st.sock) throw new TrainError('whatsapp_call', 'socket not connected');
|
|
219
|
+
return st.sock;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function send(
|
|
223
|
+
st: State,
|
|
224
|
+
jid: string,
|
|
225
|
+
content: SendContent,
|
|
226
|
+
opts?: SendOpts,
|
|
227
|
+
): Promise<string> {
|
|
228
|
+
const sock = await ready(st);
|
|
229
|
+
const sent = await sock.sendMessage(jid, content, opts);
|
|
230
|
+
st.keys.remember(sent?.key);
|
|
231
|
+
st.outbox.remember(sent?.key, sent?.message);
|
|
232
|
+
const messageId = sent?.key.id;
|
|
233
|
+
if (!messageId)
|
|
234
|
+
throw new TrainError(
|
|
235
|
+
'whatsapp_call',
|
|
236
|
+
`WhatsApp accepted no message for ${jid}, so there is nothing that can have arrived`,
|
|
237
|
+
);
|
|
238
|
+
const ack = await st.acks.wait(messageId, ACK_WAIT_MS);
|
|
239
|
+
const refused = ack ? rejection(ack) : undefined;
|
|
240
|
+
if (refused) throw refused;
|
|
241
|
+
return messageId;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function quotedOpts(st: State, jid: string, quotedId: string): SendOpts {
|
|
245
|
+
return {
|
|
246
|
+
quoted: {
|
|
247
|
+
key: knownKey(st.keys, jid, quotedId, false),
|
|
248
|
+
message: { conversation: '' },
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function mediaContent(m: WAMedia): SendContent {
|
|
254
|
+
const source = { url: m.path };
|
|
255
|
+
const caption = m.caption ? { caption: m.caption } : {};
|
|
256
|
+
if (m.kind === 'image') return { image: source, ...caption };
|
|
257
|
+
if (m.kind === 'video')
|
|
258
|
+
return { video: source, mimetype: m.mime, ...caption };
|
|
259
|
+
if (m.kind === 'audio')
|
|
260
|
+
return { audio: source, mimetype: m.mime, ptt: false };
|
|
261
|
+
return {
|
|
262
|
+
document: source,
|
|
263
|
+
mimetype: m.mime,
|
|
264
|
+
fileName: m.name,
|
|
265
|
+
...caption,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function createClient(account: WhatsAppAccount): WAClient {
|
|
270
|
+
const st: State = {
|
|
271
|
+
account,
|
|
272
|
+
closed: false,
|
|
273
|
+
openPromise: Promise.resolve(),
|
|
274
|
+
keys: makeKeyCache(),
|
|
275
|
+
outbox: makeOutbox(),
|
|
276
|
+
acks: makeAckWatch(),
|
|
277
|
+
};
|
|
278
|
+
resetGate(st);
|
|
279
|
+
return {
|
|
280
|
+
account,
|
|
281
|
+
self() {
|
|
282
|
+
const jid = st.sock?.user?.id;
|
|
283
|
+
if (jid === undefined) return null;
|
|
284
|
+
return jid.split(':')[0]?.split('@')[0] ?? null;
|
|
285
|
+
},
|
|
286
|
+
async start(handlers) {
|
|
287
|
+
st.handlers = handlers;
|
|
288
|
+
try {
|
|
289
|
+
await connect(st);
|
|
290
|
+
} catch (e) {
|
|
291
|
+
process.stderr.write(
|
|
292
|
+
`whatsapp[${account.id}] connect failed: ${errMsg(e)}\n`,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
sendText(jid, text, quotedId) {
|
|
297
|
+
return send(
|
|
298
|
+
st,
|
|
299
|
+
jid,
|
|
300
|
+
{ text },
|
|
301
|
+
quotedId ? quotedOpts(st, jid, quotedId) : undefined,
|
|
302
|
+
);
|
|
303
|
+
},
|
|
304
|
+
sendMedia(jid, media, quotedId) {
|
|
305
|
+
return send(
|
|
306
|
+
st,
|
|
307
|
+
jid,
|
|
308
|
+
mediaContent(media),
|
|
309
|
+
quotedId ? quotedOpts(st, jid, quotedId) : undefined,
|
|
310
|
+
);
|
|
311
|
+
},
|
|
312
|
+
async sendReaction(jid, messageId, emoji) {
|
|
313
|
+
const target = targetKey(st.keys, jid, messageId, 'react to');
|
|
314
|
+
await send(st, jid, { react: { text: emoji, key: target } });
|
|
315
|
+
},
|
|
316
|
+
async editMessage(jid, messageId, text) {
|
|
317
|
+
await send(st, jid, {
|
|
318
|
+
text,
|
|
319
|
+
edit: knownKey(st.keys, jid, messageId, true),
|
|
320
|
+
});
|
|
321
|
+
},
|
|
322
|
+
async deleteMessage(jid, messageId) {
|
|
323
|
+
await send(st, jid, {
|
|
324
|
+
delete: knownKey(st.keys, jid, messageId, true),
|
|
325
|
+
});
|
|
326
|
+
},
|
|
327
|
+
async reuploadMedia(m) {
|
|
328
|
+
const sock = await ready(st);
|
|
329
|
+
return sock.updateMediaMessage(m);
|
|
330
|
+
},
|
|
331
|
+
async lookupSender(number) {
|
|
332
|
+
const sock = await ready(st);
|
|
333
|
+
const found = (await sock.onWhatsApp(number))?.[0];
|
|
334
|
+
if (found?.exists !== true) return { exists: false, jid: null, lid: null };
|
|
335
|
+
const jid = jidNormalizedUser(found.jid);
|
|
336
|
+
return { exists: true, jid, lid: await lidFor(st, sock, jid) };
|
|
337
|
+
},
|
|
338
|
+
async disconnect() {
|
|
339
|
+
st.closed = true;
|
|
340
|
+
try {
|
|
341
|
+
await st.sock?.end(undefined);
|
|
342
|
+
} catch {
|
|
343
|
+
st.sock = undefined;
|
|
344
|
+
}
|
|
345
|
+
},
|
|
346
|
+
};
|
|
347
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { proto, type WAMessageUpdate } from 'baileys';
|
|
2
|
+
|
|
3
|
+
const STATUS_NAMES: Record<number, string> = {
|
|
4
|
+
[proto.WebMessageInfo.Status.ERROR]: 'error',
|
|
5
|
+
[proto.WebMessageInfo.Status.PENDING]: 'pending',
|
|
6
|
+
[proto.WebMessageInfo.Status.SERVER_ACK]: 'server-ack',
|
|
7
|
+
[proto.WebMessageInfo.Status.DELIVERY_ACK]: 'delivered',
|
|
8
|
+
[proto.WebMessageInfo.Status.READ]: 'read',
|
|
9
|
+
[proto.WebMessageInfo.Status.PLAYED]: 'played',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export interface DeliveryNote {
|
|
13
|
+
messageId: string;
|
|
14
|
+
jid: string;
|
|
15
|
+
status: string;
|
|
16
|
+
reachedRecipient: boolean;
|
|
17
|
+
reason?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function reasonOf(params: unknown): string | undefined {
|
|
21
|
+
if (!Array.isArray(params)) return undefined;
|
|
22
|
+
const parts: string[] = [];
|
|
23
|
+
for (const part of params)
|
|
24
|
+
if (typeof part === 'string' && part.trim() !== '') parts.push(part.trim());
|
|
25
|
+
return parts.length ? parts.join(': ') : undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function deliveryNotes(updates: WAMessageUpdate[]): DeliveryNote[] {
|
|
29
|
+
const notes: DeliveryNote[] = [];
|
|
30
|
+
for (const { key, update } of updates) {
|
|
31
|
+
const status = update.status;
|
|
32
|
+
if (typeof status !== 'number' || key.fromMe !== true) continue;
|
|
33
|
+
const jid = key.remoteJid;
|
|
34
|
+
const messageId = key.id;
|
|
35
|
+
if (!jid || !messageId) continue;
|
|
36
|
+
const reason = reasonOf(update.messageStubParameters);
|
|
37
|
+
notes.push({
|
|
38
|
+
messageId,
|
|
39
|
+
jid,
|
|
40
|
+
status: STATUS_NAMES[status] ?? String(status),
|
|
41
|
+
reachedRecipient: status >= proto.WebMessageInfo.Status.DELIVERY_ACK,
|
|
42
|
+
...(reason === undefined ? {} : { reason }),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return notes;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const describeNote = (note: DeliveryNote): string =>
|
|
49
|
+
note.reason === undefined ? note.status : `${note.status} (${note.reason})`;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { SavedAttachment } from '@metro-labs/core/stations/attachments';
|
|
2
|
+
import { lineOf } from './accounts.js';
|
|
3
|
+
import { mintId, SELF_URI } from './wire.js';
|
|
4
|
+
import type { WAMediaRef } from './media.js';
|
|
5
|
+
|
|
6
|
+
export interface InboundMessage {
|
|
7
|
+
accountId: string;
|
|
8
|
+
chatJid: string;
|
|
9
|
+
senderJid: string;
|
|
10
|
+
messageId: string;
|
|
11
|
+
text: string;
|
|
12
|
+
date: Date;
|
|
13
|
+
isPrivate: boolean;
|
|
14
|
+
pushName?: string;
|
|
15
|
+
media?: WAMediaRef;
|
|
16
|
+
replyTo?: string;
|
|
17
|
+
mentionsSelf?: boolean;
|
|
18
|
+
replyToSelf?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ReactionInput {
|
|
22
|
+
accountId: string;
|
|
23
|
+
chatJid: string;
|
|
24
|
+
senderJid: string;
|
|
25
|
+
messageId: string;
|
|
26
|
+
emoji: string;
|
|
27
|
+
date: Date;
|
|
28
|
+
isPrivate: boolean;
|
|
29
|
+
pushName?: string;
|
|
30
|
+
removed?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function attachmentView(ref: WAMediaRef): Record<string, unknown> {
|
|
34
|
+
return {
|
|
35
|
+
kind: ref.kind,
|
|
36
|
+
...(ref.name ? { name: ref.name } : {}),
|
|
37
|
+
...(ref.mime ? { mime: ref.mime } : {}),
|
|
38
|
+
...(ref.bytes === undefined ? {} : { size: ref.bytes }),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function envelope(m: InboundMessage): Record<string, unknown> {
|
|
43
|
+
return {
|
|
44
|
+
kind: 'inbound',
|
|
45
|
+
id: mintId(),
|
|
46
|
+
ts: m.date.toISOString(),
|
|
47
|
+
station: 'whatsapp',
|
|
48
|
+
line: lineOf(m.accountId, m.chatJid),
|
|
49
|
+
from: `metro://whatsapp/${m.accountId}/user/${m.senderJid}`,
|
|
50
|
+
...(m.pushName ? { from_name: m.pushName, from_display_name: m.pushName } : {}),
|
|
51
|
+
message_id: m.messageId,
|
|
52
|
+
text: m.text,
|
|
53
|
+
is_private: m.isPrivate,
|
|
54
|
+
...(m.replyTo === undefined ? {} : { reply_to: m.replyTo, event: { type: 'reply', replyTo: m.replyTo } }),
|
|
55
|
+
...(m.mentionsSelf === true ? { mentions_self: true } : {}),
|
|
56
|
+
...(m.replyToSelf === true ? { reply_to_self: true } : {}),
|
|
57
|
+
payload: {
|
|
58
|
+
account: m.accountId,
|
|
59
|
+
message_id: m.messageId,
|
|
60
|
+
...(m.media ? { attachments: [attachmentView(m.media)] } : {}),
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function mediaEvent(
|
|
66
|
+
m: InboundMessage,
|
|
67
|
+
text: string,
|
|
68
|
+
payload: Record<string, unknown>,
|
|
69
|
+
): Record<string, unknown> {
|
|
70
|
+
return {
|
|
71
|
+
kind: 'inbound',
|
|
72
|
+
id: mintId(),
|
|
73
|
+
ts: new Date().toISOString(),
|
|
74
|
+
station: 'whatsapp',
|
|
75
|
+
line: lineOf(m.accountId, m.chatJid),
|
|
76
|
+
from: SELF_URI,
|
|
77
|
+
text,
|
|
78
|
+
payload: { account: m.accountId, ...payload },
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function attachmentSavedEnvelope(
|
|
83
|
+
m: InboundMessage,
|
|
84
|
+
sourceId: string,
|
|
85
|
+
ref: WAMediaRef,
|
|
86
|
+
saved: SavedAttachment,
|
|
87
|
+
index: number,
|
|
88
|
+
): Record<string, unknown> {
|
|
89
|
+
return mediaEvent(m, `📎 saved: ${saved.path}`, {
|
|
90
|
+
contentType: 'attachmentSaved',
|
|
91
|
+
attachmentFor: sourceId,
|
|
92
|
+
index,
|
|
93
|
+
kind: ref.kind,
|
|
94
|
+
attachmentPath: saved.path,
|
|
95
|
+
localPath: saved.path,
|
|
96
|
+
mime: saved.mime,
|
|
97
|
+
name: saved.name,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function attachmentFailedEnvelope(
|
|
102
|
+
m: InboundMessage,
|
|
103
|
+
sourceId: string,
|
|
104
|
+
ref: WAMediaRef,
|
|
105
|
+
index: number,
|
|
106
|
+
reason: string,
|
|
107
|
+
): Record<string, unknown> {
|
|
108
|
+
return mediaEvent(m, `📎 not fetched: ${reason}`, {
|
|
109
|
+
contentType: 'attachmentFailed',
|
|
110
|
+
attachmentFor: sourceId,
|
|
111
|
+
index,
|
|
112
|
+
kind: ref.kind,
|
|
113
|
+
name: ref.name,
|
|
114
|
+
mime: ref.mime,
|
|
115
|
+
reason,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function reactionEnvelope(r: ReactionInput): Record<string, unknown> {
|
|
120
|
+
return {
|
|
121
|
+
kind: 'react',
|
|
122
|
+
id: mintId(),
|
|
123
|
+
ts: r.date.toISOString(),
|
|
124
|
+
station: 'whatsapp',
|
|
125
|
+
line: lineOf(r.accountId, r.chatJid),
|
|
126
|
+
from: `metro://whatsapp/${r.accountId}/user/${r.senderJid}`,
|
|
127
|
+
...(r.pushName ? { from_name: r.pushName } : {}),
|
|
128
|
+
message_id: r.messageId,
|
|
129
|
+
emoji: r.emoji,
|
|
130
|
+
event: { type: 'react', emoji: r.emoji, targetId: r.messageId },
|
|
131
|
+
is_private: r.isPrivate,
|
|
132
|
+
payload: {
|
|
133
|
+
account: r.accountId,
|
|
134
|
+
message_id: r.messageId,
|
|
135
|
+
removed: r.removed === true,
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|