@stage-labs/metro 0.1.0-beta.21 → 0.1.0-beta.210
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 +152 -0
- package/dist/cli.js +52 -109
- package/dist/control.js +47 -75
- package/dist/hold.js +210 -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 +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/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 +26 -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 +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 +24 -28
- 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/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/messaging-normalize.ts +16 -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 +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 +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 +51 -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 +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +89 -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/{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 +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 +218 -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 +70 -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 +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 +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 +284 -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 +273 -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} +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/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 +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 +195 -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 +128 -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 +224 -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 +278 -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 +14 -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 +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 +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 +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 +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 +68 -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/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 +59 -51
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +205 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +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 +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 +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 +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 → discord-bot}/package.json +3 -5
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/accounts.ts +8 -28
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/actions.ts +19 -187
- 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 +31 -72
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group-actions.ts +2 -2
- 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 +15 -21
- 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 +12 -13
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +23 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -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/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 +93 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +14 -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 +140 -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 +38 -96
- 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 +101 -147
- 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 +64 -151
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +61 -33
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/reactions.ts +3 -3
- 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-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +86 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +240 -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 +170 -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 +90 -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 +85 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +21 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +95 -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 +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 +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 +4 -43
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +41 -219
- 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 +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 +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 +4 -51
- 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 +14 -4
- 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 -364
- 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/wire.ts +0 -3
- 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/normalize.ts +0 -22
- 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/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/{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
package/dist/hold.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { createServer } from 'node:http';
|
|
4
|
+
const FUNNEL_RETRY_MS = 2_000;
|
|
5
|
+
const FUNNEL_EXIT_WAIT_MS = 5_000;
|
|
6
|
+
const LISTEN_ATTEMPTS = 10;
|
|
7
|
+
const LISTEN_RETRY_MS = 500;
|
|
8
|
+
const STOPPED = 503;
|
|
9
|
+
export const STOPPED_MESSAGE = 'metro is stopped on this machine. Start it from the Server page on metro.box, or run metro serve.';
|
|
10
|
+
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
11
|
+
function corsHeaders(req) {
|
|
12
|
+
return {
|
|
13
|
+
'access-control-allow-origin': req.headers.origin ?? '*',
|
|
14
|
+
'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
|
15
|
+
'access-control-allow-headers': 'Authorization, Content-Type',
|
|
16
|
+
'access-control-allow-private-network': 'true',
|
|
17
|
+
'access-control-max-age': '86400',
|
|
18
|
+
vary: 'Origin',
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function sendJson(req, res, status, body) {
|
|
22
|
+
res.writeHead(status, {
|
|
23
|
+
'content-type': 'application/json',
|
|
24
|
+
'cache-control': 'no-store',
|
|
25
|
+
...corsHeaders(req),
|
|
26
|
+
});
|
|
27
|
+
res.end(JSON.stringify(body));
|
|
28
|
+
}
|
|
29
|
+
function holdRequest(req, res, info, onStart) {
|
|
30
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
31
|
+
if (req.method === 'OPTIONS') {
|
|
32
|
+
res.writeHead(204, corsHeaders(req)).end();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (path === '/api/mode' && req.method === 'GET') {
|
|
36
|
+
sendJson(req, res, 200, {
|
|
37
|
+
mode: 'local',
|
|
38
|
+
owner: info.owner,
|
|
39
|
+
version: info.version,
|
|
40
|
+
stopped: true,
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (path === '/api/start') {
|
|
45
|
+
if (req.method !== 'POST') {
|
|
46
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
sendJson(req, res, 200, { starting: true });
|
|
50
|
+
onStart();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (path === '/health' || path === '/healthz') {
|
|
54
|
+
sendJson(req, res, STOPPED, { status: 'stopped', version: info.version });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
sendJson(req, res, STOPPED, { error: STOPPED_MESSAGE, stopped: true });
|
|
58
|
+
}
|
|
59
|
+
export const holdServer = (info, onStart) => createServer((req, res) => {
|
|
60
|
+
holdRequest(req, res, info, onStart);
|
|
61
|
+
});
|
|
62
|
+
const lastLine = (chunk) => String(chunk).trim().split('\n').at(-1) ?? '';
|
|
63
|
+
class HeldFunnel {
|
|
64
|
+
bin;
|
|
65
|
+
port;
|
|
66
|
+
log;
|
|
67
|
+
child = null;
|
|
68
|
+
closed = false;
|
|
69
|
+
timer = null;
|
|
70
|
+
exits = 0;
|
|
71
|
+
constructor(bin, port, log) {
|
|
72
|
+
this.bin = bin;
|
|
73
|
+
this.port = port;
|
|
74
|
+
this.log = log;
|
|
75
|
+
}
|
|
76
|
+
start() {
|
|
77
|
+
if (this.closed)
|
|
78
|
+
return;
|
|
79
|
+
const child = spawn(this.bin, ['funnel', String(this.port)], { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
80
|
+
this.child = child;
|
|
81
|
+
let last = '';
|
|
82
|
+
const read = (chunk) => {
|
|
83
|
+
const line = lastLine(chunk);
|
|
84
|
+
if (line !== '')
|
|
85
|
+
last = line;
|
|
86
|
+
};
|
|
87
|
+
child.stdout?.on('data', read);
|
|
88
|
+
child.stderr?.on('data', read);
|
|
89
|
+
child.on('error', (err) => {
|
|
90
|
+
this.child = null;
|
|
91
|
+
this.closed = true;
|
|
92
|
+
this.log(`funnel could not start (${err.message}); the loopback address is still held`);
|
|
93
|
+
});
|
|
94
|
+
child.on('exit', () => {
|
|
95
|
+
this.child = null;
|
|
96
|
+
if (this.closed)
|
|
97
|
+
return;
|
|
98
|
+
this.exits += 1;
|
|
99
|
+
if (this.exits > 1)
|
|
100
|
+
this.log(`funnel exited (${last}); retrying in ${String(FUNNEL_RETRY_MS / 1000)}s`);
|
|
101
|
+
this.timer = setTimeout(() => {
|
|
102
|
+
this.start();
|
|
103
|
+
}, FUNNEL_RETRY_MS);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async stop() {
|
|
107
|
+
this.closed = true;
|
|
108
|
+
if (this.timer !== null)
|
|
109
|
+
clearTimeout(this.timer);
|
|
110
|
+
const child = this.child;
|
|
111
|
+
this.child = null;
|
|
112
|
+
if (child === null)
|
|
113
|
+
return;
|
|
114
|
+
if (child.exitCode !== null || child.signalCode !== null)
|
|
115
|
+
return;
|
|
116
|
+
await new Promise((resolve) => {
|
|
117
|
+
const kill = setTimeout(() => {
|
|
118
|
+
child.kill('SIGKILL');
|
|
119
|
+
}, FUNNEL_EXIT_WAIT_MS);
|
|
120
|
+
child.once('exit', () => {
|
|
121
|
+
clearTimeout(kill);
|
|
122
|
+
resolve();
|
|
123
|
+
});
|
|
124
|
+
child.kill('SIGINT');
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function listen(server, info) {
|
|
129
|
+
for (let attempt = 1;; attempt += 1) {
|
|
130
|
+
try {
|
|
131
|
+
await new Promise((resolve, reject) => {
|
|
132
|
+
server.once('error', reject);
|
|
133
|
+
server.listen(info.port, info.host, () => {
|
|
134
|
+
server.off('error', reject);
|
|
135
|
+
resolve();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
if (attempt >= LISTEN_ATTEMPTS)
|
|
142
|
+
throw err;
|
|
143
|
+
await wait(LISTEN_RETRY_MS);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const CLOSE_WAIT_MS = 2_000;
|
|
148
|
+
const close = (server) => new Promise((resolve) => {
|
|
149
|
+
const timer = setTimeout(resolve, CLOSE_WAIT_MS);
|
|
150
|
+
server.close(() => {
|
|
151
|
+
clearTimeout(timer);
|
|
152
|
+
resolve();
|
|
153
|
+
});
|
|
154
|
+
server.closeAllConnections();
|
|
155
|
+
});
|
|
156
|
+
function releaseLock(lockFile) {
|
|
157
|
+
try {
|
|
158
|
+
if (readFileSync(lockFile, 'utf8').trim() === String(process.pid))
|
|
159
|
+
rmSync(lockFile, { force: true });
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function armFunnel(info, log, ending) {
|
|
166
|
+
if (info.funnel === null || ending !== null)
|
|
167
|
+
return null;
|
|
168
|
+
const funnel = new HeldFunnel(info.funnel, info.port, log);
|
|
169
|
+
funnel.start();
|
|
170
|
+
return funnel;
|
|
171
|
+
}
|
|
172
|
+
export const holdBanner = (info) => `metro is stopped. Holding http://${info.host}:${String(info.port)}${info.funnel === null ? '' : ' and the Funnel address'} ` +
|
|
173
|
+
'until Start on the Server page; Ctrl-C or metro stop ends metro serve';
|
|
174
|
+
export async function holdUntilStart(info, deps = {}) {
|
|
175
|
+
const signals = deps.signals ?? process;
|
|
176
|
+
const log = deps.log ??
|
|
177
|
+
((line) => {
|
|
178
|
+
process.stderr.write(`${line}\n`);
|
|
179
|
+
});
|
|
180
|
+
let finish = () => undefined;
|
|
181
|
+
let ending = null;
|
|
182
|
+
const ended = new Promise((resolve) => {
|
|
183
|
+
finish = (end) => {
|
|
184
|
+
ending ??= end;
|
|
185
|
+
resolve(end);
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
const server = holdServer(info, () => {
|
|
189
|
+
finish('start');
|
|
190
|
+
});
|
|
191
|
+
const onSignal = () => {
|
|
192
|
+
finish('exit');
|
|
193
|
+
};
|
|
194
|
+
signals.on('SIGINT', onSignal);
|
|
195
|
+
signals.on('SIGTERM', onSignal);
|
|
196
|
+
if (info.lockFile !== null)
|
|
197
|
+
writeFileSync(info.lockFile, String(process.pid));
|
|
198
|
+
await listen(server, info);
|
|
199
|
+
const funnel = armFunnel(info, log, ending);
|
|
200
|
+
if (ending === null)
|
|
201
|
+
log(holdBanner(info));
|
|
202
|
+
const end = await ended;
|
|
203
|
+
signals.off('SIGINT', onSignal);
|
|
204
|
+
signals.off('SIGTERM', onSignal);
|
|
205
|
+
await funnel?.stop();
|
|
206
|
+
await close(server);
|
|
207
|
+
if (info.lockFile !== null)
|
|
208
|
+
releaseLock(info.lockFile);
|
|
209
|
+
return end;
|
|
210
|
+
}
|
package/dist/local.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { localUrl } from './runtime.js';
|
|
5
|
+
const PROBE_MS = 3_000;
|
|
6
|
+
export function agentsDir() {
|
|
7
|
+
const explicit = process.env.METRO_AGENTS_DIR?.trim();
|
|
8
|
+
return explicit !== undefined && explicit !== '' ? explicit : join(homedir(), '.metro', 'agents');
|
|
9
|
+
}
|
|
10
|
+
function agentFile(dir) {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(readFileSync(join(dir, 'agent.json'), 'utf8'));
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function localAgent(dir = agentsDir()) {
|
|
19
|
+
const file = agentFile(dir);
|
|
20
|
+
return typeof file?.id === 'string' && typeof file.key === 'string' ? { id: file.id, key: file.key } : null;
|
|
21
|
+
}
|
|
22
|
+
export function localStations(dir = agentsDir()) {
|
|
23
|
+
const stations = agentFile(dir)?.stations;
|
|
24
|
+
if (!Array.isArray(stations))
|
|
25
|
+
return [];
|
|
26
|
+
const out = new Set();
|
|
27
|
+
for (const s of stations) {
|
|
28
|
+
const station = s.station;
|
|
29
|
+
if (typeof station === 'string')
|
|
30
|
+
out.add(station);
|
|
31
|
+
}
|
|
32
|
+
return [...out].sort();
|
|
33
|
+
}
|
|
34
|
+
export async function localDaemonUp(base = localUrl()) {
|
|
35
|
+
try {
|
|
36
|
+
const res = await fetch(`${base}/api/mode`, { signal: AbortSignal.timeout(PROBE_MS) });
|
|
37
|
+
const body = (await res.json());
|
|
38
|
+
return res.ok && body.mode === 'local';
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
const METRO_SERVER = 'metro';
|
|
5
|
+
export function metroMcpConfig(agentKey, port) {
|
|
6
|
+
const servers = {
|
|
7
|
+
[METRO_SERVER]: {
|
|
8
|
+
type: 'http',
|
|
9
|
+
url: `http://127.0.0.1:${String(port)}/mcp`,
|
|
10
|
+
headers: { Authorization: `Bearer ${agentKey}` },
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
return `${JSON.stringify({ mcpServers: servers }, null, 2)}\n`;
|
|
14
|
+
}
|
|
15
|
+
export function writeMcpConfig(agentKey, port, base = tmpdir()) {
|
|
16
|
+
const dir = mkdtempSync(join(base, 'metro-claude-'));
|
|
17
|
+
const path = join(dir, 'mcp.json');
|
|
18
|
+
writeFileSync(path, metroMcpConfig(agentKey, port), { mode: 0o600 });
|
|
19
|
+
return {
|
|
20
|
+
path,
|
|
21
|
+
cleanup: () => {
|
|
22
|
+
rmSync(dir, { recursive: true, force: true });
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { agentsDir } from './local.js';
|
|
6
|
+
const NODE_FILE = '.node';
|
|
7
|
+
const ALPHABET = 'abcdefghjkmnpqrstuvwxyz23456789';
|
|
8
|
+
const NAME_RE = /^metro-[a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])?$/;
|
|
9
|
+
const RENAME_WAIT_MS = 30_000;
|
|
10
|
+
const RENAME_POLL_MS = 500;
|
|
11
|
+
const renameWaitMs = () => Number(process.env.METRO_NODE_RENAME_WAIT_MS) || RENAME_WAIT_MS;
|
|
12
|
+
const pause = (ms) => {
|
|
13
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
14
|
+
};
|
|
15
|
+
const stderrLine = (line) => {
|
|
16
|
+
process.stderr.write(`${line}\n`);
|
|
17
|
+
};
|
|
18
|
+
export function newNodeName() {
|
|
19
|
+
const bytes = randomBytes(6);
|
|
20
|
+
return `metro-${[...bytes].map((b) => ALPHABET[b % ALPHABET.length] ?? 'x').join('')}`;
|
|
21
|
+
}
|
|
22
|
+
export function nodeName(dir = agentsDir()) {
|
|
23
|
+
const path = join(dir, NODE_FILE);
|
|
24
|
+
if (existsSync(path)) {
|
|
25
|
+
const held = readFileSync(path, 'utf8').trim();
|
|
26
|
+
if (NAME_RE.test(held))
|
|
27
|
+
return held;
|
|
28
|
+
}
|
|
29
|
+
const name = newNodeName();
|
|
30
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
31
|
+
writeFileSync(path, `${name}\n`, { mode: 0o600 });
|
|
32
|
+
return name;
|
|
33
|
+
}
|
|
34
|
+
export function currentNodeLabel(bin) {
|
|
35
|
+
const run = spawnSync(bin, ['status', '--json'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
36
|
+
if (run.error !== undefined || run.status !== 0)
|
|
37
|
+
return null;
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(run.stdout);
|
|
40
|
+
const dns = typeof parsed.Self?.DNSName === 'string' ? parsed.Self.DNSName : '';
|
|
41
|
+
const label = dns.split('.')[0] ?? '';
|
|
42
|
+
return label === '' ? null : label.toLowerCase();
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function awaitRename(bin, wanted, warn) {
|
|
49
|
+
const wait = renameWaitMs();
|
|
50
|
+
const until = Date.now() + wait;
|
|
51
|
+
while (Date.now() < until) {
|
|
52
|
+
if (currentNodeLabel(bin) === wanted)
|
|
53
|
+
return;
|
|
54
|
+
pause(RENAME_POLL_MS);
|
|
55
|
+
}
|
|
56
|
+
warn(`tailscale still reports the old machine name after ${String(Math.round(wait / 1000))}s; the Funnel address may come up under it until the next restart`);
|
|
57
|
+
}
|
|
58
|
+
export function ensureNodeName(bin, dir = agentsDir(), warn = stderrLine) {
|
|
59
|
+
const wanted = nodeName(dir);
|
|
60
|
+
if (currentNodeLabel(bin) === wanted)
|
|
61
|
+
return wanted;
|
|
62
|
+
const run = spawnSync(bin, ['set', '--hostname', wanted], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
|
|
63
|
+
if (run.error !== undefined || run.status !== 0) {
|
|
64
|
+
const said = run.stderr.trim();
|
|
65
|
+
const reason = said === '' ? (run.error?.message ?? `exit ${String(run.status)}`) : said;
|
|
66
|
+
throw new Error(`could not name this machine ${wanted} on your tailnet (${reason}).\n` +
|
|
67
|
+
`Run it once yourself, then start again: sudo tailscale set --hostname ${wanted}`);
|
|
68
|
+
}
|
|
69
|
+
awaitRename(bin, wanted, warn);
|
|
70
|
+
return wanted;
|
|
71
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
const FILE = '.claude.json';
|
|
5
|
+
const FLAG = 'hasCompletedOnboarding';
|
|
6
|
+
const given = (value) => value !== undefined && value.trim() !== '' ? value.trim() : undefined;
|
|
7
|
+
export function claudeConfigPath(env = process.env) {
|
|
8
|
+
return join(given(env.CLAUDE_CONFIG_DIR) ?? given(env.HOME) ?? homedir(), FILE);
|
|
9
|
+
}
|
|
10
|
+
function readConfig(path) {
|
|
11
|
+
if (!existsSync(path))
|
|
12
|
+
return {};
|
|
13
|
+
try {
|
|
14
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
15
|
+
return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed) ? parsed : null;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function markOnboardingDone(path = claudeConfigPath()) {
|
|
22
|
+
const config = readConfig(path);
|
|
23
|
+
if (config === null)
|
|
24
|
+
return 'unreadable';
|
|
25
|
+
if (config[FLAG] === true)
|
|
26
|
+
return 'already';
|
|
27
|
+
writeFileSync(path, `${JSON.stringify({ ...config, [FLAG]: true }, null, 2)}\n`, { mode: 0o600 });
|
|
28
|
+
return 'marked';
|
|
29
|
+
}
|
|
30
|
+
const FEATURES = 'cachedGrowthBookFeatures';
|
|
31
|
+
const CHANNEL_FLAGS = ['tengu_harbor', 'tengu_harbor_permissions'];
|
|
32
|
+
const isRecord = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
33
|
+
export function seedChannels(path = claudeConfigPath()) {
|
|
34
|
+
const config = readConfig(path);
|
|
35
|
+
if (config === null)
|
|
36
|
+
return 'unreadable';
|
|
37
|
+
const features = isRecord(config[FEATURES]) ? config[FEATURES] : {};
|
|
38
|
+
if (CHANNEL_FLAGS.every((flag) => features[flag] === true))
|
|
39
|
+
return 'already';
|
|
40
|
+
const seeded = Object.fromEntries(CHANNEL_FLAGS.map((flag) => [flag, true]));
|
|
41
|
+
const next = { ...config, [FEATURES]: { ...features, ...seeded } };
|
|
42
|
+
writeFileSync(path, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
|
|
43
|
+
return 'marked';
|
|
44
|
+
}
|
package/dist/route.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { agentsDir } from './local.js';
|
|
4
|
+
const PROVIDERS = new Set(['bedrock', 'openrouter', 'codex', 'gemini']);
|
|
5
|
+
const text = (value) => (typeof value === 'string' ? value : '');
|
|
6
|
+
function routeIn(cfg) {
|
|
7
|
+
if (typeof cfg !== 'object' || cfg === null)
|
|
8
|
+
return null;
|
|
9
|
+
const { route, connections } = cfg;
|
|
10
|
+
if (!Array.isArray(connections))
|
|
11
|
+
return null;
|
|
12
|
+
const conn = connections.find((c) => typeof c === 'object' && c !== null && c.id === route);
|
|
13
|
+
if (typeof conn !== 'object' || conn === null)
|
|
14
|
+
return null;
|
|
15
|
+
const provider = text(conn.provider);
|
|
16
|
+
const model = text(conn.model);
|
|
17
|
+
return PROVIDERS.has(provider) && model !== '' ? `${provider}:${model}` : null;
|
|
18
|
+
}
|
|
19
|
+
export function currentRoute(dir = agentsDir()) {
|
|
20
|
+
const path = join(dir, 'model.json');
|
|
21
|
+
if (!existsSync(path))
|
|
22
|
+
return null;
|
|
23
|
+
try {
|
|
24
|
+
return routeIn(JSON.parse(readFileSync(path, 'utf8')));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function routeModelEnv(env, route) {
|
|
31
|
+
if (route === null || (env.ANTHROPIC_MODEL ?? '').trim() !== '')
|
|
32
|
+
return env;
|
|
33
|
+
return { ...env, ANTHROPIC_MODEL: route };
|
|
34
|
+
}
|
|
35
|
+
export function permissionMode(dir = agentsDir()) {
|
|
36
|
+
const path = join(dir, 'claude-setup.json');
|
|
37
|
+
if (!existsSync(path))
|
|
38
|
+
return 'auto';
|
|
39
|
+
try {
|
|
40
|
+
const state = JSON.parse(readFileSync(path, 'utf8'));
|
|
41
|
+
return state.permissionMode === 'bypass' ? 'bypass' : 'auto';
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return 'auto';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function systemPrompt(dir = agentsDir()) {
|
|
48
|
+
const path = join(dir, 'system-prompt.md');
|
|
49
|
+
if (!existsSync(path))
|
|
50
|
+
return null;
|
|
51
|
+
try {
|
|
52
|
+
const text = readFileSync(path, 'utf8').trim();
|
|
53
|
+
return text === '' ? null : text;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { copyFileSync, cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { localStations } from './local.js';
|
|
6
|
+
import { STORE_ENTRY, daemonEntry, findBun, runtimeDir } from './runtime.js';
|
|
7
|
+
const MANIFEST_FILE = 'stations.json';
|
|
8
|
+
const MARKETPLACE = 'marketplace';
|
|
9
|
+
const METRO_SOURCES = join('node_modules', '@metro-labs');
|
|
10
|
+
const INSTALL_TIMEOUT_MS = 15 * 60_000;
|
|
11
|
+
export function runtimeStore() {
|
|
12
|
+
const env = process.env.METRO_RUNTIME_STORE?.trim() ?? '';
|
|
13
|
+
return env === '' ? join(homedir(), '.metro', 'runtime') : env;
|
|
14
|
+
}
|
|
15
|
+
const isRecord = (v) => typeof v === 'object' && v !== null;
|
|
16
|
+
const ranges = (raw) => isRecord(raw)
|
|
17
|
+
? Object.fromEntries(Object.entries(raw).filter((e) => typeof e[1] === 'string'))
|
|
18
|
+
: {};
|
|
19
|
+
export function readManifest(path) {
|
|
20
|
+
const raw = JSON.parse(readFileSync(path, 'utf8'));
|
|
21
|
+
if (!isRecord(raw))
|
|
22
|
+
throw new Error(`${path} is not a runtime manifest`);
|
|
23
|
+
const stations = isRecord(raw.stations) ? raw.stations : {};
|
|
24
|
+
return {
|
|
25
|
+
core: ranges(raw.core),
|
|
26
|
+
stations: Object.fromEntries(Object.entries(stations).map(([name, deps]) => [name, ranges(deps)])),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function dependenciesFor(manifest, stations) {
|
|
30
|
+
const out = { ...manifest.core };
|
|
31
|
+
for (const station of new Set(stations))
|
|
32
|
+
Object.assign(out, manifest.stations[station] ?? {});
|
|
33
|
+
return Object.fromEntries(Object.entries(out).sort(([a], [b]) => a.localeCompare(b)));
|
|
34
|
+
}
|
|
35
|
+
const packageText = (deps) => `${JSON.stringify({ name: 'metro-runtime', private: true, dependencies: deps }, null, 2)}\n`;
|
|
36
|
+
function readOrNull(path) {
|
|
37
|
+
try {
|
|
38
|
+
return readFileSync(path, 'utf8');
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function copyMarketplace(sources, store) {
|
|
45
|
+
const staged = join(sources, MARKETPLACE);
|
|
46
|
+
if (!existsSync(staged))
|
|
47
|
+
return false;
|
|
48
|
+
cpSync(staged, join(store, MARKETPLACE), { recursive: true });
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
function syncSources(sources, store) {
|
|
52
|
+
const shim = join(sources, STORE_ENTRY);
|
|
53
|
+
if (existsSync(shim))
|
|
54
|
+
copyFileSync(shim, join(store, STORE_ENTRY));
|
|
55
|
+
const stamp = readOrNull(join(sources, 'runtime.json'));
|
|
56
|
+
if (stamp !== null && readOrNull(join(store, 'runtime.json')) === stamp)
|
|
57
|
+
return existsSync(join(store, MARKETPLACE)) ? false : copyMarketplace(sources, store);
|
|
58
|
+
rmSync(join(store, METRO_SOURCES), { recursive: true, force: true });
|
|
59
|
+
cpSync(join(sources, METRO_SOURCES), join(store, METRO_SOURCES), { recursive: true });
|
|
60
|
+
rmSync(join(store, MARKETPLACE), { recursive: true, force: true });
|
|
61
|
+
copyMarketplace(sources, store);
|
|
62
|
+
if (stamp !== null)
|
|
63
|
+
writeFileSync(join(store, 'runtime.json'), stamp);
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
export function installDependencies(store, deps, bun, log) {
|
|
67
|
+
const wanted = packageText(deps);
|
|
68
|
+
const marker = join(store, 'node_modules', '.metro-installed');
|
|
69
|
+
if (existsSync(marker) && readOrNull(join(store, 'package.json')) === wanted)
|
|
70
|
+
return false;
|
|
71
|
+
writeFileSync(join(store, 'package.json'), wanted);
|
|
72
|
+
log(`Installing the channel SDKs this machine needs into ${store}`);
|
|
73
|
+
const run = spawnSync(bun, ['install', '--no-summary', '--no-progress'], {
|
|
74
|
+
cwd: store,
|
|
75
|
+
stdio: ['ignore', 'inherit', 'inherit'],
|
|
76
|
+
timeout: INSTALL_TIMEOUT_MS,
|
|
77
|
+
});
|
|
78
|
+
if (run.error !== undefined || run.status !== 0)
|
|
79
|
+
throw new Error(`bun install failed in ${store}: ${run.error?.message ?? `exit ${String(run.status)}`}`);
|
|
80
|
+
writeFileSync(marker, `${new Date().toISOString()}\n`);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
export function prepareRuntime(opts = {}) {
|
|
84
|
+
const sources = opts.sources ?? runtimeDir();
|
|
85
|
+
const manifestPath = join(sources, MANIFEST_FILE);
|
|
86
|
+
const manifest = readManifest(manifestPath);
|
|
87
|
+
const store = opts.store ?? runtimeStore();
|
|
88
|
+
const log = opts.log ??
|
|
89
|
+
((line) => {
|
|
90
|
+
process.stderr.write(`${line}\n`);
|
|
91
|
+
});
|
|
92
|
+
mkdirSync(join(store, 'trains'), { recursive: true });
|
|
93
|
+
syncSources(sources, store);
|
|
94
|
+
installDependencies(store, dependenciesFor(manifest, localStations(opts.agents)), opts.bun ?? findBun(), log);
|
|
95
|
+
return { dir: store, entry: daemonEntry(store), trains: join(store, 'trains'), manifest: manifestPath };
|
|
96
|
+
}
|
package/dist/runtime.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
-
import {
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
|
-
import { existsSync } from 'node:fs';
|
|
6
|
-
import { homedir } from 'node:os';
|
|
7
|
-
import { metroUrl } from './store.js';
|
|
8
5
|
const AGENT_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{10}$/;
|
|
9
|
-
const
|
|
6
|
+
export const STORE_ENTRY = 'server.ts';
|
|
7
|
+
export const daemonEntry = (dir) => join(dir, STORE_ENTRY);
|
|
10
8
|
export class MissingRuntime extends Error {
|
|
11
9
|
}
|
|
12
10
|
export function runtimeDir() {
|
|
@@ -14,11 +12,11 @@ export function runtimeDir() {
|
|
|
14
12
|
const dir = explicit !== undefined && explicit !== ''
|
|
15
13
|
? explicit
|
|
16
14
|
: join(dirname(dirname(fileURLToPath(import.meta.url))), 'runtime');
|
|
17
|
-
if (!existsSync(
|
|
15
|
+
if (!existsSync(daemonEntry(dir)))
|
|
18
16
|
throw new MissingRuntime(`no metro daemon at ${dir}. Reinstall with: npm i -g @stage-labs/metro@beta`);
|
|
19
17
|
return dir;
|
|
20
18
|
}
|
|
21
|
-
|
|
19
|
+
class MissingBun extends Error {
|
|
22
20
|
}
|
|
23
21
|
export function assertAgentId(id) {
|
|
24
22
|
if (id === undefined || !AGENT_RE.test(id))
|
|
@@ -28,54 +26,15 @@ export function assertAgentId(id) {
|
|
|
28
26
|
export function findBun() {
|
|
29
27
|
const found = spawnSync('bun', ['--version'], { stdio: 'ignore' });
|
|
30
28
|
if (found.error !== undefined || found.status !== 0)
|
|
31
|
-
throw new MissingBun('metro
|
|
29
|
+
throw new MissingBun('metro serve needs Bun, which is not on PATH.\n' +
|
|
32
30
|
'Install it with: curl -fsSL https://bun.sh/install | bash');
|
|
33
31
|
return 'bun';
|
|
34
32
|
}
|
|
35
|
-
function tokenPath(agentId) {
|
|
36
|
-
const xdg = process.env.XDG_CONFIG_HOME?.trim();
|
|
37
|
-
const base = xdg === undefined || xdg === '' ? join(homedir(), '.config') : xdg;
|
|
38
|
-
return join(base, 'metro', `runtime-${agentId}.json`);
|
|
39
|
-
}
|
|
40
|
-
export function readRunToken(agentId) {
|
|
41
|
-
const fromEnv = process.env.METRO_RUN_TOKEN?.trim();
|
|
42
|
-
if (fromEnv !== undefined && fromEnv !== '')
|
|
43
|
-
return fromEnv;
|
|
44
|
-
try {
|
|
45
|
-
const parsed = JSON.parse(readFileSync(tokenPath(agentId), 'utf8'));
|
|
46
|
-
if (typeof parsed.token !== 'string' || parsed.token === '')
|
|
47
|
-
return null;
|
|
48
|
-
return parsed.url === metroUrl() ? parsed.token : null;
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
export function writeRunToken(agentId, token) {
|
|
55
|
-
const path = tokenPath(agentId);
|
|
56
|
-
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
57
|
-
writeFileSync(path, JSON.stringify({ token, url: metroUrl() }, null, 2));
|
|
58
|
-
chmodSync(path, 0o600);
|
|
59
|
-
}
|
|
60
33
|
export const localPort = () => Number(process.env.METRO_WEBHOOK_PORT) || 8420;
|
|
61
34
|
export const localUrl = () => `http://127.0.0.1:${String(localPort())}`;
|
|
62
|
-
export
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
args: [SERVER_ENTRY],
|
|
66
|
-
cwd: opts.dir,
|
|
67
|
-
env: {
|
|
68
|
-
...process.env,
|
|
69
|
-
METRO_RUN_TOKEN: opts.token,
|
|
70
|
-
METRO_AGENT: opts.agentId,
|
|
71
|
-
METRO_URL: metroUrl(),
|
|
72
|
-
METRO_HTTP_HOST: process.env.METRO_HTTP_HOST ?? '127.0.0.1',
|
|
73
|
-
METRO_TRAINS_DIR: join(opts.dir, 'trains'),
|
|
74
|
-
},
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
export function runDaemon(agentId, plan) {
|
|
78
|
-
process.stderr.write(`Starting metro for ${agentId}\n`);
|
|
35
|
+
export const RESTART_CODE = 75;
|
|
36
|
+
export const HOLD_CODE = 76;
|
|
37
|
+
function spawnOnce(plan) {
|
|
79
38
|
const child = spawn(plan.command, plan.args, {
|
|
80
39
|
cwd: plan.cwd,
|
|
81
40
|
env: plan.env,
|
|
@@ -84,11 +43,29 @@ export function runDaemon(agentId, plan) {
|
|
|
84
43
|
const relay = (sig) => () => {
|
|
85
44
|
child.kill(sig);
|
|
86
45
|
};
|
|
87
|
-
|
|
88
|
-
|
|
46
|
+
const onInt = relay('SIGINT');
|
|
47
|
+
const onTerm = relay('SIGTERM');
|
|
48
|
+
process.on('SIGINT', onInt);
|
|
49
|
+
process.on('SIGTERM', onTerm);
|
|
89
50
|
return new Promise((resolve) => {
|
|
90
51
|
child.on('exit', (code) => {
|
|
52
|
+
process.off('SIGINT', onInt);
|
|
53
|
+
process.off('SIGTERM', onTerm);
|
|
91
54
|
resolve(code ?? 0);
|
|
92
55
|
});
|
|
93
56
|
});
|
|
94
57
|
}
|
|
58
|
+
export async function spawnPlan(plan, hold) {
|
|
59
|
+
for (;;) {
|
|
60
|
+
const code = await spawnOnce(plan());
|
|
61
|
+
if (code === RESTART_CODE) {
|
|
62
|
+
process.stderr.write('metro was updated; restarting the daemon on the new version\n');
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (code !== HOLD_CODE || hold === undefined)
|
|
66
|
+
return code;
|
|
67
|
+
if ((await hold()) === 'exit')
|
|
68
|
+
return 0;
|
|
69
|
+
process.stderr.write('starting the daemon again\n');
|
|
70
|
+
}
|
|
71
|
+
}
|