-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkflow.zod.ts
More file actions
279 lines (254 loc) · 9.05 KB
/
workflow.zod.ts
File metadata and controls
279 lines (254 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { SnakeCaseIdentifierSchema } from '../shared/identifiers.zod';
/**
* Trigger events for workflow automation
*/
export const WorkflowTriggerType = z.enum([
'on_create', // When record is created
'on_update', // When record is updated
'on_create_or_update', // Both
'on_delete', // When record is deleted
'schedule' // Time-based (cron)
]);
/**
* Schema for Workflow Field Update Action
* @example
* {
* name: "update_status",
* type: "field_update",
* field: "status",
* value: "approved"
* }
*/
export const FieldUpdateActionSchema = z.object({
name: z.string().describe('Action name'),
type: z.literal('field_update'),
field: z.string().describe('Field to update'),
value: z.unknown().describe('Value or Formula to set'),
});
/**
* Schema for Workflow Email Alert Action
* @example
* {
* name: "send_approval_email",
* type: "email_alert",
* template: "approval_request_email",
* recipients: ["user_id_123", "manager_field"]
* }
*/
export const EmailAlertActionSchema = z.object({
name: z.string().describe('Action name'),
type: z.literal('email_alert'),
template: z.string().describe('Email template ID/DevName'),
recipients: z.array(z.string()).describe('List of recipient emails or user IDs'),
});
/**
* Schema for Connector Action Reference
* Executes a capability defined in an integration connector.
* Replaces hardcoded vendor actions (Slack, Twilio, etc).
*
* @example Send Slack Message
* {
* name: "notify_slack",
* type: "connector_action",
* connectorId: "slack",
* actionId: "post_message",
* input: {
* channel: "#general",
* text: "New deal closed: {name}"
* }
* }
*/
export const ConnectorActionRefSchema = z.object({
name: z.string().describe('Action name'),
type: z.literal('connector_action'),
connectorId: z.string().describe('Target Connector ID (e.g. slack, twilio)'),
actionId: z.string().describe('Target Action ID (e.g. send_message)'),
input: z.record(z.string(), z.unknown()).describe('Input parameters matching the action schema'),
});
/**
* Schema for HTTP Callout Action
* Makes a REST API call to an external service.
* @example
* {
* name: "sync_to_erp",
* type: "http_call",
* url: "https://erp.api/orders",
* method: "POST",
* headers: { "Authorization": "Bearer {token}" },
* body: "{ ... }"
* }
*/
export const HttpCallActionSchema = z.object({
name: z.string().describe('Action name'),
type: z.literal('http_call'),
url: z.string().describe('Target URL'),
method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']).default('POST').describe('HTTP Method'),
headers: z.record(z.string(), z.string()).optional().describe('HTTP Headers'),
body: z.string().optional().describe('Request body (JSON or text)'),
});
/**
* Schema for Workflow Task Creation Action
* @example
* {
* name: "create_followup_task",
* type: "task_creation",
* taskObject: "tasks",
* subject: "Follow up with client",
* dueDate: "TODAY() + 3"
* }
*/
export const TaskCreationActionSchema = z.object({
name: z.string().describe('Action name'),
type: z.literal('task_creation'),
taskObject: z.string().describe('Task object name (e.g., "task", "project_task")'),
subject: z.string().describe('Task subject/title'),
description: z.string().optional().describe('Task description'),
assignedTo: z.string().optional().describe('User ID or field reference for assignee'),
dueDate: z.string().optional().describe('Due date (ISO string or formula)'),
priority: z.string().optional().describe('Task priority'),
relatedTo: z.string().optional().describe('Related record ID or field reference'),
additionalFields: z.record(z.string(), z.unknown()).optional().describe('Additional custom fields'),
});
/**
* Schema for Workflow Push Notification Action
*/
export const PushNotificationActionSchema = z.object({
name: z.string().describe('Action name'),
type: z.literal('push_notification'),
title: z.string().describe('Notification title'),
body: z.string().describe('Notification body text'),
recipients: z.array(z.string()).describe('User IDs or device tokens'),
data: z.record(z.string(), z.unknown()).optional().describe('Additional data payload'),
badge: z.number().optional().describe('Badge count (iOS)'),
sound: z.string().optional().describe('Notification sound'),
clickAction: z.string().optional().describe('Action/URL when notification is clicked'),
});
/**
* Schema for Workflow Custom Script Action
*/
export const CustomScriptActionSchema = z.object({
name: z.string().describe('Action name'),
type: z.literal('custom_script'),
language: z.enum(['javascript', 'typescript', 'python']).default('javascript').describe('Script language'),
code: z.string().describe('Script code to execute'),
timeout: z.number().default(30000).describe('Execution timeout in milliseconds'),
context: z.record(z.string(), z.unknown()).optional().describe('Additional context variables'),
});
/**
* Universal Workflow Action Schema
* Union of all supported action types.
*/
export const WorkflowActionSchema = z.discriminatedUnion('type', [
FieldUpdateActionSchema,
EmailAlertActionSchema,
HttpCallActionSchema,
ConnectorActionRefSchema,
TaskCreationActionSchema,
PushNotificationActionSchema,
CustomScriptActionSchema,
]);
export type WorkflowAction = z.infer<typeof WorkflowActionSchema>;
/**
* Time Trigger Definition
* Schedules actions to run relative to a specific time or date field.
*/
export const TimeTriggerSchema = z.object({
id: z.string().optional().describe('Unique identifier'),
/** Timing Logic */
timeLength: z.number().int().describe('Duration amount (e.g. 1, 30)'),
timeUnit: z.enum(['minutes', 'hours', 'days']).describe('Unit of time'),
/** Reference Point */
offsetDirection: z.enum(['before', 'after']).describe('Before or After the reference date'),
offsetFrom: z.enum(['trigger_date', 'date_field']).describe('Basis for calculation'),
dateField: z.string().optional().describe('Date field to calculate from (required if offsetFrom is date_field)'),
/** Actions */
actions: z.array(WorkflowActionSchema).describe('Actions to execute at the scheduled time'),
});
/**
* Schema for Workflow Rules (Automation)
*
* **NAMING CONVENTION:**
* Workflow names are machine identifiers and must be lowercase snake_case.
*
* @example Good workflow names
* - 'send_welcome_email'
* - 'update_lead_status'
* - 'notify_manager_on_close'
* - 'calculate_discount'
*
* @example Bad workflow names (will be rejected)
* - 'SendWelcomeEmail' (PascalCase)
* - 'updateLeadStatus' (camelCase)
* - 'Send Welcome Email' (spaces)
*
* @example Complete Workflow
* {
* name: "new_lead_process",
* objectName: "lead",
* triggerType: "on_create",
* criteria: "amount > 1000",
* active: true,
* actions: [
* {
* name: "set_status",
* type: "field_update",
* field: "status",
* value: "new"
* },
* {
* name: "notify_team",
* type: "connector_action",
* connectorId: "slack",
* actionId: "post_message",
* input: { channel: "#sales", text: "New high value lead!" }
* }
* ],
* timeTriggers: [
* {
* timeLength: 2,
* timeUnit: "days",
* offsetDirection: "after",
* offsetFrom: "trigger_date",
* actions: [
* {
* name: "followup_check",
* type: "task_creation",
* taskObject: "task",
* subject: "Follow up lead",
* dueDate: "TODAY()"
* }
* ]
* }
* ]
* }
*/
export const WorkflowRuleSchema = z.object({
/** Machine name */
name: SnakeCaseIdentifierSchema.describe('Unique workflow name (lowercase snake_case)'),
/** Target Object */
objectName: z.string().describe('Target Object'),
/** When to evaluate the rule */
triggerType: WorkflowTriggerType.describe('When to evaluate'),
/**
* Condition to start the workflow.
* If empty, runs on every trigger event.
*/
criteria: z.string().optional().describe('Formula condition. If TRUE, actions execute.'),
/** Actions to execute immediately */
actions: z.array(WorkflowActionSchema).optional().describe('Immediate actions'),
/**
* Time-Dependent Actions
* Actions scheduled to run in the future.
*/
timeTriggers: z.array(TimeTriggerSchema).optional().describe('Scheduled actions relative to trigger or date field'),
/** Active status */
active: z.boolean().default(true).describe('Whether this workflow is active'),
/** Execution Order */
executionOrder: z.number().int().min(0).default(100).describe('Deterministic execution order when multiple workflows match (lower runs first)'),
/** Recursion Control */
reevaluateOnChange: z.boolean().default(false).describe('Re-evaluate rule if field updates change the record validity'),
});
export type WorkflowRule = z.infer<typeof WorkflowRuleSchema>;
export type TimeTrigger = z.infer<typeof TimeTriggerSchema>;