add aiplugintools

This commit is contained in:
Henry
2023-04-17 20:26:57 +01:00
parent 7631f651ab
commit ed81dc228f
5 changed files with 88 additions and 17 deletions
@@ -13,13 +13,51 @@ export const flowContext = createContext(initialValue)
export const ReactFlowContext = ({ children }) => {
const [reactFlowInstance, setReactFlowInstance] = useState(null)
const deleteNode = (id) => {
reactFlowInstance.setNodes(reactFlowInstance.getNodes().filter((n) => n.id !== id))
reactFlowInstance.setEdges(reactFlowInstance.getEdges().filter((ns) => ns.source !== id && ns.target !== id))
const deleteNode = (nodeid) => {
deleteConnectedInput(nodeid, 'node')
reactFlowInstance.setNodes(reactFlowInstance.getNodes().filter((n) => n.id !== nodeid))
reactFlowInstance.setEdges(reactFlowInstance.getEdges().filter((ns) => ns.source !== nodeid && ns.target !== nodeid))
}
const deleteEdge = (id) => {
reactFlowInstance.setEdges(reactFlowInstance.getEdges().filter((edge) => edge.id !== id))
const deleteEdge = (edgeid) => {
deleteConnectedInput(edgeid, 'edge')
reactFlowInstance.setEdges(reactFlowInstance.getEdges().filter((edge) => edge.id !== edgeid))
}
const deleteConnectedInput = (id, type) => {
const connectedEdges =
type === 'node'
? reactFlowInstance.getEdges().filter((edge) => edge.source === id)
: reactFlowInstance.getEdges().filter((edge) => edge.id === id)
for (const edge of connectedEdges) {
const targetNodeId = edge.target
const sourceNodeId = edge.source
const targetInput = edge.targetHandle.split('-')[2]
reactFlowInstance.setNodes((nds) =>
nds.map((node) => {
if (node.id === targetNodeId) {
let value
const inputAnchor = node.data.inputAnchors.find((ancr) => ancr.name === targetInput)
if (inputAnchor && inputAnchor.list) {
const values = node.data.inputs[targetInput] || []
value = values.filter((item) => !item.includes(sourceNodeId))
} else {
value = ''
}
node.data = {
...node.data,
inputs: {
...node.data.inputs,
[targetInput]: value
}
}
}
return node
})
)
}
}
return (
+17
View File
@@ -233,3 +233,20 @@ export const copyToClipboard = (e) => {
navigator.clipboard.writeText(src)
}
}
export const rearrangeToolsOrdering = (newValues, sourceNodeId) => {
// RequestsGet and RequestsPost have to be in order before other tools
newValues.push(`{{${sourceNodeId}.data.instance}}`)
const sortKey = (item) => {
if (item.includes('requestsGet')) {
return 0
} else if (item.includes('requestsPost')) {
return 1
} else {
return 2
}
}
newValues.sort((a, b) => sortKey(a) - sortKey(b))
}
+6 -2
View File
@@ -38,7 +38,7 @@ import useConfirm from 'hooks/useConfirm'
import { IconX } from '@tabler/icons'
// utils
import { getUniqueNodeId, initNode, getEdgeLabelName } from 'utils/genericHelper'
import { getUniqueNodeId, initNode, getEdgeLabelName, rearrangeToolsOrdering } from 'utils/genericHelper'
import useNotifier from 'utils/useNotifier'
const nodeTypes = { customNode: CanvasNode }
@@ -110,7 +110,11 @@ const Canvas = () => {
const inputAnchor = node.data.inputAnchors.find((ancr) => ancr.name === targetInput)
if (inputAnchor && inputAnchor.list) {
const newValues = node.data.inputs[targetInput] || []
newValues.push(`{{${sourceNodeId}.data.instance}}`)
if (targetInput === 'tools') {
rearrangeToolsOrdering(newValues, sourceNodeId)
} else {
newValues.push(`{{${sourceNodeId}.data.instance}}`)
}
value = newValues
} else {
value = `{{${sourceNodeId}.data.instance}}`