filter-downstream-nodes.ts


Overview

This file provides utility functions for traversing and filtering downstream nodes in a directed graph structure, specifically within the context of agent and tool operators in a workflow or pipeline system. The graph is represented by edges connecting nodes, where each edge has a source node and a target node, along with metadata such as handles that define the type or role of connection.

The main purpose of the file is to offer reusable functions to identify all downstream nodes or subsets thereof (e.g., agent nodes, tool nodes) starting from one or more given nodes, respecting specific filtering criteria based on edge properties.


Detailed Descriptions of Functions

1. filterAllDownstreamNodeIds

function filterAllDownstreamNodeIds(
  edges: Edge[],
  nodeIds: string[],
  predicate: (edge: Edge) => boolean,
): string[]

Purpose

Recursively finds and returns a list of all downstream node IDs connected to the given list of starting node IDs, filtered by a provided predicate function applied to edges.

Parameters

Returns

Usage Example

const downstreamNodeIds = filterAllDownstreamNodeIds(
  edges,
  ['node1', 'node2'],
  (edge) => edge.sourceHandle === NodeHandleId.AgentBottom
);

This example finds all downstream nodes starting from node1 and node2 following edges whose sourceHandle is AgentBottom.

Implementation Details


2. filterAllDownstreamAgentAndToolNodeIds

function filterAllDownstreamAgentAndToolNodeIds(
  edges: Edge[],
  nodeIds: string[],
): string[]

Purpose

Finds all downstream node IDs that are either agent operators at the bottom handle or tool operators, starting from given node IDs.

Parameters

Returns

Usage Example

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

This returns all downstream nodes connected via edges with sourceHandle of either AgentBottom or Tool.

Implementation Details


3. filterAllDownstreamAgentNodeIds

function filterAllDownstreamAgentNodeIds(
  edges: Edge[],
  nodeIds: string[],
): string[]

Purpose

Finds all downstream agent operator node IDs connected from given starting nodes, where edges have the source handle AgentBottom.

Parameters

Returns

Usage Example

const downstreamAgentNodes = filterAllDownstreamAgentNodeIds(edges, ['nodeX']);

Returns all agent nodes reachable downstream from nodeX.

Implementation Details


4. filterDownstreamAgentNodeIds

function filterDownstreamAgentNodeIds(
  edges: Edge[],
  nodeId?: string,
): string[]

Purpose

Retrieves the immediate (direct child) downstream agent node IDs connected from a single given node via edges with source handle AgentBottom.

Parameters

Returns

Usage Example

const directAgentChildren = filterDownstreamAgentNodeIds(edges, 'nodeA');

Gets only the immediate agent nodes connected downstream from nodeA.

Implementation Details


Implementation & Algorithm Notes


Interaction with Other System Components


Mermaid Diagram: Function Flowchart

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

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

    B --> |uses predicate: AgentBottom or Tool| A
    C --> |uses predicate: AgentBottom| A

Summary

The filter-downstream-nodes.ts file is a utility module focused on graph traversal to extract downstream node IDs from a directed graph of operators and tools. It provides flexible and reusable functions that filter edges by role-based handles to identify relevant downstream nodes either recursively or directly connected.

These utilities support workflows or pipeline management by enabling precise identification of dependent nodes, facilitating operations such as update propagation, execution planning, or visualization in systems that model complex agent and tool interactions.