@stage-labs/metro 0.1.0-beta.13 → 0.1.0-beta.131
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +14 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +176 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +93 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +43 -0
- package/dist/plugin.js +120 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +38 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +150 -0
- package/dist/service.js +242 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -27
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +18 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +55 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +122 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/marketplace/plugin/orchestrator.md +16 -0
- package/runtime/marketplace/plugin/skills/refresh/SKILL.md +15 -0
- package/runtime/node_modules/@metro-labs/core/package.json +43 -0
- package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +87 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +108 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +365 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +300 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +371 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +95 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/seed.ts +33 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +209 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +309 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +169 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +140 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +253 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +239 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +154 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +310 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +175 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/json.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +171 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +296 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +286 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +332 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/watch.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +259 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +187 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +269 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +151 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +372 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/identity.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +261 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/http/package.json +33 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +219 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +130 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +132 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +123 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +29 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +10 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +190 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +347 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +18 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +39 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -252
- package/dist/broker.js +0 -202
- package/dist/cache.js +0 -69
- package/dist/cli/actions.js +0 -206
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -188
- package/dist/cli/skill.js +0 -62
- package/dist/cli/tail.js +0 -164
- package/dist/cli/util.js +0 -72
- package/dist/cli/webhook.js +0 -81
- package/dist/codex-rc.js +0 -245
- package/dist/dispatcher.js +0 -128
- package/dist/history.js +0 -121
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/monitor.js +0 -194
- package/dist/paths.js +0 -68
- package/dist/registry.js +0 -48
- package/dist/stations/claude.js +0 -45
- package/dist/stations/codex.js +0 -68
- package/dist/stations/discord.js +0 -216
- package/dist/stations/index.js +0 -129
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -234
- package/dist/stations/webhook.js +0 -103
- package/dist/tunnel.js +0 -64
- package/dist/webhooks.js +0 -41
- package/docs/broker.md +0 -214
- package/docs/monitor.md +0 -163
- package/docs/uri-scheme.md +0 -124
- package/docs/users.md +0 -226
- package/skills/metro/SKILL.md +0 -256
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { WAMessage } from 'baileys';
|
|
2
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
3
|
+
import { emit } from './wire.js';
|
|
4
|
+
import {
|
|
5
|
+
attachmentFailedEnvelope,
|
|
6
|
+
attachmentSavedEnvelope,
|
|
7
|
+
envelope,
|
|
8
|
+
reactionEnvelope,
|
|
9
|
+
type InboundMessage,
|
|
10
|
+
} from './format.js';
|
|
11
|
+
import { saveWhatsAppMedia } from './attachments.js';
|
|
12
|
+
import type { WAClient } from './client.js';
|
|
13
|
+
|
|
14
|
+
async function saveMedia(
|
|
15
|
+
client: WAClient,
|
|
16
|
+
m: InboundMessage,
|
|
17
|
+
raw: WAMessage,
|
|
18
|
+
sourceId: string,
|
|
19
|
+
): Promise<void> {
|
|
20
|
+
const ref = m.media;
|
|
21
|
+
if (!ref) return;
|
|
22
|
+
try {
|
|
23
|
+
const saved = await saveWhatsAppMedia(
|
|
24
|
+
m.accountId,
|
|
25
|
+
raw,
|
|
26
|
+
ref,
|
|
27
|
+
m.messageId,
|
|
28
|
+
0,
|
|
29
|
+
(msg) => client.reuploadMedia(msg),
|
|
30
|
+
);
|
|
31
|
+
emit(attachmentSavedEnvelope(m, sourceId, ref, saved, 0));
|
|
32
|
+
} catch (err) {
|
|
33
|
+
const reason = errMsg(err);
|
|
34
|
+
process.stderr.write(
|
|
35
|
+
`whatsapp[${m.accountId}] ${ref.kind} download failed for ${m.messageId}: ${reason}\n`,
|
|
36
|
+
);
|
|
37
|
+
emit(attachmentFailedEnvelope(m, sourceId, ref, 0, reason));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function startInbound(client: WAClient): Promise<void> {
|
|
42
|
+
await client.start({
|
|
43
|
+
onMessage: (m, raw) => {
|
|
44
|
+
const env = envelope(m);
|
|
45
|
+
emit(env);
|
|
46
|
+
if (!m.media) return;
|
|
47
|
+
saveMedia(client, m, raw, String(env.id)).catch((err: unknown) => {
|
|
48
|
+
process.stderr.write(
|
|
49
|
+
`whatsapp[${m.accountId}] media event not emitted: ${errMsg(err)}\n`,
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
onReaction: (r) => {
|
|
54
|
+
emit(reactionEnvelope(r));
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
2
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
3
|
+
import { readCalls } from '@metro-labs/core/trains/protocol';
|
|
4
|
+
import { accounts, loadAccounts, accountFor, lineOf, targetOf } from './accounts.js';
|
|
5
|
+
import { createClient, type WAClient } from './client.js';
|
|
6
|
+
import { startInbound } from './inbound.js';
|
|
7
|
+
import { makeHandleCall } from './actions.js';
|
|
8
|
+
|
|
9
|
+
const clients = new Map<string, WAClient>();
|
|
10
|
+
|
|
11
|
+
function clientFor(accountId: string): WAClient {
|
|
12
|
+
let client = clients.get(accountId);
|
|
13
|
+
if (!client) {
|
|
14
|
+
const account = accounts.get(accountId);
|
|
15
|
+
if (!account)
|
|
16
|
+
throw new TrainError('not_implemented', `unknown account '${accountId}'`);
|
|
17
|
+
client = createClient(account);
|
|
18
|
+
clients.set(accountId, client);
|
|
19
|
+
}
|
|
20
|
+
return client;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const handleCall = makeHandleCall(clientFor);
|
|
24
|
+
|
|
25
|
+
readCalls('whatsapp', handleCall);
|
|
26
|
+
|
|
27
|
+
function boot(): void {
|
|
28
|
+
for (const cfg of loadAccounts()) accounts.set(cfg.id, cfg);
|
|
29
|
+
for (const id of accounts.keys())
|
|
30
|
+
startInbound(clientFor(id)).catch((err: unknown) => {
|
|
31
|
+
process.stderr.write(`whatsapp[${id}] inbound failed: ${errMsg(err)}\n`);
|
|
32
|
+
});
|
|
33
|
+
process.stderr.write(
|
|
34
|
+
`whatsapp train ready (inbound+outbound) — ${accounts.size} account(s): ${[...accounts.keys()].join(', ')}\n`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
boot();
|
|
39
|
+
|
|
40
|
+
export { clients, accounts, clientFor, accountFor, lineOf, targetOf };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { TrainError } from '@metro-labs/core/train-error';
|
|
2
|
+
import type { WAMessageKey } from 'baileys';
|
|
3
|
+
import { isGroupJid } from './parse.js';
|
|
4
|
+
|
|
5
|
+
const MAX_TRACKED_KEYS = 4000;
|
|
6
|
+
|
|
7
|
+
export interface KeyCache {
|
|
8
|
+
remember(key: WAMessageKey | null | undefined): void;
|
|
9
|
+
lookup(jid: string, id: string): WAMessageKey | undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const slotOf = (jid: string, id: string): string => `${jid}|${id}`;
|
|
13
|
+
|
|
14
|
+
export function makeKeyCache(max: number = MAX_TRACKED_KEYS): KeyCache {
|
|
15
|
+
const seen = new Map<string, WAMessageKey>();
|
|
16
|
+
return {
|
|
17
|
+
remember(key) {
|
|
18
|
+
const id = key?.id;
|
|
19
|
+
const jid = key?.remoteJid;
|
|
20
|
+
if (!key || !id || !jid) return;
|
|
21
|
+
const slot = slotOf(jid, id);
|
|
22
|
+
seen.delete(slot);
|
|
23
|
+
seen.set(slot, key);
|
|
24
|
+
while (seen.size > max) {
|
|
25
|
+
const oldest = seen.keys().next().value;
|
|
26
|
+
if (oldest === undefined) break;
|
|
27
|
+
seen.delete(oldest);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
lookup(jid, id) {
|
|
31
|
+
return seen.get(slotOf(jid, id));
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function knownKey(
|
|
37
|
+
cache: KeyCache,
|
|
38
|
+
jid: string,
|
|
39
|
+
id: string,
|
|
40
|
+
fromMe: boolean,
|
|
41
|
+
): WAMessageKey {
|
|
42
|
+
return cache.lookup(jid, id) ?? { remoteJid: jid, id, fromMe };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function targetKey(
|
|
46
|
+
cache: KeyCache,
|
|
47
|
+
jid: string,
|
|
48
|
+
id: string,
|
|
49
|
+
verb: string,
|
|
50
|
+
): WAMessageKey {
|
|
51
|
+
const known = cache.lookup(jid, id);
|
|
52
|
+
if (known) return known;
|
|
53
|
+
if (isGroupJid(jid))
|
|
54
|
+
throw new TrainError(
|
|
55
|
+
'whatsapp_unknown_message',
|
|
56
|
+
`cannot ${verb} message '${id}' in group ${jid}: this connection never saw that message, so the group key naming its original sender cannot be built — WhatsApp would accept the send and display nothing`,
|
|
57
|
+
);
|
|
58
|
+
return { remoteJid: jid, id, fromMe: false };
|
|
59
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type makeWASocket from 'baileys';
|
|
2
|
+
|
|
3
|
+
type BaileysLogger = NonNullable<
|
|
4
|
+
NonNullable<Parameters<typeof makeWASocket>[0]>['logger']
|
|
5
|
+
>;
|
|
6
|
+
|
|
7
|
+
const MAX_DETAIL = 400;
|
|
8
|
+
|
|
9
|
+
const LEVELS = ['trace', 'debug', 'info', 'warn', 'error'] as const;
|
|
10
|
+
|
|
11
|
+
type Level = (typeof LEVELS)[number];
|
|
12
|
+
|
|
13
|
+
const DEFAULT_LEVEL: Level = 'warn';
|
|
14
|
+
|
|
15
|
+
const SILENT = 'silent';
|
|
16
|
+
|
|
17
|
+
const oneLine = (s: string): string => s.replace(/\s+/g, ' ').trim();
|
|
18
|
+
|
|
19
|
+
function logLevel(): Level | typeof SILENT {
|
|
20
|
+
const raw = process.env.METRO_WHATSAPP_LOG_LEVEL?.trim().toLowerCase();
|
|
21
|
+
if (raw === SILENT) return SILENT;
|
|
22
|
+
return LEVELS.find((l) => l === raw) ?? DEFAULT_LEVEL;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function detail(obj: unknown): string {
|
|
26
|
+
if (obj === undefined || obj === null) return '';
|
|
27
|
+
if (obj instanceof Error) return ` ${oneLine(obj.message)}`;
|
|
28
|
+
if (typeof obj === 'string') return ` ${oneLine(obj)}`;
|
|
29
|
+
try {
|
|
30
|
+
return ` ${oneLine(JSON.stringify(obj) ?? '').slice(0, MAX_DETAIL)}`;
|
|
31
|
+
} catch {
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function baileysLogger(accountId: string): BaileysLogger {
|
|
37
|
+
const level = logLevel();
|
|
38
|
+
const noop = (): void => undefined;
|
|
39
|
+
const write =
|
|
40
|
+
(at: Level) =>
|
|
41
|
+
(obj: unknown, msg?: string): void => {
|
|
42
|
+
process.stderr.write(
|
|
43
|
+
`whatsapp[${accountId}] baileys ${at}: ${oneLine(msg ?? '')}${detail(obj)}\n`,
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
const method = (at: Level): ((obj: unknown, msg?: string) => void) =>
|
|
47
|
+
level !== SILENT && LEVELS.indexOf(at) >= LEVELS.indexOf(level)
|
|
48
|
+
? write(at)
|
|
49
|
+
: noop;
|
|
50
|
+
const logger: BaileysLogger = {
|
|
51
|
+
level,
|
|
52
|
+
child: () => logger,
|
|
53
|
+
trace: method('trace'),
|
|
54
|
+
debug: method('debug'),
|
|
55
|
+
info: method('info'),
|
|
56
|
+
warn: method('warn'),
|
|
57
|
+
error: method('error'),
|
|
58
|
+
};
|
|
59
|
+
return logger;
|
|
60
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import makeWASocket, {
|
|
2
|
+
Browsers,
|
|
3
|
+
DisconnectReason,
|
|
4
|
+
fetchLatestWaWebVersion,
|
|
5
|
+
type WASocket,
|
|
6
|
+
} from 'baileys';
|
|
7
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
8
|
+
import { inMemoryAuthState } from './auth-state.js';
|
|
9
|
+
|
|
10
|
+
export class WhatsappLoginError extends Error {}
|
|
11
|
+
|
|
12
|
+
export interface WhatsappLoginEvents {
|
|
13
|
+
onQr: (qr: string) => void;
|
|
14
|
+
onPairingCode: (code: string) => void;
|
|
15
|
+
onPaired: (result: {
|
|
16
|
+
config: { phone: string; credentials: unknown };
|
|
17
|
+
identity: { phone: string };
|
|
18
|
+
}) => void;
|
|
19
|
+
onFailed: (message: string) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const PHONE_RE = /^[0-9]{6,15}$/;
|
|
23
|
+
const RESTART_REQUIRED = 515;
|
|
24
|
+
const MAX_REOPENS = 6;
|
|
25
|
+
|
|
26
|
+
export function normalizePhone(raw: unknown): string {
|
|
27
|
+
const phone = typeof raw === 'string' ? raw.replace(/[^0-9]/g, '') : '';
|
|
28
|
+
if (phone !== '' && !PHONE_RE.test(phone))
|
|
29
|
+
throw new WhatsappLoginError(
|
|
30
|
+
'phone must be an international number in digits, for example 447700900123',
|
|
31
|
+
);
|
|
32
|
+
return phone;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function closeReason(err: unknown): number | undefined {
|
|
36
|
+
return (err as { output?: { statusCode?: number } } | undefined)?.output
|
|
37
|
+
?.statusCode;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class WhatsappLogin {
|
|
41
|
+
private auth = inMemoryAuthState();
|
|
42
|
+
private sock: WASocket | null = null;
|
|
43
|
+
private pairingRequested = false;
|
|
44
|
+
private settled = false;
|
|
45
|
+
private reopens = 0;
|
|
46
|
+
|
|
47
|
+
constructor(
|
|
48
|
+
private phone: string,
|
|
49
|
+
private events: WhatsappLoginEvents,
|
|
50
|
+
) {}
|
|
51
|
+
|
|
52
|
+
get mode(): 'pairing-code' | 'qr' {
|
|
53
|
+
return this.phone === '' ? 'qr' : 'pairing-code';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async start(): Promise<void> {
|
|
57
|
+
const { version, error } = await fetchLatestWaWebVersion({});
|
|
58
|
+
if (error)
|
|
59
|
+
throw new WhatsappLoginError(
|
|
60
|
+
'could not reach WhatsApp to start the pairing',
|
|
61
|
+
);
|
|
62
|
+
this.open(version);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private open(version: [number, number, number]): void {
|
|
66
|
+
if (this.settled) return;
|
|
67
|
+
const sock = makeWASocket({
|
|
68
|
+
version,
|
|
69
|
+
auth: this.auth.state,
|
|
70
|
+
browser: Browsers.macOS('Safari'),
|
|
71
|
+
markOnlineOnConnect: false,
|
|
72
|
+
syncFullHistory: false,
|
|
73
|
+
printQRInTerminal: false,
|
|
74
|
+
});
|
|
75
|
+
this.sock = sock;
|
|
76
|
+
sock.ev.on('connection.update', (update) => {
|
|
77
|
+
const { connection, qr, lastDisconnect } = update;
|
|
78
|
+
if (typeof qr === 'string') this.onQr(sock, qr);
|
|
79
|
+
if (connection === 'open') this.finish();
|
|
80
|
+
if (connection === 'close')
|
|
81
|
+
this.onClose(version, closeReason(lastDisconnect?.error));
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private onQr(sock: WASocket, qr: string): void {
|
|
86
|
+
if (this.settled) return;
|
|
87
|
+
if (this.mode === 'qr') {
|
|
88
|
+
this.events.onQr(qr);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (this.pairingRequested || this.auth.state.creds.registered) return;
|
|
92
|
+
this.pairingRequested = true;
|
|
93
|
+
void sock
|
|
94
|
+
.requestPairingCode(this.phone)
|
|
95
|
+
.then((code) => {
|
|
96
|
+
if (!this.settled) this.events.onPairingCode(code);
|
|
97
|
+
})
|
|
98
|
+
.catch(() => {
|
|
99
|
+
this.fail('WhatsApp refused to issue a pairing code for that number');
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private onClose(
|
|
104
|
+
version: [number, number, number],
|
|
105
|
+
statusCode: number | undefined,
|
|
106
|
+
): void {
|
|
107
|
+
if (this.settled) return;
|
|
108
|
+
if (statusCode === DisconnectReason.loggedOut) {
|
|
109
|
+
this.fail('WhatsApp ended the pairing, start again');
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
this.reopens += 1;
|
|
113
|
+
if (this.reopens > MAX_REOPENS && statusCode !== RESTART_REQUIRED) {
|
|
114
|
+
this.fail('WhatsApp kept dropping the pairing connection, start again');
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this.open(version);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private pairedPhone(): string {
|
|
121
|
+
const linked = this.auth.state.creds.me?.id.split(':')[0] ?? '';
|
|
122
|
+
return PHONE_RE.test(linked) ? linked : this.phone;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private finish(): void {
|
|
126
|
+
if (this.settled) return;
|
|
127
|
+
const phone = this.pairedPhone();
|
|
128
|
+
if (phone === '') {
|
|
129
|
+
this.fail(
|
|
130
|
+
'WhatsApp paired but did not say which number it linked, so Metro has nothing to store',
|
|
131
|
+
);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const credentials = this.auth.serialize();
|
|
135
|
+
this.settled = true;
|
|
136
|
+
this.closeSocket()
|
|
137
|
+
.then(() => {
|
|
138
|
+
this.events.onPaired({
|
|
139
|
+
config: { phone, credentials },
|
|
140
|
+
identity: { phone },
|
|
141
|
+
});
|
|
142
|
+
})
|
|
143
|
+
.catch((err: unknown) => {
|
|
144
|
+
log.warn({ err: errMsg(err) }, 'whatsapp login: pairing report failed');
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private fail(message: string): void {
|
|
149
|
+
if (this.settled) return;
|
|
150
|
+
this.settled = true;
|
|
151
|
+
this.closeSocket()
|
|
152
|
+
.then(() => {
|
|
153
|
+
this.events.onFailed(message);
|
|
154
|
+
})
|
|
155
|
+
.catch((err: unknown) => {
|
|
156
|
+
log.warn({ err: errMsg(err) }, 'whatsapp login: failure report failed');
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private async closeSocket(): Promise<void> {
|
|
161
|
+
const sock = this.sock;
|
|
162
|
+
this.sock = null;
|
|
163
|
+
if (!sock) return;
|
|
164
|
+
try {
|
|
165
|
+
sock.ev.removeAllListeners('connection.update');
|
|
166
|
+
await sock.end(undefined);
|
|
167
|
+
} catch {
|
|
168
|
+
log.warn({}, 'whatsapp login: pairing socket did not close cleanly');
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async cancel(): Promise<void> {
|
|
173
|
+
this.settled = true;
|
|
174
|
+
await this.closeSocket();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { proto } from 'baileys';
|
|
2
|
+
import { extFromMime } from '@metro-labs/core/stations/attachments';
|
|
3
|
+
|
|
4
|
+
export type WAMediaKind =
|
|
5
|
+
| 'image'
|
|
6
|
+
| 'video'
|
|
7
|
+
| 'voice'
|
|
8
|
+
| 'audio'
|
|
9
|
+
| 'document'
|
|
10
|
+
| 'sticker';
|
|
11
|
+
|
|
12
|
+
export interface WAMediaRef {
|
|
13
|
+
kind: WAMediaKind;
|
|
14
|
+
mime?: string;
|
|
15
|
+
name?: string;
|
|
16
|
+
bytes?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Long {
|
|
20
|
+
toNumber(): number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface MediaNode {
|
|
24
|
+
mimetype?: string | null;
|
|
25
|
+
fileName?: string | null;
|
|
26
|
+
fileLength?: number | Long | null;
|
|
27
|
+
ptt?: boolean | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const MEDIA_NODES: [keyof proto.IMessage, WAMediaKind][] = [
|
|
31
|
+
['imageMessage', 'image'],
|
|
32
|
+
['videoMessage', 'video'],
|
|
33
|
+
['audioMessage', 'audio'],
|
|
34
|
+
['documentMessage', 'document'],
|
|
35
|
+
['stickerMessage', 'sticker'],
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const EXT_BY_KIND: Record<WAMediaKind, string> = {
|
|
39
|
+
image: 'jpg',
|
|
40
|
+
video: 'mp4',
|
|
41
|
+
voice: 'ogg',
|
|
42
|
+
audio: 'mp3',
|
|
43
|
+
document: 'bin',
|
|
44
|
+
sticker: 'webp',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function toBytes(v: number | Long | null | undefined): number | undefined {
|
|
48
|
+
if (typeof v === 'number') return v;
|
|
49
|
+
if (v && typeof v.toNumber === 'function') return v.toNumber();
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function baseMime(v: string | null | undefined): string | undefined {
|
|
54
|
+
const first = typeof v === 'string' ? (v.split(';')[0] ?? '').trim() : '';
|
|
55
|
+
return first || undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function fallbackName(kind: WAMediaKind, mime: string | undefined): string {
|
|
59
|
+
const ext = extFromMime(mime) ?? EXT_BY_KIND[kind];
|
|
60
|
+
return kind === 'voice' ? `voice-message.${ext}` : `${kind}.${ext}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function mediaRefIn(inner: proto.IMessage): WAMediaRef | undefined {
|
|
64
|
+
for (const [key, base] of MEDIA_NODES) {
|
|
65
|
+
const node = inner[key] as MediaNode | null | undefined;
|
|
66
|
+
if (!node) continue;
|
|
67
|
+
const kind = base === 'audio' && node.ptt === true ? 'voice' : base;
|
|
68
|
+
const mime = baseMime(node.mimetype);
|
|
69
|
+
const bytes = toBytes(node.fileLength);
|
|
70
|
+
return {
|
|
71
|
+
kind,
|
|
72
|
+
...(mime ? { mime } : {}),
|
|
73
|
+
name: node.fileName ?? fallbackName(kind, mime),
|
|
74
|
+
...(bytes === undefined ? {} : { bytes }),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function mediaTag(ref: WAMediaRef | undefined): string | undefined {
|
|
81
|
+
if (!ref) return undefined;
|
|
82
|
+
if (ref.kind === 'document') return `[document: ${ref.name ?? 'document'}]`;
|
|
83
|
+
return `[${ref.kind}]`;
|
|
84
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Normalized } from '@metro-labs/core/stations/messaging-normalize';
|
|
2
|
+
|
|
3
|
+
type Args = Record<string, unknown>;
|
|
4
|
+
|
|
5
|
+
export function normalizeWhatsApp(action: string, env: Args): Normalized {
|
|
6
|
+
if (action === 'reply') {
|
|
7
|
+
return {
|
|
8
|
+
action: 'send',
|
|
9
|
+
args: {
|
|
10
|
+
line: env.line,
|
|
11
|
+
text: env.text,
|
|
12
|
+
replyTo: env.replyTo ?? env.messageId,
|
|
13
|
+
account: env.account,
|
|
14
|
+
attachments: env.attachments,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
if (action === 'unreact') {
|
|
19
|
+
return { action: 'react', args: { ...env, emoji: '' } };
|
|
20
|
+
}
|
|
21
|
+
return { action, args: env };
|
|
22
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { WAMessageKey, proto } from 'baileys';
|
|
2
|
+
|
|
3
|
+
const MAX_TRACKED_SENDS = 256;
|
|
4
|
+
|
|
5
|
+
export interface Outbox {
|
|
6
|
+
remember(
|
|
7
|
+
key: WAMessageKey | null | undefined,
|
|
8
|
+
message: proto.IMessage | null | undefined,
|
|
9
|
+
): void;
|
|
10
|
+
lookup(key: WAMessageKey): proto.IMessage | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const slotOf = (jid: string, id: string): string => `${jid}|${id}`;
|
|
14
|
+
|
|
15
|
+
export function makeOutbox(max: number = MAX_TRACKED_SENDS): Outbox {
|
|
16
|
+
const sent = new Map<string, proto.IMessage>();
|
|
17
|
+
return {
|
|
18
|
+
remember(key, message) {
|
|
19
|
+
const id = key?.id;
|
|
20
|
+
const jid = key?.remoteJid;
|
|
21
|
+
if (!id || !jid || !message || key?.fromMe !== true) return;
|
|
22
|
+
const slot = slotOf(jid, id);
|
|
23
|
+
sent.delete(slot);
|
|
24
|
+
sent.set(slot, message);
|
|
25
|
+
while (sent.size > max) {
|
|
26
|
+
const oldest = sent.keys().next().value;
|
|
27
|
+
if (oldest === undefined) break;
|
|
28
|
+
sent.delete(oldest);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
lookup(key) {
|
|
32
|
+
const id = key.id;
|
|
33
|
+
const jid = key.remoteJid;
|
|
34
|
+
if (!id || !jid) return undefined;
|
|
35
|
+
return sent.get(slotOf(jid, id));
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|