@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,67 @@
|
|
|
1
|
+
import type { RunLifecycleDeclineReason, RunLifecyclePayload } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import type { ConversationSubject, KernelSchemaDb } from "../db/kernel-schema.js";
|
|
3
|
+
import type { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
4
|
+
/** Queue of lifecycle hook deliveries (no `:` in the name); also carries `run.lifecycle`. */
|
|
5
|
+
export declare const LIFECYCLE_QUEUE = "cap-kernel-lifecycle";
|
|
6
|
+
/** The job name of a `run.lifecycle` delivery (registered in `infra/outbox/registry.ts`). */
|
|
7
|
+
export declare const RUN_LIFECYCLE_JOB = "hook-run-lifecycle";
|
|
8
|
+
/** Job data of a `run.lifecycle` delivery (the lifecycle queue's job shape). */
|
|
9
|
+
export interface RunLifecycleJob {
|
|
10
|
+
mount: "run.lifecycle";
|
|
11
|
+
plugin: string;
|
|
12
|
+
payload: RunLifecyclePayload;
|
|
13
|
+
}
|
|
14
|
+
type Phase = RunLifecyclePayload["phase"];
|
|
15
|
+
/**
|
|
16
|
+
* Whether `plugin` is to be told about runs of `orgId`: its approved, deployed permission snapshot
|
|
17
|
+
* declares `run.lifecycle` in `permissions.hooks`, and the org has it enabled.
|
|
18
|
+
*/
|
|
19
|
+
export declare function subscribesToRunLifecycle(db: KernelSchemaDb, orgId: string, plugin: string): Promise<boolean>;
|
|
20
|
+
/** The deterministic job id of a delivery (no `:`; kernel ids carry none). */
|
|
21
|
+
export declare function runLifecycleJobId(parts: {
|
|
22
|
+
id: string;
|
|
23
|
+
phase: Phase;
|
|
24
|
+
waitId?: string | undefined;
|
|
25
|
+
}): string;
|
|
26
|
+
/**
|
|
27
|
+
* What the caller already holds of a run and its conversation: callers read these columns in the
|
|
28
|
+
* statement that changes the run, so a conversation without a subject costs no extra query.
|
|
29
|
+
*/
|
|
30
|
+
export interface RunLifecycleFacts {
|
|
31
|
+
orgId: string;
|
|
32
|
+
conversationId: string;
|
|
33
|
+
runId: string;
|
|
34
|
+
agentId: string;
|
|
35
|
+
agentVersion: number;
|
|
36
|
+
/** The run's trigger account, else the conversation's owner. */
|
|
37
|
+
accountId: string;
|
|
38
|
+
subject: ConversationSubject | null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Owes `run.lifecycle` for a run of a conversation with a subject, in the caller's transaction
|
|
42
|
+
* (the one that changed the run: the outbox row commits or rolls back with it). Nothing when the
|
|
43
|
+
* conversation has no subject, or the subject's plugin does not subscribe. Observe-only: the
|
|
44
|
+
* delivery never affects the run. `waitId` keeps each wait of one run apart.
|
|
45
|
+
*/
|
|
46
|
+
export declare function oweRunLifecycleFor(db: KernelSchemaDb, outbox: Pick<OutboxService, "add">, f: RunLifecycleFacts, a: {
|
|
47
|
+
phase: Exclude<Phase, "offer_declined">;
|
|
48
|
+
waitingOn?: "approval" | "input";
|
|
49
|
+
waitId?: string;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Owes `run.lifecycle` `offer_declined` to `plugin` (the plugin that emitted the directed event
|
|
53
|
+
* `eventId`), in the caller's transaction. Nothing when the plugin does not subscribe in the org.
|
|
54
|
+
*/
|
|
55
|
+
export declare function oweOfferDeclinedIn(db: KernelSchemaDb, outbox: Pick<OutboxService, "add">, a: {
|
|
56
|
+
orgId: string;
|
|
57
|
+
plugin: string;
|
|
58
|
+
eventId: string;
|
|
59
|
+
accountId: string;
|
|
60
|
+
agentId: string;
|
|
61
|
+
subject: {
|
|
62
|
+
kind: string;
|
|
63
|
+
id: string;
|
|
64
|
+
};
|
|
65
|
+
reason: RunLifecycleDeclineReason;
|
|
66
|
+
}): Promise<void>;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
import { deployedSnapshot } from "./deployed-snapshots.js";
|
|
3
|
+
import { isExtensionEnabled } from "./enabled.js";
|
|
4
|
+
/** Queue of lifecycle hook deliveries (no `:` in the name); also carries `run.lifecycle`. */
|
|
5
|
+
export const LIFECYCLE_QUEUE = "cap-kernel-lifecycle";
|
|
6
|
+
/** The job name of a `run.lifecycle` delivery (registered in `infra/outbox/registry.ts`). */
|
|
7
|
+
export const RUN_LIFECYCLE_JOB = "hook-run-lifecycle";
|
|
8
|
+
/**
|
|
9
|
+
* Whether `plugin` is to be told about runs of `orgId`: its approved, deployed permission snapshot
|
|
10
|
+
* declares `run.lifecycle` in `permissions.hooks`, and the org has it enabled.
|
|
11
|
+
*/
|
|
12
|
+
export async function subscribesToRunLifecycle(db, orgId, plugin) {
|
|
13
|
+
const declared = await db
|
|
14
|
+
.selectFrom("extension_permission_snapshots as s")
|
|
15
|
+
.select("s.plugin")
|
|
16
|
+
.where("s.plugin", "=", plugin)
|
|
17
|
+
.where(sql `s.permissions -> 'permissions' -> 'hooks' ? 'run.lifecycle'`)
|
|
18
|
+
.where(deployedSnapshot)
|
|
19
|
+
.limit(1)
|
|
20
|
+
.executeTakeFirst();
|
|
21
|
+
return declared !== undefined && (await isExtensionEnabled(db, orgId, plugin));
|
|
22
|
+
}
|
|
23
|
+
/** The deterministic job id of a delivery (no `:`; kernel ids carry none). */
|
|
24
|
+
export function runLifecycleJobId(parts) {
|
|
25
|
+
return [
|
|
26
|
+
"hook-run-lifecycle",
|
|
27
|
+
parts.id,
|
|
28
|
+
parts.phase === "offer_declined" ? "declined" : parts.phase,
|
|
29
|
+
]
|
|
30
|
+
.concat(parts.waitId === undefined ? [] : [parts.waitId])
|
|
31
|
+
.join("-")
|
|
32
|
+
.replaceAll(":", "-");
|
|
33
|
+
}
|
|
34
|
+
async function owe(outbox, plugin, payload, jobId) {
|
|
35
|
+
const data = { mount: "run.lifecycle", plugin, payload };
|
|
36
|
+
await outbox.add({
|
|
37
|
+
queue: LIFECYCLE_QUEUE,
|
|
38
|
+
name: RUN_LIFECYCLE_JOB,
|
|
39
|
+
jobId,
|
|
40
|
+
data,
|
|
41
|
+
orgId: payload.orgId,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Owes `run.lifecycle` for a run of a conversation with a subject, in the caller's transaction
|
|
46
|
+
* (the one that changed the run: the outbox row commits or rolls back with it). Nothing when the
|
|
47
|
+
* conversation has no subject, or the subject's plugin does not subscribe. Observe-only: the
|
|
48
|
+
* delivery never affects the run. `waitId` keeps each wait of one run apart.
|
|
49
|
+
*/
|
|
50
|
+
export async function oweRunLifecycleFor(db, outbox, f, a) {
|
|
51
|
+
const subject = f.subject;
|
|
52
|
+
if (!subject)
|
|
53
|
+
return;
|
|
54
|
+
if (!(await subscribesToRunLifecycle(db, f.orgId, subject.source)))
|
|
55
|
+
return;
|
|
56
|
+
const payload = {
|
|
57
|
+
orgId: f.orgId,
|
|
58
|
+
accountId: f.accountId,
|
|
59
|
+
agentId: f.agentId,
|
|
60
|
+
agentVersion: f.agentVersion,
|
|
61
|
+
conversationId: f.conversationId,
|
|
62
|
+
runId: f.runId,
|
|
63
|
+
phase: a.phase,
|
|
64
|
+
subject: { kind: subject.kind, id: subject.id },
|
|
65
|
+
...(a.waitingOn !== undefined && { waitingOn: a.waitingOn }),
|
|
66
|
+
};
|
|
67
|
+
await owe(outbox, subject.source, payload, runLifecycleJobId({ id: f.runId, phase: a.phase, waitId: a.waitId }));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Owes `run.lifecycle` `offer_declined` to `plugin` (the plugin that emitted the directed event
|
|
71
|
+
* `eventId`), in the caller's transaction. Nothing when the plugin does not subscribe in the org.
|
|
72
|
+
*/
|
|
73
|
+
export async function oweOfferDeclinedIn(db, outbox, a) {
|
|
74
|
+
if (!(await subscribesToRunLifecycle(db, a.orgId, a.plugin)))
|
|
75
|
+
return;
|
|
76
|
+
await owe(outbox, a.plugin, {
|
|
77
|
+
orgId: a.orgId,
|
|
78
|
+
accountId: a.accountId,
|
|
79
|
+
agentId: a.agentId,
|
|
80
|
+
phase: "offer_declined",
|
|
81
|
+
subject: { kind: a.subject.kind, id: a.subject.id },
|
|
82
|
+
reason: a.reason,
|
|
83
|
+
}, runLifecycleJobId({ id: a.eventId, phase: "offer_declined" }));
|
|
84
|
+
}
|
package/dist/http/api.module.js
CHANGED
|
@@ -23,6 +23,7 @@ import { ModelsController } from "../agents/models.controller.js";
|
|
|
23
23
|
import { PendingConnectorsController } from "../agents/pending-connectors.controller.js";
|
|
24
24
|
import { ReadinessController } from "../agents/readiness.controller.js";
|
|
25
25
|
import { AgentSaveAsTemplateController, AgentTemplatesController, } from "../agents/templates.controller.js";
|
|
26
|
+
import { AcceptanceController } from "../automations/acceptance.controller.js";
|
|
26
27
|
import { AutomationsController } from "../automations/automations.controller.js";
|
|
27
28
|
import { AutomationsModule } from "../automations/automations.module.js";
|
|
28
29
|
import { BackgroundController } from "../background/background.controller.js";
|
|
@@ -74,6 +75,7 @@ import { InitialPasswordsController } from "../identity/initial-passwords.contro
|
|
|
74
75
|
import { MeController } from "../identity/me.controller.js";
|
|
75
76
|
import { BindController, MeIdentitiesController, SsoController, } from "../identity/sso.controller.js";
|
|
76
77
|
import { InboxModule } from "../inbox/inbox.module.js";
|
|
78
|
+
import { InboxItemsController } from "../inbox/inbox-items.controller.js";
|
|
77
79
|
import { PlatformInboxController } from "../inbox/platform-inbox.controller.js";
|
|
78
80
|
import { ModelEnablementController } from "../models/enablement.controller.js";
|
|
79
81
|
import { ModelsModule } from "../models/models.module.js";
|
|
@@ -164,6 +166,7 @@ const CONTROLLERS = [
|
|
|
164
166
|
BackgroundController,
|
|
165
167
|
DevicesController,
|
|
166
168
|
AutomationsController,
|
|
169
|
+
AcceptanceController,
|
|
167
170
|
AttachmentsController,
|
|
168
171
|
LibraryFilesController,
|
|
169
172
|
SkillsController,
|
|
@@ -171,6 +174,7 @@ const CONTROLLERS = [
|
|
|
171
174
|
AgentSkillsController,
|
|
172
175
|
InvocableSkillsController,
|
|
173
176
|
InboxController,
|
|
177
|
+
InboxItemsController,
|
|
174
178
|
PlatformInboxController,
|
|
175
179
|
ConversationStreamController,
|
|
176
180
|
ConnectorContentController,
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
2
|
+
import type { OrgRole } from "../orgs/roles.js";
|
|
3
|
+
import { UiViews } from "../ui/views.js";
|
|
4
|
+
import { InboxItemsService } from "./inbox-items.service.js";
|
|
5
|
+
/**
|
|
6
|
+
* A person clicks an action of their own personal inbox item (collaboration spec §4.9,
|
|
7
|
+
* `POST inbox/items/:id/actions/:actionId`): the kernel calls the plugin's UI action the button
|
|
8
|
+
* names through {@link UiViews.act} (its access gate applies: the contribution's slot and role, the
|
|
9
|
+
* plugin enabled in the org, a valid effective config) with the clicking person as the account and
|
|
10
|
+
* role, then resolves the item. The click is serialized per item under a lock (a second click while
|
|
11
|
+
* the first runs is refused `lock.acquire_failed`), and the item is read again under it, so one
|
|
12
|
+
* item calls its plugin at most once; an action that fails leaves the item open.
|
|
13
|
+
*/
|
|
14
|
+
export declare class InboxItemActions {
|
|
15
|
+
private readonly items;
|
|
16
|
+
private readonly views;
|
|
17
|
+
private readonly locks;
|
|
18
|
+
constructor(items: Pick<InboxItemsService, "openOf" | "close">, views: Pick<UiViews, "act">, locks: Pick<LockService, "run">);
|
|
19
|
+
/**
|
|
20
|
+
* Runs action `actionId` of item `itemId` for `accountId` (role `role` in `orgId`). Not the
|
|
21
|
+
* caller's open unexpired item -> `inbox.item_not_found`; no such action ->
|
|
22
|
+
* `inbox.action_not_found`. Returns the action's toast, if any.
|
|
23
|
+
*/
|
|
24
|
+
click(input: {
|
|
25
|
+
orgId: string;
|
|
26
|
+
accountId: string;
|
|
27
|
+
role: OrgRole;
|
|
28
|
+
itemId: string;
|
|
29
|
+
actionId: string;
|
|
30
|
+
locale: "zh" | "en";
|
|
31
|
+
}): Promise<{
|
|
32
|
+
toast?: {
|
|
33
|
+
zh: string;
|
|
34
|
+
en: string;
|
|
35
|
+
};
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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 { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { ApiError } from "../errors.js";
|
|
12
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
13
|
+
import { UiViews } from "../ui/views.js";
|
|
14
|
+
import { InboxItemsService } from "./inbox-items.service.js";
|
|
15
|
+
/**
|
|
16
|
+
* A person clicks an action of their own personal inbox item (collaboration spec §4.9,
|
|
17
|
+
* `POST inbox/items/:id/actions/:actionId`): the kernel calls the plugin's UI action the button
|
|
18
|
+
* names through {@link UiViews.act} (its access gate applies: the contribution's slot and role, the
|
|
19
|
+
* plugin enabled in the org, a valid effective config) with the clicking person as the account and
|
|
20
|
+
* role, then resolves the item. The click is serialized per item under a lock (a second click while
|
|
21
|
+
* the first runs is refused `lock.acquire_failed`), and the item is read again under it, so one
|
|
22
|
+
* item calls its plugin at most once; an action that fails leaves the item open.
|
|
23
|
+
*/
|
|
24
|
+
let InboxItemActions = class InboxItemActions {
|
|
25
|
+
items;
|
|
26
|
+
views;
|
|
27
|
+
locks;
|
|
28
|
+
constructor(items, views, locks) {
|
|
29
|
+
this.items = items;
|
|
30
|
+
this.views = views;
|
|
31
|
+
this.locks = locks;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Runs action `actionId` of item `itemId` for `accountId` (role `role` in `orgId`). Not the
|
|
35
|
+
* caller's open unexpired item -> `inbox.item_not_found`; no such action ->
|
|
36
|
+
* `inbox.action_not_found`. Returns the action's toast, if any.
|
|
37
|
+
*/
|
|
38
|
+
async click(input) {
|
|
39
|
+
const { orgId, accountId, itemId } = input;
|
|
40
|
+
// checked before the lock too: another person's id never reaches the lock
|
|
41
|
+
await this.items.openOf(orgId, accountId, itemId);
|
|
42
|
+
return this.locks.run(`inbox-item:${itemId}`, { ttlMs: 60_000 }, async () => {
|
|
43
|
+
const item = await this.items.openOf(orgId, accountId, itemId);
|
|
44
|
+
const action = item.actions.find((a) => a.id === input.actionId);
|
|
45
|
+
if (action === undefined || item.plugin === null)
|
|
46
|
+
throw new ApiError("inbox.action_not_found", "Inbox item action not found");
|
|
47
|
+
const r = await this.views.act({
|
|
48
|
+
orgId,
|
|
49
|
+
accountId,
|
|
50
|
+
role: input.role,
|
|
51
|
+
plugin: item.plugin,
|
|
52
|
+
id: action.uiAction.contribution,
|
|
53
|
+
locale: input.locale,
|
|
54
|
+
}, {
|
|
55
|
+
actionId: action.uiAction.actionId,
|
|
56
|
+
...(action.uiAction.payload !== undefined && { payload: action.uiAction.payload }),
|
|
57
|
+
});
|
|
58
|
+
await this.items.close(orgId, itemId);
|
|
59
|
+
return "toast" in r && r.toast !== undefined ? { toast: r.toast } : {};
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
InboxItemActions = __decorate([
|
|
64
|
+
Injectable(),
|
|
65
|
+
__param(0, Inject(InboxItemsService)),
|
|
66
|
+
__param(1, Inject(UiViews)),
|
|
67
|
+
__param(2, Inject(LockService))
|
|
68
|
+
], InboxItemActions);
|
|
69
|
+
export { InboxItemActions };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import { type Principal } from "../identity/principal.js";
|
|
3
|
+
import { type OrgContext } from "../orgs/org.guard.js";
|
|
4
|
+
import { InboxItemActionBody, InboxItemActionParams, InboxItemParams } from "./inbox.schemas.js";
|
|
5
|
+
import { InboxItemActions } from "./inbox-item-actions.js";
|
|
6
|
+
import { InboxItemsService } from "./inbox-items.service.js";
|
|
7
|
+
/**
|
|
8
|
+
* A person's own personal inbox items (collaboration spec §4.9), user principals only; listed by
|
|
9
|
+
* `GET /api/v1/orgs/:orgId/inbox` (`type: "item"`). Only the item's person reaches it: anyone
|
|
10
|
+
* else, an org admin included, gets 404 `inbox.item_not_found`.
|
|
11
|
+
* - `POST /api/v1/orgs/:orgId/inbox/items/:id/actions/:actionId` (`{ locale? }`) → `{ toast? }`:
|
|
12
|
+
* calls the plugin UI action the button names as the clicking person (the UI routes' access gate
|
|
13
|
+
* applies), then resolves the item (`ui.use`);
|
|
14
|
+
* - `POST /api/v1/orgs/:orgId/inbox/items/:id/dismiss` → 204: resolves it without calling anything.
|
|
15
|
+
*/
|
|
16
|
+
export declare class InboxItemsController {
|
|
17
|
+
private readonly items;
|
|
18
|
+
private readonly actions;
|
|
19
|
+
constructor(items: InboxItemsService, actions: InboxItemActions);
|
|
20
|
+
act(principal: Principal, org: OrgContext, params: z.infer<typeof InboxItemActionParams>, body: z.infer<typeof InboxItemActionBody>): Promise<{
|
|
21
|
+
toast?: {
|
|
22
|
+
zh: string;
|
|
23
|
+
en: string;
|
|
24
|
+
};
|
|
25
|
+
}>;
|
|
26
|
+
dismiss(principal: Principal, org: OrgContext, params: z.infer<typeof InboxItemParams>): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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 { Controller, HttpCode, Inject, Post } from "@nestjs/common";
|
|
11
|
+
import { CurrentPrincipal, requireUser } from "../identity/principal.js";
|
|
12
|
+
import { createZodDto } from "../infra/validation/create-zod-dto.js";
|
|
13
|
+
import { ZBody, ZParam } from "../infra/validation/zod-params.js";
|
|
14
|
+
import { CurrentOrg, OrgAction } from "../orgs/org.guard.js";
|
|
15
|
+
import { InboxItemActionBody, InboxItemActionParams, InboxItemParams } from "./inbox.schemas.js";
|
|
16
|
+
import { InboxItemActions } from "./inbox-item-actions.js";
|
|
17
|
+
import { InboxItemsService } from "./inbox-items.service.js";
|
|
18
|
+
class InboxItemParamsDto extends createZodDto(InboxItemParams, "InboxItemParams") {
|
|
19
|
+
}
|
|
20
|
+
class InboxItemActionParamsDto extends createZodDto(InboxItemActionParams, "InboxItemActionParams") {
|
|
21
|
+
}
|
|
22
|
+
class InboxItemActionBodyDto extends createZodDto(InboxItemActionBody, "InboxItemActionBody") {
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A person's own personal inbox items (collaboration spec §4.9), user principals only; listed by
|
|
26
|
+
* `GET /api/v1/orgs/:orgId/inbox` (`type: "item"`). Only the item's person reaches it: anyone
|
|
27
|
+
* else, an org admin included, gets 404 `inbox.item_not_found`.
|
|
28
|
+
* - `POST /api/v1/orgs/:orgId/inbox/items/:id/actions/:actionId` (`{ locale? }`) → `{ toast? }`:
|
|
29
|
+
* calls the plugin UI action the button names as the clicking person (the UI routes' access gate
|
|
30
|
+
* applies), then resolves the item (`ui.use`);
|
|
31
|
+
* - `POST /api/v1/orgs/:orgId/inbox/items/:id/dismiss` → 204: resolves it without calling anything.
|
|
32
|
+
*/
|
|
33
|
+
let InboxItemsController = class InboxItemsController {
|
|
34
|
+
items;
|
|
35
|
+
actions;
|
|
36
|
+
constructor(items, actions) {
|
|
37
|
+
this.items = items;
|
|
38
|
+
this.actions = actions;
|
|
39
|
+
}
|
|
40
|
+
async act(principal, org, params, body) {
|
|
41
|
+
const p = requireUser(principal);
|
|
42
|
+
return this.actions.click({
|
|
43
|
+
orgId: org.orgId,
|
|
44
|
+
accountId: p.account.id,
|
|
45
|
+
role: org.role,
|
|
46
|
+
itemId: params.id,
|
|
47
|
+
actionId: params.actionId,
|
|
48
|
+
locale: body.locale,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async dismiss(principal, org, params) {
|
|
52
|
+
const p = requireUser(principal);
|
|
53
|
+
await this.items.dismiss(org.orgId, p.account.id, params.id);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
__decorate([
|
|
57
|
+
Post(":id/actions/:actionId"),
|
|
58
|
+
HttpCode(200),
|
|
59
|
+
OrgAction("ui.use"),
|
|
60
|
+
__param(0, CurrentPrincipal()),
|
|
61
|
+
__param(1, CurrentOrg()),
|
|
62
|
+
__param(2, ZParam(InboxItemActionParamsDto)),
|
|
63
|
+
__param(3, ZBody(InboxItemActionBodyDto))
|
|
64
|
+
], InboxItemsController.prototype, "act", null);
|
|
65
|
+
__decorate([
|
|
66
|
+
Post(":id/dismiss"),
|
|
67
|
+
HttpCode(204)
|
|
68
|
+
// the person's own item: any member, a guest included, reads and dismisses their own
|
|
69
|
+
,
|
|
70
|
+
OrgAction("org.read"),
|
|
71
|
+
__param(0, CurrentPrincipal()),
|
|
72
|
+
__param(1, CurrentOrg()),
|
|
73
|
+
__param(2, ZParam(InboxItemParamsDto))
|
|
74
|
+
], InboxItemsController.prototype, "dismiss", null);
|
|
75
|
+
InboxItemsController = __decorate([
|
|
76
|
+
Controller("api/v1/orgs/:orgId/inbox/items"),
|
|
77
|
+
__param(0, Inject(InboxItemsService)),
|
|
78
|
+
__param(1, Inject(InboxItemActions))
|
|
79
|
+
], InboxItemsController);
|
|
80
|
+
export { InboxItemsController };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { type InboxPost, type LocalizedName } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import type { StoredInboxAction } from "../db/kernel-schema.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
5
|
+
import { PushOwed } from "../push/push-owed.js";
|
|
6
|
+
/** Who posts an item: the kernel, or an extension (by plugin name). */
|
|
7
|
+
export type InboxSource = {
|
|
8
|
+
kind: "kernel";
|
|
9
|
+
} | {
|
|
10
|
+
kind: "plugin";
|
|
11
|
+
plugin: string;
|
|
12
|
+
};
|
|
13
|
+
/** The most open personal items one inbox read returns. */
|
|
14
|
+
export declare const INBOX_ITEMS_LIST_MAX = 100;
|
|
15
|
+
/**
|
|
16
|
+
* A personal inbox item as its person sees it (collaboration spec §4.9): never the UI actions its
|
|
17
|
+
* buttons call (those stay in the kernel), only each button's id, label and style.
|
|
18
|
+
*/
|
|
19
|
+
export interface PersonalInboxItemView {
|
|
20
|
+
id: string;
|
|
21
|
+
source: "kernel" | "plugin";
|
|
22
|
+
/** The posting extension; null for a kernel item. */
|
|
23
|
+
plugin: string | null;
|
|
24
|
+
/** The plugin's display name (its name in both languages when it has none); null for the kernel. */
|
|
25
|
+
pluginName: LocalizedName | null;
|
|
26
|
+
kind: string;
|
|
27
|
+
title: string;
|
|
28
|
+
body: string;
|
|
29
|
+
/** An in-app path; a plugin item's is one of the plugin's pages (`/p/<plugin>/…`). */
|
|
30
|
+
link: string | null;
|
|
31
|
+
actions: {
|
|
32
|
+
id: string;
|
|
33
|
+
label: LocalizedName;
|
|
34
|
+
style?: "primary" | "danger";
|
|
35
|
+
}[];
|
|
36
|
+
/** A kernel item's structured facts (its kind's copy reads them); null for a plugin item. */
|
|
37
|
+
detail: Record<string, unknown> | null;
|
|
38
|
+
expiresAt: string;
|
|
39
|
+
createdAt: string;
|
|
40
|
+
}
|
|
41
|
+
/** An open item of the caller, with its stored actions (for the click). */
|
|
42
|
+
export interface OpenInboxItem {
|
|
43
|
+
id: string;
|
|
44
|
+
orgId: string;
|
|
45
|
+
accountId: string;
|
|
46
|
+
plugin: string | null;
|
|
47
|
+
actions: StoredInboxAction[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Personal inbox items (`inbox_items`, collaboration spec §4.9): an item for one person in one
|
|
51
|
+
* org, posted by the kernel or by an extension (`ctx.inbox.post`, which calls {@link post} with the
|
|
52
|
+
* extension's own name: an extension cannot post as another plugin). Only the item's person lists
|
|
53
|
+
* it, clicks its actions ({@link InboxItemActions}) or dismisses it; everyone else, the org's
|
|
54
|
+
* admins included, gets `inbox.item_not_found`. An item past `expires_at` is gone. Every read and
|
|
55
|
+
* write filters by the item's org.
|
|
56
|
+
*/
|
|
57
|
+
export declare class InboxItemsService {
|
|
58
|
+
private readonly kdb;
|
|
59
|
+
readonly txHost: TransactionHost;
|
|
60
|
+
private readonly push;
|
|
61
|
+
constructor(kdb: KernelDb, txHost: TransactionHost, push: Pick<PushOwed, "oweInboxItem">);
|
|
62
|
+
/**
|
|
63
|
+
* Posts `p` for `source` and owes its push fanout in the same transaction. The SDK's
|
|
64
|
+
* `validateInboxPost` checks the fields (a plugin item links only to `/p/<plugin>/…`; a kernel
|
|
65
|
+
* item to any in-app path and carries no actions); the account must be an active member of the
|
|
66
|
+
* org, and a plugin must be enabled there. With `dedupeKey`, an open unexpired item of the same
|
|
67
|
+
* (org, account, source, plugin, key) is returned instead (no new push); an expired one is
|
|
68
|
+
* closed and replaced. The caller runs it in the item's org scope.
|
|
69
|
+
*
|
|
70
|
+
* `opts.detail` (kernel items only, never from an extension): structured facts the web renders
|
|
71
|
+
* in the kind's own copy.
|
|
72
|
+
*
|
|
73
|
+
* Throws `InboxError`: `invalid_item` (a missing or malformed field, not an active member),
|
|
74
|
+
* `org_disabled` (the plugin is not enabled in the org).
|
|
75
|
+
*/
|
|
76
|
+
post(source: InboxSource, p: InboxPost, opts?: {
|
|
77
|
+
detail?: Record<string, unknown>;
|
|
78
|
+
}): Promise<{
|
|
79
|
+
itemId: string;
|
|
80
|
+
}>;
|
|
81
|
+
/** The open items of (org, account, source, plugin) with `dedupe`, closed as `expired`. */
|
|
82
|
+
private sameKey;
|
|
83
|
+
/**
|
|
84
|
+
* An extension resolves an open item it posted (`ctx.inbox.resolve`); an item of this plugin
|
|
85
|
+
* already resolved or expired is left as it is. Another plugin's item, a kernel item or an
|
|
86
|
+
* unknown id throws `InboxError("not_found")`. The caller runs it in the org's scope.
|
|
87
|
+
*/
|
|
88
|
+
resolveForPlugin(plugin: string, orgId: string, itemId: string): Promise<void>;
|
|
89
|
+
/** Marks an open item resolved; whether it was open. */
|
|
90
|
+
close(orgId: string, itemId: string): Promise<boolean>;
|
|
91
|
+
/** The open, unexpired item `itemId` of `accountId` in `orgId`, or `inbox.item_not_found`. */
|
|
92
|
+
openOf(orgId: string, accountId: string, itemId: string): Promise<OpenInboxItem>;
|
|
93
|
+
/**
|
|
94
|
+
* The person dismisses their own open item (`POST inbox/items/:id/dismiss`): resolved without
|
|
95
|
+
* calling anything. Not theirs, not open or expired -> `inbox.item_not_found`.
|
|
96
|
+
*/
|
|
97
|
+
dismiss(orgId: string, accountId: string, itemId: string): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* The open, unexpired items of `accountId` in `orgId`, newest first, at most
|
|
100
|
+
* {@link INBOX_ITEMS_LIST_MAX}.
|
|
101
|
+
*/
|
|
102
|
+
listOpen(orgId: string, accountId: string): Promise<PersonalInboxItemView[]>;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* {@link InboxItemsService}: infra and the push owing only, so the trigger dispatcher (kernel
|
|
106
|
+
* items) and the extension host (`ctx.inbox`, through {@link INBOX_ITEMS_BACKEND} at the time of
|
|
107
|
+
* use) reach it without a module cycle.
|
|
108
|
+
*/
|
|
109
|
+
export declare class InboxItemsModule {
|
|
110
|
+
}
|