@stage-labs/metro 0.1.0-beta.13 → 0.1.0-beta.131
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/seed.ts +33 -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 +209 -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,300 @@
|
|
|
1
|
+
import { webhookPort } from '../net/tunnel.js';
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
|
+
import { parseId } from '@metro-labs/core/ids';
|
|
4
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
5
|
+
import {
|
|
6
|
+
apiFailure,
|
|
7
|
+
apiSession,
|
|
8
|
+
projectParam,
|
|
9
|
+
cors,
|
|
10
|
+
readJsonBody,
|
|
11
|
+
sendJson,
|
|
12
|
+
type ApiSession,
|
|
13
|
+
} from '@metro-labs/http/api-http';
|
|
14
|
+
import { ATTACHABLE, handleAccountRoute, type AccountApiDeps } from './accounts-api.js';
|
|
15
|
+
import {
|
|
16
|
+
accountRoute,
|
|
17
|
+
accountRouteAllows,
|
|
18
|
+
type AccountRoute,
|
|
19
|
+
} from './account-routes.js';
|
|
20
|
+
import {
|
|
21
|
+
type AgentSummary,
|
|
22
|
+
type CreatedAgent,
|
|
23
|
+
type DeletedAgent,
|
|
24
|
+
type ResetAgentKey,
|
|
25
|
+
} from './admin.js';
|
|
26
|
+
|
|
27
|
+
const PREFIX = '/api/agents';
|
|
28
|
+
const SERVER_NAME = 'metro';
|
|
29
|
+
|
|
30
|
+
export interface AgentApiDeps extends AccountApiDeps {
|
|
31
|
+
listAgents: (subject: string, project: string) => Promise<AgentSummary[]>;
|
|
32
|
+
createAgent: (
|
|
33
|
+
subject: string,
|
|
34
|
+
project: string,
|
|
35
|
+
name: string,
|
|
36
|
+
) => Promise<CreatedAgent>;
|
|
37
|
+
deleteAgent: (
|
|
38
|
+
subject: string,
|
|
39
|
+
id: string,
|
|
40
|
+
) => Promise<DeletedAgent>;
|
|
41
|
+
resetKey: (
|
|
42
|
+
subject: string,
|
|
43
|
+
id: string,
|
|
44
|
+
) => Promise<ResetAgentKey>;
|
|
45
|
+
gatherAccounts: (allowed: Set<string>) => Promise<{
|
|
46
|
+
accounts: Record<string, unknown[]>;
|
|
47
|
+
unavailable: string[];
|
|
48
|
+
}>;
|
|
49
|
+
capabilities: () => Record<string, string[]>;
|
|
50
|
+
attachable?: string[];
|
|
51
|
+
liveness: () => Map<string, { connected: boolean; lastSeenAt: number }>;
|
|
52
|
+
connectorIds: (agentIds: string[]) => Promise<Map<string, string[]>>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type Routable =
|
|
56
|
+
| { kind: 'collection' }
|
|
57
|
+
| { kind: 'agent'; id: string }
|
|
58
|
+
| { kind: 'key'; id: string }
|
|
59
|
+
| { kind: 'accounts'; id: string; route: AccountRoute };
|
|
60
|
+
|
|
61
|
+
type Target = Routable | { kind: 'unknown' } | null;
|
|
62
|
+
|
|
63
|
+
function subTarget(id: string, rest: string[]): Target {
|
|
64
|
+
if (rest.length === 0) return { kind: 'agent', id };
|
|
65
|
+
if (rest.length === 1 && rest[0] === 'key') return { kind: 'key', id };
|
|
66
|
+
if (rest[0] !== 'accounts') return { kind: 'unknown' };
|
|
67
|
+
const route = accountRoute(rest.slice(1));
|
|
68
|
+
return route === null ? { kind: 'unknown' } : { kind: 'accounts', id, route };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function target(path: string): Target {
|
|
72
|
+
if (path === PREFIX || path === `${PREFIX}/`) return { kind: 'collection' };
|
|
73
|
+
if (!path.startsWith(`${PREFIX}/`)) return null;
|
|
74
|
+
const segments = path.slice(PREFIX.length + 1).split('/').filter(Boolean);
|
|
75
|
+
const head = segments[0];
|
|
76
|
+
if (head === undefined) return { kind: 'collection' };
|
|
77
|
+
const id = parseId(head);
|
|
78
|
+
return id === null ? { kind: 'unknown' } : subTarget(id, segments.slice(1));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const localMcpEndpoint = (): string => `http://127.0.0.1:${String(webhookPort())}/mcp`;
|
|
82
|
+
|
|
83
|
+
export function mcpAddCommand(key: string): string {
|
|
84
|
+
const url = `${localMcpEndpoint()}?token=${key}`;
|
|
85
|
+
return `claude mcp add --transport http ${SERVER_NAME} "${url}"`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface KeyPayload {
|
|
89
|
+
key: string | null;
|
|
90
|
+
endpoint: string | null;
|
|
91
|
+
command: string | null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function credentials(key: string): KeyPayload {
|
|
95
|
+
return {
|
|
96
|
+
key,
|
|
97
|
+
endpoint: `${localMcpEndpoint()}?token=${key}`,
|
|
98
|
+
command: mcpAddCommand(key),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function keyPayload(agent: AgentSummary): KeyPayload {
|
|
103
|
+
const value = agent.owned ? agent.key : null;
|
|
104
|
+
if (value === null) return { key: null, endpoint: null, command: null };
|
|
105
|
+
return credentials(value);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function livenessPayload(
|
|
109
|
+
agent: AgentSummary,
|
|
110
|
+
live: Map<string, { connected: boolean; lastSeenAt: number }>,
|
|
111
|
+
): Record<string, unknown> {
|
|
112
|
+
const found = agent.owned ? live.get(agent.id) : undefined;
|
|
113
|
+
if (found === undefined) return { connected: false, last_seen: null };
|
|
114
|
+
return {
|
|
115
|
+
connected: found.connected,
|
|
116
|
+
last_seen: new Date(found.lastSeenAt).toISOString(),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function agentPayload(
|
|
121
|
+
agent: AgentSummary,
|
|
122
|
+
live: Map<string, { connected: boolean; lastSeenAt: number }>,
|
|
123
|
+
connectors: Map<string, string[]>,
|
|
124
|
+
): Record<string, unknown> {
|
|
125
|
+
return {
|
|
126
|
+
id: agent.id,
|
|
127
|
+
name: agent.name,
|
|
128
|
+
owned: agent.owned,
|
|
129
|
+
connector_ids: connectors.get(agent.id) ?? [],
|
|
130
|
+
...livenessPayload(agent, live),
|
|
131
|
+
...keyPayload(agent),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function wantsAccounts(req: IncomingMessage): boolean {
|
|
136
|
+
const query = (req.url ?? '').split('?')[1] ?? '';
|
|
137
|
+
return new URLSearchParams(query).get('accounts') === '1';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function handleList(
|
|
141
|
+
req: IncomingMessage,
|
|
142
|
+
res: ServerResponse,
|
|
143
|
+
deps: AgentApiDeps,
|
|
144
|
+
session: ApiSession,
|
|
145
|
+
): Promise<void> {
|
|
146
|
+
const project = projectParam(req);
|
|
147
|
+
if (project === null) {
|
|
148
|
+
sendJson(req, res, 400, { error: 'a project is required' });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const list = await deps.listAgents(session.subject, project);
|
|
152
|
+
const live = deps.liveness();
|
|
153
|
+
const connectors = await deps.connectorIds(list.map((a) => a.id));
|
|
154
|
+
const base = {
|
|
155
|
+
agents: list.map((a) => agentPayload(a, live, connectors)),
|
|
156
|
+
capabilities: deps.capabilities(),
|
|
157
|
+
attachable: deps.attachable ?? ATTACHABLE,
|
|
158
|
+
};
|
|
159
|
+
if (!wantsAccounts(req)) {
|
|
160
|
+
sendJson(req, res, 200, base);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const { accounts, unavailable } = await deps.gatherAccounts(
|
|
164
|
+
new Set(list.map((a) => a.id)),
|
|
165
|
+
);
|
|
166
|
+
sendJson(req, res, 200, { ...base, accounts, unavailable });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function handleCreate(
|
|
170
|
+
req: IncomingMessage,
|
|
171
|
+
res: ServerResponse,
|
|
172
|
+
deps: AgentApiDeps,
|
|
173
|
+
session: ApiSession,
|
|
174
|
+
): Promise<void> {
|
|
175
|
+
const body = await readJsonBody(req);
|
|
176
|
+
const name = (body as { name?: unknown }).name;
|
|
177
|
+
const project = projectParam(req);
|
|
178
|
+
if (project === null) {
|
|
179
|
+
sendJson(req, res, 400, { error: 'a project is required' });
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const created = await deps.createAgent(session.subject, project, name as string);
|
|
183
|
+
log.info(
|
|
184
|
+
{ agent: created.name, id: created.id, owner: session.subject },
|
|
185
|
+
'agent-api: created agent',
|
|
186
|
+
);
|
|
187
|
+
sendJson(req, res, 201, {
|
|
188
|
+
id: created.id,
|
|
189
|
+
name: created.name,
|
|
190
|
+
...credentials(created.key),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function handleResetKey(
|
|
195
|
+
req: IncomingMessage,
|
|
196
|
+
res: ServerResponse,
|
|
197
|
+
deps: AgentApiDeps,
|
|
198
|
+
session: ApiSession,
|
|
199
|
+
id: string,
|
|
200
|
+
): Promise<void> {
|
|
201
|
+
const reset = await deps.resetKey(session.subject, id);
|
|
202
|
+
log.info(
|
|
203
|
+
{ agent: reset.name, id: reset.id, owner: session.subject },
|
|
204
|
+
'agent-api: reset agent key',
|
|
205
|
+
);
|
|
206
|
+
sendJson(req, res, 200, {
|
|
207
|
+
id: reset.id,
|
|
208
|
+
name: reset.name,
|
|
209
|
+
reset: true,
|
|
210
|
+
...credentials(reset.key),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async function handleDelete(
|
|
215
|
+
req: IncomingMessage,
|
|
216
|
+
res: ServerResponse,
|
|
217
|
+
deps: AgentApiDeps,
|
|
218
|
+
session: ApiSession,
|
|
219
|
+
id: string,
|
|
220
|
+
): Promise<void> {
|
|
221
|
+
const gone = await deps.deleteAgent(session.subject, id);
|
|
222
|
+
log.info(
|
|
223
|
+
{ agent: gone.name, id: gone.id, owner: session.subject },
|
|
224
|
+
'agent-api: deleted agent',
|
|
225
|
+
);
|
|
226
|
+
sendJson(req, res, 200, { id: gone.id, name: gone.name, deleted: true });
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
type AgentTarget = Exclude<Routable, { kind: 'accounts' }>;
|
|
230
|
+
|
|
231
|
+
async function routeAgent(
|
|
232
|
+
req: IncomingMessage,
|
|
233
|
+
res: ServerResponse,
|
|
234
|
+
deps: AgentApiDeps,
|
|
235
|
+
session: ApiSession,
|
|
236
|
+
tgt: AgentTarget,
|
|
237
|
+
): Promise<void> {
|
|
238
|
+
try {
|
|
239
|
+
if (tgt.kind === 'key') await handleResetKey(req, res, deps, session, tgt.id);
|
|
240
|
+
else if (tgt.kind === 'agent')
|
|
241
|
+
await handleDelete(req, res, deps, session, tgt.id);
|
|
242
|
+
else if (req.method === 'GET') await handleList(req, res, deps, session);
|
|
243
|
+
else await handleCreate(req, res, deps, session);
|
|
244
|
+
} catch (err) {
|
|
245
|
+
apiFailure(req, res, err);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const ALLOWED: Record<AgentTarget['kind'], string[]> = {
|
|
250
|
+
collection: ['GET', 'POST'],
|
|
251
|
+
agent: ['DELETE'],
|
|
252
|
+
key: ['POST'],
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
function methodAllowed(tgt: Routable, method: string | undefined): boolean {
|
|
256
|
+
if (tgt.kind === 'accounts') return accountRouteAllows(tgt.route, method);
|
|
257
|
+
return ALLOWED[tgt.kind].includes(method ?? '');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async function dispatch(
|
|
261
|
+
req: IncomingMessage,
|
|
262
|
+
res: ServerResponse,
|
|
263
|
+
deps: AgentApiDeps,
|
|
264
|
+
tgt: Routable,
|
|
265
|
+
): Promise<void> {
|
|
266
|
+
const session = await apiSession(req);
|
|
267
|
+
if (!session) {
|
|
268
|
+
sendJson(req, res, 401, { error: 'unauthorized' });
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (tgt.kind === 'accounts')
|
|
272
|
+
await handleAccountRoute(req, res, deps, session, tgt.id, tgt.route);
|
|
273
|
+
else await routeAgent(req, res, deps, session, tgt);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function handleAgentApiRequest(
|
|
277
|
+
req: IncomingMessage,
|
|
278
|
+
res: ServerResponse,
|
|
279
|
+
deps: AgentApiDeps,
|
|
280
|
+
): boolean {
|
|
281
|
+
const tgt = target((req.url ?? '').split('?')[0] ?? '');
|
|
282
|
+
if (tgt === null) return false;
|
|
283
|
+
if (req.method === 'OPTIONS') {
|
|
284
|
+
res.writeHead(204, cors(req)).end();
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
if (tgt.kind === 'unknown') {
|
|
288
|
+
sendJson(req, res, 404, { error: 'no such agent' });
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
if (!methodAllowed(tgt, req.method)) {
|
|
292
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
dispatch(req, res, deps, tgt).catch((err: unknown) => {
|
|
296
|
+
log.warn({ err: errMsg(err) }, 'agent-api: unhandled error');
|
|
297
|
+
if (!res.headersSent) sendJson(req, res, 500, { error: 'agent api failed' });
|
|
298
|
+
});
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
3
|
+
import { apiFailure, apiSession, cors, readJsonBody, sendJson } from '@metro-labs/http/api-http';
|
|
4
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
6
|
+
import { parseId } from '@metro-labs/core/ids';
|
|
7
|
+
import type { LoadedAccount, LoadedAgent, LoadedConnector } from '../stations/materialize.js';
|
|
8
|
+
import { STATIONS } from '@metro-labs/core/station-names';
|
|
9
|
+
|
|
10
|
+
const AGENTS = '/api/agents';
|
|
11
|
+
const RESTORE_PATH = `${AGENTS}/restore`;
|
|
12
|
+
const BUNDLE_MAX = 2 * 1024 * 1024;
|
|
13
|
+
const STATION_NAMES = new Set<string>(STATIONS);
|
|
14
|
+
|
|
15
|
+
export interface AgentBundle {
|
|
16
|
+
version: 1;
|
|
17
|
+
agent: { id: string; name: string; key: string; stations: LoadedAccount[] };
|
|
18
|
+
connectors: LoadedConnector[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const loadedAgentOf = (bundle: AgentBundle): LoadedAgent => ({
|
|
22
|
+
id: bundle.agent.id,
|
|
23
|
+
name: bundle.agent.name,
|
|
24
|
+
key: bundle.agent.key,
|
|
25
|
+
accounts: bundle.agent.stations,
|
|
26
|
+
connectors: [],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export interface RestoredAgent {
|
|
30
|
+
id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
stations: number;
|
|
33
|
+
connectors: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type ImportMode = 'append' | 'overwrite';
|
|
37
|
+
|
|
38
|
+
export interface BundleApiDeps {
|
|
39
|
+
bundle: (subject: string, agentId: string) => Promise<AgentBundle>;
|
|
40
|
+
restore: (subject: string, bundle: AgentBundle, mode: ImportMode) => Promise<RestoredAgent>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function parseMode(raw: unknown): ImportMode {
|
|
44
|
+
const mode = isRecord(raw) && typeof raw.mode === 'string' ? raw.mode : 'overwrite';
|
|
45
|
+
if (mode !== 'append' && mode !== 'overwrite') throw bad("mode is 'append' or 'overwrite'");
|
|
46
|
+
return mode;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const bad = (what: string): ApiError => new ApiError(`bundle: ${what}`, 400);
|
|
50
|
+
|
|
51
|
+
function stationOf(raw: unknown): LoadedAccount {
|
|
52
|
+
if (!isRecord(raw) || typeof raw.station !== 'string' || !STATION_NAMES.has(raw.station))
|
|
53
|
+
throw bad('a station is not a known station');
|
|
54
|
+
if (typeof raw.id !== 'string' || !isRecord(raw.config)) throw bad('a station has no id or config');
|
|
55
|
+
const allowlist = Array.isArray(raw.allowlist)
|
|
56
|
+
? raw.allowlist.filter((s): s is string => typeof s === 'string')
|
|
57
|
+
: null;
|
|
58
|
+
return { station: raw.station as LoadedAccount['station'], id: raw.id, allowlist, enabled: raw.enabled !== false, config: raw.config };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function connectorOf(raw: unknown): LoadedConnector {
|
|
62
|
+
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.name !== 'string' || typeof raw.url !== 'string')
|
|
63
|
+
throw bad('a connector is missing its id, name or url');
|
|
64
|
+
if (!isRecord(raw.config)) throw bad('a connector has no config');
|
|
65
|
+
return { id: raw.id, name: raw.name, url: raw.url, transport: 'http', config: raw.config };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function agentOf(raw: unknown): AgentBundle['agent'] {
|
|
69
|
+
if (!isRecord(raw)) throw bad('not a v1 agent bundle');
|
|
70
|
+
const { id, name, key, stations } = raw;
|
|
71
|
+
if (typeof id !== 'string' || parseId(id) === null) throw bad('agent id is not an id');
|
|
72
|
+
if (typeof name !== 'string' || typeof key !== 'string' || key === '') throw bad('agent has no name or key');
|
|
73
|
+
if (!Array.isArray(stations)) throw bad('agent has no station list');
|
|
74
|
+
return { id, name, key, stations: stations.map(stationOf) };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function parseBundle(raw: unknown): AgentBundle {
|
|
78
|
+
if (!isRecord(raw) || raw.version !== 1) throw bad('not a v1 agent bundle');
|
|
79
|
+
const connectors = Array.isArray(raw.connectors) ? raw.connectors : [];
|
|
80
|
+
return { version: 1, agent: agentOf(raw.agent), connectors: connectors.map(connectorOf) };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function answer(req: IncomingMessage, deps: BundleApiDeps, subject: string, path: string): Promise<unknown> {
|
|
84
|
+
if (path === RESTORE_PATH) {
|
|
85
|
+
const body: unknown = await readJsonBody(req, BUNDLE_MAX);
|
|
86
|
+
return deps.restore(subject, parseBundle(body), parseMode(body));
|
|
87
|
+
}
|
|
88
|
+
const id = parseId(path.slice(AGENTS.length + 1).split('/')[0] ?? '');
|
|
89
|
+
if (id === null) throw new ApiError('no such agent', 404);
|
|
90
|
+
return deps.bundle(subject, id);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function handleBundleRequest(req: IncomingMessage, res: ServerResponse, deps: BundleApiDeps): boolean {
|
|
94
|
+
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
95
|
+
const isRestore = path === RESTORE_PATH;
|
|
96
|
+
const isBundle = /^\/api\/agents\/[^/]+\/bundle$/.test(path);
|
|
97
|
+
if (!isRestore && !isBundle) return false;
|
|
98
|
+
if (req.method === 'OPTIONS') {
|
|
99
|
+
res.writeHead(204, cors(req)).end();
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
if (req.method !== (isRestore ? 'POST' : 'GET')) {
|
|
103
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
apiSession(req)
|
|
107
|
+
.then((session) => {
|
|
108
|
+
if (!session) throw new ApiError('unauthorized', 401);
|
|
109
|
+
return answer(req, deps, session.subject, path);
|
|
110
|
+
})
|
|
111
|
+
.then((body) => {
|
|
112
|
+
sendJson(req, res, isRestore ? 201 : 200, body);
|
|
113
|
+
})
|
|
114
|
+
.catch((err: unknown) => {
|
|
115
|
+
if (err instanceof ApiError) apiFailure(req, res, err, 'bundle-api');
|
|
116
|
+
else {
|
|
117
|
+
log.warn({ err: errMsg(err) }, 'bundle-api: request failed');
|
|
118
|
+
if (!res.headersSent) sendJson(req, res, 500, { error: 'bundle api failed' });
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return true;
|
|
122
|
+
}
|