notifkit 0.1.9 → 0.1.11

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.
Files changed (80) hide show
  1. package/README.md +66 -72
  2. package/dashboard/dist/assets/{index-7mvXb4bS.js → index-Bsrmo9LE.js} +1 -1
  3. package/dashboard/dist/assets/index-s-SXGIwv.css +1 -0
  4. package/dashboard/dist/index.html +2 -2
  5. package/dist/bootstrap-GRXzgPvW.mjs +89 -0
  6. package/dist/bulk-BvRfz6Of.mjs +77 -0
  7. package/dist/index.d.mts +1107 -8885
  8. package/dist/index.mjs +2 -38
  9. package/dist/{main-DSmh1e-V.mjs → main-BENOY7HG.mjs} +49 -75
  10. package/dist/{main-BPbPnBIG.mjs → main-BpnuFX62.mjs} +119 -112
  11. package/dist/{main-4Wyge9ms.mjs → main-BqVH2BHj.mjs} +57 -54
  12. package/dist/{main-D19Wxs0x.mjs → main-C_5VFIwp.mjs} +135 -79
  13. package/dist/main-D6od9N8h.mjs +312 -0
  14. package/dist/main-D84OX0ux.mjs +811 -0
  15. package/dist/{main-JdOvJ70b.mjs → main-HH-WwsMM.mjs} +395 -184
  16. package/dist/{main-AO0JaY5z.mjs → main-HY_TDs61.mjs} +149 -134
  17. package/dist/{src-CGmXRfsM.mjs → src-CI7FGf3h.mjs} +1223 -651
  18. package/drizzle/0006_message_logs_redundant_indexes.sql +7 -0
  19. package/drizzle/0007_workflow_status_canceled.sql +3 -0
  20. package/drizzle/meta/_journal.json +14 -0
  21. package/package.json +17 -17
  22. package/scripts/create-admin.mjs +8 -11
  23. package/src/client.ts +15 -5
  24. package/src/config/index.ts +26 -3
  25. package/src/contracts/events/notification-ai-pending.ts +12 -0
  26. package/src/contracts/events/notification-delivered.ts +7 -0
  27. package/src/contracts/events/notification-dispatched.ts +21 -2
  28. package/src/contracts/events/notification-enriched.ts +16 -0
  29. package/src/contracts/events/notification-failed.ts +2 -0
  30. package/src/contracts/events/notification-requested.ts +2 -0
  31. package/src/contracts/registry.ts +2 -2
  32. package/src/contracts/sdk.ts +77 -29
  33. package/src/db/bulk.ts +132 -0
  34. package/src/db/index.ts +2 -2
  35. package/src/db/schema.ts +3 -50
  36. package/src/idempotency/index.ts +60 -6
  37. package/src/logger/index.ts +0 -12
  38. package/src/queue/index.ts +255 -76
  39. package/src/rate-limiter/index.ts +217 -16
  40. package/src/redis/index.ts +265 -0
  41. package/src/repositories/index.ts +171 -82
  42. package/src/server.ts +71 -41
  43. package/src/services/ai/main.ts +83 -70
  44. package/src/services/api/admin-static.ts +41 -12
  45. package/src/services/api/handlers.ts +357 -159
  46. package/src/services/api/http.ts +34 -7
  47. package/src/services/api/main.ts +104 -67
  48. package/src/services/api/router.ts +18 -5
  49. package/src/services/delivery/main.ts +495 -230
  50. package/src/services/delivery/throttle.ts +42 -59
  51. package/src/services/engine/main.ts +206 -152
  52. package/src/services/enricher/main.ts +155 -107
  53. package/src/services/events/main.ts +100 -55
  54. package/src/services/scheduler/main.ts +151 -79
  55. package/src/services/scheduler/send-gate.ts +117 -0
  56. package/src/services/workflow/main.ts +220 -100
  57. package/src/shared/batch-processor.ts +65 -34
  58. package/src/shared/dataloader.ts +58 -22
  59. package/src/shared/events.ts +3 -0
  60. package/src/shared/index.ts +0 -1
  61. package/src/shared/utils.ts +2 -47
  62. package/src/templates/index.ts +0 -67
  63. package/src/transport/index.ts +6 -0
  64. package/src/workers/bootstrap.ts +124 -0
  65. package/src/workers/index.ts +99 -24
  66. package/src/workflows/sdk.ts +2 -0
  67. package/dashboard/dist/assets/index-o7L-f3-c.css +0 -1
  68. package/dist/index.d.mts.map +0 -1
  69. package/dist/index.mjs.map +0 -1
  70. package/dist/main-4Wyge9ms.mjs.map +0 -1
  71. package/dist/main-AO0JaY5z.mjs.map +0 -1
  72. package/dist/main-BMYSYcv2.mjs +0 -224
  73. package/dist/main-BMYSYcv2.mjs.map +0 -1
  74. package/dist/main-BPbPnBIG.mjs.map +0 -1
  75. package/dist/main-CyeQ_yDt.mjs +0 -632
  76. package/dist/main-CyeQ_yDt.mjs.map +0 -1
  77. package/dist/main-D19Wxs0x.mjs.map +0 -1
  78. package/dist/main-DSmh1e-V.mjs.map +0 -1
  79. package/dist/main-JdOvJ70b.mjs.map +0 -1
  80. package/dist/src-CGmXRfsM.mjs.map +0 -1
package/src/db/bulk.ts ADDED
@@ -0,0 +1,132 @@
1
+ import { sql as drizzleSql } from "drizzle-orm";
2
+ import { deliveryOutbox, messageLogs } from "./schema.js";
3
+
4
+ /**
5
+ * Bulk writes for the two tables every delivered notification touches.
6
+ *
7
+ * Drizzle rebuilds the statement for each call and walks every parameter as it
8
+ * does, which for a few hundred rows per batch was the largest single CPU cost
9
+ * in a worker. These send one fixed statement over `unnest()`d arrays instead,
10
+ * so Postgres prepares it once and the client serialises a handful of arrays
11
+ * rather than thousands of parameters.
12
+ *
13
+ * Handles that are not postgres.js-backed Drizzle instances (tests, other
14
+ * drivers) fall back to the equivalent Drizzle query.
15
+ */
16
+
17
+ interface RawClient {
18
+ unsafe: (query: string, params: unknown[], options?: { prepare?: boolean }) => Promise<unknown>;
19
+ }
20
+
21
+ function rawClient(db: any): RawClient | null {
22
+ const client = db?.$client;
23
+ return client && typeof client.unsafe === "function" ? client : null;
24
+ }
25
+
26
+ export interface MessageLogRow {
27
+ projectId: string;
28
+ taskId: string;
29
+ providerMessageId: string | null;
30
+ channel: string;
31
+ attempt: number;
32
+ kind: string;
33
+ status: string;
34
+ templateId: string | null;
35
+ workflowInstanceId: string | null;
36
+ campaignId: string | null;
37
+ /** ISO time the event happened; the insert time when omitted. */
38
+ timestamp?: string;
39
+ }
40
+
41
+ const INSERT_MESSAGE_LOGS = `
42
+ INSERT INTO message_logs
43
+ (project_id, task_id, provider_message_id, channel, attempt, kind, status,
44
+ template_id, workflow_instance_id, campaign_id, "timestamp")
45
+ SELECT project_id::uuid, task_id, provider_message_id, channel::channel, attempt, kind, status,
46
+ template_id, workflow_instance_id::uuid, campaign_id, COALESCE(ts::timestamptz, now())
47
+ FROM unnest($1::text[], $2::text[], $3::text[], $4::text[], $5::int[], $6::text[], $7::text[],
48
+ $8::text[], $9::text[], $10::text[], $11::text[])
49
+ AS t(project_id, task_id, provider_message_id, channel, attempt, kind, status,
50
+ template_id, workflow_instance_id, campaign_id, ts)
51
+ ON CONFLICT DO NOTHING`;
52
+
53
+ /** Inserts delivery log rows, skipping any already recorded. */
54
+ export async function insertMessageLogs(db: any, rows: MessageLogRow[]): Promise<void> {
55
+ if (rows.length === 0) return;
56
+ const client = rawClient(db);
57
+ if (!client) {
58
+ await db
59
+ .insert(messageLogs)
60
+ .values(
61
+ rows.map(({ timestamp, ...r }) =>
62
+ timestamp ? { ...r, timestamp: new Date(timestamp) } : r,
63
+ ),
64
+ )
65
+ .onConflictDoNothing();
66
+ return;
67
+ }
68
+ await client.unsafe(
69
+ INSERT_MESSAGE_LOGS,
70
+ [
71
+ rows.map((r) => r.projectId),
72
+ rows.map((r) => r.taskId),
73
+ rows.map((r) => r.providerMessageId),
74
+ rows.map((r) => r.channel),
75
+ rows.map((r) => r.attempt),
76
+ rows.map((r) => r.kind),
77
+ rows.map((r) => r.status),
78
+ rows.map((r) => r.templateId),
79
+ rows.map((r) => r.workflowInstanceId),
80
+ rows.map((r) => r.campaignId),
81
+ rows.map((r) => r.timestamp ?? null),
82
+ ],
83
+ { prepare: true },
84
+ );
85
+ }
86
+
87
+ export interface OutboxRow {
88
+ taskId: string;
89
+ channel: string;
90
+ destination: string;
91
+ providerMessageId: string | null;
92
+ }
93
+
94
+ const UPSERT_OUTBOX = `
95
+ INSERT INTO delivery_outbox (task_id, channel, destination, provider_message_id)
96
+ SELECT task_id, channel::channel, destination, provider_message_id
97
+ FROM unnest($1::text[], $2::text[], $3::text[], $4::text[])
98
+ AS t(task_id, channel, destination, provider_message_id)
99
+ ON CONFLICT (task_id, channel, destination)
100
+ DO UPDATE SET provider_message_id = EXCLUDED.provider_message_id`;
101
+
102
+ /** Records the provider's message id for each sent task, inserting rows as needed. */
103
+ export async function upsertOutboxProviderIds(db: any, rows: OutboxRow[]): Promise<void> {
104
+ if (rows.length === 0) return;
105
+ // ON CONFLICT DO UPDATE refuses to touch one row twice in a statement, which
106
+ // a retried task landing in the same batch as its first attempt would do.
107
+ const byKey = new Map<string, OutboxRow>();
108
+ for (const r of rows) byKey.set(`${r.taskId}\u0000${r.channel}\u0000${r.destination}`, r);
109
+ const unique = [...byKey.values()];
110
+
111
+ const client = rawClient(db);
112
+ if (!client) {
113
+ await db
114
+ .insert(deliveryOutbox)
115
+ .values(unique)
116
+ .onConflictDoUpdate({
117
+ target: [deliveryOutbox.taskId, deliveryOutbox.channel, deliveryOutbox.destination],
118
+ set: { providerMessageId: drizzleSql`EXCLUDED.provider_message_id` },
119
+ });
120
+ return;
121
+ }
122
+ await client.unsafe(
123
+ UPSERT_OUTBOX,
124
+ [
125
+ unique.map((r) => r.taskId),
126
+ unique.map((r) => r.channel),
127
+ unique.map((r) => r.destination),
128
+ unique.map((r) => r.providerMessageId),
129
+ ],
130
+ { prepare: true },
131
+ );
132
+ }
package/src/db/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import postgres from "postgres";
2
2
  import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";
3
- import type { Logger } from "@/index.js";
3
+ import type { Logger } from "@/logger/index.js";
4
4
  import * as schema from "./schema.js";
5
- import { readBaseConfig } from "@/index.js";
5
+ import { readBaseConfig } from "@/config/index.js";
6
6
 
7
7
  export type Sql = postgres.Sql;
8
8
  export type Db = PostgresJsDatabase<typeof schema>;
package/src/db/schema.ts CHANGED
@@ -15,7 +15,6 @@ import {
15
15
  integer,
16
16
  } from "drizzle-orm/pg-core";
17
17
  import { sql } from "drizzle-orm";
18
- import { createInsertSchema, createSelectSchema } from "drizzle-zod";
19
18
 
20
19
  export const channelEnum = pgEnum("channel", [
21
20
  "email",
@@ -211,8 +210,8 @@ export const messageLogs = pgTable(
211
210
  timestamp: timestamp("timestamp", { withTimezone: true }).defaultNow().notNull(),
212
211
  },
213
212
  (table) => ({
214
- projectIdx: index("message_logs_project_idx").on(table.projectId),
215
- taskIdx: index("task_idx").on(table.taskId),
213
+ // No standalone project_id or task_id index: the composites below lead
214
+ // with project_id, and the unique constraint leads with task_id.
216
215
  projectIdTaskIdIdx: index("message_logs_project_task_idx").on(table.projectId, table.taskId),
217
216
  providerMsgIdx: index("provider_msg_idx").on(table.providerMessageId),
218
217
  projectTimeIdx: index("msg_log_proj_time_idx").on(table.projectId, table.timestamp),
@@ -271,6 +270,7 @@ export const workflowStatusEnum = pgEnum("workflow_status", [
271
270
  "running",
272
271
  "completed",
273
272
  "failed",
273
+ "canceled",
274
274
  ]);
275
275
 
276
276
  export const workflowDefinitions = pgTable(
@@ -376,50 +376,3 @@ export const adminUsers = pgTable("admin_users", {
376
376
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
377
377
  updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
378
378
  });
379
-
380
- // ─── Generated Zod Schemas ──────────────────────────────────────────────────
381
-
382
- export const insertAdminUserSchema = createInsertSchema(adminUsers);
383
- export const selectAdminUserSchema = createSelectSchema(adminUsers);
384
-
385
- export const insertProjectSchema = createInsertSchema(projects);
386
- export const selectProjectSchema = createSelectSchema(projects);
387
-
388
- export const insertProjectApiKeySchema = createInsertSchema(projectApiKeys);
389
- export const selectProjectApiKeySchema = createSelectSchema(projectApiKeys);
390
-
391
- export const insertUserSchema = createInsertSchema(users);
392
- export const selectUserSchema = createSelectSchema(users);
393
-
394
- export const insertUserSegmentSchema = createInsertSchema(userSegments);
395
- export const selectUserSegmentSchema = createSelectSchema(userSegments);
396
-
397
- export const insertUserContactSchema = createInsertSchema(userContacts);
398
- export const selectUserContactSchema = createSelectSchema(userContacts);
399
-
400
- export const insertUserChannelPreferenceSchema = createInsertSchema(userChannelPreferences);
401
- export const selectUserChannelPreferenceSchema = createSelectSchema(userChannelPreferences);
402
-
403
- export const insertUserTopicPreferenceSchema = createInsertSchema(userTopicPreferences);
404
- export const selectUserTopicPreferenceSchema = createSelectSchema(userTopicPreferences);
405
-
406
- export const insertMessageLogSchema = createInsertSchema(messageLogs);
407
- export const selectMessageLogSchema = createSelectSchema(messageLogs);
408
-
409
- export const insertSuppressionSchema = createInsertSchema(suppressions);
410
- export const selectSuppressionSchema = createSelectSchema(suppressions);
411
-
412
- export const insertWorkflowInstanceSchema = createInsertSchema(workflowInstances);
413
- export const selectWorkflowInstanceSchema = createSelectSchema(workflowInstances);
414
-
415
- export const insertWorkflowStepSchema = createInsertSchema(workflowSteps);
416
- export const selectWorkflowStepSchema = createSelectSchema(workflowSteps);
417
-
418
- export const insertWorkflowWaiterSchema = createInsertSchema(workflowWaiters);
419
- export const selectWorkflowWaiterSchema = createSelectSchema(workflowWaiters);
420
-
421
- export const insertDeliveryOutboxSchema = createInsertSchema(deliveryOutbox);
422
- export const selectDeliveryOutboxSchema = createSelectSchema(deliveryOutbox);
423
-
424
- export const insertScheduledPayloadSchema = createInsertSchema(scheduledPayloads);
425
- export const selectScheduledPayloadSchema = createSelectSchema(scheduledPayloads);
@@ -6,10 +6,13 @@ export interface IdempotencyOptions {
6
6
  ttlSeconds?: number;
7
7
  }
8
8
 
9
+ export type LeaseResult = "acquired" | "locked" | "completed";
10
+
9
11
  /**
10
- * SETNX-based idempotency guard.
11
- * Returns true from checkAndMark() only the first time a given ID is seen
12
- * within the TTL window; subsequent calls return false (duplicate / retry).
12
+ * Two-phase SETNX-based idempotency guard.
13
+ * Supports:
14
+ * - Phase 1 (:lock): Short lease acquired at the start of message processing.
15
+ * - Phase 2 (:sent): Long-lived completion marker recorded when processing succeeds.
13
16
  */
14
17
  export class IdempotencyGuard {
15
18
  private readonly redis: Redis;
@@ -26,6 +29,52 @@ export class IdempotencyGuard {
26
29
  return `${this.keyPrefix}:${id}`;
27
30
  }
28
31
 
32
+ private lockKey(id: string): string {
33
+ return `${this.keyPrefix}:${id}:lock`;
34
+ }
35
+
36
+ private completedKey(id: string): string {
37
+ return `${this.keyPrefix}:${id}:sent`;
38
+ }
39
+
40
+ /**
41
+ * Two-phase lease acquisition.
42
+ * Atomically checks existing state and acquires lock if available.
43
+ * - Happy path (new message): single native SET NX command storing "L" (lock lease).
44
+ * - If already present: checks if "S" (sent/completed) or "L" (locked by another worker).
45
+ */
46
+ async acquireLease(id: string, lockTtlSeconds: number = 30): Promise<LeaseResult> {
47
+ const mainKey = this.key(id);
48
+ const lockKey = this.lockKey(id);
49
+
50
+ // Fast path: attempt native atomic SET NX
51
+ const acquired = await this.redis.set(mainKey, "L", "EX", lockTtlSeconds, "NX");
52
+ if (acquired === "OK") {
53
+ return "acquired";
54
+ }
55
+
56
+ // Key exists — check whether it's completed ("S" / "1" / :sent) or in-flight ("L" / :lock)
57
+ const [val, sent, lockVal] = await Promise.all([
58
+ this.redis.get(mainKey),
59
+ this.redis.get(this.completedKey(id)),
60
+ this.redis.get(lockKey),
61
+ ]);
62
+
63
+ if (val === "S" || val === "1" || sent !== null) {
64
+ return "completed";
65
+ }
66
+
67
+ if (val === "L" || lockVal !== null) {
68
+ return "locked";
69
+ }
70
+
71
+ // Key may have expired between initial SET NX and GETs; retry once
72
+ const retryAcquired = await this.redis.set(mainKey, "L", "EX", lockTtlSeconds, "NX");
73
+ if (retryAcquired === "OK") return "acquired";
74
+
75
+ return "locked";
76
+ }
77
+
29
78
  /** Atomically mark id as processed. Returns true on first call; false if already seen. */
30
79
  async checkAndMark(id: string, customTtlSeconds?: number): Promise<boolean> {
31
80
  const ttl = customTtlSeconds ?? this.ttlSeconds;
@@ -33,18 +82,23 @@ export class IdempotencyGuard {
33
82
  return result === "OK";
34
83
  }
35
84
 
36
- /** Unconditionally mark id as processed (e.g. to upgrade a short-lived lock). */
85
+ /** Unconditionally mark id as processed (upgrades lock to completed "1" with full TTL). */
37
86
  async markProcessed(id: string, customTtlSeconds?: number): Promise<void> {
38
87
  const ttl = customTtlSeconds ?? this.ttlSeconds;
39
88
  await this.redis.set(this.key(id), "1", "EX", ttl);
40
89
  }
41
90
 
42
91
  async isProcessed(id: string): Promise<boolean> {
43
- return (await this.redis.get(this.key(id))) !== null;
92
+ const val = await this.redis.get(this.key(id));
93
+ return val !== null && val !== undefined;
44
94
  }
45
95
 
46
96
  /** Remove the idempotency marker (useful in tests or manual rollbacks). */
47
97
  async unmark(id: string): Promise<void> {
48
- await this.redis.del(this.key(id));
98
+ await Promise.all([
99
+ this.redis.del(this.key(id)),
100
+ this.redis.del(this.lockKey(id)),
101
+ this.redis.del(this.completedKey(id)),
102
+ ]);
49
103
  }
50
104
  }
@@ -46,15 +46,3 @@ export function createLogger({ name, level = "info", pretty, context }: LoggerOp
46
46
 
47
47
  return pino(options);
48
48
  }
49
-
50
- export function withRequestId(logger: Logger, requestId: string): Logger {
51
- return logger.child({ requestId });
52
- }
53
-
54
- export function withContext(logger: Logger, context: Record<string, unknown>): Logger {
55
- return logger.child(context);
56
- }
57
-
58
- export function childLogger(logger: Logger, bindings: Record<string, unknown>): Logger {
59
- return logger.child(bindings);
60
- }