Feat: The query variable of a loop operator can be a nested array variable. #10866 (#10921)

### 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:
balibabu
2025-11-03 09:40:47 +08:00
committed by GitHub
parent 33371cda11
commit 410c0a829d
5 changed files with 96 additions and 29 deletions

View File

@ -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;
}