store.ts


Overview

The store.ts file implements a centralized state management store for managing graph data structures, specifically nodes and edges, within a React application. It leverages the Zustand state management library with middleware for devtools integration and immutable state updates via Immer.

This store is tailored for a flow editor or graph visualization UI that handles domain-specific nodes (RAGFlowNodeType) and edges (Edge) representing connections between nodes. It provides a rich set of actions and selectors for manipulating the graph state, including adding, updating, duplicating, and deleting nodes and edges, as well as managing selections and form data associated with nodes.

The store orchestrates complex workflows such as:

It is designed to be consumed by React components to enable reactive and efficient graph editing experiences.


Detailed Documentation

Types

RFState (Type Alias)

Defines the shape of the graph store state and its actions.

Property / Method

Type / Signature

Description

nodes

RAGFlowNodeType[]

Array of graph nodes.

edges

Edge[]

Array of edges representing connections between nodes.

selectedNodeIds

string[]

Array of currently selected node IDs.

selectedEdgeIds

string[]

Array of currently selected edge IDs.

clickedNodeId

string

ID of the node currently clicked/active.

clickedToolId

string

ID of the currently selected tool.

onNodesChange

(changes: NodeChange[]) => void

Handler for node changes (add, update, remove).

onEdgesChange

(changes: EdgeChange[]) => void

Handler for edge changes.

onEdgeMouseEnter

[EdgeMouseHandler

undefined](/projects/311/73683)

onEdgeMouseLeave

[EdgeMouseHandler

undefined](/projects/311/73683)

onConnect

(connection: Connection) => void

Handler called when a new edge is created by connecting nodes.

onSelectionChange

(params: OnSelectionChangeParams) => void

Handler called when selection of nodes/edges changes.

setNodes

(nodes: RAGFlowNodeType[]) => void

Replace all nodes in the store.

setEdges

(edges: Edge[]) => void

Replace all edges in the store.

setEdgesByNodeId

(nodeId: string, edges: Edge[]) => void

Replace edges downstream from a given node.

updateNodeForm

[(nodeId: string, values: any, path?: (string

number)[]) => RAGFlowNodeType[]](/projects/311/73962)

addNode

(node: RAGFlowNodeType) => void

Add a new node.

getNode

[(id?: string

null) => RAGFlowNodeType

updateNode

(node: RAGFlowNodeType) => void

Update an existing node by replacing it.

addEdge

(connection: Connection) => void

Add a new edge/connection.

getEdge

[(id: string) => Edge

undefined](/projects/311/71659)

updateFormDataOnConnect

(connection: Connection) => void

Update node form data when a new edge connection is established.

updateSwitchFormData

[(source: string, sourceHandle?: string

null, target?: string

duplicateNode

(id: string, name: string) => void

Duplicate a node by ID with a new name.

duplicateIterationNode

(id: string, name: string) => void

Specialized duplication for Iteration nodes including their children.

deleteEdge

() => void

Delete all currently selected edges.

deleteEdgeById

(id: string) => void

Delete an edge by its ID, including form updates to related nodes.

deleteNodeById

(id: string) => void

Delete a node and its edges by ID.

deleteAgentDownstreamNodesById

(id: string) => void

Delete an Agent node and all its downstream Agents and Tools recursively.

deleteAgentToolNodeById

(id: string) => void

Delete a Tool node linked to an Agent node by ID.

deleteIterationNodeById

(id: string) => void

Delete an Iteration node and all its child nodes and related edges.

findNodeByName

[(operatorName: Operator) => RAGFlowNodeType

undefined](/projects/311/73962)

updateMutableNodeFormItem

(id: string, field: string, value: any) => void

Update a single mutable form field in a node.

getOperatorTypeFromId

[(id?: string

null) => string

getParentIdById

[(id?: string

null) => string

updateNodeName

(id: string, name: string) => void

Update the display name of a node.

generateNodeName

(name: string) => string

Generate a unique node name given a base name by incrementing suffixes.

setClickedNodeId

(id?: string) => void

Set the clicked node ID in state.

setClickedToolId

(id?: string) => void

Set the clicked tool ID in state.

findUpstreamNodeById

[(id?: string

null) => RAGFlowNodeType

deleteEdgesBySourceAndSourceHandle

(source: string, sourceHandle: string) => void

Delete edges from a source node and source handle (used for Switch conditions).

findAgentToolNodeById

[(id: string

null) => string

selectNodeIds

(nodeIds: string[]) => void

Update nodes' selection state based on an array of node IDs.


The useGraphStore Hook

The core of this file is the useGraphStore hook created via zustand:

const useGraphStore = create<RFState>()(
  devtools(
    immer((set, get) => ({
      // ...state and actions as described above
    })),
    { name: 'graph', trace: true },
  ),
);

Key Functions and Methods

onNodesChange(changes: NodeChange[]): void

onEdgesChange(changes: EdgeChange[]): void

onConnect(connection: Connection): void

updateFormDataOnConnect(connection: Connection): void

duplicateNode(id: string, name: string): void

deleteEdgeById(id: string): void

deleteAgentDownstreamNodesById(id: string): void

updateSwitchFormData(source: string, sourceHandle?: string | null, target?: string | null, isConnecting?: boolean): void


Implementation Details and Algorithms


Interactions with Other Parts of the System


Usage Example

import useGraphStore from './store';

// In a React component
function GraphEditor() {
  const nodes = useGraphStore((state) => state.nodes);
  const edges = useGraphStore((state) => state.edges);
  const addNode = useGraphStore((state) => state.addNode);

  // Add new node on button click
  const handleAddNode = () => {
    const newNode = {
      id: 'node-1',
      data: { label: 'New Node', form: {} },
      position: { x: 100, y: 200 },
      type: 'default',
    };
    addNode(newNode);
  };

  return (
    <>
      <button onClick={handleAddNode}>Add Node</button>
      {/* React Flow component here with nodes and edges */}
    </>
  );
}

Visual Diagram

classDiagram
    class useGraphStore {
        -nodes: RAGFlowNodeType[]
        -edges: Edge[]
        -selectedNodeIds: string[]
        -selectedEdgeIds: string[]
        -clickedNodeId: string
        -clickedToolId: string
        +onNodesChange(changes)
        +onEdgesChange(changes)
        +onConnect(connection)
        +onEdgeMouseEnter(event, edge)
        +onEdgeMouseLeave(event, edge)
        +onSelectionChange(params)
        +setNodes(nodes)
        +setEdges(edges)
        +setEdgesByNodeId(nodeId, edges)
        +addNode(node)
        +getNode(id)
        +updateNode(node)
        +addEdge(connection)
        +getEdge(id)
        +updateFormDataOnConnect(connection)
        +updateSwitchFormData(source, sourceHandle, target, isConnecting)
        +duplicateNode(id, name)
        +duplicateIterationNode(id, name)
        +deleteEdge()
        +deleteEdgeById(id)
        +deleteNodeById(id)
        +deleteAgentDownstreamNodesById(id)
        +deleteAgentToolNodeById(id)
        +deleteIterationNodeById(id)
        +findNodeByName(name)
        +updateNodeForm(nodeId, values, path)
        +updateMutableNodeFormItem(id, field, value)
        +getOperatorTypeFromId(id)
        +getParentIdById(id)
        +updateNodeName(id, name)
        +generateNodeName(name)
        +setClickedNodeId(id)
        +setClickedToolId(id)
        +findUpstreamNodeById(id)
        +deleteEdgesBySourceAndSourceHandle(source, sourceHandle)
        +findAgentToolNodeById(id)
        +selectNodeIds(nodeIds)
    }

Summary

The store.ts file is a comprehensive state management module for a React Flow graph editor, managing nodes, edges, and associated UI state with domain-specific logic for node types and operator behavior. It provides a robust API for graph manipulation, ensuring state consistency and enabling advanced features like duplication, deletion, and form synchronization, all integrated with React Flow and Zustand's modern state management approach.