Skip to content

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

Fix: Changing the variable assignment operator results in the error: `c.trim() is not a function` (#20937)

balibabu296691996129
diff --git a/web/src/pages/agent/form/components/query-variable.tsx b/web/src/pages/agent/form/components/query-variable.tsx
index 2cbb2246fb..810f14fc82 100644
--- a/web/src/pages/agent/form/components/query-variable.tsx
+++ b/web/src/pages/agent/form/components/query-variable.tsx
@@ -5,7 +5,7 @@ import {
FormLabel,
FormMessage,
} from '@/components/ui/form';
-import { ReactNode } from 'react';
+import { ReactNode, forwardRef } from 'react';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { JsonSchemaDataType, VariableType } from '../../constant';
@@ -29,62 +29,72 @@ type QueryVariableProps = {
value?: string;
} & BuildQueryVariableOptions;
-export function QueryVariable({
- name = 'query',
- types = [],
- label,
- hideLabel = false,
- className,
- onChange,
- pureQuery = false,
- value,
- nodeIds = [],
- variablesExceptOperatorOutputs,
-}: QueryVariableProps) {
- const { t } = useTranslation();
- const form = useFormContext();
+// RAGFlowFormItem's FormControl (a Radix Slot) clones its child with a ref,
+// so this component must forward it to the underlying select trigger.
+export const QueryVariable = forwardRef<HTMLButtonElement, QueryVariableProps>(
+ function QueryVariable(
+ {
+ name = 'query',
+ types = [],
+ label,
+ hideLabel = false,
+ className,
+ onChange,
+ pureQuery = false,
+ value,
+ nodeIds = [],
+ variablesExceptOperatorOutputs,
+ },
+ ref,
+ ) {
+ const { t } = useTranslation();
+ const form = useFormContext();
- const finalOptions = useFilterQueryVariableOptionsByTypes({
- types,
- nodeIds,
- variablesExceptOperatorOutputs,
- });
+ const finalOptions = useFilterQueryVariableOptionsByTypes({
+ types,
+ nodeIds,
+ variablesExceptOperatorOutputs,
+ });
- const renderWidget = (
- value?: string,
- handleChange?: (value: string) => void,
- ) => (
- <GroupedSelectWithSecondaryMenu
- options={finalOptions}
- value={value}
- onChange={(val) => {
- handleChange?.(val);
- onChange?.(val);
- }}
- // allowClear
- types={types}
- ></GroupedSelectWithSecondaryMenu>
- );
+ const renderWidget = (
+ value?: string,
+ handleChange?: (value: string) => void,
+ ) => (
+ <GroupedSelectWithSecondaryMenu
+ ref={ref}
+ options={finalOptions}
+ value={value}
+ onChange={(val) => {
+ handleChange?.(val);
+ onChange?.(val);
+ }}
+ // allowClear
+ types={types}
+ ></GroupedSelectWithSecondaryMenu>
+ );
- if (pureQuery) {
- return renderWidget(value, onChange);
- }
+ if (pureQuery) {
+ return renderWidget(value, onChange);
+ }
- return (
- <FormField
- control={form.control}
- name={name}
- render={({ field }) => (
- <FormItem className={className}>
- {hideLabel || (
- <FormLabel tooltip={label ? undefined : t('flow.queryTip')}>
- {label || t('flow.query')}
- </FormLabel>
- )}
- <FormControl>{renderWidget(field.value, field.onChange)}</FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- );
-}
+ return (
+ <FormField
+ control={form.control}
+ name={name}
+ render={({ field }) => (
+ <FormItem className={className}>
+ {hideLabel || (
+ <FormLabel tooltip={label ? undefined : t('flow.queryTip')}>
+ {label || t('flow.query')}
+ </FormLabel>
+ )}
+ <FormControl>
+ {renderWidget(field.value, field.onChange)}
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ );
+ },
+);
diff --git a/web/src/pages/agent/form/components/select-with-secondary-menu.tsx b/web/src/pages/agent/form/components/select-with-secondary-menu.tsx
index 34c86847d8..0acee3fa2c 100644
--- a/web/src/pages/agent/form/components/select-with-secondary-menu.tsx
+++ b/web/src/pages/agent/form/components/select-with-secondary-menu.tsx
@@ -47,7 +47,10 @@ type Group = {
options: Option[];
};
-interface GroupedSelectWithSecondaryMenuProps {
+interface GroupedSelectWithSecondaryMenuProps extends Omit<
+ React.ComponentPropsWithoutRef<'button'>,
+ 'value' | 'onChange'
+> {
options: Group[];
value?: string;
onChange?: (value: string) => void;
@@ -55,28 +58,35 @@ interface GroupedSelectWithSecondaryMenuProps {
types?: JsonSchemaDataType[];
}
-export function GroupedSelectWithSecondaryMenu({
- options,
- value,
- onChange,
- placeholder,
- types,
-}: GroupedSelectWithSecondaryMenuProps) {
+export const GroupedSelectWithSecondaryMenu = React.forwardRef<
+ HTMLButtonElement,
+ GroupedSelectWithSecondaryMenuProps
+>(function GroupedSelectWithSecondaryMenu(
+ { options, value, onChange, placeholder, types, ...props },
+ ref,
+) {
const { t } = useTranslation();
const [open, setOpen] = React.useState(false);
const showSecondaryMenu = useShowSecondaryMenu();
const findAgentStructuredOutputLabel = useFindAgentStructuredOutputLabel();
+ // cmdk calls .trim() on Command's controlled value, so a non-string value
+ // from a stale persisted form (e.g. an array) would crash the whole page.
+ const stringValue = typeof value === 'string' ? value : '';
+
// Find the label of the selected item
const flattenedOptions = options.flatMap((g) => g.options);
let selectedItem = flattenedOptions
.flatMap((o) => [o, ...(o.children || [])])
- .find((o) => o.value === value);
+ .find((o) => o.value === stringValue);
- if (!selectedItem && value) {
- selectedItem = findAgentStructuredOutputLabel(value, flattenedOptions);
+ if (!selectedItem && stringValue) {
+ selectedItem = findAgentStructuredOutputLabel(
+ stringValue,
+ flattenedOptions,
+ );
}
// Handle clear click
@@ -98,15 +108,17 @@ export function GroupedSelectWithSecondaryMenu({
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
+ ref={ref}
variant="outline"
role="combobox"
aria-expanded={open}
className={cn(
'!bg-bg-input hover:bg-background border-input w-full justify-between px-3 font-normal outline-offset-0 outline-none focus-visible:outline-[3px] [&_svg]:pointer-events-auto',
- !value && 'text-muted-foreground',
+ !stringValue && 'text-muted-foreground',
)}
+ {...props}
>
- {value ? (
+ {stringValue ? (
<div className="truncate flex items-center gap-1">
<span>{get(selectedItem, 'parentLabel')}</span>
<span className="text-text-disabled">/</span>
@@ -118,7 +130,7 @@ export function GroupedSelectWithSecondaryMenu({
</span>
)}
<div className="flex items-center justify-between">
- {value && (
+ {stringValue && (
<>
<XIcon
className="h-4 mx-2 cursor-pointer text-muted-foreground"
@@ -140,7 +152,7 @@ export function GroupedSelectWithSecondaryMenu({
</PopoverTrigger>
<PopoverContent className="p-0" align="start">
- <Command value={value}>
+ <Command value={stringValue}>
<CommandInput placeholder="Search..." />
<CommandList className="overflow-auto">
{options.map((group, idx) => (
@@ -191,7 +203,7 @@ export function GroupedSelectWithSecondaryMenu({
key={child.value}
className={cn(
'cursor-pointer rounded-sm px-2 py-1.5 text-sm hover:bg-bg-card hover:text-accent-foreground',
- value === child.value &&
+ stringValue === child.value &&
'bg-accent text-accent-foreground',
)}
onClick={() => {
@@ -229,4 +241,4 @@ export function GroupedSelectWithSecondaryMenu({
</PopoverContent>
</Popover>
);
-}
+});
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 7c776520bb..4ddb28f511 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
@@ -44,25 +44,28 @@ type RadioButtonProps = Partial<
}
>;
-function RadioButton({ value, onChange }: RadioButtonProps) {
- return (
- <RadioGroup
- defaultValue="yes"
- className="flex"
- value={value}
- onValueChange={onChange}
- >
- <div className="flex items-center gap-3">
- <RadioGroupItem value="yes" id="r1" />
- <Label htmlFor="r1">Yes</Label>
- </div>
- <div className="flex items-center gap-3">
- <RadioGroupItem value="no" id="r2" />
- <Label htmlFor="r2">No</Label>
- </div>
- </RadioGroup>
- );
-}
+const RadioButton = forwardRef<HTMLDivElement, RadioButtonProps>(
+ function RadioButton({ value, onChange }, ref) {
+ return (
+ <RadioGroup
+ ref={ref}
+ defaultValue="yes"
+ className="flex"
+ value={value}
+ onValueChange={onChange}
+ >
+ <div className="flex items-center gap-3">
+ <RadioGroupItem value="yes" id="r1" />
+ <Label htmlFor="r1">Yes</Label>
+ </div>
+ <div className="flex items-center gap-3">
+ <RadioGroupItem value="no" id="r2" />
+ <Label htmlFor="r2">No</Label>
+ </div>
+ </RadioGroup>
+ );
+ },
+);
const EmptyValueMap = {
[JsonSchemaDataType.String]: '',
@@ -171,7 +174,10 @@ export function DynamicVariables({
const subType = getArrayElementType(type);
return (
<QueryVariable
- types={[subType as JsonSchemaDataType]}
+ // A bare `array` type carries no element type; filtering by the
+ // empty string would hide every option. The backend accepts any
+ // element for an empty list, so leave the options unfiltered.
+ types={subType ? [subType as JsonSchemaDataType] : []}
hideLabel
pureQuery
></QueryVariable>
@@ -205,7 +211,15 @@ export function DynamicVariables({
let parameter = EmptyValueMap[type as keyof typeof EmptyValueMap];
- if (value === VariableAssignerLogicalOperator.Overwrite) {
+ // Overwrite, Extend, and Append all render a QueryVariable (a
+ // string-valued reference select), so the parameter must be reset to a
+ // string — passing EmptyValueMap's [] for array-typed variables would
+ // crash cmdk, which calls .trim() on its controlled value.
+ if (
+ value === VariableAssignerLogicalOperator.Overwrite ||
+ value === VariableAssignerLogicalArrayOperator.Extend ||
+ value === VariableAssignerLogicalArrayOperator.Append
+ ) {
parameter = '';
}