@flowuent-org/diagramming-core 1.4.1 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. package/apps/diagramming/src/AutomationDiagramData.ts +110 -4
  2. package/package.json +1 -1
  3. package/packages/diagrams/src/lib/components/automation/AutomationApiNode.tsx +3 -13
  4. package/packages/diagrams/src/lib/components/automation/AutomationDecisionNode.tsx +0 -1
  5. package/packages/diagrams/src/lib/components/automation/AutomationEndNode.tsx +3 -13
  6. package/packages/diagrams/src/lib/components/automation/AutomationFormattingNode.tsx +3 -13
  7. package/packages/diagrams/src/lib/components/automation/AutomationInteractionNode.tsx +3 -13
  8. package/packages/diagrams/src/lib/components/automation/AutomationLoopNode.tsx +105 -0
  9. package/packages/diagrams/src/lib/components/automation/AutomationNavigationNode.tsx +3 -13
  10. package/packages/diagrams/src/lib/components/automation/AutomationNodeCard.tsx +32 -11
  11. package/packages/diagrams/src/lib/components/automation/AutomationNodeHeader.tsx +45 -0
  12. package/packages/diagrams/src/lib/components/automation/AutomationSlackNode.tsx +3 -13
  13. package/packages/diagrams/src/lib/components/automation/AutomationSmsNode.tsx +0 -1
  14. package/packages/diagrams/src/lib/components/automation/AutomationStartNode.tsx +3 -13
  15. package/packages/diagrams/src/lib/components/automation/AutomationTelegramNode.tsx +3 -13
  16. package/packages/diagrams/src/lib/components/automation/AutomationVoiceCallNode.tsx +0 -1
  17. package/packages/diagrams/src/lib/components/automation/AutomationWorkflowNode.tsx +0 -1
  18. package/packages/diagrams/src/lib/components/automation/index.ts +11 -0
  19. package/packages/diagrams/src/lib/icons/icons.tsx +10 -0
  20. package/packages/diagrams/src/lib/icons/index.ts +1 -0
  21. package/packages/diagrams/src/lib/types/automation-node-data-types.ts +14 -1
  22. package/packages/diagrams/src/lib/types/node-types.ts +2 -0
  23. package/packages/diagrams/src/lib/utils/AutomationExecutionEngine.ts +268 -41
  24. package/packages/diagrams/src/lib/utils/automation-flow-processor.ts +70 -0
  25. package/packages/diagrams/src/lib/utils/validateAutomationLoopNode.ts +104 -0
@@ -43,7 +43,6 @@ export const AutomationSmsNode: React.FC<AutomationSmsNodeProps> = ({
43
43
  data={{ ...data, description: descriptionText }}
44
44
  selected={selected}
45
45
  icon={<SmsNodeIcon />}
46
- headerText="Send SMS (recipientPhone)"
47
46
  aiAssistantTitle="SMS Node"
48
47
  descriptionContent={
49
48
  <>
@@ -12,11 +12,13 @@ import { showNodeAIAssistantPopup } from './NodeAIAssistantPopup';
12
12
  import { useSearch } from '../../contexts/SearchContext';
13
13
  import { getStatusColor } from './statusColors';
14
14
  import { StartNodeIcon } from '../../icons';
15
+ import { AutomationNodeHeaderSection } from './AutomationNodeHeader';
15
16
 
16
17
  interface AutomationStartNodeProps {
17
18
  data: {
18
19
  label: string;
19
20
  description: string;
21
+ headerText?: string;
20
22
  status: 'Ready' | 'Running' | 'Completed' | 'Error';
21
23
  triggerType: 'manual' | 'scheduled';
22
24
  scheduleConfig?: {
@@ -235,19 +237,7 @@ export const AutomationStartNode: React.FC<AutomationStartNodeProps> = ({ data,
235
237
  onClick={handleJsonClick}
236
238
  >
237
239
  {/* Top Header Section */}
238
- <Box sx={{
239
- backgroundColor: "rgba(67, 93, 132, 0.1)",
240
- padding: '8px 16px',
241
- borderRadius: '12px 12px 0 0'
242
- }}>
243
- <Typography variant="body2" sx={{
244
- color: '#ffffff',
245
- fontSize: '12px',
246
- fontWeight: 500
247
- }}>
248
- {highlightText(data.formData?.description || t('automation.startNode.headerDescription'))}
249
- </Typography>
250
- </Box>
240
+ <AutomationNodeHeaderSection data={data} highlightText={highlightText} />
251
241
 
252
242
  {/* Main Content */}
253
243
  <Box sx={{ padding: '16px' }}>
@@ -31,6 +31,7 @@ import { useDiagram } from '../../contexts/DiagramProvider';
31
31
  import { useSearch } from '../../contexts/SearchContext';
32
32
  import { NodeActionButtons } from './NodeActionButtons';
33
33
  import { getStatusColor } from './statusColors';
34
+ import { AutomationNodeHeaderSection } from './AutomationNodeHeader';
34
35
 
35
36
  // ========================
36
37
  // Types
@@ -57,6 +58,7 @@ export interface TelegramTokenData {
57
58
  export interface AutomationTelegramNodeData {
58
59
  label: string;
59
60
  description: string;
61
+ headerText?: string;
60
62
  operationType?: TelegramOperationType;
61
63
  status: 'Ready' | 'Running' | 'Completed' | 'Failed' | 'Need to Config' | 'authenticated';
62
64
  parameters?: {
@@ -462,19 +464,7 @@ export const AutomationTelegramNode: React.FC<AutomationTelegramNodeProps> = ({
462
464
  />
463
465
 
464
466
  {/* Top Header Section */}
465
- <Box sx={{
466
- backgroundColor: "rgba(67, 93, 132, 0.1)",
467
- padding: '8px 16px',
468
- borderRadius: '12px 12px 0 0'
469
- }}>
470
- <Typography variant="body2" sx={{
471
- color: '#ffffff',
472
- fontSize: '12px',
473
- fontWeight: 500
474
- }}>
475
- {data.description || operationConfig.description}
476
- </Typography>
477
- </Box>
467
+ <AutomationNodeHeaderSection data={data} highlightText={highlightText} />
478
468
 
479
469
  {/* Header */}
480
470
  <Box
@@ -43,7 +43,6 @@ export const AutomationVoiceCallNode: React.FC<AutomationVoiceCallNodeProps> = (
43
43
  data={data}
44
44
  selected={selected}
45
45
  icon={<VoiceCallNodeIcon />}
46
- headerText="Voice calls with spoken content"
47
46
  aiAssistantTitle="Voice Call Node"
48
47
  descriptionContent={
49
48
  <>
@@ -43,7 +43,6 @@ export const AutomationWorkflowNode: React.FC<AutomationWorkflowNodeProps> = ({
43
43
  data={data}
44
44
  selected={selected}
45
45
  icon={<SubWorkflowNodeIcon />}
46
- headerText="Run/embed another workflow (workflowId)"
47
46
  aiAssistantTitle="Workflow Node"
48
47
  descriptionContent={
49
48
  <>
@@ -51,6 +51,12 @@ export type {
51
51
  AutomationDecisionNodeData,
52
52
  AutomationDecisionNodeProps,
53
53
  } from './AutomationDecisionNode';
54
+ // Loop / for-each node
55
+ export { AutomationLoopNode } from './AutomationLoopNode';
56
+ export type {
57
+ AutomationLoopNodeData,
58
+ AutomationLoopNodeProps,
59
+ } from './AutomationLoopNode';
54
60
  // SMS node
55
61
  export { AutomationSmsNode } from './AutomationSmsNode';
56
62
  export type {
@@ -69,6 +75,11 @@ export type {
69
75
  AutomationWorkflowNodeData,
70
76
  AutomationWorkflowNodeProps,
71
77
  } from './AutomationWorkflowNode';
78
+ export {
79
+ getAutomationNodeHeaderText,
80
+ AutomationNodeHeaderSection,
81
+ } from './AutomationNodeHeader';
82
+ export type { AutomationNodeHeaderData } from './AutomationNodeHeader';
72
83
  export { AutomationNodeCard } from './AutomationNodeCard';
73
84
  export type {
74
85
  AutomationNodeCardData,
@@ -299,6 +299,16 @@ export const DecisionNodeIcon: React.FC<SvgIconProps> = ({ size, color, style })
299
299
  </svg>
300
300
  );
301
301
 
302
+ export const LoopNodeIcon: React.FC<SvgIconProps> = ({ size, color, style }) => (
303
+ <svg width="35" height="35" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg" style={style}>
304
+ <path d="M0 14C0 6.26801 6.26801 0 14 0C21.732 0 28 6.26801 28 14C28 21.732 21.732 28 14 28C6.26801 28 0 21.732 0 14Z" fill="#1E3A8A"/>
305
+ <path d="M17.5 9.5H11.5C10.3954 9.5 9.5 10.3954 9.5 11.5V13.5" stroke="#86EFAC" strokeLinecap="round" strokeLinejoin="round"/>
306
+ <path d="M10.5 18.5H16.5C17.6046 18.5 18.5 17.6046 18.5 16.5V14.5" stroke="#86EFAC" strokeLinecap="round" strokeLinejoin="round"/>
307
+ <path d="M9.5 13.5L7.5 11.5L9.5 9.5" stroke="#86EFAC" strokeLinecap="round" strokeLinejoin="round"/>
308
+ <path d="M18.5 14.5L20.5 16.5L18.5 18.5" stroke="#86EFAC" strokeLinecap="round" strokeLinejoin="round"/>
309
+ </svg>
310
+ );
311
+
302
312
  export const SmsNodeIcon: React.FC<SvgIconProps> = ({ size, color, style }) => (
303
313
  <svg width="35" height="35" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg" style={style}>
304
314
  <path d="M0 14C0 6.26801 6.26801 0 14 0C21.732 0 28 6.26801 28 14C28 21.732 21.732 28 14 28C6.26801 28 0 21.732 0 14Z" fill="#1E3A8A"/>
@@ -23,6 +23,7 @@ export {
23
23
  FormattingNodeIcon,
24
24
  ApiNodeIcon,
25
25
  DecisionNodeIcon,
26
+ LoopNodeIcon,
26
27
  SmsNodeIcon,
27
28
  VoiceCallNodeIcon,
28
29
  SubWorkflowNodeIcon,
@@ -2,7 +2,8 @@
2
2
  export interface AutomationNodeFormData {
3
3
  nodeId: string;
4
4
  title: string;
5
- type: 'start' | 'api' | 'formatting' | 'end' | 'navigation' | 'decision' | 'sms' | 'voice-call' | 'workflow';
5
+ headerText?: string;
6
+ type: 'start' | 'api' | 'formatting' | 'end' | 'navigation' | 'decision' | 'loop' | 'sms' | 'voice-call' | 'workflow';
6
7
  isPinned?: boolean;
7
8
  isBlock?: boolean;
8
9
  blocks?: Array<{
@@ -202,6 +203,17 @@ export interface AutomationDecisionNodeForm extends AutomationNodeFormData {
202
203
  defaultBranch?: string;
203
204
  }
204
205
 
206
+ export interface AutomationLoopNodeForm extends AutomationNodeFormData {
207
+ type: 'loop';
208
+ arraySource: {
209
+ type: 'previousStep' | 'variable';
210
+ variableName?: string;
211
+ };
212
+ maxIterations?: number;
213
+ currentItemVariable?: string;
214
+ loopIndexVariable?: string;
215
+ }
216
+
205
217
  export interface AutomationSmsNodeForm extends AutomationNodeFormData {
206
218
  type: 'sms';
207
219
  recipientPhone: string;
@@ -255,6 +267,7 @@ export type AutomationNodeForm =
255
267
  | AutomationEndNodeForm
256
268
  | AutomationNavigationNodeForm
257
269
  | AutomationDecisionNodeForm
270
+ | AutomationLoopNodeForm
258
271
  | AutomationSmsNodeForm
259
272
  | AutomationVoiceCallNodeForm
260
273
  | AutomationWorkflowNodeForm;
@@ -15,6 +15,7 @@ import {
15
15
  AutomationInteractionNode,
16
16
  AutomationMonitoringNode,
17
17
  AutomationDecisionNode,
18
+ AutomationLoopNode,
18
19
  AutomationSmsNode,
19
20
  AutomationVoiceCallNode,
20
21
  AutomationWorkflowNode,
@@ -36,6 +37,7 @@ const nodeTypes = {
36
37
  AutomationInteractionNode,
37
38
  AutomationMonitoringNode,
38
39
  AutomationDecisionNode,
40
+ AutomationLoopNode,
39
41
  AutomationSmsNode,
40
42
  AutomationVoiceCallNode,
41
43
  AutomationWorkflowNode,
@@ -5,7 +5,9 @@ import {
5
5
  AutomationApiNodeForm,
6
6
  AutomationFormattingNodeForm,
7
7
  AutomationEndNodeForm,
8
+ AutomationLoopNodeForm,
8
9
  } from '../types/automation-node-data-types';
10
+ import { assertAutomationLoopNodeValid } from './validateAutomationLoopNode';
9
11
 
10
12
  export interface AutomationContext {
11
13
  runId: string;
@@ -128,6 +130,192 @@ export class AutomationExecutionEngine {
128
130
  return this.nodes.filter((node) => nextNodeIds.includes(node.id));
129
131
  }
130
132
 
133
+ private getOutgoingEdge(
134
+ nodeId: string,
135
+ sourceHandle: string,
136
+ ): Edge | undefined {
137
+ return this.edges.find(
138
+ (edge) => edge.source === nodeId && edge.sourceHandle === sourceHandle,
139
+ );
140
+ }
141
+
142
+ private getTargetNode(edge: Edge): Node | undefined {
143
+ return this.nodes.find((node) => node.id === edge.target);
144
+ }
145
+
146
+ private getFirstOutgoingEdge(nodeId: string): Edge | undefined {
147
+ return this.edges.find((edge) => edge.source === nodeId);
148
+ }
149
+
150
+ private resolveArraySource(
151
+ formData: AutomationLoopNodeForm,
152
+ inputData: string | number | boolean | object | null,
153
+ ): unknown[] {
154
+ if (formData.arraySource.type === 'variable') {
155
+ const variableName = formData.arraySource.variableName;
156
+ if (!variableName) {
157
+ throw new Error('Loop node variable name is not configured');
158
+ }
159
+ const value = this.context.variables[variableName];
160
+ if (!Array.isArray(value)) {
161
+ throw new Error(
162
+ `Loop variable "${variableName}" must be an array, got ${typeof value}`,
163
+ );
164
+ }
165
+ return value;
166
+ }
167
+
168
+ if (inputData === null || inputData === undefined) {
169
+ throw new Error('Loop node previous step output is empty');
170
+ }
171
+ if (Array.isArray(inputData)) {
172
+ return inputData;
173
+ }
174
+ return [inputData];
175
+ }
176
+
177
+ private async runNodeWithStatus(
178
+ node: Node,
179
+ inputData?: string | number | boolean | object | null,
180
+ ): Promise<string | number | boolean | object | null> {
181
+ this.log(
182
+ 'info',
183
+ node.id,
184
+ String(node.type || 'node'),
185
+ 'Executing node...',
186
+ );
187
+
188
+ (node.data as Record<string, unknown>).status = 'Running';
189
+ if (this.onNodeUpdate) {
190
+ this.onNodeUpdate(node.id, { ...node.data });
191
+ }
192
+
193
+ try {
194
+ const result = await this.executeNode(node, inputData);
195
+ (node.data as Record<string, unknown>).status = 'Completed';
196
+ (node.data as Record<string, unknown>).lastRun = new Date().toISOString();
197
+ if (this.onNodeUpdate) {
198
+ this.onNodeUpdate(node.id, { ...node.data });
199
+ }
200
+ return result;
201
+ } catch (error) {
202
+ (node.data as Record<string, unknown>).status = 'Error';
203
+ (node.data as Record<string, unknown>).lastRun = new Date().toISOString();
204
+ if (this.onNodeUpdate) {
205
+ this.onNodeUpdate(node.id, { ...node.data });
206
+ }
207
+ throw error;
208
+ }
209
+ }
210
+
211
+ private async executeBodySubgraph(
212
+ startNode: Node,
213
+ loopNodeId: string,
214
+ inputData: string | number | boolean | object | null,
215
+ ): Promise<string | number | boolean | object | null> {
216
+ let currentNode: Node | null = startNode;
217
+ let currentData = inputData;
218
+ const visitedInBody = new Set<string>();
219
+
220
+ while (currentNode && currentNode.id !== loopNodeId) {
221
+ if (visitedInBody.has(currentNode.id)) {
222
+ throw new Error(
223
+ `Infinite loop detected in loop body at node ${currentNode.id}`,
224
+ );
225
+ }
226
+ visitedInBody.add(currentNode.id);
227
+
228
+ currentData = await this.runNodeWithStatus(currentNode, currentData);
229
+
230
+ const nextEdge = this.getFirstOutgoingEdge(currentNode.id);
231
+ if (!nextEdge) {
232
+ throw new Error(
233
+ `Loop body node ${currentNode.id} has no outgoing edge back to the loop node`,
234
+ );
235
+ }
236
+
237
+ const nextNode = this.getTargetNode(nextEdge);
238
+ if (!nextNode) {
239
+ throw new Error(`Loop body target node not found for edge ${nextEdge.id}`);
240
+ }
241
+
242
+ currentNode = nextNode;
243
+ }
244
+
245
+ return currentData;
246
+ }
247
+
248
+ private async executeLoopNode(
249
+ node: Node,
250
+ inputData: string | number | boolean | object | null,
251
+ ): Promise<{ doneNextNode: Node | null; result: object }> {
252
+ assertAutomationLoopNodeValid(node, this.edges);
253
+
254
+ const formData = (node.data.formData || node.data) as AutomationLoopNodeForm;
255
+ const maxIterations = formData.maxIterations ?? 100;
256
+ const currentItemVariable = formData.currentItemVariable ?? 'currentItem';
257
+ const loopIndexVariable = formData.loopIndexVariable ?? 'loopIndex';
258
+
259
+ const array = this.resolveArraySource(formData, inputData);
260
+ const iterationCount = Math.min(array.length, maxIterations);
261
+
262
+ this.log(
263
+ 'info',
264
+ node.id,
265
+ 'AutomationLoopNode',
266
+ `Starting loop over ${iterationCount} item(s)`,
267
+ { totalItems: array.length, maxIterations },
268
+ );
269
+
270
+ const loopEdge = this.getOutgoingEdge(node.id, 'loop');
271
+ if (!loopEdge) {
272
+ throw new Error('Loop node is missing the "loop" handle edge');
273
+ }
274
+
275
+ const bodyStartNode = this.getTargetNode(loopEdge);
276
+ if (!bodyStartNode) {
277
+ throw new Error('Loop body entry node not found');
278
+ }
279
+
280
+ for (let i = 0; i < iterationCount; i++) {
281
+ this.context.variables[currentItemVariable] = array[i] as
282
+ | string
283
+ | number
284
+ | boolean
285
+ | object
286
+ | null;
287
+ this.context.variables[loopIndexVariable] = i;
288
+
289
+ this.log(
290
+ 'info',
291
+ node.id,
292
+ 'AutomationLoopNode',
293
+ `Loop iteration ${i + 1}/${iterationCount}`,
294
+ { [currentItemVariable]: array[i], [loopIndexVariable]: i },
295
+ );
296
+
297
+ await this.executeBodySubgraph(bodyStartNode, node.id, array[i] as
298
+ | string
299
+ | number
300
+ | boolean
301
+ | object
302
+ | null);
303
+ }
304
+
305
+ const result = {
306
+ iterationsRun: iterationCount,
307
+ totalItems: array.length,
308
+ maxIterations,
309
+ };
310
+
311
+ this.storeExecutionResult(node.id, result, true);
312
+
313
+ const doneEdge = this.getOutgoingEdge(node.id, 'done');
314
+ const doneNextNode = doneEdge ? this.getTargetNode(doneEdge) ?? null : null;
315
+
316
+ return { doneNextNode, result };
317
+ }
318
+
131
319
  private async executeStartNode(
132
320
  node: Node,
133
321
  ): Promise<{ runId: string; startTime: string }> {
@@ -522,6 +710,21 @@ export class AutomationExecutionEngine {
522
710
  return inputData;
523
711
  }
524
712
 
713
+ private async executeStubNode(
714
+ node: Node,
715
+ inputData: string | number | boolean | object | null,
716
+ ): Promise<string | number | boolean | object | null> {
717
+ this.log(
718
+ 'info',
719
+ node.id,
720
+ String(node.type || 'node'),
721
+ 'Node executed (stub — full runtime not yet implemented)',
722
+ inputData,
723
+ );
724
+ this.storeExecutionResult(node.id, inputData, true);
725
+ return inputData;
726
+ }
727
+
525
728
  private async executeNode(
526
729
  node: Node,
527
730
  inputData?: string | number | boolean | object | null,
@@ -537,7 +740,14 @@ export class AutomationExecutionEngine {
537
740
  return await this.executeFormattingNode(node, inputData || null);
538
741
  case 'AutomationEndNode':
539
742
  return await this.executeEndNode(node, inputData || null);
743
+ case 'AutomationLoopNode':
744
+ throw new Error(
745
+ 'Loop nodes must be executed via executeLoopNode from the workflow runner',
746
+ );
540
747
  default:
748
+ if (typeof nodeType === 'string' && nodeType.startsWith('Automation')) {
749
+ return await this.executeStubNode(node, inputData || null);
750
+ }
541
751
  throw new Error(`Unknown node type: ${nodeType}`);
542
752
  }
543
753
  }
@@ -560,60 +770,77 @@ export class AutomationExecutionEngine {
560
770
  const visitedNodes = new Set<string>();
561
771
 
562
772
  while (currentNode) {
563
- if (visitedNodes.has(currentNode.id)) {
773
+ if (
774
+ currentNode.type !== 'AutomationLoopNode' &&
775
+ visitedNodes.has(currentNode.id)
776
+ ) {
564
777
  throw new Error(
565
778
  `Circular dependency detected: node ${currentNode.id} already visited`,
566
779
  );
567
780
  }
568
781
 
569
- visitedNodes.add(currentNode.id);
570
-
571
- // Announce node execution start
572
- this.log(
573
- 'info',
574
- currentNode.id,
575
- String(currentNode.type || 'node'),
576
- 'Executing node...',
577
- );
578
-
579
- // Update node status to running
580
- (currentNode.data as Record<string, unknown>).status = 'Running';
581
- if (this.onNodeUpdate) {
582
- // Create new object reference so React detects the change
583
- this.onNodeUpdate(currentNode.id, { ...currentNode.data });
782
+ if (currentNode.type !== 'AutomationLoopNode') {
783
+ visitedNodes.add(currentNode.id);
584
784
  }
585
785
 
586
- try {
587
- currentData = await this.executeNode(currentNode, currentData);
588
- (currentNode.data as Record<string, unknown>).status = 'Completed';
589
- (currentNode.data as Record<string, unknown>).lastRun =
590
- new Date().toISOString();
786
+ if (currentNode.type === 'AutomationLoopNode') {
787
+ (currentNode.data as Record<string, unknown>).status = 'Running';
591
788
  if (this.onNodeUpdate) {
592
- // Create new object reference so React detects the change
593
789
  this.onNodeUpdate(currentNode.id, { ...currentNode.data });
594
790
  }
595
- } catch (error) {
596
- (currentNode.data as Record<string, unknown>).status = 'Error';
597
- (currentNode.data as Record<string, unknown>).lastRun =
598
- new Date().toISOString();
599
- if (this.onNodeUpdate) {
600
- // Create new object reference so React detects the change
601
- this.onNodeUpdate(currentNode.id, { ...currentNode.data });
791
+
792
+ try {
793
+ const { doneNextNode, result } = await this.executeLoopNode(
794
+ currentNode,
795
+ currentData,
796
+ );
797
+ currentData = result;
798
+ (currentNode.data as Record<string, unknown>).status = 'Completed';
799
+ (currentNode.data as Record<string, unknown>).lastRun =
800
+ new Date().toISOString();
801
+ if (this.onNodeUpdate) {
802
+ this.onNodeUpdate(currentNode.id, { ...currentNode.data });
803
+ }
804
+
805
+ if (doneNextNode) {
806
+ this.log(
807
+ 'info',
808
+ doneNextNode.id,
809
+ String(doneNextNode.type || 'node'),
810
+ 'Moving to done path after loop',
811
+ );
812
+ currentNode = doneNextNode;
813
+ } else {
814
+ currentNode = null;
815
+ }
816
+ } catch (error) {
817
+ (currentNode.data as Record<string, unknown>).status = 'Error';
818
+ (currentNode.data as Record<string, unknown>).lastRun =
819
+ new Date().toISOString();
820
+ if (this.onNodeUpdate) {
821
+ this.onNodeUpdate(currentNode.id, { ...currentNode.data });
822
+ }
823
+ throw error;
602
824
  }
603
- throw error;
825
+ continue;
604
826
  }
605
827
 
606
- // Find next node
607
- const nextNodes = this.getNextNodes(currentNode.id);
608
- if (nextNodes.length > 0) {
609
- const next = nextNodes[0];
610
- this.log(
611
- 'info',
612
- next.id,
613
- String(next.type || 'node'),
614
- 'Moving to next node',
615
- );
616
- currentNode = next;
828
+ currentData = await this.runNodeWithStatus(currentNode, currentData);
829
+
830
+ const nextEdge = this.getFirstOutgoingEdge(currentNode.id);
831
+ if (nextEdge) {
832
+ const next = this.getTargetNode(nextEdge);
833
+ if (next) {
834
+ this.log(
835
+ 'info',
836
+ next.id,
837
+ String(next.type || 'node'),
838
+ 'Moving to next node',
839
+ );
840
+ currentNode = next;
841
+ } else {
842
+ currentNode = null;
843
+ }
617
844
  } else {
618
845
  this.log('info', 'workflow', 'engine', 'No next node. Ending.');
619
846
  currentNode = null;
@@ -5,6 +5,7 @@ import {
5
5
  AutomationApiNodeForm,
6
6
  AutomationFormattingNodeForm,
7
7
  AutomationEndNodeForm,
8
+ AutomationLoopNodeForm,
8
9
  } from '../types/automation-node-data-types';
9
10
  import { Node, Edge } from '@xyflow/react';
10
11
 
@@ -165,6 +166,22 @@ export class AutomationFlowProcessor {
165
166
  case 'end':
166
167
  // End node doesn't create new variables
167
168
  break;
169
+
170
+ case 'loop': {
171
+ const currentItemVar = formData.currentItemVariable ?? 'currentItem';
172
+ const loopIndexVar = formData.loopIndexVariable ?? 'loopIndex';
173
+ for (const varName of [currentItemVar, loopIndexVar]) {
174
+ if (!variables.find((v) => v.name === varName)) {
175
+ variables.push({
176
+ name: varName,
177
+ type: varName === loopIndexVar ? 'number' : 'object',
178
+ defaultValue: null,
179
+ description: `Loop iteration variable from ${formData.title}`,
180
+ });
181
+ }
182
+ }
183
+ break;
184
+ }
168
185
  }
169
186
  }
170
187
 
@@ -252,6 +269,12 @@ module.exports = { executeAutomationWorkflow };
252
269
  case 'end':
253
270
  code += this.generateEndNodeCode(formData as AutomationEndNodeForm);
254
271
  break;
272
+ case 'loop':
273
+ code += this.generateLoopNodeCode(
274
+ formData as AutomationLoopNodeForm,
275
+ this.edges,
276
+ );
277
+ break;
255
278
  }
256
279
 
257
280
  return code;
@@ -539,6 +562,53 @@ module.exports = { executeAutomationWorkflow };
539
562
  `;
540
563
  }
541
564
 
565
+ private generateLoopNodeCode(
566
+ formData: AutomationLoopNodeForm,
567
+ edges: Edge[],
568
+ ): string {
569
+ const maxIterations = formData.maxIterations ?? 100;
570
+ const currentItemVariable = formData.currentItemVariable ?? 'currentItem';
571
+ const loopIndexVariable = formData.loopIndexVariable ?? 'loopIndex';
572
+
573
+ const arrayExpr =
574
+ formData.arraySource.type === 'variable'
575
+ ? `context.variables['${formData.arraySource.variableName}']`
576
+ : '/* previous step output */ context.variables.__previousStepOutput';
577
+
578
+ const loopEdge = edges.find(
579
+ (edge) => edge.source === formData.nodeId && edge.sourceHandle === 'loop',
580
+ );
581
+ const doneEdge = edges.find(
582
+ (edge) => edge.source === formData.nodeId && edge.sourceHandle === 'done',
583
+ );
584
+
585
+ const bodyComment = loopEdge
586
+ ? `// Body starts at node: ${loopEdge.target}`
587
+ : '// Body entry not connected';
588
+ const doneComment = doneEdge
589
+ ? `// Continue on done path at node: ${doneEdge.target}`
590
+ : '// Done path not connected';
591
+
592
+ return ` // Loop Node: ${formData.title}
593
+ context.logs.push({
594
+ timestamp: new Date().toISOString(),
595
+ level: 'info',
596
+ nodeId: '${formData.nodeId}',
597
+ message: 'Starting for-each loop',
598
+ });
599
+
600
+ const __loopArray = Array.isArray(${arrayExpr}) ? ${arrayExpr} : [${arrayExpr}];
601
+ const __maxIter = ${maxIterations};
602
+ for (let __i = 0; __i < Math.min(__loopArray.length, __maxIter); __i++) {
603
+ context.variables['${currentItemVariable}'] = __loopArray[__i];
604
+ context.variables['${loopIndexVariable}'] = __i;
605
+ ${bodyComment}
606
+ // Execute loop body nodes here (back-edge returns to loop node)
607
+ }
608
+ ${doneComment}
609
+ `;
610
+ }
611
+
542
612
  /**
543
613
  * Export workflow data as JSON
544
614
  */