mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-01-03 02:55:29 +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:
@ -2,6 +2,42 @@ import { JSONSchema } from '@/components/jsonjoy-builder';
|
||||
import { Operator } from '@/constants/agent';
|
||||
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(
|
||||
structuredOutput: JSONSchema,
|
||||
operator?: string,
|
||||
@ -13,32 +49,39 @@ export function filterAgentStructuredOutput(
|
||||
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) {
|
||||
return filterByPredicate(
|
||||
(value) => typeof value !== 'boolean' && value.type === 'array',
|
||||
);
|
||||
} else {
|
||||
return filterByPredicate(
|
||||
(value) => typeof value !== 'boolean' && value.type !== 'array',
|
||||
);
|
||||
return filterLoopOperatorInput(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