@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,295 @@
|
|
|
1
|
+
import { accountFor, accounts, tg, targetOf } from './accounts.js';
|
|
2
|
+
import { respond } from './wire.js';
|
|
3
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
4
|
+
import { normalizeTelegram } from '@metro-labs/core/stations/messaging-normalize';
|
|
5
|
+
import {
|
|
6
|
+
adminMemberList,
|
|
7
|
+
inaccessibleMemberList,
|
|
8
|
+
type TgChatMember,
|
|
9
|
+
} from './members.js';
|
|
10
|
+
import {
|
|
11
|
+
makeStation,
|
|
12
|
+
type CallMsg,
|
|
13
|
+
type StationHandler,
|
|
14
|
+
} from '@metro-labs/core/stations/station-runtime';
|
|
15
|
+
import { mediaKindOf } from './attachments.js';
|
|
16
|
+
import { assertContentLength } from '@metro-labs/core/stations/attachments';
|
|
17
|
+
import {
|
|
18
|
+
emitOutbound,
|
|
19
|
+
finishSend,
|
|
20
|
+
media,
|
|
21
|
+
MEDIA_METHOD_FIELD,
|
|
22
|
+
sendDice,
|
|
23
|
+
sendLocation,
|
|
24
|
+
sendMedia,
|
|
25
|
+
} from './media-actions.js';
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export type { CallMsg };
|
|
31
|
+
|
|
32
|
+
const meCache = new Map<string, { id: number; username: string | null }>();
|
|
33
|
+
async function getMe(
|
|
34
|
+
accountId: string,
|
|
35
|
+
): Promise<{ id: number; username: string | null } | null> {
|
|
36
|
+
const cached = meCache.get(accountId);
|
|
37
|
+
if (cached) return cached;
|
|
38
|
+
try {
|
|
39
|
+
const me = await tg<{ id: number; username?: string }>(
|
|
40
|
+
accountId,
|
|
41
|
+
'getMe',
|
|
42
|
+
{},
|
|
43
|
+
10_000,
|
|
44
|
+
);
|
|
45
|
+
const v = { id: me.id, username: me.username ?? null };
|
|
46
|
+
meCache.set(accountId, v);
|
|
47
|
+
return v;
|
|
48
|
+
} catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function listAccounts(id: string): Promise<void> {
|
|
54
|
+
const list = await Promise.all(
|
|
55
|
+
[...accounts.values()].map(async (a) => {
|
|
56
|
+
const me = await getMe(a.cfg.id);
|
|
57
|
+
const username = me?.username ?? null;
|
|
58
|
+
return {
|
|
59
|
+
id: a.cfg.id,
|
|
60
|
+
handle: username === null ? null : `@${username}`,
|
|
61
|
+
url: username === null ? null : `https://t.me/${username}`,
|
|
62
|
+
owner: a.cfg.owner ?? null,
|
|
63
|
+
botId: me?.id ?? null,
|
|
64
|
+
username,
|
|
65
|
+
};
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
respond(id, { result: { accounts: list } });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface SendArgs {
|
|
72
|
+
line: string;
|
|
73
|
+
text: string;
|
|
74
|
+
replyTo?: string;
|
|
75
|
+
parseMode?: string;
|
|
76
|
+
buttons?: { text: string; url: string }[][];
|
|
77
|
+
account?: string;
|
|
78
|
+
attachments?: {
|
|
79
|
+
kind?: string;
|
|
80
|
+
path?: string;
|
|
81
|
+
url?: string;
|
|
82
|
+
mime?: string;
|
|
83
|
+
name?: string;
|
|
84
|
+
}[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
type WireAttachment = NonNullable<SendArgs['attachments']>[number];
|
|
89
|
+
|
|
90
|
+
const attachmentSrc = (att: WireAttachment): string | undefined =>
|
|
91
|
+
att.path ?? att.url;
|
|
92
|
+
|
|
93
|
+
const attachmentKind = (att: WireAttachment): string =>
|
|
94
|
+
mediaKindOf(att.kind, att.mime, attachmentSrc(att) ?? att.name);
|
|
95
|
+
|
|
96
|
+
async function sendOneAttachment(
|
|
97
|
+
a: SendArgs,
|
|
98
|
+
att: WireAttachment,
|
|
99
|
+
caption: string | undefined,
|
|
100
|
+
): Promise<{ accountId: string; message_id: number; kind: string }> {
|
|
101
|
+
const kind = attachmentKind(att);
|
|
102
|
+
const mf = MEDIA_METHOD_FIELD[kind];
|
|
103
|
+
if (!mf) throw new Error(`telegram-bot cannot send a '${kind}' attachment`);
|
|
104
|
+
const sent = await sendMedia(mf.method, mf.field, {
|
|
105
|
+
line: a.line,
|
|
106
|
+
path: attachmentSrc(att),
|
|
107
|
+
caption,
|
|
108
|
+
replyTo: a.replyTo,
|
|
109
|
+
parseMode: a.parseMode,
|
|
110
|
+
account: a.account,
|
|
111
|
+
name: att.name,
|
|
112
|
+
});
|
|
113
|
+
return { ...sent, kind };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const captionLabel = (caption: string | undefined, kind: string): string =>
|
|
117
|
+
caption !== undefined && caption !== '' ? caption : `[${kind}]`;
|
|
118
|
+
|
|
119
|
+
async function sendAttachments(id: string, a: SendArgs): Promise<void> {
|
|
120
|
+
const { line, text, replyTo, attachments = [] } = a;
|
|
121
|
+
let last: { accountId: string; message_id: number } | undefined;
|
|
122
|
+
const delivered: string[] = [];
|
|
123
|
+
for (let i = 0; i < attachments.length; i++) {
|
|
124
|
+
const att = attachments[i];
|
|
125
|
+
if (!att) continue;
|
|
126
|
+
const caption = i === 0 ? text : undefined;
|
|
127
|
+
const sent = await sendOneAttachment(a, att, caption);
|
|
128
|
+
last = sent;
|
|
129
|
+
delivered.push(sent.kind);
|
|
130
|
+
emitOutbound(
|
|
131
|
+
sent.accountId,
|
|
132
|
+
line,
|
|
133
|
+
String(sent.message_id),
|
|
134
|
+
captionLabel(caption, sent.kind),
|
|
135
|
+
replyTo,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (!last) throw new Error('no attachments were sent');
|
|
139
|
+
respond(id, {
|
|
140
|
+
result: {
|
|
141
|
+
messageId: String(last.message_id),
|
|
142
|
+
account: last.accountId,
|
|
143
|
+
attachments: delivered,
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function send(id: string, args: Record<string, unknown>): Promise<void> {
|
|
149
|
+
const a = args as unknown as SendArgs;
|
|
150
|
+
if (a.attachments?.length) {
|
|
151
|
+
await sendAttachments(id, a);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const { line, text, replyTo, parseMode, buttons, account } = a;
|
|
155
|
+
const { accountId, chatId, topicId } = targetOf(line, account);
|
|
156
|
+
const body: Record<string, unknown> = { chat_id: chatId, text };
|
|
157
|
+
if (topicId !== undefined) body.message_thread_id = topicId;
|
|
158
|
+
if (replyTo) body.reply_parameters = { message_id: Number(replyTo) };
|
|
159
|
+
if (parseMode) body.parse_mode = parseMode;
|
|
160
|
+
if (buttons) body.reply_markup = { inline_keyboard: buttons };
|
|
161
|
+
const sent = await tg<{ message_id: number }>(accountId, 'sendMessage', body);
|
|
162
|
+
finishSend(id, accountId, line, String(sent.message_id), text, replyTo);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function react(id: string, args: Record<string, unknown>): Promise<void> {
|
|
166
|
+
const { line, messageId, emoji, account } = args as {
|
|
167
|
+
line: string;
|
|
168
|
+
messageId: string;
|
|
169
|
+
emoji: string;
|
|
170
|
+
account?: string;
|
|
171
|
+
};
|
|
172
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
173
|
+
await tg(accountId, 'setMessageReaction', {
|
|
174
|
+
chat_id: chatId,
|
|
175
|
+
message_id: Number(messageId),
|
|
176
|
+
reaction: emoji ? [{ type: 'emoji', emoji }] : [],
|
|
177
|
+
});
|
|
178
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function edit(id: string, args: Record<string, unknown>): Promise<void> {
|
|
182
|
+
const { line, messageId, text, parseMode, account } = args as {
|
|
183
|
+
line: string;
|
|
184
|
+
messageId: string;
|
|
185
|
+
text: string;
|
|
186
|
+
parseMode?: string;
|
|
187
|
+
account?: string;
|
|
188
|
+
};
|
|
189
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
190
|
+
await tg(accountId, 'editMessageText', {
|
|
191
|
+
chat_id: chatId,
|
|
192
|
+
message_id: Number(messageId),
|
|
193
|
+
text,
|
|
194
|
+
parse_mode: parseMode,
|
|
195
|
+
});
|
|
196
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function remove(id: string, args: Record<string, unknown>): Promise<void> {
|
|
200
|
+
const { line, messageId, account } = args as {
|
|
201
|
+
line: string;
|
|
202
|
+
messageId: string;
|
|
203
|
+
account?: string;
|
|
204
|
+
};
|
|
205
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
206
|
+
await tg(accountId, 'deleteMessage', {
|
|
207
|
+
chat_id: chatId,
|
|
208
|
+
message_id: Number(messageId),
|
|
209
|
+
});
|
|
210
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
async function download(id: string, args: Record<string, unknown>): Promise<void> {
|
|
216
|
+
const {
|
|
217
|
+
fileId,
|
|
218
|
+
outDir = '/tmp',
|
|
219
|
+
account,
|
|
220
|
+
} = args as { fileId: string; outDir?: string; account?: string };
|
|
221
|
+
const accountId = accountFor({ account });
|
|
222
|
+
const acct = accounts.get(accountId);
|
|
223
|
+
if (!acct) throw new Error(`unknown account '${accountId}'`);
|
|
224
|
+
const meta = await tg<{ file_path: string }>(accountId, 'getFile', {
|
|
225
|
+
file_id: fileId,
|
|
226
|
+
});
|
|
227
|
+
const res = await fetch(`${acct.fileApi}/${meta.file_path}`, {
|
|
228
|
+
signal: AbortSignal.timeout(60_000),
|
|
229
|
+
});
|
|
230
|
+
if (!res.ok) throw new Error(`telegram-bot download ${res.status}`);
|
|
231
|
+
assertContentLength(res.headers.get('content-length'));
|
|
232
|
+
const data = await res.arrayBuffer();
|
|
233
|
+
const filename = meta.file_path.split('/').pop() ?? `${fileId}.bin`;
|
|
234
|
+
const path = `${outDir}/${Date.now()}-${filename}`;
|
|
235
|
+
await Bun.write(path, data);
|
|
236
|
+
respond(id, {
|
|
237
|
+
result: { path, fileSize: data.byteLength, account: accountId },
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function listMembers(
|
|
242
|
+
id: string,
|
|
243
|
+
args: Record<string, unknown>,
|
|
244
|
+
): Promise<void> {
|
|
245
|
+
const { line, account } = args as { line: string; account?: string };
|
|
246
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
247
|
+
let admins: TgChatMember[];
|
|
248
|
+
try {
|
|
249
|
+
admins = await tg<TgChatMember[]>(accountId, 'getChatAdministrators', {
|
|
250
|
+
chat_id: chatId,
|
|
251
|
+
});
|
|
252
|
+
} catch (e) {
|
|
253
|
+
respond(id, { result: inaccessibleMemberList(errMsg(e)) });
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
let total: number | null = null;
|
|
257
|
+
try {
|
|
258
|
+
total = await tg<number>(accountId, 'getChatMemberCount', {
|
|
259
|
+
chat_id: chatId,
|
|
260
|
+
});
|
|
261
|
+
} catch {
|
|
262
|
+
total = null;
|
|
263
|
+
}
|
|
264
|
+
respond(id, { result: adminMemberList(admins, total) });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const HANDLERS: Record<string, StationHandler> = {
|
|
268
|
+
accounts: (id) => listAccounts(id),
|
|
269
|
+
send,
|
|
270
|
+
react,
|
|
271
|
+
edit,
|
|
272
|
+
delete: remove,
|
|
273
|
+
listMembers,
|
|
274
|
+
send_photo: (id, args) =>
|
|
275
|
+
media(id, 'sendPhoto', 'photo', ((args.caption as string) ?? '') + ' [image]', args),
|
|
276
|
+
send_document: (id, args) =>
|
|
277
|
+
media(id, 'sendDocument', 'document', ((args.caption as string) ?? '') + ' [file]', args),
|
|
278
|
+
send_voice: (id, args) => media(id, 'sendVoice', 'voice', '[voice]', args),
|
|
279
|
+
send_sticker: (id, args) => media(id, 'sendSticker', 'sticker', '[sticker]', args),
|
|
280
|
+
send_dice: sendDice,
|
|
281
|
+
send_location: sendLocation,
|
|
282
|
+
download,
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
export const handleCall = makeStation({
|
|
286
|
+
handlers: HANDLERS,
|
|
287
|
+
normalize: normalizeTelegram,
|
|
288
|
+
preDispatch: (id, action) => {
|
|
289
|
+
if (action === 'read') {
|
|
290
|
+
respond(id, { error: "unsupported verb 'read' on telegram-bot" });
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
return false;
|
|
294
|
+
},
|
|
295
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import {
|
|
2
|
+
saveBufferToCache,
|
|
3
|
+
assertContentLength,
|
|
4
|
+
} from '@metro-labs/core/stations/attachments';
|
|
5
|
+
import type { SavedAttachment } from '@metro-labs/core/stations/attachments';
|
|
6
|
+
import { tg, accounts } from './accounts.js';
|
|
7
|
+
import type { TgMsg } from './types.js';
|
|
8
|
+
|
|
9
|
+
export type { SavedAttachment };
|
|
10
|
+
|
|
11
|
+
function isOggRef(mime: string | undefined, ref: string | undefined): boolean {
|
|
12
|
+
const ext = ref?.split('?')[0]?.split('.').pop()?.toLowerCase();
|
|
13
|
+
return mime === 'audio/ogg' || ext === 'ogg' || ext === 'oga';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isVoiceRef(
|
|
17
|
+
kind: string | undefined,
|
|
18
|
+
mime: string | undefined,
|
|
19
|
+
ref: string | undefined,
|
|
20
|
+
): boolean {
|
|
21
|
+
const isOgg = isOggRef(mime, ref);
|
|
22
|
+
if (kind === 'voice') return true;
|
|
23
|
+
if (kind === 'audio') return isOgg;
|
|
24
|
+
return !kind && isOgg;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function mediaKindOf(
|
|
28
|
+
kind: string | undefined,
|
|
29
|
+
mime: string | undefined,
|
|
30
|
+
ref: string | undefined,
|
|
31
|
+
): 'image' | 'voice' | 'audio' | 'video' | 'document' {
|
|
32
|
+
const m = mime ?? '';
|
|
33
|
+
if (kind === 'image' || m.startsWith('image/')) return 'image';
|
|
34
|
+
if (isVoiceRef(kind, mime, ref)) return 'voice';
|
|
35
|
+
if (kind === 'video' || m.startsWith('video/')) return 'video';
|
|
36
|
+
if (kind === 'audio' || m.startsWith('audio/')) return 'audio';
|
|
37
|
+
return 'document';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface TgMediaRef {
|
|
41
|
+
fileId: string;
|
|
42
|
+
name?: string;
|
|
43
|
+
mime?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const MEDIA_EXTRACTORS: ((m: TgMsg) => TgMediaRef | null)[] = [
|
|
47
|
+
(m) => {
|
|
48
|
+
const largest = m.photo?.[m.photo.length - 1];
|
|
49
|
+
return largest ? { fileId: largest.file_id, mime: 'image/jpeg' } : null;
|
|
50
|
+
},
|
|
51
|
+
(m) =>
|
|
52
|
+
m.document?.file_id
|
|
53
|
+
? {
|
|
54
|
+
fileId: m.document.file_id,
|
|
55
|
+
name: m.document.file_name,
|
|
56
|
+
mime: m.document.mime_type,
|
|
57
|
+
}
|
|
58
|
+
: null,
|
|
59
|
+
(m) =>
|
|
60
|
+
m.video?.file_id
|
|
61
|
+
? {
|
|
62
|
+
fileId: m.video.file_id,
|
|
63
|
+
name: m.video.file_name,
|
|
64
|
+
mime: m.video.mime_type ?? 'video/mp4',
|
|
65
|
+
}
|
|
66
|
+
: null,
|
|
67
|
+
(m) =>
|
|
68
|
+
m.animation?.file_id
|
|
69
|
+
? {
|
|
70
|
+
fileId: m.animation.file_id,
|
|
71
|
+
name: m.animation.file_name,
|
|
72
|
+
mime: m.animation.mime_type,
|
|
73
|
+
}
|
|
74
|
+
: null,
|
|
75
|
+
(m) =>
|
|
76
|
+
m.audio?.file_id
|
|
77
|
+
? {
|
|
78
|
+
fileId: m.audio.file_id,
|
|
79
|
+
name: m.audio.file_name,
|
|
80
|
+
mime: m.audio.mime_type,
|
|
81
|
+
}
|
|
82
|
+
: null,
|
|
83
|
+
(m) =>
|
|
84
|
+
m.voice?.file_id
|
|
85
|
+
? { fileId: m.voice.file_id, mime: m.voice.mime_type ?? 'audio/ogg' }
|
|
86
|
+
: null,
|
|
87
|
+
(m) =>
|
|
88
|
+
m.sticker?.file_id
|
|
89
|
+
? { fileId: m.sticker.file_id, mime: 'image/webp' }
|
|
90
|
+
: null,
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
export function mediaRefOf(m: TgMsg): TgMediaRef | null {
|
|
94
|
+
for (const extract of MEDIA_EXTRACTORS) {
|
|
95
|
+
const ref = extract(m);
|
|
96
|
+
if (ref) return ref;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function saveTelegramMedia(
|
|
102
|
+
accountId: string,
|
|
103
|
+
ref: TgMediaRef,
|
|
104
|
+
messageId: string,
|
|
105
|
+
index = 0,
|
|
106
|
+
): Promise<SavedAttachment> {
|
|
107
|
+
const file = await tg<{ file_path?: string }>(accountId, 'getFile', {
|
|
108
|
+
file_id: ref.fileId,
|
|
109
|
+
});
|
|
110
|
+
if (!file.file_path)
|
|
111
|
+
throw new Error(`telegram-bot getFile returned no file_path for ${ref.fileId}`);
|
|
112
|
+
const acct = accounts.get(accountId);
|
|
113
|
+
if (!acct) throw new Error(`unknown account '${accountId}'`);
|
|
114
|
+
const res = await fetch(`${acct.fileApi}/${file.file_path}`, {
|
|
115
|
+
signal: AbortSignal.timeout(60_000),
|
|
116
|
+
});
|
|
117
|
+
if (!res.ok)
|
|
118
|
+
throw new Error(
|
|
119
|
+
`telegram-bot file download ${res.status} for ${file.file_path}`,
|
|
120
|
+
);
|
|
121
|
+
assertContentLength(res.headers.get('content-length'));
|
|
122
|
+
const data = new Uint8Array(await res.arrayBuffer());
|
|
123
|
+
const saved = await saveBufferToCache(data, messageId, index, {
|
|
124
|
+
mime: ref.mime,
|
|
125
|
+
name: ref.name ?? file.file_path,
|
|
126
|
+
});
|
|
127
|
+
return { ...saved, name: ref.name };
|
|
128
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
2
|
+
import { accounts, lineOf } from './accounts.js';
|
|
3
|
+
import { mintId, SELF_URI } from './wire.js';
|
|
4
|
+
import { mediaRefOf, saveTelegramMedia } from './attachments.js';
|
|
5
|
+
import type { TgMsg, TgReaction, TgReactionCount } from './types.js';
|
|
6
|
+
|
|
7
|
+
export type { TgMsg, TgReaction, TgReactionCount };
|
|
8
|
+
|
|
9
|
+
function lineForMsg(
|
|
10
|
+
accountId: string,
|
|
11
|
+
m: TgMsg,
|
|
12
|
+
): { line: string; topicId?: number } {
|
|
13
|
+
const topicId = m.is_topic_message ? m.message_thread_id : undefined;
|
|
14
|
+
return { line: lineOf(accountId, m.chat.id, topicId), topicId };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function stickerTag(s: NonNullable<TgMsg['sticker']>): string {
|
|
18
|
+
const set = s.set_name ? ` · ${s.set_name}` : '';
|
|
19
|
+
return `[sticker${s.emoji ? ` ${s.emoji}` : ''}${set}]`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const TAG_EXTRACTORS: ((m: TgMsg) => string | null)[] = [
|
|
23
|
+
(m) => (m.photo?.length ? '[image]' : null),
|
|
24
|
+
(m) => (m.voice ? '[voice]' : null),
|
|
25
|
+
(m) => (m.audio ? `[audio: ${m.audio.file_name ?? 'audio'}]` : null),
|
|
26
|
+
(m) => (m.video ? `[video: ${m.video.file_name ?? 'video'}]` : null),
|
|
27
|
+
(m) => (m.animation ? `[gif: ${m.animation.file_name ?? 'gif'}]` : null),
|
|
28
|
+
(m) => (m.sticker ? stickerTag(m.sticker) : null),
|
|
29
|
+
(m) =>
|
|
30
|
+
m.document && !m.animation
|
|
31
|
+
? `[file: ${m.document.file_name ?? 'doc'}]`
|
|
32
|
+
: null,
|
|
33
|
+
(m) =>
|
|
34
|
+
m.location
|
|
35
|
+
? `[location: ${m.location.latitude}, ${m.location.longitude}]`
|
|
36
|
+
: null,
|
|
37
|
+
(m) => (m.dice ? `[dice ${m.dice.emoji} = ${m.dice.value}]` : null),
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
function projectText(m: TgMsg): string {
|
|
41
|
+
const tags = TAG_EXTRACTORS.map((f) => f(m)).filter(
|
|
42
|
+
(t): t is string => t !== null,
|
|
43
|
+
);
|
|
44
|
+
return [m.text ?? m.caption, ...tags].filter(Boolean).join(' ');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const botIdOf = (accountId: string): number | null => {
|
|
48
|
+
const token = accounts.get(accountId)?.cfg.token ?? '';
|
|
49
|
+
const id = Number(token.split(':')[0]);
|
|
50
|
+
return Number.isInteger(id) && id > 0 ? id : null;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function mentionsBot(m: TgMsg, botId: number | null, username: string | undefined): boolean {
|
|
54
|
+
const text = m.text ?? m.caption ?? '';
|
|
55
|
+
const handle = username === undefined ? null : `@${username.toLowerCase()}`;
|
|
56
|
+
return [...(m.entities ?? []), ...(m.caption_entities ?? [])].some((e) => {
|
|
57
|
+
if (e.type === 'text_mention') return botId !== null && e.user?.id === botId;
|
|
58
|
+
if (e.type !== 'mention' || handle === null) return false;
|
|
59
|
+
return text.slice(e.offset, e.offset + e.length).toLowerCase() === handle;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function addressing(accountId: string, m: TgMsg): Record<string, unknown> {
|
|
64
|
+
const botId = botIdOf(accountId);
|
|
65
|
+
const topicRoot = m.is_topic_message === true && m.reply_to_message?.message_id === m.message_thread_id;
|
|
66
|
+
const replied = topicRoot ? undefined : m.reply_to_message;
|
|
67
|
+
const replyTo = replied === undefined ? undefined : String(replied.message_id);
|
|
68
|
+
return {
|
|
69
|
+
...(replyTo === undefined ? {} : { reply_to: replyTo, event: { type: 'reply', replyTo } }),
|
|
70
|
+
mentions_self: mentionsBot(m, botId, accounts.get(accountId)?.username),
|
|
71
|
+
reply_to_self: botId !== null && replied?.from?.id === botId,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function envelope(accountId: string, m: TgMsg): Record<string, unknown> {
|
|
76
|
+
const { line } = lineForMsg(accountId, m);
|
|
77
|
+
return {
|
|
78
|
+
kind: 'inbound',
|
|
79
|
+
id: mintId(),
|
|
80
|
+
ts: new Date(m.date * 1000).toISOString(),
|
|
81
|
+
station: 'telegram-bot',
|
|
82
|
+
line,
|
|
83
|
+
line_name: m.chat.title ?? m.chat.first_name ?? undefined,
|
|
84
|
+
from: `metro://telegram-bot/${accountId}/user/${m.from?.id ?? 'unknown'}`,
|
|
85
|
+
from_name: m.from?.username ? `@${m.from.username}` : m.from?.first_name,
|
|
86
|
+
from_display_name: m.from?.first_name,
|
|
87
|
+
message_id: String(m.message_id),
|
|
88
|
+
text: projectText(m),
|
|
89
|
+
payload: m,
|
|
90
|
+
is_private: m.chat.type === 'private',
|
|
91
|
+
...addressing(accountId, m),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function reactionEnvelope(
|
|
96
|
+
accountId: string,
|
|
97
|
+
r: TgReaction,
|
|
98
|
+
): Record<string, unknown> | null {
|
|
99
|
+
if (r.user?.is_bot) return null;
|
|
100
|
+
const newEmojis = r.new_reaction
|
|
101
|
+
.filter((x) => x.type === 'emoji')
|
|
102
|
+
.map((x) => x.emoji ?? '');
|
|
103
|
+
const oldEmojis = r.old_reaction
|
|
104
|
+
.filter((x) => x.type === 'emoji')
|
|
105
|
+
.map((x) => x.emoji ?? '');
|
|
106
|
+
const added = newEmojis.filter((e) => !oldEmojis.includes(e));
|
|
107
|
+
if (!added.length) return null;
|
|
108
|
+
return {
|
|
109
|
+
kind: 'react',
|
|
110
|
+
id: mintId(),
|
|
111
|
+
ts: new Date(r.date * 1000).toISOString(),
|
|
112
|
+
station: 'telegram-bot',
|
|
113
|
+
line: lineOf(accountId, r.chat.id),
|
|
114
|
+
from: `metro://telegram-bot/${accountId}/user/${r.user?.id ?? 'unknown'}`,
|
|
115
|
+
from_name: r.user?.username ? `@${r.user.username}` : r.user?.first_name,
|
|
116
|
+
from_display_name: r.user?.first_name,
|
|
117
|
+
message_id: String(r.message_id),
|
|
118
|
+
emoji: added[0],
|
|
119
|
+
event: { type: 'react', emoji: added[0], targetId: String(r.message_id) },
|
|
120
|
+
is_private: r.chat.type === 'private',
|
|
121
|
+
payload: r,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function reactionCountEnvelope(
|
|
126
|
+
accountId: string,
|
|
127
|
+
rc: TgReactionCount,
|
|
128
|
+
): Record<string, unknown> | null {
|
|
129
|
+
const emojiReactions = rc.reactions.filter(
|
|
130
|
+
(r) => r.type.type === 'emoji' && r.type.emoji,
|
|
131
|
+
);
|
|
132
|
+
if (!emojiReactions.length) return null;
|
|
133
|
+
const top = emojiReactions.reduce((a, b) =>
|
|
134
|
+
b.total_count > a.total_count ? b : a,
|
|
135
|
+
);
|
|
136
|
+
const emoji = top.type.emoji ?? '';
|
|
137
|
+
if (!emoji) return null;
|
|
138
|
+
return {
|
|
139
|
+
kind: 'react',
|
|
140
|
+
id: mintId(),
|
|
141
|
+
ts: new Date(rc.date * 1000).toISOString(),
|
|
142
|
+
station: 'telegram-bot',
|
|
143
|
+
line: lineOf(accountId, rc.chat.id),
|
|
144
|
+
from: `metro://telegram-bot/${accountId}/user/unknown`,
|
|
145
|
+
message_id: String(rc.message_id),
|
|
146
|
+
emoji,
|
|
147
|
+
event: { type: 'react', emoji, targetId: String(rc.message_id) },
|
|
148
|
+
is_private: rc.chat.type === 'private',
|
|
149
|
+
payload: rc,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function emitInbound(
|
|
154
|
+
emit: (e: unknown) => void,
|
|
155
|
+
accountId: string,
|
|
156
|
+
e: Record<string, unknown>,
|
|
157
|
+
): void {
|
|
158
|
+
const owner = accounts.get(accountId)?.cfg.owner;
|
|
159
|
+
const payload = {
|
|
160
|
+
...(e.payload as Record<string, unknown> | undefined),
|
|
161
|
+
account: accountId,
|
|
162
|
+
};
|
|
163
|
+
emit({ ...e, ...(owner ? { to: owner } : {}), account: accountId, payload });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function saveMediaAndEmit(
|
|
167
|
+
emit: (e: unknown) => void,
|
|
168
|
+
accountId: string,
|
|
169
|
+
m: TgMsg,
|
|
170
|
+
sourceEnvId: string,
|
|
171
|
+
): void {
|
|
172
|
+
const ref = mediaRefOf(m);
|
|
173
|
+
if (!ref) return;
|
|
174
|
+
const line = lineForMsg(accountId, m).line;
|
|
175
|
+
void saveTelegramMedia(accountId, ref, String(m.message_id), 0)
|
|
176
|
+
.then((saved) => {
|
|
177
|
+
emitInbound(emit, accountId, {
|
|
178
|
+
kind: 'inbound',
|
|
179
|
+
id: mintId(),
|
|
180
|
+
ts: new Date().toISOString(),
|
|
181
|
+
station: 'telegram-bot',
|
|
182
|
+
line,
|
|
183
|
+
from: SELF_URI || `metro://telegram-bot/${accountId}/self`,
|
|
184
|
+
text: `📎 saved: ${saved.path}`,
|
|
185
|
+
payload: {
|
|
186
|
+
contentType: 'attachmentSaved',
|
|
187
|
+
attachmentFor: sourceEnvId,
|
|
188
|
+
index: 0,
|
|
189
|
+
attachmentPath: saved.path,
|
|
190
|
+
localPath: saved.path,
|
|
191
|
+
mime: saved.mime,
|
|
192
|
+
name: saved.name,
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
})
|
|
196
|
+
.catch((err: unknown) =>
|
|
197
|
+
process.stderr.write(
|
|
198
|
+
`telegram-bot media save failed: ${errMsg(err)}\n`,
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
}
|