store.ts

Overview

store.ts defines a centralized state management store for handling a graph-based flow editor using React and Zustand. It manages nodes and edges of a flow diagram, which are of type RAGFlowNodeType and Edge, respectively. The store provides reactive state and a comprehensive set of actions to manipulate the graph, including adding, updating, deleting nodes and edges, handling selections, duplications, and specific logic for different operator types such as switches, iterations, and agents.

This file acts as the core state hub for the flow editor, encapsulating all graph-related logic, and enabling components within the application to subscribe to and modify the graph state in a consistent and performant manner.


Detailed Explanation

Types and Imports


RFState Interface

Defines the shape of the store's state and actions:

Property / Method

Type / Signature

Description

nodes

RAGFlowNodeType[]

Array of nodes in the graph.

edges

Edge[]

Array of edges connecting nodes.

selectedNodeIds

string[]

IDs of currently selected nodes.

selectedEdgeIds

string[]

IDs of currently selected edges.

clickedNodeId

string

ID of the node currently clicked/active.

clickedToolId

string

ID of the currently clicked tool.

onNodesChange

(changes) => void

Callback to update nodes on changes.

onEdgesChange

(changes) => void

Callback to update edges on changes.

onEdgeMouseEnter

EdgeMouseHandler (optional)

Handler for mouse enter events on edges.

onEdgeMouseLeave

EdgeMouseHandler (optional)

Handler for mouse leave events on edges.

onConnect

(connection: Connection) => void

Handler when a new edge connection is created.

setNodes

(nodes: RAGFlowNodeType[]) => void

Replaces current nodes with the given array.

setEdges

(edges: Edge[]) => void

Replaces current edges with the given array.

setEdgesByNodeId

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

Updates downstream edges of a given node based on provided edges.

updateNodeForm

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

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

onSelectionChange

(params: OnSelectionChangeParams) => void

Updates selected node and edge IDs based on user selection.

addNode

(node: RAGFlowNodeType) => void

Adds a new node to the graph.

getNode

[(id?: string

null) => RAGFlowNodeType

updateNode

(node: RAGFlowNodeType) => void

Updates an existing node by replacing it in the nodes array.

addEdge

(connection: Connection) => void

Adds a new edge connection between nodes.

getEdge

[(id: string) => Edge

undefined](/projects/311/71659)

updateFormDataOnConnect

(connection: Connection) => void

Updates node form data when an edge is connected (e.g., special logic for switch nodes).

updateSwitchFormData

[(source: string, sourceHandle?: string

null, target?: string

duplicateNode

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

Duplicates a node with a new name, with special handling for iteration nodes.

duplicateIterationNode

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

Duplicates an iteration node along with all its child nodes recursively.

deleteEdge

() => void

Deletes all currently selected edges.

deleteEdgeById

(id: string) => void

Deletes a specific edge and updates related node forms if necessary.

deleteNodeById

(id: string) => void

Deletes a node by ID, with special handling if node is an Agent type.

deleteAgentDownstreamNodesById

(id: string) => void

Deletes all downstream nodes and edges related to an Agent node.

deleteAgentToolNodeById

(id: string) => void

Deletes the tool node connected to an Agent node.

deleteIterationNodeById

(id: string) => void

Deletes an iteration node and all its child nodes.

findNodeByName

[(operatorName: Operator) => RAGFlowNodeType

undefined](/projects/311/73962)

updateMutableNodeFormItem

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

Updates a single form field value on a node.

getOperatorTypeFromId

[(id?: string

null) => string

getParentIdById

[(id?: string

null) => string

updateNodeName

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

Updates the display name of a node.

generateNodeName

(name: string) => string

Generates a unique node name based on existing nodes.

setClickedNodeId

(id?: string) => void

Sets the current clicked node ID.

setClickedToolId

(id?: string) => void

Sets the current clicked tool ID.

findUpstreamNodeById

[(id?: string

null) => RAGFlowNodeType

deleteEdgesBySourceAndSourceHandle

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

Deletes edges from a source node with a specific source handle.

findAgentToolNodeById

[(id: string

null) => string

selectNodeIds

(nodeIds: string[]) => void

Updates nodes' selected property based on provided IDs.


Key Implementations and Algorithms

Edge and Node Change Handlers

Edge Updates by Node

Node Duplication

Form Data Updates on Connections

Deletion Logic


Interaction with Other Parts of the System


Usage Examples

Adding a Node

const newNode: RAGFlowNodeType = {
  id: 'node-1',
  data: { label: 'Agent', name: 'Agent 1', form: {} },
  position: { x: 100, y: 100 },
};
useGraphStore.getState().addNode(newNode);

Connecting Nodes

useGraphStore.getState().onConnect({
  source: 'node-1',
  sourceHandle: 'output-1',
  target: 'node-2',
  targetHandle: 'input-1',
});

Deleting a Node

useGraphStore.getState().deleteNodeById('node-1');

Updating Node Form Data

useGraphStore.getState().updateNodeForm('node-1', { description: 'Updated agent' });

Mermaid Class Diagram

classDiagram
    class RFState {
        +nodes: RAGFlowNodeType[]
        +edges: Edge[]
        +selectedNodeIds: string[]
        +selectedEdgeIds: string[]
        +clickedNodeId: string
        +clickedToolId: string
        +onNodesChange(changes)
        +onEdgesChange(changes)
        +onEdgeMouseEnter(event, edge)
        +onEdgeMouseLeave(event, edge)
        +onConnect(connection)
        +onSelectionChange(params)
        +setNodes(nodes)
        +setEdges(edges)
        +setEdgesByNodeId(nodeId, edges)
        +addNode(node)
        +updateNode(node)
        +getNode(id)
        +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)
    }
    
    class useGraphStore {
        <<singleton>>
        +RFState state and actions
    }

Summary


End of documentation.