Feat: Delete the agent and tool nodes downstream of the agent node #3221 (#8450)

### What problem does this PR solve?

Feat: Delete the agent and tool nodes downstream of the agent node #3221

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2025-06-24 11:33:01 +08:00
committed by GitHub
parent 49d67cbcb7
commit 07545fbfd3
10 changed files with 162 additions and 43 deletions

View File

@ -5,9 +5,9 @@ import {
} from '@/components/ui/popover';
import { Operator } from '@/pages/agent/constant';
import { AgentFormContext, AgentInstanceContext } from '@/pages/agent/context';
import useGraphStore from '@/pages/agent/store';
import { Position } from '@xyflow/react';
import { PropsWithChildren, useCallback, useContext } from 'react';
import { useDeleteToolNode } from '../use-delete-tool-node';
import { useGetAgentToolNames } from '../use-get-tools';
import { ToolCommand } from './tool-command';
import { useUpdateAgentNodeTools } from './use-update-tools';
@ -17,7 +17,9 @@ export function ToolPopover({ children }: PropsWithChildren) {
const node = useContext(AgentFormContext);
const { updateNodeTools } = useUpdateAgentNodeTools();
const { toolNames } = useGetAgentToolNames();
const { deleteToolNode } = useDeleteToolNode();
const deleteAgentToolNodeById = useGraphStore(
(state) => state.deleteAgentToolNodeById,
);
const handleChange = useCallback(
(value: string[]) => {
@ -29,11 +31,11 @@ export function ToolPopover({ children }: PropsWithChildren) {
nodeId: node?.id,
})();
} else {
deleteToolNode(node.id); // TODO: The tool node should be derived from the agent tools data
deleteAgentToolNodeById(node.id); // TODO: The tool node should be derived from the agent tools data
}
}
},
[addCanvasNode, deleteToolNode, node?.id, updateNodeTools],
[addCanvasNode, deleteAgentToolNodeById, node?.id, updateNodeTools],
);
return (

View File

@ -3,7 +3,6 @@ import { AgentFormContext } from '@/pages/agent/context';
import useGraphStore from '@/pages/agent/store';
import { get } from 'lodash';
import { useCallback, useContext, useMemo } from 'react';
import { useDeleteToolNode } from '../use-delete-tool-node';
export function useGetNodeTools() {
const node = useContext(AgentFormContext);
@ -48,7 +47,9 @@ export function useDeleteAgentNodeTools() {
const { updateNodeForm } = useGraphStore((state) => state);
const tools = useGetNodeTools();
const node = useContext(AgentFormContext);
const { deleteToolNode } = useDeleteToolNode();
const deleteAgentToolNodeById = useGraphStore(
(state) => state.deleteAgentToolNodeById,
);
const deleteNodeTool = useCallback(
(value: string) => () => {
@ -56,11 +57,11 @@ export function useDeleteAgentNodeTools() {
if (node?.id) {
updateNodeForm(node?.id, nextTools, ['tools']);
if (nextTools.length === 0) {
deleteToolNode(node?.id);
deleteAgentToolNodeById(node?.id);
}
}
},
[deleteToolNode, node?.id, tools, updateNodeForm],
[deleteAgentToolNodeById, node?.id, tools, updateNodeForm],
);
return { deleteNodeTool };

View File

@ -1,24 +0,0 @@
import { useCallback } from 'react';
import { NodeHandleId } from '../../constant';
import useGraphStore from '../../store';
export function useDeleteToolNode() {
const { edges, deleteEdgeById, deleteNodeById } = useGraphStore(
(state) => state,
);
const deleteToolNode = useCallback(
(agentNodeId: string) => {
const edge = edges.find(
(x) => x.source === agentNodeId && x.sourceHandle === NodeHandleId.Tool,
);
if (edge) {
deleteEdgeById(edge.id);
deleteNodeById(edge.target);
}
},
[deleteEdgeById, deleteNodeById, edges],
);
return { deleteToolNode };
}