Skip to content

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

Fix: Validate the parameters of the variable assignment operator. (#20915)

balibabu0d94f1761a27
diff --git a/web/src/locales/en.ts b/web/src/locales/en.ts
index f237fd16c2..1796d8bf9e 100644
--- a/web/src/locales/en.ts
+++ b/web/src/locales/en.ts
@@ -3771,6 +3771,9 @@ Important structured information may include: names, dates, locations, events, k
removeFirst: 'Remove first',
removeLast: 'Remove last',
},
+ variableAssignerVariableMissing: 'Variable is required',
+ variableAssignerOperatorMissing: 'Operator is required',
+ variableAssignerParameterMissing: 'Parameter is required',
webhook: {
name: 'Webhook',
methods: 'Methods',
diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.ts
index 2518a8c1bb..7e859ea13d 100644
--- a/web/src/locales/zh.ts
+++ b/web/src/locales/zh.ts
@@ -3268,6 +3268,9 @@ Tokenizer 会根据所选方式将内容存储为对应的数据结构。`,
removeFirst: '移除第一个',
removeLast: '移除最后一个',
},
+ variableAssignerVariableMissing: '请选择变量',
+ variableAssignerOperatorMissing: '请选择运算符',
+ variableAssignerParameterMissing: '请填写参数',
webhook: {
name: '网络钩子',
methods: '方法',
diff --git a/web/src/pages/agent/canvas/node/variable-assigner-node.tsx b/web/src/pages/agent/canvas/node/variable-assigner-node.tsx
index 0f6c79a62b..19bd5eb986 100644
--- a/web/src/pages/agent/canvas/node/variable-assigner-node.tsx
+++ b/web/src/pages/agent/canvas/node/variable-assigner-node.tsx
@@ -2,7 +2,7 @@ import { NodeCollapsible } from '@/components/collapse';
import { BaseNode } from '@/interfaces/database/agent';
import { NodeProps } from '@xyflow/react';
import { RagNode } from '.';
-import { VariableAssignerFormSchemaType } from '../../form/variable-assigner-form';
+import { VariableAssignerFormSchemaType } from '../../form/variable-assigner-form/schema';
import { useGetVariableLabelOrTypeByValue } from '../../hooks/use-get-begin-query';
import { LabelCard } from './card';
diff --git a/web/src/pages/agent/form/components/query-variable.tsx b/web/src/pages/agent/form/components/query-variable.tsx
index a6eef62451..2cbb2246fb 100644
--- a/web/src/pages/agent/form/components/query-variable.tsx
+++ b/web/src/pages/agent/form/components/query-variable.tsx
@@ -67,7 +67,7 @@ export function QueryVariable({
);
if (pureQuery) {
- renderWidget(value, onChange);
+ return renderWidget(value, onChange);
}
return (
diff --git a/web/src/pages/agent/form/loop-form/loop-termination-condition.tsx b/web/src/pages/agent/form/loop-form/loop-termination-condition.tsx
index 52d5987119..fc9cb5b70a 100644
--- a/web/src/pages/agent/form/loop-form/loop-termination-condition.tsx
+++ b/web/src/pages/agent/form/loop-form/loop-termination-condition.tsx
@@ -195,10 +195,11 @@ export function LoopTerminationCondition({
<Input type="number" />
</RAGFlowFormItem>
) : (
+ // No wrapping RAGFlowFormItem here: QueryVariable's own
+ // FormField registers valueFieldAlias and shows its errors.
<QueryVariable
types={[JsonSchemaDataType.Number]}
hideLabel
- pureQuery
name={valueFieldAlias}
className="flex-1 min-w-0"
></QueryVariable>
diff --git a/web/src/pages/agent/form/variable-assigner-form/dynamic-variables.tsx b/web/src/pages/agent/form/variable-assigner-form/dynamic-variables.tsx
index 5e8e987993..7c776520bb 100644
--- a/web/src/pages/agent/form/variable-assigner-form/dynamic-variables.tsx
+++ b/web/src/pages/agent/form/variable-assigner-form/dynamic-variables.tsx
@@ -22,6 +22,7 @@ import { useGetVariableLabelOrTypeByValue } from '../../hooks/use-get-begin-quer
import { getArrayElementType } from '../../utils';
import { DynamicFormHeader } from '../components/dynamic-fom-header';
import { QueryVariable } from '../components/query-variable';
+import { ParameterlessOperators } from './schema';
import { useBuildLogicalOptions } from './use-build-logical-options';
loader.config({ paths: { vs: '/vs' } });
@@ -63,12 +64,6 @@ function RadioButton({ value, onChange }: RadioButtonProps) {
);
}
-const EmptyFields = [
- VariableAssignerLogicalOperator.Clear,
- VariableAssignerLogicalArrayOperator.RemoveFirst,
- VariableAssignerLogicalArrayOperator.RemoveLast,
-];
-
const EmptyValueMap = {
[JsonSchemaDataType.String]: '',
[JsonSchemaDataType.Number]: 0,
@@ -139,7 +134,7 @@ export function DynamicVariables({
const logicalOperator = form.getValues(operatorFieldName);
const type = getVariableType(keyFieldName);
- if (EmptyFields.includes(logicalOperator)) {
+ if (ParameterlessOperators.includes(logicalOperator)) {
return null;
} else if (
logicalOperator === VariableAssignerLogicalOperator.Overwrite ||
@@ -220,6 +215,13 @@ export function DynamicVariables({
shouldValidate: true,
});
}
+
+ // The parameter input is hidden for these operators and its value is
+ // ignored at run time, so nothing re-validates the field — drop any
+ // stale error left over from the previous operator explicitly.
+ if (ParameterlessOperators.includes(value)) {
+ form.clearErrors(valueFieldAlias);
+ }
},
[form, getVariableType],
);
@@ -241,7 +243,7 @@ export function DynamicVariables({
return (
<section key={field.id} className="flex gap-2">
<div className="flex-1 space-y-3 min-w-0">
- <div className="flex items-center">
+ <div className="flex items-start">
<QueryVariable
name={keyFieldAlias}
hideLabel
@@ -252,7 +254,13 @@ export function DynamicVariables({
)}
></QueryVariable>
- <Separator className="w-2" />
+ {/* h-8 matches the default Button height both select
+ triggers render at; with items-start above, an error
+ message under either select no longer pushes the dash
+ or the operator select out of alignment. */}
+ <div className="flex h-8 items-center">
+ <Separator className="w-2" />
+ </div>
<RAGFlowFormItem name={operatorFieldAlias} className="w-1/3">
{({ onChange, value }) => (
diff --git a/web/src/pages/agent/form/variable-assigner-form/index.tsx b/web/src/pages/agent/form/variable-assigner-form/index.tsx
index 931351e7e3..24fc8b4876 100644
--- a/web/src/pages/agent/form/variable-assigner-form/index.tsx
+++ b/web/src/pages/agent/form/variable-assigner-form/index.tsx
@@ -2,27 +2,13 @@ import { Form } from '@/components/ui/form';
import { zodResolver } from '@hookform/resolvers/zod';
import { memo } from 'react';
import { useForm } from 'react-hook-form';
-import { z } from 'zod';
import { initialDataOperationsValues } from '../../constant';
import { useFormValues } from '../../hooks/use-form-values';
import { useWatchFormChange } from '../../hooks/use-watch-form-change';
import { INextOperatorForm } from '../../interface';
import { FormWrapper } from '../components/form-wrapper';
import { DynamicVariables } from './dynamic-variables';
-
-export const VariableAssignerSchema = {
- variables: z.array(
- z.object({
- variable: z.string().optional(),
- operator: z.string().optional(),
- parameter: z.string().or(z.number()).or(z.boolean()).optional(),
- }),
- ),
-};
-
-export const FormSchema = z.object(VariableAssignerSchema);
-
-export type VariableAssignerFormSchemaType = z.infer<typeof FormSchema>;
+import { FormSchema, VariableAssignerFormSchemaType } from './schema';
function VariableAssignerForm({ node }: INextOperatorForm) {
const defaultValues = useFormValues(initialDataOperationsValues, node);
diff --git a/web/src/pages/agent/form/variable-assigner-form/schema.ts b/web/src/pages/agent/form/variable-assigner-form/schema.ts
new file mode 100644
index 0000000000..76fdd20f15
--- /dev/null
+++ b/web/src/pages/agent/form/variable-assigner-form/schema.ts
@@ -0,0 +1,89 @@
+import i18n from '@/locales/config';
+import { z } from 'zod';
+import {
+ VariableAssignerLogicalArrayOperator,
+ VariableAssignerLogicalOperator,
+} from '../../constant';
+
+// Operators that consume no parameter: the form hides the parameter input
+// for them (renderParameter in dynamic-variables.tsx), so they are exempt
+// from the required check.
+export const ParameterlessOperators: string[] = [
+ VariableAssignerLogicalOperator.Clear,
+ VariableAssignerLogicalArrayOperator.RemoveFirst,
+ VariableAssignerLogicalArrayOperator.RemoveLast,
+];
+
+// The parameter control switches with the variable type and operator: a
+// variable-ref string (overwrite/append/extend), free text or a JSON string
+// (set), a number-input string (arithmetic), or an EmptyValueMap default
+// (0 / 'yes' / '{}' / []). Persisted DSLs may carry actual numbers, booleans,
+// arrays, objects or null, so all of those parse; emptiness is judged by the
+// refine below.
+const ParameterSchema = z
+ .union([
+ z.string(),
+ z.number(),
+ z.boolean(),
+ z.array(z.any()),
+ z.record(z.any()),
+ z.null(),
+ ])
+ .optional();
+
+// 0, false and '{}' are legitimate parameter values; empty means "nothing
+// entered" — unset, null, '' or the empty-array default an array-typed
+// variable gets before a ref is picked.
+function isEmptyParameter(value: unknown): boolean {
+ return (
+ value === undefined ||
+ value === null ||
+ value === '' ||
+ (Array.isArray(value) && value.length === 0)
+ );
+}
+
+// All emptiness checks live in the refine rather than on the fields: an
+// object-level superRefine is skipped when the base shape fails, and a
+// field-level check cannot see the sibling operator that decides whether
+// parameter is required at all. Reporting every empty field of a row at
+// once is also the better editing experience.
+const VariableItemSchema = z
+ .object({
+ variable: z.string().optional(),
+ operator: z.string().optional(),
+ parameter: ParameterSchema,
+ })
+ .superRefine((item, ctx) => {
+ if (!item.variable) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ path: ['variable'],
+ message: i18n.t('flow.variableAssignerVariableMissing'),
+ });
+ }
+ if (!item.operator) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ path: ['operator'],
+ message: i18n.t('flow.variableAssignerOperatorMissing'),
+ });
+ }
+ if (
+ item.operator &&
+ !ParameterlessOperators.includes(item.operator) &&
+ isEmptyParameter(item.parameter)
+ ) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ path: ['parameter'],
+ message: i18n.t('flow.variableAssignerParameterMissing'),
+ });
+ }
+ });
+
+export const FormSchema = z.object({
+ variables: z.array(VariableItemSchema),
+});
+
+export type VariableAssignerFormSchemaType = z.infer<typeof FormSchema>;
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 16e863633c..4fda8baadf 100644
--- a/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts
+++ b/web/src/pages/agent/utils/__tests__/canvas-checklist.test.ts
@@ -653,6 +653,94 @@ describe('collectCanvasIssues: missing required fields', () => {
}),
);
});
+
+ describe('variable assigner rows', () => {
+ const fileNode = () => makeNode('File', 'File', {});
+ const assignerNode = (variables: Record<string, any>[]) =>
+ makeNode('VariableAssigner:v1', 'VariableAssigner', { variables });
+ const wire = () => [makeEdge('File', 'VariableAssigner:v1')];
+ const edited = ['VariableAssigner:v1'];
+
+ const collectRowIssues = (variables: Record<string, any>[]) =>
+ collect({
+ nodes: [fileNode(), assignerNode(variables)],
+ edges: wire(),
+ editedNodeFormIds: edited,
+ }).filter(
+ (x) =>
+ x.nodeId === 'VariableAssigner:v1' &&
+ x.messageKey === 'flow.nodeFormInvalid',
+ );
+
+ it('flags an incomplete row only after the node was edited', () => {
+ const variables = [
+ { variable: '', operator: 'overwrite', parameter: '' },
+ ];
+
+ const untouched = collect({
+ nodes: [fileNode(), assignerNode(variables)],
+ edges: wire(),
+ });
+
+ expect(
+ untouched.some((x) => x.messageKey === 'flow.nodeFormInvalid'),
+ ).toBe(false);
+ expect(collectRowIssues(variables)).toHaveLength(1);
+ });
+
+ it('flags a row with an empty variable or operator', () => {
+ expect(
+ collectRowIssues([{ operator: 'set', parameter: 'x' }]),
+ ).toHaveLength(1);
+ expect(
+ collectRowIssues([{ variable: 'begin@a', parameter: 'x' }]),
+ ).toHaveLength(1);
+ });
+
+ it('flags a missing, empty-string or empty-array parameter', () => {
+ expect(
+ collectRowIssues([{ variable: 'begin@a', operator: 'overwrite' }]),
+ ).toHaveLength(1);
+ expect(
+ collectRowIssues([
+ { variable: 'begin@a', operator: 'overwrite', parameter: '' },
+ ]),
+ ).toHaveLength(1);
+ expect(
+ collectRowIssues([
+ { variable: 'begin@a', operator: 'append', parameter: [] },
+ ]),
+ ).toHaveLength(1);
+ });
+
+ it.each([['clear'], ['remove_first'], ['remove_last']])(
+ 'skips the parameter check for the parameterless %s operator',
+ (operator) => {
+ expect(
+ collectRowIssues([{ variable: 'begin@a', operator }]),
+ ).toHaveLength(0);
+ },
+ );
+
+ it('treats 0, yes and {} as filled parameters', () => {
+ expect(
+ collectRowIssues([
+ { variable: 'begin@a', operator: '+=', parameter: 0 },
+ { variable: 'begin@b', operator: 'set', parameter: 'yes' },
+ { variable: 'begin@c', operator: 'set', parameter: '{}' },
+ ]),
+ ).toHaveLength(0);
+ });
+
+ it('accepts a complete row and an empty variables list', () => {
+ expect(
+ collectRowIssues([
+ { variable: 'begin@a', operator: 'set', parameter: 'x' },
+ ]),
+ ).toHaveLength(0);
+ expect(collectRowIssues([])).toHaveLength(0);
+ });
+ });
});
describe('collectCanvasIssues: stale resources', () => {
diff --git a/web/src/pages/agent/utils/canvas-checklist.ts b/web/src/pages/agent/utils/canvas-checklist.ts
index 31c653d8e3..dbf0f7f22e 100644
--- a/web/src/pages/agent/utils/canvas-checklist.ts
+++ b/web/src/pages/agent/utils/canvas-checklist.ts
@@ -26,6 +26,7 @@ import {
VariableRegex,
} from '../constant';
import { FormSchema as ParserFormSchema } from '../form/parser-form/schema';
+import { FormSchema as VariableAssignerFormSchema } from '../form/variable-assigner-form/schema';
import { isEmptyMessageContent } from '../utils';
/**
@@ -630,6 +631,21 @@ export function collectCanvasIssues({
});
}
+ // The backend hard-fails a run on an empty variable/operator and reports
+ // per-item errors for an empty parameter, so an incomplete row must be
+ // caught before Run/Publish. Same edited-node gate as the Parser check.
+ if (
+ label === Operator.VariableAssigner &&
+ editedNodeFormIds.includes(node.id) &&
+ !VariableAssignerFormSchema.safeParse(form).success
+ ) {
+ issues.push({
+ ...target,
+ type: CanvasIssueType.MissingRequired,
+ messageKey: 'flow.nodeFormInvalid',
+ });
+ }
+
if (
label === Operator.Message &&
memoryIds &&