@elevasis/core 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.js +9 -2
  3. package/dist/organization-model/index.d.ts +1 -1
  4. package/dist/organization-model/index.js +9 -2
  5. package/dist/test-utils/index.d.ts +463 -377
  6. package/dist/test-utils/index.js +9 -2
  7. package/package.json +1 -1
  8. package/src/_gen/__tests__/__snapshots__/contracts.md.snap +2324 -0
  9. package/src/business/acquisition/activity-events.test.ts +250 -0
  10. package/src/business/acquisition/activity-events.ts +7 -65
  11. package/src/business/acquisition/api-schemas.test.ts +1180 -0
  12. package/src/business/acquisition/api-schemas.ts +1075 -859
  13. package/src/business/acquisition/crm-state-actions.test.ts +160 -0
  14. package/src/business/acquisition/derive-actions.test.ts +518 -0
  15. package/src/business/acquisition/derive-actions.ts +103 -90
  16. package/src/business/acquisition/index.ts +149 -111
  17. package/src/business/acquisition/stateful.ts +30 -0
  18. package/src/business/acquisition/types.ts +44 -77
  19. package/src/execution/engine/index.ts +437 -434
  20. package/src/execution/engine/tools/integration/server/adapters/attio/__tests__/attio-crud.integration.test.ts +363 -360
  21. package/src/execution/engine/tools/integration/server/adapters/attio/fetch/get-record/index.test.ts +162 -186
  22. package/src/execution/engine/tools/integration/server/adapters/attio/fetch/list-records/index.test.ts +316 -338
  23. package/src/execution/engine/tools/integration/server/adapters/gmail/gmail-adapter.ts +204 -210
  24. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/send-email/index.test.ts +88 -0
  25. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/send-email/index.ts +141 -134
  26. package/src/execution/engine/tools/integration/server/adapters/resend/fetch/utils/types.ts +76 -75
  27. package/src/execution/engine/tools/integration/service.test.ts +34 -9
  28. package/src/execution/engine/tools/integration/service.ts +6 -3
  29. package/src/execution/engine/tools/lead-service-types.ts +945 -888
  30. package/src/execution/engine/tools/platform/acquisition/types.ts +266 -260
  31. package/src/execution/engine/tools/registry.ts +701 -699
  32. package/src/execution/engine/tools/tool-maps.ts +816 -791
  33. package/src/execution/engine/workflow/types.ts +11 -0
  34. package/src/organization-model/contracts.ts +4 -4
  35. package/src/organization-model/domains/navigation.ts +62 -62
  36. package/src/organization-model/domains/sales.ts +272 -0
  37. package/src/organization-model/published.ts +21 -21
  38. package/src/organization-model/resolve.ts +21 -8
  39. package/src/platform/constants/versions.ts +1 -1
  40. package/src/reference/_generated/contracts.md +2324 -0
  41. package/src/supabase/database.types.ts +2958 -2886
@@ -0,0 +1,250 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { ActivityEventSchema } from './activity-events'
3
+ import type { ActivityEvent } from './activity-events'
4
+
5
+ const TIMESTAMP = '2026-04-28T12:00:00.000Z'
6
+
7
+ function makeEvent<T extends ActivityEvent['type']>(
8
+ type: T,
9
+ overrides: Record<string, unknown> = {}
10
+ ): Record<string, unknown> {
11
+ const bases: Record<string, Record<string, unknown>> = {
12
+ stage_change: { type, timestamp: TIMESTAMP, stageBefore: 'interested', stageAfter: 'proposal' },
13
+ state_change: { type, timestamp: TIMESTAMP, stateBefore: null, stateAfter: 'discovery_replied' },
14
+ action_taken: { type, timestamp: TIMESTAMP, actionKey: 'send_link' },
15
+ approval_created: { type, timestamp: TIMESTAMP, commandId: 'cmd_abc' },
16
+ approval_resolved: { type, timestamp: TIMESTAMP, commandId: 'cmd_abc', resolution: 'superseded' },
17
+ approval_stale: { type, timestamp: TIMESTAMP, commandId: 'cmd_abc' },
18
+ task_created: { type, timestamp: TIMESTAMP, taskId: 'task_001' },
19
+ deal_created: { type, timestamp: TIMESTAMP }
20
+ }
21
+ return { ...bases[type], ...overrides }
22
+ }
23
+
24
+ function without(event: Record<string, unknown>, key: string): Record<string, unknown> {
25
+ const copy = { ...event }
26
+ delete copy[key]
27
+ return copy
28
+ }
29
+
30
+ describe('ActivityEventSchema', () => {
31
+ describe('positive cases — all 8 variants with minimal-valid payloads', () => {
32
+ const variants: ActivityEvent['type'][] = [
33
+ 'stage_change',
34
+ 'state_change',
35
+ 'action_taken',
36
+ 'approval_created',
37
+ 'approval_resolved',
38
+ 'approval_stale',
39
+ 'task_created',
40
+ 'deal_created'
41
+ ]
42
+
43
+ it.each(variants)('accepts minimal valid %s event', (type) => {
44
+ expect(ActivityEventSchema.safeParse(makeEvent(type)).success).toBe(true)
45
+ })
46
+ })
47
+
48
+ describe('optional fields', () => {
49
+ it('accepts stage_change without reason', () => {
50
+ expect(ActivityEventSchema.safeParse(makeEvent('stage_change')).success).toBe(true)
51
+ })
52
+
53
+ it('accepts stage_change with reason', () => {
54
+ expect(ActivityEventSchema.safeParse(makeEvent('stage_change', { reason: 'qualified' })).success).toBe(true)
55
+ })
56
+
57
+ it('accepts state_change without reason', () => {
58
+ expect(ActivityEventSchema.safeParse(makeEvent('state_change')).success).toBe(true)
59
+ })
60
+
61
+ it('accepts state_change with reason', () => {
62
+ expect(ActivityEventSchema.safeParse(makeEvent('state_change', { reason: 'manual override' })).success).toBe(true)
63
+ })
64
+
65
+ it('accepts action_taken without payload', () => {
66
+ expect(ActivityEventSchema.safeParse(makeEvent('action_taken')).success).toBe(true)
67
+ })
68
+
69
+ it('accepts action_taken with payload', () => {
70
+ expect(
71
+ ActivityEventSchema.safeParse(makeEvent('action_taken', { payload: { channel: 'email', count: 3 } })).success
72
+ ).toBe(true)
73
+ })
74
+
75
+ it('accepts approval_created without dealStageBefore and dealStageAfter', () => {
76
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_created')).success).toBe(true)
77
+ })
78
+
79
+ it('accepts approval_created with both stage fields', () => {
80
+ expect(
81
+ ActivityEventSchema.safeParse(
82
+ makeEvent('approval_created', { dealStageBefore: 'interested', dealStageAfter: 'proposal' })
83
+ ).success
84
+ ).toBe(true)
85
+ })
86
+
87
+ it('accepts approval_resolved without originResourceType', () => {
88
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved')).success).toBe(true)
89
+ })
90
+
91
+ it('accepts approval_resolved with originResourceType', () => {
92
+ expect(
93
+ ActivityEventSchema.safeParse(makeEvent('approval_resolved', { originResourceType: 'crm_action' })).success
94
+ ).toBe(true)
95
+ })
96
+
97
+ it('accepts approval_stale without dealStageBefore and dealStageAfter', () => {
98
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_stale')).success).toBe(true)
99
+ })
100
+
101
+ it('accepts approval_stale with both stage fields', () => {
102
+ expect(
103
+ ActivityEventSchema.safeParse(
104
+ makeEvent('approval_stale', { dealStageBefore: 'interested', dealStageAfter: 'proposal' })
105
+ ).success
106
+ ).toBe(true)
107
+ })
108
+ })
109
+
110
+ describe('state_change nullable fields', () => {
111
+ it('accepts stateBefore: null and stateAfter: null', () => {
112
+ expect(
113
+ ActivityEventSchema.safeParse(makeEvent('state_change', { stateBefore: null, stateAfter: null })).success
114
+ ).toBe(true)
115
+ })
116
+
117
+ it('accepts stateBefore as string and stateAfter as string', () => {
118
+ expect(
119
+ ActivityEventSchema.safeParse(
120
+ makeEvent('state_change', { stateBefore: 'discovery_link_sent', stateAfter: 'discovery_replied' })
121
+ ).success
122
+ ).toBe(true)
123
+ })
124
+ })
125
+
126
+ describe('required-field enforcement', () => {
127
+ it('rejects stage_change missing stageBefore', () => {
128
+ expect(ActivityEventSchema.safeParse(without(makeEvent('stage_change'), 'stageBefore')).success).toBe(false)
129
+ })
130
+
131
+ it('rejects stage_change missing stageAfter', () => {
132
+ expect(ActivityEventSchema.safeParse(without(makeEvent('stage_change'), 'stageAfter')).success).toBe(false)
133
+ })
134
+
135
+ it('rejects stage_change missing timestamp', () => {
136
+ expect(ActivityEventSchema.safeParse(without(makeEvent('stage_change'), 'timestamp')).success).toBe(false)
137
+ })
138
+
139
+ it('rejects state_change missing stateBefore', () => {
140
+ expect(ActivityEventSchema.safeParse(without(makeEvent('state_change'), 'stateBefore')).success).toBe(false)
141
+ })
142
+
143
+ it('rejects state_change missing stateAfter', () => {
144
+ expect(ActivityEventSchema.safeParse(without(makeEvent('state_change'), 'stateAfter')).success).toBe(false)
145
+ })
146
+
147
+ it('rejects action_taken missing actionKey', () => {
148
+ expect(ActivityEventSchema.safeParse(without(makeEvent('action_taken'), 'actionKey')).success).toBe(false)
149
+ })
150
+
151
+ it('rejects approval_created missing commandId', () => {
152
+ expect(ActivityEventSchema.safeParse(without(makeEvent('approval_created'), 'commandId')).success).toBe(false)
153
+ })
154
+
155
+ it('rejects approval_resolved missing commandId', () => {
156
+ expect(ActivityEventSchema.safeParse(without(makeEvent('approval_resolved'), 'commandId')).success).toBe(false)
157
+ })
158
+
159
+ it('rejects approval_resolved missing resolution', () => {
160
+ expect(ActivityEventSchema.safeParse(without(makeEvent('approval_resolved'), 'resolution')).success).toBe(false)
161
+ })
162
+
163
+ it('rejects approval_stale missing commandId', () => {
164
+ expect(ActivityEventSchema.safeParse(without(makeEvent('approval_stale'), 'commandId')).success).toBe(false)
165
+ })
166
+
167
+ it('rejects task_created missing taskId', () => {
168
+ expect(ActivityEventSchema.safeParse(without(makeEvent('task_created'), 'taskId')).success).toBe(false)
169
+ })
170
+ })
171
+
172
+ describe('approval_resolved enum constraint', () => {
173
+ it('accepts resolution: superseded', () => {
174
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved', { resolution: 'superseded' })).success).toBe(
175
+ true
176
+ )
177
+ })
178
+
179
+ it('rejects resolution: approved', () => {
180
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved', { resolution: 'approved' })).success).toBe(
181
+ false
182
+ )
183
+ })
184
+
185
+ it('rejects resolution: rejected', () => {
186
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved', { resolution: 'rejected' })).success).toBe(
187
+ false
188
+ )
189
+ })
190
+
191
+ it('rejects resolution: pending', () => {
192
+ expect(ActivityEventSchema.safeParse(makeEvent('approval_resolved', { resolution: 'pending' })).success).toBe(
193
+ false
194
+ )
195
+ })
196
+ })
197
+
198
+ describe('timestamp validation', () => {
199
+ it('accepts a full ISO datetime string', () => {
200
+ expect(ActivityEventSchema.safeParse(makeEvent('deal_created', { timestamp: TIMESTAMP })).success).toBe(true)
201
+ })
202
+
203
+ it('rejects a non-date string', () => {
204
+ expect(ActivityEventSchema.safeParse(makeEvent('deal_created', { timestamp: 'not-a-date' })).success).toBe(false)
205
+ })
206
+
207
+ it('rejects a date-only string without time component', () => {
208
+ expect(ActivityEventSchema.safeParse(makeEvent('deal_created', { timestamp: '2026-04-28' })).success).toBe(false)
209
+ })
210
+
211
+ it('rejects an empty timestamp string', () => {
212
+ expect(ActivityEventSchema.safeParse(makeEvent('deal_created', { timestamp: '' })).success).toBe(false)
213
+ })
214
+ })
215
+
216
+ describe('discriminator strictness', () => {
217
+ it('rejects an unknown type value', () => {
218
+ expect(ActivityEventSchema.safeParse({ type: 'booking_nudge_sent', timestamp: TIMESTAMP }).success).toBe(false)
219
+ })
220
+
221
+ it('rejects a payload with no type field', () => {
222
+ expect(ActivityEventSchema.safeParse({ timestamp: TIMESTAMP }).success).toBe(false)
223
+ })
224
+
225
+ it('rejects a payload where type is null', () => {
226
+ expect(ActivityEventSchema.safeParse({ type: null, timestamp: TIMESTAMP }).success).toBe(false)
227
+ })
228
+
229
+ it('rejects a completely empty object', () => {
230
+ expect(ActivityEventSchema.safeParse({}).success).toBe(false)
231
+ })
232
+ })
233
+
234
+ describe('round-trip serialization', () => {
235
+ it.each([
236
+ 'stage_change',
237
+ 'state_change',
238
+ 'action_taken',
239
+ 'approval_created',
240
+ 'approval_resolved',
241
+ 'approval_stale',
242
+ 'task_created',
243
+ 'deal_created'
244
+ ] as const)('round-trips %s through JSON.stringify → JSON.parse', (type) => {
245
+ const original = makeEvent(type)
246
+ const parsed = JSON.parse(JSON.stringify(original))
247
+ expect(ActivityEventSchema.safeParse(parsed).success).toBe(true)
248
+ })
249
+ })
250
+ })
@@ -1,7 +1,11 @@
1
1
  import { z } from 'zod'
2
2
 
3
3
  // ---------------------------------------------------------------------------
4
- // Individual event member schemas
4
+ // Platform event member schemas (8 members — closed union, stays in @repo/core)
5
+ //
6
+ // Domain-specific events (reply_received, booking_nudge_sent, etc.) live in
7
+ // external/elevasis/operations as CrmDomainActivityEventSchema.
8
+ // See Open Decision #5 in crm-action-system.mdx for rationale.
5
9
  // ---------------------------------------------------------------------------
6
10
 
7
11
  const StageChangeEventSchema = z.object({
@@ -62,61 +66,8 @@ const DealCreatedEventSchema = z.object({
62
66
  timestamp: z.string().datetime()
63
67
  })
64
68
 
65
- const ReplyReceivedEventSchema = z.object({
66
- type: z.literal('reply_received'),
67
- timestamp: z.string().datetime(),
68
- messageId: z.string().optional(),
69
- source: z.string().optional()
70
- })
71
-
72
- const ReplySentToLeadEventSchema = z.object({
73
- type: z.literal('reply_sent_to_lead'),
74
- timestamp: z.string().datetime(),
75
- messageId: z.string().optional(),
76
- source: z.string().optional()
77
- })
78
-
79
- const BookingNudgeSentEventSchema = z.object({
80
- type: z.literal('booking_nudge_sent'),
81
- timestamp: z.string().datetime(),
82
- followupDay: z.number()
83
- })
84
-
85
- const ReminderSentEventSchema = z.object({
86
- type: z.literal('reminder_sent'),
87
- timestamp: z.string().datetime(),
88
- followupDay: z.number().optional()
89
- })
90
-
91
- const BookingCancelledEventSchema = z.object({
92
- type: z.literal('booking_cancelled'),
93
- timestamp: z.string().datetime(),
94
- reason: z.string().optional()
95
- })
96
-
97
- const DiscoverySubmittedEventSchema = z.object({
98
- type: z.literal('discovery_submitted'),
99
- timestamp: z.string().datetime()
100
- })
101
-
102
- const MovedToNurturingEventSchema = z.object({
103
- type: z.literal('moved_to_nurturing'),
104
- timestamp: z.string().datetime()
105
- })
106
-
107
- const NoShowEventSchema = z.object({
108
- type: z.literal('no_show'),
109
- timestamp: z.string().datetime()
110
- })
111
-
112
- const FollowupEmailSentEventSchema = z.object({
113
- type: z.literal('followup_email_sent'),
114
- timestamp: z.string().datetime(),
115
- followupDay: z.number().optional()
116
- })
117
-
118
69
  // ---------------------------------------------------------------------------
119
- // Union
70
+ // Union — closed 8-member platform union
120
71
  // ---------------------------------------------------------------------------
121
72
 
122
73
  export const ActivityEventSchema = z.discriminatedUnion('type', [
@@ -127,16 +78,7 @@ export const ActivityEventSchema = z.discriminatedUnion('type', [
127
78
  ApprovalResolvedEventSchema,
128
79
  ApprovalStaleEventSchema,
129
80
  TaskCreatedEventSchema,
130
- DealCreatedEventSchema,
131
- ReplyReceivedEventSchema,
132
- ReplySentToLeadEventSchema,
133
- BookingNudgeSentEventSchema,
134
- ReminderSentEventSchema,
135
- BookingCancelledEventSchema,
136
- DiscoverySubmittedEventSchema,
137
- MovedToNurturingEventSchema,
138
- NoShowEventSchema,
139
- FollowupEmailSentEventSchema
81
+ DealCreatedEventSchema
140
82
  ])
141
83
 
142
84
  export type ActivityEvent = z.infer<typeof ActivityEventSchema>