@stage-labs/metro 0.1.0-beta.21 → 0.1.0-beta.210
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +9 -101
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +152 -0
- package/dist/cli.js +52 -109
- package/dist/control.js +47 -75
- package/dist/hold.js +210 -0
- package/dist/local.js +43 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +44 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +58 -0
- package/dist/runtime-install.js +96 -0
- package/dist/runtime.js +29 -52
- package/dist/serve.js +157 -0
- package/dist/service.js +304 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +13 -1
- package/package.json +2 -23
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +17 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +71 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +30 -0
- package/runtime/marketplace/plugin/bin/policy.mjs +97 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/marketplace/plugin/orchestrator.md +26 -0
- package/runtime/node_modules/@metro-labs/core/package.json +47 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/events.ts +11 -7
- package/runtime/node_modules/@metro-labs/{mcp/src/db → core/src}/ids.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/stations → core/src}/lines.ts +24 -28
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/protocol.ts +7 -4
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/secure-fs.ts +17 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +83 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/attachments.ts +0 -1
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +16 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/station-runtime.ts +6 -11
- package/runtime/node_modules/@metro-labs/core/src/stations/train-boot.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-events.ts +84 -0
- package/runtime/node_modules/@metro-labs/{mcp → core}/src/stations/types.ts +18 -1
- package/runtime/node_modules/@metro-labs/core/src/tickets.ts +37 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/agent-fs.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files-api.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files.ts +89 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/home-fs.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/job-env.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +80 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules.ts +144 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +89 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-lookups.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +76 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +357 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +30 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +59 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +305 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +142 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/db/key-map.ts → daemon/src/agents/keys.ts} +1 -11
- package/runtime/node_modules/@metro-labs/{mcp/src/db/agent-map.ts → daemon/src/agents/map.ts} +58 -1
- package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/db/agent-scope.ts → daemon/src/agents/scope.ts} +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +94 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/api.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/preview.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +218 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +36 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/boot}/crash-guard.ts +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +21 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/boot}/paths.ts +6 -34
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +26 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/inbound.ts +70 -45
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/media-note.ts +11 -7
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/pending.ts +4 -1
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/relay.ts +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +355 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +359 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +284 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +273 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +113 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-api.ts → daemon/src/connectors/api.ts} +79 -113
- package/runtime/node_modules/@metro-labs/{mcp/src/db/connector-config.ts → daemon/src/connectors/config.ts} +67 -41
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/health.ts +18 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/connectors}/oauth-client.ts +14 -17
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/connectors}/oauth-discovery.ts +33 -20
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth-routes.ts → daemon/src/connectors/oauth-routes.ts} +30 -32
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/connector-oauth.ts → daemon/src/connectors/oauth.ts} +61 -22
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +347 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +385 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +81 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +106 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-grant.ts +4 -4
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-owner.ts +3 -3
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-reaper.ts +174 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/attach-serve.ts +12 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/upload-api.ts +10 -27
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/files}/upload-store.ts +4 -4
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +233 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +173 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +195 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-version.ts +44 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/frames.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +274 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +160 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-client.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-setup.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-stream.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +224 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +199 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +278 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +193 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/pkce.ts +36 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/subscription.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/text.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +244 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/accounts.ts +28 -17
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/call-tools.ts +49 -20
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/ctx.ts +4 -4
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/event-store.ts +15 -16
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/group-tools.ts +2 -2
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/index.ts +24 -79
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +19 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/member-tools.ts +2 -2
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-lookup.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/read-tool.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-slot.ts +48 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/session.ts +33 -42
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +88 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-def.ts +9 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/tool-dispatch.ts +53 -40
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/tool-schemas.ts +68 -119
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/upload-tool.ts +6 -11
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/helper-script.ts +126 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/install.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/privilege.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +316 -0
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/routes}/http.ts +59 -51
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +205 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +58 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/owner.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-driver.ts +42 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-inline.ts +29 -1
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-interactive.ts +9 -36
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-outlook.ts +63 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-resolve.ts +38 -13
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/attach-session.ts +32 -22
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach-xmtp.ts +8 -9
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/attach.ts +75 -9
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +265 -0
- package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/stations/registry.ts +22 -6
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor-io.ts +3 -3
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/supervisor.ts +6 -9
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon → daemon/src/stations}/train-call.ts +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/stations/webhook-endpoints.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +124 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/api.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/config.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/firewall.ts +9 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/index.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/install.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/paths.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/proxy.ts +147 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +44 -0
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/package.json +3 -5
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/accounts.ts +8 -28
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/actions.ts +19 -187
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/format.ts +31 -72
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group-actions.ts +2 -2
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/index.ts +15 -21
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/members.ts +2 -2
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -0
- package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/send-files.ts +12 -13
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +23 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +135 -0
- package/runtime/node_modules/@metro-labs/{mcp/src/daemon/session-api.ts → http/src/mode-api.ts} +13 -9
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/workos-token.ts +149 -0
- package/runtime/node_modules/@metro-labs/{telegram-user → outlook}/package.json +2 -5
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +93 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +14 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +97 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +53 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +36 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +140 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +60 -0
- package/runtime/node_modules/@metro-labs/outlook/src/html.ts +72 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +104 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +41 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +116 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +119 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +43 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +3 -2
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +38 -96
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +214 -253
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +67 -108
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/client.ts +6 -6
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +101 -147
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/group.ts +4 -4
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/history.ts +1 -1
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +6 -102
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/login.ts +20 -20
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +64 -151
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +61 -33
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/{telegram-user → telegram}/src/reactions.ts +3 -3
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +11 -1
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +5 -47
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +86 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +240 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +170 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +90 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +85 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +21 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +95 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +124 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +152 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/files.ts +47 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +164 -0
- package/runtime/node_modules/@metro-labs/threema/src/groups.ts +96 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +89 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +24 -0
- package/runtime/node_modules/@metro-labs/threema/src/messages.ts +264 -0
- package/runtime/node_modules/@metro-labs/threema/src/outbound.ts +160 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +1 -1
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +5 -8
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +2 -5
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +6 -28
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +54 -11
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +2 -2
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +86 -4
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +7 -58
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +11 -28
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +4 -37
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +1 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/names.ts +75 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +44 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +16 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +5 -9
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +0 -1
- package/runtime/node_modules/@metro-labs/xmtp/package.json +3 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +10 -20
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +2 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +15 -204
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +4 -43
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +41 -219
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +3 -3
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +7 -24
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +8 -18
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +1 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +2 -2
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +30 -16
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +12 -20
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +1 -19
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +1 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/names.ts +61 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/profile.ts +153 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/sender.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/smart.ts +72 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +5 -1
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +4 -51
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +30 -44
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +4 -6
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +19 -5
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +14 -4
- package/runtime/runtime.json +1 -1
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +37 -0
- package/dist/prompt.js +0 -10
- package/dist/store.js +0 -70
- package/runtime/node_modules/@metro-labs/discord/src/station.ts +0 -19
- package/runtime/node_modules/@metro-labs/discord/src/voice-speak.ts +0 -195
- package/runtime/node_modules/@metro-labs/discord/src/voice-transcribe.ts +0 -292
- package/runtime/node_modules/@metro-labs/discord/src/voice.ts +0 -197
- package/runtime/node_modules/@metro-labs/discord/src/wire.ts +0 -5
- package/runtime/node_modules/@metro-labs/mcp/package.json +0 -66
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +0 -24
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +0 -311
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +0 -335
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +0 -129
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +0 -322
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +0 -111
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair.ts +0 -20
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/collection-api.ts +0 -202
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +0 -31
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +0 -100
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +0 -57
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +0 -333
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +0 -164
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +0 -208
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +0 -79
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +0 -65
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +0 -58
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +0 -214
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +0 -125
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-pair.ts +0 -18
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +0 -105
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +0 -41
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +0 -218
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +0 -169
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +0 -91
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +0 -215
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +0 -37
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-collections.ts +0 -218
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +0 -329
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +0 -364
- package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +0 -300
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +0 -31
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +0 -105
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +0 -131
- package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +0 -45
- package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +0 -57
- package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +0 -18
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +0 -29
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +0 -21
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +0 -66
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +0 -73
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +0 -172
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +0 -43
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +0 -240
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +0 -2
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +0 -80
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +0 -88
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +0 -3
- package/runtime/node_modules/@metro-labs/telegram-user/src/accounts.ts +0 -81
- package/runtime/node_modules/@metro-labs/telegram-user/src/actions.ts +0 -254
- package/runtime/node_modules/@metro-labs/telegram-user/src/attachments.ts +0 -87
- package/runtime/node_modules/@metro-labs/telegram-user/src/format.ts +0 -141
- package/runtime/node_modules/@metro-labs/telegram-user/src/inbound.ts +0 -78
- package/runtime/node_modules/@metro-labs/telegram-user/src/index.ts +0 -46
- package/runtime/node_modules/@metro-labs/telegram-user/src/media-actions.ts +0 -80
- package/runtime/node_modules/@metro-labs/telegram-user/src/members.ts +0 -75
- package/runtime/node_modules/@metro-labs/telegram-user/src/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/telegram-user/src/station.ts +0 -24
- package/runtime/node_modules/@metro-labs/telegram-user/src/types.ts +0 -7
- package/runtime/node_modules/@metro-labs/telegram-user/src/wire.ts +0 -1
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +0 -22
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +0 -5
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +0 -87
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +0 -55
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +0 -349
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/log.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/mcp → core/src}/str.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → core/src}/train-error.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/channels/webhook-note.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp → daemon}/src/mcp/raw-get-stream.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/api-base.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{discord → discord-bot}/src/verify.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → http/src}/api-error.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{mcp/src/daemon → http/src}/cors.ts +0 -0
- /package/runtime/node_modules/@metro-labs/{telegram → telegram-bot}/src/verify.ts +0 -0
|
@@ -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
|
+
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
|
+
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
|
+
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,32 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { totalmem } from 'node:os';
|
|
3
|
+
import { helperArgv, runningAsMetro } from '../metro-user/privilege.js';
|
|
4
|
+
|
|
5
|
+
const GIB = 1024 ** 3;
|
|
6
|
+
const RESERVE = 1.5 * GIB;
|
|
7
|
+
const FLOOR = GIB;
|
|
8
|
+
|
|
9
|
+
export interface ScopeHost {
|
|
10
|
+
metro: boolean;
|
|
11
|
+
systemd: boolean;
|
|
12
|
+
total: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const realHost = (): ScopeHost => ({
|
|
16
|
+
metro: runningAsMetro(),
|
|
17
|
+
systemd: existsSync('/run/systemd/system'),
|
|
18
|
+
total: totalmem(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export function sessionMemoryLimit(total: number): number {
|
|
22
|
+
const kept = Math.min(Math.floor(total * 0.8), total - RESERVE);
|
|
23
|
+
return Math.max(FLOOR, Math.floor(kept / (1024 * 1024)) * 1024 * 1024);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function inSessionScope(command: [string, string[]], host: ScopeHost = realHost()): [string, string[]] {
|
|
27
|
+
const [file, args] = command;
|
|
28
|
+
const asAgent = file === 'sudo' && args[0] === '-n' && args[1] === '-u' && args[3] === '--';
|
|
29
|
+
if (!host.metro || !host.systemd || !asAgent) return command;
|
|
30
|
+
const limit = sessionMemoryLimit(host.total);
|
|
31
|
+
return helperArgv(['as-agent-scope', String(limit), String(Math.floor(limit * 0.9)), ...args.slice(4)]);
|
|
32
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync } from '../agent-user/agent-fs.js';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
5
|
+
import { writeHomeText } from '../agent-user/home-fs.js';
|
|
6
|
+
import { agentUser } from '../agent-user/user.js';
|
|
7
|
+
|
|
8
|
+
const FILE = '.claude.json';
|
|
9
|
+
const FLAG = 'hasCompletedOnboarding';
|
|
10
|
+
|
|
11
|
+
export type OnboardingMark = 'marked' | 'already' | 'unreadable';
|
|
12
|
+
|
|
13
|
+
const given = (value: string | undefined): string | undefined =>
|
|
14
|
+
value !== undefined && value.trim() !== '' ? value.trim() : undefined;
|
|
15
|
+
|
|
16
|
+
export function claudeConfigPath(env: NodeJS.ProcessEnv = process.env): string {
|
|
17
|
+
const user = agentUser();
|
|
18
|
+
if (user !== null) return join(user.home, FILE);
|
|
19
|
+
return join(given(env.CLAUDE_CONFIG_DIR) ?? given(env.HOME) ?? homedir(), FILE);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function readConfig(path: string): Record<string, unknown> | null {
|
|
23
|
+
if (!existsSync(path)) return {};
|
|
24
|
+
try {
|
|
25
|
+
const parsed: unknown = JSON.parse(readFileSync(path, 'utf8'));
|
|
26
|
+
return isRecord(parsed) ? parsed : null;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function markOnboardingDone(path = claudeConfigPath()): OnboardingMark {
|
|
33
|
+
const config = readConfig(path);
|
|
34
|
+
if (config === null) return 'unreadable';
|
|
35
|
+
if (config[FLAG] === true) return 'already';
|
|
36
|
+
writeHomeText(path, `${JSON.stringify({ ...config, [FLAG]: true }, null, 2)}\n`, 0o600);
|
|
37
|
+
return 'marked';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const TRUST = 'hasTrustDialogAccepted';
|
|
41
|
+
|
|
42
|
+
function realDir(dir: string): string {
|
|
43
|
+
try {
|
|
44
|
+
return realpathSync(dir);
|
|
45
|
+
} catch {
|
|
46
|
+
return dir;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function trustFolder(dir: string, path = claudeConfigPath()): OnboardingMark {
|
|
51
|
+
const config = readConfig(path);
|
|
52
|
+
if (config === null) return 'unreadable';
|
|
53
|
+
const key = realDir(dir);
|
|
54
|
+
const projects = isRecord(config.projects) ? config.projects : {};
|
|
55
|
+
const project = isRecord(projects[key]) ? projects[key] : {};
|
|
56
|
+
if (project[TRUST] === true) return 'already';
|
|
57
|
+
const next = { ...config, projects: { ...projects, [key]: { ...project, [TRUST]: true } } };
|
|
58
|
+
writeHomeText(path, `${JSON.stringify(next, null, 2)}\n`, 0o600);
|
|
59
|
+
return 'marked';
|
|
60
|
+
}
|