@capybara-hq/kernel 0.1.0-rc.60 → 0.1.0-rc.62
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/admin/admin.module.js +3 -0
- package/dist/admin/extensions.controller.d.ts +32 -1
- package/dist/admin/extensions.controller.js +72 -3
- package/dist/admin/health.service.d.ts +2 -2
- package/dist/admin/health.service.js +6 -2
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/config/schema.d.ts +6 -0
- package/dist/config/schema.js +12 -0
- package/dist/conversations/attachments.controller.d.ts +11 -3
- package/dist/conversations/attachments.controller.js +18 -5
- package/dist/conversations/conversations.controller.d.ts +11 -2
- package/dist/conversations/conversations.controller.js +17 -4
- package/dist/conversations/conversations.schemas.d.ts +8 -0
- package/dist/conversations/conversations.schemas.js +6 -0
- package/dist/conversations/event-log.d.ts +12 -3
- package/dist/conversations/event-log.js +17 -5
- package/dist/ctx/secrets.d.ts +10 -5
- package/dist/ctx/secrets.js +16 -5
- package/dist/db/kernel-schema.d.ts +34 -2
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +7 -0
- package/dist/extensions/config-hash.d.ts +2 -0
- package/dist/extensions/config-hash.js +1 -1
- package/dist/extensions/guide-places.d.ts +5 -1
- package/dist/extensions/guide-places.js +9 -2
- package/dist/extensions/permission-view.d.ts +6 -4
- package/dist/extensions/permission-view.js +6 -3
- package/dist/extensions/plugin-guides.d.ts +20 -0
- package/dist/extensions/plugin-guides.js +49 -1
- package/dist/extensions/registry.js +1 -0
- package/dist/http/api.module.js +7 -0
- package/dist/http/meta.controller.d.ts +7 -1
- package/dist/http/meta.controller.js +14 -3
- package/dist/identity/accounts.js +5 -0
- package/dist/identity/sessions.d.ts +7 -0
- package/dist/identity/sessions.js +26 -4
- package/dist/inbox/platform-inbox.service.d.ts +21 -11
- package/dist/inbox/platform-inbox.service.js +27 -12
- package/dist/inbox/platform-notices.d.ts +15 -1
- package/dist/inbox/platform-notices.js +20 -0
- package/dist/infra/config/kernel-config.service.js +2 -0
- package/dist/infra/outbox/registry.d.ts +36 -0
- package/dist/infra/outbox/registry.js +30 -0
- package/dist/infra/queue/queue-names.d.ts +1 -1
- package/dist/infra/queue/queue-names.js +1 -0
- package/dist/orgs/offboarding.js +3 -0
- package/dist/overrides.d.ts +4 -0
- package/dist/overrides.js +2 -0
- package/dist/platform/exclusions.js +15 -0
- package/dist/platform/ops/extensions-admin.ops.d.ts +5 -1
- package/dist/platform/ops/extensions-admin.ops.js +97 -2
- package/dist/platform/ops/inbox.ops.d.ts +1 -1
- package/dist/platform/ops/inbox.ops.js +17 -7
- package/dist/platform/ops/platform.ops.d.ts +4 -2
- package/dist/platform/ops/platform.ops.js +55 -3
- package/dist/platform/platform-ops.module.js +2 -0
- package/dist/platform/route-schemas.js +3 -0
- package/dist/push/delivery.d.ts +88 -0
- package/dist/push/delivery.js +269 -0
- package/dist/push/fanout.d.ts +29 -0
- package/dist/push/fanout.js +95 -0
- package/dist/push/jobs.d.ts +43 -0
- package/dist/push/jobs.js +19 -0
- package/dist/push/preferences.service.d.ts +18 -0
- package/dist/push/preferences.service.js +82 -0
- package/dist/push/provider-host.d.ts +34 -0
- package/dist/push/provider-host.js +94 -0
- package/dist/push/push-owed.d.ts +33 -0
- package/dist/push/push-owed.js +93 -0
- package/dist/push/push-processor.d.ts +40 -0
- package/dist/push/push-processor.js +114 -0
- package/dist/push/push.controller.d.ts +28 -0
- package/dist/push/push.controller.js +92 -0
- package/dist/push/push.module.d.ts +6 -0
- package/dist/push/push.module.js +29 -0
- package/dist/push/push.schemas.d.ts +88 -0
- package/dist/push/push.schemas.js +53 -0
- package/dist/push/recipients.d.ts +29 -0
- package/dist/push/recipients.js +263 -0
- package/dist/push/registration-cleanup.d.ts +9 -0
- package/dist/push/registration-cleanup.js +16 -0
- package/dist/push/registrations.service.d.ts +55 -0
- package/dist/push/registrations.service.js +254 -0
- package/dist/push/stale.job.d.ts +25 -0
- package/dist/push/stale.job.js +76 -0
- package/dist/push/texts.d.ts +31 -0
- package/dist/push/texts.js +46 -0
- package/dist/push/token-store.d.ts +31 -0
- package/dist/push/token-store.js +52 -0
- package/dist/push/triggers.d.ts +19 -0
- package/dist/push/triggers.js +30 -0
- package/dist/roles/worker-role.d.ts +7 -4
- package/dist/roles/worker-role.js +12 -5
- package/dist/roles/worker.module.d.ts +1 -1
- package/dist/roles/worker.module.js +4 -1
- package/dist/schedules/run-tracker.d.ts +10 -2
- package/dist/schedules/run-tracker.js +18 -4
- package/dist/setup/setup-checks.schemas.d.ts +29 -0
- package/dist/setup/setup-checks.schemas.js +32 -0
- package/dist/setup/setup-checks.service.d.ts +36 -3
- package/dist/setup/setup-checks.service.js +137 -29
- package/package.json +4 -4
- package/src/migrations/0172_push.sql +40 -0
- package/src/migrations/0173_push_auth_notice.sql +10 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The push queue's names, job ids and job data (mobile app spec §9.4). Ids are deterministic and
|
|
3
|
+
* carry no `:` (kernel ids are UUIDs, seq is a number), so the outbox fast path and the relay
|
|
4
|
+
* adding the same job twice, or an event owed again, is one job.
|
|
5
|
+
*/
|
|
6
|
+
/** Queue of push fanouts and deliveries (no `:` in the name). */
|
|
7
|
+
export const PUSH_QUEUE = "cap-push-deliver";
|
|
8
|
+
/** Resolves an event's recipients and owes one {@link PUSH_DELIVER_JOB} per registration. */
|
|
9
|
+
export const PUSH_FANOUT_JOB = "push-fanout";
|
|
10
|
+
/** Sends one message to one registration through the provider it names. */
|
|
11
|
+
export const PUSH_DELIVER_JOB = "push-deliver";
|
|
12
|
+
/** The event id of a conversation event (`conv-<conversationId>-<seq>`). */
|
|
13
|
+
export const conversationEventId = (conversationId, seq) => `conv-${conversationId}-${seq}`;
|
|
14
|
+
/** The event id of a scheduled task run's settle (`sched-<taskRunId>`). */
|
|
15
|
+
export const scheduleRunEventId = (taskRunId) => `sched-${taskRunId}`;
|
|
16
|
+
/** One fanout per event: `push-fanout-<eventId>`. */
|
|
17
|
+
export const pushFanoutJobId = (eventId) => `push-fanout-${eventId}`;
|
|
18
|
+
/** One delivery per event and registration: `push-<eventId>-<registrationId>`. */
|
|
19
|
+
export const pushDeliverJobId = (eventId, registrationId) => `push-${eventId}-${registrationId}`;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
2
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
3
|
+
import { type PushCategory, type PushPreferences, type PushPreferencesPatch } from "./push.schemas.js";
|
|
4
|
+
/**
|
|
5
|
+
* A person's push category switches (mobile app spec §9.4): one row per category switched, an
|
|
6
|
+
* absent row is on. They belong to the account and apply in every org.
|
|
7
|
+
*/
|
|
8
|
+
export declare class PushPreferencesService {
|
|
9
|
+
private readonly db;
|
|
10
|
+
readonly txHost: TransactionHost;
|
|
11
|
+
constructor(db: KernelDb, txHost: TransactionHost);
|
|
12
|
+
/** The four switches of `accountId`. */
|
|
13
|
+
get(accountId: string): Promise<PushPreferences>;
|
|
14
|
+
/** Sets the switches given (the others keep their value) and answers all four. */
|
|
15
|
+
set(accountId: string, patch: PushPreferencesPatch): Promise<PushPreferences>;
|
|
16
|
+
/** The accounts among `accountIds` that have `category` on. */
|
|
17
|
+
enabledFor(accountIds: readonly string[], category: PushCategory): Promise<Set<string>>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
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 { sql } from "kysely";
|
|
12
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
13
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
14
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
15
|
+
import { PUSH_CATEGORIES, } from "./push.schemas.js";
|
|
16
|
+
/**
|
|
17
|
+
* A person's push category switches (mobile app spec §9.4): one row per category switched, an
|
|
18
|
+
* absent row is on. They belong to the account and apply in every org.
|
|
19
|
+
*/
|
|
20
|
+
let PushPreferencesService = class PushPreferencesService {
|
|
21
|
+
db;
|
|
22
|
+
txHost;
|
|
23
|
+
constructor(db, txHost) {
|
|
24
|
+
this.db = db;
|
|
25
|
+
this.txHost = txHost;
|
|
26
|
+
}
|
|
27
|
+
/** The four switches of `accountId`. */
|
|
28
|
+
async get(accountId) {
|
|
29
|
+
const rows = await this.db
|
|
30
|
+
.selectFrom("push_preferences")
|
|
31
|
+
.select(["category", "enabled"])
|
|
32
|
+
.where("account_id", "=", accountId)
|
|
33
|
+
.execute();
|
|
34
|
+
const out = { waiting: true, replies: true, tasks: true, schedules: true };
|
|
35
|
+
for (const r of rows)
|
|
36
|
+
out[r.category] = r.enabled;
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/** Sets the switches given (the others keep their value) and answers all four. */
|
|
40
|
+
async set(accountId, patch) {
|
|
41
|
+
const values = PUSH_CATEGORIES.flatMap((category) => {
|
|
42
|
+
const enabled = patch[category];
|
|
43
|
+
return enabled === undefined ? [] : [{ account_id: accountId, category, enabled }];
|
|
44
|
+
});
|
|
45
|
+
if (values.length > 0) {
|
|
46
|
+
await this.db
|
|
47
|
+
.insertInto("push_preferences")
|
|
48
|
+
.values(values)
|
|
49
|
+
.onConflict((oc) => oc.columns(["account_id", "category"]).doUpdateSet((eb) => ({
|
|
50
|
+
enabled: eb.ref("excluded.enabled"),
|
|
51
|
+
updated_at: sql `now()`,
|
|
52
|
+
})))
|
|
53
|
+
.execute();
|
|
54
|
+
}
|
|
55
|
+
return this.get(accountId);
|
|
56
|
+
}
|
|
57
|
+
/** The accounts among `accountIds` that have `category` on. */
|
|
58
|
+
async enabledFor(accountIds, category) {
|
|
59
|
+
const out = new Set(accountIds);
|
|
60
|
+
if (out.size === 0)
|
|
61
|
+
return out;
|
|
62
|
+
const off = await this.db
|
|
63
|
+
.selectFrom("push_preferences")
|
|
64
|
+
.select("account_id")
|
|
65
|
+
.where("account_id", "in", [...out])
|
|
66
|
+
.where("category", "=", category)
|
|
67
|
+
.where("enabled", "=", false)
|
|
68
|
+
.execute();
|
|
69
|
+
for (const r of off)
|
|
70
|
+
out.delete(r.account_id);
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
__decorate([
|
|
75
|
+
Transactional()
|
|
76
|
+
], PushPreferencesService.prototype, "set", null);
|
|
77
|
+
PushPreferencesService = __decorate([
|
|
78
|
+
Injectable(),
|
|
79
|
+
__param(0, Inject(KernelDb)),
|
|
80
|
+
__param(1, Inject(TransactionHost))
|
|
81
|
+
], PushPreferencesService);
|
|
82
|
+
export { PushPreferencesService };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type PushProvider } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { ExtensionRegistry } from "../extensions/registry.js";
|
|
3
|
+
import type { KernelLogger } from "../log.js";
|
|
4
|
+
/** One app id and the push providers whose credentials cover it. */
|
|
5
|
+
export interface PushCoverage {
|
|
6
|
+
appId: string;
|
|
7
|
+
providers: string[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Reads the `push-provider@1` extensions the registry holds. Nothing is cached (the public meta
|
|
11
|
+
* calls {@link coverage} on every request): `apps()` is synchronous and derived from deployment
|
|
12
|
+
* config, so a disabled or reconfigured provider is reflected at once.
|
|
13
|
+
*/
|
|
14
|
+
export declare class PushProviderHost {
|
|
15
|
+
private readonly registry;
|
|
16
|
+
private readonly log;
|
|
17
|
+
constructor(registry: ExtensionRegistry, log: KernelLogger);
|
|
18
|
+
/** The `@1` provider of `plugin`, or null when it provides none (or is not loaded). */
|
|
19
|
+
provider(plugin: string): PushProvider | null;
|
|
20
|
+
/** Whether `plugin`'s credentials cover `appId`; a throwing `apps()` counts as not covered. */
|
|
21
|
+
covers(plugin: string, appId: string): boolean;
|
|
22
|
+
/** Every covered app id with its providers, sorted by app id, then provider name. */
|
|
23
|
+
coverage(): PushCoverage[];
|
|
24
|
+
/** Whether any push provider is loaded (the fanout gate). */
|
|
25
|
+
any(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* `apps()` of one provider; a throw is logged with the plugin name only (never config). The
|
|
28
|
+
* public meta lists coverage on every request, so that path logs at debug, not warn.
|
|
29
|
+
*/
|
|
30
|
+
private appsOf;
|
|
31
|
+
}
|
|
32
|
+
/** {@link PushProviderHost}: below the extension host, imported by the domains that push. */
|
|
33
|
+
export declare class PushHostModule {
|
|
34
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
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 { PUSH_PROVIDER_EXTENSION_POINT } from "@capybara-hq/plugin-sdk";
|
|
11
|
+
import { Inject, Injectable, Module } from "@nestjs/common";
|
|
12
|
+
import { ExtensionRegistry, ExtensionRegistryModule } from "../extensions/registry.js";
|
|
13
|
+
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
14
|
+
/**
|
|
15
|
+
* Reads the `push-provider@1` extensions the registry holds. Nothing is cached (the public meta
|
|
16
|
+
* calls {@link coverage} on every request): `apps()` is synchronous and derived from deployment
|
|
17
|
+
* config, so a disabled or reconfigured provider is reflected at once.
|
|
18
|
+
*/
|
|
19
|
+
let PushProviderHost = class PushProviderHost {
|
|
20
|
+
registry;
|
|
21
|
+
log;
|
|
22
|
+
constructor(registry, log) {
|
|
23
|
+
this.registry = registry;
|
|
24
|
+
this.log = log;
|
|
25
|
+
}
|
|
26
|
+
/** The `@1` provider of `plugin`, or null when it provides none (or is not loaded). */
|
|
27
|
+
provider(plugin) {
|
|
28
|
+
const entry = this.registry
|
|
29
|
+
.list("push-provider")
|
|
30
|
+
.find((e) => e.plugin === plugin && e.capability === PUSH_PROVIDER_EXTENSION_POINT);
|
|
31
|
+
return entry === undefined ? null : entry.impl;
|
|
32
|
+
}
|
|
33
|
+
/** Whether `plugin`'s credentials cover `appId`; a throwing `apps()` counts as not covered. */
|
|
34
|
+
covers(plugin, appId) {
|
|
35
|
+
const provider = this.provider(plugin);
|
|
36
|
+
if (provider === null)
|
|
37
|
+
return false;
|
|
38
|
+
return this.appsOf(plugin, provider, "warn").includes(appId);
|
|
39
|
+
}
|
|
40
|
+
/** Every covered app id with its providers, sorted by app id, then provider name. */
|
|
41
|
+
coverage() {
|
|
42
|
+
const byApp = new Map();
|
|
43
|
+
for (const entry of this.registry.list("push-provider")) {
|
|
44
|
+
if (entry.capability !== PUSH_PROVIDER_EXTENSION_POINT)
|
|
45
|
+
continue;
|
|
46
|
+
for (const appId of this.appsOf(entry.plugin, entry.impl, "debug")) {
|
|
47
|
+
const set = byApp.get(appId) ?? new Set();
|
|
48
|
+
set.add(entry.plugin);
|
|
49
|
+
byApp.set(appId, set);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
53
|
+
return [...byApp.keys()].sort(cmp).map((appId) => ({
|
|
54
|
+
appId,
|
|
55
|
+
providers: [...byApp.get(appId)].sort(cmp),
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
/** Whether any push provider is loaded (the fanout gate). */
|
|
59
|
+
any() {
|
|
60
|
+
return this.registry
|
|
61
|
+
.list("push-provider")
|
|
62
|
+
.some((e) => e.capability === PUSH_PROVIDER_EXTENSION_POINT);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* `apps()` of one provider; a throw is logged with the plugin name only (never config). The
|
|
66
|
+
* public meta lists coverage on every request, so that path logs at debug, not warn.
|
|
67
|
+
*/
|
|
68
|
+
appsOf(plugin, provider, level) {
|
|
69
|
+
try {
|
|
70
|
+
return provider.apps();
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
this.log[level]("push provider apps() failed", { plugin });
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
PushProviderHost = __decorate([
|
|
79
|
+
Injectable(),
|
|
80
|
+
__param(0, Inject(ExtensionRegistry)),
|
|
81
|
+
__param(1, Inject(KERNEL_LOGGER))
|
|
82
|
+
], PushProviderHost);
|
|
83
|
+
export { PushProviderHost };
|
|
84
|
+
/** {@link PushProviderHost}: below the extension host, imported by the domains that push. */
|
|
85
|
+
let PushHostModule = class PushHostModule {
|
|
86
|
+
};
|
|
87
|
+
PushHostModule = __decorate([
|
|
88
|
+
Module({
|
|
89
|
+
imports: [ExtensionRegistryModule],
|
|
90
|
+
providers: [PushProviderHost],
|
|
91
|
+
exports: [PushProviderHost],
|
|
92
|
+
})
|
|
93
|
+
], PushHostModule);
|
|
94
|
+
export { PushHostModule };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ConversationEvent } from "../conversations/events.js";
|
|
2
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
3
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
4
|
+
import { PushProviderHost } from "./provider-host.js";
|
|
5
|
+
/**
|
|
6
|
+
* Owes the push fanout of a trigger event in the event's own transaction (mobile app spec §9.4):
|
|
7
|
+
* one `push-fanout` outbox row per event, under `push-fanout-<eventId>`, naming the event by id
|
|
8
|
+
* only (recipients are resolved when the job runs). Nothing is written while no `push-provider`
|
|
9
|
+
* is loaded (plan decision 10), for an eval conversation, or for a run's end outside a chat
|
|
10
|
+
* conversation.
|
|
11
|
+
*/
|
|
12
|
+
export declare class PushOwed {
|
|
13
|
+
private readonly outbox;
|
|
14
|
+
private readonly host;
|
|
15
|
+
readonly txHost: TransactionHost;
|
|
16
|
+
constructor(outbox: Pick<OutboxService, "add" | "addMany">, host: Pick<PushProviderHost, "any">, txHost: TransactionHost);
|
|
17
|
+
/**
|
|
18
|
+
* The fanouts of `events`, just appended to `conversationId` (of `kind`), in the caller's
|
|
19
|
+
* transaction.
|
|
20
|
+
*/
|
|
21
|
+
oweIn(conversationId: string, orgId: string, kind: string, events: readonly ConversationEvent[]): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* The fanout of a scheduled task run's settle (completed or failed), in the caller's
|
|
24
|
+
* transaction.
|
|
25
|
+
*/
|
|
26
|
+
oweScheduleRun(taskRunId: string, orgId: string): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* {@link PushOwed}: infra and the extension registry only, so the event log and the scheduled task
|
|
30
|
+
* run tracker import it without a cycle.
|
|
31
|
+
*/
|
|
32
|
+
export declare class PushOwedModule {
|
|
33
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
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, Module } from "@nestjs/common";
|
|
11
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
12
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
13
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
14
|
+
import { conversationEventId, PUSH_FANOUT_JOB, PUSH_QUEUE, pushFanoutJobId, scheduleRunEventId, } from "./jobs.js";
|
|
15
|
+
import { PushHostModule, PushProviderHost } from "./provider-host.js";
|
|
16
|
+
import { pushTriggerOf } from "./triggers.js";
|
|
17
|
+
/**
|
|
18
|
+
* Owes the push fanout of a trigger event in the event's own transaction (mobile app spec §9.4):
|
|
19
|
+
* one `push-fanout` outbox row per event, under `push-fanout-<eventId>`, naming the event by id
|
|
20
|
+
* only (recipients are resolved when the job runs). Nothing is written while no `push-provider`
|
|
21
|
+
* is loaded (plan decision 10), for an eval conversation, or for a run's end outside a chat
|
|
22
|
+
* conversation.
|
|
23
|
+
*/
|
|
24
|
+
let PushOwed = class PushOwed {
|
|
25
|
+
outbox;
|
|
26
|
+
host;
|
|
27
|
+
txHost;
|
|
28
|
+
constructor(outbox, host, txHost) {
|
|
29
|
+
this.outbox = outbox;
|
|
30
|
+
this.host = host;
|
|
31
|
+
this.txHost = txHost;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The fanouts of `events`, just appended to `conversationId` (of `kind`), in the caller's
|
|
35
|
+
* transaction.
|
|
36
|
+
*/
|
|
37
|
+
async oweIn(conversationId, orgId, kind, events) {
|
|
38
|
+
if (kind === "eval")
|
|
39
|
+
return;
|
|
40
|
+
const owed = events.filter((e) => {
|
|
41
|
+
const t = pushTriggerOf(e);
|
|
42
|
+
return t !== null && (t.category !== "replies" || kind === "chat");
|
|
43
|
+
});
|
|
44
|
+
if (owed.length === 0 || !this.host.any())
|
|
45
|
+
return;
|
|
46
|
+
await this.outbox.addMany(owed.map((e) => fanoutJob(conversationEventId(conversationId, e.seq), orgId, {
|
|
47
|
+
kind: "conversation",
|
|
48
|
+
conversationId,
|
|
49
|
+
seq: e.seq,
|
|
50
|
+
})));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The fanout of a scheduled task run's settle (completed or failed), in the caller's
|
|
54
|
+
* transaction.
|
|
55
|
+
*/
|
|
56
|
+
async oweScheduleRun(taskRunId, orgId) {
|
|
57
|
+
if (!this.host.any())
|
|
58
|
+
return;
|
|
59
|
+
await this.outbox.add(fanoutJob(scheduleRunEventId(taskRunId), orgId, { kind: "schedule", taskRunId }));
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
__decorate([
|
|
63
|
+
Transactional({ propagation: "mandatory" })
|
|
64
|
+
], PushOwed.prototype, "oweIn", null);
|
|
65
|
+
__decorate([
|
|
66
|
+
Transactional({ propagation: "mandatory" })
|
|
67
|
+
], PushOwed.prototype, "oweScheduleRun", null);
|
|
68
|
+
PushOwed = __decorate([
|
|
69
|
+
Injectable(),
|
|
70
|
+
__param(0, Inject(OutboxService)),
|
|
71
|
+
__param(1, Inject(PushProviderHost)),
|
|
72
|
+
__param(2, Inject(TransactionHost))
|
|
73
|
+
], PushOwed);
|
|
74
|
+
export { PushOwed };
|
|
75
|
+
function fanoutJob(eventId, orgId, source) {
|
|
76
|
+
return {
|
|
77
|
+
queue: PUSH_QUEUE,
|
|
78
|
+
name: PUSH_FANOUT_JOB,
|
|
79
|
+
jobId: pushFanoutJobId(eventId),
|
|
80
|
+
data: { eventId, source },
|
|
81
|
+
orgId,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* {@link PushOwed}: infra and the extension registry only, so the event log and the scheduled task
|
|
86
|
+
* run tracker import it without a cycle.
|
|
87
|
+
*/
|
|
88
|
+
let PushOwedModule = class PushOwedModule {
|
|
89
|
+
};
|
|
90
|
+
PushOwedModule = __decorate([
|
|
91
|
+
Module({ imports: [PushHostModule], providers: [PushOwed], exports: [PushOwed] })
|
|
92
|
+
], PushOwedModule);
|
|
93
|
+
export { PushOwedModule };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { WorkerHost } from "@nestjs/bullmq";
|
|
2
|
+
import { type Job, type Worker } from "bullmq";
|
|
3
|
+
import type { KernelLogger } from "../log.js";
|
|
4
|
+
import { PushDelivery } from "./delivery.js";
|
|
5
|
+
import { PushFanout } from "./fanout.js";
|
|
6
|
+
import { type PushDeliverData, type PushFanoutData } from "./jobs.js";
|
|
7
|
+
/** Data of any job on {@link PUSH_QUEUE}. */
|
|
8
|
+
type PushJobData = PushDeliverData | PushFanoutData;
|
|
9
|
+
/**
|
|
10
|
+
* The consumer of {@link PUSH_QUEUE} (`cap-push-deliver`): `push-fanout` jobs go to
|
|
11
|
+
* {@link PushFanout}, `push-deliver` jobs to {@link PushDelivery}, each inside `runJob`. Worker
|
|
12
|
+
* role only (`PushWorkersModule`); built with `autorun: false` and started by the worker role
|
|
13
|
+
* after the extensions loaded, so no job runs
|
|
14
|
+
* before its provider could be loaded. A retry logs a warning and the last failed attempt an
|
|
15
|
+
* error, with ids and the failure only (never the token).
|
|
16
|
+
*/
|
|
17
|
+
export declare class PushProcessor extends WorkerHost<Worker<PushJobData>> {
|
|
18
|
+
private readonly delivery;
|
|
19
|
+
private readonly fanout;
|
|
20
|
+
private readonly log;
|
|
21
|
+
constructor(delivery: PushDelivery, fanout: PushFanout, log: KernelLogger);
|
|
22
|
+
process(job: Job<PushJobData>): Promise<unknown>;
|
|
23
|
+
/** Starts consuming (worker role, after the extensions loaded). */
|
|
24
|
+
start(): void;
|
|
25
|
+
/**
|
|
26
|
+
* Closes the worker, waiting for the jobs in flight: each send is bounded by the send time
|
|
27
|
+
* limit, and aborting one the provider may already have accepted would only resend it.
|
|
28
|
+
*/
|
|
29
|
+
close(): Promise<void>;
|
|
30
|
+
failed(job: Job<PushJobData> | undefined, err: Error): void;
|
|
31
|
+
error(err: Error): void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The push queue's consumer (fanout and delivery) and the stale registration prune; imported only
|
|
35
|
+
* by the worker role (`WorkerModule`). The queue itself is registered by {@link PushModule} (every
|
|
36
|
+
* role owes jobs).
|
|
37
|
+
*/
|
|
38
|
+
export declare class PushWorkersModule {
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
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 { OnWorkerEvent, Processor, WorkerHost } from "@nestjs/bullmq";
|
|
11
|
+
import { Inject, Injectable, Module } from "@nestjs/common";
|
|
12
|
+
import { UnrecoverableError } from "bullmq";
|
|
13
|
+
import { ExtensionsModule } from "../extensions/extensions.module.js";
|
|
14
|
+
import { runJob } from "../infra/queue/run-job.js";
|
|
15
|
+
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
16
|
+
import { PushDelivery } from "./delivery.js";
|
|
17
|
+
import { PushFanout } from "./fanout.js";
|
|
18
|
+
import { PUSH_DELIVER_JOB, PUSH_FANOUT_JOB, PUSH_QUEUE, } from "./jobs.js";
|
|
19
|
+
import { PushHostModule } from "./provider-host.js";
|
|
20
|
+
import { PushModule } from "./push.module.js";
|
|
21
|
+
import { PushStaleJob } from "./stale.job.js";
|
|
22
|
+
/** Concurrent push jobs per worker replica (each is one provider call, mostly network wait). */
|
|
23
|
+
const PUSH_CONCURRENCY = 8;
|
|
24
|
+
/**
|
|
25
|
+
* The consumer of {@link PUSH_QUEUE} (`cap-push-deliver`): `push-fanout` jobs go to
|
|
26
|
+
* {@link PushFanout}, `push-deliver` jobs to {@link PushDelivery}, each inside `runJob`. Worker
|
|
27
|
+
* role only (`PushWorkersModule`); built with `autorun: false` and started by the worker role
|
|
28
|
+
* after the extensions loaded, so no job runs
|
|
29
|
+
* before its provider could be loaded. A retry logs a warning and the last failed attempt an
|
|
30
|
+
* error, with ids and the failure only (never the token).
|
|
31
|
+
*/
|
|
32
|
+
let PushProcessor = class PushProcessor extends WorkerHost {
|
|
33
|
+
delivery;
|
|
34
|
+
fanout;
|
|
35
|
+
log;
|
|
36
|
+
constructor(delivery, fanout, log) {
|
|
37
|
+
super();
|
|
38
|
+
this.delivery = delivery;
|
|
39
|
+
this.fanout = fanout;
|
|
40
|
+
this.log = log;
|
|
41
|
+
}
|
|
42
|
+
process(job) {
|
|
43
|
+
return runJob(PUSH_QUEUE, job, async () => {
|
|
44
|
+
switch (job.name) {
|
|
45
|
+
case PUSH_FANOUT_JOB:
|
|
46
|
+
return this.fanout.run(job.data);
|
|
47
|
+
case PUSH_DELIVER_JOB:
|
|
48
|
+
return this.delivery.deliver(job.data);
|
|
49
|
+
default:
|
|
50
|
+
throw new UnrecoverableError(`unknown push job ${job.name}`);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/** Starts consuming (worker role, after the extensions loaded). */
|
|
55
|
+
start() {
|
|
56
|
+
void this.worker.run().catch((err) => this.error(err));
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Closes the worker, waiting for the jobs in flight: each send is bounded by the send time
|
|
60
|
+
* limit, and aborting one the provider may already have accepted would only resend it.
|
|
61
|
+
*/
|
|
62
|
+
async close() {
|
|
63
|
+
await this.worker.close();
|
|
64
|
+
}
|
|
65
|
+
failed(job, err) {
|
|
66
|
+
if (!job)
|
|
67
|
+
return;
|
|
68
|
+
const fields = {
|
|
69
|
+
jobId: job.id,
|
|
70
|
+
job: job.name,
|
|
71
|
+
eventId: job.data.eventId,
|
|
72
|
+
...("registrationId" in job.data && { registrationId: job.data.registrationId }),
|
|
73
|
+
attempt: job.attemptsMade,
|
|
74
|
+
error: err.message,
|
|
75
|
+
};
|
|
76
|
+
if (job.attemptsMade >= (job.opts.attempts ?? 1)) {
|
|
77
|
+
this.log.error("push job failed on its last attempt; given up", fields);
|
|
78
|
+
}
|
|
79
|
+
else
|
|
80
|
+
this.log.warn("push job failed; retrying", fields);
|
|
81
|
+
}
|
|
82
|
+
error(err) {
|
|
83
|
+
this.log.error("push worker error", { err });
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
__decorate([
|
|
87
|
+
OnWorkerEvent("failed")
|
|
88
|
+
], PushProcessor.prototype, "failed", null);
|
|
89
|
+
__decorate([
|
|
90
|
+
OnWorkerEvent("error")
|
|
91
|
+
], PushProcessor.prototype, "error", null);
|
|
92
|
+
PushProcessor = __decorate([
|
|
93
|
+
Injectable(),
|
|
94
|
+
Processor(PUSH_QUEUE, { autorun: false, concurrency: PUSH_CONCURRENCY }),
|
|
95
|
+
__param(0, Inject(PushDelivery)),
|
|
96
|
+
__param(1, Inject(PushFanout)),
|
|
97
|
+
__param(2, Inject(KERNEL_LOGGER))
|
|
98
|
+
], PushProcessor);
|
|
99
|
+
export { PushProcessor };
|
|
100
|
+
/**
|
|
101
|
+
* The push queue's consumer (fanout and delivery) and the stale registration prune; imported only
|
|
102
|
+
* by the worker role (`WorkerModule`). The queue itself is registered by {@link PushModule} (every
|
|
103
|
+
* role owes jobs).
|
|
104
|
+
*/
|
|
105
|
+
let PushWorkersModule = class PushWorkersModule {
|
|
106
|
+
};
|
|
107
|
+
PushWorkersModule = __decorate([
|
|
108
|
+
Module({
|
|
109
|
+
imports: [PushModule, PushHostModule, ExtensionsModule],
|
|
110
|
+
providers: [PushDelivery, PushFanout, PushProcessor, PushStaleJob],
|
|
111
|
+
exports: [PushProcessor],
|
|
112
|
+
})
|
|
113
|
+
], PushWorkersModule);
|
|
114
|
+
export { PushWorkersModule };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import { type Principal } from "../identity/principal.js";
|
|
3
|
+
import { PushPreferencesService } from "./preferences.service.js";
|
|
4
|
+
import { type PushPreferences, type PushPreferencesPatchSchema, type PushRegistrationParams, type PushRegistrationSchema, type PushRegistrationView } from "./push.schemas.js";
|
|
5
|
+
import { PushRegistrationsService } from "./registrations.service.js";
|
|
6
|
+
/**
|
|
7
|
+
* The person's push registrations and switches (mobile app spec §9.3, §9.4). Registrations are a
|
|
8
|
+
* device action of the person's app: web sessions only, so a registration always has the session
|
|
9
|
+
* it follows. The token is never answered.
|
|
10
|
+
*/
|
|
11
|
+
export declare class PushController {
|
|
12
|
+
private readonly registrations;
|
|
13
|
+
private readonly preferences;
|
|
14
|
+
constructor(registrations: PushRegistrationsService, preferences: PushPreferencesService);
|
|
15
|
+
/** Registers this app install; 422 `push.provider_unavailable` when no provider covers it. */
|
|
16
|
+
register(principal: Principal, body: z.infer<typeof PushRegistrationSchema>): Promise<{
|
|
17
|
+
id: string;
|
|
18
|
+
}>;
|
|
19
|
+
/** The person's installs that receive push, newest seen first. */
|
|
20
|
+
list(principal: Principal): Promise<{
|
|
21
|
+
items: PushRegistrationView[];
|
|
22
|
+
}>;
|
|
23
|
+
/** Removes one of the person's installs; another account's id → 404. */
|
|
24
|
+
remove(principal: Principal, { id }: z.infer<typeof PushRegistrationParams>): Promise<void>;
|
|
25
|
+
getPreferences(principal: Principal): Promise<PushPreferences>;
|
|
26
|
+
/** Sets the switches given (at least one) and answers all four. */
|
|
27
|
+
setPreferences(principal: Principal, body: z.infer<typeof PushPreferencesPatchSchema>): Promise<PushPreferences>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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, Delete, Get, HttpCode, Inject, Post, Put } from "@nestjs/common";
|
|
11
|
+
import { ApiError } from "../errors.js";
|
|
12
|
+
import { CurrentPrincipal, requireUser, SessionOnly, } from "../identity/principal.js";
|
|
13
|
+
import { ZBody, ZParam } from "../infra/validation/zod-params.js";
|
|
14
|
+
import { PushPreferencesService } from "./preferences.service.js";
|
|
15
|
+
import { PushPreferencesPatchDto, PushRegistrationDto, PushRegistrationParamsDto, } from "./push.schemas.js";
|
|
16
|
+
import { PushRegistrationsService } from "./registrations.service.js";
|
|
17
|
+
/** The web session behind a request; a personal token never reaches here (`@SessionOnly`). */
|
|
18
|
+
function sessionOf(p) {
|
|
19
|
+
if (p.via !== "session") {
|
|
20
|
+
throw new ApiError("auth.session_required", "This endpoint requires a web session");
|
|
21
|
+
}
|
|
22
|
+
return p.sessionId;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The person's push registrations and switches (mobile app spec §9.3, §9.4). Registrations are a
|
|
26
|
+
* device action of the person's app: web sessions only, so a registration always has the session
|
|
27
|
+
* it follows. The token is never answered.
|
|
28
|
+
*/
|
|
29
|
+
let PushController = class PushController {
|
|
30
|
+
registrations;
|
|
31
|
+
preferences;
|
|
32
|
+
constructor(registrations, preferences) {
|
|
33
|
+
this.registrations = registrations;
|
|
34
|
+
this.preferences = preferences;
|
|
35
|
+
}
|
|
36
|
+
/** Registers this app install; 422 `push.provider_unavailable` when no provider covers it. */
|
|
37
|
+
async register(principal, body) {
|
|
38
|
+
const p = requireUser(principal);
|
|
39
|
+
return this.registrations.register(p.account.id, sessionOf(p), body);
|
|
40
|
+
}
|
|
41
|
+
/** The person's installs that receive push, newest seen first. */
|
|
42
|
+
async list(principal) {
|
|
43
|
+
const p = requireUser(principal);
|
|
44
|
+
return { items: await this.registrations.list(p.account.id, sessionOf(p)) };
|
|
45
|
+
}
|
|
46
|
+
/** Removes one of the person's installs; another account's id → 404. */
|
|
47
|
+
async remove(principal, { id }) {
|
|
48
|
+
// @SessionOnly() already refused personal tokens; removal needs no session id
|
|
49
|
+
await this.registrations.remove(requireUser(principal).account.id, id);
|
|
50
|
+
}
|
|
51
|
+
async getPreferences(principal) {
|
|
52
|
+
return this.preferences.get(requireUser(principal).account.id);
|
|
53
|
+
}
|
|
54
|
+
/** Sets the switches given (at least one) and answers all four. */
|
|
55
|
+
async setPreferences(principal, body) {
|
|
56
|
+
return this.preferences.set(requireUser(principal).account.id, body);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
__decorate([
|
|
60
|
+
Post("push-registrations"),
|
|
61
|
+
SessionOnly(),
|
|
62
|
+
HttpCode(201),
|
|
63
|
+
__param(0, CurrentPrincipal()),
|
|
64
|
+
__param(1, ZBody(PushRegistrationDto))
|
|
65
|
+
], PushController.prototype, "register", null);
|
|
66
|
+
__decorate([
|
|
67
|
+
Get("push-registrations"),
|
|
68
|
+
SessionOnly(),
|
|
69
|
+
__param(0, CurrentPrincipal())
|
|
70
|
+
], PushController.prototype, "list", null);
|
|
71
|
+
__decorate([
|
|
72
|
+
Delete("push-registrations/:id"),
|
|
73
|
+
SessionOnly(),
|
|
74
|
+
HttpCode(204),
|
|
75
|
+
__param(0, CurrentPrincipal()),
|
|
76
|
+
__param(1, ZParam(PushRegistrationParamsDto))
|
|
77
|
+
], PushController.prototype, "remove", null);
|
|
78
|
+
__decorate([
|
|
79
|
+
Get("push-preferences"),
|
|
80
|
+
__param(0, CurrentPrincipal())
|
|
81
|
+
], PushController.prototype, "getPreferences", null);
|
|
82
|
+
__decorate([
|
|
83
|
+
Put("push-preferences"),
|
|
84
|
+
__param(0, CurrentPrincipal()),
|
|
85
|
+
__param(1, ZBody(PushPreferencesPatchDto))
|
|
86
|
+
], PushController.prototype, "setPreferences", null);
|
|
87
|
+
PushController = __decorate([
|
|
88
|
+
Controller("api/v1/me"),
|
|
89
|
+
__param(0, Inject(PushRegistrationsService)),
|
|
90
|
+
__param(1, Inject(PushPreferencesService))
|
|
91
|
+
], PushController);
|
|
92
|
+
export { PushController };
|