use-before-delete.tsx


Overview

The use-before-delete.tsx file defines a React hook, useBeforeDelete, that controls the deletion process of nodes and edges within a flow graph editor UI. The core functionality is to selectively filter and manage which nodes and edges can be deleted when a user triggers a delete action, enforcing business rules such as protecting certain undeletable nodes and ensuring downstream dependencies (agents and tools) are also cleaned up properly.

This file plays a critical role in maintaining data integrity and enforcing workflow constraints in a directed graph structure representing some process flow, likely in a visual programming or automation application.


Detailed Explanation

Imports and Constants


useBeforeDelete Hook

This is the default export and main functionality of the file.

export function useBeforeDelete() { ... }

Purpose

To provide a handleBeforeDelete callback function that filters out nodes and edges that should not be deleted and expands the deletion to include downstream dependent nodes (agents and tools).

Internal Functions and Variables

handleBeforeDelete

Signature:

const handleBeforeDelete: OnBeforeDelete<RAGFlowNodeType> = async ({ nodes, edges }) => { ... }

Key Logic Steps

  1. Filter Nodes to Delete
    Nodes of type Begin are never deleted.
    Nodes of type IterationStart are only deletable if their parent node is also in the deletion list.
    All other nodes are deletable.

  2. Filter Edges to Delete
    Edges whose source node is undeletable (Begin or IterationStart) are only deleted if the downstream node is also being deleted or the source node is not in the deletion list. This prevents orphaning by partially deleting edges related to undeletable nodes.

  3. Handle Downstream Agent and Tool Cleanup
    If any Agent nodes are slated for deletion, the function calls deleteAllDownstreamAgentsAndTool utility to retrieve all downstream agents and tools connected via edges.
    It appends these downstream nodes and edges to the deletion list to ensure a clean cascade delete.

  4. Return filtered nodes and edges
    The final filtered lists are returned to the caller, typically the graph editing component, which will proceed with the actual deletion.


Usage Example

import { useBeforeDelete } from './use-before-delete';

function FlowEditor() {
  const { handleBeforeDelete } = useBeforeDelete();

  const onDeleteNodesAndEdges = async (nodes, edges) => {
    const deletionResult = await handleBeforeDelete({ nodes, edges });
    // deletionResult.nodes and deletionResult.edges now contain only allowed deletions
    // Proceed to delete these nodes and edges from the graph state
  };

  return (
    // JSX rendering the graph editor component with onDeleteNodesAndEdges as the delete handler
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram

componentDiagram
    component useBeforeDelete {
        +handleBeforeDelete({nodes, edges})
        -agentPredicate(node)
    }
    component useGraphStore {
        +getOperatorTypeFromId(id)
        +getNode(id)
    }
    component deleteAllDownstreamAgentsAndTool {
        +(nodeId, edges) => {downstreamAgentAndToolEdges, downstreamAgentAndToolNodeIds}
    }
    useBeforeDelete --> useGraphStore : uses
    useBeforeDelete --> deleteAllDownstreamAgentsAndTool : uses

Summary

The use-before-delete.tsx file encapsulates a specialized hook that governs which nodes and edges can be deleted from a flow graph based on operator types, parent-child relationships, and downstream dependencies. It ensures critical nodes remain intact and that cascading deletions handle complex downstream relationships, particularly for agent and tool nodes. It integrates tightly with the graph state store and utility functions, serving as a gatekeeper for safe and consistent graph modification operations in the application.