@@ -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// ---------------------------------------------------------------------------
@@ -593,7 +619,7 @@ export function insertNode(
593619// Resolve the FlowGraph at the given GraphPath. Throws on invalid paths.
594620//
595621// Segment consumption rules per node type:
596- // loop → 1 segment (nodeId → bodyGraph)
622+ // loop → 2 segments (nodeId + 'body' → bodyGraph)
597623// switch → 2 segments (nodeId + branchId → branch.graph)
598624// fork → 2 segments (nodeId + branchId → branch.graph)
599625// try → 2 segments (nodeId + 'tryGraph'|'catchGraph' → section)
@@ -615,13 +641,7 @@ export function getGraphAtPath(file: WorkflowFile, path: GraphPath): FlowGraph {
615641 throw new Error ( `Node ${ nodeId } is a task and has no sub-graph` ) ;
616642 }
617643
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
644+ // switch, fork, try, loop — consume one additional segment for the sub-graph id
625645 i += 1 ;
626646 if ( i >= path . segments . length ) {
627647 throw new Error (
@@ -630,6 +650,17 @@ export function getGraphAtPath(file: WorkflowFile, path: GraphPath): FlowGraph {
630650 }
631651 const subId = path . segments [ i ] ! ;
632652
653+ if ( node . type === 'loop' ) {
654+ if ( subId !== 'body' ) {
655+ throw new Error (
656+ `Expected "body" after loop node ${ nodeId } , got "${ subId } "` ,
657+ ) ;
658+ }
659+ graph = node . bodyGraph ;
660+ i += 1 ;
661+ continue ;
662+ }
663+
633664 if ( node . type === 'switch' ) {
634665 const branch = node . branches . find ( ( b ) => b . id === subId ) ;
635666 if ( ! branch ) {
@@ -693,17 +724,7 @@ function applyTransformAt(
693724 const node = graph . nodes [ nodeId ] ;
694725 if ( ! node ) throw new Error ( `Node ${ nodeId } not found at segment ${ i } ` ) ;
695726
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
727+ // switch, fork, try, loop — next segment identifies which sub-graph to descend into
707728 const subIndex = i + 1 ;
708729 if ( subIndex >= segments . length ) {
709730 throw new Error (
@@ -756,6 +777,17 @@ function applyTransformAt(
756777 }
757778 }
758779
780+ if ( node . type === 'loop' ) {
781+ // subId is the 'body' literal — descend into bodyGraph.
782+ const newBody = applyTransformAt (
783+ node . bodyGraph ,
784+ segments ,
785+ subIndex + 1 ,
786+ transform ,
787+ ) ;
788+ return replaceNode ( graph , { ...node , bodyGraph : newBody } ) ;
789+ }
790+
759791 throw new Error (
760792 `Node ${ nodeId } (type: ${ node . type } ) cannot be navigated into` ,
761793 ) ;
0 commit comments