use-save-graph.ts


Overview

The use-save-graph.ts file provides a set of React hooks designed to manage the saving and state synchronization of a graph-based flow or agent configuration within an application. It primarily handles saving the current state of a flow graph to a backend service, resetting the agent before debugging, and automatically persisting changes with debounced effects. These hooks encapsulate complex interactions with APIs and global state stores, ensuring a smooth user experience when editing, saving, and running graph-based agents.


Detailed Explanation of Exports

1. useSaveGraph(showMessage: boolean = true)

Purpose

A custom React hook to save the current graph (agent flow) state to the backend.

Parameters

Returns

Usage

const { saveGraph, loading } = useSaveGraph();

async function onSave() {
  await saveGraph();
}

Description


2. useSaveGraphBeforeOpeningDebugDrawer(show: () => void)

Purpose

A React hook to save the graph and reset the agent before opening a debug drawer (a UI panel for debugging).

Parameters

Returns

Usage

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

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

Description


3. useWatchAgentChange(chatDrawerVisible: boolean)

Purpose

Monitors changes in the graph's nodes and edges, automatically saves the agent after a debounce delay when changes occur, and tracks the last save timestamp.

Parameters

Returns

Usage

const lastSavedTime = useWatchAgentChange(chatDrawerVisible);
return <div>Last saved at: {lastSavedTime}</div>

Description


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[useSaveGraph] -->|calls| B[useFetchAgent]
    A -->|calls| C[useSetAgent]
    A -->|calls| D[useBuildDslData]
    A -->|uses| E[useParams]

    F[useSaveGraphBeforeOpeningDebugDrawer] -->|calls| A
    F -->|calls| G[useResetAgent]
    F -->|executes| H[show() callback]

    I[useWatchAgentChange] -->|reads| J[useGraphStore (nodes, edges)]
    I -->|calls| A
    I -->|calls| B
    I -->|uses| K[formatDate]
    I -->|uses| L[useDebounceEffect]

    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

This file is a utility layer that abstracts the complexity of saving and synchronizing the graph-based agent flows with backend persistence and UI workflows. It provides reusable hooks for saving the graph, preparing the agent for debugging, and automatically persisting changes with debouncing, thereby enhancing developer productivity and user experience in flow-based agent editing applications.