@stage-labs/metro 0.1.0-beta.8 → 0.1.0-beta.80
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 +107 -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/node-name.js +71 -0
- package/dist/plugin.js +80 -0
- package/dist/provider-flags.js +9 -0
- package/dist/runtime-install.js +87 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +146 -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 -25
- 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 +82 -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 +12 -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 +107 -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 +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +311 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +302 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +329 -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 +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +176 -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 +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +329 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +167 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +307 -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 +144 -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 +69 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +159 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +117 -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 +157 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +127 -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 +295 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +277 -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 +330 -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 +252 -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 +220 -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 +182 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +197 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +195 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +198 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +40 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +137 -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/http.ts +390 -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 +187 -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/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 +184 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +35 -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/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +71 -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/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 +178 -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 +327 -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/station.ts +17 -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 +36 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -249
- package/dist/cache.js +0 -75
- package/dist/cli/actions.js +0 -139
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -177
- package/dist/cli/skill.js +0 -62
- package/dist/cli/util.js +0 -72
- package/dist/cli/webhook.js +0 -83
- package/dist/codex-rc.js +0 -245
- package/dist/dispatcher.js +0 -122
- package/dist/history.js +0 -99
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- 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 -200
- package/dist/stations/index.js +0 -133
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -213
- package/dist/stations/webhook.js +0 -100
- package/dist/tunnel.js +0 -49
- package/dist/webhooks.js +0 -40
- package/docs/agents.md +0 -216
- package/docs/uri-scheme.md +0 -125
- package/skills/metro/SKILL.md +0 -249
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
3
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
4
|
+
import {
|
|
5
|
+
apiFailure,
|
|
6
|
+
bodyField,
|
|
7
|
+
readJsonBody,
|
|
8
|
+
sendJson,
|
|
9
|
+
type ApiSession,
|
|
10
|
+
} from '@metro-labs/http/api-http';
|
|
11
|
+
import {
|
|
12
|
+
isStationName,
|
|
13
|
+
parseAccountId,
|
|
14
|
+
type AccountRef,
|
|
15
|
+
} from './account-attach.js';
|
|
16
|
+
import type { StationName } from '@metro-labs/core/station-names';
|
|
17
|
+
import {
|
|
18
|
+
ATTACHABLE_STATIONS,
|
|
19
|
+
isAttachStation,
|
|
20
|
+
type AttachInput,
|
|
21
|
+
type OneTimeSecret,
|
|
22
|
+
type PreparedAccount,
|
|
23
|
+
} from '../stations/attach.js';
|
|
24
|
+
import {
|
|
25
|
+
INTERACTIVE_STATIONS,
|
|
26
|
+
isInteractiveStation,
|
|
27
|
+
type InteractiveStation,
|
|
28
|
+
} from '../stations/attach-interactive.js';
|
|
29
|
+
import {
|
|
30
|
+
ATTACH_ID_RE,
|
|
31
|
+
type AttachOwner,
|
|
32
|
+
type AttachView,
|
|
33
|
+
} from '../stations/attach-session.js';
|
|
34
|
+
|
|
35
|
+
export interface AttachSessionApi {
|
|
36
|
+
start: (
|
|
37
|
+
owner: AttachOwner,
|
|
38
|
+
station: InteractiveStation,
|
|
39
|
+
input: Record<string, unknown>,
|
|
40
|
+
) => Promise<AttachView>;
|
|
41
|
+
view: (owner: AttachOwner, attachId: string) => AttachView;
|
|
42
|
+
submit: (
|
|
43
|
+
owner: AttachOwner,
|
|
44
|
+
attachId: string,
|
|
45
|
+
input: { code?: unknown; password?: unknown },
|
|
46
|
+
) => Promise<AttachView>;
|
|
47
|
+
cancel: (owner: AttachOwner, attachId: string) => Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface AccountApiDeps {
|
|
51
|
+
attachSessions: AttachSessionApi;
|
|
52
|
+
prepareAccount: (input: AttachInput) => Promise<PreparedAccount>;
|
|
53
|
+
attachAccount: (
|
|
54
|
+
subject: string,
|
|
55
|
+
agentId: string,
|
|
56
|
+
station: StationName,
|
|
57
|
+
config: Record<string, unknown>,
|
|
58
|
+
) => Promise<AccountRef>;
|
|
59
|
+
detachAccount: (
|
|
60
|
+
subject: string,
|
|
61
|
+
agentId: string,
|
|
62
|
+
station: StationName,
|
|
63
|
+
accountId: string,
|
|
64
|
+
) => Promise<AccountRef>;
|
|
65
|
+
syncStations: (station: StationName) => Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type AccountRoute =
|
|
69
|
+
| { kind: 'start' }
|
|
70
|
+
| { kind: 'session'; attachId: string }
|
|
71
|
+
| { kind: 'step'; attachId: string }
|
|
72
|
+
| { kind: 'account'; station: StationName; accountId: string };
|
|
73
|
+
|
|
74
|
+
export const ATTACHABLE: string[] = [
|
|
75
|
+
...ATTACHABLE_STATIONS,
|
|
76
|
+
...INTERACTIVE_STATIONS,
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
const ROUTE_METHODS: Record<AccountRoute['kind'], string[]> = {
|
|
80
|
+
start: ['POST'],
|
|
81
|
+
session: ['GET', 'DELETE'],
|
|
82
|
+
step: ['POST'],
|
|
83
|
+
account: ['DELETE'],
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
function twoSegmentRoute(head: string, tail: string): AccountRoute | null {
|
|
87
|
+
if (ATTACH_ID_RE.test(head))
|
|
88
|
+
return tail === 'step' ? { kind: 'step', attachId: head } : null;
|
|
89
|
+
if (!isStationName(head)) return null;
|
|
90
|
+
const accountId = parseAccountId(tail);
|
|
91
|
+
return accountId === null ? null : { kind: 'account', station: head, accountId };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function accountRoute(rest: string[]): AccountRoute | null {
|
|
95
|
+
const [head, tail] = rest;
|
|
96
|
+
if (head === undefined) return null;
|
|
97
|
+
if (rest.length === 1)
|
|
98
|
+
return head === 'start'
|
|
99
|
+
? { kind: 'start' }
|
|
100
|
+
: ATTACH_ID_RE.test(head)
|
|
101
|
+
? { kind: 'session', attachId: head }
|
|
102
|
+
: null;
|
|
103
|
+
if (rest.length !== 2 || tail === undefined) return null;
|
|
104
|
+
return twoSegmentRoute(head, tail);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function accountRouteAllows(
|
|
108
|
+
route: AccountRoute,
|
|
109
|
+
method: string | undefined,
|
|
110
|
+
): boolean {
|
|
111
|
+
return ROUTE_METHODS[route.kind].includes(method ?? '');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function activate(
|
|
115
|
+
deps: AccountApiDeps,
|
|
116
|
+
station: StationName,
|
|
117
|
+
): Promise<boolean> {
|
|
118
|
+
try {
|
|
119
|
+
await deps.syncStations(station);
|
|
120
|
+
return true;
|
|
121
|
+
} catch (err) {
|
|
122
|
+
log.warn(
|
|
123
|
+
{ station, err: errMsg(err) },
|
|
124
|
+
'account-api: station reload failed, the change lands at the next boot',
|
|
125
|
+
);
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
interface AttachPayload {
|
|
131
|
+
status: 'done';
|
|
132
|
+
agentId: string;
|
|
133
|
+
station: string;
|
|
134
|
+
accountId: string;
|
|
135
|
+
identity: Record<string, string>;
|
|
136
|
+
activated: boolean;
|
|
137
|
+
secret?: OneTimeSecret;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function storeAccount(
|
|
141
|
+
deps: AccountApiDeps,
|
|
142
|
+
session: ApiSession,
|
|
143
|
+
agentId: string,
|
|
144
|
+
station: StationName,
|
|
145
|
+
prepared: PreparedAccount,
|
|
146
|
+
): Promise<AccountRef> {
|
|
147
|
+
try {
|
|
148
|
+
return await deps.attachAccount(
|
|
149
|
+
session.subject,
|
|
150
|
+
agentId,
|
|
151
|
+
station,
|
|
152
|
+
prepared.config,
|
|
153
|
+
);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
prepared.discard?.();
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function ownerOf(session: ApiSession, agentId: string): AttachOwner {
|
|
161
|
+
return { subject: session.subject, agentId };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function asInput(body: unknown): Record<string, unknown> {
|
|
165
|
+
const out: Record<string, unknown> = {};
|
|
166
|
+
for (const key of ['apiId', 'apiHash', 'phone'])
|
|
167
|
+
out[key] = bodyField(body, key);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function handleStart(
|
|
172
|
+
req: IncomingMessage,
|
|
173
|
+
res: ServerResponse,
|
|
174
|
+
deps: AccountApiDeps,
|
|
175
|
+
session: ApiSession,
|
|
176
|
+
agentId: string,
|
|
177
|
+
): Promise<void> {
|
|
178
|
+
const body = await readJsonBody(req);
|
|
179
|
+
const station = bodyField(body, 'station');
|
|
180
|
+
if (isInteractiveStation(station)) {
|
|
181
|
+
const view = await deps.attachSessions.start(
|
|
182
|
+
ownerOf(session, agentId),
|
|
183
|
+
station,
|
|
184
|
+
asInput(body),
|
|
185
|
+
);
|
|
186
|
+
sendJson(req, res, 201, view);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (!isAttachStation(station))
|
|
190
|
+
throw new ApiError(`station must be one of ${ATTACHABLE.join(', ')}`, 400);
|
|
191
|
+
const prepared = await deps.prepareAccount({
|
|
192
|
+
station,
|
|
193
|
+
token: bodyField(body, 'token'),
|
|
194
|
+
});
|
|
195
|
+
const ref = await storeAccount(deps, session, agentId, station, prepared);
|
|
196
|
+
log.info(
|
|
197
|
+
{ agentId: ref.agentId, station, account: ref.accountId },
|
|
198
|
+
'account-api: attached a station account',
|
|
199
|
+
);
|
|
200
|
+
const payload: AttachPayload = {
|
|
201
|
+
status: 'done',
|
|
202
|
+
agentId: ref.agentId,
|
|
203
|
+
station,
|
|
204
|
+
accountId: ref.accountId,
|
|
205
|
+
identity: prepared.identity,
|
|
206
|
+
activated: await activate(deps, station),
|
|
207
|
+
};
|
|
208
|
+
sendJson(
|
|
209
|
+
req,
|
|
210
|
+
res,
|
|
211
|
+
201,
|
|
212
|
+
prepared.secret === undefined
|
|
213
|
+
? payload
|
|
214
|
+
: { ...payload, secret: prepared.secret },
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function handleDetach(
|
|
219
|
+
req: IncomingMessage,
|
|
220
|
+
res: ServerResponse,
|
|
221
|
+
deps: AccountApiDeps,
|
|
222
|
+
session: ApiSession,
|
|
223
|
+
agentId: string,
|
|
224
|
+
target: { station: StationName; accountId: string },
|
|
225
|
+
): Promise<void> {
|
|
226
|
+
const ref = await deps.detachAccount(
|
|
227
|
+
session.subject,
|
|
228
|
+
agentId,
|
|
229
|
+
target.station,
|
|
230
|
+
target.accountId,
|
|
231
|
+
);
|
|
232
|
+
log.info(
|
|
233
|
+
{ agentId: ref.agentId, station: ref.station, account: ref.accountId },
|
|
234
|
+
'account-api: detached a station account',
|
|
235
|
+
);
|
|
236
|
+
sendJson(req, res, 200, {
|
|
237
|
+
agentId: ref.agentId,
|
|
238
|
+
station: ref.station,
|
|
239
|
+
accountId: ref.accountId,
|
|
240
|
+
detached: true,
|
|
241
|
+
activated: await activate(deps, ref.station),
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function handleSession(
|
|
246
|
+
req: IncomingMessage,
|
|
247
|
+
res: ServerResponse,
|
|
248
|
+
deps: AccountApiDeps,
|
|
249
|
+
owner: AttachOwner,
|
|
250
|
+
attachId: string,
|
|
251
|
+
): Promise<void> {
|
|
252
|
+
if (req.method === 'DELETE') {
|
|
253
|
+
await deps.attachSessions.cancel(owner, attachId);
|
|
254
|
+
sendJson(req, res, 200, { attachId, cancelled: true });
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
sendJson(req, res, 200, deps.attachSessions.view(owner, attachId));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async function handleStep(
|
|
261
|
+
req: IncomingMessage,
|
|
262
|
+
res: ServerResponse,
|
|
263
|
+
deps: AccountApiDeps,
|
|
264
|
+
owner: AttachOwner,
|
|
265
|
+
attachId: string,
|
|
266
|
+
): Promise<void> {
|
|
267
|
+
const body = await readJsonBody(req);
|
|
268
|
+
const view = await deps.attachSessions.submit(owner, attachId, {
|
|
269
|
+
code: bodyField(body, 'code'),
|
|
270
|
+
password: bodyField(body, 'password'),
|
|
271
|
+
});
|
|
272
|
+
sendJson(req, res, 200, view);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function dispatchRoute(
|
|
276
|
+
req: IncomingMessage,
|
|
277
|
+
res: ServerResponse,
|
|
278
|
+
deps: AccountApiDeps,
|
|
279
|
+
session: ApiSession,
|
|
280
|
+
agentId: string,
|
|
281
|
+
route: AccountRoute,
|
|
282
|
+
): Promise<void> {
|
|
283
|
+
if (route.kind === 'start')
|
|
284
|
+
return handleStart(req, res, deps, session, agentId);
|
|
285
|
+
if (route.kind === 'session')
|
|
286
|
+
return handleSession(
|
|
287
|
+
req,
|
|
288
|
+
res,
|
|
289
|
+
deps,
|
|
290
|
+
ownerOf(session, agentId),
|
|
291
|
+
route.attachId,
|
|
292
|
+
);
|
|
293
|
+
if (route.kind === 'step')
|
|
294
|
+
return handleStep(req, res, deps, ownerOf(session, agentId), route.attachId);
|
|
295
|
+
return handleDetach(req, res, deps, session, agentId, route);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export async function handleAccountRoute(
|
|
299
|
+
req: IncomingMessage,
|
|
300
|
+
res: ServerResponse,
|
|
301
|
+
deps: AccountApiDeps,
|
|
302
|
+
session: ApiSession,
|
|
303
|
+
agentId: string,
|
|
304
|
+
route: AccountRoute,
|
|
305
|
+
): Promise<void> {
|
|
306
|
+
try {
|
|
307
|
+
await dispatchRoute(req, res, deps, session, agentId, route);
|
|
308
|
+
} catch (err) {
|
|
309
|
+
apiFailure(req, res, err);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
3
|
+
import { AGENT_NAME_RE } from '@metro-labs/core/ids';
|
|
4
|
+
|
|
5
|
+
export class AgentAdminError extends ApiError {}
|
|
6
|
+
|
|
7
|
+
export interface AgentSummary {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
owned: boolean;
|
|
11
|
+
key: string | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface CreatedAgent {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
key: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface OwnedAgent {
|
|
21
|
+
id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type DeletedAgent = OwnedAgent;
|
|
26
|
+
|
|
27
|
+
export interface ResetAgentKey extends OwnedAgent {
|
|
28
|
+
key: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function normalizeAgentName(raw: unknown): string {
|
|
32
|
+
const name = typeof raw === 'string' ? raw.trim() : '';
|
|
33
|
+
if (!AGENT_NAME_RE.test(name))
|
|
34
|
+
throw new AgentAdminError(
|
|
35
|
+
'name must be 2-32 characters of A-Z, a-z, 0-9, - or _, starting with a letter or digit',
|
|
36
|
+
400,
|
|
37
|
+
);
|
|
38
|
+
return name;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function newApiKey(): string {
|
|
42
|
+
return `mk_${randomBytes(32).toString('base64url')}`;
|
|
43
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
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 {
|
|
15
|
+
accountRoute,
|
|
16
|
+
accountRouteAllows,
|
|
17
|
+
ATTACHABLE,
|
|
18
|
+
handleAccountRoute,
|
|
19
|
+
type AccountApiDeps,
|
|
20
|
+
type AccountRoute,
|
|
21
|
+
} from './accounts-api.js';
|
|
22
|
+
import {
|
|
23
|
+
type AgentSummary,
|
|
24
|
+
type CreatedAgent,
|
|
25
|
+
type DeletedAgent,
|
|
26
|
+
type ResetAgentKey,
|
|
27
|
+
} from './admin.js';
|
|
28
|
+
|
|
29
|
+
const PREFIX = '/api/agents';
|
|
30
|
+
const SERVER_NAME = 'metro';
|
|
31
|
+
|
|
32
|
+
export interface AgentApiDeps extends AccountApiDeps {
|
|
33
|
+
listAgents: (subject: string, project: string) => Promise<AgentSummary[]>;
|
|
34
|
+
createAgent: (
|
|
35
|
+
subject: string,
|
|
36
|
+
project: string,
|
|
37
|
+
name: string,
|
|
38
|
+
) => Promise<CreatedAgent>;
|
|
39
|
+
deleteAgent: (
|
|
40
|
+
subject: string,
|
|
41
|
+
id: string,
|
|
42
|
+
) => Promise<DeletedAgent>;
|
|
43
|
+
resetKey: (
|
|
44
|
+
subject: string,
|
|
45
|
+
id: string,
|
|
46
|
+
) => Promise<ResetAgentKey>;
|
|
47
|
+
gatherAccounts: (allowed: Set<string>) => Promise<{
|
|
48
|
+
accounts: Record<string, unknown[]>;
|
|
49
|
+
unavailable: string[];
|
|
50
|
+
}>;
|
|
51
|
+
capabilities: () => Record<string, string[]>;
|
|
52
|
+
attachable?: string[];
|
|
53
|
+
liveness: () => Map<string, { connected: boolean; lastSeenAt: number }>;
|
|
54
|
+
connectorIds: (agentIds: string[]) => Promise<Map<string, string[]>>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Routable =
|
|
58
|
+
| { kind: 'collection' }
|
|
59
|
+
| { kind: 'agent'; id: string }
|
|
60
|
+
| { kind: 'key'; id: string }
|
|
61
|
+
| { kind: 'accounts'; id: string; route: AccountRoute };
|
|
62
|
+
|
|
63
|
+
type Target = Routable | { kind: 'unknown' } | null;
|
|
64
|
+
|
|
65
|
+
function subTarget(id: string, rest: string[]): Target {
|
|
66
|
+
if (rest.length === 0) return { kind: 'agent', id };
|
|
67
|
+
if (rest.length === 1 && rest[0] === 'key') return { kind: 'key', id };
|
|
68
|
+
if (rest[0] !== 'accounts') return { kind: 'unknown' };
|
|
69
|
+
const route = accountRoute(rest.slice(1));
|
|
70
|
+
return route === null ? { kind: 'unknown' } : { kind: 'accounts', id, route };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function target(path: string): Target {
|
|
74
|
+
if (path === PREFIX || path === `${PREFIX}/`) return { kind: 'collection' };
|
|
75
|
+
if (!path.startsWith(`${PREFIX}/`)) return null;
|
|
76
|
+
const segments = path.slice(PREFIX.length + 1).split('/').filter(Boolean);
|
|
77
|
+
const head = segments[0];
|
|
78
|
+
if (head === undefined) return { kind: 'collection' };
|
|
79
|
+
const id = parseId(head);
|
|
80
|
+
return id === null ? { kind: 'unknown' } : subTarget(id, segments.slice(1));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const localMcpEndpoint = (): string => `http://127.0.0.1:${String(webhookPort())}/mcp`;
|
|
84
|
+
|
|
85
|
+
export function mcpAddCommand(key: string): string {
|
|
86
|
+
const url = `${localMcpEndpoint()}?token=${key}`;
|
|
87
|
+
return `claude mcp add --transport http ${SERVER_NAME} "${url}"`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface KeyPayload {
|
|
91
|
+
key: string | null;
|
|
92
|
+
endpoint: string | null;
|
|
93
|
+
command: string | null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function credentials(key: string): KeyPayload {
|
|
97
|
+
return {
|
|
98
|
+
key,
|
|
99
|
+
endpoint: `${localMcpEndpoint()}?token=${key}`,
|
|
100
|
+
command: mcpAddCommand(key),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function keyPayload(agent: AgentSummary): KeyPayload {
|
|
105
|
+
const value = agent.owned ? agent.key : null;
|
|
106
|
+
if (value === null) return { key: null, endpoint: null, command: null };
|
|
107
|
+
return credentials(value);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function livenessPayload(
|
|
111
|
+
agent: AgentSummary,
|
|
112
|
+
live: Map<string, { connected: boolean; lastSeenAt: number }>,
|
|
113
|
+
): Record<string, unknown> {
|
|
114
|
+
const found = agent.owned ? live.get(agent.id) : undefined;
|
|
115
|
+
if (found === undefined) return { connected: false, last_seen: null };
|
|
116
|
+
return {
|
|
117
|
+
connected: found.connected,
|
|
118
|
+
last_seen: new Date(found.lastSeenAt).toISOString(),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function agentPayload(
|
|
123
|
+
agent: AgentSummary,
|
|
124
|
+
live: Map<string, { connected: boolean; lastSeenAt: number }>,
|
|
125
|
+
connectors: Map<string, string[]>,
|
|
126
|
+
): Record<string, unknown> {
|
|
127
|
+
return {
|
|
128
|
+
id: agent.id,
|
|
129
|
+
name: agent.name,
|
|
130
|
+
owned: agent.owned,
|
|
131
|
+
connector_ids: connectors.get(agent.id) ?? [],
|
|
132
|
+
...livenessPayload(agent, live),
|
|
133
|
+
...keyPayload(agent),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function wantsAccounts(req: IncomingMessage): boolean {
|
|
138
|
+
const query = (req.url ?? '').split('?')[1] ?? '';
|
|
139
|
+
return new URLSearchParams(query).get('accounts') === '1';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function handleList(
|
|
143
|
+
req: IncomingMessage,
|
|
144
|
+
res: ServerResponse,
|
|
145
|
+
deps: AgentApiDeps,
|
|
146
|
+
session: ApiSession,
|
|
147
|
+
): Promise<void> {
|
|
148
|
+
const project = projectParam(req);
|
|
149
|
+
if (project === null) {
|
|
150
|
+
sendJson(req, res, 400, { error: 'a project is required' });
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const list = await deps.listAgents(session.subject, project);
|
|
154
|
+
const live = deps.liveness();
|
|
155
|
+
const connectors = await deps.connectorIds(list.map((a) => a.id));
|
|
156
|
+
const base = {
|
|
157
|
+
agents: list.map((a) => agentPayload(a, live, connectors)),
|
|
158
|
+
capabilities: deps.capabilities(),
|
|
159
|
+
attachable: deps.attachable ?? ATTACHABLE,
|
|
160
|
+
};
|
|
161
|
+
if (!wantsAccounts(req)) {
|
|
162
|
+
sendJson(req, res, 200, base);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const { accounts, unavailable } = await deps.gatherAccounts(
|
|
166
|
+
new Set(list.map((a) => a.id)),
|
|
167
|
+
);
|
|
168
|
+
sendJson(req, res, 200, { ...base, accounts, unavailable });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function handleCreate(
|
|
172
|
+
req: IncomingMessage,
|
|
173
|
+
res: ServerResponse,
|
|
174
|
+
deps: AgentApiDeps,
|
|
175
|
+
session: ApiSession,
|
|
176
|
+
): Promise<void> {
|
|
177
|
+
const body = await readJsonBody(req);
|
|
178
|
+
const name = (body as { name?: unknown }).name;
|
|
179
|
+
const project = projectParam(req);
|
|
180
|
+
if (project === null) {
|
|
181
|
+
sendJson(req, res, 400, { error: 'a project is required' });
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const created = await deps.createAgent(session.subject, project, name as string);
|
|
185
|
+
log.info(
|
|
186
|
+
{ agent: created.name, id: created.id, owner: session.subject },
|
|
187
|
+
'agent-api: created agent',
|
|
188
|
+
);
|
|
189
|
+
sendJson(req, res, 201, {
|
|
190
|
+
id: created.id,
|
|
191
|
+
name: created.name,
|
|
192
|
+
...credentials(created.key),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function handleResetKey(
|
|
197
|
+
req: IncomingMessage,
|
|
198
|
+
res: ServerResponse,
|
|
199
|
+
deps: AgentApiDeps,
|
|
200
|
+
session: ApiSession,
|
|
201
|
+
id: string,
|
|
202
|
+
): Promise<void> {
|
|
203
|
+
const reset = await deps.resetKey(session.subject, id);
|
|
204
|
+
log.info(
|
|
205
|
+
{ agent: reset.name, id: reset.id, owner: session.subject },
|
|
206
|
+
'agent-api: reset agent key',
|
|
207
|
+
);
|
|
208
|
+
sendJson(req, res, 200, {
|
|
209
|
+
id: reset.id,
|
|
210
|
+
name: reset.name,
|
|
211
|
+
reset: true,
|
|
212
|
+
...credentials(reset.key),
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function handleDelete(
|
|
217
|
+
req: IncomingMessage,
|
|
218
|
+
res: ServerResponse,
|
|
219
|
+
deps: AgentApiDeps,
|
|
220
|
+
session: ApiSession,
|
|
221
|
+
id: string,
|
|
222
|
+
): Promise<void> {
|
|
223
|
+
const gone = await deps.deleteAgent(session.subject, id);
|
|
224
|
+
log.info(
|
|
225
|
+
{ agent: gone.name, id: gone.id, owner: session.subject },
|
|
226
|
+
'agent-api: deleted agent',
|
|
227
|
+
);
|
|
228
|
+
sendJson(req, res, 200, { id: gone.id, name: gone.name, deleted: true });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
type AgentTarget = Exclude<Routable, { kind: 'accounts' }>;
|
|
232
|
+
|
|
233
|
+
async function routeAgent(
|
|
234
|
+
req: IncomingMessage,
|
|
235
|
+
res: ServerResponse,
|
|
236
|
+
deps: AgentApiDeps,
|
|
237
|
+
session: ApiSession,
|
|
238
|
+
tgt: AgentTarget,
|
|
239
|
+
): Promise<void> {
|
|
240
|
+
try {
|
|
241
|
+
if (tgt.kind === 'key') await handleResetKey(req, res, deps, session, tgt.id);
|
|
242
|
+
else if (tgt.kind === 'agent')
|
|
243
|
+
await handleDelete(req, res, deps, session, tgt.id);
|
|
244
|
+
else if (req.method === 'GET') await handleList(req, res, deps, session);
|
|
245
|
+
else await handleCreate(req, res, deps, session);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
apiFailure(req, res, err);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const ALLOWED: Record<AgentTarget['kind'], string[]> = {
|
|
252
|
+
collection: ['GET', 'POST'],
|
|
253
|
+
agent: ['DELETE'],
|
|
254
|
+
key: ['POST'],
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
function methodAllowed(tgt: Routable, method: string | undefined): boolean {
|
|
258
|
+
if (tgt.kind === 'accounts') return accountRouteAllows(tgt.route, method);
|
|
259
|
+
return ALLOWED[tgt.kind].includes(method ?? '');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function dispatch(
|
|
263
|
+
req: IncomingMessage,
|
|
264
|
+
res: ServerResponse,
|
|
265
|
+
deps: AgentApiDeps,
|
|
266
|
+
tgt: Routable,
|
|
267
|
+
): Promise<void> {
|
|
268
|
+
const session = await apiSession(req);
|
|
269
|
+
if (!session) {
|
|
270
|
+
sendJson(req, res, 401, { error: 'unauthorized' });
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (tgt.kind === 'accounts')
|
|
274
|
+
await handleAccountRoute(req, res, deps, session, tgt.id, tgt.route);
|
|
275
|
+
else await routeAgent(req, res, deps, session, tgt);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function handleAgentApiRequest(
|
|
279
|
+
req: IncomingMessage,
|
|
280
|
+
res: ServerResponse,
|
|
281
|
+
deps: AgentApiDeps,
|
|
282
|
+
): boolean {
|
|
283
|
+
const tgt = target((req.url ?? '').split('?')[0] ?? '');
|
|
284
|
+
if (tgt === null) return false;
|
|
285
|
+
if (req.method === 'OPTIONS') {
|
|
286
|
+
res.writeHead(204, cors(req)).end();
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
if (tgt.kind === 'unknown') {
|
|
290
|
+
sendJson(req, res, 404, { error: 'no such agent' });
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
if (!methodAllowed(tgt, req.method)) {
|
|
294
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
dispatch(req, res, deps, tgt).catch((err: unknown) => {
|
|
298
|
+
log.warn({ err: errMsg(err) }, 'agent-api: unhandled error');
|
|
299
|
+
if (!res.headersSent) sendJson(req, res, 500, { error: 'agent api failed' });
|
|
300
|
+
});
|
|
301
|
+
return true;
|
|
302
|
+
}
|