RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(agent): prevent duplicate tools on one Agent node at creation (#19053)
euvrec81fd41ccd8d
diff --git a/web/src/pages/agent/form/agent-form/tool-popover/tool-command.tsx b/web/src/pages/agent/form/agent-form/tool-popover/tool-command.tsxindex 1624562d03..d93a215f0b 100644--- a/web/src/pages/agent/form/agent-form/tool-popover/tool-command.tsx+++ b/web/src/pages/agent/form/agent-form/tool-popover/tool-command.tsx@@ -12,7 +12,6 @@ import { Operator } from '@/pages/agent/constant';import OperatorIcon from '@/components/operator-icon';import { t } from 'i18next';import { lowerFirst } from 'lodash';-import { LucidePlus } from 'lucide-react';import { PropsWithChildren, useCallback, useEffect, useState } from 'react';import { useTranslation } from 'react-i18next';import { useGetNodeTools, useUpdateAgentNodeTools } from './use-update-tools';@@ -73,13 +72,7 @@ function ToolCommandItem({}: ToolCommandItemProps & PropsWithChildren) {return (<CommandItem className="cursor-pointer" onSelect={() => toggleOption(id)}>- {id === Operator.Retrieval ? (- <span>- <LucidePlus className="size-4" />- </span>- ) : (- <Checkbox checked={isSelected} />- )}+ <Checkbox checked={isSelected} />{children}</CommandItem>);diff --git a/web/src/pages/agent/form/agent-form/tool-popover/use-update-tools.test.tsx b/web/src/pages/agent/form/agent-form/tool-popover/use-update-tools.test.tsxnew file mode 100644index 0000000000..9e1afbd5c7--- /dev/null+++ b/web/src/pages/agent/form/agent-form/tool-popover/use-update-tools.test.tsx@@ -0,0 +1,97 @@+import { AgentFormContext } from '@/pages/agent/context';+import { Operator } from '@/pages/agent/constant';+import useGraphStore from '@/pages/agent/store';+import { act, renderHook } from '@testing-library/react';+import { useUpdateAgentNodeTools } from './use-update-tools';++// The initial-values hook pulls in the whole operator-params catalog; the+// params payload is irrelevant to duplicate-guard behavior.+jest.mock('@/pages/agent/hooks/use-agent-tool-initial-values', () => ({+ useAgentToolInitialValues: () => ({+ initializeAgentToolValues: () => ({}),+ }),+}));++type Tool = {+ component_name: string;+ name: string;+ params: object;+ id?: string;+};++const agentNode = (tools: Tool[]) =>+ ({+ id: 'agent:0',+ type: 'ragNode',+ position: { x: 0, y: 0 },+ data: { label: Operator.Agent, name: 'agent:0', form: { tools } },+ }) as any;++function seedStore(tools: Tool[]) {+ useGraphStore.setState({ nodes: [agentNode(tools)], edges: [] });+}++// Mirror the real tree: the context node always comes from the live store,+// so a toggle re-reads the tools written by the previous toggle.+function Wrapper({ children }: any) {+ const node = useGraphStore((s) => s.nodes.find((n) => n.id === 'agent:0'));+ return (+ <AgentFormContext.Provider value={node as any}>+ {children}+ </AgentFormContext.Provider>+ );+}++const currentTools = () =>+ (useGraphStore.getState().nodes[0].data.form as any).tools as Tool[];++describe('useUpdateAgentNodeTools (creation-time duplicate guard)', () => {+ beforeEach(() => seedStore([]));++ const renderTools = () =>+ renderHook(() => useUpdateAgentNodeTools(), { wrapper: Wrapper });++ it('adds a tool that is not present yet, named after the component', () => {+ const { result } = renderTools();+ act(() => result.current.updateNodeTools(Operator.Retrieval));+ expect(currentTools().map((x) => x.component_name)).toEqual([+ Operator.Retrieval,+ ]);+ expect(currentTools()[0].name).toBe(Operator.Retrieval);+ });++ it('selecting an already-added tool removes it instead of appending a duplicate', () => {+ seedStore([+ { component_name: Operator.Retrieval, name: Operator.Retrieval, params: {} },+ ]);+ const { result } = renderTools();+ act(() => result.current.updateNodeTools(Operator.Retrieval));+ expect(currentTools()).toEqual([]);+ });++ it('never holds two copies of the same tool, Retrieval included', () => {+ const { result } = renderTools();+ act(() => result.current.updateNodeTools(Operator.Retrieval));+ // A second select toggles it off — previously this appended a second+ // Retrieval tool, which later crashed the Go runtime with+ // "duplicate agent tool name \"search_my_dateset\"".+ act(() => result.current.updateNodeTools(Operator.Retrieval));+ act(() => result.current.updateNodeTools(Operator.Retrieval));+ const copies = currentTools().filter(+ (x) => x.component_name === Operator.Retrieval,+ );+ expect(copies).toHaveLength(1);+ });++ it('cleans up a legacy canvas that already carries duplicates', () => {+ seedStore([+ { component_name: Operator.Retrieval, name: 'Retrieval', params: {} },+ { component_name: Operator.Retrieval, name: 'Retrieval_0', params: {} },+ ]);+ const { result } = renderTools();+ act(() => result.current.updateNodeTools(Operator.Retrieval));+ expect(+ currentTools().filter((x) => x.component_name === Operator.Retrieval),+ ).toHaveLength(0);+ });+});diff --git a/web/src/pages/agent/form/agent-form/tool-popover/use-update-tools.ts b/web/src/pages/agent/form/agent-form/tool-popover/use-update-tools.tsindex bc91f22759..54f5278594 100644--- a/web/src/pages/agent/form/agent-form/tool-popover/use-update-tools.ts+++ b/web/src/pages/agent/form/agent-form/tool-popover/use-update-tools.ts@@ -16,8 +16,9 @@ export function useGetNodeTools() {}export function useUpdateAgentNodeTools() {- const { generateAgentToolName, generateAgentToolId, updateNodeForm } =- useGraphStore((state) => state);+ const { generateAgentToolId, updateNodeForm } = useGraphStore(+ (state) => state,+ );const node = useContext(AgentFormContext)!;const tools = useGetNodeTools();const { initializeAgentToolValues } = useAgentToolInitialValues();@@ -26,43 +27,26 @@ export function useUpdateAgentNodeTools() {(value: string) => {if (!node?.id) return;- // Append- if (value === Operator.Retrieval) {- updateNodeForm(- node.id,- [- ...tools,- {- component_name: value,- name: generateAgentToolName(node.id, value),- params: initializeAgentToolValues(value as Operator),- id: generateAgentToolId(value),- },- ],- ['tools'],- );- }- // Toggle- else {- updateNodeForm(- node.id,- tools.some((x) => x.component_name === value)- ? tools.filter((x) => x.component_name !== value)- : [- ...tools,- {- component_name: value,- name: value,- params: initializeAgentToolValues(value as Operator),- id: generateAgentToolId(value),- },- ],- ['tools'],- );- }+ // Toggle — every tool (Retrieval included) is single-instance: the+ // same tool can never be added to one Agent node twice. Selecting an+ // already-added tool removes it.+ updateNodeForm(+ node.id,+ tools.some((x) => x.component_name === value)+ ? tools.filter((x) => x.component_name !== value)+ : [+ ...tools,+ {+ component_name: value,+ name: value,+ params: initializeAgentToolValues(value as Operator),+ id: generateAgentToolId(value),+ },+ ],+ ['tools'],+ );},[- generateAgentToolName,generateAgentToolId,initializeAgentToolValues,node?.id,diff --git a/web/src/pages/agent/store.ts b/web/src/pages/agent/store.tsindex 71ccdd74b0..5297fb9f15 100644--- a/web/src/pages/agent/store.ts+++ b/web/src/pages/agent/store.ts@@ -194,7 +194,6 @@ export type RFState = {getParentIdById: (id?: string | null) => string | undefined;updateNodeName: (id: string, name: string) => void;generateNodeName: (name: string) => string;- generateAgentToolName: (id: string, name: string) => string;generateAgentToolId: (prefix: string) => string;getAllAgentTools: () => IAgentTool[];getAgentToolById: GetAgentToolByIdFunc;@@ -641,28 +640,6 @@ const useGraphStore = create<RFState>()(return generateNodeNamesWithIncreasingIndex(name, nodes);},- generateAgentToolName: (id: string, name: string) => {- const node = get().nodes.find((x) => x.id === id) as RAGFlowNodeType;-- if (!node) {- return '';- }-- const tools = (node.data.form!.tools as any[]).filter(- (x) => x.component_name === name,- );- const lastIndex = tools.length- ? (tools- .map((x) => {- const idx = x.name.match(/(\d+)$/)?.[1];- return idx && isNaN(idx) ? -1 : Number(idx);- })- .sort((a, b) => a - b)- .at(-1) ?? -1)- : -1;-- return `${name}_${lastIndex + 1}`;- },generateAgentToolId: (prefix: string) => {const allAgentToolIds = get().getAllAgentTools()