@capybara-hq/kernel 0.1.0-rc.140 → 0.1.0-rc.142
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/agents/models.controller.d.ts +9 -2
- package/dist/agents/models.controller.js +35 -4
- package/dist/catalog/catalog-scope.service.js +3 -0
- package/dist/catalog/catalog.module.js +8 -0
- package/dist/catalog/catalog.service.d.ts +2 -2
- package/dist/catalog/catalog.service.js +3 -3
- package/dist/catalog/component-scope.controller.d.ts +2 -1
- package/dist/catalog/component-scope.controller.js +5 -2
- package/dist/catalog/component-scope.service.d.ts +16 -0
- package/dist/catalog/component-scope.service.js +21 -1
- package/dist/catalog/enable-decision.d.ts +34 -0
- package/dist/catalog/enable-decision.js +17 -0
- package/dist/catalog/enable-requests.d.ts +186 -0
- package/dist/catalog/enable-requests.js +767 -0
- package/dist/catalog/enable-targets.d.ts +63 -0
- package/dist/catalog/enable-targets.js +94 -0
- package/dist/catalog/org-catalog.controller.d.ts +11 -3
- package/dist/catalog/org-catalog.controller.js +28 -5
- package/dist/catalog/org-catalog.schemas.d.ts +38 -0
- package/dist/catalog/org-catalog.schemas.js +31 -0
- package/dist/catalog/org-catalog.service.js +1 -1
- package/dist/catalog/org-extension.controller.d.ts +1 -1
- package/dist/catalog/owned-items.js +6 -1
- package/dist/catalog/plugin-install.js +7 -2
- package/dist/catalog/plugin-upgrade.js +3 -0
- package/dist/catalog/request-rules.d.ts +7 -5
- package/dist/catalog/request-rules.js +53 -14
- package/dist/catalog/requests.service.d.ts +137 -4
- package/dist/catalog/requests.service.js +574 -28
- package/dist/catalog/scope-writer.d.ts +20 -3
- package/dist/catalog/scope-writer.js +32 -3
- package/dist/config/schema.d.ts +1 -0
- package/dist/config/schema.js +10 -0
- package/dist/ctx/bus.js +3 -2
- package/dist/db/kernel-schema.d.ts +3 -2
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +7 -0
- package/dist/extensions/admin-ops.js +2 -0
- package/dist/extensions/admin-view.js +2 -1
- package/dist/extensions/host.js +1 -1
- package/dist/extensions/org-config.d.ts +8 -0
- package/dist/extensions/org-config.js +20 -5
- package/dist/extensions/permission-view.d.ts +7 -0
- package/dist/extensions/plugin-guides.d.ts +1 -1
- package/dist/extensions/plugin-guides.js +1 -1
- package/dist/extensions/plugin-items.d.ts +8 -0
- package/dist/extensions/plugin-items.js +8 -0
- package/dist/http/api.module.js +6 -0
- package/dist/infra/config/kernel-config.service.js +2 -0
- package/dist/infra/queue/run-job.js +3 -1
- package/dist/invalidation/hints.d.ts +56 -0
- package/dist/invalidation/hints.js +67 -0
- package/dist/invalidation/invalidation-hints.d.ts +24 -0
- package/dist/invalidation/invalidation-hints.js +59 -0
- package/dist/invalidation/invalidation-hints.module.d.ts +8 -0
- package/dist/invalidation/invalidation-hints.module.js +23 -0
- package/dist/invalidation/me-stream.controller.d.ts +30 -0
- package/dist/invalidation/me-stream.controller.js +100 -0
- package/dist/invalidation/me-stream.d.ts +67 -0
- package/dist/invalidation/me-stream.js +351 -0
- package/dist/invalidation/me-stream.module.d.ts +7 -0
- package/dist/invalidation/me-stream.module.js +22 -0
- package/dist/kernel.module.js +4 -0
- package/dist/models/enablement.d.ts +12 -0
- package/dist/models/enablement.js +32 -0
- package/dist/platform/exclusions.js +5 -0
- package/dist/platform/ops/admin.ops.js +2 -2
- package/dist/platform/ops/catalog-admin.ops.js +1 -1
- package/dist/platform/ops/extensions-admin.ops.js +17 -9
- package/dist/platform/ops/extensions.ops.js +6 -3
- package/dist/platform/ops/org-plugins.ops.js +1 -1
- package/dist/platform/ops/plugins.ops.js +1 -1
- package/dist/platform/ops/requests.ops.js +55 -1
- package/dist/platform/ops/unavailable.js +2 -2
- package/dist/tools/builtins/sandbox/common.d.ts +3 -3
- package/dist/tools/builtins/sandbox/common.js +3 -3
- package/dist/tools/connectors.controller.d.ts +8 -1
- package/dist/tools/connectors.controller.js +14 -1
- package/dist/tools/connectors.service.d.ts +7 -0
- package/dist/tools/connectors.service.js +30 -0
- package/package.json +1 -1
- package/src/migrations/0233_enable_request_targets.sql +64 -0
|
@@ -11,23 +11,34 @@ import { randomUUID } from "node:crypto";
|
|
|
11
11
|
import { Inject, Injectable } from "@nestjs/common";
|
|
12
12
|
import { sql } from "kysely";
|
|
13
13
|
import { ApiError } from "../errors.js";
|
|
14
|
+
import { OrgExtensionConfigs } from "../extensions/org-config.js";
|
|
15
|
+
import { ExtensionRegistry } from "../extensions/registry.js";
|
|
14
16
|
import { avatarColumns, personAvatarUrl } from "../identity/account-avatar.js";
|
|
15
17
|
import { audit } from "../identity/audit.js";
|
|
16
18
|
import { readOrgMarketplaceBrowse } from "../identity/settings.js";
|
|
17
19
|
import { SuperAdminLock } from "../identity/super-admin.js";
|
|
18
20
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
19
21
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
22
|
+
import { withOrgScope } from "../infra/database/org-scope.js";
|
|
20
23
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
21
24
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
22
25
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
23
26
|
import { WithLock } from "../infra/lock/with-lock.decorator.js";
|
|
27
|
+
import { ModelEnablement } from "../models/enablement.js";
|
|
24
28
|
import { OrgLock } from "../orgs/org-lock.js";
|
|
25
29
|
import { roleAtLeast } from "../orgs/roles.js";
|
|
26
30
|
import { ORG_SANDBOX_ALLOW_HOSTS_MAX, OrgSettingsService, readOrgSettings, } from "../orgs/settings.service.js";
|
|
31
|
+
import { ProviderRegistry } from "../runtime/providers.js";
|
|
27
32
|
import { orgEffectiveHosts, orgHostOutsideCeiling } from "../sandbox/modes.js";
|
|
28
33
|
import { orgAllows } from "../sandbox/network-grants.js";
|
|
34
|
+
import { ConnectorsService } from "../tools/connectors.service.js";
|
|
29
35
|
import { CatalogService } from "./catalog.service.js";
|
|
30
36
|
import { CatalogScopeService } from "./catalog-scope.service.js";
|
|
37
|
+
import { ComponentScopeService } from "./component-scope.service.js";
|
|
38
|
+
import { loadComponents } from "./component-state.js";
|
|
39
|
+
import { withEnableDecision } from "./enable-decision.js";
|
|
40
|
+
import { modelListingOf, resolveSubject, resolveSubjects, settleRequestRows, } from "./enable-requests.js";
|
|
41
|
+
import { decodeModelId, readEnableDetail, sameTarget, targetKey, } from "./enable-targets.js";
|
|
31
42
|
import { assertEntryNotDeprecated, loadMarketplaceEntry } from "./marketplaces.service.js";
|
|
32
43
|
import { OrgCatalogService } from "./org-catalog.service.js";
|
|
33
44
|
import { PluginImport } from "./plugin-import.js";
|
|
@@ -37,11 +48,14 @@ export const NETWORK_REQUESTS_PENDING_MAX = 10;
|
|
|
37
48
|
/** 申请列表一次最多返回的条数。 */
|
|
38
49
|
export const CATALOG_REQUESTS_LIST_MAX = 200;
|
|
39
50
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
51
|
+
/** Keeps an `in` list non-empty. */
|
|
52
|
+
const NIL_UUID = "00000000-0000-0000-0000-000000000000";
|
|
40
53
|
const requestNotFound = () => new ApiError("catalog.request_not_found", "Catalog request not found");
|
|
41
54
|
const itemNotFound = () => new ApiError("catalog.item_not_found", "Catalog item not found");
|
|
42
55
|
const itemDisabled = () => new ApiError("catalog.item_disabled", "This catalog item is disabled by the super admin and cannot be requested");
|
|
43
56
|
const alreadyDecided = () => new ApiError("catalog.request_decided", "This request has already been decided");
|
|
44
57
|
const pendingExists = () => new ApiError("catalog.request_pending", "This organization already has a pending request of this kind for it");
|
|
58
|
+
const enableStale = (why) => new ApiError("catalog.request_stale", `What this request asks for has changed (${why}); it was cancelled and the member can ask again for what is needed now`);
|
|
45
59
|
const deploymentEgressNone = () => new ApiError("catalog.request_invalid", "The platform turned sandbox network off; only a super admin can turn it on");
|
|
46
60
|
/**
|
|
47
61
|
* The allowlist the organization's sandbox-mode devices reach (org egress ceiling spec §D3): the
|
|
@@ -92,13 +106,25 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
92
106
|
orgSettings;
|
|
93
107
|
txHost;
|
|
94
108
|
lockService;
|
|
109
|
+
orgExtensions;
|
|
110
|
+
components;
|
|
111
|
+
models;
|
|
112
|
+
connectors;
|
|
113
|
+
extensions;
|
|
114
|
+
providers;
|
|
95
115
|
constructor(kdb,
|
|
96
116
|
/** The org row and super admin locks: both join the caller's transaction. */
|
|
97
117
|
orgLock, adminLock, orgCatalog, scope, catalog, config, pluginImport,
|
|
98
118
|
/** Approving a network request writes the org's `deviceNetwork` through the settings path. */
|
|
99
119
|
orgSettings, txHost,
|
|
100
120
|
/** Serializes the approvals of one `import` request (`@WithLock`). */
|
|
101
|
-
lockService
|
|
121
|
+
lockService,
|
|
122
|
+
/**
|
|
123
|
+
* The ordinary switch writers an approved point-of-use enable request calls for its target
|
|
124
|
+
* (extension, component, model, connector; a plugin goes through `orgCatalog.setSwitch`), and
|
|
125
|
+
* the deployment's live state a request is checked against (loaded extensions, listed models).
|
|
126
|
+
*/
|
|
127
|
+
orgExtensions, components, models, connectors, extensions, providers) {
|
|
102
128
|
this.kdb = kdb;
|
|
103
129
|
this.orgLock = orgLock;
|
|
104
130
|
this.adminLock = adminLock;
|
|
@@ -110,6 +136,12 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
110
136
|
this.orgSettings = orgSettings;
|
|
111
137
|
this.txHost = txHost;
|
|
112
138
|
this.lockService = lockService;
|
|
139
|
+
this.orgExtensions = orgExtensions;
|
|
140
|
+
this.components = components;
|
|
141
|
+
this.models = models;
|
|
142
|
+
this.connectors = connectors;
|
|
143
|
+
this.extensions = extensions;
|
|
144
|
+
this.providers = providers;
|
|
113
145
|
}
|
|
114
146
|
/** The kernel-schema handle of the current context (the transaction, else the pool). */
|
|
115
147
|
get db() {
|
|
@@ -174,6 +206,14 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
174
206
|
kind: input.kind,
|
|
175
207
|
// still written for the old NOT NULL / CHECK, never read
|
|
176
208
|
stage: input.kind === "enable" ? "org" : "deployment",
|
|
209
|
+
// an enable request names its switch, the plugin itself (0233)
|
|
210
|
+
...(input.kind === "enable" && {
|
|
211
|
+
detail: JSON.stringify({
|
|
212
|
+
target: { kind: "plugin", id: itemId },
|
|
213
|
+
subject: { kind: "plugin", id: itemId },
|
|
214
|
+
bundle: null,
|
|
215
|
+
}),
|
|
216
|
+
}),
|
|
177
217
|
requested_by: actor,
|
|
178
218
|
note: input.note ?? "",
|
|
179
219
|
})
|
|
@@ -300,6 +340,174 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
300
340
|
});
|
|
301
341
|
return id;
|
|
302
342
|
}
|
|
343
|
+
/**
|
|
344
|
+
* A member asks the org admins to turn on what a picker subject needs (`POST /orgs/:orgId/
|
|
345
|
+
* catalog-requests/enable`, Platform `plugins.request-enable`; `catalog.request`, checked
|
|
346
|
+
* first). The kernel resolves the subject's owning switch in the org ({@link resolveSubject}): an
|
|
347
|
+
* off plugin, a stopped extension, a switched-off skill, agent template or connector template, a
|
|
348
|
+
* model the org disabled, or a disabled connector. Hidden (scope, an unshared plugin of another
|
|
349
|
+
* org, a model its provider does not list or the platform disabled) -> the kind's not-found code;
|
|
350
|
+
* disabled by the super admin -> `catalog.item_disabled` (org admins; a member gets not-found);
|
|
351
|
+
* not offered, built in, already on, a provider unavailable or needing configuration ->
|
|
352
|
+
* `catalog.request_invalid`; a plugin or extension that cannot run on the platform now ->
|
|
353
|
+
* `catalog.request_invalid`. A pending request for the same switch is returned (`created`
|
|
354
|
+
* false): to another member redacted ({@link redactFor}), and that member becomes a follower
|
|
355
|
+
* who gets the decision notice too. Audits `catalog.requested`.
|
|
356
|
+
*/
|
|
357
|
+
async requestEnable(orgId, actor, input, opts = {}) {
|
|
358
|
+
await this.authorize(orgId, actor, "catalog.request");
|
|
359
|
+
const isAdmin = await this.isOrgAdmin(orgId, actor);
|
|
360
|
+
const live = await this.liveFor(orgId, [input.subject]);
|
|
361
|
+
const resolved = await resolveSubject(this.db, orgId, input.subject, isAdmin, live);
|
|
362
|
+
if (!resolved.ok)
|
|
363
|
+
throw resolved.error;
|
|
364
|
+
await this.assertTargetLive(orgId, actor, resolved.target);
|
|
365
|
+
const { id, created } = await this.writeEnableRequest(orgId, actor, isAdmin, input, live, opts);
|
|
366
|
+
return { request: await this.viewFor(id, actor), created };
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* What the providers list, read once when a subject is a model: for the org (its enabled
|
|
370
|
+
* providers), and the platform's view (a provider off in the org: its model names).
|
|
371
|
+
*/
|
|
372
|
+
async liveFor(orgId, subjects) {
|
|
373
|
+
const loaded = this.loadedExtensions();
|
|
374
|
+
if (!subjects.some((s) => s.kind === "model"))
|
|
375
|
+
return { loaded };
|
|
376
|
+
const [org, platform] = await Promise.all([
|
|
377
|
+
this.providers.catalog(orgId),
|
|
378
|
+
this.providers.listWithStatus(),
|
|
379
|
+
]);
|
|
380
|
+
return { models: modelListingOf(org), platformModels: modelListingOf(platform), loaded };
|
|
381
|
+
}
|
|
382
|
+
/** The extensions loaded on this replica (a config-declared one has no install row). */
|
|
383
|
+
loadedExtensions() {
|
|
384
|
+
return new Set(this.extensions.manifests().map((m) => m.name));
|
|
385
|
+
}
|
|
386
|
+
/** One request as `viewer` reads it ({@link personalize}). */
|
|
387
|
+
async viewFor(id, viewer) {
|
|
388
|
+
const row = await this.baseQuery().where("r.id", "=", id).executeTakeFirst();
|
|
389
|
+
if (!row)
|
|
390
|
+
throw requestNotFound();
|
|
391
|
+
const [view] = await this.toViews([row]);
|
|
392
|
+
return personalize(view, viewer, row.detail);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* The checks of a target that need the deployment's live state: a plugin whose extension
|
|
396
|
+
* cannot run on the platform now, an extension that is not loaded.
|
|
397
|
+
*/
|
|
398
|
+
async assertTargetLive(orgId, actor, target) {
|
|
399
|
+
if (target.kind === "plugin") {
|
|
400
|
+
const [item] = await this.orgCatalog.list(orgId, actor, target.id);
|
|
401
|
+
if (!item)
|
|
402
|
+
throw itemNotFound();
|
|
403
|
+
if (item.state === "unavailable") {
|
|
404
|
+
throw new ApiError("catalog.request_invalid", `This plugin's extension cannot run on the platform now (${item.reason ?? "unavailable"}); only the super admin can fix that`);
|
|
405
|
+
}
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (target.kind === "extension" && this.extensions.current(target.id) === undefined) {
|
|
409
|
+
throw new ApiError("catalog.request_invalid", `Extension ${target.id} cannot run on the platform now; only the super admin can fix that`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
async writeEnableRequest(orgId, actor, isAdmin, input, live, opts) {
|
|
413
|
+
const t = this.db;
|
|
414
|
+
await this.orgLock.lockAndAuthorize(orgId, actor, "catalog.request");
|
|
415
|
+
// resolved again under the org row lock (the switch writers take it too)
|
|
416
|
+
const resolved = await resolveSubject(t, orgId, input.subject, isAdmin, live);
|
|
417
|
+
if (!resolved.ok)
|
|
418
|
+
throw resolved.error;
|
|
419
|
+
const { target, bundle } = resolved;
|
|
420
|
+
// a model whose provider list cannot be read is recorded as the provider extension
|
|
421
|
+
const subject = resolved.subject ?? input.subject;
|
|
422
|
+
const existing = await t
|
|
423
|
+
.selectFrom("catalog_requests")
|
|
424
|
+
.select(["id", "requested_by", "detail"])
|
|
425
|
+
.where("org_id", "=", orgId)
|
|
426
|
+
.where("kind", "=", "enable")
|
|
427
|
+
.where("status", "=", "pending")
|
|
428
|
+
.where(sql `detail -> 'target' ->> 'kind' = ${target.kind}`)
|
|
429
|
+
.where(sql `detail -> 'target' ->> 'id' = ${target.id}`)
|
|
430
|
+
.forUpdate()
|
|
431
|
+
.executeTakeFirst();
|
|
432
|
+
// a pending request whose own subject went stale (removed, hidden, another switch first) is
|
|
433
|
+
// settled first (row locked above), so it never absorbs new askers; a fresh request follows
|
|
434
|
+
const stale = existing !== undefined &&
|
|
435
|
+
(await settleRequestRows(t, orgId, [existing], { kind: "reconcile" }, () => ({
|
|
436
|
+
actor: null,
|
|
437
|
+
}))).has(existing.id);
|
|
438
|
+
if (existing && !stale) {
|
|
439
|
+
// another member asking for the same switch follows the request: the decision or a
|
|
440
|
+
// cancellation reaches them in the inbox too; each follower keeps their own latest subject,
|
|
441
|
+
// judged on its own at settlement (the row may have dropped stale followers just above)
|
|
442
|
+
const current = await t
|
|
443
|
+
.selectFrom("catalog_requests")
|
|
444
|
+
.select("detail")
|
|
445
|
+
.where("id", "=", existing.id)
|
|
446
|
+
.where("org_id", "=", orgId)
|
|
447
|
+
.executeTakeFirstOrThrow();
|
|
448
|
+
const followers = readEnableDetail(current.detail)?.followers ?? [];
|
|
449
|
+
const mine = followers.find((f) => f.accountId === actor);
|
|
450
|
+
if (existing.requested_by !== actor && !(mine && sameTarget(mine.subject, subject))) {
|
|
451
|
+
const raw = (current.detail ?? {});
|
|
452
|
+
const next = {
|
|
453
|
+
...raw,
|
|
454
|
+
followers: [
|
|
455
|
+
...followers.filter((f) => f.accountId !== actor),
|
|
456
|
+
{ accountId: actor, subject },
|
|
457
|
+
],
|
|
458
|
+
};
|
|
459
|
+
await t
|
|
460
|
+
.updateTable("catalog_requests")
|
|
461
|
+
.set({ detail: JSON.stringify(next) })
|
|
462
|
+
.where("id", "=", existing.id)
|
|
463
|
+
.where("org_id", "=", orgId)
|
|
464
|
+
.execute();
|
|
465
|
+
}
|
|
466
|
+
return { id: existing.id, created: false };
|
|
467
|
+
}
|
|
468
|
+
const detail = { target, subject, bundle };
|
|
469
|
+
const id = randomUUID();
|
|
470
|
+
try {
|
|
471
|
+
await t
|
|
472
|
+
.insertInto("catalog_requests")
|
|
473
|
+
.values({
|
|
474
|
+
id,
|
|
475
|
+
org_id: orgId,
|
|
476
|
+
item_id: target.kind === "plugin" ? target.id : null,
|
|
477
|
+
kind: "enable",
|
|
478
|
+
// still written for the old NOT NULL / CHECK, never read
|
|
479
|
+
stage: "org",
|
|
480
|
+
detail: JSON.stringify(detail),
|
|
481
|
+
requested_by: actor,
|
|
482
|
+
note: input.note ?? "",
|
|
483
|
+
})
|
|
484
|
+
.execute();
|
|
485
|
+
}
|
|
486
|
+
catch (e) {
|
|
487
|
+
if (isUniqueViolation(e))
|
|
488
|
+
throw pendingExists();
|
|
489
|
+
throw e;
|
|
490
|
+
}
|
|
491
|
+
await audit(t, {
|
|
492
|
+
actorAccountId: actor,
|
|
493
|
+
...(opts.actorTokenId !== undefined && { actorTokenId: opts.actorTokenId }),
|
|
494
|
+
orgId,
|
|
495
|
+
action: "catalog.requested",
|
|
496
|
+
target: id,
|
|
497
|
+
detail: { kind: "enable", target, subject, ...opts.auditDetail },
|
|
498
|
+
});
|
|
499
|
+
return { id, created: true };
|
|
500
|
+
}
|
|
501
|
+
/** The account is an admin (or owner) of the org (the role read live). */
|
|
502
|
+
async isOrgAdmin(orgId, accountId) {
|
|
503
|
+
const m = await this.db
|
|
504
|
+
.selectFrom("memberships")
|
|
505
|
+
.select("role")
|
|
506
|
+
.where("org_id", "=", orgId)
|
|
507
|
+
.where("account_id", "=", accountId)
|
|
508
|
+
.executeTakeFirst();
|
|
509
|
+
return m !== undefined && roleAtLeast(m.role, "admin");
|
|
510
|
+
}
|
|
303
511
|
/**
|
|
304
512
|
* An org admin asks for a marketplace entry to be imported (`POST /orgs/:orgId/
|
|
305
513
|
* marketplace-requests`, `catalog.decide`). The deployment setting `orgMarketplaceBrowse` off ->
|
|
@@ -398,24 +606,142 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
398
606
|
if (!UUID_RE.test(requestId))
|
|
399
607
|
throw requestNotFound();
|
|
400
608
|
if (decision.approve) {
|
|
401
|
-
const
|
|
609
|
+
const detail = await this.pendingOrgRequest(orgId, actor, requestId);
|
|
402
610
|
// a network request is approved inside the decision (writeDecision), under the row lock
|
|
403
|
-
if (
|
|
404
|
-
|
|
405
|
-
}
|
|
611
|
+
if (detail !== null)
|
|
612
|
+
return this.approveEnable(orgId, actor, requestId, detail, opts);
|
|
406
613
|
}
|
|
407
614
|
await this.writeDecision(requestId, actor, decision, opts, { orgId });
|
|
408
|
-
return this.view(requestId);
|
|
615
|
+
return { request: await this.view(requestId), notice: null };
|
|
409
616
|
}
|
|
410
617
|
/**
|
|
411
|
-
*
|
|
412
|
-
*
|
|
618
|
+
* Approves an enable request (spec: checked again at decision). While the stored target is
|
|
619
|
+
* still the subject's owning switch, its ordinary writer is called with the approval in scope
|
|
620
|
+
* ({@link withEnableDecision}): the writer's own transaction re-checks the request is pending
|
|
621
|
+
* (FOR UPDATE) and records the approval with the switch write, so a concurrent reject or second
|
|
622
|
+
* approval either commits first (this one fails with `catalog.request_decided` and its write
|
|
623
|
+
* rolls back) or after (it finds the request decided). When the writer has nothing to write, or
|
|
624
|
+
* the target is no longer the owning switch, {@link finishEnable} decides under the row lock: a
|
|
625
|
+
* subject usable now is approved; one that needs another switch is cancelled and the admin gets
|
|
626
|
+
* `catalog.request_stale`.
|
|
627
|
+
*/
|
|
628
|
+
async approveEnable(orgId, actor, requestId, detail, opts) {
|
|
629
|
+
const subject = detail.subject ?? detail.target;
|
|
630
|
+
const live = await this.liveFor(orgId, [subject]);
|
|
631
|
+
const resolved = await resolveSubject(this.db, orgId, subject, true, live);
|
|
632
|
+
if (resolved.ok && sameTarget(resolved.target, detail.target)) {
|
|
633
|
+
const d = {
|
|
634
|
+
orgId,
|
|
635
|
+
requestId,
|
|
636
|
+
target: detail.target,
|
|
637
|
+
actor,
|
|
638
|
+
actorTokenId: opts.actorTokenId,
|
|
639
|
+
auditDetail: { requestId, ...opts.auditDetail },
|
|
640
|
+
};
|
|
641
|
+
await withEnableDecision(d, () => this.applyEnable(orgId, actor, requestId, detail.target, opts));
|
|
642
|
+
// the committed row says what the writer's transaction decided (a rolled-back write, e.g.
|
|
643
|
+
// a failed install, left it pending and threw)
|
|
644
|
+
const after = await this.db
|
|
645
|
+
.selectFrom("catalog_requests")
|
|
646
|
+
.select(["status", "detail"])
|
|
647
|
+
.where("id", "=", requestId)
|
|
648
|
+
.where("org_id", "=", orgId)
|
|
649
|
+
.executeTakeFirstOrThrow();
|
|
650
|
+
// decided by someone else meanwhile (this writer had nothing to write)
|
|
651
|
+
if (after.status !== "pending" && d.settled !== true)
|
|
652
|
+
throw alreadyDecided();
|
|
653
|
+
if (after.status === "approved") {
|
|
654
|
+
return { request: await this.view(requestId), notice: null };
|
|
655
|
+
}
|
|
656
|
+
if (after.status === "cancelled") {
|
|
657
|
+
// the switch is on, but the member's item needs another switch now
|
|
658
|
+
return { request: await this.view(requestId), notice: "needs_another_switch" };
|
|
659
|
+
}
|
|
660
|
+
if (after.status !== "pending")
|
|
661
|
+
throw alreadyDecided();
|
|
662
|
+
}
|
|
663
|
+
const why = await this.finishEnable(orgId, actor, requestId, live, opts);
|
|
664
|
+
if (why !== null)
|
|
665
|
+
throw enableStale(why);
|
|
666
|
+
return { request: await this.view(requestId), notice: null };
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Turns an approved enable request's target on through the ordinary switch writer of its kind,
|
|
670
|
+
* as the deciding admin (each re-checks its own rules and role under the org row lock): the
|
|
671
|
+
* plugin switch (an install), the extension switch, the component switch, the org's model
|
|
672
|
+
* list, or the connector's status. The writer settles the org's pending requests for the target
|
|
673
|
+
* in its transaction ({@link settleEnableRequests}).
|
|
674
|
+
*/
|
|
675
|
+
async applyEnable(orgId, actor, requestId, target, opts) {
|
|
676
|
+
const auditDetail = { requestId, ...opts.auditDetail };
|
|
677
|
+
switch (target.kind) {
|
|
678
|
+
case "plugin":
|
|
679
|
+
await this.orgCatalog.setSwitch(orgId, actor, target.id, { enabled: true }, opts.actorTokenId, auditDetail);
|
|
680
|
+
return;
|
|
681
|
+
case "extension":
|
|
682
|
+
await this.orgExtensions.put(orgId, target.id, { enabled: true }, actor, opts.actorTokenId, {
|
|
683
|
+
auditDetail,
|
|
684
|
+
});
|
|
685
|
+
return;
|
|
686
|
+
case "skill":
|
|
687
|
+
case "agent_template":
|
|
688
|
+
case "connector_template":
|
|
689
|
+
await this.components.setSwitch(orgId, actor, target.kind, target.id, { enabled: true }, opts.actorTokenId, auditDetail);
|
|
690
|
+
return;
|
|
691
|
+
case "model": {
|
|
692
|
+
const ref = decodeModelId(target.id);
|
|
693
|
+
if (ref === null)
|
|
694
|
+
throw new ApiError("model.not_found", "Model not found");
|
|
695
|
+
await this.models.setOrg(orgId, ref.provider, { disable: [], enable: [ref.model] }, { accountId: actor, tokenId: opts.actorTokenId, auditDetail });
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
case "connector":
|
|
699
|
+
await this.connectors.update(orgId, target.id, actor, { status: "active" }, opts.actorTokenId, auditDetail);
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Decides an enable request whose writer recorded nothing, under the org row lock and the
|
|
705
|
+
* request row FOR UPDATE: decided meanwhile -> `catalog.request_decided`; the subject usable
|
|
706
|
+
* now -> approved; otherwise cancelled (`target_changed`, the member asks again). Returns why it
|
|
707
|
+
* was cancelled, else null.
|
|
708
|
+
*/
|
|
709
|
+
async finishEnable(orgId, actor, requestId, live, opts) {
|
|
710
|
+
await this.orgLock.lockAndAuthorize(orgId, actor, "catalog.decide");
|
|
711
|
+
const row = await this.db
|
|
712
|
+
.selectFrom("catalog_requests")
|
|
713
|
+
.select(["id", "status", "detail", "item_id"])
|
|
714
|
+
.where("id", "=", requestId)
|
|
715
|
+
.where("org_id", "=", orgId)
|
|
716
|
+
.where("kind", "=", "enable")
|
|
717
|
+
.forUpdate()
|
|
718
|
+
.executeTakeFirst();
|
|
719
|
+
if (!row)
|
|
720
|
+
throw requestNotFound();
|
|
721
|
+
if (row.status !== "pending")
|
|
722
|
+
throw alreadyDecided();
|
|
723
|
+
const detail = readEnableDetail(row.detail);
|
|
724
|
+
if (detail === null)
|
|
725
|
+
throw requestNotFound();
|
|
726
|
+
const outcomes = await settleRequestRows(this.db, orgId, [row], { kind: "final" }, () => ({
|
|
727
|
+
actor,
|
|
728
|
+
actorTokenId: opts.actorTokenId,
|
|
729
|
+
auditDetail: { requestId, ...opts.auditDetail },
|
|
730
|
+
}));
|
|
731
|
+
if (outcomes.get(requestId) === "approved")
|
|
732
|
+
return null;
|
|
733
|
+
const now = await resolveSubject(this.db, orgId, detail.subject ?? detail.target, true, live);
|
|
734
|
+
return now.ok ? "another switch has to be turned on first" : now.error.message;
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* The detail (target, subject) of a pending `enable` request of the org, null for a
|
|
738
|
+
* `network_allowlist` one (the decider re-checked).
|
|
413
739
|
*/
|
|
414
740
|
async pendingOrgRequest(orgId, actor, requestId) {
|
|
415
741
|
await this.orgLock.lockAndAuthorize(orgId, actor, "catalog.decide");
|
|
416
742
|
const row = await this.db
|
|
417
743
|
.selectFrom("catalog_requests")
|
|
418
|
-
.select(["item_id", "status", "kind"])
|
|
744
|
+
.select(["item_id", "status", "kind", "detail"])
|
|
419
745
|
.where("id", "=", requestId)
|
|
420
746
|
.where("org_id", "=", orgId)
|
|
421
747
|
.where("kind", "in", [...ORG_KINDS])
|
|
@@ -429,9 +755,10 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
429
755
|
}
|
|
430
756
|
if (row.kind === "network_allowlist")
|
|
431
757
|
return null;
|
|
432
|
-
|
|
758
|
+
const detail = readEnableDetail(row.detail);
|
|
759
|
+
if (detail === null)
|
|
433
760
|
throw requestNotFound();
|
|
434
|
-
return
|
|
761
|
+
return detail;
|
|
435
762
|
}
|
|
436
763
|
/**
|
|
437
764
|
* A super admin decides an `access` or `import` request (re-checked inside the super
|
|
@@ -694,10 +1021,27 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
694
1021
|
.where("id", "=", requestId)
|
|
695
1022
|
.where("org_id", "=", orgId)
|
|
696
1023
|
.where("requested_by", "=", accountId)
|
|
697
|
-
.where("status", "in",
|
|
1024
|
+
.where("status", "in", DECIDED_STATUSES)
|
|
1025
|
+
.where(decidedForRequester("status", "detail"))
|
|
698
1026
|
.where("dismissed_at", "is", null)
|
|
699
1027
|
.executeTakeFirst();
|
|
700
|
-
if (r.numUpdatedRows
|
|
1028
|
+
if (r.numUpdatedRows > 0n)
|
|
1029
|
+
return;
|
|
1030
|
+
// a follower of an enable request dismisses their own copy of the notice
|
|
1031
|
+
const f = await this.db
|
|
1032
|
+
.updateTable("catalog_requests")
|
|
1033
|
+
.set({
|
|
1034
|
+
detail: sql `jsonb_set(detail, '{dismissedBy}', coalesce(detail -> 'dismissedBy', '[]'::jsonb) || to_jsonb(${accountId}::text))`,
|
|
1035
|
+
})
|
|
1036
|
+
.where("id", "=", requestId)
|
|
1037
|
+
.where("org_id", "=", orgId)
|
|
1038
|
+
.where("kind", "=", "enable")
|
|
1039
|
+
.where("status", "in", DECIDED_STATUSES)
|
|
1040
|
+
.where(decidedForRequester("status", "detail"))
|
|
1041
|
+
.where(sql `detail -> 'followers' @> jsonb_build_array(jsonb_build_object('accountId', ${accountId}::text))`)
|
|
1042
|
+
.where(sql `NOT coalesce(detail -> 'dismissedBy', '[]'::jsonb) ? ${accountId}`)
|
|
1043
|
+
.executeTakeFirst();
|
|
1044
|
+
if (f.numUpdatedRows === 0n)
|
|
701
1045
|
throw requestNotFound();
|
|
702
1046
|
}
|
|
703
1047
|
/** 超级管理员的申请列表(`access` / `import`;`status` 缺省 pending),新的在前。 */
|
|
@@ -750,22 +1094,53 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
750
1094
|
// nothing reveals the item's name to the org; a setup request of a deleted agent too
|
|
751
1095
|
const ids = [...new Set(rows.flatMap((r) => (r.item_id !== null ? [r.item_id] : [])))];
|
|
752
1096
|
const seen = await itemsSeenBy(this.db, orgId, ids);
|
|
1097
|
+
// an enable request whose subject or target the org can no longer see is left out too
|
|
1098
|
+
const hidden = await this.hiddenEnable(rows);
|
|
753
1099
|
return this.toViews(rows.filter((r) => r.kind === "connector_setup"
|
|
754
1100
|
? r.agent_id !== null && r.agent_deleted_at === null
|
|
755
|
-
: r.kind === "network_allowlist" ||
|
|
1101
|
+
: r.kind === "network_allowlist" ||
|
|
1102
|
+
(r.kind === "enable"
|
|
1103
|
+
? !hidden.has(r.id) && (r.item_id === null || seen.has(r.item_id))
|
|
1104
|
+
: r.item_id !== null && seen.has(r.item_id))), hidden);
|
|
756
1105
|
}
|
|
757
1106
|
/** The inbox: the account's decided requests in the org it has not dismissed, newest first. */
|
|
758
1107
|
async listDecidedForRequester(orgId, accountId) {
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
.
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
1108
|
+
// the requester's own (the partial requester index: status among the decided ones), then the
|
|
1109
|
+
// enable requests the account follows, as that follower reads them ({@link personalize})
|
|
1110
|
+
const [own, followed] = await Promise.all([
|
|
1111
|
+
this.baseQuery()
|
|
1112
|
+
.where("r.org_id", "=", orgId)
|
|
1113
|
+
.where("r.requested_by", "=", accountId)
|
|
1114
|
+
.where("r.status", "in", DECIDED_STATUSES)
|
|
1115
|
+
.where(decidedForRequester("r.status", "r.detail"))
|
|
1116
|
+
.where("r.dismissed_at", "is", null)
|
|
1117
|
+
.orderBy("r.decided_at", "desc")
|
|
1118
|
+
.orderBy("r.id")
|
|
1119
|
+
.limit(CATALOG_REQUESTS_LIST_MAX)
|
|
1120
|
+
.execute(),
|
|
1121
|
+
this.baseQuery()
|
|
1122
|
+
.where("r.org_id", "=", orgId)
|
|
1123
|
+
.where("r.kind", "=", "enable")
|
|
1124
|
+
.where("r.status", "in", DECIDED_STATUSES)
|
|
1125
|
+
.where(decidedForRequester("r.status", "r.detail"))
|
|
1126
|
+
.where(sql `r.detail -> 'followers' @> jsonb_build_array(jsonb_build_object('accountId', ${accountId}::text))`)
|
|
1127
|
+
.where(sql `NOT coalesce(r.detail -> 'dismissedBy', '[]'::jsonb) ? ${accountId}`)
|
|
1128
|
+
.orderBy("r.decided_at", "desc")
|
|
1129
|
+
.orderBy("r.id")
|
|
1130
|
+
.limit(CATALOG_REQUESTS_LIST_MAX)
|
|
1131
|
+
.execute(),
|
|
1132
|
+
]);
|
|
1133
|
+
const rows = [...own, ...followed];
|
|
1134
|
+
const views = (await this.toViews(rows)).map((v, i) => personalize(v, accountId, rows[i]?.detail));
|
|
1135
|
+
return views
|
|
1136
|
+
.sort((a, b) => (b.decidedAt ?? "") < (a.decidedAt ?? "")
|
|
1137
|
+
? -1
|
|
1138
|
+
: (b.decidedAt ?? "") > (a.decidedAt ?? "")
|
|
1139
|
+
? 1
|
|
1140
|
+
: a.id < b.id
|
|
1141
|
+
? -1
|
|
1142
|
+
: 1)
|
|
1143
|
+
.slice(0, CATALOG_REQUESTS_LIST_MAX);
|
|
769
1144
|
}
|
|
770
1145
|
/**
|
|
771
1146
|
* The account's own requests in the org, pending and decided (dismissed ones too), newest first;
|
|
@@ -789,7 +1164,9 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
789
1164
|
return (await this.toViews([row]))[0];
|
|
790
1165
|
}
|
|
791
1166
|
/** The views of `rows`, with the display names of the connector templates setup requests name. */
|
|
792
|
-
async toViews(rows
|
|
1167
|
+
async toViews(rows,
|
|
1168
|
+
/** The enable requests already known hidden (the admin inbox computed them): not read again. */
|
|
1169
|
+
hidden) {
|
|
793
1170
|
const ids = new Set();
|
|
794
1171
|
for (const r of rows) {
|
|
795
1172
|
if (r.kind !== "connector_setup")
|
|
@@ -812,7 +1189,92 @@ let CatalogRequestsService = class CatalogRequestsService {
|
|
|
812
1189
|
names.set(t.id, dn ?? null);
|
|
813
1190
|
}
|
|
814
1191
|
}
|
|
815
|
-
|
|
1192
|
+
const enableNames = await this.enableNames(rows, hidden);
|
|
1193
|
+
return rows.map((r) => toView(r, names, enableNames));
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* The ids of `rows`' enable requests whose subject or target their org can no longer see (hidden
|
|
1197
|
+
* by scope, unshared, removed, or a model the platform disabled), resolved set-based per org.
|
|
1198
|
+
*/
|
|
1199
|
+
async hiddenEnable(rows) {
|
|
1200
|
+
const out = new Set();
|
|
1201
|
+
const byOrg = new Map();
|
|
1202
|
+
for (const r of rows) {
|
|
1203
|
+
if (r.kind !== "enable")
|
|
1204
|
+
continue;
|
|
1205
|
+
const d = readEnableDetail(r.detail);
|
|
1206
|
+
if (d === null)
|
|
1207
|
+
continue;
|
|
1208
|
+
const list = byOrg.get(r.org_id) ?? [];
|
|
1209
|
+
list.push({ id: r.id, things: [d.target, d.subject ?? d.target] });
|
|
1210
|
+
byOrg.set(r.org_id, list);
|
|
1211
|
+
}
|
|
1212
|
+
for (const [orgId, list] of byOrg) {
|
|
1213
|
+
const resolved = await withOrgScope(orgId, () => resolveSubjects(this.db, orgId, list.flatMap((l) => l.things), true, { loaded: this.loadedExtensions() }));
|
|
1214
|
+
for (const l of list) {
|
|
1215
|
+
if (l.things.some((t) => {
|
|
1216
|
+
const r = resolved.get(targetKey(t));
|
|
1217
|
+
return r !== undefined && !r.ok && NOT_FOUND_CODES.has(r.error.code);
|
|
1218
|
+
})) {
|
|
1219
|
+
out.add(l.id);
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
return out;
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* The names of the targets of `rows`' enable requests, by {@link targetKey}: a plugin's item
|
|
1227
|
+
* name and display name, a component's name (a connector template's display name), a
|
|
1228
|
+
* connector's name, an extension's name, a model's own name.
|
|
1229
|
+
*/
|
|
1230
|
+
async enableNames(rows, known) {
|
|
1231
|
+
const out = new Map();
|
|
1232
|
+
// never the name of a subject or target the request's org can no longer see
|
|
1233
|
+
const hidden = known ?? (await this.hiddenEnable(rows));
|
|
1234
|
+
rows = rows.filter((r) => !hidden.has(r.id));
|
|
1235
|
+
const byKind = new Map();
|
|
1236
|
+
for (const r of rows) {
|
|
1237
|
+
if (r.kind !== "enable")
|
|
1238
|
+
continue;
|
|
1239
|
+
const d = readEnableDetail(r.detail);
|
|
1240
|
+
if (d === null)
|
|
1241
|
+
continue;
|
|
1242
|
+
const t = d.target;
|
|
1243
|
+
if (t.kind === "plugin") {
|
|
1244
|
+
if (r.name !== null)
|
|
1245
|
+
out.set(targetKey(t), { name: r.name, displayName: r.display_name });
|
|
1246
|
+
}
|
|
1247
|
+
else if (t.kind === "extension")
|
|
1248
|
+
out.set(targetKey(t), { name: t.id, displayName: null });
|
|
1249
|
+
else if (t.kind === "model") {
|
|
1250
|
+
out.set(targetKey(t), { name: decodeModelId(t.id)?.model ?? null, displayName: null });
|
|
1251
|
+
}
|
|
1252
|
+
else {
|
|
1253
|
+
const ids = byKind.get(t.kind) ?? new Set();
|
|
1254
|
+
ids.add(t.id);
|
|
1255
|
+
byKind.set(t.kind, ids);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
for (const [kind, ids] of byKind) {
|
|
1259
|
+
if (kind === "connector") {
|
|
1260
|
+
const orgIds = [...new Set(rows.map((r) => r.org_id))];
|
|
1261
|
+
const found = await this.db
|
|
1262
|
+
.selectFrom("connectors")
|
|
1263
|
+
.select(["id", "name"])
|
|
1264
|
+
.where("org_id", "in", orgIds)
|
|
1265
|
+
.where("id", "in", [...ids].filter((id) => UUID_RE.test(id)).concat([NIL_UUID]))
|
|
1266
|
+
.where("deleted_at", "is", null)
|
|
1267
|
+
.execute();
|
|
1268
|
+
for (const c of found)
|
|
1269
|
+
out.set(targetKey({ kind, id: c.id }), { name: c.name, displayName: null });
|
|
1270
|
+
}
|
|
1271
|
+
else if (kind === "skill" || kind === "agent_template" || kind === "connector_template") {
|
|
1272
|
+
for (const c of await loadComponents(this.db, kind, { ids: [...ids] })) {
|
|
1273
|
+
out.set(targetKey({ kind, id: c.id }), { name: c.name, displayName: c.displayName });
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
return out;
|
|
816
1278
|
}
|
|
817
1279
|
baseQuery() {
|
|
818
1280
|
return (this.db
|
|
@@ -861,9 +1323,15 @@ __decorate([
|
|
|
861
1323
|
__decorate([
|
|
862
1324
|
Transactional()
|
|
863
1325
|
], CatalogRequestsService.prototype, "writeNetworkRequest", null);
|
|
1326
|
+
__decorate([
|
|
1327
|
+
Transactional()
|
|
1328
|
+
], CatalogRequestsService.prototype, "writeEnableRequest", null);
|
|
864
1329
|
__decorate([
|
|
865
1330
|
Transactional()
|
|
866
1331
|
], CatalogRequestsService.prototype, "writeImportRequest", null);
|
|
1332
|
+
__decorate([
|
|
1333
|
+
Transactional()
|
|
1334
|
+
], CatalogRequestsService.prototype, "finishEnable", null);
|
|
867
1335
|
__decorate([
|
|
868
1336
|
Transactional()
|
|
869
1337
|
], CatalogRequestsService.prototype, "pendingOrgRequest", null);
|
|
@@ -891,7 +1359,13 @@ CatalogRequestsService = __decorate([
|
|
|
891
1359
|
__param(7, Inject(PluginImport)),
|
|
892
1360
|
__param(8, Inject(OrgSettingsService)),
|
|
893
1361
|
__param(9, Inject(TransactionHost)),
|
|
894
|
-
__param(10, Inject(LockService))
|
|
1362
|
+
__param(10, Inject(LockService)),
|
|
1363
|
+
__param(11, Inject(OrgExtensionConfigs)),
|
|
1364
|
+
__param(12, Inject(ComponentScopeService)),
|
|
1365
|
+
__param(13, Inject(ModelEnablement)),
|
|
1366
|
+
__param(14, Inject(ConnectorsService)),
|
|
1367
|
+
__param(15, Inject(ExtensionRegistry)),
|
|
1368
|
+
__param(16, Inject(ProviderRegistry))
|
|
895
1369
|
], CatalogRequestsService);
|
|
896
1370
|
export { CatalogRequestsService };
|
|
897
1371
|
/** The view fields of a `connector_setup` request (ruling P4-T6-5); null for other kinds. */
|
|
@@ -940,7 +1414,78 @@ function networkFields(r) {
|
|
|
940
1414
|
agentId: typeof d.agentId === "string" ? d.agentId : null,
|
|
941
1415
|
};
|
|
942
1416
|
}
|
|
943
|
-
|
|
1417
|
+
/** The `enable` field of an `enable` request; null for other kinds. */
|
|
1418
|
+
function enableFields(r, names) {
|
|
1419
|
+
if (r.kind !== "enable")
|
|
1420
|
+
return null;
|
|
1421
|
+
const d = readEnableDetail(r.detail);
|
|
1422
|
+
if (d === null)
|
|
1423
|
+
return null;
|
|
1424
|
+
const n = names.get(targetKey(d.target));
|
|
1425
|
+
return {
|
|
1426
|
+
target: d.target,
|
|
1427
|
+
subject: d.subject ?? d.target,
|
|
1428
|
+
name: n?.name ?? null,
|
|
1429
|
+
displayName: n?.displayName ?? null,
|
|
1430
|
+
cancelReason: d.cancelReason ?? null,
|
|
1431
|
+
};
|
|
1432
|
+
}
|
|
1433
|
+
/**
|
|
1434
|
+
* The requests a requester sees decided in the inbox: approved, rejected, and enable requests a
|
|
1435
|
+
* switch change cancelled (`cancelReason: "target_changed"`: they ask again for what is needed).
|
|
1436
|
+
*/
|
|
1437
|
+
/** The statuses a requester's inbox notice can have (the requester index's predicate). */
|
|
1438
|
+
const DECIDED_STATUSES = ["approved", "rejected", "cancelled"];
|
|
1439
|
+
/** The codes a resolution answers when the org cannot see the subject (it is hidden). */
|
|
1440
|
+
const NOT_FOUND_CODES = new Set([
|
|
1441
|
+
"catalog.item_not_found",
|
|
1442
|
+
"extension.not_found",
|
|
1443
|
+
"component.not_found",
|
|
1444
|
+
"connector.not_found",
|
|
1445
|
+
"model.not_found",
|
|
1446
|
+
]);
|
|
1447
|
+
/**
|
|
1448
|
+
* A request as `viewer` reads it. The requester sees it whole. Another member (a follower of an
|
|
1449
|
+
* enable request, or the member a duplicate ask returns it to) never sees the requester, their
|
|
1450
|
+
* note, the reason or the decider; a follower sees their own subject and, once settled, their
|
|
1451
|
+
* own outcome (approved, or cancelled with `target_changed`), never the requester's.
|
|
1452
|
+
*/
|
|
1453
|
+
export function personalize(view, viewer, detail) {
|
|
1454
|
+
if (view.requestedBy === viewer)
|
|
1455
|
+
return view;
|
|
1456
|
+
const redacted = {
|
|
1457
|
+
...view,
|
|
1458
|
+
requestedBy: null,
|
|
1459
|
+
requestedByName: null,
|
|
1460
|
+
requestedByAvatarUrl: null,
|
|
1461
|
+
note: "",
|
|
1462
|
+
reason: null,
|
|
1463
|
+
decidedBy: null,
|
|
1464
|
+
};
|
|
1465
|
+
const follower = view.kind === "enable"
|
|
1466
|
+
? readEnableDetail(detail)?.followers?.find((f) => f.accountId === viewer)
|
|
1467
|
+
: undefined;
|
|
1468
|
+
if (follower === undefined || redacted.enable === null)
|
|
1469
|
+
return redacted;
|
|
1470
|
+
const settled = follower.outcome !== undefined && view.status !== "rejected";
|
|
1471
|
+
return {
|
|
1472
|
+
...redacted,
|
|
1473
|
+
...(settled && { status: follower.outcome === "approved" ? "approved" : "cancelled" }),
|
|
1474
|
+
enable: {
|
|
1475
|
+
...redacted.enable,
|
|
1476
|
+
subject: follower.subject,
|
|
1477
|
+
cancelReason: settled
|
|
1478
|
+
? follower.outcome === "target_changed"
|
|
1479
|
+
? "target_changed"
|
|
1480
|
+
: null
|
|
1481
|
+
: redacted.enable.cancelReason,
|
|
1482
|
+
},
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
function decidedForRequester(status, detail) {
|
|
1486
|
+
return sql `(${sql.ref(status)} IN ('approved', 'rejected') OR (${sql.ref(status)} = 'cancelled' AND ${sql.ref(detail)} ->> 'cancelReason' = 'target_changed'))`;
|
|
1487
|
+
}
|
|
1488
|
+
function toView(r, templateNames, enableNames = new Map()) {
|
|
944
1489
|
return {
|
|
945
1490
|
id: r.id,
|
|
946
1491
|
orgId: r.org_id,
|
|
@@ -954,6 +1499,7 @@ function toView(r, templateNames) {
|
|
|
954
1499
|
: null,
|
|
955
1500
|
...setupFields(r, templateNames),
|
|
956
1501
|
network: networkFields(r),
|
|
1502
|
+
enable: enableFields(r, enableNames),
|
|
957
1503
|
status: r.status,
|
|
958
1504
|
note: r.note,
|
|
959
1505
|
reason: r.reason,
|