Skip to content

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

fix(agent): filter dragged next-step menu by source and skip empty menu (#20049)

chanx8547207b41be
diff --git a/web/src/pages/agent/canvas/index.tsx b/web/src/pages/agent/canvas/index.tsx
index 562a6c6d11..2fa0de7c8b 100644
--- a/web/src/pages/agent/canvas/index.tsx
+++ b/web/src/pages/agent/canvas/index.tsx
@@ -1,4 +1,5 @@
import { useTheme } from '@/components/theme-provider';
+import { useIsGoBackend } from '@/utils/backend-variant';
import {
Tooltip,
TooltipContent,
@@ -28,12 +29,18 @@ import {
} from '../context';
import FormSheet from '../form-sheet/next';
+import { useIsPipeline } from '../hooks/use-is-pipeline';
+import {
+ hasPipelineNextOperators,
+} from '../utils/pipeline-connection';
import { useSelectCanvasData, useValidateConnection } from '../hooks';
import { useAddNode } from '../hooks/use-add-node';
import { useBeforeDelete } from '../hooks/use-before-delete';
import { useCacheChatLog } from '../hooks/use-cache-chat-log';
import { useConnectionDrag } from '../hooks/use-connection-drag';
+import { Operator } from '../constant';
import { useDropdownPosition } from '../hooks/use-dropdown-position';
+import useGraphStore from '../store';
import { useMoveNote } from '../hooks/use-move-note';
import { usePlaceholderManager } from '../hooks/use-placeholder-manager';
import { useDropdownManager } from './context';
@@ -229,6 +236,29 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) {
const { calculateDropdownPosition } = useDropdownPosition(reactFlowInstance);
+ const isGoBackend = useIsGoBackend();
+ const isPipeline = useIsPipeline();
+ const { findNodeByName, getOperatorTypeFromId } = useGraphStore(
+ (state) => state,
+ );
+
+ // Whether the "next step" menu offers anything for the drag source. On a
+ // pipeline, single-instance operators already on canvas plus the Go
+ // topology rules can leave the menu empty; such a drag is aborted.
+ const hasNextStepOperators = useCallback(
+ (sourceNodeId: string) => {
+ if (!isPipeline) {
+ return true;
+ }
+ return hasPipelineNextOperators(
+ getOperatorTypeFromId(sourceNodeId) as Operator | undefined,
+ isGoBackend,
+ (operator) => !!findNodeByName(operator),
+ );
+ },
+ [findNodeByName, getOperatorTypeFromId, isGoBackend, isPipeline],
+ );
+
const {
onConnectStart,
onConnectEnd,
@@ -248,6 +278,7 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) {
clearActiveDropdown,
checkAndRemoveExistingPlaceholder,
reactFlowInstance,
+ hasNextStepOperators,
);
const onPaneClick = useCallback(() => {
diff --git a/web/src/pages/agent/canvas/node/dropdown/accordion-operators.tsx b/web/src/pages/agent/canvas/node/dropdown/accordion-operators.tsx
index 4660bbb4f4..3a760d49a7 100644
--- a/web/src/pages/agent/canvas/node/dropdown/accordion-operators.tsx
+++ b/web/src/pages/agent/canvas/node/dropdown/accordion-operators.tsx
@@ -6,11 +6,10 @@ import {
} from '@/components/ui/accordion';
import { Operator } from '@/constants/agent';
import { useIsGoBackend } from '@/utils/backend-variant';
-import { PropsWithChildren, useCallback, useMemo } from 'react';
+import { PropsWithChildren, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
-import { ChunkerOperators } from '../../../constant/pipeline';
import useGraphStore from '../../../store';
-import { isChunkerOperator } from '../../../utils/pipeline-connection';
+import { buildPipelineNextOperators } from '../../../utils/pipeline-connection';
import { OperatorItemList } from './operator-item-list';
function OperatorAccordionTrigger({ children }: PropsWithChildren) {
@@ -143,26 +142,6 @@ export function AccordionOperators({
);
}
-// Limit the number of operators of a certain type on the canvas to only one
-function useRestrictSingleOperatorOnCanvas() {
- const { findNodeByName } = useGraphStore((state) => state);
-
- const restrictSingleOperatorOnCanvas = useCallback(
- (singleOperators: Operator[]) => {
- const list: Operator[] = [];
- singleOperators.forEach((operator) => {
- if (!findNodeByName(operator)) {
- list.push(operator);
- }
- });
- return list;
- },
- [findNodeByName],
- );
-
- return restrictSingleOperatorOnCanvas;
-}
-
export function PipelineAccordionOperators({
isCustomDropdown = false,
mousePosition,
@@ -172,43 +151,23 @@ export function PipelineAccordionOperators({
mousePosition?: { x: number; y: number };
nodeId?: string;
}) {
- const restrictSingleOperatorOnCanvas = useRestrictSingleOperatorOnCanvas();
- const { getOperatorTypeFromId } = useGraphStore((state) => state);
+ const { findNodeByName, getOperatorTypeFromId } = useGraphStore(
+ (state) => state,
+ );
const isGoBackend = useIsGoBackend();
- const sourceOperator = getOperatorTypeFromId(nodeId);
+ const sourceOperator = getOperatorTypeFromId(nodeId) as
+ | Operator
+ | undefined;
- // Go pipelines require a Parser to feed a chunker, so from a Parser node
- // the menu offers only the chunker group.
- const parserMustFeedChunker =
- isGoBackend && sourceOperator === Operator.Parser;
-
- const operators = useMemo(() => {
- if (parserMustFeedChunker) {
- return [];
- }
- const list = [
- ...restrictSingleOperatorOnCanvas([Operator.Parser, Operator.Tokenizer]),
- ];
- list.push(Operator.Extractor);
- if (sourceOperator !== Operator.Compiler) {
- list.push(Operator.Compiler);
- }
- return list;
- }, [parserMustFeedChunker, sourceOperator, restrictSingleOperatorOnCanvas]);
-
- const chunkerOperators = useMemo(() => {
- return [...restrictSingleOperatorOnCanvas(ChunkerOperators)];
- }, [restrictSingleOperatorOnCanvas]);
-
- const showChunker = useMemo(() => {
- // Go pipelines forbid chunker -> chunker, mirroring the existing rule
- // that Extractor/Compiler never offer the chunker group.
- const sourceExcluded =
- sourceOperator === Operator.Extractor ||
- sourceOperator === Operator.Compiler ||
- (isGoBackend && isChunkerOperator(sourceOperator as Operator));
- return !sourceExcluded && chunkerOperators.length > 0;
- }, [chunkerOperators.length, isGoBackend, sourceOperator]);
+ const { operators, chunkerOperators, showChunker } = useMemo(
+ () =>
+ buildPipelineNextOperators(
+ sourceOperator,
+ isGoBackend,
+ (operator) => !!findNodeByName(operator),
+ ),
+ [findNodeByName, isGoBackend, sourceOperator],
+ );
return (
<>
diff --git a/web/src/pages/agent/hooks/use-connection-drag.ts b/web/src/pages/agent/hooks/use-connection-drag.ts
index 02ca46faad..7e494397a0 100644
--- a/web/src/pages/agent/hooks/use-connection-drag.ts
+++ b/web/src/pages/agent/hooks/use-connection-drag.ts
@@ -33,11 +33,19 @@ export const useConnectionDrag = (
clearActiveDropdown: () => void,
checkAndRemoveExistingPlaceholder: () => void,
reactFlowInstance?: ReactFlowInstance<any, any>,
+ // Returns whether the "next step" menu offers at least one operator for the
+ // drag source. When it does not, the drag ends without a placeholder node or
+ // empty dropdown.
+ hasNextStepOperators?: (sourceNodeId: string) => boolean,
) => {
// Reference for whether connection is established
const isConnectedRef = useRef(false);
// Reference for connection start parameters
const connectionStartRef = useRef<ConnectionStartParams | null>(null);
+ // Source node of the connection drag whose dropdown is open. Kept separate
+ // from connectionStartRef because that ref is reset in onConnectEnd before
+ // the menu renders and needs the source to filter its operator list.
+ const dragSourceNodeIdRef = useRef<string>();
// Reference to prevent immediate close
const preventCloseRef = useRef(false);
// Reference to track mouse position for click detection
@@ -62,6 +70,7 @@ export const useConnectionDrag = (
nodeId: params.nodeId,
handleId: params.handleId,
};
+ dragSourceNodeIdRef.current = params.nodeId;
} else {
connectionStartRef.current = null;
}
@@ -99,10 +108,20 @@ export const useConnectionDrag = (
// Check and remove existing placeholder-node before creating new one
checkAndRemoveExistingPlaceholder();
+ const sourceNodeId = connectionStartRef.current.nodeId;
+
+ // Nothing to offer from this source: end the drag without a
+ // placeholder node or an empty dropdown.
+ if (hasNextStepOperators && !hasNextStepOperators(sourceNodeId)) {
+ connectionStartRef.current = null;
+ mouseStartPosRef.current = null;
+ return;
+ }
+
// Create placeholder node and establish connection
const mockEvent = { clientX, clientY };
const contextData = {
- nodeId: connectionStartRef.current.nodeId,
+ nodeId: sourceNodeId,
id: connectionStartRef.current.handleId,
type: 'source' as const,
position: Position.Right,
@@ -157,6 +176,7 @@ export const useConnectionDrag = (
calculateDropdownPosition,
setActiveDropdown,
showModal,
+ hasNextStepOperators,
],
);
@@ -207,7 +227,7 @@ export const useConnectionDrag = (
}, [removePlaceholderNode, hideModal, clearActiveDropdown]);
return {
- nodeId: connectionStartRef.current?.nodeId,
+ nodeId: dragSourceNodeIdRef.current,
onConnectStart,
onConnectEnd,
handleConnect,
diff --git a/web/src/pages/agent/utils/pipeline-connection.ts b/web/src/pages/agent/utils/pipeline-connection.ts
index 0de4c838c5..07b3beab0c 100644
--- a/web/src/pages/agent/utils/pipeline-connection.ts
+++ b/web/src/pages/agent/utils/pipeline-connection.ts
@@ -5,6 +5,77 @@ export function isChunkerOperator(operator: Operator): boolean {
return (ChunkerOperators as Operator[]).includes(operator);
}
+export interface PipelineNextOperators {
+ // Operators offered in the flat top-level list (Parser, Tokenizer,
+ // Extractor, Compiler), minus the single-instance ones already on canvas.
+ operators: Operator[];
+ // Single-instance chunkers not yet on canvas, shown in the Chunker group.
+ chunkerOperators: Operator[];
+ showChunker: boolean;
+}
+
+/**
+ * Build the "next step" operator menu for a pipeline source node. Mirrors the
+ * Go topology rules from `isValidGoPipelineConnection`: on the Go backend a
+ * Parser offers only the chunker group, and Extractor/Compiler/chunker sources
+ * never offer the chunker group. Single-instance operators are filtered out
+ * through `hasOperator`. Pure function so both the menu component and the
+ * connection-drag gate can reuse it.
+ */
+export function buildPipelineNextOperators(
+ source: Operator | undefined,
+ isGoBackend: boolean,
+ hasOperator: (operator: Operator) => boolean,
+): PipelineNextOperators {
+ const operators: Operator[] = [];
+
+ // Go pipelines require a Parser to feed a chunker, so from a Parser node
+ // the menu offers only the chunker group.
+ if (!(isGoBackend && source === Operator.Parser)) {
+ [Operator.Parser, Operator.Tokenizer].forEach((operator) => {
+ if (!hasOperator(operator)) {
+ operators.push(operator);
+ }
+ });
+ operators.push(Operator.Extractor);
+ if (source !== Operator.Compiler) {
+ operators.push(Operator.Compiler);
+ }
+ }
+
+ const chunkerOperators = ChunkerOperators.filter(
+ (operator) => !hasOperator(operator),
+ );
+
+ // Go pipelines forbid chunker -> chunker, mirroring the existing rule
+ // that Extractor/Compiler never offer the chunker group.
+ const sourceExcluded =
+ source === Operator.Extractor ||
+ source === Operator.Compiler ||
+ (isGoBackend && !!source && isChunkerOperator(source));
+
+ return {
+ operators,
+ chunkerOperators,
+ showChunker: !sourceExcluded && chunkerOperators.length > 0,
+ };
+}
+
+/**
+ * Whether the "next step" menu for a source node contains at least one
+ * selectable operator. Used by the connection-drag flow to avoid showing an
+ * empty dropdown attached to a placeholder node.
+ */
+export function hasPipelineNextOperators(
+ source: Operator | undefined,
+ isGoBackend: boolean,
+ hasOperator: (operator: Operator) => boolean,
+): boolean {
+ const { operators, chunkerOperators, showChunker } =
+ buildPipelineNextOperators(source, isGoBackend, hasOperator);
+ return operators.length > 0 || (showChunker && chunkerOperators.length > 0);
+}
+
/**
* Go-backend pipeline topology rules, enforced when a connection is created:
* a Parser may only feed a chunker, and a chunker may not feed another