use-before-delete.tsx
Overview
The use-before-delete.tsx file defines a React hook, useBeforeDelete, which provides a pre-deletion handler for nodes and edges in a flow graph editor built with the @xyflow/react library. Its primary purpose is to implement custom logic that determines which nodes and edges can be safely deleted, enforcing specific business rules around undeletable nodes (such as "Begin" or "IterationStart") and handling cascading deletions of downstream "Agent" and "Tool" nodes.
This hook is designed to be integrated into a node-based workflow or flowchart editor where nodes represent different operators or agents and edges represent connections or data flow between them. It ensures that deletions preserve graph integrity and respect domain-specific constraints.
Detailed Explanation
Constants
UndeletableNodes: An array of operator types (
Operator.Begin,Operator.IterationStart) that should not be deleted under normal circumstances. This prevents the removal of critical start nodes or iteration control points.
Hook: useBeforeDelete
This custom React hook returns an object containing the handleBeforeDelete function, which is a callback matching the OnBeforeDelete signature provided by @xyflow/react.
Usage
The hook is used to provide a function that filters and processes nodes and edges before their deletion is finalized in the graph editor.
const { handleBeforeDelete } = useBeforeDelete();
// handleBeforeDelete is passed to the graph component's deletion lifecycle
Function: handleBeforeDelete
const handleBeforeDelete: OnBeforeDelete<RAGFlowNodeType> = async ({
nodes,
edges,
}) => { ... }
Parameters
nodes: Node[]— The list of nodes the user intends to delete.edges: Edge[]— The list of edges the user intends to delete.
Returns
A Promise resolving to an object containing the filtered lists of nodes and edges that are allowed to be deleted:
{
nodes: Node[],
edges: Edge[],
}
Functionality
Filtering Nodes:
Nodes with operator type
Beginare never deleted.Nodes with operator type
IterationStartare only deleted if their parent node is also selected for deletion.All other nodes are considered deletable.
Filtering Edges:
Edges whose source node is in
UndeletableNodes(e.g.,Begin,IterationStart) and whose target node is not selected for deletion are preserved, unless the source node itself is not selected for deletion.All other edges linked to nodes being deleted are allowed to be deleted.
Handling Agent Nodes and Downstream Deletions:
If any node to be deleted is of type
Agent, the function callsdeleteAllDownstreamAgentsAndToolutility.This utility identifies downstream agent and tool nodes and edges connected to the agent node.
These downstream nodes and edges are added to the deletion list if not already included.
This ensures a cascading delete of dependent nodes and edges downstream of an agent.
Example Usage
async function onBeforeDelete(nodes, edges) {
const { nodes: filteredNodes, edges: filteredEdges } = await handleBeforeDelete({ nodes, edges });
// Proceed with deletion using filteredNodes and filteredEdges
}
Supporting Utilities and Imports
RAGFlowNodeType: The type interface representing node data structure.Node,OnBeforeDelete: Types and interfaces from@xyflow/reactrepresenting graph nodes and lifecycle callbacks.Operator: Enum of operator types defining node roles (e.g., Begin, Agent, IterationStart).useGraphStore: Zustand store hook providing graph state accessors (getOperatorTypeFromId,getNode).deleteAllDownstreamAgentsAndTool: Utility function that, given an agent node ID and edges, returns all downstream agent and tool nodes and edges for cascading deletion.
Important Implementation Details
The hook uses Zustand store selectors (
useGraphStore) to fetch operator types and nodes by ID, ensuring the deletion logic is consistent with the current graph state.The filtering logic respects undeletable nodes, preventing accidental deletion of critical nodes.
The cascading deletion algorithm triggered for agent nodes ensures all related downstream nodes and edges are removed to maintain graph consistency.
The hook returns a promise to support asynchronous processing, such as fetching additional graph data if needed in future extensions.
Interaction with Other Parts of the System
This file is part of a flow graph editor system where users can create, delete, and manage nodes and edges representing operators and agents.
The hook's
handleBeforeDeletefunction is typically passed as a prop or lifecycle callback to a graph rendering component from@xyflow/react.It depends on graph state management via
useGraphStore, making it tightly coupled with the centralized store managing node and edge data.The utility
deleteAllDownstreamAgentsAndToolis crucial for the cascading deletion behavior and interacts with the graph's topology.This ensures that deletions respect business rules and graph structural integrity, which is important for applications like workflow automation, RAG (Retrieval-Augmented Generation) pipelines, or agent-based systems.
Visual Diagram
Below is a component diagram representing the main entities and their interactions within use-before-delete.tsx:
flowchart TD
A[useBeforeDelete Hook]
B[handleBeforeDelete Function]
C[useGraphStore]
D[deleteAllDownstreamAgentsAndTool Utility]
E[Graph Nodes & Edges]
F[UndeletableNodes Array]
A --> B
B -->|calls| C
B -->|calls| D
B -->|filters| F
B -->|processes| E
Summary
The file use-before-delete.tsx is a specialized React hook providing robust pre-deletion logic for a graph-based flow editor. It enforces rules around undeletable nodes, handles cascading deletes of downstream agents and tools, and integrates with the global graph state. This ensures the graph's integrity and correctness during node and edge deletions, making it a critical piece in maintaining workflow consistency in complex flow-based applications.