@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,123 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import type { IncomingMessage, Server } from 'node:http';
|
|
3
|
+
import type { Duplex } from 'node:stream';
|
|
4
|
+
import { WebSocketServer, type RawData, type WebSocket } from 'ws';
|
|
5
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
6
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
7
|
+
import { takeTerminalTicket, type TerminalGrant } from './tickets.js';
|
|
8
|
+
|
|
9
|
+
const TICKET_PATH = /^\/api\/terminal\/([A-Za-z0-9_-]{43})$/;
|
|
10
|
+
const MAX_MESSAGE = 1024 * 1024;
|
|
11
|
+
const DEFAULT_COLS = 120;
|
|
12
|
+
const DEFAULT_ROWS = 36;
|
|
13
|
+
const MAX_DIMENSION = 500;
|
|
14
|
+
|
|
15
|
+
export interface TerminalSocketDeps {
|
|
16
|
+
command: (session: string) => string[];
|
|
17
|
+
takeTicket?: (ticket: string) => TerminalGrant | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const rawBytes = (data: RawData): Buffer =>
|
|
21
|
+
Buffer.isBuffer(data) ? data : Array.isArray(data) ? Buffer.concat(data) : Buffer.from(data);
|
|
22
|
+
|
|
23
|
+
export const resizeWindowArgs = (session: string, cols: number, rows: number): string[] => [
|
|
24
|
+
'resize-window',
|
|
25
|
+
'-t',
|
|
26
|
+
`${session}:`,
|
|
27
|
+
'-x',
|
|
28
|
+
String(cols),
|
|
29
|
+
'-y',
|
|
30
|
+
String(rows),
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
function sizeTmuxWindow(command: string[], session: string, cols: number, rows: number): void {
|
|
34
|
+
if (command[0] !== 'tmux') return;
|
|
35
|
+
const child = spawn('tmux', resizeWindowArgs(session, cols, rows), { stdio: 'ignore' });
|
|
36
|
+
child.on('error', (err) => {
|
|
37
|
+
log.warn({ err: errMsg(err) }, 'terminal: resize-window could not run');
|
|
38
|
+
});
|
|
39
|
+
child.on('exit', (code) => {
|
|
40
|
+
if (code !== 0) log.warn({ code, session, cols, rows }, 'terminal: tmux refused the window resize');
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const dimension = (raw: unknown): number | null =>
|
|
45
|
+
typeof raw === 'number' && Number.isInteger(raw) && raw > 1 && raw <= MAX_DIMENSION ? raw : null;
|
|
46
|
+
|
|
47
|
+
export function sizeFrom(control: unknown): { cols: number; rows: number } | null {
|
|
48
|
+
if (!isRecord(control)) return null;
|
|
49
|
+
const cols = dimension(control.cols);
|
|
50
|
+
const rows = dimension(control.rows);
|
|
51
|
+
return cols === null || rows === null ? null : { cols, rows };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function runTerminal(ws: WebSocket, command: string[], subject: string, session: string): void {
|
|
55
|
+
const terminal = new Bun.Terminal({
|
|
56
|
+
cols: DEFAULT_COLS,
|
|
57
|
+
rows: DEFAULT_ROWS,
|
|
58
|
+
data: (_term, chunk) => {
|
|
59
|
+
if (ws.readyState === ws.OPEN) ws.send(chunk);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
const proc = Bun.spawn(command, {
|
|
63
|
+
terminal,
|
|
64
|
+
env: { ...process.env, TERM: 'xterm-256color', COLORTERM: 'truecolor' },
|
|
65
|
+
});
|
|
66
|
+
log.info({ subject, pid: proc.pid, command: command[0] }, 'terminal: opened');
|
|
67
|
+
ws.on('message', (data, isBinary) => {
|
|
68
|
+
const bytes = rawBytes(data);
|
|
69
|
+
if (isBinary) {
|
|
70
|
+
terminal.write(bytes);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const size = sizeFrom(JSON.parse(bytes.toString('utf8')));
|
|
75
|
+
if (size !== null) {
|
|
76
|
+
terminal.resize(size.cols, size.rows);
|
|
77
|
+
proc.kill('SIGWINCH');
|
|
78
|
+
sizeTmuxWindow(command, session, size.cols, size.rows);
|
|
79
|
+
}
|
|
80
|
+
} catch (err) {
|
|
81
|
+
log.warn({ err: errMsg(err) }, 'terminal: bad control frame');
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
ws.on('close', () => {
|
|
85
|
+
proc.kill();
|
|
86
|
+
terminal.close();
|
|
87
|
+
log.info({ subject, pid: proc.pid }, 'terminal: closed by the page');
|
|
88
|
+
});
|
|
89
|
+
proc.exited
|
|
90
|
+
.then((code) => {
|
|
91
|
+
if (ws.readyState === ws.OPEN) ws.close(1000, `exit ${String(code)}`);
|
|
92
|
+
terminal.close();
|
|
93
|
+
})
|
|
94
|
+
.catch((err: unknown) => {
|
|
95
|
+
log.warn({ err: errMsg(err) }, 'terminal: exit watch failed');
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function refuse(socket: Duplex, status: number, text: string): void {
|
|
100
|
+
socket.write(`HTTP/1.1 ${String(status)} ${text}\r\nConnection: close\r\n\r\n`);
|
|
101
|
+
socket.destroy();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function attachTerminalSockets(server: Server, deps: TerminalSocketDeps): void {
|
|
105
|
+
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_MESSAGE });
|
|
106
|
+
const take = deps.takeTicket ?? takeTerminalTicket;
|
|
107
|
+
server.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer) => {
|
|
108
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
109
|
+
const found = TICKET_PATH.exec(path);
|
|
110
|
+
if (!found) {
|
|
111
|
+
refuse(socket, 404, 'Not Found');
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const grant = take(found[1] ?? '');
|
|
115
|
+
if (grant === null) {
|
|
116
|
+
refuse(socket, 401, 'Unauthorized');
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
120
|
+
runTerminal(ws, deps.command(grant.session), grant.subject, grant.session);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
const TTL_MS = 30_000;
|
|
4
|
+
const MAX_OPEN = 20;
|
|
5
|
+
|
|
6
|
+
export interface TerminalGrant {
|
|
7
|
+
subject: string;
|
|
8
|
+
session: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface Pending extends TerminalGrant {
|
|
12
|
+
expiresAt: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const pending = new Map<string, Pending>();
|
|
16
|
+
|
|
17
|
+
function prune(now: number): void {
|
|
18
|
+
for (const [ticket, entry] of pending) if (entry.expiresAt <= now) pending.delete(ticket);
|
|
19
|
+
while (pending.size >= MAX_OPEN) {
|
|
20
|
+
const oldest = pending.keys().next().value;
|
|
21
|
+
if (oldest === undefined) break;
|
|
22
|
+
pending.delete(oldest);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function mintTerminalTicket(subject: string, session: string, now = Date.now()): { ticket: string; expiresAt: number } {
|
|
27
|
+
prune(now);
|
|
28
|
+
const ticket = randomBytes(32).toString('base64url');
|
|
29
|
+
const expiresAt = now + TTL_MS;
|
|
30
|
+
pending.set(ticket, { subject, session, expiresAt });
|
|
31
|
+
return { ticket, expiresAt };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function takeTerminalTicket(ticket: string, now = Date.now()): TerminalGrant | null {
|
|
35
|
+
const entry = pending.get(ticket);
|
|
36
|
+
pending.delete(ticket);
|
|
37
|
+
return entry !== undefined && entry.expiresAt > now ? { subject: entry.subject, session: entry.session } : null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const pendingTerminalTickets = (): number => pending.size;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metro-labs/discord-bot",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/station.ts",
|
|
8
|
+
"./train": "./src/index.ts",
|
|
9
|
+
"./verify": "./src/verify.ts"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"test": "tsc --noEmit && bun test test/"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@metro-labs/core": "workspace:*",
|
|
17
|
+
"@discordjs/voice": "^0.19.2",
|
|
18
|
+
"discord.js": "^14.26.4",
|
|
19
|
+
"prism-media": "^1.3.5"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@stage-labs/config": "0.1.0-beta.2",
|
|
23
|
+
"eslint": "^10.3.0",
|
|
24
|
+
"knip": "^6.15.0",
|
|
25
|
+
"madge": "^8.0.0",
|
|
26
|
+
"typescript": "^5"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Client } from 'discord.js';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import {
|
|
5
|
+
makeAccountStore,
|
|
6
|
+
resolveAccountId,
|
|
7
|
+
} from '@metro-labs/core/stations/account-store';
|
|
8
|
+
import { Line } from '@metro-labs/core/lines';
|
|
9
|
+
import { emit } from './wire.js';
|
|
10
|
+
import { API } from './api-base.js';
|
|
11
|
+
|
|
12
|
+
const ACCOUNTS_FILE =
|
|
13
|
+
process.env.DISCORD_BOT_ACCOUNTS_FILE ??
|
|
14
|
+
join(homedir(), '.metro', 'discord-bot-accounts.json');
|
|
15
|
+
|
|
16
|
+
export interface AccountConfig {
|
|
17
|
+
id: string;
|
|
18
|
+
token: string;
|
|
19
|
+
owner?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const { loadAccounts } = makeAccountStore<AccountConfig>({
|
|
23
|
+
prefix: 'discord-bot',
|
|
24
|
+
file: ACCOUNTS_FILE,
|
|
25
|
+
validate(raw, die) {
|
|
26
|
+
const seen = new Set<string>();
|
|
27
|
+
for (const a of raw) {
|
|
28
|
+
if (!a.id) die('account missing id');
|
|
29
|
+
if (!a.token || typeof a.token !== 'string')
|
|
30
|
+
die(`account '${a.id}' missing token`);
|
|
31
|
+
if (seen.has(a.id)) die(`duplicate account id '${a.id}'`);
|
|
32
|
+
seen.add(a.id);
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export interface Account {
|
|
38
|
+
cfg: AccountConfig;
|
|
39
|
+
client: Client;
|
|
40
|
+
}
|
|
41
|
+
export const accounts = new Map<string, Account>();
|
|
42
|
+
|
|
43
|
+
function restBody(
|
|
44
|
+
body: unknown,
|
|
45
|
+
isForm: boolean,
|
|
46
|
+
): RequestInit['body'] | undefined {
|
|
47
|
+
if (body === undefined) return undefined;
|
|
48
|
+
return isForm ? (body as RequestInit['body']) : JSON.stringify(body);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function restResult<T>(res: Response): Promise<T> {
|
|
52
|
+
if (res.status === 204) return undefined as T;
|
|
53
|
+
const ctype = res.headers.get('content-type') ?? '';
|
|
54
|
+
if (ctype.includes('application/json')) return res.json() as Promise<T>;
|
|
55
|
+
return res.arrayBuffer().then((b) => Buffer.from(b) as unknown as T);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function rest<T = unknown>(
|
|
59
|
+
accountId: string,
|
|
60
|
+
method: string,
|
|
61
|
+
path: string,
|
|
62
|
+
body?: unknown,
|
|
63
|
+
isForm = false,
|
|
64
|
+
): Promise<T> {
|
|
65
|
+
const acct = accounts.get(accountId);
|
|
66
|
+
if (!acct) throw new Error(`unknown account '${accountId}'`);
|
|
67
|
+
const headers: Record<string, string> = {
|
|
68
|
+
Authorization: `Bot ${acct.cfg.token}`,
|
|
69
|
+
'User-Agent': 'metro-discord-bot-train (https://github.com/bonustrack/stage)',
|
|
70
|
+
};
|
|
71
|
+
if (body !== undefined && !isForm)
|
|
72
|
+
headers['Content-Type'] = 'application/json';
|
|
73
|
+
emit({ op: 'log', text: `discord-bot[${accountId}] api ${method} ${path}` });
|
|
74
|
+
const res = await fetch(`${API}${path}`, {
|
|
75
|
+
method,
|
|
76
|
+
headers,
|
|
77
|
+
body: restBody(body, isForm),
|
|
78
|
+
signal: AbortSignal.timeout(30_000),
|
|
79
|
+
});
|
|
80
|
+
emit({
|
|
81
|
+
op: 'log',
|
|
82
|
+
text: `discord-bot[${accountId}] api ${method} ${path} -> ${res.status}`,
|
|
83
|
+
});
|
|
84
|
+
if (!res.ok) {
|
|
85
|
+
const text = await res.text().catch(() => '');
|
|
86
|
+
throw new Error(`discord-bot ${method} ${path}: ${res.status} ${text}`);
|
|
87
|
+
}
|
|
88
|
+
return restResult<T>(res);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function lineOf(accountId: string, channelId: string): string {
|
|
92
|
+
return `metro://discord-bot/${accountId}/${channelId}`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function parseLine(
|
|
96
|
+
line: string,
|
|
97
|
+
): { accountId: string; channelId: string } | null {
|
|
98
|
+
const p = Line.parseDiscord(line);
|
|
99
|
+
return p ? { accountId: p.accountId, channelId: p.resource } : null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function accountFor(args: { account?: string; line?: string }): string {
|
|
103
|
+
return resolveAccountId(accounts, args, (line) => parseLine(line)?.accountId);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function routeOf(
|
|
107
|
+
line: string,
|
|
108
|
+
account?: string,
|
|
109
|
+
): { accountId: string; channelId: string } {
|
|
110
|
+
const parsed = parseLine(line);
|
|
111
|
+
if (!parsed) throw new Error(`bad discord-bot line: ${line}`);
|
|
112
|
+
const accountId = account ?? parsed.accountId;
|
|
113
|
+
if (!accounts.has(accountId))
|
|
114
|
+
throw new Error(`unknown account '${accountId}' in line ${line}`);
|
|
115
|
+
return { accountId, channelId: parsed.channelId };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const encodeEmoji = (e: string): string => encodeURIComponent(e);
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ActivityType,
|
|
3
|
+
type Message,
|
|
4
|
+
type PresenceStatusData,
|
|
5
|
+
} from 'discord.js';
|
|
6
|
+
import {
|
|
7
|
+
accountFor,
|
|
8
|
+
accounts,
|
|
9
|
+
encodeEmoji,
|
|
10
|
+
lineOf,
|
|
11
|
+
rest,
|
|
12
|
+
routeOf,
|
|
13
|
+
} from './accounts.js';
|
|
14
|
+
import { emitOutbound, emitOutboundEdit, emitOutboundReact } from './format.js';
|
|
15
|
+
import { respond } from './wire.js';
|
|
16
|
+
import { normalizeDiscord } from '@metro-labs/core/stations/messaging-normalize';
|
|
17
|
+
import { assertContentLength } from '@metro-labs/core/stations/attachments';
|
|
18
|
+
import {
|
|
19
|
+
appendFiles,
|
|
20
|
+
outgoingFiles,
|
|
21
|
+
type OutgoingFile,
|
|
22
|
+
} from './send-files.js';
|
|
23
|
+
import {
|
|
24
|
+
makeStation,
|
|
25
|
+
type CallMsg,
|
|
26
|
+
type StationHandler,
|
|
27
|
+
} from '@metro-labs/core/stations/station-runtime';
|
|
28
|
+
import { joinVoice, leaveVoice, voiceDebug, voiceTranscribe } from './voice.js';
|
|
29
|
+
import { speak } from './voice-speak.js';
|
|
30
|
+
import { discordMembers } from './members.js';
|
|
31
|
+
import {
|
|
32
|
+
groupAddHandler,
|
|
33
|
+
groupCreateHandler,
|
|
34
|
+
groupRemoveHandler,
|
|
35
|
+
} from './group-actions.js';
|
|
36
|
+
|
|
37
|
+
async function sendMessage(
|
|
38
|
+
accountId: string,
|
|
39
|
+
channel: string,
|
|
40
|
+
body: Record<string, unknown>,
|
|
41
|
+
files: OutgoingFile[],
|
|
42
|
+
): Promise<{ id: string; delivered: string[] }> {
|
|
43
|
+
if (files.length === 0) {
|
|
44
|
+
const plain = await rest<{ id: string }>(
|
|
45
|
+
accountId,
|
|
46
|
+
'POST',
|
|
47
|
+
`/channels/${channel}/messages`,
|
|
48
|
+
body,
|
|
49
|
+
);
|
|
50
|
+
return { id: plain.id, delivered: [] };
|
|
51
|
+
}
|
|
52
|
+
const form = new FormData();
|
|
53
|
+
form.append('payload_json', JSON.stringify(body));
|
|
54
|
+
const delivered = await appendFiles(form, files);
|
|
55
|
+
const res = await rest<{ id: string }>(
|
|
56
|
+
accountId,
|
|
57
|
+
'POST',
|
|
58
|
+
`/channels/${channel}/messages`,
|
|
59
|
+
form,
|
|
60
|
+
true,
|
|
61
|
+
);
|
|
62
|
+
return { id: res.id, delivered };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type { CallMsg };
|
|
66
|
+
|
|
67
|
+
async function send(id: string, args: Record<string, unknown>): Promise<void> {
|
|
68
|
+
const {
|
|
69
|
+
line,
|
|
70
|
+
text,
|
|
71
|
+
replyTo,
|
|
72
|
+
embeds,
|
|
73
|
+
stickerIds,
|
|
74
|
+
images,
|
|
75
|
+
files,
|
|
76
|
+
account,
|
|
77
|
+
attachmentKinds,
|
|
78
|
+
attachmentNames,
|
|
79
|
+
} = args as {
|
|
80
|
+
line: string;
|
|
81
|
+
text?: string;
|
|
82
|
+
replyTo?: string;
|
|
83
|
+
embeds?: unknown[];
|
|
84
|
+
stickerIds?: string[];
|
|
85
|
+
images?: string[];
|
|
86
|
+
files?: string[];
|
|
87
|
+
account?: string;
|
|
88
|
+
attachmentKinds?: string[];
|
|
89
|
+
attachmentNames?: string[];
|
|
90
|
+
};
|
|
91
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
92
|
+
const body: Record<string, unknown> = { flags: 4 };
|
|
93
|
+
if (text !== undefined) body.content = text;
|
|
94
|
+
if (replyTo) body.message_reference = { message_id: replyTo };
|
|
95
|
+
if (embeds) body.embeds = embeds;
|
|
96
|
+
if (stickerIds) body.sticker_ids = stickerIds;
|
|
97
|
+
const outgoing = outgoingFiles(
|
|
98
|
+
[...(images ?? []), ...(files ?? [])],
|
|
99
|
+
attachmentNames,
|
|
100
|
+
attachmentKinds,
|
|
101
|
+
);
|
|
102
|
+
const res = await sendMessage(accountId, channelId, body, outgoing);
|
|
103
|
+
emitOutbound(accountId, line, res.id, text ?? '', replyTo);
|
|
104
|
+
respond(id, {
|
|
105
|
+
result: {
|
|
106
|
+
messageId: res.id,
|
|
107
|
+
account: accountId,
|
|
108
|
+
...(res.delivered.length ? { attachments: res.delivered } : {}),
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function presence(id: string, args: Record<string, unknown>): void {
|
|
114
|
+
const {
|
|
115
|
+
text,
|
|
116
|
+
status = 'online',
|
|
117
|
+
account,
|
|
118
|
+
} = args as {
|
|
119
|
+
text?: string;
|
|
120
|
+
status?: PresenceStatusData;
|
|
121
|
+
account?: string;
|
|
122
|
+
};
|
|
123
|
+
const accountId = accountFor({ account });
|
|
124
|
+
const acct = accounts.get(accountId);
|
|
125
|
+
if (!acct) {
|
|
126
|
+
respond(id, { error: `unknown account '${accountId}'` });
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const client = acct.client;
|
|
130
|
+
if (!client.user) {
|
|
131
|
+
respond(id, { error: `gateway not ready for account '${accountId}'` });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
client.user.setPresence({
|
|
135
|
+
status,
|
|
136
|
+
activities: text
|
|
137
|
+
? [{ name: 'Custom Status', type: ActivityType.Custom, state: text }]
|
|
138
|
+
: [],
|
|
139
|
+
});
|
|
140
|
+
respond(id, {
|
|
141
|
+
result: { ok: true, text: text ?? null, status, account: accountId },
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const profileUrl = (userId?: string): string | null =>
|
|
146
|
+
userId === undefined ? null : `https://discord.com/users/${userId}`;
|
|
147
|
+
|
|
148
|
+
function listAccounts(id: string): void {
|
|
149
|
+
respond(id, {
|
|
150
|
+
result: {
|
|
151
|
+
accounts: [...accounts.values()].map((a) => ({
|
|
152
|
+
id: a.cfg.id,
|
|
153
|
+
handle: a.client.user?.username ?? null,
|
|
154
|
+
url: profileUrl(a.client.user?.id),
|
|
155
|
+
userId: a.client.user?.id ?? null,
|
|
156
|
+
username: a.client.user?.username ?? null,
|
|
157
|
+
owner: a.cfg.owner ?? null,
|
|
158
|
+
ready: a.client.isReady(),
|
|
159
|
+
})),
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function react(id: string, args: Record<string, unknown>): Promise<void> {
|
|
165
|
+
const { line, messageId, emoji, account } = args as {
|
|
166
|
+
line: string;
|
|
167
|
+
messageId: string;
|
|
168
|
+
emoji: string;
|
|
169
|
+
account?: string;
|
|
170
|
+
};
|
|
171
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
172
|
+
if (emoji) {
|
|
173
|
+
const e = encodeEmoji(emoji);
|
|
174
|
+
await rest(
|
|
175
|
+
accountId,
|
|
176
|
+
'PUT',
|
|
177
|
+
`/channels/${channelId}/messages/${messageId}/reactions/${e}/@me`,
|
|
178
|
+
);
|
|
179
|
+
emitOutboundReact(accountId, line, messageId, emoji);
|
|
180
|
+
} else {
|
|
181
|
+
await rest(
|
|
182
|
+
accountId,
|
|
183
|
+
'DELETE',
|
|
184
|
+
`/channels/${channelId}/messages/${messageId}/reactions/@me`,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function edit(id: string, args: Record<string, unknown>): Promise<void> {
|
|
191
|
+
const { line, messageId, text, account } = args as {
|
|
192
|
+
line: string;
|
|
193
|
+
messageId: string;
|
|
194
|
+
text: string;
|
|
195
|
+
account?: string;
|
|
196
|
+
};
|
|
197
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
198
|
+
await rest(accountId, 'PATCH', `/channels/${channelId}/messages/${messageId}`, {
|
|
199
|
+
content: text,
|
|
200
|
+
});
|
|
201
|
+
emitOutboundEdit(accountId, line, messageId, text);
|
|
202
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function remove(
|
|
206
|
+
id: string,
|
|
207
|
+
args: Record<string, unknown>,
|
|
208
|
+
): Promise<void> {
|
|
209
|
+
const { line, messageId, account } = args as {
|
|
210
|
+
line: string;
|
|
211
|
+
messageId: string;
|
|
212
|
+
account?: string;
|
|
213
|
+
};
|
|
214
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
215
|
+
await rest(accountId, 'DELETE', `/channels/${channelId}/messages/${messageId}`);
|
|
216
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function fetchMessages(
|
|
220
|
+
id: string,
|
|
221
|
+
args: Record<string, unknown>,
|
|
222
|
+
): Promise<void> {
|
|
223
|
+
const {
|
|
224
|
+
line,
|
|
225
|
+
limit = 20,
|
|
226
|
+
before,
|
|
227
|
+
account,
|
|
228
|
+
} = args as {
|
|
229
|
+
line: string;
|
|
230
|
+
limit?: number;
|
|
231
|
+
before?: string;
|
|
232
|
+
account?: string;
|
|
233
|
+
};
|
|
234
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
235
|
+
const qs = new URLSearchParams({
|
|
236
|
+
limit: String(limit),
|
|
237
|
+
...(before ? { before } : {}),
|
|
238
|
+
});
|
|
239
|
+
const msgs = await rest<Message[]>(
|
|
240
|
+
accountId,
|
|
241
|
+
'GET',
|
|
242
|
+
`/channels/${channelId}/messages?${qs}`,
|
|
243
|
+
);
|
|
244
|
+
respond(id, { result: { messages: msgs, account: accountId } });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function download(
|
|
248
|
+
id: string,
|
|
249
|
+
args: Record<string, unknown>,
|
|
250
|
+
): Promise<void> {
|
|
251
|
+
const {
|
|
252
|
+
line,
|
|
253
|
+
messageId,
|
|
254
|
+
outDir = '/tmp',
|
|
255
|
+
account,
|
|
256
|
+
} = args as {
|
|
257
|
+
line: string;
|
|
258
|
+
messageId: string;
|
|
259
|
+
outDir?: string;
|
|
260
|
+
account?: string;
|
|
261
|
+
};
|
|
262
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
263
|
+
const msg = await rest<{
|
|
264
|
+
attachments: { url: string; content_type?: string; filename: string }[];
|
|
265
|
+
}>(accountId, 'GET', `/channels/${channelId}/messages/${messageId}`);
|
|
266
|
+
const files: { path: string; mediaType: string }[] = [];
|
|
267
|
+
for (const att of msg.attachments) {
|
|
268
|
+
const res = await fetch(att.url, { signal: AbortSignal.timeout(60_000) });
|
|
269
|
+
if (!res.ok)
|
|
270
|
+
throw new Error(`discord-bot download ${res.status} for ${att.url}`);
|
|
271
|
+
assertContentLength(res.headers.get('content-length'));
|
|
272
|
+
const buf = await res.arrayBuffer();
|
|
273
|
+
const path = `${outDir}/${messageId}-${att.filename}`;
|
|
274
|
+
await Bun.write(path, buf);
|
|
275
|
+
files.push({
|
|
276
|
+
path,
|
|
277
|
+
mediaType: att.content_type ?? 'application/octet-stream',
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
respond(id, { result: { files, account: accountId } });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function threadCreate(
|
|
284
|
+
id: string,
|
|
285
|
+
args: Record<string, unknown>,
|
|
286
|
+
): Promise<void> {
|
|
287
|
+
const {
|
|
288
|
+
line,
|
|
289
|
+
messageId,
|
|
290
|
+
name,
|
|
291
|
+
autoArchiveDuration = 1440,
|
|
292
|
+
account,
|
|
293
|
+
} = args as {
|
|
294
|
+
line: string;
|
|
295
|
+
messageId?: string;
|
|
296
|
+
name: string;
|
|
297
|
+
autoArchiveDuration?: number;
|
|
298
|
+
account?: string;
|
|
299
|
+
};
|
|
300
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
301
|
+
const path = messageId
|
|
302
|
+
? `/channels/${channelId}/messages/${messageId}/threads`
|
|
303
|
+
: `/channels/${channelId}/threads`;
|
|
304
|
+
const res = await rest<{ id: string }>(accountId, 'POST', path, {
|
|
305
|
+
name,
|
|
306
|
+
auto_archive_duration: autoArchiveDuration,
|
|
307
|
+
});
|
|
308
|
+
respond(id, {
|
|
309
|
+
result: {
|
|
310
|
+
threadId: res.id,
|
|
311
|
+
line: lineOf(accountId, res.id),
|
|
312
|
+
account: accountId,
|
|
313
|
+
},
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function pin(id: string, args: Record<string, unknown>): Promise<void> {
|
|
318
|
+
const { line, messageId, account } = args as {
|
|
319
|
+
line: string;
|
|
320
|
+
messageId: string;
|
|
321
|
+
account?: string;
|
|
322
|
+
};
|
|
323
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
324
|
+
await rest(accountId, 'PUT', `/channels/${channelId}/pins/${messageId}`);
|
|
325
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async function typing(id: string, args: Record<string, unknown>): Promise<void> {
|
|
329
|
+
const { line, account } = args as { line: string; account?: string };
|
|
330
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
331
|
+
await rest(accountId, 'POST', `/channels/${channelId}/typing`);
|
|
332
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async function channel(
|
|
336
|
+
id: string,
|
|
337
|
+
args: Record<string, unknown>,
|
|
338
|
+
): Promise<void> {
|
|
339
|
+
const { line, account } = args as { line: string; account?: string };
|
|
340
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
341
|
+
const res = await rest(accountId, 'GET', `/channels/${channelId}`);
|
|
342
|
+
respond(id, { result: res });
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async function listMembers(
|
|
346
|
+
id: string,
|
|
347
|
+
args: Record<string, unknown>,
|
|
348
|
+
): Promise<void> {
|
|
349
|
+
const { line, limit, account } = args as {
|
|
350
|
+
line: string;
|
|
351
|
+
limit?: number;
|
|
352
|
+
account?: string;
|
|
353
|
+
};
|
|
354
|
+
const { accountId, channelId } = routeOf(line, account);
|
|
355
|
+
const result = await discordMembers(accountId, channelId, limit);
|
|
356
|
+
respond(id, { result });
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const HANDLERS: Record<string, StationHandler> = {
|
|
360
|
+
accounts: (id) => {
|
|
361
|
+
listAccounts(id);
|
|
362
|
+
},
|
|
363
|
+
groupCreate: groupCreateHandler,
|
|
364
|
+
groupAddMembers: groupAddHandler,
|
|
365
|
+
groupRemoveMembers: groupRemoveHandler,
|
|
366
|
+
send,
|
|
367
|
+
react,
|
|
368
|
+
edit,
|
|
369
|
+
delete: remove,
|
|
370
|
+
fetch: fetchMessages,
|
|
371
|
+
download,
|
|
372
|
+
thread_create: threadCreate,
|
|
373
|
+
pin,
|
|
374
|
+
typing,
|
|
375
|
+
channel,
|
|
376
|
+
listMembers,
|
|
377
|
+
set_presence: presence,
|
|
378
|
+
joinVoice,
|
|
379
|
+
leaveVoice,
|
|
380
|
+
speak,
|
|
381
|
+
voiceDebug,
|
|
382
|
+
voiceTranscribe,
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
export const handleCall = makeStation({
|
|
386
|
+
handlers: HANDLERS,
|
|
387
|
+
normalize: normalizeDiscord,
|
|
388
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const API = 'https://discord.com/api/v10';
|