@@ -72,12 +72,7 @@ export const duplicateNodes = (
7272 const oldNodeId = node . id ;
7373
7474 if ( isTaskNode ( node ) ) {
75- const oldTaskId = nodeManager . getRefId ( oldNodeId ) ;
76- if ( ! oldTaskId ) {
77- console . warn ( "Could not find taskId for node:" , node ) ;
78- return ;
79- }
80-
75+ const oldTaskId = node . data . taskId as string ;
8176 const oldTaskName = taskIdToTaskName ( oldTaskId ) ;
8277 const newTaskName = getUniqueTaskName ( graphSpec , oldTaskName ) ;
8378 const newTaskId = taskNameToTaskId ( newTaskName ) ;
@@ -104,16 +99,14 @@ export const duplicateNodes = (
10499 } ;
105100 newTasks [ newTaskId ] = newTaskSpec ;
106101 } else if ( isInputNode ( node ) ) {
107- const inputSpec = componentSpec . inputs ?. find (
108- ( input ) => input . name === node . data . label ,
109- ) ;
102+ const inputSpec = node . data . spec ;
110103
111- const newInputName = getUniqueInputName ( componentSpec , inputSpec ? .name ) ;
104+ const newInputName = getUniqueInputName ( componentSpec , inputSpec . name ) ;
112105 const newNodeId = nodeManager . getNodeId ( newInputName , "input" ) ;
113106
114107 nodeIdMap [ oldNodeId ] = newNodeId ;
115108
116- const annotations = inputSpec ? .annotations || { } ;
109+ const annotations = inputSpec . annotations || { } ;
117110
118111 const updatedAnnotations = setPositionInAnnotations ( annotations , {
119112 x : node . position . x + OFFSET ,
@@ -128,19 +121,14 @@ export const duplicateNodes = (
128121
129122 newInputs [ newInputName ] = newInputSpec ;
130123 } else if ( isOutputNode ( node ) ) {
131- const outputSpec = componentSpec . outputs ?. find (
132- ( output ) => output . name === node . data . label ,
133- ) ;
124+ const outputSpec = node . data . spec ;
134125
135- const newOutputName = getUniqueOutputName (
136- componentSpec ,
137- outputSpec ?. name ,
138- ) ;
126+ const newOutputName = getUniqueOutputName ( componentSpec , outputSpec . name ) ;
139127 const newNodeId = nodeManager . getNodeId ( newOutputName , "output" ) ;
140128
141129 nodeIdMap [ oldNodeId ] = newNodeId ;
142130
143- const annotations = outputSpec ? .annotations || { } ;
131+ const annotations = outputSpec . annotations || { } ;
144132
145133 const updatedAnnotations = setPositionInAnnotations ( annotations , {
146134 x : node . position . x + OFFSET ,
@@ -195,13 +183,13 @@ export const duplicateNodes = (
195183 }
196184 } ) ;
197185
198- // Outputs are defined in the graph spec
199186 const updatedGraphOutputs = { ...graphSpec . outputValues } ;
200187 if ( connection !== "none" ) {
201- /* Reconfigure Outputs */
188+ /* Reconfigure Output Nodes */
202189 Object . entries ( newOutputs ) . forEach ( ( output ) => {
203190 const [ outputName ] = output ;
204191 const newNodeId = nodeManager . getNodeId ( outputName , "output" ) ;
192+
205193 const oldNodeId = Object . keys ( nodeIdMap ) . find (
206194 ( key ) => nodeIdMap [ key ] === newNodeId ,
207195 ) ;
@@ -210,15 +198,52 @@ export const duplicateNodes = (
210198 return ;
211199 }
212200
213- const oldOutputName = nodeManager . getRefId ( oldNodeId ) ;
201+ const originalOutputNode = nodesToDuplicate . find (
202+ ( node ) => node . id === oldNodeId && isOutputNode ( node ) ,
203+ ) ;
214204
215- if ( ! graphSpec . outputValues || ! oldOutputName ) {
205+ if ( ! originalOutputNode ) {
216206 return ;
217207 }
218208
219- const outputValue = graphSpec . outputValues [ oldOutputName ] ;
209+ const oldOutputName = (
210+ originalOutputNode . data . spec as OutputSpec
211+ ) . name . toLowerCase ( ) ;
212+
213+ let outputValue : TaskOutputArgument | null = null ;
214+ let connectedTaskId : string | null = null ;
215+ let outputArgName : string | null = null ;
216+
217+ for ( const node of nodesToDuplicate ) {
218+ if ( isTaskNode ( node ) ) {
219+ const taskData = node . data ;
220+ const taskId = taskData . taskId ;
221+ const taskOutputs =
222+ taskData . taskSpec . componentRef . spec ?. outputs || [ ] ;
223+
224+ for ( const taskOutput of taskOutputs ) {
225+ if ( taskOutput . name === oldOutputName ) {
226+ connectedTaskId = taskId ;
227+ outputArgName = taskOutput . name ;
228+
229+ outputValue = {
230+ taskOutput : {
231+ taskId : connectedTaskId ,
232+ outputName : outputArgName ,
233+ } ,
234+ } ;
235+ break ;
236+ }
237+ }
238+
239+ if ( outputValue ) break ;
240+ }
241+ }
220242
221- if ( ! outputValue ) {
243+ if ( ! outputValue || ! connectedTaskId ) {
244+ console . warn (
245+ `No connecting task found for output ${ oldOutputName } in duplicated nodes` ,
246+ ) ;
222247 return ;
223248 }
224249
@@ -233,19 +258,26 @@ export const duplicateNodes = (
233258 ) {
234259 if ( "taskOutput" in updatedOutputValue ) {
235260 const oldTaskId = updatedOutputValue . taskOutput . taskId ;
236- const oldTaskNodeId = nodeManager . getNodeId ( oldTaskId , "task" ) ;
237- if ( oldTaskNodeId in nodeIdMap ) {
238- const newTaskId = nodeManager . getRefId ( nodeIdMap [ oldTaskNodeId ] ) ;
239- if ( ! newTaskId ) {
240- return ;
241- }
242261
243- updatedOutputValue . taskOutput = {
244- ...updatedOutputValue . taskOutput ,
245- taskId : newTaskId ,
246- } ;
262+ const taskNode = nodesToDuplicate . find (
263+ ( node ) => isTaskNode ( node ) && node . data . taskId === oldTaskId ,
264+ ) ;
247265
248- isInternal = true ;
266+ if ( taskNode ) {
267+ const newTaskNodeId = nodeIdMap [ taskNode . id ] ;
268+ if ( newTaskNodeId ) {
269+ const newTaskId = nodeManager . getRefId ( newTaskNodeId ) ;
270+ if ( ! newTaskId ) {
271+ return ;
272+ }
273+
274+ updatedOutputValue . taskOutput = {
275+ ...updatedOutputValue . taskOutput ,
276+ taskId : newTaskId ,
277+ } ;
278+
279+ isInternal = true ;
280+ }
249281 }
250282 }
251283 }
@@ -260,7 +292,7 @@ export const duplicateNodes = (
260292 } ) ;
261293 }
262294
263- /* Update the Graph Spec & Inputs */
295+ /* Update the Graph Spec */
264296 const updatedTasks = { ...graphSpec . tasks , ...newTasks } ;
265297 const updatedGraphSpec = {
266298 ...graphSpec ,
@@ -516,43 +548,61 @@ function reconfigureConnections(
516548
517549 if ( "taskOutput" in argument ) {
518550 const oldTaskId = argument . taskOutput . taskId ;
519- oldNodeId = nodeManager . getNodeId ( oldTaskId , "task" ) ;
520551
521- if ( ! isGraphImplementation ( componentSpec . implementation ) ) {
522- throw new Error ( "ComponentSpec does not contain a graph implementation." ) ;
552+ const taskNode = nodes . find (
553+ ( node ) => isTaskNode ( node ) && node . data . taskId === oldTaskId ,
554+ ) ;
555+
556+ if ( taskNode ) {
557+ oldNodeId = taskNode . id ;
558+ isExternal = false ;
559+ } else {
560+ if ( ! isGraphImplementation ( componentSpec . implementation ) ) {
561+ throw new Error (
562+ "ComponentSpec does not contain a graph implementation." ,
563+ ) ;
564+ }
565+
566+ const graphSpec = componentSpec . implementation . graph ;
567+ isExternal = oldTaskId in graphSpec . tasks ;
523568 }
524569
525- const graphSpec = componentSpec . implementation . graph ;
526- isExternal = oldTaskId in graphSpec . tasks ;
570+ if ( ! oldNodeId ) {
571+ return reconfigureExternalConnection ( taskSpec , argKey , mode ) ;
572+ }
527573
528574 const newNodeId = nodeIdMap [ oldNodeId ] ;
529-
530575 if ( ! newNodeId ) {
531576 return reconfigureExternalConnection ( taskSpec , argKey , mode ) ;
532577 }
533578
534579 const newTaskId = nodeManager . getRefId ( newNodeId ) ;
535-
536580 newArgId = newTaskId ;
537581 } else if ( "graphInput" in argument ) {
538582 const oldInputName = argument . graphInput . inputName ;
539- oldNodeId = nodeManager . getNodeId ( oldInputName , "input" ) ;
540583
541- if ( ! ( "inputs" in componentSpec ) ) {
542- throw new Error ( "ComponentSpec does not contain inputs." ) ;
584+ const inputNode = nodes . find (
585+ ( node ) => isInputNode ( node ) && node . data . spec . name === oldInputName ,
586+ ) ;
587+
588+ if ( inputNode ) {
589+ oldNodeId = inputNode . id ;
590+ isExternal = false ;
591+ } else {
592+ const inputs = componentSpec . inputs || [ ] ;
593+ isExternal = inputs . some ( ( input ) => input . name === oldInputName ) ;
543594 }
544595
545- const inputs = componentSpec . inputs || [ ] ;
546- isExternal = inputs . some ( ( input ) => input . name === oldInputName ) ;
596+ if ( ! oldNodeId ) {
597+ return reconfigureExternalConnection ( taskSpec , argKey , mode ) ;
598+ }
547599
548600 const newNodeId = nodeIdMap [ oldNodeId ] ;
549-
550601 if ( ! newNodeId ) {
551602 return reconfigureExternalConnection ( taskSpec , argKey , mode ) ;
552603 }
553604
554605 const newInputName = nodeManager . getRefId ( newNodeId ) ;
555-
556606 newArgId = newInputName ;
557607 }
558608
0 commit comments