@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,91 @@
|
|
|
1
|
+
import { isUniqueViolation } from './users.js';
|
|
2
|
+
import { and, eq, sql } from 'drizzle-orm';
|
|
3
|
+
import { newId } from './ids.js';
|
|
4
|
+
import { getDb } from './client.js';
|
|
5
|
+
import { AgentAdminError, ownedAgentOrThrow, } from './agent-admin.js';
|
|
6
|
+
import { stations, STATIONS, type StationName } from './schema.js';
|
|
7
|
+
|
|
8
|
+
const ACCOUNT_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
9
|
+
const ID_ATTEMPTS = 5;
|
|
10
|
+
|
|
11
|
+
export interface AccountRef {
|
|
12
|
+
agentId: string;
|
|
13
|
+
station: StationName;
|
|
14
|
+
accountId: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isStationName(raw: unknown): raw is StationName {
|
|
18
|
+
return (
|
|
19
|
+
typeof raw === 'string' && (STATIONS as readonly string[]).includes(raw)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function parseAccountId(raw: string): string | null {
|
|
24
|
+
return ACCOUNT_ID_RE.test(raw) ? raw : null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function assertTokenFree(
|
|
28
|
+
station: StationName,
|
|
29
|
+
token: string,
|
|
30
|
+
): Promise<void> {
|
|
31
|
+
const rows = await getDb()
|
|
32
|
+
.select({ id: stations.id })
|
|
33
|
+
.from(stations)
|
|
34
|
+
.where(
|
|
35
|
+
and(
|
|
36
|
+
eq(stations.station, station),
|
|
37
|
+
sql`${stations.config}->>'token' = ${token}`,
|
|
38
|
+
),
|
|
39
|
+
);
|
|
40
|
+
if (rows.length > 0)
|
|
41
|
+
throw new AgentAdminError(
|
|
42
|
+
'that bot token is already attached to a Metro account',
|
|
43
|
+
409,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function attachAccountToAgent(
|
|
48
|
+
email: string,
|
|
49
|
+
agentId: string,
|
|
50
|
+
station: StationName,
|
|
51
|
+
config: Record<string, unknown>,
|
|
52
|
+
): Promise<AccountRef> {
|
|
53
|
+
const { agent } = await ownedAgentOrThrow(email, agentId);
|
|
54
|
+
const token = config.token;
|
|
55
|
+
if (typeof token === 'string') await assertTokenFree(station, token);
|
|
56
|
+
const db = getDb();
|
|
57
|
+
for (let attempt = 0; attempt < ID_ATTEMPTS; attempt++) {
|
|
58
|
+
const accountId = newId();
|
|
59
|
+
try {
|
|
60
|
+
await db
|
|
61
|
+
.insert(stations)
|
|
62
|
+
.values({ id: accountId, agentId: agent.id, station, config });
|
|
63
|
+
return { agentId: agent.id, station, accountId };
|
|
64
|
+
} catch (err) {
|
|
65
|
+
if (!isUniqueViolation(err)) throw err;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
throw new AgentAdminError('could not allocate a free account id', 500);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function detachAccountFromAgent(
|
|
72
|
+
email: string,
|
|
73
|
+
agentId: string,
|
|
74
|
+
station: StationName,
|
|
75
|
+
accountId: string,
|
|
76
|
+
): Promise<AccountRef> {
|
|
77
|
+
const { agent } = await ownedAgentOrThrow(email, agentId);
|
|
78
|
+
const gone = await getDb()
|
|
79
|
+
.delete(stations)
|
|
80
|
+
.where(
|
|
81
|
+
and(
|
|
82
|
+
eq(stations.agentId, agent.id),
|
|
83
|
+
eq(stations.station, station),
|
|
84
|
+
eq(stations.id, accountId),
|
|
85
|
+
),
|
|
86
|
+
)
|
|
87
|
+
.returning({ id: stations.id });
|
|
88
|
+
if (gone.length === 0)
|
|
89
|
+
throw new AgentAdminError('no such account on this agent', 404);
|
|
90
|
+
return { agentId: agent.id, station, accountId };
|
|
91
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { and, asc, eq, inArray } from 'drizzle-orm';
|
|
3
|
+
import { ApiError } from '../daemon/api-error.js';
|
|
4
|
+
import { getDb } from './client.js';
|
|
5
|
+
import { registerKey, rotateAgentKey, unregisterAgentKey } from './key-map.js';
|
|
6
|
+
import { newId, parseId } from './ids.js';
|
|
7
|
+
import { agents, stations } from './schema.js';
|
|
8
|
+
import { isUniqueViolation } from './users.js';
|
|
9
|
+
import { projectIdOrThrow } from './projects.js';
|
|
10
|
+
|
|
11
|
+
export const AGENT_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{1,31}$/;
|
|
12
|
+
const KEY_ATTEMPTS = 5;
|
|
13
|
+
|
|
14
|
+
export class AgentAdminError extends ApiError {}
|
|
15
|
+
|
|
16
|
+
export interface AgentSummary {
|
|
17
|
+
id: string;
|
|
18
|
+
name: string;
|
|
19
|
+
owned: boolean;
|
|
20
|
+
key: string | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CreatedAgent {
|
|
24
|
+
id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
key: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface OwnedAgent {
|
|
30
|
+
id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type DeletedAgent = OwnedAgent;
|
|
35
|
+
|
|
36
|
+
export interface ResetAgentKey extends OwnedAgent {
|
|
37
|
+
key: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function normalizeAgentName(raw: unknown): string {
|
|
41
|
+
const name = typeof raw === 'string' ? raw.trim() : '';
|
|
42
|
+
if (!AGENT_NAME_RE.test(name))
|
|
43
|
+
throw new AgentAdminError(
|
|
44
|
+
'name must be 2-32 characters of A-Z, a-z, 0-9, - or _, starting with a letter or digit',
|
|
45
|
+
400,
|
|
46
|
+
);
|
|
47
|
+
return name;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function newApiKey(): string {
|
|
51
|
+
return `mk_${randomBytes(32).toString('base64url')}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const agentPin = (): string => process.env.METRO_AGENT?.trim() ?? '';
|
|
55
|
+
|
|
56
|
+
export function servesEveryAgent(): boolean {
|
|
57
|
+
return agentPin() === '';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function daemonServesAgent(id: string): boolean {
|
|
61
|
+
const pin = agentPin();
|
|
62
|
+
return pin === '' || pin === id;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface AgentRow {
|
|
66
|
+
id: string;
|
|
67
|
+
name: string;
|
|
68
|
+
projectId: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface KeyRow {
|
|
72
|
+
agentId: string;
|
|
73
|
+
key: string | null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function toAgentSummaries(
|
|
77
|
+
rows: AgentRow[],
|
|
78
|
+
keyRows: KeyRow[],
|
|
79
|
+
): AgentSummary[] {
|
|
80
|
+
return rows.map((r) => ({
|
|
81
|
+
id: r.id,
|
|
82
|
+
name: r.name,
|
|
83
|
+
owned: true,
|
|
84
|
+
key: keyRows.find((k) => k.agentId === r.id)?.key ?? null,
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function selectKeyRows(ids: string[]): Promise<KeyRow[]> {
|
|
89
|
+
if (ids.length === 0) return [];
|
|
90
|
+
return getDb()
|
|
91
|
+
.select({ agentId: agents.id, key: agents.key })
|
|
92
|
+
.from(agents)
|
|
93
|
+
.where(inArray(agents.id, ids));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function listAgentsForEmail(
|
|
97
|
+
email: string,
|
|
98
|
+
project: string,
|
|
99
|
+
): Promise<AgentSummary[]> {
|
|
100
|
+
const projectId = await projectIdOrThrow(email, project);
|
|
101
|
+
const rows = await getDb()
|
|
102
|
+
.select({ id: agents.id, name: agents.name, projectId: agents.projectId })
|
|
103
|
+
.from(agents)
|
|
104
|
+
.where(eq(agents.projectId, projectId))
|
|
105
|
+
.orderBy(asc(agents.name), asc(agents.id));
|
|
106
|
+
if (rows.length === 0) return [];
|
|
107
|
+
const keyRows = await selectKeyRows(rows.map((r) => r.id));
|
|
108
|
+
return toAgentSummaries(rows, keyRows);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function insertAgent(
|
|
112
|
+
projectId: string,
|
|
113
|
+
name: string,
|
|
114
|
+
key: string,
|
|
115
|
+
): Promise<string> {
|
|
116
|
+
const inserted = await getDb()
|
|
117
|
+
.insert(agents)
|
|
118
|
+
.values({ id: newId(), name, projectId, key })
|
|
119
|
+
.returning({ id: agents.id });
|
|
120
|
+
const id = inserted[0]?.id;
|
|
121
|
+
if (id === undefined)
|
|
122
|
+
throw new AgentAdminError('agent insert returned no id', 500);
|
|
123
|
+
return id;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function createAgentForEmail(
|
|
127
|
+
email: string,
|
|
128
|
+
project: string,
|
|
129
|
+
rawName: string,
|
|
130
|
+
): Promise<CreatedAgent> {
|
|
131
|
+
const name = normalizeAgentName(rawName);
|
|
132
|
+
const key = newApiKey();
|
|
133
|
+
const id = await insertAgent(await projectIdOrThrow(email, project), name, key);
|
|
134
|
+
if (servesEveryAgent()) registerKey(key, id);
|
|
135
|
+
return { id, name, key };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function parseAgentId(raw: string): string | null {
|
|
139
|
+
return parseId(raw);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
interface Deletable {
|
|
143
|
+
agent: DeletedAgent;
|
|
144
|
+
projectId: string;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export async function ownedAgentOrThrow(
|
|
148
|
+
email: string,
|
|
149
|
+
id: string,
|
|
150
|
+
): Promise<Deletable> {
|
|
151
|
+
const rows = await getDb().select().from(agents).where(eq(agents.id, id));
|
|
152
|
+
const row = rows[0];
|
|
153
|
+
const missing = new AgentAdminError('no such agent', 404);
|
|
154
|
+
if (!row) throw missing;
|
|
155
|
+
try {
|
|
156
|
+
await projectIdOrThrow(email, row.projectId);
|
|
157
|
+
} catch {
|
|
158
|
+
throw missing;
|
|
159
|
+
}
|
|
160
|
+
return { agent: { id: row.id, name: row.name }, projectId: row.projectId };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function writeNewKey(id: string, projectId: string): Promise<string> {
|
|
164
|
+
for (let attempt = 0; attempt < KEY_ATTEMPTS; attempt += 1) {
|
|
165
|
+
const key = newApiKey();
|
|
166
|
+
try {
|
|
167
|
+
const changed = await getDb()
|
|
168
|
+
.update(agents)
|
|
169
|
+
.set({ key })
|
|
170
|
+
.where(and(eq(agents.id, id), eq(agents.projectId, projectId)))
|
|
171
|
+
.returning({ id: agents.id });
|
|
172
|
+
if (changed.length === 0)
|
|
173
|
+
throw new AgentAdminError('no such agent', 404);
|
|
174
|
+
return key;
|
|
175
|
+
} catch (err) {
|
|
176
|
+
if (!isUniqueViolation(err)) throw err;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
throw new AgentAdminError('could not allocate a free api key', 500);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function resetAgentKeyForEmail(
|
|
183
|
+
email: string,
|
|
184
|
+
id: string,
|
|
185
|
+
): Promise<ResetAgentKey> {
|
|
186
|
+
const { agent, projectId } = await ownedAgentOrThrow(email, id);
|
|
187
|
+
const key = await writeNewKey(agent.id, projectId);
|
|
188
|
+
rotateAgentKey(agent.id, daemonServesAgent(agent.id) ? key : null);
|
|
189
|
+
return { id: agent.id, name: agent.name, key };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export async function deleteAgentForEmail(
|
|
193
|
+
email: string,
|
|
194
|
+
id: string,
|
|
195
|
+
): Promise<DeletedAgent> {
|
|
196
|
+
const { agent, projectId } = await ownedAgentOrThrow(email, id);
|
|
197
|
+
await getDb().transaction(async (tx) => {
|
|
198
|
+
const attached = await tx
|
|
199
|
+
.select({ id: stations.id })
|
|
200
|
+
.from(stations)
|
|
201
|
+
.where(eq(stations.agentId, id));
|
|
202
|
+
if (attached.length > 0)
|
|
203
|
+
throw new AgentAdminError(
|
|
204
|
+
`agent '${agent.name}' still has ${attached.length} station account(s) attached — an operator must remove them first`,
|
|
205
|
+
409,
|
|
206
|
+
);
|
|
207
|
+
const gone = await tx
|
|
208
|
+
.delete(agents)
|
|
209
|
+
.where(and(eq(agents.id, id), eq(agents.projectId, projectId)))
|
|
210
|
+
.returning({ id: agents.id });
|
|
211
|
+
if (gone.length === 0) throw new AgentAdminError('no such agent', 404);
|
|
212
|
+
});
|
|
213
|
+
unregisterAgentKey(id);
|
|
214
|
+
return agent;
|
|
215
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export type AgentMap = Record<string, string>;
|
|
2
|
+
export type AgentNameMap = Record<string, string>;
|
|
3
|
+
export type AllowlistMap = Record<string, string[]>;
|
|
4
|
+
|
|
5
|
+
const mapKey = (station: string, accountId: string): string =>
|
|
6
|
+
`${station}/${accountId}`;
|
|
7
|
+
|
|
8
|
+
let agentMap: AgentMap = {};
|
|
9
|
+
let agentNames: AgentNameMap = {};
|
|
10
|
+
let allowlistMap: AllowlistMap = {};
|
|
11
|
+
|
|
12
|
+
export function setAgentMap(map: AgentMap, names: AgentNameMap): void {
|
|
13
|
+
agentMap = map;
|
|
14
|
+
agentNames = names;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function setAllowlistMap(map: AllowlistMap): void {
|
|
18
|
+
allowlistMap = map;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function accountFromLine(
|
|
22
|
+
line: string,
|
|
23
|
+
): { station: string; accountId: string } | undefined {
|
|
24
|
+
const parts = line.split('/');
|
|
25
|
+
const station = parts[2];
|
|
26
|
+
const accountId = parts[3];
|
|
27
|
+
if (!station || !accountId) return undefined;
|
|
28
|
+
return { station, accountId };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function agentIdForAccount(
|
|
32
|
+
station: string,
|
|
33
|
+
accountId: string,
|
|
34
|
+
): string | undefined {
|
|
35
|
+
return agentMap[mapKey(station, accountId)];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function agentIdForLine(line: string): string | undefined {
|
|
39
|
+
const a = accountFromLine(line);
|
|
40
|
+
return a ? agentIdForAccount(a.station, a.accountId) : undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function agentForLine(line: string): string | undefined {
|
|
44
|
+
const id = agentIdForLine(line);
|
|
45
|
+
return id === undefined ? undefined : agentNames[id];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function stationAgentIds(station: string): string[] {
|
|
49
|
+
const prefix = `${station}/`;
|
|
50
|
+
return Object.entries(agentMap)
|
|
51
|
+
.filter(([key]) => key.startsWith(prefix))
|
|
52
|
+
.map(([, id]) => id);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function allowlistForLine(line: string): string[] | undefined {
|
|
56
|
+
const a = accountFromLine(line);
|
|
57
|
+
return a ? allowlistMap[mapKey(a.station, a.accountId)] : undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function senderMatchesAllowlist(
|
|
61
|
+
allowlist: string[],
|
|
62
|
+
from: string,
|
|
63
|
+
): boolean {
|
|
64
|
+
if (allowlist.length === 0 || allowlist.includes('*')) return true;
|
|
65
|
+
const f = (from ?? '').toLowerCase();
|
|
66
|
+
const id = f.split('/').pop() ?? f;
|
|
67
|
+
return allowlist.some((a) => {
|
|
68
|
+
const v = a.toLowerCase();
|
|
69
|
+
return v === f || v === id;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
accountFromLine,
|
|
3
|
+
agentIdForAccount,
|
|
4
|
+
stationAgentIds,
|
|
5
|
+
} from './agent-map.js';
|
|
6
|
+
import { STATIONS } from './schema.js';
|
|
7
|
+
|
|
8
|
+
const ACCOUNT_STATIONS = new Set<string>(STATIONS);
|
|
9
|
+
|
|
10
|
+
const stationHasAccounts = (station: string): boolean =>
|
|
11
|
+
ACCOUNT_STATIONS.has(station);
|
|
12
|
+
|
|
13
|
+
const accountInScope = (
|
|
14
|
+
allowed: Set<string>,
|
|
15
|
+
station: string,
|
|
16
|
+
accountId: string,
|
|
17
|
+
): boolean => {
|
|
18
|
+
const agentId = agentIdForAccount(station, accountId);
|
|
19
|
+
return agentId !== undefined && allowed.has(agentId);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function lineTargetDenied(
|
|
23
|
+
allowed: Set<string>,
|
|
24
|
+
args: Record<string, unknown>,
|
|
25
|
+
station?: string,
|
|
26
|
+
): boolean {
|
|
27
|
+
const line = typeof args.line === 'string' ? args.line : undefined;
|
|
28
|
+
if (line === undefined) return false;
|
|
29
|
+
const acct = accountFromLine(line);
|
|
30
|
+
if (!acct) return true;
|
|
31
|
+
if (station !== undefined && acct.station !== station) return true;
|
|
32
|
+
if (!accountInScope(allowed, acct.station, acct.accountId)) return true;
|
|
33
|
+
const override = typeof args.account === 'string' ? args.account : undefined;
|
|
34
|
+
return override !== undefined
|
|
35
|
+
? !accountInScope(allowed, acct.station, override)
|
|
36
|
+
: false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function stationFullyScoped(
|
|
40
|
+
allowed: Set<string>,
|
|
41
|
+
station: string,
|
|
42
|
+
): boolean {
|
|
43
|
+
const ids = stationAgentIds(station);
|
|
44
|
+
return ids.length > 0 && ids.every((id) => allowed.has(id));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function callTargetDenied(
|
|
48
|
+
allowed: Set<string>,
|
|
49
|
+
station: string,
|
|
50
|
+
args: Record<string, unknown>,
|
|
51
|
+
): boolean {
|
|
52
|
+
if (typeof args.line === 'string')
|
|
53
|
+
return lineTargetDenied(allowed, args, station);
|
|
54
|
+
if (typeof args.account === 'string')
|
|
55
|
+
return !accountInScope(allowed, station, args.account);
|
|
56
|
+
return !stationFullyScoped(allowed, station);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function eventInScope(allowed: Set<string>, line: string): boolean {
|
|
60
|
+
const acct = accountFromLine(line);
|
|
61
|
+
if (!acct) return false;
|
|
62
|
+
if (!stationHasAccounts(acct.station)) return true;
|
|
63
|
+
return accountInScope(allowed, acct.station, acct.accountId);
|
|
64
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { drizzle, type PostgresJsDatabase } from 'drizzle-orm/postgres-js';
|
|
2
|
+
import postgres from 'postgres';
|
|
3
|
+
import { log } from '../daemon/log.js';
|
|
4
|
+
import * as schema from './schema.js';
|
|
5
|
+
|
|
6
|
+
export type Db = PostgresJsDatabase<typeof schema>;
|
|
7
|
+
|
|
8
|
+
let sql: ReturnType<typeof postgres> | null = null;
|
|
9
|
+
let db: Db | null = null;
|
|
10
|
+
|
|
11
|
+
export function databaseUrl(): string | undefined {
|
|
12
|
+
const url = process.env.DATABASE_URL?.trim();
|
|
13
|
+
if (!url) return undefined;
|
|
14
|
+
return url;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getDb(): Db {
|
|
18
|
+
const url = databaseUrl();
|
|
19
|
+
if (!url) throw new Error('DATABASE_URL is not set');
|
|
20
|
+
if (!db) {
|
|
21
|
+
sql = postgres(url, {
|
|
22
|
+
max: 4,
|
|
23
|
+
prepare: false,
|
|
24
|
+
onnotice: (notice) => {
|
|
25
|
+
log.debug({ notice: notice.message }, 'postgres notice');
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
db = drizzle(sql, { schema });
|
|
29
|
+
}
|
|
30
|
+
return db;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function closeDb(): Promise<void> {
|
|
34
|
+
if (sql) await sql.end({ timeout: 5 });
|
|
35
|
+
sql = null;
|
|
36
|
+
db = null;
|
|
37
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { isUniqueViolation } from './users.js';
|
|
2
|
+
import { projectIdOrThrow } from './projects.js';
|
|
3
|
+
import { and, asc, eq, inArray, ne } from 'drizzle-orm';
|
|
4
|
+
import { getDb } from './client.js';
|
|
5
|
+
import { newId } from './ids.js';
|
|
6
|
+
import { ConnectorError, connectorName } from './connector-config.js';
|
|
7
|
+
import { collectionItems, collections, connectors } from './schema.js';
|
|
8
|
+
|
|
9
|
+
export interface ConnectorCollectionRow {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
connectorIds: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface CollectionRow {
|
|
16
|
+
id: string;
|
|
17
|
+
projectId: string;
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const missing = (): ConnectorError =>
|
|
22
|
+
new ConnectorError('no such collection', 404);
|
|
23
|
+
|
|
24
|
+
const duplicate = (name: string): ConnectorError =>
|
|
25
|
+
new ConnectorError(`you already have a collection named '${name}'`, 409);
|
|
26
|
+
|
|
27
|
+
const nameClash = (name: string, collection: string): ConnectorError =>
|
|
28
|
+
new ConnectorError(
|
|
29
|
+
`the collection '${collection}' already has a connector named '${name}'`,
|
|
30
|
+
409,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
async function collidingCollection(
|
|
34
|
+
collectionIds: string[],
|
|
35
|
+
name: string,
|
|
36
|
+
exceptConnectorId: string,
|
|
37
|
+
): Promise<string | null> {
|
|
38
|
+
if (collectionIds.length === 0) return null;
|
|
39
|
+
const rows = await getDb()
|
|
40
|
+
.select({ collection: collections.name })
|
|
41
|
+
.from(collectionItems)
|
|
42
|
+
.innerJoin(connectors, eq(connectors.id, collectionItems.connectorId))
|
|
43
|
+
.innerJoin(collections, eq(collections.id, collectionItems.collectionId))
|
|
44
|
+
.where(
|
|
45
|
+
and(
|
|
46
|
+
inArray(collectionItems.collectionId, collectionIds),
|
|
47
|
+
eq(connectors.name, name),
|
|
48
|
+
ne(connectors.id, exceptConnectorId),
|
|
49
|
+
),
|
|
50
|
+
)
|
|
51
|
+
.limit(1);
|
|
52
|
+
return rows[0]?.collection ?? null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function assertRenameFreeOfClash(
|
|
56
|
+
connectorId: string,
|
|
57
|
+
name: string,
|
|
58
|
+
): Promise<void> {
|
|
59
|
+
const rows = await getDb()
|
|
60
|
+
.select({ collectionId: collectionItems.collectionId })
|
|
61
|
+
.from(collectionItems)
|
|
62
|
+
.where(eq(collectionItems.connectorId, connectorId));
|
|
63
|
+
const clash = await collidingCollection(
|
|
64
|
+
rows.map((r) => r.collectionId),
|
|
65
|
+
name,
|
|
66
|
+
connectorId,
|
|
67
|
+
);
|
|
68
|
+
if (clash !== null) throw nameClash(name, clash);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function ownedCollectionOrThrow(
|
|
72
|
+
email: string,
|
|
73
|
+
id: string,
|
|
74
|
+
): Promise<CollectionRow> {
|
|
75
|
+
const rows = await getDb()
|
|
76
|
+
.select()
|
|
77
|
+
.from(collections)
|
|
78
|
+
.where(eq(collections.id, id));
|
|
79
|
+
const row = rows[0];
|
|
80
|
+
if (row === undefined) throw missing();
|
|
81
|
+
try {
|
|
82
|
+
await projectIdOrThrow(email, row.projectId);
|
|
83
|
+
} catch {
|
|
84
|
+
throw missing();
|
|
85
|
+
}
|
|
86
|
+
return row;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function itemsOf(collectionId: string): Promise<string[]> {
|
|
90
|
+
const rows = await getDb()
|
|
91
|
+
.select({ connectorId: collectionItems.connectorId })
|
|
92
|
+
.from(collectionItems)
|
|
93
|
+
.where(eq(collectionItems.collectionId, collectionId))
|
|
94
|
+
.orderBy(asc(collectionItems.id));
|
|
95
|
+
return rows.map((r) => r.connectorId);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function withItems(row: CollectionRow): Promise<ConnectorCollectionRow> {
|
|
99
|
+
return { id: row.id, name: row.name, connectorIds: await itemsOf(row.id) };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function listCollectionsForEmail(
|
|
103
|
+
email: string,
|
|
104
|
+
project: string,
|
|
105
|
+
): Promise<ConnectorCollectionRow[]> {
|
|
106
|
+
const projectId = await projectIdOrThrow(email, project);
|
|
107
|
+
const rows = await getDb()
|
|
108
|
+
.select()
|
|
109
|
+
.from(collections)
|
|
110
|
+
.where(eq(collections.projectId, projectId))
|
|
111
|
+
.orderBy(asc(collections.id));
|
|
112
|
+
return Promise.all(rows.map(withItems));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function getCollectionForEmail(
|
|
116
|
+
email: string,
|
|
117
|
+
id: string,
|
|
118
|
+
): Promise<ConnectorCollectionRow> {
|
|
119
|
+
return withItems(await ownedCollectionOrThrow(email, id));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function createCollectionForEmail(
|
|
123
|
+
email: string,
|
|
124
|
+
project: string,
|
|
125
|
+
raw: string,
|
|
126
|
+
): Promise<ConnectorCollectionRow> {
|
|
127
|
+
const name = connectorName(raw);
|
|
128
|
+
const projectId = await projectIdOrThrow(email, project);
|
|
129
|
+
try {
|
|
130
|
+
const rows = await getDb()
|
|
131
|
+
.insert(collections)
|
|
132
|
+
.values({ id: newId(), projectId, name })
|
|
133
|
+
.returning();
|
|
134
|
+
const row = rows[0];
|
|
135
|
+
if (row === undefined)
|
|
136
|
+
throw new ConnectorError('collection insert returned no row', 500);
|
|
137
|
+
return { id: row.id, name: row.name, connectorIds: [] };
|
|
138
|
+
} catch (err) {
|
|
139
|
+
if (!isUniqueViolation(err)) throw err;
|
|
140
|
+
throw duplicate(name);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function renameCollectionForEmail(
|
|
145
|
+
email: string,
|
|
146
|
+
id: string,
|
|
147
|
+
raw: string,
|
|
148
|
+
): Promise<ConnectorCollectionRow> {
|
|
149
|
+
const name = connectorName(raw);
|
|
150
|
+
const row = await ownedCollectionOrThrow(email, id);
|
|
151
|
+
try {
|
|
152
|
+
await getDb()
|
|
153
|
+
.update(collections)
|
|
154
|
+
.set({ name })
|
|
155
|
+
.where(eq(collections.id, row.id));
|
|
156
|
+
} catch (err) {
|
|
157
|
+
if (!isUniqueViolation(err)) throw err;
|
|
158
|
+
throw duplicate(name);
|
|
159
|
+
}
|
|
160
|
+
return withItems({ ...row, name });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function deleteCollectionForEmail(
|
|
164
|
+
email: string,
|
|
165
|
+
id: string,
|
|
166
|
+
): Promise<{ id: string; name: string }> {
|
|
167
|
+
const row = await ownedCollectionOrThrow(email, id);
|
|
168
|
+
await getDb().delete(collections).where(eq(collections.id, row.id));
|
|
169
|
+
return { id: row.id, name: row.name };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function connectorNameIn(
|
|
173
|
+
projectId: string,
|
|
174
|
+
connectorId: string,
|
|
175
|
+
): Promise<string | null> {
|
|
176
|
+
const rows = await getDb()
|
|
177
|
+
.select({ name: connectors.name })
|
|
178
|
+
.from(connectors)
|
|
179
|
+
.where(and(eq(connectors.id, connectorId), eq(connectors.projectId, projectId)));
|
|
180
|
+
return rows[0]?.name ?? null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function addToCollectionForEmail(
|
|
184
|
+
email: string,
|
|
185
|
+
id: string,
|
|
186
|
+
connectorId: string,
|
|
187
|
+
): Promise<ConnectorCollectionRow> {
|
|
188
|
+
const row = await ownedCollectionOrThrow(email, id);
|
|
189
|
+
const name = await connectorNameIn(row.projectId, connectorId);
|
|
190
|
+
if (name === null) throw new ConnectorError('no such connector', 404);
|
|
191
|
+
const clash = await collidingCollection([row.id], name, connectorId);
|
|
192
|
+
if (clash !== null) throw nameClash(name, clash);
|
|
193
|
+
try {
|
|
194
|
+
await getDb()
|
|
195
|
+
.insert(collectionItems)
|
|
196
|
+
.values({ id: newId(), collectionId: row.id, connectorId });
|
|
197
|
+
} catch (err) {
|
|
198
|
+
if (!isUniqueViolation(err)) throw err;
|
|
199
|
+
}
|
|
200
|
+
return withItems(row);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export async function removeFromCollectionForEmail(
|
|
204
|
+
email: string,
|
|
205
|
+
id: string,
|
|
206
|
+
connectorId: string,
|
|
207
|
+
): Promise<ConnectorCollectionRow> {
|
|
208
|
+
const row = await ownedCollectionOrThrow(email, id);
|
|
209
|
+
await getDb()
|
|
210
|
+
.delete(collectionItems)
|
|
211
|
+
.where(
|
|
212
|
+
and(
|
|
213
|
+
eq(collectionItems.collectionId, row.id),
|
|
214
|
+
eq(collectionItems.connectorId, connectorId),
|
|
215
|
+
),
|
|
216
|
+
);
|
|
217
|
+
return withItems(row);
|
|
218
|
+
}
|