@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,292 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
2
|
+
import {
|
|
3
|
+
EndBehaviorType,
|
|
4
|
+
type VoiceConnection,
|
|
5
|
+
type VoiceReceiver,
|
|
6
|
+
} from '@discordjs/voice';
|
|
7
|
+
import type { Client } from 'discord.js';
|
|
8
|
+
import { spawn, type ChildProcess } from 'node:child_process';
|
|
9
|
+
import {
|
|
10
|
+
createWriteStream,
|
|
11
|
+
existsSync,
|
|
12
|
+
mkdtempSync,
|
|
13
|
+
readFileSync,
|
|
14
|
+
rmSync,
|
|
15
|
+
} from 'node:fs';
|
|
16
|
+
import { tmpdir } from 'node:os';
|
|
17
|
+
import { join } from 'node:path';
|
|
18
|
+
import { pipeline } from 'node:stream/promises';
|
|
19
|
+
import prism from 'prism-media';
|
|
20
|
+
import { emitInbound } from './format.js';
|
|
21
|
+
import { mintId } from './wire.js';
|
|
22
|
+
|
|
23
|
+
const WHISPER_BIN = process.env.WHISPER_CLI ?? 'whisper-cli';
|
|
24
|
+
const WHISPER_MODEL =
|
|
25
|
+
process.env.WHISPER_MODEL ??
|
|
26
|
+
join(process.env.HOME ?? '', '.whisper-models', 'ggml-base.en.bin');
|
|
27
|
+
const FFMPEG_BIN = process.env.FFMPEG_BIN ?? 'ffmpeg';
|
|
28
|
+
const MIN_PCM_BYTES = 0.5 * 48000 * 2 * 2;
|
|
29
|
+
const TRANSCRIBE_TIMEOUT_MS = 120_000;
|
|
30
|
+
|
|
31
|
+
function killAfter(p: ChildProcess, ms: number): () => void {
|
|
32
|
+
const t = setTimeout(() => {
|
|
33
|
+
p.kill('SIGKILL');
|
|
34
|
+
}, ms);
|
|
35
|
+
return () => {
|
|
36
|
+
clearTimeout(t);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Session {
|
|
41
|
+
receiver: VoiceReceiver;
|
|
42
|
+
accountId: string;
|
|
43
|
+
guildId: string;
|
|
44
|
+
channelId: string;
|
|
45
|
+
client: Client;
|
|
46
|
+
active: Set<string>;
|
|
47
|
+
tmp: string;
|
|
48
|
+
enabled: boolean;
|
|
49
|
+
onStart?: (userId: string) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const sessions = new Map<string, Session>();
|
|
53
|
+
|
|
54
|
+
function run(bin: string, args: string[]): Promise<number> {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const p = spawn(bin, args, { stdio: ['ignore', 'ignore', 'ignore'] });
|
|
57
|
+
const cancel = killAfter(p, TRANSCRIBE_TIMEOUT_MS);
|
|
58
|
+
p.on('error', (e) => {
|
|
59
|
+
cancel();
|
|
60
|
+
reject(e);
|
|
61
|
+
});
|
|
62
|
+
p.on('close', (code) => {
|
|
63
|
+
cancel();
|
|
64
|
+
resolve(code ?? 0);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function captureUtterance(
|
|
70
|
+
s: Session,
|
|
71
|
+
userId: string,
|
|
72
|
+
pcmPath: string,
|
|
73
|
+
): Promise<number> {
|
|
74
|
+
const opus = s.receiver.subscribe(userId, {
|
|
75
|
+
end: { behavior: EndBehaviorType.AfterSilence, duration: 1000 },
|
|
76
|
+
});
|
|
77
|
+
const decoder = new prism.opus.Decoder({
|
|
78
|
+
rate: 48000,
|
|
79
|
+
channels: 2,
|
|
80
|
+
frameSize: 960,
|
|
81
|
+
});
|
|
82
|
+
let bytes = 0;
|
|
83
|
+
decoder.on('data', (c: Buffer) => {
|
|
84
|
+
bytes += c.length;
|
|
85
|
+
});
|
|
86
|
+
const out = createWriteStream(pcmPath);
|
|
87
|
+
await pipeline(opus, decoder, out);
|
|
88
|
+
return bytes;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function pcmToWav(pcmPath: string, wavPath: string): Promise<void> {
|
|
92
|
+
await run(FFMPEG_BIN, [
|
|
93
|
+
'-hide_banner',
|
|
94
|
+
'-loglevel',
|
|
95
|
+
'error',
|
|
96
|
+
'-y',
|
|
97
|
+
'-f',
|
|
98
|
+
's16le',
|
|
99
|
+
'-ar',
|
|
100
|
+
'48000',
|
|
101
|
+
'-ac',
|
|
102
|
+
'2',
|
|
103
|
+
'-i',
|
|
104
|
+
pcmPath,
|
|
105
|
+
'-ar',
|
|
106
|
+
'16000',
|
|
107
|
+
'-ac',
|
|
108
|
+
'1',
|
|
109
|
+
wavPath,
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function transcribe(wavPath: string): Promise<string> {
|
|
114
|
+
const ofBase = wavPath.replace(/\.wav$/, '');
|
|
115
|
+
return new Promise((resolve) => {
|
|
116
|
+
const p = spawn(
|
|
117
|
+
WHISPER_BIN,
|
|
118
|
+
[
|
|
119
|
+
'-m',
|
|
120
|
+
WHISPER_MODEL,
|
|
121
|
+
'-f',
|
|
122
|
+
wavPath,
|
|
123
|
+
'-otxt',
|
|
124
|
+
'-of',
|
|
125
|
+
ofBase,
|
|
126
|
+
'-np',
|
|
127
|
+
'-nt',
|
|
128
|
+
],
|
|
129
|
+
{ stdio: ['ignore', 'pipe', 'ignore'] },
|
|
130
|
+
);
|
|
131
|
+
const cancel = killAfter(p, TRANSCRIBE_TIMEOUT_MS);
|
|
132
|
+
let out = '';
|
|
133
|
+
p.stdout.on('data', (d: Buffer) => {
|
|
134
|
+
out += d.toString();
|
|
135
|
+
});
|
|
136
|
+
p.on('error', () => {
|
|
137
|
+
cancel();
|
|
138
|
+
resolve('');
|
|
139
|
+
});
|
|
140
|
+
p.on('close', () => {
|
|
141
|
+
cancel();
|
|
142
|
+
let text = out.trim();
|
|
143
|
+
if (!text && existsSync(`${ofBase}.txt`)) {
|
|
144
|
+
try {
|
|
145
|
+
text = readFileSync(`${ofBase}.txt`, 'utf8').trim();
|
|
146
|
+
} catch {
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
resolve(text);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const NOISE = new Set([
|
|
155
|
+
'',
|
|
156
|
+
'[blank_audio]',
|
|
157
|
+
'[silence]',
|
|
158
|
+
'[ Silence ]',
|
|
159
|
+
'(silence)',
|
|
160
|
+
'you',
|
|
161
|
+
'Thank you.',
|
|
162
|
+
]);
|
|
163
|
+
|
|
164
|
+
function speakerName(s: Session, userId: string): string {
|
|
165
|
+
const member = s.client.guilds.cache
|
|
166
|
+
.get(s.guildId)
|
|
167
|
+
?.members.cache.get(userId);
|
|
168
|
+
return member?.displayName ?? member?.user.username ?? userId;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function emitTranscript(
|
|
172
|
+
s: Session,
|
|
173
|
+
userId: string,
|
|
174
|
+
stamp: string,
|
|
175
|
+
text: string,
|
|
176
|
+
): void {
|
|
177
|
+
emitInbound(s.accountId, {
|
|
178
|
+
kind: 'inbound',
|
|
179
|
+
id: mintId(),
|
|
180
|
+
ts: new Date().toISOString(),
|
|
181
|
+
station: 'discord-bot',
|
|
182
|
+
line: `metro://discord-bot/${s.accountId}/voice/${s.channelId}`,
|
|
183
|
+
line_name: 'voice',
|
|
184
|
+
from: `metro://discord-bot/${s.accountId}/user/${userId}`,
|
|
185
|
+
from_name: speakerName(s, userId),
|
|
186
|
+
message_id: stamp,
|
|
187
|
+
text,
|
|
188
|
+
is_private: false,
|
|
189
|
+
payload: {
|
|
190
|
+
kind: 'voice_transcript',
|
|
191
|
+
channel_id: s.channelId,
|
|
192
|
+
user_id: userId,
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function cleanupFiles(pcmPath: string, wavPath: string): void {
|
|
198
|
+
for (const f of [pcmPath, wavPath, `${wavPath.replace(/\.wav$/, '')}.txt`]) {
|
|
199
|
+
try {
|
|
200
|
+
rmSync(f, { force: true });
|
|
201
|
+
} catch {
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function transcribeUtterance(
|
|
207
|
+
s: Session,
|
|
208
|
+
userId: string,
|
|
209
|
+
pcmPath: string,
|
|
210
|
+
wavPath: string,
|
|
211
|
+
stamp: string,
|
|
212
|
+
): Promise<void> {
|
|
213
|
+
const bytes = await captureUtterance(s, userId, pcmPath);
|
|
214
|
+
if (bytes < MIN_PCM_BYTES) return;
|
|
215
|
+
await pcmToWav(pcmPath, wavPath);
|
|
216
|
+
const text = await transcribe(wavPath);
|
|
217
|
+
if (NOISE.has(text) || text.length < 2) return;
|
|
218
|
+
emitTranscript(s, userId, stamp, text);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function handleSpeaker(s: Session, userId: string): Promise<void> {
|
|
222
|
+
if (!s.enabled || s.active.has(userId)) return;
|
|
223
|
+
s.active.add(userId);
|
|
224
|
+
const stamp = `${userId}_${Date.now()}`;
|
|
225
|
+
const pcmPath = join(s.tmp, `${stamp}.pcm`);
|
|
226
|
+
const wavPath = join(s.tmp, `${stamp}.wav`);
|
|
227
|
+
try {
|
|
228
|
+
await transcribeUtterance(s, userId, pcmPath, wavPath, stamp);
|
|
229
|
+
} catch (err) {
|
|
230
|
+
process.stderr.write(
|
|
231
|
+
`discord-bot voice transcribe error (${userId}): ${errMsg(err)}\n`,
|
|
232
|
+
);
|
|
233
|
+
} finally {
|
|
234
|
+
s.active.delete(userId);
|
|
235
|
+
cleanupFiles(pcmPath, wavPath);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function startTranscription(
|
|
240
|
+
guildId: string,
|
|
241
|
+
channelId: string,
|
|
242
|
+
accountId: string,
|
|
243
|
+
client: Client,
|
|
244
|
+
conn: VoiceConnection,
|
|
245
|
+
): void {
|
|
246
|
+
stopTranscription(guildId);
|
|
247
|
+
const receiver = conn.receiver;
|
|
248
|
+
const s: Session = {
|
|
249
|
+
receiver,
|
|
250
|
+
accountId,
|
|
251
|
+
guildId,
|
|
252
|
+
channelId,
|
|
253
|
+
client,
|
|
254
|
+
active: new Set(),
|
|
255
|
+
tmp: mkdtempSync(join(tmpdir(), 'metro-voice-')),
|
|
256
|
+
enabled: true,
|
|
257
|
+
};
|
|
258
|
+
s.onStart = (userId: string) => {
|
|
259
|
+
handleSpeaker(s, userId).catch((err: unknown) => {
|
|
260
|
+
process.stderr.write(
|
|
261
|
+
`discord-bot[${accountId}] voice speaker failed: ${errMsg(err)}\n`,
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
receiver.speaking.on('start', s.onStart);
|
|
266
|
+
sessions.set(guildId, s);
|
|
267
|
+
process.stderr.write(
|
|
268
|
+
`discord-bot[${accountId}] voice transcription armed on ${channelId}\n`,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function setTranscription(guildId: string, on: boolean): boolean {
|
|
273
|
+
const s = sessions.get(guildId);
|
|
274
|
+
if (!s) return false;
|
|
275
|
+
s.enabled = on;
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function stopTranscription(guildId: string): void {
|
|
280
|
+
const s = sessions.get(guildId);
|
|
281
|
+
if (!s) return;
|
|
282
|
+
try {
|
|
283
|
+
if (s.onStart) s.receiver.speaking.removeListener('start', s.onStart);
|
|
284
|
+
} catch {
|
|
285
|
+
}
|
|
286
|
+
s.enabled = false;
|
|
287
|
+
try {
|
|
288
|
+
rmSync(s.tmp, { recursive: true, force: true });
|
|
289
|
+
} catch {
|
|
290
|
+
}
|
|
291
|
+
sessions.delete(guildId);
|
|
292
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/core/log';
|
|
2
|
+
import {
|
|
3
|
+
joinVoiceChannel,
|
|
4
|
+
getVoiceConnection,
|
|
5
|
+
VoiceConnectionStatus,
|
|
6
|
+
entersState,
|
|
7
|
+
type VoiceConnection,
|
|
8
|
+
} from '@discordjs/voice';
|
|
9
|
+
import type { Client, VoiceBasedChannel } from 'discord.js';
|
|
10
|
+
import { accountFor, accounts } from './accounts.js';
|
|
11
|
+
import { respond } from './wire.js';
|
|
12
|
+
import {
|
|
13
|
+
startTranscription,
|
|
14
|
+
stopTranscription,
|
|
15
|
+
setTranscription,
|
|
16
|
+
} from './voice-transcribe.js';
|
|
17
|
+
|
|
18
|
+
const DEFAULT_USERNAME = 'bonustrack_';
|
|
19
|
+
|
|
20
|
+
function clientFor(account?: string): { accountId: string; client: Client } {
|
|
21
|
+
const accountId = accountFor({ account });
|
|
22
|
+
const acct = accounts.get(accountId);
|
|
23
|
+
if (!acct) throw new Error(`unknown account '${accountId}'`);
|
|
24
|
+
return { accountId, client: acct.client };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function voiceStateMatches(
|
|
28
|
+
vs: { id: string; member?: { user: { username?: string } } | null },
|
|
29
|
+
opts: { userId?: string; username?: string },
|
|
30
|
+
): boolean {
|
|
31
|
+
if (opts.userId && vs.id === opts.userId) return true;
|
|
32
|
+
return Boolean(
|
|
33
|
+
opts.username &&
|
|
34
|
+
vs.member?.user.username?.toLowerCase() === opts.username.toLowerCase(),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function findUserVoiceChannel(
|
|
39
|
+
client: Client,
|
|
40
|
+
opts: { userId?: string; username?: string },
|
|
41
|
+
): VoiceBasedChannel | null {
|
|
42
|
+
for (const guild of client.guilds.cache.values()) {
|
|
43
|
+
for (const vs of guild.voiceStates.cache.values()) {
|
|
44
|
+
if (!vs.channelId) continue;
|
|
45
|
+
if (voiceStateMatches(vs, opts)) return vs.channel ?? null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function resolveTarget(
|
|
52
|
+
client: Client,
|
|
53
|
+
args: {
|
|
54
|
+
guildId?: string;
|
|
55
|
+
channelId?: string;
|
|
56
|
+
userId?: string;
|
|
57
|
+
username?: string;
|
|
58
|
+
},
|
|
59
|
+
): Promise<VoiceBasedChannel> {
|
|
60
|
+
if (args.channelId) {
|
|
61
|
+
const ch = await client.channels.fetch(args.channelId);
|
|
62
|
+
if (!ch || !('guild' in ch) || !ch.isVoiceBased()) {
|
|
63
|
+
throw new Error(`channel ${args.channelId} is not a voice channel`);
|
|
64
|
+
}
|
|
65
|
+
return ch;
|
|
66
|
+
}
|
|
67
|
+
const ch = findUserVoiceChannel(client, {
|
|
68
|
+
userId: args.userId,
|
|
69
|
+
username: args.userId ? undefined : (args.username ?? DEFAULT_USERNAME),
|
|
70
|
+
});
|
|
71
|
+
if (!ch) {
|
|
72
|
+
const who = args.userId ?? args.username ?? DEFAULT_USERNAME;
|
|
73
|
+
throw new Error(
|
|
74
|
+
`could not find a voice channel for '${who}' — ` +
|
|
75
|
+
'is the user connected to voice in a guild the bot shares, and is the ' +
|
|
76
|
+
'GuildVoiceStates intent enabled? (full daemon restart needed after intent change)',
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return ch;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function joinVoice(
|
|
83
|
+
id: string,
|
|
84
|
+
rawArgs: Record<string, unknown>,
|
|
85
|
+
): Promise<void> {
|
|
86
|
+
const args = rawArgs as {
|
|
87
|
+
account?: string;
|
|
88
|
+
guildId?: string;
|
|
89
|
+
channelId?: string;
|
|
90
|
+
userId?: string;
|
|
91
|
+
username?: string;
|
|
92
|
+
};
|
|
93
|
+
const { accountId, client } = clientFor(args.account);
|
|
94
|
+
if (!client.isReady()) {
|
|
95
|
+
respond(id, { error: `gateway not ready for '${accountId}'` });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const channel = await resolveTarget(client, args);
|
|
100
|
+
const guild = channel.guild;
|
|
101
|
+
|
|
102
|
+
const connection: VoiceConnection = joinVoiceChannel({
|
|
103
|
+
channelId: channel.id,
|
|
104
|
+
guildId: guild.id,
|
|
105
|
+
adapterCreator: guild.voiceAdapterCreator,
|
|
106
|
+
selfDeaf: false,
|
|
107
|
+
selfMute: true,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
await entersState(connection, VoiceConnectionStatus.Ready, 15_000);
|
|
112
|
+
} catch (err) {
|
|
113
|
+
connection.destroy();
|
|
114
|
+
throw new Error(
|
|
115
|
+
`voice connection failed to become Ready: ${errMsg(err)}`,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
startTranscription(guild.id, channel.id, accountId, client, connection);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
process.stderr.write(
|
|
123
|
+
`voice transcription arm failed: ${errMsg(err)}\n`,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
respond(id, {
|
|
128
|
+
result: {
|
|
129
|
+
ok: true,
|
|
130
|
+
account: accountId,
|
|
131
|
+
guildId: guild.id,
|
|
132
|
+
guildName: guild.name,
|
|
133
|
+
channelId: channel.id,
|
|
134
|
+
channelName: channel.name,
|
|
135
|
+
status: connection.state.status,
|
|
136
|
+
transcribing: true,
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function voiceDebug(id: string, rawArgs: Record<string, unknown>): void {
|
|
142
|
+
const args = rawArgs as { account?: string };
|
|
143
|
+
const { accountId, client } = clientFor(args.account);
|
|
144
|
+
const guilds = [...client.guilds.cache.values()].map((g) => {
|
|
145
|
+
const voiceChannels = [...g.channels.cache.values()]
|
|
146
|
+
.filter((c) => c.isVoiceBased())
|
|
147
|
+
.map((c) => ({ id: c.id, name: c.name }));
|
|
148
|
+
const occupants = [...g.voiceStates.cache.values()]
|
|
149
|
+
.filter((vs) => vs.channelId)
|
|
150
|
+
.map((vs) => ({
|
|
151
|
+
channelId: vs.channelId,
|
|
152
|
+
channelName: vs.channel?.name ?? null,
|
|
153
|
+
userId: vs.id,
|
|
154
|
+
username: vs.member?.user.username ?? null,
|
|
155
|
+
}));
|
|
156
|
+
return { id: g.id, name: g.name, voiceChannels, occupants };
|
|
157
|
+
});
|
|
158
|
+
respond(id, {
|
|
159
|
+
result: { ok: true, account: accountId, ready: client.isReady(), guilds },
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function leaveVoice(id: string, rawArgs: Record<string, unknown>): void {
|
|
164
|
+
const args = rawArgs as { account?: string; guildId?: string };
|
|
165
|
+
const { accountId, client } = clientFor(args.account);
|
|
166
|
+
|
|
167
|
+
const guildIds = args.guildId
|
|
168
|
+
? [args.guildId]
|
|
169
|
+
: [...client.guilds.cache.keys()];
|
|
170
|
+
const left: string[] = [];
|
|
171
|
+
for (const gid of guildIds) {
|
|
172
|
+
const conn = getVoiceConnection(gid);
|
|
173
|
+
if (conn) {
|
|
174
|
+
stopTranscription(gid);
|
|
175
|
+
conn.destroy();
|
|
176
|
+
left.push(gid);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
respond(id, { result: { ok: true, account: accountId, leftGuilds: left } });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function voiceTranscribe(
|
|
183
|
+
id: string,
|
|
184
|
+
rawArgs: Record<string, unknown>,
|
|
185
|
+
): void {
|
|
186
|
+
const args = rawArgs as { account?: string; guildId?: string; on?: boolean };
|
|
187
|
+
const { accountId, client } = clientFor(args.account);
|
|
188
|
+
const on = args.on !== false;
|
|
189
|
+
const guildIds = args.guildId
|
|
190
|
+
? [args.guildId]
|
|
191
|
+
: [...client.guilds.cache.keys()];
|
|
192
|
+
const toggled: string[] = [];
|
|
193
|
+
for (const gid of guildIds) if (setTranscription(gid, on)) toggled.push(gid);
|
|
194
|
+
respond(id, {
|
|
195
|
+
result: { ok: true, account: accountId, on, toggledGuilds: toggled },
|
|
196
|
+
});
|
|
197
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metro-labs/http",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "How a metro server authenticates and answers a request: CORS, the JSON helpers, ApiError, the signed-identity header and the identity registry. Shared by the daemon and api.metro.box, never by a station.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
"./cors": "./src/cors.ts",
|
|
9
|
+
"./api-http": "./src/api-http.ts",
|
|
10
|
+
"./api-error": "./src/api-error.ts",
|
|
11
|
+
"./signed-identity": "./src/signed-identity.ts",
|
|
12
|
+
"./identity-key": "./src/identity-key.ts",
|
|
13
|
+
"./identity-registry": "./src/identity-registry.ts",
|
|
14
|
+
"./return-to": "./src/return-to.ts",
|
|
15
|
+
"./mode-api": "./src/mode-api.ts"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"test": "tsc --noEmit && bun test test/"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@metro-labs/core": "workspace:*",
|
|
23
|
+
"@noble/curves": "^1.9.7",
|
|
24
|
+
"viem": "2.52.2"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@stage-labs/config": "0.1.0-beta.2",
|
|
28
|
+
"eslint": "^10.3.0",
|
|
29
|
+
"knip": "^6.15.0",
|
|
30
|
+
"madge": "^8.0.0",
|
|
31
|
+
"typescript": "^5"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { ApiError } from './api-error.js';
|
|
3
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
4
|
+
import { signedIdentity } from './signed-identity.js';
|
|
5
|
+
import { identitySubject } from './identity-registry.js';
|
|
6
|
+
|
|
7
|
+
const BODY_MAX = 4 * 1024;
|
|
8
|
+
|
|
9
|
+
export interface ApiSession {
|
|
10
|
+
subject: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function cors(req: IncomingMessage): Record<string, string> {
|
|
14
|
+
return {
|
|
15
|
+
'access-control-allow-origin': req.headers.origin ?? '*',
|
|
16
|
+
'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
|
17
|
+
'access-control-allow-headers': 'Authorization, Content-Type',
|
|
18
|
+
'access-control-allow-private-network': 'true',
|
|
19
|
+
'access-control-max-age': '86400',
|
|
20
|
+
vary: 'Origin',
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function sendJson(
|
|
25
|
+
req: IncomingMessage,
|
|
26
|
+
res: ServerResponse,
|
|
27
|
+
status: number,
|
|
28
|
+
body: unknown,
|
|
29
|
+
): void {
|
|
30
|
+
res.writeHead(status, {
|
|
31
|
+
'content-type': 'application/json',
|
|
32
|
+
'cache-control': 'no-store',
|
|
33
|
+
...cors(req),
|
|
34
|
+
});
|
|
35
|
+
res.end(JSON.stringify(body));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function apiSession(req: IncomingMessage): Promise<ApiSession | null> {
|
|
39
|
+
const address = await signedIdentity(req);
|
|
40
|
+
const subject = address === null ? undefined : identitySubject(address);
|
|
41
|
+
return subject === undefined ? null : { subject };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface AgentIdentity {
|
|
45
|
+
subject: string;
|
|
46
|
+
agentId: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function projectParam(req: IncomingMessage): string | null {
|
|
50
|
+
const raw = new URL(req.url ?? '/', 'http://localhost').searchParams.get(
|
|
51
|
+
'project',
|
|
52
|
+
);
|
|
53
|
+
return raw === null || raw === '' ? null : raw;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function readJsonBody(req: IncomingMessage, max = BODY_MAX): Promise<unknown> {
|
|
57
|
+
const chunks: Buffer[] = [];
|
|
58
|
+
let total = 0;
|
|
59
|
+
let over = false;
|
|
60
|
+
for await (const c of req) {
|
|
61
|
+
const buf = c as Buffer;
|
|
62
|
+
total += buf.length;
|
|
63
|
+
if (total > max) over = true;
|
|
64
|
+
if (!over) chunks.push(buf);
|
|
65
|
+
else if (total > 2 * max) break;
|
|
66
|
+
}
|
|
67
|
+
if (over) throw new ApiError('request body too large', 413);
|
|
68
|
+
const raw = Buffer.concat(chunks).toString('utf8').trim();
|
|
69
|
+
if (raw === '') return {};
|
|
70
|
+
try {
|
|
71
|
+
return JSON.parse(raw);
|
|
72
|
+
} catch {
|
|
73
|
+
throw new ApiError('body must be JSON', 400);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function apiFailure(
|
|
78
|
+
req: IncomingMessage,
|
|
79
|
+
res: ServerResponse,
|
|
80
|
+
err: unknown,
|
|
81
|
+
label = 'agent-api',
|
|
82
|
+
): void {
|
|
83
|
+
if (err instanceof ApiError) {
|
|
84
|
+
sendJson(req, res, err.status, { error: err.message });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
log.warn({ err: errMsg(err) }, `${label}: request failed`);
|
|
88
|
+
if (!res.headersSent) sendJson(req, res, 500, { error: `${label} failed` });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function bodyField(body: unknown, key: string): unknown {
|
|
92
|
+
return typeof body === 'object' && body !== null && !Array.isArray(body)
|
|
93
|
+
? (body as Record<string, unknown>)[key]
|
|
94
|
+
: undefined;
|
|
95
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
|
|
3
|
+
export function applyMcpCors(
|
|
4
|
+
req: IncomingMessage,
|
|
5
|
+
res: ServerResponse,
|
|
6
|
+
): void {
|
|
7
|
+
const origin = req.headers.origin;
|
|
8
|
+
res.setHeader(
|
|
9
|
+
'Access-Control-Allow-Origin',
|
|
10
|
+
origin !== undefined && origin !== '' ? origin : '*',
|
|
11
|
+
);
|
|
12
|
+
res.setHeader('Vary', 'Origin');
|
|
13
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
14
|
+
res.setHeader(
|
|
15
|
+
'Access-Control-Allow-Headers',
|
|
16
|
+
'Authorization, Content-Type, Accept, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID',
|
|
17
|
+
);
|
|
18
|
+
res.setHeader('Access-Control-Expose-Headers', 'Mcp-Session-Id');
|
|
19
|
+
res.setHeader('Access-Control-Max-Age', '86400');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function handleMcpPreflight(
|
|
23
|
+
req: IncomingMessage,
|
|
24
|
+
res: ServerResponse,
|
|
25
|
+
): boolean {
|
|
26
|
+
if (req.method !== 'OPTIONS') return false;
|
|
27
|
+
res.writeHead(204).end();
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { hkdfSync } from 'node:crypto';
|
|
2
|
+
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
3
|
+
import { mapHashToField } from '@noble/curves/abstract/modular';
|
|
4
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
5
|
+
|
|
6
|
+
export const ENCRYPTION_KEY_TYPED_DATA = {
|
|
7
|
+
domain: { name: 'metro', version: '1' },
|
|
8
|
+
types: {
|
|
9
|
+
EncryptionKey: [
|
|
10
|
+
{ name: 'purpose', type: 'string' },
|
|
11
|
+
{ name: 'keyVersion', type: 'uint256' },
|
|
12
|
+
],
|
|
13
|
+
},
|
|
14
|
+
primaryType: 'EncryptionKey',
|
|
15
|
+
message: { purpose: 'encryption-key', keyVersion: 1n },
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
const KEY_SALT = 'metro';
|
|
19
|
+
const IDENTITY_INFO = 'vault-secp256k1';
|
|
20
|
+
const KEY_HASH_BYTES = 48;
|
|
21
|
+
|
|
22
|
+
const toHex = (bytes: Uint8Array): `0x${string}` =>
|
|
23
|
+
`0x${[...bytes].map((b) => b.toString(16).padStart(2, '0')).join('')}`;
|
|
24
|
+
|
|
25
|
+
export function deriveIdentityKey(signature: `0x${string}`): `0x${string}` {
|
|
26
|
+
const ikm = Buffer.from(signature.slice(2), 'hex');
|
|
27
|
+
const hash = new Uint8Array(hkdfSync('sha256', ikm, KEY_SALT, IDENTITY_INFO, KEY_HASH_BYTES));
|
|
28
|
+
return toHex(mapHashToField(hash, secp256k1.CURVE.n));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function deriveIdentityAddress(signature: `0x${string}`): string {
|
|
32
|
+
return privateKeyToAccount(deriveIdentityKey(signature)).address.toLowerCase();
|
|
33
|
+
}
|