notifkit 0.1.0 → 0.1.2

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.
@@ -1 +1 @@
1
- {"version":3,"file":"main-Dztc2dqR.mjs","names":[],"sources":["../src/services/ai/main.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { loadEnv, readBaseConfig } from \"@/index.js\";\nimport { createLogger } from \"@/index.js\";\nimport { RedisClient, type Redis } from \"@/index.js\";\nimport {\n StreamConsumer,\n PendingMessageScanner,\n StreamProducer,\n type StreamMessage,\n} from \"@/index.js\";\nimport { BaseWorker } from \"@/index.js\";\nimport {\n STREAMS,\n CONSUMER_GROUPS,\n registry,\n buildStreamEvent,\n type NotificationAiPendingPayload,\n type NotificationDispatchedPayload,\n getAiConfig,\n AI_DEFAULTS,\n} from \"@/index.js\";\nimport { generateText } from \"ai\";\nimport { type StreamName } from \"@/contracts/streams.js\";\nimport { IdempotencyGuard } from \"@/index.js\";\nimport { TemplateRepository } from \"@/index.js\";\nimport { createDatabase } from \"@/db/index.js\";\nimport { scheduledPayloads } from \"@/db/schema.js\";\nimport { getPriorityBucket, globalEmitter, type WorkerOptions } from \"@/shared/index.js\";\nimport { renderWithTemplate, TemplateCache } from \"@/templates/index.js\";\nimport { startHealthReporter } from \"@/workers/index.js\";\n\n// ─── Bootstrap ─────────────────────────────────────────────────────────────\n\nloadEnv();\nconst config = readBaseConfig();\n\nlet logger: ReturnType<typeof createLogger>;\nlet redis: RedisClient;\nlet sql: any;\nlet db: any;\nlet templateRepo: TemplateRepository;\n\nlet templateCache: TemplateCache;\n\nlet consumer: StreamConsumer;\nlet pendingScanner: PendingMessageScanner;\nlet worker: BaseWorker;\nlet healthInterval: NodeJS.Timeout | null = null;\nlet subscriber: any = null;\n\n/**\n * A model failure that retrying cannot fix (bad prompt, rejected request,\n * unsupported model). Thrown so the notification fails once instead of being\n * re-billed on every retry.\n */\nexport class PermanentAiError extends Error {\n constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"PermanentAiError\";\n }\n}\n\n/** Timeouts, rate limits and 5xx are worth another attempt; 4xx are not. */\nexport function isRetryableAiError(err: unknown): boolean {\n if (err instanceof PermanentAiError) return false;\n\n const e = err as { name?: string; statusCode?: number; status?: number } | null;\n if (!e) return false;\n\n if (e.name === \"TimeoutError\" || e.name === \"AbortError\") return true;\n\n const status = e.statusCode ?? e.status;\n if (typeof status === \"number\") {\n return status === 408 || status === 409 || status === 429 || status >= 500;\n }\n\n // Unclassifiable (network errors, transport failures) — assume transient.\n return true;\n}\n\nexport interface AiWorkerOptions extends WorkerOptions {\n registry: any;\n idempotency: any;\n redis: Redis;\n generateAiContent: any;\n templateCache: TemplateCache;\n scheduledProducer: any;\n outboundProducers: any;\n db: any;\n}\n\nexport class AiWorker extends BaseWorker {\n private readonly registry: any;\n private readonly idempotency: any;\n private readonly redisCli: Redis;\n private readonly generateAiContent: any;\n private readonly templateCache: TemplateCache;\n private readonly scheduledProducer: any;\n private readonly outboundProducers: any;\n private readonly db: any;\n\n constructor(options: AiWorkerOptions) {\n super(options);\n this.registry = options.registry;\n this.idempotency = options.idempotency;\n this.redisCli = options.redis;\n this.generateAiContent = options.generateAiContent;\n this.templateCache = options.templateCache;\n this.scheduledProducer = options.scheduledProducer;\n this.outboundProducers = options.outboundProducers;\n this.db = options.db;\n }\n async process(message: StreamMessage): Promise<void> {\n const { event } = message;\n\n const payloadResult = this.registry.safeParsePayload(\"notification.ai_pending\", event.payload);\n if (!payloadResult.success) {\n this.logger.warn(\n { messageId: message.id, issues: payloadResult.error.issues },\n \"invalid notification.ai_pending payload — skipping\",\n );\n return;\n }\n\n const pending = payloadResult.data as NotificationAiPendingPayload;\n\n // Idempotency\n const idempotencyKey = `${pending.enrichedEventId}:${pending.recipientId}:${pending.channel}:ai`;\n if (!(await this.idempotency.checkAndMark(idempotencyKey))) {\n this.logger.debug(\n { messageId: message.id, eventId: event.id },\n \"duplicate ai task — skipping\",\n );\n return;\n }\n try {\n // Execute AI prompts. Each key is a separate billed model call, so the\n // count is capped rather than being driven by whatever the caller sent.\n const promptEntries = Object.entries(pending.aiPrompts);\n const maxPrompts =\n getAiConfig().maxPromptsPerNotification ?? AI_DEFAULTS.maxPromptsPerNotification;\n if (promptEntries.length > maxPrompts) {\n this.logger.warn(\n { messageId: message.id, requested: promptEntries.length, maxPrompts },\n \"aiPrompts exceeds the per-notification cap — extra prompts ignored\",\n );\n }\n\n const generatedVars: Record<string, string> = {};\n for (const [key, prompt] of promptEntries.slice(0, maxPrompts)) {\n generatedVars[key] = await this.generateAiContent(prompt, pending.templateVariables);\n }\n\n // Merge generated vars with original template vars\n const finalVars = { ...pending.templateVariables, ...generatedVars };\n\n const dbTemplate = pending.templateId\n ? await this.templateCache.getCachedTemplate(pending.projectId, pending.templateId)\n : null;\n\n const rendered = renderWithTemplate(dbTemplate, finalVars);\n\n const taskId = randomUUID();\n const destination =\n pending.channel === \"email\"\n ? pending.recipient.email\n : pending.channel === \"sms\"\n ? pending.recipient.phone\n : pending.channel === \"webhook\"\n ? pending.recipient.webhook\n : pending.channel === \"push\"\n ? (pending.recipient.pushTokens?.[0] ?? pending.recipient.pushToken)\n : undefined;\n const resolvedDestination =\n destination ?? (pending.channel === \"push\" ? undefined : pending.recipientId);\n\n const taskPayload: NotificationDispatchedPayload = {\n projectId: pending.projectId,\n taskId,\n enrichedEventId: pending.enrichedEventId,\n recipientId: pending.recipientId,\n channel: pending.channel,\n priority: pending.priority,\n templateId: pending.templateId,\n templateVariables: pending.templateVariables,\n aiPrompts: pending.aiPrompts,\n recipient: pending.recipient,\n renderedContent: rendered,\n destination: resolvedDestination,\n deliveryOptions: {\n maxAttempts: 3,\n timeoutMs: 10_000,\n },\n fallbackChain: pending.fallbackChain,\n };\n\n const envelope = buildStreamEvent(\n \"notification.dispatched\",\n taskPayload as Record<string, unknown>,\n \"ai-worker\",\n event.metadata.traceId,\n );\n\n // Route by scheduledAt\n const now = Date.now();\n const scheduledAt = pending.scheduledAt ? new Date(pending.scheduledAt).getTime() : now;\n\n if (scheduledAt > now) {\n await this.db.insert(scheduledPayloads).values({\n taskId,\n payload: taskPayload,\n });\n\n const scheduledEnvelope = buildStreamEvent(\n \"notification.scheduled\",\n {\n projectId: pending.projectId,\n enrichedEventId: pending.enrichedEventId,\n taskId,\n scheduledAt: pending.scheduledAt!,\n },\n \"ai-worker\",\n event.metadata.traceId,\n );\n\n await this.scheduledProducer.publish(scheduledEnvelope);\n this.logger.info(\n {\n messageId: message.id,\n taskId,\n scheduledAt: pending.scheduledAt,\n traceId: event.metadata.traceId,\n },\n \"task scheduled and payload cached after AI generation\",\n );\n } else {\n const p = getPriorityBucket(pending.priority);\n const outboundProducer = this.outboundProducers[p] ?? this.outboundProducers[\"normal\"]!;\n\n await outboundProducer.publish(envelope);\n this.logger.info(\n {\n messageId: message.id,\n taskId,\n recipientId: pending.recipientId,\n traceId: event.metadata.traceId,\n },\n \"task dispatched after AI generation\",\n );\n }\n } catch (err) {\n await this.idempotency.unmark(idempotencyKey);\n if (err instanceof PermanentAiError || (err as Error)?.name === \"PermanentAiError\") {\n // Retrying re-bills the same failing prompt. Fail the notification once.\n this.logger.error(\n { err, messageId: message.id, recipientId: pending.recipientId },\n \"AI generation failed permanently — dropping notification without retry\",\n );\n globalEmitter.emit(\n \"notification:failed\",\n pending.enrichedEventId,\n (err as Error).message,\n pending.channel,\n );\n return;\n }\n throw err;\n }\n }\n}\n\nexport async function startAiWorker() {\n logger = createLogger({ name: \"ai\", level: config.LOG_LEVEL });\n redis = new RedisClient({ url: config.REDIS_URL, name: \"ai\", logger });\n const dbData = createDatabase({ url: config.DATABASE_URL, applicationName: \"ai\", logger });\n sql = dbData.sql;\n db = dbData.db;\n templateRepo = new TemplateRepository(db);\n templateCache = new TemplateCache(templateRepo);\n consumer = new StreamConsumer({\n redis: redis.native,\n stream: STREAMS.AI_PENDING as StreamName,\n group: CONSUMER_GROUPS.AI,\n consumer: `ai-${process.pid}`,\n dlqStream: STREAMS.DEAD_LETTER,\n batchSize: config.WORKER_CONCURRENCY,\n logger,\n });\n\n pendingScanner = new PendingMessageScanner({\n redis: redis.native,\n stream: STREAMS.AI_PENDING as StreamName,\n group: CONSUMER_GROUPS.AI,\n consumer: `ai-${process.pid}`,\n logger,\n });\n\n const outboundProducers = {\n critical: new StreamProducer({\n redis: redis.native,\n stream: STREAMS.OUTBOUND_CRITICAL,\n logger,\n }),\n normal: new StreamProducer({ redis: redis.native, stream: STREAMS.OUTBOUND_NORMAL, logger }),\n low: new StreamProducer({ redis: redis.native, stream: STREAMS.OUTBOUND_LOW, logger }),\n };\n\n const scheduledProducer = new StreamProducer({\n redis: redis.native,\n stream: STREAMS.SCHEDULED,\n logger,\n });\n\n const idempotency = new IdempotencyGuard({\n redis: redis.native,\n keyPrefix: \"notif:processed:ai\",\n ttlSeconds: 86_400,\n });\n\n // AI generation\n async function generateAiContent(prompt: string, vars: Record<string, unknown>): Promise<string> {\n const aiConfig = getAiConfig();\n const interpolated = prompt.replace(/\\{\\{(\\w+)\\}\\}/g, (_, k: string) => String(vars[k] ?? \"\"));\n\n if (!aiConfig || !aiConfig.aiModel) {\n logger.warn(\"AI worker called but no AI model was provided to NotifkitServer\");\n return `[AI Disabled] ${interpolated}`;\n }\n\n const maxOutputTokens = aiConfig.maxOutputTokens ?? AI_DEFAULTS.maxOutputTokens;\n const timeoutMs = aiConfig.timeoutMs ?? AI_DEFAULTS.timeoutMs;\n\n try {\n const { text } = await generateText({\n model: aiConfig.aiModel,\n prompt: interpolated,\n maxOutputTokens,\n abortSignal: AbortSignal.timeout(timeoutMs),\n });\n\n return text;\n } catch (err) {\n // BaseWorker retries a throw up to maxRetriesBeforeDlq, and every retry is\n // another billed call. Only re-throw for failures a retry could actually\n // fix; a malformed prompt or a rejected request must not be re-billed.\n if (isRetryableAiError(err)) {\n logger.error({ err }, \"AI generation failed (transient) — will retry\");\n throw err;\n }\n throw new PermanentAiError(err instanceof Error ? err.message : String(err), { cause: err });\n }\n }\n\n worker = new AiWorker({\n consumer,\n pendingScanner,\n logger,\n concurrency: config.WORKER_CONCURRENCY,\n registry,\n idempotency,\n redis: redis.native,\n generateAiContent,\n templateCache,\n scheduledProducer,\n outboundProducers,\n db,\n });\n\n subscriber = redis.native.duplicate();\n await subscriber.subscribe(\"template.invalidated\");\n subscriber.on(\"message\", (channel: string, message: string) => {\n if (channel === \"template.invalidated\") {\n templateCache.invalidateKey(message);\n logger.info({ cacheKey: message }, \"invalidated template cache\");\n }\n });\n\n healthInterval = startHealthReporter(\"ai\", worker, redis, logger);\n\n logger.info({ env: config.NODE_ENV, redis: config.REDIS_URL }, \"ai starting\");\n await worker.start();\n}\n\nexport async function stopAiWorker(): Promise<void> {\n logger?.info(\"shutdown initiated\");\n if (healthInterval) {\n clearInterval(healthInterval);\n healthInterval = null;\n }\n if (subscriber) {\n subscriber.disconnect();\n subscriber = null;\n }\n if (worker) await worker.stop();\n if (sql) await sql.end();\n if (redis) await redis.disconnect();\n logger?.info(\"ai stopped\");\n}\n"],"mappings":";;;;AAiCA,QAAQ;AACR,MAAM,SAAS,eAAe;AAE9B,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AAEJ,IAAI;AAEJ,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI,iBAAwC;AAC5C,IAAI,aAAkB;;;;;;AAOtB,IAAa,mBAAb,cAAsC,MAAM;CAC1C,YAAY,SAAiB,SAAwB;EACnD,MAAM,SAAS,OAAO;EACtB,KAAK,OAAO;CACd;AACF;;AAGA,SAAgB,mBAAmB,KAAuB;CACxD,IAAI,eAAe,kBAAkB,OAAO;CAE5C,MAAM,IAAI;CACV,IAAI,CAAC,GAAG,OAAO;CAEf,IAAI,EAAE,SAAS,kBAAkB,EAAE,SAAS,cAAc,OAAO;CAEjE,MAAM,SAAS,EAAE,cAAc,EAAE;CACjC,IAAI,OAAO,WAAW,UACpB,OAAO,WAAW,OAAO,WAAW,OAAO,W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U;CACpC,MAAM,WAAW,UAAU,sBAAsB;CACjD,WAAW,GAAG,YAAY,SAAiB,YAAoB;EAC7D,IAAI,YAAY,wBAAwB;GACtC,cAAc,cAAc,OAAO;GACnC,OAAO,KAAK,EAAE,UAAU,QAAQ,GAAG,4BAA4B;EACjE;CACF,CAAC;CAED,iBAAiB,oBAAoB,MAAM,QAAQ,OAAO,MAAM;CAEhE,OAAO,KAAK;EAAE,KAAK,OAAO;EAAU,OAAO,OAAO;CAAU,GAAG,aAAa;CAC5E,MAAM,OAAO,MAAM;AACrB;AAEA,eAAsB,eAA8B;CAClD,QAAQ,KAAK,oBAAoB;CACjC,IAAI,gBAAgB;EAClB,cAAc,cAAc;EAC5B,iBAAiB;CACnB;CACA,IAAI,YAAY;EACd,WAAW,WAAW;EACtB,aAAa;CACf;CACA,IAAI,QAAQ,MAAM,OAAO,KAAK;CAC9B,IAAI,KAAK,MAAM,IAAI,IAAI;CACvB,IAAI,OAAO,MAAM,MAAM,WAAW;CAClC,QAAQ,KAAK,YAAY;AAC3B"}
1
+ {"version":3,"file":"main-CAH0_Q6d.mjs","names":[],"sources":["../src/services/ai/main.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { loadEnv, readBaseConfig } from \"@/index.js\";\nimport { createLogger } from \"@/index.js\";\nimport { RedisClient, type Redis } from \"@/index.js\";\nimport {\n StreamConsumer,\n PendingMessageScanner,\n StreamProducer,\n type StreamMessage,\n} from \"@/index.js\";\nimport { BaseWorker } from \"@/index.js\";\nimport {\n STREAMS,\n CONSUMER_GROUPS,\n registry,\n buildStreamEvent,\n type NotificationAiPendingPayload,\n type NotificationDispatchedPayload,\n getAiConfig,\n AI_DEFAULTS,\n} from \"@/index.js\";\nimport { generateText } from \"ai\";\nimport { type StreamName } from \"@/contracts/streams.js\";\nimport { IdempotencyGuard } from \"@/index.js\";\nimport { TemplateRepository } from \"@/index.js\";\nimport { createDatabase } from \"@/db/index.js\";\nimport { scheduledPayloads } from \"@/db/schema.js\";\nimport { getPriorityBucket, globalEmitter, type WorkerOptions } from \"@/shared/index.js\";\nimport { renderWithTemplate, TemplateCache } from \"@/templates/index.js\";\nimport { startHealthReporter } from \"@/workers/index.js\";\n\n// ─── Bootstrap ─────────────────────────────────────────────────────────────\n\nloadEnv();\nconst config = readBaseConfig();\n\nlet logger: ReturnType<typeof createLogger>;\nlet redis: RedisClient;\nlet sql: any;\nlet db: any;\nlet templateRepo: TemplateRepository;\n\nlet templateCache: TemplateCache;\n\nlet consumer: StreamConsumer;\nlet pendingScanner: PendingMessageScanner;\nlet worker: BaseWorker;\nlet healthInterval: NodeJS.Timeout | null = null;\nlet subscriber: any = null;\n\n/**\n * A model failure that retrying cannot fix (bad prompt, rejected request,\n * unsupported model). Thrown so the notification fails once instead of being\n * re-billed on every retry.\n */\nexport class PermanentAiError extends Error {\n constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"PermanentAiError\";\n }\n}\n\n/** Timeouts, rate limits and 5xx are worth another attempt; 4xx are not. */\nexport function isRetryableAiError(err: unknown): boolean {\n if (err instanceof PermanentAiError) return false;\n\n const e = err as { name?: string; statusCode?: number; status?: number } | null;\n if (!e) return false;\n\n if (e.name === \"TimeoutError\" || e.name === \"AbortError\") return true;\n\n const status = e.statusCode ?? e.status;\n if (typeof status === \"number\") {\n return status === 408 || status === 409 || status === 429 || status >= 500;\n }\n\n // Unclassifiable (network errors, transport failures) — assume transient.\n return true;\n}\n\nexport interface AiWorkerOptions extends WorkerOptions {\n registry: any;\n idempotency: any;\n redis: Redis;\n generateAiContent: any;\n templateCache: TemplateCache;\n scheduledProducer: any;\n outboundProducers: any;\n db: any;\n}\n\nexport class AiWorker extends BaseWorker {\n private readonly registry: any;\n private readonly idempotency: any;\n private readonly redisCli: Redis;\n private readonly generateAiContent: any;\n private readonly templateCache: TemplateCache;\n private readonly scheduledProducer: any;\n private readonly outboundProducers: any;\n private readonly db: any;\n\n constructor(options: AiWorkerOptions) {\n super(options);\n this.registry = options.registry;\n this.idempotency = options.idempotency;\n this.redisCli = options.redis;\n this.generateAiContent = options.generateAiContent;\n this.templateCache = options.templateCache;\n this.scheduledProducer = options.scheduledProducer;\n this.outboundProducers = options.outboundProducers;\n this.db = options.db;\n }\n async process(message: StreamMessage): Promise<void> {\n const { event } = message;\n\n const payloadResult = this.registry.safeParsePayload(\"notification.ai_pending\", event.payload);\n if (!payloadResult.success) {\n this.logger.warn(\n { messageId: message.id, issues: payloadResult.error.issues },\n \"invalid notification.ai_pending payload — skipping\",\n );\n return;\n }\n\n const pending = payloadResult.data as NotificationAiPendingPayload;\n\n // Idempotency\n const idempotencyKey = `${pending.enrichedEventId}:${pending.recipientId}:${pending.channel}:ai`;\n if (!(await this.idempotency.checkAndMark(idempotencyKey))) {\n this.logger.debug(\n { messageId: message.id, eventId: event.id },\n \"duplicate ai task — skipping\",\n );\n return;\n }\n try {\n // Execute AI prompts. Each key is a separate billed model call, so the\n // count is capped rather than being driven by whatever the caller sent.\n const promptEntries = Object.entries(pending.aiPrompts);\n const maxPrompts =\n getAiConfig().maxPromptsPerNotification ?? AI_DEFAULTS.maxPromptsPerNotification;\n if (promptEntries.length > maxPrompts) {\n this.logger.warn(\n { messageId: message.id, requested: promptEntries.length, maxPrompts },\n \"aiPrompts exceeds the per-notification cap — extra prompts ignored\",\n );\n }\n\n const generatedVars: Record<string, string> = {};\n for (const [key, prompt] of promptEntries.slice(0, maxPrompts)) {\n generatedVars[key] = await this.generateAiContent(prompt, pending.templateVariables);\n }\n\n // Merge generated vars with original template vars\n const finalVars = { ...pending.templateVariables, ...generatedVars };\n\n const dbTemplate = pending.templateId\n ? await this.templateCache.getCachedTemplate(pending.projectId, pending.templateId)\n : null;\n\n const rendered = renderWithTemplate(dbTemplate, finalVars);\n\n const taskId = randomUUID();\n const destination =\n pending.channel === \"email\"\n ? pending.recipient.email\n : pending.channel === \"sms\"\n ? pending.recipient.phone\n : pending.channel === \"webhook\"\n ? pending.recipient.webhook\n : pending.channel === \"push\"\n ? (pending.recipient.pushTokens?.[0] ?? pending.recipient.pushToken)\n : undefined;\n const resolvedDestination =\n destination ?? (pending.channel === \"push\" ? undefined : pending.recipientId);\n\n const taskPayload: NotificationDispatchedPayload = {\n projectId: pending.projectId,\n taskId,\n enrichedEventId: pending.enrichedEventId,\n recipientId: pending.recipientId,\n channel: pending.channel,\n priority: pending.priority,\n templateId: pending.templateId,\n templateVariables: pending.templateVariables,\n aiPrompts: pending.aiPrompts,\n recipient: pending.recipient,\n renderedContent: rendered,\n destination: resolvedDestination,\n deliveryOptions: {\n maxAttempts: 3,\n timeoutMs: 10_000,\n },\n fallbackChain: pending.fallbackChain,\n };\n\n const envelope = buildStreamEvent(\n \"notification.dispatched\",\n taskPayload as Record<string, unknown>,\n \"ai-worker\",\n event.metadata.traceId,\n );\n\n // Route by scheduledAt\n const now = Date.now();\n const scheduledAt = pending.scheduledAt ? new Date(pending.scheduledAt).getTime() : now;\n\n if (scheduledAt > now) {\n await this.db.insert(scheduledPayloads).values({\n taskId,\n payload: taskPayload,\n });\n\n const scheduledEnvelope = buildStreamEvent(\n \"notification.scheduled\",\n {\n projectId: pending.projectId,\n enrichedEventId: pending.enrichedEventId,\n taskId,\n scheduledAt: pending.scheduledAt!,\n },\n \"ai-worker\",\n event.metadata.traceId,\n );\n\n await this.scheduledProducer.publish(scheduledEnvelope);\n this.logger.info(\n {\n messageId: message.id,\n taskId,\n scheduledAt: pending.scheduledAt,\n traceId: event.metadata.traceId,\n },\n \"task scheduled and payload cached after AI generation\",\n );\n } else {\n const p = getPriorityBucket(pending.priority);\n const outboundProducer = this.outboundProducers[p] ?? this.outboundProducers[\"normal\"]!;\n\n await outboundProducer.publish(envelope);\n this.logger.info(\n {\n messageId: message.id,\n taskId,\n recipientId: pending.recipientId,\n traceId: event.metadata.traceId,\n },\n \"task dispatched after AI generation\",\n );\n }\n } catch (err) {\n await this.idempotency.unmark(idempotencyKey);\n if (err instanceof PermanentAiError || (err as Error)?.name === \"PermanentAiError\") {\n // Retrying re-bills the same failing prompt. Fail the notification once.\n this.logger.error(\n { err, messageId: message.id, recipientId: pending.recipientId },\n \"AI generation failed permanently — dropping notification without retry\",\n );\n globalEmitter.emit(\n \"notification:failed\",\n pending.enrichedEventId,\n (err as Error).message,\n pending.channel,\n );\n return;\n }\n throw err;\n }\n }\n}\n\nexport async function startAiWorker() {\n logger = createLogger({ name: \"ai\", level: config.LOG_LEVEL });\n redis = new RedisClient({ url: config.REDIS_URL, name: \"ai\", logger });\n const dbData = createDatabase({ url: config.DATABASE_URL, applicationName: \"ai\", logger });\n sql = dbData.sql;\n db = dbData.db;\n templateRepo = new TemplateRepository(db);\n templateCache = new TemplateCache(templateRepo);\n consumer = new StreamConsumer({\n redis: redis.native,\n stream: STREAMS.AI_PENDING as StreamName,\n group: CONSUMER_GROUPS.AI,\n consumer: `ai-${process.pid}`,\n dlqStream: STREAMS.DEAD_LETTER,\n batchSize: config.WORKER_CONCURRENCY,\n logger,\n });\n\n pendingScanner = new PendingMessageScanner({\n redis: redis.native,\n stream: STREAMS.AI_PENDING as StreamName,\n group: CONSUMER_GROUPS.AI,\n consumer: `ai-${process.pid}`,\n logger,\n });\n\n const outboundProducers = {\n critical: new StreamProducer({\n redis: redis.native,\n stream: STREAMS.OUTBOUND_CRITICAL,\n logger,\n }),\n normal: new StreamProducer({ redis: redis.native, stream: STREAMS.OUTBOUND_NORMAL, logger }),\n low: new StreamProducer({ redis: redis.native, stream: STREAMS.OUTBOUND_LOW, logger }),\n };\n\n const scheduledProducer = new StreamProducer({\n redis: redis.native,\n stream: STREAMS.SCHEDULED,\n logger,\n });\n\n const idempotency = new IdempotencyGuard({\n redis: redis.native,\n keyPrefix: \"notif:processed:ai\",\n ttlSeconds: 86_400,\n });\n\n // AI generation\n async function generateAiContent(prompt: string, vars: Record<string, unknown>): Promise<string> {\n const aiConfig = getAiConfig();\n const interpolated = prompt.replace(/\\{\\{(\\w+)\\}\\}/g, (_, k: string) => String(vars[k] ?? \"\"));\n\n if (!aiConfig || !aiConfig.aiModel) {\n logger.warn(\"AI worker called but no AI model was provided to NotifkitServer\");\n return `[AI Disabled] ${interpolated}`;\n }\n\n const maxOutputTokens = aiConfig.maxOutputTokens ?? AI_DEFAULTS.maxOutputTokens;\n const timeoutMs = aiConfig.timeoutMs ?? AI_DEFAULTS.timeoutMs;\n\n try {\n const { text } = await generateText({\n model: aiConfig.aiModel,\n prompt: interpolated,\n maxOutputTokens,\n abortSignal: AbortSignal.timeout(timeoutMs),\n });\n\n return text;\n } catch (err) {\n // BaseWorker retries a throw up to maxRetriesBeforeDlq, and every retry is\n // another billed call. Only re-throw for failures a retry could actually\n // fix; a malformed prompt or a rejected request must not be re-billed.\n if (isRetryableAiError(err)) {\n logger.error({ err }, \"AI generation failed (transient) — will retry\");\n throw err;\n }\n throw new PermanentAiError(err instanceof Error ? err.message : String(err), { cause: err });\n }\n }\n\n worker = new AiWorker({\n consumer,\n pendingScanner,\n logger,\n concurrency: config.WORKER_CONCURRENCY,\n registry,\n idempotency,\n redis: redis.native,\n generateAiContent,\n templateCache,\n scheduledProducer,\n outboundProducers,\n db,\n });\n\n subscriber = redis.native.duplicate();\n await subscriber.subscribe(\"template.invalidated\");\n subscriber.on(\"message\", (channel: string, message: string) => {\n if (channel === \"template.invalidated\") {\n templateCache.invalidateKey(message);\n logger.info({ cacheKey: message }, \"invalidated template cache\");\n }\n });\n\n healthInterval = startHealthReporter(\"ai\", worker, redis, logger);\n\n logger.info({ env: config.NODE_ENV, redis: config.REDIS_URL }, \"ai starting\");\n await worker.start();\n}\n\nexport async function stopAiWorker(): Promise<void> {\n logger?.info(\"shutdown initiated\");\n if (healthInterval) {\n clearInterval(healthInterval);\n healthInterval = null;\n }\n if (subscriber) {\n subscriber.disconnect();\n subscriber = null;\n }\n if (worker) await worker.stop();\n if (sql) await sql.end();\n if (redis) await redis.disconnect();\n logger?.info(\"ai stopped\");\n}\n"],"mappings":";;;;AAiCA,QAAQ;AACR,MAAM,SAAS,eAAe;AAE9B,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ent, L as CircuitBreaker, Q as metrics, Qt as OUTBOUND_STREAMS, R as BatchProcessor, W as getPriorityBucket, X as StreamProducer, Y as StreamConsumer, Yt as CONSUMER_GROUPS, an as registry, c as BaseWorker, dn as baseConfigSchema, en as STREAMS, et as createLogger, it as createDatabase, k as RedisClient, l as NonRetryableError, mn as parseConfig, ot as deliveryOutbox, pn as loadEnv, q as globalEmitter, rt as IdempotencyGuard, u as startHealthReporter, ut as scheduledPayloads, x as ContactRepository } from "./src-DrSN2wCg.mjs";
1
+ import { J as PendingMessageScanner, Jt as buildStreamEvent, L as CircuitBreaker, Q as metrics, Qt as OUTBOUND_STREAMS, R as BatchProcessor, W as getPriorityBucket, X as StreamProducer, Y as StreamConsumer, Yt as CONSUMER_GROUPS, an as registry, c as BaseWorker, dn as baseConfigSchema, en as STREAMS, et as createLogger, it as createDatabase, k as RedisClient, l as NonRetryableError, mn as parseConfig, ot as deliveryOutbox, pn as loadEnv, q as globalEmitter, rt as IdempotencyGuard, u as startHealthReporter, ut as scheduledPayloads, x as ContactRepository } from "./src-C-PfEDMY.mjs";
2
2
  import { transportRegistry } from "./index.mjs";
3
3
  //#region src/services/delivery/throttle.ts
4
4
  const LUA_THROTTLE = `
@@ -626,4 +626,4 @@ async function stopDeliveryWorker() {
626
626
  //#endregion
627
627
  export { startDeliveryWorker, stopDeliveryWorker };
628
628
 
629
- //# sourceMappingURL=main-ClEeP5qw.mjs.map
629
+ //# sourceMappingURL=main-CCfc45ev.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"main-ClEeP5qw.mjs","names":[],"sources":["../src/services/delivery/throttle.ts","../src/services/delivery/main.ts"],"sourcesContent":["import type { Redis } from \"ioredis\";\n\nexport interface ThrottleResult {\n allowed: boolean;\n retryAfterMs: number;\n}\n\nconst LUA_THROTTLE = `\n local key = KEYS[1]\n local now = tonumber(ARGV[1])\n local windowSeconds = tonumber(ARGV[2])\n local limit = tonumber(ARGV[3])\n local member = ARGV[4]\n\n local clearBefore = now - (windowSeconds * 1000)\n \n -- Cleanup expired scores\n redis.call('ZREMRANGEBYSCORE', key, 0, clearBefore)\n \n -- Get current count\n local count = redis.call('ZCARD', key)\n \n if count >= limit then\n -- Find the oldest score\n local oldest = redis.call('ZRANGE', key, 0, 0, 'WITHSCORES')\n if oldest and oldest[2] then\n return {0, tonumber(oldest[2])}\n end\n return {0, now}\n end\n \n -- Add new request\n redis.call('ZADD', key, now, member)\n redis.call('EXPIRE', key, windowSeconds * 2)\n return {1, 0}\n`;\n\nexport async function throttleProvider(\n redis: Redis,\n channel: string,\n config: { limit: number; windowSeconds: number },\n logger: any,\n): Promise<ThrottleResult> {\n const key = `rate-limit:provider:${channel}`;\n const now = Date.now();\n const zmember = `${now}:${Math.random()}`;\n\n const result = (await redis.eval(\n LUA_THROTTLE,\n 1,\n key,\n now.toString(),\n config.windowSeconds.toString(),\n config.limit.toString(),\n zmember,\n )) as [number, number];\n\n const allowed = result[0] === 1;\n const oldestTimestamp = result[1];\n\n let retryAfterMs = 0;\n if (!allowed) {\n retryAfterMs = Math.max(0, oldestTimestamp + config.windowSeconds * 1000 - now);\n logger.warn(\n { channel, limit: config.limit, windowSeconds: config.windowSeconds, retryAfterMs },\n \"Provider rate limit hit — task must be rescheduled\",\n );\n }\n\n return { allowed, retryAfterMs };\n}\n","import { loadEnv, parseConfig, baseConfigSchema } from \"@/index.js\";\nimport { createLogger } from \"@/index.js\";\nimport { RedisClient, type Redis } from \"@/index.js\";\nimport {\n StreamConsumer,\n PendingMessageScanner,\n type StreamMessage,\n StreamProducer,\n} from \"@/index.js\";\nimport { BaseWorker } from \"@/index.js\";\nimport {\n STREAMS,\n OUTBOUND_STREAMS,\n CONSUMER_GROUPS,\n registry,\n buildStreamEvent,\n type NotificationDispatchedPayload,\n} from \"@/index.js\";\nimport { type StreamName } from \"@/contracts/streams.js\";\nimport { createDatabase } from \"@/db/index.js\";\nimport { deliveryOutbox, scheduledPayloads } from \"@/db/schema.js\";\nimport { ContactRepository, IdempotencyGuard } from \"@/index.js\";\nimport { transportRegistry } from \"@/index.js\";\nimport { globalEmitter, getPriorityBucket, type WorkerOptions } from \"@/shared/index.js\";\nimport { startHealthReporter, NonRetryableError } from \"@/workers/index.js\";\nimport { BatchProcessor, CircuitBreaker } from \"@/shared/index.js\";\nimport { throttleProvider } from \"./throttle.js\";\nimport { metrics } from \"@/metrics/index.js\";\n\n// ─── App-specific config ────────────────────────────────────────────────────\n\nconst deliveryConfigSchema = baseConfigSchema.extend({});\n\n// ─── Bootstrap ─────────────────────────────────────────────────────────────\n\nloadEnv();\nconst config = parseConfig(deliveryConfigSchema, process.env);\n\nlet logger: ReturnType<typeof createLogger>;\nlet redis: RedisClient;\nlet sql: any;\nlet db: any;\n\nlet consumer: StreamConsumer;\nlet pendingScanner: PendingMessageScanner;\nlet worker: BaseWorker;\nlet scheduledProducer: StreamProducer;\nlet enrichedProducers: Record<string, StreamProducer>;\nlet healthInterval: NodeJS.Timeout | null = null;\n\n(global as any)._telemetry = (global as any)._telemetry || {\n count: 0,\n insert: 0,\n provider: 0,\n flush: 0,\n ack: 0,\n dequeue: 0,\n dbupdate: 0,\n flushCount: 0,\n};\n\nexport interface DeliveryWorkerOptions extends WorkerOptions {\n transportRegistry: any;\n idempotency: any;\n redis: Redis;\n scheduledProducer: any;\n enrichedProducers: any;\n contactRepo: any;\n eventsProducer: any;\n globalEmitter: any;\n db: any;\n}\n\nexport class DeliveryWorker extends BaseWorker {\n private readonly transportRegistry: any;\n private readonly idempotency: any;\n private readonly redisCli: Redis;\n private readonly scheduledProducer: any;\n private readonly enrichedProducers: any;\n private readonly contactRepo: any;\n private readonly eventsProducer: any;\n private readonly globalEmitter: any;\n private readonly db: any;\n\n private eventProcessor: BatchProcessor<any, void>;\n private outboxUpdateProcessor: BatchProcessor<any, void>;\n private outboxInsertProcessor: BatchProcessor<any, boolean>;\n private breakers = new Map<string, CircuitBreaker>();\n\n constructor(options: DeliveryWorkerOptions) {\n super(options);\n this.transportRegistry = options.transportRegistry;\n this.idempotency = options.idempotency;\n this.redisCli = options.redis;\n this.scheduledProducer = options.scheduledProducer;\n this.enrichedProducers = options.enrichedProducers;\n this.contactRepo = options.contactRepo;\n this.eventsProducer = options.eventsProducer;\n this.globalEmitter = options.globalEmitter;\n this.db = options.db;\n\n this.eventProcessor = new BatchProcessor<any, void>(1000, 100, async (events) => {\n await this.eventsProducer.publishBatch(events);\n return events.map(() => undefined as void);\n });\n\n this.outboxUpdateProcessor = new BatchProcessor<any, void>(500, 100, async (updates) => {\n const { sql } = await import(\"drizzle-orm\");\n const values = updates.map((update) => ({\n taskId: update.taskId,\n channel: update.channel,\n destination: update.destination,\n providerMessageId: update.providerMessageId,\n }));\n\n const tDbUpdateStart = Date.now();\n await this.db\n .insert(deliveryOutbox)\n .values(values)\n .onConflictDoUpdate({\n target: [deliveryOutbox.taskId, deliveryOutbox.channel, deliveryOutbox.destination],\n set: { providerMessageId: sql`EXCLUDED.provider_message_id` },\n })\n .catch((e: any) => this.logger.error({ err: e }, \"background update failed\"));\n\n (global as any)._telemetry.dbupdate += Date.now() - tDbUpdateStart;\n (global as any)._telemetry.flushCount++;\n return updates.map(() => undefined as void);\n });\n\n this.outboxInsertProcessor = new BatchProcessor(500, 10, async (tasks) => {\n const values = tasks.map((task: any) => ({\n taskId: task.taskId,\n channel: task.channel,\n destination: task.destination,\n }));\n\n await this.db.insert(deliveryOutbox).values(values).onConflictDoNothing();\n return tasks.map(() => true);\n });\n }\n\n override async stop(): Promise<void> {\n await Promise.all([\n this.eventProcessor.flush(),\n this.outboxUpdateProcessor.flush(),\n this.outboxInsertProcessor.flush(),\n ]);\n await super.stop();\n }\n\n private getBreaker(name: string): CircuitBreaker {\n let breaker = this.breakers.get(name);\n if (!breaker) {\n breaker = new CircuitBreaker({ failureThreshold: 5, resetTimeoutMs: 30000 });\n this.breakers.set(name, breaker);\n }\n return breaker;\n }\n\n async process(message: StreamMessage, attempt: number = 1): Promise<void> {\n const { event } = message;\n const publishPromises: Promise<void>[] = [];\n\n const payloadResult = registry.safeParsePayload(\"notification.dispatched\", event.payload);\n if (!payloadResult.success) {\n this.logger.warn(\n { messageId: message.id, issues: payloadResult.error.issues },\n \"invalid notification.dispatched payload — skipping\",\n );\n return;\n }\n\n const task = payloadResult.data as NotificationDispatchedPayload;\n\n const fallbackToNextChannel = async (reason: string) => {\n if (task.fallbackChain && task.fallbackChain.length > 0 && task.recipient) {\n const nextChannel = task.fallbackChain[0];\n const remainingChain = task.fallbackChain.slice(1);\n\n const fallbackPayload: any = {\n // NotificationEnrichedPayload\n projectId: task.projectId,\n rawEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: nextChannel,\n priority: task.priority,\n templateId: task.templateId,\n templateVariables: task.templateVariables,\n aiPrompts: task.aiPrompts,\n recipient: task.recipient,\n scheduledAt: undefined,\n fallbackChain: remainingChain.length > 0 ? remainingChain : undefined,\n };\n\n const p = getPriorityBucket(task.priority);\n const producer = this.enrichedProducers[p] ?? this.enrichedProducers[\"normal\"]!;\n await producer.publish(\n buildStreamEvent(\n \"notification.enriched\",\n fallbackPayload,\n \"delivery\",\n event.metadata.traceId,\n ),\n );\n\n this.logger.info(\n { taskId: task.taskId, reason, nextChannel, traceId: event.metadata.traceId },\n \"Delivery failed completely, rolling over to next channel in fallback chain\",\n );\n return true; // Indicates fallback was triggered\n }\n return false; // No fallback\n };\n\n const transports = this.transportRegistry.getAll(task.channel);\n\n if (transports.length === 0) {\n this.logger.warn(\n { taskId: task.taskId, channel: task.channel },\n \"no transport registered for channel — dropping\",\n );\n const fallbackTriggered = await fallbackToNextChannel(\"no_transport\");\n if (!fallbackTriggered) {\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n \"no transport\",\n task.channel,\n task.projectId,\n );\n await this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n failureReason: \"no transport registered for channel\",\n failureCode: \"no_transport\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n );\n }\n return;\n }\n\n const limitConfig = transports[0].limits;\n if (limitConfig) {\n const { allowed, retryAfterMs } = await throttleProvider(\n this.redisCli,\n task.channel,\n limitConfig,\n this.logger,\n );\n if (!allowed) {\n task.throttleAttemptCount = (task.throttleAttemptCount ?? 0) + 1;\n const maxAttempts = task.deliveryOptions?.maxAttempts ?? 3;\n\n if (task.throttleAttemptCount > maxAttempts) {\n this.logger.warn(\n { messageId: message.id, taskId: task.taskId, attempts: task.throttleAttemptCount },\n \"provider rate limit max attempts exceeded\",\n );\n const fallbackTriggered = await fallbackToNextChannel(\"provider_throttle_exceeded\");\n if (!fallbackTriggered) {\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n \"provider throttle exceeded\",\n task.channel,\n task.projectId,\n );\n await this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n failureReason: \"provider rate limit max attempts exceeded\",\n failureCode: \"provider_throttle_exceeded\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n );\n }\n return;\n }\n\n const { sql } = await import(\"drizzle-orm\");\n await this.db\n .insert(scheduledPayloads)\n .values({\n taskId: task.taskId,\n payload: task,\n })\n .onConflictDoUpdate({\n target: scheduledPayloads.taskId,\n set: { payload: sql`EXCLUDED.payload` },\n });\n\n await this.scheduledProducer.publish(\n buildStreamEvent(\n \"notification.scheduled\",\n {\n projectId: task.projectId,\n enrichedEventId: task.enrichedEventId,\n taskId: task.taskId,\n scheduledAt: new Date(Date.now() + retryAfterMs).toISOString(),\n throttleAttemptCount: task.throttleAttemptCount,\n },\n \"delivery\",\n `${task.taskId}:throttle:${Date.now()}`,\n ),\n );\n\n return;\n }\n }\n\n const tInsertStart = Date.now();\n const idempotencyKey = task.taskId;\n if (!(await this.idempotency.checkAndMark(idempotencyKey, 60))) {\n this.logger.info(\n { messageId: message.id, taskId: task.taskId, channel: task.channel, attempt },\n \"duplicate delivery — skipping\",\n );\n return;\n }\n\n try {\n await this.outboxInsertProcessor.add(task);\n const insertTime = Date.now() - tInsertStart;\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.dispatched\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n templateId: task.templateId,\n attempt,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n\n // For push: we just need to send to the pre-resolved destination,\n // but if it's invalid, we deactivate it.\n if (task.channel === \"push\") {\n let lastResult: any = { success: false, error: \"No transports\" };\n\n for (const transport of transports) {\n try {\n const tProv = Date.now();\n const breaker = this.getBreaker(`${task.channel}:${transport.constructor.name}`);\n\n lastResult = await breaker.execute(async () => {\n const timeoutMs = task.deliveryOptions?.timeoutMs ?? 10_000;\n const controller = new AbortController();\n const timeout = setTimeout(() => {\n controller.abort(new Error(`Transport timeout after ${timeoutMs}ms`));\n }, timeoutMs);\n\n try {\n (task as any).signal = controller.signal;\n const res: any = await Promise.race([\n transport.send(task),\n new Promise((_, reject) => {\n if (controller.signal.aborted) return reject(controller.signal.reason);\n controller.signal.addEventListener(\"abort\", () =>\n reject(controller.signal.reason),\n );\n }),\n ]);\n if (!res.success && !res.invalidToken) {\n throw new Error(res.error ?? \"Transport failed\");\n }\n return res;\n } finally {\n clearTimeout(timeout);\n }\n });\n\n (global as any)._telemetry.provider += Date.now() - tProv;\n if (lastResult.success || lastResult.invalidToken) break;\n } catch (err: any) {\n lastResult = { success: false, error: err.message };\n }\n }\n\n if (lastResult.invalidToken) {\n await this.contactRepo.deactivate(\n task.projectId,\n task.recipientId,\n \"push\",\n task.destination,\n );\n this.logger.info({ token: task.destination }, \"deactivated invalid push token\");\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n \"invalidToken\",\n task.channel,\n task.projectId,\n );\n metrics.deliveryFailed.inc({ channel: task.channel, reason: \"invalid_token\" });\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n failureReason: \"invalid_token\",\n failureCode: \"push_failure\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n\n const fallbackTriggered = await fallbackToNextChannel(\"invalid_token\");\n if (!fallbackTriggered) {\n throw new NonRetryableError(\"Push delivery failed: invalid token\");\n }\n } else if (lastResult.success) {\n // Record providerMessageId in outbox before publishing event\n const providerMessageId = lastResult.providerMessageId || \"push-success\";\n\n publishPromises.push(\n this.outboxUpdateProcessor.add({\n taskId: task.taskId,\n channel: task.channel,\n destination: task.destination,\n providerMessageId,\n }),\n );\n\n this.logger.debug(\n { taskId: task.taskId, messageId: providerMessageId },\n \"push delivered\",\n );\n this.globalEmitter.emit(\n \"delivery:delivered\",\n task.taskId,\n providerMessageId,\n task.channel,\n task.projectId,\n );\n metrics.deliverySuccess.inc({ channel: task.channel });\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.delivered\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n channel: task.channel,\n deliveredAt: new Date().toISOString(),\n providerMessageId,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n } else {\n this.logger.warn(\n { taskId: task.taskId, error: lastResult.error },\n \"push delivery failed\",\n );\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n lastResult.error ?? \"push delivery failed\",\n task.channel,\n task.projectId,\n );\n metrics.deliveryFailed.inc({ channel: task.channel, reason: \"push_error\" });\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n failureReason: lastResult.error ?? \"push delivery failed\",\n failureCode: \"push_failure\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n\n const fallbackTriggered = await fallbackToNextChannel(\"push_delivery_failed\");\n if (!fallbackTriggered) {\n throw new NonRetryableError(lastResult.error ?? \"Push delivery failed\");\n }\n }\n } else {\n // Other channels (email, sms, webhook) use the pre-resolved destination\n let result: any = { success: false, error: \"No transports\" };\n for (const transport of transports) {\n try {\n const tProv = Date.now();\n const breaker = this.getBreaker(`${task.channel}:${transport.constructor.name}`);\n\n result = await breaker.execute(async () => {\n const timeoutMs = task.deliveryOptions?.timeoutMs ?? 10_000;\n const controller = new AbortController();\n const timeout = setTimeout(() => {\n controller.abort(new Error(`Transport timeout after ${timeoutMs}ms`));\n }, timeoutMs);\n\n try {\n (task as any).signal = controller.signal;\n const res: any = await Promise.race([\n transport.send(task),\n new Promise((_, reject) => {\n if (controller.signal.aborted) return reject(controller.signal.reason);\n controller.signal.addEventListener(\"abort\", () =>\n reject(controller.signal.reason),\n );\n }),\n ]);\n if (!res.success) {\n throw new Error(res.error ?? \"Transport failed\");\n }\n return res;\n } finally {\n clearTimeout(timeout);\n }\n });\n\n (global as any)._telemetry.provider += Date.now() - tProv;\n if (result.success) break;\n } catch (err: any) {\n result = { success: false, error: err.message };\n }\n }\n\n if (result.success) {\n // Record providerMessageId in outbox before publishing event\n const providerMessageId = result.providerMessageId || \"success\";\n\n publishPromises.push(\n this.outboxUpdateProcessor.add({\n taskId: task.taskId,\n channel: task.channel,\n destination: task.destination,\n providerMessageId,\n }),\n );\n\n this.globalEmitter.emit(\n \"delivery:delivered\",\n task.taskId,\n providerMessageId,\n task.channel,\n task.projectId,\n );\n metrics.deliverySuccess.inc({ channel: task.channel });\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.delivered\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n channel: task.channel,\n deliveredAt: new Date().toISOString(),\n providerMessageId,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n } else {\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n channel: task.channel,\n failureReason: result.error ?? \"delivery failed completely\",\n failureCode: \"provider_error\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n }\n\n if (!result.success) {\n this.logger.warn(\n { taskId: task.taskId, channel: task.channel, error: result.error },\n \"delivery failed completely across providers\",\n );\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n result.error ?? \"delivery failed completely\",\n task.channel,\n task.projectId,\n );\n metrics.deliveryFailed.inc({ channel: task.channel, reason: \"provider_error\" });\n\n const fallbackTriggered = await fallbackToNextChannel(\"all_providers_failed\");\n if (!fallbackTriggered) {\n throw new NonRetryableError(result.error ?? \"delivery failed\");\n }\n } else {\n this.logger.info(\n { taskId: task.taskId, channel: task.channel, messageId: result.providerMessageId },\n \"notification delivered\",\n );\n }\n }\n\n const tFlushStart = Date.now();\n await Promise.all(publishPromises).catch((err) => {\n this.logger.error(\n { err, taskId: task.taskId },\n \"failed to publish post-dispatch events, swallowing error to prevent duplicate delivery\",\n );\n });\n const flushTime = Date.now() - tFlushStart;\n\n await this.idempotency.markProcessed(idempotencyKey);\n\n const t = ((global as any)._telemetry = (global as any)._telemetry || {\n count: 0,\n insert: 0,\n provider: 0,\n flush: 0,\n ack: 0,\n });\n t.count++;\n t.insert += insertTime;\n t.flush += flushTime;\n\n if (t.count % 1000 === 0) {\n this.logger.debug(\n `[Metrics 1000 msgs] Dequeue: ${t.dequeue / 1000}ms, DB Insert: ${t.insert / 1000}ms, Provider: ${t.provider / 1000}ms, Wait for Flush: ${t.flush / 1000}ms, Ack: ${t.ack / 1000}ms | DB Update (avg per flush): ${t.dbupdate / Math.max(1, t.flushCount)}ms`,\n );\n t.count = 0;\n t.insert = 0;\n t.provider = 0;\n t.flush = 0;\n t.dequeue = 0;\n t.ack = 0;\n t.dbupdate = 0;\n t.flushCount = 0;\n }\n } catch (err) {\n await this.idempotency.unmark(idempotencyKey).catch(() => {});\n throw err;\n }\n }\n}\n\nexport async function startDeliveryWorker() {\n logger = createLogger({ name: \"delivery\", level: config.LOG_LEVEL });\n\n redis = new RedisClient({ url: config.REDIS_URL, name: \"delivery\", logger });\n const dbData = createDatabase({ url: config.DATABASE_URL, applicationName: \"delivery\", logger });\n sql = dbData.sql;\n db = dbData.db;\n consumer = new StreamConsumer({\n redis: redis.native,\n stream: OUTBOUND_STREAMS as unknown as StreamName[],\n group: CONSUMER_GROUPS.DELIVERY,\n consumer: `delivery-${process.pid}`,\n dlqStream: STREAMS.DEAD_LETTER,\n batchSize: config.WORKER_CONCURRENCY,\n logger,\n });\n\n scheduledProducer = new StreamProducer({\n redis: redis.native,\n stream: STREAMS.SCHEDULED,\n logger,\n });\n\n pendingScanner = new PendingMessageScanner({\n redis: redis.native,\n stream: OUTBOUND_STREAMS as unknown as StreamName[],\n group: CONSUMER_GROUPS.DELIVERY,\n consumer: `delivery-${process.pid}`,\n logger,\n });\n\n enrichedProducers = {\n critical: new StreamProducer({\n redis: redis.native,\n stream: STREAMS.ENRICHED_CRITICAL,\n logger,\n }),\n normal: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_NORMAL, logger }),\n low: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_LOW, logger }),\n };\n\n const contactRepo = new ContactRepository(db);\n\n const idempotency = new IdempotencyGuard({\n redis: redis.native,\n keyPrefix: \"notif:processed:delivery\",\n ttlSeconds: 86_400,\n });\n\n // ─── Stage 3: Delivery Worker ───────────────────────────────────────────────\n //\n // Pipeline:\n // 1. Parse payload as notification.dispatched\n // 2. Resolve active device tokens from DB\n // 3. Send via registered transport\n // 4. Deactivate invalid tokens in DB\n\n const eventsProducer = new StreamProducer({\n redis: redis.native,\n stream: STREAMS.EVENTS_INBOUND,\n logger,\n });\n\n worker = new DeliveryWorker({\n consumer,\n pendingScanner,\n logger,\n concurrency: config.WORKER_CONCURRENCY,\n transportRegistry,\n idempotency,\n redis: redis.native,\n scheduledProducer,\n enrichedProducers,\n contactRepo,\n eventsProducer,\n globalEmitter,\n db,\n });\n\n // ─── Health check interval ──────────────────────────────────────────────────\n\n healthInterval = startHealthReporter(\"delivery\", worker, redis, logger);\n\n logger.info(\n { env: config.NODE_ENV, channels: transportRegistry.registeredChannels() },\n \"delivery starting\",\n );\n await worker.start();\n}\n\n// ─── Shutdown ──────────────────────────────────────────────────────────────\n\nexport async function stopDeliveryWorker(): Promise<void> {\n logger?.info(\"shutdown initiated\");\n if (healthInterval) {\n clearInterval(healthInterval);\n healthInterval = null;\n }\n if (worker) await worker.stop();\n if (sql) await sql.end();\n if (redis) await redis.disconnect();\n logger?.info(\"delivery stopped\");\n}\n"],"mappings":";;;AAOA,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BrB,eAAsB,iBACpB,OACA,SACA,QACA,QACyB;CACzB,MAAM,MAAM,uBAAuB;CACnC,MAAM,MAAM,KAAK,IAAI;CACrB,MAAM,UAAU,GAAG,IAAI,GAAG,KAAK,OAAO;CAEtC,MAAM,SAAU,MAAM,MAAM,KAC1B,cACA,GACA,KACA,IAAI,SAAS,GACb,OAAO,cAAc,SAAS,GAC9B,OAAO,MAAM,SAAS,GACtB,OACF;CAEA,MAAM,UAAU,OAAO,OAAO;CAC9B,MAAM,kBAAkB,OAAO;CAE/B,IAAI,eAAe;CACnB,IAAI,CAAC,SAAS;EACZ,eAAe,KAAK,IAAI,GAAG,kBAAkB,OAAO,gBAAgB,MAAO,GAAG;EAC9E,OAAO,KACL;GAAE;GAAS,OAAO,OAAO;GAAO,eAAe,OAAO;GAAe;EAAa,GAClF,oDACF;CACF;CAEA,OAAO;EAAE;EAAS;CAAa;AACjC;;;ACvCA,MAAM,uBAAuB,iBAAiB,OAAO,CAAC,CAAC;AAIvD,QAAQ;AACR,MAAM,SAAS,YAAY,sBAAsB,QAAQ,GAAG;AAE5D,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AAEJ,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI,iBAAwC;AAE5C,OAAgB,aAAc,OAAe,cAAc;CACzD,OAAO;CACP,QAAQ;CACR,UAAU;CACV,OAAO;CACP,KAAK;CACL,SAAS;CACT,UAAU;CACV,YAAY;AACd;AAcA,IAAa,iBAAb,cAAoC,WAAW;CAC7C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;CACA;CACA,2BAAmB,IAAI,IAA4B;CAEnD,YAAY,SAAgC;EAC1C,MAAM,OA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,OAAO,MAAM;GAAQ,QAAQ,QAAQ;GAAc;EAAO,CAAC;CACvF;CAEA,MAAM,cAAc,IAAI,kBAAkB,EAAE;CAE5C,MAAM,cAAc,IAAI,iBAAiB;EACvC,OAAO,MAAM;EACb,WAAW;EACX,YAAY;CACd,CAAC;CAUD,MAAM,iBAAiB,IAAI,eAAe;EACxC,OAAO,MAAM;EACb,QAAQ,QAAQ;EAChB;CACF,CAAC;CAED,SAAS,IAAI,eAAe;EAC1B;EACA;EACA;EACA,aAAa,OAAO;EACpB;EACA;EACA,OAAO,MAAM;EACb;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CAID,iBAAiB,oBAAoB,YAAY,QAAQ,OAAO,MAAM;CAEtE,OAAO,KACL;EAAE,KAAK,OAAO;EAAU,UAAU,kBAAkB,mBAAmB;CAAE,GACzE,mBACF;CACA,MAAM,OAAO,MAAM;AACrB;AAIA,eAAsB,qBAAoC;CACxD,QAAQ,KAAK,oBAAoB;CACjC,IAAI,gBAAgB;EAClB,cAAc,cAAc;EAC5B,iBAAiB;CACnB;CACA,IAAI,QAAQ,MAAM,OAAO,KAAK;CAC9B,IAAI,KAAK,MAAM,IAAI,IAAI;CACvB,IAAI,OAAO,MAAM,MAAM,WAAW;CAClC,QAAQ,KAAK,kBAAkB;AACjC"}
1
+ {"version":3,"file":"main-CCfc45ev.mjs","names":[],"sources":["../src/services/delivery/throttle.ts","../src/services/delivery/main.ts"],"sourcesContent":["import type { Redis } from \"ioredis\";\n\nexport interface ThrottleResult {\n allowed: boolean;\n retryAfterMs: number;\n}\n\nconst LUA_THROTTLE = `\n local key = KEYS[1]\n local now = tonumber(ARGV[1])\n local windowSeconds = tonumber(ARGV[2])\n local limit = tonumber(ARGV[3])\n local member = ARGV[4]\n\n local clearBefore = now - (windowSeconds * 1000)\n \n -- Cleanup expired scores\n redis.call('ZREMRANGEBYSCORE', key, 0, clearBefore)\n \n -- Get current count\n local count = redis.call('ZCARD', key)\n \n if count >= limit then\n -- Find the oldest score\n local oldest = redis.call('ZRANGE', key, 0, 0, 'WITHSCORES')\n if oldest and oldest[2] then\n return {0, tonumber(oldest[2])}\n end\n return {0, now}\n end\n \n -- Add new request\n redis.call('ZADD', key, now, member)\n redis.call('EXPIRE', key, windowSeconds * 2)\n return {1, 0}\n`;\n\nexport async function throttleProvider(\n redis: Redis,\n channel: string,\n config: { limit: number; windowSeconds: number },\n logger: any,\n): Promise<ThrottleResult> {\n const key = `rate-limit:provider:${channel}`;\n const now = Date.now();\n const zmember = `${now}:${Math.random()}`;\n\n const result = (await redis.eval(\n LUA_THROTTLE,\n 1,\n key,\n now.toString(),\n config.windowSeconds.toString(),\n config.limit.toString(),\n zmember,\n )) as [number, number];\n\n const allowed = result[0] === 1;\n const oldestTimestamp = result[1];\n\n let retryAfterMs = 0;\n if (!allowed) {\n retryAfterMs = Math.max(0, oldestTimestamp + config.windowSeconds * 1000 - now);\n logger.warn(\n { channel, limit: config.limit, windowSeconds: config.windowSeconds, retryAfterMs },\n \"Provider rate limit hit — task must be rescheduled\",\n );\n }\n\n return { allowed, retryAfterMs };\n}\n","import { loadEnv, parseConfig, baseConfigSchema } from \"@/index.js\";\nimport { createLogger } from \"@/index.js\";\nimport { RedisClient, type Redis } from \"@/index.js\";\nimport {\n StreamConsumer,\n PendingMessageScanner,\n type StreamMessage,\n StreamProducer,\n} from \"@/index.js\";\nimport { BaseWorker } from \"@/index.js\";\nimport {\n STREAMS,\n OUTBOUND_STREAMS,\n CONSUMER_GROUPS,\n registry,\n buildStreamEvent,\n type NotificationDispatchedPayload,\n} from \"@/index.js\";\nimport { type StreamName } from \"@/contracts/streams.js\";\nimport { createDatabase } from \"@/db/index.js\";\nimport { deliveryOutbox, scheduledPayloads } from \"@/db/schema.js\";\nimport { ContactRepository, IdempotencyGuard } from \"@/index.js\";\nimport { transportRegistry } from \"@/index.js\";\nimport { globalEmitter, getPriorityBucket, type WorkerOptions } from \"@/shared/index.js\";\nimport { startHealthReporter, NonRetryableError } from \"@/workers/index.js\";\nimport { BatchProcessor, CircuitBreaker } from \"@/shared/index.js\";\nimport { throttleProvider } from \"./throttle.js\";\nimport { metrics } from \"@/metrics/index.js\";\n\n// ─── App-specific config ────────────────────────────────────────────────────\n\nconst deliveryConfigSchema = baseConfigSchema.extend({});\n\n// ─── Bootstrap ─────────────────────────────────────────────────────────────\n\nloadEnv();\nconst config = parseConfig(deliveryConfigSchema, process.env);\n\nlet logger: ReturnType<typeof createLogger>;\nlet redis: RedisClient;\nlet sql: any;\nlet db: any;\n\nlet consumer: StreamConsumer;\nlet pendingScanner: PendingMessageScanner;\nlet worker: BaseWorker;\nlet scheduledProducer: StreamProducer;\nlet enrichedProducers: Record<string, StreamProducer>;\nlet healthInterval: NodeJS.Timeout | null = null;\n\n(global as any)._telemetry = (global as any)._telemetry || {\n count: 0,\n insert: 0,\n provider: 0,\n flush: 0,\n ack: 0,\n dequeue: 0,\n dbupdate: 0,\n flushCount: 0,\n};\n\nexport interface DeliveryWorkerOptions extends WorkerOptions {\n transportRegistry: any;\n idempotency: any;\n redis: Redis;\n scheduledProducer: any;\n enrichedProducers: any;\n contactRepo: any;\n eventsProducer: any;\n globalEmitter: any;\n db: any;\n}\n\nexport class DeliveryWorker extends BaseWorker {\n private readonly transportRegistry: any;\n private readonly idempotency: any;\n private readonly redisCli: Redis;\n private readonly scheduledProducer: any;\n private readonly enrichedProducers: any;\n private readonly contactRepo: any;\n private readonly eventsProducer: any;\n private readonly globalEmitter: any;\n private readonly db: any;\n\n private eventProcessor: BatchProcessor<any, void>;\n private outboxUpdateProcessor: BatchProcessor<any, void>;\n private outboxInsertProcessor: BatchProcessor<any, boolean>;\n private breakers = new Map<string, CircuitBreaker>();\n\n constructor(options: DeliveryWorkerOptions) {\n super(options);\n this.transportRegistry = options.transportRegistry;\n this.idempotency = options.idempotency;\n this.redisCli = options.redis;\n this.scheduledProducer = options.scheduledProducer;\n this.enrichedProducers = options.enrichedProducers;\n this.contactRepo = options.contactRepo;\n this.eventsProducer = options.eventsProducer;\n this.globalEmitter = options.globalEmitter;\n this.db = options.db;\n\n this.eventProcessor = new BatchProcessor<any, void>(1000, 100, async (events) => {\n await this.eventsProducer.publishBatch(events);\n return events.map(() => undefined as void);\n });\n\n this.outboxUpdateProcessor = new BatchProcessor<any, void>(500, 100, async (updates) => {\n const { sql } = await import(\"drizzle-orm\");\n const values = updates.map((update) => ({\n taskId: update.taskId,\n channel: update.channel,\n destination: update.destination,\n providerMessageId: update.providerMessageId,\n }));\n\n const tDbUpdateStart = Date.now();\n await this.db\n .insert(deliveryOutbox)\n .values(values)\n .onConflictDoUpdate({\n target: [deliveryOutbox.taskId, deliveryOutbox.channel, deliveryOutbox.destination],\n set: { providerMessageId: sql`EXCLUDED.provider_message_id` },\n })\n .catch((e: any) => this.logger.error({ err: e }, \"background update failed\"));\n\n (global as any)._telemetry.dbupdate += Date.now() - tDbUpdateStart;\n (global as any)._telemetry.flushCount++;\n return updates.map(() => undefined as void);\n });\n\n this.outboxInsertProcessor = new BatchProcessor(500, 10, async (tasks) => {\n const values = tasks.map((task: any) => ({\n taskId: task.taskId,\n channel: task.channel,\n destination: task.destination,\n }));\n\n await this.db.insert(deliveryOutbox).values(values).onConflictDoNothing();\n return tasks.map(() => true);\n });\n }\n\n override async stop(): Promise<void> {\n await Promise.all([\n this.eventProcessor.flush(),\n this.outboxUpdateProcessor.flush(),\n this.outboxInsertProcessor.flush(),\n ]);\n await super.stop();\n }\n\n private getBreaker(name: string): CircuitBreaker {\n let breaker = this.breakers.get(name);\n if (!breaker) {\n breaker = new CircuitBreaker({ failureThreshold: 5, resetTimeoutMs: 30000 });\n this.breakers.set(name, breaker);\n }\n return breaker;\n }\n\n async process(message: StreamMessage, attempt: number = 1): Promise<void> {\n const { event } = message;\n const publishPromises: Promise<void>[] = [];\n\n const payloadResult = registry.safeParsePayload(\"notification.dispatched\", event.payload);\n if (!payloadResult.success) {\n this.logger.warn(\n { messageId: message.id, issues: payloadResult.error.issues },\n \"invalid notification.dispatched payload — skipping\",\n );\n return;\n }\n\n const task = payloadResult.data as NotificationDispatchedPayload;\n\n const fallbackToNextChannel = async (reason: string) => {\n if (task.fallbackChain && task.fallbackChain.length > 0 && task.recipient) {\n const nextChannel = task.fallbackChain[0];\n const remainingChain = task.fallbackChain.slice(1);\n\n const fallbackPayload: any = {\n // NotificationEnrichedPayload\n projectId: task.projectId,\n rawEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: nextChannel,\n priority: task.priority,\n templateId: task.templateId,\n templateVariables: task.templateVariables,\n aiPrompts: task.aiPrompts,\n recipient: task.recipient,\n scheduledAt: undefined,\n fallbackChain: remainingChain.length > 0 ? remainingChain : undefined,\n };\n\n const p = getPriorityBucket(task.priority);\n const producer = this.enrichedProducers[p] ?? this.enrichedProducers[\"normal\"]!;\n await producer.publish(\n buildStreamEvent(\n \"notification.enriched\",\n fallbackPayload,\n \"delivery\",\n event.metadata.traceId,\n ),\n );\n\n this.logger.info(\n { taskId: task.taskId, reason, nextChannel, traceId: event.metadata.traceId },\n \"Delivery failed completely, rolling over to next channel in fallback chain\",\n );\n return true; // Indicates fallback was triggered\n }\n return false; // No fallback\n };\n\n const transports = this.transportRegistry.getAll(task.channel);\n\n if (transports.length === 0) {\n this.logger.warn(\n { taskId: task.taskId, channel: task.channel },\n \"no transport registered for channel — dropping\",\n );\n const fallbackTriggered = await fallbackToNextChannel(\"no_transport\");\n if (!fallbackTriggered) {\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n \"no transport\",\n task.channel,\n task.projectId,\n );\n await this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n failureReason: \"no transport registered for channel\",\n failureCode: \"no_transport\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n );\n }\n return;\n }\n\n const limitConfig = transports[0].limits;\n if (limitConfig) {\n const { allowed, retryAfterMs } = await throttleProvider(\n this.redisCli,\n task.channel,\n limitConfig,\n this.logger,\n );\n if (!allowed) {\n task.throttleAttemptCount = (task.throttleAttemptCount ?? 0) + 1;\n const maxAttempts = task.deliveryOptions?.maxAttempts ?? 3;\n\n if (task.throttleAttemptCount > maxAttempts) {\n this.logger.warn(\n { messageId: message.id, taskId: task.taskId, attempts: task.throttleAttemptCount },\n \"provider rate limit max attempts exceeded\",\n );\n const fallbackTriggered = await fallbackToNextChannel(\"provider_throttle_exceeded\");\n if (!fallbackTriggered) {\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n \"provider throttle exceeded\",\n task.channel,\n task.projectId,\n );\n await this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n failureReason: \"provider rate limit max attempts exceeded\",\n failureCode: \"provider_throttle_exceeded\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n );\n }\n return;\n }\n\n const { sql } = await import(\"drizzle-orm\");\n await this.db\n .insert(scheduledPayloads)\n .values({\n taskId: task.taskId,\n payload: task,\n })\n .onConflictDoUpdate({\n target: scheduledPayloads.taskId,\n set: { payload: sql`EXCLUDED.payload` },\n });\n\n await this.scheduledProducer.publish(\n buildStreamEvent(\n \"notification.scheduled\",\n {\n projectId: task.projectId,\n enrichedEventId: task.enrichedEventId,\n taskId: task.taskId,\n scheduledAt: new Date(Date.now() + retryAfterMs).toISOString(),\n throttleAttemptCount: task.throttleAttemptCount,\n },\n \"delivery\",\n `${task.taskId}:throttle:${Date.now()}`,\n ),\n );\n\n return;\n }\n }\n\n const tInsertStart = Date.now();\n const idempotencyKey = task.taskId;\n if (!(await this.idempotency.checkAndMark(idempotencyKey, 60))) {\n this.logger.info(\n { messageId: message.id, taskId: task.taskId, channel: task.channel, attempt },\n \"duplicate delivery — skipping\",\n );\n return;\n }\n\n try {\n await this.outboxInsertProcessor.add(task);\n const insertTime = Date.now() - tInsertStart;\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.dispatched\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n templateId: task.templateId,\n attempt,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n\n // For push: we just need to send to the pre-resolved destination,\n // but if it's invalid, we deactivate it.\n if (task.channel === \"push\") {\n let lastResult: any = { success: false, error: \"No transports\" };\n\n for (const transport of transports) {\n try {\n const tProv = Date.now();\n const breaker = this.getBreaker(`${task.channel}:${transport.constructor.name}`);\n\n lastResult = await breaker.execute(async () => {\n const timeoutMs = task.deliveryOptions?.timeoutMs ?? 10_000;\n const controller = new AbortController();\n const timeout = setTimeout(() => {\n controller.abort(new Error(`Transport timeout after ${timeoutMs}ms`));\n }, timeoutMs);\n\n try {\n (task as any).signal = controller.signal;\n const res: any = await Promise.race([\n transport.send(task),\n new Promise((_, reject) => {\n if (controller.signal.aborted) return reject(controller.signal.reason);\n controller.signal.addEventListener(\"abort\", () =>\n reject(controller.signal.reason),\n );\n }),\n ]);\n if (!res.success && !res.invalidToken) {\n throw new Error(res.error ?? \"Transport failed\");\n }\n return res;\n } finally {\n clearTimeout(timeout);\n }\n });\n\n (global as any)._telemetry.provider += Date.now() - tProv;\n if (lastResult.success || lastResult.invalidToken) break;\n } catch (err: any) {\n lastResult = { success: false, error: err.message };\n }\n }\n\n if (lastResult.invalidToken) {\n await this.contactRepo.deactivate(\n task.projectId,\n task.recipientId,\n \"push\",\n task.destination,\n );\n this.logger.info({ token: task.destination }, \"deactivated invalid push token\");\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n \"invalidToken\",\n task.channel,\n task.projectId,\n );\n metrics.deliveryFailed.inc({ channel: task.channel, reason: \"invalid_token\" });\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n failureReason: \"invalid_token\",\n failureCode: \"push_failure\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n\n const fallbackTriggered = await fallbackToNextChannel(\"invalid_token\");\n if (!fallbackTriggered) {\n throw new NonRetryableError(\"Push delivery failed: invalid token\");\n }\n } else if (lastResult.success) {\n // Record providerMessageId in outbox before publishing event\n const providerMessageId = lastResult.providerMessageId || \"push-success\";\n\n publishPromises.push(\n this.outboxUpdateProcessor.add({\n taskId: task.taskId,\n channel: task.channel,\n destination: task.destination,\n providerMessageId,\n }),\n );\n\n this.logger.debug(\n { taskId: task.taskId, messageId: providerMessageId },\n \"push delivered\",\n );\n this.globalEmitter.emit(\n \"delivery:delivered\",\n task.taskId,\n providerMessageId,\n task.channel,\n task.projectId,\n );\n metrics.deliverySuccess.inc({ channel: task.channel });\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.delivered\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n channel: task.channel,\n deliveredAt: new Date().toISOString(),\n providerMessageId,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n } else {\n this.logger.warn(\n { taskId: task.taskId, error: lastResult.error },\n \"push delivery failed\",\n );\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n lastResult.error ?? \"push delivery failed\",\n task.channel,\n task.projectId,\n );\n metrics.deliveryFailed.inc({ channel: task.channel, reason: \"push_error\" });\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n recipientId: task.recipientId,\n channel: task.channel,\n failureReason: lastResult.error ?? \"push delivery failed\",\n failureCode: \"push_failure\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n\n const fallbackTriggered = await fallbackToNextChannel(\"push_delivery_failed\");\n if (!fallbackTriggered) {\n throw new NonRetryableError(lastResult.error ?? \"Push delivery failed\");\n }\n }\n } else {\n // Other channels (email, sms, webhook) use the pre-resolved destination\n let result: any = { success: false, error: \"No transports\" };\n for (const transport of transports) {\n try {\n const tProv = Date.now();\n const breaker = this.getBreaker(`${task.channel}:${transport.constructor.name}`);\n\n result = await breaker.execute(async () => {\n const timeoutMs = task.deliveryOptions?.timeoutMs ?? 10_000;\n const controller = new AbortController();\n const timeout = setTimeout(() => {\n controller.abort(new Error(`Transport timeout after ${timeoutMs}ms`));\n }, timeoutMs);\n\n try {\n (task as any).signal = controller.signal;\n const res: any = await Promise.race([\n transport.send(task),\n new Promise((_, reject) => {\n if (controller.signal.aborted) return reject(controller.signal.reason);\n controller.signal.addEventListener(\"abort\", () =>\n reject(controller.signal.reason),\n );\n }),\n ]);\n if (!res.success) {\n throw new Error(res.error ?? \"Transport failed\");\n }\n return res;\n } finally {\n clearTimeout(timeout);\n }\n });\n\n (global as any)._telemetry.provider += Date.now() - tProv;\n if (result.success) break;\n } catch (err: any) {\n result = { success: false, error: err.message };\n }\n }\n\n if (result.success) {\n // Record providerMessageId in outbox before publishing event\n const providerMessageId = result.providerMessageId || \"success\";\n\n publishPromises.push(\n this.outboxUpdateProcessor.add({\n taskId: task.taskId,\n channel: task.channel,\n destination: task.destination,\n providerMessageId,\n }),\n );\n\n this.globalEmitter.emit(\n \"delivery:delivered\",\n task.taskId,\n providerMessageId,\n task.channel,\n task.projectId,\n );\n metrics.deliverySuccess.inc({ channel: task.channel });\n\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.delivered\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n channel: task.channel,\n deliveredAt: new Date().toISOString(),\n providerMessageId,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n } else {\n publishPromises.push(\n this.eventProcessor.add(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: task.projectId,\n taskId: task.taskId,\n enrichedEventId: task.enrichedEventId,\n channel: task.channel,\n failureReason: result.error ?? \"delivery failed completely\",\n failureCode: \"provider_error\",\n retryable: false,\n attempt,\n templateId: task.templateId,\n workflowInstanceId:\n event.metadata.source === \"workflow\" ? event.metadata.traceId : undefined,\n campaignId: task.campaignId,\n },\n \"delivery\",\n event.metadata.traceId,\n ),\n ),\n );\n }\n\n if (!result.success) {\n this.logger.warn(\n { taskId: task.taskId, channel: task.channel, error: result.error },\n \"delivery failed completely across providers\",\n );\n this.globalEmitter.emit(\n \"delivery:failed\",\n task.taskId,\n result.error ?? \"delivery failed completely\",\n task.channel,\n task.projectId,\n );\n metrics.deliveryFailed.inc({ channel: task.channel, reason: \"provider_error\" });\n\n const fallbackTriggered = await fallbackToNextChannel(\"all_providers_failed\");\n if (!fallbackTriggered) {\n throw new NonRetryableError(result.error ?? \"delivery failed\");\n }\n } else {\n this.logger.info(\n { taskId: task.taskId, channel: task.channel, messageId: result.providerMessageId },\n \"notification delivered\",\n );\n }\n }\n\n const tFlushStart = Date.now();\n await Promise.all(publishPromises).catch((err) => {\n this.logger.error(\n { err, taskId: task.taskId },\n \"failed to publish post-dispatch events, swallowing error to prevent duplicate delivery\",\n );\n });\n const flushTime = Date.now() - tFlushStart;\n\n await this.idempotency.markProcessed(idempotencyKey);\n\n const t = ((global as any)._telemetry = (global as any)._telemetry || {\n count: 0,\n insert: 0,\n provider: 0,\n flush: 0,\n ack: 0,\n });\n t.count++;\n t.insert += insertTime;\n t.flush += flushTime;\n\n if (t.count % 1000 === 0) {\n this.logger.debug(\n `[Metrics 1000 msgs] Dequeue: ${t.dequeue / 1000}ms, DB Insert: ${t.insert / 1000}ms, Provider: ${t.provider / 1000}ms, Wait for Flush: ${t.flush / 1000}ms, Ack: ${t.ack / 1000}ms | DB Update (avg per flush): ${t.dbupdate / Math.max(1, t.flushCount)}ms`,\n );\n t.count = 0;\n t.insert = 0;\n t.provider = 0;\n t.flush = 0;\n t.dequeue = 0;\n t.ack = 0;\n t.dbupdate = 0;\n t.flushCount = 0;\n }\n } catch (err) {\n await this.idempotency.unmark(idempotencyKey).catch(() => {});\n throw err;\n }\n }\n}\n\nexport async function startDeliveryWorker() {\n logger = createLogger({ name: \"delivery\", level: config.LOG_LEVEL });\n\n redis = new RedisClient({ url: config.REDIS_URL, name: \"delivery\", logger });\n const dbData = createDatabase({ url: config.DATABASE_URL, applicationName: \"delivery\", logger });\n sql = dbData.sql;\n db = dbData.db;\n consumer = new StreamConsumer({\n redis: redis.native,\n stream: OUTBOUND_STREAMS as unknown as StreamName[],\n group: CONSUMER_GROUPS.DELIVERY,\n consumer: `delivery-${process.pid}`,\n dlqStream: STREAMS.DEAD_LETTER,\n batchSize: config.WORKER_CONCURRENCY,\n logger,\n });\n\n scheduledProducer = new StreamProducer({\n redis: redis.native,\n stream: STREAMS.SCHEDULED,\n logger,\n });\n\n pendingScanner = new PendingMessageScanner({\n redis: redis.native,\n stream: OUTBOUND_STREAMS as unknown as StreamName[],\n group: CONSUMER_GROUPS.DELIVERY,\n consumer: `delivery-${process.pid}`,\n logger,\n });\n\n enrichedProducers = {\n critical: new StreamProducer({\n redis: redis.native,\n stream: STREAMS.ENRICHED_CRITICAL,\n logger,\n }),\n normal: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_NORMAL, logger }),\n low: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_LOW, logger }),\n };\n\n const contactRepo = new ContactRepository(db);\n\n const idempotency = new IdempotencyGuard({\n redis: redis.native,\n keyPrefix: \"notif:processed:delivery\",\n ttlSeconds: 86_400,\n });\n\n // ─── Stage 3: Delivery Worker ───────────────────────────────────────────────\n //\n // Pipeline:\n // 1. Parse payload as notification.dispatched\n // 2. Resolve active device tokens from DB\n // 3. Send via registered transport\n // 4. Deactivate invalid tokens in DB\n\n const eventsProducer = new StreamProducer({\n redis: redis.native,\n stream: STREAMS.EVENTS_INBOUND,\n logger,\n });\n\n worker = new DeliveryWorker({\n consumer,\n pendingScanner,\n logger,\n concurrency: config.WORKER_CONCURRENCY,\n transportRegistry,\n idempotency,\n redis: redis.native,\n scheduledProducer,\n enrichedProducers,\n contactRepo,\n eventsProducer,\n globalEmitter,\n db,\n });\n\n // ─── Health check interval ──────────────────────────────────────────────────\n\n healthInterval = startHealthReporter(\"delivery\", worker, redis, logger);\n\n logger.info(\n { env: config.NODE_ENV, channels: transportRegistry.registeredChannels() },\n \"delivery starting\",\n );\n await worker.start();\n}\n\n// ─── Shutdown ──────────────────────────────────────────────────────────────\n\nexport async function stopDeliveryWorker(): Promise<void> {\n logger?.info(\"shutdown initiated\");\n if (healthInterval) {\n clearInterval(healthInterval);\n healthInterval = null;\n }\n if (worker) await worker.stop();\n if (sql) await sql.end();\n if (redis) await redis.disconnect();\n logger?.info(\"delivery stopped\");\n}\n"],"mappings":";;;AAOA,MAAM,eAAe;;;;;;;;;;;;;;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iB;EACvC,OAAO,MAAM;EACb,WAAW;EACX,YAAY;CACd,CAAC;CAUD,MAAM,iBAAiB,IAAI,eAAe;EACxC,OAAO,MAAM;EACb,QAAQ,QAAQ;EAChB;CACF,CAAC;CAED,SAAS,IAAI,eAAe;EAC1B;EACA;EACA;EACA,aAAa,OAAO;EACpB;EACA;EACA,OAAO,MAAM;EACb;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CAID,iBAAiB,oBAAoB,YAAY,QAAQ,OAAO,MAAM;CAEtE,OAAO,KACL;EAAE,KAAK,OAAO;EAAU,UAAU,kBAAkB,mBAAmB;CAAE,GACzE,mBACF;CACA,MAAM,OAAO,MAAM;AACrB;AAIA,eAAsB,qBAAoC;CACxD,QAAQ,KAAK,oBAAoB;CACjC,IAAI,gBAAgB;EAClB,cAAc,cAAc;EAC5B,iBAAiB;CACnB;CACA,IAAI,QAAQ,MAAM,OAAO,KAAK;CAC9B,IAAI,KAAK,MAAM,IAAI,IAAI;CACvB,IAAI,OAAO,MAAM,MAAM,WAAW;CAClC,QAAQ,KAAK,kBAAkB;AACjC"}
@@ -1,4 +1,4 @@
1
- import { E as UserRepository, J as PendingMessageScanner, Jt as buildStreamEvent, S as PreferenceRepository, T as TemplateRepository, W as getPriorityBucket, X as StreamProducer, Y as StreamConsumer, Yt as CONSUMER_GROUPS, Zt as INBOUND_STREAMS, an as registry, c as BaseWorker, en as STREAMS, et as createLogger, g as TemplateCache, hn as readBaseConfig, it as createDatabase, k as RedisClient, pn as loadEnv, rt as IdempotencyGuard, u as startHealthReporter, x as ContactRepository } from "./src-DrSN2wCg.mjs";
1
+ import { E as UserRepository, J as PendingMessageScanner, Jt as buildStreamEvent, S as PreferenceRepository, T as TemplateRepository, W as getPriorityBucket, X as StreamProducer, Y as StreamConsumer, Yt as CONSUMER_GROUPS, Zt as INBOUND_STREAMS, an as registry, c as BaseWorker, en as STREAMS, et as createLogger, g as TemplateCache, hn as readBaseConfig, it as createDatabase, k as RedisClient, pn as loadEnv, rt as IdempotencyGuard, u as startHealthReporter, x as ContactRepository } from "./src-C-PfEDMY.mjs";
2
2
  //#region src/services/enricher/main.ts
3
3
  loadEnv();
4
4
  const config = readBaseConfig();
@@ -427,4 +427,4 @@ async function stopEnricherWorker() {
427
427
  //#endregion
428
428
  export { startEnricherWorker, stopEnricherWorker };
429
429
 
430
- //# sourceMappingURL=main-BIcKzWHE.mjs.map
430
+ //# sourceMappingURL=main-Ce9dcrsg.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"main-BIcKzWHE.mjs","names":[],"sources":["../src/services/enricher/main.ts"],"sourcesContent":["import { loadEnv, readBaseConfig } from \"@/index.js\";\nimport { createLogger } from \"@/index.js\";\nimport { RedisClient } from \"@/index.js\";\nimport {\n StreamConsumer,\n PendingMessageScanner,\n StreamProducer,\n type StreamMessage,\n} from \"@/index.js\";\nimport { BaseWorker } from \"@/index.js\";\nimport {\n STREAMS,\n INBOUND_STREAMS,\n CONSUMER_GROUPS,\n registry,\n buildStreamEvent,\n type NotificationCreatedPayload,\n type NotificationEnrichedPayload,\n} from \"@/index.js\";\nimport { type StreamName } from \"@/contracts/streams.js\";\nimport { IdempotencyGuard } from \"@/index.js\";\nimport { createDatabase } from \"@/db/index.js\";\nimport {\n UserRepository,\n PreferenceRepository,\n TemplateRepository,\n ContactRepository,\n} from \"@/index.js\";\nimport { TemplateCache } from \"@/templates/index.js\";\nimport { getPriorityBucket, type WorkerOptions } from \"@/shared/index.js\";\nimport { startHealthReporter } from \"@/workers/index.js\";\n\n// ─── Bootstrap ─────────────────────────────────────────────────────────────\n\nloadEnv();\nconst config = readBaseConfig();\n\nlet logger: ReturnType<typeof createLogger>;\nlet redis: RedisClient;\nlet sql: any;\nlet db: any;\n\nlet consumer: StreamConsumer;\nlet pendingScanner: PendingMessageScanner;\nlet worker: BaseWorker;\nlet healthInterval: NodeJS.Timeout | null = null;\n\nexport interface EnricherWorkerOptions extends WorkerOptions {\n producers: any;\n idempotency: any;\n userRepo: any;\n prefRepo: any;\n contactRepo: any;\n templateCache: TemplateCache;\n}\n\nexport class EnricherWorker extends BaseWorker {\n private readonly producers: any;\n private readonly idempotency: any;\n private readonly userRepo: any;\n private readonly prefRepo: any;\n private readonly contactRepo: any;\n private readonly templateCache: TemplateCache;\n\n private userBatch: {\n projectId: string;\n userId: string;\n resolve: (p: any) => void;\n reject: (e: any) => void;\n }[] = [];\n private batchTimer: NodeJS.Timeout | null = null;\n private eventBuffer: {\n producer: any;\n event: any;\n resolve: () => void;\n reject: (e: any) => void;\n }[] = [];\n private flushTimer: NodeJS.Timeout | null = null;\n\n private contactBatch: {\n projectId: string;\n userIds: string[];\n resolve: (c: Map<string, any[]>) => void;\n reject: (e: any) => void;\n }[] = [];\n private contactBatchTimer: NodeJS.Timeout | null = null;\n\n private async loadContacts(projectId: string, userIds: string[]): Promise<Map<string, any[]>> {\n return new Promise((resolve, reject) => {\n this.contactBatch.push({ projectId, userIds, resolve, reject });\n if (this.contactBatch.length >= 500) {\n if (this.contactBatchTimer) clearTimeout(this.contactBatchTimer);\n void this.flushContactBatch();\n } else if (!this.contactBatchTimer) {\n this.contactBatchTimer = setTimeout(() => void this.flushContactBatch(), 10);\n }\n });\n }\n\n private async flushContactBatch(): Promise<void> {\n const batch = this.contactBatch;\n this.contactBatch = [];\n this.contactBatchTimer = null;\n if (batch.length === 0) return;\n\n try {\n const byProject = new Map<string, typeof batch>();\n for (const b of batch) {\n if (!byProject.has(b.projectId)) byProject.set(b.projectId, []);\n byProject.get(b.projectId)!.push(b);\n }\n\n for (const [projectId, items] of byProject) {\n const userIds = Array.from(new Set(items.flatMap((i) => i.userIds)));\n const contactsMap = await this.contactRepo.findActiveByUserIds(projectId, userIds);\n for (const item of items) {\n item.resolve(contactsMap);\n }\n }\n } catch (err) {\n for (const b of batch) b.reject(err);\n }\n }\n\n constructor(options: EnricherWorkerOptions) {\n super(options);\n this.producers = options.producers;\n this.idempotency = options.idempotency;\n this.userRepo = options.userRepo;\n this.prefRepo = options.prefRepo;\n this.contactRepo = options.contactRepo;\n this.templateCache = options.templateCache;\n\n this.flushTimer = setInterval(() => void this.flushWorkerBuffers(), 100);\n }\n\n override async stop(): Promise<void> {\n if (this.batchTimer) {\n clearTimeout(this.batchTimer);\n this.batchTimer = null;\n }\n if (this.flushTimer) {\n clearInterval(this.flushTimer);\n this.flushTimer = null;\n }\n if (this.contactBatchTimer) {\n clearTimeout(this.contactBatchTimer);\n this.contactBatchTimer = null;\n }\n await this.flushContactBatch();\n await this.flushUserBatch();\n await this.flushWorkerBuffers();\n await super.stop();\n }\n\n private async flushWorkerBuffers(): Promise<void> {\n if (this.eventBuffer.length === 0) return;\n const events = this.eventBuffer;\n this.eventBuffer = [];\n\n try {\n const byProducer = new Map<any, typeof events>();\n for (const e of events) {\n if (!byProducer.has(e.producer)) byProducer.set(e.producer, []);\n byProducer.get(e.producer)!.push(e);\n }\n for (const [producer, batch] of byProducer) {\n await producer.publishBatch(batch.map((b) => b.event));\n for (const b of batch) b.resolve();\n }\n } catch (err: any) {\n this.logger.error({ err }, \"failed to flush events in EnricherWorker\");\n for (const e of events) e.reject(err);\n }\n }\n\n private async loadUser(projectId: string, userId: string): Promise<any> {\n return new Promise((resolve, reject) => {\n this.userBatch.push({ projectId, userId, resolve, reject });\n if (this.userBatch.length >= 500) {\n if (this.batchTimer) clearTimeout(this.batchTimer);\n void this.flushUserBatch();\n } else if (!this.batchTimer) {\n this.batchTimer = setTimeout(() => {\n void this.flushUserBatch();\n }, 10);\n }\n });\n }\n\n private async flushUserBatch() {\n const batch = this.userBatch;\n this.userBatch = [];\n this.batchTimer = null;\n\n const byProject = new Map<string, typeof batch>();\n for (const b of batch) {\n if (!byProject.has(b.projectId)) byProject.set(b.projectId, []);\n byProject.get(b.projectId)!.push(b);\n }\n\n for (const [projectId, reqs] of byProject.entries()) {\n try {\n const uniqueIds = Array.from(new Set(reqs.map((r) => r.userId)));\n const profiles = await this.userRepo.findRecordsByIds(projectId, uniqueIds);\n const profileMap = new Map(profiles.map((p: any) => [p.userId, p]));\n\n for (const req of reqs) {\n req.resolve(profileMap.get(req.userId) || null);\n }\n } catch (err) {\n for (const req of reqs) req.reject(err);\n }\n }\n }\n\n async process(message: StreamMessage): Promise<void> {\n const { event } = message;\n const publishPromises: Promise<void>[] = [];\n\n let isRequested = true;\n const requestedResult = registry.safeParsePayload(\"notification.requested\", event.payload);\n let createdResult: any = null;\n if (!requestedResult.success) {\n createdResult = registry.safeParsePayload(\"notification.created\", event.payload);\n isRequested = false;\n }\n\n if (!isRequested && (!createdResult || !createdResult.success)) {\n const issues = createdResult\n ? createdResult.error.issues\n : (requestedResult as any).error.issues;\n this.logger.warn({ messageId: message.id, issues }, \"invalid payload — skipping\");\n return;\n }\n\n // Handle legacy notification.created\n if (!isRequested) {\n const raw = createdResult.data as NotificationCreatedPayload;\n const dedupeId = `${raw.projectId}:${raw.idempotencyKey ?? event.id}`;\n if (!(await this.idempotency.checkAndMark(dedupeId, 60))) return;\n try {\n const profile = await this.userRepo.findRecordById(raw.projectId, raw.recipientId);\n if (!profile) return;\n\n const prefs = await this.prefRepo.findByUserId(raw.projectId, raw.recipientId);\n const optedOutTypes = new Set(\n prefs.filter((p: any) => !p.optedIn).map((p: any) => p.eventType),\n );\n\n const enrichedPayload: NotificationEnrichedPayload = {\n projectId: raw.projectId,\n rawEventId: event.id,\n recipientId: raw.recipientId,\n channel: raw.channel,\n priority: raw.priority,\n templateId: raw.templateId,\n templateVariables: raw.payload,\n recipient: {\n id: profile.userId,\n email: profile.email ?? undefined,\n locale: profile.language ?? \"en\",\n timezone: profile.timezone ?? \"UTC\",\n preferences: {\n optedOut:\n optedOutTypes.has(event.type) || profile.preferences.topics?.[event.type] === false,\n channels: Object.entries(profile.preferences.channels ?? {})\n .filter(([_, enabled]) => !enabled)\n .map(([channel]) => channel as any),\n quietHours: profile.preferences.quietHours,\n },\n },\n // No campaignId: this is the legacy `notification.created` path,\n // which predates campaigns and carries no label to attribute to.\n scheduledAt: raw.scheduledAt,\n };\n\n const p = getPriorityBucket(raw.priority);\n const producer = this.producers[p] ?? this.producers[\"normal\"]!;\n\n publishPromises.push(\n new Promise((resolve, reject) => {\n this.eventBuffer.push({\n producer,\n event: buildStreamEvent(\n \"notification.enriched\",\n enrichedPayload as Record<string, unknown>,\n \"enricher\",\n event.metadata.traceId,\n ),\n resolve,\n reject,\n });\n }),\n );\n this.logger.info(\n { messageId: message.id, eventId: event.id, recipientId: raw.recipientId },\n \"event enriched\",\n );\n } catch (err) {\n throw err;\n }\n await Promise.all(publishPromises).catch(async (err) => {\n await this.idempotency.unmark(dedupeId).catch(() => {});\n throw err;\n });\n await this.idempotency.markProcessed(dedupeId);\n return;\n }\n\n // Handle new notification.requested\n const raw = (requestedResult as any).data;\n const dedupeId = `${raw.projectId}:${raw.idempotencyKey ?? event.id}`;\n if (!(await this.idempotency.checkAndMark(dedupeId, 60))) return;\n try {\n let userIds: string[] = [];\n if (raw.target.type === \"user\") {\n userIds = [raw.target.userId];\n } else if (raw.target.type === \"segment\") {\n userIds = await this.userRepo.findUsersBySegment(raw.projectId, raw.target.segment);\n this.logger.info(\n { segment: raw.target.segment, count: userIds.length },\n \"Resolved segment\",\n );\n } else if (raw.target.type === \"topic\") {\n userIds = await this.userRepo.findUsersByTopic(raw.projectId, raw.target.topic);\n this.logger.info({ topic: raw.target.topic, count: userIds.length }, \"Resolved topic\");\n } else {\n this.logger.warn({ target: raw.target }, \"Segment/topic resolution not fully implemented\");\n // Stub: maybe resolve later\n }\n\n const maxUsers = readBaseConfig().SEGMENT_MAX_USERS;\n if (userIds.length > maxUsers) {\n this.logger.error(\n { count: userIds.length, max: maxUsers, projectId: raw.projectId, eventId: event.id },\n \"Segment fan-out exceeds maximum allowed limit\",\n );\n const p = getPriorityBucket(raw.priority ?? \"normal\");\n const producer = this.producers[p] ?? this.producers.normal;\n await producer.publish(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: raw.projectId,\n rawEventId: event.id,\n error: `Segment fan-out of ${userIds.length} exceeds limit of ${maxUsers}`,\n },\n \"enricher\",\n event.metadata.traceId,\n ),\n );\n return;\n }\n\n // Topic opt-outs are keyed on the TEMPLATE's topics, not the envelope type.\n // `event.type` here is \"notification.requested\", which no user ever sets a\n // preference against, so keying on it silently disabled every opt-out.\n const template = raw.templateId\n ? await this.templateCache.getCachedTemplate(raw.projectId, raw.templateId)\n : null;\n const topics: string[] = template?.topics ?? [];\n\n const channels =\n raw.channels && raw.channels.length > 0 ? raw.channels : ([\"email\"] as any[]);\n const isFallback = (raw as any).fallback === true;\n\n // If fallback is true, we only emit the first channel, and pass the rest in fallbackChain.\n // If fallback is false, we emit all channels concurrently.\n const channelsToProcess = isFallback ? [channels[0]] : channels;\n const fallbackChain = isFallback ? channels.slice(1) : undefined;\n\n const chunkArray = <T>(arr: T[], size: number) =>\n Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>\n arr.slice(i * size, i * size + size),\n );\n\n const chunks = chunkArray(userIds, 500);\n\n for (const chunk of chunks) {\n const profiles = (\n await Promise.all(chunk.map((id) => this.loadUser(raw.projectId, id)))\n ).filter(Boolean);\n const contactsByUser = await this.loadContacts(\n raw.projectId,\n profiles.map((profile: any) => profile.userId),\n );\n\n const batchedEvents: Record<\n \"critical\" | \"high\" | \"normal\" | \"low\",\n Omit<any, \"id\" | \"timestamp\">[]\n > = {\n critical: [],\n high: [],\n normal: [],\n low: [],\n };\n\n for (const profile of profiles) {\n for (const channel of channelsToProcess) {\n const contacts = contactsByUser.get(profile.userId) ?? [];\n const channelContacts = contacts.filter((contact: any) => contact.channel === channel);\n // Push resolves its active tokens at send time so token invalidation\n // remains current. Other channels need one task per address.\n const destinations =\n channel === \"push\" ? [undefined] : channelContacts.map((c: any) => c.target);\n if (destinations.length === 0) {\n this.logger.info(\n { recipientId: profile.userId, channel },\n \"no active contact for channel\",\n );\n continue;\n }\n for (const destination of destinations) {\n const enrichedPayload: NotificationEnrichedPayload = {\n projectId: raw.projectId,\n rawEventId: event.id,\n recipientId: profile.userId,\n channel: channel,\n priority: \"normal\",\n templateId: raw.templateId,\n templateVariables: raw.data,\n aiPrompts: raw.aiPrompts,\n recipient: {\n id: profile.userId,\n email:\n channel === \"email\"\n ? (destination ?? profile.email ?? undefined)\n : (profile.email ?? undefined),\n phone: channel === \"sms\" ? destination : undefined,\n webhook: channel === \"webhook\" ? destination : undefined,\n locale: profile.language ?? \"en\",\n timezone: profile.timezone ?? \"UTC\",\n preferences: {\n // Opted out if the user disabled ANY topic this template carries.\n optedOut: topics.some((t) => profile.preferences.topics?.[t] === false),\n channels: Object.entries(profile.preferences.channels ?? {})\n .filter(([_, enabled]) => !enabled)\n .map(([channel]) => channel as any),\n quietHours: profile.preferences.quietHours,\n },\n },\n scheduledAt: raw.scheduledAt,\n fallbackChain: fallbackChain?.length ? fallbackChain : undefined,\n campaignId: raw.campaignId,\n };\n\n const msgPriority = raw.priority ?? \"normal\";\n const p = getPriorityBucket(msgPriority);\n\n enrichedPayload.priority = msgPriority;\n\n batchedEvents[p].push(\n buildStreamEvent(\n \"notification.enriched\",\n enrichedPayload as Record<string, unknown>,\n \"enricher\",\n event.metadata.traceId,\n ),\n );\n }\n }\n }\n\n for (const p of [\"critical\", \"normal\", \"low\"] as const) {\n if (batchedEvents[p].length > 0) {\n const producer = this.producers[p] ?? this.producers.normal;\n for (const ev of batchedEvents[p]) {\n publishPromises.push(\n new Promise((resolve, reject) => {\n this.eventBuffer.push({ producer, event: ev, resolve, reject });\n }),\n );\n }\n }\n }\n }\n\n this.logger.info(\n {\n messageId: message.id,\n eventId: event.id,\n target: raw.target.type,\n traceId: event.metadata.traceId,\n },\n \"event enriched\",\n );\n } catch (err) {\n throw err;\n }\n\n await Promise.all(publishPromises).catch(async (err) => {\n await this.idempotency.unmark(dedupeId).catch(() => {});\n throw err;\n });\n\n await this.idempotency.markProcessed(dedupeId);\n }\n}\n\nexport async function startEnricherWorker() {\n logger = createLogger({ name: \"enricher\", level: config.LOG_LEVEL });\n redis = new RedisClient({ url: config.REDIS_URL, name: \"enricher\", logger });\n const dbData = createDatabase({ url: config.DATABASE_URL, applicationName: \"enricher\", logger });\n sql = dbData.sql;\n db = dbData.db;\n consumer = new StreamConsumer({\n redis: redis.native,\n stream: INBOUND_STREAMS as unknown as StreamName[],\n group: CONSUMER_GROUPS.ENRICHER,\n consumer: `enricher-${process.pid}`,\n dlqStream: STREAMS.DEAD_LETTER,\n batchSize: config.WORKER_CONCURRENCY,\n logger,\n });\n\n pendingScanner = new PendingMessageScanner({\n redis: redis.native,\n stream: INBOUND_STREAMS as unknown as StreamName[],\n group: CONSUMER_GROUPS.ENRICHER,\n consumer: `enricher-${process.pid}`,\n logger,\n });\n\n const producers = {\n critical: new StreamProducer({\n redis: redis.native,\n stream: STREAMS.ENRICHED_CRITICAL,\n logger,\n }),\n normal: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_NORMAL, logger }),\n low: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_LOW, logger }),\n };\n\n const idempotency = new IdempotencyGuard({\n redis: redis.native,\n keyPrefix: \"notif:processed:enricher\",\n ttlSeconds: 86_400,\n });\n\n const userRepo = new UserRepository(db);\n const prefRepo = new PreferenceRepository(db);\n const contactRepo = new ContactRepository(db);\n const templateCache = new TemplateCache(new TemplateRepository(db));\n\n // ─── Stage 1: Context Enricher ──────────────────────────────────────────────\n //\n // Pipeline:\n // 1. Parse payload as notification.created\n // 2. Idempotency check — drop if already processed\n // 3. Load user profile (language, timezone) from DB\n // 4. Load all stored preferences for this user\n // 5. Publish notification.enriched to ENRICHED stream\n\n // 5. Publish notification.enriched to ENRICHED stream\n\n worker = new EnricherWorker({\n consumer,\n pendingScanner,\n logger,\n maxRetriesBeforeDlq: 5,\n concurrency: config.WORKER_CONCURRENCY,\n producers,\n idempotency,\n userRepo,\n prefRepo,\n contactRepo,\n templateCache,\n });\n\n // ─── Health check interval ──────────────────────────────────────────────────\n\n healthInterval = startHealthReporter(\"enricher\", worker, redis, logger);\n\n logger.info({ env: config.NODE_ENV }, \"enricher starting\");\n await worker.start();\n}\n\n// ─── Shutdown ──────────────────────────────────────────────────────────────\n\nexport async function stopEnricherWorker(): Promise<void> {\n logger?.info(\"shutdown initiated\");\n if (healthInterval) {\n clearInterval(healthInterval);\n healthInterval = null;\n }\n if (worker) await worker.stop();\n if (sql) await sql.end();\n if (redis) await redis.disconnect();\n logger?.info(\"enricher stopped\");\n}\n"],"mappings":";;AAkCA,QAAQ;AACR,MAAM,SAAS,eAAe;AAE9B,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AAEJ,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI,iBAAwC;AAW5C,IAAa,iBAAb,cAAoC,WAAW;CAC7C;CACA;CACA;CACA;CACA;CACA;CAEA,YAKM,CAAC;CACP,aAA4C;CAC5C,cAKM,CAAC;CACP,aAA4C;CAE5C,eAKM,CAAC;CACP,oBAAmD;CAEnD,MAAc,aAAa,WAAmB,SAAgD;EAC5F,OAAO,IAAI,SAAS,SAAS,WAAW;GACtC,KAAK,aAAa,KAAK;IAAE;IAAW;IAAS;IAAS;GAAO,CAAC;GAC9D,IAAI,KAAK,aAAa,UAAU,KAAK;IACnC,IAAI,KAAK,mBAAmB,aAAa,KAAK,iBAAiB;IAC/D,KAAU,kBAAkB;GAC9B,OAAO,IAAI,CAAC,KAAK,mBACf,KAAK,oBAAoB,iBAAiB,KAAK,KAAK,kBAAkB,GAAG,EAAE;EAE/E,CAAC;CACH;CAEA,MAAc,oBAAmC;EAC/C,MAAM,QAAQ,KAAK;EACnB,KAAK,eAAe,CAAC;EACrB,KAAK,oBAAoB;EACzB,IAAI,MAAM,WAAW,GAAG;EAExB,IAAI;GACF,MAAM,4BAAY,IAAI,IAA0B;GAChD,KAAK,MAAM,KAAK,OAAO;IACrB,IAAI,CAAC,UAAU,IAAI,EAAE,SAAS,GAAG,UAAU,IAAI,EAAE,WAAW,CAAC,CAAC;IAC9D,UAAU,IAAI,EAAE,SAA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AO,MAAM;CAEtE,OAAO,KAAK,EAAE,KAAK,OAAO,SAAS,GAAG,mBAAmB;CACzD,MAAM,OAAO,MAAM;AACrB;AAIA,eAAsB,qBAAoC;CACxD,QAAQ,KAAK,oBAAoB;CACjC,IAAI,gBAAgB;EAClB,cAAc,cAAc;EAC5B,iBAAiB;CACnB;CACA,IAAI,QAAQ,MAAM,OAAO,KAAK;CAC9B,IAAI,KAAK,MAAM,IAAI,IAAI;CACvB,IAAI,OAAO,MAAM,MAAM,WAAW;CAClC,QAAQ,KAAK,kBAAkB;AACjC"}
1
+ {"version":3,"file":"main-Ce9dcrsg.mjs","names":[],"sources":["../src/services/enricher/main.ts"],"sourcesContent":["import { loadEnv, readBaseConfig } from \"@/index.js\";\nimport { createLogger } from \"@/index.js\";\nimport { RedisClient } from \"@/index.js\";\nimport {\n StreamConsumer,\n PendingMessageScanner,\n StreamProducer,\n type StreamMessage,\n} from \"@/index.js\";\nimport { BaseWorker } from \"@/index.js\";\nimport {\n STREAMS,\n INBOUND_STREAMS,\n CONSUMER_GROUPS,\n registry,\n buildStreamEvent,\n type NotificationCreatedPayload,\n type NotificationEnrichedPayload,\n} from \"@/index.js\";\nimport { type StreamName } from \"@/contracts/streams.js\";\nimport { IdempotencyGuard } from \"@/index.js\";\nimport { createDatabase } from \"@/db/index.js\";\nimport {\n UserRepository,\n PreferenceRepository,\n TemplateRepository,\n ContactRepository,\n} from \"@/index.js\";\nimport { TemplateCache } from \"@/templates/index.js\";\nimport { getPriorityBucket, type WorkerOptions } from \"@/shared/index.js\";\nimport { startHealthReporter } from \"@/workers/index.js\";\n\n// ─── Bootstrap ─────────────────────────────────────────────────────────────\n\nloadEnv();\nconst config = readBaseConfig();\n\nlet logger: ReturnType<typeof createLogger>;\nlet redis: RedisClient;\nlet sql: any;\nlet db: any;\n\nlet consumer: StreamConsumer;\nlet pendingScanner: PendingMessageScanner;\nlet worker: BaseWorker;\nlet healthInterval: NodeJS.Timeout | null = null;\n\nexport interface EnricherWorkerOptions extends WorkerOptions {\n producers: any;\n idempotency: any;\n userRepo: any;\n prefRepo: any;\n contactRepo: any;\n templateCache: TemplateCache;\n}\n\nexport class EnricherWorker extends BaseWorker {\n private readonly producers: any;\n private readonly idempotency: any;\n private readonly userRepo: any;\n private readonly prefRepo: any;\n private readonly contactRepo: any;\n private readonly templateCache: TemplateCache;\n\n private userBatch: {\n projectId: string;\n userId: string;\n resolve: (p: any) => void;\n reject: (e: any) => void;\n }[] = [];\n private batchTimer: NodeJS.Timeout | null = null;\n private eventBuffer: {\n producer: any;\n event: any;\n resolve: () => void;\n reject: (e: any) => void;\n }[] = [];\n private flushTimer: NodeJS.Timeout | null = null;\n\n private contactBatch: {\n projectId: string;\n userIds: string[];\n resolve: (c: Map<string, any[]>) => void;\n reject: (e: any) => void;\n }[] = [];\n private contactBatchTimer: NodeJS.Timeout | null = null;\n\n private async loadContacts(projectId: string, userIds: string[]): Promise<Map<string, any[]>> {\n return new Promise((resolve, reject) => {\n this.contactBatch.push({ projectId, userIds, resolve, reject });\n if (this.contactBatch.length >= 500) {\n if (this.contactBatchTimer) clearTimeout(this.contactBatchTimer);\n void this.flushContactBatch();\n } else if (!this.contactBatchTimer) {\n this.contactBatchTimer = setTimeout(() => void this.flushContactBatch(), 10);\n }\n });\n }\n\n private async flushContactBatch(): Promise<void> {\n const batch = this.contactBatch;\n this.contactBatch = [];\n this.contactBatchTimer = null;\n if (batch.length === 0) return;\n\n try {\n const byProject = new Map<string, typeof batch>();\n for (const b of batch) {\n if (!byProject.has(b.projectId)) byProject.set(b.projectId, []);\n byProject.get(b.projectId)!.push(b);\n }\n\n for (const [projectId, items] of byProject) {\n const userIds = Array.from(new Set(items.flatMap((i) => i.userIds)));\n const contactsMap = await this.contactRepo.findActiveByUserIds(projectId, userIds);\n for (const item of items) {\n item.resolve(contactsMap);\n }\n }\n } catch (err) {\n for (const b of batch) b.reject(err);\n }\n }\n\n constructor(options: EnricherWorkerOptions) {\n super(options);\n this.producers = options.producers;\n this.idempotency = options.idempotency;\n this.userRepo = options.userRepo;\n this.prefRepo = options.prefRepo;\n this.contactRepo = options.contactRepo;\n this.templateCache = options.templateCache;\n\n this.flushTimer = setInterval(() => void this.flushWorkerBuffers(), 100);\n }\n\n override async stop(): Promise<void> {\n if (this.batchTimer) {\n clearTimeout(this.batchTimer);\n this.batchTimer = null;\n }\n if (this.flushTimer) {\n clearInterval(this.flushTimer);\n this.flushTimer = null;\n }\n if (this.contactBatchTimer) {\n clearTimeout(this.contactBatchTimer);\n this.contactBatchTimer = null;\n }\n await this.flushContactBatch();\n await this.flushUserBatch();\n await this.flushWorkerBuffers();\n await super.stop();\n }\n\n private async flushWorkerBuffers(): Promise<void> {\n if (this.eventBuffer.length === 0) return;\n const events = this.eventBuffer;\n this.eventBuffer = [];\n\n try {\n const byProducer = new Map<any, typeof events>();\n for (const e of events) {\n if (!byProducer.has(e.producer)) byProducer.set(e.producer, []);\n byProducer.get(e.producer)!.push(e);\n }\n for (const [producer, batch] of byProducer) {\n await producer.publishBatch(batch.map((b) => b.event));\n for (const b of batch) b.resolve();\n }\n } catch (err: any) {\n this.logger.error({ err }, \"failed to flush events in EnricherWorker\");\n for (const e of events) e.reject(err);\n }\n }\n\n private async loadUser(projectId: string, userId: string): Promise<any> {\n return new Promise((resolve, reject) => {\n this.userBatch.push({ projectId, userId, resolve, reject });\n if (this.userBatch.length >= 500) {\n if (this.batchTimer) clearTimeout(this.batchTimer);\n void this.flushUserBatch();\n } else if (!this.batchTimer) {\n this.batchTimer = setTimeout(() => {\n void this.flushUserBatch();\n }, 10);\n }\n });\n }\n\n private async flushUserBatch() {\n const batch = this.userBatch;\n this.userBatch = [];\n this.batchTimer = null;\n\n const byProject = new Map<string, typeof batch>();\n for (const b of batch) {\n if (!byProject.has(b.projectId)) byProject.set(b.projectId, []);\n byProject.get(b.projectId)!.push(b);\n }\n\n for (const [projectId, reqs] of byProject.entries()) {\n try {\n const uniqueIds = Array.from(new Set(reqs.map((r) => r.userId)));\n const profiles = await this.userRepo.findRecordsByIds(projectId, uniqueIds);\n const profileMap = new Map(profiles.map((p: any) => [p.userId, p]));\n\n for (const req of reqs) {\n req.resolve(profileMap.get(req.userId) || null);\n }\n } catch (err) {\n for (const req of reqs) req.reject(err);\n }\n }\n }\n\n async process(message: StreamMessage): Promise<void> {\n const { event } = message;\n const publishPromises: Promise<void>[] = [];\n\n let isRequested = true;\n const requestedResult = registry.safeParsePayload(\"notification.requested\", event.payload);\n let createdResult: any = null;\n if (!requestedResult.success) {\n createdResult = registry.safeParsePayload(\"notification.created\", event.payload);\n isRequested = false;\n }\n\n if (!isRequested && (!createdResult || !createdResult.success)) {\n const issues = createdResult\n ? createdResult.error.issues\n : (requestedResult as any).error.issues;\n this.logger.warn({ messageId: message.id, issues }, \"invalid payload — skipping\");\n return;\n }\n\n // Handle legacy notification.created\n if (!isRequested) {\n const raw = createdResult.data as NotificationCreatedPayload;\n const dedupeId = `${raw.projectId}:${raw.idempotencyKey ?? event.id}`;\n if (!(await this.idempotency.checkAndMark(dedupeId, 60))) return;\n try {\n const profile = await this.userRepo.findRecordById(raw.projectId, raw.recipientId);\n if (!profile) return;\n\n const prefs = await this.prefRepo.findByUserId(raw.projectId, raw.recipientId);\n const optedOutTypes = new Set(\n prefs.filter((p: any) => !p.optedIn).map((p: any) => p.eventType),\n );\n\n const enrichedPayload: NotificationEnrichedPayload = {\n projectId: raw.projectId,\n rawEventId: event.id,\n recipientId: raw.recipientId,\n channel: raw.channel,\n priority: raw.priority,\n templateId: raw.templateId,\n templateVariables: raw.payload,\n recipient: {\n id: profile.userId,\n email: profile.email ?? undefined,\n locale: profile.language ?? \"en\",\n timezone: profile.timezone ?? \"UTC\",\n preferences: {\n optedOut:\n optedOutTypes.has(event.type) || profile.preferences.topics?.[event.type] === false,\n channels: Object.entries(profile.preferences.channels ?? {})\n .filter(([_, enabled]) => !enabled)\n .map(([channel]) => channel as any),\n quietHours: profile.preferences.quietHours,\n },\n },\n // No campaignId: this is the legacy `notification.created` path,\n // which predates campaigns and carries no label to attribute to.\n scheduledAt: raw.scheduledAt,\n };\n\n const p = getPriorityBucket(raw.priority);\n const producer = this.producers[p] ?? this.producers[\"normal\"]!;\n\n publishPromises.push(\n new Promise((resolve, reject) => {\n this.eventBuffer.push({\n producer,\n event: buildStreamEvent(\n \"notification.enriched\",\n enrichedPayload as Record<string, unknown>,\n \"enricher\",\n event.metadata.traceId,\n ),\n resolve,\n reject,\n });\n }),\n );\n this.logger.info(\n { messageId: message.id, eventId: event.id, recipientId: raw.recipientId },\n \"event enriched\",\n );\n } catch (err) {\n throw err;\n }\n await Promise.all(publishPromises).catch(async (err) => {\n await this.idempotency.unmark(dedupeId).catch(() => {});\n throw err;\n });\n await this.idempotency.markProcessed(dedupeId);\n return;\n }\n\n // Handle new notification.requested\n const raw = (requestedResult as any).data;\n const dedupeId = `${raw.projectId}:${raw.idempotencyKey ?? event.id}`;\n if (!(await this.idempotency.checkAndMark(dedupeId, 60))) return;\n try {\n let userIds: string[] = [];\n if (raw.target.type === \"user\") {\n userIds = [raw.target.userId];\n } else if (raw.target.type === \"segment\") {\n userIds = await this.userRepo.findUsersBySegment(raw.projectId, raw.target.segment);\n this.logger.info(\n { segment: raw.target.segment, count: userIds.length },\n \"Resolved segment\",\n );\n } else if (raw.target.type === \"topic\") {\n userIds = await this.userRepo.findUsersByTopic(raw.projectId, raw.target.topic);\n this.logger.info({ topic: raw.target.topic, count: userIds.length }, \"Resolved topic\");\n } else {\n this.logger.warn({ target: raw.target }, \"Segment/topic resolution not fully implemented\");\n // Stub: maybe resolve later\n }\n\n const maxUsers = readBaseConfig().SEGMENT_MAX_USERS;\n if (userIds.length > maxUsers) {\n this.logger.error(\n { count: userIds.length, max: maxUsers, projectId: raw.projectId, eventId: event.id },\n \"Segment fan-out exceeds maximum allowed limit\",\n );\n const p = getPriorityBucket(raw.priority ?? \"normal\");\n const producer = this.producers[p] ?? this.producers.normal;\n await producer.publish(\n buildStreamEvent(\n \"notification.failed\",\n {\n projectId: raw.projectId,\n rawEventId: event.id,\n error: `Segment fan-out of ${userIds.length} exceeds limit of ${maxUsers}`,\n },\n \"enricher\",\n event.metadata.traceId,\n ),\n );\n return;\n }\n\n // Topic opt-outs are keyed on the TEMPLATE's topics, not the envelope type.\n // `event.type` here is \"notification.requested\", which no user ever sets a\n // preference against, so keying on it silently disabled every opt-out.\n const template = raw.templateId\n ? await this.templateCache.getCachedTemplate(raw.projectId, raw.templateId)\n : null;\n const topics: string[] = template?.topics ?? [];\n\n const channels =\n raw.channels && raw.channels.length > 0 ? raw.channels : ([\"email\"] as any[]);\n const isFallback = (raw as any).fallback === true;\n\n // If fallback is true, we only emit the first channel, and pass the rest in fallbackChain.\n // If fallback is false, we emit all channels concurrently.\n const channelsToProcess = isFallback ? [channels[0]] : channels;\n const fallbackChain = isFallback ? channels.slice(1) : undefined;\n\n const chunkArray = <T>(arr: T[], size: number) =>\n Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>\n arr.slice(i * size, i * size + size),\n );\n\n const chunks = chunkArray(userIds, 500);\n\n for (const chunk of chunks) {\n const profiles = (\n await Promise.all(chunk.map((id) => this.loadUser(raw.projectId, id)))\n ).filter(Boolean);\n const contactsByUser = await this.loadContacts(\n raw.projectId,\n profiles.map((profile: any) => profile.userId),\n );\n\n const batchedEvents: Record<\n \"critical\" | \"high\" | \"normal\" | \"low\",\n Omit<any, \"id\" | \"timestamp\">[]\n > = {\n critical: [],\n high: [],\n normal: [],\n low: [],\n };\n\n for (const profile of profiles) {\n for (const channel of channelsToProcess) {\n const contacts = contactsByUser.get(profile.userId) ?? [];\n const channelContacts = contacts.filter((contact: any) => contact.channel === channel);\n // Push resolves its active tokens at send time so token invalidation\n // remains current. Other channels need one task per address.\n const destinations =\n channel === \"push\" ? [undefined] : channelContacts.map((c: any) => c.target);\n if (destinations.length === 0) {\n this.logger.info(\n { recipientId: profile.userId, channel },\n \"no active contact for channel\",\n );\n continue;\n }\n for (const destination of destinations) {\n const enrichedPayload: NotificationEnrichedPayload = {\n projectId: raw.projectId,\n rawEventId: event.id,\n recipientId: profile.userId,\n channel: channel,\n priority: \"normal\",\n templateId: raw.templateId,\n templateVariables: raw.data,\n aiPrompts: raw.aiPrompts,\n recipient: {\n id: profile.userId,\n email:\n channel === \"email\"\n ? (destination ?? profile.email ?? undefined)\n : (profile.email ?? undefined),\n phone: channel === \"sms\" ? destination : undefined,\n webhook: channel === \"webhook\" ? destination : undefined,\n locale: profile.language ?? \"en\",\n timezone: profile.timezone ?? \"UTC\",\n preferences: {\n // Opted out if the user disabled ANY topic this template carries.\n optedOut: topics.some((t) => profile.preferences.topics?.[t] === false),\n channels: Object.entries(profile.preferences.channels ?? {})\n .filter(([_, enabled]) => !enabled)\n .map(([channel]) => channel as any),\n quietHours: profile.preferences.quietHours,\n },\n },\n scheduledAt: raw.scheduledAt,\n fallbackChain: fallbackChain?.length ? fallbackChain : undefined,\n campaignId: raw.campaignId,\n };\n\n const msgPriority = raw.priority ?? \"normal\";\n const p = getPriorityBucket(msgPriority);\n\n enrichedPayload.priority = msgPriority;\n\n batchedEvents[p].push(\n buildStreamEvent(\n \"notification.enriched\",\n enrichedPayload as Record<string, unknown>,\n \"enricher\",\n event.metadata.traceId,\n ),\n );\n }\n }\n }\n\n for (const p of [\"critical\", \"normal\", \"low\"] as const) {\n if (batchedEvents[p].length > 0) {\n const producer = this.producers[p] ?? this.producers.normal;\n for (const ev of batchedEvents[p]) {\n publishPromises.push(\n new Promise((resolve, reject) => {\n this.eventBuffer.push({ producer, event: ev, resolve, reject });\n }),\n );\n }\n }\n }\n }\n\n this.logger.info(\n {\n messageId: message.id,\n eventId: event.id,\n target: raw.target.type,\n traceId: event.metadata.traceId,\n },\n \"event enriched\",\n );\n } catch (err) {\n throw err;\n }\n\n await Promise.all(publishPromises).catch(async (err) => {\n await this.idempotency.unmark(dedupeId).catch(() => {});\n throw err;\n });\n\n await this.idempotency.markProcessed(dedupeId);\n }\n}\n\nexport async function startEnricherWorker() {\n logger = createLogger({ name: \"enricher\", level: config.LOG_LEVEL });\n redis = new RedisClient({ url: config.REDIS_URL, name: \"enricher\", logger });\n const dbData = createDatabase({ url: config.DATABASE_URL, applicationName: \"enricher\", logger });\n sql = dbData.sql;\n db = dbData.db;\n consumer = new StreamConsumer({\n redis: redis.native,\n stream: INBOUND_STREAMS as unknown as StreamName[],\n group: CONSUMER_GROUPS.ENRICHER,\n consumer: `enricher-${process.pid}`,\n dlqStream: STREAMS.DEAD_LETTER,\n batchSize: config.WORKER_CONCURRENCY,\n logger,\n });\n\n pendingScanner = new PendingMessageScanner({\n redis: redis.native,\n stream: INBOUND_STREAMS as unknown as StreamName[],\n group: CONSUMER_GROUPS.ENRICHER,\n consumer: `enricher-${process.pid}`,\n logger,\n });\n\n const producers = {\n critical: new StreamProducer({\n redis: redis.native,\n stream: STREAMS.ENRICHED_CRITICAL,\n logger,\n }),\n normal: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_NORMAL, logger }),\n low: new StreamProducer({ redis: redis.native, stream: STREAMS.ENRICHED_LOW, logger }),\n };\n\n const idempotency = new IdempotencyGuard({\n redis: redis.native,\n keyPrefix: \"notif:processed:enricher\",\n ttlSeconds: 86_400,\n });\n\n const userRepo = new UserRepository(db);\n const prefRepo = new PreferenceRepository(db);\n const contactRepo = new ContactRepository(db);\n const templateCache = new TemplateCache(new TemplateRepository(db));\n\n // ─── Stage 1: Context Enricher ──────────────────────────────────────────────\n //\n // Pipeline:\n // 1. Parse payload as notification.created\n // 2. Idempotency check — drop if already processed\n // 3. Load user profile (language, timezone) from DB\n // 4. Load all stored preferences for this user\n // 5. Publish notification.enriched to ENRICHED stream\n\n // 5. Publish notification.enriched to ENRICHED stream\n\n worker = new EnricherWorker({\n consumer,\n pendingScanner,\n logger,\n maxRetriesBeforeDlq: 5,\n concurrency: config.WORKER_CONCURRENCY,\n producers,\n idempotency,\n userRepo,\n prefRepo,\n contactRepo,\n templateCache,\n });\n\n // ─── Health check interval ──────────────────────────────────────────────────\n\n healthInterval = startHealthReporter(\"enricher\", worker, redis, logger);\n\n logger.info({ env: config.NODE_ENV }, \"enricher starting\");\n await worker.start();\n}\n\n// ─── Shutdown ──────────────────────────────────────────────────────────────\n\nexport async function stopEnricherWorker(): Promise<void> {\n logger?.info(\"shutdown initiated\");\n if (healthInterval) {\n clearInterval(healthInterval);\n healthInterval = null;\n }\n if (worker) await worker.stop();\n if (sql) await sql.end();\n if (redis) await redis.disconnect();\n logger?.info(\"enricher stopped\");\n}\n"],"mappings":";;AAkCA,QAAQ;AACR,MAAM,SAAS,eAAe;AAE9B,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AAEJ,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI,iBAAwC;AAW5C,IAAa,iBAAb,cAAoC,WAAW;CAC7C;CACA;CACA;CACA;CACA;CACA;CAEA,YAKM,CAAC;CACP,aAA4C;CAC5C,cAKM,CAAC;CACP,aAA4C;CAE5C,eAKM,CAAC;CACP,oBAAmD;CAEnD,MAAc,aAAa,WAAmB,SAAgD;EAC5F,OAAO,IAAI,SAAS,SAAS,WAAW;GACtC,KAAK,aAAa,KAAK;IAAE;IAAW;IAAS;IAAS;GAAO,CAAC;GAC9D,IAAI,KAAK,aAAa,UAAU,KAAK;IACnC,IAAI,KAAK,mBAAmB,aAAa,KAAK,iBAAiB;IAC/D,KAAU,kBAAkB;GAC9B,OAAO,IAAI,CAAC,KAAK,mBACf,KAAK,oBAAoB,iBAAiB,KAAK,KAAK,kBAAkB,GAAG,EAAE;EAE/E,CAAC;CACH;CAEA,MAAc,oBAAmC;EAC/C,MAAM,QAAQ,KAAK;EACnB,KAAK,eAAe,CAAC;EACrB,KAAK,oBAAoB;EACzB,IAAI,MAAM,WAAW,GAAG;EAExB,IAAI;GACF,MAAM,4BAAY,IAAI,IAA0B;GAChD,KAAK,MAAM,KAAK,OAAO;IACrB,IAAI,CAAC,UAAU,IAAI,EAAE,SAAS,GAAG,UAAU,IAAI,EA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