@@ -71,12 +71,7 @@ export const duplicateNodes = (
7171 const oldNodeId = node . id ;
7272
7373 if ( isTaskNode ( node ) ) {
74- const oldTaskId = nodeManager . getRefId ( oldNodeId ) ;
75- if ( ! oldTaskId ) {
76- console . warn ( "Could not find taskId for node:" , node ) ;
77- return ;
78- }
79-
74+ const oldTaskId = node . data . taskId ;
8075 const newTaskId = getUniqueTaskId ( graphSpec , oldTaskId ) ;
8176 const newNodeId = nodeManager . getNodeId ( newTaskId , "task" ) ;
8277
@@ -101,16 +96,14 @@ export const duplicateNodes = (
10196 } ;
10297 newTasks [ newTaskId ] = newTaskSpec ;
10398 } else if ( isInputNode ( node ) ) {
104- const inputSpec = componentSpec . inputs ?. find (
105- ( input ) => input . name === node . data . label ,
106- ) ;
99+ const inputSpec = node . data . spec ;
107100
108- const newInputName = getUniqueInputName ( componentSpec , inputSpec ? .name ) ;
101+ const newInputName = getUniqueInputName ( componentSpec , inputSpec . name ) ;
109102 const newNodeId = nodeManager . getNodeId ( newInputName , "input" ) ;
110103
111104 nodeIdMap [ oldNodeId ] = newNodeId ;
112105
113- const annotations = inputSpec ? .annotations || { } ;
106+ const annotations = inputSpec . annotations || { } ;
114107
115108 const updatedAnnotations = setPositionInAnnotations ( annotations , {
116109 x : node . position . x + OFFSET ,
@@ -125,19 +118,14 @@ export const duplicateNodes = (
125118
126119 newInputs [ newInputName ] = newInputSpec ;
127120 } else if ( isOutputNode ( node ) ) {
128- const outputSpec = componentSpec . outputs ?. find (
129- ( output ) => output . name === node . data . label ,
130- ) ;
121+ const outputSpec = node . data . spec ;
131122
132- const newOutputName = getUniqueOutputName (
133- componentSpec ,
134- outputSpec ?. name ,
135- ) ;
123+ const newOutputName = getUniqueOutputName ( componentSpec , outputSpec . name ) ;
136124 const newNodeId = nodeManager . getNodeId ( newOutputName , "output" ) ;
137125
138126 nodeIdMap [ oldNodeId ] = newNodeId ;
139127
140- const annotations = outputSpec ? .annotations || { } ;
128+ const annotations = outputSpec . annotations || { } ;
141129
142130 const updatedAnnotations = setPositionInAnnotations ( annotations , {
143131 x : node . position . x + OFFSET ,
@@ -192,13 +180,13 @@ export const duplicateNodes = (
192180 }
193181 } ) ;
194182
195- // Outputs are defined in the graph spec
196183 const updatedGraphOutputs = { ...graphSpec . outputValues } ;
197184 if ( connection !== "none" ) {
198- /* Reconfigure Outputs */
185+ /* Reconfigure Output Nodes */
199186 Object . entries ( newOutputs ) . forEach ( ( output ) => {
200187 const [ outputName ] = output ;
201188 const newNodeId = nodeManager . getNodeId ( outputName , "output" ) ;
189+
202190 const oldNodeId = Object . keys ( nodeIdMap ) . find (
203191 ( key ) => nodeIdMap [ key ] === newNodeId ,
204192 ) ;
@@ -207,15 +195,52 @@ export const duplicateNodes = (
207195 return ;
208196 }
209197
210- const oldOutputName = nodeManager . getRefId ( oldNodeId ) ;
198+ const originalOutputNode = nodesToDuplicate . find (
199+ ( node ) => node . id === oldNodeId && isOutputNode ( node ) ,
200+ ) ;
211201
212- if ( ! graphSpec . outputValues || ! oldOutputName ) {
202+ if ( ! originalOutputNode ) {
213203 return ;
214204 }
215205
216- const outputValue = graphSpec . outputValues [ oldOutputName ] ;
206+ const oldOutputName = (
207+ originalOutputNode . data . spec as OutputSpec
208+ ) . name . toLowerCase ( ) ;
209+
210+ let outputValue : TaskOutputArgument | null = null ;
211+ let connectedTaskId : string | null = null ;
212+ let outputArgName : string | null = null ;
213+
214+ for ( const node of nodesToDuplicate ) {
215+ if ( isTaskNode ( node ) ) {
216+ const taskData = node . data ;
217+ const taskId = taskData . taskId ;
218+ const taskOutputs =
219+ taskData . taskSpec . componentRef . spec ?. outputs || [ ] ;
220+
221+ for ( const taskOutput of taskOutputs ) {
222+ if ( taskOutput . name === oldOutputName ) {
223+ connectedTaskId = taskId ;
224+ outputArgName = taskOutput . name ;
225+
226+ outputValue = {
227+ taskOutput : {
228+ taskId : connectedTaskId ,
229+ outputName : outputArgName ,
230+ } ,
231+ } ;
232+ break ;
233+ }
234+ }
235+
236+ if ( outputValue ) break ;
237+ }
238+ }
217239
218- if ( ! outputValue ) {
240+ if ( ! outputValue || ! connectedTaskId ) {
241+ console . warn (
242+ `No connecting task found for output ${ oldOutputName } in duplicated nodes` ,
243+ ) ;
219244 return ;
220245 }
221246
@@ -230,19 +255,26 @@ export const duplicateNodes = (
230255 ) {
231256 if ( "taskOutput" in updatedOutputValue ) {
232257 const oldTaskId = updatedOutputValue . taskOutput . taskId ;
233- const oldTaskNodeId = nodeManager . getNodeId ( oldTaskId , "task" ) ;
234- if ( oldTaskNodeId in nodeIdMap ) {
235- const newTaskId = nodeManager . getRefId ( nodeIdMap [ oldTaskNodeId ] ) ;
236- if ( ! newTaskId ) {
237- return ;
238- }
239258
240- updatedOutputValue . taskOutput = {
241- ...updatedOutputValue . taskOutput ,
242- taskId : newTaskId ,
243- } ;
259+ const taskNode = nodesToDuplicate . find (
260+ ( node ) => isTaskNode ( node ) && node . data . taskId === oldTaskId ,
261+ ) ;
244262
245- isInternal = true ;
263+ if ( taskNode ) {
264+ const newTaskNodeId = nodeIdMap [ taskNode . id ] ;
265+ if ( newTaskNodeId ) {
266+ const newTaskId = nodeManager . getRefId ( newTaskNodeId ) ;
267+ if ( ! newTaskId ) {
268+ return ;
269+ }
270+
271+ updatedOutputValue . taskOutput = {
272+ ...updatedOutputValue . taskOutput ,
273+ taskId : newTaskId ,
274+ } ;
275+
276+ isInternal = true ;
277+ }
246278 }
247279 }
248280 }
@@ -257,7 +289,7 @@ export const duplicateNodes = (
257289 } ) ;
258290 }
259291
260- /* Update the Graph Spec & Inputs */
292+ /* Update the Graph Spec */
261293 const updatedTasks = { ...graphSpec . tasks , ...newTasks } ;
262294 const updatedGraphSpec = {
263295 ...graphSpec ,
@@ -513,43 +545,61 @@ function reconfigureConnections(
513545
514546 if ( "taskOutput" in argument ) {
515547 const oldTaskId = argument . taskOutput . taskId ;
516- oldNodeId = nodeManager . getNodeId ( oldTaskId , "task" ) ;
517548
518- if ( ! isGraphImplementation ( componentSpec . implementation ) ) {
519- throw new Error ( "ComponentSpec does not contain a graph implementation." ) ;
549+ const taskNode = nodes . find (
550+ ( node ) => isTaskNode ( node ) && node . data . taskId === oldTaskId ,
551+ ) ;
552+
553+ if ( taskNode ) {
554+ oldNodeId = taskNode . id ;
555+ isExternal = false ;
556+ } else {
557+ if ( ! isGraphImplementation ( componentSpec . implementation ) ) {
558+ throw new Error (
559+ "ComponentSpec does not contain a graph implementation." ,
560+ ) ;
561+ }
562+
563+ const graphSpec = componentSpec . implementation . graph ;
564+ isExternal = oldTaskId in graphSpec . tasks ;
520565 }
521566
522- const graphSpec = componentSpec . implementation . graph ;
523- isExternal = oldTaskId in graphSpec . tasks ;
567+ if ( ! oldNodeId ) {
568+ return reconfigureExternalConnection ( taskSpec , argKey , mode ) ;
569+ }
524570
525571 const newNodeId = nodeIdMap [ oldNodeId ] ;
526-
527572 if ( ! newNodeId ) {
528573 return reconfigureExternalConnection ( taskSpec , argKey , mode ) ;
529574 }
530575
531576 const newTaskId = nodeManager . getRefId ( newNodeId ) ;
532-
533577 newArgId = newTaskId ;
534578 } else if ( "graphInput" in argument ) {
535579 const oldInputName = argument . graphInput . inputName ;
536- oldNodeId = nodeManager . getNodeId ( oldInputName , "input" ) ;
537580
538- if ( ! ( "inputs" in componentSpec ) ) {
539- throw new Error ( "ComponentSpec does not contain inputs." ) ;
581+ const inputNode = nodes . find (
582+ ( node ) => isInputNode ( node ) && node . data . spec . name === oldInputName ,
583+ ) ;
584+
585+ if ( inputNode ) {
586+ oldNodeId = inputNode . id ;
587+ isExternal = false ;
588+ } else {
589+ const inputs = componentSpec . inputs || [ ] ;
590+ isExternal = inputs . some ( ( input ) => input . name === oldInputName ) ;
540591 }
541592
542- const inputs = componentSpec . inputs || [ ] ;
543- isExternal = inputs . some ( ( input ) => input . name === oldInputName ) ;
593+ if ( ! oldNodeId ) {
594+ return reconfigureExternalConnection ( taskSpec , argKey , mode ) ;
595+ }
544596
545597 const newNodeId = nodeIdMap [ oldNodeId ] ;
546-
547598 if ( ! newNodeId ) {
548599 return reconfigureExternalConnection ( taskSpec , argKey , mode ) ;
549600 }
550601
551602 const newInputName = nodeManager . getRefId ( newNodeId ) ;
552-
553603 newArgId = newInputName ;
554604 }
555605
0 commit comments