Skip to content

Commit 5559d1d

Browse files
committed
Add Input Id and Output Id conversions
1 parent 95162d3 commit 5559d1d

File tree

6 files changed

+67
-56
lines changed

6 files changed

+67
-56
lines changed

src/components/shared/ReactFlow/FlowCanvas/utils/addTask.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { deepClone } from "@/utils/deepClone";
1313
import {
1414
getUniqueInputName,
1515
getUniqueOutputName,
16-
getUniqueTaskName,
16+
getUniqueTaskId,
1717
} from "@/utils/unique";
1818

1919
const addTask = (
@@ -62,7 +62,7 @@ const addTask = (
6262
arguments: taskArguments ?? {},
6363
};
6464

65-
const taskId = getUniqueTaskName(
65+
const uniqueTaskId = getUniqueTaskId(
6666
graphSpec,
6767
taskSpec.componentRef.spec?.name ?? "Task",
6868
);
@@ -71,17 +71,17 @@ const addTask = (
7171
...graphSpec,
7272
tasks: {
7373
...graphSpec.tasks,
74-
[taskId]: updatedTaskSpec,
74+
[uniqueTaskId]: updatedTaskSpec,
7575
},
7676
};
7777

7878
newComponentSpec.implementation.graph = newGraphSpec;
7979
}
8080

8181
if (nodeType === "input") {
82-
const inputId = getUniqueInputName(newComponentSpec);
82+
const inputName = getUniqueInputName(newComponentSpec);
8383
const inputSpec: InputSpec = {
84-
name: inputId,
84+
name: inputName,
8585
annotations: positionAnnotations,
8686
};
8787
const inputs = (newComponentSpec.inputs ?? []).concat([inputSpec]);
@@ -90,9 +90,9 @@ const addTask = (
9090
}
9191

9292
if (nodeType === "output") {
93-
const outputId = getUniqueOutputName(newComponentSpec);
93+
const outputName = getUniqueOutputName(newComponentSpec);
9494
const outputSpec: OutputSpec = {
95-
name: outputId,
95+
name: outputName,
9696
annotations: positionAnnotations,
9797
};
9898

src/components/shared/ReactFlow/FlowCanvas/utils/duplicateNodes.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { convertTaskCallbacksToNodeCallbacks } from "@/utils/nodes/taskCallbackU
3232
import {
3333
getUniqueInputName,
3434
getUniqueOutputName,
35-
getUniqueTaskName,
35+
getUniqueTaskId,
3636
} from "@/utils/unique";
3737

3838
const OFFSET = 10;
@@ -78,7 +78,7 @@ export const duplicateNodes = (
7878

7979
if (isTaskNode(node)) {
8080
const oldTaskId = nodeIdToTaskId(oldNodeId);
81-
const newTaskId = getUniqueTaskName(graphSpec, oldTaskId);
81+
const newTaskId = getUniqueTaskId(graphSpec, oldTaskId);
8282
const newNodeId = taskIdToNodeId(newTaskId);
8383

8484
nodeIdMap[oldNodeId] = newNodeId;
@@ -101,9 +101,8 @@ export const duplicateNodes = (
101101
(input) => input.name === node.data.label,
102102
);
103103

104-
const newInputId = getUniqueInputName(componentSpec, inputSpec?.name);
105-
106-
const newNodeId = inputNameToNodeId(newInputId);
104+
const newInputName = getUniqueInputName(componentSpec, inputSpec?.name);
105+
const newNodeId = inputNameToNodeId(newInputName);
107106

108107
nodeIdMap[oldNodeId] = newNodeId;
109108

@@ -116,19 +115,21 @@ export const duplicateNodes = (
116115

117116
const newInputSpec = {
118117
...inputSpec,
119-
name: newInputId,
118+
name: newInputName,
120119
annotations: updatedAnnotations,
121120
};
122121

123-
newInputs[newInputId] = newInputSpec;
122+
newInputs[newInputName] = newInputSpec;
124123
} else if (isOutputNode(node)) {
125124
const outputSpec = componentSpec.outputs?.find(
126125
(output) => output.name === node.data.label,
127126
);
128127

129-
const newOutputId = getUniqueOutputName(componentSpec, outputSpec?.name);
130-
131-
const newNodeId = outputNameToNodeId(newOutputId);
128+
const newOutputName = getUniqueOutputName(
129+
componentSpec,
130+
outputSpec?.name,
131+
);
132+
const newNodeId = outputNameToNodeId(newOutputName);
132133

133134
nodeIdMap[oldNodeId] = newNodeId;
134135

@@ -141,11 +142,11 @@ export const duplicateNodes = (
141142

142143
const newOutputSpec = {
143144
...outputSpec,
144-
name: newOutputId,
145+
name: newOutputName,
145146
annotations: updatedAnnotations,
146147
};
147148

148-
newOutputs[newOutputId] = newOutputSpec;
149+
newOutputs[newOutputName] = newOutputSpec;
149150
}
150151
});
151152

@@ -191,8 +192,8 @@ export const duplicateNodes = (
191192
if (connection !== "none") {
192193
/* Reconfigure Outputs */
193194
Object.entries(newOutputs).forEach((output) => {
194-
const [outputId] = output;
195-
const newNodeId = outputNameToNodeId(outputId);
195+
const [outputName] = output;
196+
const newNodeId = outputNameToNodeId(outputName);
196197
const oldNodeId = Object.keys(nodeIdMap).find(
197198
(key) => nodeIdMap[key] === newNodeId,
198199
);
@@ -201,13 +202,13 @@ export const duplicateNodes = (
201202
return;
202203
}
203204

204-
const oldOutputId = nodeIdToOutputName(oldNodeId);
205+
const oldOutputName = nodeIdToOutputName(oldNodeId);
205206

206207
if (!graphSpec.outputValues) {
207208
return;
208209
}
209210

210-
const outputValue = graphSpec.outputValues[oldOutputId];
211+
const outputValue = graphSpec.outputValues[oldOutputName];
211212

212213
if (!outputValue) {
213214
return;
@@ -242,7 +243,7 @@ export const duplicateNodes = (
242243
(!isInternal && connection === "external") ||
243244
connection === "all"
244245
) {
245-
updatedGraphOutputs[outputId] = updatedOutputValue;
246+
updatedGraphOutputs[outputName] = updatedOutputValue;
246247
}
247248
});
248249
}
@@ -307,9 +308,9 @@ export const duplicateNodes = (
307308

308309
return newNode;
309310
} else if (isInputNode(originalNode)) {
310-
const newInputId = nodeIdToInputName(newNodeId);
311+
const newInputName = nodeIdToInputName(newNodeId);
311312
const newInputSpec = updatedInputs.find(
312-
(input) => input.name === newInputId,
313+
(input) => input.name === newInputName,
313314
);
314315

315316
if (!newInputSpec) {
@@ -337,9 +338,9 @@ export const duplicateNodes = (
337338

338339
return newNode;
339340
} else if (isOutputNode(originalNode)) {
340-
const newOutputId = nodeIdToOutputName(newNodeId);
341+
const newOutputName = nodeIdToOutputName(newNodeId);
341342
const newOutputSpec = updatedOutputs.find(
342-
(output) => output.name === newOutputId,
343+
(output) => output.name === newOutputName,
343344
);
344345

345346
if (!newOutputSpec) {
@@ -406,9 +407,11 @@ export const duplicateNodes = (
406407

407408
updatedGraphSpec.tasks[taskId] = newTaskSpec;
408409
} else if (isInputNode(node)) {
409-
const inputId = nodeIdToInputName(node.id);
410+
const newInputName = nodeIdToInputName(node.id);
410411

411-
const inputSpec = updatedInputs.find((input) => input.name === inputId);
412+
const inputSpec = updatedInputs.find(
413+
(input) => input.name === newInputName,
414+
);
412415

413416
if (!inputSpec) {
414417
return;
@@ -427,17 +430,17 @@ export const duplicateNodes = (
427430
};
428431

429432
const updatedInputIndex = updatedInputs.findIndex(
430-
(input) => input.name === inputId,
433+
(input) => input.name === newInputName,
431434
);
432435

433436
if (updatedInputIndex !== -1) {
434437
updatedInputs[updatedInputIndex] = newInputSpec;
435438
}
436439
} else if (isOutputNode(node)) {
437-
const outputId = nodeIdToOutputName(node.id);
440+
const newOutputName = nodeIdToOutputName(node.id);
438441

439442
const outputSpec = updatedOutputs.find(
440-
(output) => output.name === outputId,
443+
(output) => output.name === newOutputName,
441444
);
442445

443446
if (!outputSpec) {
@@ -457,7 +460,7 @@ export const duplicateNodes = (
457460
};
458461

459462
const updatedOutputIndex = updatedOutputs.findIndex(
460-
(output) => output.name === outputId,
463+
(output) => output.name === newOutputName,
461464
);
462465

463466
if (updatedOutputIndex !== -1) {
@@ -516,25 +519,25 @@ function reconfigureConnections(
516519

517520
newArgId = newTaskId;
518521
} else if ("graphInput" in argument) {
519-
const oldInputId = argument.graphInput.inputName;
520-
oldNodeId = inputNameToNodeId(oldInputId);
522+
const oldInputName = argument.graphInput.inputName;
523+
oldNodeId = inputNameToNodeId(oldInputName);
521524

522525
if (!("inputs" in componentSpec)) {
523526
throw new Error("ComponentSpec does not contain inputs.");
524527
}
525528

526529
const inputs = componentSpec.inputs || [];
527-
isExternal = inputs.some((input) => input.name === oldInputId);
530+
isExternal = inputs.some((input) => input.name === oldInputName);
528531

529532
const newNodeId = nodeIdMap[oldNodeId];
530533

531534
if (!newNodeId) {
532535
return reconfigureExternalConnection(taskSpec, argKey, mode);
533536
}
534537

535-
const newInputId = nodeIdToInputName(newNodeId);
538+
const newInputName = nodeIdToInputName(newNodeId);
536539

537-
newArgId = newInputId;
540+
newArgId = newInputName;
538541
}
539542

540543
if (!newArgId) {

src/components/shared/ReactFlow/FlowCanvas/utils/removeEdge.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { Edge } from "@xyflow/react";
22

33
import type { ComponentSpec, GraphImplementation } from "@/utils/componentSpec";
4-
import { nodeIdToOutputName, nodeIdToTaskId } from "@/utils/nodes/nodeIdUtils";
4+
import {
5+
nodeIdToInputName,
6+
nodeIdToOutputName,
7+
nodeIdToTaskId,
8+
} from "@/utils/nodes/nodeIdUtils";
59

610
import { setGraphOutputValue } from "./setGraphOutputValue";
711
import { setTaskArgument } from "./setTaskArgument";
@@ -10,7 +14,11 @@ export const removeEdge = (edge: Edge, componentSpec: ComponentSpec) => {
1014
const graphSpec = (componentSpec.implementation as GraphImplementation)
1115
?.graph;
1216

13-
const inputName = edge.targetHandle?.replace(/^input_/, "");
17+
if (!edge.targetHandle) {
18+
return componentSpec;
19+
}
20+
21+
const inputName = nodeIdToInputName(edge.targetHandle);
1422

1523
const updatedComponentSpec = {
1624
...componentSpec,

src/components/shared/ReactFlow/FlowCanvas/utils/replaceTaskNode.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
TaskSpec,
77
} from "@/utils/componentSpec";
88
import { deepClone } from "@/utils/deepClone";
9-
import { getUniqueTaskName } from "@/utils/unique";
9+
import { getUniqueTaskId } from "@/utils/unique";
1010

1111
export const replaceTaskNode = (
1212
taskId: string,
@@ -15,15 +15,14 @@ export const replaceTaskNode = (
1515
) => {
1616
const updatedGraphSpec = deepClone(graphSpec);
1717

18-
const taskName = newComponentRef.spec?.name;
19-
const newTaskInputs = newComponentRef.spec?.inputs;
20-
const newTaskOutputs = newComponentRef.spec?.outputs;
21-
const newTaskId = getUniqueTaskName(graphSpec, taskName);
22-
2318
const oldTaskId = taskId;
2419
const oldTask = deepClone(updatedGraphSpec.tasks[oldTaskId]) as TaskSpec;
2520
const oldTaskInputs = oldTask.componentRef.spec?.inputs;
2621

22+
const newTaskInputs = newComponentRef.spec?.inputs;
23+
const newTaskOutputs = newComponentRef.spec?.outputs;
24+
const newTaskId = getUniqueTaskId(graphSpec, oldTaskId);
25+
2726
// Migrate the task to the new componentRef
2827
const updatedTask = {
2928
...oldTask,

src/hooks/useComponentSpecToEdges.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import {
66
} from "@xyflow/react";
77
import { useEffect } from "react";
88

9-
import type {
10-
ArgumentType,
11-
ComponentSpec,
12-
GraphInputArgument,
13-
GraphSpec,
14-
TaskOutputArgument,
15-
TaskSpec,
9+
import {
10+
type ArgumentType,
11+
type ComponentSpec,
12+
type GraphInputArgument,
13+
type GraphSpec,
14+
isGraphImplementation,
15+
type TaskOutputArgument,
16+
type TaskSpec,
1617
} from "@/utils/componentSpec";
1718
import {
1819
inputNameToNodeId,
@@ -42,7 +43,7 @@ const useComponentSpecToEdges = (
4243
};
4344

4445
const getEdges = (componentSpec: ComponentSpec) => {
45-
if (!("graph" in componentSpec.implementation)) {
46+
if (!isGraphImplementation(componentSpec.implementation)) {
4647
return [];
4748
}
4849

src/utils/unique.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const getUniqueOutputName = (
3333
);
3434
};
3535

36-
export const getUniqueTaskName = (
36+
export const getUniqueTaskId = (
3737
graphSpec: GraphSpec,
3838
name: string = "Task",
3939
) => {

0 commit comments

Comments
 (0)