@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,359 @@
|
|
|
1
|
+
import { createReadStream, existsSync, readdirEntries, readdirNames, readRange, statSync } from '../agent-user/agent-fs.js';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { createInterface } from 'node:readline';
|
|
5
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
6
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
7
|
+
import { removeHome, writeHomeText } from '../agent-user/home-fs.js';
|
|
8
|
+
import { agentUser } from '../agent-user/user.js';
|
|
9
|
+
|
|
10
|
+
export const PROJECT_RE = /^[A-Za-z0-9._-]{1,200}$/;
|
|
11
|
+
export const SESSION_RE = /^[A-Za-z0-9][A-Za-z0-9-]{7,63}$/;
|
|
12
|
+
const MEMORY_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,119}\.md$/;
|
|
13
|
+
const FOLDER_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,119}$/;
|
|
14
|
+
const MEMORY_DEPTH = 6;
|
|
15
|
+
const MEMORY_FILES_MAX = 5000;
|
|
16
|
+
const EDGE_BYTES = 64 * 1024;
|
|
17
|
+
const TEXT_CAP = 20_000;
|
|
18
|
+
const RESULT_CAP = 8_000;
|
|
19
|
+
const INPUT_CAP = 4_000;
|
|
20
|
+
const FILE_CAP = 1_000_000;
|
|
21
|
+
const MEMORY_MAX = 256 * 1024;
|
|
22
|
+
const MEMORY_MODE = 0o644;
|
|
23
|
+
const NOISE = ['<task-notification>', '<system-reminder>', '<local-command', '<command-name>'];
|
|
24
|
+
|
|
25
|
+
export function claudeDir(): string {
|
|
26
|
+
const explicit = process.env.METRO_CLAUDE_DIR?.trim();
|
|
27
|
+
if (explicit !== undefined && explicit !== '') return explicit;
|
|
28
|
+
const cfg = process.env.CLAUDE_CONFIG_DIR?.trim();
|
|
29
|
+
if (cfg !== undefined && cfg !== '') return cfg;
|
|
30
|
+
const user = agentUser();
|
|
31
|
+
return join(user === null ? homedir() : user.home, '.claude');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const str = (v: unknown): string | null => (typeof v === 'string' && v !== '' ? v : null);
|
|
35
|
+
|
|
36
|
+
export function safeName(value: string, re: RegExp, what: string): string {
|
|
37
|
+
if (!re.test(value) || value === '.' || value === '..')
|
|
38
|
+
throw new ApiError(`that is not a ${what}`, 400);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function projectDir(project: string, dir: string): string {
|
|
43
|
+
const path = join(dir, 'projects', safeName(project, PROJECT_RE, 'Claude project id'));
|
|
44
|
+
if (!existsSync(path)) throw new ApiError('no such Claude project', 404);
|
|
45
|
+
return path;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function parseLines(text: string): Record<string, unknown>[] {
|
|
49
|
+
const out: Record<string, unknown>[] = [];
|
|
50
|
+
for (const line of text.split('\n')) {
|
|
51
|
+
if (line.trim() === '') continue;
|
|
52
|
+
try {
|
|
53
|
+
const parsed: unknown = JSON.parse(line);
|
|
54
|
+
if (isRecord(parsed)) out.push(parsed);
|
|
55
|
+
} catch {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function readEdge(path: string, fromEnd: boolean): Record<string, unknown>[] {
|
|
63
|
+
const size = statSync(path).size;
|
|
64
|
+
const length = Math.min(EDGE_BYTES, size);
|
|
65
|
+
const text = readRange(path, fromEnd ? size - length : 0, length).toString('utf8');
|
|
66
|
+
const cut = fromEnd && length < size ? text.indexOf('\n') + 1 : 0;
|
|
67
|
+
return parseLines(text.slice(cut));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface ClaudeProject {
|
|
71
|
+
id: string;
|
|
72
|
+
cwd: string | null;
|
|
73
|
+
sessions: number;
|
|
74
|
+
lastActiveAt: string | null;
|
|
75
|
+
hasMemory: boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function sessionFiles(path: string): { name: string; mtime: number; size: number }[] {
|
|
79
|
+
return readdirNames(path)
|
|
80
|
+
.filter((name) => name.endsWith('.jsonl'))
|
|
81
|
+
.map((name) => {
|
|
82
|
+
const s = statSync(join(path, name));
|
|
83
|
+
return { name, mtime: s.mtimeMs, size: s.size };
|
|
84
|
+
})
|
|
85
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function cwdOf(path: string): string | null {
|
|
89
|
+
for (const line of readEdge(path, false)) {
|
|
90
|
+
const cwd = str(line.cwd);
|
|
91
|
+
if (cwd !== null) return cwd;
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function listClaudeProjects(dir = claudeDir()): ClaudeProject[] {
|
|
97
|
+
const root = join(dir, 'projects');
|
|
98
|
+
if (!existsSync(root)) return [];
|
|
99
|
+
return readdirEntries(root)
|
|
100
|
+
.filter((entry) => entry.isDirectory() && PROJECT_RE.test(entry.name))
|
|
101
|
+
.map((entry) => {
|
|
102
|
+
const path = join(root, entry.name);
|
|
103
|
+
const files = sessionFiles(path);
|
|
104
|
+
const newest = files[0];
|
|
105
|
+
return {
|
|
106
|
+
id: entry.name,
|
|
107
|
+
cwd: newest === undefined ? null : cwdOf(join(path, newest.name)),
|
|
108
|
+
sessions: files.length,
|
|
109
|
+
lastActiveAt: newest === undefined ? null : new Date(newest.mtime).toISOString(),
|
|
110
|
+
hasMemory: existsSync(join(path, 'memory')),
|
|
111
|
+
};
|
|
112
|
+
})
|
|
113
|
+
.sort((a, b) => (b.lastActiveAt ?? '').localeCompare(a.lastActiveAt ?? ''));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface ClaudeSession {
|
|
117
|
+
id: string;
|
|
118
|
+
title: string;
|
|
119
|
+
startedAt: string | null;
|
|
120
|
+
lastAt: string;
|
|
121
|
+
bytes: number;
|
|
122
|
+
gitBranch: string | null;
|
|
123
|
+
version: string | null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function firstPrompt(lines: Record<string, unknown>[]): string | null {
|
|
127
|
+
for (const line of lines) {
|
|
128
|
+
if (line.type !== 'user' || line.isSidechain === true) continue;
|
|
129
|
+
const message = isRecord(line.message) ? line.message : {};
|
|
130
|
+
const text = promptText(message.content);
|
|
131
|
+
if (text !== null && !NOISE.some((n) => text.startsWith(n))) return text.slice(0, 120);
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function promptText(content: unknown): string | null {
|
|
137
|
+
if (typeof content === 'string') return content.trim() === '' ? null : content.trim();
|
|
138
|
+
if (!Array.isArray(content)) return null;
|
|
139
|
+
for (const block of content)
|
|
140
|
+
if (isRecord(block) && block.type === 'text') {
|
|
141
|
+
const text = str(block.text);
|
|
142
|
+
if (text !== null) return text.trim();
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function sessionOf(path: string, id: string, mtime: number, size: number): ClaudeSession {
|
|
148
|
+
const head = readEdge(path, false);
|
|
149
|
+
const tail = readEdge(path, true);
|
|
150
|
+
const titled = [...tail].reverse().find((l) => l.type === 'ai-title' && str(l.aiTitle) !== null);
|
|
151
|
+
const meta = head.find((l) => l.type === 'user' || l.type === 'assistant') ?? {};
|
|
152
|
+
return {
|
|
153
|
+
id,
|
|
154
|
+
title: (titled === undefined ? null : str(titled.aiTitle)) ?? firstPrompt(head) ?? 'Untitled session',
|
|
155
|
+
startedAt: str(head.find((l) => str(l.timestamp) !== null)?.timestamp) ?? null,
|
|
156
|
+
lastAt: new Date(mtime).toISOString(),
|
|
157
|
+
bytes: size,
|
|
158
|
+
gitBranch: str(meta.gitBranch),
|
|
159
|
+
version: str(meta.version),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function listClaudeSessions(project: string, dir = claudeDir()): ClaudeSession[] {
|
|
164
|
+
const path = projectDir(project, dir);
|
|
165
|
+
return sessionFiles(path).map((f) =>
|
|
166
|
+
sessionOf(join(path, f.name), f.name.slice(0, -'.jsonl'.length), f.mtime, f.size),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export type Block =
|
|
171
|
+
| { kind: 'text'; text: string }
|
|
172
|
+
| { kind: 'tool_use'; name: string; input: string }
|
|
173
|
+
| { kind: 'tool_result'; text: string; isError: boolean }
|
|
174
|
+
| { kind: 'thinking' }
|
|
175
|
+
| { kind: 'image' };
|
|
176
|
+
|
|
177
|
+
export interface TranscriptEntry {
|
|
178
|
+
uuid: string;
|
|
179
|
+
at: string | null;
|
|
180
|
+
role: 'user' | 'assistant';
|
|
181
|
+
blocks: Block[];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export interface TranscriptPage {
|
|
185
|
+
entries: TranscriptEntry[];
|
|
186
|
+
total: number;
|
|
187
|
+
next: number | null;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const cap = (text: string, max: number): string =>
|
|
191
|
+
text.length > max ? `${text.slice(0, max)}\n… (${String(text.length - max)} more characters)` : text;
|
|
192
|
+
|
|
193
|
+
function resultText(content: unknown): string {
|
|
194
|
+
if (typeof content === 'string') return content;
|
|
195
|
+
if (!Array.isArray(content)) return '';
|
|
196
|
+
return content
|
|
197
|
+
.map((b) => (isRecord(b) && b.type === 'text' ? (str(b.text) ?? '') : ''))
|
|
198
|
+
.filter((t) => t !== '')
|
|
199
|
+
.join('\n');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function textBlock(raw: Record<string, unknown>): Block | null {
|
|
203
|
+
const text = str(raw.text);
|
|
204
|
+
if (text === null || NOISE.some((n) => text.startsWith(n))) return null;
|
|
205
|
+
return { kind: 'text', text: cap(text, TEXT_CAP) };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const BLOCKS: Record<string, (raw: Record<string, unknown>) => Block | null> = {
|
|
209
|
+
text: textBlock,
|
|
210
|
+
tool_use: (raw) => ({
|
|
211
|
+
kind: 'tool_use',
|
|
212
|
+
name: str(raw.name) ?? 'tool',
|
|
213
|
+
input: cap(JSON.stringify(raw.input ?? {}, null, 1), INPUT_CAP),
|
|
214
|
+
}),
|
|
215
|
+
tool_result: (raw) => ({
|
|
216
|
+
kind: 'tool_result',
|
|
217
|
+
text: cap(resultText(raw.content), RESULT_CAP),
|
|
218
|
+
isError: raw.is_error === true,
|
|
219
|
+
}),
|
|
220
|
+
thinking: () => ({ kind: 'thinking' }),
|
|
221
|
+
image: () => ({ kind: 'image' }),
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
function blockOf(raw: unknown): Block | null {
|
|
225
|
+
if (!isRecord(raw) || typeof raw.type !== 'string') return null;
|
|
226
|
+
const make = BLOCKS[raw.type];
|
|
227
|
+
return make === undefined ? null : make(raw);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function entryOf(line: Record<string, unknown>): TranscriptEntry | null {
|
|
231
|
+
if ((line.type !== 'user' && line.type !== 'assistant') || line.isSidechain === true) return null;
|
|
232
|
+
const message = isRecord(line.message) ? line.message : {};
|
|
233
|
+
const content = message.content;
|
|
234
|
+
const blocks =
|
|
235
|
+
typeof content === 'string'
|
|
236
|
+
? [blockOf({ type: 'text', text: content })]
|
|
237
|
+
: Array.isArray(content)
|
|
238
|
+
? content.map(blockOf)
|
|
239
|
+
: [];
|
|
240
|
+
const kept = blocks.filter((b): b is Block => b !== null);
|
|
241
|
+
if (kept.length === 0) return null;
|
|
242
|
+
return { uuid: str(line.uuid) ?? '', at: str(line.timestamp), role: line.type, blocks: kept };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export async function readTranscript(
|
|
246
|
+
project: string,
|
|
247
|
+
session: string,
|
|
248
|
+
offset: number,
|
|
249
|
+
limit: number,
|
|
250
|
+
dir = claudeDir(),
|
|
251
|
+
): Promise<TranscriptPage> {
|
|
252
|
+
const path = join(projectDir(project, dir), `${safeName(session, SESSION_RE, 'session id')}.jsonl`);
|
|
253
|
+
if (!existsSync(path)) throw new ApiError('no such session', 404);
|
|
254
|
+
const entries: TranscriptEntry[] = [];
|
|
255
|
+
let total = 0;
|
|
256
|
+
const lines = createInterface({ input: createReadStream(path), crlfDelay: Infinity });
|
|
257
|
+
for await (const line of lines) {
|
|
258
|
+
const parsed = parseLines(line)[0];
|
|
259
|
+
const entry = parsed === undefined ? null : entryOf(parsed);
|
|
260
|
+
if (entry === null) continue;
|
|
261
|
+
if (total >= offset && entries.length < limit) entries.push(entry);
|
|
262
|
+
total += 1;
|
|
263
|
+
}
|
|
264
|
+
return { entries, total, next: offset + limit < total ? offset + limit : null };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function deleteClaudeSession(project: string, session: string, dir = claudeDir()): void {
|
|
268
|
+
const root = projectDir(project, dir);
|
|
269
|
+
const id = safeName(session, SESSION_RE, 'session id');
|
|
270
|
+
const file = join(root, `${id}.jsonl`);
|
|
271
|
+
if (!existsSync(file)) throw new ApiError('no such session', 404);
|
|
272
|
+
removeHome(file);
|
|
273
|
+
removeHome(join(root, id), true);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export interface MemoryFile {
|
|
277
|
+
name: string;
|
|
278
|
+
bytes: number;
|
|
279
|
+
modifiedAt: string;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export interface MemoryListing {
|
|
283
|
+
files: MemoryFile[];
|
|
284
|
+
index: string | null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function memoryDir(project: string, dir: string): string {
|
|
288
|
+
return join(projectDir(project, dir), 'memory');
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function readCapped(path: string): string {
|
|
292
|
+
const size = statSync(path).size;
|
|
293
|
+
const text = readRange(path, 0, Math.min(size, FILE_CAP)).toString('utf8');
|
|
294
|
+
return size > FILE_CAP ? `${text}\n\n… truncated` : text;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function memoryName(name: string): string[] {
|
|
298
|
+
const segments = name.split('/');
|
|
299
|
+
const file = segments.at(-1) ?? '';
|
|
300
|
+
const folders = segments.slice(0, -1);
|
|
301
|
+
if (folders.length >= MEMORY_DEPTH || !MEMORY_RE.test(file) || folders.some((f) => !FOLDER_RE.test(f)))
|
|
302
|
+
throw new ApiError('that is not a memory file name', 400);
|
|
303
|
+
return segments;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function memoryPath(project: string, name: string, dir: string): string {
|
|
307
|
+
return join(memoryDir(project, dir), ...memoryName(name));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function walkMemory(root: string, folders: string[], out: MemoryFile[]): void {
|
|
311
|
+
for (const entry of readdirEntries(join(root, ...folders))) {
|
|
312
|
+
if (out.length >= MEMORY_FILES_MAX) return;
|
|
313
|
+
const inner = [...folders, entry.name];
|
|
314
|
+
if (entry.isDirectory() && FOLDER_RE.test(entry.name) && inner.length < MEMORY_DEPTH) walkMemory(root, inner, out);
|
|
315
|
+
else if (entry.isFile() && MEMORY_RE.test(entry.name) && !(folders.length === 0 && entry.name === 'MEMORY.md')) {
|
|
316
|
+
const s = statSync(join(root, ...inner));
|
|
317
|
+
out.push({ name: inner.join('/'), bytes: s.size, modifiedAt: s.mtime.toISOString() });
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function listMemory(project: string, dir = claudeDir()): MemoryListing {
|
|
323
|
+
const path = memoryDir(project, dir);
|
|
324
|
+
if (!existsSync(path)) return { files: [], index: null };
|
|
325
|
+
const files: MemoryFile[] = [];
|
|
326
|
+
walkMemory(path, [], files);
|
|
327
|
+
files.sort((a, b) => b.modifiedAt.localeCompare(a.modifiedAt) || a.name.localeCompare(b.name));
|
|
328
|
+
const index = join(path, 'MEMORY.md');
|
|
329
|
+
return { files, index: existsSync(index) ? readCapped(index) : null };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const stampOf = (raw: string | undefined): Date | null => {
|
|
333
|
+
if (raw === undefined) return null;
|
|
334
|
+
const at = new Date(raw);
|
|
335
|
+
return Number.isNaN(at.getTime()) ? null : at;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
export function writeMemoryFile(project: string, name: string, text: string, dir = claudeDir(), modifiedAt?: string): MemoryFile {
|
|
339
|
+
if (text.trim() === '') throw new ApiError('a memory file cannot be empty', 400);
|
|
340
|
+
if (Buffer.byteLength(text, 'utf8') > MEMORY_MAX)
|
|
341
|
+
throw new ApiError(`a memory file is at most ${String(MEMORY_MAX)} bytes`, 400);
|
|
342
|
+
const path = memoryPath(project, name, dir);
|
|
343
|
+
writeHomeText(path, text, MEMORY_MODE, stampOf(modifiedAt) ?? undefined);
|
|
344
|
+
const stat = statSync(path);
|
|
345
|
+
return { name, bytes: stat.size, modifiedAt: stat.mtime.toISOString() };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function deleteMemoryFile(project: string, name: string, dir = claudeDir()): string {
|
|
349
|
+
const path = memoryPath(project, name, dir);
|
|
350
|
+
if (!existsSync(path)) throw new ApiError('no such memory file', 404);
|
|
351
|
+
removeHome(path);
|
|
352
|
+
return name;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export function readMemoryFile(project: string, name: string, dir = claudeDir()): string {
|
|
356
|
+
const path = memoryPath(project, name, dir);
|
|
357
|
+
if (!existsSync(path)) throw new ApiError('no such memory file', 404);
|
|
358
|
+
return readCapped(path);
|
|
359
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { markOnboardingDone } from './onboarding.js';
|
|
4
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
6
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
7
|
+
import { agentCommand, asAgent, claudeBin } from '../agent-user/user.js';
|
|
8
|
+
|
|
9
|
+
const LOGIN_ARGS = ['auth', 'login', '--claudeai'];
|
|
10
|
+
const STATUS_COMMAND = ['auth', 'status', '--json'];
|
|
11
|
+
const TTL_MS = 10 * 60_000;
|
|
12
|
+
const OUTPUT_MAX = 8_000;
|
|
13
|
+
const COLS = 100;
|
|
14
|
+
const ROWS = 40;
|
|
15
|
+
const ESC = String.fromCharCode(27);
|
|
16
|
+
const CSI = /^\[[0-9;?]*[a-zA-Z]/;
|
|
17
|
+
const URL_RE = /https:\/\/[^\s"'`]+/g;
|
|
18
|
+
|
|
19
|
+
export type LoginState = 'pending' | 'done' | 'failed';
|
|
20
|
+
|
|
21
|
+
export interface LoginView {
|
|
22
|
+
id: string;
|
|
23
|
+
state: LoginState;
|
|
24
|
+
url: string | null;
|
|
25
|
+
output: string;
|
|
26
|
+
error: string | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface Live {
|
|
30
|
+
id: string;
|
|
31
|
+
startedAt: number;
|
|
32
|
+
state: LoginState;
|
|
33
|
+
output: string;
|
|
34
|
+
error: string | null;
|
|
35
|
+
write: (text: string) => void;
|
|
36
|
+
kill: () => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface LoginDeps {
|
|
40
|
+
command?: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let live: Live | null = null;
|
|
44
|
+
|
|
45
|
+
export function plainText(raw: string): string {
|
|
46
|
+
return raw
|
|
47
|
+
.split(ESC)
|
|
48
|
+
.map((part, index) => (index === 0 ? part : part.replace(CSI, '')))
|
|
49
|
+
.join('');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function loginUrlIn(output: string): string | null {
|
|
53
|
+
const found = output.match(URL_RE) ?? [];
|
|
54
|
+
const trimmed = found.map((url) => url.replace(/[.,)]+$/, ''));
|
|
55
|
+
return trimmed.find((url) => url.includes('claude.ai') || url.includes('anthropic.com')) ?? trimmed[0] ?? null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const view = (session: Live): LoginView => ({
|
|
59
|
+
id: session.id,
|
|
60
|
+
state: session.state,
|
|
61
|
+
url: loginUrlIn(session.output),
|
|
62
|
+
output: session.output,
|
|
63
|
+
error: session.error,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
function sweep(now: number): void {
|
|
67
|
+
if (live !== null && live.state !== 'pending') return;
|
|
68
|
+
if (live !== null && now - live.startedAt > TTL_MS) {
|
|
69
|
+
live.kill();
|
|
70
|
+
live = null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const CHECK_MS = 30_000;
|
|
75
|
+
|
|
76
|
+
interface Checked<T> {
|
|
77
|
+
at: number;
|
|
78
|
+
value: T;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const checks: { installed?: Checked<boolean>; account?: Checked<ClaudeAccount> } = {};
|
|
82
|
+
|
|
83
|
+
function remembered<T>(slot: Checked<T> | undefined, read: () => T, keep: (c: Checked<T>) => void): T {
|
|
84
|
+
const now = Date.now();
|
|
85
|
+
if (slot !== undefined && now - slot.at < CHECK_MS) return slot.value;
|
|
86
|
+
const value = read();
|
|
87
|
+
keep({ at: now, value });
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function forgetClaudeChecks(): void {
|
|
92
|
+
delete checks.installed;
|
|
93
|
+
delete checks.account;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function claudeInstalled(): boolean {
|
|
97
|
+
return remembered(checks.installed, () => {
|
|
98
|
+
const run = spawnSync(...asAgent(claudeBin(), ['--version']), { stdio: 'ignore' });
|
|
99
|
+
return run.error === undefined && run.status === 0;
|
|
100
|
+
}, (c) => {
|
|
101
|
+
checks.installed = c;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface ClaudeAccount {
|
|
106
|
+
signedIn: boolean;
|
|
107
|
+
account: string | null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function claudeAccount(): ClaudeAccount {
|
|
111
|
+
return remembered(checks.account, readAccount, (c) => {
|
|
112
|
+
checks.account = c;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function readAccount(): ClaudeAccount {
|
|
117
|
+
const run = spawnSync(...asAgent(claudeBin(), STATUS_COMMAND), { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
118
|
+
if (run.error !== undefined || typeof run.stdout !== 'string') return { signedIn: false, account: null };
|
|
119
|
+
try {
|
|
120
|
+
const parsed: unknown = JSON.parse(run.stdout);
|
|
121
|
+
if (!isRecord(parsed)) return { signedIn: false, account: null };
|
|
122
|
+
const account = typeof parsed.email === 'string' ? parsed.email : typeof parsed.organization === 'string' ? parsed.organization : null;
|
|
123
|
+
return { signedIn: parsed.authenticated === true || parsed.loggedIn === true || account !== null, account };
|
|
124
|
+
} catch {
|
|
125
|
+
return { signedIn: false, account: null };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function note(session: Live, chunk: string): void {
|
|
130
|
+
const next = session.output + plainText(chunk);
|
|
131
|
+
session.output = next.length > OUTPUT_MAX ? next.slice(next.length - OUTPUT_MAX) : next;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function startClaudeLogin(deps: LoginDeps = {}, now = Date.now()): LoginView {
|
|
135
|
+
sweep(now);
|
|
136
|
+
if (live !== null && live.state === 'pending') return view(live);
|
|
137
|
+
const command = deps.command ?? agentCommand([claudeBin(), ...LOGIN_ARGS], { TERM: 'xterm-256color' });
|
|
138
|
+
const session: Live = {
|
|
139
|
+
id: randomBytes(9).toString('base64url'),
|
|
140
|
+
startedAt: now,
|
|
141
|
+
state: 'pending',
|
|
142
|
+
output: '',
|
|
143
|
+
error: null,
|
|
144
|
+
write: () => undefined,
|
|
145
|
+
kill: () => undefined,
|
|
146
|
+
};
|
|
147
|
+
const terminal = new Bun.Terminal({
|
|
148
|
+
cols: COLS,
|
|
149
|
+
rows: ROWS,
|
|
150
|
+
data: (_term, chunk: Uint8Array) => {
|
|
151
|
+
note(session, Buffer.from(chunk).toString('utf8'));
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
const proc = Bun.spawn(command, { terminal, env: { ...process.env, TERM: 'xterm-256color' } });
|
|
155
|
+
session.write = (text: string) => {
|
|
156
|
+
terminal.write(Buffer.from(`${text}\r`, 'utf8'));
|
|
157
|
+
};
|
|
158
|
+
session.kill = () => {
|
|
159
|
+
proc.kill();
|
|
160
|
+
terminal.close();
|
|
161
|
+
};
|
|
162
|
+
proc.exited
|
|
163
|
+
.then((code) => {
|
|
164
|
+
session.state = code === 0 ? 'done' : 'failed';
|
|
165
|
+
if (code !== 0) session.error = `the login ended with status ${String(code)}`;
|
|
166
|
+
terminal.close();
|
|
167
|
+
forgetClaudeChecks();
|
|
168
|
+
const onboarding = code === 0 ? markOnboardingDone() : 'skipped';
|
|
169
|
+
log.info({ state: session.state, onboarding }, 'claude-login: the official login finished');
|
|
170
|
+
})
|
|
171
|
+
.catch((err: unknown) => {
|
|
172
|
+
session.state = 'failed';
|
|
173
|
+
session.error = errMsg(err);
|
|
174
|
+
});
|
|
175
|
+
live = session;
|
|
176
|
+
log.info({ command: command[0] }, 'claude-login: started');
|
|
177
|
+
return view(session);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function current(id: string): Live {
|
|
181
|
+
const session = live;
|
|
182
|
+
if (session?.id !== id) throw new ApiError('that sign-in is over; start another one', 404);
|
|
183
|
+
return session;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function claudeLoginView(id: string, now = Date.now()): LoginView {
|
|
187
|
+
sweep(now);
|
|
188
|
+
return view(current(id));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function answerClaudeLogin(id: string, text: string): LoginView {
|
|
192
|
+
const session = current(id);
|
|
193
|
+
if (session.state !== 'pending') throw new ApiError('that sign-in has already finished', 409);
|
|
194
|
+
session.write(text);
|
|
195
|
+
return view(session);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function endClaudeLogin(id: string): { ended: true } {
|
|
199
|
+
const session = current(id);
|
|
200
|
+
session.kill();
|
|
201
|
+
live = null;
|
|
202
|
+
return { ended: true };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export const forgetClaudeLogin = (): void => {
|
|
206
|
+
live?.kill();
|
|
207
|
+
live = null;
|
|
208
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { totalmem } from 'node:os';
|
|
4
|
+
import { dirname } from 'node:path';
|
|
5
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
6
|
+
import { helperArgv, runningAsMetro } from '../metro-user/privilege.js';
|
|
7
|
+
|
|
8
|
+
const UNIT = '/etc/systemd/system/metro.service';
|
|
9
|
+
const DROP_IN = '/etc/systemd/system/metro.service.d/10-metro-memory.conf';
|
|
10
|
+
const DROP_IN_TEXT = '[Service]\nOOMPolicy=continue\n';
|
|
11
|
+
const GIB = 1024 ** 3;
|
|
12
|
+
const RESERVE = 1.5 * GIB;
|
|
13
|
+
const FLOOR = GIB;
|
|
14
|
+
|
|
15
|
+
export interface ScopeHost {
|
|
16
|
+
platform: string;
|
|
17
|
+
uid: number | undefined;
|
|
18
|
+
systemd: boolean;
|
|
19
|
+
total: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const realHost = (): ScopeHost => ({
|
|
23
|
+
platform: process.platform,
|
|
24
|
+
uid: process.getuid?.(),
|
|
25
|
+
systemd: existsSync('/run/systemd/system'),
|
|
26
|
+
total: totalmem(),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export function sessionMemoryLimit(total: number): number {
|
|
30
|
+
const kept = Math.min(Math.floor(total * 0.8), total - RESERVE);
|
|
31
|
+
return Math.max(FLOOR, Math.floor(kept / (1024 * 1024)) * 1024 * 1024);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const usesScopes = (host: ScopeHost): boolean => host.platform === 'linux' && host.uid === 0 && host.systemd;
|
|
35
|
+
|
|
36
|
+
function metroScope(command: [string, string[]], total: number): [string, string[]] {
|
|
37
|
+
const [file, args] = command;
|
|
38
|
+
const asAgent = file === 'sudo' && args[0] === '-n' && args[1] === '-u' && args[3] === '--';
|
|
39
|
+
if (!asAgent) return command;
|
|
40
|
+
const limit = sessionMemoryLimit(total);
|
|
41
|
+
return helperArgv(['as-agent-scope', String(limit), String(Math.floor(limit * 0.9)), ...args.slice(4)]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function inSessionScope(command: [string, string[]], host: ScopeHost = realHost(), now = Date.now()): [string, string[]] {
|
|
45
|
+
if (host.systemd && runningAsMetro()) return metroScope(command, host.total);
|
|
46
|
+
if (!usesScopes(host)) return command;
|
|
47
|
+
const limit = sessionMemoryLimit(host.total);
|
|
48
|
+
const [file, args] = command;
|
|
49
|
+
return [
|
|
50
|
+
'systemd-run',
|
|
51
|
+
[
|
|
52
|
+
'--scope',
|
|
53
|
+
'--quiet',
|
|
54
|
+
'--collect',
|
|
55
|
+
`--unit=metro-claude-${String(now)}`,
|
|
56
|
+
'-p',
|
|
57
|
+
`MemoryMax=${String(limit)}`,
|
|
58
|
+
'-p',
|
|
59
|
+
`MemoryHigh=${String(Math.floor(limit * 0.9))}`,
|
|
60
|
+
'-p',
|
|
61
|
+
'OOMPolicy=continue',
|
|
62
|
+
'--',
|
|
63
|
+
file,
|
|
64
|
+
...args,
|
|
65
|
+
],
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function ensureServiceOomPolicy(host: ScopeHost = realHost(), env: NodeJS.ProcessEnv = process.env): 'written' | 'present' | 'skipped' {
|
|
70
|
+
if (!usesScopes(host) || (env.INVOCATION_ID ?? '') === '' || !existsSync(UNIT)) return 'skipped';
|
|
71
|
+
try {
|
|
72
|
+
if (existsSync(DROP_IN) && readFileSync(DROP_IN, 'utf8') === DROP_IN_TEXT) return 'present';
|
|
73
|
+
mkdirSync(dirname(DROP_IN), { recursive: true });
|
|
74
|
+
writeFileSync(DROP_IN, DROP_IN_TEXT, { mode: 0o644 });
|
|
75
|
+
const reload = spawnSync('systemctl', ['daemon-reload'], { stdio: 'ignore' });
|
|
76
|
+
log.info({ reloaded: reload.status === 0 }, 'claude-memory: the metro service no longer stops as a whole when the kernel kills one process for memory');
|
|
77
|
+
return 'written';
|
|
78
|
+
} catch (err) {
|
|
79
|
+
log.warn({ err: errMsg(err) }, 'claude-memory: could not set the service memory policy');
|
|
80
|
+
return 'skipped';
|
|
81
|
+
}
|
|
82
|
+
}
|