@stage-labs/metro 0.1.0-beta.20 → 0.1.0-beta.201
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/claude-settings.js +40 -0
- package/dist/claude.js +163 -0
- package/dist/cli.js +50 -109
- package/dist/control.js +47 -75
- package/dist/hold.js +211 -0
- package/dist/local.js +43 -0
- package/dist/mcp-config.js +25 -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 +97 -0
- package/dist/runtime.js +32 -52
- package/dist/serve.js +158 -0
- package/dist/service.js +243 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +13 -1
- package/package.json +1 -22
- 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/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/marketplace/plugin/orchestrator.md +24 -0
- package/runtime/node_modules/@metro-labs/core/package.json +47 -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 +8 -6
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- 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 +12 -3
- 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 +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/account-store.ts +4 -24
- 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 +3 -6
- 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 +89 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/types.ts +16 -0
- 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 +48 -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/files-api.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files.ts +119 -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 +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +100 -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 +252 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +61 -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 +168 -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/{mcp/src/db/agent-map.ts → daemon/src/agents/map.ts} +58 -1
- 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 +86 -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/needs.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +149 -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 +221 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -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/daemon/src/channels/dedupe.ts +12 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/inbound.ts +47 -44
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/media-note.ts +9 -4
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/pending.ts +4 -1
- package/runtime/node_modules/@metro-labs/daemon/src/channels/react-note.ts +13 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/relay.ts +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/channels/reply-id.ts +1 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +355 -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.ts +82 -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 +283 -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 +271 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +120 -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} +77 -109
- package/runtime/node_modules/@metro-labs/{mcp/src/db/connector-config.ts → daemon/src/connectors/config.ts} +55 -26
- 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 +28 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth-routes.ts → daemon/src/connectors/oauth-routes.ts} +30 -29
- 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-target.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +348 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +81 -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 +11 -12
- 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 +2 -2
- 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 +208 -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 +194 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +137 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +77 -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 +274 -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 +223 -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 +199 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +299 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +193 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -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 +66 -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 +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +244 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/accounts.ts +59 -18
- 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 +43 -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 +24 -79
- 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 +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +83 -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 +48 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/session.ts +33 -42
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +88 -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 +55 -119
- 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/api.ts +43 -0
- 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/move.ts +165 -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 +59 -51
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +71 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/session-api.ts → daemon/src/routes/session.ts} +9 -7
- 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 +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-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 +9 -36
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-outlook.ts +63 -0
- 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 +30 -20
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-xmtp.ts +2 -2
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach.ts +75 -9
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +265 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/registry.ts +22 -6
- 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 +2 -2
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor.ts +5 -8
- 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 +48 -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 +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +130 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +61 -0
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/package.json +3 -5
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/accounts.ts +11 -13
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/actions.ts +6 -154
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/format.ts +28 -64
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group-actions.ts +1 -1
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/index.ts +14 -19
- package/runtime/node_modules/@metro-labs/{discord → 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 → discord-bot}/src/send-files.ts +1 -1
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +22 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +138 -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/workos-token.ts +149 -0
- package/runtime/node_modules/@metro-labs/{telegram-user → outlook}/package.json +2 -5
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +107 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +20 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +97 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +53 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +36 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +141 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +60 -0
- package/runtime/node_modules/@metro-labs/outlook/src/html.ts +72 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +104 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +41 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +116 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +119 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +43 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +3 -2
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +48 -88
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +214 -253
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +67 -108
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/client.ts +6 -6
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +102 -146
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/group.ts +4 -4
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/history.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +6 -102
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/login.ts +20 -20
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +65 -151
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +61 -33
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/normalize.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/reactions.ts +2 -2
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +11 -1
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +5 -47
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -3
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +119 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +246 -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 +173 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +95 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +65 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +20 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +106 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +134 -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 +58 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +169 -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 +89 -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 +1 -3
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +1 -2
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +40 -5
- 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 +74 -4
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +7 -56
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +10 -27
- 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 +28 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +1 -1
- 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 +8 -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/whatsapp/src/wire.ts +1 -5
- package/runtime/node_modules/@metro-labs/xmtp/package.json +3 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +9 -8
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +1 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +3 -201
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +3 -42
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +12 -143
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +7 -24
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +7 -16
- 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 +11 -14
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +1 -19
- 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/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 +5 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +3 -50
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +30 -44
- 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 +15 -3
- package/runtime/runtime.json +1 -1
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +37 -0
- package/dist/prompt.js +0 -10
- package/dist/store.js +0 -70
- package/runtime/node_modules/@metro-labs/discord/src/station.ts +0 -19
- package/runtime/node_modules/@metro-labs/discord/src/voice-speak.ts +0 -195
- package/runtime/node_modules/@metro-labs/discord/src/voice-transcribe.ts +0 -292
- package/runtime/node_modules/@metro-labs/discord/src/voice.ts +0 -197
- package/runtime/node_modules/@metro-labs/discord/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 -335
- 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 -322
- 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/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/materialize.ts +0 -340
- 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 -28
- 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/telegram-user/src/accounts.ts +0 -81
- package/runtime/node_modules/@metro-labs/telegram-user/src/actions.ts +0 -254
- package/runtime/node_modules/@metro-labs/telegram-user/src/attachments.ts +0 -87
- package/runtime/node_modules/@metro-labs/telegram-user/src/format.ts +0 -141
- package/runtime/node_modules/@metro-labs/telegram-user/src/inbound.ts +0 -78
- package/runtime/node_modules/@metro-labs/telegram-user/src/index.ts +0 -46
- package/runtime/node_modules/@metro-labs/telegram-user/src/media-actions.ts +0 -80
- package/runtime/node_modules/@metro-labs/telegram-user/src/members.ts +0 -75
- package/runtime/node_modules/@metro-labs/telegram-user/src/station.ts +0 -24
- package/runtime/node_modules/@metro-labs/telegram-user/src/types.ts +0 -7
- package/runtime/node_modules/@metro-labs/telegram-user/src/wire.ts +0 -1
- 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 → core}/src/stations/attachments.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/messaging-normalize.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/{discord → discord-bot}/src/api-base.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/verify.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
- /package/runtime/node_modules/@metro-labs/{telegram → telegram-bot}/src/verify.ts +0 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
7
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
8
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
9
|
+
import { asUser, type AgentUser } from './user.js';
|
|
10
|
+
import { metroRun, writeDropIn } from './unit-files.js';
|
|
11
|
+
|
|
12
|
+
const UNIT_DIR = '/etc/systemd/system';
|
|
13
|
+
const defaultBackups = (): string => join(homedir(), '.metro', 'agents');
|
|
14
|
+
const DROP_IN = '10-metro-agent.conf';
|
|
15
|
+
const OWN_UNITS = /^metro(-claude-\d+)?\.(service|scope)$/;
|
|
16
|
+
|
|
17
|
+
export type JobKind = 'timer' | 'cron-root' | 'cron-agent';
|
|
18
|
+
|
|
19
|
+
export interface ScheduledJob {
|
|
20
|
+
id: string;
|
|
21
|
+
kind: JobKind;
|
|
22
|
+
name: string;
|
|
23
|
+
schedule: string;
|
|
24
|
+
command: string;
|
|
25
|
+
runsAs: string;
|
|
26
|
+
next: string | null;
|
|
27
|
+
last: string | null;
|
|
28
|
+
lastResult: string | null;
|
|
29
|
+
usesRoot: boolean;
|
|
30
|
+
converted: boolean;
|
|
31
|
+
problem?: string | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface Runner {
|
|
35
|
+
run: (file: string, args: string[], input?: string) => { status: number | null; stdout: string };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const realRunner: Runner = {
|
|
39
|
+
run: (file, args, input) => {
|
|
40
|
+
const viaHelper = metroRun(file, args, input);
|
|
41
|
+
if (viaHelper !== null) return viaHelper;
|
|
42
|
+
const done = spawnSync(file, args, { encoding: 'utf8', input, stdio: ['pipe', 'pipe', 'ignore'], timeout: 20_000 });
|
|
43
|
+
return { status: done.error === undefined ? done.status : null, stdout: done.stdout ?? '' };
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const ROOT_RE = /(^|[\s="'=:])\/root(?=\/|$|[\s"';:])/;
|
|
48
|
+
export const usesRootHome = (text: string): boolean => ROOT_RE.test(text);
|
|
49
|
+
export const rehome = (text: string, home: string): string => text.replace(/(^|[\s="'=:])\/root(?=\/|$|[\s"';:])/g, `$1${home}`);
|
|
50
|
+
|
|
51
|
+
const stamp = (micros: unknown): string | null =>
|
|
52
|
+
typeof micros === 'number' && micros > 0 ? new Date(micros / 1000).toISOString() : null;
|
|
53
|
+
|
|
54
|
+
export function showProps(runner: Runner, unit: string, props: string[]): Record<string, string> {
|
|
55
|
+
const out = runner.run('systemctl', ['show', unit, '--no-pager', ...props.map((p) => `-p${p}`)]).stdout;
|
|
56
|
+
const found: Record<string, string> = {};
|
|
57
|
+
for (const line of out.split('\n')) {
|
|
58
|
+
const at = line.indexOf('=');
|
|
59
|
+
if (at > 0) found[line.slice(0, at)] = line.slice(at + 1);
|
|
60
|
+
}
|
|
61
|
+
return found;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function execArgv(execStart: string): string[] {
|
|
65
|
+
const m = /argv\[\]=(.*?) ;/.exec(execStart);
|
|
66
|
+
return m?.[1] === undefined ? [] : m[1].split(' ').filter((a) => a !== '');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const calendarOf = (timers: string): string =>
|
|
70
|
+
/OnCalendar=([^;]*?) ;/.exec(timers)?.[1]?.trim() ?? /(OnUnitActiveUSec|OnBootUSec|OnActiveUSec)=([^;]*?) ;/.exec(timers)?.[0]?.replace(/ ;$/, '') ?? '';
|
|
71
|
+
|
|
72
|
+
function listTimers(runner: Runner): Record<string, unknown>[] {
|
|
73
|
+
try {
|
|
74
|
+
const raw: unknown = JSON.parse(runner.run('systemctl', ['list-timers', '--all', '--output=json', '--no-pager']).stdout);
|
|
75
|
+
return Array.isArray(raw) ? raw.filter(isRecord) : [];
|
|
76
|
+
} catch {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const text = (v: unknown): string => (typeof v === 'string' ? v : '');
|
|
82
|
+
const orRoot = (user: string | undefined): string => (user === undefined || user === '' ? 'root' : user);
|
|
83
|
+
|
|
84
|
+
function timerJob(runner: Runner, t: Record<string, unknown>): ScheduledJob | null {
|
|
85
|
+
const unit = text(t.unit);
|
|
86
|
+
const service = text(t.activates);
|
|
87
|
+
if (unit === '' || service === '' || OWN_UNITS.test(service)) return null;
|
|
88
|
+
const timer = showProps(runner, unit, ['FragmentPath', 'TimersCalendar', 'TimersMonotonic']);
|
|
89
|
+
if (!text(timer.FragmentPath).startsWith(`${UNIT_DIR}/`)) return null;
|
|
90
|
+
const svc = showProps(runner, service, ['ExecStart', 'User', 'Result', 'DropInPaths', 'Environment', 'WorkingDirectory']);
|
|
91
|
+
const command = execArgv(text(svc.ExecStart)).join(' ');
|
|
92
|
+
return {
|
|
93
|
+
id: `timer:${unit}`,
|
|
94
|
+
kind: 'timer',
|
|
95
|
+
name: unit.replace(/\.timer$/, ''),
|
|
96
|
+
schedule: calendarOf(`${text(timer.TimersCalendar)} ${text(timer.TimersMonotonic)}`),
|
|
97
|
+
command,
|
|
98
|
+
runsAs: orRoot(svc.User),
|
|
99
|
+
next: stamp(t.next),
|
|
100
|
+
last: stamp(t.last),
|
|
101
|
+
lastResult: svc.Result ?? null,
|
|
102
|
+
usesRoot: usesRootHome(`${command} ${text(svc.Environment)} ${text(svc.WorkingDirectory)}`),
|
|
103
|
+
converted: text(svc.DropInPaths).includes(DROP_IN),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const timerJobs = (runner: Runner): ScheduledJob[] =>
|
|
108
|
+
listTimers(runner).flatMap((t) => {
|
|
109
|
+
const job = timerJob(runner, t);
|
|
110
|
+
return job === null ? [] : [job];
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
export interface CronLine {
|
|
114
|
+
schedule: string;
|
|
115
|
+
command: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function parseCronLine(line: string): CronLine | null {
|
|
119
|
+
const text = line.trim();
|
|
120
|
+
if (text === '' || text.startsWith('#') || /^[A-Za-z_][A-Za-z0-9_]*=/.test(text)) return null;
|
|
121
|
+
if (text.startsWith('@')) {
|
|
122
|
+
const [at = '', ...rest] = text.split(/\s+/);
|
|
123
|
+
return rest.length === 0 ? null : { schedule: at, command: rest.join(' ') };
|
|
124
|
+
}
|
|
125
|
+
const parts = text.split(/\s+/);
|
|
126
|
+
if (parts.length < 6) return null;
|
|
127
|
+
return { schedule: parts.slice(0, 5).join(' '), command: parts.slice(5).join(' ') };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const lineId = (kind: JobKind, line: string): string => `${kind}:${createHash('sha256').update(line.trim()).digest('hex').slice(0, 16)}`;
|
|
131
|
+
|
|
132
|
+
function crontabOf(runner: Runner, user: string): string[] {
|
|
133
|
+
const out = runner.run('crontab', ['-l', '-u', user]);
|
|
134
|
+
return out.status === 0 ? out.stdout.split('\n') : [];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function cronJobs(runner: Runner, user: string, kind: JobKind): ScheduledJob[] {
|
|
138
|
+
return crontabOf(runner, user).flatMap((line): ScheduledJob[] => {
|
|
139
|
+
const parsed = parseCronLine(line);
|
|
140
|
+
if (parsed === null) return [];
|
|
141
|
+
return [{
|
|
142
|
+
id: lineId(kind, line),
|
|
143
|
+
kind,
|
|
144
|
+
name: parsed.command.split(/\s+/)[0]?.split('/').pop() ?? 'cron',
|
|
145
|
+
schedule: parsed.schedule,
|
|
146
|
+
command: parsed.command,
|
|
147
|
+
runsAs: user,
|
|
148
|
+
next: null,
|
|
149
|
+
last: null,
|
|
150
|
+
lastResult: null,
|
|
151
|
+
usesRoot: usesRootHome(parsed.command),
|
|
152
|
+
converted: false,
|
|
153
|
+
}];
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function allJobs(user: AgentUser | null, runner: Runner): ScheduledJob[] {
|
|
158
|
+
return [...timerJobs(runner), ...cronJobs(runner, 'root', 'cron-root'), ...(user === null ? [] : cronJobs(runner, user.name, 'cron-agent'))];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const quote = (arg: string): string => `"${arg.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
|
162
|
+
|
|
163
|
+
export function dropInText(user: AgentUser, argv: string[], environment: string): string {
|
|
164
|
+
const env = environment.split(' ').filter((pair) => pair.includes('=') && !pair.startsWith('HOME=') && !pair.startsWith('PATH='));
|
|
165
|
+
return [
|
|
166
|
+
'[Service]',
|
|
167
|
+
`User=${user.name}`,
|
|
168
|
+
`Group=${String(user.gid)}`,
|
|
169
|
+
`WorkingDirectory=${user.home}`,
|
|
170
|
+
`Environment=HOME=${user.home}`,
|
|
171
|
+
`Environment=PATH=${user.home}/.local/bin:/usr/local/bin:/usr/bin:/bin`,
|
|
172
|
+
...env.map((pair) => `Environment=${quote(rehome(pair, user.home))}`),
|
|
173
|
+
'ExecStart=',
|
|
174
|
+
`ExecStart=${argv.map((a) => quote(rehome(a, user.home))).join(' ')}`,
|
|
175
|
+
'',
|
|
176
|
+
].join('\n');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function agentSees(user: AgentUser, path: string): boolean {
|
|
180
|
+
if (existsSync(path)) return true;
|
|
181
|
+
const [file, args] = asUser(user, 'test', ['-e', path]);
|
|
182
|
+
return spawnSync(file, args, { stdio: 'ignore' }).status === 0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function convertTimer(job: ScheduledJob, user: AgentUser, runner: Runner): void {
|
|
186
|
+
const unit = job.id.slice('timer:'.length);
|
|
187
|
+
const service = showProps(runner, unit, ['Unit']).Unit ?? unit.replace(/\.timer$/, '.service');
|
|
188
|
+
const svc = showProps(runner, service, ['ExecStart', 'Environment']);
|
|
189
|
+
const argv = execArgv(svc.ExecStart ?? '');
|
|
190
|
+
if (argv.length === 0) throw new ApiError('that timer runs no command metro can read', 400);
|
|
191
|
+
const missing = argv.map((a) => rehome(a, user.home)).find((a) => a.startsWith(`${user.home}/`) && !agentSees(user, a));
|
|
192
|
+
if (missing !== undefined) throw new ApiError(`${missing} does not exist; move it to the agent first`, 409);
|
|
193
|
+
writeDropIn(service, DROP_IN, dropInText(user, argv, svc.Environment ?? ''));
|
|
194
|
+
runner.run('systemctl', ['daemon-reload']);
|
|
195
|
+
log.info({ unit, service, user: user.name }, 'schedules: a timer now runs as the agent user');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function convertCron(job: ScheduledJob, user: AgentUser, backups: string, runner: Runner): void {
|
|
199
|
+
const lines = crontabOf(runner, 'root');
|
|
200
|
+
const line = lines.find((l) => lineId('cron-root', l) === job.id);
|
|
201
|
+
if (line === undefined) throw new ApiError('that cron line is gone', 404);
|
|
202
|
+
mkdirSync(backups, { recursive: true });
|
|
203
|
+
writeFileSync(join(backups, `crontab-root.${new Date().toISOString().replace(/[:.]/g, '-')}.bak`), lines.join('\n'), { mode: 0o600 });
|
|
204
|
+
const agentLines = crontabOf(runner, user.name).filter((l) => l !== '');
|
|
205
|
+
const moved = runner.run('crontab', ['-u', user.name, '-'], `${[...agentLines, rehome(line, user.home)].join('\n')}\n`);
|
|
206
|
+
if (moved.status !== 0) throw new ApiError("the agent's crontab refused the line", 500);
|
|
207
|
+
const kept = lines.filter((l) => l !== line && l !== '');
|
|
208
|
+
runner.run('crontab', ['-u', 'root', '-'], `${kept.join('\n')}\n`);
|
|
209
|
+
log.info({ user: user.name }, "schedules: a root cron line moved to the agent's crontab");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function toAgent(job: ScheduledJob, user: AgentUser, backups: string, runner: Runner): void {
|
|
213
|
+
if (job.kind === 'timer') convertTimer(job, user, runner);
|
|
214
|
+
else if (job.kind === 'cron-root') convertCron(job, user, backups, runner);
|
|
215
|
+
else throw new ApiError('that job already runs as the agent', 400);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const problems = new Map<string, string>();
|
|
219
|
+
|
|
220
|
+
const needsAgent = (job: ScheduledJob): boolean => job.kind !== 'cron-agent' && job.runsAs === 'root' && job.usesRoot;
|
|
221
|
+
|
|
222
|
+
function tryAgent(job: ScheduledJob, user: AgentUser, backups: string, runner: Runner): boolean {
|
|
223
|
+
try {
|
|
224
|
+
toAgent(job, user, backups, runner);
|
|
225
|
+
problems.delete(job.id);
|
|
226
|
+
return true;
|
|
227
|
+
} catch (err) {
|
|
228
|
+
problems.set(job.id, errMsg(err));
|
|
229
|
+
log.warn({ job: job.name, err: errMsg(err) }, 'schedules: a job still pointing into /root could not be switched to the agent user');
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function convertRootJobs(user: AgentUser | null, backups = defaultBackups(), runner: Runner = realRunner): number {
|
|
235
|
+
if (user === null) return 0;
|
|
236
|
+
const pending = allJobs(user, runner).filter(needsAgent);
|
|
237
|
+
return pending.filter((job) => tryAgent(job, user, backups, runner)).length;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export const listSchedules = (user: AgentUser | null, runner: Runner = realRunner): ScheduledJob[] =>
|
|
241
|
+
allJobs(user, runner)
|
|
242
|
+
.filter((job) => job.runsAs !== 'root' || job.usesRoot)
|
|
243
|
+
.map((job) => ({ ...job, problem: problems.get(job.id) ?? null }));
|
|
244
|
+
|
|
245
|
+
export function retrySchedule(id: string, user: AgentUser | null, backups = defaultBackups(), runner: Runner = realRunner): ScheduledJob[] {
|
|
246
|
+
if (user === null) throw new ApiError('Claude Code does not run as its own user on this machine', 409);
|
|
247
|
+
const job = allJobs(user, runner).find((j) => j.id === id);
|
|
248
|
+
if (job === undefined) throw new ApiError('no such scheduled job', 404);
|
|
249
|
+
if (needsAgent(job)) toAgent(job, user, backups, runner);
|
|
250
|
+
problems.delete(id);
|
|
251
|
+
return listSchedules(user, runner);
|
|
252
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { mustHelper, rootHelper, runningAsMetro } from '../metro-user/privilege.js';
|
|
6
|
+
import { agentUser, asUser } from './user.js';
|
|
7
|
+
|
|
8
|
+
const UNIT_DIR = '/etc/systemd/system';
|
|
9
|
+
|
|
10
|
+
export function writeDropIn(service: string, name: string, text: string): void {
|
|
11
|
+
if (runningAsMetro()) {
|
|
12
|
+
mustHelper(['dropin-write', service, name], text);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
mkdirSync(join(UNIT_DIR, `${service}.d`), { recursive: true });
|
|
16
|
+
writeFileSync(join(UNIT_DIR, `${service}.d`, name), text, { mode: 0o644 });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function removeDropIn(service: string, name: string): boolean {
|
|
20
|
+
const file = join(UNIT_DIR, `${service}.d`, name);
|
|
21
|
+
if (!existsSync(file)) return false;
|
|
22
|
+
if (runningAsMetro()) mustHelper(['dropin-remove', service, name]);
|
|
23
|
+
else rmSync(file, { force: true });
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const lineHash = (line: string): string => createHash('sha256').update(line.trim()).digest('hex').slice(0, 16);
|
|
28
|
+
|
|
29
|
+
function asAgentRun(args: string[], input?: string): { status: number | null; stdout: string } {
|
|
30
|
+
const [file, argv] = asUser(agentUser(), 'crontab', args);
|
|
31
|
+
const run = spawnSync(file, argv, { encoding: 'utf8', input, stdio: ['pipe', 'pipe', 'ignore'], timeout: 20_000 });
|
|
32
|
+
return { status: run.error === undefined ? run.status : null, stdout: run.stdout ?? '' };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function rootCrontabWrite(input: string): { status: number | null; stdout: string } {
|
|
36
|
+
const wanted = new Set(input.split('\n').filter((l) => l.trim() !== '').map(lineHash));
|
|
37
|
+
const current = rootHelper(['root-crontab']).stdout.split('\n').filter((l) => l.trim() !== '');
|
|
38
|
+
for (const line of current) if (!wanted.has(lineHash(line))) mustHelper(['root-crontab-drop', lineHash(line)]);
|
|
39
|
+
return { status: 0, stdout: '' };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function crontab(args: string[], input?: string): { status: number | null; stdout: string } {
|
|
43
|
+
const at = args.indexOf('-u');
|
|
44
|
+
const who = at >= 0 ? args[at + 1] : undefined;
|
|
45
|
+
const rest = args.filter((_, i) => i !== at && i !== at + 1);
|
|
46
|
+
if (who === 'root') return rest[0] === '-l' ? { status: 0, stdout: rootHelper(['root-crontab']).stdout } : rootCrontabWrite(input ?? '');
|
|
47
|
+
return asAgentRun(rest, input);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function systemctl(args: string[]): { status: number | null; stdout: string } | null {
|
|
51
|
+
if (args[0] === 'daemon-reload') return { status: rootHelper(['daemon-reload']).status, stdout: '' };
|
|
52
|
+
if (args[0] === 'start') return { status: rootHelper(['start-job', args[args.length - 1] ?? '']).status, stdout: '' };
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function metroRun(file: string, args: string[], input?: string): { status: number | null; stdout: string } | null {
|
|
57
|
+
if (!runningAsMetro()) return null;
|
|
58
|
+
if (file === 'crontab') return crontab(args, input);
|
|
59
|
+
if (file === 'systemctl') return systemctl(args);
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { runningAsMetro } from '../metro-user/privilege.js';
|
|
4
|
+
|
|
5
|
+
export const AGENT_NAME = 'agent';
|
|
6
|
+
const AGENT_PATH = ['.local/bin', '/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin'];
|
|
7
|
+
|
|
8
|
+
export interface AgentUser {
|
|
9
|
+
name: string;
|
|
10
|
+
uid: number;
|
|
11
|
+
gid: number;
|
|
12
|
+
home: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface UserHost {
|
|
16
|
+
platform: string;
|
|
17
|
+
uid: number | undefined;
|
|
18
|
+
lookup: (name: string) => AgentUser | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function lookupUser(name: string): AgentUser | null {
|
|
22
|
+
const run = spawnSync('getent', ['passwd', name], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
23
|
+
if (run.error !== undefined || run.status !== 0) return null;
|
|
24
|
+
const [, , uid = '', gid = '', , home = ''] = run.stdout.trim().split(':');
|
|
25
|
+
const u = Number(uid);
|
|
26
|
+
const g = Number(gid);
|
|
27
|
+
return Number.isInteger(u) && u > 0 && Number.isInteger(g) && home.startsWith('/') ? { name, uid: u, gid: g, home } : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const realHost: UserHost = { platform: process.platform, uid: process.getuid?.(), lookup: lookupUser };
|
|
31
|
+
|
|
32
|
+
export const agentUserExpected = (host: UserHost = realHost): boolean => host.platform === 'linux' && (host.uid === 0 || runningAsMetro());
|
|
33
|
+
|
|
34
|
+
let cached: AgentUser | null = null;
|
|
35
|
+
|
|
36
|
+
export function agentUser(host: UserHost = realHost): AgentUser | null {
|
|
37
|
+
if (!agentUserExpected(host)) return null;
|
|
38
|
+
cached ??= host.lookup(AGENT_NAME);
|
|
39
|
+
return cached;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function forgetAgentUser(): void {
|
|
43
|
+
cached = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let extraEnv: Record<string, string> = {};
|
|
47
|
+
|
|
48
|
+
export function setAgentExtraEnv(env: Record<string, string>): void {
|
|
49
|
+
extraEnv = { ...env };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const agentExtraEnv = (): Record<string, string> => ({ ...extraEnv });
|
|
53
|
+
|
|
54
|
+
export function agentEnv(user: AgentUser, extra: Record<string, string> = {}): Record<string, string> {
|
|
55
|
+
return {
|
|
56
|
+
HOME: user.home,
|
|
57
|
+
USER: user.name,
|
|
58
|
+
LOGNAME: user.name,
|
|
59
|
+
SHELL: '/bin/bash',
|
|
60
|
+
LANG: process.env.LANG ?? 'C.UTF-8',
|
|
61
|
+
TERM: process.env.TERM ?? 'xterm-256color',
|
|
62
|
+
PATH: AGENT_PATH.map((p) => (p.startsWith('/') ? p : join(user.home, p))).join(':'),
|
|
63
|
+
...extraEnv,
|
|
64
|
+
...extra,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function asUser(user: AgentUser | null, file: string, args: readonly string[], extra: Record<string, string> = {}): [string, string[]] {
|
|
69
|
+
if (user === null) return [file, [...args]];
|
|
70
|
+
const env = Object.entries(agentEnv(user, extra)).map(([k, v]) => `${k}=${v}`);
|
|
71
|
+
if (runningAsMetro()) return ['sudo', ['-n', '-u', user.name, '--', 'env', '-i', ...env, file, ...args]];
|
|
72
|
+
return ['setpriv', [`--reuid=${String(user.uid)}`, `--regid=${String(user.gid)}`, '--init-groups', 'env', '-i', ...env, file, ...args]];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const asAgent = (file: string, args: readonly string[], extra: Record<string, string> = {}): [string, string[]] =>
|
|
76
|
+
asUser(agentUser(), file, args, extra);
|
|
77
|
+
|
|
78
|
+
export const agentCommand = (command: readonly string[], extra: Record<string, string> = {}): string[] => {
|
|
79
|
+
const [file = '', ...args] = command;
|
|
80
|
+
const [bin, argv] = asAgent(file, args, extra);
|
|
81
|
+
return [bin, ...argv];
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export function claudeBin(user = agentUser()): string {
|
|
85
|
+
return user === null ? 'claude' : join(user.home, '.local', 'bin', 'claude');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const claudeHome = (user = agentUser()): string | null => (user === null ? null : user.home);
|
|
89
|
+
|
|
90
|
+
export const agentMarketplaceDir = (user: AgentUser): string => join(user.home, '.metro', 'marketplace');
|
|
91
|
+
export const agentViewDir = (user: AgentUser): string => join(user.home, '.metro', 'agents');
|
|
@@ -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
|
+
export 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
|
+
export 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('a number 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
|
+
}
|