Skip to content

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

Fix: Canvas variables are not displaying in the agent's version history. (#21675)

balibabu4ff865cc5bcb
diff --git a/web/src/pages/agent/context.ts b/web/src/pages/agent/context.ts
index b3241dc7d3..b8ee308ad6 100644
--- a/web/src/pages/agent/context.ts
+++ b/web/src/pages/agent/context.ts
@@ -1,5 +1,5 @@
import { INodeEvent } from '@/hooks/use-send-message';
-import { RAGFlowNodeType } from '@/interfaces/database/agent';
+import { DSL, IGraph, RAGFlowNodeType } from '@/interfaces/database/agent';
import { IMessage } from '@/interfaces/database/chat';
import { HandleType, Position } from '@xyflow/react';
import { Dispatch, SetStateAction, createContext, useContext } from 'react';
@@ -27,6 +27,18 @@ export const CanvasReadonlyContext = createContext(false);
// oxlint-disable-next-line react-refresh/only-export-components
export const useCanvasReadonly = () => useContext(CanvasReadonlyContext);
+// CanvasSnapshotContext carries one immutable canvas snapshot (graph plus
+// globals/conversation variables) for read-only previews such as the version
+// history dialog. Variable-resolution hooks prefer it over the live canvas
+// store so a previewed version resolves variables against its own graph
+// instead of the current draft.
+export type CanvasSnapshot = IGraph & Pick<DSL, 'globals' | 'variables'>;
+
+export const CanvasSnapshotContext = createContext<CanvasSnapshot | null>(null);
+
+// oxlint-disable-next-line react-refresh/only-export-components
+export const useCanvasSnapshot = () => useContext(CanvasSnapshotContext);
+
type AgentInstanceContextType = Pick<
ReturnType<typeof useAddNode>,
'addCanvasNode'
diff --git a/web/src/pages/agent/hooks/use-build-structured-output.ts b/web/src/pages/agent/hooks/use-build-structured-output.ts
index c9d34bd43d..a8099c4c4d 100644
--- a/web/src/pages/agent/hooks/use-build-structured-output.ts
+++ b/web/src/pages/agent/hooks/use-build-structured-output.ts
@@ -6,7 +6,10 @@ import {
JsonSchemaDataType,
Operator,
} from '../constant';
-import useGraphStore from '../store';
+import {
+ useSnapshotAwareGetNode,
+ useSnapshotAwareGetOperatorTypeFromId,
+} from './use-snapshot-graph';
function splitValue(value?: string) {
return typeof value === 'string' ? value?.split('@') : [];
@@ -16,7 +19,7 @@ function getNodeId(value: string) {
}
export function useShowSecondaryMenu() {
- const { getOperatorTypeFromId } = useGraphStore((state) => state);
+ const getOperatorTypeFromId = useSnapshotAwareGetOperatorTypeFromId();
const showSecondaryMenu = useCallback(
(value: string, outputLabel: string) => {
@@ -33,7 +36,7 @@ export function useShowSecondaryMenu() {
}
export function useGetStructuredOutputByValue() {
- const { getNode } = useGraphStore((state) => state);
+ const getNode = useSnapshotAwareGetNode();
const getStructuredOutput = useCallback(
(value: string) => {
@@ -52,9 +55,7 @@ export function useGetStructuredOutputByValue() {
}
export function useFindAgentStructuredOutputLabel() {
- const getOperatorTypeFromId = useGraphStore(
- (state) => state.getOperatorTypeFromId,
- );
+ const getOperatorTypeFromId = useSnapshotAwareGetOperatorTypeFromId();
const findAgentStructuredOutputLabel = useCallback(
(
@@ -92,7 +93,7 @@ export function useFindAgentStructuredOutputLabel() {
}
export function useFindAgentStructuredOutputTypeByValue() {
- const { getOperatorTypeFromId } = useGraphStore((state) => state);
+ const getOperatorTypeFromId = useSnapshotAwareGetOperatorTypeFromId();
const filterStructuredOutput = useGetStructuredOutputByValue();
const findTypeByValue = useCallback(
@@ -159,7 +160,7 @@ export function useFindAgentStructuredOutputTypeByValue() {
}
export function useFindAgentStructuredOutputLabelByValue() {
- const { getNode } = useGraphStore((state) => state);
+ const getNode = useSnapshotAwareGetNode();
const findAgentStructuredOutputLabel = useCallback(
(value?: string) => {
diff --git a/web/src/pages/agent/hooks/use-get-begin-query.tsx b/web/src/pages/agent/hooks/use-get-begin-query.tsx
index f64158b611..17f3e693b4 100644
--- a/web/src/pages/agent/hooks/use-get-begin-query.tsx
+++ b/web/src/pages/agent/hooks/use-get-begin-query.tsx
@@ -23,7 +23,7 @@ import {
Operator,
VariableType,
} from '../constant';
-import { AgentFormContext } from '../context';
+import { AgentFormContext, useCanvasSnapshot } from '../context';
import { buildBeginInputListFromObject } from '../form/begin-form/utils';
import { BeginQuery } from '../interface';
import OperatorIcon from '@/components/operator-icon';
@@ -32,9 +32,15 @@ import {
useFindAgentStructuredOutputLabelByValue,
useFindAgentStructuredOutputTypeByValue,
} from './use-build-structured-output';
+import {
+ useSnapshotAwareEdges,
+ useSnapshotAwareGetNode,
+ useSnapshotAwareGetOperatorTypeFromId,
+ useSnapshotAwareNodes,
+} from './use-snapshot-graph';
export function useSelectBeginNodeDataInputs() {
- const getNode = useGraphStore((state) => state.getNode);
+ const getNode = useSnapshotAwareGetNode();
return buildBeginInputListFromObject(
getNode(BeginId)?.data?.form?.inputs ?? {},
@@ -93,8 +99,8 @@ export const useGetBeginNodeDataQueryIsSafe = () => {
};
export function useBuildUpstreamNodeOutputOptions(nodeId?: string) {
- const nodes = useGraphStore((state) => state.nodes);
- const edges = useGraphStore((state) => state.edges);
+ const nodes = useSnapshotAwareNodes();
+ const edges = useSnapshotAwareEdges();
return useMemo(() => {
return buildUpstreamNodeOutputOptions({
@@ -106,7 +112,8 @@ export function useBuildUpstreamNodeOutputOptions(nodeId?: string) {
}
export function useBuildParentOutputOptions(parentId?: string) {
- const { getNode, getOperatorTypeFromId } = useGraphStore((state) => state);
+ const getNode = useSnapshotAwareGetNode();
+ const getOperatorTypeFromId = useSnapshotAwareGetOperatorTypeFromId();
const parentNode = getNode(parentId);
const parentType = getOperatorTypeFromId(parentId);
@@ -206,9 +213,10 @@ function filterDocGeneratorDownloadOutputOptions(
}
export function useBuildGlobalWithBeginVariableOptions() {
+ const snapshot = useCanvasSnapshot();
const { data } = useFetchAgent();
const dynamicBeginOptions = useBuildBeginDynamicVariableOptions();
- const globals = data?.dsl?.globals ?? {};
+ const globals = snapshot?.globals ?? data?.dsl?.globals ?? {};
const globalOptions = Object.entries(globals)
.filter(([key]) => !key.startsWith(Env))
.map(([key, value]) => ({
@@ -238,11 +246,12 @@ export function useBuildGlobalWithBeginVariableOptions() {
}
export function useBuildConversationVariableOptions() {
+ const snapshot = useCanvasSnapshot();
const { data } = useFetchAgent();
const conversationVariables = useMemo(
- () => data?.dsl?.variables ?? {},
- [data?.dsl?.variables],
+ () => snapshot?.variables ?? data?.dsl?.variables ?? {},
+ [snapshot, data?.dsl?.variables],
);
const options = useMemo(() => {
@@ -301,10 +310,8 @@ export function useBuildQueryVariableOptions({
n?: RAGFlowNodeType;
} & BuildQueryVariableOptions = {}) {
const node = useContext(AgentFormContext) || n;
- const nodes = useGraphStore((state) => state.nodes);
- const getOperatorTypeFromId = useGraphStore(
- (state) => state.getOperatorTypeFromId,
- );
+ const nodes = useSnapshotAwareNodes();
+ const getOperatorTypeFromId = useSnapshotAwareGetOperatorTypeFromId();
const options = useBuildVariableOptions(node?.id, node?.parentId);
@@ -394,7 +401,7 @@ export function useFilterQueryVariableOptionsByTypes({
}
export function useBuildComponentIdOptions(nodeId?: string, parentId?: string) {
- const nodes = useGraphStore((state) => state.nodes);
+ const nodes = useSnapshotAwareNodes();
// Limit the nodes inside iteration to only reference peer nodes with the same parentId and other external nodes other than their parent nodes
const filterChildNodesToSameParentOrExternal = useCallback(
@@ -473,7 +480,7 @@ export function useFlattenQueryVariableOptions({
}: {
nodeId?: string;
} & BuildQueryVariableOptions = {}) {
- const { getNode } = useGraphStore((state) => state);
+ const getNode = useSnapshotAwareGetNode();
const nextOptions = useBuildQueryVariableOptions({
n: getNode(nodeId),
nodeIds,
diff --git a/web/src/pages/agent/hooks/use-snapshot-graph.test.tsx b/web/src/pages/agent/hooks/use-snapshot-graph.test.tsx
new file mode 100644
index 0000000000..8097763c9e
--- /dev/null
+++ b/web/src/pages/agent/hooks/use-snapshot-graph.test.tsx
@@ -0,0 +1,103 @@
+import { RAGFlowNodeType } from '@/interfaces/database/agent';
+import { renderHook } from '@testing-library/react';
+import { Edge } from '@xyflow/react';
+import { PropsWithChildren } from 'react';
+import { Operator } from '../constant';
+import { CanvasSnapshot, CanvasSnapshotContext } from '../context';
+import useGraphStore from '../store';
+import { useBuildUpstreamNodeOutputOptions } from './use-get-begin-query';
+import { useSnapshotAwareGetNode } from './use-snapshot-graph';
+
+// Regression: the version history preview reuses live canvas node components,
+// whose variable resolution used to read the global canvas store (the current
+// draft). Deleting a node from the draft then broke variable labels of every
+// published version still referencing it. CanvasSnapshotContext redirects the
+// resolution to the previewed version's own graph.
+
+const AgentNodeId = 'Agent:HappyEyesJuggle';
+const MessageNodeId = 'Message:CuriousPanda';
+
+function makeNode(
+ id: string,
+ label: Operator,
+ name: string,
+ outputs: Record<string, any> = {},
+): RAGFlowNodeType {
+ return {
+ id,
+ position: { x: 0, y: 0 },
+ data: { label, name, form: { outputs } },
+ } as RAGFlowNodeType;
+}
+
+const AgentNode = makeNode(AgentNodeId, Operator.Agent, 'glm-4_jin', {
+ text: { type: 'string' },
+});
+const MessageNode = makeNode(MessageNodeId, Operator.Message, 'reply');
+
+const SnapshotEdge: Edge = {
+ id: 'edge-agent-message',
+ source: AgentNodeId,
+ target: MessageNodeId,
+};
+
+// The version being previewed: Begin -> Agent -> Message.
+const PreviewSnapshot: CanvasSnapshot = {
+ nodes: [AgentNode, MessageNode],
+ edges: [SnapshotEdge],
+ globals: {},
+ variables: {},
+};
+
+function renderWithSnapshot<T>(useHook: () => T) {
+ function Wrapper({ children }: PropsWithChildren) {
+ return (
+ <CanvasSnapshotContext.Provider value={PreviewSnapshot}>
+ {children}
+ </CanvasSnapshotContext.Provider>
+ );
+ }
+ return renderHook(useHook, { wrapper: Wrapper });
+}
+
+beforeEach(() => {
+ // The current draft: the Agent node was deleted, only Message remains.
+ useGraphStore.setState({ nodes: [MessageNode], edges: [] });
+});
+
+describe('useSnapshotAwareGetNode', () => {
+ it('resolves nodes from the live canvas store without a snapshot', () => {
+ const { result } = renderHook(() => useSnapshotAwareGetNode());
+
+ expect(result.current(MessageNodeId)?.id).toBe(MessageNodeId);
+ expect(result.current(AgentNodeId)).toBeUndefined();
+ });
+
+ it('resolves nodes from the snapshot when one is provided', () => {
+ const { result } = renderWithSnapshot(() => useSnapshotAwareGetNode());
+
+ // The draft has no Agent node; the snapshot does.
+ expect(result.current(AgentNodeId)?.data.name).toBe('glm-4_jin');
+ });
+});
+
+describe('useBuildUpstreamNodeOutputOptions', () => {
+ it('finds no upstream outputs in the current draft', () => {
+ const { result } = renderHook(() =>
+ useBuildUpstreamNodeOutputOptions(MessageNodeId),
+ );
+
+ expect(result.current).toEqual([]);
+ });
+
+ it('resolves upstream outputs from the snapshot graph', () => {
+ const { result } = renderWithSnapshot(() =>
+ useBuildUpstreamNodeOutputOptions(MessageNodeId),
+ );
+
+ const values = result.current.flatMap((group) =>
+ group.options.map((option) => option.value),
+ );
+ expect(values).toContain(`${AgentNodeId}@text`);
+ });
+});
diff --git a/web/src/pages/agent/hooks/use-snapshot-graph.ts b/web/src/pages/agent/hooks/use-snapshot-graph.ts
new file mode 100644
index 0000000000..5099389342
--- /dev/null
+++ b/web/src/pages/agent/hooks/use-snapshot-graph.ts
@@ -0,0 +1,44 @@
+import { RAGFlowNodeType } from '@/interfaces/database/agent';
+import { Edge } from '@xyflow/react';
+import { useCallback } from 'react';
+import { useCanvasSnapshot } from '../context';
+import useGraphStore from '../store';
+
+// Snapshot-aware graph accessors. Inside a read-only snapshot preview (e.g.
+// the version history dialog) they resolve nodes/edges from the provided
+// snapshot instead of the live canvas store, which always holds the current
+// draft; outside a preview they fall back to the store unchanged.
+
+export function useSnapshotAwareNodes(): RAGFlowNodeType[] {
+ const snapshot = useCanvasSnapshot();
+ const storeNodes = useGraphStore((state) => state.nodes);
+
+ return snapshot?.nodes ?? storeNodes;
+}
+
+export function useSnapshotAwareEdges(): Edge[] {
+ const snapshot = useCanvasSnapshot();
+ const storeEdges = useGraphStore((state) => state.edges);
+
+ return snapshot?.edges ?? storeEdges;
+}
+
+export function useSnapshotAwareGetNode() {
+ const snapshot = useCanvasSnapshot();
+ const getNode = useGraphStore((state) => state.getNode);
+
+ return useCallback(
+ (id?: string | null) =>
+ snapshot ? snapshot.nodes.find((x) => x.id === id) : getNode(id),
+ [snapshot, getNode],
+ );
+}
+
+export function useSnapshotAwareGetOperatorTypeFromId() {
+ const getNode = useSnapshotAwareGetNode();
+
+ return useCallback(
+ (id?: string | null) => getNode(id)?.data?.label,
+ [getNode],
+ );
+}
diff --git a/web/src/pages/agent/version-dialog/index.tsx b/web/src/pages/agent/version-dialog/index.tsx
index bd602bc636..0cdcee6a28 100644
--- a/web/src/pages/agent/version-dialog/index.tsx
+++ b/web/src/pages/agent/version-dialog/index.tsx
@@ -56,6 +56,7 @@ import { useParams } from 'react-router';
import { nodeTypes } from '../canvas';
import { useIsPipeline } from '../hooks/use-is-pipeline';
import { AliasFormDialog } from '../components/alias-form-dialog';
+import { CanvasSnapshot, CanvasSnapshotContext } from '../context';
import { downloadDsl } from '../utils/download-dsl';
type PendingAction =
@@ -108,6 +109,22 @@ export function VersionDialog({
[released, selectedId],
);
+ // Snapshot handed to the preview so variable labels inside node components
+ // resolve against the version's own graph instead of the live canvas store
+ // (which holds the current draft).
+ const previewSnapshot = useMemo<CanvasSnapshot | null>(() => {
+ const dsl = agent?.dsl;
+ if (!dsl?.graph?.nodes) {
+ return null;
+ }
+ return {
+ nodes: dsl.graph.nodes,
+ edges: dsl.graph.edges ?? [],
+ globals: dsl.globals ?? {},
+ variables: dsl.variables ?? {},
+ };
+ }, [agent?.dsl]);
+
// Filtering shrinks the list, so jump back to page 1; otherwise a stale
// page 2+ selection would render an empty list with matches out of view.
const handleAliasFilterChange = useCallback(
@@ -344,30 +361,36 @@ export function VersionDialog({
</section>
<section className="relative flex-1">
<ReactFlowProvider key={'flow-' + selectedId}>
- <ReactFlow
- connectionMode={ConnectionMode.Loose}
- nodes={agent?.dsl?.graph?.nodes || []}
- edges={
- agent?.dsl?.graph?.edges?.flatMap((x) => ({
- ...x,
- type: 'default',
- })) || []
- }
- fitView
- nodeTypes={nodeTypes}
- edgeTypes={{}}
- zoomOnScroll={true}
- panOnDrag={true}
- zoomOnDoubleClick={false}
- preventScrolling={true}
- minZoom={0.1}
- nodesDraggable={false}
- nodesConnectable={false}
- elementsSelectable={false}
- >
- <AgentBackground></AgentBackground>
- <Spotlight className="z-0" opcity={0.7} coverage={70} />
- </ReactFlow>
+ <CanvasSnapshotContext.Provider value={previewSnapshot}>
+ <ReactFlow
+ connectionMode={ConnectionMode.Loose}
+ nodes={agent?.dsl?.graph?.nodes || []}
+ edges={
+ agent?.dsl?.graph?.edges?.flatMap((x) => ({
+ ...x,
+ type: 'default',
+ })) || []
+ }
+ fitView
+ nodeTypes={nodeTypes}
+ edgeTypes={{}}
+ zoomOnScroll={true}
+ panOnDrag={true}
+ zoomOnDoubleClick={false}
+ preventScrolling={true}
+ minZoom={0.1}
+ nodesDraggable={false}
+ nodesConnectable={false}
+ elementsSelectable={false}
+ >
+ <AgentBackground></AgentBackground>
+ <Spotlight
+ className="z-0"
+ opcity={0.7}
+ coverage={70}
+ />
+ </ReactFlow>
+ </CanvasSnapshotContext.Provider>
</ReactFlowProvider>
</section>
</CardContent>