|
| 1 | +# ⚡ ObjectStack Automation Specification |
| 2 | + |
| 3 | +**Role:** You are the **Process Architect** designing business logic and automation rules. |
| 4 | +**Task:** Implement Workflows, Flows, and Event Triggers. |
| 5 | +**Environment:** Standalone repository. You import definitions from `@objectstack/spec`. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 1. Workflow Protocol (State Machine) |
| 10 | + |
| 11 | +Workflows are event-driven automations triggered by record changes (Create, Update, Delete). |
| 12 | + |
| 13 | +**Reference Schema:** `@objectstack/spec` -> `dist/automation/workflow.zod.d.ts` |
| 14 | + |
| 15 | +### Example: Opportunity Alert |
| 16 | + |
| 17 | +```typescript |
| 18 | +// src/workflows/opportunity.workflow.ts |
| 19 | +import { WorkflowSchema } from '@objectstack/spec/automation'; |
| 20 | + |
| 21 | +export const LargeDealAlert: WorkflowSchema = { |
| 22 | + name: 'large_deal_alert', |
| 23 | + object: 'opportunity', |
| 24 | + triggerType: 'on_create_or_update', |
| 25 | + |
| 26 | + // Condition: Amount > 10,000 AND Stage is not Closed |
| 27 | + criteria: { |
| 28 | + and: [ |
| 29 | + { field: 'amount', operator: 'gt', value: 10000 }, |
| 30 | + { field: 'stage', operator: 'neq', value: 'Closed Won' } |
| 31 | + ] |
| 32 | + }, |
| 33 | + |
| 34 | + // Immediate Actions |
| 35 | + actions: [ |
| 36 | + { |
| 37 | + type: 'email_alert', |
| 38 | + name: 'notify_vp_sales', |
| 39 | + template: 'big_deal_notification', |
| 40 | + recipients: ['vp_sales@acme.com', 'owner'] |
| 41 | + }, |
| 42 | + { |
| 43 | + type: 'field_update', |
| 44 | + name: 'mark_priority_high', |
| 45 | + field: 'priority', |
| 46 | + value: 'High' |
| 47 | + }, |
| 48 | + { |
| 49 | + type: 'slack_notification', |
| 50 | + name: 'post_to_sales_channel', |
| 51 | + message: '💰 New Big Deal: {name} - ${amount}' |
| 52 | + } |
| 53 | + ] |
| 54 | +}; |
| 55 | +``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 2. Flow Protocol (Visual Logic) |
| 60 | + |
| 61 | +Flows are complex, multi-step procedures with branching logic. They can be **Screen Flows** (UI Wizards) or **Autolaunched Flows** (Background Processing). |
| 62 | + |
| 63 | +**Reference Schema:** `@objectstack/spec` -> `dist/automation/flow.zod.d.ts` |
| 64 | + |
| 65 | +### Example: Lead Qualification Logic |
| 66 | + |
| 67 | +```typescript |
| 68 | +// src/flows/lead_qualify.flow.ts |
| 69 | +import { FlowSchema } from '@objectstack/spec/automation'; |
| 70 | + |
| 71 | +export const LeadQualifyFlow: FlowSchema = { |
| 72 | + name: 'lead_qualification_process', |
| 73 | + label: 'Qualify Lead', |
| 74 | + type: 'autolaunched', |
| 75 | + |
| 76 | + variables: [ |
| 77 | + { name: 'leadId', type: 'string', isInput: true }, |
| 78 | + { name: 'score', type: 'number', isInput: false } |
| 79 | + ], |
| 80 | + |
| 81 | + nodes: [ |
| 82 | + { |
| 83 | + id: 'get_lead', |
| 84 | + type: 'get_record', |
| 85 | + label: 'Get Lead Details', |
| 86 | + config: { |
| 87 | + object: 'lead', |
| 88 | + filter: [['_id', '=', '{leadId}']] |
| 89 | + } |
| 90 | + }, |
| 91 | + { |
| 92 | + id: 'score_check', |
| 93 | + type: 'decision', |
| 94 | + label: 'Check Score', |
| 95 | + config: { |
| 96 | + rules: [ |
| 97 | + { label: 'Hot', condition: 'score > 80', target: 'mark_hot' }, |
| 98 | + { label: 'Cold', condition: 'score <= 80', target: 'mark_nurture' } |
| 99 | + ] |
| 100 | + } |
| 101 | + }, |
| 102 | + { |
| 103 | + id: 'mark_hot', |
| 104 | + type: 'update_record', |
| 105 | + label: 'Set Status Hot', |
| 106 | + config: { |
| 107 | + object: 'lead', |
| 108 | + id: '{leadId}', |
| 109 | + values: { status: 'Working', rating: 'Hot' } |
| 110 | + } |
| 111 | + } |
| 112 | + ] |
| 113 | +}; |
| 114 | +``` |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## 3. Best Practices |
| 119 | + |
| 120 | +1. **Idempotency:** Ensure Flows can run multiple times without corrupting data. |
| 121 | +2. **Naming:** Use `snake_case` for API Names (`large_deal_alert`) and `Title Case` for Labels. |
| 122 | +3. **Complexity:** If a Flow gets too complex (too many Script nodes), consider moving logic to a code-based **Plugin Hook** (`beforeInsert`, etc.) instead. |
0 commit comments