Skip to content

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

Fix: check retrieval node required fields (#20638)

Lynna6e67ee8a5c0
diff --git a/web/src/pages/agent/constant/index.tsx b/web/src/pages/agent/constant/index.tsx
index e456ceaf62..e22cf5fc14 100644
--- a/web/src/pages/agent/constant/index.tsx
+++ b/web/src/pages/agent/constant/index.tsx
@@ -91,6 +91,10 @@ export const initialRetrievalValues = {
top_n: 8,
rerank_candidates_count: 64,
dataset_ids: [],
+ // Both bindings default to an empty array rather than being absent: the
+ // checklist treats a missing key as unbound, and without a default an
+ // unbound node would never be written back with the key.
+ memory_ids: [],
rerank_id: '',
...initialSimilarityThresholdValue,
...initialKeywordsSimilarityWeightValue,
diff --git a/web/src/pages/agent/form/retrieval-form/next.tsx b/web/src/pages/agent/form/retrieval-form/next.tsx
index 43d3101007..dc2440191c 100644
--- a/web/src/pages/agent/form/retrieval-form/next.tsx
+++ b/web/src/pages/agent/form/retrieval-form/next.tsx
@@ -57,37 +57,43 @@ export const RetrievalPartialSchema = {
user_id: z.string().optional(),
};
+// A Retrieval node sourcing from datasets must name at least one dataset, and
+// one sourcing from memories must name at least one memory. The backend
+// otherwise rejects the run with a `dataset_ids`/`memory_ids is required`
+// error that only surfaces at runtime. Shared with the Agent-embedded
+// Retrieval tool form, which owns the same fields.
+export function refineRetrievalBindings(
+ data: Record<string, any>,
+ ctx: z.RefinementCtx,
+) {
+ if (
+ data.retrieval_from !== RetrievalFrom.Memory &&
+ (data.dataset_ids ?? []).length === 0
+ ) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ path: ['dataset_ids'],
+ message: t('flow.retrievalDatasetRequired'),
+ });
+ }
+ if (
+ data.retrieval_from === RetrievalFrom.Memory &&
+ (data.memory_ids ?? []).length === 0
+ ) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ path: ['memory_ids'],
+ message: t('flow.retrievalMemoryRequired'),
+ });
+ }
+}
+
export const FormSchema = z
.object({
query: z.string().optional(),
...RetrievalPartialSchema,
})
- .superRefine((data, ctx) => {
- // A Retrieval node sourcing from datasets must name at least one dataset,
- // and one sourcing from memories must name at least one memory. The
- // backend otherwise rejects the run with a `dataset_ids`/`memory_ids is
- // required` error that only surfaces at runtime.
- if (
- data.retrieval_from === RetrievalFrom.Dataset &&
- (data.dataset_ids ?? []).length === 0
- ) {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- path: ['dataset_ids'],
- message: t('flow.retrievalDatasetRequired'),
- });
- }
- if (
- data.retrieval_from === RetrievalFrom.Memory &&
- (data.memory_ids ?? []).length === 0
- ) {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- path: ['memory_ids'],
- message: t('flow.retrievalMemoryRequired'),
- });
- }
- });
+ .superRefine(refineRetrievalBindings);
export type RetrievalFormSchemaType = z.infer<typeof FormSchema>;
diff --git a/web/src/pages/agent/form/retrieval-form/use-values.ts b/web/src/pages/agent/form/retrieval-form/use-values.ts
index 7296b424c5..4803b81038 100644
--- a/web/src/pages/agent/form/retrieval-form/use-values.ts
+++ b/web/src/pages/agent/form/retrieval-form/use-values.ts
@@ -1,7 +1,7 @@
import { RAGFlowNodeType } from '@/interfaces/database/agent';
import { isEmpty, omit } from 'lodash';
import { useMemo } from 'react';
-import { initialRetrievalValues } from '../../constant';
+import { RetrievalFrom, initialRetrievalValues } from '../../constant';
export function useValues(node?: RAGFlowNodeType) {
const defaultValues = useMemo(
@@ -12,7 +12,7 @@ export function useValues(node?: RAGFlowNodeType) {
);
const values = useMemo(() => {
- const formData = node?.data?.form;
+ const formData = node?.data?.form as Record<string, any> | undefined;
if (isEmpty(formData)) {
return defaultValues;
@@ -21,12 +21,19 @@ export function useValues(node?: RAGFlowNodeType) {
// `dataset_ids` is the canonical field name today; older DSLs still
// persist the dataset list under `kb_ids`, so fold the legacy key in on
// load to keep the form and the saved DSL on a single field name.
- const legacyKbIds = (formData as Record<string, any>)?.kb_ids;
- const datasetIds =
- (formData as Record<string, any>)?.dataset_ids ?? legacyKbIds ?? [];
+ const legacyKbIds = formData?.kb_ids;
return omit(
- { ...(formData as Record<string, any>), dataset_ids: datasetIds },
+ {
+ ...formData,
+ dataset_ids: formData?.dataset_ids ?? legacyKbIds ?? [],
+ // DSLs saved before Memory support carry neither key. Without a
+ // concrete array the Memories select registers no value at all, so
+ // the node would round-trip without `memory_ids` and the canvas
+ // checklist could never tell it is unbound.
+ memory_ids: formData?.memory_ids ?? [],
+ retrieval_from: formData?.retrieval_from ?? RetrievalFrom.Dataset,
+ },
'top_k',
);
}, [defaultValues, node?.data?.form]);
diff --git a/web/src/pages/agent/form/tool-form/retrieval-form/index.tsx b/web/src/pages/agent/form/tool-form/retrieval-form/index.tsx
index a64a4db319..19df2c8843 100644
--- a/web/src/pages/agent/form/tool-form/retrieval-form/index.tsx
+++ b/web/src/pages/agent/form/tool-form/retrieval-form/index.tsx
@@ -24,15 +24,18 @@ import { FormWrapper } from '../../components/form-wrapper';
import {
MemoryDatasetForm,
RetrievalPartialSchema,
+ refineRetrievalBindings,
useHideKnowledgeGraphField,
} from '../../retrieval-form/next';
import { useValues } from '../use-values';
import { useWatchFormChange } from '../use-watch-change';
-export const FormSchema = z.object({
- ...RetrievalPartialSchema,
- description: z.string().optional(),
-});
+export const FormSchema = z
+ .object({
+ ...RetrievalPartialSchema,
+ description: z.string().optional(),
+ })
+ .superRefine(refineRetrievalBindings);
const RetrievalForm = () => {
const defaultValues = omit(useValues(), 'top_k');
diff --git a/web/src/pages/agent/form/tool-form/use-values.ts b/web/src/pages/agent/form/tool-form/use-values.ts
index 6cab20d386..fd994f3129 100644
--- a/web/src/pages/agent/form/tool-form/use-values.ts
+++ b/web/src/pages/agent/form/tool-form/use-values.ts
@@ -1,6 +1,6 @@
import { isEmpty } from 'lodash';
import { useMemo } from 'react';
-import { Operator } from '../../constant';
+import { Operator, RetrievalFrom } from '../../constant';
import { useAgentToolInitialValues } from '../../hooks/use-agent-tool-initial-values';
import useGraphStore from '../../store';
@@ -42,14 +42,20 @@ export function useValues() {
// field so the edited tool persists `dataset_ids` and save/run
// validation can see the binding.
const legacyDatasetIds = Array.isArray(formData?.dataset_ids)
- ? undefined
+ ? formData.dataset_ids
: Array.isArray(formData?.kb_ids)
? formData.kb_ids
: undefined;
- return legacyDatasetIds
- ? { ...formData, dataset_ids: legacyDatasetIds }
- : { ...formData };
+ // Both bindings stay concrete arrays and `retrieval_from` always resolves,
+ // so an unbound tool is written back with the keys the canvas checklist
+ // and the form schema inspect instead of dropping them.
+ return {
+ ...formData,
+ dataset_ids: legacyDatasetIds ?? [],
+ memory_ids: formData?.memory_ids ?? [],
+ retrieval_from: formData?.retrieval_from ?? RetrievalFrom.Dataset,
+ };
}, [
clickedNodeId,
clickedToolId,
diff --git a/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts b/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts
index fe3f886608..bcf9ecdc35 100644
--- a/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts
+++ b/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts
@@ -553,6 +553,52 @@ describe('collectCanvasIssues: missing required fields', () => {
);
});
+ // Switching the source to memories writes no `memory_ids` key until a
+ // memory is picked, so the unbound node round-trips without it.
+ it('flags a Retrieval node whose binding key is absent', () => {
+ const datasetIssues = collect({
+ nodes: [
+ makeNode('Retrieval:r1', 'Retrieval', { retrieval_from: 'dataset' }),
+ ],
+ });
+ const memoryIssues = collect({
+ nodes: [
+ makeNode('Retrieval:r2', 'Retrieval', { retrieval_from: 'memory' }),
+ ],
+ });
+
+ expect(datasetIssues).toContainEqual(
+ expect.objectContaining({ messageKey: 'flow.retrievalDatasetMissing' }),
+ );
+ expect(memoryIssues).toContainEqual(
+ expect.objectContaining({ messageKey: 'flow.retrievalMemoryMissing' }),
+ );
+ });
+
+ it('treats a Retrieval node without retrieval_from as a dataset node', () => {
+ const emptyIssues = collect({
+ nodes: [makeNode('Retrieval:r1', 'Retrieval', {})],
+ });
+ const legacyIssues = collect({
+ nodes: [makeNode('Retrieval:r2', 'Retrieval', { kb_ids: ['kb-1'] })],
+ });
+ const memoryIssues = collect({
+ nodes: [makeNode('Retrieval:r3', 'Retrieval', { memory_ids: ['mem-1'] })],
+ });
+
+ expect(emptyIssues).toContainEqual(
+ expect.objectContaining({ messageKey: 'flow.retrievalDatasetMissing' }),
+ );
+ // A legacy `kb_ids` binding counts as bound; a legacy memory binding is
+ // read as a memory source instead of an empty dataset.
+ expect(
+ legacyIssues.filter((x) => x.messageKey.startsWith('flow.retrieval')),
+ ).toHaveLength(0);
+ expect(
+ memoryIssues.filter((x) => x.messageKey.startsWith('flow.retrieval')),
+ ).toHaveLength(0);
+ });
+
it('flags an Agent node without a model', () => {
const issues = collect({
nodes: [makeNode('Agent:a1', 'Agent', { llm_id: '' })],
diff --git a/web/src/pages/agent/utils/canvas-checklist.ts b/web/src/pages/agent/utils/canvas-checklist.ts
index 8f21ea09a7..7e2ffbd7ef 100644
--- a/web/src/pages/agent/utils/canvas-checklist.ts
+++ b/web/src/pages/agent/utils/canvas-checklist.ts
@@ -292,10 +292,32 @@ function collectNodeReferenceIssues(
return issues;
}
+// An absent binding counts as empty. Switching the source to memories writes
+// no `memory_ids` key until a memory is picked, so requiring an actual array
+// here would let an unbound node through.
+const isEmptyIdList = (ids: unknown) => !Array.isArray(ids) || ids.length === 0;
+
+/**
+ * Params persisted before `retrieval_from` existed carry only one of the two
+ * bindings, so infer the source from whichever is populated. A node with
+ * neither falls back to Dataset, matching the form's default selection.
+ */
+function resolveRetrievalFrom(params: Record<string, any>): RetrievalFrom {
+ const source = params.retrieval_from;
+ if (source === RetrievalFrom.Memory || source === RetrievalFrom.Dataset) {
+ return source;
+ }
+ return !isEmptyIdList(params.memory_ids) &&
+ isEmptyIdList(params.dataset_ids ?? params.kb_ids)
+ ? RetrievalFrom.Memory
+ : RetrievalFrom.Dataset;
+}
+
/**
* Shared by Retrieval nodes and Agent-embedded Retrieval tools. Emptiness is
- * asserted directly instead of re-parsing the form schema so legacy DSLs keyed
- * on `kb_ids` are never flagged (ported from find-invalid-retrieval.ts).
+ * asserted directly instead of re-parsing the form schema so a binding still
+ * counts when it is keyed on the legacy `kb_ids`
+ * (ported from find-invalid-retrieval.ts).
*/
function collectRetrievalBindingIssues(
params: Record<string, any> | undefined,
@@ -307,23 +329,17 @@ function collectRetrievalBindingIssues(
}
const { memoryIds, staleDatasetIds } = inputs;
const issues: CanvasIssue[] = [];
+ const datasetIds = params.dataset_ids ?? params.kb_ids;
+ const isMemory = resolveRetrievalFrom(params) === RetrievalFrom.Memory;
- if (
- params.retrieval_from === RetrievalFrom.Dataset &&
- Array.isArray(params.dataset_ids) &&
- params.dataset_ids.length === 0
- ) {
+ if (!isMemory && isEmptyIdList(datasetIds)) {
issues.push({
...target,
type: CanvasIssueType.MissingRequired,
messageKey: 'flow.retrievalDatasetMissing',
});
}
- if (
- params.retrieval_from === RetrievalFrom.Memory &&
- Array.isArray(params.memory_ids) &&
- params.memory_ids.length === 0
- ) {
+ if (isMemory && isEmptyIdList(params.memory_ids)) {
issues.push({
...target,
type: CanvasIssueType.MissingRequired,
@@ -332,9 +348,9 @@ function collectRetrievalBindingIssues(
}
if (
staleDatasetIds &&
- params.retrieval_from !== RetrievalFrom.Memory &&
- Array.isArray(params.dataset_ids) &&
- params.dataset_ids.some((id: string) => staleDatasetIds.has(id))
+ !isMemory &&
+ Array.isArray(datasetIds) &&
+ datasetIds.some((id: string) => staleDatasetIds.has(id))
) {
issues.push({
...target,
@@ -344,7 +360,7 @@ function collectRetrievalBindingIssues(
}
if (
memoryIds &&
- params.retrieval_from === RetrievalFrom.Memory &&
+ isMemory &&
Array.isArray(params.memory_ids) &&
params.memory_ids.some((id: string) => !memoryIds.has(id))
) {