|
1 | 1 | import type { Connection } from "@xyflow/react"; |
2 | 2 |
|
3 | | -import type { NodeManager } from "@/nodeManager"; |
| 3 | +import type { NodeManager, NodeType } from "@/nodeManager"; |
4 | 4 | import type { |
5 | 5 | GraphInputArgument, |
6 | 6 | GraphSpec, |
7 | 7 | TaskOutputArgument, |
8 | 8 | } from "@/utils/componentSpec"; |
9 | | -import { inputIdToInputName } from "@/utils/nodes/conversions"; |
| 9 | +import { |
| 10 | + inputIdToInputName, |
| 11 | + outputIdToOutputName, |
| 12 | +} from "@/utils/nodes/conversions"; |
10 | 13 |
|
11 | 14 | import { setGraphOutputValue } from "./setGraphOutputValue"; |
12 | 15 | import { setTaskArgument } from "./setTaskArgument"; |
13 | 16 |
|
| 17 | +type NodeInfo = { |
| 18 | + id: string; |
| 19 | + handle?: string; |
| 20 | + type?: NodeType; |
| 21 | +}; |
| 22 | + |
14 | 23 | export const handleConnection = ( |
15 | 24 | graphSpec: GraphSpec, |
16 | 25 | connection: Connection, |
17 | 26 | nodeManager: NodeManager, |
18 | 27 | ) => { |
19 | | - const targetTaskInputName = connection.targetHandle?.replace(/^input_/, ""); |
20 | | - const sourceTaskOutputName = connection.sourceHandle?.replace(/^output_/, ""); |
21 | | - |
22 | | - const targetId = nodeManager.getTaskId(connection.target); |
23 | 28 | const sourceId = nodeManager.getTaskId(connection.source); |
| 29 | + const targetId = nodeManager.getTaskId(connection.target); |
24 | 30 |
|
25 | | - if (sourceTaskOutputName !== undefined) { |
26 | | - if (!sourceId) { |
27 | | - console.error( |
28 | | - "addConnection: Could not find task ID for source node: ", |
29 | | - connection.source, |
30 | | - ); |
31 | | - return graphSpec; |
32 | | - } |
33 | | - |
34 | | - const taskOutputArgument: TaskOutputArgument = { |
35 | | - taskOutput: { |
36 | | - taskId: sourceId, |
37 | | - outputName: sourceTaskOutputName, |
38 | | - }, |
39 | | - }; |
40 | | - |
41 | | - if (targetTaskInputName !== undefined) { |
42 | | - if (!targetId) { |
43 | | - console.error( |
44 | | - "addConnection: Could not find Input ID for target node: ", |
45 | | - connection.target, |
46 | | - ); |
47 | | - return graphSpec; |
48 | | - } |
49 | | - |
50 | | - return setTaskArgument( |
51 | | - graphSpec, |
52 | | - targetId, |
53 | | - targetTaskInputName, |
54 | | - taskOutputArgument, |
55 | | - ); |
56 | | - } else { |
57 | | - if (!targetId) { |
58 | | - console.error( |
59 | | - "addConnection: Could not find Output ID for target node: ", |
60 | | - connection.target, |
61 | | - ); |
62 | | - return graphSpec; |
63 | | - } |
64 | | - |
65 | | - return setGraphOutputValue(graphSpec, targetId, taskOutputArgument); |
66 | | - // TODO: Perhaps propagate type information |
67 | | - } |
68 | | - } else { |
69 | | - if (!sourceId) { |
70 | | - console.error( |
71 | | - "addConnection: Could not find task ID for source node: ", |
72 | | - connection.source, |
73 | | - ); |
| 31 | + if (!sourceId || !targetId) { |
| 32 | + console.warn("Source or Target ID is missing in the connection."); |
| 33 | + return graphSpec; |
| 34 | + } |
| 35 | + |
| 36 | + if (sourceId === targetId) { |
| 37 | + console.warn( |
| 38 | + "Source and Target IDs are the same. Self-connections are not allowed.", |
| 39 | + ); |
| 40 | + return graphSpec; |
| 41 | + } |
| 42 | + |
| 43 | + const sourceHandleId = connection.sourceHandle |
| 44 | + ? nodeManager.getTaskId(connection.sourceHandle) |
| 45 | + : undefined; |
| 46 | + |
| 47 | + const targetHandleId = connection.targetHandle |
| 48 | + ? nodeManager.getTaskId(connection.targetHandle) |
| 49 | + : undefined; |
| 50 | + |
| 51 | + const source: NodeInfo = { |
| 52 | + id: sourceId, |
| 53 | + handle: sourceHandleId, |
| 54 | + type: nodeManager.getNodeType(connection.source), |
| 55 | + }; |
| 56 | + |
| 57 | + const target: NodeInfo = { |
| 58 | + id: targetId, |
| 59 | + handle: targetHandleId, |
| 60 | + type: nodeManager.getNodeType(connection.target), |
| 61 | + }; |
| 62 | + |
| 63 | + const connectionType = `${source.type}_to_${target.type}` as const; |
| 64 | + |
| 65 | + switch (connectionType) { |
| 66 | + case "input_to_task": |
| 67 | + return handleGraphInputToTask(graphSpec, source, target); |
| 68 | + |
| 69 | + case "task_to_task": |
| 70 | + return handleTaskToTask(graphSpec, source, target); |
| 71 | + |
| 72 | + case "task_to_output": |
| 73 | + return handleTaskToGraphOutput(graphSpec, source, target); |
| 74 | + |
| 75 | + default: |
| 76 | + console.warn("Unsupported connection pattern:", connectionType); |
74 | 77 | return graphSpec; |
75 | | - } |
76 | | - const inputName = inputIdToInputName(sourceId); |
77 | | - const graphInputArgument: GraphInputArgument = { |
78 | | - graphInput: { |
79 | | - inputName: inputName, |
80 | | - }, |
81 | | - }; |
82 | | - if (targetTaskInputName !== undefined) { |
83 | | - if (!targetId) { |
84 | | - console.error( |
85 | | - "addConnection: Could not find Output ID for target node: ", |
86 | | - connection.target, |
87 | | - ); |
88 | | - return graphSpec; |
89 | | - } |
90 | | - |
91 | | - return setTaskArgument( |
92 | | - graphSpec, |
93 | | - targetId, |
94 | | - targetTaskInputName, |
95 | | - graphInputArgument, |
96 | | - ); |
97 | | - } else { |
98 | | - console.error( |
99 | | - "addConnection: Cannot directly connect graph input to graph output: ", |
100 | | - connection, |
101 | | - ); |
102 | | - } |
103 | 78 | } |
| 79 | +}; |
| 80 | + |
| 81 | +const handleGraphInputToTask = ( |
| 82 | + graphSpec: GraphSpec, |
| 83 | + source: NodeInfo, |
| 84 | + target: NodeInfo, |
| 85 | +): GraphSpec => { |
| 86 | + if (!target.handle) { |
| 87 | + console.warn("Handle ID is missing for target task node."); |
| 88 | + return graphSpec; |
| 89 | + } |
| 90 | + |
| 91 | + const sourceInputName = inputIdToInputName(source.id); |
| 92 | + const targetInputName = inputIdToInputName(target.handle); |
| 93 | + |
| 94 | + const graphInputArgument: GraphInputArgument = { |
| 95 | + graphInput: { inputName: sourceInputName }, |
| 96 | + }; |
| 97 | + |
| 98 | + return setTaskArgument( |
| 99 | + graphSpec, |
| 100 | + target.id, |
| 101 | + targetInputName, |
| 102 | + graphInputArgument, |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +const handleTaskToTask = ( |
| 107 | + graphSpec: GraphSpec, |
| 108 | + source: NodeInfo, |
| 109 | + target: NodeInfo, |
| 110 | +): GraphSpec => { |
| 111 | + if (!source.handle) { |
| 112 | + console.warn("Handle ID is missing for source task node."); |
| 113 | + return graphSpec; |
| 114 | + } |
| 115 | + |
| 116 | + if (!target.handle) { |
| 117 | + console.warn("Handle ID is missing for target task node."); |
| 118 | + return graphSpec; |
| 119 | + } |
| 120 | + |
| 121 | + const sourceOutputName = outputIdToOutputName(source.handle); |
| 122 | + const targetInputName = inputIdToInputName(target.handle); |
| 123 | + |
| 124 | + const taskOutputArgument: TaskOutputArgument = { |
| 125 | + taskOutput: { |
| 126 | + taskId: source.id, |
| 127 | + outputName: sourceOutputName, |
| 128 | + }, |
| 129 | + }; |
| 130 | + |
| 131 | + return setTaskArgument( |
| 132 | + graphSpec, |
| 133 | + target.id, |
| 134 | + targetInputName, |
| 135 | + taskOutputArgument, |
| 136 | + ); |
| 137 | +}; |
| 138 | + |
| 139 | +const handleTaskToGraphOutput = ( |
| 140 | + graphSpec: GraphSpec, |
| 141 | + source: NodeInfo, |
| 142 | + target: NodeInfo, |
| 143 | +): GraphSpec => { |
| 144 | + if (!source.handle) { |
| 145 | + console.warn("Handle ID is missing for source task node."); |
| 146 | + return graphSpec; |
| 147 | + } |
| 148 | + |
| 149 | + const sourceOutputName = outputIdToOutputName(source.handle); |
| 150 | + |
| 151 | + const taskOutputArgument: TaskOutputArgument = { |
| 152 | + taskOutput: { |
| 153 | + taskId: source.id, |
| 154 | + outputName: sourceOutputName, |
| 155 | + }, |
| 156 | + }; |
104 | 157 |
|
105 | | - // GraphSpec was not updated (due to an error or other reason) |
106 | | - return graphSpec; |
| 158 | + return setGraphOutputValue(graphSpec, target.id, taskOutputArgument); |
107 | 159 | }; |
0 commit comments