@stage-labs/metro 0.1.0-beta.2 → 0.1.0-beta.21
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 +103 -0
- package/dist/cli.js +134 -565
- package/dist/control.js +102 -0
- package/dist/prompt.js +10 -0
- package/dist/runtime.js +94 -0
- package/dist/store.js +70 -0
- package/dist/update.js +33 -0
- package/dist/version.js +97 -0
- package/package.json +34 -22
- package/runtime/node_modules/@metro-labs/discord/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord/src/accounts.ts +119 -0
- package/runtime/node_modules/@metro-labs/discord/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord/src/format.ts +210 -0
- package/runtime/node_modules/@metro-labs/discord/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/package.json +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/inbound.ts +367 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +311 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +335 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-serve.ts +124 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-session.ts +278 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +322 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair.ts +20 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/collection-api.ts +202 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-api.ts +338 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +31 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth-routes.ts +157 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth.ts +127 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +333 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/events.ts +117 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +164 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +208 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/http.ts +357 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +79 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-client.ts +162 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-discovery.ts +120 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +58 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/paths.ts +134 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +214 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/protocol.ts +224 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +125 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-pair.ts +18 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +105 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +41 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +218 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor.ts +323 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +169 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-api.ts +295 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +215 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-map.ts +71 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +37 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-collections.ts +218 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-config.ts +147 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +329 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/ids.ts +19 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/key-map.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +364 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +300 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +31 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +105 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +131 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +18 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/accounts.ts +145 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/call-tools.ts +203 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/index.ts +192 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +73 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session.ts +211 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-dispatch.ts +212 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-schemas.ts +397 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +240 -0
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +80 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-resolve.ts +200 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach.ts +182 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/lines.ts +82 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/registry.ts +35 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/types.ts +107 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +172 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +48 -0
- package/runtime/node_modules/@metro-labs/telegram/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/telegram-user/package.json +28 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/accounts.ts +81 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/format.ts +141 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +29 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +67 -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 +318 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +132 -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 +141 -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 +113 -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 +333 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +78 -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 +39 -0
- package/runtime/runtime.json +3 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -78
- package/dist/channels/discord.js +0 -148
- package/dist/channels/telegram.js +0 -137
- package/dist/lib/address.js +0 -21
- package/dist/lib/codex-rc.js +0 -274
- package/dist/lib/dotenv.js +0 -31
- package/dist/log.js +0 -12
- package/dist/paths.js +0 -45
- package/dist/tail.js +0 -161
- package/skills/metro/SKILL.md +0 -122
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { errMsg, log } from './log.js';
|
|
3
|
+
import { publicBaseOrDefault } from './attach-serve.js';
|
|
4
|
+
import {
|
|
5
|
+
apiFailure,
|
|
6
|
+
apiSession,
|
|
7
|
+
projectParam,
|
|
8
|
+
cors,
|
|
9
|
+
readJsonBody,
|
|
10
|
+
sendJson,
|
|
11
|
+
type ApiSession,
|
|
12
|
+
} from './api-http.js';
|
|
13
|
+
import {
|
|
14
|
+
accountRoute,
|
|
15
|
+
accountRouteAllows,
|
|
16
|
+
ATTACHABLE,
|
|
17
|
+
handleAccountRoute,
|
|
18
|
+
type AccountApiDeps,
|
|
19
|
+
type AccountRoute,
|
|
20
|
+
} from './account-api.js';
|
|
21
|
+
import {
|
|
22
|
+
parseAgentId,
|
|
23
|
+
type AgentSummary,
|
|
24
|
+
type CreatedAgent,
|
|
25
|
+
type DeletedAgent,
|
|
26
|
+
type ResetAgentKey,
|
|
27
|
+
} from '../db/agent-admin.js';
|
|
28
|
+
|
|
29
|
+
const PREFIX = '/api/agents';
|
|
30
|
+
const SERVER_NAME = 'metro';
|
|
31
|
+
|
|
32
|
+
export interface AgentApiDeps extends AccountApiDeps {
|
|
33
|
+
listAgents: (email: string, project: string) => Promise<AgentSummary[]>;
|
|
34
|
+
createAgent: (
|
|
35
|
+
email: string,
|
|
36
|
+
project: string,
|
|
37
|
+
name: string,
|
|
38
|
+
) => Promise<CreatedAgent>;
|
|
39
|
+
deleteAgent: (
|
|
40
|
+
email: string,
|
|
41
|
+
id: string,
|
|
42
|
+
) => Promise<DeletedAgent>;
|
|
43
|
+
resetKey: (
|
|
44
|
+
email: 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
|
+
liveness: () => Map<string, { connected: boolean; lastSeenAt: number }>;
|
|
53
|
+
mintRuntimeCode: (
|
|
54
|
+
email: string,
|
|
55
|
+
agentId: string,
|
|
56
|
+
) => Promise<{ code: string; expiresAt: number }>;
|
|
57
|
+
releaseRuntime: (email: string, agentId: string) => Promise<void>;
|
|
58
|
+
runtimes: (agentIds: string[]) => Promise<Map<string, string>>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type Routable =
|
|
62
|
+
| { kind: 'collection' }
|
|
63
|
+
| { kind: 'agent'; id: string }
|
|
64
|
+
| { kind: 'key'; id: string }
|
|
65
|
+
| { kind: 'runtime'; id: string }
|
|
66
|
+
| { kind: 'accounts'; id: string; route: AccountRoute };
|
|
67
|
+
|
|
68
|
+
type Target = Routable | { kind: 'unknown' } | null;
|
|
69
|
+
|
|
70
|
+
function subTarget(id: string, rest: string[]): Target {
|
|
71
|
+
if (rest.length === 0) return { kind: 'agent', id };
|
|
72
|
+
if (rest.length === 1 && rest[0] === 'key') return { kind: 'key', id };
|
|
73
|
+
if (rest.length === 1 && rest[0] === 'runtime')
|
|
74
|
+
return { kind: 'runtime', id };
|
|
75
|
+
if (rest[0] !== 'accounts') return { kind: 'unknown' };
|
|
76
|
+
const route = accountRoute(rest.slice(1));
|
|
77
|
+
return route === null ? { kind: 'unknown' } : { kind: 'accounts', id, route };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function target(path: string): Target {
|
|
81
|
+
if (path === PREFIX || path === `${PREFIX}/`) return { kind: 'collection' };
|
|
82
|
+
if (!path.startsWith(`${PREFIX}/`)) return null;
|
|
83
|
+
const segments = path.slice(PREFIX.length + 1).split('/').filter(Boolean);
|
|
84
|
+
const head = segments[0];
|
|
85
|
+
if (head === undefined) return { kind: 'collection' };
|
|
86
|
+
const id = parseAgentId(head);
|
|
87
|
+
return id === null ? { kind: 'unknown' } : subTarget(id, segments.slice(1));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function mcpEndpoint(): string {
|
|
91
|
+
return `${publicBaseOrDefault()}/mcp`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function mcpAddCommand(key: string): string {
|
|
95
|
+
const url = `${mcpEndpoint()}?token=${key}`;
|
|
96
|
+
return `claude mcp add --transport http ${SERVER_NAME} "${url}"`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface KeyPayload {
|
|
100
|
+
key: string | null;
|
|
101
|
+
endpoint: string | null;
|
|
102
|
+
command: string | null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function credentials(key: string): KeyPayload {
|
|
106
|
+
return {
|
|
107
|
+
key,
|
|
108
|
+
endpoint: `${mcpEndpoint()}?token=${key}`,
|
|
109
|
+
command: mcpAddCommand(key),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function keyPayload(agent: AgentSummary): KeyPayload {
|
|
114
|
+
const value = agent.owned ? agent.key : null;
|
|
115
|
+
if (value === null) return { key: null, endpoint: null, command: null };
|
|
116
|
+
return credentials(value);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function livenessPayload(
|
|
120
|
+
agent: AgentSummary,
|
|
121
|
+
live: Map<string, { connected: boolean; lastSeenAt: number }>,
|
|
122
|
+
): Record<string, unknown> {
|
|
123
|
+
const found = agent.owned ? live.get(agent.id) : undefined;
|
|
124
|
+
if (found === undefined) return { connected: false, last_seen: null };
|
|
125
|
+
return {
|
|
126
|
+
connected: found.connected,
|
|
127
|
+
last_seen: new Date(found.lastSeenAt).toISOString(),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function agentPayload(
|
|
132
|
+
agent: AgentSummary,
|
|
133
|
+
live: Map<string, { connected: boolean; lastSeenAt: number }>,
|
|
134
|
+
runtimes: Map<string, string>,
|
|
135
|
+
): Record<string, unknown> {
|
|
136
|
+
return {
|
|
137
|
+
id: agent.id,
|
|
138
|
+
name: agent.name,
|
|
139
|
+
owned: agent.owned,
|
|
140
|
+
runtime: agent.owned ? (runtimes.get(agent.id) ?? null) : null,
|
|
141
|
+
...livenessPayload(agent, live),
|
|
142
|
+
...keyPayload(agent),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function wantsAccounts(req: IncomingMessage): boolean {
|
|
147
|
+
const query = (req.url ?? '').split('?')[1] ?? '';
|
|
148
|
+
return new URLSearchParams(query).get('accounts') === '1';
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function handleList(
|
|
152
|
+
req: IncomingMessage,
|
|
153
|
+
res: ServerResponse,
|
|
154
|
+
deps: AgentApiDeps,
|
|
155
|
+
session: ApiSession,
|
|
156
|
+
): Promise<void> {
|
|
157
|
+
const project = projectParam(req);
|
|
158
|
+
if (project === null) {
|
|
159
|
+
sendJson(req, res, 400, { error: 'a project is required' });
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const list = await deps.listAgents(session.email, project);
|
|
163
|
+
const live = deps.liveness();
|
|
164
|
+
const held = await deps.runtimes(
|
|
165
|
+
list.filter((a) => a.owned).map((a) => a.id),
|
|
166
|
+
);
|
|
167
|
+
const base = {
|
|
168
|
+
email: session.email,
|
|
169
|
+
endpoint: mcpEndpoint(),
|
|
170
|
+
agents: list.map((a) => agentPayload(a, live, held)),
|
|
171
|
+
capabilities: deps.capabilities(),
|
|
172
|
+
attachable: ATTACHABLE,
|
|
173
|
+
};
|
|
174
|
+
if (!wantsAccounts(req)) {
|
|
175
|
+
sendJson(req, res, 200, base);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const { accounts, unavailable } = await deps.gatherAccounts(
|
|
179
|
+
new Set(list.map((a) => a.id)),
|
|
180
|
+
);
|
|
181
|
+
sendJson(req, res, 200, { ...base, accounts, unavailable });
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function handleRuntime(
|
|
185
|
+
req: IncomingMessage,
|
|
186
|
+
res: ServerResponse,
|
|
187
|
+
deps: AgentApiDeps,
|
|
188
|
+
session: ApiSession,
|
|
189
|
+
id: string,
|
|
190
|
+
): Promise<void> {
|
|
191
|
+
if (req.method === 'DELETE') {
|
|
192
|
+
await deps.releaseRuntime(session.email, id);
|
|
193
|
+
log.info({ agent: id }, 'agent-api: runtime released');
|
|
194
|
+
sendJson(req, res, 200, { agent: id, runtime: null });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const minted = await deps.mintRuntimeCode(session.email, id);
|
|
198
|
+
sendJson(req, res, 201, minted);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function handleCreate(
|
|
202
|
+
req: IncomingMessage,
|
|
203
|
+
res: ServerResponse,
|
|
204
|
+
deps: AgentApiDeps,
|
|
205
|
+
session: ApiSession,
|
|
206
|
+
): Promise<void> {
|
|
207
|
+
const body = await readJsonBody(req);
|
|
208
|
+
const name = (body as { name?: unknown }).name;
|
|
209
|
+
const project = projectParam(req);
|
|
210
|
+
if (project === null) {
|
|
211
|
+
sendJson(req, res, 400, { error: 'a project is required' });
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const created = await deps.createAgent(session.email, project, name as string);
|
|
215
|
+
log.info(
|
|
216
|
+
{ agent: created.name, id: created.id, owner: session.email },
|
|
217
|
+
'agent-api: created agent',
|
|
218
|
+
);
|
|
219
|
+
sendJson(req, res, 201, {
|
|
220
|
+
id: created.id,
|
|
221
|
+
name: created.name,
|
|
222
|
+
...credentials(created.key),
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function handleResetKey(
|
|
227
|
+
req: IncomingMessage,
|
|
228
|
+
res: ServerResponse,
|
|
229
|
+
deps: AgentApiDeps,
|
|
230
|
+
session: ApiSession,
|
|
231
|
+
id: string,
|
|
232
|
+
): Promise<void> {
|
|
233
|
+
const reset = await deps.resetKey(session.email, id);
|
|
234
|
+
log.info(
|
|
235
|
+
{ agent: reset.name, id: reset.id, owner: session.email },
|
|
236
|
+
'agent-api: reset agent key',
|
|
237
|
+
);
|
|
238
|
+
sendJson(req, res, 200, {
|
|
239
|
+
id: reset.id,
|
|
240
|
+
name: reset.name,
|
|
241
|
+
reset: true,
|
|
242
|
+
...credentials(reset.key),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function handleDelete(
|
|
247
|
+
req: IncomingMessage,
|
|
248
|
+
res: ServerResponse,
|
|
249
|
+
deps: AgentApiDeps,
|
|
250
|
+
session: ApiSession,
|
|
251
|
+
id: string,
|
|
252
|
+
): Promise<void> {
|
|
253
|
+
const gone = await deps.deleteAgent(session.email, id);
|
|
254
|
+
log.info(
|
|
255
|
+
{ agent: gone.name, id: gone.id, owner: session.email },
|
|
256
|
+
'agent-api: deleted agent',
|
|
257
|
+
);
|
|
258
|
+
sendJson(req, res, 200, { id: gone.id, name: gone.name, deleted: true });
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
type AgentTarget = Exclude<Routable, { kind: 'accounts' }>;
|
|
262
|
+
|
|
263
|
+
async function routeAgent(
|
|
264
|
+
req: IncomingMessage,
|
|
265
|
+
res: ServerResponse,
|
|
266
|
+
deps: AgentApiDeps,
|
|
267
|
+
session: ApiSession,
|
|
268
|
+
tgt: AgentTarget,
|
|
269
|
+
): Promise<void> {
|
|
270
|
+
try {
|
|
271
|
+
if (tgt.kind === 'key') await handleResetKey(req, res, deps, session, tgt.id);
|
|
272
|
+
else if (tgt.kind === 'runtime')
|
|
273
|
+
await handleRuntime(req, res, deps, session, tgt.id);
|
|
274
|
+
else if (tgt.kind === 'agent')
|
|
275
|
+
await handleDelete(req, res, deps, session, tgt.id);
|
|
276
|
+
else if (req.method === 'GET') await handleList(req, res, deps, session);
|
|
277
|
+
else await handleCreate(req, res, deps, session);
|
|
278
|
+
} catch (err) {
|
|
279
|
+
apiFailure(req, res, err);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const ALLOWED: Record<AgentTarget['kind'], string[]> = {
|
|
284
|
+
collection: ['GET', 'POST'],
|
|
285
|
+
agent: ['DELETE'],
|
|
286
|
+
key: ['POST'],
|
|
287
|
+
runtime: ['POST', 'DELETE'],
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
function methodAllowed(tgt: Routable, method: string | undefined): boolean {
|
|
291
|
+
if (tgt.kind === 'accounts') return accountRouteAllows(tgt.route, method);
|
|
292
|
+
return ALLOWED[tgt.kind].includes(method ?? '');
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function dispatch(
|
|
296
|
+
req: IncomingMessage,
|
|
297
|
+
res: ServerResponse,
|
|
298
|
+
deps: AgentApiDeps,
|
|
299
|
+
tgt: Routable,
|
|
300
|
+
): Promise<void> {
|
|
301
|
+
const session = apiSession(req);
|
|
302
|
+
if (!session) {
|
|
303
|
+
sendJson(req, res, 401, { error: 'unauthorized' });
|
|
304
|
+
return Promise.resolve();
|
|
305
|
+
}
|
|
306
|
+
if (tgt.kind === 'accounts')
|
|
307
|
+
return handleAccountRoute(req, res, deps, session, tgt.id, tgt.route);
|
|
308
|
+
return routeAgent(req, res, deps, session, tgt);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function handleAgentApiRequest(
|
|
312
|
+
req: IncomingMessage,
|
|
313
|
+
res: ServerResponse,
|
|
314
|
+
deps: AgentApiDeps,
|
|
315
|
+
): boolean {
|
|
316
|
+
const tgt = target((req.url ?? '').split('?')[0] ?? '');
|
|
317
|
+
if (tgt === null) return false;
|
|
318
|
+
if (req.method === 'OPTIONS') {
|
|
319
|
+
res.writeHead(204, cors(req)).end();
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
if (tgt.kind === 'unknown') {
|
|
323
|
+
sendJson(req, res, 404, { error: 'no such agent' });
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
if (!methodAllowed(tgt, req.method)) {
|
|
327
|
+
sendJson(req, res, 405, { error: 'method not allowed' });
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
dispatch(req, res, deps, tgt).catch((err: unknown) => {
|
|
331
|
+
log.warn({ err: errMsg(err) }, 'agent-api: unhandled error');
|
|
332
|
+
if (!res.headersSent) sendJson(req, res, 500, { error: 'agent api failed' });
|
|
333
|
+
});
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { ApiError } from './api-error.js';
|
|
3
|
+
import { errMsg, log } from './log.js';
|
|
4
|
+
import {
|
|
5
|
+
verifyCliToken,
|
|
6
|
+
verifyRunToken,
|
|
7
|
+
verifySession,
|
|
8
|
+
type CliClaims,
|
|
9
|
+
type RunClaims,
|
|
10
|
+
} from './session.js';
|
|
11
|
+
import { extractToken } from '../mcp/request-identity.js';
|
|
12
|
+
|
|
13
|
+
const BODY_MAX = 4 * 1024;
|
|
14
|
+
const DRAIN_MAX = 2 * BODY_MAX;
|
|
15
|
+
|
|
16
|
+
export interface ApiSession {
|
|
17
|
+
email: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function cors(req: IncomingMessage): Record<string, string> {
|
|
21
|
+
return {
|
|
22
|
+
'access-control-allow-origin': req.headers.origin ?? '*',
|
|
23
|
+
'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
|
24
|
+
'access-control-allow-headers': 'Authorization, Content-Type',
|
|
25
|
+
'access-control-max-age': '86400',
|
|
26
|
+
vary: 'Origin',
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function sendJson(
|
|
31
|
+
req: IncomingMessage,
|
|
32
|
+
res: ServerResponse,
|
|
33
|
+
status: number,
|
|
34
|
+
body: unknown,
|
|
35
|
+
): void {
|
|
36
|
+
res.writeHead(status, {
|
|
37
|
+
'content-type': 'application/json',
|
|
38
|
+
'cache-control': 'no-store',
|
|
39
|
+
...cors(req),
|
|
40
|
+
});
|
|
41
|
+
res.end(JSON.stringify(body));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function apiSession(req: IncomingMessage): ApiSession | null {
|
|
45
|
+
const secret = process.env.METRO_SESSION_SECRET?.trim() ?? '';
|
|
46
|
+
if (secret === '') return null;
|
|
47
|
+
const token = extractToken(req) ?? '';
|
|
48
|
+
if (token === '') return null;
|
|
49
|
+
try {
|
|
50
|
+
const { email } = verifySession(token, secret);
|
|
51
|
+
return { email: email.toLowerCase() };
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function runIdentity(req: IncomingMessage): RunClaims | null {
|
|
58
|
+
const secret = process.env.METRO_SESSION_SECRET?.trim() ?? '';
|
|
59
|
+
if (secret === '') return null;
|
|
60
|
+
const token = extractToken(req) ?? '';
|
|
61
|
+
if (token === '') return null;
|
|
62
|
+
try {
|
|
63
|
+
const claims = verifyRunToken(token, secret);
|
|
64
|
+
return { ...claims, email: claims.email.toLowerCase() };
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function cliIdentity(req: IncomingMessage): CliClaims | null {
|
|
71
|
+
const secret = process.env.METRO_SESSION_SECRET?.trim() ?? '';
|
|
72
|
+
if (secret === '') return null;
|
|
73
|
+
const token = extractToken(req) ?? '';
|
|
74
|
+
if (token === '') return null;
|
|
75
|
+
try {
|
|
76
|
+
const { email, collectionId } = verifyCliToken(token, secret);
|
|
77
|
+
return { email: email.toLowerCase(), collectionId };
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function projectParam(req: IncomingMessage): string | null {
|
|
84
|
+
const raw = new URL(req.url ?? '/', 'http://localhost').searchParams.get(
|
|
85
|
+
'project',
|
|
86
|
+
);
|
|
87
|
+
return raw === null || raw === '' ? null : raw;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function readJsonBody(req: IncomingMessage): Promise<unknown> {
|
|
91
|
+
const chunks: Buffer[] = [];
|
|
92
|
+
let total = 0;
|
|
93
|
+
let over = false;
|
|
94
|
+
for await (const c of req) {
|
|
95
|
+
const buf = c as Buffer;
|
|
96
|
+
total += buf.length;
|
|
97
|
+
if (total > BODY_MAX) over = true;
|
|
98
|
+
if (!over) chunks.push(buf);
|
|
99
|
+
else if (total > DRAIN_MAX) break;
|
|
100
|
+
}
|
|
101
|
+
if (over) throw new ApiError('request body too large', 413);
|
|
102
|
+
const raw = Buffer.concat(chunks).toString('utf8').trim();
|
|
103
|
+
if (raw === '') return {};
|
|
104
|
+
try {
|
|
105
|
+
return JSON.parse(raw);
|
|
106
|
+
} catch {
|
|
107
|
+
throw new ApiError('body must be JSON', 400);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function apiFailure(
|
|
112
|
+
req: IncomingMessage,
|
|
113
|
+
res: ServerResponse,
|
|
114
|
+
err: unknown,
|
|
115
|
+
label = 'agent-api',
|
|
116
|
+
): void {
|
|
117
|
+
if (err instanceof ApiError) {
|
|
118
|
+
sendJson(req, res, err.status, { error: err.message });
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
log.warn({ err: errMsg(err) }, `${label}: request failed`);
|
|
122
|
+
if (!res.headersSent) sendJson(req, res, 500, { error: `${label} failed` });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function bodyField(body: unknown, key: string): unknown {
|
|
126
|
+
return typeof body === 'object' && body !== null && !Array.isArray(body)
|
|
127
|
+
? (body as Record<string, unknown>)[key]
|
|
128
|
+
: undefined;
|
|
129
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { ID_RE } from '../db/ids.js';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { resolveCachedAttachment } from '../stations/attachments.js';
|
|
5
|
+
import { writeSecure } from './secure-fs.js';
|
|
6
|
+
|
|
7
|
+
const GRANT_SUFFIX = '.grant';
|
|
8
|
+
|
|
9
|
+
export interface AttachmentGrant {
|
|
10
|
+
token: string;
|
|
11
|
+
agentId: string;
|
|
12
|
+
mintedAt: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function newAttachmentToken(prefix = 'at'): string {
|
|
16
|
+
return `${prefix}_${randomBytes(32).toString('base64url')}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function parseGrant(raw: string): AttachmentGrant | undefined {
|
|
20
|
+
let parsed: unknown;
|
|
21
|
+
try {
|
|
22
|
+
parsed = JSON.parse(raw);
|
|
23
|
+
} catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
if (typeof parsed !== 'object' || parsed === null) return undefined;
|
|
27
|
+
const { token, agentId, mintedAt } = parsed as Record<string, unknown>;
|
|
28
|
+
if (typeof token !== 'string' || token === '') return undefined;
|
|
29
|
+
if (typeof agentId !== 'string' || !ID_RE.test(agentId)) return undefined;
|
|
30
|
+
return {
|
|
31
|
+
token,
|
|
32
|
+
agentId,
|
|
33
|
+
mintedAt: typeof mintedAt === 'number' ? mintedAt : 0,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function readGrant(path: string): AttachmentGrant | undefined {
|
|
38
|
+
try {
|
|
39
|
+
return parseGrant(readFileSync(`${path}${GRANT_SUFFIX}`, 'utf8'));
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function issueGrant(
|
|
46
|
+
path: string,
|
|
47
|
+
agentId: string,
|
|
48
|
+
prefix?: string,
|
|
49
|
+
): string {
|
|
50
|
+
const existing = readGrant(path);
|
|
51
|
+
if (existing?.agentId === agentId) return existing.token;
|
|
52
|
+
const grant: AttachmentGrant = {
|
|
53
|
+
token: newAttachmentToken(prefix),
|
|
54
|
+
agentId,
|
|
55
|
+
mintedAt: Date.now(),
|
|
56
|
+
};
|
|
57
|
+
writeSecure(`${path}${GRANT_SUFFIX}`, `${JSON.stringify(grant)}\n`);
|
|
58
|
+
return grant.token;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function sameToken(presented: string, stored: string): boolean {
|
|
62
|
+
const a = Buffer.from(presented, 'utf8');
|
|
63
|
+
const b = Buffer.from(stored, 'utf8');
|
|
64
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function grantAllowsPath(
|
|
68
|
+
path: string,
|
|
69
|
+
owner: string,
|
|
70
|
+
presented: string,
|
|
71
|
+
): boolean {
|
|
72
|
+
if (presented === '') return false;
|
|
73
|
+
const grant = readGrant(path);
|
|
74
|
+
if (grant === undefined) return false;
|
|
75
|
+
if (grant.agentId !== owner) return false;
|
|
76
|
+
return sameToken(presented, grant.token);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function readAttachmentGrant(name: string): AttachmentGrant | undefined {
|
|
80
|
+
const path = resolveCachedAttachment(name);
|
|
81
|
+
return path === null ? undefined : readGrant(path);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function issueAttachmentGrant(
|
|
85
|
+
name: string,
|
|
86
|
+
agentId: string,
|
|
87
|
+
): string | undefined {
|
|
88
|
+
const path = resolveCachedAttachment(name);
|
|
89
|
+
return path === null ? undefined : issueGrant(path, agentId);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function grantAllows(
|
|
93
|
+
name: string,
|
|
94
|
+
owner: string,
|
|
95
|
+
presented: string,
|
|
96
|
+
): boolean {
|
|
97
|
+
const path = resolveCachedAttachment(name);
|
|
98
|
+
return path === null ? false : grantAllowsPath(path, owner, presented);
|
|
99
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { ID_RE } from '../db/ids.js';
|
|
3
|
+
import { resolveCachedAttachment } from '../stations/attachments.js';
|
|
4
|
+
import { writeSecure } from './secure-fs.js';
|
|
5
|
+
|
|
6
|
+
const OWNER_SUFFIX = '.owner';
|
|
7
|
+
|
|
8
|
+
export function recordOwner(path: string, agentId: string): void {
|
|
9
|
+
writeSecure(`${path}${OWNER_SUFFIX}`, `${agentId}\n`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function ownerOf(path: string): string | undefined {
|
|
13
|
+
let raw: string;
|
|
14
|
+
try {
|
|
15
|
+
raw = readFileSync(`${path}${OWNER_SUFFIX}`, 'utf8');
|
|
16
|
+
} catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const agentId = raw.trim();
|
|
20
|
+
return ID_RE.test(agentId) ? agentId : undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function recordAttachmentOwner(name: string, agentId: string): void {
|
|
24
|
+
const path = resolveCachedAttachment(name);
|
|
25
|
+
if (path === null) return;
|
|
26
|
+
recordOwner(path, agentId);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function attachmentOwner(name: string): string | undefined {
|
|
30
|
+
const path = resolveCachedAttachment(name);
|
|
31
|
+
return path === null ? undefined : ownerOf(path);
|
|
32
|
+
}
|