@stage-labs/metro 0.1.0-beta.13 → 0.1.0-beta.130
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +14 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +176 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +93 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +43 -0
- package/dist/plugin.js +120 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +38 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +150 -0
- package/dist/service.js +242 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -27
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +18 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +55 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +122 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/marketplace/plugin/orchestrator.md +16 -0
- package/runtime/marketplace/plugin/skills/refresh/SKILL.md +15 -0
- package/runtime/node_modules/@metro-labs/core/package.json +43 -0
- package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +87 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +108 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +365 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +300 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +371 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +95 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +309 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +169 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +140 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +253 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +239 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +154 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +310 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +175 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/json.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +171 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +296 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +286 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +332 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/watch.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +259 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +187 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +269 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +151 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +372 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/identity.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +261 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/http/package.json +33 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +219 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +130 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +132 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +123 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +29 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +10 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +190 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +347 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +18 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +39 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -252
- package/dist/broker.js +0 -202
- package/dist/cache.js +0 -69
- package/dist/cli/actions.js +0 -206
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -188
- package/dist/cli/skill.js +0 -62
- package/dist/cli/tail.js +0 -164
- package/dist/cli/util.js +0 -72
- package/dist/cli/webhook.js +0 -81
- package/dist/codex-rc.js +0 -245
- package/dist/dispatcher.js +0 -128
- package/dist/history.js +0 -121
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/monitor.js +0 -194
- package/dist/paths.js +0 -68
- package/dist/registry.js +0 -48
- package/dist/stations/claude.js +0 -45
- package/dist/stations/codex.js +0 -68
- package/dist/stations/discord.js +0 -216
- package/dist/stations/index.js +0 -129
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -234
- package/dist/stations/webhook.js +0 -103
- package/dist/tunnel.js +0 -64
- package/dist/webhooks.js +0 -41
- package/docs/broker.md +0 -214
- package/docs/monitor.md +0 -163
- package/docs/uri-scheme.md +0 -124
- package/docs/users.md +0 -226
- package/skills/metro/SKILL.md +0 -256
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { log } from '@metro-labs/core/log';
|
|
5
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
6
|
+
import type { StationName } from '@metro-labs/core/station-names';
|
|
7
|
+
import { knownAccounts } from '../agents/map.js';
|
|
8
|
+
|
|
9
|
+
const INSTALL_TIMEOUT_MS = 15 * 60_000;
|
|
10
|
+
|
|
11
|
+
export interface RuntimeManifest {
|
|
12
|
+
core: Record<string, string>;
|
|
13
|
+
stations: Record<string, Record<string, string>>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface RuntimeStore {
|
|
17
|
+
dir: string;
|
|
18
|
+
manifest: RuntimeManifest;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ranges = (raw: unknown): Record<string, string> =>
|
|
22
|
+
isRecord(raw)
|
|
23
|
+
? Object.fromEntries(Object.entries(raw).filter((e): e is [string, string] => typeof e[1] === 'string'))
|
|
24
|
+
: {};
|
|
25
|
+
|
|
26
|
+
export function readManifest(path: string): RuntimeManifest {
|
|
27
|
+
const raw: unknown = JSON.parse(readFileSync(path, 'utf8'));
|
|
28
|
+
if (!isRecord(raw)) throw new Error(`${path} is not a runtime manifest`);
|
|
29
|
+
const stations = isRecord(raw.stations) ? raw.stations : {};
|
|
30
|
+
return {
|
|
31
|
+
core: ranges(raw.core),
|
|
32
|
+
stations: Object.fromEntries(Object.entries(stations).map(([name, deps]) => [name, ranges(deps)])),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function runtimeStore(): RuntimeStore | null {
|
|
37
|
+
const dir = process.env.METRO_RUNTIME_STORE?.trim() ?? '';
|
|
38
|
+
const manifest = process.env.METRO_RUNTIME_MANIFEST?.trim() ?? '';
|
|
39
|
+
if (dir === '' || manifest === '') return null;
|
|
40
|
+
return { dir, manifest: readManifest(manifest) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function dependenciesFor(manifest: RuntimeManifest, stations: Iterable<string>): Record<string, string> {
|
|
44
|
+
const out: Record<string, string> = { ...manifest.core };
|
|
45
|
+
for (const station of new Set(stations)) Object.assign(out, manifest.stations[station] ?? {});
|
|
46
|
+
return Object.fromEntries(Object.entries(out).sort(([a], [b]) => a.localeCompare(b)));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const packageText = (deps: Record<string, string>): string =>
|
|
50
|
+
`${JSON.stringify({ name: 'metro-runtime', private: true, dependencies: deps }, null, 2)}\n`;
|
|
51
|
+
|
|
52
|
+
function current(dir: string): string | null {
|
|
53
|
+
try {
|
|
54
|
+
return readFileSync(join(dir, 'package.json'), 'utf8');
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function installRuntime(store: RuntimeStore, stations: Iterable<string>): boolean {
|
|
61
|
+
const wanted = packageText(dependenciesFor(store.manifest, stations));
|
|
62
|
+
const installed = existsSync(join(store.dir, 'node_modules', '.metro-installed'));
|
|
63
|
+
if (installed && current(store.dir) === wanted) return false;
|
|
64
|
+
mkdirSync(store.dir, { recursive: true });
|
|
65
|
+
writeFileSync(join(store.dir, 'package.json'), wanted);
|
|
66
|
+
log.info({ dir: store.dir }, 'runtime: installing the channel SDKs this machine needs');
|
|
67
|
+
const run = spawnSync('bun', ['install', '--no-summary', '--no-progress'], {
|
|
68
|
+
cwd: store.dir,
|
|
69
|
+
stdio: ['ignore', 'inherit', 'inherit'],
|
|
70
|
+
timeout: INSTALL_TIMEOUT_MS,
|
|
71
|
+
});
|
|
72
|
+
if (run.error !== undefined || run.status !== 0)
|
|
73
|
+
throw new Error(`bun install failed in ${store.dir}: ${run.error?.message ?? `exit ${String(run.status)}`}`);
|
|
74
|
+
writeFileSync(join(store.dir, 'node_modules', '.metro-installed'), `${new Date().toISOString()}\n`);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function ensureStationDeps(station: StationName): void {
|
|
79
|
+
const store = runtimeStore();
|
|
80
|
+
if (store === null) return;
|
|
81
|
+
installRuntime(store, [...knownAccounts().map((a) => a.station), station]);
|
|
82
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { watch, type FSWatcher } from 'node:fs';
|
|
2
|
+
import { join, parse as parsePath } from 'node:path';
|
|
3
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
4
|
+
import type { Pending } from '@metro-labs/core/trains/protocol';
|
|
5
|
+
|
|
6
|
+
export const RESTART_BACKOFFS_MS = [1_000, 5_000, 30_000] as const;
|
|
7
|
+
export const MAX_CONSECUTIVE_FAILS = 5;
|
|
8
|
+
export const HOT_RELOAD_DEBOUNCE_MS = 300;
|
|
9
|
+
export const TRAIN_EXT = /\.(ts|js|mjs)$/;
|
|
10
|
+
|
|
11
|
+
export interface TrainState {
|
|
12
|
+
name: string;
|
|
13
|
+
path: string;
|
|
14
|
+
proc: ReturnType<typeof Bun.spawn> | null;
|
|
15
|
+
pending: Map<string, Pending>;
|
|
16
|
+
buf: string;
|
|
17
|
+
errBuf: string;
|
|
18
|
+
failCount: number;
|
|
19
|
+
restartTimer: ReturnType<typeof setTimeout> | null;
|
|
20
|
+
startedAt: string | null;
|
|
21
|
+
stopped: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function startWatcher(
|
|
25
|
+
dir: string,
|
|
26
|
+
timers: Map<string, ReturnType<typeof setTimeout>>,
|
|
27
|
+
onChange: (name: string, path: string) => void,
|
|
28
|
+
): FSWatcher | null {
|
|
29
|
+
try {
|
|
30
|
+
const w = watch(dir, (_event, filename) => {
|
|
31
|
+
if (!filename) return;
|
|
32
|
+
const base = filename;
|
|
33
|
+
if (!TRAIN_EXT.test(base) || base.startsWith('_') || base.startsWith('.'))
|
|
34
|
+
return;
|
|
35
|
+
const name = parsePath(base).name;
|
|
36
|
+
const existing = timers.get(name);
|
|
37
|
+
if (existing) clearTimeout(existing);
|
|
38
|
+
timers.set(
|
|
39
|
+
name,
|
|
40
|
+
setTimeout(() => {
|
|
41
|
+
timers.delete(name);
|
|
42
|
+
onChange(name, join(dir, base));
|
|
43
|
+
}, HOT_RELOAD_DEBOUNCE_MS),
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
w.on('error', (err) => {
|
|
47
|
+
log.warn({ err: errMsg(err) }, 'train hot-reload: watcher error');
|
|
48
|
+
});
|
|
49
|
+
log.info({ dir }, 'train hot-reload: watching');
|
|
50
|
+
return w;
|
|
51
|
+
} catch (err) {
|
|
52
|
+
log.warn(
|
|
53
|
+
{ err: errMsg(err) },
|
|
54
|
+
'train hot-reload: watch unavailable (on-demand restart only)',
|
|
55
|
+
);
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function killGracefully(
|
|
61
|
+
proc: ReturnType<typeof Bun.spawn> | null,
|
|
62
|
+
): Promise<unknown> | null {
|
|
63
|
+
if (proc?.exitCode !== null) return null;
|
|
64
|
+
try {
|
|
65
|
+
proc.kill('SIGTERM');
|
|
66
|
+
} catch {
|
|
67
|
+
}
|
|
68
|
+
const grace = setTimeout(() => {
|
|
69
|
+
try {
|
|
70
|
+
proc.kill('SIGKILL');
|
|
71
|
+
} catch {
|
|
72
|
+
}
|
|
73
|
+
}, 2_000);
|
|
74
|
+
return proc.exited.finally(() => {
|
|
75
|
+
clearTimeout(grace);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function pumpStream(
|
|
80
|
+
stream: unknown,
|
|
81
|
+
name: string,
|
|
82
|
+
kind: 'stdout' | 'stderr',
|
|
83
|
+
onChunk: (s: string) => void,
|
|
84
|
+
): Promise<void> {
|
|
85
|
+
if (!stream || typeof stream === 'number') return;
|
|
86
|
+
const reader = (stream as ReadableStream<Uint8Array>).getReader();
|
|
87
|
+
const dec = new TextDecoder();
|
|
88
|
+
try {
|
|
89
|
+
while (true) {
|
|
90
|
+
const { value, done } = await reader.read();
|
|
91
|
+
if (done) break;
|
|
92
|
+
onChunk(dec.decode(value, { stream: true }));
|
|
93
|
+
}
|
|
94
|
+
} catch (err) {
|
|
95
|
+
log.debug({ name, err: errMsg(err) }, `train: ${kind} pump ended`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import { mkdirSync, statSync, type FSWatcher } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
4
|
+
import { daemonSelf } from '@metro-labs/core/events';
|
|
5
|
+
import {
|
|
6
|
+
drainLines,
|
|
7
|
+
failAllPending,
|
|
8
|
+
listTrainFiles,
|
|
9
|
+
mintCallId,
|
|
10
|
+
parseTrainLine,
|
|
11
|
+
sendCall,
|
|
12
|
+
type TrainCallResponse,
|
|
13
|
+
type TrainEvent,
|
|
14
|
+
} from '@metro-labs/core/trains/protocol';
|
|
15
|
+
import {
|
|
16
|
+
HOT_RELOAD_DEBOUNCE_MS,
|
|
17
|
+
MAX_CONSECUTIVE_FAILS,
|
|
18
|
+
RESTART_BACKOFFS_MS,
|
|
19
|
+
killGracefully,
|
|
20
|
+
pumpStream,
|
|
21
|
+
startWatcher,
|
|
22
|
+
type TrainState,
|
|
23
|
+
} from './supervisor-io.js';
|
|
24
|
+
import { trainsDir } from '../boot/paths.js';
|
|
25
|
+
|
|
26
|
+
const DYLD_FALLBACK_ENV: Record<string, string> =
|
|
27
|
+
process.platform === 'darwin'
|
|
28
|
+
? {
|
|
29
|
+
DYLD_FALLBACK_LIBRARY_PATH: [
|
|
30
|
+
'/usr/lib',
|
|
31
|
+
process.env.DYLD_FALLBACK_LIBRARY_PATH,
|
|
32
|
+
]
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
.join(':'),
|
|
35
|
+
}
|
|
36
|
+
: {};
|
|
37
|
+
|
|
38
|
+
export class TrainSupervisor {
|
|
39
|
+
private trains = new Map<string, TrainState>();
|
|
40
|
+
private onEvent: ((event: TrainEvent, train: string) => void) | null = null;
|
|
41
|
+
private nextCallId = 1;
|
|
42
|
+
private watcher: FSWatcher | null = null;
|
|
43
|
+
private reloadTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
44
|
+
|
|
45
|
+
constructor(private dir: string = trainsDir()) {}
|
|
46
|
+
|
|
47
|
+
running(): string[] {
|
|
48
|
+
return [...this.trains.keys()];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
onTrainEvent(handler: (event: TrainEvent, train: string) => void): void {
|
|
52
|
+
this.onEvent = handler;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
start(): void {
|
|
56
|
+
mkdirSync(this.dir, { recursive: true });
|
|
57
|
+
for (const t of listTrainFiles(this.dir)) this.startTrain(t.name, t.path);
|
|
58
|
+
log.info(
|
|
59
|
+
{ dir: this.dir, count: this.trains.size },
|
|
60
|
+
'train supervisor: started',
|
|
61
|
+
);
|
|
62
|
+
this.watchForReloads();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private watchForReloads(): void {
|
|
66
|
+
this.watcher = startWatcher(this.dir, this.reloadTimers, (name, path) => {
|
|
67
|
+
this.handleSourceChange(name, path);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
requestReload(name: string): void {
|
|
72
|
+
const path = join(this.dir, `${name}.ts`);
|
|
73
|
+
const existing = this.reloadTimers.get(name);
|
|
74
|
+
if (existing) clearTimeout(existing);
|
|
75
|
+
this.reloadTimers.set(
|
|
76
|
+
name,
|
|
77
|
+
setTimeout(() => {
|
|
78
|
+
this.reloadTimers.delete(name);
|
|
79
|
+
this.handleSourceChange(name, path);
|
|
80
|
+
}, HOT_RELOAD_DEBOUNCE_MS),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async stopTrain(name: string): Promise<void> {
|
|
85
|
+
const t = this.trains.get(name);
|
|
86
|
+
if (!t) return;
|
|
87
|
+
const timer = this.reloadTimers.get(name);
|
|
88
|
+
if (timer) {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
this.reloadTimers.delete(name);
|
|
91
|
+
}
|
|
92
|
+
t.stopped = true;
|
|
93
|
+
if (t.restartTimer) {
|
|
94
|
+
clearTimeout(t.restartTimer);
|
|
95
|
+
t.restartTimer = null;
|
|
96
|
+
}
|
|
97
|
+
failAllPending(t.pending, `train '${name}' stopped`);
|
|
98
|
+
const wait = killGracefully(t.proc);
|
|
99
|
+
if (wait) await wait;
|
|
100
|
+
this.trains.delete(name);
|
|
101
|
+
log.info({ name }, 'train: stopped, last account detached');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private handleSourceChange(name: string, path: string): void {
|
|
105
|
+
try {
|
|
106
|
+
if (!statSync(path).isFile()) return;
|
|
107
|
+
} catch {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (this.trains.has(name)) {
|
|
111
|
+
log.info({ name }, 'train hot-reload: source changed, restarting');
|
|
112
|
+
this.restart(name).catch((err: unknown) => {
|
|
113
|
+
log.warn(
|
|
114
|
+
{ name, err: errMsg(err) },
|
|
115
|
+
'train hot-reload: restart failed',
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
log.info({ name }, 'train hot-reload: new train, spawning');
|
|
121
|
+
this.startTrain(name, path);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async stop(): Promise<void> {
|
|
125
|
+
if (this.watcher) {
|
|
126
|
+
try {
|
|
127
|
+
this.watcher.close();
|
|
128
|
+
} catch {
|
|
129
|
+
}
|
|
130
|
+
this.watcher = null;
|
|
131
|
+
}
|
|
132
|
+
for (const timer of this.reloadTimers.values()) clearTimeout(timer);
|
|
133
|
+
this.reloadTimers.clear();
|
|
134
|
+
const tasks: Promise<unknown>[] = [];
|
|
135
|
+
for (const t of this.trains.values()) {
|
|
136
|
+
t.stopped = true;
|
|
137
|
+
if (t.restartTimer) {
|
|
138
|
+
clearTimeout(t.restartTimer);
|
|
139
|
+
t.restartTimer = null;
|
|
140
|
+
}
|
|
141
|
+
failAllPending(t.pending, 'train shutting down');
|
|
142
|
+
const wait = killGracefully(t.proc);
|
|
143
|
+
if (wait) tasks.push(wait);
|
|
144
|
+
}
|
|
145
|
+
await Promise.all(tasks);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async restart(name: string): Promise<void> {
|
|
149
|
+
const t = this.trains.get(name);
|
|
150
|
+
if (!t) throw new Error(`no train named '${name}'`);
|
|
151
|
+
if (t.restartTimer) {
|
|
152
|
+
clearTimeout(t.restartTimer);
|
|
153
|
+
t.restartTimer = null;
|
|
154
|
+
}
|
|
155
|
+
failAllPending(t.pending, `train '${name}' restarting`);
|
|
156
|
+
t.stopped = true;
|
|
157
|
+
const wait = killGracefully(t.proc);
|
|
158
|
+
if (wait) await wait;
|
|
159
|
+
t.stopped = false;
|
|
160
|
+
t.failCount = 0;
|
|
161
|
+
t.proc = null;
|
|
162
|
+
this.spawn(t);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async call(
|
|
166
|
+
name: string,
|
|
167
|
+
action: string,
|
|
168
|
+
args: unknown,
|
|
169
|
+
): Promise<TrainCallResponse> {
|
|
170
|
+
const t = this.trains.get(name);
|
|
171
|
+
if (!t)
|
|
172
|
+
throw new Error(
|
|
173
|
+
`no train named '${name}' (have: ${[...this.trains.keys()].join(', ') || '(none)'})`,
|
|
174
|
+
);
|
|
175
|
+
if (t.proc?.exitCode !== null)
|
|
176
|
+
throw new Error(`train '${name}' is not running`);
|
|
177
|
+
return sendCall(t as never, mintCallId(this.nextCallId++), action, args);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private startTrain(name: string, path: string): void {
|
|
181
|
+
if (this.trains.has(name)) {
|
|
182
|
+
log.warn({ name }, 'train supervisor: duplicate name, skipping');
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const state: TrainState = {
|
|
186
|
+
name,
|
|
187
|
+
path,
|
|
188
|
+
proc: null,
|
|
189
|
+
pending: new Map(),
|
|
190
|
+
buf: '',
|
|
191
|
+
errBuf: '',
|
|
192
|
+
failCount: 0,
|
|
193
|
+
restartTimer: null,
|
|
194
|
+
startedAt: null,
|
|
195
|
+
stopped: false,
|
|
196
|
+
};
|
|
197
|
+
this.trains.set(name, state);
|
|
198
|
+
this.spawn(state);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private spawn(state: TrainState): void {
|
|
202
|
+
if (state.stopped) return;
|
|
203
|
+
try {
|
|
204
|
+
const proc = Bun.spawn(['bun', 'run', state.path], {
|
|
205
|
+
stdin: 'pipe',
|
|
206
|
+
stdout: 'pipe',
|
|
207
|
+
stderr: 'pipe',
|
|
208
|
+
env: {
|
|
209
|
+
...process.env,
|
|
210
|
+
...DYLD_FALLBACK_ENV,
|
|
211
|
+
METRO_TRAIN_NAME: state.name,
|
|
212
|
+
METRO_SELF_URI: daemonSelf(),
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
state.proc = proc;
|
|
216
|
+
state.startedAt = new Date().toISOString();
|
|
217
|
+
state.buf = '';
|
|
218
|
+
state.errBuf = '';
|
|
219
|
+
log.info({ name: state.name, pid: proc.pid }, 'train: spawned');
|
|
220
|
+
const pumpFailed = (err: unknown): void => {
|
|
221
|
+
log.warn({ name: state.name, err: errMsg(err) }, 'train: pump failed');
|
|
222
|
+
};
|
|
223
|
+
this.pumpStdout(state).catch(pumpFailed);
|
|
224
|
+
this.pumpStderr(state).catch(pumpFailed);
|
|
225
|
+
proc.exited
|
|
226
|
+
.then((code) => {
|
|
227
|
+
this.onExit(state, code ?? 0);
|
|
228
|
+
})
|
|
229
|
+
.catch((err: unknown) => {
|
|
230
|
+
log.error(
|
|
231
|
+
{ name: state.name, err: errMsg(err) },
|
|
232
|
+
'train: exit handler failed',
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
} catch (err) {
|
|
236
|
+
log.warn({ name: state.name, err: errMsg(err) }, 'train: spawn failed');
|
|
237
|
+
this.scheduleRestart(state);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private pumpStdout(state: TrainState): Promise<void> {
|
|
242
|
+
return pumpStream(state.proc?.stdout, state.name, 'stdout', (chunk) => {
|
|
243
|
+
state.buf += chunk;
|
|
244
|
+
state.buf = drainLines(state.name, state.buf, (line) => {
|
|
245
|
+
this.handleLine(state, line);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private pumpStderr(state: TrainState): Promise<void> {
|
|
251
|
+
return pumpStream(state.proc?.stderr, state.name, 'stderr', (chunk) => {
|
|
252
|
+
state.errBuf += chunk;
|
|
253
|
+
state.errBuf = drainLines(state.name, state.errBuf, (line) => {
|
|
254
|
+
log.warn({ train: state.name }, line);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private handleLine(state: TrainState, line: string): void {
|
|
260
|
+
const msg = parseTrainLine(state.name, line);
|
|
261
|
+
if (!msg || msg.op === 'ignore') return;
|
|
262
|
+
if (msg.op === 'response') {
|
|
263
|
+
const pending = state.pending.get(msg.id);
|
|
264
|
+
if (!pending) {
|
|
265
|
+
log.debug({ name: state.name, id: msg.id }, 'train: stale response');
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
state.pending.delete(msg.id);
|
|
269
|
+
clearTimeout(pending.timer);
|
|
270
|
+
pending.resolve({
|
|
271
|
+
result: msg.result,
|
|
272
|
+
error: msg.error,
|
|
273
|
+
errorInfo: msg.errorInfo,
|
|
274
|
+
});
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (msg.op === 'log') {
|
|
278
|
+
log.info({ name: state.name, msg: msg.text }, 'train log');
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
this.onEvent?.(msg.event, state.name);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private onExit(state: TrainState, code: number): void {
|
|
285
|
+
log.warn({ name: state.name, code }, 'train: exited');
|
|
286
|
+
state.proc = null;
|
|
287
|
+
state.startedAt = null;
|
|
288
|
+
failAllPending(
|
|
289
|
+
state.pending,
|
|
290
|
+
`train '${state.name}' exited (code=${code}) before responding`,
|
|
291
|
+
);
|
|
292
|
+
if (state.stopped) return;
|
|
293
|
+
state.failCount++;
|
|
294
|
+
if (state.failCount >= MAX_CONSECUTIVE_FAILS) {
|
|
295
|
+
log.error(
|
|
296
|
+
{ name: state.name, fails: state.failCount },
|
|
297
|
+
'train: too many consecutive failures, giving up (restart metro to retry)',
|
|
298
|
+
);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
this.scheduleRestart(state);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private scheduleRestart(state: TrainState): void {
|
|
305
|
+
if (state.stopped) return;
|
|
306
|
+
const idx = Math.min(state.failCount, RESTART_BACKOFFS_MS.length - 1);
|
|
307
|
+
const delay = RESTART_BACKOFFS_MS[idx];
|
|
308
|
+
log.info(
|
|
309
|
+
{ name: state.name, delay, attempt: state.failCount },
|
|
310
|
+
'train: restart scheduled',
|
|
311
|
+
);
|
|
312
|
+
state.restartTimer = setTimeout(() => {
|
|
313
|
+
state.restartTimer = null;
|
|
314
|
+
this.spawn(state);
|
|
315
|
+
setTimeout(() => {
|
|
316
|
+
if (state.proc?.exitCode === null) state.failCount = 0;
|
|
317
|
+
}, 30_000);
|
|
318
|
+
}, delay);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readJson } from '@metro-labs/core/secure-fs';
|
|
2
|
+
import { accountFilePath } from './materialize.js';
|
|
3
|
+
|
|
4
|
+
export interface ThreemaCallback {
|
|
5
|
+
id: string;
|
|
6
|
+
callbackId: string;
|
|
7
|
+
callbackToken: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const str = (v: unknown): string | undefined =>
|
|
11
|
+
typeof v === 'string' && v !== '' ? v : undefined;
|
|
12
|
+
|
|
13
|
+
function toCallback(row: unknown): ThreemaCallback | null {
|
|
14
|
+
if (typeof row !== 'object' || row === null) return null;
|
|
15
|
+
const rec = row as Record<string, unknown>;
|
|
16
|
+
const id = str(rec.id);
|
|
17
|
+
const callbackId = str(rec.callbackId);
|
|
18
|
+
const callbackToken = str(rec.callbackToken);
|
|
19
|
+
if (id === undefined || callbackId === undefined || callbackToken === undefined) return null;
|
|
20
|
+
return { id, callbackId, callbackToken };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function listThreemaCallbacks(): ThreemaCallback[] {
|
|
24
|
+
const raw = readJson<unknown[]>(accountFilePath('threema'), [], {
|
|
25
|
+
warn: 'threema-accounts.json: malformed, ignoring',
|
|
26
|
+
});
|
|
27
|
+
if (!Array.isArray(raw)) return [];
|
|
28
|
+
return raw.map(toCallback).filter((c): c is ThreemaCallback => c !== null);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const findThreemaCallback = (callbackId: string): ThreemaCallback | undefined =>
|
|
32
|
+
listThreemaCallbacks().find((c) => c.callbackId === callbackId);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { TrainCallResponse } from '@metro-labs/core/trains/protocol';
|
|
2
|
+
|
|
3
|
+
export type TrainCallBackend = (
|
|
4
|
+
train: string,
|
|
5
|
+
action: string,
|
|
6
|
+
args: unknown,
|
|
7
|
+
) => Promise<TrainCallResponse>;
|
|
8
|
+
|
|
9
|
+
let backend: TrainCallBackend | null = null;
|
|
10
|
+
|
|
11
|
+
export function setTrainCallBackend(fn: TrainCallBackend): void {
|
|
12
|
+
backend = fn;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function forwardTrainCall(
|
|
16
|
+
train: string,
|
|
17
|
+
action: string,
|
|
18
|
+
args: unknown,
|
|
19
|
+
): Promise<TrainCallResponse> {
|
|
20
|
+
if (!backend) throw new Error('train-call backend not wired');
|
|
21
|
+
return backend(train, action, args);
|
|
22
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
+
import { apiFailure, apiSession, bodyField, cors, readJsonBody, sendJson } from '@metro-labs/http/api-http';
|
|
6
|
+
import { log } from '@metro-labs/core/log';
|
|
7
|
+
import { mintTerminalTicket } from './tickets.js';
|
|
8
|
+
|
|
9
|
+
const PREFIX = '/api/terminal';
|
|
10
|
+
const TICKETS = `${PREFIX}/tickets`;
|
|
11
|
+
const SESSION_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,31}$/;
|
|
12
|
+
|
|
13
|
+
export const tmuxCommand = (session: string, home = homedir()): string[] => [
|
|
14
|
+
'tmux',
|
|
15
|
+
'new-session',
|
|
16
|
+
'-A',
|
|
17
|
+
'-D',
|
|
18
|
+
'-s',
|
|
19
|
+
session,
|
|
20
|
+
'-c',
|
|
21
|
+
home,
|
|
22
|
+
';',
|
|
23
|
+
'set-option',
|
|
24
|
+
'-g',
|
|
25
|
+
'mouse',
|
|
26
|
+
'on',
|
|
27
|
+
';',
|
|
28
|
+
'set-option',
|
|
29
|
+
'-s',
|
|
30
|
+
'set-clipboard',
|
|
31
|
+
'on',
|
|
32
|
+
';',
|
|
33
|
+
'set-option',
|
|
34
|
+
'-as',
|
|
35
|
+
'terminal-features',
|
|
36
|
+
',xterm-256color:clipboard',
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
export interface TerminalApiDeps {
|
|
40
|
+
authorize: (subject: string) => void;
|
|
41
|
+
command?: (session: string) => string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function tmuxAvailable(deps: TerminalApiDeps): boolean {
|
|
45
|
+
if (deps.command !== undefined) return true;
|
|
46
|
+
const run = spawnSync('tmux', ['-V'], { stdio: 'ignore' });
|
|
47
|
+
return run.error === undefined && run.status === 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function tmuxSessions(): string[] {
|
|
51
|
+
const run = spawnSync('tmux', ['list-sessions', '-F', '#{session_name}'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
52
|
+
if (run.error !== undefined || run.status !== 0) return [];
|
|
53
|
+
return run.stdout
|
|
54
|
+
.split('\n')
|
|
55
|
+
.map((l) => l.trim())
|
|
56
|
+
.filter((l) => SESSION_RE.test(l));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function sessionOf(raw: unknown): string {
|
|
60
|
+
if (raw === undefined || raw === null || raw === '')
|
|
61
|
+
throw new ApiError('name the tmux session to open; GET /api/terminal lists the ones that exist', 400);
|
|
62
|
+
if (typeof raw !== 'string' || !SESSION_RE.test(raw))
|
|
63
|
+
throw new ApiError('a session name is 1 to 32 letters, digits, dots, dashes or underscores', 400);
|
|
64
|
+
return raw;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function handleTerminalRequest(req: IncomingMessage, res: ServerResponse, deps: TerminalApiDeps): boolean {
|
|
68
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
69
|
+
if (path !== PREFIX && path !== TICKETS) return false;
|
|
70
|
+
if (req.method === 'OPTIONS') {
|
|
71
|
+
res.writeHead(204, cors(req)).end();
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
const wanted = path === TICKETS ? 'POST' : 'GET';
|
|
75
|
+
if (req.method !== wanted) {
|
|
76
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
apiSession(req)
|
|
80
|
+
.then(async (session) => {
|
|
81
|
+
if (!session) throw new ApiError('unauthorized', 401);
|
|
82
|
+
deps.authorize(session.subject);
|
|
83
|
+
if (path === PREFIX) {
|
|
84
|
+
sendJson(req, res, 200, { available: tmuxAvailable(deps), sessions: tmuxSessions() });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const wanted = sessionOf(bodyField(await readJsonBody(req), 'session'));
|
|
88
|
+
const minted = mintTerminalTicket(session.subject, wanted);
|
|
89
|
+
log.info({ subject: session.subject, session: wanted }, 'terminal: ticket minted');
|
|
90
|
+
sendJson(req, res, 200, { ...minted, session: wanted, path: `${PREFIX}/${minted.ticket}` });
|
|
91
|
+
})
|
|
92
|
+
.catch((err: unknown) => {
|
|
93
|
+
apiFailure(req, res, err, 'terminal-api');
|
|
94
|
+
});
|
|
95
|
+
return true;
|
|
96
|
+
}
|