@capybara-hq/kernel 0.1.0-rc.60 → 0.1.0-rc.61
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/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,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 };
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
import { Module } from "@nestjs/common";
|
|
8
|
+
import { QueueModule } from "../infra/queue/queue.module.js";
|
|
9
|
+
import { PUSH_QUEUE } from "./jobs.js";
|
|
10
|
+
import { PushPreferencesService } from "./preferences.service.js";
|
|
11
|
+
import { PushHostModule } from "./provider-host.js";
|
|
12
|
+
import { PushRegistrationsService } from "./registrations.service.js";
|
|
13
|
+
import { PushTokenStore } from "./token-store.js";
|
|
14
|
+
/** The push queue (`cap-push-deliver`): registered on every role, since every role owes jobs. */
|
|
15
|
+
const PUSH_QUEUE_MODULE = QueueModule.registerQueue(PUSH_QUEUE);
|
|
16
|
+
/**
|
|
17
|
+
* Push registrations and preferences (mobile app spec §9.3, §9.4) and the push queue; the
|
|
18
|
+
* controller is the API's, the queue's consumer the worker role's (`PushWorkersModule`).
|
|
19
|
+
*/
|
|
20
|
+
let PushModule = class PushModule {
|
|
21
|
+
};
|
|
22
|
+
PushModule = __decorate([
|
|
23
|
+
Module({
|
|
24
|
+
imports: [PushHostModule, PUSH_QUEUE_MODULE],
|
|
25
|
+
providers: [PushTokenStore, PushRegistrationsService, PushPreferencesService],
|
|
26
|
+
exports: [PushTokenStore, PushRegistrationsService, PushPreferencesService, PUSH_QUEUE_MODULE],
|
|
27
|
+
})
|
|
28
|
+
], PushModule);
|
|
29
|
+
export { PushModule };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { RouteSchemas } from "../platform/route-schemas.js";
|
|
3
|
+
/** The push categories a person switches (mobile app spec §9.4); an absent preference is on. */
|
|
4
|
+
export declare const PUSH_CATEGORIES: readonly ["waiting", "replies", "tasks", "schedules"];
|
|
5
|
+
export type PushCategory = (typeof PUSH_CATEGORIES)[number];
|
|
6
|
+
/**
|
|
7
|
+
* `POST /me/push-registrations`: what the app's shell returns from `push@1.register()`, plus the
|
|
8
|
+
* person's locale and a device name. The token is sealed at rest and never echoed.
|
|
9
|
+
*/
|
|
10
|
+
export declare const PushRegistrationSchema: z.ZodObject<{
|
|
11
|
+
platform: z.ZodEnum<{
|
|
12
|
+
android: "android";
|
|
13
|
+
ios: "ios";
|
|
14
|
+
}>;
|
|
15
|
+
appId: z.ZodString;
|
|
16
|
+
provider: z.ZodString;
|
|
17
|
+
token: z.ZodString;
|
|
18
|
+
locale: z.ZodEnum<{
|
|
19
|
+
en: "en";
|
|
20
|
+
zh: "zh";
|
|
21
|
+
}>;
|
|
22
|
+
deviceName: z.ZodString;
|
|
23
|
+
}, z.core.$strict>;
|
|
24
|
+
export type PushRegistrationBody = z.infer<typeof PushRegistrationSchema>;
|
|
25
|
+
declare const PushRegistrationDto_base: import("../infra/validation/create-zod-dto.js").ZodDtoClass<z.ZodObject<{
|
|
26
|
+
platform: z.ZodEnum<{
|
|
27
|
+
android: "android";
|
|
28
|
+
ios: "ios";
|
|
29
|
+
}>;
|
|
30
|
+
appId: z.ZodString;
|
|
31
|
+
provider: z.ZodString;
|
|
32
|
+
token: z.ZodString;
|
|
33
|
+
locale: z.ZodEnum<{
|
|
34
|
+
en: "en";
|
|
35
|
+
zh: "zh";
|
|
36
|
+
}>;
|
|
37
|
+
deviceName: z.ZodString;
|
|
38
|
+
}, z.core.$strict>>;
|
|
39
|
+
export declare class PushRegistrationDto extends PushRegistrationDto_base {
|
|
40
|
+
}
|
|
41
|
+
export declare const PushRegistrationParams: z.ZodObject<{
|
|
42
|
+
id: z.ZodString;
|
|
43
|
+
}, z.core.$strict>;
|
|
44
|
+
declare const PushRegistrationParamsDto_base: import("../infra/validation/create-zod-dto.js").ZodDtoClass<z.ZodObject<{
|
|
45
|
+
id: z.ZodString;
|
|
46
|
+
}, z.core.$strict>>;
|
|
47
|
+
export declare class PushRegistrationParamsDto extends PushRegistrationParamsDto_base {
|
|
48
|
+
}
|
|
49
|
+
/** One of the person's registrations as the settings page lists it (never the token). */
|
|
50
|
+
export interface PushRegistrationView {
|
|
51
|
+
id: string;
|
|
52
|
+
platform: "ios" | "android";
|
|
53
|
+
appId: string;
|
|
54
|
+
provider: string;
|
|
55
|
+
deviceName: string;
|
|
56
|
+
locale: "zh" | "en";
|
|
57
|
+
createdAt: string;
|
|
58
|
+
lastSeenAt: string;
|
|
59
|
+
/** Posted by the session making the request. */
|
|
60
|
+
current: boolean;
|
|
61
|
+
}
|
|
62
|
+
/** The person's four push switches, as `GET` / `PUT /me/push-preferences` answer them. */
|
|
63
|
+
export declare const PushPreferencesSchema: z.ZodObject<{
|
|
64
|
+
waiting: z.ZodBoolean;
|
|
65
|
+
replies: z.ZodBoolean;
|
|
66
|
+
tasks: z.ZodBoolean;
|
|
67
|
+
schedules: z.ZodBoolean;
|
|
68
|
+
}, z.core.$strict>;
|
|
69
|
+
export type PushPreferences = z.infer<typeof PushPreferencesSchema>;
|
|
70
|
+
/** `PUT /me/push-preferences`: the switches to change, at least one. */
|
|
71
|
+
export declare const PushPreferencesPatchSchema: z.ZodObject<{
|
|
72
|
+
waiting: z.ZodOptional<z.ZodBoolean>;
|
|
73
|
+
replies: z.ZodOptional<z.ZodBoolean>;
|
|
74
|
+
tasks: z.ZodOptional<z.ZodBoolean>;
|
|
75
|
+
schedules: z.ZodOptional<z.ZodBoolean>;
|
|
76
|
+
}, z.core.$strict>;
|
|
77
|
+
export type PushPreferencesPatch = z.infer<typeof PushPreferencesPatchSchema>;
|
|
78
|
+
declare const PushPreferencesPatchDto_base: import("../infra/validation/create-zod-dto.js").ZodDtoClass<z.ZodObject<{
|
|
79
|
+
waiting: z.ZodOptional<z.ZodBoolean>;
|
|
80
|
+
replies: z.ZodOptional<z.ZodBoolean>;
|
|
81
|
+
tasks: z.ZodOptional<z.ZodBoolean>;
|
|
82
|
+
schedules: z.ZodOptional<z.ZodBoolean>;
|
|
83
|
+
}, z.core.$strict>>;
|
|
84
|
+
export declare class PushPreferencesPatchDto extends PushPreferencesPatchDto_base {
|
|
85
|
+
}
|
|
86
|
+
/** The push routes the Platform operations mirror (`check:platform-ops` keys). */
|
|
87
|
+
export declare const PUSH_ROUTES: readonly RouteSchemas[];
|
|
88
|
+
export {};
|