delete-node.ts


Overview

The delete-node.ts file provides functionality to identify and collect all downstream agent and tool nodes, along with their associated edges, starting from a specified node in a graph structure. This is particularly useful in workflows or graph-based applications where deleting a node requires also removing or handling downstream dependent nodes (agents and tools) and their connecting edges to maintain consistency and integrity of the graph.

The core functionality centers around the deleteAllDownstreamAgentsAndTool function which leverages graph traversal utilities to filter downstream nodes and then accumulates all related edges for these nodes.


Detailed Documentation

Imports


Function: deleteAllDownstreamAgentsAndTool

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

Purpose

Given a starting node ID and the complete list of edges in the graph, this function:

  1. Retrieves all downstream agent and tool node IDs connected to the starting node.

  2. Identifies all edges related to these downstream nodes (edges where the node is either a source or a target).

  3. Returns both the node IDs and the edges for further processing, such as deletion or update.

Parameters

Returns

An object containing:

Usage Example

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

const edges: Edge[] = [
  { id: 'e1', source: 'node1', target: 'node2' },
  { id: 'e2', source: 'node2', target: 'node3' },
  // ... other edges
];

const nodeId = 'node1';

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

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

Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[deleteAllDownstreamAgentsAndTool(nodeId, edges)]
    A --> B[filterAllDownstreamAgentAndToolNodeIds(edges, [nodeId])]
    B --> C[Array of downstream agent and tool node IDs]
    C --> D[Reduce over downstream node IDs]
    D --> E[Filter edges related to each node ID]
    E --> F[Accumulate unique edges]
    F --> G[Return downstreamAgentAndToolNodeIds and downstreamAgentAndToolEdges]

Summary

This file is a specialized utility to support consistent and safe deletion of nodes and their dependent downstream nodes in graph-based applications or workflow engines.