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
Edge(from@xyflow/react): Represents a directed connection between two nodes in the graph. Each edge has properties such assourceandtargetindicating the connected nodes.filterAllDownstreamAgentAndToolNodeIds(from./filter-downstream-nodes): A utility function that, given a set of edges and starting node IDs, returns all downstream nodes that are identified as agent or tool nodes.
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
nodeId(string): The identifier of the starting agent operator node from which downstream agent and tool nodes are to be found.edges(Edge[]): The list of all edges in the current graph representing the connections between nodes.
Returns
An object containing:
downstreamAgentAndToolNodeIds(string[]): An array of node IDs representing all downstream agent and tool nodes connected to the inputnodeId.downstreamAgentAndToolEdges(Edge[]): An array of edges that are connected to any of the downstream agent and tool nodes. These edges are relevant for deletion or graph updates.
Description of Internal Logic
Identify downstream nodes: Calls
filterAllDownstreamAgentAndToolNodeIdswith the full edge list and the starting node ID wrapped in an array. This function returns all downstream nodes of type agent or tool.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.
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
Avoiding duplicate edges: The function ensures no duplicate edges are added to the result by checking if an edge with the same
idis already included.Dependency on external filtering: This file does not implement the downstream node filtering logic itself; instead, it relies on the imported
filterAllDownstreamAgentAndToolNodeIdsfunction. This separation keeps responsibilities clean: filtering node IDs is delegated, while this file focuses on aggregating nodes and edges for deletion.Edge relationship: Both incoming and outgoing edges related to downstream nodes are collected, ensuring a comprehensive cleanup of connections.
Interactions with Other Parts of the System
filterAllDownstreamAgentAndToolNodeIds(from./filter-downstream-nodes): This is a critical dependency that performs the graph traversal and filtering logic to identify downstream agent and tool nodes.Graph Representation: The file assumes the graph is represented as a collection of nodes identified by IDs and edges that connect nodes. It works exclusively with edges and node IDs, making it agnostic of node data beyond their IDs and types (agent/tool).
Use in Node Deletion Workflow: Typically, this utility function would be called as part of a larger flow that handles node deletion in the system, ensuring that all dependent downstream nodes (agents/tools) and their edges are also identified and removed or handled accordingly.
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.