use-add-node.ts


Overview

The use-add-node.ts file provides a set of React hooks and utility functions designed to facilitate the creation, positioning, and linking of nodes within a graph-based user interface, specifically for a React Flow diagram environment. It integrates with a custom graph state management store (useGraphStore), manages node initialization parameters, calculates logical node positions to avoid overlap, and handles creation of special node types like Iteration and Tool nodes.

This file is crucial for dynamically adding nodes to a flow graph, initializing node data with default parameters, correctly positioning nodes relative to their parent or siblings, and establishing edges (connections) between nodes. It supports various operator types (e.g., Agent, Tool, Iteration) with specialized behaviors.


Detailed Explanation

Key Concepts


Functions and Hooks

Helper Function: isBottomSubAgent(type: string, position: Position): boolean

Determines if a node type positioned at the bottom is considered a "sub-agent" or a tool.


Hook: useInitializeOperatorParams

Initializes default form parameters for each operator type, leveraging a fetched large language model ID (llmId) for some operators that require it.

const { initializeOperatorParams } = useInitializeOperatorParams();
const initialParams = initializeOperatorParams(Operator.Agent, Position.Bottom);

Hook: useGetNodeName

Provides a function to get the localized display name of an operator type.

const getNodeName = useGetNodeName();
const displayName = getNodeName(Operator.Agent); // e.g., "Agent"

Hook: useCalculateNewlyChildPosition

Calculates the position of a new child node relative to a parent node to avoid overlapping existing child nodes.


Hook: useAddChildEdge

Adds a child edge (connection) from a source node to a target node on the graph.


Hook: useAddToolNode

Handles adding a Tool-type node as a child of an Agent node, ensuring only one Tool node per Agent.


Hook: useResizeIterationNode

Resizes an Iteration parent node dynamically based on the positions of its child nodes to maintain layout integrity.


Hook: useAddNode

The primary hook that exposes functions to add nodes to the graph on the canvas.

const { addCanvasNode, addNoteNode } = useAddNode(reactFlowInstance);

// Add an Agent node to the right of node "node-1"
const addAgentNode = addCanvasNode(Operator.Agent, { nodeId: "node-1", position: Position.Right });
const newNodeId = addAgentNode();

// Add a Note node at mouse click event
addNoteNode(mouseEvent);

Important Implementation Details


Interaction With Other Parts of the System


Visual Diagram

classDiagram
    class useAddNode {
        +addCanvasNode(type: string, params): (event?) => string | undefined
        +addNoteNode(event)
    }
    class useInitializeOperatorParams {
        +initializeOperatorParams(operatorName: Operator, position: Position): object
        +initialFormValuesMap: object
    }
    class useGetNodeName {
        +getNodeName(type: string): string
    }
    class useCalculateNewlyChildPosition {
        +calculateNewlyBackChildPosition(id?: string, sourceHandle?: string): {x: number, y: number}
    }
    class useAddChildEdge {
        +addChildEdge(position: Position, edge: Partial<Connection>): void
    }
    class useAddToolNode {
        +addToolNode(newNode: Node, nodeId?: string): boolean
    }
    class useResizeIterationNode {
        +resizeIterationNode(type: string, position: Position, parentId?: string): void
    }

    useAddNode --> useInitializeOperatorParams : uses
    useAddNode --> useGetNodeName : uses
    useAddNode --> useCalculateNewlyChildPosition : uses
    useAddNode --> useAddChildEdge : uses
    useAddNode --> useAddToolNode : uses
    useAddNode --> useResizeIterationNode : uses

Summary

The use-add-node.ts module is a comprehensive utility for managing the addition of nodes within a React Flow graph environment. It abstracts the complexity of node initialization, positioning, edge creation, and special node handling, providing an easy-to-use API for higher-level components and features to dynamically build and manipulate flow graphs.

It ensures proper layout, uniqueness, and consistency of nodes, supporting a wide variety of operator types with localized labels and default parameters. Its design promotes modularity and reusability via React hooks and integrates seamlessly with the global graph state and React Flow's rendering engine.