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


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

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

  1. Filtering Nodes:

    • Nodes with operator type Begin are never deleted.

    • Nodes with operator type IterationStart are only deleted if their parent node is also selected for deletion.

    • All other nodes are considered deletable.

  2. 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.

  3. Handling Agent Nodes and Downstream Deletions:

    • If any node to be deleted is of type Agent, the function calls deleteAllDownstreamAgentsAndTool utility.

    • 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


Important Implementation Details


Interaction with Other Parts of the System


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.