@stage-labs/metro 0.1.0-beta.3 → 0.1.0-beta.300
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/agent.js +128 -0
- package/dist/api.js +11 -0
- package/dist/automation.js +56 -0
- package/dist/background.js +141 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +204 -0
- package/dist/cli.js +109 -258
- package/dist/control.js +74 -0
- package/dist/hold.js +210 -0
- package/dist/local.js +43 -0
- package/dist/mcp-config.js +27 -0
- package/dist/memory.js +222 -0
- package/dist/native-recovery-scan.js +246 -0
- package/dist/native-recovery-state.js +154 -0
- package/dist/native-recovery.js +164 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +44 -0
- package/dist/provider-flags.js +9 -0
- package/dist/root-release-io.js +161 -0
- package/dist/root-release.js +74 -0
- package/dist/route.js +66 -0
- package/dist/runtime-install.js +114 -0
- package/dist/runtime.js +71 -0
- package/dist/serve.js +157 -0
- package/dist/service.js +328 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/dist/voice.js +56 -0
- package/package.json +14 -22
- 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/MEMORY.md +78 -0
- package/runtime/marketplace/plugin/METRO.md +26 -0
- package/runtime/marketplace/plugin/STAGE.md +109 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +75 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +30 -0
- package/runtime/marketplace/plugin/bin/policy.mjs +105 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/node_modules/@metro-labs/core/package.json +59 -0
- package/runtime/node_modules/@metro-labs/core/src/automation-files.ts +216 -0
- package/runtime/node_modules/@metro-labs/core/src/automation-store.ts +171 -0
- package/runtime/node_modules/@metro-labs/core/src/automation-types.ts +141 -0
- package/runtime/node_modules/@metro-labs/core/src/call.ts +81 -0
- package/runtime/node_modules/@metro-labs/core/src/connector-sync.ts +17 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +165 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +28 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +78 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +227 -0
- package/runtime/node_modules/@metro-labs/core/src/runner-activity.ts +263 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +74 -0
- package/runtime/node_modules/@metro-labs/core/src/skill-source.ts +96 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +14 -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/core/src/stations/attachments.ts +202 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/channel-directory.ts +159 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/channel-pages.ts +129 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/mail-text.ts +72 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/mail-trust.ts +29 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/mail.ts +212 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +16 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/oauth.ts +126 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +59 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-boot.ts +63 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-events.ts +84 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +126 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/tickets.ts +44 -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 +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/agent-fs.ts +246 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/crontab.ts +44 -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 +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/metro-home.ts +42 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +92 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +93 -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 +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +93 -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 +362 -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 +133 -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 +317 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +142 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +135 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +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/owner-line.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +189 -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 +250 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +36 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +390 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +89 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/react-note.ts +13 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +133 -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 +316 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/channel-check.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +359 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/github-api.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/github-fetch.ts +160 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/github-skills.ts +259 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/github-validate.ts +53 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +242 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory-routine.ts +123 -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/pinned.ts +16 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/runner-activity.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/runner-process.ts +52 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/runner.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-continuity.ts +57 -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 +382 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup-api.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup-state.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +346 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/usage-probe.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +76 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/agent-key.ts +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +316 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +105 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/enroll-api.ts +101 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +114 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/health.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/metro-api.ts +93 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-api.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-control.ts +366 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-guard.ts +95 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-operations.ts +226 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-preview.ts +235 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-source.ts +42 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-state.ts +142 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-url.ts +9 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +178 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +171 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-relay.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime-discovery.ts +115 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime.ts +327 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-api.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-control.ts +191 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-setting.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-watch.ts +53 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +170 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/publication.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-id.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-passive.ts +96 -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-session.ts +11 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +4 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +392 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/server-keys.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +393 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +1 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/transfer-api.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/transfer-crypto.ts +44 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/transfer-store.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-reaper.ts +174 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +278 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +233 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/claude-logins.ts +168 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +219 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +119 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +179 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +209 -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 +143 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/fallback.ts +147 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +285 -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 +396 -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 +236 -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 +259 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +362 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-lists.ts +57 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +157 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +83 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter-login.ts +192 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter-signin.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +104 -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 +117 -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 +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/tool-search.ts +126 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage-refresh.ts +115 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +343 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +165 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-bridge.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-permission.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +253 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-tool.ts +80 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/diagnostics.ts +102 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-schema.ts +44 -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 +169 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +30 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +108 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-lookup.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +245 -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 +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-slot.ts +141 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +270 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/speech-schema.ts +11 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/speech-send.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-def.ts +9 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +232 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +370 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/helper-script.ts +183 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/imds.ts +16 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/install.ts +78 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/privilege.ts +50 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/root-follow.ts +16 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/run-event.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/run-events.ts +101 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +323 -0
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +119 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/gmail-attach.ts +67 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/gmail-detach.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +371 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +229 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +82 -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/cloudwatch.ts +178 -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-browser.ts +107 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-driver.ts +42 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-gmail.ts +78 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +135 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +271 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +314 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +110 -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 +267 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +53 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +135 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/webhook-endpoints.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +124 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/api.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/config.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/firewall.ts +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/daemon/src/voice/api.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/approvals.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/audio.ts +193 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/brain.ts +146 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/call.ts +274 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/calls.ts +156 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/peer.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/scribe.ts +115 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/shared-talk.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/shared.ts +200 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/speakable.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/speech-queue.ts +200 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/speech.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/store.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/voice/talk.ts +264 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +26 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +99 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +222 -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/channel-source.ts +79 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/channels.ts +186 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +169 -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 +121 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +42 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/gmail/package.json +27 -0
- package/runtime/node_modules/@metro-labs/gmail/src/accounts.ts +133 -0
- package/runtime/node_modules/@metro-labs/gmail/src/actions.ts +20 -0
- package/runtime/node_modules/@metro-labs/gmail/src/api.ts +34 -0
- package/runtime/node_modules/@metro-labs/gmail/src/attachments.ts +38 -0
- package/runtime/node_modules/@metro-labs/gmail/src/auth.ts +115 -0
- package/runtime/node_modules/@metro-labs/gmail/src/channels.ts +66 -0
- package/runtime/node_modules/@metro-labs/gmail/src/config.ts +15 -0
- package/runtime/node_modules/@metro-labs/gmail/src/inbound.ts +97 -0
- package/runtime/node_modules/@metro-labs/gmail/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/gmail/src/login.ts +39 -0
- package/runtime/node_modules/@metro-labs/gmail/src/managed-binding.ts +33 -0
- package/runtime/node_modules/@metro-labs/gmail/src/managed.ts +45 -0
- package/runtime/node_modules/@metro-labs/gmail/src/message.ts +125 -0
- package/runtime/node_modules/@metro-labs/gmail/src/mime.ts +70 -0
- package/runtime/node_modules/@metro-labs/gmail/src/outbound.ts +125 -0
- package/runtime/node_modules/@metro-labs/gmail/src/query.ts +101 -0
- package/runtime/node_modules/@metro-labs/gmail/src/state.ts +30 -0
- package/runtime/node_modules/@metro-labs/gmail/src/station.ts +16 -0
- package/runtime/node_modules/@metro-labs/gmail/src/trust.ts +42 -0
- package/runtime/node_modules/@metro-labs/http/package.json +43 -0
- package/runtime/node_modules/@metro-labs/http/src/agent-signature.ts +110 -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 +147 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-delivery-validation.ts +116 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-delivery.ts +19 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-discovery.ts +150 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-grant.ts +59 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-input.ts +73 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-mcp.ts +108 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-migration.ts +178 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-oauth.ts +182 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-policy.ts +92 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-public-url.ts +64 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-resource.ts +108 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-tools.ts +94 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-url.ts +37 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/ed25519.ts +56 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +31 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +25 -0
- package/runtime/node_modules/@metro-labs/http/src/sigv4.ts +90 -0
- package/runtime/node_modules/@metro-labs/http/src/transfer-crypto.ts +44 -0
- package/runtime/node_modules/@metro-labs/http/src/workos-token.ts +149 -0
- package/runtime/node_modules/@metro-labs/outlook/package.json +25 -0
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +78 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +81 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +120 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +42 -0
- package/runtime/node_modules/@metro-labs/outlook/src/channels.ts +60 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +23 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +59 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +55 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +96 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +129 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +31 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +103 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +84 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +28 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +16 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +41 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +62 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +275 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/channels.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +126 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +79 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +28 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +6 -0
- 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 +242 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/channel-files.ts +3 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/channels.ts +43 -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 +74 -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/observed-chats.ts +172 -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 +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +83 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/updates.ts +26 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +108 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +130 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +152 -0
- package/runtime/node_modules/@metro-labs/threema/src/channels.ts +37 -0
- package/runtime/node_modules/@metro-labs/threema/src/chats.ts +88 -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 +92 -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 +161 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +21 -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 +26 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +45 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +263 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/app-state.ts +49 -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/channel-chats.ts +137 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/channels.ts +48 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client-history.ts +31 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client-types.ts +48 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +400 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +81 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/history-events.ts +127 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/history-files.ts +39 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/history-identity.ts +58 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/history-message.ts +161 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/history-state.ts +344 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/history-types.ts +91 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/history.ts +195 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +7 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/members.ts +94 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/messages.ts +70 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/names.ts +75 -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/send-expiry.ts +65 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +94 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +103 -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 +130 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +151 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +266 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +150 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/call-leftovers.ts +48 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/channels.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +282 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +254 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/delete-requests.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +346 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +68 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/frames.ts +190 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +73 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +209 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/names.ts +61 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/network.ts +114 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/profile.ts +160 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/read-attachments.ts +37 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/resolve.ts +44 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/sender.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/smart.ts +79 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +21 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/stream.ts +66 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +235 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +88 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +98 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wallet.ts +28 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +49 -0
- package/runtime/patches/5bca2c07d5852d3f58453cff0448dd85da13d3665b61e3d758905b298d432d7c.patch +31 -0
- package/runtime/runtime.json +3 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/package.json +59 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/automation-files.ts +216 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/automation-store.ts +171 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/automation-types.ts +141 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/call.ts +81 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/connector-sync.ts +17 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/events.ts +165 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/ids.ts +28 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/lines.ts +78 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/protocol.ts +227 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/runner-activity.ts +263 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/secure-fs.ts +74 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/skill-source.ts +96 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/station-names.ts +14 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/account-store.ts +83 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/attachments.ts +202 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/channel-directory.ts +159 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/channel-pages.ts +129 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/mail-text.ts +72 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/mail-trust.ts +29 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/mail.ts +212 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +16 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/oauth.ts +126 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/station-runtime.ts +59 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/train-boot.ts +63 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/train-events.ts +84 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/types.ts +126 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/tickets.ts +44 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/sdk-runner/runner.json +10 -0
- package/runtime/sdk-runner/src/activity-tasks.ts +194 -0
- package/runtime/sdk-runner/src/activity.ts +391 -0
- package/runtime/sdk-runner/src/app.ts +87 -0
- package/runtime/sdk-runner/src/approvals.ts +37 -0
- package/runtime/sdk-runner/src/automation-cli.ts +10 -0
- package/runtime/sdk-runner/src/automation-command.ts +151 -0
- package/runtime/sdk-runner/src/automation.ts +145 -0
- package/runtime/sdk-runner/src/calls.ts +133 -0
- package/runtime/sdk-runner/src/channel-text.ts +26 -0
- package/runtime/sdk-runner/src/config.ts +50 -0
- package/runtime/sdk-runner/src/errors.ts +131 -0
- package/runtime/sdk-runner/src/inbox.ts +168 -0
- package/runtime/sdk-runner/src/input-queue.ts +77 -0
- package/runtime/sdk-runner/src/input-timing.ts +68 -0
- package/runtime/sdk-runner/src/link.ts +154 -0
- package/runtime/sdk-runner/src/main.ts +55 -0
- package/runtime/sdk-runner/src/recovery.ts +46 -0
- package/runtime/sdk-runner/src/rules.ts +28 -0
- package/runtime/sdk-runner/src/runner.ts +357 -0
- package/runtime/sdk-runner/src/session-store.ts +129 -0
- package/runtime/sdk-runner/src/session-watch.ts +158 -0
- package/runtime/sdk-runner/src/singleton.ts +15 -0
- package/runtime/sdk-runner/src/skill-work.ts +76 -0
- package/runtime/sdk-runner/src/skills-files.ts +169 -0
- package/runtime/sdk-runner/src/skills.ts +150 -0
- package/runtime/sdk-runner/src/task-ledger.ts +229 -0
- package/runtime/sdk-runner/src/task-recovery.ts +203 -0
- package/runtime/sdk-runner/src/task-state.ts +71 -0
- package/runtime/sdk-runner/src/tool-proxy.ts +40 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +43 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -114
- package/dist/agents/claude.js +0 -229
- package/dist/agents/codex.js +0 -282
- package/dist/agents/types.js +0 -3
- package/dist/channels/discord.js +0 -142
- package/dist/channels/telegram.js +0 -209
- package/dist/lib/dotenv.js +0 -31
- package/dist/lib/scope-cache.js +0 -85
- package/dist/lib/streaming.js +0 -207
- package/dist/log.js +0 -12
- package/dist/orchestrator.js +0 -399
- package/dist/paths.js +0 -33
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { ToolGroup } from '@metro-labs/core/stations/types';
|
|
2
|
+
import { decide, policyFor, setPolicies, type PolicyTarget, type ToolPolicy } from '../policy/policy.js';
|
|
3
|
+
import { serverKeysOf } from './plugin-sync.js';
|
|
4
|
+
import { NEEDS_APPROVAL, revokeToolPrompts, settlePrompts, takeGrant } from '../approvals/pending.js';
|
|
5
|
+
import { isOrgRelayId } from './relay-id.js';
|
|
6
|
+
import type { BlockedReason } from './relay-policy.js';
|
|
7
|
+
|
|
8
|
+
export interface GateRow {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
config: { policy?: ToolPolicy; toolGroups?: Record<string, ToolGroup> };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ConnectorGate {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
server: string;
|
|
18
|
+
policy: ToolPolicy;
|
|
19
|
+
tools: Record<string, ToolGroup>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let gates: ConnectorGate[] = [];
|
|
23
|
+
|
|
24
|
+
const normalized = (name: string): string => name.replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
25
|
+
|
|
26
|
+
function approvalChanged(gate: ConnectorGate, tool: string, next: GateRow | undefined, keys: ReadonlyMap<string, string>): boolean {
|
|
27
|
+
if (next === undefined || gate.server !== keys.get(gate.id) || JSON.stringify([gate.name, gate.policy]) !== JSON.stringify([next.name, next.config.policy ?? {}])) return true;
|
|
28
|
+
const before = decide(gate.policy, { name: tool, group: toolGroupOf(gate, tool) });
|
|
29
|
+
const after = decide(next.config.policy, { name: tool, group: next.config.toolGroups?.[tool] === 'read' ? 'read' : 'write' });
|
|
30
|
+
return before !== after;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function registerConnectors(rows: readonly GateRow[], keys: ReadonlyMap<string, string> = serverKeysOf([...rows])): void {
|
|
34
|
+
const next = rows.map((row) => ({
|
|
35
|
+
id: row.id,
|
|
36
|
+
name: row.name,
|
|
37
|
+
server: keys.get(row.id) ?? '',
|
|
38
|
+
policy: row.config.policy ?? {},
|
|
39
|
+
tools: row.config.toolGroups ?? {},
|
|
40
|
+
}));
|
|
41
|
+
revokeToolPrompts((tool) => {
|
|
42
|
+
const before = connectorToolOf(tool);
|
|
43
|
+
const after = connectorToolOf(tool, next);
|
|
44
|
+
if (before?.gate.id !== after?.gate.id) return true;
|
|
45
|
+
return before !== undefined && approvalChanged(before.gate, before.tool, rows.find((row) => row.id === before.gate.id), keys);
|
|
46
|
+
});
|
|
47
|
+
gates = next;
|
|
48
|
+
const entries = rows.flatMap((row): [PolicyTarget, ToolPolicy][] =>
|
|
49
|
+
row.config.policy === undefined ? [] : [[{ kind: 'connector', id: row.id }, row.config.policy]],
|
|
50
|
+
);
|
|
51
|
+
setPolicies('connector', entries);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const connectorGates = (): readonly ConnectorGate[] => gates;
|
|
55
|
+
|
|
56
|
+
export const toolGroupOf = (gate: ConnectorGate, tool: string): ToolGroup => (gate.tools[tool] === 'read' ? 'read' : 'write');
|
|
57
|
+
|
|
58
|
+
const promptFor = (connectorId: string, tool: string) => (asked: string): boolean => {
|
|
59
|
+
const found = connectorToolOf(asked);
|
|
60
|
+
return found?.gate.id === connectorId && found.tool === tool;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export function blockedReason(connectorId: string, tool: string, args: Record<string, unknown> = {}): string | null {
|
|
64
|
+
const gate = gates.find((g) => g.id === connectorId);
|
|
65
|
+
if (gate === undefined) return 'This connector is not available.';
|
|
66
|
+
settlePrompts(promptFor(connectorId, tool), args);
|
|
67
|
+
const access = policyFor({ kind: 'connector', id: connectorId }, { name: tool, group: toolGroupOf(gate, tool) });
|
|
68
|
+
if (access === 'deny') return `Blocked by the owner's policy for ${gate.name} (${tool}).`;
|
|
69
|
+
if (access === 'allow' || takeGrant(promptFor(connectorId, tool), args)) return null;
|
|
70
|
+
return NEEDS_APPROVAL(gate.name, tool);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function requestConnectorBlocker(): () => BlockedReason {
|
|
74
|
+
const permits = new Map<string, number>();
|
|
75
|
+
return () => {
|
|
76
|
+
const seen = new Map<string, number>();
|
|
77
|
+
return (id, tool, args) => {
|
|
78
|
+
const gate = gates.find((entry) => entry.id === id);
|
|
79
|
+
if (gate === undefined) return 'This organization connector is not available.';
|
|
80
|
+
const group = toolGroupOf(gate, tool);
|
|
81
|
+
const access = policyFor({ kind: 'connector', id }, { name: tool, group });
|
|
82
|
+
if (access !== 'ask') return blockedReason(id, tool, args);
|
|
83
|
+
const key = JSON.stringify([id, gate.server, gate.policy, group, tool, args]);
|
|
84
|
+
const occurrence = seen.get(key) ?? 0;
|
|
85
|
+
seen.set(key, occurrence + 1);
|
|
86
|
+
if (occurrence < (permits.get(key) ?? 0)) return null;
|
|
87
|
+
const reason = blockedReason(id, tool, args);
|
|
88
|
+
if (reason === null) permits.set(key, (permits.get(key) ?? 0) + 1);
|
|
89
|
+
return reason;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function resolvedTool(gate: ConnectorGate, called: string): { gate: ConnectorGate; tool: string } {
|
|
95
|
+
const known = [...Object.keys(gate.tools), ...Object.keys(gate.policy.tools ?? {})];
|
|
96
|
+
return { gate, tool: known.find((name) => name === called) ?? known.find((name) => normalized(name) === called) ?? called };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function connectorToolOf(toolName: string, entries: readonly ConnectorGate[] = gates): { gate: ConnectorGate; tool: string } | undefined {
|
|
100
|
+
if (toolName.startsWith('mcp__metro__')) return undefined;
|
|
101
|
+
for (const gate of entries)
|
|
102
|
+
for (const prefix of [`mcp__plugin_metro_${gate.server}__`, `mcp__${gate.server}__`])
|
|
103
|
+
if (toolName.startsWith(prefix) && toolName.length > prefix.length) return resolvedTool(gate, toolName.slice(prefix.length));
|
|
104
|
+
return legacyToolOf(toolName, entries);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function legacyToolOf(toolName: string, entries: readonly ConnectorGate[]): { gate: ConnectorGate; tool: string } | undefined {
|
|
108
|
+
if (!toolName.startsWith('mcp__metro_box_')) return undefined;
|
|
109
|
+
const legacy = entries.filter((gate) => !isOrgRelayId(gate.id) && toolName.startsWith(`mcp__${normalized(`metro.box ${gate.name}`)}__`));
|
|
110
|
+
const gate = legacy.length === 1 ? legacy[0] : undefined;
|
|
111
|
+
if (gate === undefined) return undefined;
|
|
112
|
+
const called = toolName.slice(`mcp__${normalized(`metro.box ${gate.name}`)}__`.length);
|
|
113
|
+
return called === '' ? undefined : resolvedTool(gate, called);
|
|
114
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface ConnectorHealth {
|
|
2
|
+
ok: boolean;
|
|
3
|
+
reason: string | null;
|
|
4
|
+
at: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const seen = new Map<string, ConnectorHealth>();
|
|
8
|
+
|
|
9
|
+
export function noteHealth(id: string, ok: boolean, reason: string | null = null): void {
|
|
10
|
+
seen.set(id, { ok, reason: ok ? null : reason, at: new Date().toISOString() });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const healthOf = (id: string): ConnectorHealth | null => seen.get(id) ?? null;
|
|
14
|
+
|
|
15
|
+
export function forgetHealth(id?: string): void {
|
|
16
|
+
if (id === undefined) seen.clear();
|
|
17
|
+
else seen.delete(id);
|
|
18
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
2
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
3
|
+
import { signAgentRequest } from '@metro-labs/http/agent-signature';
|
|
4
|
+
import type { AgentKey } from './agent-key.js';
|
|
5
|
+
|
|
6
|
+
const TIMEOUT_MS = 15_000;
|
|
7
|
+
const ANSWER_MAX = 64 * 1024;
|
|
8
|
+
|
|
9
|
+
export type AgentFetch = (url: string, init: RequestInit) => Promise<Response>;
|
|
10
|
+
|
|
11
|
+
export interface MetroApi {
|
|
12
|
+
base: string;
|
|
13
|
+
fetch: AgentFetch;
|
|
14
|
+
now: () => number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const METRO_API: MetroApi = {
|
|
18
|
+
base: 'https://api.metro.box',
|
|
19
|
+
fetch: (url, init) => fetch(url, init),
|
|
20
|
+
now: () => Date.now(),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const tooLarge = (): ApiError => new ApiError('api.metro.box sent an answer too large for Metro.', 503);
|
|
24
|
+
const unreachable = (): ApiError => new ApiError('Metro could not reach api.metro.box. Try again.', 503);
|
|
25
|
+
|
|
26
|
+
async function answerOf(response: Response, max: number): Promise<unknown> {
|
|
27
|
+
if (Number(response.headers.get('content-length') ?? '0') > max) throw tooLarge();
|
|
28
|
+
const reader = response.body?.getReader();
|
|
29
|
+
if (reader === undefined) return null;
|
|
30
|
+
const parts: Uint8Array[] = [];
|
|
31
|
+
let size = 0;
|
|
32
|
+
for (let part = await reader.read(); !part.done; part = await reader.read()) {
|
|
33
|
+
size += part.value.length;
|
|
34
|
+
if (size > max) {
|
|
35
|
+
await reader.cancel();
|
|
36
|
+
throw tooLarge();
|
|
37
|
+
}
|
|
38
|
+
parts.push(part.value);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
return JSON.parse(Buffer.concat(parts).toString('utf8')) as unknown;
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class MetroApiRefusal extends ApiError {
|
|
48
|
+
constructor(message: string, readonly upstreamStatus: number, readonly code: string | null) {
|
|
49
|
+
super(message, upstreamStatus >= 500 ? 503 : 400);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function refusal(status: number, value: unknown): ApiError {
|
|
54
|
+
const said = isRecord(value) && typeof value.error === 'string' && value.error.length <= 300 ? value.error : `api.metro.box answered ${String(status)}`;
|
|
55
|
+
const code = isRecord(value) && typeof value.code === 'string' ? value.code : null;
|
|
56
|
+
return new MetroApiRefusal(said, status, code);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface AgentCallOptions {
|
|
60
|
+
answerMax?: number;
|
|
61
|
+
timeoutMs?: number;
|
|
62
|
+
signal?: AbortSignal;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function signedCall(api: MetroApi, key: AgentKey, path: string, body: Record<string, unknown> | undefined): { url: URL; method: string; text: string; authorization: string } {
|
|
66
|
+
const url = new URL(path, api.base);
|
|
67
|
+
const method = body === undefined ? 'GET' : 'POST';
|
|
68
|
+
const text = body === undefined ? '' : JSON.stringify(body);
|
|
69
|
+
const authorization = signAgentRequest({ method, host: url.host, path: `${url.pathname}${url.search}`, body: Buffer.from(text, 'utf8') }, key.signing, key.keyId, api.now());
|
|
70
|
+
return { url, method, text, authorization };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function agentCall(api: MetroApi, key: AgentKey, path: string, body?: Record<string, unknown>, options: AgentCallOptions = {}): Promise<Record<string, unknown>> {
|
|
74
|
+
const { url, method, text, authorization } = signedCall(api, key, path, body);
|
|
75
|
+
let response: Response;
|
|
76
|
+
try {
|
|
77
|
+
response = await api.fetch(url.toString(), {
|
|
78
|
+
method,
|
|
79
|
+
headers: { authorization, ...(body === undefined ? {} : { 'content-type': 'application/json' }) },
|
|
80
|
+
...(body === undefined ? {} : { body: text }),
|
|
81
|
+
redirect: 'error',
|
|
82
|
+
signal: AbortSignal.any([AbortSignal.timeout(options.timeoutMs ?? TIMEOUT_MS), ...(options.signal === undefined ? [] : [options.signal])]),
|
|
83
|
+
});
|
|
84
|
+
} catch {
|
|
85
|
+
throw unreachable();
|
|
86
|
+
}
|
|
87
|
+
const value = await answerOf(response, options.answerMax ?? ANSWER_MAX).catch((err: unknown) => {
|
|
88
|
+
throw err instanceof ApiError ? err : unreachable();
|
|
89
|
+
});
|
|
90
|
+
if (!response.ok) throw refusal(response.status, value);
|
|
91
|
+
if (!isRecord(value)) throw new ApiError('api.metro.box sent an answer Metro cannot read.', 503);
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
3
|
+
import { parseId } from '@metro-labs/core/ids';
|
|
4
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
+
import { readJsonBody, sessionRoute } from '@metro-labs/http/api-http';
|
|
6
|
+
import { publicOAuthResource } from '@metro-labs/http/connector-public-url';
|
|
7
|
+
import type { ConnectorMigrations, MigrationAction } from './migration-control.js';
|
|
8
|
+
|
|
9
|
+
const PATH = '/api/connectors/migrations';
|
|
10
|
+
const actions: readonly MigrationAction[] = ['move', 'prepare', 'reconcile', 'cutover', 'rollback', 'resolve'];
|
|
11
|
+
export type MigrationApiDeps = Pick<ConnectorMigrations, 'status' | 'run'>;
|
|
12
|
+
|
|
13
|
+
interface MigrationInput {
|
|
14
|
+
sourceId: string;
|
|
15
|
+
action: MigrationAction;
|
|
16
|
+
scope: string;
|
|
17
|
+
operationId: string | null;
|
|
18
|
+
uncertainty: string | null;
|
|
19
|
+
oauthResource?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function operationOf(value: unknown): string | null {
|
|
23
|
+
if (value === null) return null;
|
|
24
|
+
const id = parseId(value);
|
|
25
|
+
if (id === null) throw new ApiError('A migration needs its current operation ID, or null for an unprepared source.', 400);
|
|
26
|
+
return id;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function receiptOf(body: Record<string, unknown>, action: MigrationAction): string | null {
|
|
30
|
+
if (action !== 'resolve') {
|
|
31
|
+
if (Object.hasOwn(body, 'uncertainty')) throw new ApiError('Only request settlement accepts an uncertainty receipt.', 400);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
if (typeof body.uncertainty !== 'string' || !/^[a-f0-9]{64}$/.test(body.uncertainty)) throw new ApiError('Confirming settled vendor requests needs the current uncertainty receipt.', 400);
|
|
35
|
+
return body.uncertainty;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function inputOf(body: unknown): MigrationInput {
|
|
39
|
+
if (!isRecord(body) || Object.keys(body).some((key) => !['scope', 'sourceId', 'action', 'operationId', 'uncertainty', 'oauthResource'].includes(key)) || typeof body.scope !== 'string' || !/^[a-f0-9]{64}$/.test(body.scope)) {
|
|
40
|
+
throw new ApiError('A migration needs its connector, action and current enrollment scope.', 400);
|
|
41
|
+
}
|
|
42
|
+
const sourceId = parseId(body.sourceId);
|
|
43
|
+
const action = actions.find((value) => value === body.action);
|
|
44
|
+
if (sourceId === null || action === undefined) throw new ApiError('A migration needs a valid connector and action.', 400);
|
|
45
|
+
return {
|
|
46
|
+
sourceId, action, scope: body.scope, operationId: operationOf(body.operationId), uncertainty: receiptOf(body, action),
|
|
47
|
+
...(body.oauthResource === undefined ? {} : { oauthResource: publicOAuthResource(body.oauthResource) }),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function handleMigrationRequest(req: IncomingMessage, res: ServerResponse, deps: MigrationApiDeps): boolean {
|
|
52
|
+
return sessionRoute(req, res, { methods: { [PATH]: ['GET', 'POST'] }, admin: true, label: 'connector-migration-api' }, async (session) => {
|
|
53
|
+
const body = req.method === 'POST' ? await readJsonBody(req) : null;
|
|
54
|
+
const current = deps.status();
|
|
55
|
+
if (current.owner === null || current.owner !== session.subject) throw new ApiError('This agent belongs to another organization.', 403);
|
|
56
|
+
if (req.method !== 'POST') return current;
|
|
57
|
+
const { sourceId, action, scope, operationId, uncertainty, oauthResource } = inputOf(body);
|
|
58
|
+
return deps.run(sourceId, action, scope, operationId, uncertainty, oauthResource);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { newId } from '@metro-labs/core/ids';
|
|
2
|
+
import { log } from '@metro-labs/core/log';
|
|
3
|
+
import type { ConnectorSyncStatus } from '@metro-labs/core/connector-sync';
|
|
4
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
+
import { publicOAuthResource } from '@metro-labs/http/connector-public-url';
|
|
6
|
+
import type { DeliverySnapshot } from '@metro-labs/http/connector-delivery';
|
|
7
|
+
import { migrationResponseOf, sealConnectorMigration, type MigrationResponse, type MigrationView as RemoteMigration } from '@metro-labs/http/connector-migration';
|
|
8
|
+
import { agentsDir } from '../agents/files.js';
|
|
9
|
+
import { readAgentKey, type AgentKey } from './agent-key.js';
|
|
10
|
+
import { agentCall, METRO_API, MetroApiRefusal, type MetroApi } from './metro-api.js';
|
|
11
|
+
import { migrationSource } from './migration-preview.js';
|
|
12
|
+
import { MigrationBlocked, migrationPayload } from './migration-source.js';
|
|
13
|
+
import { localMigrationIdle } from './migration-guard.js';
|
|
14
|
+
import { migrationUncertaintyReceipt, migrationRefreshUncertain, migrationOperationsIdle, resolveMigrationUncertainty } from './migration-operations.js';
|
|
15
|
+
import { flushMigrationSource, localRollbackMigrationSource } from './store.js';
|
|
16
|
+
import { freshOrgConnectors, orgConnectorMigrationIdle, orgConnectorSyncStatus, refreshOrgConnectorRuntime } from './org-sync-watch.js';
|
|
17
|
+
import { migrationRecord, migrationRecords, saveMigration, type MigrationRecord } from './migration-state.js';
|
|
18
|
+
|
|
19
|
+
export type MigrationAction = 'move' | 'prepare' | 'reconcile' | 'cutover' | 'rollback' | 'resolve';
|
|
20
|
+
interface Context { key: AgentKey; owner: string; agentId: string; scope: string }
|
|
21
|
+
export interface MigrationView extends Pick<MigrationRecord, 'id' | 'sourceId' | 'name' | 'destinationId' | 'phase' | 'auth' | 'reason'> {
|
|
22
|
+
uncertainty: string | null;
|
|
23
|
+
move: boolean;
|
|
24
|
+
grantImported: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface MigrationStatus {
|
|
27
|
+
scope: string;
|
|
28
|
+
owner: string | null;
|
|
29
|
+
agentId: string | null;
|
|
30
|
+
enabled: boolean;
|
|
31
|
+
enrolled: boolean;
|
|
32
|
+
migrations: MigrationView[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface MigrationDeps {
|
|
36
|
+
dir?: string;
|
|
37
|
+
api?: MetroApi;
|
|
38
|
+
status?: () => ConnectorSyncStatus;
|
|
39
|
+
key?: () => AgentKey | null;
|
|
40
|
+
fresh?: () => Promise<DeliverySnapshot>;
|
|
41
|
+
changed?: () => void;
|
|
42
|
+
localIdle?: (id: string) => boolean;
|
|
43
|
+
orgIdle?: (id: string) => boolean;
|
|
44
|
+
restore?: (id: string, oauth: boolean) => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const conflict = (message: string): ApiError => new ApiError(message, 409);
|
|
48
|
+
const uncertain = (): ApiError => new ApiError('The migration request was not confirmed. Refresh and reconcile this connector before trying another action; an interrupted request may already have reached Metro.', 503);
|
|
49
|
+
const viewOf = ({ id, sourceId, name, destinationId, phase, auth, reason, move, grantImported }: MigrationRecord, uncertainty: string | null): MigrationView => ({ id, sourceId, name, destinationId, phase, auth, reason, uncertainty, move: move === true, grantImported: grantImported === true });
|
|
50
|
+
const reasons: Record<RemoteMigration['readiness'], string | null> = { pending: 'not_verified', ready: null, signin: 'sign_in', changed: 'destination_changed', unavailable: 'unavailable', refresh_uncertain: 'refresh_uncertain' };
|
|
51
|
+
const samePolicy = (a: MigrationRecord['policy'], b: MigrationRecord['policy']): boolean =>
|
|
52
|
+
(a.read ?? 'allow') === (b.read ?? 'allow') && (a.write ?? 'allow') === (b.write ?? 'allow') && JSON.stringify(Object.entries(a.tools ?? {}).sort()) === JSON.stringify(Object.entries(b.tools ?? {}).sort());
|
|
53
|
+
|
|
54
|
+
const grantTransfer = (record: MigrationRecord): boolean => record.move === true && record.auth === 'oauth';
|
|
55
|
+
const upgrading = (record: MigrationRecord, remote: RemoteMigration): boolean => grantTransfer(record) && remote.grantImported !== true;
|
|
56
|
+
const canUpgrade = (record: MigrationRecord): boolean => record.move !== true && ['preparing', 'prepared'].includes(record.phase);
|
|
57
|
+
|
|
58
|
+
function checkResponse(record: MigrationRecord, remote: RemoteMigration): void {
|
|
59
|
+
if (remote.operationId !== record.id || remote.sourceId !== record.sourceId || (record.destinationId !== null && remote.destinationId !== record.destinationId)) throw uncertain();
|
|
60
|
+
if (remote.sourceStamp === null || remote.sourceStamp === record.sourceStamp || upgrading(record, remote)) return;
|
|
61
|
+
throw conflict('The prepared destination no longer matches this local connector. Reconcile its current operation before continuing.');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function callFailure(error: unknown, action: string): ApiError {
|
|
65
|
+
if (error instanceof MetroApiRefusal && error.code === 'migration_conflict') return new MigrationBlocked('destination_changed', 'The mapped destination already has credentials or changed settings. Inspect it and reconcile; Metro will not overwrite credentials or create a duplicate.');
|
|
66
|
+
if (action === 'prepare' && error instanceof MetroApiRefusal && error.upstreamStatus === 400 && error.code === 'oauth_resource_required') {
|
|
67
|
+
return new ApiError("Enter the provider's public OAuth resource. Roll back this preparation, then prepare again with that public value; never enter credentials.", 400);
|
|
68
|
+
}
|
|
69
|
+
return uncertain();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function received(record: MigrationRecord, snapshot: DeliverySnapshot): boolean {
|
|
73
|
+
const delivered = snapshot.connectors.find((row) => row.id === record.destinationId);
|
|
74
|
+
if (delivered?.state !== 'ready' || delivered.name !== record.name) return false;
|
|
75
|
+
const layers = delivered.policyLayers;
|
|
76
|
+
return layers === undefined ? samePolicy(delivered.policy, record.policy) : samePolicy(layers.organization, record.policy) && samePolicy(layers.agent, {});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export class ConnectorMigrations {
|
|
80
|
+
private readonly dir: string;
|
|
81
|
+
private readonly api: MetroApi;
|
|
82
|
+
private running = false;
|
|
83
|
+
|
|
84
|
+
constructor(private readonly deps: MigrationDeps = {}) {
|
|
85
|
+
this.dir = deps.dir ?? agentsDir();
|
|
86
|
+
this.api = deps.api ?? METRO_API;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private syncStatus(): ConnectorSyncStatus { return (this.deps.status ?? orgConnectorSyncStatus)(); }
|
|
90
|
+
private key(): AgentKey | null { return this.deps.key === undefined ? readAgentKey(this.dir) : this.deps.key(); }
|
|
91
|
+
private changed(): void { (this.deps.changed ?? refreshOrgConnectorRuntime)(); }
|
|
92
|
+
private localIdle(id: string): boolean { return this.deps.localIdle?.(id) ?? localMigrationIdle(id, this.dir); }
|
|
93
|
+
private orgIdle(id: string): boolean { return (this.deps.orgIdle ?? orgConnectorMigrationIdle)(id); }
|
|
94
|
+
|
|
95
|
+
status(): MigrationStatus {
|
|
96
|
+
const { scope, owner, agentId, enabled, enrolled } = this.syncStatus();
|
|
97
|
+
const keyId = this.key()?.keyId;
|
|
98
|
+
const migrations = migrationRecords(this.dir).filter((row) => row.owner === owner).map((row) => {
|
|
99
|
+
const uncertainty = migrationUncertaintyReceipt(row.sourceId, this.dir);
|
|
100
|
+
const reason = uncertainty === null ? row.reason : migrationRefreshUncertain(row.sourceId, this.dir) ? 'local_refresh_uncertain' : 'requests_uncertain';
|
|
101
|
+
return viewOf({ ...row, reason: row.agentId !== agentId || row.keyId !== keyId ? 'identity_changed' : reason }, uncertainty);
|
|
102
|
+
});
|
|
103
|
+
return { scope, owner, agentId, enabled, enrolled, migrations };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private context(scope: string, requireSync: boolean): Context {
|
|
107
|
+
const status = this.syncStatus();
|
|
108
|
+
const key = this.key();
|
|
109
|
+
if (scope !== status.scope) throw conflict('The organization, agent or enrollment changed. Refresh before continuing the migration.');
|
|
110
|
+
if (!status.enrolled || key?.enrollment?.organization !== status.owner || key.enrollment.server !== status.agentId) throw conflict('Enroll this agent with the current organization before migrating connectors.');
|
|
111
|
+
if (requireSync && !status.enabled) throw conflict('Enable organization connector sync on the Server page before preparing or switching a connector.');
|
|
112
|
+
return { key, owner: key.enrollment.organization, agentId: key.enrollment.server, scope };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private check(context: Context, record?: MigrationRecord): void {
|
|
116
|
+
const current = this.context(context.scope, false);
|
|
117
|
+
if (current.key.keyId !== context.key.keyId || current.owner !== context.owner || current.agentId !== context.agentId) throw conflict('The migration identity changed. Refresh before continuing.');
|
|
118
|
+
if (record !== undefined && (record.owner !== current.owner || record.agentId !== current.agentId || record.keyId !== current.key.keyId)) throw conflict('This migration belongs to a different enrollment. Restore its original enrollment before reconciling it.');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private save(context: Context, record: MigrationRecord): MigrationRecord {
|
|
122
|
+
this.check(context, record);
|
|
123
|
+
saveMigration(record, this.dir);
|
|
124
|
+
this.changed();
|
|
125
|
+
return record;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private source(record: MigrationRecord): ReturnType<typeof migrationPayload> {
|
|
129
|
+
const payload = migrationPayload(migrationSource(record.sourceId, this.dir, record.move === true && record.captured === true), record.oauthResource, record.move === true && record.captured === true);
|
|
130
|
+
if (payload.sourceStamp !== record.sourceStamp) throw conflict('The retained local connector changed. Restore its original settings before continuing; Metro will not overwrite it.');
|
|
131
|
+
return payload;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private async call(context: Context, record: MigrationRecord, action: 'begin' | 'prepare' | 'handover' | 'status' | 'activate' | 'rollback', sealed?: ReturnType<typeof sealConnectorMigration>): Promise<MigrationResponse> {
|
|
135
|
+
this.check(context, record);
|
|
136
|
+
let response: MigrationResponse;
|
|
137
|
+
try {
|
|
138
|
+
response = migrationResponseOf(await agentCall(this.api, context.key, '/api/agents/connector-migrations', {
|
|
139
|
+
action, operationId: record.id, sourceId: record.sourceId, ...(sealed === undefined ? {} : { sealed }),
|
|
140
|
+
}, { timeoutMs: 60_000 }));
|
|
141
|
+
} catch (error) {
|
|
142
|
+
throw callFailure(error, action);
|
|
143
|
+
}
|
|
144
|
+
this.check(context, record);
|
|
145
|
+
checkResponse(record, response.migration);
|
|
146
|
+
return response;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private accept(context: Context, record: MigrationRecord, remote: RemoteMigration): MigrationRecord {
|
|
150
|
+
if (remote.phase !== 'preparing' && remote.phase !== 'rolled_back' && (remote.name !== record.name || remote.auth !== record.auth || remote.policy === null || !samePolicy(record.policy, remote.policy))) {
|
|
151
|
+
return this.save(context, { ...record, destinationId: remote.destinationId, reason: 'destination_changed' });
|
|
152
|
+
}
|
|
153
|
+
return this.save(context, { ...record, destinationId: remote.destinationId, grantImported: remote.grantImported === true, reason: reasons[remote.readiness] });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private create(context: Context, id: string, oauthResource?: string, move = false): MigrationRecord {
|
|
157
|
+
const payload = migrationPayload(migrationSource(id, this.dir), oauthResource);
|
|
158
|
+
return this.save(context, {
|
|
159
|
+
id: newId(), owner: context.owner, agentId: context.agentId, keyId: context.key.keyId,
|
|
160
|
+
sourceId: id, name: payload.name, destinationId: null, phase: move ? 'freezing' : 'preparing', move, auth: payload.auth.kind,
|
|
161
|
+
sourceStamp: payload.sourceStamp, policy: payload.policy, reason: null,
|
|
162
|
+
...(payload.oauthResource === undefined ? {} : { oauthResource: payload.oauthResource }),
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private async prepare(context: Context, record: MigrationRecord): Promise<void> {
|
|
167
|
+
const payload = this.source(record);
|
|
168
|
+
const begun = await this.call(context, record, 'begin');
|
|
169
|
+
record = this.accept(context, record, begun.migration);
|
|
170
|
+
if (begun.migration.phase === 'prepared' && !upgrading(record, begun.migration)) {
|
|
171
|
+
this.save(context, { ...record, phase: 'prepared' });
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (!['preparing', 'prepared'].includes(begun.migration.phase) || begun.receiving === undefined) throw uncertain();
|
|
175
|
+
const receiving = begun.receiving;
|
|
176
|
+
const expected = { owner: context.owner, agent: context.agentId, keyId: context.key.keyId, operationId: record.id, sourceId: record.sourceId, destinationId: record.destinationId };
|
|
177
|
+
for (const [key, value] of Object.entries(expected)) if (receiving.context[key as keyof typeof receiving.context] !== value) throw uncertain();
|
|
178
|
+
const sealed = sealConnectorMigration(receiving.publicKey, receiving.context, payload);
|
|
179
|
+
const { migration } = await this.call(context, record, grantTransfer(record) ? 'handover' : 'prepare', sealed);
|
|
180
|
+
record = this.accept(context, record, migration);
|
|
181
|
+
if (migration.phase !== 'prepared') throw uncertain();
|
|
182
|
+
this.save(context, { ...record, phase: 'prepared' });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private async freeze(context: Context, record: MigrationRecord): Promise<MigrationRecord | null> {
|
|
186
|
+
const until = Date.now() + 10_000;
|
|
187
|
+
while (!this.localIdle(record.sourceId) && migrationUncertaintyReceipt(record.sourceId, this.dir) === null && Date.now() < until) {
|
|
188
|
+
await new Promise((resolve) => { setTimeout(resolve, 100); });
|
|
189
|
+
this.check(context, record);
|
|
190
|
+
}
|
|
191
|
+
if (this.waiting(context, record)) return null;
|
|
192
|
+
flushMigrationSource(this.dir);
|
|
193
|
+
const payload = migrationPayload(migrationSource(record.sourceId, this.dir, true), record.oauthResource, true);
|
|
194
|
+
return this.save(context, { ...record, sourceStamp: payload.sourceStamp, captured: true, phase: 'preparing', reason: null });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private async move(context: Context, record: MigrationRecord): Promise<void> {
|
|
198
|
+
if (record.phase === 'recovering') return this.finishRecovery(context, record);
|
|
199
|
+
if (['active', 'rolled_back'].includes(record.phase)) return this.reconcile(context, record);
|
|
200
|
+
if (record.phase === 'rollback') return this.rollback(context, record);
|
|
201
|
+
if (record.phase === 'freezing') {
|
|
202
|
+
const captured = await this.freeze(context, record);
|
|
203
|
+
if (captured === null) return;
|
|
204
|
+
record = captured;
|
|
205
|
+
}
|
|
206
|
+
if (record.phase === 'preparing') {
|
|
207
|
+
await this.prepare(context, record);
|
|
208
|
+
record = migrationRecord(record.sourceId, this.dir) ?? record;
|
|
209
|
+
}
|
|
210
|
+
if (record.phase === 'prepared' || record.phase === 'cutover') await this.cutover(context, record);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private waiting(context: Context, record: MigrationRecord, includeOrganization = false): boolean {
|
|
214
|
+
const uncertain = migrationUncertaintyReceipt(record.sourceId, this.dir) !== null;
|
|
215
|
+
const localIdle = this.localIdle(record.sourceId);
|
|
216
|
+
if (!uncertain && localIdle && (!includeOrganization || this.orgIdle(record.sourceId))) return false;
|
|
217
|
+
this.save(context, { ...record, reason: uncertain ? migrationRefreshUncertain(record.sourceId, this.dir) ? 'local_refresh_uncertain' : 'requests_uncertain' : 'waiting_for_local' });
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private async cutover(context: Context, record: MigrationRecord): Promise<void> {
|
|
222
|
+
this.context(context.scope, true);
|
|
223
|
+
this.source(record);
|
|
224
|
+
if (record.phase === 'prepared') {
|
|
225
|
+
const { migration } = await this.call(context, record, 'status');
|
|
226
|
+
record = this.accept(context, record, migration);
|
|
227
|
+
if (migration.phase !== 'prepared' || record.reason !== null) return;
|
|
228
|
+
record = this.save(context, { ...record, phase: 'cutover' });
|
|
229
|
+
}
|
|
230
|
+
if (this.waiting(context, record)) return;
|
|
231
|
+
const { migration } = await this.call(context, record, 'activate');
|
|
232
|
+
record = this.accept(context, record, migration);
|
|
233
|
+
if (migration.phase !== 'active' || record.reason !== null) return;
|
|
234
|
+
const snapshot = await (this.deps.fresh ?? freshOrgConnectors)().catch(() => {
|
|
235
|
+
throw new ApiError('Fresh organization delivery could not be verified. The source stays stopped; restore sync and reconcile this migration.', 503);
|
|
236
|
+
});
|
|
237
|
+
this.check(context, record);
|
|
238
|
+
this.context(context.scope, true);
|
|
239
|
+
if (!received(record, snapshot)) {
|
|
240
|
+
this.save(context, { ...record, reason: 'sync_unavailable' });
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
this.save(context, { ...record, phase: 'active', reason: null });
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private async rollback(context: Context, record: MigrationRecord): Promise<void> {
|
|
247
|
+
if (record.phase === 'recovering') return this.finishRecovery(context, record);
|
|
248
|
+
if (record.grantImported === true) throw new MigrationBlocked('finish_forward', 'Central owns this OAuth grant. Finish forward or sign in centrally; the old local tokens cannot be restored.');
|
|
249
|
+
const begun = await this.call(context, record, 'begin');
|
|
250
|
+
if (begun.migration.grantImported === true) {
|
|
251
|
+
record = this.accept(context, record, begun.migration);
|
|
252
|
+
this.save(context, { ...record, phase: begun.migration.phase === 'active' ? 'cutover' : 'prepared', reason: 'finish_forward' });
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (record.phase !== 'freezing') this.source(record);
|
|
256
|
+
record = this.save(context, { ...record, phase: 'rollback' });
|
|
257
|
+
if (this.waiting(context, record, true)) return;
|
|
258
|
+
const { migration } = await this.call(context, record, 'rollback');
|
|
259
|
+
if (migration.phase !== 'rolled_back') {
|
|
260
|
+
this.accept(context, record, migration);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
this.check(context, record);
|
|
264
|
+
this.restore(context, record);
|
|
265
|
+
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private restore(context: Context, record: MigrationRecord): void {
|
|
269
|
+
const signOut = record.auth === 'oauth' && record.move !== true;
|
|
270
|
+
if (this.deps.restore !== undefined) this.deps.restore(record.sourceId, signOut);
|
|
271
|
+
else localRollbackMigrationSource(record.sourceId, signOut, this.dir);
|
|
272
|
+
this.save(context, { ...record, phase: 'rolled_back', reason: signOut ? 'sign_in' : null });
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private async reconcile(context: Context, record: MigrationRecord): Promise<void> {
|
|
276
|
+
if (record.phase === 'recovering') return this.finishRecovery(context, record);
|
|
277
|
+
if (record.phase === 'rollback') return this.rollback(context, record);
|
|
278
|
+
if (record.move === true && record.phase !== 'active' && record.phase !== 'rolled_back') return this.move(context, record);
|
|
279
|
+
if (record.phase === 'preparing') return this.prepare(context, record);
|
|
280
|
+
if (record.phase === 'cutover') return this.cutover(context, record);
|
|
281
|
+
if (record.phase === 'rolled_back') return;
|
|
282
|
+
const { migration } = await this.call(context, record, 'status');
|
|
283
|
+
this.accept(context, record, migration);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private obtain(context: Context, sourceId: string, action: MigrationAction, oauthResource?: string): MigrationRecord | null {
|
|
287
|
+
const record = migrationRecord(sourceId, this.dir);
|
|
288
|
+
if (record !== null) this.check(context, record);
|
|
289
|
+
if (['prepare', 'move'].includes(action) && (record === null || record.phase === 'rolled_back')) return this.create(context, sourceId, oauthResource, action === 'move');
|
|
290
|
+
if (action === 'move' && record !== null && canUpgrade(record)) return this.save(context, { ...record, move: true, phase: 'freezing' });
|
|
291
|
+
if (record === null && action !== 'reconcile') throw conflict('Prepare this connector before moving it.');
|
|
292
|
+
return record;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private perform(context: Context, record: MigrationRecord, action: MigrationAction): Promise<void> {
|
|
296
|
+
if (action === 'move') return this.move(context, record);
|
|
297
|
+
if (action === 'prepare' && record.phase === 'preparing') return this.prepare(context, record);
|
|
298
|
+
if (action === 'cutover' && (record.phase === 'prepared' || record.phase === 'cutover')) return this.cutover(context, record);
|
|
299
|
+
if (action === 'rollback' && record.phase !== 'rolled_back') return this.rollback(context, record);
|
|
300
|
+
return this.reconcile(context, record);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private async resolve(context: Context, record: MigrationRecord, receipt: string | null): Promise<void> {
|
|
304
|
+
this.check(context, record);
|
|
305
|
+
if (receipt === null || receipt !== migrationUncertaintyReceipt(record.sourceId, this.dir)) throw conflict('The outstanding request receipt changed. Refresh before confirming that vendor requests have settled.');
|
|
306
|
+
localMigrationIdle(record.sourceId, this.dir, true);
|
|
307
|
+
if (migrationRefreshUncertain(record.sourceId, this.dir)) return this.recoverRefresh(context, record, receipt);
|
|
308
|
+
if (!resolveMigrationUncertainty(record.sourceId, receipt, this.dir)) throw conflict('Requests are still running or their receipt changed. Wait for vendor completion, then refresh before confirming settlement.');
|
|
309
|
+
this.save(context, { ...record, reason: record.phase === 'prepared' ? 'not_verified' : null });
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private async recoverRefresh(context: Context, record: MigrationRecord, receipt: string): Promise<void> {
|
|
313
|
+
if (record.move !== true || !['freezing', 'rollback'].includes(record.phase) || !migrationOperationsIdle(record.sourceId, this.dir, false)) throw conflict('The uncertain local refresh must stop before aborting the handover.');
|
|
314
|
+
const { migration } = await this.call(context, record, 'rollback');
|
|
315
|
+
if (migration.phase !== 'rolled_back') throw uncertain();
|
|
316
|
+
record = this.save(context, { ...record, phase: 'recovering', recoveryReceipt: receipt });
|
|
317
|
+
await this.finishRecovery(context, record);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private async finishRecovery(context: Context, record: MigrationRecord): Promise<void> {
|
|
321
|
+
const { migration } = await this.call(context, record, 'rollback');
|
|
322
|
+
if (migration.phase !== 'rolled_back' || record.recoveryReceipt === undefined) throw uncertain();
|
|
323
|
+
localRollbackMigrationSource(record.sourceId, true, this.dir, true);
|
|
324
|
+
const receipt = migrationUncertaintyReceipt(record.sourceId, this.dir);
|
|
325
|
+
if (receipt !== null && !resolveMigrationUncertainty(record.sourceId, record.recoveryReceipt, this.dir)) throw uncertain();
|
|
326
|
+
this.save(context, { ...record, phase: 'rolled_back', recoveryReceipt: undefined, reason: 'sign_in' });
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private validateResource(sourceId: string, action: MigrationAction, oauthResource: string | undefined): void {
|
|
330
|
+
if (oauthResource === undefined) return;
|
|
331
|
+
const resource = publicOAuthResource(oauthResource);
|
|
332
|
+
const record = migrationRecord(sourceId, this.dir);
|
|
333
|
+
if (action === 'prepare' && (record === null || record.phase === 'rolled_back')) return;
|
|
334
|
+
if (record?.oauthResource !== resource) throw conflict('The public OAuth resource is fixed for this preparation. Roll back, then prepare again to change it.');
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private validateIntent(sourceId: string, action: MigrationAction, operationId: string | null, receipt: string | null): void {
|
|
338
|
+
if ((migrationRecord(sourceId, this.dir)?.id ?? null) !== operationId) throw conflict('This connector migration changed. Refresh before acting on its current operation.');
|
|
339
|
+
if (action === 'resolve' && (receipt === null || receipt !== migrationUncertaintyReceipt(sourceId, this.dir))) throw conflict('The outstanding request receipt changed. Refresh before confirming that vendor requests have settled.');
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async run(sourceId: string, action: MigrationAction, scope: string, operationId: string | null, uncertainty: string | null = null, oauthResource?: string): Promise<MigrationStatus> {
|
|
343
|
+
if (this.running) throw conflict('Another migration request is still running on this agent. Refresh after it finishes.');
|
|
344
|
+
const context = this.context(scope, ['move', 'prepare', 'cutover'].includes(action));
|
|
345
|
+
this.validateIntent(sourceId, action, operationId, uncertainty);
|
|
346
|
+
this.validateResource(sourceId, action, oauthResource);
|
|
347
|
+
this.running = true;
|
|
348
|
+
try {
|
|
349
|
+
const record = this.obtain(context, sourceId, action, oauthResource);
|
|
350
|
+
if (record !== null) {
|
|
351
|
+
if (action === 'resolve') await this.resolve(context, record, uncertainty);
|
|
352
|
+
else await this.perform(context, record, action);
|
|
353
|
+
}
|
|
354
|
+
this.check(context);
|
|
355
|
+
return this.status();
|
|
356
|
+
} catch (error) {
|
|
357
|
+
const record = migrationRecord(sourceId, this.dir);
|
|
358
|
+
if (record !== null) {
|
|
359
|
+
try { this.save(context, { ...record, reason: error instanceof MigrationBlocked ? error.reason : 'unavailable' }); } catch {
|
|
360
|
+
log.warn('connector migration: the interrupted operation needs reconciliation in its original enrollment');
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
throw error;
|
|
364
|
+
} finally { this.running = false; }
|
|
365
|
+
}
|
|
366
|
+
}
|