mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Feat: The query variable of a loop operator can be a nested array variable. #10866 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -61,15 +61,17 @@ export function CardWithForm() {
|
|||||||
|
|
||||||
type LabelCardProps = {
|
type LabelCardProps = {
|
||||||
className?: string;
|
className?: string;
|
||||||
} & PropsWithChildren;
|
} & PropsWithChildren &
|
||||||
|
React.HTMLAttributes<HTMLElement>;
|
||||||
|
|
||||||
export function LabelCard({ children, className }: LabelCardProps) {
|
export function LabelCard({ children, className, ...props }: LabelCardProps) {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
'bg-bg-card rounded-sm p-1 text-text-secondary text-xs',
|
'bg-bg-card rounded-sm p-1 text-text-secondary text-xs',
|
||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
|
{...props}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -56,6 +56,7 @@ export function QueryVariable({
|
|||||||
options={finalOptions}
|
options={finalOptions}
|
||||||
{...field}
|
{...field}
|
||||||
// allowClear
|
// allowClear
|
||||||
|
type={type}
|
||||||
></GroupedSelectWithSecondaryMenu>
|
></GroupedSelectWithSecondaryMenu>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import { ChevronDownIcon, XIcon } from 'lucide-react';
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { VariableType } from '../../constant';
|
||||||
import {
|
import {
|
||||||
useFilterStructuredOutputByValue,
|
useFilterStructuredOutputByValue,
|
||||||
useFindAgentStructuredOutputLabel,
|
useFindAgentStructuredOutputLabel,
|
||||||
@ -52,6 +53,7 @@ interface GroupedSelectWithSecondaryMenuProps {
|
|||||||
value?: string;
|
value?: string;
|
||||||
onChange?: (value: string) => void;
|
onChange?: (value: string) => void;
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
|
type?: VariableType;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function GroupedSelectWithSecondaryMenu({
|
export function GroupedSelectWithSecondaryMenu({
|
||||||
@ -59,6 +61,7 @@ export function GroupedSelectWithSecondaryMenu({
|
|||||||
value,
|
value,
|
||||||
onChange,
|
onChange,
|
||||||
placeholder,
|
placeholder,
|
||||||
|
type,
|
||||||
}: GroupedSelectWithSecondaryMenuProps) {
|
}: GroupedSelectWithSecondaryMenuProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [open, setOpen] = React.useState(false);
|
const [open, setOpen] = React.useState(false);
|
||||||
@ -69,6 +72,7 @@ export function GroupedSelectWithSecondaryMenu({
|
|||||||
|
|
||||||
// Find the label of the selected item
|
// Find the label of the selected item
|
||||||
const flattenedOptions = options.flatMap((g) => g.options);
|
const flattenedOptions = options.flatMap((g) => g.options);
|
||||||
|
|
||||||
let selectedItem = flattenedOptions
|
let selectedItem = flattenedOptions
|
||||||
.flatMap((o) => [o, ...(o.children || [])])
|
.flatMap((o) => [o, ...(o.children || [])])
|
||||||
.find((o) => o.value === value);
|
.find((o) => o.value === value);
|
||||||
@ -140,7 +144,7 @@ export function GroupedSelectWithSecondaryMenu({
|
|||||||
<PopoverContent className="p-0" align="start">
|
<PopoverContent className="p-0" align="start">
|
||||||
<Command value={value}>
|
<Command value={value}>
|
||||||
<CommandInput placeholder="Search..." />
|
<CommandInput placeholder="Search..." />
|
||||||
<CommandList className="overflow-visible">
|
<CommandList className="overflow-auto">
|
||||||
{options.map((group, idx) => (
|
{options.map((group, idx) => (
|
||||||
<CommandGroup key={idx} heading={group.label}>
|
<CommandGroup key={idx} heading={group.label}>
|
||||||
{group.options.map((option) => {
|
{group.options.map((option) => {
|
||||||
@ -159,6 +163,7 @@ export function GroupedSelectWithSecondaryMenu({
|
|||||||
data={option}
|
data={option}
|
||||||
click={handleSecondaryMenuClick}
|
click={handleSecondaryMenuClick}
|
||||||
filteredStructuredOutput={filteredStructuredOutput}
|
filteredStructuredOutput={filteredStructuredOutput}
|
||||||
|
type={type}
|
||||||
></StructuredOutputSecondaryMenu>
|
></StructuredOutputSecondaryMenu>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import { cn } from '@/lib/utils';
|
|||||||
import { get, isPlainObject } from 'lodash';
|
import { get, isPlainObject } from 'lodash';
|
||||||
import { ChevronRight } from 'lucide-react';
|
import { ChevronRight } from 'lucide-react';
|
||||||
import { PropsWithChildren, ReactNode, useCallback } from 'react';
|
import { PropsWithChildren, ReactNode, useCallback } from 'react';
|
||||||
|
import { VariableType } from '../../constant';
|
||||||
|
|
||||||
type DataItem = { label: ReactNode; value: string; parentLabel?: ReactNode };
|
type DataItem = { label: ReactNode; value: string; parentLabel?: ReactNode };
|
||||||
|
|
||||||
@ -15,12 +16,24 @@ type StructuredOutputSecondaryMenuProps = {
|
|||||||
data: DataItem;
|
data: DataItem;
|
||||||
click(option: { label: ReactNode; value: string }): void;
|
click(option: { label: ReactNode; value: string }): void;
|
||||||
filteredStructuredOutput: JSONSchema;
|
filteredStructuredOutput: JSONSchema;
|
||||||
|
type?: VariableType;
|
||||||
} & PropsWithChildren;
|
} & PropsWithChildren;
|
||||||
export function StructuredOutputSecondaryMenu({
|
export function StructuredOutputSecondaryMenu({
|
||||||
data,
|
data,
|
||||||
click,
|
click,
|
||||||
filteredStructuredOutput,
|
filteredStructuredOutput,
|
||||||
|
type,
|
||||||
}: StructuredOutputSecondaryMenuProps) {
|
}: StructuredOutputSecondaryMenuProps) {
|
||||||
|
const handleSubMenuClick = useCallback(
|
||||||
|
(option: { label: ReactNode; value: string }, dataType?: string) => () => {
|
||||||
|
// The query variable of the iteration operator can only select array type data.
|
||||||
|
if ((type && type === dataType) || !type) {
|
||||||
|
click(option);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[click, type],
|
||||||
|
);
|
||||||
|
|
||||||
const renderAgentStructuredOutput = useCallback(
|
const renderAgentStructuredOutput = useCallback(
|
||||||
(values: any, option: { label: ReactNode; value: string }) => {
|
(values: any, option: { label: ReactNode; value: string }) => {
|
||||||
if (isPlainObject(values) && 'properties' in values) {
|
if (isPlainObject(values) && 'properties' in values) {
|
||||||
@ -37,7 +50,7 @@ export function StructuredOutputSecondaryMenu({
|
|||||||
return (
|
return (
|
||||||
<li key={key} className="pl-1">
|
<li key={key} className="pl-1">
|
||||||
<div
|
<div
|
||||||
onClick={() => click(nextOption)}
|
onClick={handleSubMenuClick(nextOption, dataType)}
|
||||||
className="hover:bg-bg-card p-1 text-text-primary rounded-sm flex justify-between"
|
className="hover:bg-bg-card p-1 text-text-primary rounded-sm flex justify-between"
|
||||||
>
|
>
|
||||||
{key}
|
{key}
|
||||||
@ -54,13 +67,16 @@ export function StructuredOutputSecondaryMenu({
|
|||||||
|
|
||||||
return <div></div>;
|
return <div></div>;
|
||||||
},
|
},
|
||||||
[click],
|
[handleSubMenuClick],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<HoverCard key={data.value} openDelay={100} closeDelay={100}>
|
<HoverCard key={data.value} openDelay={100} closeDelay={100}>
|
||||||
<HoverCardTrigger asChild>
|
<HoverCardTrigger asChild>
|
||||||
<li className="hover:bg-bg-card py-1 px-2 text-text-primary rounded-sm text-sm flex justify-between items-center">
|
<li
|
||||||
|
onClick={() => click(data)}
|
||||||
|
className="hover:bg-bg-card py-1 px-2 text-text-primary rounded-sm text-sm flex justify-between items-center"
|
||||||
|
>
|
||||||
{data.label} <ChevronRight className="size-3.5 text-text-secondary" />
|
{data.label} <ChevronRight className="size-3.5 text-text-secondary" />
|
||||||
</li>
|
</li>
|
||||||
</HoverCardTrigger>
|
</HoverCardTrigger>
|
||||||
|
|||||||
@ -2,6 +2,42 @@ import { JSONSchema } from '@/components/jsonjoy-builder';
|
|||||||
import { Operator } from '@/constants/agent';
|
import { Operator } from '@/constants/agent';
|
||||||
import { isPlainObject } from 'lodash';
|
import { isPlainObject } from 'lodash';
|
||||||
|
|
||||||
|
// Loop operators can only accept variables of type list.
|
||||||
|
|
||||||
|
// Recursively traverse the JSON schema, keeping attributes with type "array" and discarding others.
|
||||||
|
|
||||||
|
export function filterLoopOperatorInput(
|
||||||
|
structuredOutput: JSONSchema,
|
||||||
|
path = [],
|
||||||
|
) {
|
||||||
|
if (typeof structuredOutput === 'boolean') {
|
||||||
|
return structuredOutput;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
structuredOutput.properties &&
|
||||||
|
isPlainObject(structuredOutput.properties)
|
||||||
|
) {
|
||||||
|
const properties = Object.entries({
|
||||||
|
...structuredOutput.properties,
|
||||||
|
}).reduce(
|
||||||
|
(pre, [key, value]) => {
|
||||||
|
if (
|
||||||
|
typeof value !== 'boolean' &&
|
||||||
|
(value.type === 'array' || hasArrayChild(value))
|
||||||
|
) {
|
||||||
|
pre[key] = filterLoopOperatorInput(value, path);
|
||||||
|
}
|
||||||
|
return pre;
|
||||||
|
},
|
||||||
|
{} as Record<string, JSONSchema>,
|
||||||
|
);
|
||||||
|
|
||||||
|
return { ...structuredOutput, properties };
|
||||||
|
}
|
||||||
|
|
||||||
|
return structuredOutput;
|
||||||
|
}
|
||||||
|
|
||||||
export function filterAgentStructuredOutput(
|
export function filterAgentStructuredOutput(
|
||||||
structuredOutput: JSONSchema,
|
structuredOutput: JSONSchema,
|
||||||
operator?: string,
|
operator?: string,
|
||||||
@ -13,32 +49,39 @@ export function filterAgentStructuredOutput(
|
|||||||
structuredOutput.properties &&
|
structuredOutput.properties &&
|
||||||
isPlainObject(structuredOutput.properties)
|
isPlainObject(structuredOutput.properties)
|
||||||
) {
|
) {
|
||||||
const filterByPredicate = (predicate: (value: JSONSchema) => boolean) => {
|
|
||||||
const properties = Object.entries({
|
|
||||||
...structuredOutput.properties,
|
|
||||||
}).reduce(
|
|
||||||
(pre, [key, value]) => {
|
|
||||||
if (predicate(value)) {
|
|
||||||
pre[key] = value;
|
|
||||||
}
|
|
||||||
return pre;
|
|
||||||
},
|
|
||||||
{} as Record<string, JSONSchema>,
|
|
||||||
);
|
|
||||||
|
|
||||||
return { ...structuredOutput, properties };
|
|
||||||
};
|
|
||||||
|
|
||||||
if (operator === Operator.Iteration) {
|
if (operator === Operator.Iteration) {
|
||||||
return filterByPredicate(
|
return filterLoopOperatorInput(structuredOutput);
|
||||||
(value) => typeof value !== 'boolean' && value.type === 'array',
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return filterByPredicate(
|
|
||||||
(value) => typeof value !== 'boolean' && value.type !== 'array',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return structuredOutput;
|
return structuredOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return structuredOutput;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function hasArrayChild(data: Record<string, any> | Array<any>) {
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
for (const value of data) {
|
||||||
|
if (isPlainObject(value) && value.type === 'array') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (hasArrayChild(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isPlainObject(data)) {
|
||||||
|
for (const value of Object.values(data)) {
|
||||||
|
if (isPlainObject(value) && value.type === 'array') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasArrayChild(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user