notifkit 0.1.10 → 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 (78) hide show
  1. package/dashboard/dist/assets/{index-7mvXb4bS.js → index-Bsrmo9LE.js} +1 -1
  2. package/dashboard/dist/assets/index-s-SXGIwv.css +1 -0
  3. package/dashboard/dist/index.html +2 -2
  4. package/dist/bootstrap-GRXzgPvW.mjs +89 -0
  5. package/dist/bulk-BvRfz6Of.mjs +77 -0
  6. package/dist/index.d.mts +1100 -8883
  7. package/dist/index.mjs +2 -38
  8. package/dist/{main-C9s-2ArH.mjs → main-BENOY7HG.mjs} +49 -75
  9. package/dist/{main-BiFiOX9D.mjs → main-BpnuFX62.mjs} +119 -112
  10. package/dist/{main-Dz2sUh3c.mjs → main-BqVH2BHj.mjs} +57 -54
  11. package/dist/{main-BgmT1MPe.mjs → main-C_5VFIwp.mjs} +135 -79
  12. package/dist/main-D6od9N8h.mjs +312 -0
  13. package/dist/main-D84OX0ux.mjs +811 -0
  14. package/dist/{main-39DtKrX4.mjs → main-HH-WwsMM.mjs} +395 -184
  15. package/dist/{main-B3iQrl3t.mjs → main-HY_TDs61.mjs} +149 -134
  16. package/dist/{src-QiTu5e_a.mjs → src-CI7FGf3h.mjs} +1220 -651
  17. package/drizzle/0006_message_logs_redundant_indexes.sql +7 -0
  18. package/drizzle/0007_workflow_status_canceled.sql +3 -0
  19. package/drizzle/meta/_journal.json +14 -0
  20. package/package.json +17 -17
  21. package/src/client.ts +15 -5
  22. package/src/config/index.ts +26 -3
  23. package/src/contracts/events/notification-ai-pending.ts +12 -0
  24. package/src/contracts/events/notification-delivered.ts +7 -0
  25. package/src/contracts/events/notification-dispatched.ts +21 -2
  26. package/src/contracts/events/notification-enriched.ts +16 -0
  27. package/src/contracts/events/notification-failed.ts +2 -0
  28. package/src/contracts/events/notification-requested.ts +2 -0
  29. package/src/contracts/registry.ts +2 -2
  30. package/src/contracts/sdk.ts +77 -29
  31. package/src/db/bulk.ts +132 -0
  32. package/src/db/index.ts +2 -2
  33. package/src/db/schema.ts +3 -50
  34. package/src/idempotency/index.ts +60 -6
  35. package/src/logger/index.ts +0 -12
  36. package/src/queue/index.ts +255 -76
  37. package/src/rate-limiter/index.ts +217 -16
  38. package/src/redis/index.ts +265 -0
  39. package/src/repositories/index.ts +171 -82
  40. package/src/server.ts +61 -41
  41. package/src/services/ai/main.ts +83 -70
  42. package/src/services/api/admin-static.ts +41 -12
  43. package/src/services/api/handlers.ts +357 -159
  44. package/src/services/api/http.ts +34 -7
  45. package/src/services/api/main.ts +104 -67
  46. package/src/services/api/router.ts +18 -5
  47. package/src/services/delivery/main.ts +495 -230
  48. package/src/services/delivery/throttle.ts +42 -59
  49. package/src/services/engine/main.ts +206 -152
  50. package/src/services/enricher/main.ts +155 -107
  51. package/src/services/events/main.ts +100 -55
  52. package/src/services/scheduler/main.ts +151 -79
  53. package/src/services/scheduler/send-gate.ts +117 -0
  54. package/src/services/workflow/main.ts +220 -100
  55. package/src/shared/batch-processor.ts +65 -34
  56. package/src/shared/dataloader.ts +58 -22
  57. package/src/shared/events.ts +3 -0
  58. package/src/shared/index.ts +0 -1
  59. package/src/shared/utils.ts +2 -47
  60. package/src/templates/index.ts +0 -67
  61. package/src/transport/index.ts +6 -0
  62. package/src/workers/bootstrap.ts +124 -0
  63. package/src/workers/index.ts +99 -24
  64. package/src/workflows/sdk.ts +2 -0
  65. package/dashboard/dist/assets/index-o7L-f3-c.css +0 -1
  66. package/dist/index.d.mts.map +0 -1
  67. package/dist/index.mjs.map +0 -1
  68. package/dist/main-39DtKrX4.mjs.map +0 -1
  69. package/dist/main-B3iQrl3t.mjs.map +0 -1
  70. package/dist/main-BM8DH3v3.mjs +0 -632
  71. package/dist/main-BM8DH3v3.mjs.map +0 -1
  72. package/dist/main-BgmT1MPe.mjs.map +0 -1
  73. package/dist/main-BiFiOX9D.mjs.map +0 -1
  74. package/dist/main-C9s-2ArH.mjs.map +0 -1
  75. package/dist/main-Cqo3rSb5.mjs +0 -224
  76. package/dist/main-Cqo3rSb5.mjs.map +0 -1
  77. package/dist/main-Dz2sUh3c.mjs.map +0 -1
  78. package/dist/src-QiTu5e_a.mjs.map +0 -1
package/dist/index.mjs CHANGED
@@ -1,38 +1,2 @@
1
- import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
- import { $ as metrics, $t as ENRICHED_STREAMS, A as RedisClient, At as NotificationCreatedPayloadSchema, B as AsyncSemaphore, Bt as NotifyRequestSchema, C as PreferenceRepository, Ct as NotificationDeliveredPayloadSchema, D as UserRepository, Dt as NotificationScheduledPayloadSchema, E as TemplateRepository, Et as RenderedContentSchema, F as generateId, Ft as BatchAddContactsSchema, G as getPriorityBucket, Gt as TriggerWorkflowSchema, H as LUA_RENEW_LOCK, Ht as QuietHoursSchema, I as sleep, It as ContactChannelSchema, J as globalEmitter, Jt as UserContactInputSchema, K as normaliseTarget, Kt as UpdateProjectSchema, L as DataLoader, Lt as CreateWorkflowSchema, M as UserThrottle, Mt as NotificationTargetSchema, N as AppError, Nt as AddContactSchema, O as WorkflowRepository, Ot as NotificationEnrichedPayloadSchema, P as ValidationError, Pt as AddUserSchema, Q as getMetricsRegistry, Qt as CONSUMER_GROUPS, R as CircuitBreaker, Rt as IngestEventSchema, S as ContactRepository, St as NotificationFailedPayloadSchema, T as SegmentRepository, Tt as NotificationDispatchedPayloadSchema, U as LUA_SCHEDULER_CLAIM, Ut as SyncTemplatesSchema, V as LUA_RELEASE_LOCK, Vt as PreferencesSchema, W as LUA_SCHEDULER_POLL, Wt as TemplateSchema, X as StreamConsumer, Xt as WorkflowStepSchema, Y as PendingMessageScanner, Yt as WorkflowNotifyPayloadSchema, Z as StreamProducer, Zt as buildStreamEvent, _ as escapeHeader, _n as parseConfig, a as SuspendExecutionError, an as StreamEventMetadataSchema, at as createDatabase, b as renderWithTemplate, bn as setGlobalConfig, bt as NotificationCanceledPayloadSchema, c as BaseWorker, cn as registry, d as buildUnsubscribeHeaders, dn as NotificationPrioritySchema, en as INBOUND_STREAMS, et as childLogger, f as signUnsubscribeToken, fn as NotificationStatusSchema, g as TemplateCache, gn as loadEnv, h as templateRegistry, hn as getAiConfig, i as workflowRegistry, in as EventEnvelopeSchema, it as IdempotencyGuard, j as ProjectSettingsCache, jt as NotificationRequestedPayloadSchema, k as Redis, kt as RecipientProfileSchema, l as NonRetryableError, ln as EventMetadataSchema, m as renderTemplate, mn as baseConfigSchema, n as NotifkitClient, nn as PUBSUB_CHANNELS, nt as withContext, o as buildStepNotifyPayload, on as StreamEventSchema, ot as runMigrations, p as verifyUnsubscribeToken, pn as AI_DEFAULTS, q as LRUCache, qt as UpdateUserSchema, r as workflow, rn as STREAMS, rt as withRequestId, s as resolveStepTarget, sn as EventRegistry, t as NotifkitServer, tn as OUTBOUND_STREAMS, tt as createLogger, u as startHealthReporter, un as NotificationChannelSchema, v as escapeHtml, vn as readBaseConfig, w as ProjectRepository, wt as DeliveryOptionsSchema, x as AdminUserRepository, xt as NotificationSkippedPayloadSchema, y as interpolate, yn as setAiConfig, yt as NotificationAiPendingPayloadSchema, z as BatchProcessor, zt as InlineUserSchema } from "./src-QiTu5e_a.mjs";
3
- //#region src/transport/index.ts
4
- var transport_exports = /* @__PURE__ */ __exportAll({
5
- registerTransport: () => registerTransport,
6
- transportRegistry: () => transportRegistry
7
- });
8
- var TransportRegistry = class {
9
- transports = /* @__PURE__ */ new Map();
10
- register(transport, priority = 0) {
11
- const list = this.transports.get(transport.channel) || [];
12
- list.push({
13
- transport,
14
- priority
15
- });
16
- list.sort((a, b) => b.priority - a.priority);
17
- this.transports.set(transport.channel, list);
18
- }
19
- get(channel) {
20
- const list = this.transports.get(channel);
21
- return list && list.length > 0 ? list[0]?.transport : void 0;
22
- }
23
- getAll(channel) {
24
- const list = this.transports.get(channel);
25
- return list ? list.map((item) => item.transport) : [];
26
- }
27
- registeredChannels() {
28
- return [...this.transports.keys()];
29
- }
30
- };
31
- const transportRegistry = new TransportRegistry();
32
- function registerTransport(transport, priority = 0) {
33
- transportRegistry.register(transport, priority);
34
- }
35
- //#endregion
36
- export { AI_DEFAULTS, AddContactSchema, AddUserSchema, AdminUserRepository, AppError, AsyncSemaphore, BaseWorker, BatchAddContactsSchema, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, ContactChannelSchema, ContactRepository, CreateWorkflowSchema, DataLoader, DeliveryOptionsSchema, ENRICHED_STREAMS, EventEnvelopeSchema, EventMetadataSchema, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IngestEventSchema, InlineUserSchema, LRUCache, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_CLAIM, LUA_SCHEDULER_POLL, NonRetryableError, NotificationAiPendingPayloadSchema, NotificationCanceledPayloadSchema, NotificationChannelSchema, NotificationCreatedPayloadSchema, NotificationDeliveredPayloadSchema, NotificationDispatchedPayloadSchema, NotificationEnrichedPayloadSchema, NotificationFailedPayloadSchema, NotificationPrioritySchema, NotificationRequestedPayloadSchema, NotificationScheduledPayloadSchema, NotificationSkippedPayloadSchema, NotificationStatusSchema, NotificationTargetSchema, NotifkitClient, NotifkitServer, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, PendingMessageScanner, PreferenceRepository, PreferencesSchema, ProjectRepository, ProjectSettingsCache, QuietHoursSchema, RecipientProfileSchema, Redis, RedisClient, RenderedContentSchema, STREAMS, SegmentRepository, StreamConsumer, StreamEventMetadataSchema, StreamEventSchema, StreamProducer, SuspendExecutionError, SyncTemplatesSchema, TemplateCache, TemplateRepository, TemplateSchema, TriggerWorkflowSchema, UpdateProjectSchema, UpdateUserSchema, UserContactInputSchema, UserRepository, UserThrottle, ValidationError, WorkflowNotifyPayloadSchema, WorkflowRepository, WorkflowStepSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, childLogger, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, normaliseTarget, parseConfig, readBaseConfig, registerTransport, registry, renderTemplate, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transport_exports as t, templateRegistry, transportRegistry, verifyUnsubscribeToken, withContext, withRequestId, workflow, workflowRegistry };
37
-
38
- //# sourceMappingURL=index.mjs.map
1
+ import { $t as EventRegistry, A as LUA_ACQUIRE_LEASE, At as IngestEventSchema, B as ApiRateLimiter, Bt as UserContactInputSchema, C as ContactRepository, Cn as LRUCache, Ct as NotificationRequestedPayloadSchema, D as TemplateRepository, Dt as BatchAddContactsSchema, E as SegmentRepository, Et as AddUserSchema, F as LUA_SCHEDULER_POLL, Ft as SyncTemplatesSchema, G as StreamConsumer, Gt as ENRICHED_STREAMS, H as UserThrottle, Ht as WorkflowStepSchema, I as LUA_USER_THROTTLE, It as TemplateSchema, J as metrics, Jt as PUBSUB_CHANNELS, K as StreamProducer, Kt as INBOUND_STREAMS, L as Redis, Lt as TriggerWorkflowSchema, M as LUA_MARK_PROCESSED, Mt as NotifyRequestSchema, N as LUA_RELEASE_LOCK, Nt as PreferencesSchema, O as UserRepository, Ot as ContactChannelSchema, P as LUA_RENEW_LOCK, Pt as QuietHoursSchema, Q as runMigrations, Qt as StreamEventSchema, R as RedisClient, Rt as UpdateProjectSchema, S as AdminUserRepository, Sn as normaliseTarget, St as NotificationCreatedPayloadSchema, T as ProjectRepository, Tt as AddContactSchema, U as calculateTokenBatchSize, Ut as buildStreamEvent, V as ProjectSettingsCache, Vt as WorkflowNotifyPayloadSchema, W as PendingMessageScanner, Wt as CONSUMER_GROUPS, X as IdempotencyGuard, Xt as EventEnvelopeSchema, Y as createLogger, Yt as STREAMS, Z as createDatabase, Zt as StreamEventMetadataSchema, _ as TemplateCache, _n as DataLoader, _t as NotificationDispatchedPayloadSchema, a as SuspendExecutionError, an as AI_DEFAULTS, b as interpolate, bn as AsyncSemaphore, bt as NotificationEnrichedPayloadSchema, c as BaseWorker, cn as loadEnv, d as startHealthReporter, dn as setAiConfig, dt as NotificationAiPendingPayloadSchema, en as registry, f as buildUnsubscribeHeaders, fn as setGlobalConfig, ft as NotificationCanceledPayloadSchema, g as transportRegistry, gn as sleep, gt as DeliveryOptionsSchema, h as registerTransport, hn as generateId, ht as NotificationDeliveredPayloadSchema, i as workflowRegistry, in as NotificationStatusSchema, j as LUA_LEASE_API_RATE_LIMIT, jt as InlineUserSchema, k as WorkflowRepository, kt as CreateWorkflowSchema, l as LockHeldError, ln as parseConfig, m as verifyUnsubscribeToken, mn as ValidationError, mt as NotificationFailedPayloadSchema, n as NotifkitClient, nn as NotificationChannelSchema, o as buildStepNotifyPayload, on as baseConfigSchema, p as signUnsubscribeToken, pn as AppError, pt as NotificationSkippedPayloadSchema, q as getMetricsRegistry, qt as OUTBOUND_STREAMS, r as workflow, rn as NotificationPrioritySchema, s as resolveStepTarget, sn as getAiConfig, t as NotifkitServer, tn as EventMetadataSchema, u as NonRetryableError, un as readBaseConfig, v as escapeHeader, vn as CircuitBreaker, vt as RenderedContentSchema, w as PreferenceRepository, wn as globalEmitter, wt as NotificationTargetSchema, x as renderWithTemplate, xn as getPriorityBucket, xt as RecipientProfileSchema, y as escapeHtml, yn as BatchProcessor, yt as NotificationScheduledPayloadSchema, z as registerCustomCommands, zt as UpdateUserSchema } from "./src-CI7FGf3h.mjs";
2
+ export { AI_DEFAULTS, AddContactSchema, AddUserSchema, AdminUserRepository, ApiRateLimiter, AppError, AsyncSemaphore, BaseWorker, BatchAddContactsSchema, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, ContactChannelSchema, ContactRepository, CreateWorkflowSchema, DataLoader, DeliveryOptionsSchema, ENRICHED_STREAMS, EventEnvelopeSchema, EventMetadataSchema, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IngestEventSchema, InlineUserSchema, LRUCache, LUA_ACQUIRE_LEASE, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LockHeldError, NonRetryableError, NotificationAiPendingPayloadSchema, NotificationCanceledPayloadSchema, NotificationChannelSchema, NotificationCreatedPayloadSchema, NotificationDeliveredPayloadSchema, NotificationDispatchedPayloadSchema, NotificationEnrichedPayloadSchema, NotificationFailedPayloadSchema, NotificationPrioritySchema, NotificationRequestedPayloadSchema, NotificationScheduledPayloadSchema, NotificationSkippedPayloadSchema, NotificationStatusSchema, NotificationTargetSchema, NotifkitClient, NotifkitServer, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, PendingMessageScanner, PreferenceRepository, PreferencesSchema, ProjectRepository, ProjectSettingsCache, QuietHoursSchema, RecipientProfileSchema, Redis, RedisClient, RenderedContentSchema, STREAMS, SegmentRepository, StreamConsumer, StreamEventMetadataSchema, StreamEventSchema, StreamProducer, SuspendExecutionError, SyncTemplatesSchema, TemplateCache, TemplateRepository, TemplateSchema, TriggerWorkflowSchema, UpdateProjectSchema, UpdateUserSchema, UserContactInputSchema, UserRepository, UserThrottle, ValidationError, WorkflowNotifyPayloadSchema, WorkflowRepository, WorkflowStepSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, normaliseTarget, parseConfig, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
@@ -1,5 +1,5 @@
1
- import { A as RedisClient, E as TemplateRepository, G as getPriorityBucket, J as globalEmitter, Qt as CONSUMER_GROUPS, X as StreamConsumer, Y as PendingMessageScanner, Z as StreamProducer, Zt as buildStreamEvent, at as createDatabase, b as renderWithTemplate, c as BaseWorker, cn as registry, dt as scheduledPayloads, g as TemplateCache, gn as loadEnv, hn as getAiConfig, it as IdempotencyGuard, pn as AI_DEFAULTS, rn as STREAMS, tt as createLogger, u as startHealthReporter, vn as readBaseConfig } from "./src-QiTu5e_a.mjs";
2
- import { randomUUID } from "node:crypto";
1
+ import { D as TemplateRepository, K as StreamProducer, Ut as buildStreamEvent, Wt as CONSUMER_GROUPS, X as IdempotencyGuard, Yt as STREAMS, _ as TemplateCache, an as AI_DEFAULTS, c as BaseWorker, cn as loadEnv, d as startHealthReporter, en as registry, rt as scheduledPayloads, sn as getAiConfig, un as readBaseConfig, wn as globalEmitter, x as renderWithTemplate, xn as getPriorityBucket } from "./src-CI7FGf3h.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-GRXzgPvW.mjs";
3
3
  import { generateText } from "ai";
4
4
  //#region src/services/ai/main.ts
5
5
  loadEnv();
@@ -67,17 +67,28 @@ var AiWorker = class extends BaseWorker {
67
67
  return;
68
68
  }
69
69
  const pending = payloadResult.data;
70
- const idempotencyKey = `${pending.enrichedEventId}:${pending.recipientId}:${pending.channel}:ai`;
71
- if (!await this.idempotency.checkAndMark(idempotencyKey)) {
70
+ const taskId = pending.taskId ?? `${pending.enrichedEventId}:${pending.recipientId}:${pending.channel}`;
71
+ const idempotencyKey = `${taskId}:ai`;
72
+ const aiConfig = getAiConfig();
73
+ const maxPrompts = aiConfig.maxPromptsPerNotification ?? AI_DEFAULTS.maxPromptsPerNotification;
74
+ const leaseSeconds = Math.ceil((aiConfig.timeoutMs ?? AI_DEFAULTS.timeoutMs) * maxPrompts / 1e3) + 30;
75
+ const lease = this.idempotency.acquireLease ? await this.idempotency.acquireLease(idempotencyKey, leaseSeconds) : await this.idempotency.checkAndMark(idempotencyKey, leaseSeconds) ? "acquired" : "locked";
76
+ if (lease === "completed") {
72
77
  this.logger.debug({
73
78
  messageId: message.id,
74
79
  eventId: event.id
75
- }, "duplicate ai task — skipping");
80
+ }, "ai task already completed — acking message");
76
81
  return;
77
82
  }
83
+ if (lease === "locked") {
84
+ this.logger.debug({
85
+ messageId: message.id,
86
+ eventId: event.id
87
+ }, "duplicate ai task — lock held by another worker, leaving pending in stream");
88
+ return { ack: false };
89
+ }
78
90
  try {
79
91
  const promptEntries = Object.entries(pending.aiPrompts);
80
- const maxPrompts = getAiConfig().maxPromptsPerNotification ?? AI_DEFAULTS.maxPromptsPerNotification;
81
92
  if (promptEntries.length > maxPrompts) this.logger.warn({
82
93
  messageId: message.id,
83
94
  requested: promptEntries.length,
@@ -91,8 +102,7 @@ var AiWorker = class extends BaseWorker {
91
102
  };
92
103
  const dbTemplate = pending.templateId ? await this.templateCache.getCachedTemplate(pending.projectId, pending.templateId) : null;
93
104
  const rendered = renderWithTemplate(dbTemplate, finalVars);
94
- const taskId = randomUUID();
95
- const resolvedDestination = (pending.channel === "email" ? pending.recipient.email : pending.channel === "sms" || pending.channel === "whatsapp" ? pending.recipient.phone : pending.channel === "webhook" ? pending.recipient.webhook : pending.channel === "telegram" ? pending.recipient.telegram : pending.channel === "discord" ? pending.recipient.discord : pending.channel === "slack" ? pending.recipient.slack : pending.channel === "push" ? pending.recipient.pushTokens?.[0] ?? pending.recipient.pushToken : void 0) ?? (pending.channel === "push" ? void 0 : pending.recipientId);
105
+ const resolvedDestination = pending.destination ?? (pending.channel === "email" ? pending.recipient.email : pending.channel === "sms" || pending.channel === "whatsapp" ? pending.recipient.phone : pending.channel === "webhook" ? pending.recipient.webhook : pending.channel === "telegram" ? pending.recipient.telegram : pending.channel === "discord" ? pending.recipient.discord : pending.channel === "slack" ? pending.recipient.slack : pending.channel === "push" ? pending.recipient.pushTokens?.[0] ?? pending.recipient.pushToken : void 0) ?? (pending.channel === "push" ? void 0 : pending.recipientId);
96
106
  const taskPayload = {
97
107
  projectId: pending.projectId,
98
108
  taskId,
@@ -108,17 +118,23 @@ var AiWorker = class extends BaseWorker {
108
118
  destination: resolvedDestination,
109
119
  deliveryOptions: {
110
120
  maxAttempts: 3,
111
- timeoutMs: 1e4
121
+ timeoutMs: 1e4,
122
+ ...pending.deliveryHeaders ? { headers: pending.deliveryHeaders } : {}
112
123
  },
113
- fallbackChain: pending.fallbackChain
124
+ fallbackChain: pending.fallbackChain,
125
+ campaignId: pending.campaignId,
126
+ workflowInstanceId: pending.workflowInstanceId
114
127
  };
115
128
  const envelope = buildStreamEvent("notification.dispatched", taskPayload, "ai-worker", event.metadata.traceId);
116
129
  const now = Date.now();
117
130
  if ((pending.scheduledAt ? new Date(pending.scheduledAt).getTime() : now) > now) {
118
131
  await this.db.insert(scheduledPayloads).values({
119
132
  taskId,
120
- payload: taskPayload
121
- });
133
+ payload: {
134
+ ...taskPayload,
135
+ scheduledAt: pending.scheduledAt
136
+ }
137
+ }).onConflictDoNothing();
122
138
  const scheduledEnvelope = buildStreamEvent("notification.scheduled", {
123
139
  projectId: pending.projectId,
124
140
  enrichedEventId: pending.enrichedEventId,
@@ -142,6 +158,7 @@ var AiWorker = class extends BaseWorker {
142
158
  traceId: event.metadata.traceId
143
159
  }, "task dispatched after AI generation");
144
160
  }
161
+ await this.idempotency.markProcessed?.(idempotencyKey);
145
162
  } catch (err) {
146
163
  await this.idempotency.unmark(idempotencyKey);
147
164
  if (err instanceof PermanentAiError || err?.name === "PermanentAiError") {
@@ -158,57 +175,19 @@ var AiWorker = class extends BaseWorker {
158
175
  }
159
176
  };
160
177
  async function startAiWorker() {
161
- logger = createLogger({
162
- name: "ai",
163
- level: config.LOG_LEVEL
164
- });
165
- redis = new RedisClient({
166
- url: config.REDIS_URL,
167
- name: "ai",
168
- logger
169
- });
170
- const dbData = createDatabase({
171
- url: config.DATABASE_URL,
172
- applicationName: "ai",
173
- logger
174
- });
175
- sql = dbData.sql;
176
- db = dbData.db;
178
+ ({logger, redis, sql, db} = createWorkerRuntime(config, "ai"));
177
179
  templateRepo = new TemplateRepository(db);
178
180
  templateCache = new TemplateCache(templateRepo);
179
- consumer = new StreamConsumer({
180
- redis: redis.native,
181
- stream: STREAMS.AI_PENDING,
182
- group: CONSUMER_GROUPS.AI,
183
- consumer: `ai-${process.pid}`,
184
- dlqStream: STREAMS.DEAD_LETTER,
185
- batchSize: config.WORKER_CONCURRENCY,
181
+ ({consumer, pendingScanner} = createStreamConsumer({
182
+ redis,
186
183
  logger
187
- });
188
- pendingScanner = new PendingMessageScanner({
189
- redis: redis.native,
184
+ }, {
190
185
  stream: STREAMS.AI_PENDING,
191
186
  group: CONSUMER_GROUPS.AI,
192
- consumer: `ai-${process.pid}`,
193
- logger
194
- });
195
- const outboundProducers = {
196
- critical: new StreamProducer({
197
- redis: redis.native,
198
- stream: STREAMS.OUTBOUND_CRITICAL,
199
- logger
200
- }),
201
- normal: new StreamProducer({
202
- redis: redis.native,
203
- stream: STREAMS.OUTBOUND_NORMAL,
204
- logger
205
- }),
206
- low: new StreamProducer({
207
- redis: redis.native,
208
- stream: STREAMS.OUTBOUND_LOW,
209
- logger
210
- })
211
- };
187
+ consumer: uniqueConsumerId("ai"),
188
+ batchSize: config.WORKER_CONCURRENCY
189
+ }));
190
+ const outboundProducers = createPriorityProducers(redis.native, logger, "OUTBOUND");
212
191
  const scheduledProducer = new StreamProducer({
213
192
  redis: redis.native,
214
193
  stream: STREAMS.SCHEDULED,
@@ -217,7 +196,7 @@ async function startAiWorker() {
217
196
  const idempotency = new IdempotencyGuard({
218
197
  redis: redis.native,
219
198
  keyPrefix: "notif:processed:ai",
220
- ttlSeconds: 86400
199
+ ttlSeconds: config.IDEMPOTENCY_TTL_SECONDS
221
200
  });
222
201
  async function generateAiContent(prompt, vars) {
223
202
  const aiConfig = getAiConfig();
@@ -258,7 +237,7 @@ async function startAiWorker() {
258
237
  outboundProducers,
259
238
  db
260
239
  });
261
- subscriber = redis.native.duplicate();
240
+ subscriber = redis.native.duplicate({ enableAutoPipelining: false });
262
241
  await subscriber.subscribe("template.invalidated");
263
242
  subscriber.on("message", (channel, message) => {
264
243
  if (channel === "template.invalidated") {
@@ -274,21 +253,16 @@ async function startAiWorker() {
274
253
  await worker.start();
275
254
  }
276
255
  async function stopAiWorker() {
277
- logger?.info("shutdown initiated");
278
- if (healthInterval) {
279
- clearInterval(healthInterval);
280
- healthInterval = null;
281
- }
282
- if (subscriber) {
283
- subscriber.disconnect();
284
- subscriber = null;
285
- }
286
- if (worker) await worker.stop();
287
- if (sql) await sql.end();
288
- if (redis) await redis.disconnect();
289
- logger?.info("ai stopped");
256
+ await shutdownWorker("ai", {
257
+ logger,
258
+ timers: [healthInterval],
259
+ subscriber,
260
+ worker,
261
+ sql,
262
+ redis
263
+ });
264
+ healthInterval = null;
265
+ subscriber = null;
290
266
  }
291
267
  //#endregion
292
268
  export { startAiWorker, stopAiWorker };
293
-
294
- //# sourceMappingURL=main-C9s-2ArH.mjs.map