@@ -461,6 +461,32 @@ export function renameSwitchBranch(
461461 } ;
462462}
463463
464+ export function updateSwitchBranchCondition (
465+ node : SwitchNode ,
466+ branchId : string ,
467+ condition : string | undefined ,
468+ ) : SwitchNode {
469+ return {
470+ ...node ,
471+ branches : node . branches . map ( ( b ) =>
472+ b . id === branchId ? { ...b , condition } : b ,
473+ ) ,
474+ } ;
475+ }
476+
477+ export function updateSwitchBranchTarget (
478+ node : SwitchNode ,
479+ branchId : string ,
480+ thenWorkflowName : string | undefined ,
481+ ) : SwitchNode {
482+ return {
483+ ...node ,
484+ branches : node . branches . map ( ( b ) =>
485+ b . id === branchId ? { ...b , thenWorkflowName } : b ,
486+ ) ,
487+ } ;
488+ }
489+
464490// ---------------------------------------------------------------------------
465491// ForkNode
466492// ---------------------------------------------------------------------------
@@ -477,11 +503,22 @@ export function createForkNode(name: string): ForkNode {
477503 } ;
478504}
479505
480- export function addForkBranch ( node : ForkNode , label : string ) : ForkNode {
506+ /**
507+ * Returns the next available branch label of the form `branch-N`, starting
508+ * from 1 and incrementing until a name not already in use is found.
509+ */
510+ export function nextForkBranchLabel ( branches : ForkBranch [ ] ) : string {
511+ const existing = new Set ( branches . map ( ( b ) => b . label ) ) ;
512+ let n = 1 ;
513+ while ( existing . has ( `branch-${ n } ` ) ) n ++ ;
514+ return `branch-${ n } ` ;
515+ }
516+
517+ export function addForkBranch ( node : ForkNode ) : ForkNode {
481518 const zid = newId ( ) ;
482519 const branch : ForkBranch = {
483520 id : zid ,
484- label,
521+ label : nextForkBranchLabel ( node . branches ) ,
485522 graph : emptyFlowGraph ( ) ,
486523 metadata : { [ ZIGFLOW_ID_KEY ] : zid } ,
487524 } ;
@@ -593,7 +630,7 @@ export function insertNode(
593630// Resolve the FlowGraph at the given GraphPath. Throws on invalid paths.
594631//
595632// Segment consumption rules per node type:
596- // loop → 1 segment (nodeId → bodyGraph)
633+ // loop → 2 segments (nodeId + 'body' → bodyGraph)
597634// switch → 2 segments (nodeId + branchId → branch.graph)
598635// fork → 2 segments (nodeId + branchId → branch.graph)
599636// try → 2 segments (nodeId + 'tryGraph'|'catchGraph' → section)
@@ -615,13 +652,7 @@ export function getGraphAtPath(file: WorkflowFile, path: GraphPath): FlowGraph {
615652 throw new Error ( `Node ${ nodeId } is a task and has no sub-graph` ) ;
616653 }
617654
618- if ( node . type === 'loop' ) {
619- graph = node . bodyGraph ;
620- i += 1 ;
621- continue ;
622- }
623-
624- // switch, fork, try — consume one additional segment for the sub-graph id
655+ // switch, fork, try, loop — consume one additional segment for the sub-graph id
625656 i += 1 ;
626657 if ( i >= path . segments . length ) {
627658 throw new Error (
@@ -630,6 +661,17 @@ export function getGraphAtPath(file: WorkflowFile, path: GraphPath): FlowGraph {
630661 }
631662 const subId = path . segments [ i ] ! ;
632663
664+ if ( node . type === 'loop' ) {
665+ if ( subId !== 'body' ) {
666+ throw new Error (
667+ `Expected "body" after loop node ${ nodeId } , got "${ subId } "` ,
668+ ) ;
669+ }
670+ graph = node . bodyGraph ;
671+ i += 1 ;
672+ continue ;
673+ }
674+
633675 if ( node . type === 'switch' ) {
634676 const branch = node . branches . find ( ( b ) => b . id === subId ) ;
635677 if ( ! branch ) {
@@ -693,17 +735,7 @@ function applyTransformAt(
693735 const node = graph . nodes [ nodeId ] ;
694736 if ( ! node ) throw new Error ( `Node ${ nodeId } not found at segment ${ i } ` ) ;
695737
696- if ( node . type === 'loop' ) {
697- const newBody = applyTransformAt (
698- node . bodyGraph ,
699- segments ,
700- i + 1 ,
701- transform ,
702- ) ;
703- return replaceNode ( graph , { ...node , bodyGraph : newBody } ) ;
704- }
705-
706- // switch, fork, try — next segment identifies which sub-graph to descend into
738+ // switch, fork, try, loop — next segment identifies which sub-graph to descend into
707739 const subIndex = i + 1 ;
708740 if ( subIndex >= segments . length ) {
709741 throw new Error (
@@ -756,6 +788,17 @@ function applyTransformAt(
756788 }
757789 }
758790
791+ if ( node . type === 'loop' ) {
792+ // subId is the 'body' literal — descend into bodyGraph.
793+ const newBody = applyTransformAt (
794+ node . bodyGraph ,
795+ segments ,
796+ subIndex + 1 ,
797+ transform ,
798+ ) ;
799+ return replaceNode ( graph , { ...node , bodyGraph : newBody } ) ;
800+ }
801+
759802 throw new Error (
760803 `Node ${ nodeId } (type: ${ node . type } ) cannot be navigated into` ,
761804 ) ;
0 commit comments