delete-node.ts


Overview

The delete-node.ts file provides utility functionality related to the deletion of nodes within a directed graph structure, specifically targeting downstream agent and tool operator nodes connected to a given agent operator node. This is primarily used in the context of a flow or pipeline system where nodes represent operators (agents or tools) and edges represent connections or dependencies between these nodes.

The main purpose of this file is to identify and collect all downstream nodes categorized as agents or tools starting from a specified node, along with the edges associated with those nodes, facilitating their removal or further processing.


Detailed Explanation

Imports


Function: deleteAllDownstreamAgentsAndTool

export function deleteAllDownstreamAgentsAndTool(
  nodeId: string,
  edges: Edge[],
): {
  downstreamAgentAndToolNodeIds: string[],
  downstreamAgentAndToolEdges: Edge[],
}

Purpose

This function identifies all downstream agent and tool operator nodes starting from a given nodeId within the graph and collects all edges connected to these nodes. This is useful for cascading deletions or updates when an agent operator node is removed or modified.

Parameters

Returns

An object containing:

Description of Internal Logic

  1. Identify downstream nodes: Calls filterAllDownstreamAgentAndToolNodeIds with the full edge list and the starting node ID wrapped in an array. This function returns all downstream nodes of type agent or tool.

  2. Collect related edges: For each downstream node ID found:

    • Filters edges to find those where the node ID is either the source or the target.

    • Checks if each edge is already included in the accumulator array (pre) to avoid duplicates.

    • Adds new edges to the accumulator.

  3. Return results: The function returns an object with the collected downstream node IDs and their related edges.

Usage Example

import { deleteAllDownstreamAgentsAndTool } from './delete-node';
import { Edge } from '@xyflow/react';

const edges: Edge[] = [
  { id: 'e1', source: 'nodeA', target: 'nodeB' },
  { id: 'e2', source: 'nodeB', target: 'nodeC' },
  // ... other edges
];

const nodeIdToDelete = 'nodeA';

const { downstreamAgentAndToolNodeIds, downstreamAgentAndToolEdges } = deleteAllDownstreamAgentsAndTool(nodeIdToDelete, edges);

console.log('Nodes to delete:', downstreamAgentAndToolNodeIds);
console.log('Edges to delete:', downstreamAgentAndToolEdges);

Important Implementation Details


Interactions with Other Parts of the System


Mermaid Diagram

This class-free utility file can be best represented by a flowchart showing the main function and its relationship with the imported function and data.

flowchart TD
    A[Start: deleteAllDownstreamAgentsAndTool(nodeId, edges)] --> B[Call filterAllDownstreamAgentAndToolNodeIds(edges, [nodeId])]
    B --> C[Receive downstreamAgentAndToolNodeIds]
    C --> D[For each nodeId in downstreamAgentAndToolNodeIds]
    D --> E[Filter edges where source == nodeId or target == nodeId]
    E --> F[Add unique edges to downstreamAgentAndToolEdges]
    F --> G[Return { downstreamAgentAndToolNodeIds, downstreamAgentAndToolEdges }]

Summary

The delete-node.ts file provides a focused utility for graph-based systems to identify and collect all downstream agent and tool nodes and their edges for deletion purposes. It cleanly separates concerns by depending on an external filtering function to find downstream nodes and focuses on assembling the relevant edges for complete removal. This utility is essential for maintaining graph integrity and cascading deletions in workflows involving agent and tool operators.


If you need further details on the related filterAllDownstreamAgentAndToolNodeIds function or integration examples, please refer to the filter-downstream-nodes module or the graph management subsystem documentation.