@synoikia/core 0.1.0
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/LICENSE +21 -0
- package/dist/app.d.ts +79 -0
- package/dist/app.js +155 -0
- package/dist/app.js.map +1 -0
- package/dist/approvals/links.d.ts +23 -0
- package/dist/approvals/links.js +49 -0
- package/dist/approvals/links.js.map +1 -0
- package/dist/approvals/service.d.ts +110 -0
- package/dist/approvals/service.js +182 -0
- package/dist/approvals/service.js.map +1 -0
- package/dist/audit-query.d.ts +52 -0
- package/dist/audit-query.js +116 -0
- package/dist/audit-query.js.map +1 -0
- package/dist/audit.d.ts +5 -0
- package/dist/audit.js +8 -0
- package/dist/audit.js.map +1 -0
- package/dist/auth/mcp-auth.d.ts +50 -0
- package/dist/auth/mcp-auth.js +160 -0
- package/dist/auth/mcp-auth.js.map +1 -0
- package/dist/auth/mcp-tokens.d.ts +57 -0
- package/dist/auth/mcp-tokens.js +143 -0
- package/dist/auth/mcp-tokens.js.map +1 -0
- package/dist/auth/oauth.d.ts +188 -0
- package/dist/auth/oauth.js +470 -0
- package/dist/auth/oauth.js.map +1 -0
- package/dist/auth/oidc.d.ts +110 -0
- package/dist/auth/oidc.js +221 -0
- package/dist/auth/oidc.js.map +1 -0
- package/dist/auth/sessions.d.ts +34 -0
- package/dist/auth/sessions.js +83 -0
- package/dist/auth/sessions.js.map +1 -0
- package/dist/auth/throttle.d.ts +31 -0
- package/dist/auth/throttle.js +52 -0
- package/dist/auth/throttle.js.map +1 -0
- package/dist/auth/tokens.d.ts +7 -0
- package/dist/auth/tokens.js +40 -0
- package/dist/auth/tokens.js.map +1 -0
- package/dist/auth/totp.d.ts +14 -0
- package/dist/auth/totp.js +81 -0
- package/dist/auth/totp.js.map +1 -0
- package/dist/auth/users.d.ts +70 -0
- package/dist/auth/users.js +296 -0
- package/dist/auth/users.js.map +1 -0
- package/dist/catalog/groups.d.ts +102 -0
- package/dist/catalog/groups.js +363 -0
- package/dist/catalog/groups.js.map +1 -0
- package/dist/catalog/registry.d.ts +25 -0
- package/dist/catalog/registry.js +80 -0
- package/dist/catalog/registry.js.map +1 -0
- package/dist/catalog/rules.d.ts +128 -0
- package/dist/catalog/rules.js +178 -0
- package/dist/catalog/rules.js.map +1 -0
- package/dist/catalog/sync.d.ts +29 -0
- package/dist/catalog/sync.js +222 -0
- package/dist/catalog/sync.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +83 -0
- package/dist/cli.js.map +1 -0
- package/dist/config/env.d.ts +40 -0
- package/dist/config/env.js +71 -0
- package/dist/config/env.js.map +1 -0
- package/dist/crypto/index.d.ts +28 -0
- package/dist/crypto/index.js +88 -0
- package/dist/crypto/index.js.map +1 -0
- package/dist/crypto/rotate.d.ts +15 -0
- package/dist/crypto/rotate.js +61 -0
- package/dist/crypto/rotate.js.map +1 -0
- package/dist/db/index.d.ts +17 -0
- package/dist/db/index.js +29 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/schema.d.ts +4389 -0
- package/dist/db/schema.js +372 -0
- package/dist/db/schema.js.map +1 -0
- package/dist/endpoints/slug.d.ts +3 -0
- package/dist/endpoints/slug.js +7 -0
- package/dist/endpoints/slug.js.map +1 -0
- package/dist/errors.d.ts +16 -0
- package/dist/errors.js +29 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +43 -0
- package/dist/events.js +16 -0
- package/dist/events.js.map +1 -0
- package/dist/gate/access.d.ts +47 -0
- package/dist/gate/access.js +48 -0
- package/dist/gate/access.js.map +1 -0
- package/dist/gate/attestation.d.ts +23 -0
- package/dist/gate/attestation.js +33 -0
- package/dist/gate/attestation.js.map +1 -0
- package/dist/gate/canonical.d.ts +3 -0
- package/dist/gate/canonical.js +11 -0
- package/dist/gate/canonical.js.map +1 -0
- package/dist/gate/match.d.ts +67 -0
- package/dist/gate/match.js +133 -0
- package/dist/gate/match.js.map +1 -0
- package/dist/gate/pipeline.d.ts +61 -0
- package/dist/gate/pipeline.js +284 -0
- package/dist/gate/pipeline.js.map +1 -0
- package/dist/gate/preapproval.d.ts +24 -0
- package/dist/gate/preapproval.js +48 -0
- package/dist/gate/preapproval.js.map +1 -0
- package/dist/gate/rate-limit.d.ts +35 -0
- package/dist/gate/rate-limit.js +82 -0
- package/dist/gate/rate-limit.js.map +1 -0
- package/dist/gate/redact.d.ts +23 -0
- package/dist/gate/redact.js +87 -0
- package/dist/gate/redact.js.map +1 -0
- package/dist/http/admin/auth.d.ts +33 -0
- package/dist/http/admin/auth.js +338 -0
- package/dist/http/admin/auth.js.map +1 -0
- package/dist/http/admin/instances.d.ts +5 -0
- package/dist/http/admin/instances.js +193 -0
- package/dist/http/admin/instances.js.map +1 -0
- package/dist/http/admin/system.d.ts +5 -0
- package/dist/http/admin/system.js +225 -0
- package/dist/http/admin/system.js.map +1 -0
- package/dist/http/admin-app.d.ts +9 -0
- package/dist/http/admin-app.js +51 -0
- package/dist/http/admin-app.js.map +1 -0
- package/dist/http/common.d.ts +32 -0
- package/dist/http/common.js +80 -0
- package/dist/http/common.js.map +1 -0
- package/dist/http/mcp/approval-routes.d.ts +11 -0
- package/dist/http/mcp/approval-routes.js +117 -0
- package/dist/http/mcp/approval-routes.js.map +1 -0
- package/dist/http/mcp/endpoint.d.ts +32 -0
- package/dist/http/mcp/endpoint.js +293 -0
- package/dist/http/mcp/endpoint.js.map +1 -0
- package/dist/http/mcp/oauth-routes.d.ts +29 -0
- package/dist/http/mcp/oauth-routes.js +465 -0
- package/dist/http/mcp/oauth-routes.js.map +1 -0
- package/dist/http/mcp/pages.d.ts +63 -0
- package/dist/http/mcp/pages.js +200 -0
- package/dist/http/mcp/pages.js.map +1 -0
- package/dist/http/mcp-app.d.ts +7 -0
- package/dist/http/mcp-app.js +30 -0
- package/dist/http/mcp-app.js.map +1 -0
- package/dist/instances/connection.d.ts +37 -0
- package/dist/instances/connection.js +109 -0
- package/dist/instances/connection.js.map +1 -0
- package/dist/instances/manager.d.ts +313 -0
- package/dist/instances/manager.js +600 -0
- package/dist/instances/manager.js.map +1 -0
- package/dist/instances/settings.d.ts +42 -0
- package/dist/instances/settings.js +57 -0
- package/dist/instances/settings.js.map +1 -0
- package/dist/lenient.d.ts +16 -0
- package/dist/lenient.js +51 -0
- package/dist/lenient.js.map +1 -0
- package/dist/lock.d.ts +28 -0
- package/dist/lock.js +77 -0
- package/dist/lock.js.map +1 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +38 -0
- package/dist/main.js.map +1 -0
- package/dist/maintenance.d.ts +10 -0
- package/dist/maintenance.js +48 -0
- package/dist/maintenance.js.map +1 -0
- package/dist/notify/service.d.ts +149 -0
- package/dist/notify/service.js +358 -0
- package/dist/notify/service.js.map +1 -0
- package/dist/plugins/discovery.d.ts +39 -0
- package/dist/plugins/discovery.js +157 -0
- package/dist/plugins/discovery.js.map +1 -0
- package/dist/plugins/minisign.d.ts +25 -0
- package/dist/plugins/minisign.js +49 -0
- package/dist/plugins/minisign.js.map +1 -0
- package/dist/plugins/process.d.ts +55 -0
- package/dist/plugins/process.js +162 -0
- package/dist/plugins/process.js.map +1 -0
- package/dist/plugins/repos.d.ts +198 -0
- package/dist/plugins/repos.js +612 -0
- package/dist/plugins/repos.js.map +1 -0
- package/dist/plugins/supervisor.d.ts +50 -0
- package/dist/plugins/supervisor.js +125 -0
- package/dist/plugins/supervisor.js.map +1 -0
- package/dist/runtime/index.d.ts +28 -0
- package/dist/runtime/index.js +248 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/sandbox/index.d.ts +53 -0
- package/dist/sandbox/index.js +197 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/server.d.ts +16 -0
- package/dist/server.js +36 -0
- package/dist/server.js.map +1 -0
- package/dist/settings.d.ts +43 -0
- package/dist/settings.js +99 -0
- package/dist/settings.js.map +1 -0
- package/dist/testing/index.d.ts +73 -0
- package/dist/testing/index.js +174 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +4 -0
- package/dist/version.js.map +1 -0
- package/drizzle/0000_init.sql +334 -0
- package/drizzle/0001_totp_last_step.sql +1 -0
- package/drizzle/0002_access_levels.sql +16 -0
- package/drizzle/0003_drop_exclusion_flags.sql +2 -0
- package/drizzle/0004_grant_instance_ids.sql +1 -0
- package/drizzle/0005_rule_strict_miss.sql +1 -0
- package/drizzle/0006_attestation_waived.sql +1 -0
- package/drizzle/meta/0000_snapshot.json +2318 -0
- package/drizzle/meta/0001_snapshot.json +2325 -0
- package/drizzle/meta/0002_snapshot.json +2348 -0
- package/drizzle/meta/0003_snapshot.json +2332 -0
- package/drizzle/meta/0004_snapshot.json +2339 -0
- package/drizzle/meta/0005_snapshot.json +2346 -0
- package/drizzle/meta/0006_snapshot.json +2354 -0
- package/drizzle/meta/_journal.json +55 -0
- package/package.json +58 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { and, asc, eq, inArray } from 'drizzle-orm';
|
|
3
|
+
import { writeAudit } from '../audit.js';
|
|
4
|
+
import { operationGroupAliases, operationGroups, operations, pluginInstances } from '../db/schema.js';
|
|
5
|
+
import { ConflictError, NotFoundError, ValidationError } from '../errors.js';
|
|
6
|
+
import { ACCESS_LEVELS, FULL_ACCESS, effectiveAccess, isAccessLevel, minLevel } from '../gate/access.js';
|
|
7
|
+
function assertLevel(level) {
|
|
8
|
+
if (!isAccessLevel(level)) {
|
|
9
|
+
throw new ValidationError('invalid_level', `Level must be one of ${ACCESS_LEVELS.join(', ')}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export const accessInput = (op) => ({
|
|
13
|
+
classification: op.classification,
|
|
14
|
+
locked: op.locked,
|
|
15
|
+
levelOverride: op.levelOverride,
|
|
16
|
+
writeAcknowledged: op.writeAcknowledged,
|
|
17
|
+
});
|
|
18
|
+
const isPlainWrite = (op) => !op.stale && !op.locked && op.classification === 'write';
|
|
19
|
+
/**
|
|
20
|
+
* Writes that would run without asking once their group is at `write`: plain writes that follow the
|
|
21
|
+
* group. Raising a group to `write` must acknowledge exactly these (locked ops never auto-run).
|
|
22
|
+
*/
|
|
23
|
+
const followsGroupWrite = (op) => isPlainWrite(op) && op.levelOverride === null;
|
|
24
|
+
/** Writes whose effective level is `write` but that nobody acknowledged yet: they ask until someone does. */
|
|
25
|
+
function isPendingWrite(op, group) {
|
|
26
|
+
return isPlainWrite(op) && !op.writeAcknowledged && (op.levelOverride ?? group?.level) === 'write';
|
|
27
|
+
}
|
|
28
|
+
const sameSet = (a = [], b) => a.length === b.length && new Set(a).size === b.length && b.every((x) => a.includes(x));
|
|
29
|
+
function getGroup(db, instanceId, key) {
|
|
30
|
+
const group = db
|
|
31
|
+
.select()
|
|
32
|
+
.from(operationGroups)
|
|
33
|
+
.where(and(eq(operationGroups.instanceId, instanceId), eq(operationGroups.key, key)))
|
|
34
|
+
.get();
|
|
35
|
+
if (!group)
|
|
36
|
+
throw new NotFoundError('group_not_found', `No group "${key}" on this instance`);
|
|
37
|
+
return group;
|
|
38
|
+
}
|
|
39
|
+
function opsInGroups(db, groupIds) {
|
|
40
|
+
if (groupIds.length === 0)
|
|
41
|
+
return [];
|
|
42
|
+
return db.select().from(operations).where(inArray(operations.groupId, groupIds)).orderBy(asc(operations.key)).all();
|
|
43
|
+
}
|
|
44
|
+
function acknowledge(db, opIds, actor, now) {
|
|
45
|
+
if (opIds.length === 0)
|
|
46
|
+
return;
|
|
47
|
+
db.update(operations)
|
|
48
|
+
.set({ writeAcknowledged: true, acknowledgedAt: now, acknowledgedBy: actor.userId ?? null })
|
|
49
|
+
.where(inArray(operations.id, opIds))
|
|
50
|
+
.run();
|
|
51
|
+
}
|
|
52
|
+
export function listGroups(db, instanceId) {
|
|
53
|
+
const groups = db
|
|
54
|
+
.select()
|
|
55
|
+
.from(operationGroups)
|
|
56
|
+
.where(eq(operationGroups.instanceId, instanceId))
|
|
57
|
+
.orderBy(asc(operationGroups.key))
|
|
58
|
+
.all();
|
|
59
|
+
const ops = opsInGroups(db, groups.map((g) => g.id)).filter((o) => !o.stale);
|
|
60
|
+
return groups.map((g) => {
|
|
61
|
+
const mine = ops.filter((o) => o.groupId === g.id);
|
|
62
|
+
return {
|
|
63
|
+
key: g.key,
|
|
64
|
+
label: g.label,
|
|
65
|
+
level: g.level,
|
|
66
|
+
stale: g.stale,
|
|
67
|
+
counts: {
|
|
68
|
+
read: mine.filter((o) => !o.locked && o.classification === 'read').length,
|
|
69
|
+
write: mine.filter((o) => !o.locked && o.classification === 'write').length,
|
|
70
|
+
locked: mine.filter((o) => o.locked).length,
|
|
71
|
+
pendingReview: mine.filter((o) => isPendingWrite(o, g)).length,
|
|
72
|
+
overridden: mine.filter((o) => o.levelOverride !== null).length,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/** Access decision for one operation key; unknown or stale operations are unreachable. */
|
|
78
|
+
export function resolveAccess(db, instanceId, key, principal = FULL_ACCESS) {
|
|
79
|
+
const op = db
|
|
80
|
+
.select()
|
|
81
|
+
.from(operations)
|
|
82
|
+
.where(and(eq(operations.instanceId, instanceId), eq(operations.key, key)))
|
|
83
|
+
.get();
|
|
84
|
+
if (!op || op.stale)
|
|
85
|
+
return { reachable: false, reason: 'unknown_operation' };
|
|
86
|
+
const group = db.select().from(operationGroups).where(eq(operationGroups.id, op.groupId)).get();
|
|
87
|
+
return effectiveAccess(accessInput(op), group, principal);
|
|
88
|
+
}
|
|
89
|
+
// ── group level ──────────────────────────────────────────────────────────────────────────────────
|
|
90
|
+
/**
|
|
91
|
+
* Sets one group's level; operations with their own level keep it. Raising to `write` must carry
|
|
92
|
+
* `acknowledge` = exactly the writes that will then run without asking (the list the admin was
|
|
93
|
+
* shown); if a sync changed that list in between, this fails with 409 and the fresh list.
|
|
94
|
+
*/
|
|
95
|
+
export function setGroupLevel(db, instanceId, key, level, opts = {}) {
|
|
96
|
+
assertLevel(level);
|
|
97
|
+
const now = opts.now ?? new Date();
|
|
98
|
+
const actor = opts.actor ?? {};
|
|
99
|
+
db.transaction((tx) => {
|
|
100
|
+
const group = getGroup(tx, instanceId, key);
|
|
101
|
+
let acknowledged = [];
|
|
102
|
+
if (level === 'write') {
|
|
103
|
+
const autoRun = opsInGroups(tx, [group.id]).filter(followsGroupWrite);
|
|
104
|
+
const expected = autoRun.map((o) => o.id);
|
|
105
|
+
if (!sameSet(opts.acknowledge, expected)) {
|
|
106
|
+
throw new ConflictError('acknowledgement_mismatch', 'Acknowledge exactly the writes that will run without asking', {
|
|
107
|
+
expected: autoRun.map((o) => ({ id: o.id, key: o.key })),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const fresh = autoRun.filter((o) => !o.writeAcknowledged);
|
|
111
|
+
acknowledge(tx, fresh.map((o) => o.id), actor, now);
|
|
112
|
+
acknowledged = fresh.map((o) => o.key);
|
|
113
|
+
}
|
|
114
|
+
if (group.level === level && acknowledged.length === 0)
|
|
115
|
+
return;
|
|
116
|
+
tx.update(operationGroups)
|
|
117
|
+
.set({ level, levelChangedAt: now, levelChangedBy: actor.userId ?? null })
|
|
118
|
+
.where(eq(operationGroups.id, group.id))
|
|
119
|
+
.run();
|
|
120
|
+
writeAudit(tx, {
|
|
121
|
+
kind: 'config',
|
|
122
|
+
instanceId,
|
|
123
|
+
decision: 'group_level_changed',
|
|
124
|
+
actorKind: 'user',
|
|
125
|
+
actorId: actor.userId,
|
|
126
|
+
detail: { group: key, from: group.level, to: level, acknowledged },
|
|
127
|
+
}, now);
|
|
128
|
+
});
|
|
129
|
+
return listGroups(db, instanceId).find((g) => g.key === key);
|
|
130
|
+
}
|
|
131
|
+
export function previewBulkLevel(db, instanceId, level) {
|
|
132
|
+
assertLevel(level);
|
|
133
|
+
const groups = db
|
|
134
|
+
.select()
|
|
135
|
+
.from(operationGroups)
|
|
136
|
+
.where(eq(operationGroups.instanceId, instanceId))
|
|
137
|
+
.orderBy(asc(operationGroups.key))
|
|
138
|
+
.all();
|
|
139
|
+
const pending = level === 'write'
|
|
140
|
+
? opsInGroups(db, groups.map((g) => g.id)).filter(followsGroupWrite)
|
|
141
|
+
: [];
|
|
142
|
+
const preview = groups.map((g) => ({
|
|
143
|
+
key: g.key,
|
|
144
|
+
label: g.label,
|
|
145
|
+
from: g.level,
|
|
146
|
+
exposes: pending.filter((o) => o.groupId === g.id).map((o) => ({ id: o.id, key: o.key })),
|
|
147
|
+
}));
|
|
148
|
+
return { level, groups: preview, acknowledge: pending.map((o) => o.id) };
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Sets every group of an instance to one level (operations with their own level keep it).
|
|
152
|
+
* `none`/`read`/`ask` apply directly. `write` requires `confirm` = the instance slug and
|
|
153
|
+
* `acknowledge` = the current preview's list. Locked operations never auto-run.
|
|
154
|
+
*/
|
|
155
|
+
export function applyBulkLevel(db, instanceId, level, opts = {}) {
|
|
156
|
+
assertLevel(level);
|
|
157
|
+
const now = opts.now ?? new Date();
|
|
158
|
+
const actor = opts.actor ?? {};
|
|
159
|
+
return db.transaction((tx) => {
|
|
160
|
+
const instance = tx.select().from(pluginInstances).where(eq(pluginInstances.id, instanceId)).get();
|
|
161
|
+
if (!instance)
|
|
162
|
+
throw new NotFoundError('instance_not_found', 'No such instance');
|
|
163
|
+
const preview = previewBulkLevel(tx, instanceId, level);
|
|
164
|
+
if (level === 'write') {
|
|
165
|
+
if (opts.confirm !== instance.slug) {
|
|
166
|
+
throw new ConflictError('confirmation_required', `Type the endpoint slug "${instance.slug}" to confirm`);
|
|
167
|
+
}
|
|
168
|
+
if (!sameSet(opts.acknowledge, preview.acknowledge)) {
|
|
169
|
+
throw new ConflictError('acknowledgement_mismatch', 'The list of exposed writes changed; review it again', {
|
|
170
|
+
preview,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
acknowledge(tx, opsInGroups(tx, tx
|
|
174
|
+
.select({ id: operationGroups.id })
|
|
175
|
+
.from(operationGroups)
|
|
176
|
+
.where(eq(operationGroups.instanceId, instanceId))
|
|
177
|
+
.all()
|
|
178
|
+
.map((g) => g.id))
|
|
179
|
+
.filter((o) => followsGroupWrite(o) && !o.writeAcknowledged)
|
|
180
|
+
.map((o) => o.id), actor, now);
|
|
181
|
+
}
|
|
182
|
+
tx.update(operationGroups)
|
|
183
|
+
.set({ level, levelChangedAt: now, levelChangedBy: actor.userId ?? null })
|
|
184
|
+
.where(eq(operationGroups.instanceId, instanceId))
|
|
185
|
+
.run();
|
|
186
|
+
writeAudit(tx, {
|
|
187
|
+
kind: 'config',
|
|
188
|
+
instanceId,
|
|
189
|
+
decision: 'group_level_bulk_changed',
|
|
190
|
+
actorKind: 'user',
|
|
191
|
+
actorId: actor.userId,
|
|
192
|
+
detail: {
|
|
193
|
+
to: level,
|
|
194
|
+
groups: preview.groups.map((g) => ({ key: g.key, from: g.from, to: level })),
|
|
195
|
+
acknowledged: preview.groups.flatMap((g) => g.exposes.map((o) => o.key)),
|
|
196
|
+
},
|
|
197
|
+
}, now);
|
|
198
|
+
return preview.groups;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
// ── regrouping ───────────────────────────────────────────────────────────────────────────────────
|
|
202
|
+
export function renameGroup(db, instanceId, key, label, opts = {}) {
|
|
203
|
+
const trimmed = label.trim();
|
|
204
|
+
if (!trimmed)
|
|
205
|
+
throw new ValidationError('invalid_label', 'Label must not be empty');
|
|
206
|
+
db.transaction((tx) => {
|
|
207
|
+
const group = getGroup(tx, instanceId, key);
|
|
208
|
+
tx.update(operationGroups).set({ label: trimmed }).where(eq(operationGroups.id, group.id)).run();
|
|
209
|
+
writeAudit(tx, {
|
|
210
|
+
kind: 'config',
|
|
211
|
+
instanceId,
|
|
212
|
+
decision: 'group_renamed',
|
|
213
|
+
actorKind: 'user',
|
|
214
|
+
actorId: opts.actor?.userId,
|
|
215
|
+
detail: { group: key, from: group.label, to: trimmed },
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Merges groups into `into` (created if new). Stored as aliases so future syncs keep the grouping.
|
|
221
|
+
* The merged group takes the lowest level involved, so merging never widens access.
|
|
222
|
+
*/
|
|
223
|
+
export function mergeGroups(db, instanceId, input, opts = {}) {
|
|
224
|
+
const now = opts.now ?? new Date();
|
|
225
|
+
const into = input.into.trim();
|
|
226
|
+
if (!/^[a-z0-9][a-z0-9._-]*$/.test(into))
|
|
227
|
+
throw new ValidationError('invalid_group_key', 'Invalid group key');
|
|
228
|
+
const fromKeys = [...new Set(input.from)].filter((k) => k !== into);
|
|
229
|
+
if (fromKeys.length === 0)
|
|
230
|
+
throw new ValidationError('nothing_to_merge', 'Pick at least one other group to merge');
|
|
231
|
+
db.transaction((tx) => {
|
|
232
|
+
const sources = fromKeys.map((k) => getGroup(tx, instanceId, k));
|
|
233
|
+
let target = tx
|
|
234
|
+
.select()
|
|
235
|
+
.from(operationGroups)
|
|
236
|
+
.where(and(eq(operationGroups.instanceId, instanceId), eq(operationGroups.key, into)))
|
|
237
|
+
.get();
|
|
238
|
+
const level = [...sources, ...(target ? [target] : [])].map((g) => g.level).reduce(minLevel);
|
|
239
|
+
if (!target) {
|
|
240
|
+
target = {
|
|
241
|
+
id: randomUUID(),
|
|
242
|
+
instanceId,
|
|
243
|
+
key: into,
|
|
244
|
+
label: input.label?.trim() || into,
|
|
245
|
+
level,
|
|
246
|
+
levelChangedAt: now,
|
|
247
|
+
levelChangedBy: opts.actor?.userId ?? null,
|
|
248
|
+
firstSeenAt: now,
|
|
249
|
+
stale: false,
|
|
250
|
+
};
|
|
251
|
+
tx.insert(operationGroups).values(target).run();
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
tx.update(operationGroups)
|
|
255
|
+
.set({
|
|
256
|
+
level,
|
|
257
|
+
stale: false,
|
|
258
|
+
...(input.label?.trim() ? { label: input.label.trim() } : {}),
|
|
259
|
+
...(level !== target.level ? { levelChangedAt: now, levelChangedBy: opts.actor?.userId ?? null } : {}),
|
|
260
|
+
})
|
|
261
|
+
.where(eq(operationGroups.id, target.id))
|
|
262
|
+
.run();
|
|
263
|
+
}
|
|
264
|
+
const sourceIds = sources.map((g) => g.id);
|
|
265
|
+
const pluginGroups = new Set(opsInGroups(tx, sourceIds).map((o) => o.pluginGroup));
|
|
266
|
+
// Aliases that pointed at a merged group now point at the target.
|
|
267
|
+
for (const a of tx
|
|
268
|
+
.select()
|
|
269
|
+
.from(operationGroupAliases)
|
|
270
|
+
.where(and(eq(operationGroupAliases.instanceId, instanceId), inArray(operationGroupAliases.groupKey, fromKeys)))
|
|
271
|
+
.all()) {
|
|
272
|
+
pluginGroups.add(a.pluginGroup);
|
|
273
|
+
}
|
|
274
|
+
for (const k of fromKeys)
|
|
275
|
+
pluginGroups.add(k);
|
|
276
|
+
for (const pluginGroup of pluginGroups) {
|
|
277
|
+
tx.insert(operationGroupAliases)
|
|
278
|
+
.values({ instanceId, pluginGroup, groupKey: into })
|
|
279
|
+
.onConflictDoUpdate({
|
|
280
|
+
target: [operationGroupAliases.instanceId, operationGroupAliases.pluginGroup],
|
|
281
|
+
set: { groupKey: into },
|
|
282
|
+
})
|
|
283
|
+
.run();
|
|
284
|
+
}
|
|
285
|
+
tx.update(operations).set({ groupId: target.id }).where(inArray(operations.groupId, sourceIds)).run();
|
|
286
|
+
tx.delete(operationGroups).where(inArray(operationGroups.id, sourceIds)).run();
|
|
287
|
+
writeAudit(tx, {
|
|
288
|
+
kind: 'config',
|
|
289
|
+
instanceId,
|
|
290
|
+
decision: 'groups_merged',
|
|
291
|
+
actorKind: 'user',
|
|
292
|
+
actorId: opts.actor?.userId,
|
|
293
|
+
detail: { from: sources.map((g) => ({ key: g.key, level: g.level })), into, level },
|
|
294
|
+
}, now);
|
|
295
|
+
});
|
|
296
|
+
return listGroups(db, instanceId).find((g) => g.key === into);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Per-operation level, acknowledgement and classification override. Locked operations can't be set
|
|
300
|
+
* to `write` and can't change classification (409). Setting a write to `write`, or overriding an
|
|
301
|
+
* operation's classification to write, acknowledges it.
|
|
302
|
+
*/
|
|
303
|
+
export function updateOperation(db, instanceId, opId, patch, opts = {}) {
|
|
304
|
+
const now = opts.now ?? new Date();
|
|
305
|
+
const actor = opts.actor ?? {};
|
|
306
|
+
db.transaction((tx) => {
|
|
307
|
+
const op = tx
|
|
308
|
+
.select()
|
|
309
|
+
.from(operations)
|
|
310
|
+
.where(and(eq(operations.id, opId), eq(operations.instanceId, instanceId)))
|
|
311
|
+
.get();
|
|
312
|
+
if (!op)
|
|
313
|
+
throw new NotFoundError('operation_not_found', 'No such operation on this instance');
|
|
314
|
+
const set = {};
|
|
315
|
+
if (patch.classification !== undefined && patch.classification !== op.classification) {
|
|
316
|
+
if (op.locked)
|
|
317
|
+
throw new ConflictError('operation_locked', `${op.key} is locked; its classification can't change`);
|
|
318
|
+
set.classification = patch.classification;
|
|
319
|
+
set.classificationSource = 'override';
|
|
320
|
+
}
|
|
321
|
+
if (patch.level !== undefined && patch.level !== op.levelOverride) {
|
|
322
|
+
if (patch.level !== null)
|
|
323
|
+
assertLevel(patch.level);
|
|
324
|
+
if (patch.level === 'write' && op.locked) {
|
|
325
|
+
throw new ConflictError('operation_locked', `${op.key} is locked; it always asks for approval`);
|
|
326
|
+
}
|
|
327
|
+
set.levelOverride = patch.level;
|
|
328
|
+
}
|
|
329
|
+
const isWrite = (set.classification ?? op.classification) === 'write';
|
|
330
|
+
const acknowledges = patch.acknowledged === true || set.classification === 'write' || (patch.level === 'write' && isWrite);
|
|
331
|
+
if (acknowledges && !op.writeAcknowledged) {
|
|
332
|
+
Object.assign(set, { writeAcknowledged: true, acknowledgedAt: now, acknowledgedBy: actor.userId ?? null });
|
|
333
|
+
}
|
|
334
|
+
if (patch.attestationRequired !== undefined && patch.attestationRequired !== op.attestationRequired) {
|
|
335
|
+
set.attestationRequired = patch.attestationRequired;
|
|
336
|
+
set.attestationWaived = !patch.attestationRequired;
|
|
337
|
+
}
|
|
338
|
+
if (patch.acknowledged === false) {
|
|
339
|
+
Object.assign(set, { writeAcknowledged: false, acknowledgedAt: null, acknowledgedBy: null });
|
|
340
|
+
}
|
|
341
|
+
if (Object.keys(set).length === 0)
|
|
342
|
+
return;
|
|
343
|
+
tx.update(operations).set(set).where(eq(operations.id, op.id)).run();
|
|
344
|
+
writeAudit(tx, {
|
|
345
|
+
kind: 'config',
|
|
346
|
+
instanceId,
|
|
347
|
+
operationKey: op.key,
|
|
348
|
+
decision: 'operation_updated',
|
|
349
|
+
actorKind: 'user',
|
|
350
|
+
actorId: actor.userId,
|
|
351
|
+
detail: {
|
|
352
|
+
before: {
|
|
353
|
+
level: op.levelOverride,
|
|
354
|
+
writeAcknowledged: op.writeAcknowledged,
|
|
355
|
+
classification: op.classification,
|
|
356
|
+
attestationRequired: op.attestationRequired,
|
|
357
|
+
},
|
|
358
|
+
after: set,
|
|
359
|
+
},
|
|
360
|
+
}, now);
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
//# sourceMappingURL=groups.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"groups.js","sourceRoot":"","sources":["../../src/catalog/groups.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACtG,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,eAAe,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAezG,SAAS,WAAW,CAAC,KAAc;IACjC,IAAI,CAA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AK,CAAC;YAAE,OAAO;QAE1C,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,UAAU,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QACrE,UAAU,CACR,EAAE,EACF;YACE,IAAI,EAAE,QAAQ;YACd,UAAU;YACV,YAAY,EAAE,EAAE,CAAC,GAAG;YACpB,QAAQ,EAAE,mBAAmB;YAC7B,SAAS,EAAE,MAAM;YACjB,OAAO,EAAE,KAAK,CAAC,MAAM;YACrB,MAAM,EAAE;gBACN,MAAM,EAAE;oBACN,KAAK,EAAE,EAAE,CAAC,aAAa;oBACvB,iBAAiB,EAAE,EAAE,CAAC,iBAAiB;oBACvC,cAAc,EAAE,EAAE,CAAC,cAAc;oBACjC,mBAAmB,EAAE,EAAE,CAAC,mBAAmB;iBAC5C;gBACD,KAAK,EAAE,GAAG;aACX;SACF,EACD,GAAG,CACJ,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Db, DbLike } from '../db/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Registry mirror (design §2.4 / HA §2.4): pickable upstream objects — HA areas, devices, entities —
|
|
4
|
+
* mirrored locally so pickers and `search` never pull the whole registry from the upstream.
|
|
5
|
+
*/
|
|
6
|
+
export declare function applyRegistrySync(db: Db, instanceId: string, raw: unknown, now?: Date): {
|
|
7
|
+
upserted: number;
|
|
8
|
+
staled: number;
|
|
9
|
+
};
|
|
10
|
+
export interface RegistryQuery {
|
|
11
|
+
kind?: string;
|
|
12
|
+
text?: string;
|
|
13
|
+
parent?: string;
|
|
14
|
+
domain?: string;
|
|
15
|
+
limit?: number;
|
|
16
|
+
offset?: number;
|
|
17
|
+
}
|
|
18
|
+
export declare function findRegistryEntries(db: DbLike, instanceId: string, q?: RegistryQuery): {
|
|
19
|
+
kind: string;
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
parentId: string | null;
|
|
23
|
+
domain: string | null;
|
|
24
|
+
attrs: unknown;
|
|
25
|
+
}[];
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { RegistryEntrySchema } from '@synoikia/plugin-sdk';
|
|
3
|
+
import { and, asc, eq, or, sql } from 'drizzle-orm';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { registryEntries } from '../db/schema.js';
|
|
6
|
+
/**
|
|
7
|
+
* Registry mirror (design §2.4 / HA §2.4): pickable upstream objects — HA areas, devices, entities —
|
|
8
|
+
* mirrored locally so pickers and `search` never pull the whole registry from the upstream.
|
|
9
|
+
*/
|
|
10
|
+
export function applyRegistrySync(db, instanceId, raw, now = new Date()) {
|
|
11
|
+
const entries = z.array(RegistryEntrySchema).parse(raw);
|
|
12
|
+
return db.transaction((tx) => {
|
|
13
|
+
const existing = new Map(tx
|
|
14
|
+
.select()
|
|
15
|
+
.from(registryEntries)
|
|
16
|
+
.where(eq(registryEntries.instanceId, instanceId))
|
|
17
|
+
.all()
|
|
18
|
+
.map((e) => [`${e.kind}\u0000${e.extId}`, e]));
|
|
19
|
+
const seen = new Set();
|
|
20
|
+
for (const e of entries) {
|
|
21
|
+
const k = `${e.kind}\u0000${e.id}`;
|
|
22
|
+
if (seen.has(k))
|
|
23
|
+
continue;
|
|
24
|
+
seen.add(k);
|
|
25
|
+
const fields = {
|
|
26
|
+
name: e.name,
|
|
27
|
+
parentExtId: e.parentId ?? null,
|
|
28
|
+
domain: e.domain ?? null,
|
|
29
|
+
attrs: e.attrs ?? null,
|
|
30
|
+
stale: false,
|
|
31
|
+
lastSyncedAt: now,
|
|
32
|
+
};
|
|
33
|
+
const prev = existing.get(k);
|
|
34
|
+
if (prev)
|
|
35
|
+
tx.update(registryEntries).set(fields).where(eq(registryEntries.id, prev.id)).run();
|
|
36
|
+
else
|
|
37
|
+
tx.insert(registryEntries)
|
|
38
|
+
.values({ id: randomUUID(), instanceId, kind: e.kind, extId: e.id, ...fields })
|
|
39
|
+
.run();
|
|
40
|
+
}
|
|
41
|
+
let staled = 0;
|
|
42
|
+
for (const [k, prev] of existing) {
|
|
43
|
+
if (!seen.has(k) && !prev.stale) {
|
|
44
|
+
tx.update(registryEntries).set({ stale: true }).where(eq(registryEntries.id, prev.id)).run();
|
|
45
|
+
staled++;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return { upserted: seen.size, staled };
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export function findRegistryEntries(db, instanceId, q = {}) {
|
|
52
|
+
const limit = Math.min(Math.max(1, Number(q.limit) || 50), 500);
|
|
53
|
+
const conditions = [eq(registryEntries.instanceId, instanceId), eq(registryEntries.stale, false)];
|
|
54
|
+
if (q.kind)
|
|
55
|
+
conditions.push(eq(registryEntries.kind, q.kind));
|
|
56
|
+
if (q.parent)
|
|
57
|
+
conditions.push(eq(registryEntries.parentExtId, q.parent));
|
|
58
|
+
if (q.domain)
|
|
59
|
+
conditions.push(eq(registryEntries.domain, q.domain));
|
|
60
|
+
if (q.text) {
|
|
61
|
+
const pattern = `%${q.text.replace(/[%_\\]/g, (c) => `\\${c}`)}%`;
|
|
62
|
+
conditions.push(or(sql `${registryEntries.name} LIKE ${pattern} ESCAPE '\\'`, sql `${registryEntries.extId} LIKE ${pattern} ESCAPE '\\'`));
|
|
63
|
+
}
|
|
64
|
+
return db
|
|
65
|
+
.select({
|
|
66
|
+
kind: registryEntries.kind,
|
|
67
|
+
id: registryEntries.extId,
|
|
68
|
+
name: registryEntries.name,
|
|
69
|
+
parentId: registryEntries.parentExtId,
|
|
70
|
+
domain: registryEntries.domain,
|
|
71
|
+
attrs: registryEntries.attrs,
|
|
72
|
+
})
|
|
73
|
+
.from(registryEntries)
|
|
74
|
+
.where(and(...conditions))
|
|
75
|
+
.orderBy(asc(registryEntries.kind), asc(registryEntries.name))
|
|
76
|
+
.limit(limit)
|
|
77
|
+
.offset(Math.max(0, Number(q.offset) || 0))
|
|
78
|
+
.all();
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/catalog/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD;;;GAGG;AAEH,MAAM,UAAU,iBAAiB,CAC/B,EAAM,EACN,UAAkB,EAClB,GAAY,EACZ,GAAG,GAAG,IAAI,IAAI,EAAE;IAEhB,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACxD,OAAO,EAAE,CAAC,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE;QAC3B,MAAM,QAAQ,GAAG,IAAI,GAAG,CACtB,EAAE;aACC,MAAM,EAAE;aACR,IAAI,CAAC,eAAe,CAAC;aACrB,KAAK,CAAC,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;aACjD,GAAG,EAAE;aACL,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,CAChD,CAAC;QACF,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;QAC/B,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE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E,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC;QAClE,UAAU,CAAC,IAAI,CACb,EAAE,CACA,GAAG,CAAA,GAAG,eAAe,CAAC,IAAI,SAAS,OAAO,cAAc,EACxD,GAAG,CAAA,GAAG,eAAe,CAAC,KAAK,SAAS,OAAO,cAAc,CACzD,CACH,CAAC;IACJ,CAAC;IACD,OAAO,EAAE;SACN,MAAM,CAAC;QACN,IAAI,EAAE,eAAe,CAAC,IAAI;QAC1B,EAAE,EAAE,eAAe,CAAC,KAAK;QACzB,IAAI,EAAE,eAAe,CAAC,IAAI;QAC1B,QAAQ,EAAE,eAAe,CAAC,WAAW;QACrC,MAAM,EAAE,eAAe,CAAC,MAAM;QAC9B,KAAK,EAAE,eAAe,CAAC,KAAK;KAC7B,CAAC;SACD,IAAI,CAAC,eAAe,CAAC;SACrB,KAAK,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC;SACzB,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;SAC7D,KAAK,CAAC,KAAK,CAAC;SACZ,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;SAC1C,GAAG,EAAE,CAAC;AACX,CAAC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { Manifest } from '@synoikia/plugin-sdk';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import type { Db } from '../db/index.js';
|
|
4
|
+
import { operations } from '../db/schema.js';
|
|
5
|
+
import { ValidationError } from '../errors.js';
|
|
6
|
+
/**
|
|
7
|
+
* Pre-approval rules (design §5.2, TN §3.5): picked from the synced catalog, never free text; a
|
|
8
|
+
* required reason; structured match; optional rate limit and expiry. Locked operations can never be
|
|
9
|
+
* referenced (409), enforced here regardless of what the UI offers.
|
|
10
|
+
*/
|
|
11
|
+
export declare const RuleInputSchema: z.ZodObject<{
|
|
12
|
+
operationId: z.ZodString;
|
|
13
|
+
match: z.ZodDefault<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
14
|
+
field: z.ZodLiteral<"$targets">;
|
|
15
|
+
areas: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
16
|
+
entities: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
17
|
+
domains: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
18
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
19
|
+
field: z.ZodString;
|
|
20
|
+
op: z.ZodEnum<{
|
|
21
|
+
any: "any";
|
|
22
|
+
in: "in";
|
|
23
|
+
eq: "eq";
|
|
24
|
+
prefix: "prefix";
|
|
25
|
+
range: "range";
|
|
26
|
+
bool: "bool";
|
|
27
|
+
}>;
|
|
28
|
+
value: z.ZodOptional<z.ZodUnknown>;
|
|
29
|
+
}, z.core.$strip>]>>>;
|
|
30
|
+
rateLimit: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
31
|
+
windowSeconds: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
32
|
+
expiresAt: z.ZodOptional<z.ZodNullable<z.ZodCoercedDate<unknown>>>;
|
|
33
|
+
reason: z.ZodString;
|
|
34
|
+
enabled: z.ZodDefault<z.ZodBoolean>;
|
|
35
|
+
}, z.core.$strip>;
|
|
36
|
+
export type RuleInput = z.infer<typeof RuleInputSchema>;
|
|
37
|
+
type OperationRow = typeof operations.$inferSelect;
|
|
38
|
+
/** Why a rule's match doesn't fit the operation's declared fields (design §8.3), or null if it does. */
|
|
39
|
+
export declare function matchMisfit(manifest: Pick<Manifest, 'matchProfiles'>, op: Pick<OperationRow, 'key' | 'matchProfile'>, match: RuleInput['match']): ValidationError | null;
|
|
40
|
+
export declare function listRules(db: Db, instanceId: string): {
|
|
41
|
+
operation: {
|
|
42
|
+
id: string;
|
|
43
|
+
key: string;
|
|
44
|
+
locked: boolean;
|
|
45
|
+
matchProfile: string | null;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Rules only apply at level `ask`: on an unreachable operation, or one at level `write`, a rule
|
|
49
|
+
* never fires; the UI flags it (design §5.2.1).
|
|
50
|
+
*/
|
|
51
|
+
inert: string | null;
|
|
52
|
+
id: string;
|
|
53
|
+
match: unknown;
|
|
54
|
+
createdAt: Date;
|
|
55
|
+
expiresAt: Date | null;
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
instanceId: string;
|
|
58
|
+
operationId: string;
|
|
59
|
+
rateLimit: number | null;
|
|
60
|
+
windowSeconds: number | null;
|
|
61
|
+
reason: string;
|
|
62
|
+
createdBy: string | null;
|
|
63
|
+
updatedAt: Date | null;
|
|
64
|
+
lastTriggeredAt: Date | null;
|
|
65
|
+
strictMissAt: Date | null;
|
|
66
|
+
}[];
|
|
67
|
+
export declare function createRule(db: Db, manifest: Manifest, instanceId: string, raw: unknown, actor?: {
|
|
68
|
+
userId?: string;
|
|
69
|
+
}): {
|
|
70
|
+
operation: {
|
|
71
|
+
id: string;
|
|
72
|
+
key: string;
|
|
73
|
+
locked: boolean;
|
|
74
|
+
matchProfile: string | null;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Rules only apply at level `ask`: on an unreachable operation, or one at level `write`, a rule
|
|
78
|
+
* never fires; the UI flags it (design §5.2.1).
|
|
79
|
+
*/
|
|
80
|
+
inert: string | null;
|
|
81
|
+
id: string;
|
|
82
|
+
match: unknown;
|
|
83
|
+
createdAt: Date;
|
|
84
|
+
expiresAt: Date | null;
|
|
85
|
+
enabled: boolean;
|
|
86
|
+
instanceId: string;
|
|
87
|
+
operationId: string;
|
|
88
|
+
rateLimit: number | null;
|
|
89
|
+
windowSeconds: number | null;
|
|
90
|
+
reason: string;
|
|
91
|
+
createdBy: string | null;
|
|
92
|
+
updatedAt: Date | null;
|
|
93
|
+
lastTriggeredAt: Date | null;
|
|
94
|
+
strictMissAt: Date | null;
|
|
95
|
+
};
|
|
96
|
+
export declare function updateRule(db: Db, manifest: Manifest, instanceId: string, ruleId: string, raw: unknown, actor?: {
|
|
97
|
+
userId?: string;
|
|
98
|
+
}): {
|
|
99
|
+
operation: {
|
|
100
|
+
id: string;
|
|
101
|
+
key: string;
|
|
102
|
+
locked: boolean;
|
|
103
|
+
matchProfile: string | null;
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Rules only apply at level `ask`: on an unreachable operation, or one at level `write`, a rule
|
|
107
|
+
* never fires; the UI flags it (design §5.2.1).
|
|
108
|
+
*/
|
|
109
|
+
inert: string | null;
|
|
110
|
+
id: string;
|
|
111
|
+
match: unknown;
|
|
112
|
+
createdAt: Date;
|
|
113
|
+
expiresAt: Date | null;
|
|
114
|
+
enabled: boolean;
|
|
115
|
+
instanceId: string;
|
|
116
|
+
operationId: string;
|
|
117
|
+
rateLimit: number | null;
|
|
118
|
+
windowSeconds: number | null;
|
|
119
|
+
reason: string;
|
|
120
|
+
createdBy: string | null;
|
|
121
|
+
updatedAt: Date | null;
|
|
122
|
+
lastTriggeredAt: Date | null;
|
|
123
|
+
strictMissAt: Date | null;
|
|
124
|
+
};
|
|
125
|
+
export declare function deleteRule(db: Db, instanceId: string, ruleId: string, actor?: {
|
|
126
|
+
userId?: string;
|
|
127
|
+
}): void;
|
|
128
|
+
export {};
|