use-add-node.ts


Overview

The use-add-node.ts file provides a collection of React hooks and utility functions designed for managing the creation and positioning of nodes within a graph-based user interface powered by @xyflow/react. It is primarily focused on adding new nodes (operators) to a flow canvas, positioning them according to specific logic, initializing their parameters, and managing their relationships through edges.

This file integrates tightly with a centralized graph state store (useGraphStore) and supports various operator types, each with distinct initial states and behaviors. It implements complex layout logic to prevent overlap of nodes and ensure intuitive graph structures, including support for hierarchical and iteration nodes, tool nodes, and special handling of agents and subagents.


Detailed Documentation

Imports and Dependencies


Key Concepts and Types


Functions and Hooks


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

Utility function to identify if a node is a "bottom sub-agent", which influences initialization and resizing behavior.


useInitializeOperatorParams()

Custom hook that returns a function to initialize operator parameters based on the operator type and position.

Returns:

Usage Example:

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

useGetNodeName()

Returns a function to get the localized display name for a given node type string.

Usage Example:

const getNodeName = useGetNodeName();
const nodeName = getNodeName('Agent'); // localized "Agent" string

useCalculateNewlyChildPosition()

Provides a function to calculate the position for a newly added child node such that it does not overlap existing child nodes.

Returns:

Implementation Details:

Usage Example:

const { calculateNewlyBackChildPosition } = useCalculateNewlyChildPosition();
const newPos = calculateNewlyBackChildPosition('parentNodeId', 'sourceHandleId');

useAddChildEdge()

Hook to add an edge connecting a parent node to a newly created child node when the position is Right.

Returns:

Behavior:

Usage Example:

const { addChildEdge } = useAddChildEdge();
addChildEdge(Position.Right, {
  source: 'node1',
  target: 'node2',
  sourceHandle: 'handle1',
});

useAddToolNode()

Hook to add a "Tool" type node as a child of an "Agent" node.

Returns:

Implementation Details:


useResizeIterationNode()

Hook to resize a parent iteration node dynamically when new child nodes are added, preventing overlaps.

Returns:

Behavior:


useAddNode(reactFlowInstance?: ReactFlowInstance<any, any>)

The principal hook of the file, responsible for adding nodes of various types to the graph canvas.

Parameters:

Returns:

Implementation Highlights:

Usage Example:

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

// Add a retrieval node to the right of node with ID 'abc123'
const nodeId = addCanvasNode(Operator.Retrieval, { nodeId: 'abc123', position: Position.Right })();

// Add a note node on canvas click event
addNoteNode(mouseEvent);

Important Implementation Details and Algorithms


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
    }

    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>)
    }

    class useAddToolNode {
        +addToolNode(newNode: Node, nodeId?: string): boolean
    }

    class useResizeIterationNode {
        +resizeIterationNode(type: string, position: Position, parentId?: string)
    }

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

Summary

The use-add-node.ts file is a critical part of the application's flow graph editor. It encapsulates logic for adding and positioning nodes of various operator types, initializing their state, managing parent-child relationships, and maintaining a clear visual layout. Its design leverages React hooks, centralized state management, and localization, ensuring modularity and maintainability within the larger system.

By using this file's hooks, developers can programmatically and interactively add nodes with complex relationships, supporting a rich, user-friendly graph-building experience.