You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.9 KiB
TypeScript
140 lines
3.9 KiB
TypeScript
import { Connection, Edge, Node } from '@xyflow/react';
|
|
import { getHandleType, validateDataType } from '@/utils/flowCommon';
|
|
|
|
export interface ConnectionValidationResult {
|
|
isValid: boolean;
|
|
lineType?: string;
|
|
message?: string;
|
|
}
|
|
|
|
export const validateWorkflowConnection = (
|
|
nodes: Node[],
|
|
connection: Connection
|
|
): ConnectionValidationResult => {
|
|
const sourceNode = nodes.find((node) => node.id === connection.source);
|
|
const targetNode = nodes.find((node) => node.id === connection.target);
|
|
|
|
if (!sourceNode || !targetNode) {
|
|
return { isValid: false };
|
|
}
|
|
|
|
if (sourceNode.id === targetNode.id) {
|
|
return { isValid: false, message: '不允许自旋链接' };
|
|
}
|
|
|
|
const sourceParams: any = sourceNode.data?.parameters || {};
|
|
const targetParams: any = targetNode.data?.parameters || {};
|
|
const sourceHandleType = getHandleType(connection.sourceHandle, sourceParams);
|
|
const targetHandleType = getHandleType(connection.targetHandle, targetParams);
|
|
|
|
if (sourceHandleType !== targetHandleType) {
|
|
return {
|
|
isValid: false,
|
|
message: `连接类型不匹配: ${sourceHandleType}, ${targetHandleType}`,
|
|
};
|
|
}
|
|
|
|
if (
|
|
!validateDataType(
|
|
sourceNode,
|
|
targetNode,
|
|
connection.sourceHandle,
|
|
connection.targetHandle
|
|
)
|
|
) {
|
|
return { isValid: false, message: '数据类型不匹配' };
|
|
}
|
|
|
|
return { isValid: true, lineType: sourceHandleType };
|
|
};
|
|
|
|
export const buildWorkflowConnectionEdge = (
|
|
nodes: Node[],
|
|
connection: Connection,
|
|
lineType: string
|
|
) => {
|
|
const sourceNode = nodes.find((node) => node.id === connection.source);
|
|
const targetNode = nodes.find((node) => node.id === connection.target);
|
|
if (!sourceNode || !targetNode) return null;
|
|
|
|
const sourceParams: any = sourceNode.data?.parameters || {};
|
|
const targetParams: any = targetNode.data?.parameters || {};
|
|
|
|
if (lineType === 'data') {
|
|
const sourceDataOut = (sourceParams.dataOuts || []).find(
|
|
(dataOut: any) =>
|
|
dataOut.name === connection.sourceHandle ||
|
|
dataOut.id === connection.sourceHandle
|
|
);
|
|
const targetDataIn = (targetParams.dataIns || []).find(
|
|
(dataIn: any) =>
|
|
dataIn.name === connection.targetHandle ||
|
|
dataIn.id === connection.targetHandle
|
|
);
|
|
|
|
if (
|
|
sourceDataOut &&
|
|
targetDataIn &&
|
|
sourceDataOut.dataType !== targetDataIn.dataType
|
|
) {
|
|
return {
|
|
edge: null,
|
|
message: `数据类型不匹配,源节点数据类型: ${sourceDataOut.dataType},目标节点数据类型: ${targetDataIn.dataType}`,
|
|
};
|
|
}
|
|
}
|
|
|
|
const edgeParams: Edge = {
|
|
...connection,
|
|
id:
|
|
connection.source && connection.target
|
|
? `e${connection.source}-${connection.target}-${connection.sourceHandle}-${connection.targetHandle}`
|
|
: '',
|
|
type: 'custom',
|
|
data: {
|
|
...(connection as any).data,
|
|
lineType,
|
|
},
|
|
} as Edge;
|
|
|
|
const sourceApi = (sourceParams.apiOuts || []).find(
|
|
(api: any) =>
|
|
(api?.eventId || api.name || api.id) === connection.sourceHandle
|
|
);
|
|
const targetApi = (targetParams.apiIns || []).find(
|
|
(api: any) =>
|
|
(api?.eventId || api.name || api.id) === connection.targetHandle
|
|
);
|
|
|
|
if (sourceApi?.topic) {
|
|
if (
|
|
!targetApi ||
|
|
!targetApi.topic ||
|
|
targetApi.topic.includes('**empty**') ||
|
|
!sourceApi.topic.includes('**empty**')
|
|
) {
|
|
edgeParams.data = {
|
|
...edgeParams.data,
|
|
lineType: 'api',
|
|
displayData: {
|
|
name: sourceApi.eventName,
|
|
eventId: sourceApi.eventId,
|
|
topic: sourceApi.topic,
|
|
},
|
|
};
|
|
}
|
|
} else if (targetApi?.topic && !targetApi.topic.includes('**empty**')) {
|
|
edgeParams.data = {
|
|
...edgeParams.data,
|
|
lineType: 'api',
|
|
displayData: {
|
|
name: targetApi.eventName,
|
|
eventId: targetApi.eventId,
|
|
topic: targetApi.topic,
|
|
},
|
|
};
|
|
}
|
|
|
|
return { edge: edgeParams };
|
|
};
|