@stage-labs/metro 0.1.0-beta.13 → 0.1.0-beta.130
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +14 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +176 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +93 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +43 -0
- package/dist/plugin.js +120 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +38 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +150 -0
- package/dist/service.js +242 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -27
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +18 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +55 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +122 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/marketplace/plugin/orchestrator.md +16 -0
- package/runtime/marketplace/plugin/skills/refresh/SKILL.md +15 -0
- package/runtime/node_modules/@metro-labs/core/package.json +43 -0
- package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +87 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +108 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +365 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +300 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +371 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +95 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +309 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +169 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +140 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +253 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +239 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +154 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +310 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +175 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/json.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +171 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +122 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +296 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +286 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +332 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/watch.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +259 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +187 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +269 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +151 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +372 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/identity.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +261 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/http/package.json +33 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +219 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +130 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +132 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +123 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +29 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +10 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +190 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +347 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +18 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +39 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -252
- package/dist/broker.js +0 -202
- package/dist/cache.js +0 -69
- package/dist/cli/actions.js +0 -206
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -188
- package/dist/cli/skill.js +0 -62
- package/dist/cli/tail.js +0 -164
- package/dist/cli/util.js +0 -72
- package/dist/cli/webhook.js +0 -81
- package/dist/codex-rc.js +0 -245
- package/dist/dispatcher.js +0 -128
- package/dist/history.js +0 -121
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/monitor.js +0 -194
- package/dist/paths.js +0 -68
- package/dist/registry.js +0 -48
- package/dist/stations/claude.js +0 -45
- package/dist/stations/codex.js +0 -68
- package/dist/stations/discord.js +0 -216
- package/dist/stations/index.js +0 -129
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -234
- package/dist/stations/webhook.js +0 -103
- package/dist/tunnel.js +0 -64
- package/dist/webhooks.js +0 -41
- package/docs/broker.md +0 -214
- package/docs/monitor.md +0 -163
- package/docs/uri-scheme.md +0 -124
- package/docs/users.md +0 -226
- package/skills/metro/SKILL.md +0 -256
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import {
|
|
2
|
+
closeSync,
|
|
3
|
+
createReadStream,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
openSync,
|
|
7
|
+
readdirSync,
|
|
8
|
+
readSync,
|
|
9
|
+
renameSync,
|
|
10
|
+
rmSync,
|
|
11
|
+
statSync,
|
|
12
|
+
utimesSync,
|
|
13
|
+
writeFileSync,
|
|
14
|
+
} from 'node:fs';
|
|
15
|
+
import { homedir } from 'node:os';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
import { createInterface } from 'node:readline';
|
|
18
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
19
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
20
|
+
|
|
21
|
+
export const PROJECT_RE = /^[A-Za-z0-9._-]{1,200}$/;
|
|
22
|
+
export const SESSION_RE = /^[A-Za-z0-9][A-Za-z0-9-]{7,63}$/;
|
|
23
|
+
const MEMORY_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,119}\.md$/;
|
|
24
|
+
const EDGE_BYTES = 64 * 1024;
|
|
25
|
+
const TEXT_CAP = 20_000;
|
|
26
|
+
const RESULT_CAP = 8_000;
|
|
27
|
+
const INPUT_CAP = 4_000;
|
|
28
|
+
const FILE_CAP = 1_000_000;
|
|
29
|
+
const MEMORY_MAX = 256 * 1024;
|
|
30
|
+
const MEMORY_MODE = 0o644;
|
|
31
|
+
const NOISE = ['<task-notification>', '<system-reminder>', '<local-command', '<command-name>'];
|
|
32
|
+
|
|
33
|
+
export function claudeDir(): string {
|
|
34
|
+
const explicit = process.env.METRO_CLAUDE_DIR?.trim();
|
|
35
|
+
if (explicit !== undefined && explicit !== '') return explicit;
|
|
36
|
+
const cfg = process.env.CLAUDE_CONFIG_DIR?.trim();
|
|
37
|
+
if (cfg !== undefined && cfg !== '') return cfg;
|
|
38
|
+
return join(homedir(), '.claude');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const str = (v: unknown): string | null => (typeof v === 'string' && v !== '' ? v : null);
|
|
42
|
+
|
|
43
|
+
export function safeName(value: string, re: RegExp, what: string): string {
|
|
44
|
+
if (!re.test(value) || value === '.' || value === '..')
|
|
45
|
+
throw new ApiError(`that is not a ${what}`, 400);
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function projectDir(project: string, dir: string): string {
|
|
50
|
+
const path = join(dir, 'projects', safeName(project, PROJECT_RE, 'Claude project id'));
|
|
51
|
+
if (!existsSync(path)) throw new ApiError('no such Claude project', 404);
|
|
52
|
+
return path;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parseLines(text: string): Record<string, unknown>[] {
|
|
56
|
+
const out: Record<string, unknown>[] = [];
|
|
57
|
+
for (const line of text.split('\n')) {
|
|
58
|
+
if (line.trim() === '') continue;
|
|
59
|
+
try {
|
|
60
|
+
const parsed: unknown = JSON.parse(line);
|
|
61
|
+
if (isRecord(parsed)) out.push(parsed);
|
|
62
|
+
} catch {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function readEdge(path: string, fromEnd: boolean): Record<string, unknown>[] {
|
|
70
|
+
const size = statSync(path).size;
|
|
71
|
+
const length = Math.min(EDGE_BYTES, size);
|
|
72
|
+
const fd = openSync(path, 'r');
|
|
73
|
+
try {
|
|
74
|
+
const buffer = Buffer.alloc(length);
|
|
75
|
+
readSync(fd, buffer, 0, length, fromEnd ? size - length : 0);
|
|
76
|
+
const text = buffer.toString('utf8');
|
|
77
|
+
const cut = fromEnd && length < size ? text.indexOf('\n') + 1 : 0;
|
|
78
|
+
return parseLines(text.slice(cut));
|
|
79
|
+
} finally {
|
|
80
|
+
closeSync(fd);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface ClaudeProject {
|
|
85
|
+
id: string;
|
|
86
|
+
cwd: string | null;
|
|
87
|
+
sessions: number;
|
|
88
|
+
lastActiveAt: string | null;
|
|
89
|
+
hasMemory: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function sessionFiles(path: string): { name: string; mtime: number; size: number }[] {
|
|
93
|
+
return readdirSync(path)
|
|
94
|
+
.filter((name) => name.endsWith('.jsonl'))
|
|
95
|
+
.map((name) => {
|
|
96
|
+
const s = statSync(join(path, name));
|
|
97
|
+
return { name, mtime: s.mtimeMs, size: s.size };
|
|
98
|
+
})
|
|
99
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function cwdOf(path: string): string | null {
|
|
103
|
+
for (const line of readEdge(path, false)) {
|
|
104
|
+
const cwd = str(line.cwd);
|
|
105
|
+
if (cwd !== null) return cwd;
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function listClaudeProjects(dir = claudeDir()): ClaudeProject[] {
|
|
111
|
+
const root = join(dir, 'projects');
|
|
112
|
+
if (!existsSync(root)) return [];
|
|
113
|
+
return readdirSync(root, { withFileTypes: true })
|
|
114
|
+
.filter((entry) => entry.isDirectory() && PROJECT_RE.test(entry.name))
|
|
115
|
+
.map((entry) => {
|
|
116
|
+
const path = join(root, entry.name);
|
|
117
|
+
const files = sessionFiles(path);
|
|
118
|
+
const newest = files[0];
|
|
119
|
+
return {
|
|
120
|
+
id: entry.name,
|
|
121
|
+
cwd: newest === undefined ? null : cwdOf(join(path, newest.name)),
|
|
122
|
+
sessions: files.length,
|
|
123
|
+
lastActiveAt: newest === undefined ? null : new Date(newest.mtime).toISOString(),
|
|
124
|
+
hasMemory: existsSync(join(path, 'memory')),
|
|
125
|
+
};
|
|
126
|
+
})
|
|
127
|
+
.sort((a, b) => (b.lastActiveAt ?? '').localeCompare(a.lastActiveAt ?? ''));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface ClaudeSession {
|
|
131
|
+
id: string;
|
|
132
|
+
title: string;
|
|
133
|
+
startedAt: string | null;
|
|
134
|
+
lastAt: string;
|
|
135
|
+
bytes: number;
|
|
136
|
+
gitBranch: string | null;
|
|
137
|
+
version: string | null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function firstPrompt(lines: Record<string, unknown>[]): string | null {
|
|
141
|
+
for (const line of lines) {
|
|
142
|
+
if (line.type !== 'user' || line.isSidechain === true) continue;
|
|
143
|
+
const message = isRecord(line.message) ? line.message : {};
|
|
144
|
+
const text = promptText(message.content);
|
|
145
|
+
if (text !== null && !NOISE.some((n) => text.startsWith(n))) return text.slice(0, 120);
|
|
146
|
+
}
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function promptText(content: unknown): string | null {
|
|
151
|
+
if (typeof content === 'string') return content.trim() === '' ? null : content.trim();
|
|
152
|
+
if (!Array.isArray(content)) return null;
|
|
153
|
+
for (const block of content)
|
|
154
|
+
if (isRecord(block) && block.type === 'text') {
|
|
155
|
+
const text = str(block.text);
|
|
156
|
+
if (text !== null) return text.trim();
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function sessionOf(path: string, id: string, mtime: number, size: number): ClaudeSession {
|
|
162
|
+
const head = readEdge(path, false);
|
|
163
|
+
const tail = readEdge(path, true);
|
|
164
|
+
const titled = [...tail].reverse().find((l) => l.type === 'ai-title' && str(l.aiTitle) !== null);
|
|
165
|
+
const meta = head.find((l) => l.type === 'user' || l.type === 'assistant') ?? {};
|
|
166
|
+
return {
|
|
167
|
+
id,
|
|
168
|
+
title: (titled === undefined ? null : str(titled.aiTitle)) ?? firstPrompt(head) ?? 'Untitled session',
|
|
169
|
+
startedAt: str(head.find((l) => str(l.timestamp) !== null)?.timestamp) ?? null,
|
|
170
|
+
lastAt: new Date(mtime).toISOString(),
|
|
171
|
+
bytes: size,
|
|
172
|
+
gitBranch: str(meta.gitBranch),
|
|
173
|
+
version: str(meta.version),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function listClaudeSessions(project: string, dir = claudeDir()): ClaudeSession[] {
|
|
178
|
+
const path = projectDir(project, dir);
|
|
179
|
+
return sessionFiles(path).map((f) =>
|
|
180
|
+
sessionOf(join(path, f.name), f.name.slice(0, -'.jsonl'.length), f.mtime, f.size),
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export type Block =
|
|
185
|
+
| { kind: 'text'; text: string }
|
|
186
|
+
| { kind: 'tool_use'; name: string; input: string }
|
|
187
|
+
| { kind: 'tool_result'; text: string; isError: boolean }
|
|
188
|
+
| { kind: 'thinking' }
|
|
189
|
+
| { kind: 'image' };
|
|
190
|
+
|
|
191
|
+
export interface TranscriptEntry {
|
|
192
|
+
uuid: string;
|
|
193
|
+
at: string | null;
|
|
194
|
+
role: 'user' | 'assistant';
|
|
195
|
+
blocks: Block[];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export interface TranscriptPage {
|
|
199
|
+
entries: TranscriptEntry[];
|
|
200
|
+
total: number;
|
|
201
|
+
next: number | null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const cap = (text: string, max: number): string =>
|
|
205
|
+
text.length > max ? `${text.slice(0, max)}\n… (${String(text.length - max)} more characters)` : text;
|
|
206
|
+
|
|
207
|
+
function resultText(content: unknown): string {
|
|
208
|
+
if (typeof content === 'string') return content;
|
|
209
|
+
if (!Array.isArray(content)) return '';
|
|
210
|
+
return content
|
|
211
|
+
.map((b) => (isRecord(b) && b.type === 'text' ? (str(b.text) ?? '') : ''))
|
|
212
|
+
.filter((t) => t !== '')
|
|
213
|
+
.join('\n');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function textBlock(raw: Record<string, unknown>): Block | null {
|
|
217
|
+
const text = str(raw.text);
|
|
218
|
+
if (text === null || NOISE.some((n) => text.startsWith(n))) return null;
|
|
219
|
+
return { kind: 'text', text: cap(text, TEXT_CAP) };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const BLOCKS: Record<string, (raw: Record<string, unknown>) => Block | null> = {
|
|
223
|
+
text: textBlock,
|
|
224
|
+
tool_use: (raw) => ({
|
|
225
|
+
kind: 'tool_use',
|
|
226
|
+
name: str(raw.name) ?? 'tool',
|
|
227
|
+
input: cap(JSON.stringify(raw.input ?? {}, null, 1), INPUT_CAP),
|
|
228
|
+
}),
|
|
229
|
+
tool_result: (raw) => ({
|
|
230
|
+
kind: 'tool_result',
|
|
231
|
+
text: cap(resultText(raw.content), RESULT_CAP),
|
|
232
|
+
isError: raw.is_error === true,
|
|
233
|
+
}),
|
|
234
|
+
thinking: () => ({ kind: 'thinking' }),
|
|
235
|
+
image: () => ({ kind: 'image' }),
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
function blockOf(raw: unknown): Block | null {
|
|
239
|
+
if (!isRecord(raw) || typeof raw.type !== 'string') return null;
|
|
240
|
+
const make = BLOCKS[raw.type];
|
|
241
|
+
return make === undefined ? null : make(raw);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function entryOf(line: Record<string, unknown>): TranscriptEntry | null {
|
|
245
|
+
if ((line.type !== 'user' && line.type !== 'assistant') || line.isSidechain === true) return null;
|
|
246
|
+
const message = isRecord(line.message) ? line.message : {};
|
|
247
|
+
const content = message.content;
|
|
248
|
+
const blocks =
|
|
249
|
+
typeof content === 'string'
|
|
250
|
+
? [blockOf({ type: 'text', text: content })]
|
|
251
|
+
: Array.isArray(content)
|
|
252
|
+
? content.map(blockOf)
|
|
253
|
+
: [];
|
|
254
|
+
const kept = blocks.filter((b): b is Block => b !== null);
|
|
255
|
+
if (kept.length === 0) return null;
|
|
256
|
+
return { uuid: str(line.uuid) ?? '', at: str(line.timestamp), role: line.type, blocks: kept };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export async function readTranscript(
|
|
260
|
+
project: string,
|
|
261
|
+
session: string,
|
|
262
|
+
offset: number,
|
|
263
|
+
limit: number,
|
|
264
|
+
dir = claudeDir(),
|
|
265
|
+
): Promise<TranscriptPage> {
|
|
266
|
+
const path = join(projectDir(project, dir), `${safeName(session, SESSION_RE, 'session id')}.jsonl`);
|
|
267
|
+
if (!existsSync(path)) throw new ApiError('no such session', 404);
|
|
268
|
+
const entries: TranscriptEntry[] = [];
|
|
269
|
+
let total = 0;
|
|
270
|
+
const lines = createInterface({ input: createReadStream(path, { encoding: 'utf8' }), crlfDelay: Infinity });
|
|
271
|
+
for await (const line of lines) {
|
|
272
|
+
const parsed = parseLines(line)[0];
|
|
273
|
+
const entry = parsed === undefined ? null : entryOf(parsed);
|
|
274
|
+
if (entry === null) continue;
|
|
275
|
+
if (total >= offset && entries.length < limit) entries.push(entry);
|
|
276
|
+
total += 1;
|
|
277
|
+
}
|
|
278
|
+
return { entries, total, next: offset + limit < total ? offset + limit : null };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function deleteClaudeSession(project: string, session: string, dir = claudeDir()): void {
|
|
282
|
+
const root = projectDir(project, dir);
|
|
283
|
+
const id = safeName(session, SESSION_RE, 'session id');
|
|
284
|
+
const file = join(root, `${id}.jsonl`);
|
|
285
|
+
if (!existsSync(file)) throw new ApiError('no such session', 404);
|
|
286
|
+
rmSync(file);
|
|
287
|
+
rmSync(join(root, id), { recursive: true, force: true });
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export interface MemoryFile {
|
|
291
|
+
name: string;
|
|
292
|
+
bytes: number;
|
|
293
|
+
modifiedAt: string;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export interface MemoryListing {
|
|
297
|
+
files: MemoryFile[];
|
|
298
|
+
index: string | null;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function memoryDir(project: string, dir: string): string {
|
|
302
|
+
return join(projectDir(project, dir), 'memory');
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function readCapped(path: string): string {
|
|
306
|
+
const size = statSync(path).size;
|
|
307
|
+
const fd = openSync(path, 'r');
|
|
308
|
+
try {
|
|
309
|
+
const buffer = Buffer.alloc(Math.min(size, FILE_CAP));
|
|
310
|
+
readSync(fd, buffer, 0, buffer.length, 0);
|
|
311
|
+
return size > FILE_CAP ? `${buffer.toString('utf8')}\n\n… truncated` : buffer.toString('utf8');
|
|
312
|
+
} finally {
|
|
313
|
+
closeSync(fd);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export function listMemory(project: string, dir = claudeDir()): MemoryListing {
|
|
318
|
+
const path = memoryDir(project, dir);
|
|
319
|
+
if (!existsSync(path)) return { files: [], index: null };
|
|
320
|
+
const files = readdirSync(path)
|
|
321
|
+
.filter((name) => MEMORY_RE.test(name) && name !== 'MEMORY.md')
|
|
322
|
+
.map((name) => {
|
|
323
|
+
const s = statSync(join(path, name));
|
|
324
|
+
return { name, bytes: s.size, modifiedAt: s.mtime.toISOString() };
|
|
325
|
+
})
|
|
326
|
+
.sort((a, b) => b.modifiedAt.localeCompare(a.modifiedAt) || a.name.localeCompare(b.name));
|
|
327
|
+
const index = join(path, 'MEMORY.md');
|
|
328
|
+
return { files, index: existsSync(index) ? readCapped(index) : null };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const stampOf = (raw: string | undefined): Date | null => {
|
|
332
|
+
if (raw === undefined) return null;
|
|
333
|
+
const at = new Date(raw);
|
|
334
|
+
return Number.isNaN(at.getTime()) ? null : at;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
export function writeMemoryFile(project: string, name: string, text: string, dir = claudeDir(), modifiedAt?: string): MemoryFile {
|
|
338
|
+
if (text.trim() === '') throw new ApiError('a memory file cannot be empty', 400);
|
|
339
|
+
if (Buffer.byteLength(text, 'utf8') > MEMORY_MAX)
|
|
340
|
+
throw new ApiError(`a memory file is at most ${String(MEMORY_MAX)} bytes`, 400);
|
|
341
|
+
const folder = memoryDir(project, dir);
|
|
342
|
+
const file = safeName(name, MEMORY_RE, 'memory file name');
|
|
343
|
+
mkdirSync(folder, { recursive: true });
|
|
344
|
+
const path = join(folder, file);
|
|
345
|
+
const tmp = `${path}.metro-${String(process.pid)}`;
|
|
346
|
+
writeFileSync(tmp, text, { mode: MEMORY_MODE });
|
|
347
|
+
renameSync(tmp, path);
|
|
348
|
+
const stamp = stampOf(modifiedAt);
|
|
349
|
+
if (stamp !== null) utimesSync(path, stamp, stamp);
|
|
350
|
+
const stat = statSync(path);
|
|
351
|
+
return { name: file, bytes: stat.size, modifiedAt: stat.mtime.toISOString() };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export function readMemoryFile(project: string, name: string, dir = claudeDir()): string {
|
|
355
|
+
const path = join(memoryDir(project, dir), safeName(name, MEMORY_RE, 'memory file name'));
|
|
356
|
+
if (!existsSync(path)) throw new ApiError('no such memory file', 404);
|
|
357
|
+
return readCapped(path);
|
|
358
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
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
|
+
|
|
8
|
+
const LOGIN_COMMAND = ['claude', 'auth', 'login', '--claudeai'];
|
|
9
|
+
const STATUS_COMMAND = ['auth', 'status', '--json'];
|
|
10
|
+
const TTL_MS = 10 * 60_000;
|
|
11
|
+
const OUTPUT_MAX = 8_000;
|
|
12
|
+
const COLS = 100;
|
|
13
|
+
const ROWS = 40;
|
|
14
|
+
const ESC = String.fromCharCode(27);
|
|
15
|
+
const CSI = /^\[[0-9;?]*[a-zA-Z]/;
|
|
16
|
+
const URL_RE = /https:\/\/[^\s"'`]+/g;
|
|
17
|
+
|
|
18
|
+
export type LoginState = 'pending' | 'done' | 'failed';
|
|
19
|
+
|
|
20
|
+
export interface LoginView {
|
|
21
|
+
id: string;
|
|
22
|
+
state: LoginState;
|
|
23
|
+
url: string | null;
|
|
24
|
+
output: string;
|
|
25
|
+
error: string | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface Live {
|
|
29
|
+
id: string;
|
|
30
|
+
startedAt: number;
|
|
31
|
+
state: LoginState;
|
|
32
|
+
output: string;
|
|
33
|
+
error: string | null;
|
|
34
|
+
write: (text: string) => void;
|
|
35
|
+
kill: () => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface LoginDeps {
|
|
39
|
+
command?: string[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let live: Live | null = null;
|
|
43
|
+
|
|
44
|
+
export function plainText(raw: string): string {
|
|
45
|
+
return raw
|
|
46
|
+
.split(ESC)
|
|
47
|
+
.map((part, index) => (index === 0 ? part : part.replace(CSI, '')))
|
|
48
|
+
.join('');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function loginUrlIn(output: string): string | null {
|
|
52
|
+
const found = output.match(URL_RE) ?? [];
|
|
53
|
+
const trimmed = found.map((url) => url.replace(/[.,)]+$/, ''));
|
|
54
|
+
return trimmed.find((url) => url.includes('claude.ai') || url.includes('anthropic.com')) ?? trimmed[0] ?? null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const view = (session: Live): LoginView => ({
|
|
58
|
+
id: session.id,
|
|
59
|
+
state: session.state,
|
|
60
|
+
url: loginUrlIn(session.output),
|
|
61
|
+
output: session.output,
|
|
62
|
+
error: session.error,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
function sweep(now: number): void {
|
|
66
|
+
if (live !== null && live.state !== 'pending') return;
|
|
67
|
+
if (live !== null && now - live.startedAt > TTL_MS) {
|
|
68
|
+
live.kill();
|
|
69
|
+
live = null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function claudeInstalled(): boolean {
|
|
74
|
+
const run = spawnSync('claude', ['--version'], { stdio: 'ignore' });
|
|
75
|
+
return run.error === undefined && run.status === 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function claudeAccount(): { signedIn: boolean; account: string | null } {
|
|
79
|
+
const run = spawnSync('claude', STATUS_COMMAND, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
80
|
+
if (run.error !== undefined || typeof run.stdout !== 'string') return { signedIn: false, account: null };
|
|
81
|
+
try {
|
|
82
|
+
const parsed: unknown = JSON.parse(run.stdout);
|
|
83
|
+
if (!isRecord(parsed)) return { signedIn: false, account: null };
|
|
84
|
+
const account = typeof parsed.email === 'string' ? parsed.email : typeof parsed.organization === 'string' ? parsed.organization : null;
|
|
85
|
+
return { signedIn: parsed.authenticated === true || parsed.loggedIn === true || account !== null, account };
|
|
86
|
+
} catch {
|
|
87
|
+
return { signedIn: false, account: null };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function note(session: Live, chunk: string): void {
|
|
92
|
+
const next = session.output + plainText(chunk);
|
|
93
|
+
session.output = next.length > OUTPUT_MAX ? next.slice(next.length - OUTPUT_MAX) : next;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function startClaudeLogin(deps: LoginDeps = {}, now = Date.now()): LoginView {
|
|
97
|
+
sweep(now);
|
|
98
|
+
if (live !== null && live.state === 'pending') return view(live);
|
|
99
|
+
const command = deps.command ?? LOGIN_COMMAND;
|
|
100
|
+
const session: Live = {
|
|
101
|
+
id: randomBytes(9).toString('base64url'),
|
|
102
|
+
startedAt: now,
|
|
103
|
+
state: 'pending',
|
|
104
|
+
output: '',
|
|
105
|
+
error: null,
|
|
106
|
+
write: () => undefined,
|
|
107
|
+
kill: () => undefined,
|
|
108
|
+
};
|
|
109
|
+
const terminal = new Bun.Terminal({
|
|
110
|
+
cols: COLS,
|
|
111
|
+
rows: ROWS,
|
|
112
|
+
data: (_term, chunk: Uint8Array) => {
|
|
113
|
+
note(session, Buffer.from(chunk).toString('utf8'));
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
const proc = Bun.spawn(command, { terminal, env: { ...process.env, TERM: 'xterm-256color' } });
|
|
117
|
+
session.write = (text: string) => {
|
|
118
|
+
terminal.write(Buffer.from(`${text}\r`, 'utf8'));
|
|
119
|
+
};
|
|
120
|
+
session.kill = () => {
|
|
121
|
+
proc.kill();
|
|
122
|
+
terminal.close();
|
|
123
|
+
};
|
|
124
|
+
proc.exited
|
|
125
|
+
.then((code) => {
|
|
126
|
+
session.state = code === 0 ? 'done' : 'failed';
|
|
127
|
+
if (code !== 0) session.error = `the login ended with status ${String(code)}`;
|
|
128
|
+
terminal.close();
|
|
129
|
+
const onboarding = code === 0 ? markOnboardingDone() : 'skipped';
|
|
130
|
+
log.info({ state: session.state, onboarding }, 'claude-login: the official login finished');
|
|
131
|
+
})
|
|
132
|
+
.catch((err: unknown) => {
|
|
133
|
+
session.state = 'failed';
|
|
134
|
+
session.error = errMsg(err);
|
|
135
|
+
});
|
|
136
|
+
live = session;
|
|
137
|
+
log.info({ command: command[0] }, 'claude-login: started');
|
|
138
|
+
return view(session);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function current(id: string): Live {
|
|
142
|
+
const session = live;
|
|
143
|
+
if (session?.id !== id) throw new ApiError('that sign-in is over; start another one', 404);
|
|
144
|
+
return session;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function claudeLoginView(id: string, now = Date.now()): LoginView {
|
|
148
|
+
sweep(now);
|
|
149
|
+
return view(current(id));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function answerClaudeLogin(id: string, text: string): LoginView {
|
|
153
|
+
const session = current(id);
|
|
154
|
+
if (session.state !== 'pending') throw new ApiError('that sign-in has already finished', 409);
|
|
155
|
+
session.write(text);
|
|
156
|
+
return view(session);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function endClaudeLogin(id: string): { ended: true } {
|
|
160
|
+
const session = current(id);
|
|
161
|
+
session.kill();
|
|
162
|
+
live = null;
|
|
163
|
+
return { ended: true };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const forgetClaudeLogin = (): void => {
|
|
167
|
+
live?.kill();
|
|
168
|
+
live = null;
|
|
169
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
5
|
+
|
|
6
|
+
const FILE = '.claude.json';
|
|
7
|
+
const FLAG = 'hasCompletedOnboarding';
|
|
8
|
+
|
|
9
|
+
export type OnboardingMark = 'marked' | 'already' | 'unreadable';
|
|
10
|
+
|
|
11
|
+
const given = (value: string | undefined): string | undefined =>
|
|
12
|
+
value !== undefined && value.trim() !== '' ? value.trim() : undefined;
|
|
13
|
+
|
|
14
|
+
export function claudeConfigPath(env: NodeJS.ProcessEnv = process.env): string {
|
|
15
|
+
return join(given(env.CLAUDE_CONFIG_DIR) ?? given(env.HOME) ?? homedir(), FILE);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function readConfig(path: string): Record<string, unknown> | null {
|
|
19
|
+
if (!existsSync(path)) return {};
|
|
20
|
+
try {
|
|
21
|
+
const parsed: unknown = JSON.parse(readFileSync(path, 'utf8'));
|
|
22
|
+
return isRecord(parsed) ? parsed : null;
|
|
23
|
+
} catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function markOnboardingDone(path = claudeConfigPath()): OnboardingMark {
|
|
29
|
+
const config = readConfig(path);
|
|
30
|
+
if (config === null) return 'unreadable';
|
|
31
|
+
if (config[FLAG] === true) return 'already';
|
|
32
|
+
writeFileSync(path, `${JSON.stringify({ ...config, [FLAG]: true }, null, 2)}\n`, { mode: 0o600 });
|
|
33
|
+
return 'marked';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const TRUST = 'hasTrustDialogAccepted';
|
|
37
|
+
|
|
38
|
+
function realDir(dir: string): string {
|
|
39
|
+
try {
|
|
40
|
+
return realpathSync(dir);
|
|
41
|
+
} catch {
|
|
42
|
+
return dir;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function trustFolder(dir: string, path = claudeConfigPath()): OnboardingMark {
|
|
47
|
+
const config = readConfig(path);
|
|
48
|
+
if (config === null) return 'unreadable';
|
|
49
|
+
const key = realDir(dir);
|
|
50
|
+
const projects = isRecord(config.projects) ? config.projects : {};
|
|
51
|
+
const project = isRecord(projects[key]) ? projects[key] : {};
|
|
52
|
+
if (project[TRUST] === true) return 'already';
|
|
53
|
+
const next = { ...config, projects: { ...projects, [key]: { ...project, [TRUST]: true } } };
|
|
54
|
+
writeFileSync(path, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
|
|
55
|
+
return 'marked';
|
|
56
|
+
}
|