filter-downstream-nodes.ts


Overview

This file provides utility functions to traverse and filter downstream nodes in a directed graph structure, specifically within the context of a flow or pipeline system represented by nodes and edges. The main focus is on identifying downstream nodes connected via specific types of edges, such as agent operators or tools, by filtering edges based on source handles.

These utilities are essential for determining the propagation of effects, dependencies, or operations downstream from a given set of nodes, enabling workflows and logic to be executed or analyzed in a structured manner.


Functions

filterAllDownstreamNodeIds(edges, nodeIds, predicate)

Description

Recursively retrieves all downstream node IDs starting from the specified nodeIds, considering only edges that satisfy the given predicate. It performs a depth-first traversal of the graph edges, filtering edges based on a user-provided condition.

Parameters

Returns

Usage Example

const downstreamNodes = filterAllDownstreamNodeIds(
  edges,
  ['node1', 'node2'],
  (edge) => edge.sourceHandle === 'someHandleId'
);

filterAllDownstreamAgentAndToolNodeIds(edges, nodeIds)

Description

Finds all downstream nodes that are either agent operators or tool operators starting from the given node IDs. This function uses a predicate that matches edges with sourceHandle equal to either AgentBottom or Tool.

Parameters

Returns

Usage Example

const downstreamAgentsAndTools = filterAllDownstreamAgentAndToolNodeIds(
  edges,
  ['agentNode1']
);

filterAllDownstreamAgentNodeIds(edges, nodeIds)

Description

Retrieves all downstream agent operator nodes starting from the given node IDs. It filters edges with sourceHandle equal to AgentBottom only.

Parameters

Returns

Usage Example

const downstreamAgents = filterAllDownstreamAgentNodeIds(edges, ['agentNode1']);

filterDownstreamAgentNodeIds(edges, nodeId)

Description

Gets the direct child agent nodes of the specified single node by filtering edges where the source is the node and the sourceHandle is AgentBottom. Unlike the recursive functions above, this only returns immediate downstream nodes.

Parameters

Returns

Usage Example

const directChildren = filterDownstreamAgentNodeIds(edges, 'agentNode1');

Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram - Function Flowchart

flowchart TD
    A[filterAllDownstreamNodeIds] -->|calls recursively| A
    B[filterAllDownstreamAgentAndToolNodeIds] -->|calls| A
    C[filterAllDownstreamAgentNodeIds] -->|calls| A
    D[filterDownstreamAgentNodeIds]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:1.5px
    style C fill:#bbf,stroke:#333,stroke-width:1.5px
    style D fill:#afa,stroke:#333,stroke-width:1.5px

    B --> A
    C --> A

Diagram Explanation:


Summary

This file offers a set of utility functions to traverse downstream nodes in a graph, filtering based on edge properties to isolate agent and tool nodes in particular. By combining recursion, predicate filtering, and duplicate checks, it efficiently extracts relevant downstream node IDs for use in graph processing, visualization, or execution workflows within the larger application.