balibabu commited on
Commit
67bae62
·
1 Parent(s): b06739e

feat: limit there to be only one line between two nodes #918 (#1310)

Browse files

### What problem does this PR solve?

feat: limit there to be only one line between two nodes #918

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

web/src/pages/flow/canvas/index.tsx CHANGED
@@ -16,11 +16,11 @@ import {
16
  useHandleKeyUp,
17
  useSelectCanvasData,
18
  useShowDrawer,
 
19
  } from '../hooks';
20
  import { RagNode } from './node';
21
 
22
  import ChatDrawer from '../chat/drawer';
23
- import { isValidConnection } from '../utils';
24
  import styles from './index.less';
25
  import { BeginNode } from './node/begin-node';
26
  import { CategorizeNode } from './node/categorize-node';
@@ -49,6 +49,7 @@ function FlowCanvas({ chatDrawerVisible, hideChatDrawer }: IProps) {
49
  onNodesChange,
50
  onSelectionChange,
51
  } = useSelectCanvasData();
 
52
 
53
  const { drawerVisible, hideDrawer, showDrawer, clickedNode } =
54
  useShowDrawer();
 
16
  useHandleKeyUp,
17
  useSelectCanvasData,
18
  useShowDrawer,
19
+ useValidateConnection,
20
  } from '../hooks';
21
  import { RagNode } from './node';
22
 
23
  import ChatDrawer from '../chat/drawer';
 
24
  import styles from './index.less';
25
  import { BeginNode } from './node/begin-node';
26
  import { CategorizeNode } from './node/categorize-node';
 
49
  onNodesChange,
50
  onSelectionChange,
51
  } = useSelectCanvasData();
52
+ const isValidConnection = useValidateConnection();
53
 
54
  const { drawerVisible, hideDrawer, showDrawer, clickedNode } =
55
  useShowDrawer();
web/src/pages/flow/hooks.ts CHANGED
@@ -13,7 +13,7 @@ import React, {
13
  useEffect,
14
  useState,
15
  } from 'react';
16
- import { Node, Position, ReactFlowInstance } from 'reactflow';
17
  // import { shallow } from 'zustand/shallow';
18
  import { variableEnabledFieldMap } from '@/constants/chat';
19
  import {
@@ -25,9 +25,9 @@ import { useDebounceEffect } from 'ahooks';
25
  import { FormInstance } from 'antd';
26
  import { humanId } from 'human-id';
27
  import { useParams } from 'umi';
28
- import { NodeMap, Operator } from './constant';
29
  import useGraphStore, { RFState } from './store';
30
- import { buildDslComponentsByGraph } from './utils';
31
 
32
  const selector = (state: RFState) => ({
33
  nodes: state.nodes,
@@ -247,3 +247,26 @@ export const useSetLlmSetting = (form?: FormInstance) => {
247
  form?.setFieldsValue({ ...switchBoxValues, ...otherValues });
248
  }, [form, initialLlmSetting]);
249
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  useEffect,
14
  useState,
15
  } from 'react';
16
+ import { Connection, Node, Position, ReactFlowInstance } from 'reactflow';
17
  // import { shallow } from 'zustand/shallow';
18
  import { variableEnabledFieldMap } from '@/constants/chat';
19
  import {
 
25
  import { FormInstance } from 'antd';
26
  import { humanId } from 'human-id';
27
  import { useParams } from 'umi';
28
+ import { NodeMap, Operator, RestrictedUpstreamMap } from './constant';
29
  import useGraphStore, { RFState } from './store';
30
+ import { buildDslComponentsByGraph, getOperatorTypeFromId } from './utils';
31
 
32
  const selector = (state: RFState) => ({
33
  nodes: state.nodes,
 
247
  form?.setFieldsValue({ ...switchBoxValues, ...otherValues });
248
  }, [form, initialLlmSetting]);
249
  };
250
+
251
+ export const useValidateConnection = () => {
252
+ const edges = useGraphStore((state) => state.edges);
253
+ // restricted lines cannot be connected successfully.
254
+ const isValidConnection = useCallback(
255
+ (connection: Connection) => {
256
+ // limit there to be only one line between two nodes
257
+ const hasLine = edges.some(
258
+ (x) => x.source === connection.source && x.target === connection.target,
259
+ );
260
+
261
+ const ret =
262
+ !hasLine &&
263
+ RestrictedUpstreamMap[
264
+ getOperatorTypeFromId(connection.source) as Operator
265
+ ]?.every((x) => x !== getOperatorTypeFromId(connection.target));
266
+ return ret;
267
+ },
268
+ [edges],
269
+ );
270
+
271
+ return isValidConnection;
272
+ };
web/src/pages/flow/utils.ts CHANGED
@@ -3,13 +3,9 @@ import { removeUselessFieldsFromValues } from '@/utils/form';
3
  import dagre from 'dagre';
4
  import { curry, isEmpty } from 'lodash';
5
  import pipe from 'lodash/fp/pipe';
6
- import { Connection, Edge, MarkerType, Node, Position } from 'reactflow';
7
  import { v4 as uuidv4 } from 'uuid';
8
- import {
9
- Operator,
10
- RestrictedUpstreamMap,
11
- initialFormValuesMap,
12
- } from './constant';
13
  import { NodeData } from './interface';
14
 
15
  const buildEdges = (
@@ -170,11 +166,3 @@ export const buildDslComponentsByGraph = (
170
  export const getOperatorTypeFromId = (id: string | null) => {
171
  return id?.split(':')[0] as Operator | undefined;
172
  };
173
-
174
- // restricted lines cannot be connected successfully.
175
- export const isValidConnection = (connection: Connection) => {
176
- const ret = RestrictedUpstreamMap[
177
- getOperatorTypeFromId(connection.source) as Operator
178
- ]?.every((x) => x !== getOperatorTypeFromId(connection.target));
179
- return ret;
180
- };
 
3
  import dagre from 'dagre';
4
  import { curry, isEmpty } from 'lodash';
5
  import pipe from 'lodash/fp/pipe';
6
+ import { Edge, MarkerType, Node, Position } from 'reactflow';
7
  import { v4 as uuidv4 } from 'uuid';
8
+ import { Operator, initialFormValuesMap } from './constant';
 
 
 
 
9
  import { NodeData } from './interface';
10
 
11
  const buildEdges = (
 
166
  export const getOperatorTypeFromId = (id: string | null) => {
167
  return id?.split(':')[0] as Operator | undefined;
168
  };