use-save-graph.ts


Overview

The use-save-graph.ts file provides React hooks to manage the persistence and state synchronization of a flow graph within an agent-based application. It primarily handles saving the graph's data structure (nodes and edges) to a backend, resetting the agent state before running debug sessions, and automatically saving updates to the graph with debouncing.

This file integrates several custom hooks and utilities to facilitate:


Detailed Explanation of Exports

1. useSaveGraph

Purpose

Provides a hook that returns a saveGraph function to save the current graph state to the backend, along with a loading status indicating if the save operation is in progress.

Signature

useSaveGraph(showMessage?: boolean): { saveGraph: (currentNodes?: RAGFlowNodeType[]) => Promise<any>, loading: boolean }

Parameters

Returns

Behavior

Usage Example

const { saveGraph, loading } = useSaveGraph();

const onSaveClick = async () => {
  await saveGraph(currentNodes);
};

2. useSaveGraphBeforeOpeningDebugDrawer

Purpose

Provides a hook that returns a handleRun function which first saves the graph, then resets the agent state, and finally triggers the opening of a debug drawer UI upon successful operations.

Signature

useSaveGraphBeforeOpeningDebugDrawer(show: () => void): { handleRun: (nextNodes?: RAGFlowNodeType[]) => Promise<void>, loading: boolean }

Parameters

Returns

Behavior

Usage Example

const { handleRun, loading } = useSaveGraphBeforeOpeningDebugDrawer(() => setShowDebugDrawer(true));

<Button onClick={() => handleRun()}>Run Debug</Button>

3. useWatchAgentChange

Purpose

A hook that monitors changes to the graph's nodes and edges, automatically saving the agent after 20 seconds of inactivity (debounced). It also tracks and provides the last save time formatted for display.

Signature

useWatchAgentChange(chatDrawerVisible: boolean): string | undefined

Parameters

Returns

Behavior

Usage Example

const lastSaveTime = useWatchAgentChange(chatDrawerVisible);

return <div>Last saved at: {lastSaveTime || 'never saved'}</div>;

Implementation Details


Interaction with Other Parts of the System

This file acts as the glue between the UI graph representation, agent backend APIs, and user interaction flows (saving, debugging).


Visual Diagram

flowchart TD
    subgraph SaveGraphHook[useSaveGraph]
        SG1[useFetchAgent: fetch agent data]
        SG2[useSetAgent: save agent data]
        SG3[useParams: get agent id]
        SG4[useBuildDslData: build DSL from nodes]
        saveGraph[saveGraph(currentNodes?)]
        saveGraph --> SG4
        saveGraph --> SG2
        saveGraph --> SG1
        saveGraph --> SG3
    end

    subgraph SaveGraphBeforeDebug[useSaveGraphBeforeOpeningDebugDrawer]
        SGBD1[useSaveGraph]
        SGBD2[useResetAgent: reset agent]
        handleRun[handleRun(nextNodes?)]
        handleRun --> SGBD1.saveGraph
        handleRun --> SGBD2.resetAgent
        handleRun --> show[show debug drawer]
    end

    subgraph WatchAgentChange[useWatchAgentChange]
        WAC1[useGraphStore: nodes, edges]
        WAC2[useSaveGraph(showMessage=false)]
        WAC3[useFetchAgent: fetch flow detail]
        WAC4[useDebounceEffect(20s)]
        saveAgent[saveAgent()]
        setSaveTime[setSaveTime(formatted time)]
        saveAgent --> WAC2.saveGraph
        saveAgent --> setSaveTime
        WAC4 --> saveAgent
        useEffectUpdateTime[update time on flowDetail change]
        useEffectUpdateTime --> setSaveTime
    end

    SaveGraphHook --> SaveGraphBeforeDebug
    SaveGraphHook --> WatchAgentChange

Summary

use-save-graph.ts provides essential React hooks for saving and managing the state of an agent flow graph. It handles asynchronous save operations, integrates with backend agent APIs, and supports user workflows including debugging and automatic saving with debouncing. This file is central to synchronizing the frontend graph state with persistent backend storage and ensuring a smooth user experience during graph editing and debugging.