@periskope/types 0.6.423 → 0.6.425

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,1296 +1,1296 @@
1
- import { Merge, OverrideProperties } from 'type-fest';
2
- import { MessageType } from './index';
3
- import { AppendTypes } from './rules.types';
4
- import { Tables } from './supabase.types';
5
- import { TaskType } from './types';
6
-
7
- /***************************** WORKFLOWS *****************************/
8
-
9
- export type WorkflowActionTypes =
10
- | NormalWorkflowActionTypes
11
- | SplitPathWorkflowAction;
12
-
13
- export type WorkflowActionCategory =
14
- | 'search'
15
- | 'message'
16
- | 'ticket'
17
- | 'chat'
18
- | 'custom'
19
- | 'flow'
20
- | 'ai';
21
-
22
- export type SendMessageToChatActionReturnInfo = {
23
- message_details: {
24
- queue_id: string;
25
- queue_position: number;
26
- };
27
- };
28
-
29
- export type CreateTicketActionReturnInfo = {
30
- ticket_details: Tables<'tbl_chat_tickets'>;
31
- };
32
-
33
- export type AssignChatActionReturnInfo = {
34
- chat_details: {
35
- chat_id: string;
36
- assigned_to: string;
37
- org_id: string;
38
- };
39
- };
40
-
41
- export type AddChatLabelActionReturnInfo = {
42
- chat_details: {
43
- chat_id: string;
44
- org_id: string;
45
- labels: string[];
46
- };
47
- };
48
-
49
- export type NotifyHTTPActionReturnInfo = {
50
- status: number | 'Unknown';
51
- data: Record<string, unknown>;
52
- error: any;
53
- };
54
-
55
- export type ForwardMessageActionReturnInfo = {
56
- [key: string]: boolean;
57
- };
58
-
59
- export type DeleteMessageActionReturnInfo = {};
60
-
61
- export type AssignTicketActionReturnInfo = Tables<'tbl_chat_tickets'>;
62
- export type CloseTicketActionReturnInfo = Tables<'tbl_chat_tickets'>;
63
-
64
- export type AIPromptActionReturnInfo = {
65
- query: string;
66
- input: string;
67
- output: string | Record<string, unknown> | boolean | null;
68
- };
69
-
70
- export type FlagMessageActionReturnInfo = {
71
- message_details: {
72
- message_id: string;
73
- chat_id: string;
74
- org_id: string;
75
- org_phone: string;
76
- };
77
- };
78
-
79
- export type CloseChatActionReturnInfo = {
80
- chat_details: {
81
- chat_id: string;
82
- org_id: string;
83
- };
84
- };
85
-
86
- export type RemoveChatLabelActionReturnInfo = {
87
- chat_details: {
88
- chat_id: string;
89
- org_id: string;
90
- labels: string[];
91
- };
92
- };
93
-
94
- export type UnflagMessageActionReturnInfo = {
95
- message_details: {
96
- message_id: string;
97
- chat_id: string;
98
- org_id: string;
99
- org_phone: string;
100
- };
101
- };
102
-
103
- export type AddTicketLabelActionReturnInfo = {
104
- ticket_details: {
105
- ticket_id: string;
106
- org_id: string;
107
- labels: string[];
108
- };
109
- };
110
-
111
- export type RemoveTicketLabelActionReturnInfo = {
112
- ticket_details: {
113
- ticket_id: string;
114
- org_id: string;
115
- labels: string[];
116
- };
117
- };
118
-
119
- export type SendEmailActionReturnInfo = {
120
- email_details: {
121
- email: string;
122
- subject: string;
123
- body: string;
124
- };
125
- };
126
-
127
- export type SendSlackNotificationActionReturnInfo = {
128
- slack_details: {
129
- url: string;
130
- slack_payload: string;
131
- res: any;
132
- };
133
- };
134
-
135
- export type ActionInfo = {
136
- send_message_to_chat: SendMessageToChatActionReturnInfo;
137
- create_ticket: CreateTicketActionReturnInfo;
138
- assign_chat: AssignChatActionReturnInfo;
139
- close_chat: CloseChatActionReturnInfo;
140
- add_chat_label: AddChatLabelActionReturnInfo;
141
- remove_chat_label: RemoveChatLabelActionReturnInfo;
142
- notify_http: NotifyHTTPActionReturnInfo;
143
- forward_message: ForwardMessageActionReturnInfo;
144
- delete_message: DeleteMessageActionReturnInfo;
145
- assign_ticket: AssignTicketActionReturnInfo;
146
- close_ticket: CloseTicketActionReturnInfo;
147
- add_ticket_label: AddTicketLabelActionReturnInfo;
148
- remove_ticket_label: RemoveTicketLabelActionReturnInfo;
149
- ai_prompt: AIPromptActionReturnInfo;
150
- flag_message: FlagMessageActionReturnInfo;
151
- unflag_message: UnflagMessageActionReturnInfo;
152
- send_email: SendEmailActionReturnInfo;
153
- send_slack_notification: SendSlackNotificationActionReturnInfo;
154
- };
155
-
156
- // Example workflow type ->
157
-
158
- // const demoWorkflow: WorkflowType = {
159
- // id: 'workflow-1',
160
- // name: 'Urgent Chat Auto-Responder',
161
- // trigger: Triggers.MESSAGE_CREATED,
162
- // trigger_metadata: {
163
- // org_phones: ['phone-1'],
164
- // allow_internal_messages: false,
165
- // first_action_id: 'action-validate',
166
- // },
167
- // actions: [
168
- // {
169
- // id: 'action-validate',
170
- // action_metadata: {
171
- // type: 'validate_action',
172
- // metadata: {
173
- // operator: 'AND',
174
- // conditions: [
175
- // {
176
- // id: 'cond-1',
177
- // variable_name: 'message.text',
178
- // variable_type: 'string',
179
- // condition: 'CONTAINS',
180
- // value: 'urgent',
181
- // },
182
- // {
183
- // id: 'cond-2',
184
- // variable_name: 'chat.assigned',
185
- // variable_type: 'boolean',
186
- // condition: 'IS',
187
- // value: false,
188
- // },
189
- // ],
190
- // },
191
- // },
192
- // next: 'action-add-label',
193
- // },
194
- // {
195
- // id: 'action-add-label',
196
- // action_metadata: {
197
- // type: 'add_chat_label',
198
- // metadata: {
199
- // labels: ['urgent'],
200
- // },
201
- // },
202
- // next: 'action-assign-chat',
203
- // },
204
- // {
205
- // id: 'action-assign-chat',
206
- // action_metadata: {
207
- // type: 'assign_chat',
208
- // metadata: {
209
- // assignee: {
210
- // round_robin: true,
211
- // emails: ['agent1@example.com', 'agent2@example.com'],
212
- // assignee_check_for: 'all',
213
- // },
214
- // },
215
- // },
216
- // next: 'action-delay',
217
- // },
218
- // {
219
- // id: 'action-delay',
220
- // action_metadata: {
221
- // type: 'delay',
222
- // metadata: {
223
- // delay: '5 minutes',
224
- // },
225
- // },
226
- // next: 'action-send-message',
227
- // },
228
- // {
229
- // id: 'action-send-message',
230
- // action_metadata: {
231
- // type: 'send_message_to_chat',
232
- // metadata: {
233
- // message: 'We’re on it!',
234
- // chat_id: 'trigger_chat',
235
- // },
236
- // },
237
- // next: 'action-split',
238
- // },
239
- // {
240
- // id: 'action-split',
241
- // action_metadata: {
242
- // type: 'split_path',
243
- // metadata: {
244
- // paths: [
245
- // {
246
- // id: 'path-flagged',
247
- // condition_action_id: 'action-condition-flagged',
248
- // },
249
- // {
250
- // id: 'path-not-flagged',
251
- // condition_action_id: 'action-condition-not-flagged',
252
- // },
253
- // ],
254
- // },
255
- // },
256
- // {
257
- // id: 'action-condition-flagged',
258
- // action_metadata: {
259
- // type: 'validate_action',
260
- // metadata: {
261
- // operator: 'AND',
262
- // conditions: [
263
- // {
264
- // id: 'cond-flagged',
265
- // variable_name: 'chat.has_flagged_messages',
266
- // variable_type: 'boolean',
267
- // condition: 'IS',
268
- // value: true,
269
- // },
270
- // ],
271
- // },
272
- // },
273
- // next: 'action-create-ticket',
274
- // },
275
- // {
276
- // id: 'action-condition-not-flagged',
277
- // action_metadata: {
278
- // type: 'validate_action',
279
- // metadata: {
280
- // operator: 'AND',
281
- // conditions: [
282
- // {
283
- // id: 'cond-unflagged',
284
- // variable_name: 'chat.has_flagged_messages',
285
- // variable_type: 'boolean',
286
- // condition: 'IS',
287
- // value: false,
288
- // },
289
- // ],
290
- // },
291
- // },
292
- // next: 'action-send-slack',
293
- // },
294
- // {
295
- // id: 'action-create-ticket',
296
- // action_metadata: {
297
- // type: 'create_ticket',
298
- // metadata: {
299
- // subject: 'Urgent Chat Follow-up',
300
- // assignee: {
301
- // email: 'manager@example.com',
302
- // },
303
- // priority: '4',
304
- // status: 'open',
305
- // labels: ['urgent', 'auto-created'],
306
- // due_date: '30 minutes',
307
- // },
308
- // },
309
- // next: '', // End
310
- // },
311
- // {
312
- // id: 'action-send-slack',
313
- // action_metadata: {
314
- // type: 'send_slack_notification',
315
- // metadata: {
316
- // slack_payload: JSON.stringify({
317
- // text: 'An urgent chat was received, no flagged messages.',
318
- // }),
319
- // url: 'https://hooks.slack.com/services/XXX/YYY/ZZZ',
320
- // },
321
- // },
322
- // next: '', // End
323
- // },
324
- // ],
325
- // };
326
-
327
- /***************************** WORKFLOW FE TYPES @harshgour *****************************/
328
- /***************************** WORKFLOWS *****************************/
329
-
330
- export type NormalWorkflowActions = {
331
- id: string;
332
- action_metadata: NormalWorkflowActionTypes;
333
- next: string | null;
334
- };
335
-
336
- export type SplitPathActions = {
337
- id: string;
338
- action_metadata: SplitPathWorkflowAction;
339
- };
340
-
341
- export type WorkflowAction = NormalWorkflowActions | SplitPathActions;
342
-
343
- export type WorkflowType = OverrideProperties<
344
- Tables<'tbl_org_workflows'>,
345
- {
346
- trigger_metadata: {
347
- org_phones: string[];
348
- allow_internal_messages?: boolean;
349
- first_action_id: string | null;
350
- };
351
- trigger: Triggers;
352
- actions: Array<NormalWorkflowActions | SplitPathActions>;
353
- }
354
- >;
355
-
356
- export function isNormalWorkflowAction(
357
- workflow_action:
358
- | WorkflowType['actions'][number]
359
- | {
360
- trigger_metadata?: WorkflowType['trigger_metadata'];
361
- trigger: WorkflowType['trigger'];
362
- }
363
- | {}
364
- ): workflow_action is NormalWorkflowActions {
365
- return (
366
- 'action_metadata' in workflow_action &&
367
- workflow_action?.action_metadata?.type !== 'split_path'
368
- );
369
- }
370
-
371
- export function isSplitPathAction(
372
- workflow_action: WorkflowType['actions'][number]
373
- ): workflow_action is SplitPathActions {
374
- return workflow_action.action_metadata.type === 'split_path';
375
- }
376
-
377
- /***************************** CONDITIONS TYPES *****************************/
378
-
379
- // {
380
- // "operator": "AND",
381
- // "conditions": [
382
- // {
383
- // "id": "cond1",
384
- // "variable_name": "message",
385
- // "variable_type": "string",
386
- // "condition": "CONTAINS",
387
- // "value": "urgent"
388
- // },
389
- // {
390
- // "operator": "OR",
391
- // "conditions": [
392
- // {
393
- // "id": "cond2",
394
- // "variable_name": "priority",
395
- // "variable_type": "number",
396
- // "condition": "GT",
397
- // "value": 2
398
- // },
399
- // {
400
- // "id": "cond3",
401
- // "variable_name": "assigned",
402
- // "variable_type": "boolean",
403
- // "condition": "NKNOWN"
404
- // }
405
- // ]
406
- // }
407
- // ]
408
- // }
409
-
410
- export enum ConditionsNameMap {
411
- // string
412
- CONTAINS = 'CONTAINS',
413
- NCONTAINS = 'NOT CONTAINS',
414
- EQUALS = 'EQUALS',
415
- NEQUALS = 'NOT EQUALS',
416
- STARTS_WITH = 'STARTS WITH',
417
- ENDS_WITH = 'ENDS WITH',
418
-
419
- // number, date, time
420
- GREATER_THAN = 'GREATER THAN',
421
- GREATER_THAN_OR_EQUAL = 'GREATER THAN OR EQUAL',
422
- LESS_THAN = 'LESS THAN',
423
- LESS_THAN_OR_EQUAL = 'LESS THAN OR EQUAL',
424
-
425
- // none
426
- IS_KNOWN = 'IS KNOWN',
427
- IS_UNKNOWN = 'IS UNKNOWN',
428
-
429
- // boolean
430
- IS = 'IS',
431
- IS_NOT = 'IS NOT',
432
-
433
- // array
434
- IS_ANY_OF = 'IS ANY OF',
435
- IS_NOT_ANY_OF = 'IS NOT ANY OF',
436
-
437
- // day
438
- ON = 'ON',
439
- }
440
-
441
- type BaseCondition<T extends Triggers> = {
442
- id: string;
443
- variable_name: keyof WorkflowDataType<T> | null;
444
- };
445
-
446
- type StringCondition = BaseCondition<Triggers> & {
447
- condition:
448
- | 'CONTAINS'
449
- | 'NCONTAINS'
450
- | 'EQUALS'
451
- | 'NEQUALS'
452
- | 'STARTS_WITH'
453
- | 'ENDS_WITH';
454
- value: string;
455
- variable_type: 'string';
456
- };
457
-
458
- type DateTimeNumberCondition = BaseCondition<Triggers> & {
459
- condition:
460
- | 'GREATER_THAN'
461
- | 'GREATER_THAN_OR_EQUAL'
462
- | 'LESS_THAN'
463
- | 'LESS_THAN_OR_EQUAL';
464
- } & (
465
- | {
466
- value: string;
467
- variable_type: 'number';
468
- }
469
- | {
470
- value: string;
471
- timezone: string;
472
- variable_type: 'date' | 'time';
473
- }
474
- );
475
-
476
- type ExistsCondition = BaseCondition<Triggers> & {
477
- condition: 'IS_KNOWN' | 'IS_UNKNOWN';
478
- value: undefined | null;
479
- variable_type: null;
480
- };
481
-
482
- type OnCondition = BaseCondition<Triggers> & {
483
- condition: 'ON';
484
- value: string | string[];
485
- timezone: string;
486
- variable_type: 'day';
487
- };
488
-
489
- type BooleanCondition = BaseCondition<Triggers> & {
490
- condition: 'IS' | 'IS_NOT';
491
- value: boolean | 'true' | 'false';
492
- variable_type: 'boolean';
493
- };
494
-
495
- type ArrayCondition = BaseCondition<Triggers> & {
496
- condition: 'IS_ANY_OF' | 'IS_NOT_ANY_OF';
497
- value: string[];
498
- variable_type: 'array';
499
- };
500
-
501
- export type ConditionLeaf =
502
- | StringCondition
503
- | DateTimeNumberCondition
504
- | ExistsCondition
505
- | OnCondition
506
- | BooleanCondition
507
- | ArrayCondition;
508
-
509
- type ConditionOperator = 'AND' | 'OR';
510
-
511
- export type SubConditions = {
512
- operator: ConditionOperator;
513
- conditions: Array<ConditionLeaf>;
514
- };
515
-
516
- export type WorkflowConditionGroup = {
517
- operator: ConditionOperator;
518
- conditions: Array<SubConditions>;
519
- };
520
-
521
- export function isConditionLeaf(
522
- condition: ConditionLeaf | SubConditions
523
- ): condition is ConditionLeaf {
524
- return 'id' in condition && !('operator' in condition);
525
- }
526
-
527
- /***************************** WORKFLOW DATA TYPES *****************************/
528
-
529
- export type RawDataTypes = {
530
- message: Tables<'tbl_chat_messages'>;
531
- reaction: Tables<'tbl_chat_reactions'>;
532
- chat: Tables<'tbl_chats'>;
533
- ticket: Tables<'tbl_chat_tickets'>;
534
- task: Tables<'tbl_org_tasks'>;
535
- sender: Tables<'tbl_contacts'>;
536
- chat_label: Tables<'tbl_chat_properties'>;
537
- };
538
-
539
- type MessageRawDataType = Merge<
540
- Pick<
541
- Tables<'tbl_chat_messages'>,
542
- | 'body'
543
- | 'flag_status'
544
- | 'has_quoted_msg'
545
- | 'message_id'
546
- | 'message_ticket_id'
547
- | 'message_type'
548
- | 'performed_by'
549
- | 'sender_phone'
550
- | 'timestamp'
551
- | 'media'
552
- >,
553
- {
554
- media: string | null;
555
- flag_status: boolean;
556
- has_quoted_msg: boolean;
557
- message_type: MessageType['message_type'];
558
- is_latest_message: boolean;
559
- }
560
- >;
561
-
562
- type ReactionRawDataType = OverrideProperties<
563
- Pick<Tables<'tbl_chat_reactions'>, 'reaction' | 'sender_id' | 'reaction_id'>,
564
- {}
565
- >;
566
-
567
- type SenderRawDataType = Pick<
568
- Merge<Tables<'tbl_contacts'>, Tables<'tbl_chat_participants'>>,
569
- | 'contact_name'
570
- | 'contact_id'
571
- | 'contact_lid'
572
- | 'contact_type'
573
- | 'is_internal'
574
- | 'is_wa_contact'
575
- | 'is_admin'
576
- | 'is_super_admin'
577
- >;
578
-
579
- type ChatRawDataType = Merge<
580
- Pick<
581
- Tables<'view_chats'>,
582
- | 'assigned_to'
583
- | 'chat_id'
584
- | 'chat_name'
585
- | 'chat_type'
586
- | 'created_at'
587
- | 'group_description'
588
- | 'info_admins_only'
589
- | 'org_id'
590
- | 'org_phone'
591
- | 'chat_org_phones'
592
- | 'messages_admins_only'
593
- | 'custom_properties'
594
- | 'initiated_by'
595
- >,
596
- {
597
- has_flagged_messages: boolean;
598
- labels: string[];
599
- members: string[];
600
- custom_properties: { [key: string]: string } | null;
601
- assignee_name: string | null;
602
- is_latest_org_message: boolean;
603
- }
604
- >;
605
-
606
- type TicketRawDataType = Merge<
607
- Pick<
608
- Tables<'tbl_chat_tickets'>,
609
- | 'ticket_id'
610
- | 'assignee'
611
- | 'status'
612
- | 'priority'
613
- | 'closed_at'
614
- | 'custom_properties'
615
- | 'quoted_message_id'
616
- | 'subject'
617
- | 'raised_by'
618
- | 'is_deleted'
619
- | 'due_date'
620
- >,
621
- {
622
- labels: string[];
623
- custom_properties: { [key: string]: string } | null;
624
- assignee_name: string | null;
625
- }
626
- >;
627
-
628
- type TaskRawDataType = Pick<
629
- TaskType,
630
- | 'assignee'
631
- | 'notes'
632
- | 'priority'
633
- | 'due_date'
634
- | 'remind_at'
635
- | 'title'
636
- | 'type'
637
- | 'status'
638
- >;
639
-
640
- export type WorkflowRawDataTypes = {
641
- message: MessageRawDataType;
642
- reaction: ReactionRawDataType;
643
- chat: ChatRawDataType;
644
- ticket: TicketRawDataType;
645
- task: TaskRawDataType;
646
- sender: SenderRawDataType;
647
- };
648
-
649
- export type MessageWorkflowDataTypes = AppendTypes<
650
- {
651
- message: WorkflowRawDataTypes['message'];
652
- sender: WorkflowRawDataTypes['sender'];
653
- chat: WorkflowRawDataTypes['chat'];
654
- },
655
- '.'
656
- >;
657
-
658
- export type ReactionWorkflowDataTypes = AppendTypes<
659
- {
660
- reaction: WorkflowRawDataTypes['reaction'];
661
- sender: WorkflowRawDataTypes['sender'];
662
- message: WorkflowRawDataTypes['message'];
663
- chat: WorkflowRawDataTypes['chat'];
664
- },
665
- '.'
666
- >;
667
-
668
- export type TicketWorkflowDataTypes = AppendTypes<
669
- {
670
- ticket: WorkflowRawDataTypes['ticket'];
671
- message: WorkflowRawDataTypes['message'];
672
- chat: WorkflowRawDataTypes['chat'];
673
- sender: WorkflowRawDataTypes['sender'];
674
- },
675
- '.'
676
- >;
677
-
678
- export type TaskWorkflowDataTypes = AppendTypes<
679
- {
680
- task: WorkflowRawDataTypes['task'];
681
- message?: WorkflowRawDataTypes['message'];
682
- ticket?: WorkflowRawDataTypes['ticket'];
683
- chat?: WorkflowRawDataTypes['chat'];
684
- sender?: WorkflowRawDataTypes['sender'];
685
- },
686
- '.'
687
- >;
688
-
689
- export type ChatWorkflowDataTypes = AppendTypes<
690
- {
691
- chat: WorkflowRawDataTypes['chat'];
692
- },
693
- '.'
694
- >;
695
-
696
- export enum Triggers {
697
- MESSAGE_CREATED = 'message.created',
698
- MESSAGE_UPDATED = 'message.updated',
699
- MESSAGE_DELETED = 'message.deleted',
700
- MESSAGE_FLAGGED = 'message.flagged',
701
- MESSAGE_UNFLAGGED = 'message.unflagged',
702
- REACTION_ADDED = 'reaction.added',
703
- TICKET_CREATED = 'ticket.created',
704
- TICKET_UPDATED = 'ticket.updated',
705
- TICKET_DELETED = 'ticket.deleted',
706
- TICKET_CLOSED = 'ticket.closed',
707
- TICKET_DUE = 'ticket.due',
708
- CHAT_CREATED = 'chat.created',
709
- CHAT_LABEL_UPDATED = 'chat.label.updated',
710
- CHAT_CLOSED = 'chat.closed',
711
- TASK_CREATED = 'task.created',
712
- TASK_DUE = 'task.due',
713
- }
714
-
715
- export type WorkflowDataType<T extends Triggers> = T extends
716
- | Triggers.MESSAGE_CREATED
717
- | Triggers.MESSAGE_DELETED
718
- | Triggers.MESSAGE_UPDATED
719
- | Triggers.MESSAGE_FLAGGED
720
- | Triggers.MESSAGE_UNFLAGGED
721
- ? MessageWorkflowDataTypes
722
- : T extends
723
- | Triggers.CHAT_CREATED
724
- | Triggers.CHAT_LABEL_UPDATED
725
- | Triggers.CHAT_CLOSED
726
- ? ChatWorkflowDataTypes
727
- : T extends
728
- | Triggers.TICKET_CREATED
729
- | Triggers.TICKET_UPDATED
730
- | Triggers.TICKET_DELETED
731
- | Triggers.TICKET_CLOSED
732
- | Triggers.TICKET_DUE
733
- ? TicketWorkflowDataTypes
734
- : T extends Triggers.REACTION_ADDED
735
- ? ReactionWorkflowDataTypes
736
- : T extends Triggers.TASK_CREATED | Triggers.TASK_DUE
737
- ? TaskWorkflowDataTypes
738
- : never;
739
-
740
- /***************************** ACTION TYPES *****************************/
741
-
742
- export type SendMessageToChatWorkflowAction = {
743
- type: 'send_message_to_chat';
744
- metadata: {
745
- message: string;
746
- media?: {
747
- url: string;
748
- type: 'image' | 'video' | 'audio' | 'document';
749
- name: string;
750
- } | null;
751
- debounce?: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}` | null;
752
- chat_id: string | null;
753
- unflag_chat?: boolean;
754
- as_reply?: boolean;
755
- };
756
- };
757
-
758
- export type CreateTicketWorkflowAction = {
759
- type: 'create_ticket';
760
- metadata: {
761
- subject: string | null;
762
- assignee:
763
- | {
764
- round_robin: true;
765
- emails: string[];
766
- assignee_check_for?: 'shift_times' | 'online_offline' | 'all';
767
- }
768
- | {
769
- email: string | null;
770
- round_robin: false;
771
- };
772
- priority?: '0' | '1' | '2' | '3' | '4';
773
- status: 'open' | 'inprogress' | 'closed';
774
- labels: string[];
775
- due_in: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}` | null;
776
- message_source: string | null;
777
- };
778
- };
779
-
780
- export type AttachToLatestTicketWorkflowAction = {
781
- type: 'attach_to_latest_ticket';
782
- metadata: {
783
- status: ('open' | 'inprogress' | 'closed')[];
784
- };
785
- };
786
-
787
- export type NotifyHTTPWorkflowAction = {
788
- type: 'notify_http';
789
- metadata: {
790
- url: string;
791
- method: 'GET' | 'POST' | 'PUT' | 'DELETE';
792
- headers?: { id: string; key: string; value: string }[] | null;
793
- body?: string | { id: string; key: string; value: string }[] | null;
794
- body_layout?: 'json' | 'fields';
795
- };
796
- };
797
-
798
- export type FlagMessageWorkflowAction = {
799
- type: 'flag_message';
800
- metadata: {
801
- message_id?: string;
802
- };
803
- };
804
-
805
- export type UnflagMessageWorkflowAction = {
806
- type: 'unflag_message';
807
- metadata: {
808
- message_id?: string;
809
- };
810
- };
811
-
812
- export type AssignTicketWorkflowAction = {
813
- type: 'assign_ticket';
814
- metadata: {
815
- assignee:
816
- | {
817
- round_robin: true;
818
- emails: string[];
819
- assignee_check_for?: 'shift_times' | 'online_offline' | 'all';
820
- }
821
- | {
822
- email: string | null;
823
- round_robin: false;
824
- };
825
- };
826
- };
827
-
828
- export type CloseTicketWorkflowAction = {
829
- type: 'close_ticket';
830
- metadata: {
831
- bypass_mandatory_fields?: boolean;
832
- closing_note: string;
833
- };
834
- };
835
-
836
- export type AddChatLabelWorkflowAction = {
837
- type: 'add_chat_label';
838
- metadata: {
839
- labels: string[];
840
- };
841
- };
842
-
843
- export type RemoveChatLabelWorkflowAction = {
844
- type: 'remove_chat_label';
845
- metadata: {
846
- labels: string[];
847
- };
848
- };
849
-
850
- export type AddTicketLabelWorkflowAction = {
851
- type: 'add_ticket_label';
852
- metadata: {
853
- labels: string[];
854
- };
855
- };
856
-
857
- export type RemoveTicketLabelWorkflowAction = {
858
- type: 'remove_ticket_label';
859
- metadata: {
860
- labels: string[];
861
- };
862
- };
863
-
864
- export type AssignChatWorkflowAction = {
865
- type: 'assign_chat';
866
- metadata: {
867
- assignee:
868
- | {
869
- round_robin: true;
870
- emails: string[];
871
- assignee_check_for?: 'shift_times' | 'online_offline' | 'all';
872
- }
873
- | {
874
- email: string | null;
875
- round_robin: false;
876
- };
877
- };
878
- };
879
-
880
- export type ForwardMessageWorkflowAction = {
881
- type: 'forward_message';
882
- metadata: {
883
- chat_id: string | null;
884
- };
885
- };
886
-
887
- export type SendEmailWorkflowAction = {
888
- type: 'send_email';
889
- metadata: {
890
- email: string;
891
- subject: string;
892
- body: string;
893
- };
894
- };
895
-
896
- export type DeleteMessageWorkflowAction = {
897
- type: 'delete_message';
898
- metadata: {};
899
- };
900
-
901
- export type CloseChatWorkflowAction = {
902
- type: 'close_chat';
903
- metadata: {};
904
- };
905
-
906
- export type ValidateWorkflowAction = {
907
- type: 'validate_action';
908
- metadata: WorkflowConditionGroup;
909
- };
910
-
911
- export type DelayWorkflowAction = {
912
- type: 'delay';
913
- metadata: {
914
- delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
915
- };
916
- };
917
-
918
- export type SendSlackNotificationWorkflowAction = {
919
- type: 'send_slack_notification';
920
- metadata: {
921
- slack_payload: string;
922
- url: string;
923
- };
924
- };
925
-
926
- export type SplitPathWorkflowAction = {
927
- type: 'split_path';
928
- metadata: {
929
- paths: Array<{
930
- id: string;
931
- condition_action_id: string;
932
- }>;
933
- };
934
- };
935
-
936
- // experimental
937
- export type AIPromptWorkflowAction = {
938
- type: 'ai_prompt';
939
- metadata: {
940
- query: string;
941
- body: string;
942
- };
943
- };
944
-
945
- export type NormalWorkflowActionTypes =
946
- | SendMessageToChatWorkflowAction
947
- | CreateTicketWorkflowAction
948
- | AttachToLatestTicketWorkflowAction
949
- | NotifyHTTPWorkflowAction
950
- | FlagMessageWorkflowAction
951
- | UnflagMessageWorkflowAction
952
- | AssignTicketWorkflowAction
953
- | CloseTicketWorkflowAction
954
- | AddChatLabelWorkflowAction
955
- | RemoveChatLabelWorkflowAction
956
- | AddTicketLabelWorkflowAction
957
- | RemoveTicketLabelWorkflowAction
958
- | AssignChatWorkflowAction
959
- | ForwardMessageWorkflowAction
960
- | SendEmailWorkflowAction
961
- | DeleteMessageWorkflowAction
962
- | CloseChatWorkflowAction
963
- | ValidateWorkflowAction
964
- | DelayWorkflowAction
965
- | SendSlackNotificationWorkflowAction
966
- | AIPromptWorkflowAction;
967
-
968
- export type WorkflowActionsTypeMap = {
969
- send_message_to_chat: SendMessageToChatWorkflowAction;
970
- create_ticket: CreateTicketWorkflowAction;
971
- attach_to_latest_ticket: AttachToLatestTicketWorkflowAction;
972
- notify_http: NotifyHTTPWorkflowAction;
973
- flag_message: FlagMessageWorkflowAction;
974
- unflag_message: UnflagMessageWorkflowAction;
975
- assign_ticket: AssignTicketWorkflowAction;
976
- close_ticket: CloseTicketWorkflowAction;
977
- add_chat_label: AddChatLabelWorkflowAction;
978
- remove_chat_label: RemoveChatLabelWorkflowAction;
979
- add_ticket_label: AddTicketLabelWorkflowAction;
980
- remove_ticket_label: RemoveTicketLabelWorkflowAction;
981
- assign_chat: AssignChatWorkflowAction;
982
- forward_message: ForwardMessageWorkflowAction;
983
- send_email: SendEmailWorkflowAction;
984
- delete_message: DeleteMessageWorkflowAction;
985
- close_chat: CloseChatWorkflowAction;
986
- validate_action: ValidateWorkflowAction;
987
- delay: DelayWorkflowAction;
988
- send_slack_notification: SendSlackNotificationWorkflowAction;
989
- ai_prompt: AIPromptWorkflowAction;
990
- split_path: SplitPathWorkflowAction;
991
- };
992
-
993
- export const ActionCategoryMap: Record<
994
- WorkflowActionTypes['type'],
995
- WorkflowActionCategory
996
- > = {
997
- send_message_to_chat: 'message',
998
- create_ticket: 'ticket',
999
- attach_to_latest_ticket: 'ticket',
1000
- notify_http: 'custom',
1001
- flag_message: 'message',
1002
- unflag_message: 'message',
1003
- assign_ticket: 'ticket',
1004
- close_ticket: 'ticket',
1005
- add_chat_label: 'chat',
1006
- remove_chat_label: 'chat',
1007
- add_ticket_label: 'ticket',
1008
- remove_ticket_label: 'ticket',
1009
- assign_chat: 'chat',
1010
- forward_message: 'message',
1011
- send_email: 'custom',
1012
- delete_message: 'message',
1013
- close_chat: 'chat',
1014
- validate_action: 'flow',
1015
- delay: 'flow',
1016
- send_slack_notification: 'custom',
1017
- ai_prompt: 'ai',
1018
- split_path: 'flow',
1019
- };
1020
-
1021
- export const CategoryNameMap: Record<WorkflowActionCategory, string> = {
1022
- search: 'Search',
1023
- message: 'Message',
1024
- ticket: 'Ticket',
1025
- chat: 'Chat',
1026
- custom: 'Custom',
1027
- flow: 'Flow',
1028
- ai: 'AI',
1029
- };
1030
-
1031
- export const TriggerNameMap: Record<Triggers, string> = {
1032
- 'chat.created': 'Chat Created',
1033
- 'message.created': 'Message Created',
1034
- 'chat.closed': 'Chat Closed',
1035
- 'chat.label.updated': 'Chat Label Updated',
1036
- 'message.deleted': 'Message Deleted',
1037
- 'message.flagged': 'Message Flagged',
1038
- 'message.unflagged': 'Message Unflagged',
1039
- 'message.updated': 'Message Updated',
1040
- 'reaction.added': 'Reaction Added',
1041
- 'ticket.closed': 'Ticket Closed',
1042
- 'ticket.created': 'Ticket Created',
1043
- 'ticket.deleted': 'Ticket Deleted',
1044
- 'ticket.due': 'Ticket Due',
1045
- 'ticket.updated': 'Ticket Updated',
1046
- 'task.created': 'Task Created',
1047
- 'task.due': 'Task Due',
1048
- };
1049
-
1050
- export const WorkflowActionNameMap: Record<
1051
- WorkflowType['actions'][number]['action_metadata']['type'],
1052
- {
1053
- title: string;
1054
- description: string;
1055
- }
1056
- > = {
1057
- add_chat_label: {
1058
- title: 'Add Chat Label',
1059
- description: 'Add a label to the trigger chat',
1060
- },
1061
- add_ticket_label: {
1062
- title: 'Add Ticket Label',
1063
- description: 'Add a label to the trigger ticket',
1064
- },
1065
- assign_chat: {
1066
- title: 'Assign Chat',
1067
- description: 'Assign a chat to org user',
1068
- },
1069
- attach_to_latest_ticket: {
1070
- title: 'Attach to Latest Ticket',
1071
- description: 'Attach to the latest ticket of trigger chat',
1072
- },
1073
- close_chat: {
1074
- title: 'Close Chat',
1075
- description: 'Close the chat',
1076
- },
1077
- close_ticket: {
1078
- title: 'Close Ticket',
1079
- description: 'Close the trigger ticket or ticket attached to the message',
1080
- },
1081
- create_ticket: {
1082
- title: 'Create Ticket',
1083
- description: 'Create a ticket attached to the trigger message',
1084
- },
1085
- delay: {
1086
- title: 'Delay',
1087
- description: 'Add a delay to next sequential actions',
1088
- },
1089
- ai_prompt: {
1090
- title: 'AI Prompt',
1091
- description: 'Analyse, filter, categorise, and generate text using AI',
1092
- },
1093
- flag_message: {
1094
- title: 'Flag Message',
1095
- description: 'Flag the trigger message or message attached to the ticket',
1096
- },
1097
- remove_chat_label: {
1098
- title: 'Remove Chat Label',
1099
- description: 'Remove a label from the trigger chat',
1100
- },
1101
- remove_ticket_label: {
1102
- title: 'Remove Ticket Label',
1103
- description:
1104
- 'Remove a label from the trigger ticket or ticket attached to the message',
1105
- },
1106
- send_email: {
1107
- title: 'Send Email',
1108
- description: 'Send custom text to an email',
1109
- },
1110
- send_message_to_chat: {
1111
- title: 'Send Message to Chat',
1112
- description: 'Send a message to the chat attached to the trigger',
1113
- },
1114
- send_slack_notification: {
1115
- title: 'Send Slack Notification',
1116
- description: 'Send a slack notification',
1117
- },
1118
- split_path: {
1119
- title: 'Split Path',
1120
- description: 'Split the workflow path based on the condition',
1121
- },
1122
- unflag_message: {
1123
- title: 'Unflag Message',
1124
- description: 'Unflag the trigger message or message attached to the ticket',
1125
- },
1126
- validate_action: {
1127
- title: 'Validate Action',
1128
- description: 'Validate the action based on multiple criterias',
1129
- },
1130
- notify_http: {
1131
- title: 'Notify HTTP',
1132
- description: 'Send a custom HTTP request to an endpoint',
1133
- },
1134
- assign_ticket: {
1135
- title: 'Assign Ticket',
1136
- description: 'Assign a ticket to an org user',
1137
- },
1138
- forward_message: {
1139
- title: 'Forward Message',
1140
- description: 'Forward a message to a chat',
1141
- },
1142
- delete_message: {
1143
- title: 'Delete Message',
1144
- description: 'Delete the trigger message or message attached to the ticket',
1145
- },
1146
- };
1147
-
1148
- export const ActionResponseVariables = {
1149
- send_message_to_chat: {
1150
- 'Queue ID': 'queue_id',
1151
- 'Queue Position': 'queue_position',
1152
- 'Chat ID': 'chat_id',
1153
- 'Org Phone': 'org_phone',
1154
- },
1155
- create_ticket: {
1156
- 'Ticket ID': 'ticket_id',
1157
- 'Ticket Subject': 'ticket_subject',
1158
- 'Chat ID': 'chat_id',
1159
- },
1160
- forward_message: {
1161
- 'Queue ID': 'queue_id',
1162
- 'Queue Position': 'queue_position',
1163
- 'Chat ID': 'chat_id',
1164
- 'Org Phone': 'org_phone',
1165
- },
1166
- validate_action: {
1167
- 'Is Valid': 'is_valid',
1168
- },
1169
- };
1170
-
1171
- const ChatCreatedVariables: Record<keyof ChatWorkflowDataTypes, string> = {
1172
- 'chat.chat_id': 'Chat ID',
1173
- 'chat.chat_name': 'Chat Name',
1174
- 'chat.chat_type': 'Chat Type',
1175
- 'chat.members': 'Chat Members',
1176
- 'chat.has_flagged_messages': 'Chat Has Flagged Messages',
1177
- 'chat.labels': 'Chat Labels',
1178
- 'chat.assigned_to': 'Chat Assigned To',
1179
- 'chat.created_at': 'Chat Created At',
1180
- 'chat.chat_org_phones': 'Chat Org Phones',
1181
- 'chat.assignee_name': 'Chat Assignee Name',
1182
- 'chat.custom_properties': 'Chat Custom Properties',
1183
- 'chat.group_description': 'Chat Group Description',
1184
- 'chat.info_admins_only': 'Chat Info Admins Only',
1185
- 'chat.initiated_by': 'Chat Initiated By',
1186
- 'chat.messages_admins_only': 'Chat Messages Admins Only',
1187
- 'chat.org_id': 'Chat Org ID',
1188
- 'chat.org_phone': 'Chat Org Phone',
1189
- 'chat.is_latest_org_message': 'Chat Latest Message Self',
1190
- };
1191
-
1192
- const SenderCreatedVariables: Record<
1193
- keyof AppendTypes<{ sender: SenderRawDataType }, '.'>,
1194
- string
1195
- > = {
1196
- 'sender.contact_name': 'Sender Contact Name',
1197
- 'sender.contact_id': 'Sender Contact ID',
1198
- 'sender.contact_lid': 'Sender Contact LID',
1199
- 'sender.contact_type': 'Sender Contact Type',
1200
- 'sender.is_internal': 'Sender Is Internal',
1201
- 'sender.is_wa_contact': 'Sender Is WA Contact',
1202
- 'sender.is_admin': 'Sender Is Admin',
1203
- 'sender.is_super_admin': 'Sender Is Super Admin',
1204
- };
1205
-
1206
- const MessageCreatedVariables: Record<
1207
- keyof AppendTypes<{ message: MessageRawDataType }, '.'>,
1208
- string
1209
- > = {
1210
- 'message.body': 'Message Body',
1211
- 'message.flag_status': 'Message Flag Status',
1212
- 'message.has_quoted_msg': 'Message Has Quoted Msg',
1213
- 'message.message_type': 'Message Type',
1214
- 'message.media': 'Message Media',
1215
- 'message.message_id': 'Message ID',
1216
- 'message.message_ticket_id': 'Message Ticket ID',
1217
- 'message.performed_by': 'Message Performed By',
1218
- 'message.sender_phone': 'Message Sender Phone',
1219
- 'message.timestamp': 'Message Timestamp',
1220
- 'message.is_latest_message': 'Message is Latest Message',
1221
- };
1222
-
1223
- const ReactionAddedVariables: Record<
1224
- keyof AppendTypes<{ reaction: ReactionRawDataType }, '.'>,
1225
- string
1226
- > = {
1227
- 'reaction.reaction': 'Reaction',
1228
- 'reaction.sender_id': 'Reaction Sender ID',
1229
- 'reaction.reaction_id': 'Reaction ID',
1230
- };
1231
-
1232
- const TicketCreatedVariables: Record<
1233
- keyof AppendTypes<{ ticket: TicketRawDataType }, '.'>,
1234
- string
1235
- > = {
1236
- 'ticket.ticket_id': 'Ticket ID',
1237
- 'ticket.subject': 'Ticket Subject',
1238
- 'ticket.assignee': 'Ticket Assignee',
1239
- 'ticket.status': 'Ticket Status',
1240
- 'ticket.priority': 'Ticket Priority',
1241
- 'ticket.closed_at': 'Ticket Closed At',
1242
- 'ticket.custom_properties': 'Ticket Custom Properties',
1243
- 'ticket.labels': 'Ticket Labels',
1244
- 'ticket.quoted_message_id': 'Ticket Quoted Message ID',
1245
- 'ticket.raised_by': 'Ticket Raised By',
1246
- 'ticket.is_deleted': 'Ticket Is Deleted',
1247
- 'ticket.due_date': 'Ticket Due Date',
1248
- 'ticket.assignee_name': 'Ticket Assignee Name',
1249
- };
1250
-
1251
- const TaskCreatedVariables: Record<
1252
- keyof AppendTypes<{ task: TaskRawDataType }, '.'>,
1253
- string
1254
- > = {
1255
- 'task.assignee': 'Task Assignee',
1256
- 'task.notes': 'Task Notes',
1257
- 'task.priority': 'Task Priority',
1258
- 'task.due_date': 'Task Due Date',
1259
- 'task.remind_at': 'Task Remind At',
1260
- 'task.title': 'Task Title',
1261
- 'task.type': 'Task Type',
1262
- 'task.status': 'Task Status',
1263
- };
1264
-
1265
- export const TriggerVariables: Record<
1266
- 'message' | 'chat' | 'ticket' | 'reaction' | 'task',
1267
- Record<string, string>
1268
- > = {
1269
- message: {
1270
- ...MessageCreatedVariables,
1271
- ...ChatCreatedVariables,
1272
- ...SenderCreatedVariables,
1273
- },
1274
- chat: {
1275
- ...ChatCreatedVariables,
1276
- },
1277
- ticket: {
1278
- ...TicketCreatedVariables,
1279
- ...MessageCreatedVariables,
1280
- ...ChatCreatedVariables,
1281
- ...SenderCreatedVariables,
1282
- },
1283
- reaction: {
1284
- ...ReactionAddedVariables,
1285
- ...MessageCreatedVariables,
1286
- ...ChatCreatedVariables,
1287
- ...SenderCreatedVariables,
1288
- },
1289
- task: {
1290
- ...TaskCreatedVariables,
1291
- ...MessageCreatedVariables,
1292
- ...ChatCreatedVariables,
1293
- ...SenderCreatedVariables,
1294
- ...TicketCreatedVariables,
1295
- },
1296
- };
1
+ import { Merge, OverrideProperties } from 'type-fest';
2
+ import { MessageType } from './index';
3
+ import { AppendTypes } from './rules.types';
4
+ import { Tables } from './supabase.types';
5
+ import { TaskType } from './types';
6
+
7
+ /***************************** WORKFLOWS *****************************/
8
+
9
+ export type WorkflowActionTypes =
10
+ | NormalWorkflowActionTypes
11
+ | SplitPathWorkflowAction;
12
+
13
+ export type WorkflowActionCategory =
14
+ | 'search'
15
+ | 'message'
16
+ | 'ticket'
17
+ | 'chat'
18
+ | 'custom'
19
+ | 'flow'
20
+ | 'ai';
21
+
22
+ export type SendMessageToChatActionReturnInfo = {
23
+ message_details: {
24
+ queue_id: string;
25
+ queue_position: number;
26
+ };
27
+ };
28
+
29
+ export type CreateTicketActionReturnInfo = {
30
+ ticket_details: Tables<'tbl_chat_tickets'>;
31
+ };
32
+
33
+ export type AssignChatActionReturnInfo = {
34
+ chat_details: {
35
+ chat_id: string;
36
+ assigned_to: string;
37
+ org_id: string;
38
+ };
39
+ };
40
+
41
+ export type AddChatLabelActionReturnInfo = {
42
+ chat_details: {
43
+ chat_id: string;
44
+ org_id: string;
45
+ labels: string[];
46
+ };
47
+ };
48
+
49
+ export type NotifyHTTPActionReturnInfo = {
50
+ status: number | 'Unknown';
51
+ data: Record<string, unknown>;
52
+ error: any;
53
+ };
54
+
55
+ export type ForwardMessageActionReturnInfo = {
56
+ [key: string]: boolean;
57
+ };
58
+
59
+ export type DeleteMessageActionReturnInfo = {};
60
+
61
+ export type AssignTicketActionReturnInfo = Tables<'tbl_chat_tickets'>;
62
+ export type CloseTicketActionReturnInfo = Tables<'tbl_chat_tickets'>;
63
+
64
+ export type AIPromptActionReturnInfo = {
65
+ query: string;
66
+ input: string;
67
+ output: string | Record<string, unknown> | boolean | null;
68
+ };
69
+
70
+ export type FlagMessageActionReturnInfo = {
71
+ message_details: {
72
+ message_id: string;
73
+ chat_id: string;
74
+ org_id: string;
75
+ org_phone: string;
76
+ };
77
+ };
78
+
79
+ export type CloseChatActionReturnInfo = {
80
+ chat_details: {
81
+ chat_id: string;
82
+ org_id: string;
83
+ };
84
+ };
85
+
86
+ export type RemoveChatLabelActionReturnInfo = {
87
+ chat_details: {
88
+ chat_id: string;
89
+ org_id: string;
90
+ labels: string[];
91
+ };
92
+ };
93
+
94
+ export type UnflagMessageActionReturnInfo = {
95
+ message_details: {
96
+ message_id: string;
97
+ chat_id: string;
98
+ org_id: string;
99
+ org_phone: string;
100
+ };
101
+ };
102
+
103
+ export type AddTicketLabelActionReturnInfo = {
104
+ ticket_details: {
105
+ ticket_id: string;
106
+ org_id: string;
107
+ labels: string[];
108
+ };
109
+ };
110
+
111
+ export type RemoveTicketLabelActionReturnInfo = {
112
+ ticket_details: {
113
+ ticket_id: string;
114
+ org_id: string;
115
+ labels: string[];
116
+ };
117
+ };
118
+
119
+ export type SendEmailActionReturnInfo = {
120
+ email_details: {
121
+ email: string;
122
+ subject: string;
123
+ body: string;
124
+ };
125
+ };
126
+
127
+ export type SendSlackNotificationActionReturnInfo = {
128
+ slack_details: {
129
+ url: string;
130
+ slack_payload: string;
131
+ res: any;
132
+ };
133
+ };
134
+
135
+ export type ActionInfo = {
136
+ send_message_to_chat: SendMessageToChatActionReturnInfo;
137
+ create_ticket: CreateTicketActionReturnInfo;
138
+ assign_chat: AssignChatActionReturnInfo;
139
+ close_chat: CloseChatActionReturnInfo;
140
+ add_chat_label: AddChatLabelActionReturnInfo;
141
+ remove_chat_label: RemoveChatLabelActionReturnInfo;
142
+ notify_http: NotifyHTTPActionReturnInfo;
143
+ forward_message: ForwardMessageActionReturnInfo;
144
+ delete_message: DeleteMessageActionReturnInfo;
145
+ assign_ticket: AssignTicketActionReturnInfo;
146
+ close_ticket: CloseTicketActionReturnInfo;
147
+ add_ticket_label: AddTicketLabelActionReturnInfo;
148
+ remove_ticket_label: RemoveTicketLabelActionReturnInfo;
149
+ ai_prompt: AIPromptActionReturnInfo;
150
+ flag_message: FlagMessageActionReturnInfo;
151
+ unflag_message: UnflagMessageActionReturnInfo;
152
+ send_email: SendEmailActionReturnInfo;
153
+ send_slack_notification: SendSlackNotificationActionReturnInfo;
154
+ };
155
+
156
+ // Example workflow type ->
157
+
158
+ // const demoWorkflow: WorkflowType = {
159
+ // id: 'workflow-1',
160
+ // name: 'Urgent Chat Auto-Responder',
161
+ // trigger: Triggers.MESSAGE_CREATED,
162
+ // trigger_metadata: {
163
+ // org_phones: ['phone-1'],
164
+ // allow_internal_messages: false,
165
+ // first_action_id: 'action-validate',
166
+ // },
167
+ // actions: [
168
+ // {
169
+ // id: 'action-validate',
170
+ // action_metadata: {
171
+ // type: 'validate_action',
172
+ // metadata: {
173
+ // operator: 'AND',
174
+ // conditions: [
175
+ // {
176
+ // id: 'cond-1',
177
+ // variable_name: 'message.text',
178
+ // variable_type: 'string',
179
+ // condition: 'CONTAINS',
180
+ // value: 'urgent',
181
+ // },
182
+ // {
183
+ // id: 'cond-2',
184
+ // variable_name: 'chat.assigned',
185
+ // variable_type: 'boolean',
186
+ // condition: 'IS',
187
+ // value: false,
188
+ // },
189
+ // ],
190
+ // },
191
+ // },
192
+ // next: 'action-add-label',
193
+ // },
194
+ // {
195
+ // id: 'action-add-label',
196
+ // action_metadata: {
197
+ // type: 'add_chat_label',
198
+ // metadata: {
199
+ // labels: ['urgent'],
200
+ // },
201
+ // },
202
+ // next: 'action-assign-chat',
203
+ // },
204
+ // {
205
+ // id: 'action-assign-chat',
206
+ // action_metadata: {
207
+ // type: 'assign_chat',
208
+ // metadata: {
209
+ // assignee: {
210
+ // round_robin: true,
211
+ // emails: ['agent1@example.com', 'agent2@example.com'],
212
+ // assignee_check_for: 'all',
213
+ // },
214
+ // },
215
+ // },
216
+ // next: 'action-delay',
217
+ // },
218
+ // {
219
+ // id: 'action-delay',
220
+ // action_metadata: {
221
+ // type: 'delay',
222
+ // metadata: {
223
+ // delay: '5 minutes',
224
+ // },
225
+ // },
226
+ // next: 'action-send-message',
227
+ // },
228
+ // {
229
+ // id: 'action-send-message',
230
+ // action_metadata: {
231
+ // type: 'send_message_to_chat',
232
+ // metadata: {
233
+ // message: 'We’re on it!',
234
+ // chat_id: 'trigger_chat',
235
+ // },
236
+ // },
237
+ // next: 'action-split',
238
+ // },
239
+ // {
240
+ // id: 'action-split',
241
+ // action_metadata: {
242
+ // type: 'split_path',
243
+ // metadata: {
244
+ // paths: [
245
+ // {
246
+ // id: 'path-flagged',
247
+ // condition_action_id: 'action-condition-flagged',
248
+ // },
249
+ // {
250
+ // id: 'path-not-flagged',
251
+ // condition_action_id: 'action-condition-not-flagged',
252
+ // },
253
+ // ],
254
+ // },
255
+ // },
256
+ // {
257
+ // id: 'action-condition-flagged',
258
+ // action_metadata: {
259
+ // type: 'validate_action',
260
+ // metadata: {
261
+ // operator: 'AND',
262
+ // conditions: [
263
+ // {
264
+ // id: 'cond-flagged',
265
+ // variable_name: 'chat.has_flagged_messages',
266
+ // variable_type: 'boolean',
267
+ // condition: 'IS',
268
+ // value: true,
269
+ // },
270
+ // ],
271
+ // },
272
+ // },
273
+ // next: 'action-create-ticket',
274
+ // },
275
+ // {
276
+ // id: 'action-condition-not-flagged',
277
+ // action_metadata: {
278
+ // type: 'validate_action',
279
+ // metadata: {
280
+ // operator: 'AND',
281
+ // conditions: [
282
+ // {
283
+ // id: 'cond-unflagged',
284
+ // variable_name: 'chat.has_flagged_messages',
285
+ // variable_type: 'boolean',
286
+ // condition: 'IS',
287
+ // value: false,
288
+ // },
289
+ // ],
290
+ // },
291
+ // },
292
+ // next: 'action-send-slack',
293
+ // },
294
+ // {
295
+ // id: 'action-create-ticket',
296
+ // action_metadata: {
297
+ // type: 'create_ticket',
298
+ // metadata: {
299
+ // subject: 'Urgent Chat Follow-up',
300
+ // assignee: {
301
+ // email: 'manager@example.com',
302
+ // },
303
+ // priority: '4',
304
+ // status: 'open',
305
+ // labels: ['urgent', 'auto-created'],
306
+ // due_date: '30 minutes',
307
+ // },
308
+ // },
309
+ // next: '', // End
310
+ // },
311
+ // {
312
+ // id: 'action-send-slack',
313
+ // action_metadata: {
314
+ // type: 'send_slack_notification',
315
+ // metadata: {
316
+ // slack_payload: JSON.stringify({
317
+ // text: 'An urgent chat was received, no flagged messages.',
318
+ // }),
319
+ // url: 'https://hooks.slack.com/services/XXX/YYY/ZZZ',
320
+ // },
321
+ // },
322
+ // next: '', // End
323
+ // },
324
+ // ],
325
+ // };
326
+
327
+ /***************************** WORKFLOW FE TYPES @harshgour *****************************/
328
+ /***************************** WORKFLOWS *****************************/
329
+
330
+ export type NormalWorkflowActions = {
331
+ id: string;
332
+ action_metadata: NormalWorkflowActionTypes;
333
+ next: string | null;
334
+ };
335
+
336
+ export type SplitPathActions = {
337
+ id: string;
338
+ action_metadata: SplitPathWorkflowAction;
339
+ };
340
+
341
+ export type WorkflowAction = NormalWorkflowActions | SplitPathActions;
342
+
343
+ export type WorkflowType = OverrideProperties<
344
+ Tables<'tbl_org_workflows'>,
345
+ {
346
+ trigger_metadata: {
347
+ org_phones: string[];
348
+ allow_internal_messages?: boolean;
349
+ first_action_id: string | null;
350
+ };
351
+ trigger: Triggers;
352
+ actions: Array<NormalWorkflowActions | SplitPathActions>;
353
+ }
354
+ >;
355
+
356
+ export function isNormalWorkflowAction(
357
+ workflow_action:
358
+ | WorkflowType['actions'][number]
359
+ | {
360
+ trigger_metadata?: WorkflowType['trigger_metadata'];
361
+ trigger: WorkflowType['trigger'];
362
+ }
363
+ | {}
364
+ ): workflow_action is NormalWorkflowActions {
365
+ return (
366
+ 'action_metadata' in workflow_action &&
367
+ workflow_action?.action_metadata?.type !== 'split_path'
368
+ );
369
+ }
370
+
371
+ export function isSplitPathAction(
372
+ workflow_action: WorkflowType['actions'][number]
373
+ ): workflow_action is SplitPathActions {
374
+ return workflow_action.action_metadata.type === 'split_path';
375
+ }
376
+
377
+ /***************************** CONDITIONS TYPES *****************************/
378
+
379
+ // {
380
+ // "operator": "AND",
381
+ // "conditions": [
382
+ // {
383
+ // "id": "cond1",
384
+ // "variable_name": "message",
385
+ // "variable_type": "string",
386
+ // "condition": "CONTAINS",
387
+ // "value": "urgent"
388
+ // },
389
+ // {
390
+ // "operator": "OR",
391
+ // "conditions": [
392
+ // {
393
+ // "id": "cond2",
394
+ // "variable_name": "priority",
395
+ // "variable_type": "number",
396
+ // "condition": "GT",
397
+ // "value": 2
398
+ // },
399
+ // {
400
+ // "id": "cond3",
401
+ // "variable_name": "assigned",
402
+ // "variable_type": "boolean",
403
+ // "condition": "NKNOWN"
404
+ // }
405
+ // ]
406
+ // }
407
+ // ]
408
+ // }
409
+
410
+ export enum ConditionsNameMap {
411
+ // string
412
+ CONTAINS = 'CONTAINS',
413
+ NCONTAINS = 'NOT CONTAINS',
414
+ EQUALS = 'EQUALS',
415
+ NEQUALS = 'NOT EQUALS',
416
+ STARTS_WITH = 'STARTS WITH',
417
+ ENDS_WITH = 'ENDS WITH',
418
+
419
+ // number, date, time
420
+ GREATER_THAN = 'GREATER THAN',
421
+ GREATER_THAN_OR_EQUAL = 'GREATER THAN OR EQUAL',
422
+ LESS_THAN = 'LESS THAN',
423
+ LESS_THAN_OR_EQUAL = 'LESS THAN OR EQUAL',
424
+
425
+ // none
426
+ IS_KNOWN = 'IS KNOWN',
427
+ IS_UNKNOWN = 'IS UNKNOWN',
428
+
429
+ // boolean
430
+ IS = 'IS',
431
+ IS_NOT = 'IS NOT',
432
+
433
+ // array
434
+ IS_ANY_OF = 'IS ANY OF',
435
+ IS_NOT_ANY_OF = 'IS NOT ANY OF',
436
+
437
+ // day
438
+ ON = 'ON',
439
+ }
440
+
441
+ type BaseCondition<T extends Triggers> = {
442
+ id: string;
443
+ variable_name: keyof WorkflowDataType<T> | null;
444
+ };
445
+
446
+ type StringCondition = BaseCondition<Triggers> & {
447
+ condition:
448
+ | 'CONTAINS'
449
+ | 'NCONTAINS'
450
+ | 'EQUALS'
451
+ | 'NEQUALS'
452
+ | 'STARTS_WITH'
453
+ | 'ENDS_WITH';
454
+ value: string;
455
+ variable_type: 'string';
456
+ };
457
+
458
+ type DateTimeNumberCondition = BaseCondition<Triggers> & {
459
+ condition:
460
+ | 'GREATER_THAN'
461
+ | 'GREATER_THAN_OR_EQUAL'
462
+ | 'LESS_THAN'
463
+ | 'LESS_THAN_OR_EQUAL';
464
+ } & (
465
+ | {
466
+ value: string;
467
+ variable_type: 'number';
468
+ }
469
+ | {
470
+ value: string;
471
+ timezone: string;
472
+ variable_type: 'date' | 'time';
473
+ }
474
+ );
475
+
476
+ type ExistsCondition = BaseCondition<Triggers> & {
477
+ condition: 'IS_KNOWN' | 'IS_UNKNOWN';
478
+ value: undefined | null;
479
+ variable_type: null;
480
+ };
481
+
482
+ type OnCondition = BaseCondition<Triggers> & {
483
+ condition: 'ON';
484
+ value: string | string[];
485
+ timezone: string;
486
+ variable_type: 'day';
487
+ };
488
+
489
+ type BooleanCondition = BaseCondition<Triggers> & {
490
+ condition: 'IS' | 'IS_NOT';
491
+ value: boolean | 'true' | 'false';
492
+ variable_type: 'boolean';
493
+ };
494
+
495
+ type ArrayCondition = BaseCondition<Triggers> & {
496
+ condition: 'IS_ANY_OF' | 'IS_NOT_ANY_OF';
497
+ value: string[];
498
+ variable_type: 'array';
499
+ };
500
+
501
+ export type ConditionLeaf =
502
+ | StringCondition
503
+ | DateTimeNumberCondition
504
+ | ExistsCondition
505
+ | OnCondition
506
+ | BooleanCondition
507
+ | ArrayCondition;
508
+
509
+ type ConditionOperator = 'AND' | 'OR';
510
+
511
+ export type SubConditions = {
512
+ operator: ConditionOperator;
513
+ conditions: Array<ConditionLeaf>;
514
+ };
515
+
516
+ export type WorkflowConditionGroup = {
517
+ operator: ConditionOperator;
518
+ conditions: Array<SubConditions>;
519
+ };
520
+
521
+ export function isConditionLeaf(
522
+ condition: ConditionLeaf | SubConditions
523
+ ): condition is ConditionLeaf {
524
+ return 'id' in condition && !('operator' in condition);
525
+ }
526
+
527
+ /***************************** WORKFLOW DATA TYPES *****************************/
528
+
529
+ export type RawDataTypes = {
530
+ message: Tables<'tbl_chat_messages'>;
531
+ reaction: Tables<'tbl_chat_reactions'>;
532
+ chat: Tables<'tbl_chats'>;
533
+ ticket: Tables<'tbl_chat_tickets'>;
534
+ task: Tables<'tbl_org_tasks'>;
535
+ sender: Tables<'tbl_contacts'>;
536
+ chat_label: Tables<'tbl_chat_properties'>;
537
+ };
538
+
539
+ type MessageRawDataType = Merge<
540
+ Pick<
541
+ Tables<'tbl_chat_messages'>,
542
+ | 'body'
543
+ | 'flag_status'
544
+ | 'has_quoted_msg'
545
+ | 'message_id'
546
+ | 'message_ticket_id'
547
+ | 'message_type'
548
+ | 'performed_by'
549
+ | 'sender_phone'
550
+ | 'timestamp'
551
+ | 'media'
552
+ >,
553
+ {
554
+ media: string | null;
555
+ flag_status: boolean;
556
+ has_quoted_msg: boolean;
557
+ message_type: MessageType['message_type'];
558
+ is_latest_message: boolean;
559
+ }
560
+ >;
561
+
562
+ type ReactionRawDataType = OverrideProperties<
563
+ Pick<Tables<'tbl_chat_reactions'>, 'reaction' | 'sender_id' | 'reaction_id'>,
564
+ {}
565
+ >;
566
+
567
+ type SenderRawDataType = Pick<
568
+ Merge<Tables<'tbl_contacts'>, Tables<'tbl_chat_participants'>>,
569
+ | 'contact_name'
570
+ | 'contact_id'
571
+ | 'contact_lid'
572
+ | 'contact_type'
573
+ | 'is_internal'
574
+ | 'is_wa_contact'
575
+ | 'is_admin'
576
+ | 'is_super_admin'
577
+ >;
578
+
579
+ type ChatRawDataType = Merge<
580
+ Pick<
581
+ Tables<'view_chats'>,
582
+ | 'assigned_to'
583
+ | 'chat_id'
584
+ | 'chat_name'
585
+ | 'chat_type'
586
+ | 'created_at'
587
+ | 'group_description'
588
+ | 'info_admins_only'
589
+ | 'org_id'
590
+ | 'org_phone'
591
+ | 'chat_org_phones'
592
+ | 'messages_admins_only'
593
+ | 'custom_properties'
594
+ | 'initiated_by'
595
+ >,
596
+ {
597
+ has_flagged_messages: boolean;
598
+ labels: string[];
599
+ members: string[];
600
+ custom_properties: { [key: string]: string } | null;
601
+ assignee_name: string | null;
602
+ is_latest_org_message: boolean;
603
+ }
604
+ >;
605
+
606
+ type TicketRawDataType = Merge<
607
+ Pick<
608
+ Tables<'tbl_chat_tickets'>,
609
+ | 'ticket_id'
610
+ | 'assignee'
611
+ | 'status'
612
+ | 'priority'
613
+ | 'closed_at'
614
+ | 'custom_properties'
615
+ | 'quoted_message_id'
616
+ | 'subject'
617
+ | 'raised_by'
618
+ | 'is_deleted'
619
+ | 'due_date'
620
+ >,
621
+ {
622
+ labels: string[];
623
+ custom_properties: { [key: string]: string } | null;
624
+ assignee_name: string | null;
625
+ }
626
+ >;
627
+
628
+ type TaskRawDataType = Pick<
629
+ TaskType,
630
+ | 'assignee'
631
+ | 'notes'
632
+ | 'priority'
633
+ | 'due_date'
634
+ | 'remind_at'
635
+ | 'title'
636
+ | 'type'
637
+ | 'status'
638
+ >;
639
+
640
+ export type WorkflowRawDataTypes = {
641
+ message: MessageRawDataType;
642
+ reaction: ReactionRawDataType;
643
+ chat: ChatRawDataType;
644
+ ticket: TicketRawDataType;
645
+ task: TaskRawDataType;
646
+ sender: SenderRawDataType;
647
+ };
648
+
649
+ export type MessageWorkflowDataTypes = AppendTypes<
650
+ {
651
+ message: WorkflowRawDataTypes['message'];
652
+ sender: WorkflowRawDataTypes['sender'];
653
+ chat: WorkflowRawDataTypes['chat'];
654
+ },
655
+ '.'
656
+ >;
657
+
658
+ export type ReactionWorkflowDataTypes = AppendTypes<
659
+ {
660
+ reaction: WorkflowRawDataTypes['reaction'];
661
+ sender: WorkflowRawDataTypes['sender'];
662
+ message: WorkflowRawDataTypes['message'];
663
+ chat: WorkflowRawDataTypes['chat'];
664
+ },
665
+ '.'
666
+ >;
667
+
668
+ export type TicketWorkflowDataTypes = AppendTypes<
669
+ {
670
+ ticket: WorkflowRawDataTypes['ticket'];
671
+ message: WorkflowRawDataTypes['message'];
672
+ chat: WorkflowRawDataTypes['chat'];
673
+ sender: WorkflowRawDataTypes['sender'];
674
+ },
675
+ '.'
676
+ >;
677
+
678
+ export type TaskWorkflowDataTypes = AppendTypes<
679
+ {
680
+ task: WorkflowRawDataTypes['task'];
681
+ message?: WorkflowRawDataTypes['message'];
682
+ ticket?: WorkflowRawDataTypes['ticket'];
683
+ chat?: WorkflowRawDataTypes['chat'];
684
+ sender?: WorkflowRawDataTypes['sender'];
685
+ },
686
+ '.'
687
+ >;
688
+
689
+ export type ChatWorkflowDataTypes = AppendTypes<
690
+ {
691
+ chat: WorkflowRawDataTypes['chat'];
692
+ },
693
+ '.'
694
+ >;
695
+
696
+ export enum Triggers {
697
+ MESSAGE_CREATED = 'message.created',
698
+ MESSAGE_UPDATED = 'message.updated',
699
+ MESSAGE_DELETED = 'message.deleted',
700
+ MESSAGE_FLAGGED = 'message.flagged',
701
+ MESSAGE_UNFLAGGED = 'message.unflagged',
702
+ REACTION_ADDED = 'reaction.added',
703
+ TICKET_CREATED = 'ticket.created',
704
+ TICKET_UPDATED = 'ticket.updated',
705
+ TICKET_DELETED = 'ticket.deleted',
706
+ TICKET_CLOSED = 'ticket.closed',
707
+ TICKET_DUE = 'ticket.due',
708
+ CHAT_CREATED = 'chat.created',
709
+ CHAT_LABEL_UPDATED = 'chat.label.updated',
710
+ CHAT_CLOSED = 'chat.closed',
711
+ TASK_CREATED = 'task.created',
712
+ TASK_DUE = 'task.due',
713
+ }
714
+
715
+ export type WorkflowDataType<T extends Triggers> = T extends
716
+ | Triggers.MESSAGE_CREATED
717
+ | Triggers.MESSAGE_DELETED
718
+ | Triggers.MESSAGE_UPDATED
719
+ | Triggers.MESSAGE_FLAGGED
720
+ | Triggers.MESSAGE_UNFLAGGED
721
+ ? MessageWorkflowDataTypes
722
+ : T extends
723
+ | Triggers.CHAT_CREATED
724
+ | Triggers.CHAT_LABEL_UPDATED
725
+ | Triggers.CHAT_CLOSED
726
+ ? ChatWorkflowDataTypes
727
+ : T extends
728
+ | Triggers.TICKET_CREATED
729
+ | Triggers.TICKET_UPDATED
730
+ | Triggers.TICKET_DELETED
731
+ | Triggers.TICKET_CLOSED
732
+ | Triggers.TICKET_DUE
733
+ ? TicketWorkflowDataTypes
734
+ : T extends Triggers.REACTION_ADDED
735
+ ? ReactionWorkflowDataTypes
736
+ : T extends Triggers.TASK_CREATED | Triggers.TASK_DUE
737
+ ? TaskWorkflowDataTypes
738
+ : never;
739
+
740
+ /***************************** ACTION TYPES *****************************/
741
+
742
+ export type SendMessageToChatWorkflowAction = {
743
+ type: 'send_message_to_chat';
744
+ metadata: {
745
+ message: string;
746
+ media?: {
747
+ url: string;
748
+ type: 'image' | 'video' | 'audio' | 'document';
749
+ name: string;
750
+ } | null;
751
+ debounce?: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}` | null;
752
+ chat_id: string | null;
753
+ unflag_chat?: boolean;
754
+ as_reply?: boolean;
755
+ };
756
+ };
757
+
758
+ export type CreateTicketWorkflowAction = {
759
+ type: 'create_ticket';
760
+ metadata: {
761
+ subject: string | null;
762
+ assignee:
763
+ | {
764
+ round_robin: true;
765
+ emails: string[];
766
+ assignee_check_for?: 'shift_times' | 'online_offline' | 'all';
767
+ }
768
+ | {
769
+ email: string | null;
770
+ round_robin: false;
771
+ };
772
+ priority?: '0' | '1' | '2' | '3' | '4';
773
+ status: 'open' | 'inprogress' | 'closed';
774
+ labels: string[];
775
+ due_in: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}` | null;
776
+ message_source: string | null;
777
+ };
778
+ };
779
+
780
+ export type AttachToLatestTicketWorkflowAction = {
781
+ type: 'attach_to_latest_ticket';
782
+ metadata: {
783
+ status: ('open' | 'inprogress' | 'closed')[];
784
+ };
785
+ };
786
+
787
+ export type NotifyHTTPWorkflowAction = {
788
+ type: 'notify_http';
789
+ metadata: {
790
+ url: string;
791
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
792
+ headers?: { id: string; key: string; value: string }[] | null;
793
+ body?: string | { id: string; key: string; value: string }[] | null;
794
+ body_layout?: 'json' | 'fields';
795
+ };
796
+ };
797
+
798
+ export type FlagMessageWorkflowAction = {
799
+ type: 'flag_message';
800
+ metadata: {
801
+ message_id?: string;
802
+ };
803
+ };
804
+
805
+ export type UnflagMessageWorkflowAction = {
806
+ type: 'unflag_message';
807
+ metadata: {
808
+ message_id?: string;
809
+ };
810
+ };
811
+
812
+ export type AssignTicketWorkflowAction = {
813
+ type: 'assign_ticket';
814
+ metadata: {
815
+ assignee:
816
+ | {
817
+ round_robin: true;
818
+ emails: string[];
819
+ assignee_check_for?: 'shift_times' | 'online_offline' | 'all';
820
+ }
821
+ | {
822
+ email: string | null;
823
+ round_robin: false;
824
+ };
825
+ };
826
+ };
827
+
828
+ export type CloseTicketWorkflowAction = {
829
+ type: 'close_ticket';
830
+ metadata: {
831
+ bypass_mandatory_fields?: boolean;
832
+ closing_note: string;
833
+ };
834
+ };
835
+
836
+ export type AddChatLabelWorkflowAction = {
837
+ type: 'add_chat_label';
838
+ metadata: {
839
+ labels: string[];
840
+ };
841
+ };
842
+
843
+ export type RemoveChatLabelWorkflowAction = {
844
+ type: 'remove_chat_label';
845
+ metadata: {
846
+ labels: string[];
847
+ };
848
+ };
849
+
850
+ export type AddTicketLabelWorkflowAction = {
851
+ type: 'add_ticket_label';
852
+ metadata: {
853
+ labels: string[];
854
+ };
855
+ };
856
+
857
+ export type RemoveTicketLabelWorkflowAction = {
858
+ type: 'remove_ticket_label';
859
+ metadata: {
860
+ labels: string[];
861
+ };
862
+ };
863
+
864
+ export type AssignChatWorkflowAction = {
865
+ type: 'assign_chat';
866
+ metadata: {
867
+ assignee:
868
+ | {
869
+ round_robin: true;
870
+ emails: string[];
871
+ assignee_check_for?: 'shift_times' | 'online_offline' | 'all';
872
+ }
873
+ | {
874
+ email: string | null;
875
+ round_robin: false;
876
+ };
877
+ };
878
+ };
879
+
880
+ export type ForwardMessageWorkflowAction = {
881
+ type: 'forward_message';
882
+ metadata: {
883
+ chat_id: string | null;
884
+ };
885
+ };
886
+
887
+ export type SendEmailWorkflowAction = {
888
+ type: 'send_email';
889
+ metadata: {
890
+ email: string;
891
+ subject: string;
892
+ body: string;
893
+ };
894
+ };
895
+
896
+ export type DeleteMessageWorkflowAction = {
897
+ type: 'delete_message';
898
+ metadata: {};
899
+ };
900
+
901
+ export type CloseChatWorkflowAction = {
902
+ type: 'close_chat';
903
+ metadata: {};
904
+ };
905
+
906
+ export type ValidateWorkflowAction = {
907
+ type: 'validate_action';
908
+ metadata: WorkflowConditionGroup;
909
+ };
910
+
911
+ export type DelayWorkflowAction = {
912
+ type: 'delay';
913
+ metadata: {
914
+ delay: `${number} ${'seconds' | 'minutes' | 'hours' | 'days'}`;
915
+ };
916
+ };
917
+
918
+ export type SendSlackNotificationWorkflowAction = {
919
+ type: 'send_slack_notification';
920
+ metadata: {
921
+ slack_payload: string;
922
+ url: string;
923
+ };
924
+ };
925
+
926
+ export type SplitPathWorkflowAction = {
927
+ type: 'split_path';
928
+ metadata: {
929
+ paths: Array<{
930
+ id: string;
931
+ condition_action_id: string;
932
+ }>;
933
+ };
934
+ };
935
+
936
+ // experimental
937
+ export type AIPromptWorkflowAction = {
938
+ type: 'ai_prompt';
939
+ metadata: {
940
+ query: string;
941
+ body: string;
942
+ };
943
+ };
944
+
945
+ export type NormalWorkflowActionTypes =
946
+ | SendMessageToChatWorkflowAction
947
+ | CreateTicketWorkflowAction
948
+ | AttachToLatestTicketWorkflowAction
949
+ | NotifyHTTPWorkflowAction
950
+ | FlagMessageWorkflowAction
951
+ | UnflagMessageWorkflowAction
952
+ | AssignTicketWorkflowAction
953
+ | CloseTicketWorkflowAction
954
+ | AddChatLabelWorkflowAction
955
+ | RemoveChatLabelWorkflowAction
956
+ | AddTicketLabelWorkflowAction
957
+ | RemoveTicketLabelWorkflowAction
958
+ | AssignChatWorkflowAction
959
+ | ForwardMessageWorkflowAction
960
+ | SendEmailWorkflowAction
961
+ | DeleteMessageWorkflowAction
962
+ | CloseChatWorkflowAction
963
+ | ValidateWorkflowAction
964
+ | DelayWorkflowAction
965
+ | SendSlackNotificationWorkflowAction
966
+ | AIPromptWorkflowAction;
967
+
968
+ export type WorkflowActionsTypeMap = {
969
+ send_message_to_chat: SendMessageToChatWorkflowAction;
970
+ create_ticket: CreateTicketWorkflowAction;
971
+ attach_to_latest_ticket: AttachToLatestTicketWorkflowAction;
972
+ notify_http: NotifyHTTPWorkflowAction;
973
+ flag_message: FlagMessageWorkflowAction;
974
+ unflag_message: UnflagMessageWorkflowAction;
975
+ assign_ticket: AssignTicketWorkflowAction;
976
+ close_ticket: CloseTicketWorkflowAction;
977
+ add_chat_label: AddChatLabelWorkflowAction;
978
+ remove_chat_label: RemoveChatLabelWorkflowAction;
979
+ add_ticket_label: AddTicketLabelWorkflowAction;
980
+ remove_ticket_label: RemoveTicketLabelWorkflowAction;
981
+ assign_chat: AssignChatWorkflowAction;
982
+ forward_message: ForwardMessageWorkflowAction;
983
+ send_email: SendEmailWorkflowAction;
984
+ delete_message: DeleteMessageWorkflowAction;
985
+ close_chat: CloseChatWorkflowAction;
986
+ validate_action: ValidateWorkflowAction;
987
+ delay: DelayWorkflowAction;
988
+ send_slack_notification: SendSlackNotificationWorkflowAction;
989
+ ai_prompt: AIPromptWorkflowAction;
990
+ split_path: SplitPathWorkflowAction;
991
+ };
992
+
993
+ export const ActionCategoryMap: Record<
994
+ WorkflowActionTypes['type'],
995
+ WorkflowActionCategory
996
+ > = {
997
+ send_message_to_chat: 'message',
998
+ create_ticket: 'ticket',
999
+ attach_to_latest_ticket: 'ticket',
1000
+ notify_http: 'custom',
1001
+ flag_message: 'message',
1002
+ unflag_message: 'message',
1003
+ assign_ticket: 'ticket',
1004
+ close_ticket: 'ticket',
1005
+ add_chat_label: 'chat',
1006
+ remove_chat_label: 'chat',
1007
+ add_ticket_label: 'ticket',
1008
+ remove_ticket_label: 'ticket',
1009
+ assign_chat: 'chat',
1010
+ forward_message: 'message',
1011
+ send_email: 'custom',
1012
+ delete_message: 'message',
1013
+ close_chat: 'chat',
1014
+ validate_action: 'flow',
1015
+ delay: 'flow',
1016
+ send_slack_notification: 'custom',
1017
+ ai_prompt: 'ai',
1018
+ split_path: 'flow',
1019
+ };
1020
+
1021
+ export const CategoryNameMap: Record<WorkflowActionCategory, string> = {
1022
+ search: 'Search',
1023
+ message: 'Message',
1024
+ ticket: 'Ticket',
1025
+ chat: 'Chat',
1026
+ custom: 'Custom',
1027
+ flow: 'Flow',
1028
+ ai: 'AI',
1029
+ };
1030
+
1031
+ export const TriggerNameMap: Record<Triggers, string> = {
1032
+ 'chat.created': 'Chat Created',
1033
+ 'message.created': 'Message Created',
1034
+ 'chat.closed': 'Chat Closed',
1035
+ 'chat.label.updated': 'Chat Label Updated',
1036
+ 'message.deleted': 'Message Deleted',
1037
+ 'message.flagged': 'Message Flagged',
1038
+ 'message.unflagged': 'Message Unflagged',
1039
+ 'message.updated': 'Message Updated',
1040
+ 'reaction.added': 'Reaction Added',
1041
+ 'ticket.closed': 'Ticket Closed',
1042
+ 'ticket.created': 'Ticket Created',
1043
+ 'ticket.deleted': 'Ticket Deleted',
1044
+ 'ticket.due': 'Ticket Due',
1045
+ 'ticket.updated': 'Ticket Updated',
1046
+ 'task.created': 'Task Created',
1047
+ 'task.due': 'Task Due',
1048
+ };
1049
+
1050
+ export const WorkflowActionNameMap: Record<
1051
+ WorkflowType['actions'][number]['action_metadata']['type'],
1052
+ {
1053
+ title: string;
1054
+ description: string;
1055
+ }
1056
+ > = {
1057
+ add_chat_label: {
1058
+ title: 'Add Chat Label',
1059
+ description: 'Add a label to the trigger chat',
1060
+ },
1061
+ add_ticket_label: {
1062
+ title: 'Add Ticket Label',
1063
+ description: 'Add a label to the trigger ticket',
1064
+ },
1065
+ assign_chat: {
1066
+ title: 'Assign Chat',
1067
+ description: 'Assign a chat to org user',
1068
+ },
1069
+ attach_to_latest_ticket: {
1070
+ title: 'Attach to Latest Ticket',
1071
+ description: 'Attach to the latest ticket of trigger chat',
1072
+ },
1073
+ close_chat: {
1074
+ title: 'Close Chat',
1075
+ description: 'Close the chat',
1076
+ },
1077
+ close_ticket: {
1078
+ title: 'Close Ticket',
1079
+ description: 'Close the trigger ticket or ticket attached to the message',
1080
+ },
1081
+ create_ticket: {
1082
+ title: 'Create Ticket',
1083
+ description: 'Create a ticket attached to the trigger message',
1084
+ },
1085
+ delay: {
1086
+ title: 'Delay',
1087
+ description: 'Add a delay to next sequential actions',
1088
+ },
1089
+ ai_prompt: {
1090
+ title: 'AI Prompt',
1091
+ description: 'Analyse, filter, categorise, and generate text using AI',
1092
+ },
1093
+ flag_message: {
1094
+ title: 'Flag Message',
1095
+ description: 'Flag the trigger message or message attached to the ticket',
1096
+ },
1097
+ remove_chat_label: {
1098
+ title: 'Remove Chat Label',
1099
+ description: 'Remove a label from the trigger chat',
1100
+ },
1101
+ remove_ticket_label: {
1102
+ title: 'Remove Ticket Label',
1103
+ description:
1104
+ 'Remove a label from the trigger ticket or ticket attached to the message',
1105
+ },
1106
+ send_email: {
1107
+ title: 'Send Email',
1108
+ description: 'Send custom text to an email',
1109
+ },
1110
+ send_message_to_chat: {
1111
+ title: 'Send Message to Chat',
1112
+ description: 'Send a message to the chat attached to the trigger',
1113
+ },
1114
+ send_slack_notification: {
1115
+ title: 'Send Slack Notification',
1116
+ description: 'Send a slack notification',
1117
+ },
1118
+ split_path: {
1119
+ title: 'Split Path',
1120
+ description: 'Split the workflow path based on the condition',
1121
+ },
1122
+ unflag_message: {
1123
+ title: 'Unflag Message',
1124
+ description: 'Unflag the trigger message or message attached to the ticket',
1125
+ },
1126
+ validate_action: {
1127
+ title: 'Validate Action',
1128
+ description: 'Validate the action based on multiple criterias',
1129
+ },
1130
+ notify_http: {
1131
+ title: 'Notify HTTP',
1132
+ description: 'Send a custom HTTP request to an endpoint',
1133
+ },
1134
+ assign_ticket: {
1135
+ title: 'Assign Ticket',
1136
+ description: 'Assign a ticket to an org user',
1137
+ },
1138
+ forward_message: {
1139
+ title: 'Forward Message',
1140
+ description: 'Forward a message to a chat',
1141
+ },
1142
+ delete_message: {
1143
+ title: 'Delete Message',
1144
+ description: 'Delete the trigger message or message attached to the ticket',
1145
+ },
1146
+ };
1147
+
1148
+ export const ActionResponseVariables = {
1149
+ send_message_to_chat: {
1150
+ 'Queue ID': 'queue_id',
1151
+ 'Queue Position': 'queue_position',
1152
+ 'Chat ID': 'chat_id',
1153
+ 'Org Phone': 'org_phone',
1154
+ },
1155
+ create_ticket: {
1156
+ 'Ticket ID': 'ticket_id',
1157
+ 'Ticket Subject': 'ticket_subject',
1158
+ 'Chat ID': 'chat_id',
1159
+ },
1160
+ forward_message: {
1161
+ 'Queue ID': 'queue_id',
1162
+ 'Queue Position': 'queue_position',
1163
+ 'Chat ID': 'chat_id',
1164
+ 'Org Phone': 'org_phone',
1165
+ },
1166
+ validate_action: {
1167
+ 'Is Valid': 'is_valid',
1168
+ },
1169
+ };
1170
+
1171
+ const ChatCreatedVariables: Record<keyof ChatWorkflowDataTypes, string> = {
1172
+ 'chat.chat_id': 'Chat ID',
1173
+ 'chat.chat_name': 'Chat Name',
1174
+ 'chat.chat_type': 'Chat Type',
1175
+ 'chat.members': 'Chat Members',
1176
+ 'chat.has_flagged_messages': 'Chat Has Flagged Messages',
1177
+ 'chat.labels': 'Chat Labels',
1178
+ 'chat.assigned_to': 'Chat Assigned To',
1179
+ 'chat.created_at': 'Chat Created At',
1180
+ 'chat.chat_org_phones': 'Chat Org Phones',
1181
+ 'chat.assignee_name': 'Chat Assignee Name',
1182
+ 'chat.custom_properties': 'Chat Custom Properties',
1183
+ 'chat.group_description': 'Chat Group Description',
1184
+ 'chat.info_admins_only': 'Chat Info Admins Only',
1185
+ 'chat.initiated_by': 'Chat Initiated By',
1186
+ 'chat.messages_admins_only': 'Chat Messages Admins Only',
1187
+ 'chat.org_id': 'Chat Org ID',
1188
+ 'chat.org_phone': 'Chat Org Phone',
1189
+ 'chat.is_latest_org_message': 'Chat Latest Message Self',
1190
+ };
1191
+
1192
+ const SenderCreatedVariables: Record<
1193
+ keyof AppendTypes<{ sender: SenderRawDataType }, '.'>,
1194
+ string
1195
+ > = {
1196
+ 'sender.contact_name': 'Sender Contact Name',
1197
+ 'sender.contact_id': 'Sender Contact ID',
1198
+ 'sender.contact_lid': 'Sender Contact LID',
1199
+ 'sender.contact_type': 'Sender Contact Type',
1200
+ 'sender.is_internal': 'Sender Is Internal',
1201
+ 'sender.is_wa_contact': 'Sender Is WA Contact',
1202
+ 'sender.is_admin': 'Sender Is Admin',
1203
+ 'sender.is_super_admin': 'Sender Is Super Admin',
1204
+ };
1205
+
1206
+ const MessageCreatedVariables: Record<
1207
+ keyof AppendTypes<{ message: MessageRawDataType }, '.'>,
1208
+ string
1209
+ > = {
1210
+ 'message.body': 'Message Body',
1211
+ 'message.flag_status': 'Message Flag Status',
1212
+ 'message.has_quoted_msg': 'Message Has Quoted Msg',
1213
+ 'message.message_type': 'Message Type',
1214
+ 'message.media': 'Message Media',
1215
+ 'message.message_id': 'Message ID',
1216
+ 'message.message_ticket_id': 'Message Ticket ID',
1217
+ 'message.performed_by': 'Message Performed By',
1218
+ 'message.sender_phone': 'Message Sender Phone',
1219
+ 'message.timestamp': 'Message Timestamp',
1220
+ 'message.is_latest_message': 'Message is Latest Message',
1221
+ };
1222
+
1223
+ const ReactionAddedVariables: Record<
1224
+ keyof AppendTypes<{ reaction: ReactionRawDataType }, '.'>,
1225
+ string
1226
+ > = {
1227
+ 'reaction.reaction': 'Reaction',
1228
+ 'reaction.sender_id': 'Reaction Sender ID',
1229
+ 'reaction.reaction_id': 'Reaction ID',
1230
+ };
1231
+
1232
+ const TicketCreatedVariables: Record<
1233
+ keyof AppendTypes<{ ticket: TicketRawDataType }, '.'>,
1234
+ string
1235
+ > = {
1236
+ 'ticket.ticket_id': 'Ticket ID',
1237
+ 'ticket.subject': 'Ticket Subject',
1238
+ 'ticket.assignee': 'Ticket Assignee',
1239
+ 'ticket.status': 'Ticket Status',
1240
+ 'ticket.priority': 'Ticket Priority',
1241
+ 'ticket.closed_at': 'Ticket Closed At',
1242
+ 'ticket.custom_properties': 'Ticket Custom Properties',
1243
+ 'ticket.labels': 'Ticket Labels',
1244
+ 'ticket.quoted_message_id': 'Ticket Quoted Message ID',
1245
+ 'ticket.raised_by': 'Ticket Raised By',
1246
+ 'ticket.is_deleted': 'Ticket Is Deleted',
1247
+ 'ticket.due_date': 'Ticket Due Date',
1248
+ 'ticket.assignee_name': 'Ticket Assignee Name',
1249
+ };
1250
+
1251
+ const TaskCreatedVariables: Record<
1252
+ keyof AppendTypes<{ task: TaskRawDataType }, '.'>,
1253
+ string
1254
+ > = {
1255
+ 'task.assignee': 'Task Assignee',
1256
+ 'task.notes': 'Task Notes',
1257
+ 'task.priority': 'Task Priority',
1258
+ 'task.due_date': 'Task Due Date',
1259
+ 'task.remind_at': 'Task Remind At',
1260
+ 'task.title': 'Task Title',
1261
+ 'task.type': 'Task Type',
1262
+ 'task.status': 'Task Status',
1263
+ };
1264
+
1265
+ export const TriggerVariables: Record<
1266
+ 'message' | 'chat' | 'ticket' | 'reaction' | 'task',
1267
+ Record<string, string>
1268
+ > = {
1269
+ message: {
1270
+ ...MessageCreatedVariables,
1271
+ ...ChatCreatedVariables,
1272
+ ...SenderCreatedVariables,
1273
+ },
1274
+ chat: {
1275
+ ...ChatCreatedVariables,
1276
+ },
1277
+ ticket: {
1278
+ ...TicketCreatedVariables,
1279
+ ...MessageCreatedVariables,
1280
+ ...ChatCreatedVariables,
1281
+ ...SenderCreatedVariables,
1282
+ },
1283
+ reaction: {
1284
+ ...ReactionAddedVariables,
1285
+ ...MessageCreatedVariables,
1286
+ ...ChatCreatedVariables,
1287
+ ...SenderCreatedVariables,
1288
+ },
1289
+ task: {
1290
+ ...TaskCreatedVariables,
1291
+ ...MessageCreatedVariables,
1292
+ ...ChatCreatedVariables,
1293
+ ...SenderCreatedVariables,
1294
+ ...TicketCreatedVariables,
1295
+ },
1296
+ };