use-save-graph.ts
Overview
use-save-graph.ts is a React hook utility file designed to manage the saving and state synchronization of a graph-based flow within a React application. It provides hooks that encapsulate the logic for persisting the flow graph data, handling flow resets before debugging sessions, and monitoring changes in the agent's state within the graph editor.
This file interacts primarily with flow-related APIs (fetching, setting, resetting flows), a graph state store, and utility hooks to build DSL (Domain Specific Language) data representation of the flow graph. It is intended to be used within functional React components that handle flow editing, saving, and debugging workflows.
Exports and Their Functionalities
1. useSaveGraph
Purpose
A hook to save the current state of the graph flow to a backend or persistent storage by dispatching a "setFlow" API call with the latest DSL representation of the graph.
Usage
const { saveGraph, loading } = useSaveGraph();
await saveGraph(currentNodes);
Details
Parameters:
currentNodes?: RAGFlowNodeType[](optional): An array of nodes currently present in the graph to be saved. If omitted, the default data from the flow fetch is used.
Returns:
An object containing:
saveGraph: An async function that saves the graph data.loading: A boolean indicating whether the save operation is in progress.
Behavior:
Uses
buildDslDatato convert the nodes into a DSL format.Calls
setFlowwith the flow ID, title, and DSL.Uses React's
useCallbackto memoize the function and avoid unnecessary re-renders.
2. useSaveGraphBeforeOpeningDebugDrawer
Purpose
A hook to save the graph and reset the flow's state before opening a debugging drawer UI. This ensures that the flow is saved and reset for a fresh debugging session.
Usage
const { handleRun, loading } = useSaveGraphBeforeOpeningDebugDrawer(() => {
// logic to show debug drawer
});
await handleRun(nextNodes);
Details
Parameters:
show: () => void— A callback function to open or show the debug drawer UI.
Returns:
An object containing:
handleRun: An async function that performs the save and reset sequence before showing the drawer.loading: A boolean flag indicating the save operation status.
Behavior:
Calls
saveGraphto persist the graph.Upon successful save, calls
resetFlowto reset the flow's runtime state.Shows the debug drawer only if both save and reset are successful.
Important:
This hook ensures that all previous debug messages are cleared by resetting the flow before debugging.
3. useWatchAgentChange
Purpose
Monitors changes to the graph's nodes and edges and tracks the last save time of the flow. It is designed to detect and reflect updates in the graph state, primarily used to display or log the last saved time.
Usage
const lastSaveTime = useWatchAgentChange(chatDrawerVisible);
Details
Parameters:
chatDrawerVisible: boolean— A flag indicating whether the chat/debug drawer is visible.
Returns:
A string representing the formatted last save timestamp (
YYYY-MM-DD HH:mm:ss).
Behavior:
Retrieves nodes and edges from a centralized graph store.
Fetches flow details to get the last update time.
Uses
useDebounceEffect(fromahooks) to debounce any graph state changes (nodes or edges) over a 20-second window before triggering side effects (currently commented out save logic).The commented code hints at an intended automatic save feature when the chat drawer is not visible.
Implementation Notes:
Uses
dayjsfor date formatting.The hook currently logs the visibility flag and tracks time but disables auto-saving (commented out).
Important Implementation Details and Algorithms
DSL Building:
The transformation of graph nodes into DSL format is abstracted byuseBuildDslData. This encapsulation simplifies saving by providing a consistent data structure expected by the backend.Debounced Side Effects:
TheuseDebounceEffecthook is used to debounce graph changes to avoid frequent API calls or saves when the user is actively editing the graph. The wait time is set to 20 seconds, balancing responsiveness and performance.Flow Reset Before Debugging:
TheuseSaveGraphBeforeOpeningDebugDrawerhook ensures that the flow is saved and reset atomically before debugging, preventing stale state or messages from impacting the debug session.React Hooks and Memoization:
The use ofuseCallbackand other React hooks ensures efficient re-renders and stable function references, critical for performance in React functional components.
Interaction with Other Parts of the System
API Hooks:
useFetchFlow: Fetches flow data including title and update time.useSetFlow: Provides a method to save or update the flow.useResetFlow: Resets the runtime state of the flow.
State Management:
useGraphStore: A central store managing graph nodes and edges, likely implemented with Zustand or similar state management.
DSL Builder:
useBuildDslData: Converts graph nodes to a DSL representation for backend consumption.
Routing:
useParamsfromumiis used to extract the flowidfrom the URL parameters, ensuring the correct flow is targeted.
UI Components:
The
showcallback inuseSaveGraphBeforeOpeningDebugDraweris intended to show a debug drawer component, integrating UI logic with backend state management.
Visual Diagram
flowchart TD
A[useSaveGraph] -->|calls| B[useFetchFlow]
A -->|calls| C[useSetFlow]
A -->|calls| D[useBuildDslData]
A -->|uses| E[useParams]
F[useSaveGraphBeforeOpeningDebugDrawer] -->|uses| A
F -->|calls| G[useResetFlow]
F -->|invokes| H[show() callback]
I[useWatchAgentChange] -->|reads| J[useGraphStore.nodes & edges]
I -->|reads| B
I -->|uses| K[useDebounceEffect]
I --> L[useState(time)]
style A fill:#f9f,stroke:#333,stroke-width:1px
style F fill:#bbf,stroke:#333,stroke-width:1px
style I fill:#bfb,stroke:#333,stroke-width:1px
Summary
use-save-graph.ts is a utility file that provides React hooks to manage graph flow persistence and debugging state in a React application. It encapsulates asynchronous save operations, flow resetting before debug sessions, and time tracking for changes in the graph. The design leverages React hooks, debounced effects, and centralized state management to optimize performance and ensure data consistency. This file is a critical part of ensuring that graph editing and debugging workflows are smooth, reliable, and integrated with backend services.