mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 12:32:30 +08:00
### What problem does this PR solve? Feat: Filter structured output data directly during the rendering stage. #10866 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -31,12 +31,10 @@ import * as ReactDOM from 'react-dom';
|
|||||||
import { $createVariableNode } from './variable-node';
|
import { $createVariableNode } from './variable-node';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
useFilterStructuredOutputByValue,
|
|
||||||
useFindAgentStructuredOutputLabel,
|
useFindAgentStructuredOutputLabel,
|
||||||
useShowSecondaryMenu,
|
useShowSecondaryMenu,
|
||||||
} from '@/pages/agent/hooks/use-build-structured-output';
|
} from '@/pages/agent/hooks/use-build-structured-output';
|
||||||
import { useBuildQueryVariableOptions } from '@/pages/agent/hooks/use-get-begin-query';
|
import { useBuildQueryVariableOptions } from '@/pages/agent/hooks/use-get-begin-query';
|
||||||
import { hasJsonSchemaChild } from '@/pages/agent/utils/filter-agent-structured-output';
|
|
||||||
import { PromptIdentity } from '../../agent-form/use-build-prompt-options';
|
import { PromptIdentity } from '../../agent-form/use-build-prompt-options';
|
||||||
import { StructuredOutputSecondaryMenu } from '../structured-output-secondary-menu';
|
import { StructuredOutputSecondaryMenu } from '../structured-output-secondary-menu';
|
||||||
import { ProgrammaticTag } from './constant';
|
import { ProgrammaticTag } from './constant';
|
||||||
@ -89,8 +87,6 @@ function VariablePickerMenuItem({
|
|||||||
option: VariableOption | VariableInnerOption,
|
option: VariableOption | VariableInnerOption,
|
||||||
) => void;
|
) => void;
|
||||||
}) {
|
}) {
|
||||||
const filterStructuredOutput = useFilterStructuredOutputByValue();
|
|
||||||
|
|
||||||
const showSecondaryMenu = useShowSecondaryMenu();
|
const showSecondaryMenu = useShowSecondaryMenu();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -108,12 +104,6 @@ function VariablePickerMenuItem({
|
|||||||
const shouldShowSecondary = showSecondaryMenu(x.value, x.label);
|
const shouldShowSecondary = showSecondaryMenu(x.value, x.label);
|
||||||
|
|
||||||
if (shouldShowSecondary) {
|
if (shouldShowSecondary) {
|
||||||
const filteredStructuredOutput = filterStructuredOutput(x.value);
|
|
||||||
|
|
||||||
if (!hasJsonSchemaChild(filteredStructuredOutput)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StructuredOutputSecondaryMenu
|
<StructuredOutputSecondaryMenu
|
||||||
key={x.value}
|
key={x.value}
|
||||||
@ -124,7 +114,6 @@ function VariablePickerMenuItem({
|
|||||||
...y,
|
...y,
|
||||||
} as VariableInnerOption)
|
} as VariableInnerOption)
|
||||||
}
|
}
|
||||||
filteredStructuredOutput={filteredStructuredOutput}
|
|
||||||
></StructuredOutputSecondaryMenu>
|
></StructuredOutputSecondaryMenu>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,11 +25,9 @@ import { useCallback } from 'react';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { VariableType } from '../../constant';
|
import { VariableType } from '../../constant';
|
||||||
import {
|
import {
|
||||||
useFilterStructuredOutputByValue,
|
|
||||||
useFindAgentStructuredOutputLabel,
|
useFindAgentStructuredOutputLabel,
|
||||||
useShowSecondaryMenu,
|
useShowSecondaryMenu,
|
||||||
} from '../../hooks/use-build-structured-output';
|
} from '../../hooks/use-build-structured-output';
|
||||||
import { hasJsonSchemaChild } from '../../utils/filter-agent-structured-output';
|
|
||||||
import { StructuredOutputSecondaryMenu } from './structured-output-secondary-menu';
|
import { StructuredOutputSecondaryMenu } from './structured-output-secondary-menu';
|
||||||
|
|
||||||
type Item = {
|
type Item = {
|
||||||
@ -68,7 +66,6 @@ export function GroupedSelectWithSecondaryMenu({
|
|||||||
const [open, setOpen] = React.useState(false);
|
const [open, setOpen] = React.useState(false);
|
||||||
|
|
||||||
const showSecondaryMenu = useShowSecondaryMenu();
|
const showSecondaryMenu = useShowSecondaryMenu();
|
||||||
const filterStructuredOutput = useFilterStructuredOutputByValue();
|
|
||||||
const findAgentStructuredOutputLabel = useFindAgentStructuredOutputLabel();
|
const findAgentStructuredOutputLabel = useFindAgentStructuredOutputLabel();
|
||||||
|
|
||||||
// Find the label of the selected item
|
// Find the label of the selected item
|
||||||
@ -155,21 +152,11 @@ export function GroupedSelectWithSecondaryMenu({
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (shouldShowSecondary) {
|
if (shouldShowSecondary) {
|
||||||
const filteredStructuredOutput = filterStructuredOutput(
|
|
||||||
option.value,
|
|
||||||
type,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!hasJsonSchemaChild(filteredStructuredOutput)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StructuredOutputSecondaryMenu
|
<StructuredOutputSecondaryMenu
|
||||||
key={option.value}
|
key={option.value}
|
||||||
data={option}
|
data={option}
|
||||||
click={handleSecondaryMenuClick}
|
click={handleSecondaryMenuClick}
|
||||||
filteredStructuredOutput={filteredStructuredOutput}
|
|
||||||
type={type}
|
type={type}
|
||||||
></StructuredOutputSecondaryMenu>
|
></StructuredOutputSecondaryMenu>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import { JSONSchema } from '@/components/jsonjoy-builder';
|
|
||||||
import {
|
import {
|
||||||
HoverCard,
|
HoverCard,
|
||||||
HoverCardContent,
|
HoverCardContent,
|
||||||
@ -9,22 +8,28 @@ import { get, isEmpty, 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 { JsonSchemaDataType, VariableType } from '../../constant';
|
import { JsonSchemaDataType, VariableType } from '../../constant';
|
||||||
|
import { useGetStructuredOutputByValue } from '../../hooks/use-build-structured-output';
|
||||||
|
import {
|
||||||
|
hasJsonSchemaChild,
|
||||||
|
hasSpecificTypeChild,
|
||||||
|
} from '../../utils/filter-agent-structured-output';
|
||||||
|
|
||||||
type DataItem = { label: ReactNode; value: string; parentLabel?: ReactNode };
|
type DataItem = { label: ReactNode; value: string; parentLabel?: ReactNode };
|
||||||
|
|
||||||
type StructuredOutputSecondaryMenuProps = {
|
type StructuredOutputSecondaryMenuProps = {
|
||||||
data: DataItem;
|
data: DataItem;
|
||||||
click(option: { label: ReactNode; value: string }): void;
|
click(option: { label: ReactNode; value: string }): void;
|
||||||
filteredStructuredOutput: JSONSchema;
|
|
||||||
type?: VariableType | JsonSchemaDataType;
|
type?: VariableType | JsonSchemaDataType;
|
||||||
} & PropsWithChildren;
|
} & PropsWithChildren;
|
||||||
|
|
||||||
export function StructuredOutputSecondaryMenu({
|
export function StructuredOutputSecondaryMenu({
|
||||||
data,
|
data,
|
||||||
click,
|
click,
|
||||||
filteredStructuredOutput,
|
|
||||||
type,
|
type,
|
||||||
}: StructuredOutputSecondaryMenuProps) {
|
}: StructuredOutputSecondaryMenuProps) {
|
||||||
|
const filterStructuredOutput = useGetStructuredOutputByValue();
|
||||||
|
const structuredOutput = filterStructuredOutput(data.value);
|
||||||
|
|
||||||
const handleSubMenuClick = useCallback(
|
const handleSubMenuClick = useCallback(
|
||||||
(option: { label: ReactNode; value: string }, dataType?: string) => () => {
|
(option: { label: ReactNode; value: string }, dataType?: string) => () => {
|
||||||
// The query variable of the iteration operator can only select array type data.
|
// The query variable of the iteration operator can only select array type data.
|
||||||
@ -54,19 +59,28 @@ export function StructuredOutputSecondaryMenu({
|
|||||||
|
|
||||||
const dataType = get(value, 'type');
|
const dataType = get(value, 'type');
|
||||||
|
|
||||||
return (
|
if (
|
||||||
<li key={key} className="pl-1">
|
!type ||
|
||||||
<div
|
(type &&
|
||||||
onClick={handleSubMenuClick(nextOption, dataType)}
|
(dataType === type ||
|
||||||
className="hover:bg-bg-card p-1 text-text-primary rounded-sm flex justify-between"
|
hasSpecificTypeChild(value ?? {}, type)))
|
||||||
>
|
) {
|
||||||
{key}
|
return (
|
||||||
<span className="text-text-secondary">{dataType}</span>
|
<li key={key} className="pl-1">
|
||||||
</div>
|
<div
|
||||||
{dataType === JsonSchemaDataType.Object &&
|
onClick={handleSubMenuClick(nextOption, dataType)}
|
||||||
renderAgentStructuredOutput(value, nextOption)}
|
className="hover:bg-bg-card p-1 text-text-primary rounded-sm flex justify-between"
|
||||||
</li>
|
>
|
||||||
);
|
{key}
|
||||||
|
<span className="text-text-secondary">{dataType}</span>
|
||||||
|
</div>
|
||||||
|
{dataType === JsonSchemaDataType.Object &&
|
||||||
|
renderAgentStructuredOutput(value, nextOption)}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
})}
|
})}
|
||||||
</ul>
|
</ul>
|
||||||
);
|
);
|
||||||
@ -74,9 +88,13 @@ export function StructuredOutputSecondaryMenu({
|
|||||||
|
|
||||||
return <div></div>;
|
return <div></div>;
|
||||||
},
|
},
|
||||||
[handleSubMenuClick],
|
[handleSubMenuClick, type],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!hasJsonSchemaChild(structuredOutput)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<HoverCard key={data.value} openDelay={100} closeDelay={100}>
|
<HoverCard key={data.value} openDelay={100} closeDelay={100}>
|
||||||
<HoverCardTrigger asChild>
|
<HoverCardTrigger asChild>
|
||||||
@ -96,7 +114,7 @@ export function StructuredOutputSecondaryMenu({
|
|||||||
>
|
>
|
||||||
<section className="p-2">
|
<section className="p-2">
|
||||||
<div className="p-1">{data?.parentLabel} structured output:</div>
|
<div className="p-1">{data?.parentLabel} structured output:</div>
|
||||||
{renderAgentStructuredOutput(filteredStructuredOutput, data)}
|
{renderAgentStructuredOutput(structuredOutput, data)}
|
||||||
</section>
|
</section>
|
||||||
</HoverCardContent>
|
</HoverCardContent>
|
||||||
</HoverCard>
|
</HoverCard>
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import { get } from 'lodash';
|
|||||||
import { ReactNode, useCallback } from 'react';
|
import { ReactNode, useCallback } from 'react';
|
||||||
import { AgentStructuredOutputField, Operator } from '../constant';
|
import { AgentStructuredOutputField, Operator } from '../constant';
|
||||||
import useGraphStore from '../store';
|
import useGraphStore from '../store';
|
||||||
import { filterAgentStructuredOutput } from '../utils/filter-agent-structured-output';
|
|
||||||
|
|
||||||
function getNodeId(value: string) {
|
function getNodeId(value: string) {
|
||||||
return value.split('@').at(0);
|
return value.split('@').at(0);
|
||||||
@ -25,28 +24,23 @@ export function useShowSecondaryMenu() {
|
|||||||
return showSecondaryMenu;
|
return showSecondaryMenu;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useFilterStructuredOutputByValue() {
|
export function useGetStructuredOutputByValue() {
|
||||||
const { getNode } = useGraphStore((state) => state);
|
const { getNode } = useGraphStore((state) => state);
|
||||||
|
|
||||||
const filterStructuredOutput = useCallback(
|
const getStructuredOutput = useCallback(
|
||||||
(value: string, type?: string) => {
|
(value: string) => {
|
||||||
const node = getNode(getNodeId(value));
|
const node = getNode(getNodeId(value));
|
||||||
const structuredOutput = get(
|
const structuredOutput = get(
|
||||||
node,
|
node,
|
||||||
`data.form.outputs.${AgentStructuredOutputField}`,
|
`data.form.outputs.${AgentStructuredOutputField}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const filteredStructuredOutput = filterAgentStructuredOutput(
|
return structuredOutput;
|
||||||
structuredOutput,
|
|
||||||
type,
|
|
||||||
);
|
|
||||||
|
|
||||||
return filteredStructuredOutput;
|
|
||||||
},
|
},
|
||||||
[getNode],
|
[getNode],
|
||||||
);
|
);
|
||||||
|
|
||||||
return filterStructuredOutput;
|
return getStructuredOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useFindAgentStructuredOutputLabel() {
|
export function useFindAgentStructuredOutputLabel() {
|
||||||
|
|||||||
@ -2,64 +2,6 @@ import { JSONSchema } from '@/components/jsonjoy-builder';
|
|||||||
import { get, isPlainObject } from 'lodash';
|
import { get, isPlainObject } from 'lodash';
|
||||||
import { JsonSchemaDataType } from '../constant';
|
import { JsonSchemaDataType } from '../constant';
|
||||||
|
|
||||||
// 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,
|
|
||||||
type: string,
|
|
||||||
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 === type || hasArrayChild(value))
|
|
||||||
) {
|
|
||||||
pre[key] = filterLoopOperatorInput(value, type, path);
|
|
||||||
}
|
|
||||||
return pre;
|
|
||||||
},
|
|
||||||
{} as Record<string, JSONSchema>,
|
|
||||||
);
|
|
||||||
|
|
||||||
return { ...structuredOutput, properties };
|
|
||||||
}
|
|
||||||
|
|
||||||
return structuredOutput;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function filterAgentStructuredOutput(
|
|
||||||
structuredOutput: JSONSchema,
|
|
||||||
type?: string,
|
|
||||||
) {
|
|
||||||
if (typeof structuredOutput === 'boolean') {
|
|
||||||
return structuredOutput;
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
structuredOutput.properties &&
|
|
||||||
isPlainObject(structuredOutput.properties)
|
|
||||||
) {
|
|
||||||
if (type) {
|
|
||||||
return filterLoopOperatorInput(structuredOutput, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
return structuredOutput;
|
|
||||||
}
|
|
||||||
|
|
||||||
return structuredOutput;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function hasSpecificTypeChild(
|
export function hasSpecificTypeChild(
|
||||||
data: Record<string, any> | Array<any>,
|
data: Record<string, any> | Array<any>,
|
||||||
type: string,
|
type: string,
|
||||||
|
|||||||
Reference in New Issue
Block a user