-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
1228 lines (1205 loc) · 43.5 KB
/
Copy pathindex.ts
File metadata and controls
1228 lines (1205 loc) · 43.5 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { defineFlow } from '@objectstack/spec';
/**
* Task Completed → Notify — an autolaunched, record-triggered flow that fires
* when a task transitions to Done and emails the project owner.
*/
export const TaskCompletedFlow = defineFlow({
name: 'showcase_task_completed',
label: 'Notify on Task Completed',
description: 'Emails the project owner when a task is marked Done.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Task Update',
config: {
objectName: 'showcase_task',
triggerType: 'record-after-update',
condition: 'status == "done" && previous.status != "done"',
},
},
{
id: 'notify',
type: 'script',
label: 'Send Completion Email',
config: {
actionType: 'email',
inputs: {
to: '{record.project.owner}',
subject: '✅ Task done: {record.title}',
template: 'showcase_task_done_email',
},
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'notify' },
{ id: 'e2', source: 'notify', target: 'end' },
],
});
/**
* Reassign Wizard — a screen flow launched from the Tasks toolbar action
* (`showcase_bulk_reassign`). Collects a new assignee and writes it back.
*/
export const ReassignWizardFlow = defineFlow({
name: 'showcase_reassign_wizard',
label: 'Reassign Task',
description: 'Screen flow that reassigns a task to a new owner.',
type: 'screen',
status: 'active',
runAs: 'user',
variables: [
{ name: 'recordId', type: 'text', isInput: true, isOutput: false },
{ name: 'new_assignee', type: 'text', isInput: true, isOutput: false },
],
nodes: [
{ id: 'start', type: 'start', label: 'Start' },
{
id: 'collect',
type: 'screen',
label: 'New Assignee',
config: {
fields: [
{ name: 'new_assignee', label: 'New Assignee', type: 'text', required: true },
],
},
},
{
id: 'apply',
type: 'update_record',
label: 'Apply Reassignment',
config: {
objectName: 'showcase_task',
filter: { id: '{recordId}' },
fields: { assignee: '{new_assignee}' },
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'collect' },
{ id: 'e2', source: 'collect', target: 'apply' },
{ id: 'e3', source: 'apply', target: 'end' },
],
});
/**
* Task Assigned → Notify Assignee — the worked `notify` example (ADR-0012).
*
* Where {@link TaskCompletedFlow} hand-waves notification through a `script`
* node, this flow uses the baseline `notify` node: it hands a topic +
* recipient + message to the messaging service, which fans out to the user's
* channels (inbox by default). The `notify` node ships in every automation
* engine; delivery is backed by `@objectstack/service-messaging`
* (`MessagingServicePlugin`). Without that plugin the node degrades to a
* logged no-op instead of failing the flow — install it and this flow starts
* landing inbox rows with no edit.
*/
export const TaskAssignedNotifyFlow = defineFlow({
name: 'showcase_task_assigned_notify',
label: 'Notify Assignee on Task Assignment',
description: 'Notifies the new assignee (inbox channel) when a task is reassigned.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Task Assignee Change',
config: {
objectName: 'showcase_task',
triggerType: 'record-after-update',
condition: 'assignee != previous.assignee',
},
},
{
id: 'notify_assignee',
type: 'notify',
label: 'Notify Assignee',
config: {
topic: 'task.assigned',
recipients: ['{record.assignee}'],
channels: ['inbox'],
severity: 'info',
title: 'New task assigned: {record.title}',
message: 'You have been assigned "{record.title}".',
actionUrl: '/showcase_task/{record.id}',
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'notify_assignee' },
{ id: 'e2', source: 'notify_assignee', target: 'end' },
],
});
/**
* Project Budget Approval — ADR-0019 approval-as-flow-node.
*
* What used to be a standalone two-step approval *process* is now an ordinary
* autolaunched flow with two `approval` nodes. The flow suspends on each
* approval and resumes down the matching `approve` / `reject` edge. The
* executive step only runs for budgets above $500k — that gate is a decision
* node on the manager step's approve edge.
*
* The manager step also demos ADR-0044 **send back for revision**: its
* `revise` edge walks to a signal `wait` node where the record unlocks for
* rework, and the submitter's resubmit re-enters the approval node over the
* declared back-edge (round 2, fresh approver slate). `maxRevisions: 2` keeps
* the loop guarded — a third send-back auto-rejects. The executive step has
* NO revise edge on purpose: send-back there is rejected with a clear error.
*/
export const BudgetApprovalFlow = defineFlow({
name: 'showcase_budget_approval',
label: 'Project Budget Approval',
description: 'Two-step approval for projects above budget thresholds.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Large Budget',
config: {
objectName: 'showcase_project',
triggerType: 'record-after-update',
// Gate on the budget CHANGING, not on every update of a large-budget
// project — otherwise any unrelated edit (status, health, …) re-opens
// an approval and collides with other approval flows on the same
// record (the approvals service dedupes pending requests per record).
condition: 'budget > 100000 && budget != previous.budget',
},
},
{
id: 'manager_review',
type: 'approval',
label: 'Manager Review',
config: {
approvers: [{ type: 'role', value: 'manager' }],
behavior: 'first_response',
lockRecord: true,
// ADR-0044: at most two send-backs; the third auto-rejects.
maxRevisions: 2,
},
},
{
// ADR-0044 revise window: the run parks here while the submitter reworks
// the (now unlocked) record; their resubmit resumes it over the back-edge.
id: 'wait_revision',
type: 'wait',
label: 'Awaiting Revision',
config: { eventType: 'signal', signalName: 'budget_revision' },
},
{
id: 'needs_exec',
type: 'decision',
label: 'Budget Above $500k?',
config: { condition: 'budget > 500000' },
},
{
id: 'exec_review',
type: 'approval',
label: 'Executive Review',
config: {
approvers: [{ type: 'role', value: 'exec' }],
behavior: 'unanimous',
lockRecord: true,
},
},
{ id: 'approved', type: 'end', label: 'Approved' },
{ id: 'rejected', type: 'end', label: 'Rejected' },
],
edges: [
{ id: 'e1', source: 'start', target: 'manager_review' },
{ id: 'e2', source: 'manager_review', target: 'needs_exec', label: 'approve' },
{ id: 'e3', source: 'manager_review', target: 'rejected', label: 'reject' },
// Decision branching is edge-condition driven (flow spec): the engine
// routes a decision node by evaluating each out-edge's `condition`. Carry
// the predicate on the edges (the node `config.condition` alone is not
// evaluated by the engine), so budgets ≤ $500k skip the executive step.
{ id: 'e4', source: 'needs_exec', target: 'exec_review', label: 'true', condition: 'budget > 500000' },
{ id: 'e5', source: 'needs_exec', target: 'approved', label: 'false', condition: 'budget <= 500000' },
{ id: 'e6', source: 'exec_review', target: 'approved', label: 'approve' },
{ id: 'e7', source: 'exec_review', target: 'rejected', label: 'reject' },
// ADR-0044 send-back-for-revision loop on the manager step: revise walks
// to the wait node; the resubmit edge is the declared back-edge closing
// the cycle (type 'back' — excluded from DAG validation, traversed
// normally), re-entering the approval node as round 2.
{ id: 'e8', source: 'manager_review', target: 'wait_revision', label: 'revise' },
{ id: 'e9', source: 'wait_revision', target: 'manager_review', label: 'resubmit', type: 'back' },
],
});
/**
* Task Completed → Post to Slack — the worked `connector_action` example
* (ADR-0018 §Addendum, ADR-0022).
*
* Unlike {@link TaskCompletedFlow}, which hand-waves notification via a `script`
* node, this flow takes the "raw API call" path: a baseline `connector_action`
* node dispatches to the `slack` connector's `chat.postMessage` action. The
* `connector_action` node type is built into every automation engine; the
* `slack` connector itself is contributed at runtime by the
* `@objectstack/connector-slack` plugin (static bot-token auth). Load that
* plugin in your stack and the node resolves; omit it and the step fails with a
* clear "connector slack not registered" error rather than silently no-op'ing.
*
* The connector → action → input pickers the designer shows for this node are
* fed by `GET /api/v1/automation/connectors`, which enumerates the live
* registry (see `getConnectorDescriptors`).
*/
export const TaskCompletedSlackFlow = defineFlow({
name: 'showcase_task_completed_slack',
label: 'Post to Slack on Task Completed',
description: 'Posts to a Slack channel via the slack connector when a task is marked Done.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Task Update',
config: {
objectName: 'showcase_task',
triggerType: 'record-after-update',
condition: 'status == "done" && previous.status != "done"',
},
},
{
id: 'post_to_slack',
type: 'connector_action',
label: 'Post to #wins',
connectorConfig: {
connectorId: 'slack',
actionId: 'chat.postMessage',
input: {
channel: 'C0WINS000',
text: '✅ Task done: {record.title}',
},
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'post_to_slack' },
{ id: 'e2', source: 'post_to_slack', target: 'end' },
],
});
/**
* Scheduled Digest — the worked `schedule` trigger example.
*
* A `type: 'schedule'` flow whose start node carries an interval descriptor.
* The automation engine parses that into a schedule binding; the schedule
* trigger plugin (`@objectstack/trigger-schedule`, paired with the job
* service) registers a job that fires this flow every interval. Each tick runs
* the `notify` node, dropping a fresh `sys_inbox_message` row — so the
* scheduled fire is observable end-to-end with no manual `engine.execute()`.
*
* Install `requires: ['automation', 'triggers', 'job', 'messaging']` and this
* flow auto-launches on the interval.
*/
export const ScheduledDigestFlow = defineFlow({
name: 'showcase_scheduled_digest',
label: 'Scheduled Project Digest (interval)',
description: 'Fires on a fixed interval and posts a digest to an inbox — demonstrates the schedule trigger.',
type: 'schedule',
nodes: [
{
id: 'start',
type: 'start',
label: 'Every 60s (demo)',
config: {
// DEMO-ONLY interval. Each tick fans out into job_run + notification +
// delivery + receipt + inbox rows (all append-only, ADR-0057). At 20s
// this filled dev.db to 260MB+ over a multi-day `pnpm dev`. 60s keeps
// the schedule trigger observable within a minute while cutting the
// write rate 3x. Production digests use a cron expression instead,
// e.g. { type: 'cron', expression: '0 8 * * *' }. Real bounding comes
// from the lifecycle/retention work (ADR-0057), not this number.
schedule: { type: 'interval', intervalMs: 60000 },
},
},
{
id: 'digest',
type: 'notify',
label: 'Post Digest to Inbox',
config: {
topic: 'project.digest',
recipients: ['admin@objectos.ai'],
channels: ['inbox'],
severity: 'info',
title: 'Scheduled project digest',
message: 'Your periodic project digest is ready — open Projects for the latest health.',
actionUrl: '/showcase_project',
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'digest' },
{ id: 'e2', source: 'digest', target: 'end' },
],
});
/**
* Task Completed → REST Ping (self) — the worked `connector_action` example on
* the generic `rest` connector.
*
* Where {@link TaskCompletedSlackFlow} targets the `slack` connector (which
* needs a real bot token + channel), this flow dispatches to the `rest`
* connector contributed by `@objectstack/connector-rest`, configured to point
* at the running server itself. On task completion it issues
* `GET /api/v1/health`; the request and its `{ status: 'ok' }` response are
* captured on the flow run, so the connector dispatch is fully observable
* without any external service or credentials.
*/
export const TaskCompletedRestPingFlow = defineFlow({
name: 'showcase_task_completed_rest_ping',
label: 'REST Ping on Task Completed',
description: 'Calls the local server health endpoint via the rest connector when a task is marked Done.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Task Update',
config: {
objectName: 'showcase_task',
triggerType: 'record-after-update',
condition: 'status == "done" && previous.status != "done"',
},
},
{
id: 'ping',
type: 'connector_action',
label: 'GET /api/v1/health',
connectorConfig: {
connectorId: 'rest',
actionId: 'request',
input: {
method: 'GET',
path: '/api/v1/health',
},
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'ping' },
{ id: 'e2', source: 'ping', target: 'end' },
],
});
/**
* Task Follow-up Reminder — the worked `wait` (durable timer) example.
*
* When a task is created, the flow pauses at a `wait` node for a fixed delay,
* then reminds the assignee to update it. The `wait` node *suspends* the run
* (ADR-0019 durable pause, like `screen`/`approval`); a one-shot job scheduled
* via the job service (`{ type: 'once', at }`) resumes it when the timer
* elapses — so the delayed reminder fires end-to-end with no manual
* `engine.resume()`. Without a job service the run still suspends and can be
* resumed by an external `resume(runId)` (it never silently no-ops).
*
* A short demo delay keeps it observable in-session; a production reminder would
* use e.g. `timerDuration: 'P3D'`. Install
* `requires: ['automation', 'triggers', 'job', 'messaging']`.
*/
export const TaskFollowUpFlow = defineFlow({
name: 'showcase_task_follow_up',
label: 'Task Follow-up Reminder (wait)',
description: 'Waits a fixed delay after a task is created, then reminds the assignee — demonstrates the durable wait node.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Task Created',
config: {
objectName: 'showcase_task',
triggerType: 'record-after-create',
},
},
{
id: 'hold',
type: 'wait',
label: 'Wait 1 min',
// Timer wait: suspends the run, then a one-shot job resumes it after the
// duration. ISO-8601 duration; production reminders would use e.g. 'P3D'.
waitEventConfig: { eventType: 'timer', timerDuration: 'PT1M', onTimeout: 'continue' },
},
{
id: 'remind',
type: 'notify',
label: 'Remind Assignee',
config: {
topic: 'task.followup',
recipients: ['{record.assignee}'],
channels: ['inbox'],
severity: 'info',
title: 'Follow up on: {record.title}',
message: 'This task has been open for a while — please update its status.',
actionUrl: '/showcase_task/{record.id}',
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'hold' },
{ id: 'e2', source: 'hold', target: 'remind' },
{ id: 'e3', source: 'remind', target: 'end' },
],
});
/**
* Notify Owner — a reusable **subflow** (`template: true`). Other flows invoke
* it through a `subflow` node, passing `ownerId` + `message`; it fans a
* notification to the owner. Centralising "how we notify an owner" here means
* callers don't duplicate the notify wiring.
*/
export const NotifyOwnerSubflow = defineFlow({
name: 'showcase_notify_owner',
label: 'Notify Owner (reusable subflow)',
description: 'Reusable subflow: notifies a record owner. Invoked by other flows via a subflow node.',
type: 'autolaunched',
template: true,
variables: [
{ name: 'ownerId', type: 'text', isInput: true },
{ name: 'message', type: 'text', isInput: true },
],
nodes: [
{ id: 'start', type: 'start', label: 'Start' },
{
id: 'notify',
type: 'notify',
label: 'Notify Owner',
config: {
topic: 'project.notice',
recipients: ['{ownerId}'],
channels: ['inbox'],
severity: 'info',
title: 'Project update',
message: '{message}',
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'notify' },
{ id: 'e2', source: 'notify', target: 'end' },
],
});
/**
* Task Done → Notify Owner (subflow) — the worked `subflow` example. On task
* completion it invokes {@link NotifyOwnerSubflow} via a `subflow` node, mapping
* the task's owner + a message into the subflow's input variables.
*/
export const TaskDoneNotifyOwnerFlow = defineFlow({
name: 'showcase_task_done_notify_owner',
label: 'Task Done → Notify Owner (subflow)',
description: 'On task completion, invokes the reusable notify-owner subflow — demonstrates subflow reuse.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Task Done',
config: {
objectName: 'showcase_task',
triggerType: 'record-after-update',
condition: 'status == "done" && previous.status != "done"',
},
},
{
id: 'call_notify',
type: 'subflow',
label: 'Notify Owner',
config: {
flowName: 'showcase_notify_owner',
input: {
ownerId: '{record.project.owner}',
message: 'Task "{record.title}" is done.',
},
outputVariable: 'notifyResult',
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'call_notify' },
{ id: 'e2', source: 'call_notify', target: 'end' },
],
});
/**
* Closure Sign-off — a reusable **approval subflow**: pauses on a manager
* approval and reports the decision as its output. Together with
* {@link ProjectClosureFlow} this is the worked example of **nested durable
* pause** (linked-runs model): a pausing node (`approval`) inside a `subflow`
* suspends BOTH runs — the child at the approval, the parent at its subflow
* node (`correlation: subflow:<childRunId>`) — and the eventual decision
* bubbles back up through the chain.
*/
export const ClosureSignoffSubflow = defineFlow({
name: 'showcase_closure_signoff',
label: 'Closure Sign-off (approval subflow)',
description: 'Reusable subflow: requests a manager sign-off and outputs the decision. Demonstrates approval inside a subflow (nested durable pause).',
type: 'autolaunched',
template: true,
variables: [
{ name: 'reason', type: 'text', isInput: true },
{ name: 'decision', type: 'text', isOutput: true },
],
nodes: [
{ id: 'start', type: 'start', label: 'Start' },
{
id: 'ask_signoff',
type: 'approval',
label: 'Manager Sign-off',
config: {
approvers: [{ type: 'role', value: 'manager' }],
behavior: 'first_response',
// The parent project just hit a terminal status — no point locking it.
lockRecord: false,
},
},
{
id: 'mark_approved',
type: 'assignment',
label: 'Record Approval',
config: { assignments: { decision: 'approved' } },
},
{
id: 'mark_rejected',
type: 'assignment',
label: 'Record Rejection',
config: { assignments: { decision: 'rejected' } },
},
{ id: 'end_ok', type: 'end', label: 'Signed Off' },
{ id: 'end_no', type: 'end', label: 'Declined' },
],
edges: [
{ id: 'e1', source: 'start', target: 'ask_signoff' },
{ id: 'e2', source: 'ask_signoff', target: 'mark_approved', label: 'approve' },
{ id: 'e3', source: 'ask_signoff', target: 'mark_rejected', label: 'reject' },
{ id: 'e4', source: 'mark_approved', target: 'end_ok' },
{ id: 'e5', source: 'mark_rejected', target: 'end_no' },
],
});
/**
* Project Closure with Sign-off — the worked **nested durable pause** example.
*
* When a project is marked Completed, the flow invokes
* {@link ClosureSignoffSubflow} through a `subflow` node. The child suspends on
* its `approval` node, which suspends THIS run too — both continuations are
* persisted as linked runs (`sys_automation_run`), surviving restarts. When a
* manager decides (approvals API / inbox), the child resumes down the matching
* branch, completes, and **bubbles** its `decision` output back into this run
* (`signoffResult`), which continues to notify the project owner.
*
* Observe it end-to-end: complete a project → both runs show `paused` in the
* Runs panel (parent at `signoff`, child at `ask_signoff`) → approve via
* `POST /api/v1/approvals/requests/:id/approve` → both runs complete and the
* owner's inbox gets the decision.
*/
export const ProjectClosureFlow = defineFlow({
name: 'showcase_project_closure',
label: 'Project Closure with Sign-off (nested pause)',
description: 'On project completion, requests sign-off via an approval-inside-subflow, then notifies the owner — demonstrates nested durable pause.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Project Completed',
config: {
objectName: 'showcase_project',
triggerType: 'record-after-update',
condition: 'status == "completed" && previous.status != "completed"',
},
},
{
id: 'signoff',
type: 'subflow',
label: 'Request Sign-off',
config: {
flowName: 'showcase_closure_signoff',
input: {
reason: 'Project "{record.name}" was marked completed — please sign off the closure.',
},
outputVariable: 'signoffResult',
},
},
{
id: 'notify_owner',
type: 'notify',
label: 'Notify Owner of Decision',
config: {
topic: 'project.closure',
recipients: ['{record.owner}'],
channels: ['inbox'],
severity: 'info',
title: 'Closure sign-off: {record.name}',
message: 'Closure sign-off decision for "{record.name}": {signoffResult.decision}.',
actionUrl: '/showcase_project/{record.id}',
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'signoff' },
{ id: 'e2', source: 'signoff', target: 'notify_owner' },
{ id: 'e3', source: 'notify_owner', target: 'end' },
],
});
/**
* Batch Reminders — demonstrates the ADR-0031 **structured loop container**.
*
* The `loop` node owns a bounded **body region** (`config.body`, a
* single-entry/single-exit sub-graph) and iterates it over a collection: each
* task is bound to `task` (and its index to `taskIndex`) in the enclosing
* variable scope, and the body sends a reminder. A hard `maxIterations` guard
* keeps iteration bounded. The loop node's ordinary out-edge (`→ end`) is the
* after-loop continuation — the DAG invariant for ordinary edges is preserved.
*/
export const BatchRemindersFlow = defineFlow({
name: 'showcase_batch_reminders',
label: 'Batch Task Reminders (Loop)',
description: 'Iterates a collection of tasks and sends a reminder for each (structured loop container, ADR-0031).',
type: 'autolaunched',
variables: [
{ name: 'tasks', type: 'list', isInput: true, isOutput: false },
],
nodes: [
{ id: 'start', type: 'start', label: 'Start' },
{
id: 'loop_tasks',
type: 'loop',
label: 'For each task',
config: {
collection: '{tasks}',
iteratorVariable: 'task',
indexVariable: 'taskIndex',
maxIterations: 500,
body: {
nodes: [
{
id: 'send_reminder',
type: 'script',
label: 'Send Reminder',
config: {
actionType: 'email',
inputs: {
to: '{task.owner.email}',
subject: 'Reminder ({taskIndex}): {task.title}',
template: 'showcase_task_reminder_email',
},
},
},
],
edges: [],
},
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'loop_tasks' },
{ id: 'e2', source: 'loop_tasks', target: 'end' },
],
});
/**
* Fan-out Notify — demonstrates the ADR-0031 **structured parallel block**.
*
* The `parallel` node declares two branch regions in `config.branches[]`; both
* run concurrently in the enclosing variable scope and **join implicitly** at
* block end (the engine continues once both complete). There is no
* author-visible split/join gateway. The node's ordinary out-edge (`→ end`) is
* the after-block continuation.
*/
export const FanOutNotifyFlow = defineFlow({
name: 'showcase_fan_out_notify',
label: 'Fan-out Notify (Parallel)',
description: 'Notifies owner and watchers concurrently via a parallel block, joining before completion (ADR-0031).',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Task Completed',
config: {
objectName: 'showcase_task',
triggerType: 'record-after-update',
condition: 'status == "done" && previous.status != "done"',
},
},
{
id: 'fan_out',
type: 'parallel',
label: 'Notify in parallel',
config: {
branches: [
{
name: 'Email the owner',
nodes: [
{
id: 'email_owner',
type: 'script',
label: 'Email Owner',
config: {
actionType: 'email',
inputs: { to: '{record.project.owner}', subject: '✅ Done: {record.title}' },
},
},
],
edges: [],
},
{
name: 'Post to Slack',
nodes: [
{
id: 'slack_post',
type: 'script',
label: 'Slack Notify',
config: {
actionType: 'slack',
inputs: { channel: '#tasks', text: 'Task done: {record.title}' },
},
},
],
edges: [],
},
],
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'fan_out' },
{ id: 'e2', source: 'fan_out', target: 'end' },
],
});
/**
* Resilient Sync — demonstrates the ADR-0031 **try/catch/retry** construct.
*
* The `try_catch` node runs a protected `try` region (an outbound HTTP push);
* on failure it retries with exponential backoff, and if it still fails the
* `catch` region records the failure with the caught error bound to `$error`.
* Both regions are single-entry/single-exit and run in the enclosing scope; the
* node's ordinary out-edge (`→ end`) is the after-block continuation.
*/
export const ResilientSyncFlow = defineFlow({
name: 'showcase_resilient_sync',
label: 'Resilient Sync (Try/Catch/Retry)',
description: 'Pushes a task to an external system, retrying on failure and recording errors via try/catch (ADR-0031).',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Task Completed',
config: {
objectName: 'showcase_task',
triggerType: 'record-after-update',
condition: 'status == "done" && previous.status != "done"',
},
},
{
id: 'guarded_push',
type: 'try_catch',
label: 'Push with retry',
config: {
retry: { maxRetries: 3, retryDelayMs: 1000, backoffMultiplier: 2, maxRetryDelayMs: 10000 },
errorVariable: '$error',
try: {
nodes: [
{
id: 'push',
type: 'http_request',
label: 'Push to CRM',
config: {
url: 'https://api.example.com/v1/tasks',
method: 'POST',
body: { id: '{record.id}', title: '{record.title}', status: 'done' },
},
},
],
edges: [],
},
catch: {
nodes: [
{
id: 'record_failure',
type: 'update_record',
label: 'Flag Sync Failure',
config: {
objectName: 'showcase_task',
filter: { id: '{record.id}' },
fields: { sync_status: 'failed', sync_error: '{$error.message}' },
},
},
],
edges: [],
},
},
},
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'guarded_push' },
{ id: 'e2', source: 'guarded_push', target: 'end' },
],
});
/**
* Invoice Dual Sign-off — the worked **parallel-approval** example (ADR-0039
* Track A: aggregating approval node, no engine-core change).
*
* "Finance AND legal must both sign off before an invoice is sent" is expressed
* as a **single `approval` node** with two approver groups and
* `behavior: 'unanimous'`. On entry the node opens ONE `sys_approval_request`
* whose `pending_approvers` holds *both* groups — they are notified
* concurrently (parallel). The node stays suspended until **every** group has
* approved (the aggregation / AND), then resumes down the `approve` edge; any
* rejection resumes down `reject`. One node, one suspend/resume, no token tree —
* the multi-instance pattern Camunda and Step Functions use for exactly this.
*
* Decide via the approvals API (never a raw engine `resume`):
* POST /api/v1/automation/showcase_invoice_signoff/runs/{runId}/... ← no
* POST /api/v1/approvals/requests/{id}/approve { actorId: 'role:finance' }
* POST /api/v1/approvals/requests/{id}/approve { actorId: 'role:legal' } ← now it continues
*/
export const InvoiceDualSignoffFlow = defineFlow({
name: 'showcase_invoice_signoff',
label: 'Invoice Dual Sign-off (parallel approval)',
description: 'On send, requires finance AND legal to both approve via one aggregating approval node — demonstrates parallel approvals without a token tree (ADR-0039 Track A).',
type: 'autolaunched',
// The revert-on-reject write is an approval-process outcome, not an act of the
// submitter — run it as the system principal so it lands regardless of whether
// the submitter still has edit rights on a "sent" invoice (#1888 runAs enforced).
runAs: 'system',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Invoice Sent',
config: {
objectName: 'showcase_invoice',
triggerType: 'record-after-update',
condition: 'status == "sent" && previous.status != "sent"',
},
},
{
id: 'dual_signoff',
type: 'approval',
label: 'Finance + Legal Sign-off',
config: {
// Two approver groups, notified in parallel; `unanimous` waits for both.
approvers: [
{ type: 'role', value: 'finance' },
{ type: 'role', value: 'legal' },
],
behavior: 'unanimous',
// The invoice keeps flowing through other automations while it waits.
lockRecord: false,
},
},
{
id: 'notify_cleared',
type: 'notify',
label: 'Notify: Cleared',
config: {
topic: 'invoice.signoff',
recipients: ['{record.account.owner}'],
channels: ['inbox'],
severity: 'info',
title: 'Invoice cleared: {record.name}',
message: 'Invoice "{record.name}" passed finance + legal sign-off and is on its way.',
actionUrl: '/showcase_invoice/{record.id}',
},
},
{
id: 'flag_held',
type: 'update_record',
label: 'Flag: Held',
config: {
objectName: 'showcase_invoice',
filter: { id: '{record.id}' },
fields: { status: 'draft' },
},
},
{ id: 'end_ok', type: 'end', label: 'Sent' },
{ id: 'end_held', type: 'end', label: 'Held' },
],
edges: [
{ id: 'e1', source: 'start', target: 'dual_signoff' },
{ id: 'e2', source: 'dual_signoff', target: 'notify_cleared', label: 'approve' },
{ id: 'e3', source: 'dual_signoff', target: 'flag_held', label: 'reject' },
{ id: 'e4', source: 'notify_cleared', target: 'end_ok' },
{ id: 'e5', source: 'flag_held', target: 'end_held' },
],
});
/**
* Project Escalation — the worked **composite** example: several constructs
* nested in one realistic flow, where every other showcase flow demos one
* construct in isolation. When a project's health turns red:
*
* decision (critical budget?)
* ├─ critical → parallel { alert owner ∥ alert exec } → try/catch {
* │ push to the incident system, catch → log the failure }
* └─ normal → a single owner notification
* → converge → end
*
* It exercises construct **interactions** (parallel + try/catch under a decision
* branch, converging edges) that single-construct flows don't — and runs
* synchronously (no pause), so it completes in one pass and is fully visible in
* the Runs panel with nested step folding.
*/
export const ProjectEscalationFlow = defineFlow({
name: 'showcase_project_escalation',
label: 'Project Escalation (composite)',
description: 'On health → red, branches on severity then alerts in parallel and pushes to an incident system with try/catch — demonstrates nested construct composition.',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
label: 'On Health Red',
config: {
objectName: 'showcase_project',
triggerType: 'record-after-update',
condition: 'health == "red" && previous.health != "red"',
},
},
{ id: 'triage', type: 'decision', label: 'Critical budget?' },
{
id: 'alert',
type: 'parallel',
label: 'Alert in parallel',
config: {
branches: [
{
name: 'Owner',
nodes: [{ id: 'alert_owner', type: 'script', label: 'Alert Owner', config: { actionType: 'email', inputs: { to: '{record.owner}', subject: '🔴 Critical: {record.name}' } } }],
edges: [],
},
{
name: 'Exec',
nodes: [{ id: 'alert_exec', type: 'script', label: 'Alert Exec', config: { actionType: 'email', inputs: { to: 'exec@example.com', subject: '🔴 Critical project: {record.name}' } } }],
edges: [],
},