Skip to content

Commit 1862972

Browse files
committed
Rework TaskInput and TaskOutput logic
1 parent e3bdb34 commit 1862972

File tree

4 files changed

+160
-80
lines changed

4 files changed

+160
-80
lines changed

src/components/shared/ReactFlow/FlowCanvas/TaskNode/TaskNodeCard/TaskNodeInputs.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { useTaskNode } from "@/providers/TaskNodeProvider";
1111
import { inputsWithInvalidArguments } from "@/services/componentService";
1212
import type { InputSpec } from "@/utils/componentSpec";
1313
import { ComponentSearchFilter } from "@/utils/constants";
14-
import { inputNameToInputId } from "@/utils/nodes/conversions";
1514
import { checkArtifactMatchesSearchFilters } from "@/utils/searchUtils";
1615

1716
import { InputHandle } from "./Handles";
@@ -28,8 +27,8 @@ export function TaskNodeInputs({
2827
expanded,
2928
onBackgroundClick,
3029
}: TaskNodeInputsProps) {
31-
const { getInputNodeId } = useNodeManager();
32-
const { inputs, taskSpec, state, select } = useTaskNode();
30+
const { getTaskInputNodeId } = useNodeManager();
31+
const { taskId, inputs, taskSpec, state, select } = useTaskNode();
3332
const { graphSpec } = useComponentSpec();
3433
const {
3534
highlightSearchFilter,
@@ -147,7 +146,7 @@ export function TaskNodeInputs({
147146
}
148147

149148
const input = inputs.find(
150-
(i) => getInputNodeId(inputNameToInputId(i.name)) === fromHandle?.id,
149+
(i) => getTaskInputNodeId(taskId, i.name) === fromHandle?.id,
151150
);
152151

153152
if (!input) return;

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

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
} from "@/utils/componentSpec";
1111
import { DEFAULT_NODE_DIMENSIONS } from "@/utils/constants";
1212
import {
13-
inputNameToInputId,
14-
outputNameToOutputId,
13+
inputIdToInputName,
14+
outputIdToOutputName,
1515
} from "@/utils/nodes/conversions";
1616

1717
import addTask from "./addTask";
@@ -44,8 +44,58 @@ export function addAndConnectNode({
4444

4545
const oldGraphSpec = componentSpec.implementation.graph;
4646

47-
const fromHandleId = fromHandle?.id;
48-
const fromHandleType = fromHandleId?.startsWith("input") ? "input" : "output";
47+
if (!fromHandle?.id) {
48+
return componentSpec;
49+
}
50+
51+
const isTaskHandle = nodeManager.isManaged(fromHandle.id);
52+
let fromHandleType: "input" | "output";
53+
let fromHandleName: string | undefined;
54+
let fromTaskId: string | undefined;
55+
56+
if (isTaskHandle) {
57+
// Handle is managed by NodeManager (task handle)
58+
const fromHandleInfo = nodeManager.getHandleInfo(fromHandle.id);
59+
const fromNodeType = nodeManager.getNodeType(fromHandle.id);
60+
61+
if (!fromHandleInfo || !fromNodeType) {
62+
return componentSpec;
63+
}
64+
65+
fromHandleType = fromNodeType === "taskInput" ? "input" : "output";
66+
fromHandleName = fromHandleInfo.handleName;
67+
fromTaskId = fromHandleInfo.taskId;
68+
} else {
69+
// Simple IO node handle - get info from the source node, not the handle
70+
const fromNodeId = fromHandle.nodeId;
71+
const fromNodeType = nodeManager.getNodeType(fromNodeId);
72+
73+
if (!fromNodeType) {
74+
return componentSpec;
75+
}
76+
77+
if (fromNodeType === "input") {
78+
fromHandleType = "output";
79+
const inputId = nodeManager.getTaskId(fromNodeId);
80+
if (inputId) {
81+
fromHandleName = inputIdToInputName(inputId);
82+
fromTaskId = inputId;
83+
}
84+
} else if (fromNodeType === "output") {
85+
fromHandleType = "input";
86+
const outputId = nodeManager.getTaskId(fromNodeId);
87+
if (outputId) {
88+
fromHandleName = outputIdToOutputName(outputId);
89+
fromTaskId = outputId;
90+
}
91+
} else {
92+
return componentSpec;
93+
}
94+
}
95+
96+
if (!fromTaskId || !fromHandleName) {
97+
return componentSpec;
98+
}
4999

50100
const adjustedPosition =
51101
fromHandleType === "input"
@@ -77,22 +127,17 @@ export function addAndConnectNode({
77127
const newNodeId = nodeManager.getNodeId(newTaskId, "task");
78128

79129
// 3. Determine the connection data type and find the first matching handle on the new node
80-
if (!fromHandle) {
81-
return newComponentSpec;
130+
let fromComponentSpec: ComponentSpec | undefined;
131+
132+
if (isTaskHandle) {
133+
// Get spec from task
134+
const fromTaskSpec = graphSpec.tasks[fromTaskId];
135+
fromComponentSpec = fromTaskSpec?.componentRef.spec;
136+
} else {
137+
// For IO nodes, get spec from component spec
138+
fromComponentSpec = componentSpec;
82139
}
83140

84-
const fromTaskId = nodeManager.getTaskId(fromHandle.nodeId);
85-
if (!fromTaskId) {
86-
return newComponentSpec;
87-
}
88-
89-
const fromTaskSpec = graphSpec.tasks[fromTaskId];
90-
const fromComponentSpec = fromTaskSpec?.componentRef.spec;
91-
92-
const fromNodeId = fromHandle.nodeId;
93-
94-
const fromHandleName = fromHandleId?.replace(`${fromHandleType}_`, "");
95-
96141
let connectionType: TypeSpecType | undefined;
97142
if (fromHandleType === "input") {
98143
connectionType = fromComponentSpec?.inputs?.find(
@@ -106,7 +151,6 @@ export function addAndConnectNode({
106151

107152
// Find the first matching handle on the new node
108153
const toHandleType = fromHandleType === "input" ? "output" : "input";
109-
110154
let targetHandleId: string | undefined;
111155

112156
if (toHandleType === "input") {
@@ -117,8 +161,11 @@ export function addAndConnectNode({
117161
return newComponentSpec;
118162
}
119163

120-
const inputId = inputNameToInputId(handleName);
121-
targetHandleId = nodeManager.getNodeId(inputId, "taskInput");
164+
targetHandleId = nodeManager.getTaskHandleNodeId(
165+
newTaskId,
166+
handleName,
167+
"taskInput",
168+
);
122169
} else if (toHandleType === "output") {
123170
const handleName = componentRef.spec?.outputs?.find(
124171
(io) => io.type === connectionType,
@@ -127,14 +174,21 @@ export function addAndConnectNode({
127174
return newComponentSpec;
128175
}
129176

130-
const outputId = outputNameToOutputId(handleName);
131-
targetHandleId = nodeManager.getNodeId(outputId, "taskOutput");
177+
targetHandleId = nodeManager.getTaskHandleNodeId(
178+
newTaskId,
179+
handleName,
180+
"taskOutput",
181+
);
132182
}
133183

134184
// 4. Build a Connection object and use handleConnection to add the edge
135-
if (fromNodeId && fromHandleId && targetHandleId) {
185+
if (targetHandleId) {
186+
const fromNodeId = fromHandle.nodeId;
187+
const fromHandleId = fromHandle.id;
188+
136189
const isReversedConnection =
137190
fromHandleType === "input" && toHandleType === "output";
191+
138192
const connection: Connection = isReversedConnection
139193
? // Drawing from an input handle to a new output handle
140194
{

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

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,60 @@ import { setTaskArgument } from "./setTaskArgument";
1616

1717
type NodeInfo = {
1818
id: string;
19-
handle?: string;
2019
type?: NodeType;
20+
handle?: {
21+
taskId: string;
22+
handleName: string;
23+
};
2124
};
2225

2326
export const handleConnection = (
2427
graphSpec: GraphSpec,
2528
connection: Connection,
2629
nodeManager: NodeManager,
2730
) => {
28-
const sourceId = nodeManager.getTaskId(connection.source);
29-
const targetId = nodeManager.getTaskId(connection.target);
30-
31-
if (!sourceId || !targetId) {
32-
console.warn("Source or Target ID is missing in the connection.");
31+
const sourceId = nodeManager.getTaskId(connection.source!);
32+
const sourceType = nodeManager.getNodeType(connection.source!);
33+
34+
const targetId = nodeManager.getTaskId(connection.target!);
35+
const targetType = nodeManager.getNodeType(connection.target!);
36+
37+
if (!sourceId || !targetId || !sourceType || !targetType) {
38+
console.error("Could not resolve node information:", {
39+
sourceId,
40+
sourceType,
41+
targetId,
42+
targetType,
43+
});
3344
return graphSpec;
3445
}
3546

3647
if (sourceId === targetId) {
37-
console.warn(
38-
"Source and Target IDs are the same. Self-connections are not allowed.",
39-
);
48+
console.warn("Cannot connect node to itself");
4049
return graphSpec;
4150
}
4251

43-
const sourceHandleId = connection.sourceHandle
44-
? nodeManager.getTaskId(connection.sourceHandle)
45-
: undefined;
52+
let sourceHandleInfo: { taskId: string; handleName: string } | undefined;
53+
let targetHandleInfo: { taskId: string; handleName: string } | undefined;
54+
55+
if (connection.sourceHandle) {
56+
sourceHandleInfo = nodeManager.getHandleInfo(connection.sourceHandle);
57+
}
4658

47-
const targetHandleId = connection.targetHandle
48-
? nodeManager.getTaskId(connection.targetHandle)
49-
: undefined;
59+
if (connection.targetHandle) {
60+
targetHandleInfo = nodeManager.getHandleInfo(connection.targetHandle);
61+
}
5062

5163
const source: NodeInfo = {
5264
id: sourceId,
53-
handle: sourceHandleId,
54-
type: nodeManager.getNodeType(connection.source),
65+
type: sourceType,
66+
handle: sourceHandleInfo,
5567
};
5668

5769
const target: NodeInfo = {
5870
id: targetId,
59-
handle: targetHandleId,
60-
type: nodeManager.getNodeType(connection.target),
71+
type: targetType,
72+
handle: targetHandleInfo,
6173
};
6274

6375
const connectionType = `${source.type}_to_${target.type}` as const;
@@ -73,7 +85,7 @@ export const handleConnection = (
7385
return handleTaskToGraphOutput(graphSpec, source, target);
7486

7587
default:
76-
console.warn("Unsupported connection pattern:", connectionType);
88+
console.error("Unsupported connection pattern:", connectionType);
7789
return graphSpec;
7890
}
7991
};
@@ -83,16 +95,19 @@ const handleGraphInputToTask = (
8395
source: NodeInfo,
8496
target: NodeInfo,
8597
): GraphSpec => {
86-
if (!target.handle) {
87-
console.warn("Handle ID is missing for target task node.");
98+
if (!target.handle?.handleName) {
99+
console.error(
100+
"Target handle name missing for graph input to task connection",
101+
);
88102
return graphSpec;
89103
}
90104

91-
const sourceInputName = inputIdToInputName(source.id);
92-
const targetInputName = inputIdToInputName(target.handle);
105+
const inputId = source.id;
106+
const inputName = inputIdToInputName(inputId);
107+
const targetInputName = target.handle.handleName;
93108

94109
const graphInputArgument: GraphInputArgument = {
95-
graphInput: { inputName: sourceInputName },
110+
graphInput: { inputName },
96111
};
97112

98113
return setTaskArgument(
@@ -108,18 +123,18 @@ const handleTaskToTask = (
108123
source: NodeInfo,
109124
target: NodeInfo,
110125
): GraphSpec => {
111-
if (!source.handle) {
112-
console.warn("Handle ID is missing for source task node.");
126+
if (!source.handle?.handleName) {
127+
console.error("Source handle name missing for task to task connection");
113128
return graphSpec;
114129
}
115130

116-
if (!target.handle) {
117-
console.warn("Handle ID is missing for target task node.");
131+
if (!target.handle?.handleName) {
132+
console.error("Target handle name missing for task to task connection");
118133
return graphSpec;
119134
}
120135

121-
const sourceOutputName = outputIdToOutputName(source.handle);
122-
const targetInputName = inputIdToInputName(target.handle);
136+
const sourceOutputName = source.handle.handleName;
137+
const targetInputName = target.handle.handleName;
123138

124139
const taskOutputArgument: TaskOutputArgument = {
125140
taskOutput: {
@@ -141,12 +156,16 @@ const handleTaskToGraphOutput = (
141156
source: NodeInfo,
142157
target: NodeInfo,
143158
): GraphSpec => {
144-
if (!source.handle) {
145-
console.warn("Handle ID is missing for source task node.");
159+
if (!source.handle?.handleName) {
160+
console.error(
161+
"Source handle name missing for task to graph output connection",
162+
);
146163
return graphSpec;
147164
}
148165

149-
const sourceOutputName = outputIdToOutputName(source.handle);
166+
const sourceOutputName = source.handle.handleName;
167+
const outputId = target.id;
168+
const outputName = outputIdToOutputName(outputId);
150169

151170
const taskOutputArgument: TaskOutputArgument = {
152171
taskOutput: {
@@ -155,5 +174,5 @@ const handleTaskToGraphOutput = (
155174
},
156175
};
157176

158-
return setGraphOutputValue(graphSpec, target.id, taskOutputArgument);
177+
return setGraphOutputValue(graphSpec, outputName, taskOutputArgument);
159178
};

0 commit comments

Comments
 (0)