@claroshq/claros 0.8.3 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/worker/index.js +66 -27
- package/package.json +1 -1
package/assets/worker/index.js
CHANGED
|
@@ -23573,6 +23573,8 @@ __name(assertValidTransition, "assertValidTransition");
|
|
|
23573
23573
|
// ../../packages/core/dist/lifecycle/evaluate.js
|
|
23574
23574
|
function evaluateEventTransition(input) {
|
|
23575
23575
|
const { currentState, activationSatisfied } = input;
|
|
23576
|
+
if (input.countsAsActivity === false && currentState !== "signed_up")
|
|
23577
|
+
return null;
|
|
23576
23578
|
switch (currentState) {
|
|
23577
23579
|
case "signed_up": {
|
|
23578
23580
|
if (!activationSatisfied)
|
|
@@ -23675,6 +23677,14 @@ function isActivationRelevantEvent(eventName, activationEvents) {
|
|
|
23675
23677
|
return activationEvents.includes(eventName);
|
|
23676
23678
|
}
|
|
23677
23679
|
__name(isActivationRelevantEvent, "isActivationRelevantEvent");
|
|
23680
|
+
function isEngagementEvent(eventName, engagementEvents) {
|
|
23681
|
+
if (engagementEvents.length === 0)
|
|
23682
|
+
return true;
|
|
23683
|
+
if (eventName === null)
|
|
23684
|
+
return false;
|
|
23685
|
+
return engagementEvents.includes(eventName);
|
|
23686
|
+
}
|
|
23687
|
+
__name(isEngagementEvent, "isEngagementEvent");
|
|
23678
23688
|
function daysBetween(from, to) {
|
|
23679
23689
|
const ms = to.getTime() - from.getTime();
|
|
23680
23690
|
return Math.max(0, ms / (1e3 * 60 * 60 * 24));
|
|
@@ -23684,6 +23694,7 @@ __name(daysBetween, "daysBetween");
|
|
|
23684
23694
|
// ../../packages/core/dist/lifecycle/constants.js
|
|
23685
23695
|
var LIFECYCLE_DEFAULTS = {
|
|
23686
23696
|
activation_events: [],
|
|
23697
|
+
engagement_events: [],
|
|
23687
23698
|
activation_window_days: 10,
|
|
23688
23699
|
natural_frequency_days: 7,
|
|
23689
23700
|
at_risk_missed_intervals: 2,
|
|
@@ -23697,6 +23708,7 @@ function resolveLifecycleConfig(overrides) {
|
|
|
23697
23708
|
return { ...LIFECYCLE_DEFAULTS };
|
|
23698
23709
|
return {
|
|
23699
23710
|
activation_events: overrides.activation_events ?? LIFECYCLE_DEFAULTS.activation_events,
|
|
23711
|
+
engagement_events: overrides.engagement_events ?? LIFECYCLE_DEFAULTS.engagement_events,
|
|
23700
23712
|
activation_window_days: overrides.activation_window_days ?? LIFECYCLE_DEFAULTS.activation_window_days,
|
|
23701
23713
|
natural_frequency_days: overrides.natural_frequency_days ?? LIFECYCLE_DEFAULTS.natural_frequency_days,
|
|
23702
23714
|
at_risk_missed_intervals: overrides.at_risk_missed_intervals ?? LIFECYCLE_DEFAULTS.at_risk_missed_intervals,
|
|
@@ -29844,6 +29856,8 @@ var textRuleSchema = external_exports.union([
|
|
|
29844
29856
|
var LOCALE = /^[a-z]{2,3}(-[A-Za-z0-9]{2,8})*$/;
|
|
29845
29857
|
var configLifecycleSchema = external_exports.object({
|
|
29846
29858
|
activation_events: external_exports.array(external_exports.string().min(1).max(200)).max(50).optional(),
|
|
29859
|
+
/** Track events that count as activity; unset or empty = every track and identify. */
|
|
29860
|
+
engagement_events: external_exports.array(external_exports.string().min(1).max(200)).max(50).optional(),
|
|
29847
29861
|
activation_window_days: external_exports.number().int().min(1).max(365).optional(),
|
|
29848
29862
|
natural_frequency_days: external_exports.number().int().min(1).max(365).optional(),
|
|
29849
29863
|
at_risk_missed_intervals: external_exports.number().int().min(1).max(52).optional(),
|
|
@@ -30551,10 +30565,13 @@ async function attemptDedupInsert(tx, tenantId, messageId) {
|
|
|
30551
30565
|
return { deduplicated: false };
|
|
30552
30566
|
}
|
|
30553
30567
|
__name(attemptDedupInsert, "attemptDedupInsert");
|
|
30554
|
-
async function ensureContact(db, tenantId, externalId, now) {
|
|
30568
|
+
async function ensureContact(db, tenantId, externalId, now, activity) {
|
|
30555
30569
|
const existing = await db.select({ id: contacts.id, lifecycleState: contacts.lifecycleState }).from(contacts).where(and(eq(contacts.tenantId, tenantId), eq(contacts.externalId, externalId))).limit(1);
|
|
30556
30570
|
if (existing.length > 0) {
|
|
30557
30571
|
const contact = existing[0];
|
|
30572
|
+
if (!activity) {
|
|
30573
|
+
return { id: contact.id, lifecycleState: contact.lifecycleState };
|
|
30574
|
+
}
|
|
30558
30575
|
await db.update(contacts).set({
|
|
30559
30576
|
lastSeenAt: now,
|
|
30560
30577
|
eventCountBucketCurrent: sql`COALESCE(event_count_bucket_current, 0) + 1`
|
|
@@ -30567,13 +30584,14 @@ async function ensureContact(db, tenantId, externalId, now) {
|
|
|
30567
30584
|
lifecycleState: "signed_up",
|
|
30568
30585
|
firstSeenAt: now,
|
|
30569
30586
|
lastSeenAt: now,
|
|
30570
|
-
eventCountBucketCurrent: 1
|
|
30587
|
+
eventCountBucketCurrent: activity ? 1 : 0
|
|
30571
30588
|
}).onConflictDoUpdate({
|
|
30572
30589
|
target: [contacts.tenantId, contacts.externalId],
|
|
30573
|
-
|
|
30590
|
+
// Not activity: a no-op update, so the row is still returned.
|
|
30591
|
+
set: activity ? {
|
|
30574
30592
|
lastSeenAt: now,
|
|
30575
30593
|
eventCountBucketCurrent: sql`COALESCE("contacts"."event_count_bucket_current", 0) + 1`
|
|
30576
|
-
}
|
|
30594
|
+
} : { lastSeenAt: sql`"contacts"."last_seen_at"` }
|
|
30577
30595
|
}).returning({ id: contacts.id, lifecycleState: contacts.lifecycleState });
|
|
30578
30596
|
return {
|
|
30579
30597
|
id: result[0].id,
|
|
@@ -30581,14 +30599,14 @@ async function ensureContact(db, tenantId, externalId, now) {
|
|
|
30581
30599
|
};
|
|
30582
30600
|
}
|
|
30583
30601
|
__name(ensureContact, "ensureContact");
|
|
30584
|
-
async function upsertContactWithTraits(db, tenantId, externalId, traitPlan, now) {
|
|
30602
|
+
async function upsertContactWithTraits(db, tenantId, externalId, traitPlan, now, activity) {
|
|
30585
30603
|
if (!traitPlan) {
|
|
30586
|
-
const { id: contactId2, lifecycleState: lifecycleState2 } = await ensureContact(db, tenantId, externalId, now);
|
|
30604
|
+
const { id: contactId2, lifecycleState: lifecycleState2 } = await ensureContact(db, tenantId, externalId, now, activity);
|
|
30587
30605
|
return { contactId: contactId2, lifecycleState: lifecycleState2, conflicts: [] };
|
|
30588
30606
|
}
|
|
30589
30607
|
const { columnUpdates, propsToMerge, keysToRemove } = traitPlan;
|
|
30590
30608
|
const conflicts = [...traitPlan.conflicts];
|
|
30591
|
-
const { id: contactId, lifecycleState } = await ensureContact(db, tenantId, externalId, now);
|
|
30609
|
+
const { id: contactId, lifecycleState } = await ensureContact(db, tenantId, externalId, now, activity);
|
|
30592
30610
|
const hasEmail = "email" in columnUpdates;
|
|
30593
30611
|
const emailValue = columnUpdates.email;
|
|
30594
30612
|
let emailConflict = false;
|
|
@@ -30675,13 +30693,12 @@ async function recordConflicts(db, tenantId, contactId, eventId, conflicts) {
|
|
|
30675
30693
|
})));
|
|
30676
30694
|
}
|
|
30677
30695
|
__name(recordConflicts, "recordConflicts");
|
|
30678
|
-
async function evaluateAndApplyTransition(db, tenantId, contactId, currentState, eventName, eventId, now) {
|
|
30696
|
+
async function evaluateAndApplyTransition(db, tenantId, contactId, currentState, eventName, eventId, now, config) {
|
|
30679
30697
|
if (currentState === "engaged" || currentState === "resurrected") {
|
|
30680
30698
|
return null;
|
|
30681
30699
|
}
|
|
30682
30700
|
let activationSatisfied = false;
|
|
30683
30701
|
if (currentState === "signed_up") {
|
|
30684
|
-
const config = await loadLifecycleConfig(db, tenantId);
|
|
30685
30702
|
if (config.activation_events.length === 0) {
|
|
30686
30703
|
return null;
|
|
30687
30704
|
}
|
|
@@ -30700,7 +30717,8 @@ async function evaluateAndApplyTransition(db, tenantId, contactId, currentState,
|
|
|
30700
30717
|
const transition = evaluateEventTransition({
|
|
30701
30718
|
currentState,
|
|
30702
30719
|
eventName,
|
|
30703
|
-
activationSatisfied
|
|
30720
|
+
activationSatisfied,
|
|
30721
|
+
countsAsActivity: isEngagementEvent(eventName, config.engagement_events)
|
|
30704
30722
|
});
|
|
30705
30723
|
if (transition === null) {
|
|
30706
30724
|
return null;
|
|
@@ -30744,7 +30762,9 @@ function pgApiIngest(db) {
|
|
|
30744
30762
|
if (deduplicated) {
|
|
30745
30763
|
return { deduplicated: true };
|
|
30746
30764
|
}
|
|
30747
|
-
const
|
|
30765
|
+
const config2 = await loadLifecycleConfig(tx, tenantId);
|
|
30766
|
+
const activity2 = isEngagementEvent(eventName, config2.engagement_events);
|
|
30767
|
+
const { id: contactId2, lifecycleState: lifecycleState2 } = await ensureContact(tx, tenantId, userId, now, activity2);
|
|
30748
30768
|
const [insertedEvent2] = await tx.insert(events).values({
|
|
30749
30769
|
tenantId,
|
|
30750
30770
|
contactId: contactId2,
|
|
@@ -30755,12 +30775,14 @@ function pgApiIngest(db) {
|
|
|
30755
30775
|
messageId: messageId ?? null,
|
|
30756
30776
|
timestamp: eventTimestamp
|
|
30757
30777
|
}).returning({ id: events.id });
|
|
30758
|
-
await evaluateAndApplyTransition(tx, tenantId, contactId2, lifecycleState2, eventName, insertedEvent2.id, now);
|
|
30778
|
+
await evaluateAndApplyTransition(tx, tenantId, contactId2, lifecycleState2, eventName, insertedEvent2.id, now, config2);
|
|
30759
30779
|
return { deduplicated: false, contactId: contactId2 };
|
|
30760
30780
|
});
|
|
30761
30781
|
return result;
|
|
30762
30782
|
}
|
|
30763
|
-
const
|
|
30783
|
+
const config = await loadLifecycleConfig(db, tenantId);
|
|
30784
|
+
const activity = isEngagementEvent(eventName, config.engagement_events);
|
|
30785
|
+
const { id: contactId, lifecycleState } = await ensureContact(db, tenantId, userId, now, activity);
|
|
30764
30786
|
const [insertedEvent] = await db.insert(events).values({
|
|
30765
30787
|
tenantId,
|
|
30766
30788
|
contactId,
|
|
@@ -30771,7 +30793,7 @@ function pgApiIngest(db) {
|
|
|
30771
30793
|
messageId: messageId ?? null,
|
|
30772
30794
|
timestamp: eventTimestamp
|
|
30773
30795
|
}).returning({ id: events.id });
|
|
30774
|
-
await evaluateAndApplyTransition(db, tenantId, contactId, lifecycleState, eventName, insertedEvent.id, now);
|
|
30796
|
+
await evaluateAndApplyTransition(db, tenantId, contactId, lifecycleState, eventName, insertedEvent.id, now, config);
|
|
30775
30797
|
return { deduplicated: false, contactId };
|
|
30776
30798
|
},
|
|
30777
30799
|
async recordIdentifyEvent(input) {
|
|
@@ -30782,7 +30804,9 @@ function pgApiIngest(db) {
|
|
|
30782
30804
|
if (deduplicated) {
|
|
30783
30805
|
return { deduplicated: true };
|
|
30784
30806
|
}
|
|
30785
|
-
const
|
|
30807
|
+
const config2 = await loadLifecycleConfig(tx, tenantId);
|
|
30808
|
+
const activity2 = isEngagementEvent(null, config2.engagement_events);
|
|
30809
|
+
const { contactId: contactId2, lifecycleState: lifecycleState2, conflicts: conflicts2 } = await upsertContactWithTraits(tx, tenantId, userId, traitPlan, now, activity2);
|
|
30786
30810
|
const [insertedEvent2] = await tx.insert(events).values({
|
|
30787
30811
|
tenantId,
|
|
30788
30812
|
contactId: contactId2,
|
|
@@ -30796,12 +30820,14 @@ function pgApiIngest(db) {
|
|
|
30796
30820
|
if (conflicts2.length > 0) {
|
|
30797
30821
|
await recordConflicts(tx, tenantId, contactId2, insertedEvent2.id, conflicts2);
|
|
30798
30822
|
}
|
|
30799
|
-
await evaluateAndApplyTransition(tx, tenantId, contactId2, lifecycleState2, null, insertedEvent2.id, now);
|
|
30823
|
+
await evaluateAndApplyTransition(tx, tenantId, contactId2, lifecycleState2, null, insertedEvent2.id, now, config2);
|
|
30800
30824
|
return { deduplicated: false };
|
|
30801
30825
|
});
|
|
30802
30826
|
return { deduplicated: result.deduplicated };
|
|
30803
30827
|
}
|
|
30804
|
-
const
|
|
30828
|
+
const config = await loadLifecycleConfig(db, tenantId);
|
|
30829
|
+
const activity = isEngagementEvent(null, config.engagement_events);
|
|
30830
|
+
const { contactId, lifecycleState, conflicts } = await upsertContactWithTraits(db, tenantId, userId, traitPlan, now, activity);
|
|
30805
30831
|
const [insertedEvent] = await db.insert(events).values({
|
|
30806
30832
|
tenantId,
|
|
30807
30833
|
contactId,
|
|
@@ -30823,7 +30849,8 @@ function pgApiIngest(db) {
|
|
|
30823
30849
|
null,
|
|
30824
30850
|
// identify events have no event name
|
|
30825
30851
|
insertedEvent.id,
|
|
30826
|
-
now
|
|
30852
|
+
now,
|
|
30853
|
+
config
|
|
30827
30854
|
);
|
|
30828
30855
|
return { deduplicated: false };
|
|
30829
30856
|
},
|
|
@@ -37138,6 +37165,10 @@ function lifecycleConfigFromSettings(settings) {
|
|
|
37138
37165
|
return resolveLifecycleConfig(lifecycleOverrides);
|
|
37139
37166
|
}
|
|
37140
37167
|
__name(lifecycleConfigFromSettings, "lifecycleConfigFromSettings");
|
|
37168
|
+
function countsAsActivity(eventName, config) {
|
|
37169
|
+
return isEngagementEvent(eventName, config.engagement_events);
|
|
37170
|
+
}
|
|
37171
|
+
__name(countsAsActivity, "countsAsActivity");
|
|
37141
37172
|
function activationEventsToRead(currentState, eventName, config) {
|
|
37142
37173
|
if (currentState !== "signed_up")
|
|
37143
37174
|
return [];
|
|
@@ -37163,7 +37194,12 @@ function planIngestTransition(input) {
|
|
|
37163
37194
|
names.add(eventName);
|
|
37164
37195
|
activationSatisfied = checkActivationSatisfied(names, config.activation_events);
|
|
37165
37196
|
}
|
|
37166
|
-
const transition = evaluateEventTransition({
|
|
37197
|
+
const transition = evaluateEventTransition({
|
|
37198
|
+
currentState,
|
|
37199
|
+
eventName,
|
|
37200
|
+
activationSatisfied,
|
|
37201
|
+
countsAsActivity: isEngagementEvent(eventName, config.engagement_events)
|
|
37202
|
+
});
|
|
37167
37203
|
if (transition === null)
|
|
37168
37204
|
return null;
|
|
37169
37205
|
return { from: transition.from, to: transition.to, setActivatedAt: transition.setActivatedAt };
|
|
@@ -37284,7 +37320,7 @@ async function planEvent(db, input) {
|
|
|
37284
37320
|
config,
|
|
37285
37321
|
priorEventNames
|
|
37286
37322
|
});
|
|
37287
|
-
return { currentState, transition };
|
|
37323
|
+
return { currentState, transition, activity: countsAsActivity(input.eventName, config) };
|
|
37288
37324
|
}
|
|
37289
37325
|
__name(planEvent, "planEvent");
|
|
37290
37326
|
function mergedPropertiesSql(propsToMerge, keysToRemove) {
|
|
@@ -37312,7 +37348,7 @@ function mergedPropertiesSql(propsToMerge, keysToRemove) {
|
|
|
37312
37348
|
)`;
|
|
37313
37349
|
}
|
|
37314
37350
|
__name(mergedPropertiesSql, "mergedPropertiesSql");
|
|
37315
|
-
async function writeEvent(db, input, transition) {
|
|
37351
|
+
async function writeEvent(db, input, transition, activity) {
|
|
37316
37352
|
const { tenantId, userId, messageId, now } = input;
|
|
37317
37353
|
const nowMs = now.getTime();
|
|
37318
37354
|
const eventId = crypto.randomUUID();
|
|
@@ -37325,11 +37361,13 @@ async function writeEvent(db, input, transition) {
|
|
|
37325
37361
|
id, tenant_id, external_id, lifecycle_state, first_seen_at, last_seen_at,
|
|
37326
37362
|
event_count_bucket_current, created_at
|
|
37327
37363
|
)
|
|
37328
|
-
SELECT ${crypto.randomUUID()}, ${tenantId}, ${userId}, 'signed_up', ${nowMs}, ${nowMs}, 1, ${nowMs}
|
|
37364
|
+
SELECT ${crypto.randomUUID()}, ${tenantId}, ${userId}, 'signed_up', ${nowMs}, ${nowMs}, ${activity ? 1 : 0}, ${nowMs}
|
|
37329
37365
|
WHERE ${notYetStored}
|
|
37330
|
-
ON CONFLICT (tenant_id, external_id) DO UPDATE SET
|
|
37331
|
-
|
|
37332
|
-
|
|
37366
|
+
ON CONFLICT (tenant_id, external_id) DO UPDATE SET ${activity ? sql`last_seen_at = excluded.last_seen_at,
|
|
37367
|
+
event_count_bucket_current = COALESCE(contacts.event_count_bucket_current, 0) + 1` : (
|
|
37368
|
+
// Not activity: a no-op update, so RETURNING still yields the row.
|
|
37369
|
+
sql`last_seen_at = contacts.last_seen_at`
|
|
37370
|
+
)}
|
|
37333
37371
|
RETURNING id, lifecycle_state
|
|
37334
37372
|
`;
|
|
37335
37373
|
const insertEvent = sql`
|
|
@@ -37407,7 +37445,7 @@ async function recordEvent(db, input) {
|
|
|
37407
37445
|
const planned = await planEvent(db, input);
|
|
37408
37446
|
if (planned === null)
|
|
37409
37447
|
return { deduplicated: true };
|
|
37410
|
-
return writeEvent(db, input, planned.transition);
|
|
37448
|
+
return writeEvent(db, input, planned.transition, planned.activity);
|
|
37411
37449
|
}
|
|
37412
37450
|
__name(recordEvent, "recordEvent");
|
|
37413
37451
|
function d1ApiIngest(db) {
|
|
@@ -51760,7 +51798,8 @@ var FEATURES = [
|
|
|
51760
51798
|
"event_template_variables",
|
|
51761
51799
|
"archived_products",
|
|
51762
51800
|
"value_gate_settings",
|
|
51763
|
-
"webhook_batch_interval"
|
|
51801
|
+
"webhook_batch_interval",
|
|
51802
|
+
"engagement_events"
|
|
51764
51803
|
];
|
|
51765
51804
|
var anyCaller = /* @__PURE__ */ __name(async (c2, next) => {
|
|
51766
51805
|
if (c2.var.tenant)
|