@stage-labs/metro 0.1.0-beta.2 → 0.1.0-beta.200
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 +11 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +163 -0
- package/dist/cli.js +77 -567
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +43 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +44 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +58 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +158 -0
- package/dist/service.js +243 -0
- package/dist/tail.js +102 -144
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -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 +24 -0
- package/runtime/node_modules/@metro-labs/core/package.json +47 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +119 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +24 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +91 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +227 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +74 -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 +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/profile.ts +58 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +61 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-boot.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-events.ts +89 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +123 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/tickets.ts +37 -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/agent-user/agent-fs.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files-api.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files.ts +119 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/home-fs.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/job-env.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules.ts +252 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-lookups.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +76 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +357 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +30 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +59 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +305 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +168 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +86 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/api.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/needs.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/preview.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +221 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +21 -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 +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +370 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +78 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/react-note.ts +13 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/reply-id.ts +1 -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 +355 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +359 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +283 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +271 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +306 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +176 -0
- 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/daemon/src/connectors/oauth-client.ts +159 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +135 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +158 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +348 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +81 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +106 -0
- package/runtime/node_modules/@metro-labs/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-reaper.ts +174 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +278 -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 +233 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +173 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +194 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +137 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/frames.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +274 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +160 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-client.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-setup.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-stream.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +199 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +299 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +193 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/pkce.ts +36 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/subscription.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/text.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +244 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +156 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +226 -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 +90 -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 +137 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +19 -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-prompt.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +83 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-lookup.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -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/daemon/src/mcp/session.ts +202 -0
- 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/daemon/src/mcp/tool-dispatch.ts +225 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +333 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/api.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/helper-script.ts +126 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/move.ts +165 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/privilege.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +316 -0
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +365 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +71 -0
- package/runtime/node_modules/@metro-labs/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/daemon/src/stations/attach-inline.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-outlook.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +225 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +288 -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 +265 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +51 -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/stations/webhook-endpoints.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +124 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/api.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/config.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/firewall.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/index.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/install.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/paths.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/proxy.ts +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +130 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +61 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +26 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +117 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +240 -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 +174 -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 +122 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +22 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +138 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/workos-token.ts +149 -0
- package/runtime/node_modules/@metro-labs/outlook/package.json +25 -0
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +107 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +20 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +97 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +53 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +36 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +141 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +60 -0
- package/runtime/node_modules/@metro-labs/outlook/src/html.ts +72 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +104 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +41 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +116 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +119 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +43 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +256 -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 +128 -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 +57 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +7 -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/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +27 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +6 -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 +119 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +246 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +173 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +95 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +65 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +20 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +106 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +134 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +152 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/files.ts +58 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +169 -0
- package/runtime/node_modules/@metro-labs/threema/src/groups.ts +96 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +89 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +24 -0
- package/runtime/node_modules/@metro-labs/threema/src/messages.ts +264 -0
- package/runtime/node_modules/@metro-labs/threema/src/outbound.ts +160 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +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 +213 -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 +388 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +83 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +7 -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/names.ts +28 -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 +24 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +94 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +5 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +114 -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 +101 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +118 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +244 -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-payloads.ts +316 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +69 -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 +47 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +122 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +139 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- 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 +17 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +79 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +198 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +88 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +98 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +51 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +37 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -78
- package/dist/channels/discord.js +0 -148
- package/dist/channels/telegram.js +0 -137
- package/dist/lib/address.js +0 -21
- package/dist/lib/codex-rc.js +0 -274
- package/dist/lib/dotenv.js +0 -31
- package/dist/log.js +0 -12
- package/dist/paths.js +0 -45
- package/skills/metro/SKILL.md +0 -122
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { join, parse as parsePath } from 'node:path';
|
|
3
|
+
import { errMsg, log } from './log.js';
|
|
4
|
+
import { coerceErrorInfo, type TrainErrorInfo } from './train-error.js';
|
|
5
|
+
import type { StructuredEvent } from './events.js';
|
|
6
|
+
import type { CallMsg } from './stations/station-runtime.js';
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
TrainError,
|
|
10
|
+
serializeTrainError,
|
|
11
|
+
type TrainErrorInfo,
|
|
12
|
+
} from './train-error.js';
|
|
13
|
+
|
|
14
|
+
const CALL_TIMEOUT_MS = 60_000;
|
|
15
|
+
|
|
16
|
+
export interface Pending {
|
|
17
|
+
resolve: (r: TrainCallResponse) => void;
|
|
18
|
+
reject: (err: Error) => void;
|
|
19
|
+
timer: ReturnType<typeof setTimeout>;
|
|
20
|
+
}
|
|
21
|
+
export interface CallTarget {
|
|
22
|
+
name: string;
|
|
23
|
+
pending: Map<string, Pending>;
|
|
24
|
+
proc: ({ stdin?: unknown } & Record<string, unknown>) | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const STDOUT_LINE_MAX = 4 * 1024 * 1024;
|
|
28
|
+
|
|
29
|
+
export type TrainEvent = {
|
|
30
|
+
station?: string;
|
|
31
|
+
line?: string;
|
|
32
|
+
line_name?: string;
|
|
33
|
+
from?: string;
|
|
34
|
+
from_name?: string;
|
|
35
|
+
from_display_name?: string;
|
|
36
|
+
to?: string;
|
|
37
|
+
message_id?: string;
|
|
38
|
+
reply_to?: string;
|
|
39
|
+
is_private?: boolean;
|
|
40
|
+
mentions_self?: boolean;
|
|
41
|
+
reply_to_self?: boolean;
|
|
42
|
+
sender_verified?: boolean;
|
|
43
|
+
text?: string;
|
|
44
|
+
emoji?: string;
|
|
45
|
+
payload?: unknown;
|
|
46
|
+
ts?: string;
|
|
47
|
+
id?: string;
|
|
48
|
+
event?: StructuredEvent;
|
|
49
|
+
} & Record<string, unknown>;
|
|
50
|
+
|
|
51
|
+
export interface TrainCallResponse {
|
|
52
|
+
result?: unknown;
|
|
53
|
+
error?: string;
|
|
54
|
+
errorInfo?: TrainErrorInfo;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type TrainMessage =
|
|
58
|
+
| {
|
|
59
|
+
op: 'response';
|
|
60
|
+
id: string;
|
|
61
|
+
result?: unknown;
|
|
62
|
+
error?: string;
|
|
63
|
+
errorInfo?: TrainErrorInfo;
|
|
64
|
+
}
|
|
65
|
+
| { op: 'log'; text?: string }
|
|
66
|
+
| { op: 'event'; event: TrainEvent }
|
|
67
|
+
| { op: 'ignore' };
|
|
68
|
+
|
|
69
|
+
export function parseTrainLine(
|
|
70
|
+
name: string,
|
|
71
|
+
line: string,
|
|
72
|
+
): TrainMessage | null {
|
|
73
|
+
let msg: {
|
|
74
|
+
op?: string;
|
|
75
|
+
id?: string;
|
|
76
|
+
result?: unknown;
|
|
77
|
+
error?: string;
|
|
78
|
+
errorInfo?: unknown;
|
|
79
|
+
text?: string;
|
|
80
|
+
} & Record<string, unknown>;
|
|
81
|
+
try {
|
|
82
|
+
msg = JSON.parse(line) as typeof msg;
|
|
83
|
+
} catch (err) {
|
|
84
|
+
log.warn(
|
|
85
|
+
{ name, err: errMsg(err), line: line.slice(0, 200) },
|
|
86
|
+
'train: bad JSON',
|
|
87
|
+
);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
if (msg.op === 'response') {
|
|
91
|
+
if (typeof msg.id !== 'string') return { op: 'ignore' };
|
|
92
|
+
return {
|
|
93
|
+
op: 'response',
|
|
94
|
+
id: msg.id,
|
|
95
|
+
result: msg.result,
|
|
96
|
+
error: msg.error,
|
|
97
|
+
errorInfo: coerceErrorInfo(msg.errorInfo),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
if (msg.op === 'log') return { op: 'log', text: msg.text };
|
|
101
|
+
if (typeof msg.line !== 'string') {
|
|
102
|
+
log.warn(
|
|
103
|
+
{ name, line: line.slice(0, 200) },
|
|
104
|
+
'train: event missing `line` (string) — dropped',
|
|
105
|
+
);
|
|
106
|
+
return { op: 'ignore' };
|
|
107
|
+
}
|
|
108
|
+
return { op: 'event', event: msg };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function drainLines(
|
|
112
|
+
name: string,
|
|
113
|
+
buf: string,
|
|
114
|
+
onLine: (line: string) => void,
|
|
115
|
+
): string {
|
|
116
|
+
if (buf.length > STDOUT_LINE_MAX && !buf.includes('\n')) {
|
|
117
|
+
log.warn(
|
|
118
|
+
{ name, bytes: buf.length },
|
|
119
|
+
'train: dropping oversized stdout line',
|
|
120
|
+
);
|
|
121
|
+
return '';
|
|
122
|
+
}
|
|
123
|
+
let nl;
|
|
124
|
+
while ((nl = buf.indexOf('\n')) !== -1) {
|
|
125
|
+
const line = buf.slice(0, nl).trim();
|
|
126
|
+
buf = buf.slice(nl + 1);
|
|
127
|
+
if (!line) continue;
|
|
128
|
+
onLine(line);
|
|
129
|
+
}
|
|
130
|
+
return buf;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function readCalls(
|
|
134
|
+
name: string,
|
|
135
|
+
handleCall: (msg: CallMsg) => Promise<void>,
|
|
136
|
+
): void {
|
|
137
|
+
let buf = '';
|
|
138
|
+
process.stdin.setEncoding('utf8');
|
|
139
|
+
process.stdin.on('data', (chunk: Buffer | string) => {
|
|
140
|
+
buf += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
|
|
141
|
+
buf = drainLines(name, buf, (line) => {
|
|
142
|
+
try {
|
|
143
|
+
const msg = JSON.parse(line) as Partial<CallMsg>;
|
|
144
|
+
if (msg.op === 'call')
|
|
145
|
+
handleCall(msg as CallMsg).catch((e: unknown) => {
|
|
146
|
+
process.stderr.write(`call failed: ${errMsg(e)}\n`);
|
|
147
|
+
});
|
|
148
|
+
} catch (err: unknown) {
|
|
149
|
+
process.stderr.write(`bad stdin line: ${errMsg(err)}\n`);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function encodeCall(id: string, action: string, args: unknown): string {
|
|
156
|
+
return JSON.stringify({ op: 'call', id, action, args }) + '\n';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function isTrainFile(name: string): boolean {
|
|
160
|
+
return (
|
|
161
|
+
/\.(ts|js|mjs)$/.test(name) &&
|
|
162
|
+
!name.startsWith('_') &&
|
|
163
|
+
!name.startsWith('.')
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function listTrainFiles(dir: string): { name: string; path: string }[] {
|
|
168
|
+
return readdirSync(dir)
|
|
169
|
+
.filter(isTrainFile)
|
|
170
|
+
.map((f) => ({ name: parsePath(f).name, path: join(dir, f) }))
|
|
171
|
+
.filter((t) => {
|
|
172
|
+
try {
|
|
173
|
+
return statSync(t.path).isFile();
|
|
174
|
+
} catch {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function mintCallId(seq: number): string {
|
|
181
|
+
return `req_${seq}_${Math.random().toString(36).slice(2, 8)}`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function failAllPending(
|
|
185
|
+
pending: Map<string, Pending>,
|
|
186
|
+
reason: string,
|
|
187
|
+
): void {
|
|
188
|
+
for (const p of pending.values()) {
|
|
189
|
+
clearTimeout(p.timer);
|
|
190
|
+
p.reject(new Error(reason));
|
|
191
|
+
}
|
|
192
|
+
pending.clear();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function sendCall(
|
|
196
|
+
t: CallTarget,
|
|
197
|
+
id: string,
|
|
198
|
+
action: string,
|
|
199
|
+
args: unknown,
|
|
200
|
+
): Promise<TrainCallResponse> {
|
|
201
|
+
return new Promise<TrainCallResponse>((resolve, reject) => {
|
|
202
|
+
const timer = setTimeout(() => {
|
|
203
|
+
t.pending.delete(id);
|
|
204
|
+
reject(
|
|
205
|
+
new Error(
|
|
206
|
+
`train '${t.name}' call '${action}' timed out after ${CALL_TIMEOUT_MS}ms`,
|
|
207
|
+
),
|
|
208
|
+
);
|
|
209
|
+
}, CALL_TIMEOUT_MS);
|
|
210
|
+
t.pending.set(id, { resolve, reject, timer });
|
|
211
|
+
try {
|
|
212
|
+
const stdin = (
|
|
213
|
+
t.proc as {
|
|
214
|
+
stdin?: { write: (s: string) => void; flush: () => void };
|
|
215
|
+
} | null
|
|
216
|
+
)?.stdin;
|
|
217
|
+
if (!stdin || typeof stdin === 'number')
|
|
218
|
+
throw new Error('stdin not piped');
|
|
219
|
+
stdin.write(encodeCall(id, action, args));
|
|
220
|
+
stdin.flush();
|
|
221
|
+
} catch (err) {
|
|
222
|
+
clearTimeout(timer);
|
|
223
|
+
t.pending.delete(id);
|
|
224
|
+
reject(new Error(`train '${t.name}' stdin write failed: ${errMsg(err)}`));
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chmodSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
renameSync,
|
|
7
|
+
statSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from 'node:fs';
|
|
10
|
+
import { dirname } from 'node:path';
|
|
11
|
+
import { errMsg, log } from './log.js';
|
|
12
|
+
|
|
13
|
+
export function ensureSecureDir(dir: string): void {
|
|
14
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
15
|
+
try {
|
|
16
|
+
chmodSync(dir, 0o700);
|
|
17
|
+
} catch {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function chmodIfExists(path: string, mode = 0o600): void {
|
|
22
|
+
if (!existsSync(path)) return;
|
|
23
|
+
try {
|
|
24
|
+
if ((statSync(path).mode & 0o777) !== mode) chmodSync(path, mode);
|
|
25
|
+
} catch {
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function writeSecure(path: string, data: string): void {
|
|
30
|
+
ensureSecureDir(dirname(path));
|
|
31
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
32
|
+
writeFileSync(tmp, data, { mode: 0o600 });
|
|
33
|
+
try {
|
|
34
|
+
chmodSync(tmp, 0o600);
|
|
35
|
+
} catch {
|
|
36
|
+
}
|
|
37
|
+
renameSync(tmp, path);
|
|
38
|
+
chmodIfExists(path, 0o600);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function modeOf(path: string): number | null {
|
|
42
|
+
try {
|
|
43
|
+
return statSync(path).mode & 0o777;
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function writeAtomic(path: string, data: string, mode?: number): void {
|
|
50
|
+
const chosen = mode ?? modeOf(path) ?? 0o644;
|
|
51
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
52
|
+
const tmp = `${path}.metro-${String(process.pid)}.tmp`;
|
|
53
|
+
writeFileSync(tmp, data, { mode: chosen });
|
|
54
|
+
chmodSync(tmp, chosen);
|
|
55
|
+
renameSync(tmp, path);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function readJson<T>(
|
|
59
|
+
path: string,
|
|
60
|
+
fallback: T,
|
|
61
|
+
opts?: { warn?: string },
|
|
62
|
+
): T {
|
|
63
|
+
if (!existsSync(path)) return fallback;
|
|
64
|
+
try {
|
|
65
|
+
return JSON.parse(readFileSync(path, 'utf8')) as T;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (opts?.warn) log.warn({ err: errMsg(err), path }, opts.warn);
|
|
68
|
+
return fallback;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function writeJson(path: string, value: unknown): void {
|
|
73
|
+
writeFileSync(path, JSON.stringify(value, null, 2));
|
|
74
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { readdirSync, rmSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { log } from '../log.js';
|
|
5
|
+
|
|
6
|
+
export interface AccountFiles {
|
|
7
|
+
path: (accountId: string) => string;
|
|
8
|
+
forget: (accountId: string) => void;
|
|
9
|
+
forgetExcept: (keptAccountIds: readonly string[]) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const safeSegment = (s: string): string => s.replace(/[^A-Za-z0-9_-]/g, '_');
|
|
13
|
+
|
|
14
|
+
function listed(dir: string): string[] {
|
|
15
|
+
try {
|
|
16
|
+
return readdirSync(dir);
|
|
17
|
+
} catch {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function accountFiles(dirEnv: string, prefix: string): AccountFiles {
|
|
23
|
+
const dir = (): string => process.env[dirEnv] ?? join(homedir(), '.metro');
|
|
24
|
+
const name = (accountId: string): string => `${prefix}${safeSegment(accountId)}.json`;
|
|
25
|
+
const remove = (file: string, reason: string): void => {
|
|
26
|
+
rmSync(join(dir(), file), { force: true });
|
|
27
|
+
log.info({ file: join(dir(), file) }, reason);
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
path: (accountId) => join(dir(), name(accountId)),
|
|
31
|
+
forget: (accountId) => {
|
|
32
|
+
remove(name(accountId), 'station: removed the files of a detached account');
|
|
33
|
+
},
|
|
34
|
+
forgetExcept: (keptAccountIds) => {
|
|
35
|
+
const kept = new Set(keptAccountIds.map(name));
|
|
36
|
+
for (const file of listed(dir()))
|
|
37
|
+
if (file.startsWith(prefix) && file.endsWith('.json') && !kept.has(file))
|
|
38
|
+
remove(file, 'station: removed the files of an account that is no longer attached');
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { errMsg } from '../log.js';
|
|
3
|
+
import { chmodIfExists } from '../secure-fs.js';
|
|
4
|
+
|
|
5
|
+
export type Die = (msg: string) => never;
|
|
6
|
+
|
|
7
|
+
export interface MakeLoaderOpts<T> {
|
|
8
|
+
prefix: string;
|
|
9
|
+
file: string;
|
|
10
|
+
validate: (raw: T[], die: Die) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Loader<T> {
|
|
14
|
+
die: Die;
|
|
15
|
+
loadAccounts: () => T[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function makeAccountStore<T extends { id: string }>(
|
|
19
|
+
opts: MakeLoaderOpts<T>,
|
|
20
|
+
): Loader<T> {
|
|
21
|
+
const die: Die = (msg) => {
|
|
22
|
+
process.stderr.write(`${opts.prefix}: ${msg}\n`);
|
|
23
|
+
process.exit(2);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function loadAccounts(): T[] {
|
|
27
|
+
if (!existsSync(opts.file))
|
|
28
|
+
return die(
|
|
29
|
+
`no accounts file ${opts.file} — the daemon writes it from ~/.metro/agents/agent.json at boot; attach an account to this station from the page first`,
|
|
30
|
+
);
|
|
31
|
+
chmodIfExists(opts.file);
|
|
32
|
+
let raw: T[];
|
|
33
|
+
try {
|
|
34
|
+
raw = JSON.parse(readFileSync(opts.file, 'utf8')) as T[];
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return die(`bad ${opts.file}: ${errMsg(e)}`);
|
|
37
|
+
}
|
|
38
|
+
if (!Array.isArray(raw) || raw.length === 0)
|
|
39
|
+
die(`${opts.file} must be a non-empty array`);
|
|
40
|
+
opts.validate(raw, die);
|
|
41
|
+
return raw;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { die, loadAccounts };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function resolveAccountId(
|
|
48
|
+
accounts: Map<string, unknown>,
|
|
49
|
+
args: { account?: string; line?: string },
|
|
50
|
+
parseAccountId: (line: string) => string | undefined,
|
|
51
|
+
): string {
|
|
52
|
+
let id = args.account;
|
|
53
|
+
id ??= args.line ? parseAccountId(args.line) : undefined;
|
|
54
|
+
id ??= (accounts.size === 1 ? [...accounts.keys()][0] : 'default') ?? 'default';
|
|
55
|
+
if (!accounts.has(id))
|
|
56
|
+
throw new Error(
|
|
57
|
+
`unknown account '${id}' (have: ${[...accounts.keys()].join(', ')})`,
|
|
58
|
+
);
|
|
59
|
+
return id;
|
|
60
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
export const attachDir = (): string =>
|
|
2
|
+
process.env.METRO_XMTP_ATTACH_DIR ??
|
|
3
|
+
`${process.env.HOME}/.cache/metro/messenger-uploads`;
|
|
4
|
+
|
|
5
|
+
const CACHE_NAME_RE = /^msg_[A-Za-z0-9]{1,16}_\d+\.[A-Za-z0-9]{1,5}$/;
|
|
6
|
+
|
|
7
|
+
export const resolveCachedAttachment = (name: string): string | null =>
|
|
8
|
+
CACHE_NAME_RE.test(name) ? `${attachDir()}/${name}` : null;
|
|
9
|
+
|
|
10
|
+
const MIME_EXT: Record<string, string> = {
|
|
11
|
+
'image/jpeg': 'jpg',
|
|
12
|
+
'image/jpg': 'jpg',
|
|
13
|
+
'image/png': 'png',
|
|
14
|
+
'image/gif': 'gif',
|
|
15
|
+
'image/webp': 'webp',
|
|
16
|
+
'image/heic': 'heic',
|
|
17
|
+
'audio/mp4': 'm4a',
|
|
18
|
+
'audio/mpeg': 'mp3',
|
|
19
|
+
'audio/ogg': 'ogg',
|
|
20
|
+
'audio/wav': 'wav',
|
|
21
|
+
'video/mp4': 'mp4',
|
|
22
|
+
'video/quicktime': 'mov',
|
|
23
|
+
'video/webm': 'webm',
|
|
24
|
+
'application/pdf': 'pdf',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const extFromName = (filename: string | undefined): string | undefined => {
|
|
28
|
+
const ext = filename
|
|
29
|
+
?.split('?')[0]
|
|
30
|
+
?.split('#')[0]
|
|
31
|
+
?.split('.')
|
|
32
|
+
.pop()
|
|
33
|
+
?.toLowerCase();
|
|
34
|
+
return ext !== undefined && ext.length >= 1 && ext.length <= 5
|
|
35
|
+
? ext
|
|
36
|
+
: undefined;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const extFromMime = (mime: string | undefined): string | undefined => {
|
|
40
|
+
if (mime === undefined) return undefined;
|
|
41
|
+
const mapped = MIME_EXT[mime];
|
|
42
|
+
if (mapped) return mapped;
|
|
43
|
+
if (mime.startsWith('image/')) return mime.slice(6).replace('jpeg', 'jpg');
|
|
44
|
+
return undefined;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const extFor = (
|
|
48
|
+
filename: string | undefined,
|
|
49
|
+
mime: string | undefined,
|
|
50
|
+
): string => extFromName(filename) ?? extFromMime(mime) ?? 'bin';
|
|
51
|
+
|
|
52
|
+
const cacheFileName = (
|
|
53
|
+
messageId: string,
|
|
54
|
+
index: number,
|
|
55
|
+
filename: string | undefined,
|
|
56
|
+
mime: string | undefined,
|
|
57
|
+
): string => {
|
|
58
|
+
const safeId =
|
|
59
|
+
messageId.replace(/[^a-zA-Z0-9]/g, '').slice(0, 16) || 'unknown';
|
|
60
|
+
return `msg_${safeId}_${index}.${extFor(filename, mime)}`;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export interface SavedAttachment {
|
|
64
|
+
path: string;
|
|
65
|
+
mime?: string;
|
|
66
|
+
name?: string;
|
|
67
|
+
bytes: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const MAX_ATTACHMENT_BYTES = Number(
|
|
71
|
+
process.env.METRO_ATTACH_MAX_BYTES ?? 100 * 1024 * 1024,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
export function assertAttachmentSize(bytes: number): void {
|
|
75
|
+
if (bytes > MAX_ATTACHMENT_BYTES)
|
|
76
|
+
throw new Error(
|
|
77
|
+
`attachment size ${bytes} exceeds limit of ${MAX_ATTACHMENT_BYTES} bytes`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function assertContentLength(header: string | null | undefined): void {
|
|
82
|
+
if (header == null) return;
|
|
83
|
+
const n = Number(header);
|
|
84
|
+
if (Number.isFinite(n)) assertAttachmentSize(n);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const cachePathFor = async (
|
|
88
|
+
messageId: string,
|
|
89
|
+
index: number,
|
|
90
|
+
meta: { mime?: string; name?: string },
|
|
91
|
+
): Promise<string> => {
|
|
92
|
+
const { mkdir } = await import('node:fs/promises');
|
|
93
|
+
const nodePath = await import('node:path');
|
|
94
|
+
const dir = attachDir();
|
|
95
|
+
await mkdir(dir, { recursive: true });
|
|
96
|
+
return nodePath.join(
|
|
97
|
+
dir,
|
|
98
|
+
cacheFileName(messageId, index, meta.name, meta.mime),
|
|
99
|
+
);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const saveBufferToCache = async (
|
|
103
|
+
data: Uint8Array,
|
|
104
|
+
messageId: string,
|
|
105
|
+
index: number,
|
|
106
|
+
meta: { mime?: string; name?: string },
|
|
107
|
+
): Promise<SavedAttachment> => {
|
|
108
|
+
assertAttachmentSize(data.length);
|
|
109
|
+
const { writeFile } = await import('node:fs/promises');
|
|
110
|
+
const path = await cachePathFor(messageId, index, meta);
|
|
111
|
+
await writeFile(path, data);
|
|
112
|
+
return { path, mime: meta.mime, name: meta.name, bytes: data.length };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const saveStreamToCache = async (
|
|
116
|
+
chunks: AsyncIterable<Uint8Array>,
|
|
117
|
+
messageId: string,
|
|
118
|
+
index: number,
|
|
119
|
+
meta: { mime?: string; name?: string },
|
|
120
|
+
): Promise<SavedAttachment> => {
|
|
121
|
+
const { open, rename, rm } = await import('node:fs/promises');
|
|
122
|
+
const path = await cachePathFor(messageId, index, meta);
|
|
123
|
+
const part = `${path}.part`;
|
|
124
|
+
const handle = await open(part, 'w');
|
|
125
|
+
let bytes = 0;
|
|
126
|
+
try {
|
|
127
|
+
for await (const chunk of chunks) {
|
|
128
|
+
bytes += chunk.length;
|
|
129
|
+
assertAttachmentSize(bytes);
|
|
130
|
+
await handle.write(chunk);
|
|
131
|
+
}
|
|
132
|
+
} catch (err) {
|
|
133
|
+
await handle.close();
|
|
134
|
+
await rm(part, { force: true });
|
|
135
|
+
throw err;
|
|
136
|
+
}
|
|
137
|
+
await handle.close();
|
|
138
|
+
await rename(part, path);
|
|
139
|
+
return { path, mime: meta.mime, name: meta.name, bytes };
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export const guessMime = (path: string): string => {
|
|
143
|
+
const ext = (path.split('.').pop() ?? '').toLowerCase();
|
|
144
|
+
const map: Record<string, string> = {
|
|
145
|
+
png: 'image/png',
|
|
146
|
+
jpg: 'image/jpeg',
|
|
147
|
+
jpeg: 'image/jpeg',
|
|
148
|
+
gif: 'image/gif',
|
|
149
|
+
webp: 'image/webp',
|
|
150
|
+
heic: 'image/heic',
|
|
151
|
+
mp4: 'video/mp4',
|
|
152
|
+
mov: 'video/quicktime',
|
|
153
|
+
webm: 'video/webm',
|
|
154
|
+
m4v: 'video/x-m4v',
|
|
155
|
+
m4a: 'audio/mp4',
|
|
156
|
+
mp3: 'audio/mpeg',
|
|
157
|
+
ogg: 'audio/ogg',
|
|
158
|
+
wav: 'audio/wav',
|
|
159
|
+
pdf: 'application/pdf',
|
|
160
|
+
};
|
|
161
|
+
return map[ext] ?? 'application/octet-stream';
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export const isImageMime = (mime: string): boolean =>
|
|
165
|
+
mime.toLowerCase().startsWith('image/');
|
|
166
|
+
export const isImageExt = (path: string): boolean =>
|
|
167
|
+
/\.(png|jpe?g|gif|webp|heic|bmp|svg)$/i.test(path);
|
|
168
|
+
|
|
169
|
+
export const appendFile = async (
|
|
170
|
+
form: FormData,
|
|
171
|
+
field: string,
|
|
172
|
+
path: string,
|
|
173
|
+
name: string,
|
|
174
|
+
): Promise<void> => {
|
|
175
|
+
const data = await Bun.file(path).arrayBuffer();
|
|
176
|
+
form.append(field, new Blob([data]), name);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const kindOf = (mime: string, path: string): string => {
|
|
180
|
+
if (isImageMime(mime) || isImageExt(path)) return 'image';
|
|
181
|
+
if (mime.startsWith('video/')) return 'video';
|
|
182
|
+
if (mime.startsWith('audio/')) return 'audio';
|
|
183
|
+
return 'file';
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export const toCanonical = (a: {
|
|
187
|
+
path: string;
|
|
188
|
+
mime: string;
|
|
189
|
+
name: string;
|
|
190
|
+
}): Record<string, unknown> => ({
|
|
191
|
+
kind: kindOf(a.mime, a.path),
|
|
192
|
+
path: a.path,
|
|
193
|
+
url: a.path,
|
|
194
|
+
name: a.name,
|
|
195
|
+
mime: a.mime,
|
|
196
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
type Args = Record<string, unknown>;
|
|
2
|
+
export interface Normalized {
|
|
3
|
+
action: string;
|
|
4
|
+
args: Args;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface Attachment {
|
|
8
|
+
kind?: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
url?: string;
|
|
11
|
+
data?: string;
|
|
12
|
+
mime?: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface DiscordFile {
|
|
17
|
+
file: string;
|
|
18
|
+
kind: string;
|
|
19
|
+
name: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const discordFile = (a: Attachment): DiscordFile | null => {
|
|
23
|
+
const file = a.path ?? a.url;
|
|
24
|
+
if (typeof file !== 'string') return null;
|
|
25
|
+
return { file, kind: a.kind ?? 'file', name: a.name ?? '' };
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function discordAttachments(att: Attachment[]): Args {
|
|
29
|
+
const usable = att
|
|
30
|
+
.map(discordFile)
|
|
31
|
+
.filter((f): f is DiscordFile => f !== null);
|
|
32
|
+
if (!usable.length) return {};
|
|
33
|
+
return {
|
|
34
|
+
files: usable.map((f) => f.file),
|
|
35
|
+
attachmentKinds: usable.map((f) => f.kind),
|
|
36
|
+
attachmentNames: usable.map((f) => f.name),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function normalizeDiscord(action: string, env: Args): Normalized {
|
|
41
|
+
const att = (env.attachments as Attachment[] | undefined) ?? [];
|
|
42
|
+
if (action === 'send') {
|
|
43
|
+
return { action: 'send', args: { ...env, ...discordAttachments(att) } };
|
|
44
|
+
}
|
|
45
|
+
if (action === 'reply') {
|
|
46
|
+
const replyTo = env.replyTo ?? env.messageId;
|
|
47
|
+
return {
|
|
48
|
+
action: 'send',
|
|
49
|
+
args: { ...env, replyTo, ...discordAttachments(att) },
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (action === 'unreact')
|
|
53
|
+
return { action: 'react', args: { ...env, emoji: '' } };
|
|
54
|
+
if (action === 'read') {
|
|
55
|
+
return {
|
|
56
|
+
action: 'fetch',
|
|
57
|
+
args: { line: env.line, limit: env.limit, before: env.before },
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return { action, args: env };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function normalizeTelegram(action: string, env: Args): Normalized {
|
|
64
|
+
if (action === 'reply') {
|
|
65
|
+
return {
|
|
66
|
+
action: 'send',
|
|
67
|
+
args: {
|
|
68
|
+
line: env.line,
|
|
69
|
+
text: env.text,
|
|
70
|
+
replyTo: env.replyTo,
|
|
71
|
+
attachments: env.attachments,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (action === 'unreact')
|
|
76
|
+
return { action: 'react', args: { ...env, emoji: '' } };
|
|
77
|
+
return { action, args: env };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function normalizeXmtp(action: string, env: Args): Normalized {
|
|
81
|
+
if (action === 'unreact') {
|
|
82
|
+
return { action: 'react', args: { ...env, action: 'removed' } };
|
|
83
|
+
}
|
|
84
|
+
if (action === 'read') {
|
|
85
|
+
return { action: 'query', args: { line: env.line, limit: env.limit } };
|
|
86
|
+
}
|
|
87
|
+
return { action, args: env };
|
|
88
|
+
}
|