@capybara-hq/kernel 0.1.0-rc.107 → 0.1.0-rc.109
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/bootstrap.json +13 -13
- package/dist/automations/acceptance.controller.d.ts +29 -0
- package/dist/automations/acceptance.controller.js +87 -0
- package/dist/automations/automations.module.js +4 -1
- package/dist/automations/automations.schemas.d.ts +46 -0
- package/dist/automations/automations.schemas.js +31 -0
- package/dist/automations/automations.service.js +10 -1
- package/dist/automations/errors.d.ts +5 -0
- package/dist/automations/errors.js +5 -0
- package/dist/automations/events/acceptance.d.ts +205 -0
- package/dist/automations/events/acceptance.js +822 -0
- package/dist/automations/events/deliver.d.ts +39 -0
- package/dist/automations/events/deliver.js +112 -0
- package/dist/automations/events/dispatcher.d.ts +26 -10
- package/dist/automations/events/dispatcher.js +98 -99
- package/dist/automations/events/sweep.job.d.ts +104 -18
- package/dist/automations/events/sweep.job.js +311 -45
- package/dist/automations/events/trigger-events.service.d.ts +31 -2
- package/dist/automations/events/trigger-events.service.js +113 -30
- package/dist/automations/store.d.ts +7 -3
- package/dist/automations/store.js +6 -0
- package/dist/automations/types.d.ts +5 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/config/schema.d.ts +3 -0
- package/dist/config/schema.js +14 -0
- package/dist/ctx/create-ctx.d.ts +24 -4
- package/dist/ctx/create-ctx.js +119 -6
- package/dist/db/kernel-schema.d.ts +89 -1
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +18 -0
- package/dist/extensions/hooks.d.ts +8 -5
- package/dist/extensions/hooks.js +2 -1
- package/dist/extensions/host.d.ts +12 -2
- package/dist/extensions/host.js +21 -1
- package/dist/extensions/lifecycle-hooks.d.ts +21 -7
- package/dist/extensions/lifecycle-hooks.js +51 -3
- package/dist/extensions/permission-set.d.ts +33 -1
- package/dist/extensions/permission-set.js +28 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +24 -1
- package/dist/extensions/run-lifecycle-owe.d.ts +67 -0
- package/dist/extensions/run-lifecycle-owe.js +84 -0
- package/dist/http/api.module.js +4 -0
- package/dist/inbox/inbox-item-actions.d.ts +37 -0
- package/dist/inbox/inbox-item-actions.js +69 -0
- package/dist/inbox/inbox-items.controller.d.ts +27 -0
- package/dist/inbox/inbox-items.controller.js +80 -0
- package/dist/inbox/inbox-items.service.d.ts +110 -0
- package/dist/inbox/inbox-items.service.js +324 -0
- package/dist/inbox/inbox.module.d.ts +2 -1
- package/dist/inbox/inbox.module.js +15 -4
- package/dist/inbox/inbox.schemas.d.ts +20 -1
- package/dist/inbox/inbox.schemas.js +16 -1
- package/dist/inbox/inbox.service.d.ts +29 -7
- package/dist/inbox/inbox.service.js +29 -8
- package/dist/inbox/token.d.ts +5 -0
- package/dist/inbox/token.js +5 -0
- package/dist/infra/config/kernel-config.service.js +2 -0
- package/dist/infra/outbox/registry.d.ts +17 -0
- package/dist/infra/outbox/registry.js +15 -0
- package/dist/kernel.module.js +6 -0
- package/dist/orgs/settings.service.d.ts +20 -0
- package/dist/orgs/settings.service.js +4 -0
- package/dist/platform/exclusions.js +15 -0
- package/dist/platform/operation.d.ts +8 -0
- package/dist/platform/ops/automations.ops.d.ts +3 -1
- package/dist/platform/ops/automations.ops.js +45 -3
- package/dist/platform/ops/inbox.ops.d.ts +3 -1
- package/dist/platform/ops/inbox.ops.js +69 -4
- package/dist/platform/ops/platform.ops.js +4 -3
- package/dist/plugin-reads/plugin-reads.service.d.ts +29 -0
- package/dist/plugin-reads/plugin-reads.service.js +210 -0
- package/dist/plugin-reads/token.d.ts +5 -0
- package/dist/plugin-reads/token.js +5 -0
- package/dist/push/jobs.d.ts +5 -0
- package/dist/push/jobs.js +2 -0
- package/dist/push/preferences.service.d.ts +2 -2
- package/dist/push/preferences.service.js +9 -3
- package/dist/push/push-owed.d.ts +2 -0
- package/dist/push/push-owed.js +10 -1
- package/dist/push/push.schemas.d.ts +5 -2
- package/dist/push/push.schemas.js +4 -3
- package/dist/push/recipients.d.ts +2 -0
- package/dist/push/recipients.js +44 -3
- package/dist/push/texts.d.ts +14 -5
- package/dist/push/texts.js +2 -1
- package/dist/push/triggers.d.ts +1 -1
- package/dist/runtime/run-finish.js +19 -1
- package/dist/runtime/run-jobs.d.ts +12 -2
- package/dist/runtime/run-jobs.js +41 -4
- package/dist/runtime/run-queue.js +10 -0
- package/dist/runtime/waits.js +24 -1
- package/dist/tools/builtins/platform.js +2 -0
- package/dist/tools/pipeline.d.ts +5 -0
- package/dist/tools/pipeline.js +13 -1
- package/dist/tools/registry.d.ts +6 -0
- package/package.json +3 -3
- package/src/migrations/0192_inbox_items.sql +50 -0
- package/src/migrations/0193_trigger_cause_handles.sql +49 -0
- package/src/migrations/0194_automation_acceptance.sql +83 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { randomUUID } from "node:crypto";
|
|
11
|
+
import { INBOX_LIMITS, InboxError, validateInboxPost, } from "@capybara-hq/plugin-sdk";
|
|
12
|
+
import { Inject, Injectable, Module } from "@nestjs/common";
|
|
13
|
+
import { sql } from "kysely";
|
|
14
|
+
import { ApiError } from "../errors.js";
|
|
15
|
+
import { isExtensionEnabled } from "../extensions/enabled.js";
|
|
16
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
17
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
18
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
19
|
+
import { PushOwed, PushOwedModule } from "../push/push-owed.js";
|
|
20
|
+
import { pluginDisplayNames } from "../tools/display-names.js";
|
|
21
|
+
import { INBOX_ITEMS_BACKEND } from "./token.js";
|
|
22
|
+
/** The most open personal items one inbox read returns. */
|
|
23
|
+
export const INBOX_ITEMS_LIST_MAX = 100;
|
|
24
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
25
|
+
const invalid = (message) => new InboxError("invalid_item", message);
|
|
26
|
+
/**
|
|
27
|
+
* A kernel item's link: an in-app path (`/…`, never `//`), no backslash or control character, at
|
|
28
|
+
* most {@link INBOX_LIMITS}.linkChars characters.
|
|
29
|
+
*/
|
|
30
|
+
const kernelLinkOk = (link) => link.startsWith("/") &&
|
|
31
|
+
!link.startsWith("//") &&
|
|
32
|
+
link.length <= INBOX_LIMITS.linkChars &&
|
|
33
|
+
!/[\\\p{Cc}]/u.test(link);
|
|
34
|
+
const notFound = () => new ApiError("inbox.item_not_found", "Inbox item not found");
|
|
35
|
+
/**
|
|
36
|
+
* Personal inbox items (`inbox_items`, collaboration spec §4.9): an item for one person in one
|
|
37
|
+
* org, posted by the kernel or by an extension (`ctx.inbox.post`, which calls {@link post} with the
|
|
38
|
+
* extension's own name: an extension cannot post as another plugin). Only the item's person lists
|
|
39
|
+
* it, clicks its actions ({@link InboxItemActions}) or dismisses it; everyone else, the org's
|
|
40
|
+
* admins included, gets `inbox.item_not_found`. An item past `expires_at` is gone. Every read and
|
|
41
|
+
* write filters by the item's org.
|
|
42
|
+
*/
|
|
43
|
+
let InboxItemsService = class InboxItemsService {
|
|
44
|
+
kdb;
|
|
45
|
+
txHost;
|
|
46
|
+
push;
|
|
47
|
+
constructor(kdb, txHost, push) {
|
|
48
|
+
this.kdb = kdb;
|
|
49
|
+
this.txHost = txHost;
|
|
50
|
+
this.push = push;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Posts `p` for `source` and owes its push fanout in the same transaction. The SDK's
|
|
54
|
+
* `validateInboxPost` checks the fields (a plugin item links only to `/p/<plugin>/…`; a kernel
|
|
55
|
+
* item to any in-app path and carries no actions); the account must be an active member of the
|
|
56
|
+
* org, and a plugin must be enabled there. With `dedupeKey`, an open unexpired item of the same
|
|
57
|
+
* (org, account, source, plugin, key) is returned instead (no new push); an expired one is
|
|
58
|
+
* closed and replaced. The caller runs it in the item's org scope.
|
|
59
|
+
*
|
|
60
|
+
* `opts.detail` (kernel items only, never from an extension): structured facts the web renders
|
|
61
|
+
* in the kind's own copy.
|
|
62
|
+
*
|
|
63
|
+
* Throws `InboxError`: `invalid_item` (a missing or malformed field, not an active member),
|
|
64
|
+
* `org_disabled` (the plugin is not enabled in the org).
|
|
65
|
+
*/
|
|
66
|
+
async post(source, p, opts = {}) {
|
|
67
|
+
if (typeof p !== "object" || p === null)
|
|
68
|
+
throw invalid("the item must be an object");
|
|
69
|
+
const plugin = source.kind === "plugin" ? source.plugin : null;
|
|
70
|
+
const errs = validateInboxPost(p, plugin ?? "").filter((e) => plugin !== null || e !== "link_invalid");
|
|
71
|
+
if (plugin === null) {
|
|
72
|
+
if (p.link !== undefined && (typeof p.link !== "string" || !kernelLinkOk(p.link)))
|
|
73
|
+
errs.push("link_invalid");
|
|
74
|
+
if (p.actions !== undefined && p.actions.length > 0)
|
|
75
|
+
errs.push("kernel_item_actions");
|
|
76
|
+
}
|
|
77
|
+
if (errs.length > 0)
|
|
78
|
+
throw invalid(`invalid inbox item: ${errs.join(", ")}`);
|
|
79
|
+
const db = this.kdb.db;
|
|
80
|
+
const org = await db
|
|
81
|
+
.selectFrom("orgs")
|
|
82
|
+
.select("id")
|
|
83
|
+
.where("id", "=", p.orgId)
|
|
84
|
+
.where("deleted_at", "is", null)
|
|
85
|
+
.executeTakeFirst();
|
|
86
|
+
if (org === undefined)
|
|
87
|
+
throw invalid(`org ${p.orgId} does not exist or is deleted`);
|
|
88
|
+
if (plugin !== null && !(await isExtensionEnabled(db, p.orgId, plugin)))
|
|
89
|
+
throw new InboxError("org_disabled", `${plugin} is not enabled in org ${p.orgId}`);
|
|
90
|
+
const member = await db
|
|
91
|
+
.selectFrom("memberships")
|
|
92
|
+
.innerJoin("accounts", "accounts.id", "memberships.account_id")
|
|
93
|
+
.select("accounts.id")
|
|
94
|
+
.where("memberships.org_id", "=", p.orgId)
|
|
95
|
+
.where("memberships.account_id", "=", p.accountId)
|
|
96
|
+
.where("accounts.status", "=", "active")
|
|
97
|
+
.executeTakeFirst();
|
|
98
|
+
if (member === undefined)
|
|
99
|
+
throw invalid(`account ${p.accountId} is not an active member of org ${p.orgId}`);
|
|
100
|
+
const dedupe = p.dedupeKey ?? null;
|
|
101
|
+
if (dedupe !== null) {
|
|
102
|
+
// an expired item with the key leaves the index's way: it is closed, a new one replaces it
|
|
103
|
+
await this.sameKey(p.orgId, p.accountId, source, dedupe)
|
|
104
|
+
.where("expires_at", "<=", sql `now()`)
|
|
105
|
+
.execute();
|
|
106
|
+
}
|
|
107
|
+
const actions = (p.actions ?? []).map((a) => ({
|
|
108
|
+
id: a.id,
|
|
109
|
+
label: { zh: a.label.zh, en: a.label.en },
|
|
110
|
+
...(a.style !== undefined && { style: a.style }),
|
|
111
|
+
uiAction: {
|
|
112
|
+
contribution: a.uiAction.contribution,
|
|
113
|
+
actionId: a.uiAction.actionId,
|
|
114
|
+
...(a.uiAction.payload !== undefined && { payload: a.uiAction.payload }),
|
|
115
|
+
},
|
|
116
|
+
}));
|
|
117
|
+
const seconds = p.expiresInSeconds ?? INBOX_LIMITS.defaultExpirySeconds;
|
|
118
|
+
const id = randomUUID();
|
|
119
|
+
const insert = () => db
|
|
120
|
+
.insertInto("inbox_items")
|
|
121
|
+
.values({
|
|
122
|
+
id,
|
|
123
|
+
org_id: p.orgId,
|
|
124
|
+
account_id: p.accountId,
|
|
125
|
+
source: source.kind,
|
|
126
|
+
plugin,
|
|
127
|
+
kind: p.kind,
|
|
128
|
+
title: p.title,
|
|
129
|
+
body: p.body ?? "",
|
|
130
|
+
link: p.link ?? null,
|
|
131
|
+
actions: JSON.stringify(actions),
|
|
132
|
+
dedupe_key: dedupe,
|
|
133
|
+
detail: source.kind === "kernel" && opts.detail !== undefined
|
|
134
|
+
? JSON.stringify(opts.detail)
|
|
135
|
+
: null,
|
|
136
|
+
expires_at: sql `now() + make_interval(secs => ${seconds})`,
|
|
137
|
+
})
|
|
138
|
+
// only the partial dedupe index can conflict (the id is new)
|
|
139
|
+
.onConflict((oc) => oc.doNothing())
|
|
140
|
+
.returning("id")
|
|
141
|
+
.executeTakeFirst();
|
|
142
|
+
// the conflicting item may close between the insert and the read: then the insert runs again
|
|
143
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
144
|
+
if ((await insert()) !== undefined) {
|
|
145
|
+
await this.push.oweInboxItem(id, p.orgId);
|
|
146
|
+
return { itemId: id };
|
|
147
|
+
}
|
|
148
|
+
if (dedupe === null)
|
|
149
|
+
throw new Error("inbox item insert conflicted without a key");
|
|
150
|
+
const existing = await db
|
|
151
|
+
.selectFrom("inbox_items")
|
|
152
|
+
.select("id")
|
|
153
|
+
.where("org_id", "=", p.orgId)
|
|
154
|
+
.where("account_id", "=", p.accountId)
|
|
155
|
+
.where("source", "=", source.kind)
|
|
156
|
+
.where(sql `coalesce(plugin, '') = ${plugin ?? ""}`)
|
|
157
|
+
.where("dedupe_key", "=", dedupe)
|
|
158
|
+
.where("state", "=", "open")
|
|
159
|
+
.executeTakeFirst();
|
|
160
|
+
if (existing !== undefined)
|
|
161
|
+
return { itemId: existing.id };
|
|
162
|
+
}
|
|
163
|
+
throw new Error("inbox item insert kept conflicting with a closed item");
|
|
164
|
+
}
|
|
165
|
+
/** The open items of (org, account, source, plugin) with `dedupe`, closed as `expired`. */
|
|
166
|
+
sameKey(orgId, accountId, source, dedupe) {
|
|
167
|
+
const plugin = source.kind === "plugin" ? source.plugin : "";
|
|
168
|
+
return this.kdb.db
|
|
169
|
+
.updateTable("inbox_items")
|
|
170
|
+
.set({ state: "expired" })
|
|
171
|
+
.where("org_id", "=", orgId)
|
|
172
|
+
.where("account_id", "=", accountId)
|
|
173
|
+
.where("source", "=", source.kind)
|
|
174
|
+
.where(sql `coalesce(plugin, '') = ${plugin}`)
|
|
175
|
+
.where("dedupe_key", "=", dedupe)
|
|
176
|
+
.where("state", "=", "open");
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* An extension resolves an open item it posted (`ctx.inbox.resolve`); an item of this plugin
|
|
180
|
+
* already resolved or expired is left as it is. Another plugin's item, a kernel item or an
|
|
181
|
+
* unknown id throws `InboxError("not_found")`. The caller runs it in the org's scope.
|
|
182
|
+
*/
|
|
183
|
+
async resolveForPlugin(plugin, orgId, itemId) {
|
|
184
|
+
if (typeof orgId !== "string" || typeof itemId !== "string" || !UUID_RE.test(itemId))
|
|
185
|
+
throw new InboxError("not_found", "inbox item not found");
|
|
186
|
+
const row = await this.kdb.db
|
|
187
|
+
.selectFrom("inbox_items")
|
|
188
|
+
.select("id")
|
|
189
|
+
.where("org_id", "=", orgId)
|
|
190
|
+
.where("id", "=", itemId)
|
|
191
|
+
.where("source", "=", "plugin")
|
|
192
|
+
.where("plugin", "=", plugin)
|
|
193
|
+
.executeTakeFirst();
|
|
194
|
+
if (row === undefined)
|
|
195
|
+
throw new InboxError("not_found", "inbox item not found");
|
|
196
|
+
await this.close(orgId, itemId);
|
|
197
|
+
}
|
|
198
|
+
/** Marks an open item resolved; whether it was open. */
|
|
199
|
+
async close(orgId, itemId) {
|
|
200
|
+
const r = await this.kdb.db
|
|
201
|
+
.updateTable("inbox_items")
|
|
202
|
+
.set({ state: "resolved", resolved_at: sql `now()` })
|
|
203
|
+
.where("org_id", "=", orgId)
|
|
204
|
+
.where("id", "=", itemId)
|
|
205
|
+
.where("state", "=", "open")
|
|
206
|
+
.executeTakeFirst();
|
|
207
|
+
return r.numUpdatedRows > 0n;
|
|
208
|
+
}
|
|
209
|
+
/** The open, unexpired item `itemId` of `accountId` in `orgId`, or `inbox.item_not_found`. */
|
|
210
|
+
async openOf(orgId, accountId, itemId) {
|
|
211
|
+
if (!UUID_RE.test(itemId))
|
|
212
|
+
throw notFound();
|
|
213
|
+
const row = await this.kdb.db
|
|
214
|
+
.selectFrom("inbox_items")
|
|
215
|
+
.select(["id", "org_id", "account_id", "plugin", "actions"])
|
|
216
|
+
.where("org_id", "=", orgId)
|
|
217
|
+
.where("account_id", "=", accountId)
|
|
218
|
+
.where("id", "=", itemId)
|
|
219
|
+
.where("state", "=", "open")
|
|
220
|
+
.where("expires_at", ">", sql `now()`)
|
|
221
|
+
.executeTakeFirst();
|
|
222
|
+
if (row === undefined)
|
|
223
|
+
throw notFound();
|
|
224
|
+
return {
|
|
225
|
+
id: row.id,
|
|
226
|
+
orgId: row.org_id,
|
|
227
|
+
accountId: row.account_id,
|
|
228
|
+
plugin: row.plugin,
|
|
229
|
+
actions: row.actions,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* The person dismisses their own open item (`POST inbox/items/:id/dismiss`): resolved without
|
|
234
|
+
* calling anything. Not theirs, not open or expired -> `inbox.item_not_found`.
|
|
235
|
+
*/
|
|
236
|
+
async dismiss(orgId, accountId, itemId) {
|
|
237
|
+
await this.openOf(orgId, accountId, itemId);
|
|
238
|
+
if (!(await this.close(orgId, itemId)))
|
|
239
|
+
throw notFound();
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* The open, unexpired items of `accountId` in `orgId`, newest first, at most
|
|
243
|
+
* {@link INBOX_ITEMS_LIST_MAX}.
|
|
244
|
+
*/
|
|
245
|
+
async listOpen(orgId, accountId) {
|
|
246
|
+
const db = this.kdb.db;
|
|
247
|
+
const rows = await db
|
|
248
|
+
.selectFrom("inbox_items")
|
|
249
|
+
.select([
|
|
250
|
+
"id",
|
|
251
|
+
"source",
|
|
252
|
+
"plugin",
|
|
253
|
+
"kind",
|
|
254
|
+
"title",
|
|
255
|
+
"body",
|
|
256
|
+
"link",
|
|
257
|
+
"actions",
|
|
258
|
+
"detail",
|
|
259
|
+
"expires_at",
|
|
260
|
+
"created_at",
|
|
261
|
+
])
|
|
262
|
+
.where("org_id", "=", orgId)
|
|
263
|
+
.where("account_id", "=", accountId)
|
|
264
|
+
.where("state", "=", "open")
|
|
265
|
+
.where("expires_at", ">", sql `now()`)
|
|
266
|
+
.orderBy("created_at", "desc")
|
|
267
|
+
.orderBy("id")
|
|
268
|
+
.limit(INBOX_ITEMS_LIST_MAX)
|
|
269
|
+
.execute();
|
|
270
|
+
const names = await pluginDisplayNames(db, rows.flatMap((r) => (r.plugin === null ? [] : [r.plugin])));
|
|
271
|
+
return rows.map((r) => ({
|
|
272
|
+
id: r.id,
|
|
273
|
+
source: r.source,
|
|
274
|
+
plugin: r.plugin,
|
|
275
|
+
pluginName: r.plugin === null ? null : (names.get(r.plugin) ?? null),
|
|
276
|
+
kind: r.kind,
|
|
277
|
+
title: r.title,
|
|
278
|
+
body: r.body,
|
|
279
|
+
link: r.link,
|
|
280
|
+
actions: r.actions.map((a) => ({
|
|
281
|
+
id: a.id,
|
|
282
|
+
label: { zh: a.label.zh, en: a.label.en },
|
|
283
|
+
...(a.style !== undefined && { style: a.style }),
|
|
284
|
+
})),
|
|
285
|
+
detail: r.source === "kernel" ? (r.detail ?? null) : null,
|
|
286
|
+
expiresAt: r.expires_at.toISOString(),
|
|
287
|
+
createdAt: r.created_at.toISOString(),
|
|
288
|
+
}));
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
__decorate([
|
|
292
|
+
Transactional()
|
|
293
|
+
], InboxItemsService.prototype, "post", null);
|
|
294
|
+
__decorate([
|
|
295
|
+
Transactional()
|
|
296
|
+
], InboxItemsService.prototype, "resolveForPlugin", null);
|
|
297
|
+
__decorate([
|
|
298
|
+
Transactional()
|
|
299
|
+
], InboxItemsService.prototype, "close", null);
|
|
300
|
+
__decorate([
|
|
301
|
+
Transactional()
|
|
302
|
+
], InboxItemsService.prototype, "dismiss", null);
|
|
303
|
+
InboxItemsService = __decorate([
|
|
304
|
+
Injectable(),
|
|
305
|
+
__param(0, Inject(KernelDb)),
|
|
306
|
+
__param(1, Inject(TransactionHost)),
|
|
307
|
+
__param(2, Inject(PushOwed))
|
|
308
|
+
], InboxItemsService);
|
|
309
|
+
export { InboxItemsService };
|
|
310
|
+
/**
|
|
311
|
+
* {@link InboxItemsService}: infra and the push owing only, so the trigger dispatcher (kernel
|
|
312
|
+
* items) and the extension host (`ctx.inbox`, through {@link INBOX_ITEMS_BACKEND} at the time of
|
|
313
|
+
* use) reach it without a module cycle.
|
|
314
|
+
*/
|
|
315
|
+
let InboxItemsModule = class InboxItemsModule {
|
|
316
|
+
};
|
|
317
|
+
InboxItemsModule = __decorate([
|
|
318
|
+
Module({
|
|
319
|
+
imports: [PushOwedModule],
|
|
320
|
+
providers: [InboxItemsService, { provide: INBOX_ITEMS_BACKEND, useExisting: InboxItemsService }],
|
|
321
|
+
exports: [InboxItemsService, INBOX_ITEMS_BACKEND],
|
|
322
|
+
})
|
|
323
|
+
], InboxItemsModule);
|
|
324
|
+
export { InboxItemsModule };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The inboxes, read live from PG: an org member's (pending tool approvals, skill changes, catalog
|
|
3
|
-
* requests, org notices) and the super admin's (`PlatformInboxService`)
|
|
3
|
+
* requests, org notices, personal items) and the super admin's (`PlatformInboxService`); the
|
|
4
|
+
* clicks on personal items' actions ({@link InboxItemActions}, through the UI actions).
|
|
4
5
|
*/
|
|
5
6
|
export declare class InboxModule {
|
|
6
7
|
}
|
|
@@ -9,19 +9,30 @@ import { AutomationsModule } from "../automations/automations.module.js";
|
|
|
9
9
|
import { CatalogModule } from "../catalog/catalog.module.js";
|
|
10
10
|
import { SkillsModule } from "../skills/skills.module.js";
|
|
11
11
|
import { ToolsModule } from "../tools/tools.module.js";
|
|
12
|
+
import { UiModule } from "../ui/ui.module.js";
|
|
12
13
|
import { InboxService } from "./inbox.service.js";
|
|
14
|
+
import { InboxItemActions } from "./inbox-item-actions.js";
|
|
15
|
+
import { InboxItemsModule } from "./inbox-items.service.js";
|
|
13
16
|
import { PlatformInboxService } from "./platform-inbox.service.js";
|
|
14
17
|
/**
|
|
15
18
|
* The inboxes, read live from PG: an org member's (pending tool approvals, skill changes, catalog
|
|
16
|
-
* requests, org notices) and the super admin's (`PlatformInboxService`)
|
|
19
|
+
* requests, org notices, personal items) and the super admin's (`PlatformInboxService`); the
|
|
20
|
+
* clicks on personal items' actions ({@link InboxItemActions}, through the UI actions).
|
|
17
21
|
*/
|
|
18
22
|
let InboxModule = class InboxModule {
|
|
19
23
|
};
|
|
20
24
|
InboxModule = __decorate([
|
|
21
25
|
Module({
|
|
22
|
-
imports: [
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
imports: [
|
|
27
|
+
ToolsModule,
|
|
28
|
+
SkillsModule,
|
|
29
|
+
CatalogModule,
|
|
30
|
+
AutomationsModule,
|
|
31
|
+
InboxItemsModule,
|
|
32
|
+
UiModule,
|
|
33
|
+
],
|
|
34
|
+
providers: [InboxService, PlatformInboxService, InboxItemActions],
|
|
35
|
+
exports: [InboxService, PlatformInboxService, InboxItemActions, InboxItemsModule],
|
|
25
36
|
})
|
|
26
37
|
], InboxModule);
|
|
27
38
|
export { InboxModule };
|
|
@@ -4,5 +4,24 @@ import type { RouteSchemas } from "../platform/route-schemas.js";
|
|
|
4
4
|
export declare const InboxNoticeParams: z.ZodObject<{
|
|
5
5
|
id: z.ZodString;
|
|
6
6
|
}, z.core.$strip>;
|
|
7
|
-
/**
|
|
7
|
+
/** `:id` of a personal inbox item. */
|
|
8
|
+
export declare const InboxItemParams: z.ZodObject<{
|
|
9
|
+
id: z.ZodString;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
/** `:id/actions/:actionId` of a personal inbox item's button. */
|
|
12
|
+
export declare const InboxItemActionParams: z.ZodObject<{
|
|
13
|
+
id: z.ZodString;
|
|
14
|
+
actionId: z.ZodString;
|
|
15
|
+
}, z.core.$strip>;
|
|
16
|
+
/** The body of an action click: the locale the action's toast is for. */
|
|
17
|
+
export declare const InboxItemActionBody: z.ZodObject<{
|
|
18
|
+
locale: z.ZodDefault<z.ZodEnum<{
|
|
19
|
+
en: "en";
|
|
20
|
+
zh: "zh";
|
|
21
|
+
}>>;
|
|
22
|
+
}, z.core.$strict>;
|
|
23
|
+
/**
|
|
24
|
+
* The inbox routes (the org's and the super admin's) the Platform inbox operations mirror; the
|
|
25
|
+
* click on a personal item's action is excluded (`human-only`, `platform/exclusions.ts`).
|
|
26
|
+
*/
|
|
8
27
|
export declare const INBOX_ROUTES: readonly RouteSchemas[];
|
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
/** `:id` of a notice dismissal (the org's and the platform's inbox). */
|
|
3
3
|
export const InboxNoticeParams = z.object({ id: z.string().min(1).max(100) });
|
|
4
|
-
/**
|
|
4
|
+
/** `:id` of a personal inbox item. */
|
|
5
|
+
export const InboxItemParams = z.object({ id: z.string().min(1).max(100) });
|
|
6
|
+
/** `:id/actions/:actionId` of a personal inbox item's button. */
|
|
7
|
+
export const InboxItemActionParams = z.object({
|
|
8
|
+
id: z.string().min(1).max(100),
|
|
9
|
+
actionId: z.string().min(1).max(100),
|
|
10
|
+
});
|
|
11
|
+
/** The body of an action click: the locale the action's toast is for. */
|
|
12
|
+
export const InboxItemActionBody = z
|
|
13
|
+
.object({ locale: z.enum(["zh", "en"]).default("en") })
|
|
14
|
+
.strict();
|
|
15
|
+
/**
|
|
16
|
+
* The inbox routes (the org's and the super admin's) the Platform inbox operations mirror; the
|
|
17
|
+
* click on a personal item's action is excluded (`human-only`, `platform/exclusions.ts`).
|
|
18
|
+
*/
|
|
5
19
|
export const INBOX_ROUTES = [
|
|
6
20
|
{ route: "GET /api/v1/orgs/:orgId/inbox" },
|
|
7
21
|
{ route: "POST /api/v1/orgs/:orgId/inbox/notices/:id/dismiss", params: InboxNoticeParams },
|
|
22
|
+
{ route: "POST /api/v1/orgs/:orgId/inbox/items/:id/dismiss", params: InboxItemParams },
|
|
8
23
|
{ route: "GET /api/v1/admin/inbox" },
|
|
9
24
|
{ route: "GET /api/v1/admin/inbox/count" },
|
|
10
25
|
{ route: "POST /api/v1/admin/inbox/notices/:id/dismiss", params: InboxNoticeParams },
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type AutomationAttention, AutomationsService } from "../automations/automations.service.js";
|
|
2
|
+
import { AcceptanceService, type AutomationOfferView } from "../automations/events/acceptance.js";
|
|
2
3
|
import { CatalogRequestsService, type CatalogRequestView } from "../catalog/requests.service.js";
|
|
3
4
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
5
|
import { SkillChangesService, type SkillChangeView } from "../skills/changes.service.js";
|
|
5
6
|
import { ApprovalsService, type ApprovalView } from "../tools/approvals.service.js";
|
|
7
|
+
import { InboxItemsService, type PersonalInboxItemView } from "./inbox-items.service.js";
|
|
6
8
|
import type { OrgNoticeDetails } from "./org-notices.js";
|
|
7
9
|
/** The most open org notices one inbox read returns. */
|
|
8
10
|
export declare const ORG_NOTICES_LIST_MAX = 100;
|
|
@@ -20,11 +22,12 @@ export type OrgNoticeView = {
|
|
|
20
22
|
detail: OrgNoticeDetails[K];
|
|
21
23
|
};
|
|
22
24
|
}[keyof OrgNoticeDetails];
|
|
23
|
-
/**
|
|
25
|
+
/** The most items one inbox read returns. */
|
|
24
26
|
export declare const INBOX_MAX_ITEMS = 100;
|
|
25
27
|
/**
|
|
26
|
-
*
|
|
27
|
-
* - `tool_approval
|
|
28
|
+
* One inbox entry (told apart by `type`, stable shapes):
|
|
29
|
+
* - `tool_approval`: a pending tool call approval the current user can decide (the same view as
|
|
30
|
+
* `GET /approvals?status=pending`);
|
|
28
31
|
* - `skill_change`: pending skill share requests and proposed versions of shared skills, when the
|
|
29
32
|
* current user is an org admin (`skillChange.kind`; spec §5.4, §5.5);
|
|
30
33
|
* - `catalog_request`: pending `enable` and `network_allowlist` requests from members and pending
|
|
@@ -39,7 +42,14 @@ export declare const INBOX_MAX_ITEMS = 100;
|
|
|
39
42
|
* or connections; derived live, gone once the task is resumed or runs clean;
|
|
40
43
|
* - the org notices (`item_shared`, `shared_item_updated`, `upload_confirmed`,
|
|
41
44
|
* `upload_rejected`; `type` = the notice's kind): for org admins, until one dismisses it
|
|
42
|
-
* (`POST inbox/notices/:id/dismiss`)
|
|
45
|
+
* (`POST inbox/notices/:id/dismiss`);
|
|
46
|
+
* - `item`: the current user's own open personal items (`inbox_items`, collaboration spec §4.9),
|
|
47
|
+
* posted by the kernel or an extension, until they click an action or dismiss it
|
|
48
|
+
* (`POST inbox/items/:id/actions/:actionId`, `POST inbox/items/:id/dismiss`) or it expires;
|
|
49
|
+
* a work offer's push-carrying item (kernel kind `automation_offer`) is shown as the offer;
|
|
50
|
+
* - `automation_offer`: work an extension assigned to the current user and their agent, waiting
|
|
51
|
+
* for them under a `confirm` acceptance rule (collaboration spec §4.4), until they accept or
|
|
52
|
+
* decline it (`POST automations/offers/:id/accept|decline`) or it expires (seven days).
|
|
43
53
|
*/
|
|
44
54
|
export type InboxItem = {
|
|
45
55
|
type: "tool_approval";
|
|
@@ -68,6 +78,16 @@ export type InboxItem = {
|
|
|
68
78
|
id: string;
|
|
69
79
|
createdAt: string;
|
|
70
80
|
attention: AutomationAttentionView;
|
|
81
|
+
} | {
|
|
82
|
+
type: "item";
|
|
83
|
+
id: string;
|
|
84
|
+
createdAt: string;
|
|
85
|
+
item: PersonalInboxItemView;
|
|
86
|
+
} | {
|
|
87
|
+
type: "automation_offer";
|
|
88
|
+
id: string;
|
|
89
|
+
createdAt: string;
|
|
90
|
+
offer: AutomationOfferView;
|
|
71
91
|
} | {
|
|
72
92
|
type: OrgNoticeView["kind"];
|
|
73
93
|
id: string;
|
|
@@ -76,19 +96,21 @@ export type InboxItem = {
|
|
|
76
96
|
};
|
|
77
97
|
/** An automation that needs its creator ({@link AutomationAttention}, dates as ISO strings). */
|
|
78
98
|
export type AutomationAttentionView = Omit<AutomationAttention, "at">;
|
|
79
|
-
/** `GET /inbox`
|
|
99
|
+
/** What `GET /inbox` answers: newest first, at most {@link INBOX_MAX_ITEMS}; `truncated`: more wait. */
|
|
80
100
|
export interface InboxView {
|
|
81
101
|
items: InboxItem[];
|
|
82
102
|
truncated: boolean;
|
|
83
103
|
}
|
|
84
|
-
/**
|
|
104
|
+
/** The inbox (plan decision C6): what waits, gathered by type from each source; read live from PG. */
|
|
85
105
|
export declare class InboxService {
|
|
86
106
|
private readonly approvals;
|
|
87
107
|
private readonly skillChanges;
|
|
88
108
|
private readonly kdb;
|
|
89
109
|
private readonly catalogRequests;
|
|
90
110
|
private readonly automations;
|
|
91
|
-
|
|
111
|
+
private readonly items;
|
|
112
|
+
private readonly acceptance;
|
|
113
|
+
constructor(approvals: Pick<ApprovalsService, "list">, skillChanges: Pick<SkillChangesService, "listPending">, kdb: KernelDb, catalogRequests: Pick<CatalogRequestsService, "listForOrgAdmin" | "listDecidedForRequester">, automations: Pick<AutomationsService, "attentionFor">, items: Pick<InboxItemsService, "listOpen">, acceptance: Pick<AcceptanceService, "openOffers">);
|
|
92
114
|
list(orgId: string, accountId: string): Promise<InboxView>;
|
|
93
115
|
/** The org's open notices when the account is an org admin (else none), newest first. */
|
|
94
116
|
private notices;
|
|
@@ -10,39 +10,49 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
10
10
|
import { Inject, Injectable } from "@nestjs/common";
|
|
11
11
|
import { sql } from "kysely";
|
|
12
12
|
import { AutomationsService, } from "../automations/automations.service.js";
|
|
13
|
+
import { AcceptanceService, OFFERS_LIST_MAX, } from "../automations/events/acceptance.js";
|
|
13
14
|
import { CATALOG_REQUESTS_LIST_MAX, CatalogRequestsService, } from "../catalog/requests.service.js";
|
|
14
15
|
import { ApiError } from "../errors.js";
|
|
15
16
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
16
17
|
import { roleAtLeast } from "../orgs/roles.js";
|
|
17
18
|
import { SKILL_CHANGES_LIST_MAX, SkillChangesService, } from "../skills/changes.service.js";
|
|
18
19
|
import { APPROVALS_LIST_MAX, ApprovalsService, } from "../tools/approvals.service.js";
|
|
20
|
+
import { INBOX_ITEMS_LIST_MAX, InboxItemsService, } from "./inbox-items.service.js";
|
|
19
21
|
/** The most open org notices one inbox read returns. */
|
|
20
22
|
export const ORG_NOTICES_LIST_MAX = 100;
|
|
21
|
-
/**
|
|
23
|
+
/** The most items one inbox read returns. */
|
|
22
24
|
export const INBOX_MAX_ITEMS = 100;
|
|
23
|
-
/**
|
|
25
|
+
/** The inbox (plan decision C6): what waits, gathered by type from each source; read live from PG. */
|
|
24
26
|
let InboxService = class InboxService {
|
|
25
27
|
approvals;
|
|
26
28
|
skillChanges;
|
|
27
29
|
kdb;
|
|
28
30
|
catalogRequests;
|
|
29
31
|
automations;
|
|
30
|
-
|
|
32
|
+
items;
|
|
33
|
+
acceptance;
|
|
34
|
+
constructor(approvals, skillChanges, kdb, catalogRequests, automations, items, acceptance) {
|
|
31
35
|
this.approvals = approvals;
|
|
32
36
|
this.skillChanges = skillChanges;
|
|
33
37
|
this.kdb = kdb;
|
|
34
38
|
this.catalogRequests = catalogRequests;
|
|
35
39
|
this.automations = automations;
|
|
40
|
+
this.items = items;
|
|
41
|
+
this.acceptance = acceptance;
|
|
36
42
|
}
|
|
37
43
|
async list(orgId, accountId) {
|
|
38
|
-
const [approvals, changes, requests, decided, notices, attention] = await Promise.all([
|
|
44
|
+
const [approvals, changes, requests, decided, notices, attention, allItems, offers] = await Promise.all([
|
|
39
45
|
this.approvals.list(orgId, accountId, "pending"),
|
|
40
46
|
this.skillChanges.listPending(orgId, accountId),
|
|
41
47
|
this.catalogRequests.listForOrgAdmin(orgId, accountId),
|
|
42
48
|
this.catalogRequests.listDecidedForRequester(orgId, accountId),
|
|
43
49
|
this.notices(orgId, accountId),
|
|
44
50
|
this.automations.attentionFor(orgId, accountId),
|
|
51
|
+
this.items.listOpen(orgId, accountId),
|
|
52
|
+
this.acceptance.openOffers(orgId, accountId),
|
|
45
53
|
]);
|
|
54
|
+
// an offer's own item carries its push; the inbox shows the offer (with accept and decline)
|
|
55
|
+
const items = allItems.filter((i) => !(i.source === "kernel" && i.kind === "automation_offer"));
|
|
46
56
|
const all = [
|
|
47
57
|
...approvals.map((a) => ({
|
|
48
58
|
type: "tool_approval",
|
|
@@ -75,8 +85,15 @@ let InboxService = class InboxService {
|
|
|
75
85
|
attention: view,
|
|
76
86
|
})),
|
|
77
87
|
...notices.map((n) => ({ type: n.kind, id: n.id, createdAt: n.createdAt, notice: n })),
|
|
88
|
+
...items.map((i) => ({ type: "item", id: i.id, createdAt: i.createdAt, item: i })),
|
|
89
|
+
...offers.map((o) => ({
|
|
90
|
+
type: "automation_offer",
|
|
91
|
+
id: o.id,
|
|
92
|
+
createdAt: o.createdAt,
|
|
93
|
+
offer: o,
|
|
94
|
+
})),
|
|
78
95
|
];
|
|
79
|
-
//
|
|
96
|
+
// newest first; the id orders items of the same moment (a stable result)
|
|
80
97
|
all.sort((a, b) => a.createdAt !== b.createdAt
|
|
81
98
|
? a.createdAt < b.createdAt
|
|
82
99
|
? 1
|
|
@@ -86,12 +103,14 @@ let InboxService = class InboxService {
|
|
|
86
103
|
: a.id > b.id
|
|
87
104
|
? 1
|
|
88
105
|
: 0);
|
|
89
|
-
//
|
|
106
|
+
// each source has its own cap: one read in full counts as more waiting
|
|
90
107
|
const full = approvals.length >= APPROVALS_LIST_MAX ||
|
|
91
108
|
changes.length >= SKILL_CHANGES_LIST_MAX ||
|
|
92
109
|
requests.length >= CATALOG_REQUESTS_LIST_MAX ||
|
|
93
110
|
decided.length >= CATALOG_REQUESTS_LIST_MAX ||
|
|
94
|
-
notices.length >= ORG_NOTICES_LIST_MAX
|
|
111
|
+
notices.length >= ORG_NOTICES_LIST_MAX ||
|
|
112
|
+
allItems.length >= INBOX_ITEMS_LIST_MAX ||
|
|
113
|
+
offers.length >= OFFERS_LIST_MAX;
|
|
95
114
|
return {
|
|
96
115
|
items: all.slice(0, INBOX_MAX_ITEMS),
|
|
97
116
|
truncated: full || all.length > INBOX_MAX_ITEMS,
|
|
@@ -150,7 +169,9 @@ InboxService = __decorate([
|
|
|
150
169
|
__param(1, Inject(SkillChangesService)),
|
|
151
170
|
__param(2, Inject(KernelDb)),
|
|
152
171
|
__param(3, Inject(CatalogRequestsService)),
|
|
153
|
-
__param(4, Inject(AutomationsService))
|
|
172
|
+
__param(4, Inject(AutomationsService)),
|
|
173
|
+
__param(5, Inject(InboxItemsService)),
|
|
174
|
+
__param(6, Inject(AcceptanceService))
|
|
154
175
|
], InboxService);
|
|
155
176
|
export { InboxService };
|
|
156
177
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
@@ -85,6 +85,8 @@ const HOT_APPLIERS = {
|
|
|
85
85
|
"automations.maxEventRunsPerSubjectPerHourCap": () => { },
|
|
86
86
|
// The trigger event sweep reads the retention at every tick.
|
|
87
87
|
"automations.eventRetentionDays": () => { },
|
|
88
|
+
// ... and the inbox item retention.
|
|
89
|
+
"inbox.retentionDays": () => { },
|
|
88
90
|
// Run start reads the skills catalog budget when it records a snapshot.
|
|
89
91
|
"skills.catalogBudgetChars": () => { },
|
|
90
92
|
// The attribution defaults are read live at every run start, admin settings view and org settings.
|
|
@@ -560,6 +560,23 @@ export declare const OUTBOX_JOBS: {
|
|
|
560
560
|
readonly removeOnFail: true;
|
|
561
561
|
};
|
|
562
562
|
};
|
|
563
|
+
readonly "hook-run-lifecycle": {
|
|
564
|
+
readonly queue: "cap-kernel-lifecycle";
|
|
565
|
+
readonly idempotent: "the `run.lifecycle` handler sets a display state keyed by the run and phase (the SDK contract: handlers are idempotent); the job id is deterministic per run, phase and wait";
|
|
566
|
+
readonly processedIn: "packages/kernel/src/extensions/lifecycle-hooks.ts";
|
|
567
|
+
readonly opts: {
|
|
568
|
+
readonly attempts: 10;
|
|
569
|
+
readonly backoff: {
|
|
570
|
+
readonly type: "exponential";
|
|
571
|
+
readonly delay: 5000;
|
|
572
|
+
};
|
|
573
|
+
readonly removeOnComplete: true;
|
|
574
|
+
readonly removeOnFail: {
|
|
575
|
+
readonly age: number;
|
|
576
|
+
readonly count: 1000;
|
|
577
|
+
};
|
|
578
|
+
};
|
|
579
|
+
};
|
|
563
580
|
readonly lifecycle: {
|
|
564
581
|
readonly queue: "cap-kernel-lifecycle";
|
|
565
582
|
readonly idempotent: "lifecycle handlers must be idempotent (the SDK contract): they delete what the plugin holds for the removed member, agent or org, and deleting it again is a no-op";
|