Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(web): skip canvas autosave of empty nodes/edges (#18783)

Souravrajvi0229503bc8019
diff --git a/web/src/pages/agent/hooks/use-fetch-data.test.ts b/web/src/pages/agent/hooks/use-fetch-data.test.ts
new file mode 100644
index 0000000000..5d5af8a33d
--- /dev/null
+++ b/web/src/pages/agent/hooks/use-fetch-data.test.ts
@@ -0,0 +1,83 @@
+import { act, renderHook } from '@testing-library/react';
+import { useFetchDataOnMount } from './use-fetch-data';
+
+const mockSetGraphInfo = jest.fn();
+const mockRefetch = jest.fn();
+const mockUseFetchAgent = jest.fn();
+
+jest.mock('@/hooks/use-agent-request', () => ({
+ useFetchAgent: () => mockUseFetchAgent(),
+}));
+
+jest.mock('./use-set-graph', () => ({
+ useSetGraphInfo: () => mockSetGraphInfo,
+}));
+
+jest.mock('../utils/dsl-bridge', () => ({
+ dslToGraph: (dsl: { graph?: { nodes: unknown[]; edges: unknown[] } }) => ({
+ nodes: dsl.graph?.nodes ?? [],
+ edges: dsl.graph?.edges ?? [],
+ }),
+}));
+
+describe('useFetchDataOnMount', () => {
+ beforeEach(() => {
+ mockSetGraphInfo.mockClear();
+ mockRefetch.mockClear();
+ });
+
+ it('does not apply an empty graph while agent detail has no dsl', () => {
+ mockUseFetchAgent.mockReturnValue({
+ loading: true,
+ data: {},
+ refetch: mockRefetch,
+ });
+
+ renderHook(() => useFetchDataOnMount());
+
+ expect(mockSetGraphInfo).not.toHaveBeenCalled();
+ });
+
+ it('applies the fetched dsl once it is present', () => {
+ mockUseFetchAgent.mockReturnValue({
+ loading: false,
+ data: {
+ id: 'agent-1',
+ dsl: { graph: { nodes: [{ id: 'begin:0' }], edges: [] } },
+ },
+ refetch: mockRefetch,
+ });
+
+ renderHook(() => useFetchDataOnMount());
+
+ expect(mockSetGraphInfo).toHaveBeenCalledWith({
+ nodes: [{ id: 'begin:0' }],
+ edges: [],
+ });
+ });
+
+ it('does not clear a loaded canvas when later detail snapshots omit dsl', () => {
+ mockUseFetchAgent.mockReturnValue({
+ loading: false,
+ data: {
+ id: 'agent-1',
+ dsl: { graph: { nodes: [{ id: 'begin:0' }], edges: [] } },
+ },
+ refetch: mockRefetch,
+ });
+
+ const { rerender } = renderHook(() => useFetchDataOnMount());
+
+ mockUseFetchAgent.mockReturnValue({
+ loading: false,
+ data: {},
+ refetch: mockRefetch,
+ });
+
+ act(() => {
+ rerender();
+ });
+
+ expect(mockSetGraphInfo).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/web/src/pages/agent/hooks/use-fetch-data.ts b/web/src/pages/agent/hooks/use-fetch-data.ts
index 930739c0b6..f0925ecb09 100644
--- a/web/src/pages/agent/hooks/use-fetch-data.ts
+++ b/web/src/pages/agent/hooks/use-fetch-data.ts
@@ -8,7 +8,10 @@ export const useFetchDataOnMount = () => {
const setGraphInfo = useSetGraphInfo();
useEffect(() => {
- setGraphInfo(dslToGraph(data?.dsl));
+ if (!data?.dsl) {
+ return;
+ }
+ setGraphInfo(dslToGraph(data.dsl));
}, [setGraphInfo, data]);
useEffect(() => {
diff --git a/web/src/pages/agent/hooks/use-save-graph.test.ts b/web/src/pages/agent/hooks/use-save-graph.test.ts
new file mode 100644
index 0000000000..5325b0ad3f
--- /dev/null
+++ b/web/src/pages/agent/hooks/use-save-graph.test.ts
@@ -0,0 +1,39 @@
+import { shouldAutosaveCanvas } from './use-save-graph';
+
+describe('shouldAutosaveCanvas', () => {
+ const ready = {
+ chatDrawerVisible: false,
+ agentId: 'agent-1',
+ agentLoaded: true,
+ nodeCount: 2,
+ edgeCount: 1,
+ };
+
+ it('autosaves a loaded canvas with nodes', () => {
+ expect(shouldAutosaveCanvas(ready)).toBe(true);
+ });
+
+ it('skips while the chat drawer is open', () => {
+ expect(shouldAutosaveCanvas({ ...ready, chatDrawerVisible: true })).toBe(
+ false,
+ );
+ });
+
+ it('skips before the agent id is on the route', () => {
+ expect(shouldAutosaveCanvas({ ...ready, agentId: undefined })).toBe(false);
+ });
+
+ it('skips before the agent detail has loaded', () => {
+ expect(shouldAutosaveCanvas({ ...ready, agentLoaded: false })).toBe(false);
+ });
+
+ it('skips an empty nodes/edges store so autosave cannot wipe a pipeline', () => {
+ expect(
+ shouldAutosaveCanvas({ ...ready, nodeCount: 0, edgeCount: 0 }),
+ ).toBe(false);
+ });
+
+ it('still autosaves when there are nodes but no edges', () => {
+ expect(shouldAutosaveCanvas({ ...ready, edgeCount: 0 })).toBe(true);
+ });
+});
diff --git a/web/src/pages/agent/hooks/use-save-graph.ts b/web/src/pages/agent/hooks/use-save-graph.ts
index d052a39850..d7f5332c46 100644
--- a/web/src/pages/agent/hooks/use-save-graph.ts
+++ b/web/src/pages/agent/hooks/use-save-graph.ts
@@ -31,6 +31,10 @@ export const useSaveGraph = (
},
release?: boolean,
) => {
+ if (!id) {
+ return;
+ }
+
const params: Record<string, any> = {
id,
title: data.title,
@@ -71,8 +75,37 @@ export const useSaveGraphBeforeOpeningDebugDrawer = (show: () => void) => {
return { handleRun, loading };
};
+export function shouldAutosaveCanvas({
+ chatDrawerVisible,
+ agentId,
+ agentLoaded,
+ nodeCount,
+ edgeCount,
+}: {
+ chatDrawerVisible: boolean;
+ agentId?: string;
+ agentLoaded: boolean;
+ nodeCount: number;
+ edgeCount: number;
+}): boolean {
+ if (chatDrawerVisible) {
+ return false;
+ }
+ if (!agentId || !agentLoaded) {
+ return false;
+ }
+ // An empty store is the Zustand default and also the fallback when DSL has
+ // not been applied yet. Autosaving it would PUT components:{} over a real
+ // pipeline (#18771). Explicit Save still persists an empty canvas.
+ if (nodeCount === 0 && edgeCount === 0) {
+ return false;
+ }
+ return true;
+}
+
export const useWatchAgentChange = (chatDrawerVisible: boolean) => {
const [time, setTime] = useState<string>();
+ const { id } = useParams();
const nodes = useGraphStore((state) => state.nodes);
const edges = useGraphStore((state) => state.edges);
const { saveGraph } = useSaveGraph(false, true);
@@ -87,11 +120,30 @@ export const useWatchAgentChange = (chatDrawerVisible: boolean) => {
}, [flowDetail, setSaveTime]);
const saveAgent = useCallback(async () => {
- if (!chatDrawerVisible) {
- const ret = await saveGraph();
- setSaveTime(ret.data.update_time ?? Date.now());
+ if (
+ !shouldAutosaveCanvas({
+ chatDrawerVisible,
+ agentId: id,
+ agentLoaded: Boolean(flowDetail?.id),
+ nodeCount: nodes.length,
+ edgeCount: edges.length,
+ })
+ ) {
+ return;
+ }
+ const ret = await saveGraph();
+ if (ret?.data?.update_time) {
+ setSaveTime(ret.data.update_time);
}
- }, [chatDrawerVisible, saveGraph, setSaveTime]);
+ }, [
+ chatDrawerVisible,
+ edges.length,
+ flowDetail?.id,
+ id,
+ nodes.length,
+ saveGraph,
+ setSaveTime,
+ ]);
useDebounceEffect(
() => {