@stage-labs/metro 0.1.0-beta.23 → 0.1.0-beta.231
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 +9 -101
- package/dist/background.js +123 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +197 -0
- package/dist/cli.js +52 -110
- package/dist/control.js +19 -103
- package/dist/hold.js +210 -0
- package/dist/local.js +43 -0
- package/dist/mcp-config.js +25 -0
- package/dist/memory.js +222 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +44 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +58 -0
- package/dist/runtime-install.js +96 -0
- package/dist/runtime.js +29 -52
- package/dist/serve.js +157 -0
- package/dist/service.js +304 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +13 -1
- package/package.json +2 -23
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +17 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/MEMORY.md +78 -0
- package/runtime/marketplace/plugin/METRO.md +26 -0
- package/runtime/marketplace/plugin/STAGE.md +42 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +71 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +30 -0
- package/runtime/marketplace/plugin/bin/policy.mjs +97 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/node_modules/@metro-labs/core/package.json +51 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/events.ts +11 -7
- package/runtime/node_modules/@metro-labs/{mcp/src/db → core/src}/ids.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/stations → core/src}/lines.ts +22 -26
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/protocol.ts +7 -4
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/secure-fs.ts +17 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +14 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +83 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/attachments.ts +0 -1
- package/runtime/node_modules/@metro-labs/core/src/stations/mail-text.ts +72 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/mail-trust.ts +29 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/mail.ts +212 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +16 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/oauth.ts +126 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/station-runtime.ts +6 -11
- package/runtime/node_modules/@metro-labs/core/src/stations/train-boot.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-events.ts +84 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/types.ts +18 -1
- package/runtime/node_modules/@metro-labs/core/src/tickets.ts +37 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/agent-fs.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/crontab.ts +44 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files-api.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files.ts +89 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/home-fs.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/job-env.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +80 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules.ts +144 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +87 -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-lookups.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +76 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +357 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +30 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +59 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +305 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +142 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/db/key-map.ts → daemon/src/agents/keys.ts} +1 -11
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +133 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/db/agent-scope.ts → daemon/src/agents/scope.ts} +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +94 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/api.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/preview.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +36 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/boot}/crash-guard.ts +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +21 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/boot}/paths.ts +6 -34
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +26 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/inbound.ts +75 -45
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/media-note.ts +11 -7
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/pending.ts +4 -1
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/relay.ts +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +380 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +359 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory-routine.ts +114 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +295 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +346 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/usage-probe.ts +127 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +113 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-api.ts → daemon/src/connectors/api.ts} +79 -113
- package/runtime/node_modules/@metro-labs/{mcp/src/db/connector-config.ts → daemon/src/connectors/config.ts} +67 -41
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/health.ts +18 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/connectors}/oauth-client.ts +14 -17
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/connectors}/oauth-discovery.ts +33 -20
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth-routes.ts → daemon/src/connectors/oauth-routes.ts} +30 -32
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth.ts → daemon/src/connectors/oauth.ts} +61 -22
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +347 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +385 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +81 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/transfer-api.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/transfer-crypto.ts +44 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/transfer-store.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +106 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-grant.ts +4 -4
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-owner.ts +3 -3
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-reaper.ts +174 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-serve.ts +12 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/upload-api.ts +10 -27
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/upload-store.ts +4 -4
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +233 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +214 -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 +173 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +209 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-version.ts +44 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +93 -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 +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/frames.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +307 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +160 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-client.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-setup.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-stream.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +235 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +285 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-lists.ts +57 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +104 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/pkce.ts +36 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/subscription.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/text.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/tool-search.ts +126 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage-refresh.ts +102 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +348 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/accounts.ts +28 -17
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/call-tools.ts +49 -20
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/ctx.ts +4 -4
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/event-store.ts +15 -16
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/group-tools.ts +2 -2
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/index.ts +34 -80
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +19 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/member-tools.ts +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +88 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-lookup.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/read-tool.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-slot.ts +71 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/session.ts +47 -44
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +93 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-def.ts +9 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/tool-dispatch.ts +53 -40
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/tool-schemas.ts +64 -113
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/upload-tool.ts +6 -11
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/helper-script.ts +126 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/install.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/privilege.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +316 -0
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/routes}/http.ts +63 -51
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +209 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +69 -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/cloudwatch.ts +178 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +58 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/owner.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-browser.ts +102 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-driver.ts +42 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-inline.ts +29 -1
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-interactive.ts +5 -31
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-resolve.ts +38 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/attach-session.ts +32 -22
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-xmtp.ts +8 -9
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach.ts +69 -3
- package/runtime/node_modules/@metro-labs/{mcp/src/db → daemon/src/stations}/materialize.ts +60 -179
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/registry.ts +20 -2
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor-io.ts +3 -3
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor.ts +6 -9
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/train-call.ts +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/stations/webhook-endpoints.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +124 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/api.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/config.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/firewall.ts +9 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/index.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/install.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/paths.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/proxy.ts +147 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +44 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +2 -4
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +3 -23
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +19 -187
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +2 -2
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +26 -67
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +1 -1
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +8 -14
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +2 -2
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +12 -13
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +5 -1
- package/runtime/node_modules/@metro-labs/gmail/package.json +25 -0
- package/runtime/node_modules/@metro-labs/gmail/src/accounts.ts +81 -0
- package/runtime/node_modules/@metro-labs/gmail/src/actions.ts +14 -0
- package/runtime/node_modules/@metro-labs/gmail/src/api.ts +31 -0
- package/runtime/node_modules/@metro-labs/gmail/src/attachments.ts +35 -0
- package/runtime/node_modules/@metro-labs/gmail/src/auth.ts +105 -0
- package/runtime/node_modules/@metro-labs/gmail/src/config.ts +13 -0
- package/runtime/node_modules/@metro-labs/gmail/src/inbound.ts +96 -0
- package/runtime/node_modules/@metro-labs/gmail/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/gmail/src/login.ts +37 -0
- package/runtime/node_modules/@metro-labs/gmail/src/message.ts +125 -0
- package/runtime/node_modules/@metro-labs/gmail/src/mime.ts +70 -0
- package/runtime/node_modules/@metro-labs/gmail/src/outbound.ts +124 -0
- package/runtime/node_modules/@metro-labs/gmail/src/query.ts +99 -0
- package/runtime/node_modules/@metro-labs/gmail/src/state.ts +24 -0
- package/runtime/node_modules/@metro-labs/gmail/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/gmail/src/trust.ts +42 -0
- package/runtime/node_modules/@metro-labs/http/package.json +30 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +135 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/session-api.ts → http/src/mode-api.ts} +13 -9
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/sigv4.ts +90 -0
- package/runtime/node_modules/@metro-labs/http/src/workos-token.ts +149 -0
- package/runtime/node_modules/@metro-labs/outlook/package.json +25 -0
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +78 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +14 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +81 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +120 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +42 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +23 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +59 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +55 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +96 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +129 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +31 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +103 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +84 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +28 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +41 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +1 -3
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +5 -24
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +8 -6
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +2 -2
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +19 -34
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +3 -3
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +13 -34
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +4 -43
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +2 -3
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +2 -2
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +4 -1
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +0 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +1 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +7 -41
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +22 -77
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +2 -2
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +46 -48
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +15 -15
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +6 -82
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +85 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +5 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +20 -12
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +96 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +124 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +152 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/files.ts +47 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +164 -0
- package/runtime/node_modules/@metro-labs/threema/src/groups.ts +96 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +87 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +24 -0
- package/runtime/node_modules/@metro-labs/threema/src/messages.ts +264 -0
- package/runtime/node_modules/@metro-labs/threema/src/outbound.ts +160 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +1 -1
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +5 -8
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +2 -5
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +6 -28
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +54 -11
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +2 -2
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +86 -4
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +7 -58
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +11 -28
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +4 -37
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/names.ts +75 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +44 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +16 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +5 -9
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +0 -1
- package/runtime/node_modules/@metro-labs/xmtp/package.json +3 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +10 -20
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +2 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +15 -204
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +40 -53
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +72 -220
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +27 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +11 -7
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +7 -24
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +8 -18
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +1 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +30 -16
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +12 -20
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +57 -33
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +1 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/names.ts +61 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/profile.ts +153 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/resolve.ts +44 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/sender.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/smart.ts +72 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +7 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +22 -58
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +55 -48
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +4 -6
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +19 -5
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +14 -4
- package/runtime/runtime.json +1 -1
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +38 -0
- package/dist/prompt.js +0 -61
- package/dist/store.js +0 -70
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +0 -195
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +0 -292
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +0 -197
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +0 -5
- package/runtime/node_modules/@metro-labs/mcp/package.json +0 -66
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +0 -24
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +0 -311
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +0 -341
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +0 -129
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +0 -320
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +0 -111
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair.ts +0 -20
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/collection-api.ts +0 -202
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +0 -31
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +0 -100
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +0 -57
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +0 -333
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +0 -164
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +0 -208
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +0 -79
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +0 -65
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +0 -58
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +0 -214
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +0 -125
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-pair.ts +0 -18
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +0 -105
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +0 -41
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +0 -218
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +0 -169
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +0 -91
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +0 -215
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-map.ts +0 -71
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +0 -37
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-collections.ts +0 -218
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +0 -329
- package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +0 -300
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +0 -31
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +0 -105
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +0 -131
- package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +0 -45
- package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +0 -57
- package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +0 -18
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +0 -29
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +0 -21
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +0 -66
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +0 -73
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +0 -172
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +0 -43
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +0 -240
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +0 -2
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +0 -80
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +0 -88
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +0 -1
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +0 -3
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +0 -5
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +0 -87
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +0 -55
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +0 -349
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/log.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/mcp → core/src}/str.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/train-error.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/webhook-note.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/raw-get-stream.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → http/src}/api-error.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → http/src}/cors.ts +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
4
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
5
|
+
import { agentsDir } from '../agents/files.js';
|
|
6
|
+
import { removeHome, writeHomeText } from './home-fs.js';
|
|
7
|
+
import { agentUser, agentViewDir, type AgentUser } from './user.js';
|
|
8
|
+
|
|
9
|
+
const COPIED = ['policy.json', 'claude-setup.json', 'system-prompt.md'] as const;
|
|
10
|
+
const SYNC_MS = 5_000;
|
|
11
|
+
|
|
12
|
+
const readText = (path: string): string | null => {
|
|
13
|
+
try {
|
|
14
|
+
return readFileSync(path, 'utf8');
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const readRecord = (path: string): Record<string, unknown> | null => {
|
|
21
|
+
const text = readText(path);
|
|
22
|
+
if (text === null) return null;
|
|
23
|
+
try {
|
|
24
|
+
const parsed: unknown = JSON.parse(text);
|
|
25
|
+
return isRecord(parsed) ? parsed : null;
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const json = (value: unknown): string => `${JSON.stringify(value, null, 2)}\n`;
|
|
32
|
+
|
|
33
|
+
function agentFile(dir: string): string | null {
|
|
34
|
+
const file = readRecord(join(dir, 'agent.json'));
|
|
35
|
+
if (file === null || typeof file.id !== 'string' || typeof file.key !== 'string') return null;
|
|
36
|
+
return json({ version: 1, id: file.id, key: file.key });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function modelFile(dir: string): string | null {
|
|
40
|
+
const cfg = readRecord(join(dir, 'model.json'));
|
|
41
|
+
if (cfg === null || !Array.isArray(cfg.connections)) return null;
|
|
42
|
+
const connections = cfg.connections.filter(isRecord).map((c) => ({ id: c.id, provider: c.provider, model: c.model }));
|
|
43
|
+
return json({ version: 2, route: cfg.route, connections });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function viewFiles(dir = agentsDir()): Map<string, string | null> {
|
|
47
|
+
const files = new Map<string, string | null>([
|
|
48
|
+
['agent.json', agentFile(dir)],
|
|
49
|
+
['model.json', modelFile(dir)],
|
|
50
|
+
]);
|
|
51
|
+
for (const name of COPIED) files.set(name, existsSync(join(dir, name)) ? readText(join(dir, name)) : null);
|
|
52
|
+
return files;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const written = new Map<string, string | null>();
|
|
56
|
+
|
|
57
|
+
function syncAgentView(user: AgentUser | null = agentUser(), dir = agentsDir()): number {
|
|
58
|
+
if (user === null) return 0;
|
|
59
|
+
const target = agentViewDir(user);
|
|
60
|
+
let changed = 0;
|
|
61
|
+
for (const [name, text] of viewFiles(dir)) {
|
|
62
|
+
const path = join(target, name);
|
|
63
|
+
if (written.has(path) && written.get(path) === text) continue;
|
|
64
|
+
if (text === null) removeHome(path, false, user);
|
|
65
|
+
else writeHomeText(path, text, 0o600, undefined, user);
|
|
66
|
+
written.set(path, text);
|
|
67
|
+
changed += 1;
|
|
68
|
+
}
|
|
69
|
+
return changed;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let timer: ReturnType<typeof setInterval> | null = null;
|
|
73
|
+
|
|
74
|
+
export function watchAgentView(everyMs = SYNC_MS): void {
|
|
75
|
+
if (timer !== null) return;
|
|
76
|
+
const tick = (): void => {
|
|
77
|
+
try {
|
|
78
|
+
const changed = syncAgentView();
|
|
79
|
+
if (changed > 0) log.info({ changed }, 'agent-user: refreshed the Metro files the agent may read');
|
|
80
|
+
} catch (err) {
|
|
81
|
+
log.warn({ err: errMsg(err) }, 'agent-user: could not refresh the Metro files the agent may read');
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
tick();
|
|
85
|
+
timer = setInterval(tick, everyMs);
|
|
86
|
+
timer.unref();
|
|
87
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { STATIONS, type StationName } from '@metro-labs/core/station-names';
|
|
2
|
+
|
|
3
|
+
const ACCOUNT_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
4
|
+
|
|
5
|
+
export interface AccountRef {
|
|
6
|
+
agentId: string;
|
|
7
|
+
station: StationName;
|
|
8
|
+
accountId: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function isStationName(raw: unknown): raw is StationName {
|
|
12
|
+
return (
|
|
13
|
+
typeof raw === 'string' && (STATIONS as readonly string[]).includes(raw)
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function parseAccountId(raw: string): string | null {
|
|
18
|
+
return ACCOUNT_ID_RE.test(raw) ? raw : null;
|
|
19
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import type { StationName } from '@metro-labs/core/station-names';
|
|
3
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
4
|
+
import { bodyField, readJsonBody, sendJson } from '@metro-labs/http/api-http';
|
|
5
|
+
import { stationByName } from '../stations/registry.js';
|
|
6
|
+
import type { AccountRoute } from './account-routes.js';
|
|
7
|
+
import { handleSenderCards, type SenderCardDeps } from './sender-cards.js';
|
|
8
|
+
|
|
9
|
+
interface Target {
|
|
10
|
+
station: StationName;
|
|
11
|
+
accountId: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface LookupDeps extends SenderCardDeps {
|
|
15
|
+
resolveSender: (station: StationName, accountId: string, query: string) => Promise<unknown>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type Lookup = (req: IncomingMessage, res: ServerResponse, deps: LookupDeps, target: Target) => Promise<void>;
|
|
19
|
+
|
|
20
|
+
function lookupQuery(req: IncomingMessage): string {
|
|
21
|
+
const params = new URLSearchParams((req.url ?? '').split('?')[1] ?? '');
|
|
22
|
+
return (params.get('q') ?? '').trim();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function handleResolve(req: IncomingMessage, res: ServerResponse, deps: LookupDeps, target: Target): Promise<void> {
|
|
26
|
+
if (stationByName(target.station)?.resolvesSenders !== true)
|
|
27
|
+
throw new ApiError(`metro cannot look a sender up on ${target.station}`, 400);
|
|
28
|
+
const query = lookupQuery(req);
|
|
29
|
+
if (query === '') throw new ApiError('something to look up is required', 400);
|
|
30
|
+
sendJson(req, res, 200, await deps.resolveSender(target.station, target.accountId, query));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function handleName(req: IncomingMessage, res: ServerResponse, deps: LookupDeps, target: Target): Promise<void> {
|
|
34
|
+
if (stationByName(target.station)?.claimsName !== true)
|
|
35
|
+
throw new ApiError(`${target.station} accounts have no name to claim`, 400);
|
|
36
|
+
if (req.method === 'GET') {
|
|
37
|
+
sendJson(req, res, 200, await deps.accountCall(target.station, 'name', { account: target.accountId }));
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const label = bodyField(await readJsonBody(req), 'label');
|
|
41
|
+
if (typeof label !== 'string' || label.trim() === '') throw new ApiError('a label is required', 400);
|
|
42
|
+
sendJson(req, res, 200, await deps.accountCall(target.station, 'claim_name', { account: target.accountId, label }));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function handleRecent(req: IncomingMessage, res: ServerResponse, deps: LookupDeps, target: Target): Promise<void> {
|
|
46
|
+
sendJson(req, res, 200, { senders: deps.recentSenders(target.station, target.accountId) });
|
|
47
|
+
return Promise.resolve();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const LOOKUPS: Partial<Record<AccountRoute['kind'], Lookup>> = {
|
|
51
|
+
profiles: handleSenderCards,
|
|
52
|
+
resolve: handleResolve,
|
|
53
|
+
name: handleName,
|
|
54
|
+
senders: handleRecent,
|
|
55
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { isStationName, parseAccountId } from './account-attach.js';
|
|
2
|
+
import type { StationName } from '@metro-labs/core/station-names';
|
|
3
|
+
import { ATTACH_ID_RE } from '../stations/attach-session.js';
|
|
4
|
+
|
|
5
|
+
export type AccountRoute =
|
|
6
|
+
| { kind: 'start' }
|
|
7
|
+
| { kind: 'session'; attachId: string }
|
|
8
|
+
| { kind: 'step'; attachId: string }
|
|
9
|
+
| { kind: 'account'; station: StationName; accountId: string }
|
|
10
|
+
| { kind: 'allowlist'; station: StationName; accountId: string }
|
|
11
|
+
| { kind: 'senders'; station: StationName; accountId: string }
|
|
12
|
+
| { kind: 'resolve'; station: StationName; accountId: string }
|
|
13
|
+
| { kind: 'enabled'; station: StationName; accountId: string }
|
|
14
|
+
| { kind: 'policy'; station: StationName; accountId: string }
|
|
15
|
+
| { kind: 'profiles'; station: StationName; accountId: string }
|
|
16
|
+
| { kind: 'name'; station: StationName; accountId: string };
|
|
17
|
+
|
|
18
|
+
const ROUTE_METHODS: Record<AccountRoute['kind'], string[]> = {
|
|
19
|
+
start: ['POST'],
|
|
20
|
+
session: ['GET', 'DELETE'],
|
|
21
|
+
step: ['POST'],
|
|
22
|
+
account: ['DELETE'],
|
|
23
|
+
allowlist: ['PUT'],
|
|
24
|
+
senders: ['GET'],
|
|
25
|
+
resolve: ['GET'],
|
|
26
|
+
enabled: ['PUT'],
|
|
27
|
+
policy: ['PUT'],
|
|
28
|
+
profiles: ['GET'],
|
|
29
|
+
name: ['GET', 'POST'],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const SUB_ROUTES = ['allowlist', 'senders', 'resolve', 'enabled', 'name', 'policy', 'profiles'] as const;
|
|
33
|
+
|
|
34
|
+
type SubRoute = (typeof SUB_ROUTES)[number];
|
|
35
|
+
|
|
36
|
+
const isSubRoute = (value: string | undefined): value is SubRoute =>
|
|
37
|
+
SUB_ROUTES.some((sub) => sub === value);
|
|
38
|
+
|
|
39
|
+
function twoSegmentRoute(head: string, tail: string): AccountRoute | null {
|
|
40
|
+
if (ATTACH_ID_RE.test(head))
|
|
41
|
+
return tail === 'step' ? { kind: 'step', attachId: head } : null;
|
|
42
|
+
if (!isStationName(head)) return null;
|
|
43
|
+
const accountId = parseAccountId(tail);
|
|
44
|
+
return accountId === null ? null : { kind: 'account', station: head, accountId };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function accountSubRoute(
|
|
48
|
+
head: string,
|
|
49
|
+
tail: string,
|
|
50
|
+
sub: string | undefined,
|
|
51
|
+
): AccountRoute | null {
|
|
52
|
+
if (!isStationName(head) || !isSubRoute(sub)) return null;
|
|
53
|
+
const accountId = parseAccountId(tail);
|
|
54
|
+
return accountId === null ? null : { kind: sub, station: head, accountId };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function accountRoute(rest: string[]): AccountRoute | null {
|
|
58
|
+
const [head, tail] = rest;
|
|
59
|
+
if (head === undefined) return null;
|
|
60
|
+
if (rest.length === 1)
|
|
61
|
+
return head === 'start'
|
|
62
|
+
? { kind: 'start' }
|
|
63
|
+
: ATTACH_ID_RE.test(head)
|
|
64
|
+
? { kind: 'session', attachId: head }
|
|
65
|
+
: null;
|
|
66
|
+
if (rest.length === 3 && tail !== undefined) return accountSubRoute(head, tail, rest[2]);
|
|
67
|
+
if (rest.length !== 2 || tail === undefined) return null;
|
|
68
|
+
return twoSegmentRoute(head, tail);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function accountRouteAllows(
|
|
72
|
+
route: AccountRoute,
|
|
73
|
+
method: string | undefined,
|
|
74
|
+
): boolean {
|
|
75
|
+
return ROUTE_METHODS[route.kind].includes(method ?? '');
|
|
76
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
3
|
+
import { normalizeAllowlist, normalizeApprovers } from './allowlist.js';
|
|
4
|
+
import { approversForAccount } from './map.js';
|
|
5
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
6
|
+
import type { RecentSender } from './senders.js';
|
|
7
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
8
|
+
import { apiFailure, bodyField, readJsonBody, sendJson } from '@metro-labs/http/api-http';
|
|
9
|
+
import { type AccountRef } from './account-attach.js';
|
|
10
|
+
import { type AccountRoute } from './account-routes.js';
|
|
11
|
+
import { handlePolicy, type SetPolicy } from './policy-route.js';
|
|
12
|
+
import { LOOKUPS } from './account-lookups.js';
|
|
13
|
+
import { unconfirmedSenders } from './sender-cards.js';
|
|
14
|
+
import { stationByName } from '../stations/registry.js';
|
|
15
|
+
import type { StationName } from '@metro-labs/core/station-names';
|
|
16
|
+
import {
|
|
17
|
+
ATTACHABLE_STATIONS,
|
|
18
|
+
attachInputOf,
|
|
19
|
+
isAttachStation,
|
|
20
|
+
type AttachInput,
|
|
21
|
+
type OneTimeSecret,
|
|
22
|
+
type PreparedAccount,
|
|
23
|
+
} from '../stations/attach.js';
|
|
24
|
+
import {
|
|
25
|
+
INTERACTIVE_STATIONS,
|
|
26
|
+
isInteractiveStation,
|
|
27
|
+
type InteractiveStation,
|
|
28
|
+
} from '../stations/attach-interactive.js';
|
|
29
|
+
import type { StepInput } from '../stations/attach-driver.js';
|
|
30
|
+
import {
|
|
31
|
+
type AttachOwner,
|
|
32
|
+
type AttachView,
|
|
33
|
+
} from '../stations/attach-session.js';
|
|
34
|
+
|
|
35
|
+
interface AttachSessionApi {
|
|
36
|
+
start: (
|
|
37
|
+
owner: AttachOwner,
|
|
38
|
+
station: InteractiveStation,
|
|
39
|
+
input: Record<string, unknown>,
|
|
40
|
+
) => Promise<AttachView>;
|
|
41
|
+
view: (owner: AttachOwner, attachId: string) => AttachView;
|
|
42
|
+
submit: (
|
|
43
|
+
owner: AttachOwner,
|
|
44
|
+
attachId: string,
|
|
45
|
+
input: StepInput,
|
|
46
|
+
) => Promise<AttachView>;
|
|
47
|
+
cancel: (owner: AttachOwner, attachId: string) => Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface AccountApiDeps {
|
|
51
|
+
attachSessions: AttachSessionApi;
|
|
52
|
+
prepareAccount: (input: AttachInput) => Promise<PreparedAccount>;
|
|
53
|
+
attachAccount: (
|
|
54
|
+
agentId: string,
|
|
55
|
+
station: StationName,
|
|
56
|
+
config: Record<string, unknown>,
|
|
57
|
+
) => Promise<AccountRef>;
|
|
58
|
+
detachAccount: (
|
|
59
|
+
agentId: string,
|
|
60
|
+
station: StationName,
|
|
61
|
+
accountId: string,
|
|
62
|
+
) => Promise<AccountRef>;
|
|
63
|
+
syncStations: (station: StationName) => Promise<void>;
|
|
64
|
+
reloadAgents: () => Promise<void>;
|
|
65
|
+
setAllowlist: (
|
|
66
|
+
agentId: string,
|
|
67
|
+
station: StationName,
|
|
68
|
+
accountId: string,
|
|
69
|
+
allowlist: string[],
|
|
70
|
+
approvers?: string[],
|
|
71
|
+
) => Promise<string[]>;
|
|
72
|
+
setPolicy: SetPolicy;
|
|
73
|
+
recentSenders: (station: StationName, accountId: string) => RecentSender[];
|
|
74
|
+
resolveSender: (
|
|
75
|
+
station: StationName,
|
|
76
|
+
accountId: string,
|
|
77
|
+
query: string,
|
|
78
|
+
) => Promise<unknown>;
|
|
79
|
+
accountCall: (
|
|
80
|
+
station: StationName,
|
|
81
|
+
action: string,
|
|
82
|
+
args: Record<string, unknown>,
|
|
83
|
+
) => Promise<unknown>;
|
|
84
|
+
setAccountEnabled: (
|
|
85
|
+
agentId: string,
|
|
86
|
+
station: StationName,
|
|
87
|
+
accountId: string,
|
|
88
|
+
enabled: boolean,
|
|
89
|
+
) => Promise<boolean>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const ATTACHABLE: string[] = [...ATTACHABLE_STATIONS, ...INTERACTIVE_STATIONS];
|
|
93
|
+
|
|
94
|
+
async function activate(
|
|
95
|
+
deps: AccountApiDeps,
|
|
96
|
+
station: StationName,
|
|
97
|
+
): Promise<boolean> {
|
|
98
|
+
try {
|
|
99
|
+
await deps.syncStations(station);
|
|
100
|
+
return true;
|
|
101
|
+
} catch (err) {
|
|
102
|
+
log.warn(
|
|
103
|
+
{ station, err: errMsg(err) },
|
|
104
|
+
'account-api: station reload failed, the change lands at the next boot',
|
|
105
|
+
);
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface AttachPayload {
|
|
111
|
+
status: 'done';
|
|
112
|
+
agentId: string;
|
|
113
|
+
station: string;
|
|
114
|
+
accountId: string;
|
|
115
|
+
identity: Record<string, string>;
|
|
116
|
+
activated: boolean;
|
|
117
|
+
secret?: OneTimeSecret;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function storeAccount(
|
|
121
|
+
deps: AccountApiDeps,
|
|
122
|
+
agentId: string,
|
|
123
|
+
station: StationName,
|
|
124
|
+
prepared: PreparedAccount,
|
|
125
|
+
): Promise<AccountRef> {
|
|
126
|
+
try {
|
|
127
|
+
return await deps.attachAccount(
|
|
128
|
+
agentId,
|
|
129
|
+
station,
|
|
130
|
+
prepared.config,
|
|
131
|
+
);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
prepared.discard?.();
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
function asInput(body: unknown): Record<string, unknown> {
|
|
140
|
+
const out: Record<string, unknown> = {};
|
|
141
|
+
for (const key of ['apiId', 'apiHash', 'phone', 'mailbox', 'clientId', 'clientSecret'])
|
|
142
|
+
out[key] = bodyField(body, key);
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function handleStart(
|
|
147
|
+
req: IncomingMessage,
|
|
148
|
+
res: ServerResponse,
|
|
149
|
+
deps: AccountApiDeps,
|
|
150
|
+
agentId: string,
|
|
151
|
+
): Promise<void> {
|
|
152
|
+
const body = await readJsonBody(req);
|
|
153
|
+
const station = bodyField(body, 'station');
|
|
154
|
+
if (isInteractiveStation(station)) {
|
|
155
|
+
const view = await deps.attachSessions.start(
|
|
156
|
+
{ agentId },
|
|
157
|
+
station,
|
|
158
|
+
asInput(body),
|
|
159
|
+
);
|
|
160
|
+
sendJson(req, res, 201, view);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (!isAttachStation(station))
|
|
164
|
+
throw new ApiError(`station must be one of ${ATTACHABLE.join(', ')}`, 400);
|
|
165
|
+
const prepared = await deps.prepareAccount(attachInputOf(station, body));
|
|
166
|
+
const ref = await storeAccount(deps, agentId, station, prepared);
|
|
167
|
+
log.info(
|
|
168
|
+
{ agentId: ref.agentId, station, account: ref.accountId },
|
|
169
|
+
'account-api: attached a station account',
|
|
170
|
+
);
|
|
171
|
+
const payload: AttachPayload = {
|
|
172
|
+
status: 'done',
|
|
173
|
+
agentId: ref.agentId,
|
|
174
|
+
station,
|
|
175
|
+
accountId: ref.accountId,
|
|
176
|
+
identity: prepared.identity,
|
|
177
|
+
activated: await activate(deps, station),
|
|
178
|
+
};
|
|
179
|
+
sendJson(
|
|
180
|
+
req,
|
|
181
|
+
res,
|
|
182
|
+
201,
|
|
183
|
+
prepared.secret === undefined
|
|
184
|
+
? payload
|
|
185
|
+
: { ...payload, secret: prepared.secret },
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async function handleDetach(
|
|
190
|
+
req: IncomingMessage,
|
|
191
|
+
res: ServerResponse,
|
|
192
|
+
deps: AccountApiDeps,
|
|
193
|
+
agentId: string,
|
|
194
|
+
target: { station: StationName; accountId: string },
|
|
195
|
+
): Promise<void> {
|
|
196
|
+
const ref = await deps.detachAccount(
|
|
197
|
+
agentId,
|
|
198
|
+
target.station,
|
|
199
|
+
target.accountId,
|
|
200
|
+
);
|
|
201
|
+
log.info(
|
|
202
|
+
{ agentId: ref.agentId, station: ref.station, account: ref.accountId },
|
|
203
|
+
'account-api: detached a station account',
|
|
204
|
+
);
|
|
205
|
+
sendJson(req, res, 200, {
|
|
206
|
+
agentId: ref.agentId,
|
|
207
|
+
station: ref.station,
|
|
208
|
+
accountId: ref.accountId,
|
|
209
|
+
detached: true,
|
|
210
|
+
activated: await activate(deps, ref.station),
|
|
211
|
+
});
|
|
212
|
+
forgetAccount(ref.station, ref.accountId);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function forgetAccount(station: StationName, accountId: string): void {
|
|
216
|
+
try {
|
|
217
|
+
stationByName(station)?.forget?.(accountId);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
log.warn(
|
|
220
|
+
{ station, account: accountId, err: errMsg(err) },
|
|
221
|
+
'account-api: could not remove the files of a detached account',
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function applied(deps: AccountApiDeps): Promise<boolean> {
|
|
227
|
+
try {
|
|
228
|
+
await deps.reloadAgents();
|
|
229
|
+
return true;
|
|
230
|
+
} catch (err) {
|
|
231
|
+
log.warn({ err: errMsg(err) }, 'account-api: reload failed, the change lands at the next boot');
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function handleAllowlist(
|
|
237
|
+
req: IncomingMessage,
|
|
238
|
+
res: ServerResponse,
|
|
239
|
+
deps: AccountApiDeps,
|
|
240
|
+
agentId: string,
|
|
241
|
+
target: { station: StationName; accountId: string },
|
|
242
|
+
): Promise<void> {
|
|
243
|
+
const body = await readJsonBody(req);
|
|
244
|
+
const wanted = normalizeAllowlist(bodyField(body, 'allowlist'));
|
|
245
|
+
const asked = isRecord(body) && 'approvers' in body ? body.approvers : approversForAccount(target.station, target.accountId);
|
|
246
|
+
const approvers = normalizeApprovers(asked, wanted);
|
|
247
|
+
const before = new Set(approversForAccount(target.station, target.accountId).map((a) => a.toLowerCase()));
|
|
248
|
+
const unknown = await unconfirmedSenders(deps, target.station, target.accountId, approvers.filter((a) => !before.has(a.toLowerCase())));
|
|
249
|
+
if (unknown.length > 0) throw new ApiError(`${target.station} does not know ${unknown.join(', ')}. Check the id, or have them write to the agent once, then try again.`, 400);
|
|
250
|
+
const allowlist = await deps.setAllowlist(agentId, target.station, target.accountId, wanted, approvers);
|
|
251
|
+
log.info({ agentId, station: target.station, account: target.accountId, senders: allowlist.length }, 'account-api: allowlist set');
|
|
252
|
+
sendJson(req, res, 200, {
|
|
253
|
+
agentId,
|
|
254
|
+
station: target.station,
|
|
255
|
+
accountId: target.accountId,
|
|
256
|
+
allowlist,
|
|
257
|
+
approvers,
|
|
258
|
+
activated: await applied(deps),
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function handleEnabled(
|
|
263
|
+
req: IncomingMessage,
|
|
264
|
+
res: ServerResponse,
|
|
265
|
+
deps: AccountApiDeps,
|
|
266
|
+
agentId: string,
|
|
267
|
+
target: { station: StationName; accountId: string },
|
|
268
|
+
): Promise<void> {
|
|
269
|
+
const wanted = bodyField(await readJsonBody(req), 'enabled');
|
|
270
|
+
if (typeof wanted !== 'boolean') throw new ApiError('enabled must be true or false', 400);
|
|
271
|
+
const enabled = await deps.setAccountEnabled(agentId, target.station, target.accountId, wanted);
|
|
272
|
+
log.info({ agentId, station: target.station, account: target.accountId, enabled }, 'account-api: account enabled flag set');
|
|
273
|
+
sendJson(req, res, 200, {
|
|
274
|
+
agentId,
|
|
275
|
+
station: target.station,
|
|
276
|
+
accountId: target.accountId,
|
|
277
|
+
enabled,
|
|
278
|
+
activated: await applied(deps),
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function handleSession(
|
|
283
|
+
req: IncomingMessage,
|
|
284
|
+
res: ServerResponse,
|
|
285
|
+
deps: AccountApiDeps,
|
|
286
|
+
owner: AttachOwner,
|
|
287
|
+
attachId: string,
|
|
288
|
+
): Promise<void> {
|
|
289
|
+
if (req.method === 'DELETE') {
|
|
290
|
+
await deps.attachSessions.cancel(owner, attachId);
|
|
291
|
+
sendJson(req, res, 200, { attachId, cancelled: true });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
sendJson(req, res, 200, deps.attachSessions.view(owner, attachId));
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async function handleStep(
|
|
298
|
+
req: IncomingMessage,
|
|
299
|
+
res: ServerResponse,
|
|
300
|
+
deps: AccountApiDeps,
|
|
301
|
+
owner: AttachOwner,
|
|
302
|
+
attachId: string,
|
|
303
|
+
): Promise<void> {
|
|
304
|
+
const body = await readJsonBody(req);
|
|
305
|
+
const field = (key: string): unknown => bodyField(body, key);
|
|
306
|
+
const view = await deps.attachSessions.submit(owner, attachId, {
|
|
307
|
+
code: field('code'),
|
|
308
|
+
password: field('password'),
|
|
309
|
+
state: field('state'),
|
|
310
|
+
mode: field('mode'),
|
|
311
|
+
error: field('error'),
|
|
312
|
+
errorDescription: field('errorDescription'),
|
|
313
|
+
});
|
|
314
|
+
sendJson(req, res, 200, view);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function dispatchAttach(
|
|
318
|
+
req: IncomingMessage,
|
|
319
|
+
res: ServerResponse,
|
|
320
|
+
deps: AccountApiDeps,
|
|
321
|
+
agentId: string,
|
|
322
|
+
route: Extract<AccountRoute, { kind: 'start' | 'session' | 'step' }>,
|
|
323
|
+
): Promise<void> {
|
|
324
|
+
if (route.kind === 'start') return handleStart(req, res, deps, agentId);
|
|
325
|
+
if (route.kind === 'session') return handleSession(req, res, deps, { agentId }, route.attachId);
|
|
326
|
+
return handleStep(req, res, deps, { agentId }, route.attachId);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async function dispatchRoute(
|
|
330
|
+
req: IncomingMessage,
|
|
331
|
+
res: ServerResponse,
|
|
332
|
+
deps: AccountApiDeps,
|
|
333
|
+
agentId: string,
|
|
334
|
+
route: AccountRoute,
|
|
335
|
+
): Promise<void> {
|
|
336
|
+
if (route.kind === 'start' || route.kind === 'session' || route.kind === 'step') return dispatchAttach(req, res, deps, agentId, route);
|
|
337
|
+
if (route.kind === 'allowlist') return handleAllowlist(req, res, deps, agentId, route);
|
|
338
|
+
if (route.kind === 'enabled') return handleEnabled(req, res, deps, agentId, route);
|
|
339
|
+
if (route.kind === 'policy') return handlePolicy(req, res, deps, agentId, route);
|
|
340
|
+
const lookup = LOOKUPS[route.kind];
|
|
341
|
+
if (lookup !== undefined && 'station' in route) return lookup(req, res, deps, route);
|
|
342
|
+
return handleDetach(req, res, deps, agentId, route);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export async function handleAccountRoute(
|
|
346
|
+
req: IncomingMessage,
|
|
347
|
+
res: ServerResponse,
|
|
348
|
+
deps: AccountApiDeps,
|
|
349
|
+
agentId: string,
|
|
350
|
+
route: AccountRoute,
|
|
351
|
+
): Promise<void> {
|
|
352
|
+
try {
|
|
353
|
+
await dispatchRoute(req, res, deps, agentId, route);
|
|
354
|
+
} catch (err) {
|
|
355
|
+
apiFailure(req, res, err);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
3
|
+
import { AGENT_NAME_RE } from '@metro-labs/core/ids';
|
|
4
|
+
|
|
5
|
+
export class AgentAdminError extends ApiError {}
|
|
6
|
+
|
|
7
|
+
export interface AgentSummary {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string | null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface CreatedAgent {
|
|
13
|
+
id: string;
|
|
14
|
+
name: string | null;
|
|
15
|
+
key: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function normalizeAgentName(raw: unknown): string {
|
|
19
|
+
const name = typeof raw === 'string' ? raw.trim() : '';
|
|
20
|
+
if (!AGENT_NAME_RE.test(name))
|
|
21
|
+
throw new AgentAdminError(
|
|
22
|
+
'name must be 2-32 characters of A-Z, a-z, 0-9, - or _, starting with a letter or digit',
|
|
23
|
+
400,
|
|
24
|
+
);
|
|
25
|
+
return name;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function newApiKey(): string {
|
|
29
|
+
return `mk_${randomBytes(32).toString('base64url')}`;
|
|
30
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
2
|
+
|
|
3
|
+
const EVERYONE = '*';
|
|
4
|
+
const MAX_ENTRIES = 500;
|
|
5
|
+
const MAX_LENGTH = 200;
|
|
6
|
+
|
|
7
|
+
function hasControlChar(value: string): boolean {
|
|
8
|
+
for (const ch of value) {
|
|
9
|
+
const code = ch.codePointAt(0) ?? 0;
|
|
10
|
+
if (code < 0x20 || code === 0x7f) return true;
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const DOMAIN_ENTRY_RE = /^@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
|
|
16
|
+
|
|
17
|
+
function domainEntry(value: string): string {
|
|
18
|
+
const domain = value.toLowerCase();
|
|
19
|
+
if (!DOMAIN_ENTRY_RE.test(domain))
|
|
20
|
+
throw new ApiError(`'${value}' is not a domain; write it as @example.com`, 400);
|
|
21
|
+
return domain;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function senderId(entry: unknown): string {
|
|
25
|
+
if (typeof entry !== 'string') throw new ApiError('allowlist must be a list of sender ids', 400);
|
|
26
|
+
const value = entry.trim();
|
|
27
|
+
if (value.length > MAX_LENGTH || hasControlChar(value))
|
|
28
|
+
throw new ApiError(`a sender id is at most ${String(MAX_LENGTH)} plain characters`, 400);
|
|
29
|
+
return value.startsWith('@') ? domainEntry(value) : value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function normalizeApprovers(raw: unknown, allowlist: string[]): string[] {
|
|
33
|
+
if (raw === undefined) return [];
|
|
34
|
+
if (!Array.isArray(raw)) throw new ApiError('approvers must be a list of sender ids', 400);
|
|
35
|
+
const listed = new Set(allowlist.map((entry) => entry.toLowerCase()));
|
|
36
|
+
const seen = new Set<string>();
|
|
37
|
+
return raw.map(senderId).filter((value) => {
|
|
38
|
+
const key = value.toLowerCase();
|
|
39
|
+
if (!listed.has(key) || seen.has(key)) return false;
|
|
40
|
+
seen.add(key);
|
|
41
|
+
return true;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function normalizeAllowlist(raw: unknown): string[] {
|
|
46
|
+
if (!Array.isArray(raw)) throw new ApiError('allowlist must be a list of sender ids', 400);
|
|
47
|
+
if (raw.length > MAX_ENTRIES) throw new ApiError(`allowlist holds at most ${String(MAX_ENTRIES)} senders`, 400);
|
|
48
|
+
const seen = new Set<string>();
|
|
49
|
+
const out: string[] = [];
|
|
50
|
+
for (const entry of raw) {
|
|
51
|
+
const value = senderId(entry);
|
|
52
|
+
if (value === EVERYONE) return [EVERYONE];
|
|
53
|
+
const key = value.toLowerCase();
|
|
54
|
+
if (key === '' || seen.has(key)) continue;
|
|
55
|
+
seen.add(key);
|
|
56
|
+
out.push(value);
|
|
57
|
+
}
|
|
58
|
+
return out.length === 0 ? [EVERYONE] : out;
|
|
59
|
+
}
|